original development tree for Linux kernel GTP module; now long in mainline.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3111 lines
97 KiB

  1. /**
  2. * inode.c - NTFS kernel inode handling. Part of the Linux-NTFS project.
  3. *
  4. * Copyright (c) 2001-2007 Anton Altaparmakov
  5. *
  6. * This program/include file is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as published
  8. * by the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program/include file is distributed in the hope that it will be
  12. * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  13. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program (in the main directory of the Linux-NTFS
  18. * distribution in the file COPYING); if not, write to the Free Software
  19. * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/buffer_head.h>
  22. #include <linux/fs.h>
  23. #include <linux/mm.h>
  24. #include <linux/mount.h>
  25. #include <linux/mutex.h>
  26. #include <linux/pagemap.h>
  27. #include <linux/quotaops.h>
  28. #include <linux/slab.h>
  29. #include <linux/log2.h>
  30. #include <linux/aio.h>
  31. #include "aops.h"
  32. #include "attrib.h"
  33. #include "bitmap.h"
  34. #include "dir.h"
  35. #include "debug.h"
  36. #include "inode.h"
  37. #include "lcnalloc.h"
  38. #include "malloc.h"
  39. #include "mft.h"
  40. #include "time.h"
  41. #include "ntfs.h"
  42. /**
  43. * ntfs_test_inode - compare two (possibly fake) inodes for equality
  44. * @vi: vfs inode which to test
  45. * @na: ntfs attribute which is being tested with
  46. *
  47. * Compare the ntfs attribute embedded in the ntfs specific part of the vfs
  48. * inode @vi for equality with the ntfs attribute @na.
  49. *
  50. * If searching for the normal file/directory inode, set @na->type to AT_UNUSED.
  51. * @na->name and @na->name_len are then ignored.
  52. *
  53. * Return 1 if the attributes match and 0 if not.
  54. *
  55. * NOTE: This function runs with the inode_hash_lock spin lock held so it is not
  56. * allowed to sleep.
  57. */
  58. int ntfs_test_inode(struct inode *vi, ntfs_attr *na)
  59. {
  60. ntfs_inode *ni;
  61. if (vi->i_ino != na->mft_no)
  62. return 0;
  63. ni = NTFS_I(vi);
  64. /* If !NInoAttr(ni), @vi is a normal file or directory inode. */
  65. if (likely(!NInoAttr(ni))) {
  66. /* If not looking for a normal inode this is a mismatch. */
  67. if (unlikely(na->type != AT_UNUSED))
  68. return 0;
  69. } else {
  70. /* A fake inode describing an attribute. */
  71. if (ni->type != na->type)
  72. return 0;
  73. if (ni->name_len != na->name_len)
  74. return 0;
  75. if (na->name_len && memcmp(ni->name, na->name,
  76. na->name_len * sizeof(ntfschar)))
  77. return 0;
  78. }
  79. /* Match! */
  80. return 1;
  81. }
  82. /**
  83. * ntfs_init_locked_inode - initialize an inode
  84. * @vi: vfs inode to initialize
  85. * @na: ntfs attribute which to initialize @vi to
  86. *
  87. * Initialize the vfs inode @vi with the values from the ntfs attribute @na in
  88. * order to enable ntfs_test_inode() to do its work.
  89. *
  90. * If initializing the normal file/directory inode, set @na->type to AT_UNUSED.
  91. * In that case, @na->name and @na->name_len should be set to NULL and 0,
  92. * respectively. Although that is not strictly necessary as
  93. * ntfs_read_locked_inode() will fill them in later.
  94. *
  95. * Return 0 on success and -errno on error.
  96. *
  97. * NOTE: This function runs with the inode->i_lock spin lock held so it is not
  98. * allowed to sleep. (Hence the GFP_ATOMIC allocation.)
  99. */
  100. static int ntfs_init_locked_inode(struct inode *vi, ntfs_attr *na)
  101. {
  102. ntfs_inode *ni = NTFS_I(vi);
  103. vi->i_ino = na->mft_no;
  104. ni->type = na->type;
  105. if (na->type == AT_INDEX_ALLOCATION)
  106. NInoSetMstProtected(ni);
  107. ni->name = na->name;
  108. ni->name_len = na->name_len;
  109. /* If initializing a normal inode, we are done. */
  110. if (likely(na->type == AT_UNUSED)) {
  111. BUG_ON(na->name);
  112. BUG_ON(na->name_len);
  113. return 0;
  114. }
  115. /* It is a fake inode. */
  116. NInoSetAttr(ni);
  117. /*
  118. * We have I30 global constant as an optimization as it is the name
  119. * in >99.9% of named attributes! The other <0.1% incur a GFP_ATOMIC
  120. * allocation but that is ok. And most attributes are unnamed anyway,
  121. * thus the fraction of named attributes with name != I30 is actually
  122. * absolutely tiny.
  123. */
  124. if (na->name_len && na->name != I30) {
  125. unsigned int i;
  126. BUG_ON(!na->name);
  127. i = na->name_len * sizeof(ntfschar);
  128. ni->name = kmalloc(i + sizeof(ntfschar), GFP_ATOMIC);
  129. if (!ni->name)
  130. return -ENOMEM;
  131. memcpy(ni->name, na->name, i);
  132. ni->name[na->name_len] = 0;
  133. }
  134. return 0;
  135. }
  136. typedef int (*set_t)(struct inode *, void *);
  137. static int ntfs_read_locked_inode(struct inode *vi);
  138. static int ntfs_read_locked_attr_inode(struct inode *base_vi, struct inode *vi);
  139. static int ntfs_read_locked_index_inode(struct inode *base_vi,
  140. struct inode *vi);
  141. /**
  142. * ntfs_iget - obtain a struct inode corresponding to a specific normal inode
  143. * @sb: super block of mounted volume
  144. * @mft_no: mft record number / inode number to obtain
  145. *
  146. * Obtain the struct inode corresponding to a specific normal inode (i.e. a
  147. * file or directory).
  148. *
  149. * If the inode is in the cache, it is just returned with an increased
  150. * reference count. Otherwise, a new struct inode is allocated and initialized,
  151. * and finally ntfs_read_locked_inode() is called to read in the inode and
  152. * fill in the remainder of the inode structure.
  153. *
  154. * Return the struct inode on success. Check the return value with IS_ERR() and
  155. * if true, the function failed and the error code is obtained from PTR_ERR().
  156. */
  157. struct inode *ntfs_iget(struct super_block *sb, unsigned long mft_no)
  158. {
  159. struct inode *vi;
  160. int err;
  161. ntfs_attr na;
  162. na.mft_no = mft_no;
  163. na.type = AT_UNUSED;
  164. na.name = NULL;
  165. na.name_len = 0;
  166. vi = iget5_locked(sb, mft_no, (test_t)ntfs_test_inode,
  167. (set_t)ntfs_init_locked_inode, &na);
  168. if (unlikely(!vi))
  169. return ERR_PTR(-ENOMEM);
  170. err = 0;
  171. /* If this is a freshly allocated inode, need to read it now. */
  172. if (vi->i_state & I_NEW) {
  173. err = ntfs_read_locked_inode(vi);
  174. unlock_new_inode(vi);
  175. }
  176. /*
  177. * There is no point in keeping bad inodes around if the failure was
  178. * due to ENOMEM. We want to be able to retry again later.
  179. */
  180. if (unlikely(err == -ENOMEM)) {
  181. iput(vi);
  182. vi = ERR_PTR(err);
  183. }
  184. return vi;
  185. }
  186. /**
  187. * ntfs_attr_iget - obtain a struct inode corresponding to an attribute
  188. * @base_vi: vfs base inode containing the attribute
  189. * @type: attribute type
  190. * @name: Unicode name of the attribute (NULL if unnamed)
  191. * @name_len: length of @name in Unicode characters (0 if unnamed)
  192. *
  193. * Obtain the (fake) struct inode corresponding to the attribute specified by
  194. * @type, @name, and @name_len, which is present in the base mft record
  195. * specified by the vfs inode @base_vi.
  196. *
  197. * If the attribute inode is in the cache, it is just returned with an
  198. * increased reference count. Otherwise, a new struct inode is allocated and
  199. * initialized, and finally ntfs_read_locked_attr_inode() is called to read the
  200. * attribute and fill in the inode structure.
  201. *
  202. * Note, for index allocation attributes, you need to use ntfs_index_iget()
  203. * instead of ntfs_attr_iget() as working with indices is a lot more complex.
  204. *
  205. * Return the struct inode of the attribute inode on success. Check the return
  206. * value with IS_ERR() and if true, the function failed and the error code is
  207. * obtained from PTR_ERR().
  208. */
  209. struct inode *ntfs_attr_iget(struct inode *base_vi, ATTR_TYPE type,
  210. ntfschar *name, u32 name_len)
  211. {
  212. struct inode *vi;
  213. int err;
  214. ntfs_attr na;
  215. /* Make sure no one calls ntfs_attr_iget() for indices. */
  216. BUG_ON(type == AT_INDEX_ALLOCATION);
  217. na.mft_no = base_vi->i_ino;
  218. na.type = type;
  219. na.name = name;
  220. na.name_len = name_len;
  221. vi = iget5_locked(base_vi->i_sb, na.mft_no, (test_t)ntfs_test_inode,
  222. (set_t)ntfs_init_locked_inode, &na);
  223. if (unlikely(!vi))
  224. return ERR_PTR(-ENOMEM);
  225. err = 0;
  226. /* If this is a freshly allocated inode, need to read it now. */
  227. if (vi->i_state & I_NEW) {
  228. err = ntfs_read_locked_attr_inode(base_vi, vi);
  229. unlock_new_inode(vi);
  230. }
  231. /*
  232. * There is no point in keeping bad attribute inodes around. This also
  233. * simplifies things in that we never need to check for bad attribute
  234. * inodes elsewhere.
  235. */
  236. if (unlikely(err)) {
  237. iput(vi);
  238. vi = ERR_PTR(err);
  239. }
  240. return vi;
  241. }
  242. /**
  243. * ntfs_index_iget - obtain a struct inode corresponding to an index
  244. * @base_vi: vfs base inode containing the index related attributes
  245. * @name: Unicode name of the index
  246. * @name_len: length of @name in Unicode characters
  247. *
  248. * Obtain the (fake) struct inode corresponding to the index specified by @name
  249. * and @name_len, which is present in the base mft record specified by the vfs
  250. * inode @base_vi.
  251. *
  252. * If the index inode is in the cache, it is just returned with an increased
  253. * reference count. Otherwise, a new struct inode is allocated and
  254. * initialized, and finally ntfs_read_locked_index_inode() is called to read
  255. * the index related attributes and fill in the inode structure.
  256. *
  257. * Return the struct inode of the index inode on success. Check the return
  258. * value with IS_ERR() and if true, the function failed and the error code is
  259. * obtained from PTR_ERR().
  260. */
  261. struct inode *ntfs_index_iget(struct inode *base_vi, ntfschar *name,
  262. u32 name_len)
  263. {
  264. struct inode *vi;
  265. int err;
  266. ntfs_attr na;
  267. na.mft_no = base_vi->i_ino;
  268. na.type = AT_INDEX_ALLOCATION;
  269. na.name = name;
  270. na.name_len = name_len;
  271. vi = iget5_locked(base_vi->i_sb, na.mft_no, (test_t)ntfs_test_inode,
  272. (set_t)ntfs_init_locked_inode, &na);
  273. if (unlikely(!vi))
  274. return ERR_PTR(-ENOMEM);
  275. err = 0;
  276. /* If this is a freshly allocated inode, need to read it now. */
  277. if (vi->i_state & I_NEW) {
  278. err = ntfs_read_locked_index_inode(base_vi, vi);
  279. unlock_new_inode(vi);
  280. }
  281. /*
  282. * There is no point in keeping bad index inodes around. This also
  283. * simplifies things in that we never need to check for bad index
  284. * inodes elsewhere.
  285. */
  286. if (unlikely(err)) {
  287. iput(vi);
  288. vi = ERR_PTR(err);
  289. }
  290. return vi;
  291. }
  292. struct inode *ntfs_alloc_big_inode(struct super_block *sb)
  293. {
  294. ntfs_inode *ni;
  295. ntfs_debug("Entering.");
  296. ni = kmem_cache_alloc(ntfs_big_inode_cache, GFP_NOFS);
  297. if (likely(ni != NULL)) {
  298. ni->state = 0;
  299. return VFS_I(ni);
  300. }
  301. ntfs_error(sb, "Allocation of NTFS big inode structure failed.");
  302. return NULL;
  303. }
  304. static void ntfs_i_callback(struct rcu_head *head)
  305. {
  306. struct inode *inode = container_of(head, struct inode, i_rcu);
  307. kmem_cache_free(ntfs_big_inode_cache, NTFS_I(inode));
  308. }
  309. void ntfs_destroy_big_inode(struct inode *inode)
  310. {
  311. ntfs_inode *ni = NTFS_I(inode);
  312. ntfs_debug("Entering.");
  313. BUG_ON(ni->page);
  314. if (!atomic_dec_and_test(&ni->count))
  315. BUG();
  316. call_rcu(&inode->i_rcu, ntfs_i_callback);
  317. }
  318. static inline ntfs_inode *ntfs_alloc_extent_inode(void)
  319. {
  320. ntfs_inode *ni;
  321. ntfs_debug("Entering.");
  322. ni = kmem_cache_alloc(ntfs_inode_cache, GFP_NOFS);
  323. if (likely(ni != NULL)) {
  324. ni->state = 0;
  325. return ni;
  326. }
  327. ntfs_error(NULL, "Allocation of NTFS inode structure failed.");
  328. return NULL;
  329. }
  330. static void ntfs_destroy_extent_inode(ntfs_inode *ni)
  331. {
  332. ntfs_debug("Entering.");
  333. BUG_ON(ni->page);
  334. if (!atomic_dec_and_test(&ni->count))
  335. BUG();
  336. kmem_cache_free(ntfs_inode_cache, ni);
  337. }
  338. /*
  339. * The attribute runlist lock has separate locking rules from the
  340. * normal runlist lock, so split the two lock-classes:
  341. */
  342. static struct lock_class_key attr_list_rl_lock_class;
  343. /**
  344. * __ntfs_init_inode - initialize ntfs specific part of an inode
  345. * @sb: super block of mounted volume
  346. * @ni: freshly allocated ntfs inode which to initialize
  347. *
  348. * Initialize an ntfs inode to defaults.
  349. *
  350. * NOTE: ni->mft_no, ni->state, ni->type, ni->name, and ni->name_len are left
  351. * untouched. Make sure to initialize them elsewhere.
  352. *
  353. * Return zero on success and -ENOMEM on error.
  354. */
  355. void __ntfs_init_inode(struct super_block *sb, ntfs_inode *ni)
  356. {
  357. ntfs_debug("Entering.");
  358. rwlock_init(&ni->size_lock);
  359. ni->initialized_size = ni->allocated_size = 0;
  360. ni->seq_no = 0;
  361. atomic_set(&ni->count, 1);
  362. ni->vol = NTFS_SB(sb);
  363. ntfs_init_runlist(&ni->runlist);
  364. mutex_init(&ni->mrec_lock);
  365. ni->page = NULL;
  366. ni->page_ofs = 0;
  367. ni->attr_list_size = 0;
  368. ni->attr_list = NULL;
  369. ntfs_init_runlist(&ni->attr_list_rl);
  370. lockdep_set_class(&ni->attr_list_rl.lock,
  371. &attr_list_rl_lock_class);
  372. ni->itype.index.block_size = 0;
  373. ni->itype.index.vcn_size = 0;
  374. ni->itype.index.collation_rule = 0;
  375. ni->itype.index.block_size_bits = 0;
  376. ni->itype.index.vcn_size_bits = 0;
  377. mutex_init(&ni->extent_lock);
  378. ni->nr_extents = 0;
  379. ni->ext.base_ntfs_ino = NULL;
  380. }
  381. /*
  382. * Extent inodes get MFT-mapped in a nested way, while the base inode
  383. * is still mapped. Teach this nesting to the lock validator by creating
  384. * a separate class for nested inode's mrec_lock's:
  385. */
  386. static struct lock_class_key extent_inode_mrec_lock_key;
  387. inline ntfs_inode *ntfs_new_extent_inode(struct super_block *sb,
  388. unsigned long mft_no)
  389. {
  390. ntfs_inode *ni = ntfs_alloc_extent_inode();
  391. ntfs_debug("Entering.");
  392. if (likely(ni != NULL)) {
  393. __ntfs_init_inode(sb, ni);
  394. lockdep_set_class(&ni->mrec_lock, &extent_inode_mrec_lock_key);
  395. ni->mft_no = mft_no;
  396. ni->type = AT_UNUSED;
  397. ni->name = NULL;
  398. ni->name_len = 0;
  399. }
  400. return ni;
  401. }
  402. /**
  403. * ntfs_is_extended_system_file - check if a file is in the $Extend directory
  404. * @ctx: initialized attribute search context
  405. *
  406. * Search all file name attributes in the inode described by the attribute
  407. * search context @ctx and check if any of the names are in the $Extend system
  408. * directory.
  409. *
  410. * Return values:
  411. * 1: file is in $Extend directory
  412. * 0: file is not in $Extend directory
  413. * -errno: failed to determine if the file is in the $Extend directory
  414. */
  415. static int ntfs_is_extended_system_file(ntfs_attr_search_ctx *ctx)
  416. {
  417. int nr_links, err;
  418. /* Restart search. */
  419. ntfs_attr_reinit_search_ctx(ctx);
  420. /* Get number of hard links. */
  421. nr_links = le16_to_cpu(ctx->mrec->link_count);
  422. /* Loop through all hard links. */
  423. while (!(err = ntfs_attr_lookup(AT_FILE_NAME, NULL, 0, 0, 0, NULL, 0,
  424. ctx))) {
  425. FILE_NAME_ATTR *file_name_attr;
  426. ATTR_RECORD *attr = ctx->attr;
  427. u8 *p, *p2;
  428. nr_links--;
  429. /*
  430. * Maximum sanity checking as we are called on an inode that
  431. * we suspect might be corrupt.
  432. */
  433. p = (u8*)attr + le32_to_cpu(attr->length);
  434. if (p < (u8*)ctx->mrec || (u8*)p > (u8*)ctx->mrec +
  435. le32_to_cpu(ctx->mrec->bytes_in_use)) {
  436. err_corrupt_attr:
  437. ntfs_error(ctx->ntfs_ino->vol->sb, "Corrupt file name "
  438. "attribute. You should run chkdsk.");
  439. return -EIO;
  440. }
  441. if (attr->non_resident) {
  442. ntfs_error(ctx->ntfs_ino->vol->sb, "Non-resident file "
  443. "name. You should run chkdsk.");
  444. return -EIO;
  445. }
  446. if (attr->flags) {
  447. ntfs_error(ctx->ntfs_ino->vol->sb, "File name with "
  448. "invalid flags. You should run "
  449. "chkdsk.");
  450. return -EIO;
  451. }
  452. if (!(attr->data.resident.flags & RESIDENT_ATTR_IS_INDEXED)) {
  453. ntfs_error(ctx->ntfs_ino->vol->sb, "Unindexed file "
  454. "name. You should run chkdsk.");
  455. return -EIO;
  456. }
  457. file_name_attr = (FILE_NAME_ATTR*)((u8*)attr +
  458. le16_to_cpu(attr->data.resident.value_offset));
  459. p2 = (u8*)attr + le32_to_cpu(attr->data.resident.value_length);
  460. if (p2 < (u8*)attr || p2 > p)
  461. goto err_corrupt_attr;
  462. /* This attribute is ok, but is it in the $Extend directory? */
  463. if (MREF_LE(file_name_attr->parent_directory) == FILE_Extend)
  464. return 1; /* YES, it's an extended system file. */
  465. }
  466. if (unlikely(err != -ENOENT))
  467. return err;
  468. if (unlikely(nr_links)) {
  469. ntfs_error(ctx->ntfs_ino->vol->sb, "Inode hard link count "
  470. "doesn't match number of name attributes. You "
  471. "should run chkdsk.");
  472. return -EIO;
  473. }
  474. return 0; /* NO, it is not an extended system file. */
  475. }
  476. /**
  477. * ntfs_read_locked_inode - read an inode from its device
  478. * @vi: inode to read
  479. *
  480. * ntfs_read_locked_inode() is called from ntfs_iget() to read the inode
  481. * described by @vi into memory from the device.
  482. *
  483. * The only fields in @vi that we need to/can look at when the function is
  484. * called are i_sb, pointing to the mounted device's super block, and i_ino,
  485. * the number of the inode to load.
  486. *
  487. * ntfs_read_locked_inode() maps, pins and locks the mft record number i_ino
  488. * for reading and sets up the necessary @vi fields as well as initializing
  489. * the ntfs inode.
  490. *
  491. * Q: What locks are held when the function is called?
  492. * A: i_state has I_NEW set, hence the inode is locked, also
  493. * i_count is set to 1, so it is not going to go away
  494. * i_flags is set to 0 and we have no business touching it. Only an ioctl()
  495. * is allowed to write to them. We should of course be honouring them but
  496. * we need to do that using the IS_* macros defined in include/linux/fs.h.
  497. * In any case ntfs_read_locked_inode() has nothing to do with i_flags.
  498. *
  499. * Return 0 on success and -errno on error. In the error case, the inode will
  500. * have had make_bad_inode() executed on it.
  501. */
  502. static int ntfs_read_locked_inode(struct inode *vi)
  503. {
  504. ntfs_volume *vol = NTFS_SB(vi->i_sb);
  505. ntfs_inode *ni;
  506. struct inode *bvi;
  507. MFT_RECORD *m;
  508. ATTR_RECORD *a;
  509. STANDARD_INFORMATION *si;
  510. ntfs_attr_search_ctx *ctx;
  511. int err = 0;
  512. ntfs_debug("Entering for i_ino 0x%lx.", vi->i_ino);
  513. /* Setup the generic vfs inode parts now. */
  514. /*
  515. * This is for checking whether an inode has changed w.r.t. a file so
  516. * that the file can be updated if necessary (compare with f_version).
  517. */
  518. vi->i_version = 1;
  519. vi->i_uid = vol->uid;
  520. vi->i_gid = vol->gid;
  521. vi->i_mode = 0;
  522. /*
  523. * Initialize the ntfs specific part of @vi special casing
  524. * FILE_MFT which we need to do at mount time.
  525. */
  526. if (vi->i_ino != FILE_MFT)
  527. ntfs_init_big_inode(vi);
  528. ni = NTFS_I(vi);
  529. m = map_mft_record(ni);
  530. if (IS_ERR(m)) {
  531. err = PTR_ERR(m);
  532. goto err_out;
  533. }
  534. ctx = ntfs_attr_get_search_ctx(ni, m);
  535. if (!ctx) {
  536. err = -ENOMEM;
  537. goto unm_err_out;
  538. }
  539. if (!(m->flags & MFT_RECORD_IN_USE)) {
  540. ntfs_error(vi->i_sb, "Inode is not in use!");
  541. goto unm_err_out;
  542. }
  543. if (m->base_mft_record) {
  544. ntfs_error(vi->i_sb, "Inode is an extent inode!");
  545. goto unm_err_out;
  546. }
  547. /* Transfer information from mft record into vfs and ntfs inodes. */
  548. vi->i_generation = ni->seq_no = le16_to_cpu(m->sequence_number);
  549. /*
  550. * FIXME: Keep in mind that link_count is two for files which have both
  551. * a long file name and a short file name as separate entries, so if
  552. * we are hiding short file names this will be too high. Either we need
  553. * to account for the short file names by subtracting them or we need
  554. * to make sure we delete files even though i_nlink is not zero which
  555. * might be tricky due to vfs interactions. Need to think about this
  556. * some more when implementing the unlink command.
  557. */
  558. set_nlink(vi, le16_to_cpu(m->link_count));
  559. /*
  560. * FIXME: Reparse points can have the directory bit set even though
  561. * they would be S_IFLNK. Need to deal with this further below when we
  562. * implement reparse points / symbolic links but it will do for now.
  563. * Also if not a directory, it could be something else, rather than
  564. * a regular file. But again, will do for now.
  565. */
  566. /* Everyone gets all permissions. */
  567. vi->i_mode |= S_IRWXUGO;
  568. /* If read-only, no one gets write permissions. */
  569. if (IS_RDONLY(vi))
  570. vi->i_mode &= ~S_IWUGO;
  571. if (m->flags & MFT_RECORD_IS_DIRECTORY) {
  572. vi->i_mode |= S_IFDIR;
  573. /*
  574. * Apply the directory permissions mask set in the mount
  575. * options.
  576. */
  577. vi->i_mode &= ~vol->dmask;
  578. /* Things break without this kludge! */
  579. if (vi->i_nlink > 1)
  580. set_nlink(vi, 1);
  581. } else {
  582. vi->i_mode |= S_IFREG;
  583. /* Apply the file permissions mask set in the mount options. */
  584. vi->i_mode &= ~vol->fmask;
  585. }
  586. /*
  587. * Find the standard information attribute in the mft record. At this
  588. * stage we haven't setup the attribute list stuff yet, so this could
  589. * in fact fail if the standard information is in an extent record, but
  590. * I don't think this actually ever happens.
  591. */
  592. err = ntfs_attr_lookup(AT_STANDARD_INFORMATION, NULL, 0, 0, 0, NULL, 0,
  593. ctx);
  594. if (unlikely(err)) {
  595. if (err == -ENOENT) {
  596. /*
  597. * TODO: We should be performing a hot fix here (if the
  598. * recover mount option is set) by creating a new
  599. * attribute.
  600. */
  601. ntfs_error(vi->i_sb, "$STANDARD_INFORMATION attribute "
  602. "is missing.");
  603. }
  604. goto unm_err_out;
  605. }
  606. a = ctx->attr;
  607. /* Get the standard information attribute value. */
  608. si = (STANDARD_INFORMATION*)((u8*)a +
  609. le16_to_cpu(a->data.resident.value_offset));
  610. /* Transfer information from the standard information into vi. */
  611. /*
  612. * Note: The i_?times do not quite map perfectly onto the NTFS times,
  613. * but they are close enough, and in the end it doesn't really matter
  614. * that much...
  615. */
  616. /*
  617. * mtime is the last change of the data within the file. Not changed
  618. * when only metadata is changed, e.g. a rename doesn't affect mtime.
  619. */
  620. vi->i_mtime = ntfs2utc(si->last_data_change_time);
  621. /*
  622. * ctime is the last change of the metadata of the file. This obviously
  623. * always changes, when mtime is changed. ctime can be changed on its
  624. * own, mtime is then not changed, e.g. when a file is renamed.
  625. */
  626. vi->i_ctime = ntfs2utc(si->last_mft_change_time);
  627. /*
  628. * Last access to the data within the file. Not changed during a rename
  629. * for example but changed whenever the file is written to.
  630. */
  631. vi->i_atime = ntfs2utc(si->last_access_time);
  632. /* Find the attribute list attribute if present. */
  633. ntfs_attr_reinit_search_ctx(ctx);
  634. err = ntfs_attr_lookup(AT_ATTRIBUTE_LIST, NULL, 0, 0, 0, NULL, 0, ctx);
  635. if (err) {
  636. if (unlikely(err != -ENOENT)) {
  637. ntfs_error(vi->i_sb, "Failed to lookup attribute list "
  638. "attribute.");
  639. goto unm_err_out;
  640. }
  641. } else /* if (!err) */ {
  642. if (vi->i_ino == FILE_MFT)
  643. goto skip_attr_list_load;
  644. ntfs_debug("Attribute list found in inode 0x%lx.", vi->i_ino);
  645. NInoSetAttrList(ni);
  646. a = ctx->attr;
  647. if (a->flags & ATTR_COMPRESSION_MASK) {
  648. ntfs_error(vi->i_sb, "Attribute list attribute is "
  649. "compressed.");
  650. goto unm_err_out;
  651. }
  652. if (a->flags & ATTR_IS_ENCRYPTED ||
  653. a->flags & ATTR_IS_SPARSE) {
  654. if (a->non_resident) {
  655. ntfs_error(vi->i_sb, "Non-resident attribute "
  656. "list attribute is encrypted/"
  657. "sparse.");
  658. goto unm_err_out;
  659. }
  660. ntfs_warning(vi->i_sb, "Resident attribute list "
  661. "attribute in inode 0x%lx is marked "
  662. "encrypted/sparse which is not true. "
  663. "However, Windows allows this and "
  664. "chkdsk does not detect or correct it "
  665. "so we will just ignore the invalid "
  666. "flags and pretend they are not set.",
  667. vi->i_ino);
  668. }
  669. /* Now allocate memory for the attribute list. */
  670. ni->attr_list_size = (u32)ntfs_attr_size(a);
  671. ni->attr_list = ntfs_malloc_nofs(ni->attr_list_size);
  672. if (!ni->attr_list) {
  673. ntfs_error(vi->i_sb, "Not enough memory to allocate "
  674. "buffer for attribute list.");
  675. err = -ENOMEM;
  676. goto unm_err_out;
  677. }
  678. if (a->non_resident) {
  679. NInoSetAttrListNonResident(ni);
  680. if (a->data.non_resident.lowest_vcn) {
  681. ntfs_error(vi->i_sb, "Attribute list has non "
  682. "zero lowest_vcn.");
  683. goto unm_err_out;
  684. }
  685. /*
  686. * Setup the runlist. No need for locking as we have
  687. * exclusive access to the inode at this time.
  688. */
  689. ni->attr_list_rl.rl = ntfs_mapping_pairs_decompress(vol,
  690. a, NULL);
  691. if (IS_ERR(ni->attr_list_rl.rl)) {
  692. err = PTR_ERR(ni->attr_list_rl.rl);
  693. ni->attr_list_rl.rl = NULL;
  694. ntfs_error(vi->i_sb, "Mapping pairs "
  695. "decompression failed.");
  696. goto unm_err_out;
  697. }
  698. /* Now load the attribute list. */
  699. if ((err = load_attribute_list(vol, &ni->attr_list_rl,
  700. ni->attr_list, ni->attr_list_size,
  701. sle64_to_cpu(a->data.non_resident.
  702. initialized_size)))) {
  703. ntfs_error(vi->i_sb, "Failed to load "
  704. "attribute list attribute.");
  705. goto unm_err_out;
  706. }
  707. } else /* if (!a->non_resident) */ {
  708. if ((u8*)a + le16_to_cpu(a->data.resident.value_offset)
  709. + le32_to_cpu(
  710. a->data.resident.value_length) >
  711. (u8*)ctx->mrec + vol->mft_record_size) {
  712. ntfs_error(vi->i_sb, "Corrupt attribute list "
  713. "in inode.");
  714. goto unm_err_out;
  715. }