namei.c 13 KB

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