4 * Copyright (C) 1991, 1992 Linus Torvalds
8 * Some corrections by tytso.
11 /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
14 /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
21 #include <linux/namei.h>
22 #include <linux/quotaops.h>
23 #include <linux/pagemap.h>
24 #include <linux/dnotify.h>
25 #include <linux/smp_lock.h>
26 #include <linux/personality.h>
27 #include <linux/security.h>
28 #include <linux/mount.h>
29 #include <linux/audit.h>
30 #include <asm/namei.h>
31 #include <asm/uaccess.h>
33 #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
35 /* [Feb-1997 T. Schoebel-Theuer]
36 * Fundamental changes in the pathname lookup mechanisms (namei)
37 * were necessary because of omirr. The reason is that omirr needs
38 * to know the _real_ pathname, not the user-supplied one, in case
39 * of symlinks (and also when transname replacements occur).
41 * The new code replaces the old recursive symlink resolution with
42 * an iterative one (in case of non-nested symlink chains). It does
43 * this with calls to <fs>_follow_link().
44 * As a side effect, dir_namei(), _namei() and follow_link() are now
45 * replaced with a single function lookup_dentry() that can handle all
46 * the special cases of the former code.
48 * With the new dcache, the pathname is stored at each inode, at least as
49 * long as the refcount of the inode is positive. As a side effect, the
50 * size of the dcache depends on the inode cache and thus is dynamic.
52 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
53 * resolution to correspond with current state of the code.
55 * Note that the symlink resolution is not *completely* iterative.
56 * There is still a significant amount of tail- and mid- recursion in
57 * the algorithm. Also, note that <fs>_readlink() is not used in
58 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
59 * may return different results than <fs>_follow_link(). Many virtual
60 * filesystems (including /proc) exhibit this behavior.
63 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
64 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
65 * and the name already exists in form of a symlink, try to create the new
66 * name indicated by the symlink. The old code always complained that the
67 * name already exists, due to not following the symlink even if its target
68 * is nonexistent. The new semantics affects also mknod() and link() when
69 * the name is a symlink pointing to a non-existant name.
71 * I don't know which semantics is the right one, since I have no access
72 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
73 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
74 * "old" one. Personally, I think the new semantics is much more logical.
75 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
76 * file does succeed in both HP-UX and SunOs, but not in Solaris
77 * and in the old Linux semantics.
80 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
81 * semantics. See the comments in "open_namei" and "do_link" below.
83 * [10-Sep-98 Alan Modra] Another symlink change.
86 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
87 * inside the path - always follow.
88 * in the last component in creation/removal/renaming - never follow.
89 * if LOOKUP_FOLLOW passed - follow.
90 * if the pathname has trailing slashes - follow.
91 * otherwise - don't follow.
92 * (applied in that order).
94 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
95 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
96 * During the 2.4 we need to fix the userland stuff depending on it -
97 * hopefully we will be able to get rid of that wart in 2.5. So far only
98 * XEmacs seems to be relying on it...
101 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
102 * implemented. Let's see if raised priority of ->s_vfs_rename_sem gives
103 * any extra contention...
106 /* In order to reduce some races, while at the same time doing additional
107 * checking and hopefully speeding things up, we copy filenames to the
108 * kernel data space before using them..
110 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
111 * PATH_MAX includes the nul terminator --RR.
113 static inline int do_getname(const char __user
*filename
, char *page
)
116 unsigned long len
= PATH_MAX
;
119 if ((unsigned long) filename
>= TASK_SIZE
) {
120 if (!segment_eq(get_fs(), KERNEL_DS
))
122 } else if (TASK_SIZE
- (unsigned long) filename
< PATH_MAX
)
123 len
= TASK_SIZE
- (unsigned long) filename
;
126 retval
= strncpy_from_user((char *)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 if (unlikely(current
->audit_context
) && !IS_ERR(result
) && result
)
152 audit_getname(result
);
159 * is used to check for read/write/execute permissions on a file.
160 * We use "fsuid" for this, letting us set arbitrary permissions
161 * for filesystem access without changing the "normal" uids which
162 * are used for other things..
164 int vfs_permission(struct inode
* inode
, int mask
)
166 umode_t mode
= inode
->i_mode
;
168 if (mask
& MAY_WRITE
) {
170 * Nobody gets write access to a read-only fs.
172 if (IS_RDONLY(inode
) &&
173 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
)))
177 * Nobody gets write access to an immutable file.
179 if (IS_IMMUTABLE(inode
))
183 if (current
->fsuid
== inode
->i_uid
)
185 else if (in_group_p(inode
->i_gid
))
189 * If the DACs are ok we don't need any capability check.
191 if (((mode
& mask
& (MAY_READ
|MAY_WRITE
|MAY_EXEC
)) == mask
))
195 * Read/write DACs are always overridable.
196 * Executable DACs are overridable if at least one exec bit is set.
198 if (!(mask
& MAY_EXEC
) ||
199 (inode
->i_mode
& S_IXUGO
) || S_ISDIR(inode
->i_mode
))
200 if (capable(CAP_DAC_OVERRIDE
))
204 * Searching includes executable on directories, else just read.
206 if (mask
== MAY_READ
|| (S_ISDIR(inode
->i_mode
) && !(mask
& MAY_WRITE
)))
207 if (capable(CAP_DAC_READ_SEARCH
))
213 int permission(struct inode
* inode
,int mask
, struct nameidata
*nd
)
218 /* Ordinary permission routines do not understand MAY_APPEND. */
219 submask
= mask
& ~MAY_APPEND
;
221 if (inode
->i_op
&& inode
->i_op
->permission
)
222 retval
= inode
->i_op
->permission(inode
, submask
, nd
);
224 retval
= vfs_permission(inode
, submask
);
228 return security_inode_permission(inode
, mask
, nd
);
232 * get_write_access() gets write permission for a file.
233 * put_write_access() releases this write permission.
234 * This is used for regular files.
235 * We cannot support write (and maybe mmap read-write shared) accesses and
236 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
237 * can have the following values:
238 * 0: no writers, no VM_DENYWRITE mappings
239 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
240 * > 0: (i_writecount) users are writing to the file.
242 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
243 * except for the cases where we don't hold i_writecount yet. Then we need to
244 * use {get,deny}_write_access() - these functions check the sign and refuse
245 * to do the change if sign is wrong. Exclusion between them is provided by
246 * the inode->i_lock spinlock.
249 int get_write_access(struct inode
* inode
)
251 spin_lock(&inode
->i_lock
);
252 if (atomic_read(&inode
->i_writecount
) < 0) {
253 spin_unlock(&inode
->i_lock
);
256 atomic_inc(&inode
->i_writecount
);
257 spin_unlock(&inode
->i_lock
);
262 int deny_write_access(struct file
* file
)
264 struct inode
*inode
= file
->f_dentry
->d_inode
;
266 spin_lock(&inode
->i_lock
);
267 if (atomic_read(&inode
->i_writecount
) > 0) {
268 spin_unlock(&inode
->i_lock
);
271 atomic_dec(&inode
->i_writecount
);
272 spin_unlock(&inode
->i_lock
);
277 void path_release(struct nameidata
*nd
)
284 * umount() mustn't call path_release()/mntput() as that would clear
287 void path_release_on_umount(struct nameidata
*nd
)
294 * Internal lookup() using the new generic dcache.
297 static struct dentry
* cached_lookup(struct dentry
* parent
, struct qstr
* name
, struct nameidata
*nd
)
299 struct dentry
* dentry
= __d_lookup(parent
, name
);
301 /* lockess __d_lookup may fail due to concurrent d_move()
302 * in some unrelated directory, so try with d_lookup
305 dentry
= d_lookup(parent
, name
);
307 if (dentry
&& dentry
->d_op
&& dentry
->d_op
->d_revalidate
) {
308 if (!dentry
->d_op
->d_revalidate(dentry
, nd
) && !d_invalidate(dentry
)) {
317 * Short-cut version of permission(), for calling by
318 * path_walk(), when dcache lock is held. Combines parts
319 * of permission() and vfs_permission(), and tests ONLY for
320 * MAY_EXEC permission.
322 * If appropriate, check DAC only. If not appropriate, or
323 * short-cut DAC fails, then call permission() to do more
324 * complete permission check.
326 static inline int exec_permission_lite(struct inode
*inode
,
327 struct nameidata
*nd
)
329 umode_t mode
= inode
->i_mode
;
331 if (inode
->i_op
&& inode
->i_op
->permission
)
334 if (current
->fsuid
== inode
->i_uid
)
336 else if (in_group_p(inode
->i_gid
))
342 if ((inode
->i_mode
& S_IXUGO
) && capable(CAP_DAC_OVERRIDE
))
345 if (S_ISDIR(inode
->i_mode
) && capable(CAP_DAC_OVERRIDE
))
348 if (S_ISDIR(inode
->i_mode
) && capable(CAP_DAC_READ_SEARCH
))
353 return security_inode_permission(inode
, MAY_EXEC
, nd
);
357 * This is called when everything else fails, and we actually have
358 * to go to the low-level filesystem to find out what we should do..
360 * We get the directory semaphore, and after getting that we also
361 * make sure that nobody added the entry to the dcache in the meantime..
364 static struct dentry
* real_lookup(struct dentry
* parent
, struct qstr
* name
, struct nameidata
*nd
)
366 struct dentry
* result
;
367 struct inode
*dir
= parent
->d_inode
;
371 * First re-do the cached lookup just in case it was created
372 * while we waited for the directory semaphore..
374 * FIXME! This could use version numbering or similar to
375 * avoid unnecessary cache lookups.
377 * The "dcache_lock" is purely to protect the RCU list walker
378 * from concurrent renames at this point (we mustn't get false
379 * negatives from the RCU list walk here, unlike the optimistic
382 * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
384 result
= d_lookup(parent
, name
);
386 struct dentry
* dentry
= d_alloc(parent
, name
);
387 result
= ERR_PTR(-ENOMEM
);
389 result
= dir
->i_op
->lookup(dir
, dentry
, nd
);
400 * Uhhuh! Nasty case: the cache was re-populated while
401 * we waited on the semaphore. Need to revalidate.
404 if (result
->d_op
&& result
->d_op
->d_revalidate
) {
405 if (!result
->d_op
->d_revalidate(result
, nd
) && !d_invalidate(result
)) {
407 result
= ERR_PTR(-ENOENT
);
413 static int __emul_lookup_dentry(const char *, struct nameidata
*);
417 walk_init_root(const char *name
, struct nameidata
*nd
)
419 read_lock(¤t
->fs
->lock
);
420 if (current
->fs
->altroot
&& !(nd
->flags
& LOOKUP_NOALT
)) {
421 nd
->mnt
= mntget(current
->fs
->altrootmnt
);
422 nd
->dentry
= dget(current
->fs
->altroot
);
423 read_unlock(¤t
->fs
->lock
);
424 if (__emul_lookup_dentry(name
,nd
))
426 read_lock(¤t
->fs
->lock
);
428 nd
->mnt
= mntget(current
->fs
->rootmnt
);
429 nd
->dentry
= dget(current
->fs
->root
);
430 read_unlock(¤t
->fs
->lock
);
434 static inline int __vfs_follow_link(struct nameidata
*nd
, const char *link
)
443 if (!walk_init_root(link
, nd
))
444 /* weird __emul_prefix() stuff did it */
447 res
= link_path_walk(link
, nd
);
449 if (nd
->depth
|| res
|| nd
->last_type
!=LAST_NORM
)
452 * If it is an iterative symlinks resolution in open_namei() we
453 * have to copy the last component. And all that crap because of
454 * bloody create() on broken symlinks. Furrfu...
457 if (unlikely(!name
)) {
461 strcpy(name
, nd
->last
.name
);
462 nd
->last
.name
= name
;
466 return PTR_ERR(link
);
470 * This limits recursive symlink follows to 8, while
471 * limiting consecutive symlinks to 40.
473 * Without that kind of total limit, nasty chains of consecutive
474 * symlinks can cause almost arbitrarily long lookups.
476 static inline int do_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
479 if (current
->link_count
>= MAX_NESTED_LINKS
)
481 if (current
->total_link_count
>= 40)
483 BUG_ON(nd
->depth
>= MAX_NESTED_LINKS
);
485 err
= security_inode_follow_link(dentry
, nd
);
488 current
->link_count
++;
489 current
->total_link_count
++;
491 touch_atime(nd
->mnt
, dentry
);
492 nd_set_link(nd
, NULL
);
493 err
= dentry
->d_inode
->i_op
->follow_link(dentry
, nd
);
495 char *s
= nd_get_link(nd
);
497 err
= __vfs_follow_link(nd
, s
);
498 if (dentry
->d_inode
->i_op
->put_link
)
499 dentry
->d_inode
->i_op
->put_link(dentry
, nd
);
501 current
->link_count
--;
509 int follow_up(struct vfsmount
**mnt
, struct dentry
**dentry
)
511 struct vfsmount
*parent
;
512 struct dentry
*mountpoint
;
513 spin_lock(&vfsmount_lock
);
514 parent
=(*mnt
)->mnt_parent
;
515 if (parent
== *mnt
) {
516 spin_unlock(&vfsmount_lock
);
520 mountpoint
=dget((*mnt
)->mnt_mountpoint
);
521 spin_unlock(&vfsmount_lock
);
523 *dentry
= mountpoint
;
529 /* no need for dcache_lock, as serialization is taken care in
532 static int follow_mount(struct vfsmount
**mnt
, struct dentry
**dentry
)
535 while (d_mountpoint(*dentry
)) {
536 struct vfsmount
*mounted
= lookup_mnt(*mnt
, *dentry
);
542 *dentry
= dget(mounted
->mnt_root
);
548 /* no need for dcache_lock, as serialization is taken care in
551 static inline int __follow_down(struct vfsmount
**mnt
, struct dentry
**dentry
)
553 struct vfsmount
*mounted
;
555 mounted
= lookup_mnt(*mnt
, *dentry
);
560 *dentry
= dget(mounted
->mnt_root
);
566 int follow_down(struct vfsmount
**mnt
, struct dentry
**dentry
)
568 return __follow_down(mnt
,dentry
);
571 static inline void follow_dotdot(struct vfsmount
**mnt
, struct dentry
**dentry
)
574 struct vfsmount
*parent
;
575 struct dentry
*old
= *dentry
;
577 read_lock(¤t
->fs
->lock
);
578 if (*dentry
== current
->fs
->root
&&
579 *mnt
== current
->fs
->rootmnt
) {
580 read_unlock(¤t
->fs
->lock
);
583 read_unlock(¤t
->fs
->lock
);
584 spin_lock(&dcache_lock
);
585 if (*dentry
!= (*mnt
)->mnt_root
) {
586 *dentry
= dget((*dentry
)->d_parent
);
587 spin_unlock(&dcache_lock
);
591 spin_unlock(&dcache_lock
);
592 spin_lock(&vfsmount_lock
);
593 parent
= (*mnt
)->mnt_parent
;
594 if (parent
== *mnt
) {
595 spin_unlock(&vfsmount_lock
);
599 *dentry
= dget((*mnt
)->mnt_mountpoint
);
600 spin_unlock(&vfsmount_lock
);
605 follow_mount(mnt
, dentry
);
609 struct vfsmount
*mnt
;
610 struct dentry
*dentry
;
614 * It's more convoluted than I'd like it to be, but... it's still fairly
615 * small and for now I'd prefer to have fast path as straight as possible.
616 * It _is_ time-critical.
618 static int do_lookup(struct nameidata
*nd
, struct qstr
*name
,
621 struct vfsmount
*mnt
= nd
->mnt
;
622 struct dentry
*dentry
= __d_lookup(nd
->dentry
, name
);
626 if (dentry
->d_op
&& dentry
->d_op
->d_revalidate
)
627 goto need_revalidate
;
630 path
->dentry
= dentry
;
634 dentry
= real_lookup(nd
->dentry
, name
, nd
);
640 if (dentry
->d_op
->d_revalidate(dentry
, nd
))
642 if (d_invalidate(dentry
))
648 return PTR_ERR(dentry
);
654 * This is the basic name resolution function, turning a pathname
655 * into the final dentry.
657 * We expect 'base' to be positive and a directory.
659 int fastcall
link_path_walk(const char * name
, struct nameidata
*nd
)
664 unsigned int lookup_flags
= nd
->flags
;
671 inode
= nd
->dentry
->d_inode
;
673 lookup_flags
= LOOKUP_FOLLOW
;
675 /* At this point we know we have a real path component. */
681 err
= exec_permission_lite(inode
, nd
);
682 if (err
== -EAGAIN
) {
683 err
= permission(inode
, MAY_EXEC
, nd
);
689 c
= *(const unsigned char *)name
;
691 hash
= init_name_hash();
694 hash
= partial_name_hash(c
, hash
);
695 c
= *(const unsigned char *)name
;
696 } while (c
&& (c
!= '/'));
697 this.len
= name
- (const char *) this.name
;
698 this.hash
= end_name_hash(hash
);
700 /* remove trailing slashes? */
703 while (*++name
== '/');
705 goto last_with_slashes
;
708 * "." and ".." are special - ".." especially so because it has
709 * to be able to know about the current root directory and
710 * parent relationships.
712 if (this.name
[0] == '.') switch (this.len
) {
716 if (this.name
[1] != '.')
718 follow_dotdot(&nd
->mnt
, &nd
->dentry
);
719 inode
= nd
->dentry
->d_inode
;
725 * See if the low-level filesystem might want
726 * to use its own hash..
728 if (nd
->dentry
->d_op
&& nd
->dentry
->d_op
->d_hash
) {
729 err
= nd
->dentry
->d_op
->d_hash(nd
->dentry
, &this);
733 nd
->flags
|= LOOKUP_CONTINUE
;
734 /* This does the actual lookups.. */
735 err
= do_lookup(nd
, &this, &next
);
738 /* Check mountpoints.. */
739 follow_mount(&next
.mnt
, &next
.dentry
);
742 inode
= next
.dentry
->d_inode
;
749 if (inode
->i_op
->follow_link
) {
751 err
= do_follow_link(next
.dentry
, nd
);
757 inode
= nd
->dentry
->d_inode
;
766 nd
->dentry
= next
.dentry
;
769 if (!inode
->i_op
->lookup
)
772 /* here ends the main loop */
775 lookup_flags
|= LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
;
777 nd
->flags
&= ~LOOKUP_CONTINUE
;
778 if (lookup_flags
& LOOKUP_PARENT
)
780 if (this.name
[0] == '.') switch (this.len
) {
784 if (this.name
[1] != '.')
786 follow_dotdot(&nd
->mnt
, &nd
->dentry
);
787 inode
= nd
->dentry
->d_inode
;
792 if (nd
->dentry
->d_op
&& nd
->dentry
->d_op
->d_hash
) {
793 err
= nd
->dentry
->d_op
->d_hash(nd
->dentry
, &this);
797 err
= do_lookup(nd
, &this, &next
);
800 follow_mount(&next
.mnt
, &next
.dentry
);
801 inode
= next
.dentry
->d_inode
;
802 if ((lookup_flags
& LOOKUP_FOLLOW
)
803 && inode
&& inode
->i_op
&& inode
->i_op
->follow_link
) {
805 err
= do_follow_link(next
.dentry
, nd
);
810 inode
= nd
->dentry
->d_inode
;
814 nd
->dentry
= next
.dentry
;
819 if (lookup_flags
& LOOKUP_DIRECTORY
) {
821 if (!inode
->i_op
|| !inode
->i_op
->lookup
)
827 nd
->last_type
= LAST_NORM
;
828 if (this.name
[0] != '.')
831 nd
->last_type
= LAST_DOT
;
832 else if (this.len
== 2 && this.name
[1] == '.')
833 nd
->last_type
= LAST_DOTDOT
;
838 * We bypassed the ordinary revalidation routines.
839 * We may need to check the cached dentry for staleness.
841 if (nd
->dentry
&& nd
->dentry
->d_sb
&&
842 (nd
->dentry
->d_sb
->s_type
->fs_flags
& FS_REVAL_DOT
)) {
844 /* Note: we do not d_invalidate() */
845 if (!nd
->dentry
->d_op
->d_revalidate(nd
->dentry
, nd
))
859 int fastcall
path_walk(const char * name
, struct nameidata
*nd
)
861 current
->total_link_count
= 0;
862 return link_path_walk(name
, nd
);
866 /* returns 1 if everything is done */
867 static int __emul_lookup_dentry(const char *name
, struct nameidata
*nd
)
869 if (path_walk(name
, nd
))
870 return 0; /* something went wrong... */
872 if (!nd
->dentry
->d_inode
|| S_ISDIR(nd
->dentry
->d_inode
->i_mode
)) {
873 struct dentry
*old_dentry
= nd
->dentry
;
874 struct vfsmount
*old_mnt
= nd
->mnt
;
875 struct qstr last
= nd
->last
;
876 int last_type
= nd
->last_type
;
878 * NAME was not found in alternate root or it's a directory. Try to find
879 * it in the normal root:
881 nd
->last_type
= LAST_ROOT
;
882 read_lock(¤t
->fs
->lock
);
883 nd
->mnt
= mntget(current
->fs
->rootmnt
);
884 nd
->dentry
= dget(current
->fs
->root
);
885 read_unlock(¤t
->fs
->lock
);
886 if (path_walk(name
, nd
) == 0) {
887 if (nd
->dentry
->d_inode
) {
894 nd
->dentry
= old_dentry
;
897 nd
->last_type
= last_type
;
902 void set_fs_altroot(void)
904 char *emul
= __emul_prefix();
906 struct vfsmount
*mnt
= NULL
, *oldmnt
;
907 struct dentry
*dentry
= NULL
, *olddentry
;
912 err
= path_lookup(emul
, LOOKUP_FOLLOW
|LOOKUP_DIRECTORY
|LOOKUP_NOALT
, &nd
);
918 write_lock(¤t
->fs
->lock
);
919 oldmnt
= current
->fs
->altrootmnt
;
920 olddentry
= current
->fs
->altroot
;
921 current
->fs
->altrootmnt
= mnt
;
922 current
->fs
->altroot
= dentry
;
923 write_unlock(¤t
->fs
->lock
);
930 int fastcall
path_lookup(const char *name
, unsigned int flags
, struct nameidata
*nd
)
934 nd
->last_type
= LAST_ROOT
; /* if there are only slashes... */
938 read_lock(¤t
->fs
->lock
);
940 if (current
->fs
->altroot
&& !(nd
->flags
& LOOKUP_NOALT
)) {
941 nd
->mnt
= mntget(current
->fs
->altrootmnt
);
942 nd
->dentry
= dget(current
->fs
->altroot
);
943 read_unlock(¤t
->fs
->lock
);
944 if (__emul_lookup_dentry(name
,nd
))
946 read_lock(¤t
->fs
->lock
);
948 nd
->mnt
= mntget(current
->fs
->rootmnt
);
949 nd
->dentry
= dget(current
->fs
->root
);
951 nd
->mnt
= mntget(current
->fs
->pwdmnt
);
952 nd
->dentry
= dget(current
->fs
->pwd
);
954 read_unlock(¤t
->fs
->lock
);
955 current
->total_link_count
= 0;
956 retval
= link_path_walk(name
, nd
);
957 if (unlikely(current
->audit_context
958 && nd
&& nd
->dentry
&& nd
->dentry
->d_inode
))
960 nd
->dentry
->d_inode
->i_ino
,
961 nd
->dentry
->d_inode
->i_rdev
);
966 * Restricted form of lookup. Doesn't follow links, single-component only,
967 * needs parent already locked. Doesn't follow mounts.
970 static struct dentry
* __lookup_hash(struct qstr
*name
, struct dentry
* base
, struct nameidata
*nd
)
972 struct dentry
* dentry
;
976 inode
= base
->d_inode
;
977 err
= permission(inode
, MAY_EXEC
, nd
);
978 dentry
= ERR_PTR(err
);
983 * See if the low-level filesystem might want
984 * to use its own hash..
986 if (base
->d_op
&& base
->d_op
->d_hash
) {
987 err
= base
->d_op
->d_hash(base
, name
);
988 dentry
= ERR_PTR(err
);
993 dentry
= cached_lookup(base
, name
, nd
);
995 struct dentry
*new = d_alloc(base
, name
);
996 dentry
= ERR_PTR(-ENOMEM
);
999 dentry
= inode
->i_op
->lookup(inode
, new, nd
);
1009 struct dentry
* lookup_hash(struct qstr
*name
, struct dentry
* base
)
1011 return __lookup_hash(name
, base
, NULL
);
1015 struct dentry
* lookup_one_len(const char * name
, struct dentry
* base
, int len
)
1026 hash
= init_name_hash();
1028 c
= *(const unsigned char *)name
++;
1029 if (c
== '/' || c
== '\0')
1031 hash
= partial_name_hash(c
, hash
);
1033 this.hash
= end_name_hash(hash
);
1035 return lookup_hash(&this, base
);
1037 return ERR_PTR(-EACCES
);
1043 * is used by most simple commands to get the inode of a specified name.
1044 * Open, link etc use their own routines, but this is enough for things
1047 * namei exists in two versions: namei/lnamei. The only difference is
1048 * that namei follows links, while lnamei does not.
1051 int fastcall
__user_walk(const char __user
*name
, unsigned flags
, struct nameidata
*nd
)
1053 char *tmp
= getname(name
);
1054 int err
= PTR_ERR(tmp
);
1057 err
= path_lookup(tmp
, flags
, nd
);
1064 * It's inline, so penalty for filesystems that don't use sticky bit is
1067 static inline int check_sticky(struct inode
*dir
, struct inode
*inode
)
1069 if (!(dir
->i_mode
& S_ISVTX
))
1071 if (inode
->i_uid
== current
->fsuid
)
1073 if (dir
->i_uid
== current
->fsuid
)
1075 return !capable(CAP_FOWNER
);
1079 * Check whether we can remove a link victim from directory dir, check
1080 * whether the type of victim is right.
1081 * 1. We can't do it if dir is read-only (done in permission())
1082 * 2. We should have write and exec permissions on dir
1083 * 3. We can't remove anything from append-only dir
1084 * 4. We can't do anything with immutable dir (done in permission())
1085 * 5. If the sticky bit on dir is set we should either
1086 * a. be owner of dir, or
1087 * b. be owner of victim, or
1088 * c. have CAP_FOWNER capability
1089 * 6. If the victim is append-only or immutable we can't do antyhing with
1090 * links pointing to it.
1091 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1092 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1093 * 9. We can't remove a root or mountpoint.
1094 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1095 * nfs_async_unlink().
1097 static inline int may_delete(struct inode
*dir
,struct dentry
*victim
,int isdir
)
1101 if (!victim
->d_inode
)
1104 BUG_ON(victim
->d_parent
->d_inode
!= dir
);
1106 error
= permission(dir
,MAY_WRITE
| MAY_EXEC
, NULL
);
1111 if (check_sticky(dir
, victim
->d_inode
)||IS_APPEND(victim
->d_inode
)||
1112 IS_IMMUTABLE(victim
->d_inode
))
1115 if (!S_ISDIR(victim
->d_inode
->i_mode
))
1117 if (IS_ROOT(victim
))
1119 } else if (S_ISDIR(victim
->d_inode
->i_mode
))
1121 if (IS_DEADDIR(dir
))
1123 if (victim
->d_flags
& DCACHE_NFSFS_RENAMED
)
1128 /* Check whether we can create an object with dentry child in directory
1130 * 1. We can't do it if child already exists (open has special treatment for
1131 * this case, but since we are inlined it's OK)
1132 * 2. We can't do it if dir is read-only (done in permission())
1133 * 3. We should have write and exec permissions on dir
1134 * 4. We can't do it if dir is immutable (done in permission())
1136 static inline int may_create(struct inode
*dir
, struct dentry
*child
,
1137 struct nameidata
*nd
)
1141 if (IS_DEADDIR(dir
))
1143 return permission(dir
,MAY_WRITE
| MAY_EXEC
, nd
);
1147 * Special case: O_CREAT|O_EXCL implies O_NOFOLLOW for security
1150 * O_DIRECTORY translates into forcing a directory lookup.
1152 static inline int lookup_flags(unsigned int f
)
1154 unsigned long retval
= LOOKUP_FOLLOW
;
1157 retval
&= ~LOOKUP_FOLLOW
;
1159 if ((f
& (O_CREAT
|O_EXCL
)) == (O_CREAT
|O_EXCL
))
1160 retval
&= ~LOOKUP_FOLLOW
;
1162 if (f
& O_DIRECTORY
)
1163 retval
|= LOOKUP_DIRECTORY
;
1169 * p1 and p2 should be directories on the same fs.
1171 struct dentry
*lock_rename(struct dentry
*p1
, struct dentry
*p2
)
1176 down(&p1
->d_inode
->i_sem
);
1180 down(&p1
->d_inode
->i_sb
->s_vfs_rename_sem
);
1182 for (p
= p1
; p
->d_parent
!= p
; p
= p
->d_parent
) {
1183 if (p
->d_parent
== p2
) {
1184 down(&p2
->d_inode
->i_sem
);
1185 down(&p1
->d_inode
->i_sem
);
1190 for (p
= p2
; p
->d_parent
!= p
; p
= p
->d_parent
) {
1191 if (p
->d_parent
== p1
) {
1192 down(&p1
->d_inode
->i_sem
);
1193 down(&p2
->d_inode
->i_sem
);
1198 down(&p1
->d_inode
->i_sem
);
1199 down(&p2
->d_inode
->i_sem
);
1203 void unlock_rename(struct dentry
*p1
, struct dentry
*p2
)
1205 up(&p1
->d_inode
->i_sem
);
1207 up(&p2
->d_inode
->i_sem
);
1208 up(&p1
->d_inode
->i_sb
->s_vfs_rename_sem
);
1212 int vfs_create(struct inode
*dir
, struct dentry
*dentry
, int mode
,
1213 struct nameidata
*nd
)
1215 int error
= may_create(dir
, dentry
, nd
);
1220 if (!dir
->i_op
|| !dir
->i_op
->create
)
1221 return -EACCES
; /* shouldn't it be ENOSYS? */
1224 error
= security_inode_create(dir
, dentry
, mode
);
1228 error
= dir
->i_op
->create(dir
, dentry
, mode
, nd
);
1230 inode_dir_notify(dir
, DN_CREATE
);
1231 security_inode_post_create(dir
, dentry
, mode
);
1236 int may_open(struct nameidata
*nd
, int acc_mode
, int flag
)
1238 struct dentry
*dentry
= nd
->dentry
;
1239 struct inode
*inode
= dentry
->d_inode
;
1245 if (S_ISLNK(inode
->i_mode
))
1248 if (S_ISDIR(inode
->i_mode
) && (flag
& FMODE_WRITE
))
1251 error
= permission(inode
, acc_mode
, nd
);
1256 * FIFO's, sockets and device files are special: they don't
1257 * actually live on the filesystem itself, and as such you
1258 * can write to them even if the filesystem is read-only.
1260 if (S_ISFIFO(inode
->i_mode
) || S_ISSOCK(inode
->i_mode
)) {
1262 } else if (S_ISBLK(inode
->i_mode
) || S_ISCHR(inode
->i_mode
)) {
1263 if (nd
->mnt
->mnt_flags
& MNT_NODEV
)
1267 } else if (IS_RDONLY(inode
) && (flag
& FMODE_WRITE
))
1270 * An append-only file must be opened in append mode for writing.
1272 if (IS_APPEND(inode
)) {
1273 if ((flag
& FMODE_WRITE
) && !(flag
& O_APPEND
))
1279 /* O_NOATIME can only be set by the owner or superuser */
1280 if (flag
& O_NOATIME
)
1281 if (current
->fsuid
!= inode
->i_uid
&& !capable(CAP_FOWNER
))
1285 * Ensure there are no outstanding leases on the file.
1287 error
= break_lease(inode
, flag
);
1291 if (flag
& O_TRUNC
) {
1292 error
= get_write_access(inode
);
1297 * Refuse to truncate files with mandatory locks held on them.
1299 error
= locks_verify_locked(inode
);
1303 error
= do_truncate(dentry
, 0);
1305 put_write_access(inode
);
1309 if (flag
& FMODE_WRITE
)
1318 * namei for open - this is in fact almost the whole open-routine.
1320 * Note that the low bits of "flag" aren't the same as in the open
1321 * system call - they are 00 - no permissions needed
1322 * 01 - read permission needed
1323 * 10 - write permission needed
1324 * 11 - read/write permissions needed
1325 * which is a lot more logical, and also allows the "no perm" needed
1326 * for symlinks (where the permissions are checked later).
1329 int open_namei(const char * pathname
, int flag
, int mode
, struct nameidata
*nd
)
1331 int acc_mode
, error
= 0;
1332 struct dentry
*dentry
;
1336 acc_mode
= ACC_MODE(flag
);
1338 /* Allow the LSM permission hook to distinguish append
1339 access from general write access. */
1340 if (flag
& O_APPEND
)
1341 acc_mode
|= MAY_APPEND
;
1343 /* Fill in the open() intent data */
1344 nd
->intent
.open
.flags
= flag
;
1345 nd
->intent
.open
.create_mode
= mode
;
1348 * The simplest case - just a plain lookup.
1350 if (!(flag
& O_CREAT
)) {
1351 error
= path_lookup(pathname
, lookup_flags(flag
)|LOOKUP_OPEN
, nd
);
1358 * Create - we need to know the parent.
1360 error
= path_lookup(pathname
, LOOKUP_PARENT
|LOOKUP_OPEN
|LOOKUP_CREATE
, nd
);
1365 * We have the parent and last component. First of all, check
1366 * that we are not asked to creat(2) an obvious directory - that
1370 if (nd
->last_type
!= LAST_NORM
|| nd
->last
.name
[nd
->last
.len
])
1374 nd
->flags
&= ~LOOKUP_PARENT
;
1375 down(&dir
->d_inode
->i_sem
);
1376 dentry
= __lookup_hash(&nd
->last
, nd
->dentry
, nd
);
1379 error
= PTR_ERR(dentry
);
1380 if (IS_ERR(dentry
)) {
1381 up(&dir
->d_inode
->i_sem
);
1385 /* Negative dentry, just create the file */
1386 if (!dentry
->d_inode
) {
1387 if (!IS_POSIXACL(dir
->d_inode
))
1388 mode
&= ~current
->fs
->umask
;
1389 error
= vfs_create(dir
->d_inode
, dentry
, mode
, nd
);
1390 up(&dir
->d_inode
->i_sem
);
1392 nd
->dentry
= dentry
;
1395 /* Don't check for write permission, don't truncate */
1402 * It already exists.
1404 up(&dir
->d_inode
->i_sem
);
1410 if (d_mountpoint(dentry
)) {
1412 if (flag
& O_NOFOLLOW
)
1414 while (__follow_down(&nd
->mnt
,&dentry
) && d_mountpoint(dentry
));
1417 if (!dentry
->d_inode
)
1419 if (dentry
->d_inode
->i_op
&& dentry
->d_inode
->i_op
->follow_link
)
1423 nd
->dentry
= dentry
;
1425 if (dentry
->d_inode
&& S_ISDIR(dentry
->d_inode
->i_mode
))
1428 error
= may_open(nd
, acc_mode
, flag
);
1441 if (flag
& O_NOFOLLOW
)
1444 * This is subtle. Instead of calling do_follow_link() we do the
1445 * thing by hands. The reason is that this way we have zero link_count
1446 * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1447 * After that we have the parent and last component, i.e.
1448 * we are in the same situation as after the first path_walk().
1449 * Well, almost - if the last component is normal we get its copy
1450 * stored in nd->last.name and we will have to putname() it when we
1451 * are done. Procfs-like symlinks just set LAST_BIND.
1453 nd
->flags
|= LOOKUP_PARENT
;
1454 error
= security_inode_follow_link(dentry
, nd
);
1457 touch_atime(nd
->mnt
, dentry
);
1458 nd_set_link(nd
, NULL
);
1459 error
= dentry
->d_inode
->i_op
->follow_link(dentry
, nd
);
1461 char *s
= nd_get_link(nd
);
1463 error
= __vfs_follow_link(nd
, s
);
1464 if (dentry
->d_inode
->i_op
->put_link
)
1465 dentry
->d_inode
->i_op
->put_link(dentry
, nd
);
1470 nd
->flags
&= ~LOOKUP_PARENT
;
1471 if (nd
->last_type
== LAST_BIND
) {
1472 dentry
= nd
->dentry
;
1476 if (nd
->last_type
!= LAST_NORM
)
1478 if (nd
->last
.name
[nd
->last
.len
]) {
1479 putname(nd
->last
.name
);
1484 putname(nd
->last
.name
);
1488 down(&dir
->d_inode
->i_sem
);
1489 dentry
= __lookup_hash(&nd
->last
, nd
->dentry
, nd
);
1490 putname(nd
->last
.name
);
1495 * lookup_create - lookup a dentry, creating it if it doesn't exist
1496 * @nd: nameidata info
1497 * @is_dir: directory flag
1499 * Simple function to lookup and return a dentry and create it
1500 * if it doesn't exist. Is SMP-safe.
1502 struct dentry
*lookup_create(struct nameidata
*nd
, int is_dir
)
1504 struct dentry
*dentry
;
1506 down(&nd
->dentry
->d_inode
->i_sem
);
1507 dentry
= ERR_PTR(-EEXIST
);
1508 if (nd
->last_type
!= LAST_NORM
)
1510 nd
->flags
&= ~LOOKUP_PARENT
;
1511 dentry
= lookup_hash(&nd
->last
, nd
->dentry
);
1514 if (!is_dir
&& nd
->last
.name
[nd
->last
.len
] && !dentry
->d_inode
)
1519 dentry
= ERR_PTR(-ENOENT
);
1524 int vfs_mknod(struct inode
*dir
, struct dentry
*dentry
, int mode
, dev_t dev
)
1526 int error
= may_create(dir
, dentry
, NULL
);
1531 if ((S_ISCHR(mode
) || S_ISBLK(mode
)) && !capable(CAP_MKNOD
))
1534 if (!dir
->i_op
|| !dir
->i_op
->mknod
)
1537 error
= security_inode_mknod(dir
, dentry
, mode
, dev
);
1542 error
= dir
->i_op
->mknod(dir
, dentry
, mode
, dev
);
1544 inode_dir_notify(dir
, DN_CREATE
);
1545 security_inode_post_mknod(dir
, dentry
, mode
, dev
);
1550 asmlinkage
long sys_mknod(const char __user
* filename
, int mode
, unsigned dev
)
1554 struct dentry
* dentry
;
1555 struct nameidata nd
;
1559 tmp
= getname(filename
);
1561 return PTR_ERR(tmp
);
1563 error
= path_lookup(tmp
, LOOKUP_PARENT
, &nd
);
1566 dentry
= lookup_create(&nd
, 0);
1567 error
= PTR_ERR(dentry
);
1569 if (!IS_POSIXACL(nd
.dentry
->d_inode
))
1570 mode
&= ~current
->fs
->umask
;
1571 if (!IS_ERR(dentry
)) {
1572 switch (mode
& S_IFMT
) {
1573 case 0: case S_IFREG
:
1574 error
= vfs_create(nd
.dentry
->d_inode
,dentry
,mode
,&nd
);
1576 case S_IFCHR
: case S_IFBLK
:
1577 error
= vfs_mknod(nd
.dentry
->d_inode
,dentry
,mode
,
1578 new_decode_dev(dev
));
1580 case S_IFIFO
: case S_IFSOCK
:
1581 error
= vfs_mknod(nd
.dentry
->d_inode
,dentry
,mode
,0);
1591 up(&nd
.dentry
->d_inode
->i_sem
);
1599 int vfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
1601 int error
= may_create(dir
, dentry
, NULL
);
1606 if (!dir
->i_op
|| !dir
->i_op
->mkdir
)
1609 mode
&= (S_IRWXUGO
|S_ISVTX
);
1610 error
= security_inode_mkdir(dir
, dentry
, mode
);
1615 error
= dir
->i_op
->mkdir(dir
, dentry
, mode
);
1617 inode_dir_notify(dir
, DN_CREATE
);
1618 security_inode_post_mkdir(dir
,dentry
, mode
);
1623 asmlinkage
long sys_mkdir(const char __user
* pathname
, int mode
)
1628 tmp
= getname(pathname
);
1629 error
= PTR_ERR(tmp
);
1631 struct dentry
*dentry
;
1632 struct nameidata nd
;
1634 error
= path_lookup(tmp
, LOOKUP_PARENT
, &nd
);
1637 dentry
= lookup_create(&nd
, 1);
1638 error
= PTR_ERR(dentry
);
1639 if (!IS_ERR(dentry
)) {
1640 if (!IS_POSIXACL(nd
.dentry
->d_inode
))
1641 mode
&= ~current
->fs
->umask
;
1642 error
= vfs_mkdir(nd
.dentry
->d_inode
, dentry
, mode
);
1645 up(&nd
.dentry
->d_inode
->i_sem
);
1655 * We try to drop the dentry early: we should have
1656 * a usage count of 2 if we're the only user of this
1657 * dentry, and if that is true (possibly after pruning
1658 * the dcache), then we drop the dentry now.
1660 * A low-level filesystem can, if it choses, legally
1663 * if (!d_unhashed(dentry))
1666 * if it cannot handle the case of removing a directory
1667 * that is still in use by something else..
1669 void dentry_unhash(struct dentry
*dentry
)
1672 spin_lock(&dcache_lock
);
1673 switch (atomic_read(&dentry
->d_count
)) {
1675 spin_unlock(&dcache_lock
);
1676 shrink_dcache_parent(dentry
);
1677 spin_lock(&dcache_lock
);
1678 if (atomic_read(&dentry
->d_count
) != 2)
1683 spin_unlock(&dcache_lock
);
1686 int vfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
1688 int error
= may_delete(dir
, dentry
, 1);
1693 if (!dir
->i_op
|| !dir
->i_op
->rmdir
)
1698 down(&dentry
->d_inode
->i_sem
);
1699 dentry_unhash(dentry
);
1700 if (d_mountpoint(dentry
))
1703 error
= security_inode_rmdir(dir
, dentry
);
1705 error
= dir
->i_op
->rmdir(dir
, dentry
);
1707 dentry
->d_inode
->i_flags
|= S_DEAD
;
1710 up(&dentry
->d_inode
->i_sem
);
1712 inode_dir_notify(dir
, DN_DELETE
);
1720 asmlinkage
long sys_rmdir(const char __user
* pathname
)
1724 struct dentry
*dentry
;
1725 struct nameidata nd
;
1727 name
= getname(pathname
);
1729 return PTR_ERR(name
);
1731 error
= path_lookup(name
, LOOKUP_PARENT
, &nd
);
1735 switch(nd
.last_type
) {
1746 down(&nd
.dentry
->d_inode
->i_sem
);
1747 dentry
= lookup_hash(&nd
.last
, nd
.dentry
);
1748 error
= PTR_ERR(dentry
);
1749 if (!IS_ERR(dentry
)) {
1750 error
= vfs_rmdir(nd
.dentry
->d_inode
, dentry
);
1753 up(&nd
.dentry
->d_inode
->i_sem
);
1761 int vfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
1763 int error
= may_delete(dir
, dentry
, 0);
1768 if (!dir
->i_op
|| !dir
->i_op
->unlink
)
1773 down(&dentry
->d_inode
->i_sem
);
1774 if (d_mountpoint(dentry
))
1777 error
= security_inode_unlink(dir
, dentry
);
1779 error
= dir
->i_op
->unlink(dir
, dentry
);
1781 up(&dentry
->d_inode
->i_sem
);
1783 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
1784 if (!error
&& !(dentry
->d_flags
& DCACHE_NFSFS_RENAMED
)) {
1786 inode_dir_notify(dir
, DN_DELETE
);
1792 * Make sure that the actual truncation of the file will occur outside its
1793 * directory's i_sem. Truncate can take a long time if there is a lot of
1794 * writeout happening, and we don't want to prevent access to the directory
1795 * while waiting on the I/O.
1797 asmlinkage
long sys_unlink(const char __user
* pathname
)
1801 struct dentry
*dentry
;
1802 struct nameidata nd
;
1803 struct inode
*inode
= NULL
;
1805 name
= getname(pathname
);
1807 return PTR_ERR(name
);
1809 error
= path_lookup(name
, LOOKUP_PARENT
, &nd
);
1813 if (nd
.last_type
!= LAST_NORM
)
1815 down(&nd
.dentry
->d_inode
->i_sem
);
1816 dentry
= lookup_hash(&nd
.last
, nd
.dentry
);
1817 error
= PTR_ERR(dentry
);
1818 if (!IS_ERR(dentry
)) {
1819 /* Why not before? Because we want correct error value */
1820 if (nd
.last
.name
[nd
.last
.len
])
1822 inode
= dentry
->d_inode
;
1824 atomic_inc(&inode
->i_count
);
1825 error
= vfs_unlink(nd
.dentry
->d_inode
, dentry
);
1829 up(&nd
.dentry
->d_inode
->i_sem
);
1831 iput(inode
); /* truncate the inode here */
1839 error
= !dentry
->d_inode
? -ENOENT
:
1840 S_ISDIR(dentry
->d_inode
->i_mode
) ? -EISDIR
: -ENOTDIR
;
1844 int vfs_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *oldname
, int mode
)
1846 int error
= may_create(dir
, dentry
, NULL
);
1851 if (!dir
->i_op
|| !dir
->i_op
->symlink
)
1854 error
= security_inode_symlink(dir
, dentry
, oldname
);
1859 error
= dir
->i_op
->symlink(dir
, dentry
, oldname
);
1861 inode_dir_notify(dir
, DN_CREATE
);
1862 security_inode_post_symlink(dir
, dentry
, oldname
);
1867 asmlinkage
long sys_symlink(const char __user
* oldname
, const char __user
* newname
)
1873 from
= getname(oldname
);
1875 return PTR_ERR(from
);
1876 to
= getname(newname
);
1877 error
= PTR_ERR(to
);
1879 struct dentry
*dentry
;
1880 struct nameidata nd
;
1882 error
= path_lookup(to
, LOOKUP_PARENT
, &nd
);
1885 dentry
= lookup_create(&nd
, 0);
1886 error
= PTR_ERR(dentry
);
1887 if (!IS_ERR(dentry
)) {
1888 error
= vfs_symlink(nd
.dentry
->d_inode
, dentry
, from
, S_IALLUGO
);
1891 up(&nd
.dentry
->d_inode
->i_sem
);
1900 int vfs_link(struct dentry
*old_dentry
, struct inode
*dir
, struct dentry
*new_dentry
)
1902 struct inode
*inode
= old_dentry
->d_inode
;
1908 error
= may_create(dir
, new_dentry
, NULL
);
1912 if (dir
->i_sb
!= inode
->i_sb
)
1916 * A link to an append-only or immutable file cannot be created.
1918 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
1920 if (!dir
->i_op
|| !dir
->i_op
->link
)
1922 if (S_ISDIR(old_dentry
->d_inode
->i_mode
))
1925 error
= security_inode_link(old_dentry
, dir
, new_dentry
);
1929 down(&old_dentry
->d_inode
->i_sem
);
1931 error
= dir
->i_op
->link(old_dentry
, dir
, new_dentry
);
1932 up(&old_dentry
->d_inode
->i_sem
);
1934 inode_dir_notify(dir
, DN_CREATE
);
1935 security_inode_post_link(old_dentry
, dir
, new_dentry
);
1941 * Hardlinks are often used in delicate situations. We avoid
1942 * security-related surprises by not following symlinks on the
1945 * We don't follow them on the oldname either to be compatible
1946 * with linux 2.0, and to avoid hard-linking to directories
1947 * and other special files. --ADM
1949 asmlinkage
long sys_link(const char __user
* oldname
, const char __user
* newname
)
1951 struct dentry
*new_dentry
;
1952 struct nameidata nd
, old_nd
;
1956 to
= getname(newname
);
1960 error
= __user_walk(oldname
, 0, &old_nd
);
1963 error
= path_lookup(to
, LOOKUP_PARENT
, &nd
);
1967 if (old_nd
.mnt
!= nd
.mnt
)
1969 new_dentry
= lookup_create(&nd
, 0);
1970 error
= PTR_ERR(new_dentry
);
1971 if (!IS_ERR(new_dentry
)) {
1972 error
= vfs_link(old_nd
.dentry
, nd
.dentry
->d_inode
, new_dentry
);
1975 up(&nd
.dentry
->d_inode
->i_sem
);
1979 path_release(&old_nd
);
1987 * The worst of all namespace operations - renaming directory. "Perverted"
1988 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
1990 * a) we can get into loop creation. Check is done in is_subdir().
1991 * b) race potential - two innocent renames can create a loop together.
1992 * That's where 4.4 screws up. Current fix: serialization on
1993 * sb->s_vfs_rename_sem. We might be more accurate, but that's another
1995 * c) we have to lock _three_ objects - parents and victim (if it exists).
1996 * And that - after we got ->i_sem on parents (until then we don't know
1997 * whether the target exists). Solution: try to be smart with locking
1998 * order for inodes. We rely on the fact that tree topology may change
1999 * only under ->s_vfs_rename_sem _and_ that parent of the object we
2000 * move will be locked. Thus we can rank directories by the tree
2001 * (ancestors first) and rank all non-directories after them.
2002 * That works since everybody except rename does "lock parent, lookup,
2003 * lock child" and rename is under ->s_vfs_rename_sem.
2004 * HOWEVER, it relies on the assumption that any object with ->lookup()
2005 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2006 * we'd better make sure that there's no link(2) for them.
2007 * d) some filesystems don't support opened-but-unlinked directories,
2008 * either because of layout or because they are not ready to deal with
2009 * all cases correctly. The latter will be fixed (taking this sort of
2010 * stuff into VFS), but the former is not going away. Solution: the same
2011 * trick as in rmdir().
2012 * e) conversion from fhandle to dentry may come in the wrong moment - when
2013 * we are removing the target. Solution: we will have to grab ->i_sem
2014 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
2015 * ->i_sem on parents, which works but leads to some truely excessive
2018 int vfs_rename_dir(struct inode
*old_dir
, struct dentry
*old_dentry
,
2019 struct inode
*new_dir
, struct dentry
*new_dentry
)
2022 struct inode
*target
;
2025 * If we are going to change the parent - check write permissions,
2026 * we'll need to flip '..'.
2028 if (new_dir
!= old_dir
) {
2029 error
= permission(old_dentry
->d_inode
, MAY_WRITE
, NULL
);
2034 error
= security_inode_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
2038 target
= new_dentry
->d_inode
;
2040 down(&target
->i_sem
);
2041 dentry_unhash(new_dentry
);
2043 if (d_mountpoint(old_dentry
)||d_mountpoint(new_dentry
))
2046 error
= old_dir
->i_op
->rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
2049 target
->i_flags
|= S_DEAD
;
2051 if (d_unhashed(new_dentry
))
2052 d_rehash(new_dentry
);
2056 d_move(old_dentry
,new_dentry
);
2057 security_inode_post_rename(old_dir
, old_dentry
,
2058 new_dir
, new_dentry
);
2063 int vfs_rename_other(struct inode
*old_dir
, struct dentry
*old_dentry
,
2064 struct inode
*new_dir
, struct dentry
*new_dentry
)
2066 struct inode
*target
;
2069 error
= security_inode_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
2074 target
= new_dentry
->d_inode
;
2076 down(&target
->i_sem
);
2077 if (d_mountpoint(old_dentry
)||d_mountpoint(new_dentry
))
2080 error
= old_dir
->i_op
->rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
2082 /* The following d_move() should become unconditional */
2083 if (!(old_dir
->i_sb
->s_type
->fs_flags
& FS_ODD_RENAME
))
2084 d_move(old_dentry
, new_dentry
);
2085 security_inode_post_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
2093 int vfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
2094 struct inode
*new_dir
, struct dentry
*new_dentry
)
2097 int is_dir
= S_ISDIR(old_dentry
->d_inode
->i_mode
);
2099 if (old_dentry
->d_inode
== new_dentry
->d_inode
)
2102 error
= may_delete(old_dir
, old_dentry
, is_dir
);
2106 if (!new_dentry
->d_inode
)
2107 error
= may_create(new_dir
, new_dentry
, NULL
);
2109 error
= may_delete(new_dir
, new_dentry
, is_dir
);
2113 if (!old_dir
->i_op
|| !old_dir
->i_op
->rename
)
2116 DQUOT_INIT(old_dir
);
2117 DQUOT_INIT(new_dir
);
2120 error
= vfs_rename_dir(old_dir
,old_dentry
,new_dir
,new_dentry
);
2122 error
= vfs_rename_other(old_dir
,old_dentry
,new_dir
,new_dentry
);
2124 if (old_dir
== new_dir
)
2125 inode_dir_notify(old_dir
, DN_RENAME
);
2127 inode_dir_notify(old_dir
, DN_DELETE
);
2128 inode_dir_notify(new_dir
, DN_CREATE
);
2134 static inline int do_rename(const char * oldname
, const char * newname
)
2137 struct dentry
* old_dir
, * new_dir
;
2138 struct dentry
* old_dentry
, *new_dentry
;
2139 struct dentry
* trap
;
2140 struct nameidata oldnd
, newnd
;
2142 error
= path_lookup(oldname
, LOOKUP_PARENT
, &oldnd
);
2146 error
= path_lookup(newname
, LOOKUP_PARENT
, &newnd
);
2151 if (oldnd
.mnt
!= newnd
.mnt
)
2154 old_dir
= oldnd
.dentry
;
2156 if (oldnd
.last_type
!= LAST_NORM
)
2159 new_dir
= newnd
.dentry
;
2160 if (newnd
.last_type
!= LAST_NORM
)
2163 trap
= lock_rename(new_dir
, old_dir
);
2165 old_dentry
= lookup_hash(&oldnd
.last
, old_dir
);
2166 error
= PTR_ERR(old_dentry
);
2167 if (IS_ERR(old_dentry
))
2169 /* source must exist */
2171 if (!old_dentry
->d_inode
)
2173 /* unless the source is a directory trailing slashes give -ENOTDIR */
2174 if (!S_ISDIR(old_dentry
->d_inode
->i_mode
)) {
2176 if (oldnd
.last
.name
[oldnd
.last
.len
])
2178 if (newnd
.last
.name
[newnd
.last
.len
])
2181 /* source should not be ancestor of target */
2183 if (old_dentry
== trap
)
2185 new_dentry
= lookup_hash(&newnd
.last
, new_dir
);
2186 error
= PTR_ERR(new_dentry
);
2187 if (IS_ERR(new_dentry
))
2189 /* target should not be an ancestor of source */
2191 if (new_dentry
== trap
)
2194 error
= vfs_rename(old_dir
->d_inode
, old_dentry
,
2195 new_dir
->d_inode
, new_dentry
);
2201 unlock_rename(new_dir
, old_dir
);
2203 path_release(&newnd
);
2205 path_release(&oldnd
);
2210 asmlinkage
long sys_rename(const char __user
* oldname
, const char __user
* newname
)
2216 from
= getname(oldname
);
2218 return PTR_ERR(from
);
2219 to
= getname(newname
);
2220 error
= PTR_ERR(to
);
2222 error
= do_rename(from
,to
);
2229 int vfs_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
, const char *link
)
2233 len
= PTR_ERR(link
);
2238 if (len
> (unsigned) buflen
)
2240 if (copy_to_user(buffer
, link
, len
))
2247 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
2248 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
2249 * using) it for any given inode is up to filesystem.
2251 int generic_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
)
2253 struct nameidata nd
;
2256 res
= dentry
->d_inode
->i_op
->follow_link(dentry
, &nd
);
2258 res
= vfs_readlink(dentry
, buffer
, buflen
, nd_get_link(&nd
));
2259 if (dentry
->d_inode
->i_op
->put_link
)
2260 dentry
->d_inode
->i_op
->put_link(dentry
, &nd
);
2265 int vfs_follow_link(struct nameidata
*nd
, const char *link
)
2267 return __vfs_follow_link(nd
, link
);
2270 /* get the link contents into pagecache */
2271 static char *page_getlink(struct dentry
* dentry
, struct page
**ppage
)
2274 struct address_space
*mapping
= dentry
->d_inode
->i_mapping
;
2275 page
= read_cache_page(mapping
, 0, (filler_t
*)mapping
->a_ops
->readpage
,
2279 wait_on_page_locked(page
);
2280 if (!PageUptodate(page
))
2286 page_cache_release(page
);
2287 return ERR_PTR(-EIO
);
2293 int page_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
)
2295 struct page
*page
= NULL
;
2296 char *s
= page_getlink(dentry
, &page
);
2297 int res
= vfs_readlink(dentry
,buffer
,buflen
,s
);
2300 page_cache_release(page
);
2305 int page_follow_link_light(struct dentry
*dentry
, struct nameidata
*nd
)
2308 nd_set_link(nd
, page_getlink(dentry
, &page
));
2312 void page_put_link(struct dentry
*dentry
, struct nameidata
*nd
)
2314 if (!IS_ERR(nd_get_link(nd
))) {
2316 page
= find_get_page(dentry
->d_inode
->i_mapping
, 0);
2320 page_cache_release(page
);
2321 page_cache_release(page
);
2325 int page_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
2327 struct page
*page
= NULL
;
2328 char *s
= page_getlink(dentry
, &page
);
2329 int res
= __vfs_follow_link(nd
, s
);
2332 page_cache_release(page
);
2337 int page_symlink(struct inode
*inode
, const char *symname
, int len
)
2339 struct address_space
*mapping
= inode
->i_mapping
;
2340 struct page
*page
= grab_cache_page(mapping
, 0);
2346 err
= mapping
->a_ops
->prepare_write(NULL
, page
, 0, len
-1);
2349 kaddr
= kmap_atomic(page
, KM_USER0
);
2350 memcpy(kaddr
, symname
, len
-1);
2351 kunmap_atomic(kaddr
, KM_USER0
);
2352 mapping
->a_ops
->commit_write(NULL
, page
, 0, len
-1);
2354 * Notice that we are _not_ going to block here - end of page is
2355 * unmapped, so this will only try to map the rest of page, see
2356 * that it is unmapped (typically even will not look into inode -
2357 * ->i_size will be enough for everything) and zero it out.
2358 * OTOH it's obviously correct and should make the page up-to-date.
2360 if (!PageUptodate(page
)) {
2361 err
= mapping
->a_ops
->readpage(NULL
, page
);
2362 wait_on_page_locked(page
);
2366 page_cache_release(page
);
2369 mark_inode_dirty(inode
);
2373 page_cache_release(page
);
2378 struct inode_operations page_symlink_inode_operations
= {
2379 .readlink
= generic_readlink
,
2380 .follow_link
= page_follow_link_light
,
2381 .put_link
= page_put_link
,
2384 EXPORT_SYMBOL(__user_walk
);
2385 EXPORT_SYMBOL(follow_down
);
2386 EXPORT_SYMBOL(follow_up
);
2387 EXPORT_SYMBOL(get_write_access
); /* binfmt_aout */
2388 EXPORT_SYMBOL(getname
);
2389 EXPORT_SYMBOL(lock_rename
);
2390 EXPORT_SYMBOL(lookup_create
);
2391 EXPORT_SYMBOL(lookup_hash
);
2392 EXPORT_SYMBOL(lookup_one_len
);
2393 EXPORT_SYMBOL(page_follow_link
);
2394 EXPORT_SYMBOL(page_follow_link_light
);
2395 EXPORT_SYMBOL(page_put_link
);
2396 EXPORT_SYMBOL(page_readlink
);
2397 EXPORT_SYMBOL(page_symlink
);
2398 EXPORT_SYMBOL(page_symlink_inode_operations
);
2399 EXPORT_SYMBOL(path_lookup
);
2400 EXPORT_SYMBOL(path_release
);
2401 EXPORT_SYMBOL(path_walk
);
2402 EXPORT_SYMBOL(permission
);
2403 EXPORT_SYMBOL(unlock_rename
);
2404 EXPORT_SYMBOL(vfs_create
);
2405 EXPORT_SYMBOL(vfs_follow_link
);
2406 EXPORT_SYMBOL(vfs_link
);
2407 EXPORT_SYMBOL(vfs_mkdir
);
2408 EXPORT_SYMBOL(vfs_mknod
);
2409 EXPORT_SYMBOL(vfs_permission
);
2410 EXPORT_SYMBOL(vfs_readlink
);
2411 EXPORT_SYMBOL(vfs_rename
);
2412 EXPORT_SYMBOL(vfs_rmdir
);
2413 EXPORT_SYMBOL(vfs_symlink
);
2414 EXPORT_SYMBOL(vfs_unlink
);
2415 EXPORT_SYMBOL(dentry_unhash
);
2416 EXPORT_SYMBOL(generic_readlink
);