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