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/syscalls.h>
29 #include <linux/mount.h>
30 #include <linux/audit.h>
31 #include <asm/namei.h>
32 #include <asm/uaccess.h>
34 #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
36 /* [Feb-1997 T. Schoebel-Theuer]
37 * Fundamental changes in the pathname lookup mechanisms (namei)
38 * were necessary because of omirr. The reason is that omirr needs
39 * to know the _real_ pathname, not the user-supplied one, in case
40 * of symlinks (and also when transname replacements occur).
42 * The new code replaces the old recursive symlink resolution with
43 * an iterative one (in case of non-nested symlink chains). It does
44 * this with calls to <fs>_follow_link().
45 * As a side effect, dir_namei(), _namei() and follow_link() are now
46 * replaced with a single function lookup_dentry() that can handle all
47 * the special cases of the former code.
49 * With the new dcache, the pathname is stored at each inode, at least as
50 * long as the refcount of the inode is positive. As a side effect, the
51 * size of the dcache depends on the inode cache and thus is dynamic.
53 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
54 * resolution to correspond with current state of the code.
56 * Note that the symlink resolution is not *completely* iterative.
57 * There is still a significant amount of tail- and mid- recursion in
58 * the algorithm. Also, note that <fs>_readlink() is not used in
59 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
60 * may return different results than <fs>_follow_link(). Many virtual
61 * filesystems (including /proc) exhibit this behavior.
64 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
65 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
66 * and the name already exists in form of a symlink, try to create the new
67 * name indicated by the symlink. The old code always complained that the
68 * name already exists, due to not following the symlink even if its target
69 * is nonexistent. The new semantics affects also mknod() and link() when
70 * the name is a symlink pointing to a non-existant name.
72 * I don't know which semantics is the right one, since I have no access
73 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
74 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
75 * "old" one. Personally, I think the new semantics is much more logical.
76 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
77 * file does succeed in both HP-UX and SunOs, but not in Solaris
78 * and in the old Linux semantics.
81 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
82 * semantics. See the comments in "open_namei" and "do_link" below.
84 * [10-Sep-98 Alan Modra] Another symlink change.
87 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
88 * inside the path - always follow.
89 * in the last component in creation/removal/renaming - never follow.
90 * if LOOKUP_FOLLOW passed - follow.
91 * if the pathname has trailing slashes - follow.
92 * otherwise - don't follow.
93 * (applied in that order).
95 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
96 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
97 * During the 2.4 we need to fix the userland stuff depending on it -
98 * hopefully we will be able to get rid of that wart in 2.5. So far only
99 * XEmacs seems to be relying on it...
102 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
103 * implemented. Let's see if raised priority of ->s_vfs_rename_sem gives
104 * any extra contention...
107 /* In order to reduce some races, while at the same time doing additional
108 * checking and hopefully speeding things up, we copy filenames to the
109 * kernel data space before using them..
111 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
112 * PATH_MAX includes the nul terminator --RR.
114 static inline int do_getname(const char __user *filename, char *page)
117 unsigned long len = PATH_MAX;
119 if (!segment_eq(get_fs(), KERNEL_DS)) {
120 if ((unsigned long) filename >= TASK_SIZE)
122 if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
123 len = TASK_SIZE - (unsigned long) filename;
126 retval = strncpy_from_user(page, filename, len);
130 return -ENAMETOOLONG;
136 char * getname(const char __user * filename)
140 result = ERR_PTR(-ENOMEM);
143 int retval = do_getname(filename, tmp);
148 result = ERR_PTR(retval);
151 audit_getname(result);
155 #ifdef CONFIG_AUDITSYSCALL
156 void putname(const char *name)
158 if (unlikely(current->audit_context))
163 EXPORT_SYMBOL(putname);
168 * generic_permission - check for access rights on a Posix-like filesystem
169 * @inode: inode to check access rights for
170 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
171 * @check_acl: optional callback to check for Posix ACLs
173 * Used to check for read/write/execute permissions on a file.
174 * We use "fsuid" for this, letting us set arbitrary permissions
175 * for filesystem access without changing the "normal" uids which
176 * are used for other things..
178 int generic_permission(struct inode *inode, int mask,
179 int (*check_acl)(struct inode *inode, int mask))
181 umode_t mode = inode->i_mode;
183 if (current->fsuid == inode->i_uid)
186 if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
187 int error = check_acl(inode, mask);
188 if (error == -EACCES)
189 goto check_capabilities;
190 else if (error != -EAGAIN)
194 if (in_group_p(inode->i_gid))
199 * If the DACs are ok we don't need any capability check.
201 if (((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask))
206 * Read/write DACs are always overridable.
207 * Executable DACs are overridable if at least one exec bit is set.
209 if (!(mask & MAY_EXEC) ||
210 (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
211 if (capable(CAP_DAC_OVERRIDE))
215 * Searching includes executable on directories, else just read.
217 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
218 if (capable(CAP_DAC_READ_SEARCH))
224 int permission(struct inode *inode, int mask, struct nameidata *nd)
228 if (mask & MAY_WRITE) {
229 umode_t mode = inode->i_mode;
232 * Nobody gets write access to a read-only fs.
234 if (IS_RDONLY(inode) &&
235 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
239 * Nobody gets write access to an immutable file.
241 if (IS_IMMUTABLE(inode))
246 /* Ordinary permission routines do not understand MAY_APPEND. */
247 submask = mask & ~MAY_APPEND;
248 if (inode->i_op && inode->i_op->permission)
249 retval = inode->i_op->permission(inode, submask, nd);
251 retval = generic_permission(inode, submask, NULL);
255 return security_inode_permission(inode, mask, nd);
259 * get_write_access() gets write permission for a file.
260 * put_write_access() releases this write permission.
261 * This is used for regular files.
262 * We cannot support write (and maybe mmap read-write shared) accesses and
263 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
264 * can have the following values:
265 * 0: no writers, no VM_DENYWRITE mappings
266 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
267 * > 0: (i_writecount) users are writing to the file.
269 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
270 * except for the cases where we don't hold i_writecount yet. Then we need to
271 * use {get,deny}_write_access() - these functions check the sign and refuse
272 * to do the change if sign is wrong. Exclusion between them is provided by
273 * the inode->i_lock spinlock.
276 int get_write_access(struct inode * inode)
278 spin_lock(&inode->i_lock);
279 if (atomic_read(&inode->i_writecount) < 0) {
280 spin_unlock(&inode->i_lock);
283 atomic_inc(&inode->i_writecount);
284 spin_unlock(&inode->i_lock);
289 int deny_write_access(struct file * file)
291 struct inode *inode = file->f_dentry->d_inode;
293 spin_lock(&inode->i_lock);
294 if (atomic_read(&inode->i_writecount) > 0) {
295 spin_unlock(&inode->i_lock);
298 atomic_dec(&inode->i_writecount);
299 spin_unlock(&inode->i_lock);
304 void path_release(struct nameidata *nd)
311 * umount() mustn't call path_release()/mntput() as that would clear
314 void path_release_on_umount(struct nameidata *nd)
321 * Internal lookup() using the new generic dcache.
324 static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
326 struct dentry * dentry = __d_lookup(parent, name);
328 /* lockess __d_lookup may fail due to concurrent d_move()
329 * in some unrelated directory, so try with d_lookup
332 dentry = d_lookup(parent, name);
334 if (dentry && dentry->d_op && dentry->d_op->d_revalidate) {
335 if (!dentry->d_op->d_revalidate(dentry, nd) && !d_invalidate(dentry)) {
344 * Short-cut version of permission(), for calling by
345 * path_walk(), when dcache lock is held. Combines parts
346 * of permission() and generic_permission(), and tests ONLY for
347 * MAY_EXEC permission.
349 * If appropriate, check DAC only. If not appropriate, or
350 * short-cut DAC fails, then call permission() to do more
351 * complete permission check.
353 static inline int exec_permission_lite(struct inode *inode,
354 struct nameidata *nd)
356 umode_t mode = inode->i_mode;
358 if (inode->i_op && inode->i_op->permission)
361 if (current->fsuid == inode->i_uid)
363 else if (in_group_p(inode->i_gid))
369 if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE))
372 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_OVERRIDE))
375 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH))
380 return security_inode_permission(inode, MAY_EXEC, nd);
384 * This is called when everything else fails, and we actually have
385 * to go to the low-level filesystem to find out what we should do..
387 * We get the directory semaphore, and after getting that we also
388 * make sure that nobody added the entry to the dcache in the meantime..
391 static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
393 struct dentry * result;
394 struct inode *dir = parent->d_inode;
398 * First re-do the cached lookup just in case it was created
399 * while we waited for the directory semaphore..
401 * FIXME! This could use version numbering or similar to
402 * avoid unnecessary cache lookups.
404 * The "dcache_lock" is purely to protect the RCU list walker
405 * from concurrent renames at this point (we mustn't get false
406 * negatives from the RCU list walk here, unlike the optimistic
409 * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
411 result = d_lookup(parent, name);
413 struct dentry * dentry = d_alloc(parent, name);
414 result = ERR_PTR(-ENOMEM);
416 result = dir->i_op->lookup(dir, dentry, nd);
427 * Uhhuh! Nasty case: the cache was re-populated while
428 * we waited on the semaphore. Need to revalidate.
431 if (result->d_op && result->d_op->d_revalidate) {
432 if (!result->d_op->d_revalidate(result, nd) && !d_invalidate(result)) {
434 result = ERR_PTR(-ENOENT);
440 static int __emul_lookup_dentry(const char *, struct nameidata *);
444 walk_init_root(const char *name, struct nameidata *nd)
446 read_lock(¤t->fs->lock);
447 if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
448 nd->mnt = mntget(current->fs->altrootmnt);
449 nd->dentry = dget(current->fs->altroot);
450 read_unlock(¤t->fs->lock);
451 if (__emul_lookup_dentry(name,nd))
453 read_lock(¤t->fs->lock);
455 nd->mnt = mntget(current->fs->rootmnt);
456 nd->dentry = dget(current->fs->root);
457 read_unlock(¤t->fs->lock);
461 static inline int __vfs_follow_link(struct nameidata *nd, const char *link)
470 if (!walk_init_root(link, nd))
471 /* weird __emul_prefix() stuff did it */
474 res = link_path_walk(link, nd);
476 if (nd->depth || res || nd->last_type!=LAST_NORM)
479 * If it is an iterative symlinks resolution in open_namei() we
480 * have to copy the last component. And all that crap because of
481 * bloody create() on broken symlinks. Furrfu...
484 if (unlikely(!name)) {
488 strcpy(name, nd->last.name);
489 nd->last.name = name;
493 return PTR_ERR(link);
496 static inline int __do_follow_link(struct dentry *dentry, struct nameidata *nd)
500 touch_atime(nd->mnt, dentry);
501 nd_set_link(nd, NULL);
502 error = dentry->d_inode->i_op->follow_link(dentry, nd);
504 char *s = nd_get_link(nd);
506 error = __vfs_follow_link(nd, s);
507 if (dentry->d_inode->i_op->put_link)
508 dentry->d_inode->i_op->put_link(dentry, nd);
515 * This limits recursive symlink follows to 8, while
516 * limiting consecutive symlinks to 40.
518 * Without that kind of total limit, nasty chains of consecutive
519 * symlinks can cause almost arbitrarily long lookups.
521 static inline int do_follow_link(struct dentry *dentry, struct nameidata *nd)
524 if (current->link_count >= MAX_NESTED_LINKS)
526 if (current->total_link_count >= 40)
528 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
530 err = security_inode_follow_link(dentry, nd);
533 current->link_count++;
534 current->total_link_count++;
536 err = __do_follow_link(dentry, nd);
537 current->link_count--;
545 int follow_up(struct vfsmount **mnt, struct dentry **dentry)
547 struct vfsmount *parent;
548 struct dentry *mountpoint;
549 spin_lock(&vfsmount_lock);
550 parent=(*mnt)->mnt_parent;
551 if (parent == *mnt) {
552 spin_unlock(&vfsmount_lock);
556 mountpoint=dget((*mnt)->mnt_mountpoint);
557 spin_unlock(&vfsmount_lock);
559 *dentry = mountpoint;
565 /* no need for dcache_lock, as serialization is taken care in
568 static int follow_mount(struct vfsmount **mnt, struct dentry **dentry)
571 while (d_mountpoint(*dentry)) {
572 struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
578 *dentry = dget(mounted->mnt_root);
584 /* no need for dcache_lock, as serialization is taken care in
587 static inline int __follow_down(struct vfsmount **mnt, struct dentry **dentry)
589 struct vfsmount *mounted;
591 mounted = lookup_mnt(*mnt, *dentry);
596 *dentry = dget(mounted->mnt_root);
602 int follow_down(struct vfsmount **mnt, struct dentry **dentry)
604 return __follow_down(mnt,dentry);
607 static inline void follow_dotdot(struct vfsmount **mnt, struct dentry **dentry)
610 struct vfsmount *parent;
611 struct dentry *old = *dentry;
613 read_lock(¤t->fs->lock);
614 if (*dentry == current->fs->root &&
615 *mnt == current->fs->rootmnt) {
616 read_unlock(¤t->fs->lock);
619 read_unlock(¤t->fs->lock);
620 spin_lock(&dcache_lock);
621 if (*dentry != (*mnt)->mnt_root) {
622 *dentry = dget((*dentry)->d_parent);
623 spin_unlock(&dcache_lock);
627 spin_unlock(&dcache_lock);
628 spin_lock(&vfsmount_lock);
629 parent = (*mnt)->mnt_parent;
630 if (parent == *mnt) {
631 spin_unlock(&vfsmount_lock);
635 *dentry = dget((*mnt)->mnt_mountpoint);
636 spin_unlock(&vfsmount_lock);
641 follow_mount(mnt, dentry);
645 struct vfsmount *mnt;
646 struct dentry *dentry;
650 * It's more convoluted than I'd like it to be, but... it's still fairly
651 * small and for now I'd prefer to have fast path as straight as possible.
652 * It _is_ time-critical.
654 static int do_lookup(struct nameidata *nd, struct qstr *name,
657 struct vfsmount *mnt = nd->mnt;
658 struct dentry *dentry = __d_lookup(nd->dentry, name);
662 if (dentry->d_op && dentry->d_op->d_revalidate)
663 goto need_revalidate;
666 path->dentry = dentry;
670 dentry = real_lookup(nd->dentry, name, nd);
676 if (dentry->d_op->d_revalidate(dentry, nd))
678 if (d_invalidate(dentry))
684 return PTR_ERR(dentry);
690 * This is the basic name resolution function, turning a pathname
691 * into the final dentry.
693 * We expect 'base' to be positive and a directory.
695 static fastcall int __link_path_walk(const char * name, struct nameidata *nd)
700 unsigned int lookup_flags = nd->flags;
707 inode = nd->dentry->d_inode;
709 lookup_flags = LOOKUP_FOLLOW;
711 /* At this point we know we have a real path component. */
717 err = exec_permission_lite(inode, nd);
718 if (err == -EAGAIN) {
719 err = permission(inode, MAY_EXEC, nd);
725 c = *(const unsigned char *)name;
727 hash = init_name_hash();
730 hash = partial_name_hash(c, hash);
731 c = *(const unsigned char *)name;
732 } while (c && (c != '/'));
733 this.len = name - (const char *) this.name;
734 this.hash = end_name_hash(hash);
736 /* remove trailing slashes? */
739 while (*++name == '/');
741 goto last_with_slashes;
744 * "." and ".." are special - ".." especially so because it has
745 * to be able to know about the current root directory and
746 * parent relationships.
748 if (this.name[0] == '.') switch (this.len) {
752 if (this.name[1] != '.')
754 follow_dotdot(&nd->mnt, &nd->dentry);
755 inode = nd->dentry->d_inode;
761 * See if the low-level filesystem might want
762 * to use its own hash..
764 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
765 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
769 nd->flags |= LOOKUP_CONTINUE;
770 /* This does the actual lookups.. */
771 err = do_lookup(nd, &this, &next);
774 /* Check mountpoints.. */
775 follow_mount(&next.mnt, &next.dentry);
778 inode = next.dentry->d_inode;
785 if (inode->i_op->follow_link) {
787 err = do_follow_link(next.dentry, nd);
793 inode = nd->dentry->d_inode;
802 nd->dentry = next.dentry;
805 if (!inode->i_op->lookup)
808 /* here ends the main loop */
811 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
813 nd->flags &= ~LOOKUP_CONTINUE;
814 if (lookup_flags & LOOKUP_PARENT)
816 if (this.name[0] == '.') switch (this.len) {
820 if (this.name[1] != '.')
822 follow_dotdot(&nd->mnt, &nd->dentry);
823 inode = nd->dentry->d_inode;
828 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
829 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
833 err = do_lookup(nd, &this, &next);
836 follow_mount(&next.mnt, &next.dentry);
837 inode = next.dentry->d_inode;
838 if ((lookup_flags & LOOKUP_FOLLOW)
839 && inode && inode->i_op && inode->i_op->follow_link) {
841 err = do_follow_link(next.dentry, nd);
846 inode = nd->dentry->d_inode;
850 nd->dentry = next.dentry;
855 if (lookup_flags & LOOKUP_DIRECTORY) {
857 if (!inode->i_op || !inode->i_op->lookup)
863 nd->last_type = LAST_NORM;
864 if (this.name[0] != '.')
867 nd->last_type = LAST_DOT;
868 else if (this.len == 2 && this.name[1] == '.')
869 nd->last_type = LAST_DOTDOT;
874 * We bypassed the ordinary revalidation routines.
875 * We may need to check the cached dentry for staleness.
877 if (nd->dentry && nd->dentry->d_sb &&
878 (nd->dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
880 /* Note: we do not d_invalidate() */
881 if (!nd->dentry->d_op->d_revalidate(nd->dentry, nd))
896 * Wrapper to retry pathname resolution whenever the underlying
897 * file system returns an ESTALE.
899 * Retry the whole path once, forcing real lookup requests
900 * instead of relying on the dcache.
902 int fastcall link_path_walk(const char *name, struct nameidata *nd)
904 struct nameidata save = *nd;
907 /* make sure the stuff we saved doesn't go away */
911 result = __link_path_walk(name, nd);
912 if (result == -ESTALE) {
916 nd->flags |= LOOKUP_REVAL;
917 result = __link_path_walk(name, nd);
926 int fastcall path_walk(const char * name, struct nameidata *nd)
928 current->total_link_count = 0;
929 return link_path_walk(name, nd);
933 /* returns 1 if everything is done */
934 static int __emul_lookup_dentry(const char *name, struct nameidata *nd)
936 if (path_walk(name, nd))
937 return 0; /* something went wrong... */
939 if (!nd->dentry->d_inode || S_ISDIR(nd->dentry->d_inode->i_mode)) {
940 struct dentry *old_dentry = nd->dentry;
941 struct vfsmount *old_mnt = nd->mnt;
942 struct qstr last = nd->last;
943 int last_type = nd->last_type;
945 * NAME was not found in alternate root or it's a directory. Try to find
946 * it in the normal root:
948 nd->last_type = LAST_ROOT;
949 read_lock(¤t->fs->lock);
950 nd->mnt = mntget(current->fs->rootmnt);
951 nd->dentry = dget(current->fs->root);
952 read_unlock(¤t->fs->lock);
953 if (path_walk(name, nd) == 0) {
954 if (nd->dentry->d_inode) {
961 nd->dentry = old_dentry;
964 nd->last_type = last_type;
969 void set_fs_altroot(void)
971 char *emul = __emul_prefix();
973 struct vfsmount *mnt = NULL, *oldmnt;
974 struct dentry *dentry = NULL, *olddentry;
979 err = path_lookup(emul, LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_NOALT, &nd);
985 write_lock(¤t->fs->lock);
986 oldmnt = current->fs->altrootmnt;
987 olddentry = current->fs->altroot;
988 current->fs->altrootmnt = mnt;
989 current->fs->altroot = dentry;
990 write_unlock(¤t->fs->lock);
997 int fastcall path_lookup(const char *name, unsigned int flags, struct nameidata *nd)
1001 nd->last_type = LAST_ROOT; /* if there are only slashes... */
1005 read_lock(¤t->fs->lock);
1007 if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
1008 nd->mnt = mntget(current->fs->altrootmnt);
1009 nd->dentry = dget(current->fs->altroot);
1010 read_unlock(¤t->fs->lock);
1011 if (__emul_lookup_dentry(name,nd))
1013 read_lock(¤t->fs->lock);
1015 nd->mnt = mntget(current->fs->rootmnt);
1016 nd->dentry = dget(current->fs->root);
1018 nd->mnt = mntget(current->fs->pwdmnt);
1019 nd->dentry = dget(current->fs->pwd);
1021 read_unlock(¤t->fs->lock);
1022 current->total_link_count = 0;
1023 retval = link_path_walk(name, nd);
1024 if (unlikely(current->audit_context
1025 && nd && nd->dentry && nd->dentry->d_inode))
1026 audit_inode(name, nd->dentry->d_inode);
1031 * Restricted form of lookup. Doesn't follow links, single-component only,
1032 * needs parent already locked. Doesn't follow mounts.
1035 static struct dentry * __lookup_hash(struct qstr *name, struct dentry * base, struct nameidata *nd)
1037 struct dentry * dentry;
1038 struct inode *inode;
1041 inode = base->d_inode;
1042 err = permission(inode, MAY_EXEC, nd);
1043 dentry = ERR_PTR(err);
1048 * See if the low-level filesystem might want
1049 * to use its own hash..
1051 if (base->d_op && base->d_op->d_hash) {
1052 err = base->d_op->d_hash(base, name);
1053 dentry = ERR_PTR(err);
1058 dentry = cached_lookup(base, name, nd);
1060 struct dentry *new = d_alloc(base, name);
1061 dentry = ERR_PTR(-ENOMEM);
1064 dentry = inode->i_op->lookup(inode, new, nd);
1074 struct dentry * lookup_hash(struct qstr *name, struct dentry * base)
1076 return __lookup_hash(name, base, NULL);
1080 struct dentry * lookup_one_len(const char * name, struct dentry * base, int len)
1091 hash = init_name_hash();
1093 c = *(const unsigned char *)name++;
1094 if (c == '/' || c == '\0')
1096 hash = partial_name_hash(c, hash);
1098 this.hash = end_name_hash(hash);
1100 return lookup_hash(&this, base);
1102 return ERR_PTR(-EACCES);
1108 * is used by most simple commands to get the inode of a specified name.
1109 * Open, link etc use their own routines, but this is enough for things
1112 * namei exists in two versions: namei/lnamei. The only difference is
1113 * that namei follows links, while lnamei does not.
1116 int fastcall __user_walk(const char __user *name, unsigned flags, struct nameidata *nd)
1118 char *tmp = getname(name);
1119 int err = PTR_ERR(tmp);
1122 err = path_lookup(tmp, flags, nd);
1129 * It's inline, so penalty for filesystems that don't use sticky bit is
1132 static inline int check_sticky(struct inode *dir, struct inode *inode)
1134 if (!(dir->i_mode & S_ISVTX))
1136 if (inode->i_uid == current->fsuid)
1138 if (dir->i_uid == current->fsuid)
1140 return !capable(CAP_FOWNER);
1144 * Check whether we can remove a link victim from directory dir, check
1145 * whether the type of victim is right.
1146 * 1. We can't do it if dir is read-only (done in permission())
1147 * 2. We should have write and exec permissions on dir
1148 * 3. We can't remove anything from append-only dir
1149 * 4. We can't do anything with immutable dir (done in permission())
1150 * 5. If the sticky bit on dir is set we should either
1151 * a. be owner of dir, or
1152 * b. be owner of victim, or
1153 * c. have CAP_FOWNER capability
1154 * 6. If the victim is append-only or immutable we can't do antyhing with
1155 * links pointing to it.
1156 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1157 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1158 * 9. We can't remove a root or mountpoint.
1159 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1160 * nfs_async_unlink().
1162 static inline int may_delete(struct inode *dir,struct dentry *victim,int isdir)
1166 if (!victim->d_inode)
1169 BUG_ON(victim->d_parent->d_inode != dir);
1171 error = permission(dir,MAY_WRITE | MAY_EXEC, NULL);
1176 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1177 IS_IMMUTABLE(victim->d_inode))
1180 if (!S_ISDIR(victim->d_inode->i_mode))
1182 if (IS_ROOT(victim))
1184 } else if (S_ISDIR(victim->d_inode->i_mode))
1186 if (IS_DEADDIR(dir))
1188 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1193 /* Check whether we can create an object with dentry child in directory
1195 * 1. We can't do it if child already exists (open has special treatment for
1196 * this case, but since we are inlined it's OK)
1197 * 2. We can't do it if dir is read-only (done in permission())
1198 * 3. We should have write and exec permissions on dir
1199 * 4. We can't do it if dir is immutable (done in permission())
1201 static inline int may_create(struct inode *dir, struct dentry *child,
1202 struct nameidata *nd)
1206 if (IS_DEADDIR(dir))
1208 return permission(dir,MAY_WRITE | MAY_EXEC, nd);
1212 * Special case: O_CREAT|O_EXCL implies O_NOFOLLOW for security
1215 * O_DIRECTORY translates into forcing a directory lookup.
1217 static inline int lookup_flags(unsigned int f)
1219 unsigned long retval = LOOKUP_FOLLOW;
1222 retval &= ~LOOKUP_FOLLOW;
1224 if ((f & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
1225 retval &= ~LOOKUP_FOLLOW;
1227 if (f & O_DIRECTORY)
1228 retval |= LOOKUP_DIRECTORY;
1234 * p1 and p2 should be directories on the same fs.
1236 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1241 down(&p1->d_inode->i_sem);
1245 down(&p1->d_inode->i_sb->s_vfs_rename_sem);
1247 for (p = p1; p->d_parent != p; p = p->d_parent) {
1248 if (p->d_parent == p2) {
1249 down(&p2->d_inode->i_sem);
1250 down(&p1->d_inode->i_sem);
1255 for (p = p2; p->d_parent != p; p = p->d_parent) {
1256 if (p->d_parent == p1) {
1257 down(&p1->d_inode->i_sem);
1258 down(&p2->d_inode->i_sem);
1263 down(&p1->d_inode->i_sem);
1264 down(&p2->d_inode->i_sem);
1268 void unlock_rename(struct dentry *p1, struct dentry *p2)
1270 up(&p1->d_inode->i_sem);
1272 up(&p2->d_inode->i_sem);
1273 up(&p1->d_inode->i_sb->s_vfs_rename_sem);
1277 int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1278 struct nameidata *nd)
1280 int error = may_create(dir, dentry, nd);
1285 if (!dir->i_op || !dir->i_op->create)
1286 return -EACCES; /* shouldn't it be ENOSYS? */
1289 error = security_inode_create(dir, dentry, mode);
1293 error = dir->i_op->create(dir, dentry, mode, nd);
1295 inode_dir_notify(dir, DN_CREATE);
1296 security_inode_post_create(dir, dentry, mode);
1301 int may_open(struct nameidata *nd, int acc_mode, int flag)
1303 struct dentry *dentry = nd->dentry;
1304 struct inode *inode = dentry->d_inode;
1310 if (S_ISLNK(inode->i_mode))
1313 if (S_ISDIR(inode->i_mode) && (flag & FMODE_WRITE))
1316 error = permission(inode, acc_mode, nd);
1321 * FIFO's, sockets and device files are special: they don't
1322 * actually live on the filesystem itself, and as such you
1323 * can write to them even if the filesystem is read-only.
1325 if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
1327 } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
1328 if (nd->mnt->mnt_flags & MNT_NODEV)
1332 } else if (IS_RDONLY(inode) && (flag & FMODE_WRITE))
1335 * An append-only file must be opened in append mode for writing.
1337 if (IS_APPEND(inode)) {
1338 if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
1344 /* O_NOATIME can only be set by the owner or superuser */
1345 if (flag & O_NOATIME)
1346 if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
1350 * Ensure there are no outstanding leases on the file.
1352 error = break_lease(inode, flag);
1356 if (flag & O_TRUNC) {
1357 error = get_write_access(inode);
1362 * Refuse to truncate files with mandatory locks held on them.
1364 error = locks_verify_locked(inode);
1368 error = do_truncate(dentry, 0);
1370 put_write_access(inode);
1374 if (flag & FMODE_WRITE)
1383 * namei for open - this is in fact almost the whole open-routine.
1385 * Note that the low bits of "flag" aren't the same as in the open
1386 * system call - they are 00 - no permissions needed
1387 * 01 - read permission needed
1388 * 10 - write permission needed
1389 * 11 - read/write permissions needed
1390 * which is a lot more logical, and also allows the "no perm" needed
1391 * for symlinks (where the permissions are checked later).
1394 int open_namei(const char * pathname, int flag, int mode, struct nameidata *nd)
1396 int acc_mode, error = 0;
1397 struct dentry *dentry;
1401 acc_mode = ACC_MODE(flag);
1403 /* Allow the LSM permission hook to distinguish append
1404 access from general write access. */
1405 if (flag & O_APPEND)
1406 acc_mode |= MAY_APPEND;
1408 /* Fill in the open() intent data */
1409 nd->intent.open.flags = flag;
1410 nd->intent.open.create_mode = mode;
1413 * The simplest case - just a plain lookup.
1415 if (!(flag & O_CREAT)) {
1416 error = path_lookup(pathname, lookup_flags(flag)|LOOKUP_OPEN, nd);
1423 * Create - we need to know the parent.
1425 error = path_lookup(pathname, LOOKUP_PARENT|LOOKUP_OPEN|LOOKUP_CREATE, nd);
1430 * We have the parent and last component. First of all, check
1431 * that we are not asked to creat(2) an obvious directory - that
1435 if (nd->last_type != LAST_NORM || nd->last.name[nd->last.len])
1439 nd->flags &= ~LOOKUP_PARENT;
1440 down(&dir->d_inode->i_sem);
1441 dentry = __lookup_hash(&nd->last, nd->dentry, nd);
1444 error = PTR_ERR(dentry);
1445 if (IS_ERR(dentry)) {
1446 up(&dir->d_inode->i_sem);
1450 /* Negative dentry, just create the file */
1451 if (!dentry->d_inode) {
1452 if (!IS_POSIXACL(dir->d_inode))
1453 mode &= ~current->fs->umask;
1454 error = vfs_create(dir->d_inode, dentry, mode, nd);
1455 up(&dir->d_inode->i_sem);
1457 nd->dentry = dentry;
1460 /* Don't check for write permission, don't truncate */
1467 * It already exists.
1469 up(&dir->d_inode->i_sem);
1475 if (d_mountpoint(dentry)) {
1477 if (flag & O_NOFOLLOW)
1479 while (__follow_down(&nd->mnt,&dentry) && d_mountpoint(dentry));
1482 if (!dentry->d_inode)
1484 if (dentry->d_inode->i_op && dentry->d_inode->i_op->follow_link)
1488 nd->dentry = dentry;
1490 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode))
1493 error = may_open(nd, acc_mode, flag);
1506 if (flag & O_NOFOLLOW)
1509 * This is subtle. Instead of calling do_follow_link() we do the
1510 * thing by hands. The reason is that this way we have zero link_count
1511 * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1512 * After that we have the parent and last component, i.e.
1513 * we are in the same situation as after the first path_walk().
1514 * Well, almost - if the last component is normal we get its copy
1515 * stored in nd->last.name and we will have to putname() it when we
1516 * are done. Procfs-like symlinks just set LAST_BIND.
1518 nd->flags |= LOOKUP_PARENT;
1519 error = security_inode_follow_link(dentry, nd);
1522 error = __do_follow_link(dentry, nd);
1526 nd->flags &= ~LOOKUP_PARENT;
1527 if (nd->last_type == LAST_BIND) {
1528 dentry = nd->dentry;
1532 if (nd->last_type != LAST_NORM)
1534 if (nd->last.name[nd->last.len]) {
1535 putname(nd->last.name);
1540 putname(nd->last.name);
1544 down(&dir->d_inode->i_sem);
1545 dentry = __lookup_hash(&nd->last, nd->dentry, nd);
1546 putname(nd->last.name);
1551 * lookup_create - lookup a dentry, creating it if it doesn't exist
1552 * @nd: nameidata info
1553 * @is_dir: directory flag
1555 * Simple function to lookup and return a dentry and create it
1556 * if it doesn't exist. Is SMP-safe.
1558 struct dentry *lookup_create(struct nameidata *nd, int is_dir)
1560 struct dentry *dentry;
1562 down(&nd->dentry->d_inode->i_sem);
1563 dentry = ERR_PTR(-EEXIST);
1564 if (nd->last_type != LAST_NORM)
1566 nd->flags &= ~LOOKUP_PARENT;
1567 dentry = lookup_hash(&nd->last, nd->dentry);
1570 if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode)
1575 dentry = ERR_PTR(-ENOENT);
1580 int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1582 int error = may_create(dir, dentry, NULL);
1587 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1590 if (!dir->i_op || !dir->i_op->mknod)
1593 error = security_inode_mknod(dir, dentry, mode, dev);
1598 error = dir->i_op->mknod(dir, dentry, mode, dev);
1600 inode_dir_notify(dir, DN_CREATE);
1601 security_inode_post_mknod(dir, dentry, mode, dev);
1606 asmlinkage long sys_mknod(const char __user * filename, int mode, unsigned dev)
1610 struct dentry * dentry;
1611 struct nameidata nd;
1615 tmp = getname(filename);
1617 return PTR_ERR(tmp);
1619 error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1622 dentry = lookup_create(&nd, 0);
1623 error = PTR_ERR(dentry);
1625 if (!IS_POSIXACL(nd.dentry->d_inode))
1626 mode &= ~current->fs->umask;
1627 if (!IS_ERR(dentry)) {
1628 switch (mode & S_IFMT) {
1629 case 0: case S_IFREG:
1630 error = vfs_create(nd.dentry->d_inode,dentry,mode,&nd);
1632 case S_IFCHR: case S_IFBLK:
1633 error = vfs_mknod(nd.dentry->d_inode,dentry,mode,
1634 new_decode_dev(dev));
1636 case S_IFIFO: case S_IFSOCK:
1637 error = vfs_mknod(nd.dentry->d_inode,dentry,mode,0);
1647 up(&nd.dentry->d_inode->i_sem);
1655 int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1657 int error = may_create(dir, dentry, NULL);
1662 if (!dir->i_op || !dir->i_op->mkdir)
1665 mode &= (S_IRWXUGO|S_ISVTX);
1666 error = security_inode_mkdir(dir, dentry, mode);
1671 error = dir->i_op->mkdir(dir, dentry, mode);
1673 inode_dir_notify(dir, DN_CREATE);
1674 security_inode_post_mkdir(dir,dentry, mode);
1679 asmlinkage long sys_mkdir(const char __user * pathname, int mode)
1684 tmp = getname(pathname);
1685 error = PTR_ERR(tmp);
1687 struct dentry *dentry;
1688 struct nameidata nd;
1690 error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1693 dentry = lookup_create(&nd, 1);
1694 error = PTR_ERR(dentry);
1695 if (!IS_ERR(dentry)) {
1696 if (!IS_POSIXACL(nd.dentry->d_inode))
1697 mode &= ~current->fs->umask;
1698 error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
1701 up(&nd.dentry->d_inode->i_sem);
1711 * We try to drop the dentry early: we should have
1712 * a usage count of 2 if we're the only user of this
1713 * dentry, and if that is true (possibly after pruning
1714 * the dcache), then we drop the dentry now.
1716 * A low-level filesystem can, if it choses, legally
1719 * if (!d_unhashed(dentry))
1722 * if it cannot handle the case of removing a directory
1723 * that is still in use by something else..
1725 void dentry_unhash(struct dentry *dentry)
1728 if (atomic_read(&dentry->d_count))
1729 shrink_dcache_parent(dentry);
1730 spin_lock(&dcache_lock);
1731 spin_lock(&dentry->d_lock);
1732 if (atomic_read(&dentry->d_count) == 2)
1734 spin_unlock(&dentry->d_lock);
1735 spin_unlock(&dcache_lock);
1738 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
1740 int error = may_delete(dir, dentry, 1);
1745 if (!dir->i_op || !dir->i_op->rmdir)
1750 down(&dentry->d_inode->i_sem);
1751 dentry_unhash(dentry);
1752 if (d_mountpoint(dentry))
1755 error = security_inode_rmdir(dir, dentry);
1757 error = dir->i_op->rmdir(dir, dentry);
1759 dentry->d_inode->i_flags |= S_DEAD;
1762 up(&dentry->d_inode->i_sem);
1764 inode_dir_notify(dir, DN_DELETE);
1772 asmlinkage long sys_rmdir(const char __user * pathname)
1776 struct dentry *dentry;
1777 struct nameidata nd;
1779 name = getname(pathname);
1781 return PTR_ERR(name);
1783 error = path_lookup(name, LOOKUP_PARENT, &nd);
1787 switch(nd.last_type) {
1798 down(&nd.dentry->d_inode->i_sem);
1799 dentry = lookup_hash(&nd.last, nd.dentry);
1800 error = PTR_ERR(dentry);
1801 if (!IS_ERR(dentry)) {
1802 error = vfs_rmdir(nd.dentry->d_inode, dentry);
1805 up(&nd.dentry->d_inode->i_sem);
1813 int vfs_unlink(struct inode *dir, struct dentry *dentry)
1815 int error = may_delete(dir, dentry, 0);
1820 if (!dir->i_op || !dir->i_op->unlink)
1825 down(&dentry->d_inode->i_sem);
1826 if (d_mountpoint(dentry))
1829 error = security_inode_unlink(dir, dentry);
1831 error = dir->i_op->unlink(dir, dentry);
1833 up(&dentry->d_inode->i_sem);
1835 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
1836 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
1838 inode_dir_notify(dir, DN_DELETE);
1844 * Make sure that the actual truncation of the file will occur outside its
1845 * directory's i_sem. Truncate can take a long time if there is a lot of
1846 * writeout happening, and we don't want to prevent access to the directory
1847 * while waiting on the I/O.
1849 asmlinkage long sys_unlink(const char __user * pathname)
1853 struct dentry *dentry;
1854 struct nameidata nd;
1855 struct inode *inode = NULL;
1857 name = getname(pathname);
1859 return PTR_ERR(name);
1861 error = path_lookup(name, LOOKUP_PARENT, &nd);
1865 if (nd.last_type != LAST_NORM)
1867 down(&nd.dentry->d_inode->i_sem);
1868 dentry = lookup_hash(&nd.last, nd.dentry);
1869 error = PTR_ERR(dentry);
1870 if (!IS_ERR(dentry)) {
1871 /* Why not before? Because we want correct error value */
1872 if (nd.last.name[nd.last.len])
1874 inode = dentry->d_inode;
1876 atomic_inc(&inode->i_count);
1877 error = vfs_unlink(nd.dentry->d_inode, dentry);
1881 up(&nd.dentry->d_inode->i_sem);
1883 iput(inode); /* truncate the inode here */
1891 error = !dentry->d_inode ? -ENOENT :
1892 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
1896 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname, int mode)
1898 int error = may_create(dir, dentry, NULL);
1903 if (!dir->i_op || !dir->i_op->symlink)
1906 error = security_inode_symlink(dir, dentry, oldname);
1911 error = dir->i_op->symlink(dir, dentry, oldname);
1913 inode_dir_notify(dir, DN_CREATE);
1914 security_inode_post_symlink(dir, dentry, oldname);
1919 asmlinkage long sys_symlink(const char __user * oldname, const char __user * newname)
1925 from = getname(oldname);
1927 return PTR_ERR(from);
1928 to = getname(newname);
1929 error = PTR_ERR(to);
1931 struct dentry *dentry;
1932 struct nameidata nd;
1934 error = path_lookup(to, LOOKUP_PARENT, &nd);
1937 dentry = lookup_create(&nd, 0);
1938 error = PTR_ERR(dentry);
1939 if (!IS_ERR(dentry)) {
1940 error = vfs_symlink(nd.dentry->d_inode, dentry, from, S_IALLUGO);
1943 up(&nd.dentry->d_inode->i_sem);
1952 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
1954 struct inode *inode = old_dentry->d_inode;
1960 error = may_create(dir, new_dentry, NULL);
1964 if (dir->i_sb != inode->i_sb)
1968 * A link to an append-only or immutable file cannot be created.
1970 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1972 if (!dir->i_op || !dir->i_op->link)
1974 if (S_ISDIR(old_dentry->d_inode->i_mode))
1977 error = security_inode_link(old_dentry, dir, new_dentry);
1981 down(&old_dentry->d_inode->i_sem);
1983 error = dir->i_op->link(old_dentry, dir, new_dentry);
1984 up(&old_dentry->d_inode->i_sem);
1986 inode_dir_notify(dir, DN_CREATE);
1987 security_inode_post_link(old_dentry, dir, new_dentry);
1993 * Hardlinks are often used in delicate situations. We avoid
1994 * security-related surprises by not following symlinks on the
1997 * We don't follow them on the oldname either to be compatible
1998 * with linux 2.0, and to avoid hard-linking to directories
1999 * and other special files. --ADM
2001 asmlinkage long sys_link(const char __user * oldname, const char __user * newname)
2003 struct dentry *new_dentry;
2004 struct nameidata nd, old_nd;
2008 to = getname(newname);
2012 error = __user_walk(oldname, 0, &old_nd);
2015 error = path_lookup(to, LOOKUP_PARENT, &nd);
2019 if (old_nd.mnt != nd.mnt)
2021 new_dentry = lookup_create(&nd, 0);
2022 error = PTR_ERR(new_dentry);
2023 if (!IS_ERR(new_dentry)) {
2024 error = vfs_link(old_nd.dentry, nd.dentry->d_inode, new_dentry);
2027 up(&nd.dentry->d_inode->i_sem);
2031 path_release(&old_nd);
2039 * The worst of all namespace operations - renaming directory. "Perverted"
2040 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2042 * a) we can get into loop creation. Check is done in is_subdir().
2043 * b) race potential - two innocent renames can create a loop together.
2044 * That's where 4.4 screws up. Current fix: serialization on
2045 * sb->s_vfs_rename_sem. We might be more accurate, but that's another
2047 * c) we have to lock _three_ objects - parents and victim (if it exists).
2048 * And that - after we got ->i_sem on parents (until then we don't know
2049 * whether the target exists). Solution: try to be smart with locking
2050 * order for inodes. We rely on the fact that tree topology may change
2051 * only under ->s_vfs_rename_sem _and_ that parent of the object we
2052 * move will be locked. Thus we can rank directories by the tree
2053 * (ancestors first) and rank all non-directories after them.
2054 * That works since everybody except rename does "lock parent, lookup,
2055 * lock child" and rename is under ->s_vfs_rename_sem.
2056 * HOWEVER, it relies on the assumption that any object with ->lookup()
2057 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2058 * we'd better make sure that there's no link(2) for them.
2059 * d) some filesystems don't support opened-but-unlinked directories,
2060 * either because of layout or because they are not ready to deal with
2061 * all cases correctly. The latter will be fixed (taking this sort of
2062 * stuff into VFS), but the former is not going away. Solution: the same
2063 * trick as in rmdir().
2064 * e) conversion from fhandle to dentry may come in the wrong moment - when
2065 * we are removing the target. Solution: we will have to grab ->i_sem
2066 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
2067 * ->i_sem on parents, which works but leads to some truely excessive
2070 int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
2071 struct inode *new_dir, struct dentry *new_dentry)
2074 struct inode *target;
2077 * If we are going to change the parent - check write permissions,
2078 * we'll need to flip '..'.
2080 if (new_dir != old_dir) {
2081 error = permission(old_dentry->d_inode, MAY_WRITE, NULL);
2086 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2090 target = new_dentry->d_inode;
2092 down(&target->i_sem);
2093 dentry_unhash(new_dentry);
2095 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2098 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2101 target->i_flags |= S_DEAD;
2103 if (d_unhashed(new_dentry))
2104 d_rehash(new_dentry);
2108 d_move(old_dentry,new_dentry);
2109 security_inode_post_rename(old_dir, old_dentry,
2110 new_dir, new_dentry);
2115 int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
2116 struct inode *new_dir, struct dentry *new_dentry)
2118 struct inode *target;
2121 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2126 target = new_dentry->d_inode;
2128 down(&target->i_sem);
2129 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2132 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2134 /* The following d_move() should become unconditional */
2135 if (!(old_dir->i_sb->s_type->fs_flags & FS_ODD_RENAME))
2136 d_move(old_dentry, new_dentry);
2137 security_inode_post_rename(old_dir, old_dentry, new_dir, new_dentry);
2145 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
2146 struct inode *new_dir, struct dentry *new_dentry)
2149 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
2151 if (old_dentry->d_inode == new_dentry->d_inode)
2154 error = may_delete(old_dir, old_dentry, is_dir);
2158 if (!new_dentry->d_inode)
2159 error = may_create(new_dir, new_dentry, NULL);
2161 error = may_delete(new_dir, new_dentry, is_dir);
2165 if (!old_dir->i_op || !old_dir->i_op->rename)
2168 DQUOT_INIT(old_dir);
2169 DQUOT_INIT(new_dir);
2172 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
2174 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
2176 if (old_dir == new_dir)
2177 inode_dir_notify(old_dir, DN_RENAME);
2179 inode_dir_notify(old_dir, DN_DELETE);
2180 inode_dir_notify(new_dir, DN_CREATE);
2186 static inline int do_rename(const char * oldname, const char * newname)
2189 struct dentry * old_dir, * new_dir;
2190 struct dentry * old_dentry, *new_dentry;
2191 struct dentry * trap;
2192 struct nameidata oldnd, newnd;
2194 error = path_lookup(oldname, LOOKUP_PARENT, &oldnd);
2198 error = path_lookup(newname, LOOKUP_PARENT, &newnd);
2203 if (oldnd.mnt != newnd.mnt)
2206 old_dir = oldnd.dentry;
2208 if (oldnd.last_type != LAST_NORM)
2211 new_dir = newnd.dentry;
2212 if (newnd.last_type != LAST_NORM)
2215 trap = lock_rename(new_dir, old_dir);
2217 old_dentry = lookup_hash(&oldnd.last, old_dir);
2218 error = PTR_ERR(old_dentry);
2219 if (IS_ERR(old_dentry))
2221 /* source must exist */
2223 if (!old_dentry->d_inode)
2225 /* unless the source is a directory trailing slashes give -ENOTDIR */
2226 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
2228 if (oldnd.last.name[oldnd.last.len])
2230 if (newnd.last.name[newnd.last.len])
2233 /* source should not be ancestor of target */
2235 if (old_dentry == trap)
2237 new_dentry = lookup_hash(&newnd.last, new_dir);
2238 error = PTR_ERR(new_dentry);
2239 if (IS_ERR(new_dentry))
2241 /* target should not be an ancestor of source */
2243 if (new_dentry == trap)
2246 error = vfs_rename(old_dir->d_inode, old_dentry,
2247 new_dir->d_inode, new_dentry);
2253 unlock_rename(new_dir, old_dir);
2255 path_release(&newnd);
2257 path_release(&oldnd);
2262 asmlinkage long sys_rename(const char __user * oldname, const char __user * newname)
2268 from = getname(oldname);
2270 return PTR_ERR(from);
2271 to = getname(newname);
2272 error = PTR_ERR(to);
2274 error = do_rename(from,to);
2281 int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
2285 len = PTR_ERR(link);
2290 if (len > (unsigned) buflen)
2292 if (copy_to_user(buffer, link, len))
2299 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
2300 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
2301 * using) it for any given inode is up to filesystem.
2303 int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2305 struct nameidata nd;
2308 res = dentry->d_inode->i_op->follow_link(dentry, &nd);
2310 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
2311 if (dentry->d_inode->i_op->put_link)
2312 dentry->d_inode->i_op->put_link(dentry, &nd);
2317 int vfs_follow_link(struct nameidata *nd, const char *link)
2319 return __vfs_follow_link(nd, link);
2322 /* get the link contents into pagecache */
2323 static char *page_getlink(struct dentry * dentry, struct page **ppage)
2326 struct address_space *mapping = dentry->d_inode->i_mapping;
2327 page = read_cache_page(mapping, 0, (filler_t *)mapping->a_ops->readpage,
2331 wait_on_page_locked(page);
2332 if (!PageUptodate(page))
2338 page_cache_release(page);
2339 return ERR_PTR(-EIO);
2345 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2347 struct page *page = NULL;
2348 char *s = page_getlink(dentry, &page);
2349 int res = vfs_readlink(dentry,buffer,buflen,s);
2352 page_cache_release(page);
2357 int page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
2360 nd_set_link(nd, page_getlink(dentry, &page));
2364 void page_put_link(struct dentry *dentry, struct nameidata *nd)
2366 if (!IS_ERR(nd_get_link(nd))) {
2368 page = find_get_page(dentry->d_inode->i_mapping, 0);
2372 page_cache_release(page);
2373 page_cache_release(page);
2377 int page_symlink(struct inode *inode, const char *symname, int len)
2379 struct address_space *mapping = inode->i_mapping;
2380 struct page *page = grab_cache_page(mapping, 0);
2386 err = mapping->a_ops->prepare_write(NULL, page, 0, len-1);
2389 kaddr = kmap_atomic(page, KM_USER0);
2390 memcpy(kaddr, symname, len-1);
2391 kunmap_atomic(kaddr, KM_USER0);
2392 mapping->a_ops->commit_write(NULL, page, 0, len-1);
2394 * Notice that we are _not_ going to block here - end of page is
2395 * unmapped, so this will only try to map the rest of page, see
2396 * that it is unmapped (typically even will not look into inode -
2397 * ->i_size will be enough for everything) and zero it out.
2398 * OTOH it's obviously correct and should make the page up-to-date.
2400 if (!PageUptodate(page)) {
2401 err = mapping->a_ops->readpage(NULL, page);
2402 wait_on_page_locked(page);
2406 page_cache_release(page);
2409 mark_inode_dirty(inode);
2413 page_cache_release(page);
2418 struct inode_operations page_symlink_inode_operations = {
2419 .readlink = generic_readlink,
2420 .follow_link = page_follow_link_light,
2421 .put_link = page_put_link,
2424 EXPORT_SYMBOL(__user_walk);
2425 EXPORT_SYMBOL(follow_down);
2426 EXPORT_SYMBOL(follow_up);
2427 EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
2428 EXPORT_SYMBOL(getname);
2429 EXPORT_SYMBOL(lock_rename);
2430 EXPORT_SYMBOL(lookup_hash);
2431 EXPORT_SYMBOL(lookup_one_len);
2432 EXPORT_SYMBOL(page_follow_link_light);
2433 EXPORT_SYMBOL(page_put_link);
2434 EXPORT_SYMBOL(page_readlink);
2435 EXPORT_SYMBOL(page_symlink);
2436 EXPORT_SYMBOL(page_symlink_inode_operations);
2437 EXPORT_SYMBOL(path_lookup);
2438 EXPORT_SYMBOL(path_release);
2439 EXPORT_SYMBOL(path_walk);
2440 EXPORT_SYMBOL(permission);
2441 EXPORT_SYMBOL(unlock_rename);
2442 EXPORT_SYMBOL(vfs_create);
2443 EXPORT_SYMBOL(vfs_follow_link);
2444 EXPORT_SYMBOL(vfs_link);
2445 EXPORT_SYMBOL(vfs_mkdir);
2446 EXPORT_SYMBOL(vfs_mknod);
2447 EXPORT_SYMBOL(generic_permission);
2448 EXPORT_SYMBOL(vfs_readlink);
2449 EXPORT_SYMBOL(vfs_rename);
2450 EXPORT_SYMBOL(vfs_rmdir);
2451 EXPORT_SYMBOL(vfs_symlink);
2452 EXPORT_SYMBOL(vfs_unlink);
2453 EXPORT_SYMBOL(dentry_unhash);
2454 EXPORT_SYMBOL(generic_readlink);