2 * namei.c - NTFS kernel directory inode operations. Part of the Linux-NTFS
5 * Copyright (c) 2001-2006 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/exportfs.h>
25 #include <linux/security.h>
34 * ntfs_lookup - find the inode represented by a dentry in a directory inode
35 * @dir_ino: directory inode in which to look for the inode
36 * @dent: dentry representing the inode to look for
37 * @nd: lookup nameidata
39 * In short, ntfs_lookup() looks for the inode represented by the dentry @dent
40 * in the directory inode @dir_ino and if found attaches the inode to the
43 * In more detail, the dentry @dent specifies which inode to look for by
44 * supplying the name of the inode in @dent->d_name.name. ntfs_lookup()
45 * converts the name to Unicode and walks the contents of the directory inode
46 * @dir_ino looking for the converted Unicode name. If the name is found in the
47 * directory, the corresponding inode is loaded by calling ntfs_iget() on its
48 * inode number and the inode is associated with the dentry @dent via a call to
51 * If the name is not found in the directory, a NULL inode is inserted into the
52 * dentry @dent via a call to d_add(). The dentry is then termed a negative
55 * Only if an actual error occurs, do we return an error via ERR_PTR().
57 * In order to handle the case insensitivity issues of NTFS with regards to the
58 * dcache and the dcache requiring only one dentry per directory, we deal with
59 * dentry aliases that only differ in case in ->ntfs_lookup() while maintaining
60 * a case sensitive dcache. This means that we get the full benefit of dcache
61 * speed when the file/directory is looked up with the same case as returned by
62 * ->ntfs_readdir() but that a lookup for any other case (or for the short file
63 * name) will not find anything in dcache and will enter ->ntfs_lookup()
64 * instead, where we search the directory for a fully matching file name
65 * (including case) and if that is not found, we search for a file name that
66 * matches with different case and if that has non-POSIX semantics we return
67 * that. We actually do only one search (case sensitive) and keep tabs on
68 * whether we have found a case insensitive match in the process.
70 * To simplify matters for us, we do not treat the short vs long filenames as
71 * two hard links but instead if the lookup matches a short filename, we
72 * return the dentry for the corresponding long filename instead.
74 * There are three cases we need to distinguish here:
76 * 1) @dent perfectly matches (i.e. including case) a directory entry with a
77 * file name in the WIN32 or POSIX namespaces. In this case
78 * ntfs_lookup_inode_by_name() will return with name set to NULL and we
79 * just d_splice_alias() @dent.
80 * 2) @dent matches (not including case) a directory entry with a file name in
81 * the WIN32 namespace. In this case ntfs_lookup_inode_by_name() will return
82 * with name set to point to a kmalloc()ed ntfs_name structure containing
83 * the properly cased little endian Unicode name. We convert the name to the
84 * current NLS code page, search if a dentry with this name already exists
85 * and if so return that instead of @dent. At this point things are
86 * complicated by the possibility of 'disconnected' dentries due to NFS
87 * which we deal with appropriately (see the code comments). The VFS will
88 * then destroy the old @dent and use the one we returned. If a dentry is
89 * not found, we allocate a new one, d_splice_alias() it, and return it as
91 * 3) @dent matches either perfectly or not (i.e. we don't care about case) a
92 * directory entry with a file name in the DOS namespace. In this case
93 * ntfs_lookup_inode_by_name() will return with name set to point to a
94 * kmalloc()ed ntfs_name structure containing the mft reference (cpu endian)
95 * of the inode. We use the mft reference to read the inode and to find the
96 * file name in the WIN32 namespace corresponding to the matched short file
97 * name. We then convert the name to the current NLS code page, and proceed
98 * searching for a dentry with this name, etc, as in case 2), above.
100 * Locking: Caller must hold i_mutex on the directory.
102 static struct dentry
*ntfs_lookup(struct inode
*dir_ino
, struct dentry
*dent
,
103 struct nameidata
*nd
)
105 ntfs_volume
*vol
= NTFS_SB(dir_ino
->i_sb
);
106 struct inode
*dent_inode
;
108 ntfs_name
*name
= NULL
;
110 unsigned long dent_ino
;
113 ntfs_debug("Looking up %s in directory inode 0x%lx.",
114 dent
->d_name
.name
, dir_ino
->i_ino
);
115 /* Convert the name of the dentry to Unicode. */
116 uname_len
= ntfs_nlstoucs(vol
, dent
->d_name
.name
, dent
->d_name
.len
,
119 if (uname_len
!= -ENAMETOOLONG
)
120 ntfs_error(vol
->sb
, "Failed to convert name to "
122 return ERR_PTR(uname_len
);
124 mref
= ntfs_lookup_inode_by_name(NTFS_I(dir_ino
), uname
, uname_len
,
126 kmem_cache_free(ntfs_name_cache
, uname
);
127 if (!IS_ERR_MREF(mref
)) {
128 dent_ino
= MREF(mref
);
129 ntfs_debug("Found inode 0x%lx. Calling ntfs_iget.", dent_ino
);
130 dent_inode
= ntfs_iget(vol
->sb
, dent_ino
);
131 if (likely(!IS_ERR(dent_inode
))) {
132 /* Consistency check. */
133 if (is_bad_inode(dent_inode
) || MSEQNO(mref
) ==
134 NTFS_I(dent_inode
)->seq_no
||
135 dent_ino
== FILE_MFT
) {
136 /* Perfect WIN32/POSIX match. -- Case 1. */
138 ntfs_debug("Done. (Case 1.)");
139 return d_splice_alias(dent_inode
, dent
);
142 * We are too indented. Handle imperfect
143 * matches and short file names further below.
147 ntfs_error(vol
->sb
, "Found stale reference to inode "
148 "0x%lx (reference sequence number = "
149 "0x%x, inode sequence number = 0x%x), "
150 "returning -EIO. Run chkdsk.",
151 dent_ino
, MSEQNO(mref
),
152 NTFS_I(dent_inode
)->seq_no
);
154 dent_inode
= ERR_PTR(-EIO
);
156 ntfs_error(vol
->sb
, "ntfs_iget(0x%lx) failed with "
157 "error code %li.", dent_ino
,
158 PTR_ERR(dent_inode
));
160 /* Return the error code. */
161 return (struct dentry
*)dent_inode
;
163 /* It is guaranteed that @name is no longer allocated at this point. */
164 if (MREF_ERR(mref
) == -ENOENT
) {
165 ntfs_debug("Entry was not found, adding negative dentry.");
166 /* The dcache will handle negative entries. */
171 ntfs_error(vol
->sb
, "ntfs_lookup_ino_by_name() failed with error "
172 "code %i.", -MREF_ERR(mref
));
173 return ERR_PTR(MREF_ERR(mref
));
174 // TODO: Consider moving this lot to a separate function! (AIA)
177 struct dentry
*real_dent
, *new_dent
;
179 ntfs_attr_search_ctx
*ctx
;
180 ntfs_inode
*ni
= NTFS_I(dent_inode
);
182 struct qstr nls_name
;
184 nls_name
.name
= NULL
;
185 if (name
->type
!= FILE_NAME_DOS
) { /* Case 2. */
186 ntfs_debug("Case 2.");
187 nls_name
.len
= (unsigned)ntfs_ucstonls(vol
,
188 (ntfschar
*)&name
->name
, name
->len
,
189 (unsigned char**)&nls_name
.name
, 0);
191 } else /* if (name->type == FILE_NAME_DOS) */ { /* Case 3. */
194 ntfs_debug("Case 3.");
197 /* Find the WIN32 name corresponding to the matched DOS name. */
198 ni
= NTFS_I(dent_inode
);
199 m
= map_mft_record(ni
);
206 ctx
= ntfs_attr_get_search_ctx(ni
, m
);
207 if (unlikely(!ctx
)) {
215 err
= ntfs_attr_lookup(AT_FILE_NAME
, NULL
, 0, 0, 0,
218 ntfs_error(vol
->sb
, "Inode corrupt: No WIN32 "
219 "namespace counterpart to DOS "
220 "file name. Run chkdsk.");
225 /* Consistency checks. */
227 if (a
->non_resident
|| a
->flags
)
229 val_len
= le32_to_cpu(a
->data
.resident
.value_length
);
230 if (le16_to_cpu(a
->data
.resident
.value_offset
) +
231 val_len
> le32_to_cpu(a
->length
))
233 fn
= (FILE_NAME_ATTR
*)((u8
*)ctx
->attr
+ le16_to_cpu(
234 ctx
->attr
->data
.resident
.value_offset
));
235 if ((u32
)(fn
->file_name_length
* sizeof(ntfschar
) +
236 sizeof(FILE_NAME_ATTR
)) > val_len
)
238 } while (fn
->file_name_type
!= FILE_NAME_WIN32
);
240 /* Convert the found WIN32 name to current NLS code page. */
241 nls_name
.len
= (unsigned)ntfs_ucstonls(vol
,
242 (ntfschar
*)&fn
->file_name
, fn
->file_name_length
,
243 (unsigned char**)&nls_name
.name
, 0);
245 ntfs_attr_put_search_ctx(ctx
);
246 unmap_mft_record(ni
);
251 /* Check if a conversion error occurred. */
252 if ((signed)nls_name
.len
< 0) {
253 err
= (signed)nls_name
.len
;
256 nls_name
.hash
= full_name_hash(nls_name
.name
, nls_name
.len
);
259 * Note: No need for dent->d_lock lock as i_mutex is held on the
263 /* Does a dentry matching the nls_name exist already? */
264 real_dent
= d_lookup(dent
->d_parent
, &nls_name
);
265 /* If not, create it now. */
267 real_dent
= d_alloc(dent
->d_parent
, &nls_name
);
268 kfree(nls_name
.name
);
273 new_dent
= d_splice_alias(dent_inode
, real_dent
);
277 new_dent
= real_dent
;
278 ntfs_debug("Done. (Created new dentry.)");
281 kfree(nls_name
.name
);
282 /* Matching dentry exists, check if it is negative. */
283 if (real_dent
->d_inode
) {
284 if (unlikely(real_dent
->d_inode
!= dent_inode
)) {
285 /* This can happen because bad inodes are unhashed. */
286 BUG_ON(!is_bad_inode(dent_inode
));
287 BUG_ON(!is_bad_inode(real_dent
->d_inode
));
290 * Already have the inode and the dentry attached, decrement
291 * the reference count to balance the ntfs_iget() we did
292 * earlier on. We found the dentry using d_lookup() so it
293 * cannot be disconnected and thus we do not need to worry
294 * about any NFS/disconnectedness issues here.
297 ntfs_debug("Done. (Already had inode and dentry.)");
301 * Negative dentry: instantiate it unless the inode is a directory and
302 * has a 'disconnected' dentry (i.e. IS_ROOT and DCACHE_DISCONNECTED),
303 * in which case d_move() that in place of the found dentry.
305 if (!S_ISDIR(dent_inode
->i_mode
)) {
306 /* Not a directory; everything is easy. */
307 d_instantiate(real_dent
, dent_inode
);
308 ntfs_debug("Done. (Already had negative file dentry.)");
311 spin_lock(&dcache_lock
);
312 if (list_empty(&dent_inode
->i_dentry
)) {
314 * Directory without a 'disconnected' dentry; we need to do
315 * d_instantiate() by hand because it takes dcache_lock which
318 list_add(&real_dent
->d_alias
, &dent_inode
->i_dentry
);
319 real_dent
->d_inode
= dent_inode
;
320 spin_unlock(&dcache_lock
);
321 security_d_instantiate(real_dent
, dent_inode
);
322 ntfs_debug("Done. (Already had negative directory dentry.)");
326 * Directory with a 'disconnected' dentry; get a reference to the
327 * 'disconnected' dentry.
329 new_dent
= list_entry(dent_inode
->i_dentry
.next
, struct dentry
,
331 dget_locked(new_dent
);
332 spin_unlock(&dcache_lock
);
333 /* Do security vodoo. */
334 security_d_instantiate(real_dent
, dent_inode
);
335 /* Move new_dent in place of real_dent. */
336 d_move(new_dent
, real_dent
);
337 /* Balance the ntfs_iget() we did above. */
339 /* Throw away real_dent. */
341 /* Use new_dent as the actual dentry. */
342 ntfs_debug("Done. (Already had negative, disconnected directory "
347 ntfs_error(vol
->sb
, "Illegal file name attribute. Run chkdsk.");
351 ntfs_attr_put_search_ctx(ctx
);
353 unmap_mft_record(ni
);
355 ntfs_error(vol
->sb
, "Failed, returning error code %i.", err
);
361 * Inode operations for directories.
363 const struct inode_operations ntfs_dir_inode_ops
= {
364 .lookup
= ntfs_lookup
, /* VFS: Lookup directory. */
368 * ntfs_get_parent - find the dentry of the parent of a given directory dentry
369 * @child_dent: dentry of the directory whose parent directory to find
371 * Find the dentry for the parent directory of the directory specified by the
372 * dentry @child_dent. This function is called from
373 * fs/exportfs/expfs.c::find_exported_dentry() which in turn is called from the
374 * default ->decode_fh() which is export_decode_fh() in the same file.
376 * The code is based on the ext3 ->get_parent() implementation found in
377 * fs/ext3/namei.c::ext3_get_parent().
379 * Note: ntfs_get_parent() is called with @child_dent->d_inode->i_mutex down.
381 * Return the dentry of the parent directory on success or the error code on
382 * error (IS_ERR() is true).
384 static struct dentry
*ntfs_get_parent(struct dentry
*child_dent
)
386 struct inode
*vi
= child_dent
->d_inode
;
387 ntfs_inode
*ni
= NTFS_I(vi
);
389 ntfs_attr_search_ctx
*ctx
;
392 struct inode
*parent_vi
;
393 struct dentry
*parent_dent
;
394 unsigned long parent_ino
;
397 ntfs_debug("Entering for inode 0x%lx.", vi
->i_ino
);
398 /* Get the mft record of the inode belonging to the child dentry. */
399 mrec
= map_mft_record(ni
);
401 return (struct dentry
*)mrec
;
402 /* Find the first file name attribute in the mft record. */
403 ctx
= ntfs_attr_get_search_ctx(ni
, mrec
);
404 if (unlikely(!ctx
)) {
405 unmap_mft_record(ni
);
406 return ERR_PTR(-ENOMEM
);
409 err
= ntfs_attr_lookup(AT_FILE_NAME
, NULL
, 0, CASE_SENSITIVE
, 0, NULL
,
412 ntfs_attr_put_search_ctx(ctx
);
413 unmap_mft_record(ni
);
415 ntfs_error(vi
->i_sb
, "Inode 0x%lx does not have a "
416 "file name attribute. Run chkdsk.",
421 if (unlikely(attr
->non_resident
))
423 fn
= (FILE_NAME_ATTR
*)((u8
*)attr
+
424 le16_to_cpu(attr
->data
.resident
.value_offset
));
425 if (unlikely((u8
*)fn
+ le32_to_cpu(attr
->data
.resident
.value_length
) >
426 (u8
*)attr
+ le32_to_cpu(attr
->length
)))
428 /* Get the inode number of the parent directory. */
429 parent_ino
= MREF_LE(fn
->parent_directory
);
430 /* Release the search context and the mft record of the child. */
431 ntfs_attr_put_search_ctx(ctx
);
432 unmap_mft_record(ni
);
433 /* Get the inode of the parent directory. */
434 parent_vi
= ntfs_iget(vi
->i_sb
, parent_ino
);
435 if (IS_ERR(parent_vi
) || unlikely(is_bad_inode(parent_vi
))) {
436 if (!IS_ERR(parent_vi
))
438 ntfs_error(vi
->i_sb
, "Failed to get parent directory inode "
439 "0x%lx of child inode 0x%lx.", parent_ino
,
441 return ERR_PTR(-EACCES
);
443 /* Finally get a dentry for the parent directory and return it. */
444 parent_dent
= d_alloc_anon(parent_vi
);
445 if (unlikely(!parent_dent
)) {
447 return ERR_PTR(-ENOMEM
);
449 ntfs_debug("Done for inode 0x%lx.", vi
->i_ino
);
454 * ntfs_get_dentry - find a dentry for the inode from a file handle sub-fragment
455 * @sb: super block identifying the mounted ntfs volume
456 * @fh: the file handle sub-fragment
458 * Find a dentry for the inode given a file handle sub-fragment. This function
459 * is called from fs/exportfs/expfs.c::find_exported_dentry() which in turn is
460 * called from the default ->decode_fh() which is export_decode_fh() in the
461 * same file. The code is closely based on the default ->get_dentry() helper
462 * fs/exportfs/expfs.c::get_object().
464 * The @fh contains two 32-bit unsigned values, the first one is the inode
465 * number and the second one is the inode generation.
467 * Return the dentry on success or the error code on error (IS_ERR() is true).
469 static struct dentry
*ntfs_get_dentry(struct super_block
*sb
, void *fh
)
473 unsigned long ino
= ((u32
*)fh
)[0];
474 u32 gen
= ((u32
*)fh
)[1];
476 ntfs_debug("Entering for inode 0x%lx, generation 0x%x.", ino
, gen
);
477 vi
= ntfs_iget(sb
, ino
);
479 ntfs_error(sb
, "Failed to get inode 0x%lx.", ino
);
480 return (struct dentry
*)vi
;
482 if (unlikely(is_bad_inode(vi
) || vi
->i_generation
!= gen
)) {
483 /* We didn't find the right inode. */
484 ntfs_error(sb
, "Inode 0x%lx, bad count: %d %d or version 0x%x "
485 "0x%x.", vi
->i_ino
, vi
->i_nlink
,
486 atomic_read(&vi
->i_count
), vi
->i_generation
,
489 return ERR_PTR(-ESTALE
);
491 /* Now find a dentry. If possible, get a well-connected one. */
492 dent
= d_alloc_anon(vi
);
493 if (unlikely(!dent
)) {
495 return ERR_PTR(-ENOMEM
);
497 ntfs_debug("Done for inode 0x%lx, generation 0x%x.", ino
, gen
);
502 * Export operations allowing NFS exporting of mounted NTFS partitions.
504 * We use the default ->decode_fh() and ->encode_fh() for now. Note that they
505 * use 32 bits to store the inode number which is an unsigned long so on 64-bit
506 * architectures is usually 64 bits so it would all fail horribly on huge
507 * volumes. I guess we need to define our own encode and decode fh functions
508 * that store 64-bit inode numbers at some point but for now we will ignore the
511 * We also use the default ->get_name() helper (used by ->decode_fh() via
512 * fs/exportfs/expfs.c::find_exported_dentry()) as that is completely fs
515 * The default ->get_parent() just returns -EACCES so we have to provide our
516 * own and the default ->get_dentry() is incompatible with NTFS due to not
517 * allowing the inode number 0 which is used in NTFS for the system file $MFT
518 * and due to using iget() whereas NTFS needs ntfs_iget().
520 struct export_operations ntfs_export_ops
= {
521 .get_parent
= ntfs_get_parent
, /* Find the parent of a given
523 .get_dentry
= ntfs_get_dentry
, /* Find a dentry for the inode