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