[PATCH] namei fixes (17/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         if (path->mnt == nd->mnt)
510                 mntget(path->mnt);
511         error = dentry->d_inode->i_op->follow_link(dentry, nd);
512         if (!error) {
513                 char *s = nd_get_link(nd);
514                 if (s)
515                         error = __vfs_follow_link(nd, s);
516                 if (dentry->d_inode->i_op->put_link)
517                         dentry->d_inode->i_op->put_link(dentry, nd);
518         }
519         dput(dentry);
520         mntput(path->mnt);
521
522         return error;
523 }
524
525 /*
526  * This limits recursive symlink follows to 8, while
527  * limiting consecutive symlinks to 40.
528  *
529  * Without that kind of total limit, nasty chains of consecutive
530  * symlinks can cause almost arbitrarily long lookups. 
531  */
532 static inline int do_follow_link(struct path *path, struct nameidata *nd)
533 {
534         int err = -ELOOP;
535         if (current->link_count >= MAX_NESTED_LINKS)
536                 goto loop;
537         if (current->total_link_count >= 40)
538                 goto loop;
539         BUG_ON(nd->depth >= MAX_NESTED_LINKS);
540         cond_resched();
541         err = security_inode_follow_link(path->dentry, nd);
542         if (err)
543                 goto loop;
544         current->link_count++;
545         current->total_link_count++;
546         nd->depth++;
547         err = __do_follow_link(path, nd);
548         current->link_count--;
549         nd->depth--;
550         return err;
551 loop:
552         dput(path->dentry);
553         if (path->mnt != nd->mnt)
554                 mntput(path->mnt);
555         path_release(nd);
556         return err;
557 }
558
559 int follow_up(struct vfsmount **mnt, struct dentry **dentry)
560 {
561         struct vfsmount *parent;
562         struct dentry *mountpoint;
563         spin_lock(&vfsmount_lock);
564         parent=(*mnt)->mnt_parent;
565         if (parent == *mnt) {
566                 spin_unlock(&vfsmount_lock);
567                 return 0;
568         }
569         mntget(parent);
570         mountpoint=dget((*mnt)->mnt_mountpoint);
571         spin_unlock(&vfsmount_lock);
572         dput(*dentry);
573         *dentry = mountpoint;
574         mntput(*mnt);
575         *mnt = parent;
576         return 1;
577 }
578
579 /* no need for dcache_lock, as serialization is taken care in
580  * namespace.c
581  */
582 static int __follow_mount(struct path *path)
583 {
584         int res = 0;
585         while (d_mountpoint(path->dentry)) {
586                 struct vfsmount *mounted = lookup_mnt(path->mnt, path->dentry);
587                 if (!mounted)
588                         break;
589                 dput(path->dentry);
590                 if (res)
591                         mntput(path->mnt);
592                 path->mnt = mounted;
593                 path->dentry = dget(mounted->mnt_root);
594                 res = 1;
595         }
596         return res;
597 }
598
599 static void follow_mount(struct vfsmount **mnt, struct dentry **dentry)
600 {
601         while (d_mountpoint(*dentry)) {
602                 struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
603                 if (!mounted)
604                         break;
605                 dput(*dentry);
606                 mntput(*mnt);
607                 *mnt = mounted;
608                 *dentry = dget(mounted->mnt_root);
609         }
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 nameidata *nd)
631 {
632         while(1) {
633                 struct vfsmount *parent;
634                 struct dentry *old = nd->dentry;
635
636                 read_lock(&current->fs->lock);
637                 if (nd->dentry == current->fs->root &&
638                     nd->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 (nd->dentry != nd->mnt->mnt_root) {
645                         nd->dentry = dget(nd->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 = nd->mnt->mnt_parent;
653                 if (parent == nd->mnt) {
654                         spin_unlock(&vfsmount_lock);
655                         break;
656                 }
657                 mntget(parent);
658                 nd->dentry = dget(nd->mnt->mnt_mountpoint);
659                 spin_unlock(&vfsmount_lock);
660                 dput(old);
661                 mntput(nd->mnt);
662                 nd->mnt = parent;
663         }
664         follow_mount(&nd->mnt, &nd->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);
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                         err = do_follow_link(&next, nd);
805                         if (err)
806                                 goto return_err;
807                         err = -ENOENT;
808                         inode = nd->dentry->d_inode;
809                         if (!inode)
810                                 break;
811                         err = -ENOTDIR; 
812                         if (!inode->i_op)
813                                 break;
814                 } else {
815                         dput(nd->dentry);
816                         if (nd->mnt != next.mnt)
817                                 mntput(nd->mnt);
818                         nd->mnt = next.mnt;
819                         nd->dentry = next.dentry;
820                 }
821                 err = -ENOTDIR; 
822                 if (!inode->i_op->lookup)
823                         break;
824                 continue;
825                 /* here ends the main loop */
826
827 last_with_slashes:
828                 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
829 last_component:
830                 nd->flags &= ~LOOKUP_CONTINUE;
831                 if (lookup_flags & LOOKUP_PARENT)
832                         goto lookup_parent;
833                 if (this.name[0] == '.') switch (this.len) {
834                         default:
835                                 break;
836                         case 2: 
837                                 if (this.name[1] != '.')
838                                         break;
839                                 follow_dotdot(nd);
840                                 inode = nd->dentry->d_inode;
841                                 /* fallthrough */
842                         case 1:
843                                 goto return_reval;
844                 }
845                 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
846                         err = nd->dentry->d_op->d_hash(nd->dentry, &this);
847                         if (err < 0)
848                                 break;
849                 }
850                 err = do_lookup(nd, &this, &next);
851                 if (err)
852                         break;
853                 __follow_mount(&next);
854                 inode = next.dentry->d_inode;
855                 if ((lookup_flags & LOOKUP_FOLLOW)
856                     && inode && inode->i_op && inode->i_op->follow_link) {
857                         err = do_follow_link(&next, nd);
858                         if (err)
859                                 goto return_err;
860                         inode = nd->dentry->d_inode;
861                 } else {
862                         dput(nd->dentry);
863                         if (nd->mnt != next.mnt)
864                                 mntput(nd->mnt);
865                         nd->mnt = next.mnt;
866                         nd->dentry = next.dentry;
867                 }
868                 err = -ENOENT;
869                 if (!inode)
870                         break;
871                 if (lookup_flags & LOOKUP_DIRECTORY) {
872                         err = -ENOTDIR; 
873                         if (!inode->i_op || !inode->i_op->lookup)
874                                 break;
875                 }
876                 goto return_base;
877 lookup_parent:
878                 nd->last = this;
879                 nd->last_type = LAST_NORM;
880                 if (this.name[0] != '.')
881                         goto return_base;
882                 if (this.len == 1)
883                         nd->last_type = LAST_DOT;
884                 else if (this.len == 2 && this.name[1] == '.')
885                         nd->last_type = LAST_DOTDOT;
886                 else
887                         goto return_base;
888 return_reval:
889                 /*
890                  * We bypassed the ordinary revalidation routines.
891                  * We may need to check the cached dentry for staleness.
892                  */
893                 if (nd->dentry && nd->dentry->d_sb &&
894                     (nd->dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
895                         err = -ESTALE;
896                         /* Note: we do not d_invalidate() */
897                         if (!nd->dentry->d_op->d_revalidate(nd->dentry, nd))
898                                 break;
899                 }
900 return_base:
901                 return 0;
902 out_dput:
903                 dput(next.dentry);
904                 if (nd->mnt != next.mnt)
905                         mntput(next.mnt);
906                 break;
907         }
908         path_release(nd);
909 return_err:
910         return err;
911 }
912
913 /*
914  * Wrapper to retry pathname resolution whenever the underlying
915  * file system returns an ESTALE.
916  *
917  * Retry the whole path once, forcing real lookup requests
918  * instead of relying on the dcache.
919  */
920 int fastcall link_path_walk(const char *name, struct nameidata *nd)
921 {
922         struct nameidata save = *nd;
923         int result;
924
925         /* make sure the stuff we saved doesn't go away */
926         dget(save.dentry);
927         mntget(save.mnt);
928
929         result = __link_path_walk(name, nd);
930         if (result == -ESTALE) {
931                 *nd = save;
932                 dget(nd->dentry);
933                 mntget(nd->mnt);
934                 nd->flags |= LOOKUP_REVAL;
935                 result = __link_path_walk(name, nd);
936         }
937
938         dput(save.dentry);
939         mntput(save.mnt);
940
941         return result;
942 }
943
944 int fastcall path_walk(const char * name, struct nameidata *nd)
945 {
946         current->total_link_count = 0;
947         return link_path_walk(name, nd);
948 }
949
950 /* 
951  * SMP-safe: Returns 1 and nd will have valid dentry and mnt, if
952  * everything is done. Returns 0 and drops input nd, if lookup failed;
953  */
954 static int __emul_lookup_dentry(const char *name, struct nameidata *nd)
955 {
956         if (path_walk(name, nd))
957                 return 0;               /* something went wrong... */
958
959         if (!nd->dentry->d_inode || S_ISDIR(nd->dentry->d_inode->i_mode)) {
960                 struct dentry *old_dentry = nd->dentry;
961                 struct vfsmount *old_mnt = nd->mnt;
962                 struct qstr last = nd->last;
963                 int last_type = nd->last_type;
964                 /*
965                  * NAME was not found in alternate root or it's a directory.  Try to find
966                  * it in the normal root:
967                  */
968                 nd->last_type = LAST_ROOT;
969                 read_lock(&current->fs->lock);
970                 nd->mnt = mntget(current->fs->rootmnt);
971                 nd->dentry = dget(current->fs->root);
972                 read_unlock(&current->fs->lock);
973                 if (path_walk(name, nd) == 0) {
974                         if (nd->dentry->d_inode) {
975                                 dput(old_dentry);
976                                 mntput(old_mnt);
977                                 return 1;
978                         }
979                         path_release(nd);
980                 }
981                 nd->dentry = old_dentry;
982                 nd->mnt = old_mnt;
983                 nd->last = last;
984                 nd->last_type = last_type;
985         }
986         return 1;
987 }
988
989 void set_fs_altroot(void)
990 {
991         char *emul = __emul_prefix();
992         struct nameidata nd;
993         struct vfsmount *mnt = NULL, *oldmnt;
994         struct dentry *dentry = NULL, *olddentry;
995         int err;
996
997         if (!emul)
998                 goto set_it;
999         err = path_lookup(emul, LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_NOALT, &nd);
1000         if (!err) {
1001                 mnt = nd.mnt;
1002                 dentry = nd.dentry;
1003         }
1004 set_it:
1005         write_lock(&current->fs->lock);
1006         oldmnt = current->fs->altrootmnt;
1007         olddentry = current->fs->altroot;
1008         current->fs->altrootmnt = mnt;
1009         current->fs->altroot = dentry;
1010         write_unlock(&current->fs->lock);
1011         if (olddentry) {
1012                 dput(olddentry);
1013                 mntput(oldmnt);
1014         }
1015 }
1016
1017 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1018 int fastcall path_lookup(const char *name, unsigned int flags, struct nameidata *nd)
1019 {
1020         int retval = 0;
1021
1022         nd->last_type = LAST_ROOT; /* if there are only slashes... */
1023         nd->flags = flags;
1024         nd->depth = 0;
1025
1026         read_lock(&current->fs->lock);
1027         if (*name=='/') {
1028                 if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
1029                         nd->mnt = mntget(current->fs->altrootmnt);
1030                         nd->dentry = dget(current->fs->altroot);
1031                         read_unlock(&current->fs->lock);
1032                         if (__emul_lookup_dentry(name,nd))
1033                                 goto out; /* found in altroot */
1034                         read_lock(&current->fs->lock);
1035                 }
1036                 nd->mnt = mntget(current->fs->rootmnt);
1037                 nd->dentry = dget(current->fs->root);
1038         } else {
1039                 nd->mnt = mntget(current->fs->pwdmnt);
1040                 nd->dentry = dget(current->fs->pwd);
1041         }
1042         read_unlock(&current->fs->lock);
1043         current->total_link_count = 0;
1044         retval = link_path_walk(name, nd);
1045 out:
1046         if (unlikely(current->audit_context
1047                      && nd && nd->dentry && nd->dentry->d_inode))
1048                 audit_inode(name, nd->dentry->d_inode);
1049         return retval;
1050 }
1051
1052 /*
1053  * Restricted form of lookup. Doesn't follow links, single-component only,
1054  * needs parent already locked. Doesn't follow mounts.
1055  * SMP-safe.
1056  */
1057 static struct dentry * __lookup_hash(struct qstr *name, struct dentry * base, struct nameidata *nd)
1058 {
1059         struct dentry * dentry;
1060         struct inode *inode;
1061         int err;
1062
1063         inode = base->d_inode;
1064         err = permission(inode, MAY_EXEC, nd);
1065         dentry = ERR_PTR(err);
1066         if (err)
1067                 goto out;
1068
1069         /*
1070          * See if the low-level filesystem might want
1071          * to use its own hash..
1072          */
1073         if (base->d_op && base->d_op->d_hash) {
1074                 err = base->d_op->d_hash(base, name);
1075                 dentry = ERR_PTR(err);
1076                 if (err < 0)
1077                         goto out;
1078         }
1079
1080         dentry = cached_lookup(base, name, nd);
1081         if (!dentry) {
1082                 struct dentry *new = d_alloc(base, name);
1083                 dentry = ERR_PTR(-ENOMEM);
1084                 if (!new)
1085                         goto out;
1086                 dentry = inode->i_op->lookup(inode, new, nd);
1087                 if (!dentry)
1088                         dentry = new;
1089                 else
1090                         dput(new);
1091         }
1092 out:
1093         return dentry;
1094 }
1095
1096 struct dentry * lookup_hash(struct qstr *name, struct dentry * base)
1097 {
1098         return __lookup_hash(name, base, NULL);
1099 }
1100
1101 /* SMP-safe */
1102 struct dentry * lookup_one_len(const char * name, struct dentry * base, int len)
1103 {
1104         unsigned long hash;
1105         struct qstr this;
1106         unsigned int c;
1107
1108         this.name = name;
1109         this.len = len;
1110         if (!len)
1111                 goto access;
1112
1113         hash = init_name_hash();
1114         while (len--) {
1115                 c = *(const unsigned char *)name++;
1116                 if (c == '/' || c == '\0')
1117                         goto access;
1118                 hash = partial_name_hash(c, hash);
1119         }
1120         this.hash = end_name_hash(hash);
1121
1122         return lookup_hash(&this, base);
1123 access:
1124         return ERR_PTR(-EACCES);
1125 }
1126
1127 /*
1128  *      namei()
1129  *
1130  * is used by most simple commands to get the inode of a specified name.
1131  * Open, link etc use their own routines, but this is enough for things
1132  * like 'chmod' etc.
1133  *
1134  * namei exists in two versions: namei/lnamei. The only difference is
1135  * that namei follows links, while lnamei does not.
1136  * SMP-safe
1137  */
1138 int fastcall __user_walk(const char __user *name, unsigned flags, struct nameidata *nd)
1139 {
1140         char *tmp = getname(name);
1141         int err = PTR_ERR(tmp);
1142
1143         if (!IS_ERR(tmp)) {
1144                 err = path_lookup(tmp, flags, nd);
1145                 putname(tmp);
1146         }
1147         return err;
1148 }
1149
1150 /*
1151  * It's inline, so penalty for filesystems that don't use sticky bit is
1152  * minimal.
1153  */
1154 static inline int check_sticky(struct inode *dir, struct inode *inode)
1155 {
1156         if (!(dir->i_mode & S_ISVTX))
1157                 return 0;
1158         if (inode->i_uid == current->fsuid)
1159                 return 0;
1160         if (dir->i_uid == current->fsuid)
1161                 return 0;
1162         return !capable(CAP_FOWNER);
1163 }
1164
1165 /*
1166  *      Check whether we can remove a link victim from directory dir, check
1167  *  whether the type of victim is right.
1168  *  1. We can't do it if dir is read-only (done in permission())
1169  *  2. We should have write and exec permissions on dir
1170  *  3. We can't remove anything from append-only dir
1171  *  4. We can't do anything with immutable dir (done in permission())
1172  *  5. If the sticky bit on dir is set we should either
1173  *      a. be owner of dir, or
1174  *      b. be owner of victim, or
1175  *      c. have CAP_FOWNER capability
1176  *  6. If the victim is append-only or immutable we can't do antyhing with
1177  *     links pointing to it.
1178  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1179  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1180  *  9. We can't remove a root or mountpoint.
1181  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1182  *     nfs_async_unlink().
1183  */
1184 static inline int may_delete(struct inode *dir,struct dentry *victim,int isdir)
1185 {
1186         int error;
1187
1188         if (!victim->d_inode)
1189                 return -ENOENT;
1190
1191         BUG_ON(victim->d_parent->d_inode != dir);
1192
1193         error = permission(dir,MAY_WRITE | MAY_EXEC, NULL);
1194         if (error)
1195                 return error;
1196         if (IS_APPEND(dir))
1197                 return -EPERM;
1198         if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1199             IS_IMMUTABLE(victim->d_inode))
1200                 return -EPERM;
1201         if (isdir) {
1202                 if (!S_ISDIR(victim->d_inode->i_mode))
1203                         return -ENOTDIR;
1204                 if (IS_ROOT(victim))
1205                         return -EBUSY;
1206         } else if (S_ISDIR(victim->d_inode->i_mode))
1207                 return -EISDIR;
1208         if (IS_DEADDIR(dir))
1209                 return -ENOENT;
1210         if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1211                 return -EBUSY;
1212         return 0;
1213 }
1214
1215 /*      Check whether we can create an object with dentry child in directory
1216  *  dir.
1217  *  1. We can't do it if child already exists (open has special treatment for
1218  *     this case, but since we are inlined it's OK)
1219  *  2. We can't do it if dir is read-only (done in permission())
1220  *  3. We should have write and exec permissions on dir
1221  *  4. We can't do it if dir is immutable (done in permission())
1222  */
1223 static inline int may_create(struct inode *dir, struct dentry *child,
1224                              struct nameidata *nd)
1225 {
1226         if (child->d_inode)
1227                 return -EEXIST;
1228         if (IS_DEADDIR(dir))
1229                 return -ENOENT;
1230         return permission(dir,MAY_WRITE | MAY_EXEC, nd);
1231 }
1232
1233 /* 
1234  * Special case: O_CREAT|O_EXCL implies O_NOFOLLOW for security
1235  * reasons.
1236  *
1237  * O_DIRECTORY translates into forcing a directory lookup.
1238  */
1239 static inline int lookup_flags(unsigned int f)
1240 {
1241         unsigned long retval = LOOKUP_FOLLOW;
1242
1243         if (f & O_NOFOLLOW)
1244                 retval &= ~LOOKUP_FOLLOW;
1245         
1246         if ((f & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
1247                 retval &= ~LOOKUP_FOLLOW;
1248         
1249         if (f & O_DIRECTORY)
1250                 retval |= LOOKUP_DIRECTORY;
1251
1252         return retval;
1253 }
1254
1255 /*
1256  * p1 and p2 should be directories on the same fs.
1257  */
1258 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1259 {
1260         struct dentry *p;
1261
1262         if (p1 == p2) {
1263                 down(&p1->d_inode->i_sem);
1264                 return NULL;
1265         }
1266
1267         down(&p1->d_inode->i_sb->s_vfs_rename_sem);
1268
1269         for (p = p1; p->d_parent != p; p = p->d_parent) {
1270                 if (p->d_parent == p2) {
1271                         down(&p2->d_inode->i_sem);
1272                         down(&p1->d_inode->i_sem);
1273                         return p;
1274                 }
1275         }
1276
1277         for (p = p2; p->d_parent != p; p = p->d_parent) {
1278                 if (p->d_parent == p1) {
1279                         down(&p1->d_inode->i_sem);
1280                         down(&p2->d_inode->i_sem);
1281                         return p;
1282                 }
1283         }
1284
1285         down(&p1->d_inode->i_sem);
1286         down(&p2->d_inode->i_sem);
1287         return NULL;
1288 }
1289
1290 void unlock_rename(struct dentry *p1, struct dentry *p2)
1291 {
1292         up(&p1->d_inode->i_sem);
1293         if (p1 != p2) {
1294                 up(&p2->d_inode->i_sem);
1295                 up(&p1->d_inode->i_sb->s_vfs_rename_sem);
1296         }
1297 }
1298
1299 int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1300                 struct nameidata *nd)
1301 {
1302         int error = may_create(dir, dentry, nd);
1303
1304         if (error)
1305                 return error;
1306
1307         if (!dir->i_op || !dir->i_op->create)
1308                 return -EACCES; /* shouldn't it be ENOSYS? */
1309         mode &= S_IALLUGO;
1310         mode |= S_IFREG;
1311         error = security_inode_create(dir, dentry, mode);
1312         if (error)
1313                 return error;
1314         DQUOT_INIT(dir);
1315         error = dir->i_op->create(dir, dentry, mode, nd);
1316         if (!error) {
1317                 inode_dir_notify(dir, DN_CREATE);
1318                 security_inode_post_create(dir, dentry, mode);
1319         }
1320         return error;
1321 }
1322
1323 int may_open(struct nameidata *nd, int acc_mode, int flag)
1324 {
1325         struct dentry *dentry = nd->dentry;
1326         struct inode *inode = dentry->d_inode;
1327         int error;
1328
1329         if (!inode)
1330                 return -ENOENT;
1331
1332         if (S_ISLNK(inode->i_mode))
1333                 return -ELOOP;
1334         
1335         if (S_ISDIR(inode->i_mode) && (flag & FMODE_WRITE))
1336                 return -EISDIR;
1337
1338         error = permission(inode, acc_mode, nd);
1339         if (error)
1340                 return error;
1341
1342         /*
1343          * FIFO's, sockets and device files are special: they don't
1344          * actually live on the filesystem itself, and as such you
1345          * can write to them even if the filesystem is read-only.
1346          */
1347         if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
1348                 flag &= ~O_TRUNC;
1349         } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
1350                 if (nd->mnt->mnt_flags & MNT_NODEV)
1351                         return -EACCES;
1352
1353                 flag &= ~O_TRUNC;
1354         } else if (IS_RDONLY(inode) && (flag & FMODE_WRITE))
1355                 return -EROFS;
1356         /*
1357          * An append-only file must be opened in append mode for writing.
1358          */
1359         if (IS_APPEND(inode)) {
1360                 if  ((flag & FMODE_WRITE) && !(flag & O_APPEND))
1361                         return -EPERM;
1362                 if (flag & O_TRUNC)
1363                         return -EPERM;
1364         }
1365
1366         /* O_NOATIME can only be set by the owner or superuser */
1367         if (flag & O_NOATIME)
1368                 if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
1369                         return -EPERM;
1370
1371         /*
1372          * Ensure there are no outstanding leases on the file.
1373          */
1374         error = break_lease(inode, flag);
1375         if (error)
1376                 return error;
1377
1378         if (flag & O_TRUNC) {
1379                 error = get_write_access(inode);
1380                 if (error)
1381                         return error;
1382
1383                 /*
1384                  * Refuse to truncate files with mandatory locks held on them.
1385                  */
1386                 error = locks_verify_locked(inode);
1387                 if (!error) {
1388                         DQUOT_INIT(inode);
1389                         
1390                         error = do_truncate(dentry, 0);
1391                 }
1392                 put_write_access(inode);
1393                 if (error)
1394                         return error;
1395         } else
1396                 if (flag & FMODE_WRITE)
1397                         DQUOT_INIT(inode);
1398
1399         return 0;
1400 }
1401
1402 /*
1403  *      open_namei()
1404  *
1405  * namei for open - this is in fact almost the whole open-routine.
1406  *
1407  * Note that the low bits of "flag" aren't the same as in the open
1408  * system call - they are 00 - no permissions needed
1409  *                        01 - read permission needed
1410  *                        10 - write permission needed
1411  *                        11 - read/write permissions needed
1412  * which is a lot more logical, and also allows the "no perm" needed
1413  * for symlinks (where the permissions are checked later).
1414  * SMP-safe
1415  */
1416 int open_namei(const char * pathname, int flag, int mode, struct nameidata *nd)
1417 {
1418         int acc_mode, error = 0;
1419         struct path path;
1420         struct dentry *dir;
1421         int count = 0;
1422
1423         acc_mode = ACC_MODE(flag);
1424
1425         /* Allow the LSM permission hook to distinguish append 
1426            access from general write access. */
1427         if (flag & O_APPEND)
1428                 acc_mode |= MAY_APPEND;
1429
1430         /* Fill in the open() intent data */
1431         nd->intent.open.flags = flag;
1432         nd->intent.open.create_mode = mode;
1433
1434         /*
1435          * The simplest case - just a plain lookup.
1436          */
1437         if (!(flag & O_CREAT)) {
1438                 error = path_lookup(pathname, lookup_flags(flag)|LOOKUP_OPEN, nd);
1439                 if (error)
1440                         return error;
1441                 goto ok;
1442         }
1443
1444         /*
1445          * Create - we need to know the parent.
1446          */
1447         error = path_lookup(pathname, LOOKUP_PARENT|LOOKUP_OPEN|LOOKUP_CREATE, nd);
1448         if (error)
1449                 return error;
1450
1451         /*
1452          * We have the parent and last component. First of all, check
1453          * that we are not asked to creat(2) an obvious directory - that
1454          * will not do.
1455          */
1456         error = -EISDIR;
1457         if (nd->last_type != LAST_NORM || nd->last.name[nd->last.len])
1458                 goto exit;
1459
1460         dir = nd->dentry;
1461         nd->flags &= ~LOOKUP_PARENT;
1462         down(&dir->d_inode->i_sem);
1463         path.dentry = __lookup_hash(&nd->last, nd->dentry, nd);
1464         path.mnt = nd->mnt;
1465
1466 do_last:
1467         error = PTR_ERR(path.dentry);
1468         if (IS_ERR(path.dentry)) {
1469                 up(&dir->d_inode->i_sem);
1470                 goto exit;
1471         }
1472
1473         /* Negative dentry, just create the file */
1474         if (!path.dentry->d_inode) {
1475                 if (!IS_POSIXACL(dir->d_inode))
1476                         mode &= ~current->fs->umask;
1477                 error = vfs_create(dir->d_inode, path.dentry, mode, nd);
1478                 up(&dir->d_inode->i_sem);
1479                 dput(nd->dentry);
1480                 nd->dentry = path.dentry;
1481                 if (error)
1482                         goto exit;
1483                 /* Don't check for write permission, don't truncate */
1484                 acc_mode = 0;
1485                 flag &= ~O_TRUNC;
1486                 goto ok;
1487         }
1488
1489         /*
1490          * It already exists.
1491          */
1492         up(&dir->d_inode->i_sem);
1493
1494         error = -EEXIST;
1495         if (flag & O_EXCL)
1496                 goto exit_dput;
1497
1498         if (__follow_mount(&path)) {
1499                 error = -ELOOP;
1500                 if (flag & O_NOFOLLOW)
1501                         goto exit_dput;
1502         }
1503         error = -ENOENT;
1504         if (!path.dentry->d_inode)
1505                 goto exit_dput;
1506         if (path.dentry->d_inode->i_op && path.dentry->d_inode->i_op->follow_link)
1507                 goto do_link;
1508
1509         dput(nd->dentry);
1510         nd->dentry = path.dentry;
1511         if (nd->mnt != path.mnt)
1512                 mntput(nd->mnt);
1513         nd->mnt = path.mnt;
1514         error = -EISDIR;
1515         if (path.dentry->d_inode && S_ISDIR(path.dentry->d_inode->i_mode))
1516                 goto exit;
1517 ok:
1518         error = may_open(nd, acc_mode, flag);
1519         if (error)
1520                 goto exit;
1521         return 0;
1522
1523 exit_dput:
1524         dput(path.dentry);
1525         if (nd->mnt != path.mnt)
1526                 mntput(path.mnt);
1527 exit:
1528         path_release(nd);
1529         return error;
1530
1531 do_link:
1532         error = -ELOOP;
1533         if (flag & O_NOFOLLOW)
1534                 goto exit_dput;
1535         /*
1536          * This is subtle. Instead of calling do_follow_link() we do the
1537          * thing by hands. The reason is that this way we have zero link_count
1538          * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1539          * After that we have the parent and last component, i.e.
1540          * we are in the same situation as after the first path_walk().
1541          * Well, almost - if the last component is normal we get its copy
1542          * stored in nd->last.name and we will have to putname() it when we
1543          * are done. Procfs-like symlinks just set LAST_BIND.
1544          */
1545         nd->flags |= LOOKUP_PARENT;
1546         error = security_inode_follow_link(path.dentry, nd);
1547         if (error)
1548                 goto exit_dput;
1549         error = __do_follow_link(&path, nd);
1550         if (error)
1551                 return error;
1552         nd->flags &= ~LOOKUP_PARENT;
1553         if (nd->last_type == LAST_BIND)
1554                 goto ok;
1555         error = -EISDIR;
1556         if (nd->last_type != LAST_NORM)
1557                 goto exit;
1558         if (nd->last.name[nd->last.len]) {
1559                 putname(nd->last.name);
1560                 goto exit;
1561         }
1562         error = -ELOOP;
1563         if (count++==32) {
1564                 putname(nd->last.name);
1565                 goto exit;
1566         }
1567         dir = nd->dentry;
1568         down(&dir->d_inode->i_sem);
1569         path.dentry = __lookup_hash(&nd->last, nd->dentry, nd);
1570         path.mnt = nd->mnt;
1571         putname(nd->last.name);
1572         goto do_last;
1573 }
1574
1575 /**
1576  * lookup_create - lookup a dentry, creating it if it doesn't exist
1577  * @nd: nameidata info
1578  * @is_dir: directory flag
1579  *
1580  * Simple function to lookup and return a dentry and create it
1581  * if it doesn't exist.  Is SMP-safe.
1582  */
1583 struct dentry *lookup_create(struct nameidata *nd, int is_dir)
1584 {
1585         struct dentry *dentry;
1586
1587         down(&nd->dentry->d_inode->i_sem);
1588         dentry = ERR_PTR(-EEXIST);
1589         if (nd->last_type != LAST_NORM)
1590                 goto fail;
1591         nd->flags &= ~LOOKUP_PARENT;
1592         dentry = lookup_hash(&nd->last, nd->dentry);
1593         if (IS_ERR(dentry))
1594                 goto fail;
1595         if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode)
1596                 goto enoent;
1597         return dentry;
1598 enoent:
1599         dput(dentry);
1600         dentry = ERR_PTR(-ENOENT);
1601 fail:
1602         return dentry;
1603 }
1604 EXPORT_SYMBOL_GPL(lookup_create);
1605
1606 int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1607 {
1608         int error = may_create(dir, dentry, NULL);
1609
1610         if (error)
1611                 return error;
1612
1613         if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1614                 return -EPERM;
1615
1616         if (!dir->i_op || !dir->i_op->mknod)
1617                 return -EPERM;
1618
1619         error = security_inode_mknod(dir, dentry, mode, dev);
1620         if (error)
1621                 return error;
1622
1623         DQUOT_INIT(dir);
1624         error = dir->i_op->mknod(dir, dentry, mode, dev);
1625         if (!error) {
1626                 inode_dir_notify(dir, DN_CREATE);
1627                 security_inode_post_mknod(dir, dentry, mode, dev);
1628         }
1629         return error;
1630 }
1631
1632 asmlinkage long sys_mknod(const char __user * filename, int mode, unsigned dev)
1633 {
1634         int error = 0;
1635         char * tmp;
1636         struct dentry * dentry;
1637         struct nameidata nd;
1638
1639         if (S_ISDIR(mode))
1640                 return -EPERM;
1641         tmp = getname(filename);
1642         if (IS_ERR(tmp))
1643                 return PTR_ERR(tmp);
1644
1645         error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1646         if (error)
1647                 goto out;
1648         dentry = lookup_create(&nd, 0);
1649         error = PTR_ERR(dentry);
1650
1651         if (!IS_POSIXACL(nd.dentry->d_inode))
1652                 mode &= ~current->fs->umask;
1653         if (!IS_ERR(dentry)) {
1654                 switch (mode & S_IFMT) {
1655                 case 0: case S_IFREG:
1656                         error = vfs_create(nd.dentry->d_inode,dentry,mode,&nd);
1657                         break;
1658                 case S_IFCHR: case S_IFBLK:
1659                         error = vfs_mknod(nd.dentry->d_inode,dentry,mode,
1660                                         new_decode_dev(dev));
1661                         break;
1662                 case S_IFIFO: case S_IFSOCK:
1663                         error = vfs_mknod(nd.dentry->d_inode,dentry,mode,0);
1664                         break;
1665                 case S_IFDIR:
1666                         error = -EPERM;
1667                         break;
1668                 default:
1669                         error = -EINVAL;
1670                 }
1671                 dput(dentry);
1672         }
1673         up(&nd.dentry->d_inode->i_sem);
1674         path_release(&nd);
1675 out:
1676         putname(tmp);
1677
1678         return error;
1679 }
1680
1681 int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1682 {
1683         int error = may_create(dir, dentry, NULL);
1684
1685         if (error)
1686                 return error;
1687
1688         if (!dir->i_op || !dir->i_op->mkdir)
1689                 return -EPERM;
1690
1691         mode &= (S_IRWXUGO|S_ISVTX);
1692         error = security_inode_mkdir(dir, dentry, mode);
1693         if (error)
1694                 return error;
1695
1696         DQUOT_INIT(dir);
1697         error = dir->i_op->mkdir(dir, dentry, mode);
1698         if (!error) {
1699                 inode_dir_notify(dir, DN_CREATE);
1700                 security_inode_post_mkdir(dir,dentry, mode);
1701         }
1702         return error;
1703 }
1704
1705 asmlinkage long sys_mkdir(const char __user * pathname, int mode)
1706 {
1707         int error = 0;
1708         char * tmp;
1709
1710         tmp = getname(pathname);
1711         error = PTR_ERR(tmp);
1712         if (!IS_ERR(tmp)) {
1713                 struct dentry *dentry;
1714                 struct nameidata nd;
1715
1716                 error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1717                 if (error)
1718                         goto out;
1719                 dentry = lookup_create(&nd, 1);
1720                 error = PTR_ERR(dentry);
1721                 if (!IS_ERR(dentry)) {
1722                         if (!IS_POSIXACL(nd.dentry->d_inode))
1723                                 mode &= ~current->fs->umask;
1724                         error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
1725                         dput(dentry);
1726                 }
1727                 up(&nd.dentry->d_inode->i_sem);
1728                 path_release(&nd);
1729 out:
1730                 putname(tmp);
1731         }
1732
1733         return error;
1734 }
1735
1736 /*
1737  * We try to drop the dentry early: we should have
1738  * a usage count of 2 if we're the only user of this
1739  * dentry, and if that is true (possibly after pruning
1740  * the dcache), then we drop the dentry now.
1741  *
1742  * A low-level filesystem can, if it choses, legally
1743  * do a
1744  *
1745  *      if (!d_unhashed(dentry))
1746  *              return -EBUSY;
1747  *
1748  * if it cannot handle the case of removing a directory
1749  * that is still in use by something else..
1750  */
1751 void dentry_unhash(struct dentry *dentry)
1752 {
1753         dget(dentry);
1754         if (atomic_read(&dentry->d_count))
1755                 shrink_dcache_parent(dentry);
1756         spin_lock(&dcache_lock);
1757         spin_lock(&dentry->d_lock);
1758         if (atomic_read(&dentry->d_count) == 2)
1759                 __d_drop(dentry);
1760         spin_unlock(&dentry->d_lock);
1761         spin_unlock(&dcache_lock);
1762 }
1763
1764 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
1765 {
1766         int error = may_delete(dir, dentry, 1);
1767
1768         if (error)
1769                 return error;
1770
1771         if (!dir->i_op || !dir->i_op->rmdir)
1772                 return -EPERM;
1773
1774         DQUOT_INIT(dir);
1775
1776         down(&dentry->d_inode->i_sem);
1777         dentry_unhash(dentry);
1778         if (d_mountpoint(dentry))
1779                 error = -EBUSY;
1780         else {
1781                 error = security_inode_rmdir(dir, dentry);
1782                 if (!error) {
1783                         error = dir->i_op->rmdir(dir, dentry);
1784                         if (!error)
1785                                 dentry->d_inode->i_flags |= S_DEAD;
1786                 }
1787         }
1788         up(&dentry->d_inode->i_sem);
1789         if (!error) {
1790                 inode_dir_notify(dir, DN_DELETE);
1791                 d_delete(dentry);
1792         }
1793         dput(dentry);
1794
1795         return error;
1796 }
1797
1798 asmlinkage long sys_rmdir(const char __user * pathname)
1799 {
1800         int error = 0;
1801         char * name;
1802         struct dentry *dentry;
1803         struct nameidata nd;
1804
1805         name = getname(pathname);
1806         if(IS_ERR(name))
1807                 return PTR_ERR(name);
1808
1809         error = path_lookup(name, LOOKUP_PARENT, &nd);
1810         if (error)
1811                 goto exit;
1812
1813         switch(nd.last_type) {
1814                 case LAST_DOTDOT:
1815                         error = -ENOTEMPTY;
1816                         goto exit1;
1817                 case LAST_DOT:
1818                         error = -EINVAL;
1819                         goto exit1;
1820                 case LAST_ROOT:
1821                         error = -EBUSY;
1822                         goto exit1;
1823         }
1824         down(&nd.dentry->d_inode->i_sem);
1825         dentry = lookup_hash(&nd.last, nd.dentry);
1826         error = PTR_ERR(dentry);
1827         if (!IS_ERR(dentry)) {
1828                 error = vfs_rmdir(nd.dentry->d_inode, dentry);
1829                 dput(dentry);
1830         }
1831         up(&nd.dentry->d_inode->i_sem);
1832 exit1:
1833         path_release(&nd);
1834 exit:
1835         putname(name);
1836         return error;
1837 }
1838
1839 int vfs_unlink(struct inode *dir, struct dentry *dentry)
1840 {
1841         int error = may_delete(dir, dentry, 0);
1842
1843         if (error)
1844                 return error;
1845
1846         if (!dir->i_op || !dir->i_op->unlink)
1847                 return -EPERM;
1848
1849         DQUOT_INIT(dir);
1850
1851         down(&dentry->d_inode->i_sem);
1852         if (d_mountpoint(dentry))
1853                 error = -EBUSY;
1854         else {
1855                 error = security_inode_unlink(dir, dentry);
1856                 if (!error)
1857                         error = dir->i_op->unlink(dir, dentry);
1858         }
1859         up(&dentry->d_inode->i_sem);
1860
1861         /* We don't d_delete() NFS sillyrenamed files--they still exist. */
1862         if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
1863                 d_delete(dentry);
1864                 inode_dir_notify(dir, DN_DELETE);
1865         }
1866         return error;
1867 }
1868
1869 /*
1870  * Make sure that the actual truncation of the file will occur outside its
1871  * directory's i_sem.  Truncate can take a long time if there is a lot of
1872  * writeout happening, and we don't want to prevent access to the directory
1873  * while waiting on the I/O.
1874  */
1875 asmlinkage long sys_unlink(const char __user * pathname)
1876 {
1877         int error = 0;
1878         char * name;
1879         struct dentry *dentry;
1880         struct nameidata nd;
1881         struct inode *inode = NULL;
1882
1883         name = getname(pathname);
1884         if(IS_ERR(name))
1885                 return PTR_ERR(name);
1886
1887         error = path_lookup(name, LOOKUP_PARENT, &nd);
1888         if (error)
1889                 goto exit;
1890         error = -EISDIR;
1891         if (nd.last_type != LAST_NORM)
1892                 goto exit1;
1893         down(&nd.dentry->d_inode->i_sem);
1894         dentry = lookup_hash(&nd.last, nd.dentry);
1895         error = PTR_ERR(dentry);
1896         if (!IS_ERR(dentry)) {
1897                 /* Why not before? Because we want correct error value */
1898                 if (nd.last.name[nd.last.len])
1899                         goto slashes;
1900                 inode = dentry->d_inode;
1901                 if (inode)
1902                         atomic_inc(&inode->i_count);
1903                 error = vfs_unlink(nd.dentry->d_inode, dentry);
1904         exit2:
1905                 dput(dentry);
1906         }
1907         up(&nd.dentry->d_inode->i_sem);
1908         if (inode)
1909                 iput(inode);    /* truncate the inode here */
1910 exit1:
1911         path_release(&nd);
1912 exit:
1913         putname(name);
1914         return error;
1915
1916 slashes:
1917         error = !dentry->d_inode ? -ENOENT :
1918                 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
1919         goto exit2;
1920 }
1921
1922 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname, int mode)
1923 {
1924         int error = may_create(dir, dentry, NULL);
1925
1926         if (error)
1927                 return error;
1928
1929         if (!dir->i_op || !dir->i_op->symlink)
1930                 return -EPERM;
1931
1932         error = security_inode_symlink(dir, dentry, oldname);
1933         if (error)
1934                 return error;
1935
1936         DQUOT_INIT(dir);
1937         error = dir->i_op->symlink(dir, dentry, oldname);
1938         if (!error) {
1939                 inode_dir_notify(dir, DN_CREATE);
1940                 security_inode_post_symlink(dir, dentry, oldname);
1941         }
1942         return error;
1943 }
1944
1945 asmlinkage long sys_symlink(const char __user * oldname, const char __user * newname)
1946 {
1947         int error = 0;
1948         char * from;
1949         char * to;
1950
1951         from = getname(oldname);
1952         if(IS_ERR(from))
1953                 return PTR_ERR(from);
1954         to = getname(newname);
1955         error = PTR_ERR(to);
1956         if (!IS_ERR(to)) {
1957                 struct dentry *dentry;
1958                 struct nameidata nd;
1959
1960                 error = path_lookup(to, LOOKUP_PARENT, &nd);
1961                 if (error)
1962                         goto out;
1963                 dentry = lookup_create(&nd, 0);
1964                 error = PTR_ERR(dentry);
1965                 if (!IS_ERR(dentry)) {
1966                         error = vfs_symlink(nd.dentry->d_inode, dentry, from, S_IALLUGO);
1967                         dput(dentry);
1968                 }
1969                 up(&nd.dentry->d_inode->i_sem);
1970                 path_release(&nd);
1971 out:
1972                 putname(to);
1973         }
1974         putname(from);
1975         return error;
1976 }
1977
1978 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
1979 {
1980         struct inode *inode = old_dentry->d_inode;
1981         int error;
1982
1983         if (!inode)
1984                 return -ENOENT;
1985
1986         error = may_create(dir, new_dentry, NULL);
1987         if (error)
1988                 return error;
1989
1990         if (dir->i_sb != inode->i_sb)
1991                 return -EXDEV;
1992
1993         /*
1994          * A link to an append-only or immutable file cannot be created.
1995          */
1996         if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1997                 return -EPERM;
1998         if (!dir->i_op || !dir->i_op->link)
1999                 return -EPERM;
2000         if (S_ISDIR(old_dentry->d_inode->i_mode))
2001                 return -EPERM;
2002
2003         error = security_inode_link(old_dentry, dir, new_dentry);
2004         if (error)
2005                 return error;
2006
2007         down(&old_dentry->d_inode->i_sem);
2008         DQUOT_INIT(dir);
2009         error = dir->i_op->link(old_dentry, dir, new_dentry);
2010         up(&old_dentry->d_inode->i_sem);
2011         if (!error) {
2012                 inode_dir_notify(dir, DN_CREATE);
2013                 security_inode_post_link(old_dentry, dir, new_dentry);
2014         }
2015         return error;
2016 }
2017
2018 /*
2019  * Hardlinks are often used in delicate situations.  We avoid
2020  * security-related surprises by not following symlinks on the
2021  * newname.  --KAB
2022  *
2023  * We don't follow them on the oldname either to be compatible
2024  * with linux 2.0, and to avoid hard-linking to directories
2025  * and other special files.  --ADM
2026  */
2027 asmlinkage long sys_link(const char __user * oldname, const char __user * newname)
2028 {
2029         struct dentry *new_dentry;
2030         struct nameidata nd, old_nd;
2031         int error;
2032         char * to;
2033
2034         to = getname(newname);
2035         if (IS_ERR(to))
2036                 return PTR_ERR(to);
2037
2038         error = __user_walk(oldname, 0, &old_nd);
2039         if (error)
2040                 goto exit;
2041         error = path_lookup(to, LOOKUP_PARENT, &nd);
2042         if (error)
2043                 goto out;
2044         error = -EXDEV;
2045         if (old_nd.mnt != nd.mnt)
2046                 goto out_release;
2047         new_dentry = lookup_create(&nd, 0);
2048         error = PTR_ERR(new_dentry);
2049         if (!IS_ERR(new_dentry)) {
2050                 error = vfs_link(old_nd.dentry, nd.dentry->d_inode, new_dentry);
2051                 dput(new_dentry);
2052         }
2053         up(&nd.dentry->d_inode->i_sem);
2054 out_release:
2055         path_release(&nd);
2056 out:
2057         path_release(&old_nd);
2058 exit:
2059         putname(to);
2060
2061         return error;
2062 }
2063
2064 /*
2065  * The worst of all namespace operations - renaming directory. "Perverted"
2066  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2067  * Problems:
2068  *      a) we can get into loop creation. Check is done in is_subdir().
2069  *      b) race potential - two innocent renames can create a loop together.
2070  *         That's where 4.4 screws up. Current fix: serialization on
2071  *         sb->s_vfs_rename_sem. We might be more accurate, but that's another
2072  *         story.
2073  *      c) we have to lock _three_ objects - parents and victim (if it exists).
2074  *         And that - after we got ->i_sem on parents (until then we don't know
2075  *         whether the target exists).  Solution: try to be smart with locking
2076  *         order for inodes.  We rely on the fact that tree topology may change
2077  *         only under ->s_vfs_rename_sem _and_ that parent of the object we
2078  *         move will be locked.  Thus we can rank directories by the tree
2079  *         (ancestors first) and rank all non-directories after them.
2080  *         That works since everybody except rename does "lock parent, lookup,
2081  *         lock child" and rename is under ->s_vfs_rename_sem.
2082  *         HOWEVER, it relies on the assumption that any object with ->lookup()
2083  *         has no more than 1 dentry.  If "hybrid" objects will ever appear,
2084  *         we'd better make sure that there's no link(2) for them.
2085  *      d) some filesystems don't support opened-but-unlinked directories,
2086  *         either because of layout or because they are not ready to deal with
2087  *         all cases correctly. The latter will be fixed (taking this sort of
2088  *         stuff into VFS), but the former is not going away. Solution: the same
2089  *         trick as in rmdir().
2090  *      e) conversion from fhandle to dentry may come in the wrong moment - when
2091  *         we are removing the target. Solution: we will have to grab ->i_sem
2092  *         in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
2093  *         ->i_sem on parents, which works but leads to some truely excessive
2094  *         locking].
2095  */
2096 static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
2097                           struct inode *new_dir, struct dentry *new_dentry)
2098 {
2099         int error = 0;
2100         struct inode *target;
2101
2102         /*
2103          * If we are going to change the parent - check write permissions,
2104          * we'll need to flip '..'.
2105          */
2106         if (new_dir != old_dir) {
2107                 error = permission(old_dentry->d_inode, MAY_WRITE, NULL);
2108                 if (error)
2109                         return error;
2110         }
2111
2112         error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2113         if (error)
2114                 return error;
2115
2116         target = new_dentry->d_inode;
2117         if (target) {
2118                 down(&target->i_sem);
2119                 dentry_unhash(new_dentry);
2120         }
2121         if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2122                 error = -EBUSY;
2123         else 
2124                 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2125         if (target) {
2126                 if (!error)
2127                         target->i_flags |= S_DEAD;
2128                 up(&target->i_sem);
2129                 if (d_unhashed(new_dentry))
2130                         d_rehash(new_dentry);
2131                 dput(new_dentry);
2132         }
2133         if (!error) {
2134                 d_move(old_dentry,new_dentry);
2135                 security_inode_post_rename(old_dir, old_dentry,
2136                                            new_dir, new_dentry);
2137         }
2138         return error;
2139 }
2140
2141 static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
2142                             struct inode *new_dir, struct dentry *new_dentry)
2143 {
2144         struct inode *target;
2145         int error;
2146
2147         error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2148         if (error)
2149                 return error;
2150
2151         dget(new_dentry);
2152         target = new_dentry->d_inode;
2153         if (target)
2154                 down(&target->i_sem);
2155         if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2156                 error = -EBUSY;
2157         else
2158                 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2159         if (!error) {
2160                 /* The following d_move() should become unconditional */
2161                 if (!(old_dir->i_sb->s_type->fs_flags & FS_ODD_RENAME))
2162                         d_move(old_dentry, new_dentry);
2163                 security_inode_post_rename(old_dir, old_dentry, new_dir, new_dentry);
2164         }
2165         if (target)
2166                 up(&target->i_sem);
2167         dput(new_dentry);
2168         return error;
2169 }
2170
2171 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
2172                struct inode *new_dir, struct dentry *new_dentry)
2173 {
2174         int error;
2175         int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
2176
2177         if (old_dentry->d_inode == new_dentry->d_inode)
2178                 return 0;
2179  
2180         error = may_delete(old_dir, old_dentry, is_dir);
2181         if (error)
2182                 return error;
2183
2184         if (!new_dentry->d_inode)
2185                 error = may_create(new_dir, new_dentry, NULL);
2186         else
2187                 error = may_delete(new_dir, new_dentry, is_dir);
2188         if (error)
2189                 return error;
2190
2191         if (!old_dir->i_op || !old_dir->i_op->rename)
2192                 return -EPERM;
2193
2194         DQUOT_INIT(old_dir);
2195         DQUOT_INIT(new_dir);
2196
2197         if (is_dir)
2198                 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
2199         else
2200                 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
2201         if (!error) {
2202                 if (old_dir == new_dir)
2203                         inode_dir_notify(old_dir, DN_RENAME);
2204                 else {
2205                         inode_dir_notify(old_dir, DN_DELETE);
2206                         inode_dir_notify(new_dir, DN_CREATE);
2207                 }
2208         }
2209         return error;
2210 }
2211
2212 static inline int do_rename(const char * oldname, const char * newname)
2213 {
2214         int error = 0;
2215         struct dentry * old_dir, * new_dir;
2216         struct dentry * old_dentry, *new_dentry;
2217         struct dentry * trap;
2218         struct nameidata oldnd, newnd;
2219
2220         error = path_lookup(oldname, LOOKUP_PARENT, &oldnd);
2221         if (error)
2222                 goto exit;
2223
2224         error = path_lookup(newname, LOOKUP_PARENT, &newnd);
2225         if (error)
2226                 goto exit1;
2227
2228         error = -EXDEV;
2229         if (oldnd.mnt != newnd.mnt)
2230                 goto exit2;
2231
2232         old_dir = oldnd.dentry;
2233         error = -EBUSY;
2234         if (oldnd.last_type != LAST_NORM)
2235                 goto exit2;
2236
2237         new_dir = newnd.dentry;
2238         if (newnd.last_type != LAST_NORM)
2239                 goto exit2;
2240
2241         trap = lock_rename(new_dir, old_dir);
2242
2243         old_dentry = lookup_hash(&oldnd.last, old_dir);
2244         error = PTR_ERR(old_dentry);
2245         if (IS_ERR(old_dentry))
2246                 goto exit3;
2247         /* source must exist */
2248         error = -ENOENT;
2249         if (!old_dentry->d_inode)
2250                 goto exit4;
2251         /* unless the source is a directory trailing slashes give -ENOTDIR */
2252         if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
2253                 error = -ENOTDIR;
2254                 if (oldnd.last.name[oldnd.last.len])
2255                         goto exit4;
2256                 if (newnd.last.name[newnd.last.len])
2257                         goto exit4;
2258         }
2259         /* source should not be ancestor of target */
2260         error = -EINVAL;
2261         if (old_dentry == trap)
2262                 goto exit4;
2263         new_dentry = lookup_hash(&newnd.last, new_dir);
2264         error = PTR_ERR(new_dentry);
2265         if (IS_ERR(new_dentry))
2266                 goto exit4;
2267         /* target should not be an ancestor of source */
2268         error = -ENOTEMPTY;
2269         if (new_dentry == trap)
2270                 goto exit5;
2271
2272         error = vfs_rename(old_dir->d_inode, old_dentry,
2273                                    new_dir->d_inode, new_dentry);
2274 exit5:
2275         dput(new_dentry);
2276 exit4:
2277         dput(old_dentry);
2278 exit3:
2279         unlock_rename(new_dir, old_dir);
2280 exit2:
2281         path_release(&newnd);
2282 exit1:
2283         path_release(&oldnd);
2284 exit:
2285         return error;
2286 }
2287
2288 asmlinkage long sys_rename(const char __user * oldname, const char __user * newname)
2289 {
2290         int error;
2291         char * from;
2292         char * to;
2293
2294         from = getname(oldname);
2295         if(IS_ERR(from))
2296                 return PTR_ERR(from);
2297         to = getname(newname);
2298         error = PTR_ERR(to);
2299         if (!IS_ERR(to)) {
2300                 error = do_rename(from,to);
2301                 putname(to);
2302         }
2303         putname(from);
2304         return error;
2305 }
2306
2307 int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
2308 {
2309         int len;
2310
2311         len = PTR_ERR(link);
2312         if (IS_ERR(link))
2313                 goto out;
2314
2315         len = strlen(link);
2316         if (len > (unsigned) buflen)
2317                 len = buflen;
2318         if (copy_to_user(buffer, link, len))
2319                 len = -EFAULT;
2320 out:
2321         return len;
2322 }
2323
2324 /*
2325  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
2326  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
2327  * using) it for any given inode is up to filesystem.
2328  */
2329 int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2330 {
2331         struct nameidata nd;
2332         int res;
2333         nd.depth = 0;
2334         res = dentry->d_inode->i_op->follow_link(dentry, &nd);
2335         if (!res) {
2336                 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
2337                 if (dentry->d_inode->i_op->put_link)
2338                         dentry->d_inode->i_op->put_link(dentry, &nd);
2339         }
2340         return res;
2341 }
2342
2343 int vfs_follow_link(struct nameidata *nd, const char *link)
2344 {
2345         return __vfs_follow_link(nd, link);
2346 }
2347
2348 /* get the link contents into pagecache */
2349 static char *page_getlink(struct dentry * dentry, struct page **ppage)
2350 {
2351         struct page * page;
2352         struct address_space *mapping = dentry->d_inode->i_mapping;
2353         page = read_cache_page(mapping, 0, (filler_t *)mapping->a_ops->readpage,
2354                                 NULL);
2355         if (IS_ERR(page))
2356                 goto sync_fail;
2357         wait_on_page_locked(page);
2358         if (!PageUptodate(page))
2359                 goto async_fail;
2360         *ppage = page;
2361         return kmap(page);
2362
2363 async_fail:
2364         page_cache_release(page);
2365         return ERR_PTR(-EIO);
2366
2367 sync_fail:
2368         return (char*)page;
2369 }
2370
2371 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2372 {
2373         struct page *page = NULL;
2374         char *s = page_getlink(dentry, &page);
2375         int res = vfs_readlink(dentry,buffer,buflen,s);
2376         if (page) {
2377                 kunmap(page);
2378                 page_cache_release(page);
2379         }
2380         return res;
2381 }
2382
2383 int page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
2384 {
2385         struct page *page;
2386         nd_set_link(nd, page_getlink(dentry, &page));
2387         return 0;
2388 }
2389
2390 void page_put_link(struct dentry *dentry, struct nameidata *nd)
2391 {
2392         if (!IS_ERR(nd_get_link(nd))) {
2393                 struct page *page;
2394                 page = find_get_page(dentry->d_inode->i_mapping, 0);
2395                 if (!page)
2396                         BUG();
2397                 kunmap(page);
2398                 page_cache_release(page);
2399                 page_cache_release(page);
2400         }
2401 }
2402
2403 int page_symlink(struct inode *inode, const char *symname, int len)
2404 {
2405         struct address_space *mapping = inode->i_mapping;
2406         struct page *page = grab_cache_page(mapping, 0);
2407         int err = -ENOMEM;
2408         char *kaddr;
2409
2410         if (!page)
2411                 goto fail;
2412         err = mapping->a_ops->prepare_write(NULL, page, 0, len-1);
2413         if (err)
2414                 goto fail_map;
2415         kaddr = kmap_atomic(page, KM_USER0);
2416         memcpy(kaddr, symname, len-1);
2417         kunmap_atomic(kaddr, KM_USER0);
2418         mapping->a_ops->commit_write(NULL, page, 0, len-1);
2419         /*
2420          * Notice that we are _not_ going to block here - end of page is
2421          * unmapped, so this will only try to map the rest of page, see
2422          * that it is unmapped (typically even will not look into inode -
2423          * ->i_size will be enough for everything) and zero it out.
2424          * OTOH it's obviously correct and should make the page up-to-date.
2425          */
2426         if (!PageUptodate(page)) {
2427                 err = mapping->a_ops->readpage(NULL, page);
2428                 wait_on_page_locked(page);
2429         } else {
2430                 unlock_page(page);
2431         }
2432         page_cache_release(page);
2433         if (err < 0)
2434                 goto fail;
2435         mark_inode_dirty(inode);
2436         return 0;
2437 fail_map:
2438         unlock_page(page);
2439         page_cache_release(page);
2440 fail:
2441         return err;
2442 }
2443
2444 struct inode_operations page_symlink_inode_operations = {
2445         .readlink       = generic_readlink,
2446         .follow_link    = page_follow_link_light,
2447         .put_link       = page_put_link,
2448 };
2449
2450 EXPORT_SYMBOL(__user_walk);
2451 EXPORT_SYMBOL(follow_down);
2452 EXPORT_SYMBOL(follow_up);
2453 EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
2454 EXPORT_SYMBOL(getname);
2455 EXPORT_SYMBOL(lock_rename);
2456 EXPORT_SYMBOL(lookup_hash);
2457 EXPORT_SYMBOL(lookup_one_len);
2458 EXPORT_SYMBOL(page_follow_link_light);
2459 EXPORT_SYMBOL(page_put_link);
2460 EXPORT_SYMBOL(page_readlink);
2461 EXPORT_SYMBOL(page_symlink);
2462 EXPORT_SYMBOL(page_symlink_inode_operations);
2463 EXPORT_SYMBOL(path_lookup);
2464 EXPORT_SYMBOL(path_release);
2465 EXPORT_SYMBOL(path_walk);
2466 EXPORT_SYMBOL(permission);
2467 EXPORT_SYMBOL(unlock_rename);
2468 EXPORT_SYMBOL(vfs_create);
2469 EXPORT_SYMBOL(vfs_follow_link);
2470 EXPORT_SYMBOL(vfs_link);
2471 EXPORT_SYMBOL(vfs_mkdir);
2472 EXPORT_SYMBOL(vfs_mknod);
2473 EXPORT_SYMBOL(generic_permission);
2474 EXPORT_SYMBOL(vfs_readlink);
2475 EXPORT_SYMBOL(vfs_rename);
2476 EXPORT_SYMBOL(vfs_rmdir);
2477 EXPORT_SYMBOL(vfs_symlink);
2478 EXPORT_SYMBOL(vfs_unlink);
2479 EXPORT_SYMBOL(dentry_unhash);
2480 EXPORT_SYMBOL(generic_readlink);