namei.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * namei.c - NTFS kernel directory inode operations. Part of the Linux-NTFS
  3. * project.
  4. *
  5. * Copyright (c) 2001-2006 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/exportfs.h>
  24. #include <linux/security.h>
  25. #include "attrib.h"
  26. #include "debug.h"
  27. #include "dir.h"
  28. #include "mft.h"
  29. #include "ntfs.h"
  30. /**
  31. * ntfs_lookup - find the inode represented by a dentry in a directory inode
  32. * @dir_ino: directory inode in which to look for the inode
  33. * @dent: dentry representing the inode to look for
  34. * @nd: lookup nameidata
  35. *
  36. * In short, ntfs_lookup() looks for the inode represented by the dentry @dent
  37. * in the directory inode @dir_ino and if found attaches the inode to the
  38. * dentry @dent.
  39. *
  40. * In more detail, the dentry @dent specifies which inode to look for by
  41. * supplying the name of the inode in @dent->d_name.name. ntfs_lookup()
  42. * converts the name to Unicode and walks the contents of the directory inode
  43. * @dir_ino looking for the converted Unicode name. If the name is found in the
  44. * directory, the corresponding inode is loaded by calling ntfs_iget() on its
  45. * inode number and the inode is associated with the dentry @dent via a call to
  46. * d_splice_alias().
  47. *
  48. * If the name is not found in the directory, a NULL inode is inserted into the
  49. * dentry @dent via a call to d_add(). The dentry is then termed a negative
  50. * dentry.
  51. *
  52. * Only if an actual error occurs, do we return an error via ERR_PTR().
  53. *
  54. * In order to handle the case insensitivity issues of NTFS with regards to the
  55. * dcache and the dcache requiring only one dentry per directory, we deal with
  56. * dentry aliases that only differ in case in ->ntfs_lookup() while maintaining
  57. * a case sensitive dcache. This means that we get the full benefit of dcache
  58. * speed when the file/directory is looked up with the same case as returned by
  59. * ->ntfs_readdir() but that a lookup for any other case (or for the short file
  60. * name) will not find anything in dcache and will enter ->ntfs_lookup()
  61. * instead, where we search the directory for a fully matching file name
  62. * (including case) and if that is not found, we search for a file name that
  63. * matches with different case and if that has non-POSIX semantics we return
  64. * that. We actually do only one search (case sensitive) and keep tabs on
  65. * whether we have found a case insensitive match in the process.
  66. *
  67. * To simplify matters for us, we do not treat the short vs long filenames as
  68. * two hard links but instead if the lookup matches a short filename, we
  69. * return the dentry for the corresponding long filename instead.
  70. *
  71. * There are three cases we need to distinguish here:
  72. *
  73. * 1) @dent perfectly matches (i.e. including case) a directory entry with a
  74. * file name in the WIN32 or POSIX namespaces. In this case
  75. * ntfs_lookup_inode_by_name() will return with name set to NULL and we
  76. * just d_splice_alias() @dent.
  77. * 2) @dent matches (not including case) a directory entry with a file name in
  78. * the WIN32 namespace. In this case ntfs_lookup_inode_by_name() will return
  79. * with name set to point to a kmalloc()ed ntfs_name structure containing
  80. * the properly cased little endian Unicode name. We convert the name to the
  81. * current NLS code page, search if a dentry with this name already exists
  82. * and if so return that instead of @dent. At this point things are
  83. * complicated by the possibility of 'disconnected' dentries due to NFS
  84. * which we deal with appropriately (see the code comments). The VFS will
  85. * then destroy the old @dent and use the one we returned. If a dentry is
  86. * not found, we allocate a new one, d_splice_alias() it, and return it as
  87. * above.
  88. * 3) @dent matches either perfectly or not (i.e. we don't care about case) a
  89. * directory entry with a file name in the DOS namespace. In this case
  90. * ntfs_lookup_inode_by_name() will return with name set to point to a
  91. * kmalloc()ed ntfs_name structure containing the mft reference (cpu endian)
  92. * of the inode. We use the mft reference to read the inode and to find the
  93. * file name in the WIN32 namespace corresponding to the matched short file
  94. * name. We then convert the name to the current NLS code page, and proceed
  95. * searching for a dentry with this name, etc, as in case 2), above.
  96. *
  97. * Locking: Caller must hold i_mutex on the directory.
  98. */
  99. static struct dentry *ntfs_lookup(struct inode *dir_ino, struct dentry *dent,
  100. struct nameidata *nd)
  101. {
  102. ntfs_volume *vol = NTFS_SB(dir_ino->i_sb);
  103. struct inode *dent_inode;
  104. ntfschar *uname;
  105. ntfs_name *name = NULL;
  106. MFT_REF mref;
  107. unsigned long dent_ino;
  108. int uname_len;
  109. ntfs_debug("Looking up %s in directory inode 0x%lx.",
  110. dent->d_name.name, dir_ino->i_ino);
  111. /* Convert the name of the dentry to Unicode. */
  112. uname_len = ntfs_nlstoucs(vol, dent->d_name.name, dent->d_name.len,
  113. &uname);
  114. if (uname_len < 0) {
  115. if (uname_len != -ENAMETOOLONG)
  116. ntfs_error(vol->sb, "Failed to convert name to "
  117. "Unicode.");
  118. return ERR_PTR(uname_len);
  119. }
  120. mref = ntfs_lookup_inode_by_name(NTFS_I(dir_ino), uname, uname_len,
  121. &name);
  122. kmem_cache_free(ntfs_name_cache, uname);
  123. if (!IS_ERR_MREF(mref)) {
  124. dent_ino = MREF(mref);
  125. ntfs_debug("Found inode 0x%lx. Calling ntfs_iget.", dent_ino);
  126. dent_inode = ntfs_iget(vol->sb, dent_ino);
  127. if (likely(!IS_ERR(dent_inode))) {
  128. /* Consistency check. */
  129. if (is_bad_inode(dent_inode) || MSEQNO(mref) ==
  130. NTFS_I(dent_inode)->seq_no ||
  131. dent_ino == FILE_MFT) {
  132. /* Perfect WIN32/POSIX match. -- Case 1. */
  133. if (!name) {
  134. ntfs_debug("Done. (Case 1.)");
  135. return d_splice_alias(dent_inode, dent);
  136. }
  137. /*
  138. * We are too indented. Handle imperfect
  139. * matches and short file names further below.
  140. */
  141. goto handle_name;
  142. }
  143. ntfs_error(vol->sb, "Found stale reference to inode "
  144. "0x%lx (reference sequence number = "
  145. "0x%x, inode sequence number = 0x%x), "
  146. "returning -EIO. Run chkdsk.",
  147. dent_ino, MSEQNO(mref),
  148. NTFS_I(dent_inode)->seq_no);
  149. iput(dent_inode);
  150. dent_inode = ERR_PTR(-EIO);
  151. } else
  152. ntfs_error(vol->sb, "ntfs_iget(0x%lx) failed with "
  153. "error code %li.", dent_ino,
  154. PTR_ERR(dent_inode));
  155. kfree(name);
  156. /* Return the error code. */
  157. return (struct dentry *)dent_inode;
  158. }
  159. /* It is guaranteed that @name is no longer allocated at this point. */
  160. if (MREF_ERR(mref) == -ENOENT) {
  161. ntfs_debug("Entry was not found, adding negative dentry.");
  162. /* The dcache will handle negative entries. */
  163. d_add(dent, NULL);
  164. ntfs_debug("Done.");
  165. return NULL;
  166. }
  167. ntfs_error(vol->sb, "ntfs_lookup_ino_by_name() failed with error "
  168. "code %i.", -MREF_ERR(mref));
  169. return ERR_PTR(MREF_ERR(mref));
  170. // TODO: Consider moving this lot to a separate function! (AIA)
  171. handle_name:
  172. {
  173. MFT_RECORD *m;
  174. ntfs_attr_search_ctx *ctx;
  175. ntfs_inode *ni = NTFS_I(dent_inode);
  176. int err;
  177. struct qstr nls_name;
  178. nls_name.name = NULL;
  179. if (name->type != FILE_NAME_DOS) { /* Case 2. */
  180. ntfs_debug("Case 2.");
  181. nls_name.len = (unsigned)ntfs_ucstonls(vol,
  182. (ntfschar*)&name->name, name->len,
  183. (unsigned char**)&nls_name.name, 0);
  184. kfree(name);
  185. } else /* if (name->type == FILE_NAME_DOS) */ { /* Case 3. */
  186. FILE_NAME_ATTR *fn;
  187. ntfs_debug("Case 3.");
  188. kfree(name);
  189. /* Find the WIN32 name corresponding to the matched DOS name. */
  190. ni = NTFS_I(dent_inode);
  191. m = map_mft_record(ni);
  192. if (IS_ERR(m)) {
  193. err = PTR_ERR(m);
  194. m = NULL;
  195. ctx = NULL;
  196. goto err_out;
  197. }
  198. ctx = ntfs_attr_get_search_ctx(ni, m);
  199. if (unlikely(!ctx)) {
  200. err = -ENOMEM;
  201. goto err_out;
  202. }
  203. do {
  204. ATTR_RECORD *a;
  205. u32 val_len;
  206. err = ntfs_attr_lookup(AT_FILE_NAME, NULL, 0, 0, 0,
  207. NULL, 0, ctx);
  208. if (unlikely(err)) {
  209. ntfs_error(vol->sb, "Inode corrupt: No WIN32 "
  210. "namespace counterpart to DOS "
  211. "file name. Run chkdsk.");
  212. if (err == -ENOENT)
  213. err = -EIO;
  214. goto err_out;
  215. }
  216. /* Consistency checks. */
  217. a = ctx->attr;
  218. if (a->non_resident || a->flags)
  219. goto eio_err_out;
  220. val_len = le32_to_cpu(a->data.resident.value_length);
  221. if (le16_to_cpu(a->data.resident.value_offset) +
  222. val_len > le32_to_cpu(a->length))
  223. goto eio_err_out;
  224. fn = (FILE_NAME_ATTR*)((u8*)ctx->attr + le16_to_cpu(
  225. ctx->attr->data.resident.value_offset));
  226. if ((u32)(fn->file_name_length * sizeof(ntfschar) +
  227. sizeof(FILE_NAME_ATTR)) > val_len)
  228. goto eio_err_out;
  229. } while (fn->file_name_type != FILE_NAME_WIN32);
  230. /* Convert the found WIN32 name to current NLS code page. */
  231. nls_name.len = (unsigned)ntfs_ucstonls(vol,
  232. (ntfschar*)&fn->file_name, fn->file_name_length,
  233. (unsigned char**)&nls_name.name, 0);
  234. ntfs_attr_put_search_ctx(ctx);
  235. unmap_mft_record(ni);
  236. }
  237. m = NULL;
  238. ctx = NULL;
  239. /* Check if a conversion error occurred. */
  240. if ((signed)nls_name.len < 0) {
  241. err = (signed)nls_name.len;
  242. goto err_out;
  243. }
  244. nls_name.hash = full_name_hash(nls_name.name, nls_name.len);
  245. dent = d_add_ci(dent, dent_inode, &nls_name);
  246. kfree(nls_name.name);
  247. return dent;
  248. eio_err_out:
  249. ntfs_error(vol->sb, "Illegal file name attribute. Run chkdsk.");
  250. err = -EIO;
  251. err_out:
  252. if (ctx)
  253. ntfs_attr_put_search_ctx(ctx);
  254. if (m)
  255. unmap_mft_record(ni);
  256. iput(dent_inode);
  257. ntfs_error(vol->sb, "Failed, returning error code %i.", err);
  258. return ERR_PTR(err);
  259. }
  260. }
  261. /**
  262. * Inode operations for directories.
  263. */
  264. const struct inode_operations ntfs_dir_inode_ops = {
  265. .lookup = ntfs_lookup, /* VFS: Lookup directory. */
  266. };
  267. /**
  268. * ntfs_get_parent - find the dentry of the parent of a given directory dentry
  269. * @child_dent: dentry of the directory whose parent directory to find
  270. *
  271. * Find the dentry for the parent directory of the directory specified by the
  272. * dentry @child_dent. This function is called from
  273. * fs/exportfs/expfs.c::find_exported_dentry() which in turn is called from the
  274. * default ->decode_fh() which is export_decode_fh() in the same file.
  275. *
  276. * The code is based on the ext3 ->get_parent() implementation found in
  277. * fs/ext3/namei.c::ext3_get_parent().
  278. *
  279. * Note: ntfs_get_parent() is called with @child_dent->d_inode->i_mutex down.
  280. *
  281. * Return the dentry of the parent directory on success or the error code on
  282. * error (IS_ERR() is true).
  283. */
  284. static struct dentry *ntfs_get_parent(struct dentry *child_dent)
  285. {
  286. struct inode *vi = child_dent->d_inode;
  287. ntfs_inode *ni = NTFS_I(vi);
  288. MFT_RECORD *mrec;
  289. ntfs_attr_search_ctx *ctx;
  290. ATTR_RECORD *attr;
  291. FILE_NAME_ATTR *fn;
  292. unsigned long parent_ino;
  293. int err;
  294. ntfs_debug("Entering for inode 0x%lx.", vi->i_ino);
  295. /* Get the mft record of the inode belonging to the child dentry. */
  296. mrec = map_mft_record(ni);
  297. if (IS_ERR(mrec))
  298. return (struct dentry *)mrec;
  299. /* Find the first file name attribute in the mft record. */
  300. ctx = ntfs_attr_get_search_ctx(ni, mrec);
  301. if (unlikely(!ctx)) {
  302. unmap_mft_record(ni);
  303. return ERR_PTR(-ENOMEM);
  304. }
  305. try_next:
  306. err = ntfs_attr_lookup(AT_FILE_NAME, NULL, 0, CASE_SENSITIVE, 0, NULL,
  307. 0, ctx);
  308. if (unlikely(err)) {
  309. ntfs_attr_put_search_ctx(ctx);
  310. unmap_mft_record(ni);
  311. if (err == -ENOENT)
  312. ntfs_error(vi->i_sb, "Inode 0x%lx does not have a "
  313. "file name attribute. Run chkdsk.",
  314. vi->i_ino);
  315. return ERR_PTR(err);
  316. }
  317. attr = ctx->attr;
  318. if (unlikely(attr->non_resident))
  319. goto try_next;
  320. fn = (FILE_NAME_ATTR *)((u8 *)attr +
  321. le16_to_cpu(attr->data.resident.value_offset));
  322. if (unlikely((u8 *)fn + le32_to_cpu(attr->data.resident.value_length) >
  323. (u8*)attr + le32_to_cpu(attr->length)))
  324. goto try_next;
  325. /* Get the inode number of the parent directory. */
  326. parent_ino = MREF_LE(fn->parent_directory);
  327. /* Release the search context and the mft record of the child. */
  328. ntfs_attr_put_search_ctx(ctx);
  329. unmap_mft_record(ni);
  330. return d_obtain_alias(ntfs_iget(vi->i_sb, parent_ino));
  331. }
  332. static struct inode *ntfs_nfs_get_inode(struct super_block *sb,
  333. u64 ino, u32 generation)
  334. {
  335. struct inode *inode;
  336. inode = ntfs_iget(sb, ino);
  337. if (!IS_ERR(inode)) {
  338. if (is_bad_inode(inode) || inode->i_generation != generation) {
  339. iput(inode);
  340. inode = ERR_PTR(-ESTALE);
  341. }
  342. }
  343. return inode;
  344. }
  345. static struct dentry *ntfs_fh_to_dentry(struct super_block *sb, struct fid *fid,
  346. int fh_len, int fh_type)
  347. {
  348. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  349. ntfs_nfs_get_inode);
  350. }
  351. static struct dentry *ntfs_fh_to_parent(struct super_block *sb, struct fid *fid,
  352. int fh_len, int fh_type)
  353. {
  354. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  355. ntfs_nfs_get_inode);
  356. }
  357. /**
  358. * Export operations allowing NFS exporting of mounted NTFS partitions.
  359. *
  360. * We use the default ->encode_fh() for now. Note that they
  361. * use 32 bits to store the inode number which is an unsigned long so on 64-bit
  362. * architectures is usually 64 bits so it would all fail horribly on huge
  363. * volumes. I guess we need to define our own encode and decode fh functions
  364. * that store 64-bit inode numbers at some point but for now we will ignore the
  365. * problem...
  366. *
  367. * We also use the default ->get_name() helper (used by ->decode_fh() via
  368. * fs/exportfs/expfs.c::find_exported_dentry()) as that is completely fs
  369. * independent.
  370. *
  371. * The default ->get_parent() just returns -EACCES so we have to provide our
  372. * own and the default ->get_dentry() is incompatible with NTFS due to not
  373. * allowing the inode number 0 which is used in NTFS for the system file $MFT
  374. * and due to using iget() whereas NTFS needs ntfs_iget().
  375. */
  376. const struct export_operations ntfs_export_ops = {
  377. .get_parent = ntfs_get_parent, /* Find the parent of a given
  378. directory. */
  379. .fh_to_dentry = ntfs_fh_to_dentry,
  380. .fh_to_parent = ntfs_fh_to_parent,
  381. };