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