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