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