vserver 1.9.3
[linux-2.6.git] / fs / ntfs / namei.c
1 /*
2  * namei.c - NTFS kernel directory inode operations. Part of the Linux-NTFS
3  *           project.
4  *
5  * Copyright (c) 2001-2004 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
23 #include <linux/dcache.h>
24 #include <linux/security.h>
25
26 #include "ntfs.h"
27 #include "dir.h"
28
29 /**
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
34  *
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
37  * dentry @dent.
38  *
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
45  * d_splice_alias().
46  *
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
49  * dentry.
50  *
51  * Only if an actual error occurs, do we return an error via ERR_PTR().
52  *
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.
65  *
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.
69  *
70  * There are three cases we need to distinguish here:
71  *
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
86  *    above.
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.
95  *
96  * Locking: Caller must hold i_sem on the directory.
97  */
98 static struct dentry *ntfs_lookup(struct inode *dir_ino, struct dentry *dent,
99                 struct nameidata *nd)
100 {
101         ntfs_volume *vol = NTFS_SB(dir_ino->i_sb);
102         struct inode *dent_inode;
103         ntfschar *uname;
104         ntfs_name *name = NULL;
105         MFT_REF mref;
106         unsigned long dent_ino;
107         int uname_len;
108
109         ntfs_debug("Looking up %s in directory inode 0x%lx.",
110                         dent->d_name.name, dir_ino->i_ino);
111         /* Convert the name of the dentry to Unicode. */
112         uname_len = ntfs_nlstoucs(vol, dent->d_name.name, dent->d_name.len,
113                         &uname);
114         if (uname_len < 0) {
115                 ntfs_error(vol->sb, "Failed to convert name to Unicode.");
116                 return ERR_PTR(uname_len);
117         }
118         mref = ntfs_lookup_inode_by_name(NTFS_I(dir_ino), uname, uname_len,
119                         &name);
120         kmem_cache_free(ntfs_name_cache, uname);
121         if (!IS_ERR_MREF(mref)) {
122                 dent_ino = MREF(mref);
123                 ntfs_debug("Found inode 0x%lx. Calling ntfs_iget.", dent_ino);
124                 dent_inode = ntfs_iget(vol->sb, dent_ino);
125                 if (likely(!IS_ERR(dent_inode))) {
126                         /* Consistency check. */
127                         if (MSEQNO(mref) == NTFS_I(dent_inode)->seq_no ||
128                                         dent_ino == FILE_MFT) {
129                                 /* Perfect WIN32/POSIX match. -- Case 1. */
130                                 if (!name) {
131                                         ntfs_debug("Done.");
132                                         return d_splice_alias(dent_inode, dent);
133                                 }
134                                 /*
135                                  * We are too indented. Handle imperfect
136                                  * matches and short file names further below.
137                                  */
138                                 goto handle_name;
139                         }
140                         ntfs_error(vol->sb, "Found stale reference to inode "
141                                         "0x%lx (reference sequence number = "
142                                         "0x%x, inode sequence number = 0x%x), "
143                                         "returning -EIO. Run chkdsk.",
144                                         dent_ino, MSEQNO(mref),
145                                         NTFS_I(dent_inode)->seq_no);
146                         iput(dent_inode);
147                         dent_inode = ERR_PTR(-EIO);
148                 } else
149                         ntfs_error(vol->sb, "ntfs_iget(0x%lx) failed with "
150                                         "error code %li.", dent_ino,
151                                         PTR_ERR(dent_inode));
152                 if (name)
153                         kfree(name);
154                 /* Return the error code. */
155                 return (struct dentry *)dent_inode;
156         }
157         /* It is guaranteed that name is no longer allocated at this point. */
158         if (MREF_ERR(mref) == -ENOENT) {
159                 ntfs_debug("Entry was not found, adding negative dentry.");
160                 /* The dcache will handle negative entries. */
161                 d_add(dent, NULL);
162                 ntfs_debug("Done.");
163                 return NULL;
164         }
165         ntfs_error(vol->sb, "ntfs_lookup_ino_by_name() failed with error "
166                         "code %i.", -MREF_ERR(mref));
167         return ERR_PTR(MREF_ERR(mref));
168
169         // TODO: Consider moving this lot to a separate function! (AIA)
170 handle_name:
171    {
172         struct dentry *real_dent, *new_dent;
173         MFT_RECORD *m;
174         ntfs_attr_search_ctx *ctx;
175         ntfs_inode *ni = NTFS_I(dent_inode);
176         int err;
177         struct qstr nls_name;
178
179         nls_name.name = NULL;
180         if (name->type != FILE_NAME_DOS) {                      /* Case 2. */
181                 nls_name.len = (unsigned)ntfs_ucstonls(vol,
182                                 (ntfschar*)&name->name, name->len,
183                                 (unsigned char**)&nls_name.name, 0);
184                 kfree(name);
185         } else /* if (name->type == FILE_NAME_DOS) */ {         /* Case 3. */
186                 FILE_NAME_ATTR *fn;
187
188                 kfree(name);
189
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
208                         err = ntfs_attr_lookup(AT_FILE_NAME, NULL, 0, 0, 0,
209                                         NULL, 0, ctx);
210                         if (unlikely(err)) {
211                                 ntfs_error(vol->sb, "Inode corrupt: No WIN32 "
212                                                 "namespace counterpart to DOS "
213                                                 "file name. Run chkdsk.");
214                                 if (err == -ENOENT)
215                                         err = -EIO;
216                                 goto err_out;
217                         }
218                         /* Consistency checks. */
219                         a = ctx->attr;
220                         if (a->non_resident || a->flags)
221                                 goto eio_err_out;
222                         val_len = le32_to_cpu(a->data.resident.value_length);
223                         if (le16_to_cpu(a->data.resident.value_offset) +
224                                         val_len > le32_to_cpu(a->length))
225                                 goto eio_err_out;
226                         fn = (FILE_NAME_ATTR*)((u8*)ctx->attr + le16_to_cpu(
227                                         ctx->attr->data.resident.value_offset));
228                         if ((u32)(fn->file_name_length * sizeof(ntfschar) +
229                                         sizeof(FILE_NAME_ATTR)) > val_len)
230                                 goto eio_err_out;
231                 } while (fn->file_name_type != FILE_NAME_WIN32);
232
233                 /* Convert the found WIN32 name to current NLS code page. */
234                 nls_name.len = (unsigned)ntfs_ucstonls(vol,
235                                 (ntfschar*)&fn->file_name, fn->file_name_length,
236                                 (unsigned char**)&nls_name.name, 0);
237
238                 ntfs_attr_put_search_ctx(ctx);
239                 unmap_mft_record(ni);
240         }
241         m = NULL;
242         ctx = NULL;
243
244         /* Check if a conversion error occurred. */
245         if ((signed)nls_name.len < 0) {
246                 err = (signed)nls_name.len;
247                 goto err_out;
248         }
249         nls_name.hash = full_name_hash(nls_name.name, nls_name.len);
250
251         /*
252          * Note: No need for dent->d_lock lock as i_sem is held on the
253          * parent inode.
254          */
255
256         /* Does a dentry matching the nls_name exist already? */
257         real_dent = d_lookup(dent->d_parent, &nls_name);
258         /* If not, create it now. */
259         if (!real_dent) {
260                 real_dent = d_alloc(dent->d_parent, &nls_name);
261                 kfree(nls_name.name);
262                 if (!real_dent) {
263                         err = -ENOMEM;
264                         goto err_out;
265                 }
266                 new_dent = d_splice_alias(dent_inode, real_dent);
267                 if (new_dent)
268                         dput(real_dent);
269                 else
270                         new_dent = real_dent;
271                 return new_dent;
272         }
273         kfree(nls_name.name);
274         /* Matching dentry exists, check if it is negative. */
275         if (real_dent->d_inode) {
276                 BUG_ON(real_dent->d_inode != dent_inode);
277                 /*
278                  * Already have the inode and the dentry attached, decrement
279                  * the reference count to balance the ntfs_iget() we did
280                  * earlier on.  We found the dentry using d_lookup() so it
281                  * cannot be disconnected and thus we do not need to worry
282                  * about any NFS/disconnectedness issues here.
283                  */
284                 iput(dent_inode);
285                 return real_dent;
286         }
287         /*
288          * Negative dentry: instantiate it unless the inode is a directory and
289          * has a 'disconnected' dentry (i.e. IS_ROOT and DCACHE_DISCONNECTED),
290          * in which case d_move() that in place of the found dentry.
291          */
292         if (!S_ISDIR(dent_inode->i_mode)) {
293                 /* Not a directory; everything is easy. */
294                 d_instantiate(real_dent, dent_inode);
295                 return real_dent;
296         }
297         spin_lock(&dcache_lock);
298         if (list_empty(&dent_inode->i_dentry)) {
299                 /*
300                  * Directory without a 'disconnected' dentry; we need to do
301                  * d_instantiate() by hand because it takes dcache_lock which
302                  * we already hold.
303                  */
304                 list_add(&real_dent->d_alias, &dent_inode->i_dentry);
305                 real_dent->d_inode = dent_inode;
306                 spin_unlock(&dcache_lock);
307                 security_d_instantiate(real_dent, dent_inode);
308                 return real_dent;
309         }
310         /*
311          * Directory with a 'disconnected' dentry; get a reference to the
312          * 'disconnected' dentry.
313          */
314         new_dent = list_entry(dent_inode->i_dentry.next, struct dentry,
315                         d_alias);
316         dget_locked(new_dent);
317         spin_unlock(&dcache_lock);
318         /* Do security vodoo. */
319         security_d_instantiate(real_dent, dent_inode);
320         /* Move new_dent in place of real_dent. */
321         d_move(new_dent, real_dent);
322         /* Balance the ntfs_iget() we did above. */
323         iput(dent_inode);
324         /* Throw away real_dent. */
325         dput(real_dent);
326         /* Use new_dent as the actual dentry. */
327         return new_dent;
328
329 eio_err_out:
330         ntfs_error(vol->sb, "Illegal file name attribute. Run chkdsk.");
331         err = -EIO;
332 err_out:
333         if (ctx)
334                 ntfs_attr_put_search_ctx(ctx);
335         if (m)
336                 unmap_mft_record(ni);
337         iput(dent_inode);
338         return ERR_PTR(err);
339    }
340 }
341
342 /**
343  * Inode operations for directories.
344  */
345 struct inode_operations ntfs_dir_inode_ops = {
346         .lookup = ntfs_lookup,  /* VFS: Lookup directory. */
347 };
348
349 /**
350  * ntfs_get_parent - find the dentry of the parent of a given directory dentry
351  * @child_dent:         dentry of the directory whose parent directory to find
352  *
353  * Find the dentry for the parent directory of the directory specified by the
354  * dentry @child_dent.  This function is called from
355  * fs/exportfs/expfs.c::find_exported_dentry() which in turn is called from the
356  * default ->decode_fh() which is export_decode_fh() in the same file.
357  *
358  * The code is based on the ext3 ->get_parent() implementation found in
359  * fs/ext3/namei.c::ext3_get_parent().
360  *
361  * Note: ntfs_get_parent() is called with @child_dent->d_inode->i_sem down.
362  *
363  * Return the dentry of the parent directory on success or the error code on
364  * error (IS_ERR() is true).
365  */
366 struct dentry *ntfs_get_parent(struct dentry *child_dent)
367 {
368         struct inode *vi = child_dent->d_inode;
369         ntfs_inode *ni = NTFS_I(vi);
370         MFT_RECORD *mrec;
371         ntfs_attr_search_ctx *ctx;
372         ATTR_RECORD *attr;
373         FILE_NAME_ATTR *fn;
374         struct inode *parent_vi;
375         struct dentry *parent_dent;
376         unsigned long parent_ino;
377         int err;
378
379         ntfs_debug("Entering for inode 0x%lx.", vi->i_ino);
380         /* Get the mft record of the inode belonging to the child dentry. */
381         mrec = map_mft_record(ni);
382         if (IS_ERR(mrec))
383                 return (struct dentry *)mrec;
384         /* Find the first file name attribute in the mft record. */
385         ctx = ntfs_attr_get_search_ctx(ni, mrec);
386         if (unlikely(!ctx)) {
387                 unmap_mft_record(ni);
388                 return ERR_PTR(-ENOMEM);
389         }
390 try_next:
391         err = ntfs_attr_lookup(AT_FILE_NAME, NULL, 0, CASE_SENSITIVE, 0, NULL,
392                         0, ctx);
393         if (unlikely(err)) {
394                 ntfs_attr_put_search_ctx(ctx);
395                 unmap_mft_record(ni);
396                 if (err == -ENOENT)
397                         ntfs_error(vi->i_sb, "Inode 0x%lx does not have a "
398                                         "file name attribute.  Run chkdsk.",
399                                         vi->i_ino);
400                 return ERR_PTR(err);
401         }
402         attr = ctx->attr;
403         if (unlikely(attr->non_resident))
404                 goto try_next;
405         fn = (FILE_NAME_ATTR *)((u8 *)attr +
406                         le16_to_cpu(attr->data.resident.value_offset));
407         if (unlikely((u8 *)fn + le32_to_cpu(attr->data.resident.value_length) >
408                         (u8*)attr + le32_to_cpu(attr->length)))
409                 goto try_next;
410         /* Get the inode number of the parent directory. */
411         parent_ino = MREF_LE(fn->parent_directory);
412         /* Release the search context and the mft record of the child. */
413         ntfs_attr_put_search_ctx(ctx);
414         unmap_mft_record(ni);
415         /* Get the inode of the parent directory. */
416         parent_vi = ntfs_iget(vi->i_sb, parent_ino);
417         if (IS_ERR(parent_vi) || unlikely(is_bad_inode(parent_vi))) {
418                 if (!IS_ERR(parent_vi))
419                         iput(parent_vi);
420                 ntfs_error(vi->i_sb, "Failed to get parent directory inode "
421                                 "0x%lx of child inode 0x%lx.", parent_ino,
422                                 vi->i_ino);
423                 return ERR_PTR(-EACCES);
424         }
425         /* Finally get a dentry for the parent directory and return it. */
426         parent_dent = d_alloc_anon(parent_vi);
427         if (unlikely(!parent_dent)) {
428                 iput(parent_vi);
429                 return ERR_PTR(-ENOMEM);
430         }
431         ntfs_debug("Done for inode 0x%lx.", vi->i_ino);
432         return parent_dent;
433 }
434
435 /**
436  * ntfs_get_dentry - find a dentry for the inode from a file handle sub-fragment
437  * @sb:         super block identifying the mounted ntfs volume
438  * @fh:         the file handle sub-fragment
439  *
440  * Find a dentry for the inode given a file handle sub-fragment.  This function
441  * is called from fs/exportfs/expfs.c::find_exported_dentry() which in turn is
442  * called from the default ->decode_fh() which is export_decode_fh() in the
443  * same file.  The code is closely based on the default ->get_dentry() helper
444  * fs/exportfs/expfs.c::get_object().
445  *
446  * The @fh contains two 32-bit unsigned values, the first one is the inode
447  * number and the second one is the inode generation.
448  *
449  * Return the dentry on success or the error code on error (IS_ERR() is true).
450  */
451 struct dentry *ntfs_get_dentry(struct super_block *sb, void *fh)
452 {
453         struct inode *vi;
454         struct dentry *dent;
455         unsigned long ino = ((u32 *)fh)[0];
456         u32 gen = ((u32 *)fh)[1];
457
458         ntfs_debug("Entering for inode 0x%lx, generation 0x%x.", ino, gen);
459         vi = ntfs_iget(sb, ino);
460         if (IS_ERR(vi)) {
461                 ntfs_error(sb, "Failed to get inode 0x%lx.", ino);
462                 return (struct dentry *)vi;
463         }
464         if (unlikely(is_bad_inode(vi) || vi->i_generation != gen)) {
465                 /* We didn't find the right inode. */
466                 ntfs_error(sb, "Inode 0x%lx, bad count: %d %d or version 0x%x "
467                                 "0x%x.", vi->i_ino, vi->i_nlink,
468                                 atomic_read(&vi->i_count), vi->i_generation,
469                                 gen);
470                 iput(vi);
471                 return ERR_PTR(-ESTALE);
472         }
473         /* Now find a dentry.  If possible, get a well-connected one. */
474         dent = d_alloc_anon(vi);
475         if (unlikely(!dent)) {
476                 iput(vi);
477                 return ERR_PTR(-ENOMEM);
478         }
479         ntfs_debug("Done for inode 0x%lx, generation 0x%x.", ino, gen);
480         return dent;
481 }