4 * Copyright (C) 1991, 1992 Linus Torvalds
8 * Some corrections by tytso.
11 /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
14 /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
21 #include <linux/namei.h>
22 #include <linux/quotaops.h>
23 #include <linux/pagemap.h>
24 #include <linux/dnotify.h>
25 #include <linux/smp_lock.h>
26 #include <linux/personality.h>
27 #include <linux/security.h>
28 #include <linux/mount.h>
29 #include <linux/audit.h>
30 #include <asm/namei.h>
31 #include <asm/uaccess.h>
33 #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
35 /* [Feb-1997 T. Schoebel-Theuer]
36 * Fundamental changes in the pathname lookup mechanisms (namei)
37 * were necessary because of omirr. The reason is that omirr needs
38 * to know the _real_ pathname, not the user-supplied one, in case
39 * of symlinks (and also when transname replacements occur).
41 * The new code replaces the old recursive symlink resolution with
42 * an iterative one (in case of non-nested symlink chains). It does
43 * this with calls to <fs>_follow_link().
44 * As a side effect, dir_namei(), _namei() and follow_link() are now
45 * replaced with a single function lookup_dentry() that can handle all
46 * the special cases of the former code.
48 * With the new dcache, the pathname is stored at each inode, at least as
49 * long as the refcount of the inode is positive. As a side effect, the
50 * size of the dcache depends on the inode cache and thus is dynamic.
52 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
53 * resolution to correspond with current state of the code.
55 * Note that the symlink resolution is not *completely* iterative.
56 * There is still a significant amount of tail- and mid- recursion in
57 * the algorithm. Also, note that <fs>_readlink() is not used in
58 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
59 * may return different results than <fs>_follow_link(). Many virtual
60 * filesystems (including /proc) exhibit this behavior.
63 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
64 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
65 * and the name already exists in form of a symlink, try to create the new
66 * name indicated by the symlink. The old code always complained that the
67 * name already exists, due to not following the symlink even if its target
68 * is nonexistent. The new semantics affects also mknod() and link() when
69 * the name is a symlink pointing to a non-existant name.
71 * I don't know which semantics is the right one, since I have no access
72 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
73 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
74 * "old" one. Personally, I think the new semantics is much more logical.
75 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
76 * file does succeed in both HP-UX and SunOs, but not in Solaris
77 * and in the old Linux semantics.
80 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
81 * semantics. See the comments in "open_namei" and "do_link" below.
83 * [10-Sep-98 Alan Modra] Another symlink change.
86 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
87 * inside the path - always follow.
88 * in the last component in creation/removal/renaming - never follow.
89 * if LOOKUP_FOLLOW passed - follow.
90 * if the pathname has trailing slashes - follow.
91 * otherwise - don't follow.
92 * (applied in that order).
94 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
95 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
96 * During the 2.4 we need to fix the userland stuff depending on it -
97 * hopefully we will be able to get rid of that wart in 2.5. So far only
98 * XEmacs seems to be relying on it...
101 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
102 * implemented. Let's see if raised priority of ->s_vfs_rename_sem gives
103 * any extra contention...
106 /* In order to reduce some races, while at the same time doing additional
107 * checking and hopefully speeding things up, we copy filenames to the
108 * kernel data space before using them..
110 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
111 * PATH_MAX includes the nul terminator --RR.
113 static inline int do_getname(const char __user *filename, char *page)
116 unsigned long len = PATH_MAX;
118 if ((unsigned long) filename >= TASK_SIZE) {
119 if (!segment_eq(get_fs(), KERNEL_DS))
121 } else if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
122 len = TASK_SIZE - (unsigned long) filename;
124 retval = strncpy_from_user((char *)page, filename, len);
128 return -ENAMETOOLONG;
134 char * getname(const char __user * filename)
138 result = ERR_PTR(-ENOMEM);
141 int retval = do_getname(filename, tmp);
146 result = ERR_PTR(retval);
149 if (unlikely(current->audit_context) && !IS_ERR(result) && result)
150 audit_getname(result);
157 * is used to check for read/write/execute permissions on a file.
158 * We use "fsuid" for this, letting us set arbitrary permissions
159 * for filesystem access without changing the "normal" uids which
160 * are used for other things..
162 int vfs_permission(struct inode * inode, int mask)
164 umode_t mode = inode->i_mode;
166 if (IS_BARRIER(inode) && !vx_check(0, VX_ADMIN|VX_WATCH))
169 if (mask & MAY_WRITE) {
171 * Nobody gets write access to a read-only fs.
173 if (IS_RDONLY(inode) &&
174 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
178 * Nobody gets write access to an immutable file.
180 if (IS_IMMUTABLE(inode))
184 if (current->fsuid == inode->i_uid)
186 else if (in_group_p(inode->i_gid))
190 * If the DACs are ok we don't need any capability check.
192 if (((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask))
196 * Read/write DACs are always overridable.
197 * Executable DACs are overridable if at least one exec bit is set.
199 if (!(mask & MAY_EXEC) ||
200 (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
201 if (capable(CAP_DAC_OVERRIDE))
205 * Searching includes executable on directories, else just read.
207 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
208 if (capable(CAP_DAC_READ_SEARCH))
214 static inline int xid_permission(struct inode *inode)
216 if (inode->i_xid == 0)
218 if (vx_check(inode->i_xid, VX_ADMIN|VX_WATCH|VX_IDENT))
223 int permission(struct inode * inode,int mask, struct nameidata *nd)
228 /* Ordinary permission routines do not understand MAY_APPEND. */
229 submask = mask & ~MAY_APPEND;
231 if ((retval = xid_permission(inode)))
233 if (inode->i_op && inode->i_op->permission)
234 retval = inode->i_op->permission(inode, submask, nd);
236 retval = vfs_permission(inode, submask);
240 return security_inode_permission(inode, mask, nd);
244 * get_write_access() gets write permission for a file.
245 * put_write_access() releases this write permission.
246 * This is used for regular files.
247 * We cannot support write (and maybe mmap read-write shared) accesses and
248 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
249 * can have the following values:
250 * 0: no writers, no VM_DENYWRITE mappings
251 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
252 * > 0: (i_writecount) users are writing to the file.
254 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
255 * except for the cases where we don't hold i_writecount yet. Then we need to
256 * use {get,deny}_write_access() - these functions check the sign and refuse
257 * to do the change if sign is wrong. Exclusion between them is provided by
258 * the inode->i_lock spinlock.
261 int get_write_access(struct inode * inode)
263 spin_lock(&inode->i_lock);
264 if (atomic_read(&inode->i_writecount) < 0) {
265 spin_unlock(&inode->i_lock);
268 atomic_inc(&inode->i_writecount);
269 spin_unlock(&inode->i_lock);
274 int deny_write_access(struct file * file)
276 struct inode *inode = file->f_dentry->d_inode;
278 spin_lock(&inode->i_lock);
279 if (atomic_read(&inode->i_writecount) > 0) {
280 spin_unlock(&inode->i_lock);
283 atomic_dec(&inode->i_writecount);
284 spin_unlock(&inode->i_lock);
289 void path_release(struct nameidata *nd)
296 * Internal lookup() using the new generic dcache.
299 static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
301 struct dentry * dentry = __d_lookup(parent, name);
303 /* lockess __d_lookup may fail due to concurrent d_move()
304 * in some unrelated directory, so try with d_lookup
307 dentry = d_lookup(parent, name);
309 if (dentry && dentry->d_op && dentry->d_op->d_revalidate) {
310 if (!dentry->d_op->d_revalidate(dentry, nd) && !d_invalidate(dentry)) {
319 * Short-cut version of permission(), for calling by
320 * path_walk(), when dcache lock is held. Combines parts
321 * of permission() and vfs_permission(), and tests ONLY for
322 * MAY_EXEC permission.
324 * If appropriate, check DAC only. If not appropriate, or
325 * short-cut DAC fails, then call permission() to do more
326 * complete permission check.
328 static inline int exec_permission_lite(struct inode *inode,
329 struct nameidata *nd)
331 umode_t mode = inode->i_mode;
333 if ((inode->i_op && inode->i_op->permission))
336 if (current->fsuid == inode->i_uid)
338 else if (in_group_p(inode->i_gid))
344 if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE))
347 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH))
352 return security_inode_permission(inode, MAY_EXEC, nd);
356 * This is called when everything else fails, and we actually have
357 * to go to the low-level filesystem to find out what we should do..
359 * We get the directory semaphore, and after getting that we also
360 * make sure that nobody added the entry to the dcache in the meantime..
363 static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
365 struct dentry * result;
366 struct inode *dir = parent->d_inode;
370 * First re-do the cached lookup just in case it was created
371 * while we waited for the directory semaphore..
373 * FIXME! This could use version numbering or similar to
374 * avoid unnecessary cache lookups.
376 * The "dcache_lock" is purely to protect the RCU list walker
377 * from concurrent renames at this point (we mustn't get false
378 * negatives from the RCU list walk here, unlike the optimistic
381 * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
383 result = d_lookup(parent, name);
385 struct dentry * dentry = d_alloc(parent, name);
386 result = ERR_PTR(-ENOMEM);
388 result = dir->i_op->lookup(dir, dentry, nd);
399 * Uhhuh! Nasty case: the cache was re-populated while
400 * we waited on the semaphore. Need to revalidate.
403 if (result->d_op && result->d_op->d_revalidate) {
404 if (!result->d_op->d_revalidate(result, nd) && !d_invalidate(result)) {
406 result = ERR_PTR(-ENOENT);
413 * This limits recursive symlink follows to 8, while
414 * limiting consecutive symlinks to 40.
416 * Without that kind of total limit, nasty chains of consecutive
417 * symlinks can cause almost arbitrarily long lookups.
419 static inline int do_follow_link(struct dentry *dentry, struct nameidata *nd)
422 if (current->link_count >= 5)
424 if (current->total_link_count >= 40)
427 err = security_inode_follow_link(dentry, nd);
430 current->link_count++;
431 current->total_link_count++;
432 touch_atime(nd->mnt, dentry);
433 err = dentry->d_inode->i_op->follow_link(dentry, nd);
434 current->link_count--;
441 int follow_up(struct vfsmount **mnt, struct dentry **dentry)
443 struct vfsmount *parent;
444 struct dentry *mountpoint;
445 spin_lock(&vfsmount_lock);
446 parent=(*mnt)->mnt_parent;
447 if (parent == *mnt) {
448 spin_unlock(&vfsmount_lock);
452 mountpoint=dget((*mnt)->mnt_mountpoint);
453 spin_unlock(&vfsmount_lock);
455 *dentry = mountpoint;
461 /* no need for dcache_lock, as serialization is taken care in
464 static int follow_mount(struct vfsmount **mnt, struct dentry **dentry)
467 while (d_mountpoint(*dentry)) {
468 struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
474 *dentry = dget(mounted->mnt_root);
480 /* no need for dcache_lock, as serialization is taken care in
483 static inline int __follow_down(struct vfsmount **mnt, struct dentry **dentry)
485 struct vfsmount *mounted;
487 mounted = lookup_mnt(*mnt, *dentry);
492 *dentry = dget(mounted->mnt_root);
498 int follow_down(struct vfsmount **mnt, struct dentry **dentry)
500 return __follow_down(mnt,dentry);
503 static inline void follow_dotdot(struct vfsmount **mnt, struct dentry **dentry)
506 struct vfsmount *parent;
507 struct dentry *old = *dentry;
509 read_lock(¤t->fs->lock);
510 if (*dentry == current->fs->root &&
511 *mnt == current->fs->rootmnt) {
512 read_unlock(¤t->fs->lock);
515 read_unlock(¤t->fs->lock);
516 spin_lock(&dcache_lock);
517 if (*dentry != (*mnt)->mnt_root) {
518 *dentry = dget((*dentry)->d_parent);
519 spin_unlock(&dcache_lock);
523 spin_unlock(&dcache_lock);
524 spin_lock(&vfsmount_lock);
525 parent = (*mnt)->mnt_parent;
526 if (parent == *mnt) {
527 spin_unlock(&vfsmount_lock);
531 *dentry = dget((*mnt)->mnt_mountpoint);
532 spin_unlock(&vfsmount_lock);
537 follow_mount(mnt, dentry);
541 struct vfsmount *mnt;
542 struct dentry *dentry;
546 * It's more convoluted than I'd like it to be, but... it's still fairly
547 * small and for now I'd prefer to have fast path as straight as possible.
548 * It _is_ time-critical.
550 static int do_lookup(struct nameidata *nd, struct qstr *name,
553 struct vfsmount *mnt = nd->mnt;
554 struct dentry *dentry = __d_lookup(nd->dentry, name);
558 if (dentry->d_op && dentry->d_op->d_revalidate)
559 goto need_revalidate;
562 path->dentry = dentry;
566 dentry = real_lookup(nd->dentry, name, nd);
572 if (dentry->d_op->d_revalidate(dentry, nd))
574 if (d_invalidate(dentry))
580 return PTR_ERR(dentry);
586 * This is the basic name resolution function, turning a pathname
587 * into the final dentry.
589 * We expect 'base' to be positive and a directory.
591 int fastcall link_path_walk(const char * name, struct nameidata *nd)
596 unsigned int lookup_flags = nd->flags;
603 inode = nd->dentry->d_inode;
604 if (current->link_count)
605 lookup_flags = LOOKUP_FOLLOW;
607 /* At this point we know we have a real path component. */
613 err = exec_permission_lite(inode, nd);
614 if (err == -EAGAIN) {
615 err = permission(inode, MAY_EXEC, nd);
621 c = *(const unsigned char *)name;
623 hash = init_name_hash();
626 hash = partial_name_hash(c, hash);
627 c = *(const unsigned char *)name;
628 } while (c && (c != '/'));
629 this.len = name - (const char *) this.name;
630 this.hash = end_name_hash(hash);
632 /* remove trailing slashes? */
635 while (*++name == '/');
637 goto last_with_slashes;
640 * "." and ".." are special - ".." especially so because it has
641 * to be able to know about the current root directory and
642 * parent relationships.
644 if (this.name[0] == '.') switch (this.len) {
648 if (this.name[1] != '.')
650 follow_dotdot(&nd->mnt, &nd->dentry);
651 inode = nd->dentry->d_inode;
657 * See if the low-level filesystem might want
658 * to use its own hash..
660 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
661 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
665 nd->flags |= LOOKUP_CONTINUE;
666 /* This does the actual lookups.. */
667 err = do_lookup(nd, &this, &next);
670 /* Check mountpoints.. */
671 follow_mount(&next.mnt, &next.dentry);
674 inode = next.dentry->d_inode;
681 if (inode->i_op->follow_link) {
683 err = do_follow_link(next.dentry, nd);
689 inode = nd->dentry->d_inode;
698 nd->dentry = next.dentry;
701 if (!inode->i_op->lookup)
704 /* here ends the main loop */
707 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
709 nd->flags &= ~LOOKUP_CONTINUE;
710 if (lookup_flags & LOOKUP_PARENT)
712 if (this.name[0] == '.') switch (this.len) {
716 if (this.name[1] != '.')
718 follow_dotdot(&nd->mnt, &nd->dentry);
719 inode = nd->dentry->d_inode;
724 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
725 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
729 err = do_lookup(nd, &this, &next);
732 follow_mount(&next.mnt, &next.dentry);
733 inode = next.dentry->d_inode;
734 if ((lookup_flags & LOOKUP_FOLLOW)
735 && inode && inode->i_op && inode->i_op->follow_link) {
737 err = do_follow_link(next.dentry, nd);
742 inode = nd->dentry->d_inode;
746 nd->dentry = next.dentry;
751 if (lookup_flags & LOOKUP_DIRECTORY) {
753 if (!inode->i_op || !inode->i_op->lookup)
759 nd->last_type = LAST_NORM;
760 if (this.name[0] != '.')
763 nd->last_type = LAST_DOT;
764 else if (this.len == 2 && this.name[1] == '.')
765 nd->last_type = LAST_DOTDOT;
770 * We bypassed the ordinary revalidation routines.
771 * We may need to check the cached dentry for staleness.
773 if (nd->dentry && nd->dentry->d_sb &&
774 (nd->dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
776 /* Note: we do not d_invalidate() */
777 if (!nd->dentry->d_op->d_revalidate(nd->dentry, nd))
791 int fastcall path_walk(const char * name, struct nameidata *nd)
793 current->total_link_count = 0;
794 return link_path_walk(name, nd);
798 /* returns 1 if everything is done */
799 static int __emul_lookup_dentry(const char *name, struct nameidata *nd)
801 if (path_walk(name, nd))
802 return 0; /* something went wrong... */
804 if (!nd->dentry->d_inode || S_ISDIR(nd->dentry->d_inode->i_mode)) {
805 struct nameidata nd_root;
807 * NAME was not found in alternate root or it's a directory. Try to find
808 * it in the normal root:
810 nd_root.last_type = LAST_ROOT;
811 nd_root.flags = nd->flags;
812 memcpy(&nd_root.intent, &nd->intent, sizeof(nd_root.intent));
813 read_lock(¤t->fs->lock);
814 nd_root.mnt = mntget(current->fs->rootmnt);
815 nd_root.dentry = dget(current->fs->root);
816 read_unlock(¤t->fs->lock);
817 if (path_walk(name, &nd_root))
819 if (nd_root.dentry->d_inode) {
821 nd->dentry = nd_root.dentry;
822 nd->mnt = nd_root.mnt;
823 nd->last = nd_root.last;
826 path_release(&nd_root);
831 void set_fs_altroot(void)
833 char *emul = __emul_prefix();
835 struct vfsmount *mnt = NULL, *oldmnt;
836 struct dentry *dentry = NULL, *olddentry;
841 err = path_lookup(emul, LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_NOALT, &nd);
847 write_lock(¤t->fs->lock);
848 oldmnt = current->fs->altrootmnt;
849 olddentry = current->fs->altroot;
850 current->fs->altrootmnt = mnt;
851 current->fs->altroot = dentry;
852 write_unlock(¤t->fs->lock);
861 walk_init_root(const char *name, struct nameidata *nd)
863 read_lock(¤t->fs->lock);
864 if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
865 nd->mnt = mntget(current->fs->altrootmnt);
866 nd->dentry = dget(current->fs->altroot);
867 read_unlock(¤t->fs->lock);
868 if (__emul_lookup_dentry(name,nd))
870 read_lock(¤t->fs->lock);
872 nd->mnt = mntget(current->fs->rootmnt);
873 nd->dentry = dget(current->fs->root);
874 read_unlock(¤t->fs->lock);
878 int fastcall path_lookup(const char *name, unsigned int flags, struct nameidata *nd)
882 nd->last_type = LAST_ROOT; /* if there are only slashes... */
885 read_lock(¤t->fs->lock);
887 if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
888 nd->mnt = mntget(current->fs->altrootmnt);
889 nd->dentry = dget(current->fs->altroot);
890 read_unlock(¤t->fs->lock);
891 if (__emul_lookup_dentry(name,nd))
893 read_lock(¤t->fs->lock);
895 nd->mnt = mntget(current->fs->rootmnt);
896 nd->dentry = dget(current->fs->root);
899 nd->mnt = mntget(current->fs->pwdmnt);
900 nd->dentry = dget(current->fs->pwd);
902 read_unlock(¤t->fs->lock);
903 current->total_link_count = 0;
904 retval = link_path_walk(name, nd);
905 if (unlikely(current->audit_context
906 && nd && nd->dentry && nd->dentry->d_inode))
908 nd->dentry->d_inode->i_ino,
909 nd->dentry->d_inode->i_rdev);
914 * Restricted form of lookup. Doesn't follow links, single-component only,
915 * needs parent already locked. Doesn't follow mounts.
918 static struct dentry * __lookup_hash(struct qstr *name, struct dentry * base, struct nameidata *nd)
920 struct dentry * dentry;
924 inode = base->d_inode;
925 err = permission(inode, MAY_EXEC, nd);
926 dentry = ERR_PTR(err);
931 * See if the low-level filesystem might want
932 * to use its own hash..
934 if (base->d_op && base->d_op->d_hash) {
935 err = base->d_op->d_hash(base, name);
936 dentry = ERR_PTR(err);
941 dentry = cached_lookup(base, name, nd);
943 struct dentry *new = d_alloc(base, name);
944 dentry = ERR_PTR(-ENOMEM);
947 dentry = inode->i_op->lookup(inode, new, nd);
957 struct dentry * lookup_hash(struct qstr *name, struct dentry * base)
959 return __lookup_hash(name, base, NULL);
963 struct dentry * lookup_one_len(const char * name, struct dentry * base, int len)
974 hash = init_name_hash();
976 c = *(const unsigned char *)name++;
977 if (c == '/' || c == '\0')
979 hash = partial_name_hash(c, hash);
981 this.hash = end_name_hash(hash);
983 return lookup_hash(&this, base);
985 return ERR_PTR(-EACCES);
991 * is used by most simple commands to get the inode of a specified name.
992 * Open, link etc use their own routines, but this is enough for things
995 * namei exists in two versions: namei/lnamei. The only difference is
996 * that namei follows links, while lnamei does not.
999 int fastcall __user_walk(const char __user *name, unsigned flags, struct nameidata *nd)
1001 char *tmp = getname(name);
1002 int err = PTR_ERR(tmp);
1005 err = path_lookup(tmp, flags, nd);
1012 * It's inline, so penalty for filesystems that don't use sticky bit is
1015 static inline int check_sticky(struct inode *dir, struct inode *inode)
1017 if (!(dir->i_mode & S_ISVTX))
1019 if (inode->i_uid == current->fsuid)
1021 if (dir->i_uid == current->fsuid)
1023 return !capable(CAP_FOWNER);
1027 * Check whether we can remove a link victim from directory dir, check
1028 * whether the type of victim is right.
1029 * 1. We can't do it if dir is read-only (done in permission())
1030 * 2. We should have write and exec permissions on dir
1031 * 3. We can't remove anything from append-only dir
1032 * 4. We can't do anything with immutable dir (done in permission())
1033 * 5. If the sticky bit on dir is set we should either
1034 * a. be owner of dir, or
1035 * b. be owner of victim, or
1036 * c. have CAP_FOWNER capability
1037 * 6. If the victim is append-only or immutable we can't do antyhing with
1038 * links pointing to it.
1039 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1040 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1041 * 9. We can't remove a root or mountpoint.
1042 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1043 * nfs_async_unlink().
1045 static inline int may_delete(struct inode *dir,struct dentry *victim,int isdir)
1048 if (!victim->d_inode || victim->d_parent->d_inode != dir)
1050 error = permission(dir,MAY_WRITE | MAY_EXEC, NULL);
1055 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1056 IS_IXORUNLINK(victim->d_inode))
1059 if (!S_ISDIR(victim->d_inode->i_mode))
1061 if (IS_ROOT(victim))
1063 } else if (S_ISDIR(victim->d_inode->i_mode))
1065 if (IS_DEADDIR(dir))
1067 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1072 /* Check whether we can create an object with dentry child in directory
1074 * 1. We can't do it if child already exists (open has special treatment for
1075 * this case, but since we are inlined it's OK)
1076 * 2. We can't do it if dir is read-only (done in permission())
1077 * 3. We should have write and exec permissions on dir
1078 * 4. We can't do it if dir is immutable (done in permission())
1080 static inline int may_create(struct inode *dir, struct dentry *child,
1081 struct nameidata *nd)
1085 if (IS_DEADDIR(dir))
1087 return permission(dir,MAY_WRITE | MAY_EXEC, nd);
1091 * Special case: O_CREAT|O_EXCL implies O_NOFOLLOW for security
1094 * O_DIRECTORY translates into forcing a directory lookup.
1096 static inline int lookup_flags(unsigned int f)
1098 unsigned long retval = LOOKUP_FOLLOW;
1101 retval &= ~LOOKUP_FOLLOW;
1103 if ((f & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
1104 retval &= ~LOOKUP_FOLLOW;
1106 if (f & O_DIRECTORY)
1107 retval |= LOOKUP_DIRECTORY;
1113 * p1 and p2 should be directories on the same fs.
1115 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1120 down(&p1->d_inode->i_sem);
1124 down(&p1->d_inode->i_sb->s_vfs_rename_sem);
1126 for (p = p1; p->d_parent != p; p = p->d_parent) {
1127 if (p->d_parent == p2) {
1128 down(&p2->d_inode->i_sem);
1129 down(&p1->d_inode->i_sem);
1134 for (p = p2; p->d_parent != p; p = p->d_parent) {
1135 if (p->d_parent == p1) {
1136 down(&p1->d_inode->i_sem);
1137 down(&p2->d_inode->i_sem);
1142 down(&p1->d_inode->i_sem);
1143 down(&p2->d_inode->i_sem);
1147 void unlock_rename(struct dentry *p1, struct dentry *p2)
1149 up(&p1->d_inode->i_sem);
1151 up(&p2->d_inode->i_sem);
1152 up(&p1->d_inode->i_sb->s_vfs_rename_sem);
1156 int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1157 struct nameidata *nd)
1159 int error = may_create(dir, dentry, nd);
1164 if (!dir->i_op || !dir->i_op->create)
1165 return -EACCES; /* shouldn't it be ENOSYS? */
1168 error = security_inode_create(dir, dentry, mode);
1172 error = dir->i_op->create(dir, dentry, mode, nd);
1174 inode_dir_notify(dir, DN_CREATE);
1175 security_inode_post_create(dir, dentry, mode);
1180 int may_open(struct nameidata *nd, int acc_mode, int flag)
1182 struct dentry *dentry = nd->dentry;
1183 struct inode *inode = dentry->d_inode;
1189 if (S_ISLNK(inode->i_mode))
1192 if (S_ISDIR(inode->i_mode) && (flag & FMODE_WRITE))
1195 error = permission(inode, acc_mode, nd);
1200 * FIFO's, sockets and device files are special: they don't
1201 * actually live on the filesystem itself, and as such you
1202 * can write to them even if the filesystem is read-only.
1204 if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
1206 } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
1207 if (nd->mnt->mnt_flags & MNT_NODEV)
1211 } else if (IS_RDONLY(inode) && (flag & FMODE_WRITE))
1214 * An append-only file must be opened in append mode for writing.
1216 if (IS_APPEND(inode)) {
1217 if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
1224 * Ensure there are no outstanding leases on the file.
1226 error = break_lease(inode, flag);
1230 if (flag & O_TRUNC) {
1231 error = get_write_access(inode);
1236 * Refuse to truncate files with mandatory locks held on them.
1238 error = locks_verify_locked(inode);
1242 error = do_truncate(dentry, 0);
1244 put_write_access(inode);
1248 if (flag & FMODE_WRITE)
1257 * namei for open - this is in fact almost the whole open-routine.
1259 * Note that the low bits of "flag" aren't the same as in the open
1260 * system call - they are 00 - no permissions needed
1261 * 01 - read permission needed
1262 * 10 - write permission needed
1263 * 11 - read/write permissions needed
1264 * which is a lot more logical, and also allows the "no perm" needed
1265 * for symlinks (where the permissions are checked later).
1268 int open_namei(const char * pathname, int flag, int mode, struct nameidata *nd)
1270 int acc_mode, error = 0;
1271 struct dentry *dentry;
1275 acc_mode = ACC_MODE(flag);
1277 /* Allow the LSM permission hook to distinguish append
1278 access from general write access. */
1279 if (flag & O_APPEND)
1280 acc_mode |= MAY_APPEND;
1282 /* Fill in the open() intent data */
1283 nd->intent.open.flags = flag;
1284 nd->intent.open.create_mode = mode;
1287 * The simplest case - just a plain lookup.
1289 if (!(flag & O_CREAT)) {
1290 error = path_lookup(pathname, lookup_flags(flag)|LOOKUP_OPEN, nd);
1297 * Create - we need to know the parent.
1299 error = path_lookup(pathname, LOOKUP_PARENT|LOOKUP_OPEN|LOOKUP_CREATE, nd);
1304 * We have the parent and last component. First of all, check
1305 * that we are not asked to creat(2) an obvious directory - that
1309 if (nd->last_type != LAST_NORM || nd->last.name[nd->last.len])
1313 nd->flags &= ~LOOKUP_PARENT;
1314 down(&dir->d_inode->i_sem);
1315 dentry = __lookup_hash(&nd->last, nd->dentry, nd);
1318 error = PTR_ERR(dentry);
1319 if (IS_ERR(dentry)) {
1320 up(&dir->d_inode->i_sem);
1324 /* Negative dentry, just create the file */
1325 if (!dentry->d_inode) {
1326 if (!IS_POSIXACL(dir->d_inode))
1327 mode &= ~current->fs->umask;
1328 error = vfs_create(dir->d_inode, dentry, mode, nd);
1329 up(&dir->d_inode->i_sem);
1331 nd->dentry = dentry;
1334 /* Don't check for write permission, don't truncate */
1341 * It already exists.
1343 up(&dir->d_inode->i_sem);
1349 if (d_mountpoint(dentry)) {
1351 if (flag & O_NOFOLLOW)
1353 while (__follow_down(&nd->mnt,&dentry) && d_mountpoint(dentry));
1356 if (!dentry->d_inode)
1358 if (dentry->d_inode->i_op && dentry->d_inode->i_op->follow_link)
1362 nd->dentry = dentry;
1364 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode))
1367 error = may_open(nd, acc_mode, flag);
1380 if (flag & O_NOFOLLOW)
1383 * This is subtle. Instead of calling do_follow_link() we do the
1384 * thing by hands. The reason is that this way we have zero link_count
1385 * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1386 * After that we have the parent and last component, i.e.
1387 * we are in the same situation as after the first path_walk().
1388 * Well, almost - if the last component is normal we get its copy
1389 * stored in nd->last.name and we will have to putname() it when we
1390 * are done. Procfs-like symlinks just set LAST_BIND.
1392 nd->flags |= LOOKUP_PARENT;
1393 error = security_inode_follow_link(dentry, nd);
1396 touch_atime(nd->mnt, dentry);
1397 error = dentry->d_inode->i_op->follow_link(dentry, nd);
1401 nd->flags &= ~LOOKUP_PARENT;
1402 if (nd->last_type == LAST_BIND) {
1403 dentry = nd->dentry;
1407 if (nd->last_type != LAST_NORM)
1409 if (nd->last.name[nd->last.len]) {
1410 putname(nd->last.name);
1415 putname(nd->last.name);
1419 down(&dir->d_inode->i_sem);
1420 dentry = __lookup_hash(&nd->last, nd->dentry, nd);
1421 putname(nd->last.name);
1426 * lookup_create - lookup a dentry, creating it if it doesn't exist
1427 * @nd: nameidata info
1428 * @is_dir: directory flag
1430 * Simple function to lookup and return a dentry and create it
1431 * if it doesn't exist. Is SMP-safe.
1433 struct dentry *lookup_create(struct nameidata *nd, int is_dir)
1435 struct dentry *dentry;
1437 down(&nd->dentry->d_inode->i_sem);
1438 dentry = ERR_PTR(-EEXIST);
1439 if (nd->last_type != LAST_NORM)
1441 nd->flags &= ~LOOKUP_PARENT;
1442 dentry = lookup_hash(&nd->last, nd->dentry);
1445 if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode)
1450 dentry = ERR_PTR(-ENOENT);
1455 int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1457 int error = may_create(dir, dentry, NULL);
1462 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1465 if (!dir->i_op || !dir->i_op->mknod)
1468 error = security_inode_mknod(dir, dentry, mode, dev);
1473 error = dir->i_op->mknod(dir, dentry, mode, dev);
1475 inode_dir_notify(dir, DN_CREATE);
1476 security_inode_post_mknod(dir, dentry, mode, dev);
1481 asmlinkage long sys_mknod(const char __user * filename, int mode, unsigned dev)
1485 struct dentry * dentry;
1486 struct nameidata nd;
1490 tmp = getname(filename);
1492 return PTR_ERR(tmp);
1494 error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1497 dentry = lookup_create(&nd, 0);
1498 error = PTR_ERR(dentry);
1500 if (!IS_POSIXACL(nd.dentry->d_inode))
1501 mode &= ~current->fs->umask;
1502 if (!IS_ERR(dentry)) {
1503 switch (mode & S_IFMT) {
1504 case 0: case S_IFREG:
1505 error = vfs_create(nd.dentry->d_inode,dentry,mode,&nd);
1507 case S_IFCHR: case S_IFBLK:
1508 error = vfs_mknod(nd.dentry->d_inode,dentry,mode,
1509 new_decode_dev(dev));
1511 case S_IFIFO: case S_IFSOCK:
1512 error = vfs_mknod(nd.dentry->d_inode,dentry,mode,0);
1522 up(&nd.dentry->d_inode->i_sem);
1530 int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1532 int error = may_create(dir, dentry, NULL);
1537 if (!dir->i_op || !dir->i_op->mkdir)
1540 mode &= (S_IRWXUGO|S_ISVTX);
1541 error = security_inode_mkdir(dir, dentry, mode);
1546 error = dir->i_op->mkdir(dir, dentry, mode);
1548 inode_dir_notify(dir, DN_CREATE);
1549 security_inode_post_mkdir(dir,dentry, mode);
1554 asmlinkage long sys_mkdir(const char __user * pathname, int mode)
1559 tmp = getname(pathname);
1560 error = PTR_ERR(tmp);
1562 struct dentry *dentry;
1563 struct nameidata nd;
1565 error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1568 dentry = lookup_create(&nd, 1);
1569 error = PTR_ERR(dentry);
1570 if (!IS_ERR(dentry)) {
1571 if (!IS_POSIXACL(nd.dentry->d_inode))
1572 mode &= ~current->fs->umask;
1573 error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
1576 up(&nd.dentry->d_inode->i_sem);
1586 * We try to drop the dentry early: we should have
1587 * a usage count of 2 if we're the only user of this
1588 * dentry, and if that is true (possibly after pruning
1589 * the dcache), then we drop the dentry now.
1591 * A low-level filesystem can, if it choses, legally
1594 * if (!d_unhashed(dentry))
1597 * if it cannot handle the case of removing a directory
1598 * that is still in use by something else..
1600 static void d_unhash(struct dentry *dentry)
1603 spin_lock(&dcache_lock);
1604 switch (atomic_read(&dentry->d_count)) {
1606 spin_unlock(&dcache_lock);
1607 shrink_dcache_parent(dentry);
1608 spin_lock(&dcache_lock);
1609 if (atomic_read(&dentry->d_count) != 2)
1614 spin_unlock(&dcache_lock);
1617 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
1619 int error = may_delete(dir, dentry, 1);
1624 if (!dir->i_op || !dir->i_op->rmdir)
1629 down(&dentry->d_inode->i_sem);
1631 if (d_mountpoint(dentry))
1634 error = security_inode_rmdir(dir, dentry);
1636 error = dir->i_op->rmdir(dir, dentry);
1638 dentry->d_inode->i_flags |= S_DEAD;
1641 up(&dentry->d_inode->i_sem);
1643 inode_dir_notify(dir, DN_DELETE);
1651 asmlinkage long sys_rmdir(const char __user * pathname)
1655 struct dentry *dentry;
1656 struct nameidata nd;
1658 name = getname(pathname);
1660 return PTR_ERR(name);
1662 error = path_lookup(name, LOOKUP_PARENT, &nd);
1666 switch(nd.last_type) {
1677 down(&nd.dentry->d_inode->i_sem);
1678 dentry = lookup_hash(&nd.last, nd.dentry);
1679 error = PTR_ERR(dentry);
1680 if (!IS_ERR(dentry)) {
1681 error = vfs_rmdir(nd.dentry->d_inode, dentry);
1684 up(&nd.dentry->d_inode->i_sem);
1692 int vfs_unlink(struct inode *dir, struct dentry *dentry)
1694 int error = may_delete(dir, dentry, 0);
1699 if (!dir->i_op || !dir->i_op->unlink)
1704 down(&dentry->d_inode->i_sem);
1705 if (d_mountpoint(dentry))
1708 error = security_inode_unlink(dir, dentry);
1710 error = dir->i_op->unlink(dir, dentry);
1712 up(&dentry->d_inode->i_sem);
1714 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
1715 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
1717 inode_dir_notify(dir, DN_DELETE);
1723 * Make sure that the actual truncation of the file will occur outside its
1724 * directory's i_sem. Truncate can take a long time if there is a lot of
1725 * writeout happening, and we don't want to prevent access to the directory
1726 * while waiting on the I/O.
1728 asmlinkage long sys_unlink(const char __user * pathname)
1732 struct dentry *dentry;
1733 struct nameidata nd;
1734 struct inode *inode = NULL;
1736 name = getname(pathname);
1738 return PTR_ERR(name);
1740 error = path_lookup(name, LOOKUP_PARENT, &nd);
1744 if (nd.last_type != LAST_NORM)
1746 down(&nd.dentry->d_inode->i_sem);
1747 dentry = lookup_hash(&nd.last, nd.dentry);
1748 error = PTR_ERR(dentry);
1749 if (!IS_ERR(dentry)) {
1750 /* Why not before? Because we want correct error value */
1751 if (nd.last.name[nd.last.len])
1753 inode = dentry->d_inode;
1755 atomic_inc(&inode->i_count);
1756 error = vfs_unlink(nd.dentry->d_inode, dentry);
1760 up(&nd.dentry->d_inode->i_sem);
1767 iput(inode); /* truncate the inode here */
1771 error = !dentry->d_inode ? -ENOENT :
1772 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
1776 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
1778 int error = may_create(dir, dentry, NULL);
1783 if (!dir->i_op || !dir->i_op->symlink)
1786 error = security_inode_symlink(dir, dentry, oldname);
1791 error = dir->i_op->symlink(dir, dentry, oldname);
1793 inode_dir_notify(dir, DN_CREATE);
1794 security_inode_post_symlink(dir, dentry, oldname);
1799 asmlinkage long sys_symlink(const char __user * oldname, const char __user * newname)
1805 from = getname(oldname);
1807 return PTR_ERR(from);
1808 to = getname(newname);
1809 error = PTR_ERR(to);
1811 struct dentry *dentry;
1812 struct nameidata nd;
1814 error = path_lookup(to, LOOKUP_PARENT, &nd);
1817 dentry = lookup_create(&nd, 0);
1818 error = PTR_ERR(dentry);
1819 if (!IS_ERR(dentry)) {
1820 error = vfs_symlink(nd.dentry->d_inode, dentry, from);
1823 up(&nd.dentry->d_inode->i_sem);
1832 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
1834 struct inode *inode = old_dentry->d_inode;
1840 error = may_create(dir, new_dentry, NULL);
1844 if (dir->i_sb != inode->i_sb)
1848 * A link to an append-only or immutable file cannot be created.
1850 if (IS_APPEND(inode) || IS_IXORUNLINK(inode))
1852 if (!dir->i_op || !dir->i_op->link)
1854 if (S_ISDIR(old_dentry->d_inode->i_mode))
1857 error = security_inode_link(old_dentry, dir, new_dentry);
1861 down(&old_dentry->d_inode->i_sem);
1863 error = dir->i_op->link(old_dentry, dir, new_dentry);
1864 up(&old_dentry->d_inode->i_sem);
1866 inode_dir_notify(dir, DN_CREATE);
1867 security_inode_post_link(old_dentry, dir, new_dentry);
1873 * Hardlinks are often used in delicate situations. We avoid
1874 * security-related surprises by not following symlinks on the
1877 * We don't follow them on the oldname either to be compatible
1878 * with linux 2.0, and to avoid hard-linking to directories
1879 * and other special files. --ADM
1881 asmlinkage long sys_link(const char __user * oldname, const char __user * newname)
1883 struct dentry *new_dentry;
1884 struct nameidata nd, old_nd;
1888 to = getname(newname);
1892 error = __user_walk(oldname, 0, &old_nd);
1895 error = path_lookup(to, LOOKUP_PARENT, &nd);
1899 if (old_nd.mnt != nd.mnt)
1901 new_dentry = lookup_create(&nd, 0);
1902 error = PTR_ERR(new_dentry);
1903 if (!IS_ERR(new_dentry)) {
1904 error = vfs_link(old_nd.dentry, nd.dentry->d_inode, new_dentry);
1907 up(&nd.dentry->d_inode->i_sem);
1911 path_release(&old_nd);
1919 * The worst of all namespace operations - renaming directory. "Perverted"
1920 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
1922 * a) we can get into loop creation. Check is done in is_subdir().
1923 * b) race potential - two innocent renames can create a loop together.
1924 * That's where 4.4 screws up. Current fix: serialization on
1925 * sb->s_vfs_rename_sem. We might be more accurate, but that's another
1927 * c) we have to lock _three_ objects - parents and victim (if it exists).
1928 * And that - after we got ->i_sem on parents (until then we don't know
1929 * whether the target exists). Solution: try to be smart with locking
1930 * order for inodes. We rely on the fact that tree topology may change
1931 * only under ->s_vfs_rename_sem _and_ that parent of the object we
1932 * move will be locked. Thus we can rank directories by the tree
1933 * (ancestors first) and rank all non-directories after them.
1934 * That works since everybody except rename does "lock parent, lookup,
1935 * lock child" and rename is under ->s_vfs_rename_sem.
1936 * HOWEVER, it relies on the assumption that any object with ->lookup()
1937 * has no more than 1 dentry. If "hybrid" objects will ever appear,
1938 * we'd better make sure that there's no link(2) for them.
1939 * d) some filesystems don't support opened-but-unlinked directories,
1940 * either because of layout or because they are not ready to deal with
1941 * all cases correctly. The latter will be fixed (taking this sort of
1942 * stuff into VFS), but the former is not going away. Solution: the same
1943 * trick as in rmdir().
1944 * e) conversion from fhandle to dentry may come in the wrong moment - when
1945 * we are removing the target. Solution: we will have to grab ->i_sem
1946 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
1947 * ->i_sem on parents, which works but leads to some truely excessive
1950 int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
1951 struct inode *new_dir, struct dentry *new_dentry)
1954 struct inode *target;
1957 * If we are going to change the parent - check write permissions,
1958 * we'll need to flip '..'.
1960 if (new_dir != old_dir) {
1961 error = permission(old_dentry->d_inode, MAY_WRITE, NULL);
1966 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
1970 target = new_dentry->d_inode;
1972 down(&target->i_sem);
1973 d_unhash(new_dentry);
1975 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
1978 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
1981 target->i_flags |= S_DEAD;
1983 if (d_unhashed(new_dentry))
1984 d_rehash(new_dentry);
1988 d_move(old_dentry,new_dentry);
1989 security_inode_post_rename(old_dir, old_dentry,
1990 new_dir, new_dentry);
1995 int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
1996 struct inode *new_dir, struct dentry *new_dentry)
1998 struct inode *target;
2001 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2006 target = new_dentry->d_inode;
2008 down(&target->i_sem);
2009 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2012 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2014 /* The following d_move() should become unconditional */
2015 if (!(old_dir->i_sb->s_type->fs_flags & FS_ODD_RENAME))
2016 d_move(old_dentry, new_dentry);
2017 security_inode_post_rename(old_dir, old_dentry, new_dir, new_dentry);
2025 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
2026 struct inode *new_dir, struct dentry *new_dentry)
2029 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
2031 if (old_dentry->d_inode == new_dentry->d_inode)
2034 error = may_delete(old_dir, old_dentry, is_dir);
2038 if (!new_dentry->d_inode)
2039 error = may_create(new_dir, new_dentry, NULL);
2041 error = may_delete(new_dir, new_dentry, is_dir);
2045 if (!old_dir->i_op || !old_dir->i_op->rename)
2048 DQUOT_INIT(old_dir);
2049 DQUOT_INIT(new_dir);
2052 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
2054 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
2056 if (old_dir == new_dir)
2057 inode_dir_notify(old_dir, DN_RENAME);
2059 inode_dir_notify(old_dir, DN_DELETE);
2060 inode_dir_notify(new_dir, DN_CREATE);
2066 static inline int do_rename(const char * oldname, const char * newname)
2069 struct dentry * old_dir, * new_dir;
2070 struct dentry * old_dentry, *new_dentry;
2071 struct dentry * trap;
2072 struct nameidata oldnd, newnd;
2074 error = path_lookup(oldname, LOOKUP_PARENT, &oldnd);
2078 error = path_lookup(newname, LOOKUP_PARENT, &newnd);
2083 if (oldnd.mnt != newnd.mnt)
2086 old_dir = oldnd.dentry;
2088 if (oldnd.last_type != LAST_NORM)
2091 new_dir = newnd.dentry;
2092 if (newnd.last_type != LAST_NORM)
2095 trap = lock_rename(new_dir, old_dir);
2097 old_dentry = lookup_hash(&oldnd.last, old_dir);
2098 error = PTR_ERR(old_dentry);
2099 if (IS_ERR(old_dentry))
2101 /* source must exist */
2103 if (!old_dentry->d_inode)
2105 /* unless the source is a directory trailing slashes give -ENOTDIR */
2106 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
2108 if (oldnd.last.name[oldnd.last.len])
2110 if (newnd.last.name[newnd.last.len])
2113 /* source should not be ancestor of target */
2115 if (old_dentry == trap)
2117 new_dentry = lookup_hash(&newnd.last, new_dir);
2118 error = PTR_ERR(new_dentry);
2119 if (IS_ERR(new_dentry))
2121 /* target should not be an ancestor of source */
2123 if (new_dentry == trap)
2126 error = vfs_rename(old_dir->d_inode, old_dentry,
2127 new_dir->d_inode, new_dentry);
2133 unlock_rename(new_dir, old_dir);
2135 path_release(&newnd);
2137 path_release(&oldnd);
2142 asmlinkage long sys_rename(const char __user * oldname, const char __user * newname)
2148 from = getname(oldname);
2150 return PTR_ERR(from);
2151 to = getname(newname);
2152 error = PTR_ERR(to);
2154 error = do_rename(from,to);
2161 int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
2165 len = PTR_ERR(link);
2170 if (len > (unsigned) buflen)
2172 if (copy_to_user(buffer, link, len))
2179 __vfs_follow_link(struct nameidata *nd, const char *link)
2188 if (!walk_init_root(link, nd))
2189 /* weird __emul_prefix() stuff did it */
2192 res = link_path_walk(link, nd);
2194 if (current->link_count || res || nd->last_type!=LAST_NORM)
2197 * If it is an iterative symlinks resolution in open_namei() we
2198 * have to copy the last component. And all that crap because of
2199 * bloody create() on broken symlinks. Furrfu...
2202 if (unlikely(!name)) {
2206 strcpy(name, nd->last.name);
2207 nd->last.name = name;
2211 return PTR_ERR(link);
2214 int vfs_follow_link(struct nameidata *nd, const char *link)
2216 return __vfs_follow_link(nd, link);
2219 /* get the link contents into pagecache */
2220 static char *page_getlink(struct dentry * dentry, struct page **ppage)
2223 struct address_space *mapping = dentry->d_inode->i_mapping;
2224 page = read_cache_page(mapping, 0, (filler_t *)mapping->a_ops->readpage,
2228 wait_on_page_locked(page);
2229 if (!PageUptodate(page))
2235 page_cache_release(page);
2236 return ERR_PTR(-EIO);
2242 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2244 struct page *page = NULL;
2245 char *s = page_getlink(dentry, &page);
2246 int res = vfs_readlink(dentry,buffer,buflen,s);
2249 page_cache_release(page);
2254 int page_follow_link(struct dentry *dentry, struct nameidata *nd)
2256 struct page *page = NULL;
2257 char *s = page_getlink(dentry, &page);
2258 int res = __vfs_follow_link(nd, s);
2261 page_cache_release(page);
2266 int page_symlink(struct inode *inode, const char *symname, int len)
2268 struct address_space *mapping = inode->i_mapping;
2269 struct page *page = grab_cache_page(mapping, 0);
2275 err = mapping->a_ops->prepare_write(NULL, page, 0, len-1);
2278 kaddr = kmap_atomic(page, KM_USER0);
2279 memcpy(kaddr, symname, len-1);
2280 kunmap_atomic(kaddr, KM_USER0);
2281 mapping->a_ops->commit_write(NULL, page, 0, len-1);
2283 * Notice that we are _not_ going to block here - end of page is
2284 * unmapped, so this will only try to map the rest of page, see
2285 * that it is unmapped (typically even will not look into inode -
2286 * ->i_size will be enough for everything) and zero it out.
2287 * OTOH it's obviously correct and should make the page up-to-date.
2289 if (!PageUptodate(page)) {
2290 err = mapping->a_ops->readpage(NULL, page);
2291 wait_on_page_locked(page);
2295 page_cache_release(page);
2298 mark_inode_dirty(inode);
2302 page_cache_release(page);
2307 struct inode_operations page_symlink_inode_operations = {
2308 .readlink = page_readlink,
2309 .follow_link = page_follow_link,
2312 EXPORT_SYMBOL(__user_walk);
2313 EXPORT_SYMBOL(follow_down);
2314 EXPORT_SYMBOL(follow_up);
2315 EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
2316 EXPORT_SYMBOL(getname);
2317 EXPORT_SYMBOL(lock_rename);
2318 EXPORT_SYMBOL(lookup_create);
2319 EXPORT_SYMBOL(lookup_hash);
2320 EXPORT_SYMBOL(lookup_one_len);
2321 EXPORT_SYMBOL(page_follow_link);
2322 EXPORT_SYMBOL(page_readlink);
2323 EXPORT_SYMBOL(page_symlink);
2324 EXPORT_SYMBOL(page_symlink_inode_operations);
2325 EXPORT_SYMBOL(path_lookup);
2326 EXPORT_SYMBOL(path_release);
2327 EXPORT_SYMBOL(path_walk);
2328 EXPORT_SYMBOL(permission);
2329 EXPORT_SYMBOL(unlock_rename);
2330 EXPORT_SYMBOL(vfs_create);
2331 EXPORT_SYMBOL(vfs_follow_link);
2332 EXPORT_SYMBOL(vfs_link);
2333 EXPORT_SYMBOL(vfs_mkdir);
2334 EXPORT_SYMBOL(vfs_mknod);
2335 EXPORT_SYMBOL(vfs_permission);
2336 EXPORT_SYMBOL(vfs_readlink);
2337 EXPORT_SYMBOL(vfs_rename);
2338 EXPORT_SYMBOL(vfs_rmdir);
2339 EXPORT_SYMBOL(vfs_symlink);
2340 EXPORT_SYMBOL(vfs_unlink);