inode.c 97 KB

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