inode.c 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620
  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 (unlikely(!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 (unlikely(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 (unlikely(!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 (unlikely(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 (unlikely(!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 (unlikely(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. loff_t bvi_size;
  690. struct inode *bvi;
  691. ntfs_inode *bni;
  692. INDEX_ROOT *ir;
  693. char *ir_end, *index_end;
  694. /* It is a directory, find index root attribute. */
  695. ntfs_attr_reinit_search_ctx(ctx);
  696. err = ntfs_attr_lookup(AT_INDEX_ROOT, I30, 4, CASE_SENSITIVE,
  697. 0, NULL, 0, ctx);
  698. if (unlikely(err)) {
  699. if (err == -ENOENT) {
  700. // FIXME: File is corrupt! Hot-fix with empty
  701. // index root attribute if recovery option is
  702. // set.
  703. ntfs_error(vi->i_sb, "$INDEX_ROOT attribute "
  704. "is missing.");
  705. }
  706. goto unm_err_out;
  707. }
  708. /* Set up the state. */
  709. if (unlikely(ctx->attr->non_resident)) {
  710. ntfs_error(vol->sb, "$INDEX_ROOT attribute is not "
  711. "resident.");
  712. goto unm_err_out;
  713. }
  714. /* Ensure the attribute name is placed before the value. */
  715. if (unlikely(ctx->attr->name_length &&
  716. (le16_to_cpu(ctx->attr->name_offset) >=
  717. le16_to_cpu(ctx->attr->data.resident.
  718. value_offset)))) {
  719. ntfs_error(vol->sb, "$INDEX_ROOT attribute name is "
  720. "placed after the attribute value.");
  721. goto unm_err_out;
  722. }
  723. /*
  724. * Compressed/encrypted index root just means that the newly
  725. * created files in that directory should be created compressed/
  726. * encrypted. However index root cannot be both compressed and
  727. * encrypted.
  728. */
  729. if (ctx->attr->flags & ATTR_COMPRESSION_MASK)
  730. NInoSetCompressed(ni);
  731. if (ctx->attr->flags & ATTR_IS_ENCRYPTED) {
  732. if (ctx->attr->flags & ATTR_COMPRESSION_MASK) {
  733. ntfs_error(vi->i_sb, "Found encrypted and "
  734. "compressed attribute.");
  735. goto unm_err_out;
  736. }
  737. NInoSetEncrypted(ni);
  738. }
  739. if (ctx->attr->flags & ATTR_IS_SPARSE)
  740. NInoSetSparse(ni);
  741. ir = (INDEX_ROOT*)((char*)ctx->attr + le16_to_cpu(
  742. ctx->attr->data.resident.value_offset));
  743. ir_end = (char*)ir + le32_to_cpu(
  744. ctx->attr->data.resident.value_length);
  745. if (ir_end > (char*)ctx->mrec + vol->mft_record_size) {
  746. ntfs_error(vi->i_sb, "$INDEX_ROOT attribute is "
  747. "corrupt.");
  748. goto unm_err_out;
  749. }
  750. index_end = (char*)&ir->index +
  751. le32_to_cpu(ir->index.index_length);
  752. if (index_end > ir_end) {
  753. ntfs_error(vi->i_sb, "Directory index is corrupt.");
  754. goto unm_err_out;
  755. }
  756. if (ir->type != AT_FILE_NAME) {
  757. ntfs_error(vi->i_sb, "Indexed attribute is not "
  758. "$FILE_NAME.");
  759. goto unm_err_out;
  760. }
  761. if (ir->collation_rule != COLLATION_FILE_NAME) {
  762. ntfs_error(vi->i_sb, "Index collation rule is not "
  763. "COLLATION_FILE_NAME.");
  764. goto unm_err_out;
  765. }
  766. ni->itype.index.collation_rule = ir->collation_rule;
  767. ni->itype.index.block_size = le32_to_cpu(ir->index_block_size);
  768. if (ni->itype.index.block_size &
  769. (ni->itype.index.block_size - 1)) {
  770. ntfs_error(vi->i_sb, "Index block size (%u) is not a "
  771. "power of two.",
  772. ni->itype.index.block_size);
  773. goto unm_err_out;
  774. }
  775. if (ni->itype.index.block_size > PAGE_CACHE_SIZE) {
  776. ntfs_error(vi->i_sb, "Index block size (%u) > "
  777. "PAGE_CACHE_SIZE (%ld) is not "
  778. "supported. Sorry.",
  779. ni->itype.index.block_size,
  780. PAGE_CACHE_SIZE);
  781. err = -EOPNOTSUPP;
  782. goto unm_err_out;
  783. }
  784. if (ni->itype.index.block_size < NTFS_BLOCK_SIZE) {
  785. ntfs_error(vi->i_sb, "Index block size (%u) < "
  786. "NTFS_BLOCK_SIZE (%i) is not "
  787. "supported. Sorry.",
  788. ni->itype.index.block_size,
  789. NTFS_BLOCK_SIZE);
  790. err = -EOPNOTSUPP;
  791. goto unm_err_out;
  792. }
  793. ni->itype.index.block_size_bits =
  794. ffs(ni->itype.index.block_size) - 1;
  795. /* Determine the size of a vcn in the directory index. */
  796. if (vol->cluster_size <= ni->itype.index.block_size) {
  797. ni->itype.index.vcn_size = vol->cluster_size;
  798. ni->itype.index.vcn_size_bits = vol->cluster_size_bits;
  799. } else {
  800. ni->itype.index.vcn_size = vol->sector_size;
  801. ni->itype.index.vcn_size_bits = vol->sector_size_bits;
  802. }
  803. /* Setup the index allocation attribute, even if not present. */
  804. NInoSetMstProtected(ni);
  805. ni->type = AT_INDEX_ALLOCATION;
  806. ni->name = I30;
  807. ni->name_len = 4;
  808. if (!(ir->index.flags & LARGE_INDEX)) {
  809. /* No index allocation. */
  810. vi->i_size = ni->initialized_size =
  811. ni->allocated_size = 0;
  812. /* We are done with the mft record, so we release it. */
  813. ntfs_attr_put_search_ctx(ctx);
  814. unmap_mft_record(ni);
  815. m = NULL;
  816. ctx = NULL;
  817. goto skip_large_dir_stuff;
  818. } /* LARGE_INDEX: Index allocation present. Setup state. */
  819. NInoSetIndexAllocPresent(ni);
  820. /* Find index allocation attribute. */
  821. ntfs_attr_reinit_search_ctx(ctx);
  822. err = ntfs_attr_lookup(AT_INDEX_ALLOCATION, I30, 4,
  823. CASE_SENSITIVE, 0, NULL, 0, ctx);
  824. if (unlikely(err)) {
  825. if (err == -ENOENT)
  826. ntfs_error(vi->i_sb, "$INDEX_ALLOCATION "
  827. "attribute is not present but "
  828. "$INDEX_ROOT indicated it is.");
  829. else
  830. ntfs_error(vi->i_sb, "Failed to lookup "
  831. "$INDEX_ALLOCATION "
  832. "attribute.");
  833. goto unm_err_out;
  834. }
  835. if (!ctx->attr->non_resident) {
  836. ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute "
  837. "is resident.");
  838. goto unm_err_out;
  839. }
  840. /*
  841. * Ensure the attribute name is placed before the mapping pairs
  842. * array.
  843. */
  844. if (unlikely(ctx->attr->name_length &&
  845. (le16_to_cpu(ctx->attr->name_offset) >=
  846. le16_to_cpu(ctx->attr->data.non_resident.
  847. mapping_pairs_offset)))) {
  848. ntfs_error(vol->sb, "$INDEX_ALLOCATION attribute name "
  849. "is placed after the mapping pairs "
  850. "array.");
  851. goto unm_err_out;
  852. }
  853. if (ctx->attr->flags & ATTR_IS_ENCRYPTED) {
  854. ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute "
  855. "is encrypted.");
  856. goto unm_err_out;
  857. }
  858. if (ctx->attr->flags & ATTR_IS_SPARSE) {
  859. ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute "
  860. "is sparse.");
  861. goto unm_err_out;
  862. }
  863. if (ctx->attr->flags & ATTR_COMPRESSION_MASK) {
  864. ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute "
  865. "is compressed.");
  866. goto unm_err_out;
  867. }
  868. if (ctx->attr->data.non_resident.lowest_vcn) {
  869. ntfs_error(vi->i_sb, "First extent of "
  870. "$INDEX_ALLOCATION attribute has non "
  871. "zero lowest_vcn.");
  872. goto unm_err_out;
  873. }
  874. vi->i_size = sle64_to_cpu(
  875. ctx->attr->data.non_resident.data_size);
  876. ni->initialized_size = sle64_to_cpu(
  877. ctx->attr->data.non_resident.initialized_size);
  878. ni->allocated_size = sle64_to_cpu(
  879. ctx->attr->data.non_resident.allocated_size);
  880. /*
  881. * We are done with the mft record, so we release it. Otherwise
  882. * we would deadlock in ntfs_attr_iget().
  883. */
  884. ntfs_attr_put_search_ctx(ctx);
  885. unmap_mft_record(ni);
  886. m = NULL;
  887. ctx = NULL;
  888. /* Get the index bitmap attribute inode. */
  889. bvi = ntfs_attr_iget(vi, AT_BITMAP, I30, 4);
  890. if (IS_ERR(bvi)) {
  891. ntfs_error(vi->i_sb, "Failed to get bitmap attribute.");
  892. err = PTR_ERR(bvi);
  893. goto unm_err_out;
  894. }
  895. ni->itype.index.bmp_ino = bvi;
  896. bni = NTFS_I(bvi);
  897. if (NInoCompressed(bni) || NInoEncrypted(bni) ||
  898. NInoSparse(bni)) {
  899. ntfs_error(vi->i_sb, "$BITMAP attribute is compressed "
  900. "and/or encrypted and/or sparse.");
  901. goto unm_err_out;
  902. }
  903. /* Consistency check bitmap size vs. index allocation size. */
  904. bvi_size = i_size_read(bvi);
  905. if ((bvi_size << 3) < (vi->i_size >>
  906. ni->itype.index.block_size_bits)) {
  907. ntfs_error(vi->i_sb, "Index bitmap too small (0x%llx) "
  908. "for index allocation (0x%llx).",
  909. bvi_size << 3, vi->i_size);
  910. goto unm_err_out;
  911. }
  912. skip_large_dir_stuff:
  913. /* Setup the operations for this inode. */
  914. vi->i_op = &ntfs_dir_inode_ops;
  915. vi->i_fop = &ntfs_dir_ops;
  916. } else {
  917. /* It is a file. */
  918. ntfs_attr_reinit_search_ctx(ctx);
  919. /* Setup the data attribute, even if not present. */
  920. ni->type = AT_DATA;
  921. ni->name = NULL;
  922. ni->name_len = 0;
  923. /* Find first extent of the unnamed data attribute. */
  924. err = ntfs_attr_lookup(AT_DATA, NULL, 0, 0, 0, NULL, 0, ctx);
  925. if (unlikely(err)) {
  926. vi->i_size = ni->initialized_size =
  927. ni->allocated_size = 0;
  928. if (err != -ENOENT) {
  929. ntfs_error(vi->i_sb, "Failed to lookup $DATA "
  930. "attribute.");
  931. goto unm_err_out;
  932. }
  933. /*
  934. * FILE_Secure does not have an unnamed $DATA
  935. * attribute, so we special case it here.
  936. */
  937. if (vi->i_ino == FILE_Secure)
  938. goto no_data_attr_special_case;
  939. /*
  940. * Most if not all the system files in the $Extend
  941. * system directory do not have unnamed data
  942. * attributes so we need to check if the parent
  943. * directory of the file is FILE_Extend and if it is
  944. * ignore this error. To do this we need to get the
  945. * name of this inode from the mft record as the name
  946. * contains the back reference to the parent directory.
  947. */
  948. if (ntfs_is_extended_system_file(ctx) > 0)
  949. goto no_data_attr_special_case;
  950. // FIXME: File is corrupt! Hot-fix with empty data
  951. // attribute if recovery option is set.
  952. ntfs_error(vi->i_sb, "$DATA attribute is missing.");
  953. goto unm_err_out;
  954. }
  955. /* Setup the state. */
  956. if (ctx->attr->non_resident) {
  957. NInoSetNonResident(ni);
  958. if (ctx->attr->flags & ATTR_COMPRESSION_MASK) {
  959. NInoSetCompressed(ni);
  960. if (vol->cluster_size > 4096) {
  961. ntfs_error(vi->i_sb, "Found "
  962. "compressed data but "
  963. "compression is disabled due "
  964. "to cluster size (%i) > 4kiB.",
  965. vol->cluster_size);
  966. goto unm_err_out;
  967. }
  968. if ((ctx->attr->flags & ATTR_COMPRESSION_MASK)
  969. != ATTR_IS_COMPRESSED) {
  970. ntfs_error(vi->i_sb, "Found "
  971. "unknown compression method or "
  972. "corrupt file.");
  973. goto unm_err_out;
  974. }
  975. ni->itype.compressed.block_clusters = 1U <<
  976. ctx->attr->data.non_resident.
  977. compression_unit;
  978. if (ctx->attr->data.non_resident.
  979. compression_unit != 4) {
  980. ntfs_error(vi->i_sb, "Found "
  981. "nonstandard compression unit "
  982. "(%u instead of 4). Cannot "
  983. "handle this.",
  984. ctx->attr->data.non_resident.
  985. compression_unit);
  986. err = -EOPNOTSUPP;
  987. goto unm_err_out;
  988. }
  989. ni->itype.compressed.block_size = 1U << (
  990. ctx->attr->data.non_resident.
  991. compression_unit +
  992. vol->cluster_size_bits);
  993. ni->itype.compressed.block_size_bits = ffs(
  994. ni->itype.compressed.block_size) - 1;
  995. }
  996. if (ctx->attr->flags & ATTR_IS_ENCRYPTED) {
  997. if (ctx->attr->flags & ATTR_COMPRESSION_MASK) {
  998. ntfs_error(vi->i_sb, "Found encrypted "
  999. "and compressed data.");
  1000. goto unm_err_out;
  1001. }
  1002. NInoSetEncrypted(ni);
  1003. }
  1004. if (ctx->attr->flags & ATTR_IS_SPARSE)
  1005. NInoSetSparse(ni);
  1006. if (ctx->attr->data.non_resident.lowest_vcn) {
  1007. ntfs_error(vi->i_sb, "First extent of $DATA "
  1008. "attribute has non zero "
  1009. "lowest_vcn.");
  1010. goto unm_err_out;
  1011. }
  1012. /* Setup all the sizes. */
  1013. vi->i_size = sle64_to_cpu(
  1014. ctx->attr->data.non_resident.data_size);
  1015. ni->initialized_size = sle64_to_cpu(
  1016. ctx->attr->data.non_resident.
  1017. initialized_size);
  1018. ni->allocated_size = sle64_to_cpu(
  1019. ctx->attr->data.non_resident.
  1020. allocated_size);
  1021. if (NInoCompressed(ni)) {
  1022. ni->itype.compressed.size = sle64_to_cpu(
  1023. ctx->attr->data.non_resident.
  1024. compressed_size);
  1025. }
  1026. } else { /* Resident attribute. */
  1027. /*
  1028. * Make all sizes equal for simplicity in read code
  1029. * paths. FIXME: Need to keep this in mind when
  1030. * converting to non-resident attribute in write code
  1031. * path. (Probably only affects truncate().)
  1032. */
  1033. vi->i_size = ni->initialized_size = ni->allocated_size =
  1034. le32_to_cpu(
  1035. ctx->attr->data.resident.value_length);
  1036. }
  1037. no_data_attr_special_case:
  1038. /* We are done with the mft record, so we release it. */
  1039. ntfs_attr_put_search_ctx(ctx);
  1040. unmap_mft_record(ni);
  1041. m = NULL;
  1042. ctx = NULL;
  1043. /* Setup the operations for this inode. */
  1044. vi->i_op = &ntfs_file_inode_ops;
  1045. vi->i_fop = &ntfs_file_ops;
  1046. }
  1047. if (NInoMstProtected(ni))
  1048. vi->i_mapping->a_ops = &ntfs_mst_aops;
  1049. else
  1050. vi->i_mapping->a_ops = &ntfs_aops;
  1051. /*
  1052. * The number of 512-byte blocks used on disk (for stat). This is in so
  1053. * far inaccurate as it doesn't account for any named streams or other
  1054. * special non-resident attributes, but that is how Windows works, too,
  1055. * so we are at least consistent with Windows, if not entirely
  1056. * consistent with the Linux Way. Doing it the Linux Way would cause a
  1057. * significant slowdown as it would involve iterating over all
  1058. * attributes in the mft record and adding the allocated/compressed
  1059. * sizes of all non-resident attributes present to give us the Linux
  1060. * correct size that should go into i_blocks (after division by 512).
  1061. */
  1062. if (S_ISDIR(vi->i_mode) || !NInoCompressed(ni))
  1063. vi->i_blocks = ni->allocated_size >> 9;
  1064. else
  1065. vi->i_blocks = ni->itype.compressed.size >> 9;
  1066. ntfs_debug("Done.");
  1067. return 0;
  1068. unm_err_out:
  1069. if (!err)
  1070. err = -EIO;
  1071. if (ctx)
  1072. ntfs_attr_put_search_ctx(ctx);
  1073. if (m)
  1074. unmap_mft_record(ni);
  1075. err_out:
  1076. ntfs_error(vol->sb, "Failed with error code %i. Marking corrupt "
  1077. "inode 0x%lx as bad. Run chkdsk.", err, vi->i_ino);
  1078. make_bad_inode(vi);
  1079. if (err != -EOPNOTSUPP && err != -ENOMEM)
  1080. NVolSetErrors(vol);
  1081. return err;
  1082. }
  1083. /**
  1084. * ntfs_read_locked_attr_inode - read an attribute inode from its base inode
  1085. * @base_vi: base inode
  1086. * @vi: attribute inode to read
  1087. *
  1088. * ntfs_read_locked_attr_inode() is called from ntfs_attr_iget() to read the
  1089. * attribute inode described by @vi into memory from the base mft record
  1090. * described by @base_ni.
  1091. *
  1092. * ntfs_read_locked_attr_inode() maps, pins and locks the base inode for
  1093. * reading and looks up the attribute described by @vi before setting up the
  1094. * necessary fields in @vi as well as initializing the ntfs inode.
  1095. *
  1096. * Q: What locks are held when the function is called?
  1097. * A: i_state has I_LOCK set, hence the inode is locked, also
  1098. * i_count is set to 1, so it is not going to go away
  1099. *
  1100. * Return 0 on success and -errno on error. In the error case, the inode will
  1101. * have had make_bad_inode() executed on it.
  1102. */
  1103. static int ntfs_read_locked_attr_inode(struct inode *base_vi, struct inode *vi)
  1104. {
  1105. ntfs_volume *vol = NTFS_SB(vi->i_sb);
  1106. ntfs_inode *ni, *base_ni;
  1107. MFT_RECORD *m;
  1108. ntfs_attr_search_ctx *ctx;
  1109. int err = 0;
  1110. ntfs_debug("Entering for i_ino 0x%lx.", vi->i_ino);
  1111. ntfs_init_big_inode(vi);
  1112. ni = NTFS_I(vi);
  1113. base_ni = NTFS_I(base_vi);
  1114. /* Just mirror the values from the base inode. */
  1115. vi->i_blksize = base_vi->i_blksize;
  1116. vi->i_version = base_vi->i_version;
  1117. vi->i_uid = base_vi->i_uid;
  1118. vi->i_gid = base_vi->i_gid;
  1119. vi->i_nlink = base_vi->i_nlink;
  1120. vi->i_mtime = base_vi->i_mtime;
  1121. vi->i_ctime = base_vi->i_ctime;
  1122. vi->i_atime = base_vi->i_atime;
  1123. vi->i_generation = ni->seq_no = base_ni->seq_no;
  1124. /* Set inode type to zero but preserve permissions. */
  1125. vi->i_mode = base_vi->i_mode & ~S_IFMT;
  1126. m = map_mft_record(base_ni);
  1127. if (IS_ERR(m)) {
  1128. err = PTR_ERR(m);
  1129. goto err_out;
  1130. }
  1131. ctx = ntfs_attr_get_search_ctx(base_ni, m);
  1132. if (!ctx) {
  1133. err = -ENOMEM;
  1134. goto unm_err_out;
  1135. }
  1136. /* Find the attribute. */
  1137. err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
  1138. CASE_SENSITIVE, 0, NULL, 0, ctx);
  1139. if (unlikely(err))
  1140. goto unm_err_out;
  1141. if (!ctx->attr->non_resident) {
  1142. /* Ensure the attribute name is placed before the value. */
  1143. if (unlikely(ctx->attr->name_length &&
  1144. (le16_to_cpu(ctx->attr->name_offset) >=
  1145. le16_to_cpu(ctx->attr->data.resident.
  1146. value_offset)))) {
  1147. ntfs_error(vol->sb, "Attribute name is placed after "
  1148. "the attribute value.");
  1149. goto unm_err_out;
  1150. }
  1151. if (NInoMstProtected(ni) || ctx->attr->flags) {
  1152. ntfs_error(vi->i_sb, "Found mst protected attribute "
  1153. "or attribute with non-zero flags but "
  1154. "the attribute is resident. Please "
  1155. "report you saw this message to "
  1156. "linux-ntfs-dev@lists.sourceforge.net");
  1157. goto unm_err_out;
  1158. }
  1159. /*
  1160. * Resident attribute. Make all sizes equal for simplicity in
  1161. * read code paths.
  1162. */
  1163. vi->i_size = ni->initialized_size = ni->allocated_size =
  1164. le32_to_cpu(ctx->attr->data.resident.value_length);
  1165. } else {
  1166. NInoSetNonResident(ni);
  1167. /*
  1168. * Ensure the attribute name is placed before the mapping pairs
  1169. * array.
  1170. */
  1171. if (unlikely(ctx->attr->name_length &&
  1172. (le16_to_cpu(ctx->attr->name_offset) >=
  1173. le16_to_cpu(ctx->attr->data.non_resident.
  1174. mapping_pairs_offset)))) {
  1175. ntfs_error(vol->sb, "Attribute name is placed after "
  1176. "the mapping pairs array.");
  1177. goto unm_err_out;
  1178. }
  1179. if (ctx->attr->flags & ATTR_COMPRESSION_MASK) {
  1180. if (NInoMstProtected(ni)) {
  1181. ntfs_error(vi->i_sb, "Found mst protected "
  1182. "attribute but the attribute "
  1183. "is compressed. Please report "
  1184. "you saw this message to "
  1185. "linux-ntfs-dev@lists."
  1186. "sourceforge.net");
  1187. goto unm_err_out;
  1188. }
  1189. NInoSetCompressed(ni);
  1190. if ((ni->type != AT_DATA) || (ni->type == AT_DATA &&
  1191. ni->name_len)) {
  1192. ntfs_error(vi->i_sb, "Found compressed "
  1193. "non-data or named data "
  1194. "attribute. Please report "
  1195. "you saw this message to "
  1196. "linux-ntfs-dev@lists."
  1197. "sourceforge.net");
  1198. goto unm_err_out;
  1199. }
  1200. if (vol->cluster_size > 4096) {
  1201. ntfs_error(vi->i_sb, "Found compressed "
  1202. "attribute but compression is "
  1203. "disabled due to cluster size "
  1204. "(%i) > 4kiB.",
  1205. vol->cluster_size);
  1206. goto unm_err_out;
  1207. }
  1208. if ((ctx->attr->flags & ATTR_COMPRESSION_MASK)
  1209. != ATTR_IS_COMPRESSED) {
  1210. ntfs_error(vi->i_sb, "Found unknown "
  1211. "compression method.");
  1212. goto unm_err_out;
  1213. }
  1214. ni->itype.compressed.block_clusters = 1U <<
  1215. ctx->attr->data.non_resident.
  1216. compression_unit;
  1217. if (ctx->attr->data.non_resident.compression_unit !=
  1218. 4) {
  1219. ntfs_error(vi->i_sb, "Found nonstandard "
  1220. "compression unit (%u instead "
  1221. "of 4). Cannot handle this.",
  1222. ctx->attr->data.non_resident.
  1223. compression_unit);
  1224. err = -EOPNOTSUPP;
  1225. goto unm_err_out;
  1226. }
  1227. ni->itype.compressed.block_size = 1U << (
  1228. ctx->attr->data.non_resident.
  1229. compression_unit +
  1230. vol->cluster_size_bits);
  1231. ni->itype.compressed.block_size_bits = ffs(
  1232. ni->itype.compressed.block_size) - 1;
  1233. }
  1234. if (ctx->attr->flags & ATTR_IS_ENCRYPTED) {
  1235. if (ctx->attr->flags & ATTR_COMPRESSION_MASK) {
  1236. ntfs_error(vi->i_sb, "Found encrypted "
  1237. "and compressed data.");
  1238. goto unm_err_out;
  1239. }
  1240. if (NInoMstProtected(ni)) {
  1241. ntfs_error(vi->i_sb, "Found mst protected "
  1242. "attribute but the attribute "
  1243. "is encrypted. Please report "
  1244. "you saw this message to "
  1245. "linux-ntfs-dev@lists."
  1246. "sourceforge.net");
  1247. goto unm_err_out;
  1248. }
  1249. NInoSetEncrypted(ni);
  1250. }
  1251. if (ctx->attr->flags & ATTR_IS_SPARSE) {
  1252. if (NInoMstProtected(ni)) {
  1253. ntfs_error(vi->i_sb, "Found mst protected "
  1254. "attribute but the attribute "
  1255. "is sparse. Please report "
  1256. "you saw this message to "
  1257. "linux-ntfs-dev@lists."
  1258. "sourceforge.net");
  1259. goto unm_err_out;
  1260. }
  1261. NInoSetSparse(ni);
  1262. }
  1263. if (ctx->attr->data.non_resident.lowest_vcn) {
  1264. ntfs_error(vi->i_sb, "First extent of attribute has "
  1265. "non-zero lowest_vcn.");
  1266. goto unm_err_out;
  1267. }
  1268. /* Setup all the sizes. */
  1269. vi->i_size = sle64_to_cpu(
  1270. ctx->attr->data.non_resident.data_size);
  1271. ni->initialized_size = sle64_to_cpu(
  1272. ctx->attr->data.non_resident.initialized_size);
  1273. ni->allocated_size = sle64_to_cpu(
  1274. ctx->attr->data.non_resident.allocated_size);
  1275. if (NInoCompressed(ni)) {
  1276. ni->itype.compressed.size = sle64_to_cpu(
  1277. ctx->attr->data.non_resident.
  1278. compressed_size);
  1279. }
  1280. }
  1281. /* Setup the operations for this attribute inode. */
  1282. vi->i_op = NULL;
  1283. vi->i_fop = NULL;
  1284. if (NInoMstProtected(ni))
  1285. vi->i_mapping->a_ops = &ntfs_mst_aops;
  1286. else
  1287. vi->i_mapping->a_ops = &ntfs_aops;
  1288. if (!NInoCompressed(ni))
  1289. vi->i_blocks = ni->allocated_size >> 9;
  1290. else
  1291. vi->i_blocks = ni->itype.compressed.size >> 9;
  1292. /*
  1293. * Make sure the base inode doesn't go away and attach it to the
  1294. * attribute inode.
  1295. */
  1296. igrab(base_vi);
  1297. ni->ext.base_ntfs_ino = base_ni;
  1298. ni->nr_extents = -1;
  1299. ntfs_attr_put_search_ctx(ctx);
  1300. unmap_mft_record(base_ni);
  1301. ntfs_debug("Done.");
  1302. return 0;
  1303. unm_err_out:
  1304. if (!err)
  1305. err = -EIO;
  1306. if (ctx)
  1307. ntfs_attr_put_search_ctx(ctx);
  1308. unmap_mft_record(base_ni);
  1309. err_out:
  1310. ntfs_error(vol->sb, "Failed with error code %i while reading attribute "
  1311. "inode (mft_no 0x%lx, type 0x%x, name_len %i). "
  1312. "Marking corrupt inode and base inode 0x%lx as bad. "
  1313. "Run chkdsk.", err, vi->i_ino, ni->type, ni->name_len,
  1314. base_vi->i_ino);
  1315. make_bad_inode(vi);
  1316. make_bad_inode(base_vi);
  1317. if (err != -ENOMEM)
  1318. NVolSetErrors(vol);
  1319. return err;
  1320. }
  1321. /**
  1322. * ntfs_read_locked_index_inode - read an index inode from its base inode
  1323. * @base_vi: base inode
  1324. * @vi: index inode to read
  1325. *
  1326. * ntfs_read_locked_index_inode() is called from ntfs_index_iget() to read the
  1327. * index inode described by @vi into memory from the base mft record described
  1328. * by @base_ni.
  1329. *
  1330. * ntfs_read_locked_index_inode() maps, pins and locks the base inode for
  1331. * reading and looks up the attributes relating to the index described by @vi
  1332. * before setting up the necessary fields in @vi as well as initializing the
  1333. * ntfs inode.
  1334. *
  1335. * Note, index inodes are essentially attribute inodes (NInoAttr() is true)
  1336. * with the attribute type set to AT_INDEX_ALLOCATION. Apart from that, they
  1337. * are setup like directory inodes since directories are a special case of
  1338. * indices ao they need to be treated in much the same way. Most importantly,
  1339. * for small indices the index allocation attribute might not actually exist.
  1340. * However, the index root attribute always exists but this does not need to
  1341. * have an inode associated with it and this is why we define a new inode type
  1342. * index. Also, like for directories, we need to have an attribute inode for
  1343. * the bitmap attribute corresponding to the index allocation attribute and we
  1344. * can store this in the appropriate field of the inode, just like we do for
  1345. * normal directory inodes.
  1346. *
  1347. * Q: What locks are held when the function is called?
  1348. * A: i_state has I_LOCK set, hence the inode is locked, also
  1349. * i_count is set to 1, so it is not going to go away
  1350. *
  1351. * Return 0 on success and -errno on error. In the error case, the inode will
  1352. * have had make_bad_inode() executed on it.
  1353. */
  1354. static int ntfs_read_locked_index_inode(struct inode *base_vi, struct inode *vi)
  1355. {
  1356. loff_t bvi_size;
  1357. ntfs_volume *vol = NTFS_SB(vi->i_sb);
  1358. ntfs_inode *ni, *base_ni, *bni;
  1359. struct inode *bvi;
  1360. MFT_RECORD *m;
  1361. ntfs_attr_search_ctx *ctx;
  1362. INDEX_ROOT *ir;
  1363. u8 *ir_end, *index_end;
  1364. int err = 0;
  1365. ntfs_debug("Entering for i_ino 0x%lx.", vi->i_ino);
  1366. ntfs_init_big_inode(vi);
  1367. ni = NTFS_I(vi);
  1368. base_ni = NTFS_I(base_vi);
  1369. /* Just mirror the values from the base inode. */
  1370. vi->i_blksize = base_vi->i_blksize;
  1371. vi->i_version = base_vi->i_version;
  1372. vi->i_uid = base_vi->i_uid;
  1373. vi->i_gid = base_vi->i_gid;
  1374. vi->i_nlink = base_vi->i_nlink;
  1375. vi->i_mtime = base_vi->i_mtime;
  1376. vi->i_ctime = base_vi->i_ctime;
  1377. vi->i_atime = base_vi->i_atime;
  1378. vi->i_generation = ni->seq_no = base_ni->seq_no;
  1379. /* Set inode type to zero but preserve permissions. */
  1380. vi->i_mode = base_vi->i_mode & ~S_IFMT;
  1381. /* Map the mft record for the base inode. */
  1382. m = map_mft_record(base_ni);
  1383. if (IS_ERR(m)) {
  1384. err = PTR_ERR(m);
  1385. goto err_out;
  1386. }
  1387. ctx = ntfs_attr_get_search_ctx(base_ni, m);
  1388. if (!ctx) {
  1389. err = -ENOMEM;
  1390. goto unm_err_out;
  1391. }
  1392. /* Find the index root attribute. */
  1393. err = ntfs_attr_lookup(AT_INDEX_ROOT, ni->name, ni->name_len,
  1394. CASE_SENSITIVE, 0, NULL, 0, ctx);
  1395. if (unlikely(err)) {
  1396. if (err == -ENOENT)
  1397. ntfs_error(vi->i_sb, "$INDEX_ROOT attribute is "
  1398. "missing.");
  1399. goto unm_err_out;
  1400. }
  1401. /* Set up the state. */
  1402. if (unlikely(ctx->attr->non_resident)) {
  1403. ntfs_error(vol->sb, "$INDEX_ROOT attribute is not resident.");
  1404. goto unm_err_out;
  1405. }
  1406. /* Ensure the attribute name is placed before the value. */
  1407. if (unlikely(ctx->attr->name_length &&
  1408. (le16_to_cpu(ctx->attr->name_offset) >=
  1409. le16_to_cpu(ctx->attr->data.resident.
  1410. value_offset)))) {
  1411. ntfs_error(vol->sb, "$INDEX_ROOT attribute name is placed "
  1412. "after the attribute value.");
  1413. goto unm_err_out;
  1414. }
  1415. /* Compressed/encrypted/sparse index root is not allowed. */
  1416. if (ctx->attr->flags & (ATTR_COMPRESSION_MASK | ATTR_IS_ENCRYPTED |
  1417. ATTR_IS_SPARSE)) {
  1418. ntfs_error(vi->i_sb, "Found compressed/encrypted/sparse index "
  1419. "root attribute.");
  1420. goto unm_err_out;
  1421. }
  1422. ir = (INDEX_ROOT*)((u8*)ctx->attr +
  1423. le16_to_cpu(ctx->attr->data.resident.value_offset));
  1424. ir_end = (u8*)ir + le32_to_cpu(ctx->attr->data.resident.value_length);
  1425. if (ir_end > (u8*)ctx->mrec + vol->mft_record_size) {
  1426. ntfs_error(vi->i_sb, "$INDEX_ROOT attribute is corrupt.");
  1427. goto unm_err_out;
  1428. }
  1429. index_end = (u8*)&ir->index + le32_to_cpu(ir->index.index_length);
  1430. if (index_end > ir_end) {
  1431. ntfs_error(vi->i_sb, "Index is corrupt.");
  1432. goto unm_err_out;
  1433. }
  1434. if (ir->type) {
  1435. ntfs_error(vi->i_sb, "Index type is not 0 (type is 0x%x).",
  1436. le32_to_cpu(ir->type));
  1437. goto unm_err_out;
  1438. }
  1439. ni->itype.index.collation_rule = ir->collation_rule;
  1440. ntfs_debug("Index collation rule is 0x%x.",
  1441. le32_to_cpu(ir->collation_rule));
  1442. ni->itype.index.block_size = le32_to_cpu(ir->index_block_size);
  1443. if (ni->itype.index.block_size & (ni->itype.index.block_size - 1)) {
  1444. ntfs_error(vi->i_sb, "Index block size (%u) is not a power of "
  1445. "two.", ni->itype.index.block_size);
  1446. goto unm_err_out;
  1447. }
  1448. if (ni->itype.index.block_size > PAGE_CACHE_SIZE) {
  1449. ntfs_error(vi->i_sb, "Index block size (%u) > PAGE_CACHE_SIZE "
  1450. "(%ld) is not supported. Sorry.",
  1451. ni->itype.index.block_size, PAGE_CACHE_SIZE);
  1452. err = -EOPNOTSUPP;
  1453. goto unm_err_out;
  1454. }
  1455. if (ni->itype.index.block_size < NTFS_BLOCK_SIZE) {
  1456. ntfs_error(vi->i_sb, "Index block size (%u) < NTFS_BLOCK_SIZE "
  1457. "(%i) is not supported. Sorry.",
  1458. ni->itype.index.block_size, NTFS_BLOCK_SIZE);
  1459. err = -EOPNOTSUPP;
  1460. goto unm_err_out;
  1461. }
  1462. ni->itype.index.block_size_bits = ffs(ni->itype.index.block_size) - 1;
  1463. /* Determine the size of a vcn in the index. */
  1464. if (vol->cluster_size <= ni->itype.index.block_size) {
  1465. ni->itype.index.vcn_size = vol->cluster_size;
  1466. ni->itype.index.vcn_size_bits = vol->cluster_size_bits;
  1467. } else {
  1468. ni->itype.index.vcn_size = vol->sector_size;
  1469. ni->itype.index.vcn_size_bits = vol->sector_size_bits;
  1470. }
  1471. /* Check for presence of index allocation attribute. */
  1472. if (!(ir->index.flags & LARGE_INDEX)) {
  1473. /* No index allocation. */
  1474. vi->i_size = ni->initialized_size = ni->allocated_size = 0;
  1475. /* We are done with the mft record, so we release it. */
  1476. ntfs_attr_put_search_ctx(ctx);
  1477. unmap_mft_record(base_ni);
  1478. m = NULL;
  1479. ctx = NULL;
  1480. goto skip_large_index_stuff;
  1481. } /* LARGE_INDEX: Index allocation present. Setup state. */
  1482. NInoSetIndexAllocPresent(ni);
  1483. /* Find index allocation attribute. */
  1484. ntfs_attr_reinit_search_ctx(ctx);
  1485. err = ntfs_attr_lookup(AT_INDEX_ALLOCATION, ni->name, ni->name_len,
  1486. CASE_SENSITIVE, 0, NULL, 0, ctx);
  1487. if (unlikely(err)) {
  1488. if (err == -ENOENT)
  1489. ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is "
  1490. "not present but $INDEX_ROOT "
  1491. "indicated it is.");
  1492. else
  1493. ntfs_error(vi->i_sb, "Failed to lookup "
  1494. "$INDEX_ALLOCATION attribute.");
  1495. goto unm_err_out;
  1496. }
  1497. if (!ctx->attr->non_resident) {
  1498. ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is "
  1499. "resident.");
  1500. goto unm_err_out;
  1501. }
  1502. /*
  1503. * Ensure the attribute name is placed before the mapping pairs array.
  1504. */
  1505. if (unlikely(ctx->attr->name_length && (le16_to_cpu(
  1506. ctx->attr->name_offset) >= le16_to_cpu(
  1507. ctx->attr->data.non_resident.mapping_pairs_offset)))) {
  1508. ntfs_error(vol->sb, "$INDEX_ALLOCATION attribute name is "
  1509. "placed after the mapping pairs array.");
  1510. goto unm_err_out;
  1511. }
  1512. if (ctx->attr->flags & ATTR_IS_ENCRYPTED) {
  1513. ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is "
  1514. "encrypted.");
  1515. goto unm_err_out;
  1516. }
  1517. if (ctx->attr->flags & ATTR_IS_SPARSE) {
  1518. ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is sparse.");
  1519. goto unm_err_out;
  1520. }
  1521. if (ctx->attr->flags & ATTR_COMPRESSION_MASK) {
  1522. ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is "
  1523. "compressed.");
  1524. goto unm_err_out;
  1525. }
  1526. if (ctx->attr->data.non_resident.lowest_vcn) {
  1527. ntfs_error(vi->i_sb, "First extent of $INDEX_ALLOCATION "
  1528. "attribute has non zero lowest_vcn.");
  1529. goto unm_err_out;
  1530. }
  1531. vi->i_size = sle64_to_cpu(ctx->attr->data.non_resident.data_size);
  1532. ni->initialized_size = sle64_to_cpu(
  1533. ctx->attr->data.non_resident.initialized_size);
  1534. ni->allocated_size = sle64_to_cpu(
  1535. ctx->attr->data.non_resident.allocated_size);
  1536. /*
  1537. * We are done with the mft record, so we release it. Otherwise
  1538. * we would deadlock in ntfs_attr_iget().
  1539. */
  1540. ntfs_attr_put_search_ctx(ctx);
  1541. unmap_mft_record(base_ni);
  1542. m = NULL;
  1543. ctx = NULL;
  1544. /* Get the index bitmap attribute inode. */
  1545. bvi = ntfs_attr_iget(base_vi, AT_BITMAP, ni->name, ni->name_len);
  1546. if (IS_ERR(bvi)) {
  1547. ntfs_error(vi->i_sb, "Failed to get bitmap attribute.");
  1548. err = PTR_ERR(bvi);
  1549. goto unm_err_out;
  1550. }
  1551. bni = NTFS_I(bvi);
  1552. if (NInoCompressed(bni) || NInoEncrypted(bni) ||
  1553. NInoSparse(bni)) {
  1554. ntfs_error(vi->i_sb, "$BITMAP attribute is compressed and/or "
  1555. "encrypted and/or sparse.");
  1556. goto iput_unm_err_out;
  1557. }
  1558. /* Consistency check bitmap size vs. index allocation size. */
  1559. bvi_size = i_size_read(bvi);
  1560. if ((bvi_size << 3) < (vi->i_size >> ni->itype.index.block_size_bits)) {
  1561. ntfs_error(vi->i_sb, "Index bitmap too small (0x%llx) for "
  1562. "index allocation (0x%llx).", bvi_size << 3,
  1563. vi->i_size);
  1564. goto iput_unm_err_out;
  1565. }
  1566. ni->itype.index.bmp_ino = bvi;
  1567. skip_large_index_stuff:
  1568. /* Setup the operations for this index inode. */
  1569. vi->i_op = NULL;
  1570. vi->i_fop = NULL;
  1571. vi->i_mapping->a_ops = &ntfs_mst_aops;
  1572. vi->i_blocks = ni->allocated_size >> 9;
  1573. /*
  1574. * Make sure the base inode doesn't go away and attach it to the
  1575. * index inode.
  1576. */
  1577. igrab(base_vi);
  1578. ni->ext.base_ntfs_ino = base_ni;
  1579. ni->nr_extents = -1;
  1580. ntfs_debug("Done.");
  1581. return 0;
  1582. iput_unm_err_out:
  1583. iput(bvi);
  1584. unm_err_out:
  1585. if (!err)
  1586. err = -EIO;
  1587. if (ctx)
  1588. ntfs_attr_put_search_ctx(ctx);
  1589. if (m)
  1590. unmap_mft_record(base_ni);
  1591. err_out:
  1592. ntfs_error(vi->i_sb, "Failed with error code %i while reading index "
  1593. "inode (mft_no 0x%lx, name_len %i.", err, vi->i_ino,
  1594. ni->name_len);
  1595. make_bad_inode(vi);
  1596. if (err != -EOPNOTSUPP && err != -ENOMEM)
  1597. NVolSetErrors(vol);
  1598. return err;
  1599. }
  1600. /**
  1601. * ntfs_read_inode_mount - special read_inode for mount time use only
  1602. * @vi: inode to read
  1603. *
  1604. * Read inode FILE_MFT at mount time, only called with super_block lock
  1605. * held from within the read_super() code path.
  1606. *
  1607. * This function exists because when it is called the page cache for $MFT/$DATA
  1608. * is not initialized and hence we cannot get at the contents of mft records
  1609. * by calling map_mft_record*().
  1610. *
  1611. * Further it needs to cope with the circular references problem, i.e. cannot
  1612. * load any attributes other than $ATTRIBUTE_LIST until $DATA is loaded, because
  1613. * we do not know where the other extent mft records are yet and again, because
  1614. * we cannot call map_mft_record*() yet. Obviously this applies only when an
  1615. * attribute list is actually present in $MFT inode.
  1616. *
  1617. * We solve these problems by starting with the $DATA attribute before anything
  1618. * else and iterating using ntfs_attr_lookup($DATA) over all extents. As each
  1619. * extent is found, we ntfs_mapping_pairs_decompress() including the implied
  1620. * ntfs_runlists_merge(). Each step of the iteration necessarily provides
  1621. * sufficient information for the next step to complete.
  1622. *
  1623. * This should work but there are two possible pit falls (see inline comments
  1624. * below), but only time will tell if they are real pits or just smoke...
  1625. */
  1626. int ntfs_read_inode_mount(struct inode *vi)
  1627. {
  1628. VCN next_vcn, last_vcn, highest_vcn;
  1629. s64 block;
  1630. struct super_block *sb = vi->i_sb;
  1631. ntfs_volume *vol = NTFS_SB(sb);
  1632. struct buffer_head *bh;
  1633. ntfs_inode *ni;
  1634. MFT_RECORD *m = NULL;
  1635. ATTR_RECORD *attr;
  1636. ntfs_attr_search_ctx *ctx;
  1637. unsigned int i, nr_blocks;
  1638. int err;
  1639. ntfs_debug("Entering.");
  1640. /* Initialize the ntfs specific part of @vi. */
  1641. ntfs_init_big_inode(vi);
  1642. ni = NTFS_I(vi);
  1643. /* Setup the data attribute. It is special as it is mst protected. */
  1644. NInoSetNonResident(ni);
  1645. NInoSetMstProtected(ni);
  1646. ni->type = AT_DATA;
  1647. ni->name = NULL;
  1648. ni->name_len = 0;
  1649. /*
  1650. * This sets up our little cheat allowing us to reuse the async read io
  1651. * completion handler for directories.
  1652. */
  1653. ni->itype.index.block_size = vol->mft_record_size;
  1654. ni->itype.index.block_size_bits = vol->mft_record_size_bits;
  1655. /* Very important! Needed to be able to call map_mft_record*(). */
  1656. vol->mft_ino = vi;
  1657. /* Allocate enough memory to read the first mft record. */
  1658. if (vol->mft_record_size > 64 * 1024) {
  1659. ntfs_error(sb, "Unsupported mft record size %i (max 64kiB).",
  1660. vol->mft_record_size);
  1661. goto err_out;
  1662. }
  1663. i = vol->mft_record_size;
  1664. if (i < sb->s_blocksize)
  1665. i = sb->s_blocksize;
  1666. m = (MFT_RECORD*)ntfs_malloc_nofs(i);
  1667. if (!m) {
  1668. ntfs_error(sb, "Failed to allocate buffer for $MFT record 0.");
  1669. goto err_out;
  1670. }
  1671. /* Determine the first block of the $MFT/$DATA attribute. */
  1672. block = vol->mft_lcn << vol->cluster_size_bits >>
  1673. sb->s_blocksize_bits;
  1674. nr_blocks = vol->mft_record_size >> sb->s_blocksize_bits;
  1675. if (!nr_blocks)
  1676. nr_blocks = 1;
  1677. /* Load $MFT/$DATA's first mft record. */
  1678. for (i = 0; i < nr_blocks; i++) {
  1679. bh = sb_bread(sb, block++);
  1680. if (!bh) {
  1681. ntfs_error(sb, "Device read failed.");
  1682. goto err_out;
  1683. }
  1684. memcpy((char*)m + (i << sb->s_blocksize_bits), bh->b_data,
  1685. sb->s_blocksize);
  1686. brelse(bh);
  1687. }
  1688. /* Apply the mst fixups. */
  1689. if (post_read_mst_fixup((NTFS_RECORD*)m, vol->mft_record_size)) {
  1690. /* FIXME: Try to use the $MFTMirr now. */
  1691. ntfs_error(sb, "MST fixup failed. $MFT is corrupt.");
  1692. goto err_out;
  1693. }
  1694. /* Need this to sanity check attribute list references to $MFT. */
  1695. vi->i_generation = ni->seq_no = le16_to_cpu(m->sequence_number);
  1696. /* Provides readpage() and sync_page() for map_mft_record(). */
  1697. vi->i_mapping->a_ops = &ntfs_mst_aops;
  1698. ctx = ntfs_attr_get_search_ctx(ni, m);
  1699. if (!ctx) {
  1700. err = -ENOMEM;
  1701. goto err_out;
  1702. }
  1703. /* Find the attribute list attribute if present. */
  1704. err = ntfs_attr_lookup(AT_ATTRIBUTE_LIST, NULL, 0, 0, 0, NULL, 0, ctx);
  1705. if (err) {
  1706. if (unlikely(err != -ENOENT)) {
  1707. ntfs_error(sb, "Failed to lookup attribute list "
  1708. "attribute. You should run chkdsk.");
  1709. goto put_err_out;
  1710. }
  1711. } else /* if (!err) */ {
  1712. ATTR_LIST_ENTRY *al_entry, *next_al_entry;
  1713. u8 *al_end;
  1714. ntfs_debug("Attribute list attribute found in $MFT.");
  1715. NInoSetAttrList(ni);
  1716. if (ctx->attr->flags & ATTR_IS_ENCRYPTED ||
  1717. ctx->attr->flags & ATTR_COMPRESSION_MASK ||
  1718. ctx->attr->flags & ATTR_IS_SPARSE) {
  1719. ntfs_error(sb, "Attribute list attribute is "
  1720. "compressed/encrypted/sparse. Not "
  1721. "allowed. $MFT is corrupt. You should "
  1722. "run chkdsk.");
  1723. goto put_err_out;
  1724. }
  1725. /* Now allocate memory for the attribute list. */
  1726. ni->attr_list_size = (u32)ntfs_attr_size(ctx->attr);
  1727. ni->attr_list = ntfs_malloc_nofs(ni->attr_list_size);
  1728. if (!ni->attr_list) {
  1729. ntfs_error(sb, "Not enough memory to allocate buffer "
  1730. "for attribute list.");
  1731. goto put_err_out;
  1732. }
  1733. if (ctx->attr->non_resident) {
  1734. NInoSetAttrListNonResident(ni);
  1735. if (ctx->attr->data.non_resident.lowest_vcn) {
  1736. ntfs_error(sb, "Attribute list has non zero "
  1737. "lowest_vcn. $MFT is corrupt. "
  1738. "You should run chkdsk.");
  1739. goto put_err_out;
  1740. }
  1741. /* Setup the runlist. */
  1742. ni->attr_list_rl.rl = ntfs_mapping_pairs_decompress(vol,
  1743. ctx->attr, NULL);
  1744. if (IS_ERR(ni->attr_list_rl.rl)) {
  1745. err = PTR_ERR(ni->attr_list_rl.rl);
  1746. ni->attr_list_rl.rl = NULL;
  1747. ntfs_error(sb, "Mapping pairs decompression "
  1748. "failed with error code %i.",
  1749. -err);
  1750. goto put_err_out;
  1751. }
  1752. /* Now load the attribute list. */
  1753. if ((err = load_attribute_list(vol, &ni->attr_list_rl,
  1754. ni->attr_list, ni->attr_list_size,
  1755. sle64_to_cpu(ctx->attr->data.
  1756. non_resident.initialized_size)))) {
  1757. ntfs_error(sb, "Failed to load attribute list "
  1758. "attribute with error code %i.",
  1759. -err);
  1760. goto put_err_out;
  1761. }
  1762. } else /* if (!ctx.attr->non_resident) */ {
  1763. if ((u8*)ctx->attr + le16_to_cpu(
  1764. ctx->attr->data.resident.value_offset) +
  1765. le32_to_cpu(
  1766. ctx->attr->data.resident.value_length) >
  1767. (u8*)ctx->mrec + vol->mft_record_size) {
  1768. ntfs_error(sb, "Corrupt attribute list "
  1769. "attribute.");
  1770. goto put_err_out;
  1771. }
  1772. /* Now copy the attribute list. */
  1773. memcpy(ni->attr_list, (u8*)ctx->attr + le16_to_cpu(
  1774. ctx->attr->data.resident.value_offset),
  1775. le32_to_cpu(
  1776. ctx->attr->data.resident.value_length));
  1777. }
  1778. /* The attribute list is now setup in memory. */
  1779. /*
  1780. * FIXME: I don't know if this case is actually possible.
  1781. * According to logic it is not possible but I have seen too
  1782. * many weird things in MS software to rely on logic... Thus we
  1783. * perform a manual search and make sure the first $MFT/$DATA
  1784. * extent is in the base inode. If it is not we abort with an
  1785. * error and if we ever see a report of this error we will need
  1786. * to do some magic in order to have the necessary mft record
  1787. * loaded and in the right place in the page cache. But
  1788. * hopefully logic will prevail and this never happens...
  1789. */
  1790. al_entry = (ATTR_LIST_ENTRY*)ni->attr_list;
  1791. al_end = (u8*)al_entry + ni->attr_list_size;
  1792. for (;; al_entry = next_al_entry) {
  1793. /* Out of bounds check. */
  1794. if ((u8*)al_entry < ni->attr_list ||
  1795. (u8*)al_entry > al_end)
  1796. goto em_put_err_out;
  1797. /* Catch the end of the attribute list. */
  1798. if ((u8*)al_entry == al_end)
  1799. goto em_put_err_out;
  1800. if (!al_entry->length)
  1801. goto em_put_err_out;
  1802. if ((u8*)al_entry + 6 > al_end || (u8*)al_entry +
  1803. le16_to_cpu(al_entry->length) > al_end)
  1804. goto em_put_err_out;
  1805. next_al_entry = (ATTR_LIST_ENTRY*)((u8*)al_entry +
  1806. le16_to_cpu(al_entry->length));
  1807. if (le32_to_cpu(al_entry->type) >
  1808. const_le32_to_cpu(AT_DATA))
  1809. goto em_put_err_out;
  1810. if (AT_DATA != al_entry->type)
  1811. continue;
  1812. /* We want an unnamed attribute. */
  1813. if (al_entry->name_length)
  1814. goto em_put_err_out;
  1815. /* Want the first entry, i.e. lowest_vcn == 0. */
  1816. if (al_entry->lowest_vcn)
  1817. goto em_put_err_out;
  1818. /* First entry has to be in the base mft record. */
  1819. if (MREF_LE(al_entry->mft_reference) != vi->i_ino) {
  1820. /* MFT references do not match, logic fails. */
  1821. ntfs_error(sb, "BUG: The first $DATA extent "
  1822. "of $MFT is not in the base "
  1823. "mft record. Please report "
  1824. "you saw this message to "
  1825. "linux-ntfs-dev@lists."
  1826. "sourceforge.net");
  1827. goto put_err_out;
  1828. } else {
  1829. /* Sequence numbers must match. */
  1830. if (MSEQNO_LE(al_entry->mft_reference) !=
  1831. ni->seq_no)
  1832. goto em_put_err_out;
  1833. /* Got it. All is ok. We can stop now. */
  1834. break;
  1835. }
  1836. }
  1837. }
  1838. ntfs_attr_reinit_search_ctx(ctx);
  1839. /* Now load all attribute extents. */
  1840. attr = NULL;
  1841. next_vcn = last_vcn = highest_vcn = 0;
  1842. while (!(err = ntfs_attr_lookup(AT_DATA, NULL, 0, 0, next_vcn, NULL, 0,
  1843. ctx))) {
  1844. runlist_element *nrl;
  1845. /* Cache the current attribute. */
  1846. attr = ctx->attr;
  1847. /* $MFT must be non-resident. */
  1848. if (!attr->non_resident) {
  1849. ntfs_error(sb, "$MFT must be non-resident but a "
  1850. "resident extent was found. $MFT is "
  1851. "corrupt. Run chkdsk.");
  1852. goto put_err_out;
  1853. }
  1854. /* $MFT must be uncompressed and unencrypted. */
  1855. if (attr->flags & ATTR_COMPRESSION_MASK ||
  1856. attr->flags & ATTR_IS_ENCRYPTED ||
  1857. attr->flags & ATTR_IS_SPARSE) {
  1858. ntfs_error(sb, "$MFT must be uncompressed, "
  1859. "non-sparse, and unencrypted but a "
  1860. "compressed/sparse/encrypted extent "
  1861. "was found. $MFT is corrupt. Run "
  1862. "chkdsk.");
  1863. goto put_err_out;
  1864. }
  1865. /*
  1866. * Decompress the mapping pairs array of this extent and merge
  1867. * the result into the existing runlist. No need for locking
  1868. * as we have exclusive access to the inode at this time and we
  1869. * are a mount in progress task, too.
  1870. */
  1871. nrl = ntfs_mapping_pairs_decompress(vol, attr, ni->runlist.rl);
  1872. if (IS_ERR(nrl)) {
  1873. ntfs_error(sb, "ntfs_mapping_pairs_decompress() "
  1874. "failed with error code %ld. $MFT is "
  1875. "corrupt.", PTR_ERR(nrl));
  1876. goto put_err_out;
  1877. }
  1878. ni->runlist.rl = nrl;
  1879. /* Are we in the first extent? */
  1880. if (!next_vcn) {
  1881. if (attr->data.non_resident.lowest_vcn) {
  1882. ntfs_error(sb, "First extent of $DATA "
  1883. "attribute has non zero "
  1884. "lowest_vcn. $MFT is corrupt. "
  1885. "You should run chkdsk.");
  1886. goto put_err_out;
  1887. }
  1888. /* Get the last vcn in the $DATA attribute. */
  1889. last_vcn = sle64_to_cpu(
  1890. attr->data.non_resident.allocated_size)
  1891. >> vol->cluster_size_bits;
  1892. /* Fill in the inode size. */
  1893. vi->i_size = sle64_to_cpu(
  1894. attr->data.non_resident.data_size);
  1895. ni->initialized_size = sle64_to_cpu(attr->data.
  1896. non_resident.initialized_size);
  1897. ni->allocated_size = sle64_to_cpu(
  1898. attr->data.non_resident.allocated_size);
  1899. /*
  1900. * Verify the number of mft records does not exceed
  1901. * 2^32 - 1.
  1902. */
  1903. if ((vi->i_size >> vol->mft_record_size_bits) >=
  1904. (1ULL << 32)) {
  1905. ntfs_error(sb, "$MFT is too big! Aborting.");
  1906. goto put_err_out;
  1907. }
  1908. /*
  1909. * We have got the first extent of the runlist for
  1910. * $MFT which means it is now relatively safe to call
  1911. * the normal ntfs_read_inode() function.
  1912. * Complete reading the inode, this will actually
  1913. * re-read the mft record for $MFT, this time entering
  1914. * it into the page cache with which we complete the
  1915. * kick start of the volume. It should be safe to do
  1916. * this now as the first extent of $MFT/$DATA is
  1917. * already known and we would hope that we don't need
  1918. * further extents in order to find the other
  1919. * attributes belonging to $MFT. Only time will tell if
  1920. * this is really the case. If not we will have to play
  1921. * magic at this point, possibly duplicating a lot of
  1922. * ntfs_read_inode() at this point. We will need to
  1923. * ensure we do enough of its work to be able to call
  1924. * ntfs_read_inode() on extents of $MFT/$DATA. But lets
  1925. * hope this never happens...
  1926. */
  1927. ntfs_read_locked_inode(vi);
  1928. if (is_bad_inode(vi)) {
  1929. ntfs_error(sb, "ntfs_read_inode() of $MFT "
  1930. "failed. BUG or corrupt $MFT. "
  1931. "Run chkdsk and if no errors "
  1932. "are found, please report you "
  1933. "saw this message to "
  1934. "linux-ntfs-dev@lists."
  1935. "sourceforge.net");
  1936. ntfs_attr_put_search_ctx(ctx);
  1937. /* Revert to the safe super operations. */
  1938. ntfs_free(m);
  1939. return -1;
  1940. }
  1941. /*
  1942. * Re-initialize some specifics about $MFT's inode as
  1943. * ntfs_read_inode() will have set up the default ones.
  1944. */
  1945. /* Set uid and gid to root. */
  1946. vi->i_uid = vi->i_gid = 0;
  1947. /* Regular file. No access for anyone. */
  1948. vi->i_mode = S_IFREG;
  1949. /* No VFS initiated operations allowed for $MFT. */
  1950. vi->i_op = &ntfs_empty_inode_ops;
  1951. vi->i_fop = &ntfs_empty_file_ops;
  1952. }
  1953. /* Get the lowest vcn for the next extent. */
  1954. highest_vcn = sle64_to_cpu(attr->data.non_resident.highest_vcn);
  1955. next_vcn = highest_vcn + 1;
  1956. /* Only one extent or error, which we catch below. */
  1957. if (next_vcn <= 0)
  1958. break;
  1959. /* Avoid endless loops due to corruption. */
  1960. if (next_vcn < sle64_to_cpu(
  1961. attr->data.non_resident.lowest_vcn)) {
  1962. ntfs_error(sb, "$MFT has corrupt attribute list "
  1963. "attribute. Run chkdsk.");
  1964. goto put_err_out;
  1965. }
  1966. }
  1967. if (err != -ENOENT) {
  1968. ntfs_error(sb, "Failed to lookup $MFT/$DATA attribute extent. "
  1969. "$MFT is corrupt. Run chkdsk.");
  1970. goto put_err_out;
  1971. }
  1972. if (!attr) {
  1973. ntfs_error(sb, "$MFT/$DATA attribute not found. $MFT is "
  1974. "corrupt. Run chkdsk.");
  1975. goto put_err_out;
  1976. }
  1977. if (highest_vcn && highest_vcn != last_vcn - 1) {
  1978. ntfs_error(sb, "Failed to load the complete runlist for "
  1979. "$MFT/$DATA. Driver bug or corrupt $MFT. "
  1980. "Run chkdsk.");
  1981. ntfs_debug("highest_vcn = 0x%llx, last_vcn - 1 = 0x%llx",
  1982. (unsigned long long)highest_vcn,
  1983. (unsigned long long)last_vcn - 1);
  1984. goto put_err_out;
  1985. }
  1986. ntfs_attr_put_search_ctx(ctx);
  1987. ntfs_debug("Done.");
  1988. ntfs_free(m);
  1989. return 0;
  1990. em_put_err_out:
  1991. ntfs_error(sb, "Couldn't find first extent of $DATA attribute in "
  1992. "attribute list. $MFT is corrupt. Run chkdsk.");
  1993. put_err_out:
  1994. ntfs_attr_put_search_ctx(ctx);
  1995. err_out:
  1996. ntfs_error(sb, "Failed. Marking inode as bad.");
  1997. make_bad_inode(vi);
  1998. ntfs_free(m);
  1999. return -1;
  2000. }
  2001. /**
  2002. * ntfs_put_inode - handler for when the inode reference count is decremented
  2003. * @vi: vfs inode
  2004. *
  2005. * The VFS calls ntfs_put_inode() every time the inode reference count (i_count)
  2006. * is about to be decremented (but before the decrement itself.
  2007. *
  2008. * If the inode @vi is a directory with two references, one of which is being
  2009. * dropped, we need to put the attribute inode for the directory index bitmap,
  2010. * if it is present, otherwise the directory inode would remain pinned for
  2011. * ever.
  2012. */
  2013. void ntfs_put_inode(struct inode *vi)
  2014. {
  2015. if (S_ISDIR(vi->i_mode) && atomic_read(&vi->i_count) == 2) {
  2016. ntfs_inode *ni = NTFS_I(vi);
  2017. if (NInoIndexAllocPresent(ni)) {
  2018. struct inode *bvi = NULL;
  2019. down(&vi->i_sem);
  2020. if (atomic_read(&vi->i_count) == 2) {
  2021. bvi = ni->itype.index.bmp_ino;
  2022. if (bvi)
  2023. ni->itype.index.bmp_ino = NULL;
  2024. }
  2025. up(&vi->i_sem);
  2026. if (bvi)
  2027. iput(bvi);
  2028. }
  2029. }
  2030. }
  2031. static void __ntfs_clear_inode(ntfs_inode *ni)
  2032. {
  2033. /* Free all alocated memory. */
  2034. down_write(&ni->runlist.lock);
  2035. if (ni->runlist.rl) {
  2036. ntfs_free(ni->runlist.rl);
  2037. ni->runlist.rl = NULL;
  2038. }
  2039. up_write(&ni->runlist.lock);
  2040. if (ni->attr_list) {
  2041. ntfs_free(ni->attr_list);
  2042. ni->attr_list = NULL;
  2043. }
  2044. down_write(&ni->attr_list_rl.lock);
  2045. if (ni->attr_list_rl.rl) {
  2046. ntfs_free(ni->attr_list_rl.rl);
  2047. ni->attr_list_rl.rl = NULL;
  2048. }
  2049. up_write(&ni->attr_list_rl.lock);
  2050. if (ni->name_len && ni->name != I30) {
  2051. /* Catch bugs... */
  2052. BUG_ON(!ni->name);
  2053. kfree(ni->name);
  2054. }
  2055. }
  2056. void ntfs_clear_extent_inode(ntfs_inode *ni)
  2057. {
  2058. ntfs_debug("Entering for inode 0x%lx.", ni->mft_no);
  2059. BUG_ON(NInoAttr(ni));
  2060. BUG_ON(ni->nr_extents != -1);
  2061. #ifdef NTFS_RW
  2062. if (NInoDirty(ni)) {
  2063. if (!is_bad_inode(VFS_I(ni->ext.base_ntfs_ino)))
  2064. ntfs_error(ni->vol->sb, "Clearing dirty extent inode! "
  2065. "Losing data! This is a BUG!!!");
  2066. // FIXME: Do something!!!
  2067. }
  2068. #endif /* NTFS_RW */
  2069. __ntfs_clear_inode(ni);
  2070. /* Bye, bye... */
  2071. ntfs_destroy_extent_inode(ni);
  2072. }
  2073. /**
  2074. * ntfs_clear_big_inode - clean up the ntfs specific part of an inode
  2075. * @vi: vfs inode pending annihilation
  2076. *
  2077. * When the VFS is going to remove an inode from memory, ntfs_clear_big_inode()
  2078. * is called, which deallocates all memory belonging to the NTFS specific part
  2079. * of the inode and returns.
  2080. *
  2081. * If the MFT record is dirty, we commit it before doing anything else.
  2082. */
  2083. void ntfs_clear_big_inode(struct inode *vi)
  2084. {
  2085. ntfs_inode *ni = NTFS_I(vi);
  2086. /*
  2087. * If the inode @vi is an index inode we need to put the attribute
  2088. * inode for the index bitmap, if it is present, otherwise the index
  2089. * inode would disappear and the attribute inode for the index bitmap
  2090. * would no longer be referenced from anywhere and thus it would remain
  2091. * pinned for ever.
  2092. */
  2093. if (NInoAttr(ni) && (ni->type == AT_INDEX_ALLOCATION) &&
  2094. NInoIndexAllocPresent(ni) && ni->itype.index.bmp_ino) {
  2095. iput(ni->itype.index.bmp_ino);
  2096. ni->itype.index.bmp_ino = NULL;
  2097. }
  2098. #ifdef NTFS_RW
  2099. if (NInoDirty(ni)) {
  2100. BOOL was_bad = (is_bad_inode(vi));
  2101. /* Committing the inode also commits all extent inodes. */
  2102. ntfs_commit_inode(vi);
  2103. if (!was_bad && (is_bad_inode(vi) || NInoDirty(ni))) {
  2104. ntfs_error(vi->i_sb, "Failed to commit dirty inode "
  2105. "0x%lx. Losing data!", vi->i_ino);
  2106. // FIXME: Do something!!!
  2107. }
  2108. }
  2109. #endif /* NTFS_RW */
  2110. /* No need to lock at this stage as no one else has a reference. */
  2111. if (ni->nr_extents > 0) {
  2112. int i;
  2113. for (i = 0; i < ni->nr_extents; i++)
  2114. ntfs_clear_extent_inode(ni->ext.extent_ntfs_inos[i]);
  2115. kfree(ni->ext.extent_ntfs_inos);
  2116. }
  2117. __ntfs_clear_inode(ni);
  2118. if (NInoAttr(ni)) {
  2119. /* Release the base inode if we are holding it. */
  2120. if (ni->nr_extents == -1) {
  2121. iput(VFS_I(ni->ext.base_ntfs_ino));
  2122. ni->nr_extents = 0;
  2123. ni->ext.base_ntfs_ino = NULL;
  2124. }
  2125. }
  2126. return;
  2127. }
  2128. /**
  2129. * ntfs_show_options - show mount options in /proc/mounts
  2130. * @sf: seq_file in which to write our mount options
  2131. * @mnt: vfs mount whose mount options to display
  2132. *
  2133. * Called by the VFS once for each mounted ntfs volume when someone reads
  2134. * /proc/mounts in order to display the NTFS specific mount options of each
  2135. * mount. The mount options of the vfs mount @mnt are written to the seq file
  2136. * @sf and success is returned.
  2137. */
  2138. int ntfs_show_options(struct seq_file *sf, struct vfsmount *mnt)
  2139. {
  2140. ntfs_volume *vol = NTFS_SB(mnt->mnt_sb);
  2141. int i;
  2142. seq_printf(sf, ",uid=%i", vol->uid);
  2143. seq_printf(sf, ",gid=%i", vol->gid);
  2144. if (vol->fmask == vol->dmask)
  2145. seq_printf(sf, ",umask=0%o", vol->fmask);
  2146. else {
  2147. seq_printf(sf, ",fmask=0%o", vol->fmask);
  2148. seq_printf(sf, ",dmask=0%o", vol->dmask);
  2149. }
  2150. seq_printf(sf, ",nls=%s", vol->nls_map->charset);
  2151. if (NVolCaseSensitive(vol))
  2152. seq_printf(sf, ",case_sensitive");
  2153. if (NVolShowSystemFiles(vol))
  2154. seq_printf(sf, ",show_sys_files");
  2155. for (i = 0; on_errors_arr[i].val; i++) {
  2156. if (on_errors_arr[i].val & vol->on_errors)
  2157. seq_printf(sf, ",errors=%s", on_errors_arr[i].str);
  2158. }
  2159. seq_printf(sf, ",mft_zone_multiplier=%i", vol->mft_zone_multiplier);
  2160. return 0;
  2161. }
  2162. #ifdef NTFS_RW
  2163. /**
  2164. * ntfs_truncate - called when the i_size of an ntfs inode is changed
  2165. * @vi: inode for which the i_size was changed
  2166. *
  2167. * We do not support i_size changes yet.
  2168. *
  2169. * The kernel guarantees that @vi is a regular file (S_ISREG() is true) and
  2170. * that the change is allowed.
  2171. *
  2172. * This implies for us that @vi is a file inode rather than a directory, index,
  2173. * or attribute inode as well as that @vi is a base inode.
  2174. *
  2175. * Returns 0 on success or -errno on error.
  2176. *
  2177. * Called with ->i_sem held. In all but one case ->i_alloc_sem is held for
  2178. * writing. The only case where ->i_alloc_sem is not held is
  2179. * mm/filemap.c::generic_file_buffered_write() where vmtruncate() is called
  2180. * with the current i_size as the offset which means that it is a noop as far
  2181. * as ntfs_truncate() is concerned.
  2182. */
  2183. int ntfs_truncate(struct inode *vi)
  2184. {
  2185. ntfs_inode *ni = NTFS_I(vi);
  2186. ntfs_volume *vol = ni->vol;
  2187. ntfs_attr_search_ctx *ctx;
  2188. MFT_RECORD *m;
  2189. const char *te = " Leaving file length out of sync with i_size.";
  2190. int err;
  2191. ntfs_debug("Entering for inode 0x%lx.", vi->i_ino);
  2192. BUG_ON(NInoAttr(ni));
  2193. BUG_ON(ni->nr_extents < 0);
  2194. m = map_mft_record(ni);
  2195. if (IS_ERR(m)) {
  2196. err = PTR_ERR(m);
  2197. ntfs_error(vi->i_sb, "Failed to map mft record for inode 0x%lx "
  2198. "(error code %d).%s", vi->i_ino, err, te);
  2199. ctx = NULL;
  2200. m = NULL;
  2201. goto err_out;
  2202. }
  2203. ctx = ntfs_attr_get_search_ctx(ni, m);
  2204. if (unlikely(!ctx)) {
  2205. ntfs_error(vi->i_sb, "Failed to allocate a search context for "
  2206. "inode 0x%lx (not enough memory).%s",
  2207. vi->i_ino, te);
  2208. err = -ENOMEM;
  2209. goto err_out;
  2210. }
  2211. err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
  2212. CASE_SENSITIVE, 0, NULL, 0, ctx);
  2213. if (unlikely(err)) {
  2214. if (err == -ENOENT)
  2215. ntfs_error(vi->i_sb, "Open attribute is missing from "
  2216. "mft record. Inode 0x%lx is corrupt. "
  2217. "Run chkdsk.", vi->i_ino);
  2218. else
  2219. ntfs_error(vi->i_sb, "Failed to lookup attribute in "
  2220. "inode 0x%lx (error code %d).",
  2221. vi->i_ino, err);
  2222. goto err_out;
  2223. }
  2224. /* If the size has not changed there is nothing to do. */
  2225. if (ntfs_attr_size(ctx->attr) == i_size_read(vi))
  2226. goto done;
  2227. // TODO: Implement the truncate...
  2228. ntfs_error(vi->i_sb, "Inode size has changed but this is not "
  2229. "implemented yet. Resetting inode size to old value. "
  2230. " This is most likely a bug in the ntfs driver!");
  2231. i_size_write(vi, ntfs_attr_size(ctx->attr));
  2232. done:
  2233. ntfs_attr_put_search_ctx(ctx);
  2234. unmap_mft_record(ni);
  2235. NInoClearTruncateFailed(ni);
  2236. ntfs_debug("Done.");
  2237. return 0;
  2238. err_out:
  2239. if (err != -ENOMEM) {
  2240. NVolSetErrors(vol);
  2241. make_bad_inode(vi);
  2242. }
  2243. if (ctx)
  2244. ntfs_attr_put_search_ctx(ctx);
  2245. if (m)
  2246. unmap_mft_record(ni);
  2247. NInoSetTruncateFailed(ni);
  2248. return err;
  2249. }
  2250. /**
  2251. * ntfs_truncate_vfs - wrapper for ntfs_truncate() that has no return value
  2252. * @vi: inode for which the i_size was changed
  2253. *
  2254. * Wrapper for ntfs_truncate() that has no return value.
  2255. *
  2256. * See ntfs_truncate() description above for details.
  2257. */
  2258. void ntfs_truncate_vfs(struct inode *vi) {
  2259. ntfs_truncate(vi);
  2260. }
  2261. /**
  2262. * ntfs_setattr - called from notify_change() when an attribute is being changed
  2263. * @dentry: dentry whose attributes to change
  2264. * @attr: structure describing the attributes and the changes
  2265. *
  2266. * We have to trap VFS attempts to truncate the file described by @dentry as
  2267. * soon as possible, because we do not implement changes in i_size yet. So we
  2268. * abort all i_size changes here.
  2269. *
  2270. * We also abort all changes of user, group, and mode as we do not implement
  2271. * the NTFS ACLs yet.
  2272. *
  2273. * Called with ->i_sem held. For the ATTR_SIZE (i.e. ->truncate) case, also
  2274. * called with ->i_alloc_sem held for writing.
  2275. *
  2276. * Basically this is a copy of generic notify_change() and inode_setattr()
  2277. * functionality, except we intercept and abort changes in i_size.
  2278. */
  2279. int ntfs_setattr(struct dentry *dentry, struct iattr *attr)
  2280. {
  2281. struct inode *vi = dentry->d_inode;
  2282. int err;
  2283. unsigned int ia_valid = attr->ia_valid;
  2284. err = inode_change_ok(vi, attr);
  2285. if (err)
  2286. return err;
  2287. /* We do not support NTFS ACLs yet. */
  2288. if (ia_valid & (ATTR_UID | ATTR_GID | ATTR_MODE)) {
  2289. ntfs_warning(vi->i_sb, "Changes in user/group/mode are not "
  2290. "supported yet, ignoring.");
  2291. err = -EOPNOTSUPP;
  2292. goto out;
  2293. }
  2294. if (ia_valid & ATTR_SIZE) {
  2295. if (attr->ia_size != i_size_read(vi)) {
  2296. ntfs_warning(vi->i_sb, "Changes in inode size are not "
  2297. "supported yet, ignoring.");
  2298. err = -EOPNOTSUPP;
  2299. // TODO: Implement...
  2300. // err = vmtruncate(vi, attr->ia_size);
  2301. if (err || ia_valid == ATTR_SIZE)
  2302. goto out;
  2303. } else {
  2304. /*
  2305. * We skipped the truncate but must still update
  2306. * timestamps.
  2307. */
  2308. ia_valid |= ATTR_MTIME|ATTR_CTIME;
  2309. }
  2310. }
  2311. if (ia_valid & ATTR_ATIME)
  2312. vi->i_atime = attr->ia_atime;
  2313. if (ia_valid & ATTR_MTIME)
  2314. vi->i_mtime = attr->ia_mtime;
  2315. if (ia_valid & ATTR_CTIME)
  2316. vi->i_ctime = attr->ia_ctime;
  2317. mark_inode_dirty(vi);
  2318. out:
  2319. return err;
  2320. }
  2321. /**
  2322. * ntfs_write_inode - write out a dirty inode
  2323. * @vi: inode to write out
  2324. * @sync: if true, write out synchronously
  2325. *
  2326. * Write out a dirty inode to disk including any extent inodes if present.
  2327. *
  2328. * If @sync is true, commit the inode to disk and wait for io completion. This
  2329. * is done using write_mft_record().
  2330. *
  2331. * If @sync is false, just schedule the write to happen but do not wait for i/o
  2332. * completion. In 2.6 kernels, scheduling usually happens just by virtue of
  2333. * marking the page (and in this case mft record) dirty but we do not implement
  2334. * this yet as write_mft_record() largely ignores the @sync parameter and
  2335. * always performs synchronous writes.
  2336. *
  2337. * Return 0 on success and -errno on error.
  2338. */
  2339. int ntfs_write_inode(struct inode *vi, int sync)
  2340. {
  2341. sle64 nt;
  2342. ntfs_inode *ni = NTFS_I(vi);
  2343. ntfs_attr_search_ctx *ctx;
  2344. MFT_RECORD *m;
  2345. STANDARD_INFORMATION *si;
  2346. int err = 0;
  2347. BOOL modified = FALSE;
  2348. ntfs_debug("Entering for %sinode 0x%lx.", NInoAttr(ni) ? "attr " : "",
  2349. vi->i_ino);
  2350. /*
  2351. * Dirty attribute inodes are written via their real inodes so just
  2352. * clean them here. Access time updates are taken care off when the
  2353. * real inode is written.
  2354. */
  2355. if (NInoAttr(ni)) {
  2356. NInoClearDirty(ni);
  2357. ntfs_debug("Done.");
  2358. return 0;
  2359. }
  2360. /* Map, pin, and lock the mft record belonging to the inode. */
  2361. m = map_mft_record(ni);
  2362. if (IS_ERR(m)) {
  2363. err = PTR_ERR(m);
  2364. goto err_out;
  2365. }
  2366. /* Update the access times in the standard information attribute. */
  2367. ctx = ntfs_attr_get_search_ctx(ni, m);
  2368. if (unlikely(!ctx)) {
  2369. err = -ENOMEM;
  2370. goto unm_err_out;
  2371. }
  2372. err = ntfs_attr_lookup(AT_STANDARD_INFORMATION, NULL, 0,
  2373. CASE_SENSITIVE, 0, NULL, 0, ctx);
  2374. if (unlikely(err)) {
  2375. ntfs_attr_put_search_ctx(ctx);
  2376. goto unm_err_out;
  2377. }
  2378. si = (STANDARD_INFORMATION*)((u8*)ctx->attr +
  2379. le16_to_cpu(ctx->attr->data.resident.value_offset));
  2380. /* Update the access times if they have changed. */
  2381. nt = utc2ntfs(vi->i_mtime);
  2382. if (si->last_data_change_time != nt) {
  2383. ntfs_debug("Updating mtime for inode 0x%lx: old = 0x%llx, "
  2384. "new = 0x%llx", vi->i_ino,
  2385. sle64_to_cpu(si->last_data_change_time),
  2386. sle64_to_cpu(nt));
  2387. si->last_data_change_time = nt;
  2388. modified = TRUE;
  2389. }
  2390. nt = utc2ntfs(vi->i_ctime);
  2391. if (si->last_mft_change_time != nt) {
  2392. ntfs_debug("Updating ctime for inode 0x%lx: old = 0x%llx, "
  2393. "new = 0x%llx", vi->i_ino,
  2394. sle64_to_cpu(si->last_mft_change_time),
  2395. sle64_to_cpu(nt));
  2396. si->last_mft_change_time = nt;
  2397. modified = TRUE;
  2398. }
  2399. nt = utc2ntfs(vi->i_atime);
  2400. if (si->last_access_time != nt) {
  2401. ntfs_debug("Updating atime for inode 0x%lx: old = 0x%llx, "
  2402. "new = 0x%llx", vi->i_ino,
  2403. sle64_to_cpu(si->last_access_time),
  2404. sle64_to_cpu(nt));
  2405. si->last_access_time = nt;
  2406. modified = TRUE;
  2407. }
  2408. /*
  2409. * If we just modified the standard information attribute we need to
  2410. * mark the mft record it is in dirty. We do this manually so that
  2411. * mark_inode_dirty() is not called which would redirty the inode and
  2412. * hence result in an infinite loop of trying to write the inode.
  2413. * There is no need to mark the base inode nor the base mft record
  2414. * dirty, since we are going to write this mft record below in any case
  2415. * and the base mft record may actually not have been modified so it
  2416. * might not need to be written out.
  2417. * NOTE: It is not a problem when the inode for $MFT itself is being
  2418. * written out as mark_ntfs_record_dirty() will only set I_DIRTY_PAGES
  2419. * on the $MFT inode and hence ntfs_write_inode() will not be
  2420. * re-invoked because of it which in turn is ok since the dirtied mft
  2421. * record will be cleaned and written out to disk below, i.e. before
  2422. * this function returns.
  2423. */
  2424. if (modified && !NInoTestSetDirty(ctx->ntfs_ino))
  2425. mark_ntfs_record_dirty(ctx->ntfs_ino->page,
  2426. ctx->ntfs_ino->page_ofs);
  2427. ntfs_attr_put_search_ctx(ctx);
  2428. /* Now the access times are updated, write the base mft record. */
  2429. if (NInoDirty(ni))
  2430. err = write_mft_record(ni, m, sync);
  2431. /* Write all attached extent mft records. */
  2432. down(&ni->extent_lock);
  2433. if (ni->nr_extents > 0) {
  2434. ntfs_inode **extent_nis = ni->ext.extent_ntfs_inos;
  2435. int i;
  2436. ntfs_debug("Writing %i extent inodes.", ni->nr_extents);
  2437. for (i = 0; i < ni->nr_extents; i++) {
  2438. ntfs_inode *tni = extent_nis[i];
  2439. if (NInoDirty(tni)) {
  2440. MFT_RECORD *tm = map_mft_record(tni);
  2441. int ret;
  2442. if (IS_ERR(tm)) {
  2443. if (!err || err == -ENOMEM)
  2444. err = PTR_ERR(tm);
  2445. continue;
  2446. }
  2447. ret = write_mft_record(tni, tm, sync);
  2448. unmap_mft_record(tni);
  2449. if (unlikely(ret)) {
  2450. if (!err || err == -ENOMEM)
  2451. err = ret;
  2452. }
  2453. }
  2454. }
  2455. }
  2456. up(&ni->extent_lock);
  2457. unmap_mft_record(ni);
  2458. if (unlikely(err))
  2459. goto err_out;
  2460. ntfs_debug("Done.");
  2461. return 0;
  2462. unm_err_out:
  2463. unmap_mft_record(ni);
  2464. err_out:
  2465. if (err == -ENOMEM) {
  2466. ntfs_warning(vi->i_sb, "Not enough memory to write inode. "
  2467. "Marking the inode dirty again, so the VFS "
  2468. "retries later.");
  2469. mark_inode_dirty(vi);
  2470. } else {
  2471. ntfs_error(vi->i_sb, "Failed (error code %i): Marking inode "
  2472. "as bad. You should run chkdsk.", -err);
  2473. make_bad_inode(vi);
  2474. NVolSetErrors(ni->vol);
  2475. }
  2476. return err;
  2477. }
  2478. #endif /* NTFS_RW */