inode.c 97 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112
  1. /**
  2. * inode.c - NTFS kernel inode handling. Part of the Linux-NTFS project.
  3. *
  4. * Copyright (c) 2001-2007 Anton Altaparmakov
  5. *
  6. * This program/include file is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as published
  8. * by the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program/include file is distributed in the hope that it will be
  12. * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  13. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program (in the main directory of the Linux-NTFS
  18. * distribution in the file COPYING); if not, write to the Free Software
  19. * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/buffer_head.h>
  22. #include <linux/fs.h>
  23. #include <linux/mm.h>
  24. #include <linux/mount.h>
  25. #include <linux/mutex.h>
  26. #include <linux/pagemap.h>
  27. #include <linux/quotaops.h>
  28. #include <linux/slab.h>
  29. #include <linux/smp_lock.h>
  30. #include "aops.h"
  31. #include "attrib.h"
  32. #include "bitmap.h"
  33. #include "dir.h"
  34. #include "debug.h"
  35. #include "inode.h"
  36. #include "attrib.h"
  37. #include "lcnalloc.h"
  38. #include "malloc.h"
  39. #include "mft.h"
  40. #include "time.h"
  41. #include "ntfs.h"
  42. /**
  43. * ntfs_test_inode - compare two (possibly fake) inodes for equality
  44. * @vi: vfs inode which to test
  45. * @na: ntfs attribute which is being tested with
  46. *
  47. * Compare the ntfs attribute embedded in the ntfs specific part of the vfs
  48. * inode @vi for equality with the ntfs attribute @na.
  49. *
  50. * If searching for the normal file/directory inode, set @na->type to AT_UNUSED.
  51. * @na->name and @na->name_len are then ignored.
  52. *
  53. * Return 1 if the attributes match and 0 if not.
  54. *
  55. * NOTE: This function runs with the inode_lock spin lock held so it is not
  56. * allowed to sleep.
  57. */
  58. int ntfs_test_inode(struct inode *vi, ntfs_attr *na)
  59. {
  60. ntfs_inode *ni;
  61. if (vi->i_ino != na->mft_no)
  62. return 0;
  63. ni = NTFS_I(vi);
  64. /* If !NInoAttr(ni), @vi is a normal file or directory inode. */
  65. if (likely(!NInoAttr(ni))) {
  66. /* If not looking for a normal inode this is a mismatch. */
  67. if (unlikely(na->type != AT_UNUSED))
  68. return 0;
  69. } else {
  70. /* A fake inode describing an attribute. */
  71. if (ni->type != na->type)
  72. return 0;
  73. if (ni->name_len != na->name_len)
  74. return 0;
  75. if (na->name_len && memcmp(ni->name, na->name,
  76. na->name_len * sizeof(ntfschar)))
  77. return 0;
  78. }
  79. /* Match! */
  80. return 1;
  81. }
  82. /**
  83. * ntfs_init_locked_inode - initialize an inode
  84. * @vi: vfs inode to initialize
  85. * @na: ntfs attribute which to initialize @vi to
  86. *
  87. * Initialize the vfs inode @vi with the values from the ntfs attribute @na in
  88. * order to enable ntfs_test_inode() to do its work.
  89. *
  90. * If initializing the normal file/directory inode, set @na->type to AT_UNUSED.
  91. * In that case, @na->name and @na->name_len should be set to NULL and 0,
  92. * respectively. Although that is not strictly necessary as
  93. * ntfs_read_locked_inode() will fill them in later.
  94. *
  95. * Return 0 on success and -errno on error.
  96. *
  97. * NOTE: This function runs with the inode_lock spin lock held so it is not
  98. * allowed to sleep. (Hence the GFP_ATOMIC allocation.)
  99. */
  100. static int ntfs_init_locked_inode(struct inode *vi, ntfs_attr *na)
  101. {
  102. ntfs_inode *ni = NTFS_I(vi);
  103. vi->i_ino = na->mft_no;
  104. ni->type = na->type;
  105. if (na->type == AT_INDEX_ALLOCATION)
  106. NInoSetMstProtected(ni);
  107. ni->name = na->name;
  108. ni->name_len = na->name_len;
  109. /* If initializing a normal inode, we are done. */
  110. if (likely(na->type == AT_UNUSED)) {
  111. BUG_ON(na->name);
  112. BUG_ON(na->name_len);
  113. return 0;
  114. }
  115. /* It is a fake inode. */
  116. NInoSetAttr(ni);
  117. /*
  118. * We have I30 global constant as an optimization as it is the name
  119. * in >99.9% of named attributes! The other <0.1% incur a GFP_ATOMIC
  120. * allocation but that is ok. And most attributes are unnamed anyway,
  121. * thus the fraction of named attributes with name != I30 is actually
  122. * absolutely tiny.
  123. */
  124. if (na->name_len && na->name != I30) {
  125. unsigned int i;
  126. BUG_ON(!na->name);
  127. i = na->name_len * sizeof(ntfschar);
  128. ni->name = kmalloc(i + sizeof(ntfschar), GFP_ATOMIC);
  129. if (!ni->name)
  130. return -ENOMEM;
  131. memcpy(ni->name, na->name, i);
  132. ni->name[na->name_len] = 0;
  133. }
  134. return 0;
  135. }
  136. typedef int (*set_t)(struct inode *, void *);
  137. static int ntfs_read_locked_inode(struct inode *vi);
  138. static int ntfs_read_locked_attr_inode(struct inode *base_vi, struct inode *vi);
  139. static int ntfs_read_locked_index_inode(struct inode *base_vi,
  140. struct inode *vi);
  141. /**
  142. * ntfs_iget - obtain a struct inode corresponding to a specific normal inode
  143. * @sb: super block of mounted volume
  144. * @mft_no: mft record number / inode number to obtain
  145. *
  146. * Obtain the struct inode corresponding to a specific normal inode (i.e. a
  147. * file or directory).
  148. *
  149. * If the inode is in the cache, it is just returned with an increased
  150. * reference count. Otherwise, a new struct inode is allocated and initialized,
  151. * and finally ntfs_read_locked_inode() is called to read in the inode and
  152. * fill in the remainder of the inode structure.
  153. *
  154. * Return the struct inode on success. Check the return value with IS_ERR() and
  155. * if true, the function failed and the error code is obtained from PTR_ERR().
  156. */
  157. struct inode *ntfs_iget(struct super_block *sb, unsigned long mft_no)
  158. {
  159. struct inode *vi;
  160. int err;
  161. ntfs_attr na;
  162. na.mft_no = mft_no;
  163. na.type = AT_UNUSED;
  164. na.name = NULL;
  165. na.name_len = 0;
  166. vi = iget5_locked(sb, mft_no, (test_t)ntfs_test_inode,
  167. (set_t)ntfs_init_locked_inode, &na);
  168. if (unlikely(!vi))
  169. return ERR_PTR(-ENOMEM);
  170. err = 0;
  171. /* If this is a freshly allocated inode, need to read it now. */
  172. if (vi->i_state & I_NEW) {
  173. err = ntfs_read_locked_inode(vi);
  174. unlock_new_inode(vi);
  175. }
  176. /*
  177. * There is no point in keeping bad inodes around if the failure was
  178. * due to ENOMEM. We want to be able to retry again later.
  179. */
  180. if (unlikely(err == -ENOMEM)) {
  181. iput(vi);
  182. vi = ERR_PTR(err);
  183. }
  184. return vi;
  185. }
  186. /**
  187. * ntfs_attr_iget - obtain a struct inode corresponding to an attribute
  188. * @base_vi: vfs base inode containing the attribute
  189. * @type: attribute type
  190. * @name: Unicode name of the attribute (NULL if unnamed)
  191. * @name_len: length of @name in Unicode characters (0 if unnamed)
  192. *
  193. * Obtain the (fake) struct inode corresponding to the attribute specified by
  194. * @type, @name, and @name_len, which is present in the base mft record
  195. * specified by the vfs inode @base_vi.
  196. *
  197. * If the attribute inode is in the cache, it is just returned with an
  198. * increased reference count. Otherwise, a new struct inode is allocated and
  199. * initialized, and finally ntfs_read_locked_attr_inode() is called to read the
  200. * attribute and fill in the inode structure.
  201. *
  202. * Note, for index allocation attributes, you need to use ntfs_index_iget()
  203. * instead of ntfs_attr_iget() as working with indices is a lot more complex.
  204. *
  205. * Return the struct inode of the attribute inode on success. Check the return
  206. * value with IS_ERR() and if true, the function failed and the error code is
  207. * obtained from PTR_ERR().
  208. */
  209. struct inode *ntfs_attr_iget(struct inode *base_vi, ATTR_TYPE type,
  210. ntfschar *name, u32 name_len)
  211. {
  212. struct inode *vi;
  213. int err;
  214. ntfs_attr na;
  215. /* Make sure no one calls ntfs_attr_iget() for indices. */
  216. BUG_ON(type == AT_INDEX_ALLOCATION);
  217. na.mft_no = base_vi->i_ino;
  218. na.type = type;
  219. na.name = name;
  220. na.name_len = name_len;
  221. vi = iget5_locked(base_vi->i_sb, na.mft_no, (test_t)ntfs_test_inode,
  222. (set_t)ntfs_init_locked_inode, &na);
  223. if (unlikely(!vi))
  224. return ERR_PTR(-ENOMEM);
  225. err = 0;
  226. /* If this is a freshly allocated inode, need to read it now. */
  227. if (vi->i_state & I_NEW) {
  228. err = ntfs_read_locked_attr_inode(base_vi, vi);
  229. unlock_new_inode(vi);
  230. }
  231. /*
  232. * There is no point in keeping bad attribute inodes around. This also
  233. * simplifies things in that we never need to check for bad attribute
  234. * inodes elsewhere.
  235. */
  236. if (unlikely(err)) {
  237. iput(vi);
  238. vi = ERR_PTR(err);
  239. }
  240. return vi;
  241. }
  242. /**
  243. * ntfs_index_iget - obtain a struct inode corresponding to an index
  244. * @base_vi: vfs base inode containing the index related attributes
  245. * @name: Unicode name of the index
  246. * @name_len: length of @name in Unicode characters
  247. *
  248. * Obtain the (fake) struct inode corresponding to the index specified by @name
  249. * and @name_len, which is present in the base mft record specified by the vfs
  250. * inode @base_vi.
  251. *
  252. * If the index inode is in the cache, it is just returned with an increased
  253. * reference count. Otherwise, a new struct inode is allocated and
  254. * initialized, and finally ntfs_read_locked_index_inode() is called to read
  255. * the index related attributes and fill in the inode structure.
  256. *
  257. * Return the struct inode of the index inode on success. Check the return
  258. * value with IS_ERR() and if true, the function failed and the error code is
  259. * obtained from PTR_ERR().
  260. */
  261. struct inode *ntfs_index_iget(struct inode *base_vi, ntfschar *name,
  262. u32 name_len)
  263. {
  264. struct inode *vi;
  265. int err;
  266. ntfs_attr na;
  267. na.mft_no = base_vi->i_ino;
  268. na.type = AT_INDEX_ALLOCATION;
  269. na.name = name;
  270. na.name_len = name_len;
  271. vi = iget5_locked(base_vi->i_sb, na.mft_no, (test_t)ntfs_test_inode,
  272. (set_t)ntfs_init_locked_inode, &na);
  273. if (unlikely(!vi))
  274. return ERR_PTR(-ENOMEM);
  275. err = 0;
  276. /* If this is a freshly allocated inode, need to read it now. */
  277. if (vi->i_state & I_NEW) {
  278. err = ntfs_read_locked_index_inode(base_vi, vi);
  279. unlock_new_inode(vi);
  280. }
  281. /*
  282. * There is no point in keeping bad index inodes around. This also
  283. * simplifies things in that we never need to check for bad index
  284. * inodes elsewhere.
  285. */
  286. if (unlikely(err)) {
  287. iput(vi);
  288. vi = ERR_PTR(err);
  289. }
  290. return vi;
  291. }
  292. struct inode *ntfs_alloc_big_inode(struct super_block *sb)
  293. {
  294. ntfs_inode *ni;
  295. ntfs_debug("Entering.");
  296. ni = kmem_cache_alloc(ntfs_big_inode_cache, GFP_NOFS);
  297. if (likely(ni != NULL)) {
  298. ni->state = 0;
  299. return VFS_I(ni);
  300. }
  301. ntfs_error(sb, "Allocation of NTFS big inode structure failed.");
  302. return NULL;
  303. }
  304. void ntfs_destroy_big_inode(struct inode *inode)
  305. {
  306. ntfs_inode *ni = NTFS_I(inode);
  307. ntfs_debug("Entering.");
  308. BUG_ON(ni->page);
  309. if (!atomic_dec_and_test(&ni->count))
  310. BUG();
  311. kmem_cache_free(ntfs_big_inode_cache, NTFS_I(inode));
  312. }
  313. static inline ntfs_inode *ntfs_alloc_extent_inode(void)
  314. {
  315. ntfs_inode *ni;
  316. ntfs_debug("Entering.");
  317. ni = kmem_cache_alloc(ntfs_inode_cache, GFP_NOFS);
  318. if (likely(ni != NULL)) {
  319. ni->state = 0;
  320. return ni;
  321. }
  322. ntfs_error(NULL, "Allocation of NTFS inode structure failed.");
  323. return NULL;
  324. }
  325. static void ntfs_destroy_extent_inode(ntfs_inode *ni)
  326. {
  327. ntfs_debug("Entering.");
  328. BUG_ON(ni->page);
  329. if (!atomic_dec_and_test(&ni->count))
  330. BUG();
  331. kmem_cache_free(ntfs_inode_cache, ni);
  332. }
  333. /*
  334. * The attribute runlist lock has separate locking rules from the
  335. * normal runlist lock, so split the two lock-classes:
  336. */
  337. static struct lock_class_key attr_list_rl_lock_class;
  338. /**
  339. * __ntfs_init_inode - initialize ntfs specific part of an inode
  340. * @sb: super block of mounted volume
  341. * @ni: freshly allocated ntfs inode which to initialize
  342. *
  343. * Initialize an ntfs inode to defaults.
  344. *
  345. * NOTE: ni->mft_no, ni->state, ni->type, ni->name, and ni->name_len are left
  346. * untouched. Make sure to initialize them elsewhere.
  347. *
  348. * Return zero on success and -ENOMEM on error.
  349. */
  350. void __ntfs_init_inode(struct super_block *sb, ntfs_inode *ni)
  351. {
  352. ntfs_debug("Entering.");
  353. rwlock_init(&ni->size_lock);
  354. ni->initialized_size = ni->allocated_size = 0;
  355. ni->seq_no = 0;
  356. atomic_set(&ni->count, 1);
  357. ni->vol = NTFS_SB(sb);
  358. ntfs_init_runlist(&ni->runlist);
  359. mutex_init(&ni->mrec_lock);
  360. ni->page = NULL;
  361. ni->page_ofs = 0;
  362. ni->attr_list_size = 0;
  363. ni->attr_list = NULL;
  364. ntfs_init_runlist(&ni->attr_list_rl);
  365. lockdep_set_class(&ni->attr_list_rl.lock,
  366. &attr_list_rl_lock_class);
  367. ni->itype.index.block_size = 0;
  368. ni->itype.index.vcn_size = 0;
  369. ni->itype.index.collation_rule = 0;
  370. ni->itype.index.block_size_bits = 0;
  371. ni->itype.index.vcn_size_bits = 0;
  372. mutex_init(&ni->extent_lock);
  373. ni->nr_extents = 0;
  374. ni->ext.base_ntfs_ino = NULL;
  375. }
  376. /*
  377. * Extent inodes get MFT-mapped in a nested way, while the base inode
  378. * is still mapped. Teach this nesting to the lock validator by creating
  379. * a separate class for nested inode's mrec_lock's:
  380. */
  381. static struct lock_class_key extent_inode_mrec_lock_key;
  382. inline ntfs_inode *ntfs_new_extent_inode(struct super_block *sb,
  383. unsigned long mft_no)
  384. {
  385. ntfs_inode *ni = ntfs_alloc_extent_inode();
  386. ntfs_debug("Entering.");
  387. if (likely(ni != NULL)) {
  388. __ntfs_init_inode(sb, ni);
  389. lockdep_set_class(&ni->mrec_lock, &extent_inode_mrec_lock_key);
  390. ni->mft_no = mft_no;
  391. ni->type = AT_UNUSED;
  392. ni->name = NULL;
  393. ni->name_len = 0;
  394. }
  395. return ni;
  396. }
  397. /**
  398. * ntfs_is_extended_system_file - check if a file is in the $Extend directory
  399. * @ctx: initialized attribute search context
  400. *
  401. * Search all file name attributes in the inode described by the attribute
  402. * search context @ctx and check if any of the names are in the $Extend system
  403. * directory.
  404. *
  405. * Return values:
  406. * 1: file is in $Extend directory
  407. * 0: file is not in $Extend directory
  408. * -errno: failed to determine if the file is in the $Extend directory
  409. */
  410. static int ntfs_is_extended_system_file(ntfs_attr_search_ctx *ctx)
  411. {
  412. int nr_links, err;
  413. /* Restart search. */
  414. ntfs_attr_reinit_search_ctx(ctx);
  415. /* Get number of hard links. */
  416. nr_links = le16_to_cpu(ctx->mrec->link_count);
  417. /* Loop through all hard links. */
  418. while (!(err = ntfs_attr_lookup(AT_FILE_NAME, NULL, 0, 0, 0, NULL, 0,
  419. ctx))) {
  420. FILE_NAME_ATTR *file_name_attr;
  421. ATTR_RECORD *attr = ctx->attr;
  422. u8 *p, *p2;
  423. nr_links--;
  424. /*
  425. * Maximum sanity checking as we are called on an inode that
  426. * we suspect might be corrupt.
  427. */
  428. p = (u8*)attr + le32_to_cpu(attr->length);
  429. if (p < (u8*)ctx->mrec || (u8*)p > (u8*)ctx->mrec +
  430. le32_to_cpu(ctx->mrec->bytes_in_use)) {
  431. err_corrupt_attr:
  432. ntfs_error(ctx->ntfs_ino->vol->sb, "Corrupt file name "
  433. "attribute. You should run chkdsk.");
  434. return -EIO;
  435. }
  436. if (attr->non_resident) {
  437. ntfs_error(ctx->ntfs_ino->vol->sb, "Non-resident file "
  438. "name. You should run chkdsk.");
  439. return -EIO;
  440. }
  441. if (attr->flags) {
  442. ntfs_error(ctx->ntfs_ino->vol->sb, "File name with "
  443. "invalid flags. You should run "
  444. "chkdsk.");
  445. return -EIO;
  446. }
  447. if (!(attr->data.resident.flags & RESIDENT_ATTR_IS_INDEXED)) {
  448. ntfs_error(ctx->ntfs_ino->vol->sb, "Unindexed file "
  449. "name. You should run chkdsk.");
  450. return -EIO;
  451. }
  452. file_name_attr = (FILE_NAME_ATTR*)((u8*)attr +
  453. le16_to_cpu(attr->data.resident.value_offset));
  454. p2 = (u8*)attr + le32_to_cpu(attr->data.resident.value_length);
  455. if (p2 < (u8*)attr || p2 > p)
  456. goto err_corrupt_attr;
  457. /* This attribute is ok, but is it in the $Extend directory? */
  458. if (MREF_LE(file_name_attr->parent_directory) == FILE_Extend)
  459. return 1; /* YES, it's an extended system file. */
  460. }
  461. if (unlikely(err != -ENOENT))
  462. return err;
  463. if (unlikely(nr_links)) {
  464. ntfs_error(ctx->ntfs_ino->vol->sb, "Inode hard link count "
  465. "doesn't match number of name attributes. You "
  466. "should run chkdsk.");
  467. return -EIO;
  468. }
  469. return 0; /* NO, it is not an extended system file. */
  470. }
  471. /**
  472. * ntfs_read_locked_inode - read an inode from its device
  473. * @vi: inode to read
  474. *
  475. * ntfs_read_locked_inode() is called from ntfs_iget() to read the inode
  476. * described by @vi into memory from the device.
  477. *
  478. * The only fields in @vi that we need to/can look at when the function is
  479. * called are i_sb, pointing to the mounted device's super block, and i_ino,
  480. * the number of the inode to load.
  481. *
  482. * ntfs_read_locked_inode() maps, pins and locks the mft record number i_ino
  483. * for reading and sets up the necessary @vi fields as well as initializing
  484. * the ntfs inode.
  485. *
  486. * Q: What locks are held when the function is called?
  487. * A: i_state has I_LOCK set, hence the inode is locked, also
  488. * i_count is set to 1, so it is not going to go away
  489. * i_flags is set to 0 and we have no business touching it. Only an ioctl()
  490. * is allowed to write to them. We should of course be honouring them but
  491. * we need to do that using the IS_* macros defined in include/linux/fs.h.
  492. * In any case ntfs_read_locked_inode() has nothing to do with i_flags.
  493. *
  494. * Return 0 on success and -errno on error. In the error case, the inode will
  495. * have had make_bad_inode() executed on it.
  496. */
  497. static int ntfs_read_locked_inode(struct inode *vi)
  498. {
  499. ntfs_volume *vol = NTFS_SB(vi->i_sb);
  500. ntfs_inode *ni;
  501. struct inode *bvi;
  502. MFT_RECORD *m;
  503. ATTR_RECORD *a;
  504. STANDARD_INFORMATION *si;
  505. ntfs_attr_search_ctx *ctx;
  506. int err = 0;
  507. ntfs_debug("Entering for i_ino 0x%lx.", vi->i_ino);
  508. /* Setup the generic vfs inode parts now. */
  509. /*
  510. * This is for checking whether an inode has changed w.r.t. a file so
  511. * that the file can be updated if necessary (compare with f_version).
  512. */
  513. vi->i_version = 1;
  514. vi->i_uid = vol->uid;
  515. vi->i_gid = vol->gid;
  516. vi->i_mode = 0;
  517. /*
  518. * Initialize the ntfs specific part of @vi special casing
  519. * FILE_MFT which we need to do at mount time.
  520. */
  521. if (vi->i_ino != FILE_MFT)
  522. ntfs_init_big_inode(vi);
  523. ni = NTFS_I(vi);
  524. m = map_mft_record(ni);
  525. if (IS_ERR(m)) {
  526. err = PTR_ERR(m);
  527. goto err_out;
  528. }
  529. ctx = ntfs_attr_get_search_ctx(ni, m);
  530. if (!ctx) {
  531. err = -ENOMEM;
  532. goto unm_err_out;
  533. }
  534. if (!(m->flags & MFT_RECORD_IN_USE)) {
  535. ntfs_error(vi->i_sb, "Inode is not in use!");
  536. goto unm_err_out;
  537. }
  538. if (m->base_mft_record) {
  539. ntfs_error(vi->i_sb, "Inode is an extent inode!");
  540. goto unm_err_out;
  541. }
  542. /* Transfer information from mft record into vfs and ntfs inodes. */
  543. vi->i_generation = ni->seq_no = le16_to_cpu(m->sequence_number);
  544. /*
  545. * FIXME: Keep in mind that link_count is two for files which have both
  546. * a long file name and a short file name as separate entries, so if
  547. * we are hiding short file names this will be too high. Either we need
  548. * to account for the short file names by subtracting them or we need
  549. * to make sure we delete files even though i_nlink is not zero which
  550. * might be tricky due to vfs interactions. Need to think about this
  551. * some more when implementing the unlink command.
  552. */
  553. vi->i_nlink = le16_to_cpu(m->link_count);
  554. /*
  555. * FIXME: Reparse points can have the directory bit set even though
  556. * they would be S_IFLNK. Need to deal with this further below when we
  557. * implement reparse points / symbolic links but it will do for now.
  558. * Also if not a directory, it could be something else, rather than
  559. * a regular file. But again, will do for now.
  560. */
  561. /* Everyone gets all permissions. */
  562. vi->i_mode |= S_IRWXUGO;
  563. /* If read-only, noone gets write permissions. */
  564. if (IS_RDONLY(vi))
  565. vi->i_mode &= ~S_IWUGO;
  566. if (m->flags & MFT_RECORD_IS_DIRECTORY) {
  567. vi->i_mode |= S_IFDIR;
  568. /*
  569. * Apply the directory permissions mask set in the mount
  570. * options.
  571. */
  572. vi->i_mode &= ~vol->dmask;
  573. /* Things break without this kludge! */
  574. if (vi->i_nlink > 1)
  575. vi->i_nlink = 1;
  576. } else {
  577. vi->i_mode |= S_IFREG;
  578. /* Apply the file permissions mask set in the mount options. */
  579. vi->i_mode &= ~vol->fmask;
  580. }
  581. /*
  582. * Find the standard information attribute in the mft record. At this
  583. * stage we haven't setup the attribute list stuff yet, so this could
  584. * in fact fail if the standard information is in an extent record, but
  585. * I don't think this actually ever happens.
  586. */
  587. err = ntfs_attr_lookup(AT_STANDARD_INFORMATION, NULL, 0, 0, 0, NULL, 0,
  588. ctx);
  589. if (unlikely(err)) {
  590. if (err == -ENOENT) {
  591. /*
  592. * TODO: We should be performing a hot fix here (if the
  593. * recover mount option is set) by creating a new
  594. * attribute.
  595. */
  596. ntfs_error(vi->i_sb, "$STANDARD_INFORMATION attribute "
  597. "is missing.");
  598. }
  599. goto unm_err_out;
  600. }
  601. a = ctx->attr;
  602. /* Get the standard information attribute value. */
  603. si = (STANDARD_INFORMATION*)((u8*)a +
  604. le16_to_cpu(a->data.resident.value_offset));
  605. /* Transfer information from the standard information into vi. */
  606. /*
  607. * Note: The i_?times do not quite map perfectly onto the NTFS times,
  608. * but they are close enough, and in the end it doesn't really matter
  609. * that much...
  610. */
  611. /*
  612. * mtime is the last change of the data within the file. Not changed
  613. * when only metadata is changed, e.g. a rename doesn't affect mtime.
  614. */
  615. vi->i_mtime = ntfs2utc(si->last_data_change_time);
  616. /*
  617. * ctime is the last change of the metadata of the file. This obviously
  618. * always changes, when mtime is changed. ctime can be changed on its
  619. * own, mtime is then not changed, e.g. when a file is renamed.
  620. */
  621. vi->i_ctime = ntfs2utc(si->last_mft_change_time);
  622. /*
  623. * Last access to the data within the file. Not changed during a rename
  624. * for example but changed whenever the file is written to.
  625. */
  626. vi->i_atime = ntfs2utc(si->last_access_time);
  627. /* Find the attribute list attribute if present. */
  628. ntfs_attr_reinit_search_ctx(ctx);
  629. err = ntfs_attr_lookup(AT_ATTRIBUTE_LIST, NULL, 0, 0, 0, NULL, 0, ctx);
  630. if (err) {
  631. if (unlikely(err != -ENOENT)) {
  632. ntfs_error(vi->i_sb, "Failed to lookup attribute list "
  633. "attribute.");
  634. goto unm_err_out;
  635. }
  636. } else /* if (!err) */ {
  637. if (vi->i_ino == FILE_MFT)
  638. goto skip_attr_list_load;
  639. ntfs_debug("Attribute list found in inode 0x%lx.", vi->i_ino);
  640. NInoSetAttrList(ni);
  641. a = ctx->attr;
  642. if (a->flags & ATTR_COMPRESSION_MASK) {
  643. ntfs_error(vi->i_sb, "Attribute list attribute is "
  644. "compressed.");
  645. goto unm_err_out;
  646. }
  647. if (a->flags & ATTR_IS_ENCRYPTED ||
  648. a->flags & ATTR_IS_SPARSE) {
  649. if (a->non_resident) {
  650. ntfs_error(vi->i_sb, "Non-resident attribute "
  651. "list attribute is encrypted/"
  652. "sparse.");
  653. goto unm_err_out;
  654. }
  655. ntfs_warning(vi->i_sb, "Resident attribute list "
  656. "attribute in inode 0x%lx is marked "
  657. "encrypted/sparse which is not true. "
  658. "However, Windows allows this and "
  659. "chkdsk does not detect or correct it "
  660. "so we will just ignore the invalid "
  661. "flags and pretend they are not set.",
  662. vi->i_ino);
  663. }
  664. /* Now allocate memory for the attribute list. */
  665. ni->attr_list_size = (u32)ntfs_attr_size(a);
  666. ni->attr_list = ntfs_malloc_nofs(ni->attr_list_size);
  667. if (!ni->attr_list) {
  668. ntfs_error(vi->i_sb, "Not enough memory to allocate "
  669. "buffer for attribute list.");
  670. err = -ENOMEM;
  671. goto unm_err_out;
  672. }
  673. if (a->non_resident) {
  674. NInoSetAttrListNonResident(ni);
  675. if (a->data.non_resident.lowest_vcn) {
  676. ntfs_error(vi->i_sb, "Attribute list has non "
  677. "zero lowest_vcn.");
  678. goto unm_err_out;
  679. }
  680. /*
  681. * Setup the runlist. No need for locking as we have
  682. * exclusive access to the inode at this time.
  683. */
  684. ni->attr_list_rl.rl = ntfs_mapping_pairs_decompress(vol,
  685. a, NULL);
  686. if (IS_ERR(ni->attr_list_rl.rl)) {
  687. err = PTR_ERR(ni->attr_list_rl.rl);
  688. ni->attr_list_rl.rl = NULL;
  689. ntfs_error(vi->i_sb, "Mapping pairs "
  690. "decompression failed.");
  691. goto unm_err_out;
  692. }
  693. /* Now load the attribute list. */
  694. if ((err = load_attribute_list(vol, &ni->attr_list_rl,
  695. ni->attr_list, ni->attr_list_size,
  696. sle64_to_cpu(a->data.non_resident.
  697. initialized_size)))) {
  698. ntfs_error(vi->i_sb, "Failed to load "
  699. "attribute list attribute.");
  700. goto unm_err_out;
  701. }
  702. } else /* if (!a->non_resident) */ {
  703. if ((u8*)a + le16_to_cpu(a->data.resident.value_offset)
  704. + le32_to_cpu(
  705. a->data.resident.value_length) >
  706. (u8*)ctx->mrec + vol->mft_record_size) {
  707. ntfs_error(vi->i_sb, "Corrupt attribute list "
  708. "in inode.");
  709. goto unm_err_out;
  710. }
  711. /* Now copy the attribute list. */
  712. memcpy(ni->attr_list, (u8*)a + le16_to_cpu(
  713. a->data.resident.value_offset),
  714. le32_to_cpu(
  715. a->data.resident.value_length));
  716. }
  717. }
  718. skip_attr_list_load:
  719. /*
  720. * If an attribute list is present we now have the attribute list value
  721. * in ntfs_ino->attr_list and it is ntfs_ino->attr_list_size bytes.
  722. */
  723. if (S_ISDIR(vi->i_mode)) {
  724. loff_t bvi_size;
  725. ntfs_inode *bni;
  726. INDEX_ROOT *ir;
  727. u8 *ir_end, *index_end;
  728. /* It is a directory, find index root attribute. */
  729. ntfs_attr_reinit_search_ctx(ctx);
  730. err = ntfs_attr_lookup(AT_INDEX_ROOT, I30, 4, CASE_SENSITIVE,
  731. 0, NULL, 0, ctx);
  732. if (unlikely(err)) {
  733. if (err == -ENOENT) {
  734. // FIXME: File is corrupt! Hot-fix with empty
  735. // index root attribute if recovery option is
  736. // set.
  737. ntfs_error(vi->i_sb, "$INDEX_ROOT attribute "
  738. "is missing.");
  739. }
  740. goto unm_err_out;
  741. }
  742. a = ctx->attr;
  743. /* Set up the state. */
  744. if (unlikely(a->non_resident)) {
  745. ntfs_error(vol->sb, "$INDEX_ROOT attribute is not "
  746. "resident.");
  747. goto unm_err_out;
  748. }
  749. /* Ensure the attribute name is placed before the value. */
  750. if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >=
  751. le16_to_cpu(a->data.resident.value_offset)))) {
  752. ntfs_error(vol->sb, "$INDEX_ROOT attribute name is "
  753. "placed after the attribute value.");
  754. goto unm_err_out;
  755. }
  756. /*
  757. * Compressed/encrypted index root just means that the newly
  758. * created files in that directory should be created compressed/
  759. * encrypted. However index root cannot be both compressed and
  760. * encrypted.
  761. */
  762. if (a->flags & ATTR_COMPRESSION_MASK)
  763. NInoSetCompressed(ni);
  764. if (a->flags & ATTR_IS_ENCRYPTED) {
  765. if (a->flags & ATTR_COMPRESSION_MASK) {
  766. ntfs_error(vi->i_sb, "Found encrypted and "
  767. "compressed attribute.");
  768. goto unm_err_out;
  769. }
  770. NInoSetEncrypted(ni);
  771. }
  772. if (a->flags & ATTR_IS_SPARSE)
  773. NInoSetSparse(ni);
  774. ir = (INDEX_ROOT*)((u8*)a +
  775. le16_to_cpu(a->data.resident.value_offset));
  776. ir_end = (u8*)ir + le32_to_cpu(a->data.resident.value_length);
  777. if (ir_end > (u8*)ctx->mrec + vol->mft_record_size) {
  778. ntfs_error(vi->i_sb, "$INDEX_ROOT attribute is "
  779. "corrupt.");
  780. goto unm_err_out;
  781. }
  782. index_end = (u8*)&ir->index +
  783. le32_to_cpu(ir->index.index_length);
  784. if (index_end > ir_end) {
  785. ntfs_error(vi->i_sb, "Directory index is corrupt.");
  786. goto unm_err_out;
  787. }
  788. if (ir->type != AT_FILE_NAME) {
  789. ntfs_error(vi->i_sb, "Indexed attribute is not "
  790. "$FILE_NAME.");
  791. goto unm_err_out;
  792. }
  793. if (ir->collation_rule != COLLATION_FILE_NAME) {
  794. ntfs_error(vi->i_sb, "Index collation rule is not "
  795. "COLLATION_FILE_NAME.");
  796. goto unm_err_out;
  797. }
  798. ni->itype.index.collation_rule = ir->collation_rule;
  799. ni->itype.index.block_size = le32_to_cpu(ir->index_block_size);
  800. if (ni->itype.index.block_size &
  801. (ni->itype.index.block_size - 1)) {
  802. ntfs_error(vi->i_sb, "Index block size (%u) is not a "
  803. "power of two.",
  804. ni->itype.index.block_size);
  805. goto unm_err_out;
  806. }
  807. if (ni->itype.index.block_size > PAGE_CACHE_SIZE) {
  808. ntfs_error(vi->i_sb, "Index block size (%u) > "
  809. "PAGE_CACHE_SIZE (%ld) is not "
  810. "supported. Sorry.",
  811. ni->itype.index.block_size,
  812. PAGE_CACHE_SIZE);
  813. err = -EOPNOTSUPP;
  814. goto unm_err_out;
  815. }
  816. if (ni->itype.index.block_size < NTFS_BLOCK_SIZE) {
  817. ntfs_error(vi->i_sb, "Index block size (%u) < "
  818. "NTFS_BLOCK_SIZE (%i) is not "
  819. "supported. Sorry.",
  820. ni->itype.index.block_size,
  821. NTFS_BLOCK_SIZE);
  822. err = -EOPNOTSUPP;
  823. goto unm_err_out;
  824. }
  825. ni->itype.index.block_size_bits =
  826. ffs(ni->itype.index.block_size) - 1;
  827. /* Determine the size of a vcn in the directory index. */
  828. if (vol->cluster_size <= ni->itype.index.block_size) {
  829. ni->itype.index.vcn_size = vol->cluster_size;
  830. ni->itype.index.vcn_size_bits = vol->cluster_size_bits;
  831. } else {
  832. ni->itype.index.vcn_size = vol->sector_size;
  833. ni->itype.index.vcn_size_bits = vol->sector_size_bits;
  834. }
  835. /* Setup the index allocation attribute, even if not present. */
  836. NInoSetMstProtected(ni);
  837. ni->type = AT_INDEX_ALLOCATION;
  838. ni->name = I30;
  839. ni->name_len = 4;
  840. if (!(ir->index.flags & LARGE_INDEX)) {
  841. /* No index allocation. */
  842. vi->i_size = ni->initialized_size =
  843. ni->allocated_size = 0;
  844. /* We are done with the mft record, so we release it. */
  845. ntfs_attr_put_search_ctx(ctx);
  846. unmap_mft_record(ni);
  847. m = NULL;
  848. ctx = NULL;
  849. goto skip_large_dir_stuff;
  850. } /* LARGE_INDEX: Index allocation present. Setup state. */
  851. NInoSetIndexAllocPresent(ni);
  852. /* Find index allocation attribute. */
  853. ntfs_attr_reinit_search_ctx(ctx);
  854. err = ntfs_attr_lookup(AT_INDEX_ALLOCATION, I30, 4,
  855. CASE_SENSITIVE, 0, NULL, 0, ctx);
  856. if (unlikely(err)) {
  857. if (err == -ENOENT)
  858. ntfs_error(vi->i_sb, "$INDEX_ALLOCATION "
  859. "attribute is not present but "
  860. "$INDEX_ROOT indicated it is.");
  861. else
  862. ntfs_error(vi->i_sb, "Failed to lookup "
  863. "$INDEX_ALLOCATION "
  864. "attribute.");
  865. goto unm_err_out;
  866. }
  867. a = ctx->attr;
  868. if (!a->non_resident) {
  869. ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute "
  870. "is resident.");
  871. goto unm_err_out;
  872. }
  873. /*
  874. * Ensure the attribute name is placed before the mapping pairs
  875. * array.
  876. */
  877. if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >=
  878. le16_to_cpu(
  879. a->data.non_resident.mapping_pairs_offset)))) {
  880. ntfs_error(vol->sb, "$INDEX_ALLOCATION attribute name "
  881. "is placed after the mapping pairs "
  882. "array.");
  883. goto unm_err_out;
  884. }
  885. if (a->flags & ATTR_IS_ENCRYPTED) {
  886. ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute "
  887. "is encrypted.");
  888. goto unm_err_out;
  889. }
  890. if (a->flags & ATTR_IS_SPARSE) {
  891. ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute "
  892. "is sparse.");
  893. goto unm_err_out;
  894. }
  895. if (a->flags & ATTR_COMPRESSION_MASK) {
  896. ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute "
  897. "is compressed.");
  898. goto unm_err_out;
  899. }
  900. if (a->data.non_resident.lowest_vcn) {
  901. ntfs_error(vi->i_sb, "First extent of "
  902. "$INDEX_ALLOCATION attribute has non "
  903. "zero lowest_vcn.");
  904. goto unm_err_out;
  905. }
  906. vi->i_size = sle64_to_cpu(a->data.non_resident.data_size);
  907. ni->initialized_size = sle64_to_cpu(
  908. a->data.non_resident.initialized_size);
  909. ni->allocated_size = sle64_to_cpu(
  910. a->data.non_resident.allocated_size);
  911. /*
  912. * We are done with the mft record, so we release it. Otherwise
  913. * we would deadlock in ntfs_attr_iget().
  914. */
  915. ntfs_attr_put_search_ctx(ctx);
  916. unmap_mft_record(ni);
  917. m = NULL;
  918. ctx = NULL;
  919. /* Get the index bitmap attribute inode. */
  920. bvi = ntfs_attr_iget(vi, AT_BITMAP, I30, 4);
  921. if (IS_ERR(bvi)) {
  922. ntfs_error(vi->i_sb, "Failed to get bitmap attribute.");
  923. err = PTR_ERR(bvi);
  924. goto unm_err_out;
  925. }
  926. bni = NTFS_I(bvi);
  927. if (NInoCompressed(bni) || NInoEncrypted(bni) ||
  928. NInoSparse(bni)) {
  929. ntfs_error(vi->i_sb, "$BITMAP attribute is compressed "
  930. "and/or encrypted and/or sparse.");
  931. goto iput_unm_err_out;
  932. }
  933. /* Consistency check bitmap size vs. index allocation size. */
  934. bvi_size = i_size_read(bvi);
  935. if ((bvi_size << 3) < (vi->i_size >>
  936. ni->itype.index.block_size_bits)) {
  937. ntfs_error(vi->i_sb, "Index bitmap too small (0x%llx) "
  938. "for index allocation (0x%llx).",
  939. bvi_size << 3, vi->i_size);
  940. goto iput_unm_err_out;
  941. }
  942. /* No longer need the bitmap attribute inode. */
  943. iput(bvi);
  944. skip_large_dir_stuff:
  945. /* Setup the operations for this inode. */
  946. vi->i_op = &ntfs_dir_inode_ops;
  947. vi->i_fop = &ntfs_dir_ops;
  948. } else {
  949. /* It is a file. */
  950. ntfs_attr_reinit_search_ctx(ctx);
  951. /* Setup the data attribute, even if not present. */
  952. ni->type = AT_DATA;
  953. ni->name = NULL;
  954. ni->name_len = 0;
  955. /* Find first extent of the unnamed data attribute. */
  956. err = ntfs_attr_lookup(AT_DATA, NULL, 0, 0, 0, NULL, 0, ctx);
  957. if (unlikely(err)) {
  958. vi->i_size = ni->initialized_size =
  959. ni->allocated_size = 0;
  960. if (err != -ENOENT) {
  961. ntfs_error(vi->i_sb, "Failed to lookup $DATA "
  962. "attribute.");
  963. goto unm_err_out;
  964. }
  965. /*
  966. * FILE_Secure does not have an unnamed $DATA
  967. * attribute, so we special case it here.
  968. */
  969. if (vi->i_ino == FILE_Secure)
  970. goto no_data_attr_special_case;
  971. /*
  972. * Most if not all the system files in the $Extend
  973. * system directory do not have unnamed data
  974. * attributes so we need to check if the parent
  975. * directory of the file is FILE_Extend and if it is
  976. * ignore this error. To do this we need to get the
  977. * name of this inode from the mft record as the name
  978. * contains the back reference to the parent directory.
  979. */
  980. if (ntfs_is_extended_system_file(ctx) > 0)
  981. goto no_data_attr_special_case;
  982. // FIXME: File is corrupt! Hot-fix with empty data
  983. // attribute if recovery option is set.
  984. ntfs_error(vi->i_sb, "$DATA attribute is missing.");
  985. goto unm_err_out;
  986. }
  987. a = ctx->attr;
  988. /* Setup the state. */
  989. if (a->flags & (ATTR_COMPRESSION_MASK | ATTR_IS_SPARSE)) {
  990. if (a->flags & ATTR_COMPRESSION_MASK) {
  991. NInoSetCompressed(ni);
  992. if (vol->cluster_size > 4096) {
  993. ntfs_error(vi->i_sb, "Found "
  994. "compressed data but "
  995. "compression is "
  996. "disabled due to "
  997. "cluster size (%i) > "
  998. "4kiB.",
  999. vol->cluster_size);
  1000. goto unm_err_out;
  1001. }
  1002. if ((a->flags & ATTR_COMPRESSION_MASK)
  1003. != ATTR_IS_COMPRESSED) {
  1004. ntfs_error(vi->i_sb, "Found unknown "
  1005. "compression method "
  1006. "or corrupt file.");
  1007. goto unm_err_out;
  1008. }
  1009. }
  1010. if (a->flags & ATTR_IS_SPARSE)
  1011. NInoSetSparse(ni);
  1012. }
  1013. if (a->flags & ATTR_IS_ENCRYPTED) {
  1014. if (NInoCompressed(ni)) {
  1015. ntfs_error(vi->i_sb, "Found encrypted and "
  1016. "compressed data.");
  1017. goto unm_err_out;
  1018. }
  1019. NInoSetEncrypted(ni);
  1020. }
  1021. if (a->non_resident) {
  1022. NInoSetNonResident(ni);
  1023. if (NInoCompressed(ni) || NInoSparse(ni)) {
  1024. if (NInoCompressed(ni) && a->data.non_resident.
  1025. compression_unit != 4) {
  1026. ntfs_error(vi->i_sb, "Found "
  1027. "non-standard "
  1028. "compression unit (%u "
  1029. "instead of 4). "
  1030. "Cannot handle this.",
  1031. a->data.non_resident.
  1032. compression_unit);
  1033. err = -EOPNOTSUPP;
  1034. goto unm_err_out;
  1035. }
  1036. if (a->data.non_resident.compression_unit) {
  1037. ni->itype.compressed.block_size = 1U <<
  1038. (a->data.non_resident.
  1039. compression_unit +
  1040. vol->cluster_size_bits);
  1041. ni->itype.compressed.block_size_bits =
  1042. ffs(ni->itype.
  1043. compressed.
  1044. block_size) - 1;
  1045. ni->itype.compressed.block_clusters =
  1046. 1U << a->data.
  1047. non_resident.
  1048. compression_unit;
  1049. } else {
  1050. ni->itype.compressed.block_size = 0;
  1051. ni->itype.compressed.block_size_bits =
  1052. 0;
  1053. ni->itype.compressed.block_clusters =
  1054. 0;
  1055. }
  1056. ni->itype.compressed.size = sle64_to_cpu(
  1057. a->data.non_resident.
  1058. compressed_size);
  1059. }
  1060. if (a->data.non_resident.lowest_vcn) {
  1061. ntfs_error(vi->i_sb, "First extent of $DATA "
  1062. "attribute has non zero "
  1063. "lowest_vcn.");
  1064. goto unm_err_out;
  1065. }
  1066. vi->i_size = sle64_to_cpu(
  1067. a->data.non_resident.data_size);
  1068. ni->initialized_size = sle64_to_cpu(
  1069. a->data.non_resident.initialized_size);
  1070. ni->allocated_size = sle64_to_cpu(
  1071. a->data.non_resident.allocated_size);
  1072. } else { /* Resident attribute. */
  1073. vi->i_size = ni->initialized_size = le32_to_cpu(
  1074. a->data.resident.value_length);
  1075. ni->allocated_size = le32_to_cpu(a->length) -
  1076. le16_to_cpu(
  1077. a->data.resident.value_offset);
  1078. if (vi->i_size > ni->allocated_size) {
  1079. ntfs_error(vi->i_sb, "Resident data attribute "
  1080. "is corrupt (size exceeds "
  1081. "allocation).");
  1082. goto unm_err_out;
  1083. }
  1084. }
  1085. no_data_attr_special_case:
  1086. /* We are done with the mft record, so we release it. */
  1087. ntfs_attr_put_search_ctx(ctx);
  1088. unmap_mft_record(ni);
  1089. m = NULL;
  1090. ctx = NULL;
  1091. /* Setup the operations for this inode. */
  1092. vi->i_op = &ntfs_file_inode_ops;
  1093. vi->i_fop = &ntfs_file_ops;
  1094. }
  1095. if (NInoMstProtected(ni))
  1096. vi->i_mapping->a_ops = &ntfs_mst_aops;
  1097. else
  1098. vi->i_mapping->a_ops = &ntfs_aops;
  1099. /*
  1100. * The number of 512-byte blocks used on disk (for stat). This is in so
  1101. * far inaccurate as it doesn't account for any named streams or other
  1102. * special non-resident attributes, but that is how Windows works, too,
  1103. * so we are at least consistent with Windows, if not entirely
  1104. * consistent with the Linux Way. Doing it the Linux Way would cause a
  1105. * significant slowdown as it would involve iterating over all
  1106. * attributes in the mft record and adding the allocated/compressed
  1107. * sizes of all non-resident attributes present to give us the Linux
  1108. * correct size that should go into i_blocks (after division by 512).
  1109. */
  1110. if (S_ISREG(vi->i_mode) && (NInoCompressed(ni) || NInoSparse(ni)))
  1111. vi->i_blocks = ni->itype.compressed.size >> 9;
  1112. else
  1113. vi->i_blocks = ni->allocated_size >> 9;
  1114. ntfs_debug("Done.");
  1115. return 0;
  1116. iput_unm_err_out:
  1117. iput(bvi);
  1118. unm_err_out:
  1119. if (!err)
  1120. err = -EIO;
  1121. if (ctx)
  1122. ntfs_attr_put_search_ctx(ctx);
  1123. if (m)
  1124. unmap_mft_record(ni);
  1125. err_out:
  1126. ntfs_error(vol->sb, "Failed with error code %i. Marking corrupt "
  1127. "inode 0x%lx as bad. Run chkdsk.", err, vi->i_ino);
  1128. make_bad_inode(vi);
  1129. if (err != -EOPNOTSUPP && err != -ENOMEM)
  1130. NVolSetErrors(vol);
  1131. return err;
  1132. }
  1133. /**
  1134. * ntfs_read_locked_attr_inode - read an attribute inode from its base inode
  1135. * @base_vi: base inode
  1136. * @vi: attribute inode to read
  1137. *
  1138. * ntfs_read_locked_attr_inode() is called from ntfs_attr_iget() to read the
  1139. * attribute inode described by @vi into memory from the base mft record
  1140. * described by @base_ni.
  1141. *
  1142. * ntfs_read_locked_attr_inode() maps, pins and locks the base inode for
  1143. * reading and looks up the attribute described by @vi before setting up the
  1144. * necessary fields in @vi as well as initializing the ntfs inode.
  1145. *
  1146. * Q: What locks are held when the function is called?
  1147. * A: i_state has I_LOCK set, hence the inode is locked, also
  1148. * i_count is set to 1, so it is not going to go away
  1149. *
  1150. * Return 0 on success and -errno on error. In the error case, the inode will
  1151. * have had make_bad_inode() executed on it.
  1152. *
  1153. * Note this cannot be called for AT_INDEX_ALLOCATION.
  1154. */
  1155. static int ntfs_read_locked_attr_inode(struct inode *base_vi, struct inode *vi)
  1156. {
  1157. ntfs_volume *vol = NTFS_SB(vi->i_sb);
  1158. ntfs_inode *ni, *base_ni;
  1159. MFT_RECORD *m;
  1160. ATTR_RECORD *a;
  1161. ntfs_attr_search_ctx *ctx;
  1162. int err = 0;
  1163. ntfs_debug("Entering for i_ino 0x%lx.", vi->i_ino);
  1164. ntfs_init_big_inode(vi);
  1165. ni = NTFS_I(vi);
  1166. base_ni = NTFS_I(base_vi);
  1167. /* Just mirror the values from the base inode. */
  1168. vi->i_version = base_vi->i_version;
  1169. vi->i_uid = base_vi->i_uid;
  1170. vi->i_gid = base_vi->i_gid;
  1171. vi->i_nlink = base_vi->i_nlink;
  1172. vi->i_mtime = base_vi->i_mtime;
  1173. vi->i_ctime = base_vi->i_ctime;
  1174. vi->i_atime = base_vi->i_atime;
  1175. vi->i_generation = ni->seq_no = base_ni->seq_no;
  1176. /* Set inode type to zero but preserve permissions. */
  1177. vi->i_mode = base_vi->i_mode & ~S_IFMT;
  1178. m = map_mft_record(base_ni);
  1179. if (IS_ERR(m)) {
  1180. err = PTR_ERR(m);
  1181. goto err_out;
  1182. }
  1183. ctx = ntfs_attr_get_search_ctx(base_ni, m);
  1184. if (!ctx) {
  1185. err = -ENOMEM;
  1186. goto unm_err_out;
  1187. }
  1188. /* Find the attribute. */
  1189. err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
  1190. CASE_SENSITIVE, 0, NULL, 0, ctx);
  1191. if (unlikely(err))
  1192. goto unm_err_out;
  1193. a = ctx->attr;
  1194. if (a->flags & (ATTR_COMPRESSION_MASK | ATTR_IS_SPARSE)) {
  1195. if (a->flags & ATTR_COMPRESSION_MASK) {
  1196. NInoSetCompressed(ni);
  1197. if ((ni->type != AT_DATA) || (ni->type == AT_DATA &&
  1198. ni->name_len)) {
  1199. ntfs_error(vi->i_sb, "Found compressed "
  1200. "non-data or named data "
  1201. "attribute. Please report "
  1202. "you saw this message to "
  1203. "linux-ntfs-dev@lists."
  1204. "sourceforge.net");
  1205. goto unm_err_out;
  1206. }
  1207. if (vol->cluster_size > 4096) {
  1208. ntfs_error(vi->i_sb, "Found compressed "
  1209. "attribute but compression is "
  1210. "disabled due to cluster size "
  1211. "(%i) > 4kiB.",
  1212. vol->cluster_size);
  1213. goto unm_err_out;
  1214. }
  1215. if ((a->flags & ATTR_COMPRESSION_MASK) !=
  1216. ATTR_IS_COMPRESSED) {
  1217. ntfs_error(vi->i_sb, "Found unknown "
  1218. "compression method.");
  1219. goto unm_err_out;
  1220. }
  1221. }
  1222. /*
  1223. * The compressed/sparse flag set in an index root just means
  1224. * to compress all files.
  1225. */
  1226. if (NInoMstProtected(ni) && ni->type != AT_INDEX_ROOT) {
  1227. ntfs_error(vi->i_sb, "Found mst protected attribute "
  1228. "but the attribute is %s. Please "
  1229. "report you saw this message to "
  1230. "linux-ntfs-dev@lists.sourceforge.net",
  1231. NInoCompressed(ni) ? "compressed" :
  1232. "sparse");
  1233. goto unm_err_out;
  1234. }
  1235. if (a->flags & ATTR_IS_SPARSE)
  1236. NInoSetSparse(ni);
  1237. }
  1238. if (a->flags & ATTR_IS_ENCRYPTED) {
  1239. if (NInoCompressed(ni)) {
  1240. ntfs_error(vi->i_sb, "Found encrypted and compressed "
  1241. "data.");
  1242. goto unm_err_out;
  1243. }
  1244. /*
  1245. * The encryption flag set in an index root just means to
  1246. * encrypt all files.
  1247. */
  1248. if (NInoMstProtected(ni) && ni->type != AT_INDEX_ROOT) {
  1249. ntfs_error(vi->i_sb, "Found mst protected attribute "
  1250. "but the attribute is encrypted. "
  1251. "Please report you saw this message "
  1252. "to linux-ntfs-dev@lists.sourceforge."
  1253. "net");
  1254. goto unm_err_out;
  1255. }
  1256. if (ni->type != AT_DATA) {
  1257. ntfs_error(vi->i_sb, "Found encrypted non-data "
  1258. "attribute.");
  1259. goto unm_err_out;
  1260. }
  1261. NInoSetEncrypted(ni);
  1262. }
  1263. if (!a->non_resident) {
  1264. /* Ensure the attribute name is placed before the value. */
  1265. if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >=
  1266. le16_to_cpu(a->data.resident.value_offset)))) {
  1267. ntfs_error(vol->sb, "Attribute name is placed after "
  1268. "the attribute value.");
  1269. goto unm_err_out;
  1270. }
  1271. if (NInoMstProtected(ni)) {
  1272. ntfs_error(vi->i_sb, "Found mst protected attribute "
  1273. "but the attribute is resident. "
  1274. "Please report you saw this message to "
  1275. "linux-ntfs-dev@lists.sourceforge.net");
  1276. goto unm_err_out;
  1277. }
  1278. vi->i_size = ni->initialized_size = le32_to_cpu(
  1279. a->data.resident.value_length);
  1280. ni->allocated_size = le32_to_cpu(a->length) -
  1281. le16_to_cpu(a->data.resident.value_offset);
  1282. if (vi->i_size > ni->allocated_size) {
  1283. ntfs_error(vi->i_sb, "Resident attribute is corrupt "
  1284. "(size exceeds allocation).");
  1285. goto unm_err_out;
  1286. }
  1287. } else {
  1288. NInoSetNonResident(ni);
  1289. /*
  1290. * Ensure the attribute name is placed before the mapping pairs
  1291. * array.
  1292. */
  1293. if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >=
  1294. le16_to_cpu(
  1295. a->data.non_resident.mapping_pairs_offset)))) {
  1296. ntfs_error(vol->sb, "Attribute name is placed after "
  1297. "the mapping pairs array.");
  1298. goto unm_err_out;
  1299. }
  1300. if (NInoCompressed(ni) || NInoSparse(ni)) {
  1301. if (NInoCompressed(ni) && a->data.non_resident.
  1302. compression_unit != 4) {
  1303. ntfs_error(vi->i_sb, "Found non-standard "
  1304. "compression unit (%u instead "
  1305. "of 4). Cannot handle this.",
  1306. a->data.non_resident.
  1307. compression_unit);
  1308. err = -EOPNOTSUPP;
  1309. goto unm_err_out;
  1310. }
  1311. if (a->data.non_resident.compression_unit) {
  1312. ni->itype.compressed.block_size = 1U <<
  1313. (a->data.non_resident.
  1314. compression_unit +
  1315. vol->cluster_size_bits);
  1316. ni->itype.compressed.block_size_bits =
  1317. ffs(ni->itype.compressed.
  1318. block_size) - 1;
  1319. ni->itype.compressed.block_clusters = 1U <<
  1320. a->data.non_resident.
  1321. compression_unit;
  1322. } else {
  1323. ni->itype.compressed.block_size = 0;
  1324. ni->itype.compressed.block_size_bits = 0;
  1325. ni->itype.compressed.block_clusters = 0;
  1326. }
  1327. ni->itype.compressed.size = sle64_to_cpu(
  1328. a->data.non_resident.compressed_size);
  1329. }
  1330. if (a->data.non_resident.lowest_vcn) {
  1331. ntfs_error(vi->i_sb, "First extent of attribute has "
  1332. "non-zero lowest_vcn.");
  1333. goto unm_err_out;
  1334. }
  1335. vi->i_size = sle64_to_cpu(a->data.non_resident.data_size);
  1336. ni->initialized_size = sle64_to_cpu(
  1337. a->data.non_resident.initialized_size);
  1338. ni->allocated_size = sle64_to_cpu(
  1339. a->data.non_resident.allocated_size);
  1340. }
  1341. /* Setup the operations for this attribute inode. */
  1342. vi->i_op = NULL;
  1343. vi->i_fop = NULL;
  1344. if (NInoMstProtected(ni))
  1345. vi->i_mapping->a_ops = &ntfs_mst_aops;
  1346. else
  1347. vi->i_mapping->a_ops = &ntfs_aops;
  1348. if ((NInoCompressed(ni) || NInoSparse(ni)) && ni->type != AT_INDEX_ROOT)
  1349. vi->i_blocks = ni->itype.compressed.size >> 9;
  1350. else
  1351. vi->i_blocks = ni->allocated_size >> 9;
  1352. /*
  1353. * Make sure the base inode does not go away and attach it to the
  1354. * attribute inode.
  1355. */
  1356. igrab(base_vi);
  1357. ni->ext.base_ntfs_ino = base_ni;
  1358. ni->nr_extents = -1;
  1359. ntfs_attr_put_search_ctx(ctx);
  1360. unmap_mft_record(base_ni);
  1361. ntfs_debug("Done.");
  1362. return 0;
  1363. unm_err_out:
  1364. if (!err)
  1365. err = -EIO;
  1366. if (ctx)
  1367. ntfs_attr_put_search_ctx(ctx);
  1368. unmap_mft_record(base_ni);
  1369. err_out:
  1370. ntfs_error(vol->sb, "Failed with error code %i while reading attribute "
  1371. "inode (mft_no 0x%lx, type 0x%x, name_len %i). "
  1372. "Marking corrupt inode and base inode 0x%lx as bad. "
  1373. "Run chkdsk.", err, vi->i_ino, ni->type, ni->name_len,
  1374. base_vi->i_ino);
  1375. make_bad_inode(vi);
  1376. if (err != -ENOMEM)
  1377. NVolSetErrors(vol);
  1378. return err;
  1379. }
  1380. /**
  1381. * ntfs_read_locked_index_inode - read an index inode from its base inode
  1382. * @base_vi: base inode
  1383. * @vi: index inode to read
  1384. *
  1385. * ntfs_read_locked_index_inode() is called from ntfs_index_iget() to read the
  1386. * index inode described by @vi into memory from the base mft record described
  1387. * by @base_ni.
  1388. *
  1389. * ntfs_read_locked_index_inode() maps, pins and locks the base inode for
  1390. * reading and looks up the attributes relating to the index described by @vi
  1391. * before setting up the necessary fields in @vi as well as initializing the
  1392. * ntfs inode.
  1393. *
  1394. * Note, index inodes are essentially attribute inodes (NInoAttr() is true)
  1395. * with the attribute type set to AT_INDEX_ALLOCATION. Apart from that, they
  1396. * are setup like directory inodes since directories are a special case of
  1397. * indices ao they need to be treated in much the same way. Most importantly,
  1398. * for small indices the index allocation attribute might not actually exist.
  1399. * However, the index root attribute always exists but this does not need to
  1400. * have an inode associated with it and this is why we define a new inode type
  1401. * index. Also, like for directories, we need to have an attribute inode for
  1402. * the bitmap attribute corresponding to the index allocation attribute and we
  1403. * can store this in the appropriate field of the inode, just like we do for
  1404. * normal directory inodes.
  1405. *
  1406. * Q: What locks are held when the function is called?
  1407. * A: i_state has I_LOCK set, hence the inode is locked, also
  1408. * i_count is set to 1, so it is not going to go away
  1409. *
  1410. * Return 0 on success and -errno on error. In the error case, the inode will
  1411. * have had make_bad_inode() executed on it.
  1412. */
  1413. static int ntfs_read_locked_index_inode(struct inode *base_vi, struct inode *vi)
  1414. {
  1415. loff_t bvi_size;
  1416. ntfs_volume *vol = NTFS_SB(vi->i_sb);
  1417. ntfs_inode *ni, *base_ni, *bni;
  1418. struct inode *bvi;
  1419. MFT_RECORD *m;
  1420. ATTR_RECORD *a;
  1421. ntfs_attr_search_ctx *ctx;
  1422. INDEX_ROOT *ir;
  1423. u8 *ir_end, *index_end;
  1424. int err = 0;
  1425. ntfs_debug("Entering for i_ino 0x%lx.", vi->i_ino);
  1426. ntfs_init_big_inode(vi);
  1427. ni = NTFS_I(vi);
  1428. base_ni = NTFS_I(base_vi);
  1429. /* Just mirror the values from the base inode. */
  1430. vi->i_version = base_vi->i_version;
  1431. vi->i_uid = base_vi->i_uid;
  1432. vi->i_gid = base_vi->i_gid;
  1433. vi->i_nlink = base_vi->i_nlink;
  1434. vi->i_mtime = base_vi->i_mtime;
  1435. vi->i_ctime = base_vi->i_ctime;
  1436. vi->i_atime = base_vi->i_atime;
  1437. vi->i_generation = ni->seq_no = base_ni->seq_no;
  1438. /* Set inode type to zero but preserve permissions. */
  1439. vi->i_mode = base_vi->i_mode & ~S_IFMT;
  1440. /* Map the mft record for the base inode. */
  1441. m = map_mft_record(base_ni);
  1442. if (IS_ERR(m)) {
  1443. err = PTR_ERR(m);
  1444. goto err_out;
  1445. }
  1446. ctx = ntfs_attr_get_search_ctx(base_ni, m);
  1447. if (!ctx) {
  1448. err = -ENOMEM;
  1449. goto unm_err_out;
  1450. }
  1451. /* Find the index root attribute. */
  1452. err = ntfs_attr_lookup(AT_INDEX_ROOT, ni->name, ni->name_len,
  1453. CASE_SENSITIVE, 0, NULL, 0, ctx);
  1454. if (unlikely(err)) {
  1455. if (err == -ENOENT)
  1456. ntfs_error(vi->i_sb, "$INDEX_ROOT attribute is "
  1457. "missing.");
  1458. goto unm_err_out;
  1459. }
  1460. a = ctx->attr;
  1461. /* Set up the state. */
  1462. if (unlikely(a->non_resident)) {
  1463. ntfs_error(vol->sb, "$INDEX_ROOT attribute is not resident.");
  1464. goto unm_err_out;
  1465. }
  1466. /* Ensure the attribute name is placed before the value. */
  1467. if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >=
  1468. le16_to_cpu(a->data.resident.value_offset)))) {
  1469. ntfs_error(vol->sb, "$INDEX_ROOT attribute name is placed "
  1470. "after the attribute value.");
  1471. goto unm_err_out;
  1472. }
  1473. /*
  1474. * Compressed/encrypted/sparse index root is not allowed, except for
  1475. * directories of course but those are not dealt with here.
  1476. */
  1477. if (a->flags & (ATTR_COMPRESSION_MASK | ATTR_IS_ENCRYPTED |
  1478. ATTR_IS_SPARSE)) {
  1479. ntfs_error(vi->i_sb, "Found compressed/encrypted/sparse index "
  1480. "root attribute.");
  1481. goto unm_err_out;
  1482. }
  1483. ir = (INDEX_ROOT*)((u8*)a + le16_to_cpu(a->data.resident.value_offset));
  1484. ir_end = (u8*)ir + le32_to_cpu(a->data.resident.value_length);
  1485. if (ir_end > (u8*)ctx->mrec + vol->mft_record_size) {
  1486. ntfs_error(vi->i_sb, "$INDEX_ROOT attribute is corrupt.");
  1487. goto unm_err_out;
  1488. }
  1489. index_end = (u8*)&ir->index + le32_to_cpu(ir->index.index_length);
  1490. if (index_end > ir_end) {
  1491. ntfs_error(vi->i_sb, "Index is corrupt.");
  1492. goto unm_err_out;
  1493. }
  1494. if (ir->type) {
  1495. ntfs_error(vi->i_sb, "Index type is not 0 (type is 0x%x).",
  1496. le32_to_cpu(ir->type));
  1497. goto unm_err_out;
  1498. }
  1499. ni->itype.index.collation_rule = ir->collation_rule;
  1500. ntfs_debug("Index collation rule is 0x%x.",
  1501. le32_to_cpu(ir->collation_rule));
  1502. ni->itype.index.block_size = le32_to_cpu(ir->index_block_size);
  1503. if (ni->itype.index.block_size & (ni->itype.index.block_size - 1)) {
  1504. ntfs_error(vi->i_sb, "Index block size (%u) is not a power of "
  1505. "two.", ni->itype.index.block_size);
  1506. goto unm_err_out;
  1507. }
  1508. if (ni->itype.index.block_size > PAGE_CACHE_SIZE) {
  1509. ntfs_error(vi->i_sb, "Index block size (%u) > PAGE_CACHE_SIZE "
  1510. "(%ld) is not supported. Sorry.",
  1511. ni->itype.index.block_size, PAGE_CACHE_SIZE);
  1512. err = -EOPNOTSUPP;
  1513. goto unm_err_out;
  1514. }
  1515. if (ni->itype.index.block_size < NTFS_BLOCK_SIZE) {
  1516. ntfs_error(vi->i_sb, "Index block size (%u) < NTFS_BLOCK_SIZE "
  1517. "(%i) is not supported. Sorry.",
  1518. ni->itype.index.block_size, NTFS_BLOCK_SIZE);
  1519. err = -EOPNOTSUPP;
  1520. goto unm_err_out;
  1521. }
  1522. ni->itype.index.block_size_bits = ffs(ni->itype.index.block_size) - 1;
  1523. /* Determine the size of a vcn in the index. */
  1524. if (vol->cluster_size <= ni->itype.index.block_size) {
  1525. ni->itype.index.vcn_size = vol->cluster_size;
  1526. ni->itype.index.vcn_size_bits = vol->cluster_size_bits;
  1527. } else {
  1528. ni->itype.index.vcn_size = vol->sector_size;
  1529. ni->itype.index.vcn_size_bits = vol->sector_size_bits;
  1530. }
  1531. /* Check for presence of index allocation attribute. */
  1532. if (!(ir->index.flags & LARGE_INDEX)) {
  1533. /* No index allocation. */
  1534. vi->i_size = ni->initialized_size = ni->allocated_size = 0;
  1535. /* We are done with the mft record, so we release it. */
  1536. ntfs_attr_put_search_ctx(ctx);
  1537. unmap_mft_record(base_ni);
  1538. m = NULL;
  1539. ctx = NULL;
  1540. goto skip_large_index_stuff;
  1541. } /* LARGE_INDEX: Index allocation present. Setup state. */
  1542. NInoSetIndexAllocPresent(ni);
  1543. /* Find index allocation attribute. */
  1544. ntfs_attr_reinit_search_ctx(ctx);
  1545. err = ntfs_attr_lookup(AT_INDEX_ALLOCATION, ni->name, ni->name_len,
  1546. CASE_SENSITIVE, 0, NULL, 0, ctx);
  1547. if (unlikely(err)) {
  1548. if (err == -ENOENT)
  1549. ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is "
  1550. "not present but $INDEX_ROOT "
  1551. "indicated it is.");
  1552. else
  1553. ntfs_error(vi->i_sb, "Failed to lookup "
  1554. "$INDEX_ALLOCATION attribute.");
  1555. goto unm_err_out;
  1556. }
  1557. a = ctx->attr;
  1558. if (!a->non_resident) {
  1559. ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is "
  1560. "resident.");
  1561. goto unm_err_out;
  1562. }
  1563. /*
  1564. * Ensure the attribute name is placed before the mapping pairs array.
  1565. */
  1566. if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >=
  1567. le16_to_cpu(
  1568. a->data.non_resident.mapping_pairs_offset)))) {
  1569. ntfs_error(vol->sb, "$INDEX_ALLOCATION attribute name is "
  1570. "placed after the mapping pairs array.");
  1571. goto unm_err_out;
  1572. }
  1573. if (a->flags & ATTR_IS_ENCRYPTED) {
  1574. ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is "
  1575. "encrypted.");
  1576. goto unm_err_out;
  1577. }
  1578. if (a->flags & ATTR_IS_SPARSE) {
  1579. ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is sparse.");
  1580. goto unm_err_out;
  1581. }
  1582. if (a->flags & ATTR_COMPRESSION_MASK) {
  1583. ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is "
  1584. "compressed.");
  1585. goto unm_err_out;
  1586. }
  1587. if (a->data.non_resident.lowest_vcn) {
  1588. ntfs_error(vi->i_sb, "First extent of $INDEX_ALLOCATION "
  1589. "attribute has non zero lowest_vcn.");
  1590. goto unm_err_out;
  1591. }
  1592. vi->i_size = sle64_to_cpu(a->data.non_resident.data_size);
  1593. ni->initialized_size = sle64_to_cpu(
  1594. a->data.non_resident.initialized_size);
  1595. ni->allocated_size = sle64_to_cpu(a->data.non_resident.allocated_size);
  1596. /*
  1597. * We are done with the mft record, so we release it. Otherwise
  1598. * we would deadlock in ntfs_attr_iget().
  1599. */
  1600. ntfs_attr_put_search_ctx(ctx);
  1601. unmap_mft_record(base_ni);
  1602. m = NULL;
  1603. ctx = NULL;
  1604. /* Get the index bitmap attribute inode. */
  1605. bvi = ntfs_attr_iget(base_vi, AT_BITMAP, ni->name, ni->name_len);
  1606. if (IS_ERR(bvi)) {
  1607. ntfs_error(vi->i_sb, "Failed to get bitmap attribute.");
  1608. err = PTR_ERR(bvi);
  1609. goto unm_err_out;
  1610. }
  1611. bni = NTFS_I(bvi);
  1612. if (NInoCompressed(bni) || NInoEncrypted(bni) ||
  1613. NInoSparse(bni)) {
  1614. ntfs_error(vi->i_sb, "$BITMAP attribute is compressed and/or "
  1615. "encrypted and/or sparse.");
  1616. goto iput_unm_err_out;
  1617. }
  1618. /* Consistency check bitmap size vs. index allocation size. */
  1619. bvi_size = i_size_read(bvi);
  1620. if ((bvi_size << 3) < (vi->i_size >> ni->itype.index.block_size_bits)) {
  1621. ntfs_error(vi->i_sb, "Index bitmap too small (0x%llx) for "
  1622. "index allocation (0x%llx).", bvi_size << 3,
  1623. vi->i_size);
  1624. goto iput_unm_err_out;
  1625. }
  1626. iput(bvi);
  1627. skip_large_index_stuff:
  1628. /* Setup the operations for this index inode. */
  1629. vi->i_op = NULL;
  1630. vi->i_fop = NULL;
  1631. vi->i_mapping->a_ops = &ntfs_mst_aops;
  1632. vi->i_blocks = ni->allocated_size >> 9;
  1633. /*
  1634. * Make sure the base inode doesn't go away and attach it to the
  1635. * index inode.
  1636. */
  1637. igrab(base_vi);
  1638. ni->ext.base_ntfs_ino = base_ni;
  1639. ni->nr_extents = -1;
  1640. ntfs_debug("Done.");
  1641. return 0;
  1642. iput_unm_err_out:
  1643. iput(bvi);
  1644. unm_err_out:
  1645. if (!err)
  1646. err = -EIO;
  1647. if (ctx)
  1648. ntfs_attr_put_search_ctx(ctx);
  1649. if (m)
  1650. unmap_mft_record(base_ni);
  1651. err_out:
  1652. ntfs_error(vi->i_sb, "Failed with error code %i while reading index "
  1653. "inode (mft_no 0x%lx, name_len %i.", err, vi->i_ino,
  1654. ni->name_len);
  1655. make_bad_inode(vi);
  1656. if (err != -EOPNOTSUPP && err != -ENOMEM)
  1657. NVolSetErrors(vol);
  1658. return err;
  1659. }
  1660. /*
  1661. * The MFT inode has special locking, so teach the lock validator
  1662. * about this by splitting off the locking rules of the MFT from
  1663. * the locking rules of other inodes. The MFT inode can never be
  1664. * accessed from the VFS side (or even internally), only by the
  1665. * map_mft functions.
  1666. */
  1667. static struct lock_class_key mft_ni_runlist_lock_key, mft_ni_mrec_lock_key;
  1668. /**
  1669. * ntfs_read_inode_mount - special read_inode for mount time use only
  1670. * @vi: inode to read
  1671. *
  1672. * Read inode FILE_MFT at mount time, only called with super_block lock
  1673. * held from within the read_super() code path.
  1674. *
  1675. * This function exists because when it is called the page cache for $MFT/$DATA
  1676. * is not initialized and hence we cannot get at the contents of mft records
  1677. * by calling map_mft_record*().
  1678. *
  1679. * Further it needs to cope with the circular references problem, i.e. cannot
  1680. * load any attributes other than $ATTRIBUTE_LIST until $DATA is loaded, because
  1681. * we do not know where the other extent mft records are yet and again, because
  1682. * we cannot call map_mft_record*() yet. Obviously this applies only when an
  1683. * attribute list is actually present in $MFT inode.
  1684. *
  1685. * We solve these problems by starting with the $DATA attribute before anything
  1686. * else and iterating using ntfs_attr_lookup($DATA) over all extents. As each
  1687. * extent is found, we ntfs_mapping_pairs_decompress() including the implied
  1688. * ntfs_runlists_merge(). Each step of the iteration necessarily provides
  1689. * sufficient information for the next step to complete.
  1690. *
  1691. * This should work but there are two possible pit falls (see inline comments
  1692. * below), but only time will tell if they are real pits or just smoke...
  1693. */
  1694. int ntfs_read_inode_mount(struct inode *vi)
  1695. {
  1696. VCN next_vcn, last_vcn, highest_vcn;
  1697. s64 block;
  1698. struct super_block *sb = vi->i_sb;
  1699. ntfs_volume *vol = NTFS_SB(sb);
  1700. struct buffer_head *bh;
  1701. ntfs_inode *ni;
  1702. MFT_RECORD *m = NULL;
  1703. ATTR_RECORD *a;
  1704. ntfs_attr_search_ctx *ctx;
  1705. unsigned int i, nr_blocks;
  1706. int err;
  1707. ntfs_debug("Entering.");
  1708. /* Initialize the ntfs specific part of @vi. */
  1709. ntfs_init_big_inode(vi);
  1710. ni = NTFS_I(vi);
  1711. /* Setup the data attribute. It is special as it is mst protected. */
  1712. NInoSetNonResident(ni);
  1713. NInoSetMstProtected(ni);
  1714. NInoSetSparseDisabled(ni);
  1715. ni->type = AT_DATA;
  1716. ni->name = NULL;
  1717. ni->name_len = 0;
  1718. /*
  1719. * This sets up our little cheat allowing us to reuse the async read io
  1720. * completion handler for directories.
  1721. */
  1722. ni->itype.index.block_size = vol->mft_record_size;
  1723. ni->itype.index.block_size_bits = vol->mft_record_size_bits;
  1724. /* Very important! Needed to be able to call map_mft_record*(). */
  1725. vol->mft_ino = vi;
  1726. /* Allocate enough memory to read the first mft record. */
  1727. if (vol->mft_record_size > 64 * 1024) {
  1728. ntfs_error(sb, "Unsupported mft record size %i (max 64kiB).",
  1729. vol->mft_record_size);
  1730. goto err_out;
  1731. }
  1732. i = vol->mft_record_size;
  1733. if (i < sb->s_blocksize)
  1734. i = sb->s_blocksize;
  1735. m = (MFT_RECORD*)ntfs_malloc_nofs(i);
  1736. if (!m) {
  1737. ntfs_error(sb, "Failed to allocate buffer for $MFT record 0.");
  1738. goto err_out;
  1739. }
  1740. /* Determine the first block of the $MFT/$DATA attribute. */
  1741. block = vol->mft_lcn << vol->cluster_size_bits >>
  1742. sb->s_blocksize_bits;
  1743. nr_blocks = vol->mft_record_size >> sb->s_blocksize_bits;
  1744. if (!nr_blocks)
  1745. nr_blocks = 1;
  1746. /* Load $MFT/$DATA's first mft record. */
  1747. for (i = 0; i < nr_blocks; i++) {
  1748. bh = sb_bread(sb, block++);
  1749. if (!bh) {
  1750. ntfs_error(sb, "Device read failed.");
  1751. goto err_out;
  1752. }
  1753. memcpy((char*)m + (i << sb->s_blocksize_bits), bh->b_data,
  1754. sb->s_blocksize);
  1755. brelse(bh);
  1756. }
  1757. /* Apply the mst fixups. */
  1758. if (post_read_mst_fixup((NTFS_RECORD*)m, vol->mft_record_size)) {
  1759. /* FIXME: Try to use the $MFTMirr now. */
  1760. ntfs_error(sb, "MST fixup failed. $MFT is corrupt.");
  1761. goto err_out;
  1762. }
  1763. /* Need this to sanity check attribute list references to $MFT. */
  1764. vi->i_generation = ni->seq_no = le16_to_cpu(m->sequence_number);
  1765. /* Provides readpage() and sync_page() for map_mft_record(). */
  1766. vi->i_mapping->a_ops = &ntfs_mst_aops;
  1767. ctx = ntfs_attr_get_search_ctx(ni, m);
  1768. if (!ctx) {
  1769. err = -ENOMEM;
  1770. goto err_out;
  1771. }
  1772. /* Find the attribute list attribute if present. */
  1773. err = ntfs_attr_lookup(AT_ATTRIBUTE_LIST, NULL, 0, 0, 0, NULL, 0, ctx);
  1774. if (err) {
  1775. if (unlikely(err != -ENOENT)) {
  1776. ntfs_error(sb, "Failed to lookup attribute list "
  1777. "attribute. You should run chkdsk.");
  1778. goto put_err_out;
  1779. }
  1780. } else /* if (!err) */ {
  1781. ATTR_LIST_ENTRY *al_entry, *next_al_entry;
  1782. u8 *al_end;
  1783. static const char *es = " Not allowed. $MFT is corrupt. "
  1784. "You should run chkdsk.";
  1785. ntfs_debug("Attribute list attribute found in $MFT.");
  1786. NInoSetAttrList(ni);
  1787. a = ctx->attr;
  1788. if (a->flags & ATTR_COMPRESSION_MASK) {
  1789. ntfs_error(sb, "Attribute list attribute is "
  1790. "compressed.%s", es);
  1791. goto put_err_out;
  1792. }
  1793. if (a->flags & ATTR_IS_ENCRYPTED ||
  1794. a->flags & ATTR_IS_SPARSE) {
  1795. if (a->non_resident) {
  1796. ntfs_error(sb, "Non-resident attribute list "
  1797. "attribute is encrypted/"
  1798. "sparse.%s", es);
  1799. goto put_err_out;
  1800. }
  1801. ntfs_warning(sb, "Resident attribute list attribute "
  1802. "in $MFT system file is marked "
  1803. "encrypted/sparse which is not true. "
  1804. "However, Windows allows this and "
  1805. "chkdsk does not detect or correct it "
  1806. "so we will just ignore the invalid "
  1807. "flags and pretend they are not set.");
  1808. }
  1809. /* Now allocate memory for the attribute list. */
  1810. ni->attr_list_size = (u32)ntfs_attr_size(a);
  1811. ni->attr_list = ntfs_malloc_nofs(ni->attr_list_size);
  1812. if (!ni->attr_list) {
  1813. ntfs_error(sb, "Not enough memory to allocate buffer "
  1814. "for attribute list.");
  1815. goto put_err_out;
  1816. }
  1817. if (a->non_resident) {
  1818. NInoSetAttrListNonResident(ni);
  1819. if (a->data.non_resident.lowest_vcn) {
  1820. ntfs_error(sb, "Attribute list has non zero "
  1821. "lowest_vcn. $MFT is corrupt. "
  1822. "You should run chkdsk.");
  1823. goto put_err_out;
  1824. }
  1825. /* Setup the runlist. */
  1826. ni->attr_list_rl.rl = ntfs_mapping_pairs_decompress(vol,
  1827. a, NULL);
  1828. if (IS_ERR(ni->attr_list_rl.rl)) {
  1829. err = PTR_ERR(ni->attr_list_rl.rl);
  1830. ni->attr_list_rl.rl = NULL;
  1831. ntfs_error(sb, "Mapping pairs decompression "
  1832. "failed with error code %i.",
  1833. -err);
  1834. goto put_err_out;
  1835. }
  1836. /* Now load the attribute list. */
  1837. if ((err = load_attribute_list(vol, &ni->attr_list_rl,
  1838. ni->attr_list, ni->attr_list_size,
  1839. sle64_to_cpu(a->data.
  1840. non_resident.initialized_size)))) {
  1841. ntfs_error(sb, "Failed to load attribute list "
  1842. "attribute with error code %i.",
  1843. -err);
  1844. goto put_err_out;
  1845. }
  1846. } else /* if (!ctx.attr->non_resident) */ {
  1847. if ((u8*)a + le16_to_cpu(
  1848. a->data.resident.value_offset) +
  1849. le32_to_cpu(
  1850. a->data.resident.value_length) >
  1851. (u8*)ctx->mrec + vol->mft_record_size) {
  1852. ntfs_error(sb, "Corrupt attribute list "
  1853. "attribute.");
  1854. goto put_err_out;
  1855. }
  1856. /* Now copy the attribute list. */
  1857. memcpy(ni->attr_list, (u8*)a + le16_to_cpu(
  1858. a->data.resident.value_offset),
  1859. le32_to_cpu(
  1860. a->data.resident.value_length));
  1861. }
  1862. /* The attribute list is now setup in memory. */
  1863. /*
  1864. * FIXME: I don't know if this case is actually possible.
  1865. * According to logic it is not possible but I have seen too
  1866. * many weird things in MS software to rely on logic... Thus we
  1867. * perform a manual search and make sure the first $MFT/$DATA
  1868. * extent is in the base inode. If it is not we abort with an
  1869. * error and if we ever see a report of this error we will need
  1870. * to do some magic in order to have the necessary mft record
  1871. * loaded and in the right place in the page cache. But
  1872. * hopefully logic will prevail and this never happens...
  1873. */
  1874. al_entry = (ATTR_LIST_ENTRY*)ni->attr_list;
  1875. al_end = (u8*)al_entry + ni->attr_list_size;
  1876. for (;; al_entry = next_al_entry) {
  1877. /* Out of bounds check. */
  1878. if ((u8*)al_entry < ni->attr_list ||
  1879. (u8*)al_entry > al_end)
  1880. goto em_put_err_out;
  1881. /* Catch the end of the attribute list. */
  1882. if ((u8*)al_entry == al_end)
  1883. goto em_put_err_out;
  1884. if (!al_entry->length)
  1885. goto em_put_err_out;
  1886. if ((u8*)al_entry + 6 > al_end || (u8*)al_entry +
  1887. le16_to_cpu(al_entry->length) > al_end)
  1888. goto em_put_err_out;
  1889. next_al_entry = (ATTR_LIST_ENTRY*)((u8*)al_entry +
  1890. le16_to_cpu(al_entry->length));
  1891. if (le32_to_cpu(al_entry->type) >
  1892. const_le32_to_cpu(AT_DATA))
  1893. goto em_put_err_out;
  1894. if (AT_DATA != al_entry->type)
  1895. continue;
  1896. /* We want an unnamed attribute. */
  1897. if (al_entry->name_length)
  1898. goto em_put_err_out;
  1899. /* Want the first entry, i.e. lowest_vcn == 0. */
  1900. if (al_entry->lowest_vcn)
  1901. goto em_put_err_out;
  1902. /* First entry has to be in the base mft record. */
  1903. if (MREF_LE(al_entry->mft_reference) != vi->i_ino) {
  1904. /* MFT references do not match, logic fails. */
  1905. ntfs_error(sb, "BUG: The first $DATA extent "
  1906. "of $MFT is not in the base "
  1907. "mft record. Please report "
  1908. "you saw this message to "
  1909. "linux-ntfs-dev@lists."
  1910. "sourceforge.net");
  1911. goto put_err_out;
  1912. } else {
  1913. /* Sequence numbers must match. */
  1914. if (MSEQNO_LE(al_entry->mft_reference) !=
  1915. ni->seq_no)
  1916. goto em_put_err_out;
  1917. /* Got it. All is ok. We can stop now. */
  1918. break;
  1919. }
  1920. }
  1921. }
  1922. ntfs_attr_reinit_search_ctx(ctx);
  1923. /* Now load all attribute extents. */
  1924. a = NULL;
  1925. next_vcn = last_vcn = highest_vcn = 0;
  1926. while (!(err = ntfs_attr_lookup(AT_DATA, NULL, 0, 0, next_vcn, NULL, 0,
  1927. ctx))) {
  1928. runlist_element *nrl;
  1929. /* Cache the current attribute. */
  1930. a = ctx->attr;
  1931. /* $MFT must be non-resident. */
  1932. if (!a->non_resident) {
  1933. ntfs_error(sb, "$MFT must be non-resident but a "
  1934. "resident extent was found. $MFT is "
  1935. "corrupt. Run chkdsk.");
  1936. goto put_err_out;
  1937. }
  1938. /* $MFT must be uncompressed and unencrypted. */
  1939. if (a->flags & ATTR_COMPRESSION_MASK ||
  1940. a->flags & ATTR_IS_ENCRYPTED ||
  1941. a->flags & ATTR_IS_SPARSE) {
  1942. ntfs_error(sb, "$MFT must be uncompressed, "
  1943. "non-sparse, and unencrypted but a "
  1944. "compressed/sparse/encrypted extent "
  1945. "was found. $MFT is corrupt. Run "
  1946. "chkdsk.");
  1947. goto put_err_out;
  1948. }
  1949. /*
  1950. * Decompress the mapping pairs array of this extent and merge
  1951. * the result into the existing runlist. No need for locking
  1952. * as we have exclusive access to the inode at this time and we
  1953. * are a mount in progress task, too.
  1954. */
  1955. nrl = ntfs_mapping_pairs_decompress(vol, a, ni->runlist.rl);
  1956. if (IS_ERR(nrl)) {
  1957. ntfs_error(sb, "ntfs_mapping_pairs_decompress() "
  1958. "failed with error code %ld. $MFT is "
  1959. "corrupt.", PTR_ERR(nrl));
  1960. goto put_err_out;
  1961. }
  1962. ni->runlist.rl = nrl;
  1963. /* Are we in the first extent? */
  1964. if (!next_vcn) {
  1965. if (a->data.non_resident.lowest_vcn) {
  1966. ntfs_error(sb, "First extent of $DATA "
  1967. "attribute has non zero "
  1968. "lowest_vcn. $MFT is corrupt. "
  1969. "You should run chkdsk.");
  1970. goto put_err_out;
  1971. }
  1972. /* Get the last vcn in the $DATA attribute. */
  1973. last_vcn = sle64_to_cpu(
  1974. a->data.non_resident.allocated_size)
  1975. >> vol->cluster_size_bits;
  1976. /* Fill in the inode size. */
  1977. vi->i_size = sle64_to_cpu(
  1978. a->data.non_resident.data_size);
  1979. ni->initialized_size = sle64_to_cpu(
  1980. a->data.non_resident.initialized_size);
  1981. ni->allocated_size = sle64_to_cpu(
  1982. a->data.non_resident.allocated_size);
  1983. /*
  1984. * Verify the number of mft records does not exceed
  1985. * 2^32 - 1.
  1986. */
  1987. if ((vi->i_size >> vol->mft_record_size_bits) >=
  1988. (1ULL << 32)) {
  1989. ntfs_error(sb, "$MFT is too big! Aborting.");
  1990. goto put_err_out;
  1991. }
  1992. /*
  1993. * We have got the first extent of the runlist for
  1994. * $MFT which means it is now relatively safe to call
  1995. * the normal ntfs_read_inode() function.
  1996. * Complete reading the inode, this will actually
  1997. * re-read the mft record for $MFT, this time entering
  1998. * it into the page cache with which we complete the
  1999. * kick start of the volume. It should be safe to do
  2000. * this now as the first extent of $MFT/$DATA is
  2001. * already known and we would hope that we don't need
  2002. * further extents in order to find the other
  2003. * attributes belonging to $MFT. Only time will tell if
  2004. * this is really the case. If not we will have to play
  2005. * magic at this point, possibly duplicating a lot of
  2006. * ntfs_read_inode() at this point. We will need to
  2007. * ensure we do enough of its work to be able to call
  2008. * ntfs_read_inode() on extents of $MFT/$DATA. But lets
  2009. * hope this never happens...
  2010. */
  2011. ntfs_read_locked_inode(vi);
  2012. if (is_bad_inode(vi)) {
  2013. ntfs_error(sb, "ntfs_read_inode() of $MFT "
  2014. "failed. BUG or corrupt $MFT. "
  2015. "Run chkdsk and if no errors "
  2016. "are found, please report you "
  2017. "saw this message to "
  2018. "linux-ntfs-dev@lists."
  2019. "sourceforge.net");
  2020. ntfs_attr_put_search_ctx(ctx);
  2021. /* Revert to the safe super operations. */
  2022. ntfs_free(m);
  2023. return -1;
  2024. }
  2025. /*
  2026. * Re-initialize some specifics about $MFT's inode as
  2027. * ntfs_read_inode() will have set up the default ones.
  2028. */
  2029. /* Set uid and gid to root. */
  2030. vi->i_uid = vi->i_gid = 0;
  2031. /* Regular file. No access for anyone. */
  2032. vi->i_mode = S_IFREG;
  2033. /* No VFS initiated operations allowed for $MFT. */
  2034. vi->i_op = &ntfs_empty_inode_ops;
  2035. vi->i_fop = &ntfs_empty_file_ops;
  2036. }
  2037. /* Get the lowest vcn for the next extent. */
  2038. highest_vcn = sle64_to_cpu(a->data.non_resident.highest_vcn);
  2039. next_vcn = highest_vcn + 1;
  2040. /* Only one extent or error, which we catch below. */
  2041. if (next_vcn <= 0)
  2042. break;
  2043. /* Avoid endless loops due to corruption. */
  2044. if (next_vcn < sle64_to_cpu(
  2045. a->data.non_resident.lowest_vcn)) {
  2046. ntfs_error(sb, "$MFT has corrupt attribute list "
  2047. "attribute. Run chkdsk.");
  2048. goto put_err_out;
  2049. }
  2050. }
  2051. if (err != -ENOENT) {
  2052. ntfs_error(sb, "Failed to lookup $MFT/$DATA attribute extent. "
  2053. "$MFT is corrupt. Run chkdsk.");
  2054. goto put_err_out;
  2055. }
  2056. if (!a) {
  2057. ntfs_error(sb, "$MFT/$DATA attribute not found. $MFT is "
  2058. "corrupt. Run chkdsk.");
  2059. goto put_err_out;
  2060. }
  2061. if (highest_vcn && highest_vcn != last_vcn - 1) {
  2062. ntfs_error(sb, "Failed to load the complete runlist for "
  2063. "$MFT/$DATA. Driver bug or corrupt $MFT. "
  2064. "Run chkdsk.");
  2065. ntfs_debug("highest_vcn = 0x%llx, last_vcn - 1 = 0x%llx",
  2066. (unsigned long long)highest_vcn,
  2067. (unsigned long long)last_vcn - 1);
  2068. goto put_err_out;
  2069. }
  2070. ntfs_attr_put_search_ctx(ctx);
  2071. ntfs_debug("Done.");
  2072. ntfs_free(m);
  2073. /*
  2074. * Split the locking rules of the MFT inode from the
  2075. * locking rules of other inodes:
  2076. */
  2077. lockdep_set_class(&ni->runlist.lock, &mft_ni_runlist_lock_key);
  2078. lockdep_set_class(&ni->mrec_lock, &mft_ni_mrec_lock_key);
  2079. return 0;
  2080. em_put_err_out:
  2081. ntfs_error(sb, "Couldn't find first extent of $DATA attribute in "
  2082. "attribute list. $MFT is corrupt. Run chkdsk.");
  2083. put_err_out:
  2084. ntfs_attr_put_search_ctx(ctx);
  2085. err_out:
  2086. ntfs_error(sb, "Failed. Marking inode as bad.");
  2087. make_bad_inode(vi);
  2088. ntfs_free(m);
  2089. return -1;
  2090. }
  2091. static void __ntfs_clear_inode(ntfs_inode *ni)
  2092. {
  2093. /* Free all alocated memory. */
  2094. down_write(&ni->runlist.lock);
  2095. if (ni->runlist.rl) {
  2096. ntfs_free(ni->runlist.rl);
  2097. ni->runlist.rl = NULL;
  2098. }
  2099. up_write(&ni->runlist.lock);
  2100. if (ni->attr_list) {
  2101. ntfs_free(ni->attr_list);
  2102. ni->attr_list = NULL;
  2103. }
  2104. down_write(&ni->attr_list_rl.lock);
  2105. if (ni->attr_list_rl.rl) {
  2106. ntfs_free(ni->attr_list_rl.rl);
  2107. ni->attr_list_rl.rl = NULL;
  2108. }
  2109. up_write(&ni->attr_list_rl.lock);
  2110. if (ni->name_len && ni->name != I30) {
  2111. /* Catch bugs... */
  2112. BUG_ON(!ni->name);
  2113. kfree(ni->name);
  2114. }
  2115. }
  2116. void ntfs_clear_extent_inode(ntfs_inode *ni)
  2117. {
  2118. ntfs_debug("Entering for inode 0x%lx.", ni->mft_no);
  2119. BUG_ON(NInoAttr(ni));
  2120. BUG_ON(ni->nr_extents != -1);
  2121. #ifdef NTFS_RW
  2122. if (NInoDirty(ni)) {
  2123. if (!is_bad_inode(VFS_I(ni->ext.base_ntfs_ino)))
  2124. ntfs_error(ni->vol->sb, "Clearing dirty extent inode! "
  2125. "Losing data! This is a BUG!!!");
  2126. // FIXME: Do something!!!
  2127. }
  2128. #endif /* NTFS_RW */
  2129. __ntfs_clear_inode(ni);
  2130. /* Bye, bye... */
  2131. ntfs_destroy_extent_inode(ni);
  2132. }
  2133. /**
  2134. * ntfs_clear_big_inode - clean up the ntfs specific part of an inode
  2135. * @vi: vfs inode pending annihilation
  2136. *
  2137. * When the VFS is going to remove an inode from memory, ntfs_clear_big_inode()
  2138. * is called, which deallocates all memory belonging to the NTFS specific part
  2139. * of the inode and returns.
  2140. *
  2141. * If the MFT record is dirty, we commit it before doing anything else.
  2142. */
  2143. void ntfs_clear_big_inode(struct inode *vi)
  2144. {
  2145. ntfs_inode *ni = NTFS_I(vi);
  2146. #ifdef NTFS_RW
  2147. if (NInoDirty(ni)) {
  2148. bool was_bad = (is_bad_inode(vi));
  2149. /* Committing the inode also commits all extent inodes. */
  2150. ntfs_commit_inode(vi);
  2151. if (!was_bad && (is_bad_inode(vi) || NInoDirty(ni))) {
  2152. ntfs_error(vi->i_sb, "Failed to commit dirty inode "
  2153. "0x%lx. Losing data!", vi->i_ino);
  2154. // FIXME: Do something!!!
  2155. }
  2156. }
  2157. #endif /* NTFS_RW */
  2158. /* No need to lock at this stage as no one else has a reference. */
  2159. if (ni->nr_extents > 0) {
  2160. int i;
  2161. for (i = 0; i < ni->nr_extents; i++)
  2162. ntfs_clear_extent_inode(ni->ext.extent_ntfs_inos[i]);
  2163. kfree(ni->ext.extent_ntfs_inos);
  2164. }
  2165. __ntfs_clear_inode(ni);
  2166. if (NInoAttr(ni)) {
  2167. /* Release the base inode if we are holding it. */
  2168. if (ni->nr_extents == -1) {
  2169. iput(VFS_I(ni->ext.base_ntfs_ino));
  2170. ni->nr_extents = 0;
  2171. ni->ext.base_ntfs_ino = NULL;
  2172. }
  2173. }
  2174. return;
  2175. }
  2176. /**
  2177. * ntfs_show_options - show mount options in /proc/mounts
  2178. * @sf: seq_file in which to write our mount options
  2179. * @mnt: vfs mount whose mount options to display
  2180. *
  2181. * Called by the VFS once for each mounted ntfs volume when someone reads
  2182. * /proc/mounts in order to display the NTFS specific mount options of each
  2183. * mount. The mount options of the vfs mount @mnt are written to the seq file
  2184. * @sf and success is returned.
  2185. */
  2186. int ntfs_show_options(struct seq_file *sf, struct vfsmount *mnt)
  2187. {
  2188. ntfs_volume *vol = NTFS_SB(mnt->mnt_sb);
  2189. int i;
  2190. seq_printf(sf, ",uid=%i", vol->uid);
  2191. seq_printf(sf, ",gid=%i", vol->gid);
  2192. if (vol->fmask == vol->dmask)
  2193. seq_printf(sf, ",umask=0%o", vol->fmask);
  2194. else {
  2195. seq_printf(sf, ",fmask=0%o", vol->fmask);
  2196. seq_printf(sf, ",dmask=0%o", vol->dmask);
  2197. }
  2198. seq_printf(sf, ",nls=%s", vol->nls_map->charset);
  2199. if (NVolCaseSensitive(vol))
  2200. seq_printf(sf, ",case_sensitive");
  2201. if (NVolShowSystemFiles(vol))
  2202. seq_printf(sf, ",show_sys_files");
  2203. if (!NVolSparseEnabled(vol))
  2204. seq_printf(sf, ",disable_sparse");
  2205. for (i = 0; on_errors_arr[i].val; i++) {
  2206. if (on_errors_arr[i].val & vol->on_errors)
  2207. seq_printf(sf, ",errors=%s", on_errors_arr[i].str);
  2208. }
  2209. seq_printf(sf, ",mft_zone_multiplier=%i", vol->mft_zone_multiplier);
  2210. return 0;
  2211. }
  2212. #ifdef NTFS_RW
  2213. static const char *es = " Leaving inconsistent metadata. Unmount and run "
  2214. "chkdsk.";
  2215. /**
  2216. * ntfs_truncate - called when the i_size of an ntfs inode is changed
  2217. * @vi: inode for which the i_size was changed
  2218. *
  2219. * We only support i_size changes for normal files at present, i.e. not
  2220. * compressed and not encrypted. This is enforced in ntfs_setattr(), see
  2221. * below.
  2222. *
  2223. * The kernel guarantees that @vi is a regular file (S_ISREG() is true) and
  2224. * that the change is allowed.
  2225. *
  2226. * This implies for us that @vi is a file inode rather than a directory, index,
  2227. * or attribute inode as well as that @vi is a base inode.
  2228. *
  2229. * Returns 0 on success or -errno on error.
  2230. *
  2231. * Called with ->i_mutex held. In all but one case ->i_alloc_sem is held for
  2232. * writing. The only case in the kernel where ->i_alloc_sem is not held is
  2233. * mm/filemap.c::generic_file_buffered_write() where vmtruncate() is called
  2234. * with the current i_size as the offset. The analogous place in NTFS is in
  2235. * fs/ntfs/file.c::ntfs_file_buffered_write() where we call vmtruncate() again
  2236. * without holding ->i_alloc_sem.
  2237. */
  2238. int ntfs_truncate(struct inode *vi)
  2239. {
  2240. s64 new_size, old_size, nr_freed, new_alloc_size, old_alloc_size;
  2241. VCN highest_vcn;
  2242. unsigned long flags;
  2243. ntfs_inode *base_ni, *ni = NTFS_I(vi);
  2244. ntfs_volume *vol = ni->vol;
  2245. ntfs_attr_search_ctx *ctx;
  2246. MFT_RECORD *m;
  2247. ATTR_RECORD *a;
  2248. const char *te = " Leaving file length out of sync with i_size.";
  2249. int err, mp_size, size_change, alloc_change;
  2250. u32 attr_len;
  2251. ntfs_debug("Entering for inode 0x%lx.", vi->i_ino);
  2252. BUG_ON(NInoAttr(ni));
  2253. BUG_ON(S_ISDIR(vi->i_mode));
  2254. BUG_ON(NInoMstProtected(ni));
  2255. BUG_ON(ni->nr_extents < 0);
  2256. retry_truncate:
  2257. /*
  2258. * Lock the runlist for writing and map the mft record to ensure it is
  2259. * safe to mess with the attribute runlist and sizes.
  2260. */
  2261. down_write(&ni->runlist.lock);
  2262. if (!NInoAttr(ni))
  2263. base_ni = ni;
  2264. else
  2265. base_ni = ni->ext.base_ntfs_ino;
  2266. m = map_mft_record(base_ni);
  2267. if (IS_ERR(m)) {
  2268. err = PTR_ERR(m);
  2269. ntfs_error(vi->i_sb, "Failed to map mft record for inode 0x%lx "
  2270. "(error code %d).%s", vi->i_ino, err, te);
  2271. ctx = NULL;
  2272. m = NULL;
  2273. goto old_bad_out;
  2274. }
  2275. ctx = ntfs_attr_get_search_ctx(base_ni, m);
  2276. if (unlikely(!ctx)) {
  2277. ntfs_error(vi->i_sb, "Failed to allocate a search context for "
  2278. "inode 0x%lx (not enough memory).%s",
  2279. vi->i_ino, te);
  2280. err = -ENOMEM;
  2281. goto old_bad_out;
  2282. }
  2283. err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
  2284. CASE_SENSITIVE, 0, NULL, 0, ctx);
  2285. if (unlikely(err)) {
  2286. if (err == -ENOENT) {
  2287. ntfs_error(vi->i_sb, "Open attribute is missing from "
  2288. "mft record. Inode 0x%lx is corrupt. "
  2289. "Run chkdsk.%s", vi->i_ino, te);
  2290. err = -EIO;
  2291. } else
  2292. ntfs_error(vi->i_sb, "Failed to lookup attribute in "
  2293. "inode 0x%lx (error code %d).%s",
  2294. vi->i_ino, err, te);
  2295. goto old_bad_out;
  2296. }
  2297. m = ctx->mrec;
  2298. a = ctx->attr;
  2299. /*
  2300. * The i_size of the vfs inode is the new size for the attribute value.
  2301. */
  2302. new_size = i_size_read(vi);
  2303. /* The current size of the attribute value is the old size. */
  2304. old_size = ntfs_attr_size(a);
  2305. /* Calculate the new allocated size. */
  2306. if (NInoNonResident(ni))
  2307. new_alloc_size = (new_size + vol->cluster_size - 1) &
  2308. ~(s64)vol->cluster_size_mask;
  2309. else
  2310. new_alloc_size = (new_size + 7) & ~7;
  2311. /* The current allocated size is the old allocated size. */
  2312. read_lock_irqsave(&ni->size_lock, flags);
  2313. old_alloc_size = ni->allocated_size;
  2314. read_unlock_irqrestore(&ni->size_lock, flags);
  2315. /*
  2316. * The change in the file size. This will be 0 if no change, >0 if the
  2317. * size is growing, and <0 if the size is shrinking.
  2318. */
  2319. size_change = -1;
  2320. if (new_size - old_size >= 0) {
  2321. size_change = 1;
  2322. if (new_size == old_size)
  2323. size_change = 0;
  2324. }
  2325. /* As above for the allocated size. */
  2326. alloc_change = -1;
  2327. if (new_alloc_size - old_alloc_size >= 0) {
  2328. alloc_change = 1;
  2329. if (new_alloc_size == old_alloc_size)
  2330. alloc_change = 0;
  2331. }
  2332. /*
  2333. * If neither the size nor the allocation are being changed there is
  2334. * nothing to do.
  2335. */
  2336. if (!size_change && !alloc_change)
  2337. goto unm_done;
  2338. /* If the size is changing, check if new size is allowed in $AttrDef. */
  2339. if (size_change) {
  2340. err = ntfs_attr_size_bounds_check(vol, ni->type, new_size);
  2341. if (unlikely(err)) {
  2342. if (err == -ERANGE) {
  2343. ntfs_error(vol->sb, "Truncate would cause the "
  2344. "inode 0x%lx to %simum size "
  2345. "for its attribute type "
  2346. "(0x%x). Aborting truncate.",
  2347. vi->i_ino,
  2348. new_size > old_size ? "exceed "
  2349. "the max" : "go under the min",
  2350. le32_to_cpu(ni->type));
  2351. err = -EFBIG;
  2352. } else {
  2353. ntfs_error(vol->sb, "Inode 0x%lx has unknown "
  2354. "attribute type 0x%x. "
  2355. "Aborting truncate.",
  2356. vi->i_ino,
  2357. le32_to_cpu(ni->type));
  2358. err = -EIO;
  2359. }
  2360. /* Reset the vfs inode size to the old size. */
  2361. i_size_write(vi, old_size);
  2362. goto err_out;
  2363. }
  2364. }
  2365. if (NInoCompressed(ni) || NInoEncrypted(ni)) {
  2366. ntfs_warning(vi->i_sb, "Changes in inode size are not "
  2367. "supported yet for %s files, ignoring.",
  2368. NInoCompressed(ni) ? "compressed" :
  2369. "encrypted");
  2370. err = -EOPNOTSUPP;
  2371. goto bad_out;
  2372. }
  2373. if (a->non_resident)
  2374. goto do_non_resident_truncate;
  2375. BUG_ON(NInoNonResident(ni));
  2376. /* Resize the attribute record to best fit the new attribute size. */
  2377. if (new_size < vol->mft_record_size &&
  2378. !ntfs_resident_attr_value_resize(m, a, new_size)) {
  2379. unsigned long flags;
  2380. /* The resize succeeded! */
  2381. flush_dcache_mft_record_page(ctx->ntfs_ino);
  2382. mark_mft_record_dirty(ctx->ntfs_ino);
  2383. write_lock_irqsave(&ni->size_lock, flags);
  2384. /* Update the sizes in the ntfs inode and all is done. */
  2385. ni->allocated_size = le32_to_cpu(a->length) -
  2386. le16_to_cpu(a->data.resident.value_offset);
  2387. /*
  2388. * Note ntfs_resident_attr_value_resize() has already done any
  2389. * necessary data clearing in the attribute record. When the
  2390. * file is being shrunk vmtruncate() will already have cleared
  2391. * the top part of the last partial page, i.e. since this is
  2392. * the resident case this is the page with index 0. However,
  2393. * when the file is being expanded, the page cache page data
  2394. * between the old data_size, i.e. old_size, and the new_size
  2395. * has not been zeroed. Fortunately, we do not need to zero it
  2396. * either since on one hand it will either already be zero due
  2397. * to both readpage and writepage clearing partial page data
  2398. * beyond i_size in which case there is nothing to do or in the
  2399. * case of the file being mmap()ped at the same time, POSIX
  2400. * specifies that the behaviour is unspecified thus we do not
  2401. * have to do anything. This means that in our implementation
  2402. * in the rare case that the file is mmap()ped and a write
  2403. * occured into the mmap()ped region just beyond the file size
  2404. * and writepage has not yet been called to write out the page
  2405. * (which would clear the area beyond the file size) and we now
  2406. * extend the file size to incorporate this dirty region
  2407. * outside the file size, a write of the page would result in
  2408. * this data being written to disk instead of being cleared.
  2409. * Given both POSIX and the Linux mmap(2) man page specify that
  2410. * this corner case is undefined, we choose to leave it like
  2411. * that as this is much simpler for us as we cannot lock the
  2412. * relevant page now since we are holding too many ntfs locks
  2413. * which would result in a lock reversal deadlock.
  2414. */
  2415. ni->initialized_size = new_size;
  2416. write_unlock_irqrestore(&ni->size_lock, flags);
  2417. goto unm_done;
  2418. }
  2419. /* If the above resize failed, this must be an attribute extension. */
  2420. BUG_ON(size_change < 0);
  2421. /*
  2422. * We have to drop all the locks so we can call
  2423. * ntfs_attr_make_non_resident(). This could be optimised by try-
  2424. * locking the first page cache page and only if that fails dropping
  2425. * the locks, locking the page, and redoing all the locking and
  2426. * lookups. While this would be a huge optimisation, it is not worth
  2427. * it as this is definitely a slow code path as it only ever can happen
  2428. * once for any given file.
  2429. */
  2430. ntfs_attr_put_search_ctx(ctx);
  2431. unmap_mft_record(base_ni);
  2432. up_write(&ni->runlist.lock);
  2433. /*
  2434. * Not enough space in the mft record, try to make the attribute
  2435. * non-resident and if successful restart the truncation process.
  2436. */
  2437. err = ntfs_attr_make_non_resident(ni, old_size);
  2438. if (likely(!err))
  2439. goto retry_truncate;
  2440. /*
  2441. * Could not make non-resident. If this is due to this not being
  2442. * permitted for this attribute type or there not being enough space,
  2443. * try to make other attributes non-resident. Otherwise fail.
  2444. */
  2445. if (unlikely(err != -EPERM && err != -ENOSPC)) {
  2446. ntfs_error(vol->sb, "Cannot truncate inode 0x%lx, attribute "
  2447. "type 0x%x, because the conversion from "
  2448. "resident to non-resident attribute failed "
  2449. "with error code %i.", vi->i_ino,
  2450. (unsigned)le32_to_cpu(ni->type), err);
  2451. if (err != -ENOMEM)
  2452. err = -EIO;
  2453. goto conv_err_out;
  2454. }
  2455. /* TODO: Not implemented from here, abort. */
  2456. if (err == -ENOSPC)
  2457. ntfs_error(vol->sb, "Not enough space in the mft record/on "
  2458. "disk for the non-resident attribute value. "
  2459. "This case is not implemented yet.");
  2460. else /* if (err == -EPERM) */
  2461. ntfs_error(vol->sb, "This attribute type may not be "
  2462. "non-resident. This case is not implemented "
  2463. "yet.");
  2464. err = -EOPNOTSUPP;
  2465. goto conv_err_out;
  2466. #if 0
  2467. // TODO: Attempt to make other attributes non-resident.
  2468. if (!err)
  2469. goto do_resident_extend;
  2470. /*
  2471. * Both the attribute list attribute and the standard information
  2472. * attribute must remain in the base inode. Thus, if this is one of
  2473. * these attributes, we have to try to move other attributes out into
  2474. * extent mft records instead.
  2475. */
  2476. if (ni->type == AT_ATTRIBUTE_LIST ||
  2477. ni->type == AT_STANDARD_INFORMATION) {
  2478. // TODO: Attempt to move other attributes into extent mft
  2479. // records.
  2480. err = -EOPNOTSUPP;
  2481. if (!err)
  2482. goto do_resident_extend;
  2483. goto err_out;
  2484. }
  2485. // TODO: Attempt to move this attribute to an extent mft record, but
  2486. // only if it is not already the only attribute in an mft record in
  2487. // which case there would be nothing to gain.
  2488. err = -EOPNOTSUPP;
  2489. if (!err)
  2490. goto do_resident_extend;
  2491. /* There is nothing we can do to make enough space. )-: */
  2492. goto err_out;
  2493. #endif
  2494. do_non_resident_truncate:
  2495. BUG_ON(!NInoNonResident(ni));
  2496. if (alloc_change < 0) {
  2497. highest_vcn = sle64_to_cpu(a->data.non_resident.highest_vcn);
  2498. if (highest_vcn > 0 &&
  2499. old_alloc_size >> vol->cluster_size_bits >
  2500. highest_vcn + 1) {
  2501. /*
  2502. * This attribute has multiple extents. Not yet
  2503. * supported.
  2504. */
  2505. ntfs_error(vol->sb, "Cannot truncate inode 0x%lx, "
  2506. "attribute type 0x%x, because the "
  2507. "attribute is highly fragmented (it "
  2508. "consists of multiple extents) and "
  2509. "this case is not implemented yet.",
  2510. vi->i_ino,
  2511. (unsigned)le32_to_cpu(ni->type));
  2512. err = -EOPNOTSUPP;
  2513. goto bad_out;
  2514. }
  2515. }
  2516. /*
  2517. * If the size is shrinking, need to reduce the initialized_size and
  2518. * the data_size before reducing the allocation.
  2519. */
  2520. if (size_change < 0) {
  2521. /*
  2522. * Make the valid size smaller (i_size is already up-to-date).
  2523. */
  2524. write_lock_irqsave(&ni->size_lock, flags);
  2525. if (new_size < ni->initialized_size) {
  2526. ni->initialized_size = new_size;
  2527. a->data.non_resident.initialized_size =
  2528. cpu_to_sle64(new_size);
  2529. }
  2530. a->data.non_resident.data_size = cpu_to_sle64(new_size);
  2531. write_unlock_irqrestore(&ni->size_lock, flags);
  2532. flush_dcache_mft_record_page(ctx->ntfs_ino);
  2533. mark_mft_record_dirty(ctx->ntfs_ino);
  2534. /* If the allocated size is not changing, we are done. */
  2535. if (!alloc_change)
  2536. goto unm_done;
  2537. /*
  2538. * If the size is shrinking it makes no sense for the
  2539. * allocation to be growing.
  2540. */
  2541. BUG_ON(alloc_change > 0);
  2542. } else /* if (size_change >= 0) */ {
  2543. /*
  2544. * The file size is growing or staying the same but the
  2545. * allocation can be shrinking, growing or staying the same.
  2546. */
  2547. if (alloc_change > 0) {
  2548. /*
  2549. * We need to extend the allocation and possibly update
  2550. * the data size. If we are updating the data size,
  2551. * since we are not touching the initialized_size we do
  2552. * not need to worry about the actual data on disk.
  2553. * And as far as the page cache is concerned, there
  2554. * will be no pages beyond the old data size and any
  2555. * partial region in the last page between the old and
  2556. * new data size (or the end of the page if the new
  2557. * data size is outside the page) does not need to be
  2558. * modified as explained above for the resident
  2559. * attribute truncate case. To do this, we simply drop
  2560. * the locks we hold and leave all the work to our
  2561. * friendly helper ntfs_attr_extend_allocation().
  2562. */
  2563. ntfs_attr_put_search_ctx(ctx);
  2564. unmap_mft_record(base_ni);
  2565. up_write(&ni->runlist.lock);
  2566. err = ntfs_attr_extend_allocation(ni, new_size,
  2567. size_change > 0 ? new_size : -1, -1);
  2568. /*
  2569. * ntfs_attr_extend_allocation() will have done error
  2570. * output already.
  2571. */
  2572. goto done;
  2573. }
  2574. if (!alloc_change)
  2575. goto alloc_done;
  2576. }
  2577. /* alloc_change < 0 */
  2578. /* Free the clusters. */
  2579. nr_freed = ntfs_cluster_free(ni, new_alloc_size >>
  2580. vol->cluster_size_bits, -1, ctx);
  2581. m = ctx->mrec;
  2582. a = ctx->attr;
  2583. if (unlikely(nr_freed < 0)) {
  2584. ntfs_error(vol->sb, "Failed to release cluster(s) (error code "
  2585. "%lli). Unmount and run chkdsk to recover "
  2586. "the lost cluster(s).", (long long)nr_freed);
  2587. NVolSetErrors(vol);
  2588. nr_freed = 0;
  2589. }
  2590. /* Truncate the runlist. */
  2591. err = ntfs_rl_truncate_nolock(vol, &ni->runlist,
  2592. new_alloc_size >> vol->cluster_size_bits);
  2593. /*
  2594. * If the runlist truncation failed and/or the search context is no
  2595. * longer valid, we cannot resize the attribute record or build the
  2596. * mapping pairs array thus we mark the inode bad so that no access to
  2597. * the freed clusters can happen.
  2598. */
  2599. if (unlikely(err || IS_ERR(m))) {
  2600. ntfs_error(vol->sb, "Failed to %s (error code %li).%s",
  2601. IS_ERR(m) ?
  2602. "restore attribute search context" :
  2603. "truncate attribute runlist",
  2604. IS_ERR(m) ? PTR_ERR(m) : err, es);
  2605. err = -EIO;
  2606. goto bad_out;
  2607. }
  2608. /* Get the size for the shrunk mapping pairs array for the runlist. */
  2609. mp_size = ntfs_get_size_for_mapping_pairs(vol, ni->runlist.rl, 0, -1);
  2610. if (unlikely(mp_size <= 0)) {
  2611. ntfs_error(vol->sb, "Cannot shrink allocation of inode 0x%lx, "
  2612. "attribute type 0x%x, because determining the "
  2613. "size for the mapping pairs failed with error "
  2614. "code %i.%s", vi->i_ino,
  2615. (unsigned)le32_to_cpu(ni->type), mp_size, es);
  2616. err = -EIO;
  2617. goto bad_out;
  2618. }
  2619. /*
  2620. * Shrink the attribute record for the new mapping pairs array. Note,
  2621. * this cannot fail since we are making the attribute smaller thus by
  2622. * definition there is enough space to do so.
  2623. */
  2624. attr_len = le32_to_cpu(a->length);
  2625. err = ntfs_attr_record_resize(m, a, mp_size +
  2626. le16_to_cpu(a->data.non_resident.mapping_pairs_offset));
  2627. BUG_ON(err);
  2628. /*
  2629. * Generate the mapping pairs array directly into the attribute record.
  2630. */
  2631. err = ntfs_mapping_pairs_build(vol, (u8*)a +
  2632. le16_to_cpu(a->data.non_resident.mapping_pairs_offset),
  2633. mp_size, ni->runlist.rl, 0, -1, NULL);
  2634. if (unlikely(err)) {
  2635. ntfs_error(vol->sb, "Cannot shrink allocation of inode 0x%lx, "
  2636. "attribute type 0x%x, because building the "
  2637. "mapping pairs failed with error code %i.%s",
  2638. vi->i_ino, (unsigned)le32_to_cpu(ni->type),
  2639. err, es);
  2640. err = -EIO;
  2641. goto bad_out;
  2642. }
  2643. /* Update the allocated/compressed size as well as the highest vcn. */
  2644. a->data.non_resident.highest_vcn = cpu_to_sle64((new_alloc_size >>
  2645. vol->cluster_size_bits) - 1);
  2646. write_lock_irqsave(&ni->size_lock, flags);
  2647. ni->allocated_size = new_alloc_size;
  2648. a->data.non_resident.allocated_size = cpu_to_sle64(new_alloc_size);
  2649. if (NInoSparse(ni) || NInoCompressed(ni)) {
  2650. if (nr_freed) {
  2651. ni->itype.compressed.size -= nr_freed <<
  2652. vol->cluster_size_bits;
  2653. BUG_ON(ni->itype.compressed.size < 0);
  2654. a->data.non_resident.compressed_size = cpu_to_sle64(
  2655. ni->itype.compressed.size);
  2656. vi->i_blocks = ni->itype.compressed.size >> 9;
  2657. }
  2658. } else
  2659. vi->i_blocks = new_alloc_size >> 9;
  2660. write_unlock_irqrestore(&ni->size_lock, flags);
  2661. /*
  2662. * We have shrunk the allocation. If this is a shrinking truncate we
  2663. * have already dealt with the initialized_size and the data_size above
  2664. * and we are done. If the truncate is only changing the allocation
  2665. * and not the data_size, we are also done. If this is an extending
  2666. * truncate, need to extend the data_size now which is ensured by the
  2667. * fact that @size_change is positive.
  2668. */
  2669. alloc_done:
  2670. /*
  2671. * If the size is growing, need to update it now. If it is shrinking,
  2672. * we have already updated it above (before the allocation change).
  2673. */
  2674. if (size_change > 0)
  2675. a->data.non_resident.data_size = cpu_to_sle64(new_size);
  2676. /* Ensure the modified mft record is written out. */
  2677. flush_dcache_mft_record_page(ctx->ntfs_ino);
  2678. mark_mft_record_dirty(ctx->ntfs_ino);
  2679. unm_done:
  2680. ntfs_attr_put_search_ctx(ctx);
  2681. unmap_mft_record(base_ni);
  2682. up_write(&ni->runlist.lock);
  2683. done:
  2684. /* Update the mtime and ctime on the base inode. */
  2685. /* normally ->truncate shouldn't update ctime or mtime,
  2686. * but ntfs did before so it got a copy & paste version
  2687. * of file_update_time. one day someone should fix this
  2688. * for real.
  2689. */
  2690. if (!IS_NOCMTIME(VFS_I(base_ni)) && !IS_RDONLY(VFS_I(base_ni))) {
  2691. struct timespec now = current_fs_time(VFS_I(base_ni)->i_sb);
  2692. int sync_it = 0;
  2693. if (!timespec_equal(&VFS_I(base_ni)->i_mtime, &now) ||
  2694. !timespec_equal(&VFS_I(base_ni)->i_ctime, &now))
  2695. sync_it = 1;
  2696. VFS_I(base_ni)->i_mtime = now;
  2697. VFS_I(base_ni)->i_ctime = now;
  2698. if (sync_it)
  2699. mark_inode_dirty_sync(VFS_I(base_ni));
  2700. }
  2701. if (likely(!err)) {
  2702. NInoClearTruncateFailed(ni);
  2703. ntfs_debug("Done.");
  2704. }
  2705. return err;
  2706. old_bad_out:
  2707. old_size = -1;
  2708. bad_out:
  2709. if (err != -ENOMEM && err != -EOPNOTSUPP)
  2710. NVolSetErrors(vol);
  2711. if (err != -EOPNOTSUPP)
  2712. NInoSetTruncateFailed(ni);
  2713. else if (old_size >= 0)
  2714. i_size_write(vi, old_size);
  2715. err_out:
  2716. if (ctx)
  2717. ntfs_attr_put_search_ctx(ctx);
  2718. if (m)
  2719. unmap_mft_record(base_ni);
  2720. up_write(&ni->runlist.lock);
  2721. out:
  2722. ntfs_debug("Failed. Returning error code %i.", err);
  2723. return err;
  2724. conv_err_out:
  2725. if (err != -ENOMEM && err != -EOPNOTSUPP)
  2726. NVolSetErrors(vol);
  2727. if (err != -EOPNOTSUPP)
  2728. NInoSetTruncateFailed(ni);
  2729. else
  2730. i_size_write(vi, old_size);
  2731. goto out;
  2732. }
  2733. /**
  2734. * ntfs_truncate_vfs - wrapper for ntfs_truncate() that has no return value
  2735. * @vi: inode for which the i_size was changed
  2736. *
  2737. * Wrapper for ntfs_truncate() that has no return value.
  2738. *
  2739. * See ntfs_truncate() description above for details.
  2740. */
  2741. void ntfs_truncate_vfs(struct inode *vi) {
  2742. ntfs_truncate(vi);
  2743. }
  2744. /**
  2745. * ntfs_setattr - called from notify_change() when an attribute is being changed
  2746. * @dentry: dentry whose attributes to change
  2747. * @attr: structure describing the attributes and the changes
  2748. *
  2749. * We have to trap VFS attempts to truncate the file described by @dentry as
  2750. * soon as possible, because we do not implement changes in i_size yet. So we
  2751. * abort all i_size changes here.
  2752. *
  2753. * We also abort all changes of user, group, and mode as we do not implement
  2754. * the NTFS ACLs yet.
  2755. *
  2756. * Called with ->i_mutex held. For the ATTR_SIZE (i.e. ->truncate) case, also
  2757. * called with ->i_alloc_sem held for writing.
  2758. *
  2759. * Basically this is a copy of generic notify_change() and inode_setattr()
  2760. * functionality, except we intercept and abort changes in i_size.
  2761. */
  2762. int ntfs_setattr(struct dentry *dentry, struct iattr *attr)
  2763. {
  2764. struct inode *vi = dentry->d_inode;
  2765. int err;
  2766. unsigned int ia_valid = attr->ia_valid;
  2767. err = inode_change_ok(vi, attr);
  2768. if (err)
  2769. goto out;
  2770. /* We do not support NTFS ACLs yet. */
  2771. if (ia_valid & (ATTR_UID | ATTR_GID | ATTR_MODE)) {
  2772. ntfs_warning(vi->i_sb, "Changes in user/group/mode are not "
  2773. "supported yet, ignoring.");
  2774. err = -EOPNOTSUPP;
  2775. goto out;
  2776. }
  2777. if (ia_valid & ATTR_SIZE) {
  2778. if (attr->ia_size != i_size_read(vi)) {
  2779. ntfs_inode *ni = NTFS_I(vi);
  2780. /*
  2781. * FIXME: For now we do not support resizing of
  2782. * compressed or encrypted files yet.
  2783. */
  2784. if (NInoCompressed(ni) || NInoEncrypted(ni)) {
  2785. ntfs_warning(vi->i_sb, "Changes in inode size "
  2786. "are not supported yet for "
  2787. "%s files, ignoring.",
  2788. NInoCompressed(ni) ?
  2789. "compressed" : "encrypted");
  2790. err = -EOPNOTSUPP;
  2791. } else
  2792. err = vmtruncate(vi, attr->ia_size);
  2793. if (err || ia_valid == ATTR_SIZE)
  2794. goto out;
  2795. } else {
  2796. /*
  2797. * We skipped the truncate but must still update
  2798. * timestamps.
  2799. */
  2800. ia_valid |= ATTR_MTIME | ATTR_CTIME;
  2801. }
  2802. }
  2803. if (ia_valid & ATTR_ATIME)
  2804. vi->i_atime = timespec_trunc(attr->ia_atime,
  2805. vi->i_sb->s_time_gran);
  2806. if (ia_valid & ATTR_MTIME)
  2807. vi->i_mtime = timespec_trunc(attr->ia_mtime,
  2808. vi->i_sb->s_time_gran);
  2809. if (ia_valid & ATTR_CTIME)
  2810. vi->i_ctime = timespec_trunc(attr->ia_ctime,
  2811. vi->i_sb->s_time_gran);
  2812. mark_inode_dirty(vi);
  2813. out:
  2814. return err;
  2815. }
  2816. /**
  2817. * ntfs_write_inode - write out a dirty inode
  2818. * @vi: inode to write out
  2819. * @sync: if true, write out synchronously
  2820. *
  2821. * Write out a dirty inode to disk including any extent inodes if present.
  2822. *
  2823. * If @sync is true, commit the inode to disk and wait for io completion. This
  2824. * is done using write_mft_record().
  2825. *
  2826. * If @sync is false, just schedule the write to happen but do not wait for i/o
  2827. * completion. In 2.6 kernels, scheduling usually happens just by virtue of
  2828. * marking the page (and in this case mft record) dirty but we do not implement
  2829. * this yet as write_mft_record() largely ignores the @sync parameter and
  2830. * always performs synchronous writes.
  2831. *
  2832. * Return 0 on success and -errno on error.
  2833. */
  2834. int ntfs_write_inode(struct inode *vi, int sync)
  2835. {
  2836. sle64 nt;
  2837. ntfs_inode *ni = NTFS_I(vi);
  2838. ntfs_attr_search_ctx *ctx;
  2839. MFT_RECORD *m;
  2840. STANDARD_INFORMATION *si;
  2841. int err = 0;
  2842. bool modified = false;
  2843. ntfs_debug("Entering for %sinode 0x%lx.", NInoAttr(ni) ? "attr " : "",
  2844. vi->i_ino);
  2845. /*
  2846. * Dirty attribute inodes are written via their real inodes so just
  2847. * clean them here. Access time updates are taken care off when the
  2848. * real inode is written.
  2849. */
  2850. if (NInoAttr(ni)) {
  2851. NInoClearDirty(ni);
  2852. ntfs_debug("Done.");
  2853. return 0;
  2854. }
  2855. /* Map, pin, and lock the mft record belonging to the inode. */
  2856. m = map_mft_record(ni);
  2857. if (IS_ERR(m)) {
  2858. err = PTR_ERR(m);
  2859. goto err_out;
  2860. }
  2861. /* Update the access times in the standard information attribute. */
  2862. ctx = ntfs_attr_get_search_ctx(ni, m);
  2863. if (unlikely(!ctx)) {
  2864. err = -ENOMEM;
  2865. goto unm_err_out;
  2866. }
  2867. err = ntfs_attr_lookup(AT_STANDARD_INFORMATION, NULL, 0,
  2868. CASE_SENSITIVE, 0, NULL, 0, ctx);
  2869. if (unlikely(err)) {
  2870. ntfs_attr_put_search_ctx(ctx);
  2871. goto unm_err_out;
  2872. }
  2873. si = (STANDARD_INFORMATION*)((u8*)ctx->attr +
  2874. le16_to_cpu(ctx->attr->data.resident.value_offset));
  2875. /* Update the access times if they have changed. */
  2876. nt = utc2ntfs(vi->i_mtime);
  2877. if (si->last_data_change_time != nt) {
  2878. ntfs_debug("Updating mtime for inode 0x%lx: old = 0x%llx, "
  2879. "new = 0x%llx", vi->i_ino, (long long)
  2880. sle64_to_cpu(si->last_data_change_time),
  2881. (long long)sle64_to_cpu(nt));
  2882. si->last_data_change_time = nt;
  2883. modified = true;
  2884. }
  2885. nt = utc2ntfs(vi->i_ctime);
  2886. if (si->last_mft_change_time != nt) {
  2887. ntfs_debug("Updating ctime for inode 0x%lx: old = 0x%llx, "
  2888. "new = 0x%llx", vi->i_ino, (long long)
  2889. sle64_to_cpu(si->last_mft_change_time),
  2890. (long long)sle64_to_cpu(nt));
  2891. si->last_mft_change_time = nt;
  2892. modified = true;
  2893. }
  2894. nt = utc2ntfs(vi->i_atime);
  2895. if (si->last_access_time != nt) {
  2896. ntfs_debug("Updating atime for inode 0x%lx: old = 0x%llx, "
  2897. "new = 0x%llx", vi->i_ino,
  2898. (long long)sle64_to_cpu(si->last_access_time),
  2899. (long long)sle64_to_cpu(nt));
  2900. si->last_access_time = nt;
  2901. modified = true;
  2902. }
  2903. /*
  2904. * If we just modified the standard information attribute we need to
  2905. * mark the mft record it is in dirty. We do this manually so that
  2906. * mark_inode_dirty() is not called which would redirty the inode and
  2907. * hence result in an infinite loop of trying to write the inode.
  2908. * There is no need to mark the base inode nor the base mft record
  2909. * dirty, since we are going to write this mft record below in any case
  2910. * and the base mft record may actually not have been modified so it
  2911. * might not need to be written out.
  2912. * NOTE: It is not a problem when the inode for $MFT itself is being
  2913. * written out as mark_ntfs_record_dirty() will only set I_DIRTY_PAGES
  2914. * on the $MFT inode and hence ntfs_write_inode() will not be
  2915. * re-invoked because of it which in turn is ok since the dirtied mft
  2916. * record will be cleaned and written out to disk below, i.e. before
  2917. * this function returns.
  2918. */
  2919. if (modified) {
  2920. flush_dcache_mft_record_page(ctx->ntfs_ino);
  2921. if (!NInoTestSetDirty(ctx->ntfs_ino))
  2922. mark_ntfs_record_dirty(ctx->ntfs_ino->page,
  2923. ctx->ntfs_ino->page_ofs);
  2924. }
  2925. ntfs_attr_put_search_ctx(ctx);
  2926. /* Now the access times are updated, write the base mft record. */
  2927. if (NInoDirty(ni))
  2928. err = write_mft_record(ni, m, sync);
  2929. /* Write all attached extent mft records. */
  2930. mutex_lock(&ni->extent_lock);
  2931. if (ni->nr_extents > 0) {
  2932. ntfs_inode **extent_nis = ni->ext.extent_ntfs_inos;
  2933. int i;
  2934. ntfs_debug("Writing %i extent inodes.", ni->nr_extents);
  2935. for (i = 0; i < ni->nr_extents; i++) {
  2936. ntfs_inode *tni = extent_nis[i];
  2937. if (NInoDirty(tni)) {
  2938. MFT_RECORD *tm = map_mft_record(tni);
  2939. int ret;
  2940. if (IS_ERR(tm)) {
  2941. if (!err || err == -ENOMEM)
  2942. err = PTR_ERR(tm);
  2943. continue;
  2944. }
  2945. ret = write_mft_record(tni, tm, sync);
  2946. unmap_mft_record(tni);
  2947. if (unlikely(ret)) {
  2948. if (!err || err == -ENOMEM)
  2949. err = ret;
  2950. }
  2951. }
  2952. }
  2953. }
  2954. mutex_unlock(&ni->extent_lock);
  2955. unmap_mft_record(ni);
  2956. if (unlikely(err))
  2957. goto err_out;
  2958. ntfs_debug("Done.");
  2959. return 0;
  2960. unm_err_out:
  2961. unmap_mft_record(ni);
  2962. err_out:
  2963. if (err == -ENOMEM) {
  2964. ntfs_warning(vi->i_sb, "Not enough memory to write inode. "
  2965. "Marking the inode dirty again, so the VFS "
  2966. "retries later.");
  2967. mark_inode_dirty(vi);
  2968. } else {
  2969. ntfs_error(vi->i_sb, "Failed (error %i): Run chkdsk.", -err);
  2970. NVolSetErrors(ni->vol);
  2971. }
  2972. return err;
  2973. }
  2974. #endif /* NTFS_RW */