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 <asm/namei.h>
30 #include <asm/uaccess.h>
32 #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
34 /* [Feb-1997 T. Schoebel-Theuer]
35 * Fundamental changes in the pathname lookup mechanisms (namei)
36 * were necessary because of omirr. The reason is that omirr needs
37 * to know the _real_ pathname, not the user-supplied one, in case
38 * of symlinks (and also when transname replacements occur).
40 * The new code replaces the old recursive symlink resolution with
41 * an iterative one (in case of non-nested symlink chains). It does
42 * this with calls to <fs>_follow_link().
43 * As a side effect, dir_namei(), _namei() and follow_link() are now
44 * replaced with a single function lookup_dentry() that can handle all
45 * the special cases of the former code.
47 * With the new dcache, the pathname is stored at each inode, at least as
48 * long as the refcount of the inode is positive. As a side effect, the
49 * size of the dcache depends on the inode cache and thus is dynamic.
51 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
52 * resolution to correspond with current state of the code.
54 * Note that the symlink resolution is not *completely* iterative.
55 * There is still a significant amount of tail- and mid- recursion in
56 * the algorithm. Also, note that <fs>_readlink() is not used in
57 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
58 * may return different results than <fs>_follow_link(). Many virtual
59 * filesystems (including /proc) exhibit this behavior.
62 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
63 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
64 * and the name already exists in form of a symlink, try to create the new
65 * name indicated by the symlink. The old code always complained that the
66 * name already exists, due to not following the symlink even if its target
67 * is nonexistent. The new semantics affects also mknod() and link() when
68 * the name is a symlink pointing to a non-existant name.
70 * I don't know which semantics is the right one, since I have no access
71 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
72 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
73 * "old" one. Personally, I think the new semantics is much more logical.
74 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
75 * file does succeed in both HP-UX and SunOs, but not in Solaris
76 * and in the old Linux semantics.
79 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
80 * semantics. See the comments in "open_namei" and "do_link" below.
82 * [10-Sep-98 Alan Modra] Another symlink change.
85 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
86 * inside the path - always follow.
87 * in the last component in creation/removal/renaming - never follow.
88 * if LOOKUP_FOLLOW passed - follow.
89 * if the pathname has trailing slashes - follow.
90 * otherwise - don't follow.
91 * (applied in that order).
93 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
94 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
95 * During the 2.4 we need to fix the userland stuff depending on it -
96 * hopefully we will be able to get rid of that wart in 2.5. So far only
97 * XEmacs seems to be relying on it...
100 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
101 * implemented. Let's see if raised priority of ->s_vfs_rename_sem gives
102 * any extra contention...
105 /* In order to reduce some races, while at the same time doing additional
106 * checking and hopefully speeding things up, we copy filenames to the
107 * kernel data space before using them..
109 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
110 * PATH_MAX includes the nul terminator --RR.
112 static inline int do_getname(const char __user
*filename
, char *page
)
115 unsigned long len
= PATH_MAX
;
117 if ((unsigned long) filename
>= TASK_SIZE
) {
118 if (!segment_eq(get_fs(), KERNEL_DS
))
120 } else if (TASK_SIZE
- (unsigned long) filename
< PATH_MAX
)
121 len
= TASK_SIZE
- (unsigned long) filename
;
123 retval
= strncpy_from_user((char *)page
, filename
, len
);
127 return -ENAMETOOLONG
;
133 char * getname(const char __user
* filename
)
137 result
= ERR_PTR(-ENOMEM
);
140 int retval
= do_getname(filename
, tmp
);
145 result
= ERR_PTR(retval
);
154 * is used to check for read/write/execute permissions on a file.
155 * We use "fsuid" for this, letting us set arbitrary permissions
156 * for filesystem access without changing the "normal" uids which
157 * are used for other things..
159 int vfs_permission(struct inode
* inode
, int mask
)
161 umode_t mode
= inode
->i_mode
;
163 if (mask
& MAY_WRITE
) {
165 * Nobody gets write access to a read-only fs.
167 if (IS_RDONLY(inode
) &&
168 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
)))
172 * Nobody gets write access to an immutable file.
174 if (IS_IMMUTABLE(inode
))
178 if (current
->fsuid
== inode
->i_uid
)
180 else if (in_group_p(inode
->i_gid
))
184 * If the DACs are ok we don't need any capability check.
186 if (((mode
& mask
& (MAY_READ
|MAY_WRITE
|MAY_EXEC
)) == mask
))
190 * Read/write DACs are always overridable.
191 * Executable DACs are overridable if at least one exec bit is set.
193 if (!(mask
& MAY_EXEC
) ||
194 (inode
->i_mode
& S_IXUGO
) || S_ISDIR(inode
->i_mode
))
195 if (capable(CAP_DAC_OVERRIDE
))
199 * Searching includes executable on directories, else just read.
201 if (mask
== MAY_READ
|| (S_ISDIR(inode
->i_mode
) && !(mask
& MAY_WRITE
)))
202 if (capable(CAP_DAC_READ_SEARCH
))
208 int permission(struct inode
* inode
,int mask
, struct nameidata
*nd
)
213 /* Ordinary permission routines do not understand MAY_APPEND. */
214 submask
= mask
& ~MAY_APPEND
;
216 if (inode
->i_op
&& inode
->i_op
->permission
)
217 retval
= inode
->i_op
->permission(inode
, submask
, nd
);
219 retval
= vfs_permission(inode
, submask
);
223 return security_inode_permission(inode
, mask
, nd
);
227 * get_write_access() gets write permission for a file.
228 * put_write_access() releases this write permission.
229 * This is used for regular files.
230 * We cannot support write (and maybe mmap read-write shared) accesses and
231 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
232 * can have the following values:
233 * 0: no writers, no VM_DENYWRITE mappings
234 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
235 * > 0: (i_writecount) users are writing to the file.
237 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
238 * except for the cases where we don't hold i_writecount yet. Then we need to
239 * use {get,deny}_write_access() - these functions check the sign and refuse
240 * to do the change if sign is wrong. Exclusion between them is provided by
241 * the inode->i_lock spinlock.
244 int get_write_access(struct inode
* inode
)
246 spin_lock(&inode
->i_lock
);
247 if (atomic_read(&inode
->i_writecount
) < 0) {
248 spin_unlock(&inode
->i_lock
);
251 atomic_inc(&inode
->i_writecount
);
252 spin_unlock(&inode
->i_lock
);
257 int deny_write_access(struct file
* file
)
259 struct inode
*inode
= file
->f_dentry
->d_inode
;
261 spin_lock(&inode
->i_lock
);
262 if (atomic_read(&inode
->i_writecount
) > 0) {
263 spin_unlock(&inode
->i_lock
);
266 atomic_dec(&inode
->i_writecount
);
267 spin_unlock(&inode
->i_lock
);
272 void path_release(struct nameidata
*nd
)
279 * Internal lookup() using the new generic dcache.
282 static struct dentry
* cached_lookup(struct dentry
* parent
, struct qstr
* name
, struct nameidata
*nd
)
284 struct dentry
* dentry
= __d_lookup(parent
, name
);
286 /* lockess __d_lookup may fail due to concurrent d_move()
287 * in some unrelated directory, so try with d_lookup
290 dentry
= d_lookup(parent
, name
);
292 if (dentry
&& dentry
->d_op
&& dentry
->d_op
->d_revalidate
) {
293 if (!dentry
->d_op
->d_revalidate(dentry
, nd
) && !d_invalidate(dentry
)) {
302 * Short-cut version of permission(), for calling by
303 * path_walk(), when dcache lock is held. Combines parts
304 * of permission() and vfs_permission(), and tests ONLY for
305 * MAY_EXEC permission.
307 * If appropriate, check DAC only. If not appropriate, or
308 * short-cut DAC fails, then call permission() to do more
309 * complete permission check.
311 static inline int exec_permission_lite(struct inode
*inode
,
312 struct nameidata
*nd
)
314 umode_t mode
= inode
->i_mode
;
316 if ((inode
->i_op
&& inode
->i_op
->permission
))
319 if (current
->fsuid
== inode
->i_uid
)
321 else if (in_group_p(inode
->i_gid
))
327 if ((inode
->i_mode
& S_IXUGO
) && capable(CAP_DAC_OVERRIDE
))
330 if (S_ISDIR(inode
->i_mode
) && capable(CAP_DAC_READ_SEARCH
))
335 return security_inode_permission(inode
, MAY_EXEC
, nd
);
339 * This is called when everything else fails, and we actually have
340 * to go to the low-level filesystem to find out what we should do..
342 * We get the directory semaphore, and after getting that we also
343 * make sure that nobody added the entry to the dcache in the meantime..
346 static struct dentry
* real_lookup(struct dentry
* parent
, struct qstr
* name
, struct nameidata
*nd
)
348 struct dentry
* result
;
349 struct inode
*dir
= parent
->d_inode
;
353 * First re-do the cached lookup just in case it was created
354 * while we waited for the directory semaphore..
356 * FIXME! This could use version numbering or similar to
357 * avoid unnecessary cache lookups.
359 * The "dcache_lock" is purely to protect the RCU list walker
360 * from concurrent renames at this point (we mustn't get false
361 * negatives from the RCU list walk here, unlike the optimistic
364 * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
366 result
= d_lookup(parent
, name
);
368 struct dentry
* dentry
= d_alloc(parent
, name
);
369 result
= ERR_PTR(-ENOMEM
);
371 result
= dir
->i_op
->lookup(dir
, dentry
, nd
);
382 * Uhhuh! Nasty case: the cache was re-populated while
383 * we waited on the semaphore. Need to revalidate.
386 if (result
->d_op
&& result
->d_op
->d_revalidate
) {
387 if (!result
->d_op
->d_revalidate(result
, nd
) && !d_invalidate(result
)) {
389 result
= ERR_PTR(-ENOENT
);
396 * This limits recursive symlink follows to 8, while
397 * limiting consecutive symlinks to 40.
399 * Without that kind of total limit, nasty chains of consecutive
400 * symlinks can cause almost arbitrarily long lookups.
402 static inline int do_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
405 if (current
->link_count
>= 5)
407 if (current
->total_link_count
>= 40)
410 err
= security_inode_follow_link(dentry
, nd
);
413 current
->link_count
++;
414 current
->total_link_count
++;
415 touch_atime(nd
->mnt
, dentry
);
416 err
= dentry
->d_inode
->i_op
->follow_link(dentry
, nd
);
417 current
->link_count
--;
424 int follow_up(struct vfsmount
**mnt
, struct dentry
**dentry
)
426 struct vfsmount
*parent
;
427 struct dentry
*mountpoint
;
428 spin_lock(&vfsmount_lock
);
429 parent
=(*mnt
)->mnt_parent
;
430 if (parent
== *mnt
) {
431 spin_unlock(&vfsmount_lock
);
435 mountpoint
=dget((*mnt
)->mnt_mountpoint
);
436 spin_unlock(&vfsmount_lock
);
438 *dentry
= mountpoint
;
444 /* no need for dcache_lock, as serialization is taken care in
447 static int follow_mount(struct vfsmount
**mnt
, struct dentry
**dentry
)
450 while (d_mountpoint(*dentry
)) {
451 struct vfsmount
*mounted
= lookup_mnt(*mnt
, *dentry
);
457 *dentry
= dget(mounted
->mnt_root
);
463 /* no need for dcache_lock, as serialization is taken care in
466 static inline int __follow_down(struct vfsmount
**mnt
, struct dentry
**dentry
)
468 struct vfsmount
*mounted
;
470 mounted
= lookup_mnt(*mnt
, *dentry
);
475 *dentry
= dget(mounted
->mnt_root
);
481 int follow_down(struct vfsmount
**mnt
, struct dentry
**dentry
)
483 return __follow_down(mnt
,dentry
);
486 static inline void follow_dotdot(struct vfsmount
**mnt
, struct dentry
**dentry
)
489 struct vfsmount
*parent
;
490 struct dentry
*old
= *dentry
;
492 read_lock(¤t
->fs
->lock
);
493 if (*dentry
== current
->fs
->root
&&
494 *mnt
== current
->fs
->rootmnt
) {
495 read_unlock(¤t
->fs
->lock
);
498 read_unlock(¤t
->fs
->lock
);
499 spin_lock(&dcache_lock
);
500 if (*dentry
!= (*mnt
)->mnt_root
) {
501 *dentry
= dget((*dentry
)->d_parent
);
502 spin_unlock(&dcache_lock
);
506 spin_unlock(&dcache_lock
);
507 spin_lock(&vfsmount_lock
);
508 parent
= (*mnt
)->mnt_parent
;
509 if (parent
== *mnt
) {
510 spin_unlock(&vfsmount_lock
);
514 *dentry
= dget((*mnt
)->mnt_mountpoint
);
515 spin_unlock(&vfsmount_lock
);
520 follow_mount(mnt
, dentry
);
524 struct vfsmount
*mnt
;
525 struct dentry
*dentry
;
529 * It's more convoluted than I'd like it to be, but... it's still fairly
530 * small and for now I'd prefer to have fast path as straight as possible.
531 * It _is_ time-critical.
533 static int do_lookup(struct nameidata
*nd
, struct qstr
*name
,
536 struct vfsmount
*mnt
= nd
->mnt
;
537 struct dentry
*dentry
= __d_lookup(nd
->dentry
, name
);
541 if (dentry
->d_op
&& dentry
->d_op
->d_revalidate
)
542 goto need_revalidate
;
545 path
->dentry
= dentry
;
549 dentry
= real_lookup(nd
->dentry
, name
, nd
);
555 if (dentry
->d_op
->d_revalidate(dentry
, nd
))
557 if (d_invalidate(dentry
))
563 return PTR_ERR(dentry
);
569 * This is the basic name resolution function, turning a pathname
570 * into the final dentry.
572 * We expect 'base' to be positive and a directory.
574 int fastcall
link_path_walk(const char * name
, struct nameidata
*nd
)
579 unsigned int lookup_flags
= nd
->flags
;
586 inode
= nd
->dentry
->d_inode
;
587 if (current
->link_count
)
588 lookup_flags
= LOOKUP_FOLLOW
;
590 /* At this point we know we have a real path component. */
596 err
= exec_permission_lite(inode
, nd
);
597 if (err
== -EAGAIN
) {
598 err
= permission(inode
, MAY_EXEC
, nd
);
604 c
= *(const unsigned char *)name
;
606 hash
= init_name_hash();
609 hash
= partial_name_hash(c
, hash
);
610 c
= *(const unsigned char *)name
;
611 } while (c
&& (c
!= '/'));
612 this.len
= name
- (const char *) this.name
;
613 this.hash
= end_name_hash(hash
);
615 /* remove trailing slashes? */
618 while (*++name
== '/');
620 goto last_with_slashes
;
623 * "." and ".." are special - ".." especially so because it has
624 * to be able to know about the current root directory and
625 * parent relationships.
627 if (this.name
[0] == '.') switch (this.len
) {
631 if (this.name
[1] != '.')
633 follow_dotdot(&nd
->mnt
, &nd
->dentry
);
634 inode
= nd
->dentry
->d_inode
;
640 * See if the low-level filesystem might want
641 * to use its own hash..
643 if (nd
->dentry
->d_op
&& nd
->dentry
->d_op
->d_hash
) {
644 err
= nd
->dentry
->d_op
->d_hash(nd
->dentry
, &this);
648 nd
->flags
|= LOOKUP_CONTINUE
;
649 /* This does the actual lookups.. */
650 err
= do_lookup(nd
, &this, &next
);
653 /* Check mountpoints.. */
654 follow_mount(&next
.mnt
, &next
.dentry
);
657 inode
= next
.dentry
->d_inode
;
664 if (inode
->i_op
->follow_link
) {
666 err
= do_follow_link(next
.dentry
, nd
);
672 inode
= nd
->dentry
->d_inode
;
681 nd
->dentry
= next
.dentry
;
684 if (!inode
->i_op
->lookup
)
687 /* here ends the main loop */
690 lookup_flags
|= LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
;
692 nd
->flags
&= ~LOOKUP_CONTINUE
;
693 if (lookup_flags
& LOOKUP_PARENT
)
695 if (this.name
[0] == '.') switch (this.len
) {
699 if (this.name
[1] != '.')
701 follow_dotdot(&nd
->mnt
, &nd
->dentry
);
702 inode
= nd
->dentry
->d_inode
;
707 if (nd
->dentry
->d_op
&& nd
->dentry
->d_op
->d_hash
) {
708 err
= nd
->dentry
->d_op
->d_hash(nd
->dentry
, &this);
712 err
= do_lookup(nd
, &this, &next
);
715 follow_mount(&next
.mnt
, &next
.dentry
);
716 inode
= next
.dentry
->d_inode
;
717 if ((lookup_flags
& LOOKUP_FOLLOW
)
718 && inode
&& inode
->i_op
&& inode
->i_op
->follow_link
) {
720 err
= do_follow_link(next
.dentry
, nd
);
725 inode
= nd
->dentry
->d_inode
;
729 nd
->dentry
= next
.dentry
;
734 if (lookup_flags
& LOOKUP_DIRECTORY
) {
736 if (!inode
->i_op
|| !inode
->i_op
->lookup
)
742 nd
->last_type
= LAST_NORM
;
743 if (this.name
[0] != '.')
746 nd
->last_type
= LAST_DOT
;
747 else if (this.len
== 2 && this.name
[1] == '.')
748 nd
->last_type
= LAST_DOTDOT
;
753 * We bypassed the ordinary revalidation routines.
754 * We may need to check the cached dentry for staleness.
756 if (nd
->dentry
&& nd
->dentry
->d_sb
&&
757 (nd
->dentry
->d_sb
->s_type
->fs_flags
& FS_REVAL_DOT
)) {
759 /* Note: we do not d_invalidate() */
760 if (!nd
->dentry
->d_op
->d_revalidate(nd
->dentry
, nd
))
774 int fastcall
path_walk(const char * name
, struct nameidata
*nd
)
776 current
->total_link_count
= 0;
777 return link_path_walk(name
, nd
);
781 /* returns 1 if everything is done */
782 static int __emul_lookup_dentry(const char *name
, struct nameidata
*nd
)
784 if (path_walk(name
, nd
))
785 return 0; /* something went wrong... */
787 if (!nd
->dentry
->d_inode
|| S_ISDIR(nd
->dentry
->d_inode
->i_mode
)) {
788 struct nameidata nd_root
;
790 * NAME was not found in alternate root or it's a directory. Try to find
791 * it in the normal root:
793 nd_root
.last_type
= LAST_ROOT
;
794 nd_root
.flags
= nd
->flags
;
795 memcpy(&nd_root
.intent
, &nd
->intent
, sizeof(nd_root
.intent
));
796 read_lock(¤t
->fs
->lock
);
797 nd_root
.mnt
= mntget(current
->fs
->rootmnt
);
798 nd_root
.dentry
= dget(current
->fs
->root
);
799 read_unlock(¤t
->fs
->lock
);
800 if (path_walk(name
, &nd_root
))
802 if (nd_root
.dentry
->d_inode
) {
804 nd
->dentry
= nd_root
.dentry
;
805 nd
->mnt
= nd_root
.mnt
;
806 nd
->last
= nd_root
.last
;
809 path_release(&nd_root
);
814 void set_fs_altroot(void)
816 char *emul
= __emul_prefix();
818 struct vfsmount
*mnt
= NULL
, *oldmnt
;
819 struct dentry
*dentry
= NULL
, *olddentry
;
824 err
= path_lookup(emul
, LOOKUP_FOLLOW
|LOOKUP_DIRECTORY
|LOOKUP_NOALT
, &nd
);
830 write_lock(¤t
->fs
->lock
);
831 oldmnt
= current
->fs
->altrootmnt
;
832 olddentry
= current
->fs
->altroot
;
833 current
->fs
->altrootmnt
= mnt
;
834 current
->fs
->altroot
= dentry
;
835 write_unlock(¤t
->fs
->lock
);
844 walk_init_root(const char *name
, struct nameidata
*nd
)
846 read_lock(¤t
->fs
->lock
);
847 if (current
->fs
->altroot
&& !(nd
->flags
& LOOKUP_NOALT
)) {
848 nd
->mnt
= mntget(current
->fs
->altrootmnt
);
849 nd
->dentry
= dget(current
->fs
->altroot
);
850 read_unlock(¤t
->fs
->lock
);
851 if (__emul_lookup_dentry(name
,nd
))
853 read_lock(¤t
->fs
->lock
);
855 nd
->mnt
= mntget(current
->fs
->rootmnt
);
856 nd
->dentry
= dget(current
->fs
->root
);
857 read_unlock(¤t
->fs
->lock
);
861 int fastcall
path_lookup(const char *name
, unsigned int flags
, struct nameidata
*nd
)
863 nd
->last_type
= LAST_ROOT
; /* if there are only slashes... */
866 read_lock(¤t
->fs
->lock
);
868 if (current
->fs
->altroot
&& !(nd
->flags
& LOOKUP_NOALT
)) {
869 nd
->mnt
= mntget(current
->fs
->altrootmnt
);
870 nd
->dentry
= dget(current
->fs
->altroot
);
871 read_unlock(¤t
->fs
->lock
);
872 if (__emul_lookup_dentry(name
,nd
))
874 read_lock(¤t
->fs
->lock
);
876 nd
->mnt
= mntget(current
->fs
->rootmnt
);
877 nd
->dentry
= dget(current
->fs
->root
);
880 nd
->mnt
= mntget(current
->fs
->pwdmnt
);
881 nd
->dentry
= dget(current
->fs
->pwd
);
883 read_unlock(¤t
->fs
->lock
);
884 current
->total_link_count
= 0;
885 return link_path_walk(name
, nd
);
889 * Restricted form of lookup. Doesn't follow links, single-component only,
890 * needs parent already locked. Doesn't follow mounts.
893 static struct dentry
* __lookup_hash(struct qstr
*name
, struct dentry
* base
, struct nameidata
*nd
)
895 struct dentry
* dentry
;
899 inode
= base
->d_inode
;
900 err
= permission(inode
, MAY_EXEC
, nd
);
901 dentry
= ERR_PTR(err
);
906 * See if the low-level filesystem might want
907 * to use its own hash..
909 if (base
->d_op
&& base
->d_op
->d_hash
) {
910 err
= base
->d_op
->d_hash(base
, name
);
911 dentry
= ERR_PTR(err
);
916 dentry
= cached_lookup(base
, name
, nd
);
918 struct dentry
*new = d_alloc(base
, name
);
919 dentry
= ERR_PTR(-ENOMEM
);
922 dentry
= inode
->i_op
->lookup(inode
, new, nd
);
932 struct dentry
* lookup_hash(struct qstr
*name
, struct dentry
* base
)
934 return __lookup_hash(name
, base
, NULL
);
938 struct dentry
* lookup_one_len(const char * name
, struct dentry
* base
, int len
)
949 hash
= init_name_hash();
951 c
= *(const unsigned char *)name
++;
952 if (c
== '/' || c
== '\0')
954 hash
= partial_name_hash(c
, hash
);
956 this.hash
= end_name_hash(hash
);
958 return lookup_hash(&this, base
);
960 return ERR_PTR(-EACCES
);
966 * is used by most simple commands to get the inode of a specified name.
967 * Open, link etc use their own routines, but this is enough for things
970 * namei exists in two versions: namei/lnamei. The only difference is
971 * that namei follows links, while lnamei does not.
974 int fastcall
__user_walk(const char __user
*name
, unsigned flags
, struct nameidata
*nd
)
976 char *tmp
= getname(name
);
977 int err
= PTR_ERR(tmp
);
980 err
= path_lookup(tmp
, flags
, nd
);
987 * It's inline, so penalty for filesystems that don't use sticky bit is
990 static inline int check_sticky(struct inode
*dir
, struct inode
*inode
)
992 if (!(dir
->i_mode
& S_ISVTX
))
994 if (inode
->i_uid
== current
->fsuid
)
996 if (dir
->i_uid
== current
->fsuid
)
998 return !capable(CAP_FOWNER
);
1002 * Check whether we can remove a link victim from directory dir, check
1003 * whether the type of victim is right.
1004 * 1. We can't do it if dir is read-only (done in permission())
1005 * 2. We should have write and exec permissions on dir
1006 * 3. We can't remove anything from append-only dir
1007 * 4. We can't do anything with immutable dir (done in permission())
1008 * 5. If the sticky bit on dir is set we should either
1009 * a. be owner of dir, or
1010 * b. be owner of victim, or
1011 * c. have CAP_FOWNER capability
1012 * 6. If the victim is append-only or immutable we can't do antyhing with
1013 * links pointing to it.
1014 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1015 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1016 * 9. We can't remove a root or mountpoint.
1017 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1018 * nfs_async_unlink().
1020 static inline int may_delete(struct inode
*dir
,struct dentry
*victim
,int isdir
)
1023 if (!victim
->d_inode
|| victim
->d_parent
->d_inode
!= dir
)
1025 error
= permission(dir
,MAY_WRITE
| MAY_EXEC
, NULL
);
1030 if (check_sticky(dir
, victim
->d_inode
)||IS_APPEND(victim
->d_inode
)||
1031 IS_IMMUTABLE(victim
->d_inode
))
1034 if (!S_ISDIR(victim
->d_inode
->i_mode
))
1036 if (IS_ROOT(victim
))
1038 } else if (S_ISDIR(victim
->d_inode
->i_mode
))
1040 if (IS_DEADDIR(dir
))
1042 if (victim
->d_flags
& DCACHE_NFSFS_RENAMED
)
1047 /* Check whether we can create an object with dentry child in directory
1049 * 1. We can't do it if child already exists (open has special treatment for
1050 * this case, but since we are inlined it's OK)
1051 * 2. We can't do it if dir is read-only (done in permission())
1052 * 3. We should have write and exec permissions on dir
1053 * 4. We can't do it if dir is immutable (done in permission())
1055 static inline int may_create(struct inode
*dir
, struct dentry
*child
,
1056 struct nameidata
*nd
)
1060 if (IS_DEADDIR(dir
))
1062 return permission(dir
,MAY_WRITE
| MAY_EXEC
, nd
);
1066 * Special case: O_CREAT|O_EXCL implies O_NOFOLLOW for security
1069 * O_DIRECTORY translates into forcing a directory lookup.
1071 static inline int lookup_flags(unsigned int f
)
1073 unsigned long retval
= LOOKUP_FOLLOW
;
1076 retval
&= ~LOOKUP_FOLLOW
;
1078 if ((f
& (O_CREAT
|O_EXCL
)) == (O_CREAT
|O_EXCL
))
1079 retval
&= ~LOOKUP_FOLLOW
;
1081 if (f
& O_DIRECTORY
)
1082 retval
|= LOOKUP_DIRECTORY
;
1088 * p1 and p2 should be directories on the same fs.
1090 struct dentry
*lock_rename(struct dentry
*p1
, struct dentry
*p2
)
1095 down(&p1
->d_inode
->i_sem
);
1099 down(&p1
->d_inode
->i_sb
->s_vfs_rename_sem
);
1101 for (p
= p1
; p
->d_parent
!= p
; p
= p
->d_parent
) {
1102 if (p
->d_parent
== p2
) {
1103 down(&p2
->d_inode
->i_sem
);
1104 down(&p1
->d_inode
->i_sem
);
1109 for (p
= p2
; p
->d_parent
!= p
; p
= p
->d_parent
) {
1110 if (p
->d_parent
== p1
) {
1111 down(&p1
->d_inode
->i_sem
);
1112 down(&p2
->d_inode
->i_sem
);
1117 down(&p1
->d_inode
->i_sem
);
1118 down(&p2
->d_inode
->i_sem
);
1122 void unlock_rename(struct dentry
*p1
, struct dentry
*p2
)
1124 up(&p1
->d_inode
->i_sem
);
1126 up(&p2
->d_inode
->i_sem
);
1127 up(&p1
->d_inode
->i_sb
->s_vfs_rename_sem
);
1131 int vfs_create(struct inode
*dir
, struct dentry
*dentry
, int mode
,
1132 struct nameidata
*nd
)
1134 int error
= may_create(dir
, dentry
, nd
);
1139 if (!dir
->i_op
|| !dir
->i_op
->create
)
1140 return -EACCES
; /* shouldn't it be ENOSYS? */
1143 error
= security_inode_create(dir
, dentry
, mode
);
1147 error
= dir
->i_op
->create(dir
, dentry
, mode
, nd
);
1149 inode_dir_notify(dir
, DN_CREATE
);
1150 security_inode_post_create(dir
, dentry
, mode
);
1155 int may_open(struct nameidata
*nd
, int acc_mode
, int flag
)
1157 struct dentry
*dentry
= nd
->dentry
;
1158 struct inode
*inode
= dentry
->d_inode
;
1164 if (S_ISLNK(inode
->i_mode
))
1167 if (S_ISDIR(inode
->i_mode
) && (flag
& FMODE_WRITE
))
1170 error
= permission(inode
, acc_mode
, nd
);
1175 * FIFO's, sockets and device files are special: they don't
1176 * actually live on the filesystem itself, and as such you
1177 * can write to them even if the filesystem is read-only.
1179 if (S_ISFIFO(inode
->i_mode
) || S_ISSOCK(inode
->i_mode
)) {
1181 } else if (S_ISBLK(inode
->i_mode
) || S_ISCHR(inode
->i_mode
)) {
1182 if (nd
->mnt
->mnt_flags
& MNT_NODEV
)
1186 } else if (IS_RDONLY(inode
) && (flag
& FMODE_WRITE
))
1189 * An append-only file must be opened in append mode for writing.
1191 if (IS_APPEND(inode
)) {
1192 if ((flag
& FMODE_WRITE
) && !(flag
& O_APPEND
))
1199 * Ensure there are no outstanding leases on the file.
1201 error
= break_lease(inode
, flag
);
1205 if (flag
& O_TRUNC
) {
1206 error
= get_write_access(inode
);
1211 * Refuse to truncate files with mandatory locks held on them.
1213 error
= locks_verify_locked(inode
);
1217 error
= do_truncate(dentry
, 0);
1219 put_write_access(inode
);
1223 if (flag
& FMODE_WRITE
)
1232 * namei for open - this is in fact almost the whole open-routine.
1234 * Note that the low bits of "flag" aren't the same as in the open
1235 * system call - they are 00 - no permissions needed
1236 * 01 - read permission needed
1237 * 10 - write permission needed
1238 * 11 - read/write permissions needed
1239 * which is a lot more logical, and also allows the "no perm" needed
1240 * for symlinks (where the permissions are checked later).
1243 int open_namei(const char * pathname
, int flag
, int mode
, struct nameidata
*nd
)
1245 int acc_mode
, error
= 0;
1246 struct dentry
*dentry
;
1250 acc_mode
= ACC_MODE(flag
);
1252 /* Allow the LSM permission hook to distinguish append
1253 access from general write access. */
1254 if (flag
& O_APPEND
)
1255 acc_mode
|= MAY_APPEND
;
1257 /* Fill in the open() intent data */
1258 nd
->intent
.open
.flags
= flag
;
1259 nd
->intent
.open
.create_mode
= mode
;
1262 * The simplest case - just a plain lookup.
1264 if (!(flag
& O_CREAT
)) {
1265 error
= path_lookup(pathname
, lookup_flags(flag
)|LOOKUP_OPEN
, nd
);
1272 * Create - we need to know the parent.
1274 error
= path_lookup(pathname
, LOOKUP_PARENT
|LOOKUP_OPEN
|LOOKUP_CREATE
, nd
);
1279 * We have the parent and last component. First of all, check
1280 * that we are not asked to creat(2) an obvious directory - that
1284 if (nd
->last_type
!= LAST_NORM
|| nd
->last
.name
[nd
->last
.len
])
1288 nd
->flags
&= ~LOOKUP_PARENT
;
1289 down(&dir
->d_inode
->i_sem
);
1290 dentry
= __lookup_hash(&nd
->last
, nd
->dentry
, nd
);
1293 error
= PTR_ERR(dentry
);
1294 if (IS_ERR(dentry
)) {
1295 up(&dir
->d_inode
->i_sem
);
1299 /* Negative dentry, just create the file */
1300 if (!dentry
->d_inode
) {
1301 if (!IS_POSIXACL(dir
->d_inode
))
1302 mode
&= ~current
->fs
->umask
;
1303 error
= vfs_create(dir
->d_inode
, dentry
, mode
, nd
);
1304 up(&dir
->d_inode
->i_sem
);
1306 nd
->dentry
= dentry
;
1309 /* Don't check for write permission, don't truncate */
1316 * It already exists.
1318 up(&dir
->d_inode
->i_sem
);
1324 if (d_mountpoint(dentry
)) {
1326 if (flag
& O_NOFOLLOW
)
1328 while (__follow_down(&nd
->mnt
,&dentry
) && d_mountpoint(dentry
));
1331 if (!dentry
->d_inode
)
1333 if (dentry
->d_inode
->i_op
&& dentry
->d_inode
->i_op
->follow_link
)
1337 nd
->dentry
= dentry
;
1339 if (dentry
->d_inode
&& S_ISDIR(dentry
->d_inode
->i_mode
))
1342 error
= may_open(nd
, acc_mode
, flag
);
1355 if (flag
& O_NOFOLLOW
)
1358 * This is subtle. Instead of calling do_follow_link() we do the
1359 * thing by hands. The reason is that this way we have zero link_count
1360 * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1361 * After that we have the parent and last component, i.e.
1362 * we are in the same situation as after the first path_walk().
1363 * Well, almost - if the last component is normal we get its copy
1364 * stored in nd->last.name and we will have to putname() it when we
1365 * are done. Procfs-like symlinks just set LAST_BIND.
1367 nd
->flags
|= LOOKUP_PARENT
;
1368 error
= security_inode_follow_link(dentry
, nd
);
1371 touch_atime(nd
->mnt
, dentry
);
1372 error
= dentry
->d_inode
->i_op
->follow_link(dentry
, nd
);
1376 nd
->flags
&= ~LOOKUP_PARENT
;
1377 if (nd
->last_type
== LAST_BIND
) {
1378 dentry
= nd
->dentry
;
1382 if (nd
->last_type
!= LAST_NORM
)
1384 if (nd
->last
.name
[nd
->last
.len
]) {
1385 putname(nd
->last
.name
);
1390 putname(nd
->last
.name
);
1394 down(&dir
->d_inode
->i_sem
);
1395 dentry
= __lookup_hash(&nd
->last
, nd
->dentry
, nd
);
1396 putname(nd
->last
.name
);
1401 * lookup_create - lookup a dentry, creating it if it doesn't exist
1402 * @nd: nameidata info
1403 * @is_dir: directory flag
1405 * Simple function to lookup and return a dentry and create it
1406 * if it doesn't exist. Is SMP-safe.
1408 struct dentry
*lookup_create(struct nameidata
*nd
, int is_dir
)
1410 struct dentry
*dentry
;
1412 down(&nd
->dentry
->d_inode
->i_sem
);
1413 dentry
= ERR_PTR(-EEXIST
);
1414 if (nd
->last_type
!= LAST_NORM
)
1416 nd
->flags
&= ~LOOKUP_PARENT
;
1417 dentry
= lookup_hash(&nd
->last
, nd
->dentry
);
1420 if (!is_dir
&& nd
->last
.name
[nd
->last
.len
] && !dentry
->d_inode
)
1425 dentry
= ERR_PTR(-ENOENT
);
1430 int vfs_mknod(struct inode
*dir
, struct dentry
*dentry
, int mode
, dev_t dev
)
1432 int error
= may_create(dir
, dentry
, NULL
);
1437 if ((S_ISCHR(mode
) || S_ISBLK(mode
)) && !capable(CAP_MKNOD
))
1440 if (!dir
->i_op
|| !dir
->i_op
->mknod
)
1443 error
= security_inode_mknod(dir
, dentry
, mode
, dev
);
1448 error
= dir
->i_op
->mknod(dir
, dentry
, mode
, dev
);
1450 inode_dir_notify(dir
, DN_CREATE
);
1451 security_inode_post_mknod(dir
, dentry
, mode
, dev
);
1456 asmlinkage
long sys_mknod(const char __user
* filename
, int mode
, unsigned dev
)
1460 struct dentry
* dentry
;
1461 struct nameidata nd
;
1465 tmp
= getname(filename
);
1467 return PTR_ERR(tmp
);
1469 error
= path_lookup(tmp
, LOOKUP_PARENT
, &nd
);
1472 dentry
= lookup_create(&nd
, 0);
1473 error
= PTR_ERR(dentry
);
1475 if (!IS_POSIXACL(nd
.dentry
->d_inode
))
1476 mode
&= ~current
->fs
->umask
;
1477 if (!IS_ERR(dentry
)) {
1478 switch (mode
& S_IFMT
) {
1479 case 0: case S_IFREG
:
1480 error
= vfs_create(nd
.dentry
->d_inode
,dentry
,mode
,&nd
);
1482 case S_IFCHR
: case S_IFBLK
:
1483 error
= vfs_mknod(nd
.dentry
->d_inode
,dentry
,mode
,
1484 new_decode_dev(dev
));
1486 case S_IFIFO
: case S_IFSOCK
:
1487 error
= vfs_mknod(nd
.dentry
->d_inode
,dentry
,mode
,0);
1497 up(&nd
.dentry
->d_inode
->i_sem
);
1505 int vfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
1507 int error
= may_create(dir
, dentry
, NULL
);
1512 if (!dir
->i_op
|| !dir
->i_op
->mkdir
)
1515 mode
&= (S_IRWXUGO
|S_ISVTX
);
1516 error
= security_inode_mkdir(dir
, dentry
, mode
);
1521 error
= dir
->i_op
->mkdir(dir
, dentry
, mode
);
1523 inode_dir_notify(dir
, DN_CREATE
);
1524 security_inode_post_mkdir(dir
,dentry
, mode
);
1529 asmlinkage
long sys_mkdir(const char __user
* pathname
, int mode
)
1534 tmp
= getname(pathname
);
1535 error
= PTR_ERR(tmp
);
1537 struct dentry
*dentry
;
1538 struct nameidata nd
;
1540 error
= path_lookup(tmp
, LOOKUP_PARENT
, &nd
);
1543 dentry
= lookup_create(&nd
, 1);
1544 error
= PTR_ERR(dentry
);
1545 if (!IS_ERR(dentry
)) {
1546 if (!IS_POSIXACL(nd
.dentry
->d_inode
))
1547 mode
&= ~current
->fs
->umask
;
1548 error
= vfs_mkdir(nd
.dentry
->d_inode
, dentry
, mode
);
1551 up(&nd
.dentry
->d_inode
->i_sem
);
1561 * We try to drop the dentry early: we should have
1562 * a usage count of 2 if we're the only user of this
1563 * dentry, and if that is true (possibly after pruning
1564 * the dcache), then we drop the dentry now.
1566 * A low-level filesystem can, if it choses, legally
1569 * if (!d_unhashed(dentry))
1572 * if it cannot handle the case of removing a directory
1573 * that is still in use by something else..
1575 static void d_unhash(struct dentry
*dentry
)
1578 spin_lock(&dcache_lock
);
1579 switch (atomic_read(&dentry
->d_count
)) {
1581 spin_unlock(&dcache_lock
);
1582 shrink_dcache_parent(dentry
);
1583 spin_lock(&dcache_lock
);
1584 if (atomic_read(&dentry
->d_count
) != 2)
1589 spin_unlock(&dcache_lock
);
1592 int vfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
1594 int error
= may_delete(dir
, dentry
, 1);
1599 if (!dir
->i_op
|| !dir
->i_op
->rmdir
)
1604 down(&dentry
->d_inode
->i_sem
);
1606 if (d_mountpoint(dentry
))
1609 error
= security_inode_rmdir(dir
, dentry
);
1611 error
= dir
->i_op
->rmdir(dir
, dentry
);
1613 dentry
->d_inode
->i_flags
|= S_DEAD
;
1616 up(&dentry
->d_inode
->i_sem
);
1618 inode_dir_notify(dir
, DN_DELETE
);
1626 asmlinkage
long sys_rmdir(const char __user
* pathname
)
1630 struct dentry
*dentry
;
1631 struct nameidata nd
;
1633 name
= getname(pathname
);
1635 return PTR_ERR(name
);
1637 error
= path_lookup(name
, LOOKUP_PARENT
, &nd
);
1641 switch(nd
.last_type
) {
1652 down(&nd
.dentry
->d_inode
->i_sem
);
1653 dentry
= lookup_hash(&nd
.last
, nd
.dentry
);
1654 error
= PTR_ERR(dentry
);
1655 if (!IS_ERR(dentry
)) {
1656 error
= vfs_rmdir(nd
.dentry
->d_inode
, dentry
);
1659 up(&nd
.dentry
->d_inode
->i_sem
);
1667 int vfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
1669 int error
= may_delete(dir
, dentry
, 0);
1674 if (!dir
->i_op
|| !dir
->i_op
->unlink
)
1679 down(&dentry
->d_inode
->i_sem
);
1680 if (d_mountpoint(dentry
))
1683 error
= security_inode_unlink(dir
, dentry
);
1685 error
= dir
->i_op
->unlink(dir
, dentry
);
1687 up(&dentry
->d_inode
->i_sem
);
1689 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
1690 if (!error
&& !(dentry
->d_flags
& DCACHE_NFSFS_RENAMED
)) {
1692 inode_dir_notify(dir
, DN_DELETE
);
1698 * Make sure that the actual truncation of the file will occur outside its
1699 * directory's i_sem. Truncate can take a long time if there is a lot of
1700 * writeout happening, and we don't want to prevent access to the directory
1701 * while waiting on the I/O.
1703 asmlinkage
long sys_unlink(const char __user
* pathname
)
1707 struct dentry
*dentry
;
1708 struct nameidata nd
;
1709 struct inode
*inode
= NULL
;
1711 name
= getname(pathname
);
1713 return PTR_ERR(name
);
1715 error
= path_lookup(name
, LOOKUP_PARENT
, &nd
);
1719 if (nd
.last_type
!= LAST_NORM
)
1721 down(&nd
.dentry
->d_inode
->i_sem
);
1722 dentry
= lookup_hash(&nd
.last
, nd
.dentry
);
1723 error
= PTR_ERR(dentry
);
1724 if (!IS_ERR(dentry
)) {
1725 /* Why not before? Because we want correct error value */
1726 if (nd
.last
.name
[nd
.last
.len
])
1728 inode
= dentry
->d_inode
;
1730 atomic_inc(&inode
->i_count
);
1731 error
= vfs_unlink(nd
.dentry
->d_inode
, dentry
);
1735 up(&nd
.dentry
->d_inode
->i_sem
);
1742 iput(inode
); /* truncate the inode here */
1746 error
= !dentry
->d_inode
? -ENOENT
:
1747 S_ISDIR(dentry
->d_inode
->i_mode
) ? -EISDIR
: -ENOTDIR
;
1751 int vfs_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *oldname
)
1753 int error
= may_create(dir
, dentry
, NULL
);
1758 if (!dir
->i_op
|| !dir
->i_op
->symlink
)
1761 error
= security_inode_symlink(dir
, dentry
, oldname
);
1766 error
= dir
->i_op
->symlink(dir
, dentry
, oldname
);
1768 inode_dir_notify(dir
, DN_CREATE
);
1769 security_inode_post_symlink(dir
, dentry
, oldname
);
1774 asmlinkage
long sys_symlink(const char __user
* oldname
, const char __user
* newname
)
1780 from
= getname(oldname
);
1782 return PTR_ERR(from
);
1783 to
= getname(newname
);
1784 error
= PTR_ERR(to
);
1786 struct dentry
*dentry
;
1787 struct nameidata nd
;
1789 error
= path_lookup(to
, LOOKUP_PARENT
, &nd
);
1792 dentry
= lookup_create(&nd
, 0);
1793 error
= PTR_ERR(dentry
);
1794 if (!IS_ERR(dentry
)) {
1795 error
= vfs_symlink(nd
.dentry
->d_inode
, dentry
, from
);
1798 up(&nd
.dentry
->d_inode
->i_sem
);
1807 int vfs_link(struct dentry
*old_dentry
, struct inode
*dir
, struct dentry
*new_dentry
)
1809 struct inode
*inode
= old_dentry
->d_inode
;
1815 error
= may_create(dir
, new_dentry
, NULL
);
1819 if (dir
->i_sb
!= inode
->i_sb
)
1823 * A link to an append-only or immutable file cannot be created.
1825 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
1827 if (!dir
->i_op
|| !dir
->i_op
->link
)
1829 if (S_ISDIR(old_dentry
->d_inode
->i_mode
))
1832 error
= security_inode_link(old_dentry
, dir
, new_dentry
);
1836 down(&old_dentry
->d_inode
->i_sem
);
1838 error
= dir
->i_op
->link(old_dentry
, dir
, new_dentry
);
1839 up(&old_dentry
->d_inode
->i_sem
);
1841 inode_dir_notify(dir
, DN_CREATE
);
1842 security_inode_post_link(old_dentry
, dir
, new_dentry
);
1848 * Hardlinks are often used in delicate situations. We avoid
1849 * security-related surprises by not following symlinks on the
1852 * We don't follow them on the oldname either to be compatible
1853 * with linux 2.0, and to avoid hard-linking to directories
1854 * and other special files. --ADM
1856 asmlinkage
long sys_link(const char __user
* oldname
, const char __user
* newname
)
1858 struct dentry
*new_dentry
;
1859 struct nameidata nd
, old_nd
;
1863 to
= getname(newname
);
1867 error
= __user_walk(oldname
, 0, &old_nd
);
1870 error
= path_lookup(to
, LOOKUP_PARENT
, &nd
);
1874 if (old_nd
.mnt
!= nd
.mnt
)
1876 new_dentry
= lookup_create(&nd
, 0);
1877 error
= PTR_ERR(new_dentry
);
1878 if (!IS_ERR(new_dentry
)) {
1879 error
= vfs_link(old_nd
.dentry
, nd
.dentry
->d_inode
, new_dentry
);
1882 up(&nd
.dentry
->d_inode
->i_sem
);
1886 path_release(&old_nd
);
1894 * The worst of all namespace operations - renaming directory. "Perverted"
1895 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
1897 * a) we can get into loop creation. Check is done in is_subdir().
1898 * b) race potential - two innocent renames can create a loop together.
1899 * That's where 4.4 screws up. Current fix: serialization on
1900 * sb->s_vfs_rename_sem. We might be more accurate, but that's another
1902 * c) we have to lock _three_ objects - parents and victim (if it exists).
1903 * And that - after we got ->i_sem on parents (until then we don't know
1904 * whether the target exists). Solution: try to be smart with locking
1905 * order for inodes. We rely on the fact that tree topology may change
1906 * only under ->s_vfs_rename_sem _and_ that parent of the object we
1907 * move will be locked. Thus we can rank directories by the tree
1908 * (ancestors first) and rank all non-directories after them.
1909 * That works since everybody except rename does "lock parent, lookup,
1910 * lock child" and rename is under ->s_vfs_rename_sem.
1911 * HOWEVER, it relies on the assumption that any object with ->lookup()
1912 * has no more than 1 dentry. If "hybrid" objects will ever appear,
1913 * we'd better make sure that there's no link(2) for them.
1914 * d) some filesystems don't support opened-but-unlinked directories,
1915 * either because of layout or because they are not ready to deal with
1916 * all cases correctly. The latter will be fixed (taking this sort of
1917 * stuff into VFS), but the former is not going away. Solution: the same
1918 * trick as in rmdir().
1919 * e) conversion from fhandle to dentry may come in the wrong moment - when
1920 * we are removing the target. Solution: we will have to grab ->i_sem
1921 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
1922 * ->i_sem on parents, which works but leads to some truely excessive
1925 int vfs_rename_dir(struct inode
*old_dir
, struct dentry
*old_dentry
,
1926 struct inode
*new_dir
, struct dentry
*new_dentry
)
1929 struct inode
*target
;
1932 * If we are going to change the parent - check write permissions,
1933 * we'll need to flip '..'.
1935 if (new_dir
!= old_dir
) {
1936 error
= permission(old_dentry
->d_inode
, MAY_WRITE
, NULL
);
1941 error
= security_inode_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
1945 target
= new_dentry
->d_inode
;
1947 down(&target
->i_sem
);
1948 d_unhash(new_dentry
);
1950 if (d_mountpoint(old_dentry
)||d_mountpoint(new_dentry
))
1953 error
= old_dir
->i_op
->rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
1956 target
->i_flags
|= S_DEAD
;
1958 if (d_unhashed(new_dentry
))
1959 d_rehash(new_dentry
);
1963 d_move(old_dentry
,new_dentry
);
1964 security_inode_post_rename(old_dir
, old_dentry
,
1965 new_dir
, new_dentry
);
1970 int vfs_rename_other(struct inode
*old_dir
, struct dentry
*old_dentry
,
1971 struct inode
*new_dir
, struct dentry
*new_dentry
)
1973 struct inode
*target
;
1976 error
= security_inode_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
1981 target
= new_dentry
->d_inode
;
1983 down(&target
->i_sem
);
1984 if (d_mountpoint(old_dentry
)||d_mountpoint(new_dentry
))
1987 error
= old_dir
->i_op
->rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
1989 /* The following d_move() should become unconditional */
1990 if (!(old_dir
->i_sb
->s_type
->fs_flags
& FS_ODD_RENAME
))
1991 d_move(old_dentry
, new_dentry
);
1992 security_inode_post_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
2000 int vfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
2001 struct inode
*new_dir
, struct dentry
*new_dentry
)
2004 int is_dir
= S_ISDIR(old_dentry
->d_inode
->i_mode
);
2006 if (old_dentry
->d_inode
== new_dentry
->d_inode
)
2009 error
= may_delete(old_dir
, old_dentry
, is_dir
);
2013 if (!new_dentry
->d_inode
)
2014 error
= may_create(new_dir
, new_dentry
, NULL
);
2016 error
= may_delete(new_dir
, new_dentry
, is_dir
);
2020 if (!old_dir
->i_op
|| !old_dir
->i_op
->rename
)
2023 DQUOT_INIT(old_dir
);
2024 DQUOT_INIT(new_dir
);
2027 error
= vfs_rename_dir(old_dir
,old_dentry
,new_dir
,new_dentry
);
2029 error
= vfs_rename_other(old_dir
,old_dentry
,new_dir
,new_dentry
);
2031 if (old_dir
== new_dir
)
2032 inode_dir_notify(old_dir
, DN_RENAME
);
2034 inode_dir_notify(old_dir
, DN_DELETE
);
2035 inode_dir_notify(new_dir
, DN_CREATE
);
2041 static inline int do_rename(const char * oldname
, const char * newname
)
2044 struct dentry
* old_dir
, * new_dir
;
2045 struct dentry
* old_dentry
, *new_dentry
;
2046 struct dentry
* trap
;
2047 struct nameidata oldnd
, newnd
;
2049 error
= path_lookup(oldname
, LOOKUP_PARENT
, &oldnd
);
2053 error
= path_lookup(newname
, LOOKUP_PARENT
, &newnd
);
2058 if (oldnd
.mnt
!= newnd
.mnt
)
2061 old_dir
= oldnd
.dentry
;
2063 if (oldnd
.last_type
!= LAST_NORM
)
2066 new_dir
= newnd
.dentry
;
2067 if (newnd
.last_type
!= LAST_NORM
)
2070 trap
= lock_rename(new_dir
, old_dir
);
2072 old_dentry
= lookup_hash(&oldnd
.last
, old_dir
);
2073 error
= PTR_ERR(old_dentry
);
2074 if (IS_ERR(old_dentry
))
2076 /* source must exist */
2078 if (!old_dentry
->d_inode
)
2080 /* unless the source is a directory trailing slashes give -ENOTDIR */
2081 if (!S_ISDIR(old_dentry
->d_inode
->i_mode
)) {
2083 if (oldnd
.last
.name
[oldnd
.last
.len
])
2085 if (newnd
.last
.name
[newnd
.last
.len
])
2088 /* source should not be ancestor of target */
2090 if (old_dentry
== trap
)
2092 new_dentry
= lookup_hash(&newnd
.last
, new_dir
);
2093 error
= PTR_ERR(new_dentry
);
2094 if (IS_ERR(new_dentry
))
2096 /* target should not be an ancestor of source */
2098 if (new_dentry
== trap
)
2101 error
= vfs_rename(old_dir
->d_inode
, old_dentry
,
2102 new_dir
->d_inode
, new_dentry
);
2108 unlock_rename(new_dir
, old_dir
);
2110 path_release(&newnd
);
2112 path_release(&oldnd
);
2117 asmlinkage
long sys_rename(const char __user
* oldname
, const char __user
* newname
)
2123 from
= getname(oldname
);
2125 return PTR_ERR(from
);
2126 to
= getname(newname
);
2127 error
= PTR_ERR(to
);
2129 error
= do_rename(from
,to
);
2136 int vfs_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
, const char *link
)
2140 len
= PTR_ERR(link
);
2145 if (len
> (unsigned) buflen
)
2147 if (copy_to_user(buffer
, link
, len
))
2154 __vfs_follow_link(struct nameidata
*nd
, const char *link
)
2163 if (!walk_init_root(link
, nd
))
2164 /* weird __emul_prefix() stuff did it */
2167 res
= link_path_walk(link
, nd
);
2169 if (current
->link_count
|| res
|| nd
->last_type
!=LAST_NORM
)
2172 * If it is an iterative symlinks resolution in open_namei() we
2173 * have to copy the last component. And all that crap because of
2174 * bloody create() on broken symlinks. Furrfu...
2177 if (unlikely(!name
)) {
2181 strcpy(name
, nd
->last
.name
);
2182 nd
->last
.name
= name
;
2186 return PTR_ERR(link
);
2189 int vfs_follow_link(struct nameidata
*nd
, const char *link
)
2191 return __vfs_follow_link(nd
, link
);
2194 /* get the link contents into pagecache */
2195 static char *page_getlink(struct dentry
* dentry
, struct page
**ppage
)
2198 struct address_space
*mapping
= dentry
->d_inode
->i_mapping
;
2199 page
= read_cache_page(mapping
, 0, (filler_t
*)mapping
->a_ops
->readpage
,
2203 wait_on_page_locked(page
);
2204 if (!PageUptodate(page
))
2210 page_cache_release(page
);
2211 return ERR_PTR(-EIO
);
2217 int page_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
)
2219 struct page
*page
= NULL
;
2220 char *s
= page_getlink(dentry
, &page
);
2221 int res
= vfs_readlink(dentry
,buffer
,buflen
,s
);
2224 page_cache_release(page
);
2229 int page_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
2231 struct page
*page
= NULL
;
2232 char *s
= page_getlink(dentry
, &page
);
2233 int res
= __vfs_follow_link(nd
, s
);
2236 page_cache_release(page
);
2241 int page_symlink(struct inode
*inode
, const char *symname
, int len
)
2243 struct address_space
*mapping
= inode
->i_mapping
;
2244 struct page
*page
= grab_cache_page(mapping
, 0);
2250 err
= mapping
->a_ops
->prepare_write(NULL
, page
, 0, len
-1);
2253 kaddr
= kmap_atomic(page
, KM_USER0
);
2254 memcpy(kaddr
, symname
, len
-1);
2255 kunmap_atomic(kaddr
, KM_USER0
);
2256 mapping
->a_ops
->commit_write(NULL
, page
, 0, len
-1);
2258 * Notice that we are _not_ going to block here - end of page is
2259 * unmapped, so this will only try to map the rest of page, see
2260 * that it is unmapped (typically even will not look into inode -
2261 * ->i_size will be enough for everything) and zero it out.
2262 * OTOH it's obviously correct and should make the page up-to-date.
2264 if (!PageUptodate(page
)) {
2265 err
= mapping
->a_ops
->readpage(NULL
, page
);
2266 wait_on_page_locked(page
);
2270 page_cache_release(page
);
2273 mark_inode_dirty(inode
);
2277 page_cache_release(page
);
2282 struct inode_operations page_symlink_inode_operations
= {
2283 .readlink
= page_readlink
,
2284 .follow_link
= page_follow_link
,
2287 EXPORT_SYMBOL(__user_walk
);
2288 EXPORT_SYMBOL(follow_down
);
2289 EXPORT_SYMBOL(follow_up
);
2290 EXPORT_SYMBOL(get_write_access
); /* binfmt_aout */
2291 EXPORT_SYMBOL(getname
);
2292 EXPORT_SYMBOL(lock_rename
);
2293 EXPORT_SYMBOL(lookup_create
);
2294 EXPORT_SYMBOL(lookup_hash
);
2295 EXPORT_SYMBOL(lookup_one_len
);
2296 EXPORT_SYMBOL(page_follow_link
);
2297 EXPORT_SYMBOL(page_readlink
);
2298 EXPORT_SYMBOL(page_symlink
);
2299 EXPORT_SYMBOL(page_symlink_inode_operations
);
2300 EXPORT_SYMBOL(path_lookup
);
2301 EXPORT_SYMBOL(path_release
);
2302 EXPORT_SYMBOL(path_walk
);
2303 EXPORT_SYMBOL(permission
);
2304 EXPORT_SYMBOL(unlock_rename
);
2305 EXPORT_SYMBOL(vfs_create
);
2306 EXPORT_SYMBOL(vfs_follow_link
);
2307 EXPORT_SYMBOL(vfs_link
);
2308 EXPORT_SYMBOL(vfs_mkdir
);
2309 EXPORT_SYMBOL(vfs_mknod
);
2310 EXPORT_SYMBOL(vfs_permission
);
2311 EXPORT_SYMBOL(vfs_readlink
);
2312 EXPORT_SYMBOL(vfs_rename
);
2313 EXPORT_SYMBOL(vfs_rmdir
);
2314 EXPORT_SYMBOL(vfs_symlink
);
2315 EXPORT_SYMBOL(vfs_unlink
);