2 * namei.c - NTFS kernel directory inode operations. Part of the Linux-NTFS
5 * Copyright (c) 2001-2004 Anton Altaparmakov
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.
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.
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
23 #include <linux/dcache.h>
24 #include <linux/security.h>
33 * ntfs_lookup - find the inode represented by a dentry in a directory inode
34 * @dir_ino: directory inode in which to look for the inode
35 * @dent: dentry representing the inode to look for
36 * @nd: lookup nameidata
38 * In short, ntfs_lookup() looks for the inode represented by the dentry @dent
39 * in the directory inode @dir_ino and if found attaches the inode to the
42 * In more detail, the dentry @dent specifies which inode to look for by
43 * supplying the name of the inode in @dent->d_name.name. ntfs_lookup()
44 * converts the name to Unicode and walks the contents of the directory inode
45 * @dir_ino looking for the converted Unicode name. If the name is found in the
46 * directory, the corresponding inode is loaded by calling ntfs_iget() on its
47 * inode number and the inode is associated with the dentry @dent via a call to
50 * If the name is not found in the directory, a NULL inode is inserted into the
51 * dentry @dent via a call to d_add(). The dentry is then termed a negative
54 * Only if an actual error occurs, do we return an error via ERR_PTR().
56 * In order to handle the case insensitivity issues of NTFS with regards to the
57 * dcache and the dcache requiring only one dentry per directory, we deal with
58 * dentry aliases that only differ in case in ->ntfs_lookup() while maintaining
59 * a case sensitive dcache. This means that we get the full benefit of dcache
60 * speed when the file/directory is looked up with the same case as returned by
61 * ->ntfs_readdir() but that a lookup for any other case (or for the short file
62 * name) will not find anything in dcache and will enter ->ntfs_lookup()
63 * instead, where we search the directory for a fully matching file name
64 * (including case) and if that is not found, we search for a file name that
65 * matches with different case and if that has non-POSIX semantics we return
66 * that. We actually do only one search (case sensitive) and keep tabs on
67 * whether we have found a case insensitive match in the process.
69 * To simplify matters for us, we do not treat the short vs long filenames as
70 * two hard links but instead if the lookup matches a short filename, we
71 * return the dentry for the corresponding long filename instead.
73 * There are three cases we need to distinguish here:
75 * 1) @dent perfectly matches (i.e. including case) a directory entry with a
76 * file name in the WIN32 or POSIX namespaces. In this case
77 * ntfs_lookup_inode_by_name() will return with name set to NULL and we
78 * just d_splice_alias() @dent.
79 * 2) @dent matches (not including case) a directory entry with a file name in
80 * the WIN32 namespace. In this case ntfs_lookup_inode_by_name() will return
81 * with name set to point to a kmalloc()ed ntfs_name structure containing
82 * the properly cased little endian Unicode name. We convert the name to the
83 * current NLS code page, search if a dentry with this name already exists
84 * and if so return that instead of @dent. At this point things are
85 * complicated by the possibility of 'disconnected' dentries due to NFS
86 * which we deal with appropriately (see the code comments). The VFS will
87 * then destroy the old @dent and use the one we returned. If a dentry is
88 * not found, we allocate a new one, d_splice_alias() it, and return it as
90 * 3) @dent matches either perfectly or not (i.e. we don't care about case) a
91 * directory entry with a file name in the DOS namespace. In this case
92 * ntfs_lookup_inode_by_name() will return with name set to point to a
93 * kmalloc()ed ntfs_name structure containing the mft reference (cpu endian)
94 * of the inode. We use the mft reference to read the inode and to find the
95 * file name in the WIN32 namespace corresponding to the matched short file
96 * name. We then convert the name to the current NLS code page, and proceed
97 * searching for a dentry with this name, etc, as in case 2), above.
99 * Locking: Caller must hold i_mutex on the directory.
101 static struct dentry *ntfs_lookup(struct inode *dir_ino, struct dentry *dent,
102 struct nameidata *nd)
104 ntfs_volume *vol = NTFS_SB(dir_ino->i_sb);
105 struct inode *dent_inode;
107 ntfs_name *name = NULL;
109 unsigned long dent_ino;
112 ntfs_debug("Looking up %s in directory inode 0x%lx.",
113 dent->d_name.name, dir_ino->i_ino);
114 /* Convert the name of the dentry to Unicode. */
115 uname_len = ntfs_nlstoucs(vol, dent->d_name.name, dent->d_name.len,
118 ntfs_error(vol->sb, "Failed to convert name to Unicode.");
119 return ERR_PTR(uname_len);
121 mref = ntfs_lookup_inode_by_name(NTFS_I(dir_ino), uname, uname_len,
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. */
135 ntfs_debug("Done. (Case 1.)");
136 return d_splice_alias(dent_inode, dent);
139 * We are too indented. Handle imperfect
140 * matches and short file names further below.
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);
151 dent_inode = ERR_PTR(-EIO);
153 ntfs_error(vol->sb, "ntfs_iget(0x%lx) failed with "
154 "error code %li.", dent_ino,
155 PTR_ERR(dent_inode));
157 /* Return the error code. */
158 return (struct dentry *)dent_inode;
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. */
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));
172 // TODO: Consider moving this lot to a separate function! (AIA)
175 struct dentry *real_dent, *new_dent;
177 ntfs_attr_search_ctx *ctx;
178 ntfs_inode *ni = NTFS_I(dent_inode);
180 struct qstr nls_name;
182 nls_name.name = NULL;
183 if (name->type != FILE_NAME_DOS) { /* Case 2. */
184 ntfs_debug("Case 2.");
185 nls_name.len = (unsigned)ntfs_ucstonls(vol,
186 (ntfschar*)&name->name, name->len,
187 (unsigned char**)&nls_name.name, 0);
189 } else /* if (name->type == FILE_NAME_DOS) */ { /* Case 3. */
192 ntfs_debug("Case 3.");
195 /* Find the WIN32 name corresponding to the matched DOS name. */
196 ni = NTFS_I(dent_inode);
197 m = map_mft_record(ni);
204 ctx = ntfs_attr_get_search_ctx(ni, m);
205 if (unlikely(!ctx)) {
213 err = ntfs_attr_lookup(AT_FILE_NAME, NULL, 0, 0, 0,
216 ntfs_error(vol->sb, "Inode corrupt: No WIN32 "
217 "namespace counterpart to DOS "
218 "file name. Run chkdsk.");
223 /* Consistency checks. */
225 if (a->non_resident || a->flags)
227 val_len = le32_to_cpu(a->data.resident.value_length);
228 if (le16_to_cpu(a->data.resident.value_offset) +
229 val_len > le32_to_cpu(a->length))
231 fn = (FILE_NAME_ATTR*)((u8*)ctx->attr + le16_to_cpu(
232 ctx->attr->data.resident.value_offset));
233 if ((u32)(fn->file_name_length * sizeof(ntfschar) +
234 sizeof(FILE_NAME_ATTR)) > val_len)
236 } while (fn->file_name_type != FILE_NAME_WIN32);
238 /* Convert the found WIN32 name to current NLS code page. */
239 nls_name.len = (unsigned)ntfs_ucstonls(vol,
240 (ntfschar*)&fn->file_name, fn->file_name_length,
241 (unsigned char**)&nls_name.name, 0);
243 ntfs_attr_put_search_ctx(ctx);
244 unmap_mft_record(ni);
249 /* Check if a conversion error occurred. */
250 if ((signed)nls_name.len < 0) {
251 err = (signed)nls_name.len;
254 nls_name.hash = full_name_hash(nls_name.name, nls_name.len);
257 * Note: No need for dent->d_lock lock as i_mutex is held on the
261 /* Does a dentry matching the nls_name exist already? */
262 real_dent = d_lookup(dent->d_parent, &nls_name);
263 /* If not, create it now. */
265 real_dent = d_alloc(dent->d_parent, &nls_name);
266 kfree(nls_name.name);
271 new_dent = d_splice_alias(dent_inode, real_dent);
275 new_dent = real_dent;
276 ntfs_debug("Done. (Created new dentry.)");
279 kfree(nls_name.name);
280 /* Matching dentry exists, check if it is negative. */
281 if (real_dent->d_inode) {
282 if (unlikely(real_dent->d_inode != dent_inode)) {
283 /* This can happen because bad inodes are unhashed. */
284 BUG_ON(!is_bad_inode(dent_inode));
285 BUG_ON(!is_bad_inode(real_dent->d_inode));
288 * Already have the inode and the dentry attached, decrement
289 * the reference count to balance the ntfs_iget() we did
290 * earlier on. We found the dentry using d_lookup() so it
291 * cannot be disconnected and thus we do not need to worry
292 * about any NFS/disconnectedness issues here.
295 ntfs_debug("Done. (Already had inode and dentry.)");
299 * Negative dentry: instantiate it unless the inode is a directory and
300 * has a 'disconnected' dentry (i.e. IS_ROOT and DCACHE_DISCONNECTED),
301 * in which case d_move() that in place of the found dentry.
303 if (!S_ISDIR(dent_inode->i_mode)) {
304 /* Not a directory; everything is easy. */
305 d_instantiate(real_dent, dent_inode);
306 ntfs_debug("Done. (Already had negative file dentry.)");
309 spin_lock(&dcache_lock);
310 if (list_empty(&dent_inode->i_dentry)) {
312 * Directory without a 'disconnected' dentry; we need to do
313 * d_instantiate() by hand because it takes dcache_lock which
316 list_add(&real_dent->d_alias, &dent_inode->i_dentry);
317 real_dent->d_inode = dent_inode;
318 spin_unlock(&dcache_lock);
319 security_d_instantiate(real_dent, dent_inode);
320 ntfs_debug("Done. (Already had negative directory dentry.)");
324 * Directory with a 'disconnected' dentry; get a reference to the
325 * 'disconnected' dentry.
327 new_dent = list_entry(dent_inode->i_dentry.next, struct dentry,
329 dget_locked(new_dent);
330 spin_unlock(&dcache_lock);
331 /* Do security vodoo. */
332 security_d_instantiate(real_dent, dent_inode);
333 /* Move new_dent in place of real_dent. */
334 d_move(new_dent, real_dent);
335 /* Balance the ntfs_iget() we did above. */
337 /* Throw away real_dent. */
339 /* Use new_dent as the actual dentry. */
340 ntfs_debug("Done. (Already had negative, disconnected directory "
345 ntfs_error(vol->sb, "Illegal file name attribute. Run chkdsk.");
349 ntfs_attr_put_search_ctx(ctx);
351 unmap_mft_record(ni);
353 ntfs_error(vol->sb, "Failed, returning error code %i.", err);
359 * Inode operations for directories.
361 struct inode_operations ntfs_dir_inode_ops = {
362 .lookup = ntfs_lookup, /* VFS: Lookup directory. */
366 * ntfs_get_parent - find the dentry of the parent of a given directory dentry
367 * @child_dent: dentry of the directory whose parent directory to find
369 * Find the dentry for the parent directory of the directory specified by the
370 * dentry @child_dent. This function is called from
371 * fs/exportfs/expfs.c::find_exported_dentry() which in turn is called from the
372 * default ->decode_fh() which is export_decode_fh() in the same file.
374 * The code is based on the ext3 ->get_parent() implementation found in
375 * fs/ext3/namei.c::ext3_get_parent().
377 * Note: ntfs_get_parent() is called with @child_dent->d_inode->i_mutex down.
379 * Return the dentry of the parent directory on success or the error code on
380 * error (IS_ERR() is true).
382 static struct dentry *ntfs_get_parent(struct dentry *child_dent)
384 struct inode *vi = child_dent->d_inode;
385 ntfs_inode *ni = NTFS_I(vi);
387 ntfs_attr_search_ctx *ctx;
390 struct inode *parent_vi;
391 struct dentry *parent_dent;
392 unsigned long parent_ino;
395 ntfs_debug("Entering for inode 0x%lx.", vi->i_ino);
396 /* Get the mft record of the inode belonging to the child dentry. */
397 mrec = map_mft_record(ni);
399 return (struct dentry *)mrec;
400 /* Find the first file name attribute in the mft record. */
401 ctx = ntfs_attr_get_search_ctx(ni, mrec);
402 if (unlikely(!ctx)) {
403 unmap_mft_record(ni);
404 return ERR_PTR(-ENOMEM);
407 err = ntfs_attr_lookup(AT_FILE_NAME, NULL, 0, CASE_SENSITIVE, 0, NULL,
410 ntfs_attr_put_search_ctx(ctx);
411 unmap_mft_record(ni);
413 ntfs_error(vi->i_sb, "Inode 0x%lx does not have a "
414 "file name attribute. Run chkdsk.",
419 if (unlikely(attr->non_resident))
421 fn = (FILE_NAME_ATTR *)((u8 *)attr +
422 le16_to_cpu(attr->data.resident.value_offset));
423 if (unlikely((u8 *)fn + le32_to_cpu(attr->data.resident.value_length) >
424 (u8*)attr + le32_to_cpu(attr->length)))
426 /* Get the inode number of the parent directory. */
427 parent_ino = MREF_LE(fn->parent_directory);
428 /* Release the search context and the mft record of the child. */
429 ntfs_attr_put_search_ctx(ctx);
430 unmap_mft_record(ni);
431 /* Get the inode of the parent directory. */
432 parent_vi = ntfs_iget(vi->i_sb, parent_ino);
433 if (IS_ERR(parent_vi) || unlikely(is_bad_inode(parent_vi))) {
434 if (!IS_ERR(parent_vi))
436 ntfs_error(vi->i_sb, "Failed to get parent directory inode "
437 "0x%lx of child inode 0x%lx.", parent_ino,
439 return ERR_PTR(-EACCES);
441 /* Finally get a dentry for the parent directory and return it. */
442 parent_dent = d_alloc_anon(parent_vi);
443 if (unlikely(!parent_dent)) {
445 return ERR_PTR(-ENOMEM);
447 ntfs_debug("Done for inode 0x%lx.", vi->i_ino);
452 * ntfs_get_dentry - find a dentry for the inode from a file handle sub-fragment
453 * @sb: super block identifying the mounted ntfs volume
454 * @fh: the file handle sub-fragment
456 * Find a dentry for the inode given a file handle sub-fragment. This function
457 * is called from fs/exportfs/expfs.c::find_exported_dentry() which in turn is
458 * called from the default ->decode_fh() which is export_decode_fh() in the
459 * same file. The code is closely based on the default ->get_dentry() helper
460 * fs/exportfs/expfs.c::get_object().
462 * The @fh contains two 32-bit unsigned values, the first one is the inode
463 * number and the second one is the inode generation.
465 * Return the dentry on success or the error code on error (IS_ERR() is true).
467 static struct dentry *ntfs_get_dentry(struct super_block *sb, void *fh)
471 unsigned long ino = ((u32 *)fh)[0];
472 u32 gen = ((u32 *)fh)[1];
474 ntfs_debug("Entering for inode 0x%lx, generation 0x%x.", ino, gen);
475 vi = ntfs_iget(sb, ino);
477 ntfs_error(sb, "Failed to get inode 0x%lx.", ino);
478 return (struct dentry *)vi;
480 if (unlikely(is_bad_inode(vi) || vi->i_generation != gen)) {
481 /* We didn't find the right inode. */
482 ntfs_error(sb, "Inode 0x%lx, bad count: %d %d or version 0x%x "
483 "0x%x.", vi->i_ino, vi->i_nlink,
484 atomic_read(&vi->i_count), vi->i_generation,
487 return ERR_PTR(-ESTALE);
489 /* Now find a dentry. If possible, get a well-connected one. */
490 dent = d_alloc_anon(vi);
491 if (unlikely(!dent)) {
493 return ERR_PTR(-ENOMEM);
495 ntfs_debug("Done for inode 0x%lx, generation 0x%x.", ino, gen);
500 * Export operations allowing NFS exporting of mounted NTFS partitions.
502 * We use the default ->decode_fh() and ->encode_fh() for now. Note that they
503 * use 32 bits to store the inode number which is an unsigned long so on 64-bit
504 * architectures is usually 64 bits so it would all fail horribly on huge
505 * volumes. I guess we need to define our own encode and decode fh functions
506 * that store 64-bit inode numbers at some point but for now we will ignore the
509 * We also use the default ->get_name() helper (used by ->decode_fh() via
510 * fs/exportfs/expfs.c::find_exported_dentry()) as that is completely fs
513 * The default ->get_parent() just returns -EACCES so we have to provide our
514 * own and the default ->get_dentry() is incompatible with NTFS due to not
515 * allowing the inode number 0 which is used in NTFS for the system file $MFT
516 * and due to using iget() whereas NTFS needs ntfs_iget().
518 struct export_operations ntfs_export_ops = {
519 .get_parent = ntfs_get_parent, /* Find the parent of a given
521 .get_dentry = ntfs_get_dentry, /* Find a dentry for the inode