inode.c 96 KB

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