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