namei.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /*
  2. * namei.c - NTFS kernel directory inode operations. Part of the Linux-NTFS
  3. * project.
  4. *
  5. * Copyright (c) 2001-2004 Anton Altaparmakov
  6. *
  7. * This program/include file is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as published
  9. * by the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program/include file is distributed in the hope that it will be
  13. * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  14. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program (in the main directory of the Linux-NTFS
  19. * distribution in the file COPYING); if not, write to the Free Software
  20. * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/dcache.h>
  23. #include <linux/security.h>
  24. #include "attrib.h"
  25. #include "debug.h"
  26. #include "dir.h"
  27. #include "mft.h"
  28. #include "ntfs.h"
  29. /**
  30. * ntfs_lookup - find the inode represented by a dentry in a directory inode
  31. * @dir_ino: directory inode in which to look for the inode
  32. * @dent: dentry representing the inode to look for
  33. * @nd: lookup nameidata
  34. *
  35. * In short, ntfs_lookup() looks for the inode represented by the dentry @dent
  36. * in the directory inode @dir_ino and if found attaches the inode to the
  37. * dentry @dent.
  38. *
  39. * In more detail, the dentry @dent specifies which inode to look for by
  40. * supplying the name of the inode in @dent->d_name.name. ntfs_lookup()
  41. * converts the name to Unicode and walks the contents of the directory inode
  42. * @dir_ino looking for the converted Unicode name. If the name is found in the
  43. * directory, the corresponding inode is loaded by calling ntfs_iget() on its
  44. * inode number and the inode is associated with the dentry @dent via a call to
  45. * d_splice_alias().
  46. *
  47. * If the name is not found in the directory, a NULL inode is inserted into the
  48. * dentry @dent via a call to d_add(). The dentry is then termed a negative
  49. * dentry.
  50. *
  51. * Only if an actual error occurs, do we return an error via ERR_PTR().
  52. *
  53. * In order to handle the case insensitivity issues of NTFS with regards to the
  54. * dcache and the dcache requiring only one dentry per directory, we deal with
  55. * dentry aliases that only differ in case in ->ntfs_lookup() while maintaining
  56. * a case sensitive dcache. This means that we get the full benefit of dcache
  57. * speed when the file/directory is looked up with the same case as returned by
  58. * ->ntfs_readdir() but that a lookup for any other case (or for the short file
  59. * name) will not find anything in dcache and will enter ->ntfs_lookup()
  60. * instead, where we search the directory for a fully matching file name
  61. * (including case) and if that is not found, we search for a file name that
  62. * matches with different case and if that has non-POSIX semantics we return
  63. * that. We actually do only one search (case sensitive) and keep tabs on
  64. * whether we have found a case insensitive match in the process.
  65. *
  66. * To simplify matters for us, we do not treat the short vs long filenames as
  67. * two hard links but instead if the lookup matches a short filename, we
  68. * return the dentry for the corresponding long filename instead.
  69. *
  70. * There are three cases we need to distinguish here:
  71. *
  72. * 1) @dent perfectly matches (i.e. including case) a directory entry with a
  73. * file name in the WIN32 or POSIX namespaces. In this case
  74. * ntfs_lookup_inode_by_name() will return with name set to NULL and we
  75. * just d_splice_alias() @dent.
  76. * 2) @dent matches (not including case) a directory entry with a file name in
  77. * the WIN32 namespace. In this case ntfs_lookup_inode_by_name() will return
  78. * with name set to point to a kmalloc()ed ntfs_name structure containing
  79. * the properly cased little endian Unicode name. We convert the name to the
  80. * current NLS code page, search if a dentry with this name already exists
  81. * and if so return that instead of @dent. At this point things are
  82. * complicated by the possibility of 'disconnected' dentries due to NFS
  83. * which we deal with appropriately (see the code comments). The VFS will
  84. * then destroy the old @dent and use the one we returned. If a dentry is
  85. * not found, we allocate a new one, d_splice_alias() it, and return it as
  86. * above.
  87. * 3) @dent matches either perfectly or not (i.e. we don't care about case) a
  88. * directory entry with a file name in the DOS namespace. In this case
  89. * ntfs_lookup_inode_by_name() will return with name set to point to a
  90. * kmalloc()ed ntfs_name structure containing the mft reference (cpu endian)
  91. * of the inode. We use the mft reference to read the inode and to find the
  92. * file name in the WIN32 namespace corresponding to the matched short file
  93. * name. We then convert the name to the current NLS code page, and proceed
  94. * searching for a dentry with this name, etc, as in case 2), above.
  95. *
  96. * Locking: Caller must hold i_mutex on the directory.
  97. */
  98. static struct dentry *ntfs_lookup(struct inode *dir_ino, struct dentry *dent,
  99. struct nameidata *nd)
  100. {
  101. ntfs_volume *vol = NTFS_SB(dir_ino->i_sb);
  102. struct inode *dent_inode;
  103. ntfschar *uname;
  104. ntfs_name *name = NULL;
  105. MFT_REF mref;
  106. unsigned long dent_ino;
  107. int uname_len;
  108. ntfs_debug("Looking up %s in directory inode 0x%lx.",
  109. dent->d_name.name, dir_ino->i_ino);
  110. /* Convert the name of the dentry to Unicode. */
  111. uname_len = ntfs_nlstoucs(vol, dent->d_name.name, dent->d_name.len,
  112. &uname);
  113. if (uname_len < 0) {
  114. ntfs_error(vol->sb, "Failed to convert name to Unicode.");
  115. return ERR_PTR(uname_len);
  116. }
  117. mref = ntfs_lookup_inode_by_name(NTFS_I(dir_ino), uname, uname_len,
  118. &name);
  119. kmem_cache_free(ntfs_name_cache, uname);
  120. if (!IS_ERR_MREF(mref)) {
  121. dent_ino = MREF(mref);
  122. ntfs_debug("Found inode 0x%lx. Calling ntfs_iget.", dent_ino);
  123. dent_inode = ntfs_iget(vol->sb, dent_ino);
  124. if (likely(!IS_ERR(dent_inode))) {
  125. /* Consistency check. */
  126. if (is_bad_inode(dent_inode) || MSEQNO(mref) ==
  127. NTFS_I(dent_inode)->seq_no ||
  128. dent_ino == FILE_MFT) {
  129. /* Perfect WIN32/POSIX match. -- Case 1. */
  130. if (!name) {
  131. ntfs_debug("Done. (Case 1.)");
  132. return d_splice_alias(dent_inode, dent);
  133. }
  134. /*
  135. * We are too indented. Handle imperfect
  136. * matches and short file names further below.
  137. */
  138. goto handle_name;
  139. }
  140. ntfs_error(vol->sb, "Found stale reference to inode "
  141. "0x%lx (reference sequence number = "
  142. "0x%x, inode sequence number = 0x%x), "
  143. "returning -EIO. Run chkdsk.",
  144. dent_ino, MSEQNO(mref),
  145. NTFS_I(dent_inode)->seq_no);
  146. iput(dent_inode);
  147. dent_inode = ERR_PTR(-EIO);
  148. } else
  149. ntfs_error(vol->sb, "ntfs_iget(0x%lx) failed with "
  150. "error code %li.", dent_ino,
  151. PTR_ERR(dent_inode));
  152. kfree(name);
  153. /* Return the error code. */
  154. return (struct dentry *)dent_inode;
  155. }
  156. /* It is guaranteed that name is no longer allocated at this point. */
  157. if (MREF_ERR(mref) == -ENOENT) {
  158. ntfs_debug("Entry was not found, adding negative dentry.");
  159. /* The dcache will handle negative entries. */
  160. d_add(dent, NULL);
  161. ntfs_debug("Done.");
  162. return NULL;
  163. }
  164. ntfs_error(vol->sb, "ntfs_lookup_ino_by_name() failed with error "
  165. "code %i.", -MREF_ERR(mref));
  166. return ERR_PTR(MREF_ERR(mref));
  167. // TODO: Consider moving this lot to a separate function! (AIA)
  168. handle_name:
  169. {
  170. struct dentry *real_dent, *new_dent;
  171. MFT_RECORD *m;
  172. ntfs_attr_search_ctx *ctx;
  173. ntfs_inode *ni = NTFS_I(dent_inode);
  174. int err;
  175. struct qstr nls_name;
  176. nls_name.name = NULL;
  177. if (name->type != FILE_NAME_DOS) { /* Case 2. */
  178. ntfs_debug("Case 2.");
  179. nls_name.len = (unsigned)ntfs_ucstonls(vol,
  180. (ntfschar*)&name->name, name->len,
  181. (unsigned char**)&nls_name.name, 0);
  182. kfree(name);
  183. } else /* if (name->type == FILE_NAME_DOS) */ { /* Case 3. */
  184. FILE_NAME_ATTR *fn;
  185. ntfs_debug("Case 3.");
  186. kfree(name);
  187. /* Find the WIN32 name corresponding to the matched DOS name. */
  188. ni = NTFS_I(dent_inode);
  189. m = map_mft_record(ni);
  190. if (IS_ERR(m)) {
  191. err = PTR_ERR(m);
  192. m = NULL;
  193. ctx = NULL;
  194. goto err_out;
  195. }
  196. ctx = ntfs_attr_get_search_ctx(ni, m);
  197. if (unlikely(!ctx)) {
  198. err = -ENOMEM;
  199. goto err_out;
  200. }
  201. do {
  202. ATTR_RECORD *a;
  203. u32 val_len;
  204. err = ntfs_attr_lookup(AT_FILE_NAME, NULL, 0, 0, 0,
  205. NULL, 0, ctx);
  206. if (unlikely(err)) {
  207. ntfs_error(vol->sb, "Inode corrupt: No WIN32 "
  208. "namespace counterpart to DOS "
  209. "file name. Run chkdsk.");
  210. if (err == -ENOENT)
  211. err = -EIO;
  212. goto err_out;
  213. }
  214. /* Consistency checks. */
  215. a = ctx->attr;
  216. if (a->non_resident || a->flags)
  217. goto eio_err_out;
  218. val_len = le32_to_cpu(a->data.resident.value_length);
  219. if (le16_to_cpu(a->data.resident.value_offset) +
  220. val_len > le32_to_cpu(a->length))
  221. goto eio_err_out;
  222. fn = (FILE_NAME_ATTR*)((u8*)ctx->attr + le16_to_cpu(
  223. ctx->attr->data.resident.value_offset));
  224. if ((u32)(fn->file_name_length * sizeof(ntfschar) +
  225. sizeof(FILE_NAME_ATTR)) > val_len)
  226. goto eio_err_out;
  227. } while (fn->file_name_type != FILE_NAME_WIN32);
  228. /* Convert the found WIN32 name to current NLS code page. */
  229. nls_name.len = (unsigned)ntfs_ucstonls(vol,
  230. (ntfschar*)&fn->file_name, fn->file_name_length,
  231. (unsigned char**)&nls_name.name, 0);
  232. ntfs_attr_put_search_ctx(ctx);
  233. unmap_mft_record(ni);
  234. }
  235. m = NULL;
  236. ctx = NULL;
  237. /* Check if a conversion error occurred. */
  238. if ((signed)nls_name.len < 0) {
  239. err = (signed)nls_name.len;
  240. goto err_out;
  241. }
  242. nls_name.hash = full_name_hash(nls_name.name, nls_name.len);
  243. /*
  244. * Note: No need for dent->d_lock lock as i_mutex is held on the
  245. * parent inode.
  246. */
  247. /* Does a dentry matching the nls_name exist already? */
  248. real_dent = d_lookup(dent->d_parent, &nls_name);
  249. /* If not, create it now. */
  250. if (!real_dent) {
  251. real_dent = d_alloc(dent->d_parent, &nls_name);
  252. kfree(nls_name.name);
  253. if (!real_dent) {
  254. err = -ENOMEM;
  255. goto err_out;
  256. }
  257. new_dent = d_splice_alias(dent_inode, real_dent);
  258. if (new_dent)
  259. dput(real_dent);
  260. else
  261. new_dent = real_dent;
  262. ntfs_debug("Done. (Created new dentry.)");
  263. return new_dent;
  264. }
  265. kfree(nls_name.name);
  266. /* Matching dentry exists, check if it is negative. */
  267. if (real_dent->d_inode) {
  268. if (unlikely(real_dent->d_inode != dent_inode)) {
  269. /* This can happen because bad inodes are unhashed. */
  270. BUG_ON(!is_bad_inode(dent_inode));
  271. BUG_ON(!is_bad_inode(real_dent->d_inode));
  272. }
  273. /*
  274. * Already have the inode and the dentry attached, decrement
  275. * the reference count to balance the ntfs_iget() we did
  276. * earlier on. We found the dentry using d_lookup() so it
  277. * cannot be disconnected and thus we do not need to worry
  278. * about any NFS/disconnectedness issues here.
  279. */
  280. iput(dent_inode);
  281. ntfs_debug("Done. (Already had inode and dentry.)");
  282. return real_dent;
  283. }
  284. /*
  285. * Negative dentry: instantiate it unless the inode is a directory and
  286. * has a 'disconnected' dentry (i.e. IS_ROOT and DCACHE_DISCONNECTED),
  287. * in which case d_move() that in place of the found dentry.
  288. */
  289. if (!S_ISDIR(dent_inode->i_mode)) {
  290. /* Not a directory; everything is easy. */
  291. d_instantiate(real_dent, dent_inode);
  292. ntfs_debug("Done. (Already had negative file dentry.)");
  293. return real_dent;
  294. }
  295. spin_lock(&dcache_lock);
  296. if (list_empty(&dent_inode->i_dentry)) {
  297. /*
  298. * Directory without a 'disconnected' dentry; we need to do
  299. * d_instantiate() by hand because it takes dcache_lock which
  300. * we already hold.
  301. */
  302. list_add(&real_dent->d_alias, &dent_inode->i_dentry);
  303. real_dent->d_inode = dent_inode;
  304. spin_unlock(&dcache_lock);
  305. security_d_instantiate(real_dent, dent_inode);
  306. ntfs_debug("Done. (Already had negative directory dentry.)");
  307. return real_dent;
  308. }
  309. /*
  310. * Directory with a 'disconnected' dentry; get a reference to the
  311. * 'disconnected' dentry.
  312. */
  313. new_dent = list_entry(dent_inode->i_dentry.next, struct dentry,
  314. d_alias);
  315. dget_locked(new_dent);
  316. spin_unlock(&dcache_lock);
  317. /* Do security vodoo. */
  318. security_d_instantiate(real_dent, dent_inode);
  319. /* Move new_dent in place of real_dent. */
  320. d_move(new_dent, real_dent);
  321. /* Balance the ntfs_iget() we did above. */
  322. iput(dent_inode);
  323. /* Throw away real_dent. */
  324. dput(real_dent);
  325. /* Use new_dent as the actual dentry. */
  326. ntfs_debug("Done. (Already had negative, disconnected directory "
  327. "dentry.)");
  328. return new_dent;
  329. eio_err_out:
  330. ntfs_error(vol->sb, "Illegal file name attribute. Run chkdsk.");
  331. err = -EIO;
  332. err_out:
  333. if (ctx)
  334. ntfs_attr_put_search_ctx(ctx);
  335. if (m)
  336. unmap_mft_record(ni);
  337. iput(dent_inode);
  338. ntfs_error(vol->sb, "Failed, returning error code %i.", err);
  339. return ERR_PTR(err);
  340. }
  341. }
  342. /**
  343. * Inode operations for directories.
  344. */
  345. struct inode_operations ntfs_dir_inode_ops = {
  346. .lookup = ntfs_lookup, /* VFS: Lookup directory. */
  347. };
  348. /**
  349. * ntfs_get_parent - find the dentry of the parent of a given directory dentry
  350. * @child_dent: dentry of the directory whose parent directory to find
  351. *
  352. * Find the dentry for the parent directory of the directory specified by the
  353. * dentry @child_dent. This function is called from
  354. * fs/exportfs/expfs.c::find_exported_dentry() which in turn is called from the
  355. * default ->decode_fh() which is export_decode_fh() in the same file.
  356. *
  357. * The code is based on the ext3 ->get_parent() implementation found in
  358. * fs/ext3/namei.c::ext3_get_parent().
  359. *
  360. * Note: ntfs_get_parent() is called with @child_dent->d_inode->i_mutex down.
  361. *
  362. * Return the dentry of the parent directory on success or the error code on
  363. * error (IS_ERR() is true).
  364. */
  365. static struct dentry *ntfs_get_parent(struct dentry *child_dent)
  366. {
  367. struct inode *vi = child_dent->d_inode;
  368. ntfs_inode *ni = NTFS_I(vi);
  369. MFT_RECORD *mrec;
  370. ntfs_attr_search_ctx *ctx;
  371. ATTR_RECORD *attr;
  372. FILE_NAME_ATTR *fn;
  373. struct inode *parent_vi;
  374. struct dentry *parent_dent;
  375. unsigned long parent_ino;
  376. int err;
  377. ntfs_debug("Entering for inode 0x%lx.", vi->i_ino);
  378. /* Get the mft record of the inode belonging to the child dentry. */
  379. mrec = map_mft_record(ni);
  380. if (IS_ERR(mrec))
  381. return (struct dentry *)mrec;
  382. /* Find the first file name attribute in the mft record. */
  383. ctx = ntfs_attr_get_search_ctx(ni, mrec);
  384. if (unlikely(!ctx)) {
  385. unmap_mft_record(ni);
  386. return ERR_PTR(-ENOMEM);
  387. }
  388. try_next:
  389. err = ntfs_attr_lookup(AT_FILE_NAME, NULL, 0, CASE_SENSITIVE, 0, NULL,
  390. 0, ctx);
  391. if (unlikely(err)) {
  392. ntfs_attr_put_search_ctx(ctx);
  393. unmap_mft_record(ni);
  394. if (err == -ENOENT)
  395. ntfs_error(vi->i_sb, "Inode 0x%lx does not have a "
  396. "file name attribute. Run chkdsk.",
  397. vi->i_ino);
  398. return ERR_PTR(err);
  399. }
  400. attr = ctx->attr;
  401. if (unlikely(attr->non_resident))
  402. goto try_next;
  403. fn = (FILE_NAME_ATTR *)((u8 *)attr +
  404. le16_to_cpu(attr->data.resident.value_offset));
  405. if (unlikely((u8 *)fn + le32_to_cpu(attr->data.resident.value_length) >
  406. (u8*)attr + le32_to_cpu(attr->length)))
  407. goto try_next;
  408. /* Get the inode number of the parent directory. */
  409. parent_ino = MREF_LE(fn->parent_directory);
  410. /* Release the search context and the mft record of the child. */
  411. ntfs_attr_put_search_ctx(ctx);
  412. unmap_mft_record(ni);
  413. /* Get the inode of the parent directory. */
  414. parent_vi = ntfs_iget(vi->i_sb, parent_ino);
  415. if (IS_ERR(parent_vi) || unlikely(is_bad_inode(parent_vi))) {
  416. if (!IS_ERR(parent_vi))
  417. iput(parent_vi);
  418. ntfs_error(vi->i_sb, "Failed to get parent directory inode "
  419. "0x%lx of child inode 0x%lx.", parent_ino,
  420. vi->i_ino);
  421. return ERR_PTR(-EACCES);
  422. }
  423. /* Finally get a dentry for the parent directory and return it. */
  424. parent_dent = d_alloc_anon(parent_vi);
  425. if (unlikely(!parent_dent)) {
  426. iput(parent_vi);
  427. return ERR_PTR(-ENOMEM);
  428. }
  429. ntfs_debug("Done for inode 0x%lx.", vi->i_ino);
  430. return parent_dent;
  431. }
  432. /**
  433. * ntfs_get_dentry - find a dentry for the inode from a file handle sub-fragment
  434. * @sb: super block identifying the mounted ntfs volume
  435. * @fh: the file handle sub-fragment
  436. *
  437. * Find a dentry for the inode given a file handle sub-fragment. This function
  438. * is called from fs/exportfs/expfs.c::find_exported_dentry() which in turn is
  439. * called from the default ->decode_fh() which is export_decode_fh() in the
  440. * same file. The code is closely based on the default ->get_dentry() helper
  441. * fs/exportfs/expfs.c::get_object().
  442. *
  443. * The @fh contains two 32-bit unsigned values, the first one is the inode
  444. * number and the second one is the inode generation.
  445. *
  446. * Return the dentry on success or the error code on error (IS_ERR() is true).
  447. */
  448. static struct dentry *ntfs_get_dentry(struct super_block *sb, void *fh)
  449. {
  450. struct inode *vi;
  451. struct dentry *dent;
  452. unsigned long ino = ((u32 *)fh)[0];
  453. u32 gen = ((u32 *)fh)[1];
  454. ntfs_debug("Entering for inode 0x%lx, generation 0x%x.", ino, gen);
  455. vi = ntfs_iget(sb, ino);
  456. if (IS_ERR(vi)) {
  457. ntfs_error(sb, "Failed to get inode 0x%lx.", ino);
  458. return (struct dentry *)vi;
  459. }
  460. if (unlikely(is_bad_inode(vi) || vi->i_generation != gen)) {
  461. /* We didn't find the right inode. */
  462. ntfs_error(sb, "Inode 0x%lx, bad count: %d %d or version 0x%x "
  463. "0x%x.", vi->i_ino, vi->i_nlink,
  464. atomic_read(&vi->i_count), vi->i_generation,
  465. gen);
  466. iput(vi);
  467. return ERR_PTR(-ESTALE);
  468. }
  469. /* Now find a dentry. If possible, get a well-connected one. */
  470. dent = d_alloc_anon(vi);
  471. if (unlikely(!dent)) {
  472. iput(vi);
  473. return ERR_PTR(-ENOMEM);
  474. }
  475. ntfs_debug("Done for inode 0x%lx, generation 0x%x.", ino, gen);
  476. return dent;
  477. }
  478. /**
  479. * Export operations allowing NFS exporting of mounted NTFS partitions.
  480. *
  481. * We use the default ->decode_fh() and ->encode_fh() for now. Note that they
  482. * use 32 bits to store the inode number which is an unsigned long so on 64-bit
  483. * architectures is usually 64 bits so it would all fail horribly on huge
  484. * volumes. I guess we need to define our own encode and decode fh functions
  485. * that store 64-bit inode numbers at some point but for now we will ignore the
  486. * problem...
  487. *
  488. * We also use the default ->get_name() helper (used by ->decode_fh() via
  489. * fs/exportfs/expfs.c::find_exported_dentry()) as that is completely fs
  490. * independent.
  491. *
  492. * The default ->get_parent() just returns -EACCES so we have to provide our
  493. * own and the default ->get_dentry() is incompatible with NTFS due to not
  494. * allowing the inode number 0 which is used in NTFS for the system file $MFT
  495. * and due to using iget() whereas NTFS needs ntfs_iget().
  496. */
  497. struct export_operations ntfs_export_ops = {
  498. .get_parent = ntfs_get_parent, /* Find the parent of a given
  499. directory. */
  500. .get_dentry = ntfs_get_dentry, /* Find a dentry for the inode
  501. given a file handle
  502. sub-fragment. */
  503. };