inode.c 97 KB

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