[PATCH] namei fixes (14/19)
[safe/jmp/linux-2.6] / 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         if (path->mnt != nd->mnt)
547                 mntput(nd->mnt);
548         err = __do_follow_link(path, nd);
549         current->link_count--;
550         nd->depth--;
551         return err;
552 loop:
553         if (path->mnt != nd->mnt)
554                 mntput(nd->mnt);
555         dput(path->dentry);
556         path_release(nd);
557         return err;
558 }
559
560 int follow_up(struct vfsmount **mnt, struct dentry **dentry)
561 {
562         struct vfsmount *parent;
563         struct dentry *mountpoint;
564         spin_lock(&vfsmount_lock);
565         parent=(*mnt)->mnt_parent;
566         if (parent == *mnt) {
567                 spin_unlock(&vfsmount_lock);
568                 return 0;
569         }
570         mntget(parent);
571         mountpoint=dget((*mnt)->mnt_mountpoint);
572         spin_unlock(&vfsmount_lock);
573         dput(*dentry);
574         *dentry = mountpoint;
575         mntput(*mnt);
576         *mnt = parent;
577         return 1;
578 }
579
580 /* no need for dcache_lock, as serialization is taken care in
581  * namespace.c
582  */
583 static int __follow_mount(struct path *path)
584 {
585         int res = 0;
586         while (d_mountpoint(path->dentry)) {
587                 struct vfsmount *mounted = lookup_mnt(path->mnt, path->dentry);
588                 if (!mounted)
589                         break;
590                 dput(path->dentry);
591                 if (res)
592                         mntput(path->mnt);
593                 path->mnt = mounted;
594                 path->dentry = dget(mounted->mnt_root);
595                 res = 1;
596         }
597         return res;
598 }
599
600 static int follow_mount(struct vfsmount **mnt, struct dentry **dentry)
601 {
602         int res = 0;
603         while (d_mountpoint(*dentry)) {
604                 struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
605                 if (!mounted)
606                         break;
607                 mntput(*mnt);
608                 *mnt = mounted;
609                 dput(*dentry);
610                 *dentry = dget(mounted->mnt_root);
611                 res = 1;
612         }
613         return res;
614 }
615
616 /* no need for dcache_lock, as serialization is taken care in
617  * namespace.c
618  */
619 int follow_down(struct vfsmount **mnt, struct dentry **dentry)
620 {
621         struct vfsmount *mounted;
622
623         mounted = lookup_mnt(*mnt, *dentry);
624         if (mounted) {
625                 dput(*dentry);
626                 mntput(*mnt);
627                 *mnt = mounted;
628                 *dentry = dget(mounted->mnt_root);
629                 return 1;
630         }
631         return 0;
632 }
633
634 static inline void follow_dotdot(struct vfsmount **mnt, struct dentry **dentry)
635 {
636         while(1) {
637                 struct vfsmount *parent;
638                 struct dentry *old = *dentry;
639
640                 read_lock(&current->fs->lock);
641                 if (*dentry == current->fs->root &&
642                     *mnt == current->fs->rootmnt) {
643                         read_unlock(&current->fs->lock);
644                         break;
645                 }
646                 read_unlock(&current->fs->lock);
647                 spin_lock(&dcache_lock);
648                 if (*dentry != (*mnt)->mnt_root) {
649                         *dentry = dget((*dentry)->d_parent);
650                         spin_unlock(&dcache_lock);
651                         dput(old);
652                         break;
653                 }
654                 spin_unlock(&dcache_lock);
655                 spin_lock(&vfsmount_lock);
656                 parent = (*mnt)->mnt_parent;
657                 if (parent == *mnt) {
658                         spin_unlock(&vfsmount_lock);
659                         break;
660                 }
661                 mntget(parent);
662                 *dentry = dget((*mnt)->mnt_mountpoint);
663                 spin_unlock(&vfsmount_lock);
664                 dput(old);
665                 mntput(*mnt);
666                 *mnt = parent;
667         }
668         follow_mount(mnt, dentry);
669 }
670
671 /*
672  *  It's more convoluted than I'd like it to be, but... it's still fairly
673  *  small and for now I'd prefer to have fast path as straight as possible.
674  *  It _is_ time-critical.
675  */
676 static int do_lookup(struct nameidata *nd, struct qstr *name,
677                      struct path *path)
678 {
679         struct vfsmount *mnt = nd->mnt;
680         struct dentry *dentry = __d_lookup(nd->dentry, name);
681
682         if (!dentry)
683                 goto need_lookup;
684         if (dentry->d_op && dentry->d_op->d_revalidate)
685                 goto need_revalidate;
686 done:
687         path->mnt = mnt;
688         path->dentry = dentry;
689         return 0;
690
691 need_lookup:
692         dentry = real_lookup(nd->dentry, name, nd);
693         if (IS_ERR(dentry))
694                 goto fail;
695         goto done;
696
697 need_revalidate:
698         if (dentry->d_op->d_revalidate(dentry, nd))
699                 goto done;
700         if (d_invalidate(dentry))
701                 goto done;
702         dput(dentry);
703         goto need_lookup;
704
705 fail:
706         return PTR_ERR(dentry);
707 }
708
709 /*
710  * Name resolution.
711  * This is the basic name resolution function, turning a pathname into
712  * the final dentry. We expect 'base' to be positive and a directory.
713  *
714  * Returns 0 and nd will have valid dentry and mnt on success.
715  * Returns error and drops reference to input namei data on failure.
716  */
717 static fastcall int __link_path_walk(const char * name, struct nameidata *nd)
718 {
719         struct path next;
720         struct inode *inode;
721         int err;
722         unsigned int lookup_flags = nd->flags;
723         
724         while (*name=='/')
725                 name++;
726         if (!*name)
727                 goto return_reval;
728
729         inode = nd->dentry->d_inode;
730         if (nd->depth)
731                 lookup_flags = LOOKUP_FOLLOW;
732
733         /* At this point we know we have a real path component. */
734         for(;;) {
735                 unsigned long hash;
736                 struct qstr this;
737                 unsigned int c;
738
739                 err = exec_permission_lite(inode, nd);
740                 if (err == -EAGAIN) { 
741                         err = permission(inode, MAY_EXEC, nd);
742                 }
743                 if (err)
744                         break;
745
746                 this.name = name;
747                 c = *(const unsigned char *)name;
748
749                 hash = init_name_hash();
750                 do {
751                         name++;
752                         hash = partial_name_hash(c, hash);
753                         c = *(const unsigned char *)name;
754                 } while (c && (c != '/'));
755                 this.len = name - (const char *) this.name;
756                 this.hash = end_name_hash(hash);
757
758                 /* remove trailing slashes? */
759                 if (!c)
760                         goto last_component;
761                 while (*++name == '/');
762                 if (!*name)
763                         goto last_with_slashes;
764
765                 /*
766                  * "." and ".." are special - ".." especially so because it has
767                  * to be able to know about the current root directory and
768                  * parent relationships.
769                  */
770                 if (this.name[0] == '.') switch (this.len) {
771                         default:
772                                 break;
773                         case 2: 
774                                 if (this.name[1] != '.')
775                                         break;
776                                 follow_dotdot(&nd->mnt, &nd->dentry);
777                                 inode = nd->dentry->d_inode;
778                                 /* fallthrough */
779                         case 1:
780                                 continue;
781                 }
782                 /*
783                  * See if the low-level filesystem might want
784                  * to use its own hash..
785                  */
786                 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
787                         err = nd->dentry->d_op->d_hash(nd->dentry, &this);
788                         if (err < 0)
789                                 break;
790                 }
791                 nd->flags |= LOOKUP_CONTINUE;
792                 /* This does the actual lookups.. */
793                 err = do_lookup(nd, &this, &next);
794                 if (err)
795                         break;
796                 /* Check mountpoints.. */
797                 __follow_mount(&next);
798
799                 err = -ENOENT;
800                 inode = next.dentry->d_inode;
801                 if (!inode)
802                         goto out_dput;
803                 err = -ENOTDIR; 
804                 if (!inode->i_op)
805                         goto out_dput;
806
807                 if (inode->i_op->follow_link) {
808                         err = do_follow_link(&next, nd);
809                         if (err)
810                                 goto return_err;
811                         err = -ENOENT;
812                         inode = nd->dentry->d_inode;
813                         if (!inode)
814                                 break;
815                         err = -ENOTDIR; 
816                         if (!inode->i_op)
817                                 break;
818                 } else {
819                         dput(nd->dentry);
820                         if (nd->mnt != next.mnt)
821                                 mntput(nd->mnt);
822                         nd->mnt = next.mnt;
823                         nd->dentry = next.dentry;
824                 }
825                 err = -ENOTDIR; 
826                 if (!inode->i_op->lookup)
827                         break;
828                 continue;
829                 /* here ends the main loop */
830
831 last_with_slashes:
832                 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
833 last_component:
834                 nd->flags &= ~LOOKUP_CONTINUE;
835                 if (lookup_flags & LOOKUP_PARENT)
836                         goto lookup_parent;
837                 if (this.name[0] == '.') switch (this.len) {
838                         default:
839                                 break;
840                         case 2: 
841                                 if (this.name[1] != '.')
842                                         break;
843                                 follow_dotdot(&nd->mnt, &nd->dentry);
844                                 inode = nd->dentry->d_inode;
845                                 /* fallthrough */
846                         case 1:
847                                 goto return_reval;
848                 }
849                 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
850                         err = nd->dentry->d_op->d_hash(nd->dentry, &this);
851                         if (err < 0)
852                                 break;
853                 }
854                 err = do_lookup(nd, &this, &next);
855                 if (err)
856                         break;
857                 __follow_mount(&next);
858                 inode = next.dentry->d_inode;
859                 if ((lookup_flags & LOOKUP_FOLLOW)
860                     && inode && inode->i_op && inode->i_op->follow_link) {
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                         goto exit_dput;
1506         }
1507         error = -ENOENT;
1508         if (!path.dentry->d_inode)
1509                 goto exit_dput;
1510         if (path.dentry->d_inode->i_op && path.dentry->d_inode->i_op->follow_link)
1511                 goto do_link;
1512
1513         dput(nd->dentry);
1514         nd->dentry = path.dentry;
1515         if (nd->mnt != path.mnt)
1516                 mntput(nd->mnt);
1517         nd->mnt = path.mnt;
1518         error = -EISDIR;
1519         if (path.dentry->d_inode && S_ISDIR(path.dentry->d_inode->i_mode))
1520                 goto exit;
1521 ok:
1522         error = may_open(nd, acc_mode, flag);
1523         if (error)
1524                 goto exit;
1525         return 0;
1526
1527 exit_dput:
1528         dput(path.dentry);
1529         if (nd->mnt != path.mnt)
1530                 mntput(path.mnt);
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         if (nd->mnt != path.mnt)
1554                 mntput(nd->mnt);
1555         nd->mnt = path.mnt;
1556         error = __do_follow_link(&path, nd);
1557         if (error)
1558                 return error;
1559         nd->flags &= ~LOOKUP_PARENT;
1560         if (nd->last_type == LAST_BIND)
1561                 goto ok;
1562         error = -EISDIR;
1563         if (nd->last_type != LAST_NORM)
1564                 goto exit;
1565         if (nd->last.name[nd->last.len]) {
1566                 putname(nd->last.name);
1567                 goto exit;
1568         }
1569         error = -ELOOP;
1570         if (count++==32) {
1571                 putname(nd->last.name);
1572                 goto exit;
1573         }
1574         dir = nd->dentry;
1575         down(&dir->d_inode->i_sem);
1576         path.dentry = __lookup_hash(&nd->last, nd->dentry, nd);
1577         path.mnt = nd->mnt;
1578         putname(nd->last.name);
1579         goto do_last;
1580 }
1581
1582 /**
1583  * lookup_create - lookup a dentry, creating it if it doesn't exist
1584  * @nd: nameidata info
1585  * @is_dir: directory flag
1586  *
1587  * Simple function to lookup and return a dentry and create it
1588  * if it doesn't exist.  Is SMP-safe.
1589  */
1590 struct dentry *lookup_create(struct nameidata *nd, int is_dir)
1591 {
1592         struct dentry *dentry;
1593
1594         down(&nd->dentry->d_inode->i_sem);
1595         dentry = ERR_PTR(-EEXIST);
1596         if (nd->last_type != LAST_NORM)
1597                 goto fail;
1598         nd->flags &= ~LOOKUP_PARENT;
1599         dentry = lookup_hash(&nd->last, nd->dentry);
1600         if (IS_ERR(dentry))
1601                 goto fail;
1602         if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode)
1603                 goto enoent;
1604         return dentry;
1605 enoent:
1606         dput(dentry);
1607         dentry = ERR_PTR(-ENOENT);
1608 fail:
1609         return dentry;
1610 }
1611 EXPORT_SYMBOL_GPL(lookup_create);
1612
1613 int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1614 {
1615         int error = may_create(dir, dentry, NULL);
1616
1617         if (error)
1618                 return error;
1619
1620         if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1621                 return -EPERM;
1622
1623         if (!dir->i_op || !dir->i_op->mknod)
1624                 return -EPERM;
1625
1626         error = security_inode_mknod(dir, dentry, mode, dev);
1627         if (error)
1628                 return error;
1629
1630         DQUOT_INIT(dir);
1631         error = dir->i_op->mknod(dir, dentry, mode, dev);
1632         if (!error) {
1633                 inode_dir_notify(dir, DN_CREATE);
1634                 security_inode_post_mknod(dir, dentry, mode, dev);
1635         }
1636         return error;
1637 }
1638
1639 asmlinkage long sys_mknod(const char __user * filename, int mode, unsigned dev)
1640 {
1641         int error = 0;
1642         char * tmp;
1643         struct dentry * dentry;
1644         struct nameidata nd;
1645
1646         if (S_ISDIR(mode))
1647                 return -EPERM;
1648         tmp = getname(filename);
1649         if (IS_ERR(tmp))
1650                 return PTR_ERR(tmp);
1651
1652         error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1653         if (error)
1654                 goto out;
1655         dentry = lookup_create(&nd, 0);
1656         error = PTR_ERR(dentry);
1657
1658         if (!IS_POSIXACL(nd.dentry->d_inode))
1659                 mode &= ~current->fs->umask;
1660         if (!IS_ERR(dentry)) {
1661                 switch (mode & S_IFMT) {
1662                 case 0: case S_IFREG:
1663                         error = vfs_create(nd.dentry->d_inode,dentry,mode,&nd);
1664                         break;
1665                 case S_IFCHR: case S_IFBLK:
1666                         error = vfs_mknod(nd.dentry->d_inode,dentry,mode,
1667                                         new_decode_dev(dev));
1668                         break;
1669                 case S_IFIFO: case S_IFSOCK:
1670                         error = vfs_mknod(nd.dentry->d_inode,dentry,mode,0);
1671                         break;
1672                 case S_IFDIR:
1673                         error = -EPERM;
1674                         break;
1675                 default:
1676                         error = -EINVAL;
1677                 }
1678                 dput(dentry);
1679         }
1680         up(&nd.dentry->d_inode->i_sem);
1681         path_release(&nd);
1682 out:
1683         putname(tmp);
1684
1685         return error;
1686 }
1687
1688 int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1689 {
1690         int error = may_create(dir, dentry, NULL);
1691
1692         if (error)
1693                 return error;
1694
1695         if (!dir->i_op || !dir->i_op->mkdir)
1696                 return -EPERM;
1697
1698         mode &= (S_IRWXUGO|S_ISVTX);
1699         error = security_inode_mkdir(dir, dentry, mode);
1700         if (error)
1701                 return error;
1702
1703         DQUOT_INIT(dir);
1704         error = dir->i_op->mkdir(dir, dentry, mode);
1705         if (!error) {
1706                 inode_dir_notify(dir, DN_CREATE);
1707                 security_inode_post_mkdir(dir,dentry, mode);
1708         }
1709         return error;
1710 }
1711
1712 asmlinkage long sys_mkdir(const char __user * pathname, int mode)
1713 {
1714         int error = 0;
1715         char * tmp;
1716
1717         tmp = getname(pathname);
1718         error = PTR_ERR(tmp);
1719         if (!IS_ERR(tmp)) {
1720                 struct dentry *dentry;
1721                 struct nameidata nd;
1722
1723                 error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1724                 if (error)
1725                         goto out;
1726                 dentry = lookup_create(&nd, 1);
1727                 error = PTR_ERR(dentry);
1728                 if (!IS_ERR(dentry)) {
1729                         if (!IS_POSIXACL(nd.dentry->d_inode))
1730                                 mode &= ~current->fs->umask;
1731                         error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
1732                         dput(dentry);
1733                 }
1734                 up(&nd.dentry->d_inode->i_sem);
1735                 path_release(&nd);
1736 out:
1737                 putname(tmp);
1738         }
1739
1740         return error;
1741 }
1742
1743 /*
1744  * We try to drop the dentry early: we should have
1745  * a usage count of 2 if we're the only user of this
1746  * dentry, and if that is true (possibly after pruning
1747  * the dcache), then we drop the dentry now.
1748  *
1749  * A low-level filesystem can, if it choses, legally
1750  * do a
1751  *
1752  *      if (!d_unhashed(dentry))
1753  *              return -EBUSY;
1754  *
1755  * if it cannot handle the case of removing a directory
1756  * that is still in use by something else..
1757  */
1758 void dentry_unhash(struct dentry *dentry)
1759 {
1760         dget(dentry);
1761         if (atomic_read(&dentry->d_count))
1762                 shrink_dcache_parent(dentry);
1763         spin_lock(&dcache_lock);
1764         spin_lock(&dentry->d_lock);
1765         if (atomic_read(&dentry->d_count) == 2)
1766                 __d_drop(dentry);
1767         spin_unlock(&dentry->d_lock);
1768         spin_unlock(&dcache_lock);
1769 }
1770
1771 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
1772 {
1773         int error = may_delete(dir, dentry, 1);
1774
1775         if (error)
1776                 return error;
1777
1778         if (!dir->i_op || !dir->i_op->rmdir)
1779                 return -EPERM;
1780
1781         DQUOT_INIT(dir);
1782
1783         down(&dentry->d_inode->i_sem);
1784         dentry_unhash(dentry);
1785         if (d_mountpoint(dentry))
1786                 error = -EBUSY;
1787         else {
1788                 error = security_inode_rmdir(dir, dentry);
1789                 if (!error) {
1790                         error = dir->i_op->rmdir(dir, dentry);
1791                         if (!error)
1792                                 dentry->d_inode->i_flags |= S_DEAD;
1793                 }
1794         }
1795         up(&dentry->d_inode->i_sem);
1796         if (!error) {
1797                 inode_dir_notify(dir, DN_DELETE);
1798                 d_delete(dentry);
1799         }
1800         dput(dentry);
1801
1802         return error;
1803 }
1804
1805 asmlinkage long sys_rmdir(const char __user * pathname)
1806 {
1807         int error = 0;
1808         char * name;
1809         struct dentry *dentry;
1810         struct nameidata nd;
1811
1812         name = getname(pathname);
1813         if(IS_ERR(name))
1814                 return PTR_ERR(name);
1815
1816         error = path_lookup(name, LOOKUP_PARENT, &nd);
1817         if (error)
1818                 goto exit;
1819
1820         switch(nd.last_type) {
1821                 case LAST_DOTDOT:
1822                         error = -ENOTEMPTY;
1823                         goto exit1;
1824                 case LAST_DOT:
1825                         error = -EINVAL;
1826                         goto exit1;
1827                 case LAST_ROOT:
1828                         error = -EBUSY;
1829                         goto exit1;
1830         }
1831         down(&nd.dentry->d_inode->i_sem);
1832         dentry = lookup_hash(&nd.last, nd.dentry);
1833         error = PTR_ERR(dentry);
1834         if (!IS_ERR(dentry)) {
1835                 error = vfs_rmdir(nd.dentry->d_inode, dentry);
1836                 dput(dentry);
1837         }
1838         up(&nd.dentry->d_inode->i_sem);
1839 exit1:
1840         path_release(&nd);
1841 exit:
1842         putname(name);
1843         return error;
1844 }
1845
1846 int vfs_unlink(struct inode *dir, struct dentry *dentry)
1847 {
1848         int error = may_delete(dir, dentry, 0);
1849
1850         if (error)
1851                 return error;
1852
1853         if (!dir->i_op || !dir->i_op->unlink)
1854                 return -EPERM;
1855
1856         DQUOT_INIT(dir);
1857
1858         down(&dentry->d_inode->i_sem);
1859         if (d_mountpoint(dentry))
1860                 error = -EBUSY;
1861         else {
1862                 error = security_inode_unlink(dir, dentry);
1863                 if (!error)
1864                         error = dir->i_op->unlink(dir, dentry);
1865         }
1866         up(&dentry->d_inode->i_sem);
1867
1868         /* We don't d_delete() NFS sillyrenamed files--they still exist. */
1869         if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
1870                 d_delete(dentry);
1871                 inode_dir_notify(dir, DN_DELETE);
1872         }
1873         return error;
1874 }
1875
1876 /*
1877  * Make sure that the actual truncation of the file will occur outside its
1878  * directory's i_sem.  Truncate can take a long time if there is a lot of
1879  * writeout happening, and we don't want to prevent access to the directory
1880  * while waiting on the I/O.
1881  */
1882 asmlinkage long sys_unlink(const char __user * pathname)
1883 {
1884         int error = 0;
1885         char * name;
1886         struct dentry *dentry;
1887         struct nameidata nd;
1888         struct inode *inode = NULL;
1889
1890         name = getname(pathname);
1891         if(IS_ERR(name))
1892                 return PTR_ERR(name);
1893
1894         error = path_lookup(name, LOOKUP_PARENT, &nd);
1895         if (error)
1896                 goto exit;
1897         error = -EISDIR;
1898         if (nd.last_type != LAST_NORM)
1899                 goto exit1;
1900         down(&nd.dentry->d_inode->i_sem);
1901         dentry = lookup_hash(&nd.last, nd.dentry);
1902         error = PTR_ERR(dentry);
1903         if (!IS_ERR(dentry)) {
1904                 /* Why not before? Because we want correct error value */
1905                 if (nd.last.name[nd.last.len])
1906                         goto slashes;
1907                 inode = dentry->d_inode;
1908                 if (inode)
1909                         atomic_inc(&inode->i_count);
1910                 error = vfs_unlink(nd.dentry->d_inode, dentry);
1911         exit2:
1912                 dput(dentry);
1913         }
1914         up(&nd.dentry->d_inode->i_sem);
1915         if (inode)
1916                 iput(inode);    /* truncate the inode here */
1917 exit1:
1918         path_release(&nd);
1919 exit:
1920         putname(name);
1921         return error;
1922
1923 slashes:
1924         error = !dentry->d_inode ? -ENOENT :
1925                 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
1926         goto exit2;
1927 }
1928
1929 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname, int mode)
1930 {
1931         int error = may_create(dir, dentry, NULL);
1932
1933         if (error)
1934                 return error;
1935
1936         if (!dir->i_op || !dir->i_op->symlink)
1937                 return -EPERM;
1938
1939         error = security_inode_symlink(dir, dentry, oldname);
1940         if (error)
1941                 return error;
1942
1943         DQUOT_INIT(dir);
1944         error = dir->i_op->symlink(dir, dentry, oldname);
1945         if (!error) {
1946                 inode_dir_notify(dir, DN_CREATE);
1947                 security_inode_post_symlink(dir, dentry, oldname);
1948         }
1949         return error;
1950 }
1951
1952 asmlinkage long sys_symlink(const char __user * oldname, const char __user * newname)
1953 {
1954         int error = 0;
1955         char * from;
1956         char * to;
1957
1958         from = getname(oldname);
1959         if(IS_ERR(from))
1960                 return PTR_ERR(from);
1961         to = getname(newname);
1962         error = PTR_ERR(to);
1963         if (!IS_ERR(to)) {
1964                 struct dentry *dentry;
1965                 struct nameidata nd;
1966
1967                 error = path_lookup(to, LOOKUP_PARENT, &nd);
1968                 if (error)
1969                         goto out;
1970                 dentry = lookup_create(&nd, 0);
1971                 error = PTR_ERR(dentry);
1972                 if (!IS_ERR(dentry)) {
1973                         error = vfs_symlink(nd.dentry->d_inode, dentry, from, S_IALLUGO);
1974                         dput(dentry);
1975                 }
1976                 up(&nd.dentry->d_inode->i_sem);
1977                 path_release(&nd);
1978 out:
1979                 putname(to);
1980         }
1981         putname(from);
1982         return error;
1983 }
1984
1985 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
1986 {
1987         struct inode *inode = old_dentry->d_inode;
1988         int error;
1989
1990         if (!inode)
1991                 return -ENOENT;
1992
1993         error = may_create(dir, new_dentry, NULL);
1994         if (error)
1995                 return error;
1996
1997         if (dir->i_sb != inode->i_sb)
1998                 return -EXDEV;
1999
2000         /*
2001          * A link to an append-only or immutable file cannot be created.
2002          */
2003         if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2004                 return -EPERM;
2005         if (!dir->i_op || !dir->i_op->link)
2006                 return -EPERM;
2007         if (S_ISDIR(old_dentry->d_inode->i_mode))
2008                 return -EPERM;
2009
2010         error = security_inode_link(old_dentry, dir, new_dentry);
2011         if (error)
2012                 return error;
2013
2014         down(&old_dentry->d_inode->i_sem);
2015         DQUOT_INIT(dir);
2016         error = dir->i_op->link(old_dentry, dir, new_dentry);
2017         up(&old_dentry->d_inode->i_sem);
2018         if (!error) {
2019                 inode_dir_notify(dir, DN_CREATE);
2020                 security_inode_post_link(old_dentry, dir, new_dentry);
2021         }
2022         return error;
2023 }
2024
2025 /*
2026  * Hardlinks are often used in delicate situations.  We avoid
2027  * security-related surprises by not following symlinks on the
2028  * newname.  --KAB
2029  *
2030  * We don't follow them on the oldname either to be compatible
2031  * with linux 2.0, and to avoid hard-linking to directories
2032  * and other special files.  --ADM
2033  */
2034 asmlinkage long sys_link(const char __user * oldname, const char __user * newname)
2035 {
2036         struct dentry *new_dentry;
2037         struct nameidata nd, old_nd;
2038         int error;
2039         char * to;
2040
2041         to = getname(newname);
2042         if (IS_ERR(to))
2043                 return PTR_ERR(to);
2044
2045         error = __user_walk(oldname, 0, &old_nd);
2046         if (error)
2047                 goto exit;
2048         error = path_lookup(to, LOOKUP_PARENT, &nd);
2049         if (error)
2050                 goto out;
2051         error = -EXDEV;
2052         if (old_nd.mnt != nd.mnt)
2053                 goto out_release;
2054         new_dentry = lookup_create(&nd, 0);
2055         error = PTR_ERR(new_dentry);
2056         if (!IS_ERR(new_dentry)) {
2057                 error = vfs_link(old_nd.dentry, nd.dentry->d_inode, new_dentry);
2058                 dput(new_dentry);
2059         }
2060         up(&nd.dentry->d_inode->i_sem);
2061 out_release:
2062         path_release(&nd);
2063 out:
2064         path_release(&old_nd);
2065 exit:
2066         putname(to);
2067
2068         return error;
2069 }
2070
2071 /*
2072  * The worst of all namespace operations - renaming directory. "Perverted"
2073  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2074  * Problems:
2075  *      a) we can get into loop creation. Check is done in is_subdir().
2076  *      b) race potential - two innocent renames can create a loop together.
2077  *         That's where 4.4 screws up. Current fix: serialization on
2078  *         sb->s_vfs_rename_sem. We might be more accurate, but that's another
2079  *         story.
2080  *      c) we have to lock _three_ objects - parents and victim (if it exists).
2081  *         And that - after we got ->i_sem on parents (until then we don't know
2082  *         whether the target exists).  Solution: try to be smart with locking
2083  *         order for inodes.  We rely on the fact that tree topology may change
2084  *         only under ->s_vfs_rename_sem _and_ that parent of the object we
2085  *         move will be locked.  Thus we can rank directories by the tree
2086  *         (ancestors first) and rank all non-directories after them.
2087  *         That works since everybody except rename does "lock parent, lookup,
2088  *         lock child" and rename is under ->s_vfs_rename_sem.
2089  *         HOWEVER, it relies on the assumption that any object with ->lookup()
2090  *         has no more than 1 dentry.  If "hybrid" objects will ever appear,
2091  *         we'd better make sure that there's no link(2) for them.
2092  *      d) some filesystems don't support opened-but-unlinked directories,
2093  *         either because of layout or because they are not ready to deal with
2094  *         all cases correctly. The latter will be fixed (taking this sort of
2095  *         stuff into VFS), but the former is not going away. Solution: the same
2096  *         trick as in rmdir().
2097  *      e) conversion from fhandle to dentry may come in the wrong moment - when
2098  *         we are removing the target. Solution: we will have to grab ->i_sem
2099  *         in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
2100  *         ->i_sem on parents, which works but leads to some truely excessive
2101  *         locking].
2102  */
2103 static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
2104                           struct inode *new_dir, struct dentry *new_dentry)
2105 {
2106         int error = 0;
2107         struct inode *target;
2108
2109         /*
2110          * If we are going to change the parent - check write permissions,
2111          * we'll need to flip '..'.
2112          */
2113         if (new_dir != old_dir) {
2114                 error = permission(old_dentry->d_inode, MAY_WRITE, NULL);
2115                 if (error)
2116                         return error;
2117         }
2118
2119         error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2120         if (error)
2121                 return error;
2122
2123         target = new_dentry->d_inode;
2124         if (target) {
2125                 down(&target->i_sem);
2126                 dentry_unhash(new_dentry);
2127         }
2128         if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2129                 error = -EBUSY;
2130         else 
2131                 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2132         if (target) {
2133                 if (!error)
2134                         target->i_flags |= S_DEAD;
2135                 up(&target->i_sem);
2136                 if (d_unhashed(new_dentry))
2137                         d_rehash(new_dentry);
2138                 dput(new_dentry);
2139         }
2140         if (!error) {
2141                 d_move(old_dentry,new_dentry);
2142                 security_inode_post_rename(old_dir, old_dentry,
2143                                            new_dir, new_dentry);
2144         }
2145         return error;
2146 }
2147
2148 static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
2149                             struct inode *new_dir, struct dentry *new_dentry)
2150 {
2151         struct inode *target;
2152         int error;
2153
2154         error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2155         if (error)
2156                 return error;
2157
2158         dget(new_dentry);
2159         target = new_dentry->d_inode;
2160         if (target)
2161                 down(&target->i_sem);
2162         if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2163                 error = -EBUSY;
2164         else
2165                 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2166         if (!error) {
2167                 /* The following d_move() should become unconditional */
2168                 if (!(old_dir->i_sb->s_type->fs_flags & FS_ODD_RENAME))
2169                         d_move(old_dentry, new_dentry);
2170                 security_inode_post_rename(old_dir, old_dentry, new_dir, new_dentry);
2171         }
2172         if (target)
2173                 up(&target->i_sem);
2174         dput(new_dentry);
2175         return error;
2176 }
2177
2178 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
2179                struct inode *new_dir, struct dentry *new_dentry)
2180 {
2181         int error;
2182         int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
2183
2184         if (old_dentry->d_inode == new_dentry->d_inode)
2185                 return 0;
2186  
2187         error = may_delete(old_dir, old_dentry, is_dir);
2188         if (error)
2189                 return error;
2190
2191         if (!new_dentry->d_inode)
2192                 error = may_create(new_dir, new_dentry, NULL);
2193         else
2194                 error = may_delete(new_dir, new_dentry, is_dir);
2195         if (error)
2196                 return error;
2197
2198         if (!old_dir->i_op || !old_dir->i_op->rename)
2199                 return -EPERM;
2200
2201         DQUOT_INIT(old_dir);
2202         DQUOT_INIT(new_dir);
2203
2204         if (is_dir)
2205                 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
2206         else
2207                 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
2208         if (!error) {
2209                 if (old_dir == new_dir)
2210                         inode_dir_notify(old_dir, DN_RENAME);
2211                 else {
2212                         inode_dir_notify(old_dir, DN_DELETE);
2213                         inode_dir_notify(new_dir, DN_CREATE);
2214                 }
2215         }
2216         return error;
2217 }
2218
2219 static inline int do_rename(const char * oldname, const char * newname)
2220 {
2221         int error = 0;
2222         struct dentry * old_dir, * new_dir;
2223         struct dentry * old_dentry, *new_dentry;
2224         struct dentry * trap;
2225         struct nameidata oldnd, newnd;
2226
2227         error = path_lookup(oldname, LOOKUP_PARENT, &oldnd);
2228         if (error)
2229                 goto exit;
2230
2231         error = path_lookup(newname, LOOKUP_PARENT, &newnd);
2232         if (error)
2233                 goto exit1;
2234
2235         error = -EXDEV;
2236         if (oldnd.mnt != newnd.mnt)
2237                 goto exit2;
2238
2239         old_dir = oldnd.dentry;
2240         error = -EBUSY;
2241         if (oldnd.last_type != LAST_NORM)
2242                 goto exit2;
2243
2244         new_dir = newnd.dentry;
2245         if (newnd.last_type != LAST_NORM)
2246                 goto exit2;
2247
2248         trap = lock_rename(new_dir, old_dir);
2249
2250         old_dentry = lookup_hash(&oldnd.last, old_dir);
2251         error = PTR_ERR(old_dentry);
2252         if (IS_ERR(old_dentry))
2253                 goto exit3;
2254         /* source must exist */
2255         error = -ENOENT;
2256         if (!old_dentry->d_inode)
2257                 goto exit4;
2258         /* unless the source is a directory trailing slashes give -ENOTDIR */
2259         if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
2260                 error = -ENOTDIR;
2261                 if (oldnd.last.name[oldnd.last.len])
2262                         goto exit4;
2263                 if (newnd.last.name[newnd.last.len])
2264                         goto exit4;
2265         }
2266         /* source should not be ancestor of target */
2267         error = -EINVAL;
2268         if (old_dentry == trap)
2269                 goto exit4;
2270         new_dentry = lookup_hash(&newnd.last, new_dir);
2271         error = PTR_ERR(new_dentry);
2272         if (IS_ERR(new_dentry))
2273                 goto exit4;
2274         /* target should not be an ancestor of source */
2275         error = -ENOTEMPTY;
2276         if (new_dentry == trap)
2277                 goto exit5;
2278
2279         error = vfs_rename(old_dir->d_inode, old_dentry,
2280                                    new_dir->d_inode, new_dentry);
2281 exit5:
2282         dput(new_dentry);
2283 exit4:
2284         dput(old_dentry);
2285 exit3:
2286         unlock_rename(new_dir, old_dir);
2287 exit2:
2288         path_release(&newnd);
2289 exit1:
2290         path_release(&oldnd);
2291 exit:
2292         return error;
2293 }
2294
2295 asmlinkage long sys_rename(const char __user * oldname, const char __user * newname)
2296 {
2297         int error;
2298         char * from;
2299         char * to;
2300
2301         from = getname(oldname);
2302         if(IS_ERR(from))
2303                 return PTR_ERR(from);
2304         to = getname(newname);
2305         error = PTR_ERR(to);
2306         if (!IS_ERR(to)) {
2307                 error = do_rename(from,to);
2308                 putname(to);
2309         }
2310         putname(from);
2311         return error;
2312 }
2313
2314 int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
2315 {
2316         int len;
2317
2318         len = PTR_ERR(link);
2319         if (IS_ERR(link))
2320                 goto out;
2321
2322         len = strlen(link);
2323         if (len > (unsigned) buflen)
2324                 len = buflen;
2325         if (copy_to_user(buffer, link, len))
2326                 len = -EFAULT;
2327 out:
2328         return len;
2329 }
2330
2331 /*
2332  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
2333  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
2334  * using) it for any given inode is up to filesystem.
2335  */
2336 int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2337 {
2338         struct nameidata nd;
2339         int res;
2340         nd.depth = 0;
2341         res = dentry->d_inode->i_op->follow_link(dentry, &nd);
2342         if (!res) {
2343                 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
2344                 if (dentry->d_inode->i_op->put_link)
2345                         dentry->d_inode->i_op->put_link(dentry, &nd);
2346         }
2347         return res;
2348 }
2349
2350 int vfs_follow_link(struct nameidata *nd, const char *link)
2351 {
2352         return __vfs_follow_link(nd, link);
2353 }
2354
2355 /* get the link contents into pagecache */
2356 static char *page_getlink(struct dentry * dentry, struct page **ppage)
2357 {
2358         struct page * page;
2359         struct address_space *mapping = dentry->d_inode->i_mapping;
2360         page = read_cache_page(mapping, 0, (filler_t *)mapping->a_ops->readpage,
2361                                 NULL);
2362         if (IS_ERR(page))
2363                 goto sync_fail;
2364         wait_on_page_locked(page);
2365         if (!PageUptodate(page))
2366                 goto async_fail;
2367         *ppage = page;
2368         return kmap(page);
2369
2370 async_fail:
2371         page_cache_release(page);
2372         return ERR_PTR(-EIO);
2373
2374 sync_fail:
2375         return (char*)page;
2376 }
2377
2378 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2379 {
2380         struct page *page = NULL;
2381         char *s = page_getlink(dentry, &page);
2382         int res = vfs_readlink(dentry,buffer,buflen,s);
2383         if (page) {
2384                 kunmap(page);
2385                 page_cache_release(page);
2386         }
2387         return res;
2388 }
2389
2390 int page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
2391 {
2392         struct page *page;
2393         nd_set_link(nd, page_getlink(dentry, &page));
2394         return 0;
2395 }
2396
2397 void page_put_link(struct dentry *dentry, struct nameidata *nd)
2398 {
2399         if (!IS_ERR(nd_get_link(nd))) {
2400                 struct page *page;
2401                 page = find_get_page(dentry->d_inode->i_mapping, 0);
2402                 if (!page)
2403                         BUG();
2404                 kunmap(page);
2405                 page_cache_release(page);
2406                 page_cache_release(page);
2407         }
2408 }
2409
2410 int page_symlink(struct inode *inode, const char *symname, int len)
2411 {
2412         struct address_space *mapping = inode->i_mapping;
2413         struct page *page = grab_cache_page(mapping, 0);
2414         int err = -ENOMEM;
2415         char *kaddr;
2416
2417         if (!page)
2418                 goto fail;
2419         err = mapping->a_ops->prepare_write(NULL, page, 0, len-1);
2420         if (err)
2421                 goto fail_map;
2422         kaddr = kmap_atomic(page, KM_USER0);
2423         memcpy(kaddr, symname, len-1);
2424         kunmap_atomic(kaddr, KM_USER0);
2425         mapping->a_ops->commit_write(NULL, page, 0, len-1);
2426         /*
2427          * Notice that we are _not_ going to block here - end of page is
2428          * unmapped, so this will only try to map the rest of page, see
2429          * that it is unmapped (typically even will not look into inode -
2430          * ->i_size will be enough for everything) and zero it out.
2431          * OTOH it's obviously correct and should make the page up-to-date.
2432          */
2433         if (!PageUptodate(page)) {
2434                 err = mapping->a_ops->readpage(NULL, page);
2435                 wait_on_page_locked(page);
2436         } else {
2437                 unlock_page(page);
2438         }
2439         page_cache_release(page);
2440         if (err < 0)
2441                 goto fail;
2442         mark_inode_dirty(inode);
2443         return 0;
2444 fail_map:
2445         unlock_page(page);
2446         page_cache_release(page);
2447 fail:
2448         return err;
2449 }
2450
2451 struct inode_operations page_symlink_inode_operations = {
2452         .readlink       = generic_readlink,
2453         .follow_link    = page_follow_link_light,
2454         .put_link       = page_put_link,
2455 };
2456
2457 EXPORT_SYMBOL(__user_walk);
2458 EXPORT_SYMBOL(follow_down);
2459 EXPORT_SYMBOL(follow_up);
2460 EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
2461 EXPORT_SYMBOL(getname);
2462 EXPORT_SYMBOL(lock_rename);
2463 EXPORT_SYMBOL(lookup_hash);
2464 EXPORT_SYMBOL(lookup_one_len);
2465 EXPORT_SYMBOL(page_follow_link_light);
2466 EXPORT_SYMBOL(page_put_link);
2467 EXPORT_SYMBOL(page_readlink);
2468 EXPORT_SYMBOL(page_symlink);
2469 EXPORT_SYMBOL(page_symlink_inode_operations);
2470 EXPORT_SYMBOL(path_lookup);
2471 EXPORT_SYMBOL(path_release);
2472 EXPORT_SYMBOL(path_walk);
2473 EXPORT_SYMBOL(permission);
2474 EXPORT_SYMBOL(unlock_rename);
2475 EXPORT_SYMBOL(vfs_create);
2476 EXPORT_SYMBOL(vfs_follow_link);
2477 EXPORT_SYMBOL(vfs_link);
2478 EXPORT_SYMBOL(vfs_mkdir);
2479 EXPORT_SYMBOL(vfs_mknod);
2480 EXPORT_SYMBOL(generic_permission);
2481 EXPORT_SYMBOL(vfs_readlink);
2482 EXPORT_SYMBOL(vfs_rename);
2483 EXPORT_SYMBOL(vfs_rmdir);
2484 EXPORT_SYMBOL(vfs_symlink);
2485 EXPORT_SYMBOL(vfs_unlink);
2486 EXPORT_SYMBOL(dentry_unhash);
2487 EXPORT_SYMBOL(generic_readlink);