inode.c 80 KB

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