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