inode.c 97 KB

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