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