namei.c 14 KB

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