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