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>
30 * ntfs_lookup - find the inode represented by a dentry in a directory inode
31 * @dir_ino: directory inode in which to look for the inode
32 * @dent: dentry representing the inode to look for
33 * @nd: lookup nameidata
35 * In short, ntfs_lookup() looks for the inode represented by the dentry @dent
36 * in the directory inode @dir_ino and if found attaches the inode to the
39 * In more detail, the dentry @dent specifies which inode to look for by
40 * supplying the name of the inode in @dent->d_name.name. ntfs_lookup()
41 * converts the name to Unicode and walks the contents of the directory inode
42 * @dir_ino looking for the converted Unicode name. If the name is found in the
43 * directory, the corresponding inode is loaded by calling ntfs_iget() on its
44 * inode number and the inode is associated with the dentry @dent via a call to
47 * If the name is not found in the directory, a NULL inode is inserted into the
48 * dentry @dent via a call to d_add(). The dentry is then termed a negative
51 * Only if an actual error occurs, do we return an error via ERR_PTR().
53 * In order to handle the case insensitivity issues of NTFS with regards to the
54 * dcache and the dcache requiring only one dentry per directory, we deal with
55 * dentry aliases that only differ in case in ->ntfs_lookup() while maintaining
56 * a case sensitive dcache. This means that we get the full benefit of dcache
57 * speed when the file/directory is looked up with the same case as returned by
58 * ->ntfs_readdir() but that a lookup for any other case (or for the short file
59 * name) will not find anything in dcache and will enter ->ntfs_lookup()
60 * instead, where we search the directory for a fully matching file name
61 * (including case) and if that is not found, we search for a file name that
62 * matches with different case and if that has non-POSIX semantics we return
63 * that. We actually do only one search (case sensitive) and keep tabs on
64 * whether we have found a case insensitive match in the process.
66 * To simplify matters for us, we do not treat the short vs long filenames as
67 * two hard links but instead if the lookup matches a short filename, we
68 * return the dentry for the corresponding long filename instead.
70 * There are three cases we need to distinguish here:
72 * 1) @dent perfectly matches (i.e. including case) a directory entry with a
73 * file name in the WIN32 or POSIX namespaces. In this case
74 * ntfs_lookup_inode_by_name() will return with name set to NULL and we
75 * just d_splice_alias() @dent.
76 * 2) @dent matches (not including case) a directory entry with a file name in
77 * the WIN32 namespace. In this case ntfs_lookup_inode_by_name() will return
78 * with name set to point to a kmalloc()ed ntfs_name structure containing
79 * the properly cased little endian Unicode name. We convert the name to the
80 * current NLS code page, search if a dentry with this name already exists
81 * and if so return that instead of @dent. At this point things are
82 * complicated by the possibility of 'disconnected' dentries due to NFS
83 * which we deal with appropriately (see the code comments). The VFS will
84 * then destroy the old @dent and use the one we returned. If a dentry is
85 * not found, we allocate a new one, d_splice_alias() it, and return it as
87 * 3) @dent matches either perfectly or not (i.e. we don't care about case) a
88 * directory entry with a file name in the DOS namespace. In this case
89 * ntfs_lookup_inode_by_name() will return with name set to point to a
90 * kmalloc()ed ntfs_name structure containing the mft reference (cpu endian)
91 * of the inode. We use the mft reference to read the inode and to find the
92 * file name in the WIN32 namespace corresponding to the matched short file
93 * name. We then convert the name to the current NLS code page, and proceed
94 * searching for a dentry with this name, etc, as in case 2), above.
96 static struct dentry *ntfs_lookup(struct inode *dir_ino, struct dentry *dent,
99 ntfs_volume *vol = NTFS_SB(dir_ino->i_sb);
100 struct inode *dent_inode;
102 ntfs_name *name = NULL;
104 unsigned long dent_ino;
107 ntfs_debug("Looking up %s in directory inode 0x%lx.",
108 dent->d_name.name, dir_ino->i_ino);
109 /* Convert the name of the dentry to Unicode. */
110 uname_len = ntfs_nlstoucs(vol, dent->d_name.name, dent->d_name.len,
113 ntfs_error(vol->sb, "Failed to convert name to Unicode.");
114 return ERR_PTR(uname_len);
116 mref = ntfs_lookup_inode_by_name(NTFS_I(dir_ino), uname, uname_len,
118 kmem_cache_free(ntfs_name_cache, uname);
119 if (!IS_ERR_MREF(mref)) {
120 dent_ino = MREF(mref);
121 ntfs_debug("Found inode 0x%lx. Calling ntfs_iget.", dent_ino);
122 dent_inode = ntfs_iget(vol->sb, dent_ino);
123 if (likely(!IS_ERR(dent_inode))) {
124 /* Consistency check. */
125 if (MSEQNO(mref) == NTFS_I(dent_inode)->seq_no ||
126 dent_ino == FILE_MFT) {
127 /* Perfect WIN32/POSIX match. -- Case 1. */
130 return d_splice_alias(dent_inode, dent);
133 * We are too indented. Handle imperfect
134 * matches and short file names further below.
138 ntfs_error(vol->sb, "Found stale reference to inode "
139 "0x%lx (reference sequence number = "
140 "0x%x, inode sequence number = 0x%x), "
141 "returning -EIO. Run chkdsk.",
142 dent_ino, MSEQNO(mref),
143 NTFS_I(dent_inode)->seq_no);
145 dent_inode = ERR_PTR(-EIO);
147 ntfs_error(vol->sb, "ntfs_iget(0x%lx) failed with "
148 "error code %li.", dent_ino,
149 PTR_ERR(dent_inode));
152 /* Return the error code. */
153 return (struct dentry *)dent_inode;
155 /* It is guaranteed that name is no longer allocated at this point. */
156 if (MREF_ERR(mref) == -ENOENT) {
157 ntfs_debug("Entry was not found, adding negative dentry.");
158 /* The dcache will handle negative entries. */
163 ntfs_error(vol->sb, "ntfs_lookup_ino_by_name() failed with error "
164 "code %i.", -MREF_ERR(mref));
165 return ERR_PTR(MREF_ERR(mref));
167 // TODO: Consider moving this lot to a separate function! (AIA)
170 struct dentry *real_dent, *new_dent;
172 attr_search_context *ctx;
173 ntfs_inode *ni = NTFS_I(dent_inode);
175 struct qstr nls_name;
177 nls_name.name = NULL;
178 if (name->type != FILE_NAME_DOS) { /* Case 2. */
179 nls_name.len = (unsigned)ntfs_ucstonls(vol,
180 (ntfschar*)&name->name, name->len,
181 (unsigned char**)&nls_name.name, 0);
183 } else /* if (name->type == FILE_NAME_DOS) */ { /* Case 3. */
188 /* Find the WIN32 name corresponding to the matched DOS name. */
189 ni = NTFS_I(dent_inode);
190 m = map_mft_record(ni);
197 ctx = get_attr_search_ctx(ni, m);
206 if (!lookup_attr(AT_FILE_NAME, NULL, 0, 0, 0, NULL, 0,
208 ntfs_error(vol->sb, "Inode corrupt: No WIN32 "
209 "namespace counterpart to DOS "
210 "file name. Run chkdsk.");
214 /* Consistency checks. */
216 if (a->non_resident || a->flags)
218 val_len = le32_to_cpu(a->data.resident.value_length);
219 if (le16_to_cpu(a->data.resident.value_offset) +
220 val_len > le32_to_cpu(a->length))
222 fn = (FILE_NAME_ATTR*)((u8*)ctx->attr + le16_to_cpu(
223 ctx->attr->data.resident.value_offset));
224 if ((u32)(fn->file_name_length * sizeof(ntfschar) +
225 sizeof(FILE_NAME_ATTR)) > val_len)
227 } while (fn->file_name_type != FILE_NAME_WIN32);
229 /* Convert the found WIN32 name to current NLS code page. */
230 nls_name.len = (unsigned)ntfs_ucstonls(vol,
231 (ntfschar*)&fn->file_name, fn->file_name_length,
232 (unsigned char**)&nls_name.name, 0);
234 put_attr_search_ctx(ctx);
235 unmap_mft_record(ni);
240 /* Check if a conversion error occurred. */
241 if ((signed)nls_name.len < 0) {
242 err = (signed)nls_name.len;
245 nls_name.hash = full_name_hash(nls_name.name, nls_name.len);
248 * Note: No need for dent->d_lock lock as i_sem is held on the
252 /* Does a dentry matching the nls_name exist already? */
253 real_dent = d_lookup(dent->d_parent, &nls_name);
254 /* If not, create it now. */
256 real_dent = d_alloc(dent->d_parent, &nls_name);
257 kfree(nls_name.name);
262 new_dent = d_splice_alias(dent_inode, real_dent);
266 new_dent = real_dent;
269 kfree(nls_name.name);
270 /* Matching dentry exists, check if it is negative. */
271 if (real_dent->d_inode) {
272 BUG_ON(real_dent->d_inode != dent_inode);
274 * Already have the inode and the dentry attached, decrement
275 * the reference count to balance the ntfs_iget() we did
276 * earlier on. We found the dentry using d_lookup() so it
277 * cannot be disconnected and thus we do not need to worry
278 * about any NFS/disconnectedness issues here.
284 * Negative dentry: instantiate it unless the inode is a directory and
285 * has a 'disconnected' dentry (i.e. IS_ROOT and DCACHE_DISCONNECTED),
286 * in which case d_move() that in place of the found dentry.
288 if (!S_ISDIR(dent_inode->i_mode)) {
289 /* Not a directory; everything is easy. */
290 d_instantiate(real_dent, dent_inode);
293 spin_lock(&dcache_lock);
294 if (list_empty(&dent_inode->i_dentry)) {
296 * Directory without a 'disconnected' dentry; we need to do
297 * d_instantiate() by hand because it takes dcache_lock which
300 list_add(&real_dent->d_alias, &dent_inode->i_dentry);
301 real_dent->d_inode = dent_inode;
302 spin_unlock(&dcache_lock);
303 security_d_instantiate(real_dent, dent_inode);
307 * Directory with a 'disconnected' dentry; get a reference to the
308 * 'disconnected' dentry.
310 new_dent = list_entry(dent_inode->i_dentry.next, struct dentry,
312 dget_locked(new_dent);
313 spin_unlock(&dcache_lock);
314 /* Do security vodoo. */
315 security_d_instantiate(real_dent, dent_inode);
316 /* Move new_dent in place of real_dent. */
317 d_move(new_dent, real_dent);
318 /* Balance the ntfs_iget() we did above. */
320 /* Throw away real_dent. */
322 /* Use new_dent as the actual dentry. */
326 ntfs_error(vol->sb, "Illegal file name attribute. Run chkdsk.");
330 put_attr_search_ctx(ctx);
332 unmap_mft_record(ni);
339 * Inode operations for directories.
341 struct inode_operations ntfs_dir_inode_ops = {
342 .lookup = ntfs_lookup, /* VFS: Lookup directory. */
346 * ntfs_get_parent - find the dentry of the parent of a given directory dentry
347 * @child_dent: dentry of the directory whose parent directory to find
349 * Find the dentry for the parent directory of the directory specified by the
350 * dentry @child_dent. This function is called from
351 * fs/exportfs/expfs.c::find_exported_dentry() which in turn is called from the
352 * default ->decode_fh() which is export_decode_fh() in the same file.
354 * The code is based on the ext3 ->get_parent() implementation found in
355 * fs/ext3/namei.c::ext3_get_parent().
357 * Note: ntfs_get_parent() is called with @child_dent->d_inode->i_sem down.
359 * Return the dentry of the parent directory on success or the error code on
360 * error (IS_ERR() is true).
362 struct dentry *ntfs_get_parent(struct dentry *child_dent)
364 struct inode *vi = child_dent->d_inode;
365 ntfs_inode *ni = NTFS_I(vi);
367 attr_search_context *ctx;
370 struct inode *parent_vi;
371 struct dentry *parent_dent;
372 unsigned long parent_ino;
374 ntfs_debug("Entering for inode 0x%lx.", vi->i_ino);
375 /* Get the mft record of the inode belonging to the child dentry. */
376 mrec = map_mft_record(ni);
377 if (unlikely(IS_ERR(mrec)))
378 return (struct dentry *)mrec;
379 /* Find the first file name attribute in the mft record. */
380 ctx = get_attr_search_ctx(ni, mrec);
381 if (unlikely(!ctx)) {
382 unmap_mft_record(ni);
383 return ERR_PTR(-ENOMEM);
386 if (unlikely(!lookup_attr(AT_FILE_NAME, NULL, 0, IGNORE_CASE, 0,
388 put_attr_search_ctx(ctx);
389 unmap_mft_record(ni);
390 ntfs_error(vi->i_sb, "Inode 0x%lx does not have a file name "
391 "attribute. Run chkdsk.", vi->i_ino);
392 return ERR_PTR(-ENOENT);
395 if (unlikely(attr->non_resident))
397 fn = (FILE_NAME_ATTR *)((u8 *)attr +
398 le16_to_cpu(attr->data.resident.value_offset));
399 if (unlikely((u8 *)fn + le32_to_cpu(attr->data.resident.value_length) >
400 (u8*)attr + le32_to_cpu(attr->length)))
402 /* Get the inode number of the parent directory. */
403 parent_ino = MREF_LE(fn->parent_directory);
404 /* Release the search context and the mft record of the child. */
405 put_attr_search_ctx(ctx);
406 unmap_mft_record(ni);
407 /* Get the inode of the parent directory. */
408 parent_vi = ntfs_iget(vi->i_sb, parent_ino);
409 if (unlikely(IS_ERR(parent_vi) || is_bad_inode(parent_vi))) {
410 if (!IS_ERR(parent_vi))
412 ntfs_error(vi->i_sb, "Failed to get parent directory inode "
413 "0x%lx of child inode 0x%lx.", parent_ino,
415 return ERR_PTR(-EACCES);
417 /* Finally get a dentry for the parent directory and return it. */
418 parent_dent = d_alloc_anon(parent_vi);
419 if (unlikely(!parent_dent)) {
421 return ERR_PTR(-ENOMEM);
423 ntfs_debug("Done for inode 0x%lx.", vi->i_ino);
428 * ntfs_get_dentry - find a dentry for the inode from a file handle sub-fragment
429 * @sb: super block identifying the mounted ntfs volume
430 * @fh: the file handle sub-fragment
432 * Find a dentry for the inode given a file handle sub-fragment. This function
433 * is called from fs/exportfs/expfs.c::find_exported_dentry() which in turn is
434 * called from the default ->decode_fh() which is export_decode_fh() in the
435 * same file. The code is closely based on the default ->get_dentry() helper
436 * fs/exportfs/expfs.c::get_object().
438 * The @fh contains two 32-bit unsigned values, the first one is the inode
439 * number and the second one is the inode generation.
441 * Return the dentry on success or the error code on error (IS_ERR() is true).
443 struct dentry *ntfs_get_dentry(struct super_block *sb, void *fh)
447 unsigned long ino = ((u32 *)fh)[0];
448 u32 gen = ((u32 *)fh)[1];
450 ntfs_debug("Entering for inode 0x%lx, generation 0x%x.", ino, gen);
451 vi = ntfs_iget(sb, ino);
452 if (unlikely(IS_ERR(vi))) {
453 ntfs_error(sb, "Failed to get inode 0x%lx.", ino);
454 return (struct dentry *)vi;
456 if (unlikely(is_bad_inode(vi) || vi->i_generation != gen)) {
457 /* We didn't find the right inode. */
458 ntfs_error(sb, "Inode 0x%lx, bad count: %d %d or version 0x%x "
459 "0x%x.", vi->i_ino, vi->i_nlink,
460 atomic_read(&vi->i_count), vi->i_generation,
463 return ERR_PTR(-ESTALE);
465 /* Now find a dentry. If possible, get a well-connected one. */
466 dent = d_alloc_anon(vi);
467 if (unlikely(!dent)) {
469 return ERR_PTR(-ENOMEM);
471 ntfs_debug("Done for inode 0x%lx, generation 0x%x.", ino, gen);