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