90a0e3750e0af7bcba9918fbf61dd67854005031
[linux-2.6.git] / fs / namei.c
1 /*
2  *  linux/fs/namei.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  */
6
7 /*
8  * Some corrections by tytso.
9  */
10
11 /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
12  * lookup logic.
13  */
14 /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
15  */
16
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/fs.h>
21 #include <linux/namei.h>
22 #include <linux/quotaops.h>
23 #include <linux/pagemap.h>
24 #include <linux/fsnotify.h>
25 #include <linux/smp_lock.h>
26 #include <linux/personality.h>
27 #include <linux/security.h>
28 #include <linux/syscalls.h>
29 #include <linux/mount.h>
30 #include <linux/audit.h>
31 #include <linux/capability.h>
32 #include <linux/file.h>
33 #include <linux/fcntl.h>
34 #include <linux/namei.h>
35 #include <linux/proc_fs.h>
36 #include <linux/vserver/inode.h>
37 #include <linux/vserver/debug.h>
38 #include <asm/namei.h>
39 #include <asm/uaccess.h>
40
41 #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
42
43 /* [Feb-1997 T. Schoebel-Theuer]
44  * Fundamental changes in the pathname lookup mechanisms (namei)
45  * were necessary because of omirr.  The reason is that omirr needs
46  * to know the _real_ pathname, not the user-supplied one, in case
47  * of symlinks (and also when transname replacements occur).
48  *
49  * The new code replaces the old recursive symlink resolution with
50  * an iterative one (in case of non-nested symlink chains).  It does
51  * this with calls to <fs>_follow_link().
52  * As a side effect, dir_namei(), _namei() and follow_link() are now 
53  * replaced with a single function lookup_dentry() that can handle all 
54  * the special cases of the former code.
55  *
56  * With the new dcache, the pathname is stored at each inode, at least as
57  * long as the refcount of the inode is positive.  As a side effect, the
58  * size of the dcache depends on the inode cache and thus is dynamic.
59  *
60  * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
61  * resolution to correspond with current state of the code.
62  *
63  * Note that the symlink resolution is not *completely* iterative.
64  * There is still a significant amount of tail- and mid- recursion in
65  * the algorithm.  Also, note that <fs>_readlink() is not used in
66  * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
67  * may return different results than <fs>_follow_link().  Many virtual
68  * filesystems (including /proc) exhibit this behavior.
69  */
70
71 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
72  * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
73  * and the name already exists in form of a symlink, try to create the new
74  * name indicated by the symlink. The old code always complained that the
75  * name already exists, due to not following the symlink even if its target
76  * is nonexistent.  The new semantics affects also mknod() and link() when
77  * the name is a symlink pointing to a non-existant name.
78  *
79  * I don't know which semantics is the right one, since I have no access
80  * to standards. But I found by trial that HP-UX 9.0 has the full "new"
81  * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
82  * "old" one. Personally, I think the new semantics is much more logical.
83  * Note that "ln old new" where "new" is a symlink pointing to a non-existing
84  * file does succeed in both HP-UX and SunOs, but not in Solaris
85  * and in the old Linux semantics.
86  */
87
88 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
89  * semantics.  See the comments in "open_namei" and "do_link" below.
90  *
91  * [10-Sep-98 Alan Modra] Another symlink change.
92  */
93
94 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
95  *      inside the path - always follow.
96  *      in the last component in creation/removal/renaming - never follow.
97  *      if LOOKUP_FOLLOW passed - follow.
98  *      if the pathname has trailing slashes - follow.
99  *      otherwise - don't follow.
100  * (applied in that order).
101  *
102  * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
103  * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
104  * During the 2.4 we need to fix the userland stuff depending on it -
105  * hopefully we will be able to get rid of that wart in 2.5. So far only
106  * XEmacs seems to be relying on it...
107  */
108 /*
109  * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
110  * implemented.  Let's see if raised priority of ->s_vfs_rename_mutex gives
111  * any extra contention...
112  */
113
114 /* In order to reduce some races, while at the same time doing additional
115  * checking and hopefully speeding things up, we copy filenames to the
116  * kernel data space before using them..
117  *
118  * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
119  * PATH_MAX includes the nul terminator --RR.
120  */
121 static int do_getname(const char __user *filename, char *page)
122 {
123         int retval;
124         unsigned long len = PATH_MAX;
125
126         if (!segment_eq(get_fs(), KERNEL_DS)) {
127                 if ((unsigned long) filename >= TASK_SIZE)
128                         return -EFAULT;
129                 if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
130                         len = TASK_SIZE - (unsigned long) filename;
131         }
132
133         retval = strncpy_from_user(page, filename, len);
134         if (retval > 0) {
135                 if (retval < len)
136                         return 0;
137                 return -ENAMETOOLONG;
138         } else if (!retval)
139                 retval = -ENOENT;
140         return retval;
141 }
142
143 char * getname(const char __user * filename)
144 {
145         char *tmp, *result;
146
147         result = ERR_PTR(-ENOMEM);
148         tmp = __getname();
149         if (tmp)  {
150                 int retval = do_getname(filename, tmp);
151
152                 result = tmp;
153                 if (retval < 0) {
154                         __putname(tmp);
155                         result = ERR_PTR(retval);
156                 }
157         }
158         audit_getname(result);
159         return result;
160 }
161
162 #ifdef CONFIG_AUDITSYSCALL
163 void putname(const char *name)
164 {
165         if (unlikely(current->audit_context))
166                 audit_putname(name);
167         else
168                 __putname(name);
169 }
170 EXPORT_SYMBOL(putname);
171 #endif
172
173
174 /**
175  * generic_permission  -  check for access rights on a Posix-like filesystem
176  * @inode:      inode to check access rights for
177  * @mask:       right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
178  * @check_acl:  optional callback to check for Posix ACLs
179  *
180  * Used to check for read/write/execute permissions on a file.
181  * We use "fsuid" for this, letting us set arbitrary permissions
182  * for filesystem access without changing the "normal" uids which
183  * are used for other things..
184  */
185 int generic_permission(struct inode *inode, int mask,
186                 int (*check_acl)(struct inode *inode, int mask))
187 {
188         umode_t                 mode = inode->i_mode;
189
190         if (current->fsuid == inode->i_uid)
191                 mode >>= 6;
192         else {
193                 if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
194                         int error = check_acl(inode, mask);
195                         if (error == -EACCES)
196                                 goto check_capabilities;
197                         else if (error != -EAGAIN)
198                                 return error;
199                 }
200
201                 if (in_group_p(inode->i_gid))
202                         mode >>= 3;
203         }
204
205         /*
206          * If the DACs are ok we don't need any capability check.
207          */
208         if (((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask))
209                 return 0;
210
211  check_capabilities:
212         /*
213          * Read/write DACs are always overridable.
214          * Executable DACs are overridable if at least one exec bit is set.
215          */
216         if (!(mask & MAY_EXEC) ||
217             (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
218                 if (capable(CAP_DAC_OVERRIDE))
219                         return 0;
220
221         /*
222          * Searching includes executable on directories, else just read.
223          */
224         if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
225                 if (capable(CAP_DAC_READ_SEARCH))
226                         return 0;
227
228         return -EACCES;
229 }
230
231 static inline int xid_permission(struct inode *inode, int mask, struct nameidata *nd)
232 {
233         if (IS_BARRIER(inode) && !vx_check(0, VX_ADMIN)) {
234                 vxwprintk(1, "xid=%d did hit the barrier.",
235                         vx_current_xid());
236                 return -EACCES;
237         }
238         if (inode->i_xid == 0)
239                 return 0;
240         if (vx_check(inode->i_xid, VX_ADMIN|VX_WATCH|VX_IDENT))
241                 return 0;
242
243         vxwprintk(1, "xid=%d denied access to %p[#%d,%lu] »%s«.",
244                 vx_current_xid(), inode, inode->i_xid, inode->i_ino,
245                 vxd_cond_path(nd));
246         return -EACCES;
247 }
248
249 int permission(struct inode *inode, int mask, struct nameidata *nd)
250 {
251         int retval, submask;
252
253         if (mask & MAY_WRITE) {
254                 umode_t mode = inode->i_mode;
255
256                 /*
257                  * Nobody gets write access to a read-only fs.
258                  */
259                 if ((IS_RDONLY(inode) || (nd && MNT_IS_RDONLY(nd->mnt))) &&
260                     (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
261                         return -EROFS;
262
263                 /*
264                  * Nobody gets write access to an immutable file.
265                  */
266                 if (IS_IMMUTABLE(inode))
267                         return -EACCES;
268         }
269
270
271         /* Ordinary permission routines do not understand MAY_APPEND. */
272         submask = mask & ~MAY_APPEND;
273         if ((retval = xid_permission(inode, mask, nd)))
274                 return retval;
275         if (inode->i_op && inode->i_op->permission)
276                 retval = inode->i_op->permission(inode, submask, nd);
277         else
278                 retval = generic_permission(inode, submask, NULL);
279         if (retval)
280                 return retval;
281
282         return security_inode_permission(inode, mask, nd);
283 }
284
285 /**
286  * vfs_permission  -  check for access rights to a given path
287  * @nd:         lookup result that describes the path
288  * @mask:       right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
289  *
290  * Used to check for read/write/execute permissions on a path.
291  * We use "fsuid" for this, letting us set arbitrary permissions
292  * for filesystem access without changing the "normal" uids which
293  * are used for other things.
294  */
295 int vfs_permission(struct nameidata *nd, int mask)
296 {
297         return permission(nd->dentry->d_inode, mask, nd);
298 }
299
300 /**
301  * file_permission  -  check for additional access rights to a given file
302  * @file:       file to check access rights for
303  * @mask:       right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
304  *
305  * Used to check for read/write/execute permissions on an already opened
306  * file.
307  *
308  * Note:
309  *      Do not use this function in new code.  All access checks should
310  *      be done using vfs_permission().
311  */
312 int file_permission(struct file *file, int mask)
313 {
314         return permission(file->f_dentry->d_inode, mask, NULL);
315 }
316
317 /*
318  * get_write_access() gets write permission for a file.
319  * put_write_access() releases this write permission.
320  * This is used for regular files.
321  * We cannot support write (and maybe mmap read-write shared) accesses and
322  * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
323  * can have the following values:
324  * 0: no writers, no VM_DENYWRITE mappings
325  * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
326  * > 0: (i_writecount) users are writing to the file.
327  *
328  * Normally we operate on that counter with atomic_{inc,dec} and it's safe
329  * except for the cases where we don't hold i_writecount yet. Then we need to
330  * use {get,deny}_write_access() - these functions check the sign and refuse
331  * to do the change if sign is wrong. Exclusion between them is provided by
332  * the inode->i_lock spinlock.
333  */
334
335 int get_write_access(struct inode * inode)
336 {
337         spin_lock(&inode->i_lock);
338         if (atomic_read(&inode->i_writecount) < 0) {
339                 spin_unlock(&inode->i_lock);
340                 return -ETXTBSY;
341         }
342         atomic_inc(&inode->i_writecount);
343         spin_unlock(&inode->i_lock);
344
345         return 0;
346 }
347
348 int deny_write_access(struct file * file)
349 {
350         struct inode *inode = file->f_dentry->d_inode;
351
352         spin_lock(&inode->i_lock);
353         if (atomic_read(&inode->i_writecount) > 0) {
354                 spin_unlock(&inode->i_lock);
355                 return -ETXTBSY;
356         }
357         atomic_dec(&inode->i_writecount);
358         spin_unlock(&inode->i_lock);
359
360         return 0;
361 }
362
363 void path_release(struct nameidata *nd)
364 {
365         dput(nd->dentry);
366         mntput(nd->mnt);
367 }
368
369 /*
370  * umount() mustn't call path_release()/mntput() as that would clear
371  * mnt_expiry_mark
372  */
373 void path_release_on_umount(struct nameidata *nd)
374 {
375         dput(nd->dentry);
376         mntput_no_expire(nd->mnt);
377 }
378
379 /**
380  * release_open_intent - free up open intent resources
381  * @nd: pointer to nameidata
382  */
383 void release_open_intent(struct nameidata *nd)
384 {
385         if (nd->intent.open.file->f_dentry == NULL)
386                 put_filp(nd->intent.open.file);
387         else
388                 fput(nd->intent.open.file);
389 }
390
391 /*
392  * Internal lookup() using the new generic dcache.
393  * SMP-safe
394  */
395 static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
396 {
397         struct dentry * dentry = __d_lookup(parent, name);
398
399         /* lockess __d_lookup may fail due to concurrent d_move() 
400          * in some unrelated directory, so try with d_lookup
401          */
402         if (!dentry)
403                 dentry = d_lookup(parent, name);
404
405         if (dentry && dentry->d_op && dentry->d_op->d_revalidate) {
406                 if (!dentry->d_op->d_revalidate(dentry, nd) && !d_invalidate(dentry)) {
407                         dput(dentry);
408                         dentry = NULL;
409                 }
410         }
411         return dentry;
412 }
413
414 /*
415  * Short-cut version of permission(), for calling by
416  * path_walk(), when dcache lock is held.  Combines parts
417  * of permission() and generic_permission(), and tests ONLY for
418  * MAY_EXEC permission.
419  *
420  * If appropriate, check DAC only.  If not appropriate, or
421  * short-cut DAC fails, then call permission() to do more
422  * complete permission check.
423  */
424 static int exec_permission_lite(struct inode *inode,
425                                        struct nameidata *nd)
426 {
427         umode_t mode = inode->i_mode;
428
429         if (inode->i_op && inode->i_op->permission)
430                 return -EAGAIN;
431
432         if (current->fsuid == inode->i_uid)
433                 mode >>= 6;
434         else if (in_group_p(inode->i_gid))
435                 mode >>= 3;
436
437         if (mode & MAY_EXEC)
438                 goto ok;
439
440         if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE))
441                 goto ok;
442
443         if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_OVERRIDE))
444                 goto ok;
445
446         if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH))
447                 goto ok;
448
449         return -EACCES;
450 ok:
451         return security_inode_permission(inode, MAY_EXEC, nd);
452 }
453
454 /*
455  * This is called when everything else fails, and we actually have
456  * to go to the low-level filesystem to find out what we should do..
457  *
458  * We get the directory semaphore, and after getting that we also
459  * make sure that nobody added the entry to the dcache in the meantime..
460  * SMP-safe
461  */
462 static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
463 {
464         struct dentry * result;
465         struct inode *dir = parent->d_inode;
466
467         mutex_lock(&dir->i_mutex);
468         /*
469          * First re-do the cached lookup just in case it was created
470          * while we waited for the directory semaphore..
471          *
472          * FIXME! This could use version numbering or similar to
473          * avoid unnecessary cache lookups.
474          *
475          * The "dcache_lock" is purely to protect the RCU list walker
476          * from concurrent renames at this point (we mustn't get false
477          * negatives from the RCU list walk here, unlike the optimistic
478          * fast walk).
479          *
480          * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
481          */
482         result = d_lookup(parent, name);
483         if (!result) {
484                 struct dentry * dentry = d_alloc(parent, name);
485                 result = ERR_PTR(-ENOMEM);
486                 if (dentry) {
487                         result = dir->i_op->lookup(dir, dentry, nd);
488                         if (result)
489                                 dput(dentry);
490                         else
491                                 result = dentry;
492                 }
493                 mutex_unlock(&dir->i_mutex);
494                 return result;
495         }
496
497         /*
498          * Uhhuh! Nasty case: the cache was re-populated while
499          * we waited on the semaphore. Need to revalidate.
500          */
501         mutex_unlock(&dir->i_mutex);
502         if (result->d_op && result->d_op->d_revalidate) {
503                 if (!result->d_op->d_revalidate(result, nd) && !d_invalidate(result)) {
504                         dput(result);
505                         result = ERR_PTR(-ENOENT);
506                 }
507         }
508         return result;
509 }
510
511 static int __emul_lookup_dentry(const char *, struct nameidata *);
512
513 /* SMP-safe */
514 static __always_inline int
515 walk_init_root(const char *name, struct nameidata *nd)
516 {
517         read_lock(&current->fs->lock);
518         if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
519                 nd->mnt = mntget(current->fs->altrootmnt);
520                 nd->dentry = dget(current->fs->altroot);
521                 read_unlock(&current->fs->lock);
522                 if (__emul_lookup_dentry(name,nd))
523                         return 0;
524                 read_lock(&current->fs->lock);
525         }
526         nd->mnt = mntget(current->fs->rootmnt);
527         nd->dentry = dget(current->fs->root);
528         read_unlock(&current->fs->lock);
529         return 1;
530 }
531
532 static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
533 {
534         int res = 0;
535         char *name;
536         if (IS_ERR(link))
537                 goto fail;
538
539         if (*link == '/') {
540                 path_release(nd);
541                 if (!walk_init_root(link, nd))
542                         /* weird __emul_prefix() stuff did it */
543                         goto out;
544         }
545         res = link_path_walk(link, nd);
546 out:
547         if (nd->depth || res || nd->last_type!=LAST_NORM)
548                 return res;
549         /*
550          * If it is an iterative symlinks resolution in open_namei() we
551          * have to copy the last component. And all that crap because of
552          * bloody create() on broken symlinks. Furrfu...
553          */
554         name = __getname();
555         if (unlikely(!name)) {
556                 path_release(nd);
557                 return -ENOMEM;
558         }
559         strcpy(name, nd->last.name);
560         nd->last.name = name;
561         return 0;
562 fail:
563         path_release(nd);
564         return PTR_ERR(link);
565 }
566
567 struct path {
568         struct vfsmount *mnt;
569         struct dentry *dentry;
570 };
571
572 static inline void dput_path(struct path *path, struct nameidata *nd)
573 {
574         dput(path->dentry);
575         if (path->mnt != nd->mnt)
576                 mntput(path->mnt);
577 }
578
579 static inline void path_to_nameidata(struct path *path, struct nameidata *nd)
580 {
581         dput(nd->dentry);
582         if (nd->mnt != path->mnt)
583                 mntput(nd->mnt);
584         nd->mnt = path->mnt;
585         nd->dentry = path->dentry;
586 }
587
588 static __always_inline int __do_follow_link(struct path *path, struct nameidata *nd)
589 {
590         int error;
591         void *cookie;
592         struct dentry *dentry = path->dentry;
593
594         touch_atime(path->mnt, dentry);
595         nd_set_link(nd, NULL);
596
597         if (path->mnt != nd->mnt) {
598                 path_to_nameidata(path, nd);
599                 dget(dentry);
600         }
601         mntget(path->mnt);
602         cookie = dentry->d_inode->i_op->follow_link(dentry, nd);
603         error = PTR_ERR(cookie);
604         if (!IS_ERR(cookie)) {
605                 char *s = nd_get_link(nd);
606                 error = 0;
607                 if (s)
608                         error = __vfs_follow_link(nd, s);
609                 if (dentry->d_inode->i_op->put_link)
610                         dentry->d_inode->i_op->put_link(dentry, nd, cookie);
611         }
612         dput(dentry);
613         mntput(path->mnt);
614
615         return error;
616 }
617
618 /*
619  * This limits recursive symlink follows to 8, while
620  * limiting consecutive symlinks to 40.
621  *
622  * Without that kind of total limit, nasty chains of consecutive
623  * symlinks can cause almost arbitrarily long lookups. 
624  */
625 static inline int do_follow_link(struct path *path, struct nameidata *nd)
626 {
627         int err = -ELOOP;
628         if (current->link_count >= MAX_NESTED_LINKS)
629                 goto loop;
630         if (current->total_link_count >= 40)
631                 goto loop;
632         BUG_ON(nd->depth >= MAX_NESTED_LINKS);
633         cond_resched();
634         err = security_inode_follow_link(path->dentry, nd);
635         if (err)
636                 goto loop;
637         current->link_count++;
638         current->total_link_count++;
639         nd->depth++;
640         err = __do_follow_link(path, nd);
641         current->link_count--;
642         nd->depth--;
643         return err;
644 loop:
645         dput_path(path, nd);
646         path_release(nd);
647         return err;
648 }
649
650 int follow_up(struct vfsmount **mnt, struct dentry **dentry)
651 {
652         struct vfsmount *parent;
653         struct dentry *mountpoint;
654         spin_lock(&vfsmount_lock);
655         parent=(*mnt)->mnt_parent;
656         if (parent == *mnt) {
657                 spin_unlock(&vfsmount_lock);
658                 return 0;
659         }
660         mntget(parent);
661         mountpoint=dget((*mnt)->mnt_mountpoint);
662         spin_unlock(&vfsmount_lock);
663         dput(*dentry);
664         *dentry = mountpoint;
665         mntput(*mnt);
666         *mnt = parent;
667         return 1;
668 }
669
670 /* no need for dcache_lock, as serialization is taken care in
671  * namespace.c
672  */
673 static int __follow_mount(struct path *path)
674 {
675         int res = 0;
676         while (d_mountpoint(path->dentry)) {
677                 struct vfsmount *mounted = lookup_mnt(path->mnt, path->dentry);
678                 if (!mounted)
679                         break;
680                 dput(path->dentry);
681                 if (res)
682                         mntput(path->mnt);
683                 path->mnt = mounted;
684                 path->dentry = dget(mounted->mnt_root);
685                 res = 1;
686         }
687         return res;
688 }
689
690 static void follow_mount(struct vfsmount **mnt, struct dentry **dentry)
691 {
692         while (d_mountpoint(*dentry)) {
693                 struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
694                 if (!mounted)
695                         break;
696                 dput(*dentry);
697                 mntput(*mnt);
698                 *mnt = mounted;
699                 *dentry = dget(mounted->mnt_root);
700         }
701 }
702
703 /* no need for dcache_lock, as serialization is taken care in
704  * namespace.c
705  */
706 int follow_down(struct vfsmount **mnt, struct dentry **dentry)
707 {
708         struct vfsmount *mounted;
709
710         mounted = lookup_mnt(*mnt, *dentry);
711         if (mounted) {
712                 dput(*dentry);
713                 mntput(*mnt);
714                 *mnt = mounted;
715                 *dentry = dget(mounted->mnt_root);
716                 return 1;
717         }
718         return 0;
719 }
720
721 static __always_inline void follow_dotdot(struct nameidata *nd)
722 {
723         while(1) {
724                 struct vfsmount *parent;
725                 struct dentry *old = nd->dentry;
726
727                 read_lock(&current->fs->lock);
728                 if (nd->dentry == current->fs->root &&
729                     nd->mnt == current->fs->rootmnt) {
730                         read_unlock(&current->fs->lock);
731                         /* for sane '/' avoid follow_mount() */
732                         return;
733                 }
734                 read_unlock(&current->fs->lock);
735                 spin_lock(&dcache_lock);
736                 if (nd->dentry != nd->mnt->mnt_root) {
737                         nd->dentry = dget(nd->dentry->d_parent);
738                         spin_unlock(&dcache_lock);
739                         dput(old);
740                         break;
741                 }
742                 spin_unlock(&dcache_lock);
743                 spin_lock(&vfsmount_lock);
744                 parent = nd->mnt->mnt_parent;
745                 if (parent == nd->mnt) {
746                         spin_unlock(&vfsmount_lock);
747                         break;
748                 }
749                 mntget(parent);
750                 nd->dentry = dget(nd->mnt->mnt_mountpoint);
751                 spin_unlock(&vfsmount_lock);
752                 dput(old);
753                 mntput(nd->mnt);
754                 nd->mnt = parent;
755         }
756         follow_mount(&nd->mnt, &nd->dentry);
757 }
758
759 /*
760  *  It's more convoluted than I'd like it to be, but... it's still fairly
761  *  small and for now I'd prefer to have fast path as straight as possible.
762  *  It _is_ time-critical.
763  */
764 static int do_lookup(struct nameidata *nd, struct qstr *name,
765                      struct path *path, int atomic)
766 {
767         struct vfsmount *mnt = nd->mnt;
768         struct dentry *dentry = __d_lookup(nd->dentry, name);
769         struct inode *inode;
770
771         if (!dentry)
772                 goto need_lookup;
773         if (dentry->d_op && dentry->d_op->d_revalidate)
774                 goto need_revalidate;
775         inode = dentry->d_inode;
776         if (!inode)
777                 goto done;
778         if (!vx_check(inode->i_xid, VX_WATCH|VX_ADMIN|VX_HOSTID|VX_IDENT))
779                 goto hidden;
780         if (inode->i_sb->s_magic == PROC_SUPER_MAGIC) {
781                 struct proc_dir_entry *de = PDE(inode);
782
783                 if (de && !vx_hide_check(0, de->vx_flags))
784                         goto hidden;
785         }
786 done:
787         path->mnt = mnt;
788         path->dentry = dentry;
789         __follow_mount(path);
790         return 0;
791 hidden:
792         vxwprintk(1, "xid=%d did lookup hidden %p[#%d,%lu] »%s«.",
793                 vx_current_xid(), inode, inode->i_xid, inode->i_ino,
794                 vxd_path(dentry, mnt));
795         dput(dentry);
796         return -ENOENT;
797
798 need_lookup:
799         if (atomic)
800                 return -EWOULDBLOCKIO;
801         dentry = real_lookup(nd->dentry, name, nd);
802         if (IS_ERR(dentry))
803                 goto fail;
804         goto done;
805
806 need_revalidate:
807         if (atomic)
808                 return -EWOULDBLOCKIO;
809         if (dentry->d_op->d_revalidate(dentry, nd))
810                 goto done;
811         if (d_invalidate(dentry))
812                 goto done;
813         dput(dentry);
814         goto need_lookup;
815
816 fail:
817         return PTR_ERR(dentry);
818 }
819
820 /*
821  * Name resolution.
822  * This is the basic name resolution function, turning a pathname into
823  * the final dentry. We expect 'base' to be positive and a directory.
824  *
825  * Returns 0 and nd will have valid dentry and mnt on success.
826  * Returns error and drops reference to input namei data on failure.
827  */
828 static fastcall int __link_path_walk(const char * name, struct nameidata *nd)
829 {
830         struct path next;
831         struct inode *inode;
832         int err, atomic;
833         unsigned int lookup_flags = nd->flags;
834
835         atomic = (lookup_flags & LOOKUP_ATOMIC);
836
837         while (*name=='/')
838                 name++;
839         if (!*name)
840                 goto return_reval;
841
842         inode = nd->dentry->d_inode;
843         if (nd->depth)
844                 lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
845
846         /* At this point we know we have a real path component. */
847         for(;;) {
848                 unsigned long hash;
849                 struct qstr this;
850                 unsigned int c;
851
852                 nd->flags |= LOOKUP_CONTINUE;
853                 err = exec_permission_lite(inode, nd);
854                 if (err == -EAGAIN)
855                         err = vfs_permission(nd, MAY_EXEC);
856                 if (err)
857                         break;
858
859                 this.name = name;
860                 c = *(const unsigned char *)name;
861
862                 hash = init_name_hash();
863                 do {
864                         name++;
865                         hash = partial_name_hash(c, hash);
866                         c = *(const unsigned char *)name;
867                 } while (c && (c != '/'));
868                 this.len = name - (const char *) this.name;
869                 this.hash = end_name_hash(hash);
870
871                 /* remove trailing slashes? */
872                 if (!c)
873                         goto last_component;
874                 while (*++name == '/');
875                 if (!*name)
876                         goto last_with_slashes;
877
878                 /*
879                  * "." and ".." are special - ".." especially so because it has
880                  * to be able to know about the current root directory and
881                  * parent relationships.
882                  */
883                 if (this.name[0] == '.') switch (this.len) {
884                         default:
885                                 break;
886                         case 2: 
887                                 if (this.name[1] != '.')
888                                         break;
889                                 follow_dotdot(nd);
890                                 inode = nd->dentry->d_inode;
891                                 /* fallthrough */
892                         case 1:
893                                 continue;
894                 }
895                 /*
896                  * See if the low-level filesystem might want
897                  * to use its own hash..
898                  */
899                 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
900                         err = nd->dentry->d_op->d_hash(nd->dentry, &this);
901                         if (err < 0)
902                                 break;
903                 }
904                 /* This does the actual lookups.. */
905                 err = do_lookup(nd, &this, &next, atomic);
906                 if (err)
907                         break;
908
909                 err = -ENOENT;
910                 inode = next.dentry->d_inode;
911                 if (!inode)
912                         goto out_dput;
913                 err = -ENOTDIR; 
914                 if (!inode->i_op)
915                         goto out_dput;
916
917                 if (inode->i_op->follow_link) {
918                         err = do_follow_link(&next, nd);
919                         if (err)
920                                 goto return_err;
921                         err = -ENOENT;
922                         inode = nd->dentry->d_inode;
923                         if (!inode)
924                                 break;
925                         err = -ENOTDIR; 
926                         if (!inode->i_op)
927                                 break;
928                 } else
929                         path_to_nameidata(&next, nd);
930                 err = -ENOTDIR; 
931                 if (!inode->i_op->lookup)
932                         break;
933                 continue;
934                 /* here ends the main loop */
935
936 last_with_slashes:
937                 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
938 last_component:
939                 /* Clear LOOKUP_CONTINUE iff it was previously unset */
940                 nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
941                 if (lookup_flags & LOOKUP_PARENT)
942                         goto lookup_parent;
943                 if (this.name[0] == '.') switch (this.len) {
944                         default:
945                                 break;
946                         case 2: 
947                                 if (this.name[1] != '.')
948                                         break;
949                                 follow_dotdot(nd);
950                                 inode = nd->dentry->d_inode;
951                                 /* fallthrough */
952                         case 1:
953                                 goto return_reval;
954                 }
955                 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
956                         err = nd->dentry->d_op->d_hash(nd->dentry, &this);
957                         if (err < 0)
958                                 break;
959                 }
960                 err = do_lookup(nd, &this, &next, atomic);
961                 if (err)
962                         break;
963                 inode = next.dentry->d_inode;
964                 if ((lookup_flags & LOOKUP_FOLLOW)
965                     && inode && inode->i_op && inode->i_op->follow_link) {
966                         err = do_follow_link(&next, nd);
967                         if (err)
968                                 goto return_err;
969                         inode = nd->dentry->d_inode;
970                 } else
971                         path_to_nameidata(&next, nd);
972                 err = -ENOENT;
973                 if (!inode)
974                         break;
975                 if (lookup_flags & LOOKUP_DIRECTORY) {
976                         err = -ENOTDIR; 
977                         if (!inode->i_op || !inode->i_op->lookup)
978                                 break;
979                 }
980                 goto return_base;
981 lookup_parent:
982                 nd->last = this;
983                 nd->last_type = LAST_NORM;
984                 if (this.name[0] != '.')
985                         goto return_base;
986                 if (this.len == 1)
987                         nd->last_type = LAST_DOT;
988                 else if (this.len == 2 && this.name[1] == '.')
989                         nd->last_type = LAST_DOTDOT;
990                 else
991                         goto return_base;
992 return_reval:
993                 /*
994                  * We bypassed the ordinary revalidation routines.
995                  * We may need to check the cached dentry for staleness.
996                  */
997                 if (nd->dentry && nd->dentry->d_sb &&
998                     (nd->dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
999                         err = -ESTALE;
1000                         /* Note: we do not d_invalidate() */
1001                         if (!nd->dentry->d_op->d_revalidate(nd->dentry, nd))
1002                                 break;
1003                 }
1004 return_base:
1005                 return 0;
1006 out_dput:
1007                 dput_path(&next, nd);
1008                 break;
1009         }
1010         path_release(nd);
1011 return_err:
1012         return err;
1013 }
1014
1015 /*
1016  * Wrapper to retry pathname resolution whenever the underlying
1017  * file system returns an ESTALE.
1018  *
1019  * Retry the whole path once, forcing real lookup requests
1020  * instead of relying on the dcache.
1021  */
1022 int fastcall link_path_walk(const char *name, struct nameidata *nd)
1023 {
1024         struct nameidata save = *nd;
1025         int result;
1026
1027         /* make sure the stuff we saved doesn't go away */
1028         dget(save.dentry);
1029         mntget(save.mnt);
1030
1031         result = __link_path_walk(name, nd);
1032         if (result == -ESTALE) {
1033                 *nd = save;
1034                 dget(nd->dentry);
1035                 mntget(nd->mnt);
1036                 nd->flags |= LOOKUP_REVAL;
1037                 result = __link_path_walk(name, nd);
1038         }
1039
1040         dput(save.dentry);
1041         mntput(save.mnt);
1042
1043         return result;
1044 }
1045
1046 int fastcall path_walk(const char * name, struct nameidata *nd)
1047 {
1048         current->total_link_count = 0;
1049         return link_path_walk(name, nd);
1050 }
1051
1052 /* 
1053  * SMP-safe: Returns 1 and nd will have valid dentry and mnt, if
1054  * everything is done. Returns 0 and drops input nd, if lookup failed;
1055  */
1056 static int __emul_lookup_dentry(const char *name, struct nameidata *nd)
1057 {
1058         if (path_walk(name, nd))
1059                 return 0;               /* something went wrong... */
1060
1061         if (!nd->dentry->d_inode || S_ISDIR(nd->dentry->d_inode->i_mode)) {
1062                 struct dentry *old_dentry = nd->dentry;
1063                 struct vfsmount *old_mnt = nd->mnt;
1064                 struct qstr last = nd->last;
1065                 int last_type = nd->last_type;
1066                 /*
1067                  * NAME was not found in alternate root or it's a directory.  Try to find
1068                  * it in the normal root:
1069                  */
1070                 nd->last_type = LAST_ROOT;
1071                 read_lock(&current->fs->lock);
1072                 nd->mnt = mntget(current->fs->rootmnt);
1073                 nd->dentry = dget(current->fs->root);
1074                 read_unlock(&current->fs->lock);
1075                 if (path_walk(name, nd) == 0) {
1076                         if (nd->dentry->d_inode) {
1077                                 dput(old_dentry);
1078                                 mntput(old_mnt);
1079                                 return 1;
1080                         }
1081                         path_release(nd);
1082                 }
1083                 nd->dentry = old_dentry;
1084                 nd->mnt = old_mnt;
1085                 nd->last = last;
1086                 nd->last_type = last_type;
1087         }
1088         return 1;
1089 }
1090
1091 void set_fs_altroot(void)
1092 {
1093         char *emul = __emul_prefix();
1094         struct nameidata nd;
1095         struct vfsmount *mnt = NULL, *oldmnt;
1096         struct dentry *dentry = NULL, *olddentry;
1097         int err;
1098
1099         if (!emul)
1100                 goto set_it;
1101         err = path_lookup(emul, LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_NOALT, &nd);
1102         if (!err) {
1103                 mnt = nd.mnt;
1104                 dentry = nd.dentry;
1105         }
1106 set_it:
1107         write_lock(&current->fs->lock);
1108         oldmnt = current->fs->altrootmnt;
1109         olddentry = current->fs->altroot;
1110         current->fs->altrootmnt = mnt;
1111         current->fs->altroot = dentry;
1112         write_unlock(&current->fs->lock);
1113         if (olddentry) {
1114                 dput(olddentry);
1115                 mntput(oldmnt);
1116         }
1117 }
1118
1119 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1120 static int fastcall do_path_lookup(int dfd, const char *name,
1121                                 unsigned int flags, struct nameidata *nd)
1122 {
1123         int retval = 0;
1124         int fput_needed;
1125         struct file *file;
1126
1127         nd->last_type = LAST_ROOT; /* if there are only slashes... */
1128         nd->flags = flags;
1129         nd->depth = 0;
1130
1131         if (*name=='/') {
1132                 read_lock(&current->fs->lock);
1133                 if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
1134                         nd->mnt = mntget(current->fs->altrootmnt);
1135                         nd->dentry = dget(current->fs->altroot);
1136                         read_unlock(&current->fs->lock);
1137                         if (__emul_lookup_dentry(name,nd))
1138                                 goto out; /* found in altroot */
1139                         read_lock(&current->fs->lock);
1140                 }
1141                 nd->mnt = mntget(current->fs->rootmnt);
1142                 nd->dentry = dget(current->fs->root);
1143                 read_unlock(&current->fs->lock);
1144         } else if (dfd == AT_FDCWD) {
1145                 read_lock(&current->fs->lock);
1146                 nd->mnt = mntget(current->fs->pwdmnt);
1147                 nd->dentry = dget(current->fs->pwd);
1148                 read_unlock(&current->fs->lock);
1149         } else {
1150                 struct dentry *dentry;
1151
1152                 file = fget_light(dfd, &fput_needed);
1153                 retval = -EBADF;
1154                 if (!file)
1155                         goto out_fail;
1156
1157                 dentry = file->f_dentry;
1158
1159                 retval = -ENOTDIR;
1160                 if (!S_ISDIR(dentry->d_inode->i_mode))
1161                         goto fput_fail;
1162
1163                 retval = file_permission(file, MAY_EXEC);
1164                 if (retval)
1165                         goto fput_fail;
1166
1167                 nd->mnt = mntget(file->f_vfsmnt);
1168                 nd->dentry = dget(dentry);
1169
1170                 fput_light(file, fput_needed);
1171         }
1172         current->total_link_count = 0;
1173         retval = link_path_walk(name, nd);
1174 out:
1175         if (likely(retval == 0)) {
1176                 if (unlikely(current->audit_context && nd && nd->dentry &&
1177                                 nd->dentry->d_inode))
1178                 audit_inode(name, nd->dentry->d_inode, flags);
1179         }
1180 out_fail:
1181         return retval;
1182
1183 fput_fail:
1184         fput_light(file, fput_needed);
1185         goto out_fail;
1186 }
1187
1188 int fastcall path_lookup(const char *name, unsigned int flags,
1189                         struct nameidata *nd)
1190 {
1191         return do_path_lookup(AT_FDCWD, name, flags, nd);
1192 }
1193
1194 static int __path_lookup_intent_open(int dfd, const char *name,
1195                 unsigned int lookup_flags, struct nameidata *nd,
1196                 int open_flags, int create_mode)
1197 {
1198         struct file *filp = get_empty_filp();
1199         int err;
1200
1201         if (filp == NULL)
1202                 return -ENFILE;
1203         nd->intent.open.file = filp;
1204         nd->intent.open.flags = open_flags;
1205         nd->intent.open.create_mode = create_mode;
1206         err = do_path_lookup(dfd, name, lookup_flags|LOOKUP_OPEN, nd);
1207         if (IS_ERR(nd->intent.open.file)) {
1208                 if (err == 0) {
1209                         err = PTR_ERR(nd->intent.open.file);
1210                         path_release(nd);
1211                 }
1212         } else if (err != 0)
1213                 release_open_intent(nd);
1214         return err;
1215 }
1216
1217 /**
1218  * path_lookup_open - lookup a file path with open intent
1219  * @dfd: the directory to use as base, or AT_FDCWD
1220  * @name: pointer to file name
1221  * @lookup_flags: lookup intent flags
1222  * @nd: pointer to nameidata
1223  * @open_flags: open intent flags
1224  */
1225 int path_lookup_open(int dfd, const char *name, unsigned int lookup_flags,
1226                 struct nameidata *nd, int open_flags)
1227 {
1228         return __path_lookup_intent_open(dfd, name, lookup_flags, nd,
1229                         open_flags, 0);
1230 }
1231
1232 /**
1233  * path_lookup_create - lookup a file path with open + create intent
1234  * @dfd: the directory to use as base, or AT_FDCWD
1235  * @name: pointer to file name
1236  * @lookup_flags: lookup intent flags
1237  * @nd: pointer to nameidata
1238  * @open_flags: open intent flags
1239  * @create_mode: create intent flags
1240  */
1241 static int path_lookup_create(int dfd, const char *name,
1242                               unsigned int lookup_flags, struct nameidata *nd,
1243                               int open_flags, int create_mode)
1244 {
1245         return __path_lookup_intent_open(dfd, name, lookup_flags|LOOKUP_CREATE,
1246                         nd, open_flags, create_mode);
1247 }
1248
1249 int __user_path_lookup_open(const char __user *name, unsigned int lookup_flags,
1250                 struct nameidata *nd, int open_flags)
1251 {
1252         char *tmp = getname(name);
1253         int err = PTR_ERR(tmp);
1254
1255         if (!IS_ERR(tmp)) {
1256                 err = __path_lookup_intent_open(AT_FDCWD, tmp, lookup_flags, nd, open_flags, 0);
1257                 putname(tmp);
1258         }
1259         return err;
1260 }
1261
1262 /*
1263  * Restricted form of lookup. Doesn't follow links, single-component only,
1264  * needs parent already locked. Doesn't follow mounts.
1265  * SMP-safe.
1266  */
1267 static struct dentry * __lookup_hash(struct qstr *name, struct dentry * base, struct nameidata *nd)
1268 {
1269         struct dentry * dentry;
1270         struct inode *inode;
1271         int err;
1272
1273         inode = base->d_inode;
1274         err = permission(inode, MAY_EXEC, nd);
1275         dentry = ERR_PTR(err);
1276         if (err)
1277                 goto out;
1278
1279         /*
1280          * See if the low-level filesystem might want
1281          * to use its own hash..
1282          */
1283         if (base->d_op && base->d_op->d_hash) {
1284                 err = base->d_op->d_hash(base, name);
1285                 dentry = ERR_PTR(err);
1286                 if (err < 0)
1287                         goto out;
1288         }
1289
1290         dentry = cached_lookup(base, name, nd);
1291         if (!dentry) {
1292                 struct dentry *new = d_alloc(base, name);
1293                 dentry = ERR_PTR(-ENOMEM);
1294                 if (!new)
1295                         goto out;
1296                 dentry = inode->i_op->lookup(inode, new, nd);
1297                 if (!dentry)
1298                         dentry = new;
1299                 else
1300                         dput(new);
1301         }
1302 out:
1303         return dentry;
1304 }
1305
1306 static struct dentry *lookup_hash(struct nameidata *nd)
1307 {
1308         return __lookup_hash(&nd->last, nd->dentry, nd);
1309 }
1310
1311 /* SMP-safe */
1312 struct dentry * lookup_one_len(const char * name, struct dentry * base, int len)
1313 {
1314         unsigned long hash;
1315         struct qstr this;
1316         unsigned int c;
1317
1318         this.name = name;
1319         this.len = len;
1320         if (!len)
1321                 goto access;
1322
1323         hash = init_name_hash();
1324         while (len--) {
1325                 c = *(const unsigned char *)name++;
1326                 if (c == '/' || c == '\0')
1327                         goto access;
1328                 hash = partial_name_hash(c, hash);
1329         }
1330         this.hash = end_name_hash(hash);
1331
1332         return __lookup_hash(&this, base, NULL);
1333 access:
1334         return ERR_PTR(-EACCES);
1335 }
1336
1337 /*
1338  *      namei()
1339  *
1340  * is used by most simple commands to get the inode of a specified name.
1341  * Open, link etc use their own routines, but this is enough for things
1342  * like 'chmod' etc.
1343  *
1344  * namei exists in two versions: namei/lnamei. The only difference is
1345  * that namei follows links, while lnamei does not.
1346  * SMP-safe
1347  */
1348 int fastcall __user_walk_fd(int dfd, const char __user *name, unsigned flags,
1349                             struct nameidata *nd)
1350 {
1351         char *tmp = getname(name);
1352         int err = PTR_ERR(tmp);
1353
1354         if (!IS_ERR(tmp)) {
1355                 err = do_path_lookup(dfd, tmp, flags, nd);
1356                 putname(tmp);
1357         }
1358         return err;
1359 }
1360
1361 int fastcall __user_walk(const char __user *name, unsigned flags, struct nameidata *nd)
1362 {
1363         return __user_walk_fd(AT_FDCWD, name, flags, nd);
1364 }
1365
1366 /*
1367  * It's inline, so penalty for filesystems that don't use sticky bit is
1368  * minimal.
1369  */
1370 static inline int check_sticky(struct inode *dir, struct inode *inode)
1371 {
1372         if (!(dir->i_mode & S_ISVTX))
1373                 return 0;
1374         if (inode->i_uid == current->fsuid)
1375                 return 0;
1376         if (dir->i_uid == current->fsuid)
1377                 return 0;
1378         return !capable(CAP_FOWNER);
1379 }
1380
1381 /*
1382  *      Check whether we can remove a link victim from directory dir, check
1383  *  whether the type of victim is right.
1384  *  1. We can't do it if dir is read-only (done in permission())
1385  *  2. We should have write and exec permissions on dir
1386  *  3. We can't remove anything from append-only dir
1387  *  4. We can't do anything with immutable dir (done in permission())
1388  *  5. If the sticky bit on dir is set we should either
1389  *      a. be owner of dir, or
1390  *      b. be owner of victim, or
1391  *      c. have CAP_FOWNER capability
1392  *  6. If the victim is append-only or immutable we can't do antyhing with
1393  *     links pointing to it.
1394  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1395  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1396  *  9. We can't remove a root or mountpoint.
1397  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1398  *     nfs_async_unlink().
1399  */
1400 static int may_delete(struct inode *dir, struct dentry *victim,
1401         int isdir, struct nameidata *nd)
1402 {
1403         int error;
1404
1405         if (!victim->d_inode)
1406                 return -ENOENT;
1407
1408         BUG_ON(victim->d_parent->d_inode != dir);
1409         audit_inode_child(victim->d_name.name, victim->d_inode, dir->i_ino);
1410
1411         error = permission(dir,MAY_WRITE | MAY_EXEC, nd);
1412         if (error)
1413                 return error;
1414         if (IS_APPEND(dir))
1415                 return -EPERM;
1416         if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1417                 IS_IXORUNLINK(victim->d_inode))
1418                 return -EPERM;
1419         if (isdir) {
1420                 if (!S_ISDIR(victim->d_inode->i_mode))
1421                         return -ENOTDIR;
1422                 if (IS_ROOT(victim))
1423                         return -EBUSY;
1424         } else if (S_ISDIR(victim->d_inode->i_mode))
1425                 return -EISDIR;
1426         if (IS_DEADDIR(dir))
1427                 return -ENOENT;
1428         if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1429                 return -EBUSY;
1430         return 0;
1431 }
1432
1433 /*      Check whether we can create an object with dentry child in directory
1434  *  dir.
1435  *  1. We can't do it if child already exists (open has special treatment for
1436  *     this case, but since we are inlined it's OK)
1437  *  2. We can't do it if dir is read-only (done in permission())
1438  *  3. We should have write and exec permissions on dir
1439  *  4. We can't do it if dir is immutable (done in permission())
1440  */
1441 static inline int may_create(struct inode *dir, struct dentry *child,
1442                              struct nameidata *nd)
1443 {
1444         if (child->d_inode)
1445                 return -EEXIST;
1446         if (IS_DEADDIR(dir))
1447                 return -ENOENT;
1448         return permission(dir,MAY_WRITE | MAY_EXEC, nd);
1449 }
1450
1451 /* 
1452  * O_DIRECTORY translates into forcing a directory lookup.
1453  */
1454 static inline int lookup_flags(unsigned int f)
1455 {
1456         unsigned long retval = LOOKUP_FOLLOW;
1457
1458         if (f & O_NOFOLLOW)
1459                 retval &= ~LOOKUP_FOLLOW;
1460         
1461         if (f & O_DIRECTORY)
1462                 retval |= LOOKUP_DIRECTORY;
1463         if (f & O_ATOMICLOOKUP)
1464                 retval |= LOOKUP_ATOMIC;
1465
1466         return retval;
1467 }
1468
1469 /*
1470  * p1 and p2 should be directories on the same fs.
1471  */
1472 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1473 {
1474         struct dentry *p;
1475
1476         if (p1 == p2) {
1477                 mutex_lock(&p1->d_inode->i_mutex);
1478                 return NULL;
1479         }
1480
1481         mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
1482
1483         for (p = p1; p->d_parent != p; p = p->d_parent) {
1484                 if (p->d_parent == p2) {
1485                         mutex_lock(&p2->d_inode->i_mutex);
1486                         mutex_lock(&p1->d_inode->i_mutex);
1487                         return p;
1488                 }
1489         }
1490
1491         for (p = p2; p->d_parent != p; p = p->d_parent) {
1492                 if (p->d_parent == p1) {
1493                         mutex_lock(&p1->d_inode->i_mutex);
1494                         mutex_lock(&p2->d_inode->i_mutex);
1495                         return p;
1496                 }
1497         }
1498
1499         mutex_lock(&p1->d_inode->i_mutex);
1500         mutex_lock(&p2->d_inode->i_mutex);
1501         return NULL;
1502 }
1503
1504 void unlock_rename(struct dentry *p1, struct dentry *p2)
1505 {
1506         mutex_unlock(&p1->d_inode->i_mutex);
1507         if (p1 != p2) {
1508                 mutex_unlock(&p2->d_inode->i_mutex);
1509                 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
1510         }
1511 }
1512
1513 int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1514                 struct nameidata *nd)
1515 {
1516         int error = may_create(dir, dentry, nd);
1517
1518         if (error)
1519                 return error;
1520
1521         if (!dir->i_op || !dir->i_op->create)
1522                 return -EACCES; /* shouldn't it be ENOSYS? */
1523         mode &= S_IALLUGO;
1524         mode |= S_IFREG;
1525         error = security_inode_create(dir, dentry, mode);
1526         if (error)
1527                 return error;
1528         DQUOT_INIT(dir);
1529         error = dir->i_op->create(dir, dentry, mode, nd);
1530         if (!error)
1531                 fsnotify_create(dir, dentry);
1532         return error;
1533 }
1534
1535 int may_open(struct nameidata *nd, int acc_mode, int flag)
1536 {
1537         struct dentry *dentry = nd->dentry;
1538         struct inode *inode = dentry->d_inode;
1539         int error;
1540
1541         if (!inode)
1542                 return -ENOENT;
1543
1544         if (S_ISLNK(inode->i_mode))
1545                 return -ELOOP;
1546         
1547         if (S_ISDIR(inode->i_mode) && (flag & FMODE_WRITE))
1548                 return -EISDIR;
1549
1550         error = vfs_permission(nd, acc_mode);
1551         if (error)
1552                 return error;
1553
1554         /*
1555          * FIFO's, sockets and device files are special: they don't
1556          * actually live on the filesystem itself, and as such you
1557          * can write to them even if the filesystem is read-only.
1558          */
1559         if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
1560                 flag &= ~O_TRUNC;
1561         } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
1562                 if (nd->mnt->mnt_flags & MNT_NODEV)
1563                         return -EACCES;
1564
1565                 flag &= ~O_TRUNC;
1566         } else if ((IS_RDONLY(inode) || MNT_IS_RDONLY(nd->mnt))
1567                 && (flag & FMODE_WRITE))
1568                 return -EROFS;
1569         /*
1570          * An append-only file must be opened in append mode for writing.
1571          */
1572         if (IS_APPEND(inode)) {
1573                 if  ((flag & FMODE_WRITE) && !(flag & O_APPEND))
1574                         return -EPERM;
1575                 if (flag & O_TRUNC)
1576                         return -EPERM;
1577         }
1578
1579         /* O_NOATIME can only be set by the owner or superuser */
1580         if (flag & O_NOATIME)
1581                 if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
1582                         return -EPERM;
1583
1584         /*
1585          * Ensure there are no outstanding leases on the file.
1586          */
1587         error = break_lease(inode, flag);
1588         if (error)
1589                 return error;
1590
1591         if (flag & O_TRUNC) {
1592                 error = get_write_access(inode);
1593                 if (error)
1594                         return error;
1595
1596                 /*
1597                  * Refuse to truncate files with mandatory locks held on them.
1598                  */
1599                 error = locks_verify_locked(inode);
1600                 if (!error) {
1601                         DQUOT_INIT(inode);
1602                         
1603                         error = do_truncate(dentry, 0, ATTR_MTIME|ATTR_CTIME, NULL);
1604                 }
1605                 put_write_access(inode);
1606                 if (error)
1607                         return error;
1608         } else
1609                 if (flag & FMODE_WRITE)
1610                         DQUOT_INIT(inode);
1611
1612         return 0;
1613 }
1614
1615 /*
1616  *      open_namei()
1617  *
1618  * namei for open - this is in fact almost the whole open-routine.
1619  *
1620  * Note that the low bits of "flag" aren't the same as in the open
1621  * system call - they are 00 - no permissions needed
1622  *                        01 - read permission needed
1623  *                        10 - write permission needed
1624  *                        11 - read/write permissions needed
1625  * which is a lot more logical, and also allows the "no perm" needed
1626  * for symlinks (where the permissions are checked later).
1627  * SMP-safe
1628  */
1629 int open_namei(int dfd, const char *pathname, int flag,
1630                 int mode, struct nameidata *nd)
1631 {
1632         int acc_mode, error;
1633         struct path path;
1634         struct dentry *dir;
1635         int count = 0;
1636
1637         acc_mode = ACC_MODE(flag);
1638
1639         /* O_TRUNC implies we need access checks for write permissions */
1640         if (flag & O_TRUNC)
1641                 acc_mode |= MAY_WRITE;
1642
1643         /* Allow the LSM permission hook to distinguish append 
1644            access from general write access. */
1645         if (flag & O_APPEND)
1646                 acc_mode |= MAY_APPEND;
1647
1648         /*
1649          * The simplest case - just a plain lookup.
1650          */
1651         if (!(flag & O_CREAT)) {
1652                 error = path_lookup_open(dfd, pathname, lookup_flags(flag),
1653                                          nd, flag);
1654                 if (error)
1655                         return error;
1656                 goto ok;
1657         }
1658
1659         /*
1660          * Create - we need to know the parent.
1661          */
1662         error = path_lookup_create(dfd,pathname,LOOKUP_PARENT,nd,flag,mode);
1663         if (error)
1664                 return error;
1665
1666         /*
1667          * We have the parent and last component. First of all, check
1668          * that we are not asked to creat(2) an obvious directory - that
1669          * will not do.
1670          */
1671         error = -EISDIR;
1672         if (nd->last_type != LAST_NORM || nd->last.name[nd->last.len])
1673                 goto exit;
1674
1675         dir = nd->dentry;
1676         nd->flags &= ~LOOKUP_PARENT;
1677         mutex_lock(&dir->d_inode->i_mutex);
1678         path.dentry = lookup_hash(nd);
1679         path.mnt = nd->mnt;
1680
1681 do_last:
1682         error = PTR_ERR(path.dentry);
1683         if (IS_ERR(path.dentry)) {
1684                 mutex_unlock(&dir->d_inode->i_mutex);
1685                 goto exit;
1686         }
1687
1688         if (IS_ERR(nd->intent.open.file)) {
1689                 mutex_unlock(&dir->d_inode->i_mutex);
1690                 error = PTR_ERR(nd->intent.open.file);
1691                 goto exit_dput;
1692         }
1693
1694         /* Negative dentry, just create the file */
1695         if (!path.dentry->d_inode) {
1696                 if (!IS_POSIXACL(dir->d_inode))
1697                         mode &= ~current->fs->umask;
1698                 error = vfs_create(dir->d_inode, path.dentry, mode, nd);
1699                 mutex_unlock(&dir->d_inode->i_mutex);
1700                 dput(nd->dentry);
1701                 nd->dentry = path.dentry;
1702                 if (error)
1703                         goto exit;
1704                 /* Don't check for write permission, don't truncate */
1705                 acc_mode = 0;
1706                 flag &= ~O_TRUNC;
1707                 goto ok;
1708         }
1709
1710         /*
1711          * It already exists.
1712          */
1713         mutex_unlock(&dir->d_inode->i_mutex);
1714
1715         error = -EEXIST;
1716         if (flag & O_EXCL)
1717                 goto exit_dput;
1718
1719         if (__follow_mount(&path)) {
1720                 error = -ELOOP;
1721                 if (flag & O_NOFOLLOW)
1722                         goto exit_dput;
1723         }
1724         error = -ENOENT;
1725         if (!path.dentry->d_inode)
1726                 goto exit_dput;
1727         if (path.dentry->d_inode->i_op && path.dentry->d_inode->i_op->follow_link)
1728                 goto do_link;
1729
1730         path_to_nameidata(&path, nd);
1731         error = -EISDIR;
1732         if (path.dentry->d_inode && S_ISDIR(path.dentry->d_inode->i_mode))
1733                 goto exit;
1734 ok:
1735         error = may_open(nd, acc_mode, flag);
1736         if (error)
1737                 goto exit;
1738         return 0;
1739
1740 exit_dput:
1741         dput_path(&path, nd);
1742 exit:
1743         if (!IS_ERR(nd->intent.open.file))
1744                 release_open_intent(nd);
1745         path_release(nd);
1746         return error;
1747
1748 do_link:
1749         error = -ELOOP;
1750         if (flag & O_NOFOLLOW)
1751                 goto exit_dput;
1752         /*
1753          * This is subtle. Instead of calling do_follow_link() we do the
1754          * thing by hands. The reason is that this way we have zero link_count
1755          * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1756          * After that we have the parent and last component, i.e.
1757          * we are in the same situation as after the first path_walk().
1758          * Well, almost - if the last component is normal we get its copy
1759          * stored in nd->last.name and we will have to putname() it when we
1760          * are done. Procfs-like symlinks just set LAST_BIND.
1761          */
1762         nd->flags |= LOOKUP_PARENT;
1763         error = security_inode_follow_link(path.dentry, nd);
1764         if (error)
1765                 goto exit_dput;
1766         error = __do_follow_link(&path, nd);
1767         if (error)
1768                 return error;
1769         nd->flags &= ~LOOKUP_PARENT;
1770         if (nd->last_type == LAST_BIND)
1771                 goto ok;
1772         error = -EISDIR;
1773         if (nd->last_type != LAST_NORM)
1774                 goto exit;
1775         if (nd->last.name[nd->last.len]) {
1776                 __putname(nd->last.name);
1777                 goto exit;
1778         }
1779         error = -ELOOP;
1780         if (count++==32) {
1781                 __putname(nd->last.name);
1782                 goto exit;
1783         }
1784         dir = nd->dentry;
1785         mutex_lock(&dir->d_inode->i_mutex);
1786         path.dentry = lookup_hash(nd);
1787         path.mnt = nd->mnt;
1788         __putname(nd->last.name);
1789         goto do_last;
1790 }
1791
1792 /**
1793  * lookup_create - lookup a dentry, creating it if it doesn't exist
1794  * @nd: nameidata info
1795  * @is_dir: directory flag
1796  *
1797  * Simple function to lookup and return a dentry and create it
1798  * if it doesn't exist.  Is SMP-safe.
1799  *
1800  * Returns with nd->dentry->d_inode->i_mutex locked.
1801  */
1802 struct dentry *lookup_create(struct nameidata *nd, int is_dir)
1803 {
1804         struct dentry *dentry = ERR_PTR(-EEXIST);
1805
1806         mutex_lock(&nd->dentry->d_inode->i_mutex);
1807         /*
1808          * Yucky last component or no last component at all?
1809          * (foo/., foo/.., /////)
1810          */
1811         if (nd->last_type != LAST_NORM)
1812                 goto fail;
1813         nd->flags &= ~LOOKUP_PARENT;
1814
1815         /*
1816          * Do the final lookup.
1817          */
1818         dentry = lookup_hash(nd);
1819         if (IS_ERR(dentry))
1820                 goto fail;
1821
1822         /*
1823          * Special case - lookup gave negative, but... we had foo/bar/
1824          * From the vfs_mknod() POV we just have a negative dentry -
1825          * all is fine. Let's be bastards - you had / on the end, you've
1826          * been asking for (non-existent) directory. -ENOENT for you.
1827          */
1828         if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode)
1829                 goto enoent;
1830         return dentry;
1831 enoent:
1832         dput(dentry);
1833         dentry = ERR_PTR(-ENOENT);
1834 fail:
1835         return dentry;
1836 }
1837 EXPORT_SYMBOL_GPL(lookup_create);
1838
1839 int vfs_mknod(struct inode *dir, struct dentry *dentry,
1840         int mode, dev_t dev, struct nameidata *nd)
1841 {
1842         int error = may_create(dir, dentry, nd);
1843
1844         if (error)
1845                 return error;
1846
1847         if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1848                 return -EPERM;
1849
1850         if (!dir->i_op || !dir->i_op->mknod)
1851                 return -EPERM;
1852
1853         error = security_inode_mknod(dir, dentry, mode, dev);
1854         if (error)
1855                 return error;
1856
1857         DQUOT_INIT(dir);
1858         error = dir->i_op->mknod(dir, dentry, mode, dev);
1859         if (!error)
1860                 fsnotify_create(dir, dentry);
1861         return error;
1862 }
1863
1864 asmlinkage long sys_mknodat(int dfd, const char __user *filename, int mode,
1865                                 unsigned dev)
1866 {
1867         int error = 0;
1868         char * tmp;
1869         struct dentry * dentry;
1870         struct nameidata nd;
1871
1872         if (S_ISDIR(mode))
1873                 return -EPERM;
1874         tmp = getname(filename);
1875         if (IS_ERR(tmp))
1876                 return PTR_ERR(tmp);
1877
1878         error = do_path_lookup(dfd, tmp, LOOKUP_PARENT, &nd);
1879         if (error)
1880                 goto out;
1881         dentry = lookup_create(&nd, 0);
1882         error = PTR_ERR(dentry);
1883
1884         if (!IS_POSIXACL(nd.dentry->d_inode))
1885                 mode &= ~current->fs->umask;
1886         if (!IS_ERR(dentry)) {
1887                 switch (mode & S_IFMT) {
1888                 case 0: case S_IFREG:
1889                         error = vfs_create(nd.dentry->d_inode,dentry,mode,&nd);
1890                         break;
1891                 case S_IFCHR: case S_IFBLK:
1892                         error = vfs_mknod(nd.dentry->d_inode, dentry, mode,
1893                                         new_decode_dev(dev), &nd);
1894                         break;
1895                 case S_IFIFO: case S_IFSOCK:
1896                         error = vfs_mknod(nd.dentry->d_inode, dentry, mode,
1897                                         0, &nd);
1898                         break;
1899                 case S_IFDIR:
1900                         error = -EPERM;
1901                         break;
1902                 default:
1903                         error = -EINVAL;
1904                 }
1905                 dput(dentry);
1906         }
1907         mutex_unlock(&nd.dentry->d_inode->i_mutex);
1908         path_release(&nd);
1909 out:
1910         putname(tmp);
1911
1912         return error;
1913 }
1914
1915 asmlinkage long sys_mknod(const char __user *filename, int mode, unsigned dev)
1916 {
1917         return sys_mknodat(AT_FDCWD, filename, mode, dev);
1918 }
1919
1920 int vfs_mkdir(struct inode *dir, struct dentry *dentry,
1921         int mode, struct nameidata *nd)
1922 {
1923         int error = may_create(dir, dentry, nd);
1924
1925         if (error)
1926                 return error;
1927
1928         if (!dir->i_op || !dir->i_op->mkdir)
1929                 return -EPERM;
1930
1931         mode &= (S_IRWXUGO|S_ISVTX);
1932         error = security_inode_mkdir(dir, dentry, mode);
1933         if (error)
1934                 return error;
1935
1936         DQUOT_INIT(dir);
1937         error = dir->i_op->mkdir(dir, dentry, mode);
1938         if (!error)
1939                 fsnotify_mkdir(dir, dentry);
1940         return error;
1941 }
1942
1943 asmlinkage long sys_mkdirat(int dfd, const char __user *pathname, int mode)
1944 {
1945         int error = 0;
1946         char * tmp;
1947
1948         tmp = getname(pathname);
1949         error = PTR_ERR(tmp);
1950         if (!IS_ERR(tmp)) {
1951                 struct dentry *dentry;
1952                 struct nameidata nd;
1953
1954                 error = do_path_lookup(dfd, tmp, LOOKUP_PARENT, &nd);
1955                 if (error)
1956                         goto out;
1957                 dentry = lookup_create(&nd, 1);
1958                 error = PTR_ERR(dentry);
1959                 if (!IS_ERR(dentry)) {
1960                         if (!IS_POSIXACL(nd.dentry->d_inode))
1961                                 mode &= ~current->fs->umask;
1962                         error = vfs_mkdir(nd.dentry->d_inode, dentry,
1963                                 mode, &nd);
1964                         dput(dentry);
1965                 }
1966                 mutex_unlock(&nd.dentry->d_inode->i_mutex);
1967                 path_release(&nd);
1968 out:
1969                 putname(tmp);
1970         }
1971
1972         return error;
1973 }
1974
1975 asmlinkage long sys_mkdir(const char __user *pathname, int mode)
1976 {
1977         return sys_mkdirat(AT_FDCWD, pathname, mode);
1978 }
1979
1980 /*
1981  * We try to drop the dentry early: we should have
1982  * a usage count of 2 if we're the only user of this
1983  * dentry, and if that is true (possibly after pruning
1984  * the dcache), then we drop the dentry now.
1985  *
1986  * A low-level filesystem can, if it choses, legally
1987  * do a
1988  *
1989  *      if (!d_unhashed(dentry))
1990  *              return -EBUSY;
1991  *
1992  * if it cannot handle the case of removing a directory
1993  * that is still in use by something else..
1994  */
1995 void dentry_unhash(struct dentry *dentry)
1996 {
1997         dget(dentry);
1998         if (atomic_read(&dentry->d_count))
1999                 shrink_dcache_parent(dentry);
2000         spin_lock(&dcache_lock);
2001         spin_lock(&dentry->d_lock);
2002         if (atomic_read(&dentry->d_count) == 2)
2003                 __d_drop(dentry);
2004         spin_unlock(&dentry->d_lock);
2005         spin_unlock(&dcache_lock);
2006 }
2007
2008 int vfs_rmdir(struct inode *dir, struct dentry *dentry,
2009         struct nameidata *nd)
2010 {
2011         int error = may_delete(dir, dentry, 1, nd);
2012
2013         if (error)
2014                 return error;
2015
2016         if (!dir->i_op || !dir->i_op->rmdir)
2017                 return -EPERM;
2018
2019         DQUOT_INIT(dir);
2020
2021         mutex_lock(&dentry->d_inode->i_mutex);
2022         dentry_unhash(dentry);
2023         if (d_mountpoint(dentry))
2024                 error = -EBUSY;
2025         else {
2026                 error = security_inode_rmdir(dir, dentry);
2027                 if (!error) {
2028                         error = dir->i_op->rmdir(dir, dentry);
2029                         if (!error)
2030                                 dentry->d_inode->i_flags |= S_DEAD;
2031                 }
2032         }
2033         mutex_unlock(&dentry->d_inode->i_mutex);
2034         if (!error) {
2035                 d_delete(dentry);
2036         }
2037         dput(dentry);
2038
2039         return error;
2040 }
2041
2042 static long do_rmdir(int dfd, const char __user *pathname)
2043 {
2044         int error = 0;
2045         char * name;
2046         struct dentry *dentry;
2047         struct nameidata nd;
2048
2049         name = getname(pathname);
2050         if(IS_ERR(name))
2051                 return PTR_ERR(name);
2052
2053         error = do_path_lookup(dfd, name, LOOKUP_PARENT, &nd);
2054         if (error)
2055                 goto exit;
2056
2057         switch(nd.last_type) {
2058                 case LAST_DOTDOT:
2059                         error = -ENOTEMPTY;
2060                         goto exit1;
2061                 case LAST_DOT:
2062                         error = -EINVAL;
2063                         goto exit1;
2064                 case LAST_ROOT:
2065                         error = -EBUSY;
2066                         goto exit1;
2067         }
2068         mutex_lock(&nd.dentry->d_inode->i_mutex);
2069         dentry = lookup_hash(&nd);
2070         error = PTR_ERR(dentry);
2071         if (!IS_ERR(dentry)) {
2072                 error = vfs_rmdir(nd.dentry->d_inode, dentry, &nd);
2073                 dput(dentry);
2074         }
2075         mutex_unlock(&nd.dentry->d_inode->i_mutex);
2076 exit1:
2077         path_release(&nd);
2078 exit:
2079         putname(name);
2080         return error;
2081 }
2082
2083 asmlinkage long sys_rmdir(const char __user *pathname)
2084 {
2085         return do_rmdir(AT_FDCWD, pathname);
2086 }
2087
2088 int vfs_unlink(struct inode *dir, struct dentry *dentry,
2089         struct nameidata *nd)
2090 {
2091         int error = may_delete(dir, dentry, 0, nd);
2092
2093         if (error)
2094                 return error;
2095
2096         if (!dir->i_op || !dir->i_op->unlink)
2097                 return -EPERM;
2098
2099         DQUOT_INIT(dir);
2100
2101         mutex_lock(&dentry->d_inode->i_mutex);
2102         if (d_mountpoint(dentry))
2103                 error = -EBUSY;
2104         else {
2105                 error = security_inode_unlink(dir, dentry);
2106                 if (!error)
2107                         error = dir->i_op->unlink(dir, dentry);
2108         }
2109         mutex_unlock(&dentry->d_inode->i_mutex);
2110
2111         /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2112         if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2113                 d_delete(dentry);
2114         }
2115
2116         return error;
2117 }
2118
2119 /*
2120  * Make sure that the actual truncation of the file will occur outside its
2121  * directory's i_mutex.  Truncate can take a long time if there is a lot of
2122  * writeout happening, and we don't want to prevent access to the directory
2123  * while waiting on the I/O.
2124  */
2125 static long do_unlinkat(int dfd, const char __user *pathname)
2126 {
2127         int error = 0;
2128         char * name;
2129         struct dentry *dentry;
2130         struct nameidata nd;
2131         struct inode *inode = NULL;
2132
2133         name = getname(pathname);
2134         if(IS_ERR(name))
2135                 return PTR_ERR(name);
2136
2137         error = do_path_lookup(dfd, name, LOOKUP_PARENT, &nd);
2138         if (error)
2139                 goto exit;
2140         error = -EISDIR;
2141         if (nd.last_type != LAST_NORM)
2142                 goto exit1;
2143         mutex_lock(&nd.dentry->d_inode->i_mutex);
2144         dentry = lookup_hash(&nd);
2145         error = PTR_ERR(dentry);
2146         if (!IS_ERR(dentry)) {
2147                 /* Why not before? Because we want correct error value */
2148                 if (nd.last.name[nd.last.len])
2149                         goto slashes;
2150                 inode = dentry->d_inode;
2151                 if (inode)
2152                         atomic_inc(&inode->i_count);
2153                 error = vfs_unlink(nd.dentry->d_inode, dentry, &nd);
2154         exit2:
2155                 dput(dentry);
2156         }
2157         mutex_unlock(&nd.dentry->d_inode->i_mutex);
2158         if (inode)
2159                 iput(inode);    /* truncate the inode here */
2160 exit1:
2161         path_release(&nd);
2162 exit:
2163         putname(name);
2164         return error;
2165
2166 slashes:
2167         error = !dentry->d_inode ? -ENOENT :
2168                 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2169         goto exit2;
2170 }
2171
2172 asmlinkage long sys_unlinkat(int dfd, const char __user *pathname, int flag)
2173 {
2174         if ((flag & ~AT_REMOVEDIR) != 0)
2175                 return -EINVAL;
2176
2177         if (flag & AT_REMOVEDIR)
2178                 return do_rmdir(dfd, pathname);
2179
2180         return do_unlinkat(dfd, pathname);
2181 }
2182
2183 asmlinkage long sys_unlink(const char __user *pathname)
2184 {
2185         return do_unlinkat(AT_FDCWD, pathname);
2186 }
2187
2188 int vfs_symlink(struct inode *dir, struct dentry *dentry,
2189         const char *oldname, int mode, struct nameidata *nd)
2190 {
2191         int error = may_create(dir, dentry, nd);
2192
2193         if (error)
2194                 return error;
2195
2196         if (!dir->i_op || !dir->i_op->symlink)
2197                 return -EPERM;
2198
2199         error = security_inode_symlink(dir, dentry, oldname);
2200         if (error)
2201                 return error;
2202
2203         DQUOT_INIT(dir);
2204         error = dir->i_op->symlink(dir, dentry, oldname);
2205         if (!error)
2206                 fsnotify_create(dir, dentry);
2207         return error;
2208 }
2209
2210 asmlinkage long sys_symlinkat(const char __user *oldname,
2211                               int newdfd, const char __user *newname)
2212 {
2213         int error = 0;
2214         char * from;
2215         char * to;
2216
2217         from = getname(oldname);
2218         if(IS_ERR(from))
2219                 return PTR_ERR(from);
2220         to = getname(newname);
2221         error = PTR_ERR(to);
2222         if (!IS_ERR(to)) {
2223                 struct dentry *dentry;
2224                 struct nameidata nd;
2225
2226                 error = do_path_lookup(newdfd, to, LOOKUP_PARENT, &nd);
2227                 if (error)
2228                         goto out;
2229                 dentry = lookup_create(&nd, 0);
2230                 error = PTR_ERR(dentry);
2231                 if (!IS_ERR(dentry)) {
2232                         error = vfs_symlink(nd.dentry->d_inode, dentry,
2233                                 from, S_IALLUGO, &nd);
2234                         dput(dentry);
2235                 }
2236                 mutex_unlock(&nd.dentry->d_inode->i_mutex);
2237                 path_release(&nd);
2238 out:
2239                 putname(to);
2240         }
2241         putname(from);
2242         return error;
2243 }
2244
2245 asmlinkage long sys_symlink(const char __user *oldname, const char __user *newname)
2246 {
2247         return sys_symlinkat(oldname, AT_FDCWD, newname);
2248 }
2249
2250 int vfs_link(struct dentry *old_dentry, struct inode *dir,
2251         struct dentry *new_dentry, struct nameidata *nd)
2252 {
2253         struct inode *inode = old_dentry->d_inode;
2254         int error;
2255
2256         if (!inode)
2257                 return -ENOENT;
2258
2259         error = may_create(dir, new_dentry, nd);
2260         if (error)
2261                 return error;
2262
2263         if (dir->i_sb != inode->i_sb)
2264                 return -EXDEV;
2265
2266         /*
2267          * A link to an append-only or immutable file cannot be created.
2268          */
2269         if (IS_APPEND(inode) || IS_IXORUNLINK(inode))
2270                 return -EPERM;
2271         if (!dir->i_op || !dir->i_op->link)
2272                 return -EPERM;
2273         if (S_ISDIR(old_dentry->d_inode->i_mode))
2274                 return -EPERM;
2275
2276         error = security_inode_link(old_dentry, dir, new_dentry);
2277         if (error)
2278                 return error;
2279
2280         mutex_lock(&old_dentry->d_inode->i_mutex);
2281         DQUOT_INIT(dir);
2282         error = dir->i_op->link(old_dentry, dir, new_dentry);
2283         mutex_unlock(&old_dentry->d_inode->i_mutex);
2284         if (!error)
2285                 fsnotify_create(dir, new_dentry);
2286         return error;
2287 }
2288
2289 /*
2290  * Hardlinks are often used in delicate situations.  We avoid
2291  * security-related surprises by not following symlinks on the
2292  * newname.  --KAB
2293  *
2294  * We don't follow them on the oldname either to be compatible
2295  * with linux 2.0, and to avoid hard-linking to directories
2296  * and other special files.  --ADM
2297  */
2298 asmlinkage long sys_linkat(int olddfd, const char __user *oldname,
2299                            int newdfd, const char __user *newname,
2300                            int flags)
2301 {
2302         struct dentry *new_dentry;
2303         struct nameidata nd, old_nd;
2304         int error;
2305         char * to;
2306
2307         if (flags != 0)
2308                 return -EINVAL;
2309
2310         to = getname(newname);
2311         if (IS_ERR(to))
2312                 return PTR_ERR(to);
2313
2314         error = __user_walk_fd(olddfd, oldname, 0, &old_nd);
2315         if (error)
2316                 goto exit;
2317         error = do_path_lookup(newdfd, to, LOOKUP_PARENT, &nd);
2318         if (error)
2319                 goto out;
2320         error = -EXDEV;
2321         if (old_nd.mnt != nd.mnt)
2322                 goto out_release;
2323         new_dentry = lookup_create(&nd, 0);
2324         error = PTR_ERR(new_dentry);
2325         if (!IS_ERR(new_dentry)) {
2326                 error = vfs_link(old_nd.dentry, nd.dentry->d_inode,
2327                         new_dentry, &nd);
2328                 dput(new_dentry);
2329         }
2330         mutex_unlock(&nd.dentry->d_inode->i_mutex);
2331 out_release:
2332         path_release(&nd);
2333 out:
2334         path_release(&old_nd);
2335 exit:
2336         putname(to);
2337
2338         return error;
2339 }
2340
2341 asmlinkage long sys_link(const char __user *oldname, const char __user *newname)
2342 {
2343         return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
2344 }
2345
2346 /*
2347  * The worst of all namespace operations - renaming directory. "Perverted"
2348  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2349  * Problems:
2350  *      a) we can get into loop creation. Check is done in is_subdir().
2351  *      b) race potential - two innocent renames can create a loop together.
2352  *         That's where 4.4 screws up. Current fix: serialization on
2353  *         sb->s_vfs_rename_mutex. We might be more accurate, but that's another
2354  *         story.
2355  *      c) we have to lock _three_ objects - parents and victim (if it exists).
2356  *         And that - after we got ->i_mutex on parents (until then we don't know
2357  *         whether the target exists).  Solution: try to be smart with locking
2358  *         order for inodes.  We rely on the fact that tree topology may change
2359  *         only under ->s_vfs_rename_mutex _and_ that parent of the object we
2360  *         move will be locked.  Thus we can rank directories by the tree
2361  *         (ancestors first) and rank all non-directories after them.
2362  *         That works since everybody except rename does "lock parent, lookup,
2363  *         lock child" and rename is under ->s_vfs_rename_mutex.
2364  *         HOWEVER, it relies on the assumption that any object with ->lookup()
2365  *         has no more than 1 dentry.  If "hybrid" objects will ever appear,
2366  *         we'd better make sure that there's no link(2) for them.
2367  *      d) some filesystems don't support opened-but-unlinked directories,
2368  *         either because of layout or because they are not ready to deal with
2369  *         all cases correctly. The latter will be fixed (taking this sort of
2370  *         stuff into VFS), but the former is not going away. Solution: the same
2371  *         trick as in rmdir().
2372  *      e) conversion from fhandle to dentry may come in the wrong moment - when
2373  *         we are removing the target. Solution: we will have to grab ->i_mutex
2374  *         in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
2375  *         ->i_mutex on parents, which works but leads to some truely excessive
2376  *         locking].
2377  */
2378 static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
2379                           struct inode *new_dir, struct dentry *new_dentry)
2380 {
2381         int error = 0;
2382         struct inode *target;
2383
2384         /*
2385          * If we are going to change the parent - check write permissions,
2386          * we'll need to flip '..'.
2387          */
2388         if (new_dir != old_dir) {
2389                 error = permission(old_dentry->d_inode, MAY_WRITE, NULL);
2390                 if (error)
2391                         return error;
2392         }
2393
2394         error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2395         if (error)
2396                 return error;
2397
2398         target = new_dentry->d_inode;
2399         if (target) {
2400                 mutex_lock(&target->i_mutex);
2401                 dentry_unhash(new_dentry);
2402         }
2403         if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2404                 error = -EBUSY;
2405         else 
2406                 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2407         if (target) {
2408                 if (!error)
2409                         target->i_flags |= S_DEAD;
2410                 mutex_unlock(&target->i_mutex);
2411                 if (d_unhashed(new_dentry))
2412                         d_rehash(new_dentry);
2413                 dput(new_dentry);
2414         }
2415         if (!error)
2416                 d_move(old_dentry,new_dentry);
2417         return error;
2418 }
2419
2420 static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
2421                             struct inode *new_dir, struct dentry *new_dentry)
2422 {
2423         struct inode *target;
2424         int error;
2425
2426         error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2427         if (error)
2428                 return error;
2429
2430         dget(new_dentry);
2431         target = new_dentry->d_inode;
2432         if (target)
2433                 mutex_lock(&target->i_mutex);
2434         if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2435                 error = -EBUSY;
2436         else
2437                 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2438         if (!error) {
2439                 /* The following d_move() should become unconditional */
2440                 if (!(old_dir->i_sb->s_type->fs_flags & FS_ODD_RENAME))
2441                         d_move(old_dentry, new_dentry);
2442         }
2443         if (target)
2444                 mutex_unlock(&target->i_mutex);
2445         dput(new_dentry);
2446         return error;
2447 }
2448
2449 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
2450                struct inode *new_dir, struct dentry *new_dentry)
2451 {
2452         int error;
2453         int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
2454         const char *old_name;
2455
2456         if (old_dentry->d_inode == new_dentry->d_inode)
2457                 return 0;
2458  
2459         error = may_delete(old_dir, old_dentry, is_dir, NULL);
2460         if (error)
2461                 return error;
2462
2463         if (!new_dentry->d_inode)
2464                 error = may_create(new_dir, new_dentry, NULL);
2465         else
2466                 error = may_delete(new_dir, new_dentry, is_dir, NULL);
2467         if (error)
2468                 return error;
2469
2470         if (!old_dir->i_op || !old_dir->i_op->rename)
2471                 return -EPERM;
2472
2473         DQUOT_INIT(old_dir);
2474         DQUOT_INIT(new_dir);
2475
2476         old_name = fsnotify_oldname_init(old_dentry->d_name.name);
2477
2478         if (is_dir)
2479                 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
2480         else
2481                 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
2482         if (!error) {
2483                 const char *new_name = old_dentry->d_name.name;
2484                 fsnotify_move(old_dir, new_dir, old_name, new_name, is_dir,
2485                               new_dentry->d_inode, old_dentry->d_inode);
2486         }
2487         fsnotify_oldname_free(old_name);
2488
2489         return error;
2490 }
2491
2492 static int do_rename(int olddfd, const char *oldname,
2493                         int newdfd, const char *newname)
2494 {
2495         int error = 0;
2496         struct dentry * old_dir, * new_dir;
2497         struct dentry * old_dentry, *new_dentry;
2498         struct dentry * trap;
2499         struct nameidata oldnd, newnd;
2500
2501         error = do_path_lookup(olddfd, oldname, LOOKUP_PARENT, &oldnd);
2502         if (error)
2503                 goto exit;
2504
2505         error = do_path_lookup(newdfd, newname, LOOKUP_PARENT, &newnd);
2506         if (error)
2507                 goto exit1;
2508
2509         error = -EXDEV;
2510         if (oldnd.mnt != newnd.mnt)
2511                 goto exit2;
2512
2513         old_dir = oldnd.dentry;
2514         error = -EBUSY;
2515         if (oldnd.last_type != LAST_NORM)
2516                 goto exit2;
2517
2518         new_dir = newnd.dentry;
2519         if (newnd.last_type != LAST_NORM)
2520                 goto exit2;
2521
2522         trap = lock_rename(new_dir, old_dir);
2523
2524         old_dentry = lookup_hash(&oldnd);
2525         error = PTR_ERR(old_dentry);
2526         if (IS_ERR(old_dentry))
2527                 goto exit3;
2528         /* source must exist */
2529         error = -ENOENT;
2530         if (!old_dentry->d_inode)
2531                 goto exit4;
2532         /* unless the source is a directory trailing slashes give -ENOTDIR */
2533         if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
2534                 error = -ENOTDIR;
2535                 if (oldnd.last.name[oldnd.last.len])
2536                         goto exit4;
2537                 if (newnd.last.name[newnd.last.len])
2538                         goto exit4;
2539         }
2540         /* source should not be ancestor of target */
2541         error = -EINVAL;
2542         if (old_dentry == trap)
2543                 goto exit4;
2544         error = -EROFS;
2545         if (MNT_IS_RDONLY(newnd.mnt))
2546                 goto exit4;
2547         new_dentry = lookup_hash(&newnd);
2548         error = PTR_ERR(new_dentry);
2549         if (IS_ERR(new_dentry))
2550                 goto exit4;
2551         /* target should not be an ancestor of source */
2552         error = -ENOTEMPTY;
2553         if (new_dentry == trap)
2554                 goto exit5;
2555
2556         error = vfs_rename(old_dir->d_inode, old_dentry,
2557                                    new_dir->d_inode, new_dentry);
2558 exit5:
2559         dput(new_dentry);
2560 exit4:
2561         dput(old_dentry);
2562 exit3:
2563         unlock_rename(new_dir, old_dir);
2564 exit2:
2565         path_release(&newnd);
2566 exit1:
2567         path_release(&oldnd);
2568 exit:
2569         return error;
2570 }
2571
2572 asmlinkage long sys_renameat(int olddfd, const char __user *oldname,
2573                              int newdfd, const char __user *newname)
2574 {
2575         int error;
2576         char * from;
2577         char * to;
2578
2579         from = getname(oldname);
2580         if(IS_ERR(from))
2581                 return PTR_ERR(from);
2582         to = getname(newname);
2583         error = PTR_ERR(to);
2584         if (!IS_ERR(to)) {
2585                 error = do_rename(olddfd, from, newdfd, to);
2586                 putname(to);
2587         }
2588         putname(from);
2589         return error;
2590 }
2591
2592 asmlinkage long sys_rename(const char __user *oldname, const char __user *newname)
2593 {
2594         return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
2595 }
2596
2597 int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
2598 {
2599         int len;
2600
2601         len = PTR_ERR(link);
2602         if (IS_ERR(link))
2603                 goto out;
2604
2605         len = strlen(link);
2606         if (len > (unsigned) buflen)
2607                 len = buflen;
2608         if (copy_to_user(buffer, link, len))
2609                 len = -EFAULT;
2610 out:
2611         return len;
2612 }
2613
2614 /*
2615  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
2616  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
2617  * using) it for any given inode is up to filesystem.
2618  */
2619 int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2620 {
2621         struct nameidata nd;
2622         void *cookie;
2623
2624         nd.depth = 0;
2625         cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
2626         if (!IS_ERR(cookie)) {
2627                 int res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
2628                 if (dentry->d_inode->i_op->put_link)
2629                         dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
2630                 cookie = ERR_PTR(res);
2631         }
2632         return PTR_ERR(cookie);
2633 }
2634
2635 int vfs_follow_link(struct nameidata *nd, const char *link)
2636 {
2637         return __vfs_follow_link(nd, link);
2638 }
2639
2640
2641 /* get the link contents into pagecache */
2642 static char *page_getlink(struct dentry * dentry, struct page **ppage)
2643 {
2644         struct page * page;
2645         struct address_space *mapping = dentry->d_inode->i_mapping;
2646         page = read_cache_page(mapping, 0, (filler_t *)mapping->a_ops->readpage,
2647                                 NULL);
2648         if (IS_ERR(page))
2649                 goto sync_fail;
2650         wait_on_page_locked(page);
2651         if (!PageUptodate(page))
2652                 goto async_fail;
2653         *ppage = page;
2654         return kmap(page);
2655
2656 async_fail:
2657         page_cache_release(page);
2658         return ERR_PTR(-EIO);
2659
2660 sync_fail:
2661         return (char*)page;
2662 }
2663
2664 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2665 {
2666         struct page *page = NULL;
2667         char *s = page_getlink(dentry, &page);
2668         int res = vfs_readlink(dentry,buffer,buflen,s);
2669         if (page) {
2670                 kunmap(page);
2671                 page_cache_release(page);
2672         }
2673         return res;
2674 }
2675
2676 void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
2677 {
2678         struct page *page = NULL;
2679         nd_set_link(nd, page_getlink(dentry, &page));
2680         return page;
2681 }
2682
2683 void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
2684 {
2685         struct page *page = cookie;
2686
2687         if (page) {
2688                 kunmap(page);
2689                 page_cache_release(page);
2690         }
2691 }
2692
2693 int __page_symlink(struct inode *inode, const char *symname, int len,
2694                 gfp_t gfp_mask)
2695 {
2696         struct address_space *mapping = inode->i_mapping;
2697         struct page *page;
2698         int err = -ENOMEM;
2699         char *kaddr;
2700
2701 retry:
2702         page = find_or_create_page(mapping, 0, gfp_mask);
2703         if (!page)
2704                 goto fail;
2705         err = mapping->a_ops->prepare_write(NULL, page, 0, len-1);
2706         if (err == AOP_TRUNCATED_PAGE) {
2707                 page_cache_release(page);
2708                 goto retry;
2709         }
2710         if (err)
2711                 goto fail_map;
2712         kaddr = kmap_atomic(page, KM_USER0);
2713         memcpy(kaddr, symname, len-1);
2714         kunmap_atomic(kaddr, KM_USER0);
2715         err = mapping->a_ops->commit_write(NULL, page, 0, len-1);
2716         if (err == AOP_TRUNCATED_PAGE) {
2717                 page_cache_release(page);
2718                 goto retry;
2719         }
2720         if (err)
2721                 goto fail_map;
2722         /*
2723          * Notice that we are _not_ going to block here - end of page is
2724          * unmapped, so this will only try to map the rest of page, see
2725          * that it is unmapped (typically even will not look into inode -
2726          * ->i_size will be enough for everything) and zero it out.
2727          * OTOH it's obviously correct and should make the page up-to-date.
2728          */
2729         if (!PageUptodate(page)) {
2730                 err = mapping->a_ops->readpage(NULL, page);
2731                 if (err != AOP_TRUNCATED_PAGE)
2732                         wait_on_page_locked(page);
2733         } else {
2734                 unlock_page(page);
2735         }
2736         page_cache_release(page);
2737         if (err < 0)
2738                 goto fail;
2739         mark_inode_dirty(inode);
2740         return 0;
2741 fail_map:
2742         unlock_page(page);
2743         page_cache_release(page);
2744 fail:
2745         return err;
2746 }
2747
2748 int page_symlink(struct inode *inode, const char *symname, int len)
2749 {
2750         return __page_symlink(inode, symname, len,
2751                         mapping_gfp_mask(inode->i_mapping));
2752 }
2753
2754 struct inode_operations page_symlink_inode_operations = {
2755         .readlink       = generic_readlink,
2756         .follow_link    = page_follow_link_light,
2757         .put_link       = page_put_link,
2758 };
2759
2760 EXPORT_SYMBOL(__user_walk);
2761 EXPORT_SYMBOL(__user_walk_fd);
2762 EXPORT_SYMBOL(follow_down);
2763 EXPORT_SYMBOL(follow_up);
2764 EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
2765 EXPORT_SYMBOL(getname);
2766 EXPORT_SYMBOL(lock_rename);
2767 EXPORT_SYMBOL(lookup_one_len);
2768 EXPORT_SYMBOL(page_follow_link_light);
2769 EXPORT_SYMBOL(page_put_link);
2770 EXPORT_SYMBOL(page_readlink);
2771 EXPORT_SYMBOL(__page_symlink);
2772 EXPORT_SYMBOL(page_symlink);
2773 EXPORT_SYMBOL(page_symlink_inode_operations);
2774 EXPORT_SYMBOL(path_lookup);
2775 EXPORT_SYMBOL(path_release);
2776 EXPORT_SYMBOL(path_walk);
2777 EXPORT_SYMBOL(permission);
2778 EXPORT_SYMBOL(vfs_permission);
2779 EXPORT_SYMBOL(file_permission);
2780 EXPORT_SYMBOL(unlock_rename);
2781 EXPORT_SYMBOL(vfs_create);
2782 EXPORT_SYMBOL(vfs_follow_link);
2783 EXPORT_SYMBOL(vfs_link);
2784 EXPORT_SYMBOL(vfs_mkdir);
2785 EXPORT_SYMBOL(vfs_mknod);
2786 EXPORT_SYMBOL(generic_permission);
2787 EXPORT_SYMBOL(vfs_readlink);
2788 EXPORT_SYMBOL(vfs_rename);
2789 EXPORT_SYMBOL(vfs_rmdir);
2790 EXPORT_SYMBOL(vfs_symlink);
2791 EXPORT_SYMBOL(vfs_unlink);
2792 EXPORT_SYMBOL(dentry_unhash);
2793 EXPORT_SYMBOL(generic_readlink);