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