inode.c 80 KB

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