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