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