4 * Copyright (C) 1991, 1992 Linus Torvalds
8 * Some corrections by tytso.
11 /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
14 /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
21 #include <linux/namei.h>
22 #include <linux/quotaops.h>
23 #include <linux/pagemap.h>
24 #include <linux/dnotify.h>
25 #include <linux/smp_lock.h>
26 #include <linux/personality.h>
27 #include <linux/security.h>
28 #include <linux/mount.h>
29 #include <linux/audit.h>
30 #include <asm/namei.h>
31 #include <asm/uaccess.h>
33 #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
35 /* [Feb-1997 T. Schoebel-Theuer]
36 * Fundamental changes in the pathname lookup mechanisms (namei)
37 * were necessary because of omirr. The reason is that omirr needs
38 * to know the _real_ pathname, not the user-supplied one, in case
39 * of symlinks (and also when transname replacements occur).
41 * The new code replaces the old recursive symlink resolution with
42 * an iterative one (in case of non-nested symlink chains). It does
43 * this with calls to <fs>_follow_link().
44 * As a side effect, dir_namei(), _namei() and follow_link() are now
45 * replaced with a single function lookup_dentry() that can handle all
46 * the special cases of the former code.
48 * With the new dcache, the pathname is stored at each inode, at least as
49 * long as the refcount of the inode is positive. As a side effect, the
50 * size of the dcache depends on the inode cache and thus is dynamic.
52 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
53 * resolution to correspond with current state of the code.
55 * Note that the symlink resolution is not *completely* iterative.
56 * There is still a significant amount of tail- and mid- recursion in
57 * the algorithm. Also, note that <fs>_readlink() is not used in
58 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
59 * may return different results than <fs>_follow_link(). Many virtual
60 * filesystems (including /proc) exhibit this behavior.
63 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
64 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
65 * and the name already exists in form of a symlink, try to create the new
66 * name indicated by the symlink. The old code always complained that the
67 * name already exists, due to not following the symlink even if its target
68 * is nonexistent. The new semantics affects also mknod() and link() when
69 * the name is a symlink pointing to a non-existant name.
71 * I don't know which semantics is the right one, since I have no access
72 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
73 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
74 * "old" one. Personally, I think the new semantics is much more logical.
75 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
76 * file does succeed in both HP-UX and SunOs, but not in Solaris
77 * and in the old Linux semantics.
80 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
81 * semantics. See the comments in "open_namei" and "do_link" below.
83 * [10-Sep-98 Alan Modra] Another symlink change.
86 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
87 * inside the path - always follow.
88 * in the last component in creation/removal/renaming - never follow.
89 * if LOOKUP_FOLLOW passed - follow.
90 * if the pathname has trailing slashes - follow.
91 * otherwise - don't follow.
92 * (applied in that order).
94 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
95 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
96 * During the 2.4 we need to fix the userland stuff depending on it -
97 * hopefully we will be able to get rid of that wart in 2.5. So far only
98 * XEmacs seems to be relying on it...
101 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
102 * implemented. Let's see if raised priority of ->s_vfs_rename_sem gives
103 * any extra contention...
106 /* In order to reduce some races, while at the same time doing additional
107 * checking and hopefully speeding things up, we copy filenames to the
108 * kernel data space before using them..
110 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
111 * PATH_MAX includes the nul terminator --RR.
113 static inline int do_getname(const char __user
*filename
, char *page
)
116 unsigned long len
= PATH_MAX
;
118 if ((unsigned long) filename
>= TASK_SIZE
) {
119 if (!segment_eq(get_fs(), KERNEL_DS
))
121 } else if (TASK_SIZE
- (unsigned long) filename
< PATH_MAX
)
122 len
= TASK_SIZE
- (unsigned long) filename
;
124 retval
= strncpy_from_user((char *)page
, filename
, len
);
128 return -ENAMETOOLONG
;
134 char * getname(const char __user
* filename
)
138 result
= ERR_PTR(-ENOMEM
);
141 int retval
= do_getname(filename
, tmp
);
146 result
= ERR_PTR(retval
);
149 if (unlikely(current
->audit_context
) && !IS_ERR(result
) && result
)
150 audit_getname(result
);
157 * is used to check for read/write/execute permissions on a file.
158 * We use "fsuid" for this, letting us set arbitrary permissions
159 * for filesystem access without changing the "normal" uids which
160 * are used for other things..
162 int vfs_permission(struct inode
* inode
, int mask
)
164 umode_t mode
= inode
->i_mode
;
166 if (mask
& MAY_WRITE
) {
168 * Nobody gets write access to a read-only fs.
170 if (IS_RDONLY(inode
) &&
171 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
)))
175 * Nobody gets write access to an immutable file.
177 if (IS_IMMUTABLE(inode
))
181 if (current
->fsuid
== inode
->i_uid
)
183 else if (in_group_p(inode
->i_gid
))
187 * If the DACs are ok we don't need any capability check.
189 if (((mode
& mask
& (MAY_READ
|MAY_WRITE
|MAY_EXEC
)) == mask
))
193 * Read/write DACs are always overridable.
194 * Executable DACs are overridable if at least one exec bit is set.
196 if (!(mask
& MAY_EXEC
) ||
197 (inode
->i_mode
& S_IXUGO
) || S_ISDIR(inode
->i_mode
))
198 if (capable(CAP_DAC_OVERRIDE
))
202 * Searching includes executable on directories, else just read.
204 if (mask
== MAY_READ
|| (S_ISDIR(inode
->i_mode
) && !(mask
& MAY_WRITE
)))
205 if (capable(CAP_DAC_READ_SEARCH
))
211 int permission(struct inode
* inode
,int mask
, struct nameidata
*nd
)
216 /* Ordinary permission routines do not understand MAY_APPEND. */
217 submask
= mask
& ~MAY_APPEND
;
219 if (inode
->i_op
&& inode
->i_op
->permission
)
220 retval
= inode
->i_op
->permission(inode
, submask
, nd
);
222 retval
= vfs_permission(inode
, submask
);
226 return security_inode_permission(inode
, mask
, nd
);
230 * get_write_access() gets write permission for a file.
231 * put_write_access() releases this write permission.
232 * This is used for regular files.
233 * We cannot support write (and maybe mmap read-write shared) accesses and
234 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
235 * can have the following values:
236 * 0: no writers, no VM_DENYWRITE mappings
237 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
238 * > 0: (i_writecount) users are writing to the file.
240 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
241 * except for the cases where we don't hold i_writecount yet. Then we need to
242 * use {get,deny}_write_access() - these functions check the sign and refuse
243 * to do the change if sign is wrong. Exclusion between them is provided by
244 * the inode->i_lock spinlock.
247 int get_write_access(struct inode
* inode
)
249 spin_lock(&inode
->i_lock
);
250 if (atomic_read(&inode
->i_writecount
) < 0) {
251 spin_unlock(&inode
->i_lock
);
254 atomic_inc(&inode
->i_writecount
);
255 spin_unlock(&inode
->i_lock
);
260 int deny_write_access(struct file
* file
)
262 struct inode
*inode
= file
->f_dentry
->d_inode
;
264 spin_lock(&inode
->i_lock
);
265 if (atomic_read(&inode
->i_writecount
) > 0) {
266 spin_unlock(&inode
->i_lock
);
269 atomic_dec(&inode
->i_writecount
);
270 spin_unlock(&inode
->i_lock
);
275 void path_release(struct nameidata
*nd
)
282 * umount() mustn't call path_release()/mntput() as that would clear
285 void path_release_on_umount(struct nameidata
*nd
)
292 * Internal lookup() using the new generic dcache.
295 static struct dentry
* cached_lookup(struct dentry
* parent
, struct qstr
* name
, struct nameidata
*nd
)
297 struct dentry
* dentry
= __d_lookup(parent
, name
);
299 /* lockess __d_lookup may fail due to concurrent d_move()
300 * in some unrelated directory, so try with d_lookup
303 dentry
= d_lookup(parent
, name
);
305 if (dentry
&& dentry
->d_op
&& dentry
->d_op
->d_revalidate
) {
306 if (!dentry
->d_op
->d_revalidate(dentry
, nd
) && !d_invalidate(dentry
)) {
315 * Short-cut version of permission(), for calling by
316 * path_walk(), when dcache lock is held. Combines parts
317 * of permission() and vfs_permission(), and tests ONLY for
318 * MAY_EXEC permission.
320 * If appropriate, check DAC only. If not appropriate, or
321 * short-cut DAC fails, then call permission() to do more
322 * complete permission check.
324 static inline int exec_permission_lite(struct inode
*inode
,
325 struct nameidata
*nd
)
327 umode_t mode
= inode
->i_mode
;
329 if (inode
->i_op
&& inode
->i_op
->permission
)
332 if (current
->fsuid
== inode
->i_uid
)
334 else if (in_group_p(inode
->i_gid
))
340 if ((inode
->i_mode
& S_IXUGO
) && capable(CAP_DAC_OVERRIDE
))
343 if (S_ISDIR(inode
->i_mode
) && capable(CAP_DAC_OVERRIDE
))
346 if (S_ISDIR(inode
->i_mode
) && capable(CAP_DAC_READ_SEARCH
))
351 return security_inode_permission(inode
, MAY_EXEC
, nd
);
355 * This is called when everything else fails, and we actually have
356 * to go to the low-level filesystem to find out what we should do..
358 * We get the directory semaphore, and after getting that we also
359 * make sure that nobody added the entry to the dcache in the meantime..
362 static struct dentry
* real_lookup(struct dentry
* parent
, struct qstr
* name
, struct nameidata
*nd
)
364 struct dentry
* result
;
365 struct inode
*dir
= parent
->d_inode
;
369 * First re-do the cached lookup just in case it was created
370 * while we waited for the directory semaphore..
372 * FIXME! This could use version numbering or similar to
373 * avoid unnecessary cache lookups.
375 * The "dcache_lock" is purely to protect the RCU list walker
376 * from concurrent renames at this point (we mustn't get false
377 * negatives from the RCU list walk here, unlike the optimistic
380 * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
382 result
= d_lookup(parent
, name
);
384 struct dentry
* dentry
= d_alloc(parent
, name
);
385 result
= ERR_PTR(-ENOMEM
);
387 result
= dir
->i_op
->lookup(dir
, dentry
, nd
);
398 * Uhhuh! Nasty case: the cache was re-populated while
399 * we waited on the semaphore. Need to revalidate.
402 if (result
->d_op
&& result
->d_op
->d_revalidate
) {
403 if (!result
->d_op
->d_revalidate(result
, nd
) && !d_invalidate(result
)) {
405 result
= ERR_PTR(-ENOENT
);
411 static int __emul_lookup_dentry(const char *, struct nameidata
*);
415 walk_init_root(const char *name
, struct nameidata
*nd
)
417 read_lock(¤t
->fs
->lock
);
418 if (current
->fs
->altroot
&& !(nd
->flags
& LOOKUP_NOALT
)) {
419 nd
->mnt
= mntget(current
->fs
->altrootmnt
);
420 nd
->dentry
= dget(current
->fs
->altroot
);
421 read_unlock(¤t
->fs
->lock
);
422 if (__emul_lookup_dentry(name
,nd
))
424 read_lock(¤t
->fs
->lock
);
426 nd
->mnt
= mntget(current
->fs
->rootmnt
);
427 nd
->dentry
= dget(current
->fs
->root
);
428 read_unlock(¤t
->fs
->lock
);
432 static inline int __vfs_follow_link(struct nameidata
*nd
, const char *link
)
441 if (!walk_init_root(link
, nd
))
442 /* weird __emul_prefix() stuff did it */
445 res
= link_path_walk(link
, nd
);
447 if (nd
->depth
|| res
|| nd
->last_type
!=LAST_NORM
)
450 * If it is an iterative symlinks resolution in open_namei() we
451 * have to copy the last component. And all that crap because of
452 * bloody create() on broken symlinks. Furrfu...
455 if (unlikely(!name
)) {
459 strcpy(name
, nd
->last
.name
);
460 nd
->last
.name
= name
;
464 return PTR_ERR(link
);
468 * This limits recursive symlink follows to 8, while
469 * limiting consecutive symlinks to 40.
471 * Without that kind of total limit, nasty chains of consecutive
472 * symlinks can cause almost arbitrarily long lookups.
474 static inline int do_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
477 if (current
->link_count
>= MAX_NESTED_LINKS
)
479 if (current
->total_link_count
>= 40)
481 BUG_ON(nd
->depth
>= MAX_NESTED_LINKS
);
483 err
= security_inode_follow_link(dentry
, nd
);
486 current
->link_count
++;
487 current
->total_link_count
++;
489 touch_atime(nd
->mnt
, dentry
);
490 nd_set_link(nd
, NULL
);
491 err
= dentry
->d_inode
->i_op
->follow_link(dentry
, nd
);
493 char *s
= nd_get_link(nd
);
495 err
= __vfs_follow_link(nd
, s
);
496 if (dentry
->d_inode
->i_op
->put_link
)
497 dentry
->d_inode
->i_op
->put_link(dentry
, nd
);
499 current
->link_count
--;
507 int follow_up(struct vfsmount
**mnt
, struct dentry
**dentry
)
509 struct vfsmount
*parent
;
510 struct dentry
*mountpoint
;
511 spin_lock(&vfsmount_lock
);
512 parent
=(*mnt
)->mnt_parent
;
513 if (parent
== *mnt
) {
514 spin_unlock(&vfsmount_lock
);
518 mountpoint
=dget((*mnt
)->mnt_mountpoint
);
519 spin_unlock(&vfsmount_lock
);
521 *dentry
= mountpoint
;
527 /* no need for dcache_lock, as serialization is taken care in
530 static int follow_mount(struct vfsmount
**mnt
, struct dentry
**dentry
)
533 while (d_mountpoint(*dentry
)) {
534 struct vfsmount
*mounted
= lookup_mnt(*mnt
, *dentry
);
540 *dentry
= dget(mounted
->mnt_root
);
546 /* no need for dcache_lock, as serialization is taken care in
549 static inline int __follow_down(struct vfsmount
**mnt
, struct dentry
**dentry
)
551 struct vfsmount
*mounted
;
553 mounted
= lookup_mnt(*mnt
, *dentry
);
558 *dentry
= dget(mounted
->mnt_root
);
564 int follow_down(struct vfsmount
**mnt
, struct dentry
**dentry
)
566 return __follow_down(mnt
,dentry
);
569 static inline void follow_dotdot(struct vfsmount
**mnt
, struct dentry
**dentry
)
572 struct vfsmount
*parent
;
573 struct dentry
*old
= *dentry
;
575 read_lock(¤t
->fs
->lock
);
576 if (*dentry
== current
->fs
->root
&&
577 *mnt
== current
->fs
->rootmnt
) {
578 read_unlock(¤t
->fs
->lock
);
581 read_unlock(¤t
->fs
->lock
);
582 spin_lock(&dcache_lock
);
583 if (*dentry
!= (*mnt
)->mnt_root
) {
584 *dentry
= dget((*dentry
)->d_parent
);
585 spin_unlock(&dcache_lock
);
589 spin_unlock(&dcache_lock
);
590 spin_lock(&vfsmount_lock
);
591 parent
= (*mnt
)->mnt_parent
;
592 if (parent
== *mnt
) {
593 spin_unlock(&vfsmount_lock
);
597 *dentry
= dget((*mnt
)->mnt_mountpoint
);
598 spin_unlock(&vfsmount_lock
);
603 follow_mount(mnt
, dentry
);
607 struct vfsmount
*mnt
;
608 struct dentry
*dentry
;
612 * It's more convoluted than I'd like it to be, but... it's still fairly
613 * small and for now I'd prefer to have fast path as straight as possible.
614 * It _is_ time-critical.
616 static int do_lookup(struct nameidata
*nd
, struct qstr
*name
,
619 struct vfsmount
*mnt
= nd
->mnt
;
620 struct dentry
*dentry
= __d_lookup(nd
->dentry
, name
);
624 if (dentry
->d_op
&& dentry
->d_op
->d_revalidate
)
625 goto need_revalidate
;
628 path
->dentry
= dentry
;
632 dentry
= real_lookup(nd
->dentry
, name
, nd
);
638 if (dentry
->d_op
->d_revalidate(dentry
, nd
))
640 if (d_invalidate(dentry
))
646 return PTR_ERR(dentry
);
652 * This is the basic name resolution function, turning a pathname
653 * into the final dentry.
655 * We expect 'base' to be positive and a directory.
657 int fastcall
link_path_walk(const char * name
, struct nameidata
*nd
)
662 unsigned int lookup_flags
= nd
->flags
;
669 inode
= nd
->dentry
->d_inode
;
671 lookup_flags
= LOOKUP_FOLLOW
;
673 /* At this point we know we have a real path component. */
679 err
= exec_permission_lite(inode
, nd
);
680 if (err
== -EAGAIN
) {
681 err
= permission(inode
, MAY_EXEC
, nd
);
687 c
= *(const unsigned char *)name
;
689 hash
= init_name_hash();
692 hash
= partial_name_hash(c
, hash
);
693 c
= *(const unsigned char *)name
;
694 } while (c
&& (c
!= '/'));
695 this.len
= name
- (const char *) this.name
;
696 this.hash
= end_name_hash(hash
);
698 /* remove trailing slashes? */
701 while (*++name
== '/');
703 goto last_with_slashes
;
706 * "." and ".." are special - ".." especially so because it has
707 * to be able to know about the current root directory and
708 * parent relationships.
710 if (this.name
[0] == '.') switch (this.len
) {
714 if (this.name
[1] != '.')
716 follow_dotdot(&nd
->mnt
, &nd
->dentry
);
717 inode
= nd
->dentry
->d_inode
;
723 * See if the low-level filesystem might want
724 * to use its own hash..
726 if (nd
->dentry
->d_op
&& nd
->dentry
->d_op
->d_hash
) {
727 err
= nd
->dentry
->d_op
->d_hash(nd
->dentry
, &this);
731 nd
->flags
|= LOOKUP_CONTINUE
;
732 /* This does the actual lookups.. */
733 err
= do_lookup(nd
, &this, &next
);
736 /* Check mountpoints.. */
737 follow_mount(&next
.mnt
, &next
.dentry
);
740 inode
= next
.dentry
->d_inode
;
747 if (inode
->i_op
->follow_link
) {
749 err
= do_follow_link(next
.dentry
, nd
);
755 inode
= nd
->dentry
->d_inode
;
764 nd
->dentry
= next
.dentry
;
767 if (!inode
->i_op
->lookup
)
770 /* here ends the main loop */
773 lookup_flags
|= LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
;
775 nd
->flags
&= ~LOOKUP_CONTINUE
;
776 if (lookup_flags
& LOOKUP_PARENT
)
778 if (this.name
[0] == '.') switch (this.len
) {
782 if (this.name
[1] != '.')
784 follow_dotdot(&nd
->mnt
, &nd
->dentry
);
785 inode
= nd
->dentry
->d_inode
;
790 if (nd
->dentry
->d_op
&& nd
->dentry
->d_op
->d_hash
) {
791 err
= nd
->dentry
->d_op
->d_hash(nd
->dentry
, &this);
795 err
= do_lookup(nd
, &this, &next
);
798 follow_mount(&next
.mnt
, &next
.dentry
);
799 inode
= next
.dentry
->d_inode
;
800 if ((lookup_flags
& LOOKUP_FOLLOW
)
801 && inode
&& inode
->i_op
&& inode
->i_op
->follow_link
) {
803 err
= do_follow_link(next
.dentry
, nd
);
808 inode
= nd
->dentry
->d_inode
;
812 nd
->dentry
= next
.dentry
;
817 if (lookup_flags
& LOOKUP_DIRECTORY
) {
819 if (!inode
->i_op
|| !inode
->i_op
->lookup
)
825 nd
->last_type
= LAST_NORM
;
826 if (this.name
[0] != '.')
829 nd
->last_type
= LAST_DOT
;
830 else if (this.len
== 2 && this.name
[1] == '.')
831 nd
->last_type
= LAST_DOTDOT
;
836 * We bypassed the ordinary revalidation routines.
837 * We may need to check the cached dentry for staleness.
839 if (nd
->dentry
&& nd
->dentry
->d_sb
&&
840 (nd
->dentry
->d_sb
->s_type
->fs_flags
& FS_REVAL_DOT
)) {
842 /* Note: we do not d_invalidate() */
843 if (!nd
->dentry
->d_op
->d_revalidate(nd
->dentry
, nd
))
857 int fastcall
path_walk(const char * name
, struct nameidata
*nd
)
859 current
->total_link_count
= 0;
860 return link_path_walk(name
, nd
);
864 /* returns 1 if everything is done */
865 static int __emul_lookup_dentry(const char *name
, struct nameidata
*nd
)
867 if (path_walk(name
, nd
))
868 return 0; /* something went wrong... */
870 if (!nd
->dentry
->d_inode
|| S_ISDIR(nd
->dentry
->d_inode
->i_mode
)) {
871 struct dentry
*old_dentry
= nd
->dentry
;
872 struct vfsmount
*old_mnt
= nd
->mnt
;
873 struct qstr last
= nd
->last
;
874 int last_type
= nd
->last_type
;
876 * NAME was not found in alternate root or it's a directory. Try to find
877 * it in the normal root:
879 nd
->last_type
= LAST_ROOT
;
880 read_lock(¤t
->fs
->lock
);
881 nd
->mnt
= mntget(current
->fs
->rootmnt
);
882 nd
->dentry
= dget(current
->fs
->root
);
883 read_unlock(¤t
->fs
->lock
);
884 if (path_walk(name
, nd
) == 0) {
885 if (nd
->dentry
->d_inode
) {
892 nd
->dentry
= old_dentry
;
895 nd
->last_type
= last_type
;
900 void set_fs_altroot(void)
902 char *emul
= __emul_prefix();
904 struct vfsmount
*mnt
= NULL
, *oldmnt
;
905 struct dentry
*dentry
= NULL
, *olddentry
;
910 err
= path_lookup(emul
, LOOKUP_FOLLOW
|LOOKUP_DIRECTORY
|LOOKUP_NOALT
, &nd
);
916 write_lock(¤t
->fs
->lock
);
917 oldmnt
= current
->fs
->altrootmnt
;
918 olddentry
= current
->fs
->altroot
;
919 current
->fs
->altrootmnt
= mnt
;
920 current
->fs
->altroot
= dentry
;
921 write_unlock(¤t
->fs
->lock
);
928 int fastcall
path_lookup(const char *name
, unsigned int flags
, struct nameidata
*nd
)
932 nd
->last_type
= LAST_ROOT
; /* if there are only slashes... */
936 read_lock(¤t
->fs
->lock
);
938 if (current
->fs
->altroot
&& !(nd
->flags
& LOOKUP_NOALT
)) {
939 nd
->mnt
= mntget(current
->fs
->altrootmnt
);
940 nd
->dentry
= dget(current
->fs
->altroot
);
941 read_unlock(¤t
->fs
->lock
);
942 if (__emul_lookup_dentry(name
,nd
))
944 read_lock(¤t
->fs
->lock
);
946 nd
->mnt
= mntget(current
->fs
->rootmnt
);
947 nd
->dentry
= dget(current
->fs
->root
);
949 nd
->mnt
= mntget(current
->fs
->pwdmnt
);
950 nd
->dentry
= dget(current
->fs
->pwd
);
952 read_unlock(¤t
->fs
->lock
);
953 current
->total_link_count
= 0;
954 retval
= link_path_walk(name
, nd
);
955 if (unlikely(current
->audit_context
956 && nd
&& nd
->dentry
&& nd
->dentry
->d_inode
))
958 nd
->dentry
->d_inode
->i_ino
,
959 nd
->dentry
->d_inode
->i_rdev
);
964 * Restricted form of lookup. Doesn't follow links, single-component only,
965 * needs parent already locked. Doesn't follow mounts.
968 static struct dentry
* __lookup_hash(struct qstr
*name
, struct dentry
* base
, struct nameidata
*nd
)
970 struct dentry
* dentry
;
974 inode
= base
->d_inode
;
975 err
= permission(inode
, MAY_EXEC
, nd
);
976 dentry
= ERR_PTR(err
);
981 * See if the low-level filesystem might want
982 * to use its own hash..
984 if (base
->d_op
&& base
->d_op
->d_hash
) {
985 err
= base
->d_op
->d_hash(base
, name
);
986 dentry
= ERR_PTR(err
);
991 dentry
= cached_lookup(base
, name
, nd
);
993 struct dentry
*new = d_alloc(base
, name
);
994 dentry
= ERR_PTR(-ENOMEM
);
997 dentry
= inode
->i_op
->lookup(inode
, new, nd
);
1007 struct dentry
* lookup_hash(struct qstr
*name
, struct dentry
* base
)
1009 return __lookup_hash(name
, base
, NULL
);
1013 struct dentry
* lookup_one_len(const char * name
, struct dentry
* base
, int len
)
1024 hash
= init_name_hash();
1026 c
= *(const unsigned char *)name
++;
1027 if (c
== '/' || c
== '\0')
1029 hash
= partial_name_hash(c
, hash
);
1031 this.hash
= end_name_hash(hash
);
1033 return lookup_hash(&this, base
);
1035 return ERR_PTR(-EACCES
);
1041 * is used by most simple commands to get the inode of a specified name.
1042 * Open, link etc use their own routines, but this is enough for things
1045 * namei exists in two versions: namei/lnamei. The only difference is
1046 * that namei follows links, while lnamei does not.
1049 int fastcall
__user_walk(const char __user
*name
, unsigned flags
, struct nameidata
*nd
)
1051 char *tmp
= getname(name
);
1052 int err
= PTR_ERR(tmp
);
1055 err
= path_lookup(tmp
, flags
, nd
);
1062 * It's inline, so penalty for filesystems that don't use sticky bit is
1065 static inline int check_sticky(struct inode
*dir
, struct inode
*inode
)
1067 if (!(dir
->i_mode
& S_ISVTX
))
1069 if (inode
->i_uid
== current
->fsuid
)
1071 if (dir
->i_uid
== current
->fsuid
)
1073 return !capable(CAP_FOWNER
);
1077 * Check whether we can remove a link victim from directory dir, check
1078 * whether the type of victim is right.
1079 * 1. We can't do it if dir is read-only (done in permission())
1080 * 2. We should have write and exec permissions on dir
1081 * 3. We can't remove anything from append-only dir
1082 * 4. We can't do anything with immutable dir (done in permission())
1083 * 5. If the sticky bit on dir is set we should either
1084 * a. be owner of dir, or
1085 * b. be owner of victim, or
1086 * c. have CAP_FOWNER capability
1087 * 6. If the victim is append-only or immutable we can't do antyhing with
1088 * links pointing to it.
1089 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1090 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1091 * 9. We can't remove a root or mountpoint.
1092 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1093 * nfs_async_unlink().
1095 static inline int may_delete(struct inode
*dir
,struct dentry
*victim
,int isdir
)
1099 if (!victim
->d_inode
)
1102 BUG_ON(victim
->d_parent
->d_inode
!= dir
);
1104 error
= permission(dir
,MAY_WRITE
| MAY_EXEC
, NULL
);
1109 if (check_sticky(dir
, victim
->d_inode
)||IS_APPEND(victim
->d_inode
)||
1110 IS_IMMUTABLE(victim
->d_inode
))
1113 if (!S_ISDIR(victim
->d_inode
->i_mode
))
1115 if (IS_ROOT(victim
))
1117 } else if (S_ISDIR(victim
->d_inode
->i_mode
))
1119 if (IS_DEADDIR(dir
))
1121 if (victim
->d_flags
& DCACHE_NFSFS_RENAMED
)
1126 /* Check whether we can create an object with dentry child in directory
1128 * 1. We can't do it if child already exists (open has special treatment for
1129 * this case, but since we are inlined it's OK)
1130 * 2. We can't do it if dir is read-only (done in permission())
1131 * 3. We should have write and exec permissions on dir
1132 * 4. We can't do it if dir is immutable (done in permission())
1134 static inline int may_create(struct inode
*dir
, struct dentry
*child
,
1135 struct nameidata
*nd
)
1139 if (IS_DEADDIR(dir
))
1141 return permission(dir
,MAY_WRITE
| MAY_EXEC
, nd
);
1145 * Special case: O_CREAT|O_EXCL implies O_NOFOLLOW for security
1148 * O_DIRECTORY translates into forcing a directory lookup.
1150 static inline int lookup_flags(unsigned int f
)
1152 unsigned long retval
= LOOKUP_FOLLOW
;
1155 retval
&= ~LOOKUP_FOLLOW
;
1157 if ((f
& (O_CREAT
|O_EXCL
)) == (O_CREAT
|O_EXCL
))
1158 retval
&= ~LOOKUP_FOLLOW
;
1160 if (f
& O_DIRECTORY
)
1161 retval
|= LOOKUP_DIRECTORY
;
1167 * p1 and p2 should be directories on the same fs.
1169 struct dentry
*lock_rename(struct dentry
*p1
, struct dentry
*p2
)
1174 down(&p1
->d_inode
->i_sem
);
1178 down(&p1
->d_inode
->i_sb
->s_vfs_rename_sem
);
1180 for (p
= p1
; p
->d_parent
!= p
; p
= p
->d_parent
) {
1181 if (p
->d_parent
== p2
) {
1182 down(&p2
->d_inode
->i_sem
);
1183 down(&p1
->d_inode
->i_sem
);
1188 for (p
= p2
; p
->d_parent
!= p
; p
= p
->d_parent
) {
1189 if (p
->d_parent
== p1
) {
1190 down(&p1
->d_inode
->i_sem
);
1191 down(&p2
->d_inode
->i_sem
);
1196 down(&p1
->d_inode
->i_sem
);
1197 down(&p2
->d_inode
->i_sem
);
1201 void unlock_rename(struct dentry
*p1
, struct dentry
*p2
)
1203 up(&p1
->d_inode
->i_sem
);
1205 up(&p2
->d_inode
->i_sem
);
1206 up(&p1
->d_inode
->i_sb
->s_vfs_rename_sem
);
1210 int vfs_create(struct inode
*dir
, struct dentry
*dentry
, int mode
,
1211 struct nameidata
*nd
)
1213 int error
= may_create(dir
, dentry
, nd
);
1218 if (!dir
->i_op
|| !dir
->i_op
->create
)
1219 return -EACCES
; /* shouldn't it be ENOSYS? */
1222 error
= security_inode_create(dir
, dentry
, mode
);
1226 error
= dir
->i_op
->create(dir
, dentry
, mode
, nd
);
1228 inode_dir_notify(dir
, DN_CREATE
);
1229 security_inode_post_create(dir
, dentry
, mode
);
1234 int may_open(struct nameidata
*nd
, int acc_mode
, int flag
)
1236 struct dentry
*dentry
= nd
->dentry
;
1237 struct inode
*inode
= dentry
->d_inode
;
1243 if (S_ISLNK(inode
->i_mode
))
1246 if (S_ISDIR(inode
->i_mode
) && (flag
& FMODE_WRITE
))
1249 error
= permission(inode
, acc_mode
, nd
);
1254 * FIFO's, sockets and device files are special: they don't
1255 * actually live on the filesystem itself, and as such you
1256 * can write to them even if the filesystem is read-only.
1258 if (S_ISFIFO(inode
->i_mode
) || S_ISSOCK(inode
->i_mode
)) {
1260 } else if (S_ISBLK(inode
->i_mode
) || S_ISCHR(inode
->i_mode
)) {
1261 if (nd
->mnt
->mnt_flags
& MNT_NODEV
)
1265 } else if (IS_RDONLY(inode
) && (flag
& FMODE_WRITE
))
1268 * An append-only file must be opened in append mode for writing.
1270 if (IS_APPEND(inode
)) {
1271 if ((flag
& FMODE_WRITE
) && !(flag
& O_APPEND
))
1277 /* O_NOATIME can only be set by the owner or superuser */
1278 if (flag
& O_NOATIME
)
1279 if (current
->fsuid
!= inode
->i_uid
&& !capable(CAP_FOWNER
))
1283 * Ensure there are no outstanding leases on the file.
1285 error
= break_lease(inode
, flag
);
1289 if (flag
& O_TRUNC
) {
1290 error
= get_write_access(inode
);
1295 * Refuse to truncate files with mandatory locks held on them.
1297 error
= locks_verify_locked(inode
);
1301 error
= do_truncate(dentry
, 0);
1303 put_write_access(inode
);
1307 if (flag
& FMODE_WRITE
)
1316 * namei for open - this is in fact almost the whole open-routine.
1318 * Note that the low bits of "flag" aren't the same as in the open
1319 * system call - they are 00 - no permissions needed
1320 * 01 - read permission needed
1321 * 10 - write permission needed
1322 * 11 - read/write permissions needed
1323 * which is a lot more logical, and also allows the "no perm" needed
1324 * for symlinks (where the permissions are checked later).
1327 int open_namei(const char * pathname
, int flag
, int mode
, struct nameidata
*nd
)
1329 int acc_mode
, error
= 0;
1330 struct dentry
*dentry
;
1334 acc_mode
= ACC_MODE(flag
);
1336 /* Allow the LSM permission hook to distinguish append
1337 access from general write access. */
1338 if (flag
& O_APPEND
)
1339 acc_mode
|= MAY_APPEND
;
1341 /* Fill in the open() intent data */
1342 nd
->intent
.open
.flags
= flag
;
1343 nd
->intent
.open
.create_mode
= mode
;
1346 * The simplest case - just a plain lookup.
1348 if (!(flag
& O_CREAT
)) {
1349 error
= path_lookup(pathname
, lookup_flags(flag
)|LOOKUP_OPEN
, nd
);
1356 * Create - we need to know the parent.
1358 error
= path_lookup(pathname
, LOOKUP_PARENT
|LOOKUP_OPEN
|LOOKUP_CREATE
, nd
);
1363 * We have the parent and last component. First of all, check
1364 * that we are not asked to creat(2) an obvious directory - that
1368 if (nd
->last_type
!= LAST_NORM
|| nd
->last
.name
[nd
->last
.len
])
1372 nd
->flags
&= ~LOOKUP_PARENT
;
1373 down(&dir
->d_inode
->i_sem
);
1374 dentry
= __lookup_hash(&nd
->last
, nd
->dentry
, nd
);
1377 error
= PTR_ERR(dentry
);
1378 if (IS_ERR(dentry
)) {
1379 up(&dir
->d_inode
->i_sem
);
1383 /* Negative dentry, just create the file */
1384 if (!dentry
->d_inode
) {
1385 if (!IS_POSIXACL(dir
->d_inode
))
1386 mode
&= ~current
->fs
->umask
;
1387 error
= vfs_create(dir
->d_inode
, dentry
, mode
, nd
);
1388 up(&dir
->d_inode
->i_sem
);
1390 nd
->dentry
= dentry
;
1393 /* Don't check for write permission, don't truncate */
1400 * It already exists.
1402 up(&dir
->d_inode
->i_sem
);
1408 if (d_mountpoint(dentry
)) {
1410 if (flag
& O_NOFOLLOW
)
1412 while (__follow_down(&nd
->mnt
,&dentry
) && d_mountpoint(dentry
));
1415 if (!dentry
->d_inode
)
1417 if (dentry
->d_inode
->i_op
&& dentry
->d_inode
->i_op
->follow_link
)
1421 nd
->dentry
= dentry
;
1423 if (dentry
->d_inode
&& S_ISDIR(dentry
->d_inode
->i_mode
))
1426 error
= may_open(nd
, acc_mode
, flag
);
1439 if (flag
& O_NOFOLLOW
)
1442 * This is subtle. Instead of calling do_follow_link() we do the
1443 * thing by hands. The reason is that this way we have zero link_count
1444 * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1445 * After that we have the parent and last component, i.e.
1446 * we are in the same situation as after the first path_walk().
1447 * Well, almost - if the last component is normal we get its copy
1448 * stored in nd->last.name and we will have to putname() it when we
1449 * are done. Procfs-like symlinks just set LAST_BIND.
1451 nd
->flags
|= LOOKUP_PARENT
;
1452 error
= security_inode_follow_link(dentry
, nd
);
1455 touch_atime(nd
->mnt
, dentry
);
1456 nd_set_link(nd
, NULL
);
1457 error
= dentry
->d_inode
->i_op
->follow_link(dentry
, nd
);
1459 char *s
= nd_get_link(nd
);
1461 error
= __vfs_follow_link(nd
, s
);
1462 if (dentry
->d_inode
->i_op
->put_link
)
1463 dentry
->d_inode
->i_op
->put_link(dentry
, nd
);
1468 nd
->flags
&= ~LOOKUP_PARENT
;
1469 if (nd
->last_type
== LAST_BIND
) {
1470 dentry
= nd
->dentry
;
1474 if (nd
->last_type
!= LAST_NORM
)
1476 if (nd
->last
.name
[nd
->last
.len
]) {
1477 putname(nd
->last
.name
);
1482 putname(nd
->last
.name
);
1486 down(&dir
->d_inode
->i_sem
);
1487 dentry
= __lookup_hash(&nd
->last
, nd
->dentry
, nd
);
1488 putname(nd
->last
.name
);
1493 * lookup_create - lookup a dentry, creating it if it doesn't exist
1494 * @nd: nameidata info
1495 * @is_dir: directory flag
1497 * Simple function to lookup and return a dentry and create it
1498 * if it doesn't exist. Is SMP-safe.
1500 struct dentry
*lookup_create(struct nameidata
*nd
, int is_dir
)
1502 struct dentry
*dentry
;
1504 down(&nd
->dentry
->d_inode
->i_sem
);
1505 dentry
= ERR_PTR(-EEXIST
);
1506 if (nd
->last_type
!= LAST_NORM
)
1508 nd
->flags
&= ~LOOKUP_PARENT
;
1509 dentry
= lookup_hash(&nd
->last
, nd
->dentry
);
1512 if (!is_dir
&& nd
->last
.name
[nd
->last
.len
] && !dentry
->d_inode
)
1517 dentry
= ERR_PTR(-ENOENT
);
1522 int vfs_mknod(struct inode
*dir
, struct dentry
*dentry
, int mode
, dev_t dev
)
1524 int error
= may_create(dir
, dentry
, NULL
);
1529 if ((S_ISCHR(mode
) || S_ISBLK(mode
)) && !capable(CAP_MKNOD
))
1532 if (!dir
->i_op
|| !dir
->i_op
->mknod
)
1535 error
= security_inode_mknod(dir
, dentry
, mode
, dev
);
1540 error
= dir
->i_op
->mknod(dir
, dentry
, mode
, dev
);
1542 inode_dir_notify(dir
, DN_CREATE
);
1543 security_inode_post_mknod(dir
, dentry
, mode
, dev
);
1548 asmlinkage
long sys_mknod(const char __user
* filename
, int mode
, unsigned dev
)
1552 struct dentry
* dentry
;
1553 struct nameidata nd
;
1557 tmp
= getname(filename
);
1559 return PTR_ERR(tmp
);
1561 error
= path_lookup(tmp
, LOOKUP_PARENT
, &nd
);
1564 dentry
= lookup_create(&nd
, 0);
1565 error
= PTR_ERR(dentry
);
1567 if (!IS_POSIXACL(nd
.dentry
->d_inode
))
1568 mode
&= ~current
->fs
->umask
;
1569 if (!IS_ERR(dentry
)) {
1570 switch (mode
& S_IFMT
) {
1571 case 0: case S_IFREG
:
1572 error
= vfs_create(nd
.dentry
->d_inode
,dentry
,mode
,&nd
);
1574 case S_IFCHR
: case S_IFBLK
:
1575 error
= vfs_mknod(nd
.dentry
->d_inode
,dentry
,mode
,
1576 new_decode_dev(dev
));
1578 case S_IFIFO
: case S_IFSOCK
:
1579 error
= vfs_mknod(nd
.dentry
->d_inode
,dentry
,mode
,0);
1589 up(&nd
.dentry
->d_inode
->i_sem
);
1597 int vfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
1599 int error
= may_create(dir
, dentry
, NULL
);
1604 if (!dir
->i_op
|| !dir
->i_op
->mkdir
)
1607 mode
&= (S_IRWXUGO
|S_ISVTX
);
1608 error
= security_inode_mkdir(dir
, dentry
, mode
);
1613 error
= dir
->i_op
->mkdir(dir
, dentry
, mode
);
1615 inode_dir_notify(dir
, DN_CREATE
);
1616 security_inode_post_mkdir(dir
,dentry
, mode
);
1621 asmlinkage
long sys_mkdir(const char __user
* pathname
, int mode
)
1626 tmp
= getname(pathname
);
1627 error
= PTR_ERR(tmp
);
1629 struct dentry
*dentry
;
1630 struct nameidata nd
;
1632 error
= path_lookup(tmp
, LOOKUP_PARENT
, &nd
);
1635 dentry
= lookup_create(&nd
, 1);
1636 error
= PTR_ERR(dentry
);
1637 if (!IS_ERR(dentry
)) {
1638 if (!IS_POSIXACL(nd
.dentry
->d_inode
))
1639 mode
&= ~current
->fs
->umask
;
1640 error
= vfs_mkdir(nd
.dentry
->d_inode
, dentry
, mode
);
1643 up(&nd
.dentry
->d_inode
->i_sem
);
1653 * We try to drop the dentry early: we should have
1654 * a usage count of 2 if we're the only user of this
1655 * dentry, and if that is true (possibly after pruning
1656 * the dcache), then we drop the dentry now.
1658 * A low-level filesystem can, if it choses, legally
1661 * if (!d_unhashed(dentry))
1664 * if it cannot handle the case of removing a directory
1665 * that is still in use by something else..
1667 void dentry_unhash(struct dentry
*dentry
)
1670 spin_lock(&dcache_lock
);
1671 switch (atomic_read(&dentry
->d_count
)) {
1673 spin_unlock(&dcache_lock
);
1674 shrink_dcache_parent(dentry
);
1675 spin_lock(&dcache_lock
);
1676 if (atomic_read(&dentry
->d_count
) != 2)
1681 spin_unlock(&dcache_lock
);
1684 int vfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
1686 int error
= may_delete(dir
, dentry
, 1);
1691 if (!dir
->i_op
|| !dir
->i_op
->rmdir
)
1696 down(&dentry
->d_inode
->i_sem
);
1697 dentry_unhash(dentry
);
1698 if (d_mountpoint(dentry
))
1701 error
= security_inode_rmdir(dir
, dentry
);
1703 error
= dir
->i_op
->rmdir(dir
, dentry
);
1705 dentry
->d_inode
->i_flags
|= S_DEAD
;
1708 up(&dentry
->d_inode
->i_sem
);
1710 inode_dir_notify(dir
, DN_DELETE
);
1718 asmlinkage
long sys_rmdir(const char __user
* pathname
)
1722 struct dentry
*dentry
;
1723 struct nameidata nd
;
1725 name
= getname(pathname
);
1727 return PTR_ERR(name
);
1729 error
= path_lookup(name
, LOOKUP_PARENT
, &nd
);
1733 switch(nd
.last_type
) {
1744 down(&nd
.dentry
->d_inode
->i_sem
);
1745 dentry
= lookup_hash(&nd
.last
, nd
.dentry
);
1746 error
= PTR_ERR(dentry
);
1747 if (!IS_ERR(dentry
)) {
1748 error
= vfs_rmdir(nd
.dentry
->d_inode
, dentry
);
1751 up(&nd
.dentry
->d_inode
->i_sem
);
1759 int vfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
1761 int error
= may_delete(dir
, dentry
, 0);
1766 if (!dir
->i_op
|| !dir
->i_op
->unlink
)
1771 down(&dentry
->d_inode
->i_sem
);
1772 if (d_mountpoint(dentry
))
1775 error
= security_inode_unlink(dir
, dentry
);
1777 error
= dir
->i_op
->unlink(dir
, dentry
);
1779 up(&dentry
->d_inode
->i_sem
);
1781 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
1782 if (!error
&& !(dentry
->d_flags
& DCACHE_NFSFS_RENAMED
)) {
1784 inode_dir_notify(dir
, DN_DELETE
);
1790 * Make sure that the actual truncation of the file will occur outside its
1791 * directory's i_sem. Truncate can take a long time if there is a lot of
1792 * writeout happening, and we don't want to prevent access to the directory
1793 * while waiting on the I/O.
1795 asmlinkage
long sys_unlink(const char __user
* pathname
)
1799 struct dentry
*dentry
;
1800 struct nameidata nd
;
1801 struct inode
*inode
= NULL
;
1803 name
= getname(pathname
);
1805 return PTR_ERR(name
);
1807 error
= path_lookup(name
, LOOKUP_PARENT
, &nd
);
1811 if (nd
.last_type
!= LAST_NORM
)
1813 down(&nd
.dentry
->d_inode
->i_sem
);
1814 dentry
= lookup_hash(&nd
.last
, nd
.dentry
);
1815 error
= PTR_ERR(dentry
);
1816 if (!IS_ERR(dentry
)) {
1817 /* Why not before? Because we want correct error value */
1818 if (nd
.last
.name
[nd
.last
.len
])
1820 inode
= dentry
->d_inode
;
1822 atomic_inc(&inode
->i_count
);
1823 error
= vfs_unlink(nd
.dentry
->d_inode
, dentry
);
1827 up(&nd
.dentry
->d_inode
->i_sem
);
1829 iput(inode
); /* truncate the inode here */
1837 error
= !dentry
->d_inode
? -ENOENT
:
1838 S_ISDIR(dentry
->d_inode
->i_mode
) ? -EISDIR
: -ENOTDIR
;
1842 int vfs_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *oldname
, int mode
)
1844 int error
= may_create(dir
, dentry
, NULL
);
1849 if (!dir
->i_op
|| !dir
->i_op
->symlink
)
1852 error
= security_inode_symlink(dir
, dentry
, oldname
);
1857 error
= dir
->i_op
->symlink(dir
, dentry
, oldname
);
1859 inode_dir_notify(dir
, DN_CREATE
);
1860 security_inode_post_symlink(dir
, dentry
, oldname
);
1865 asmlinkage
long sys_symlink(const char __user
* oldname
, const char __user
* newname
)
1871 from
= getname(oldname
);
1873 return PTR_ERR(from
);
1874 to
= getname(newname
);
1875 error
= PTR_ERR(to
);
1877 struct dentry
*dentry
;
1878 struct nameidata nd
;
1880 error
= path_lookup(to
, LOOKUP_PARENT
, &nd
);
1883 dentry
= lookup_create(&nd
, 0);
1884 error
= PTR_ERR(dentry
);
1885 if (!IS_ERR(dentry
)) {
1886 error
= vfs_symlink(nd
.dentry
->d_inode
, dentry
, from
, S_IALLUGO
);
1889 up(&nd
.dentry
->d_inode
->i_sem
);
1898 int vfs_link(struct dentry
*old_dentry
, struct inode
*dir
, struct dentry
*new_dentry
)
1900 struct inode
*inode
= old_dentry
->d_inode
;
1906 error
= may_create(dir
, new_dentry
, NULL
);
1910 if (dir
->i_sb
!= inode
->i_sb
)
1914 * A link to an append-only or immutable file cannot be created.
1916 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
1918 if (!dir
->i_op
|| !dir
->i_op
->link
)
1920 if (S_ISDIR(old_dentry
->d_inode
->i_mode
))
1923 error
= security_inode_link(old_dentry
, dir
, new_dentry
);
1927 down(&old_dentry
->d_inode
->i_sem
);
1929 error
= dir
->i_op
->link(old_dentry
, dir
, new_dentry
);
1930 up(&old_dentry
->d_inode
->i_sem
);
1932 inode_dir_notify(dir
, DN_CREATE
);
1933 security_inode_post_link(old_dentry
, dir
, new_dentry
);
1939 * Hardlinks are often used in delicate situations. We avoid
1940 * security-related surprises by not following symlinks on the
1943 * We don't follow them on the oldname either to be compatible
1944 * with linux 2.0, and to avoid hard-linking to directories
1945 * and other special files. --ADM
1947 asmlinkage
long sys_link(const char __user
* oldname
, const char __user
* newname
)
1949 struct dentry
*new_dentry
;
1950 struct nameidata nd
, old_nd
;
1954 to
= getname(newname
);
1958 error
= __user_walk(oldname
, 0, &old_nd
);
1961 error
= path_lookup(to
, LOOKUP_PARENT
, &nd
);
1965 if (old_nd
.mnt
!= nd
.mnt
)
1967 new_dentry
= lookup_create(&nd
, 0);
1968 error
= PTR_ERR(new_dentry
);
1969 if (!IS_ERR(new_dentry
)) {
1970 error
= vfs_link(old_nd
.dentry
, nd
.dentry
->d_inode
, new_dentry
);
1973 up(&nd
.dentry
->d_inode
->i_sem
);
1977 path_release(&old_nd
);
1985 * The worst of all namespace operations - renaming directory. "Perverted"
1986 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
1988 * a) we can get into loop creation. Check is done in is_subdir().
1989 * b) race potential - two innocent renames can create a loop together.
1990 * That's where 4.4 screws up. Current fix: serialization on
1991 * sb->s_vfs_rename_sem. We might be more accurate, but that's another
1993 * c) we have to lock _three_ objects - parents and victim (if it exists).
1994 * And that - after we got ->i_sem on parents (until then we don't know
1995 * whether the target exists). Solution: try to be smart with locking
1996 * order for inodes. We rely on the fact that tree topology may change
1997 * only under ->s_vfs_rename_sem _and_ that parent of the object we
1998 * move will be locked. Thus we can rank directories by the tree
1999 * (ancestors first) and rank all non-directories after them.
2000 * That works since everybody except rename does "lock parent, lookup,
2001 * lock child" and rename is under ->s_vfs_rename_sem.
2002 * HOWEVER, it relies on the assumption that any object with ->lookup()
2003 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2004 * we'd better make sure that there's no link(2) for them.
2005 * d) some filesystems don't support opened-but-unlinked directories,
2006 * either because of layout or because they are not ready to deal with
2007 * all cases correctly. The latter will be fixed (taking this sort of
2008 * stuff into VFS), but the former is not going away. Solution: the same
2009 * trick as in rmdir().
2010 * e) conversion from fhandle to dentry may come in the wrong moment - when
2011 * we are removing the target. Solution: we will have to grab ->i_sem
2012 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
2013 * ->i_sem on parents, which works but leads to some truely excessive
2016 int vfs_rename_dir(struct inode
*old_dir
, struct dentry
*old_dentry
,
2017 struct inode
*new_dir
, struct dentry
*new_dentry
)
2020 struct inode
*target
;
2023 * If we are going to change the parent - check write permissions,
2024 * we'll need to flip '..'.
2026 if (new_dir
!= old_dir
) {
2027 error
= permission(old_dentry
->d_inode
, MAY_WRITE
, NULL
);
2032 error
= security_inode_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
2036 target
= new_dentry
->d_inode
;
2038 down(&target
->i_sem
);
2039 dentry_unhash(new_dentry
);
2041 if (d_mountpoint(old_dentry
)||d_mountpoint(new_dentry
))
2044 error
= old_dir
->i_op
->rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
2047 target
->i_flags
|= S_DEAD
;
2049 if (d_unhashed(new_dentry
))
2050 d_rehash(new_dentry
);
2054 d_move(old_dentry
,new_dentry
);
2055 security_inode_post_rename(old_dir
, old_dentry
,
2056 new_dir
, new_dentry
);
2061 int vfs_rename_other(struct inode
*old_dir
, struct dentry
*old_dentry
,
2062 struct inode
*new_dir
, struct dentry
*new_dentry
)
2064 struct inode
*target
;
2067 error
= security_inode_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
2072 target
= new_dentry
->d_inode
;
2074 down(&target
->i_sem
);
2075 if (d_mountpoint(old_dentry
)||d_mountpoint(new_dentry
))
2078 error
= old_dir
->i_op
->rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
2080 /* The following d_move() should become unconditional */
2081 if (!(old_dir
->i_sb
->s_type
->fs_flags
& FS_ODD_RENAME
))
2082 d_move(old_dentry
, new_dentry
);
2083 security_inode_post_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
2091 int vfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
2092 struct inode
*new_dir
, struct dentry
*new_dentry
)
2095 int is_dir
= S_ISDIR(old_dentry
->d_inode
->i_mode
);
2097 if (old_dentry
->d_inode
== new_dentry
->d_inode
)
2100 error
= may_delete(old_dir
, old_dentry
, is_dir
);
2104 if (!new_dentry
->d_inode
)
2105 error
= may_create(new_dir
, new_dentry
, NULL
);
2107 error
= may_delete(new_dir
, new_dentry
, is_dir
);
2111 if (!old_dir
->i_op
|| !old_dir
->i_op
->rename
)
2114 DQUOT_INIT(old_dir
);
2115 DQUOT_INIT(new_dir
);
2118 error
= vfs_rename_dir(old_dir
,old_dentry
,new_dir
,new_dentry
);
2120 error
= vfs_rename_other(old_dir
,old_dentry
,new_dir
,new_dentry
);
2122 if (old_dir
== new_dir
)
2123 inode_dir_notify(old_dir
, DN_RENAME
);
2125 inode_dir_notify(old_dir
, DN_DELETE
);
2126 inode_dir_notify(new_dir
, DN_CREATE
);
2132 static inline int do_rename(const char * oldname
, const char * newname
)
2135 struct dentry
* old_dir
, * new_dir
;
2136 struct dentry
* old_dentry
, *new_dentry
;
2137 struct dentry
* trap
;
2138 struct nameidata oldnd
, newnd
;
2140 error
= path_lookup(oldname
, LOOKUP_PARENT
, &oldnd
);
2144 error
= path_lookup(newname
, LOOKUP_PARENT
, &newnd
);
2149 if (oldnd
.mnt
!= newnd
.mnt
)
2152 old_dir
= oldnd
.dentry
;
2154 if (oldnd
.last_type
!= LAST_NORM
)
2157 new_dir
= newnd
.dentry
;
2158 if (newnd
.last_type
!= LAST_NORM
)
2161 trap
= lock_rename(new_dir
, old_dir
);
2163 old_dentry
= lookup_hash(&oldnd
.last
, old_dir
);
2164 error
= PTR_ERR(old_dentry
);
2165 if (IS_ERR(old_dentry
))
2167 /* source must exist */
2169 if (!old_dentry
->d_inode
)
2171 /* unless the source is a directory trailing slashes give -ENOTDIR */
2172 if (!S_ISDIR(old_dentry
->d_inode
->i_mode
)) {
2174 if (oldnd
.last
.name
[oldnd
.last
.len
])
2176 if (newnd
.last
.name
[newnd
.last
.len
])
2179 /* source should not be ancestor of target */
2181 if (old_dentry
== trap
)
2183 new_dentry
= lookup_hash(&newnd
.last
, new_dir
);
2184 error
= PTR_ERR(new_dentry
);
2185 if (IS_ERR(new_dentry
))
2187 /* target should not be an ancestor of source */
2189 if (new_dentry
== trap
)
2192 error
= vfs_rename(old_dir
->d_inode
, old_dentry
,
2193 new_dir
->d_inode
, new_dentry
);
2199 unlock_rename(new_dir
, old_dir
);
2201 path_release(&newnd
);
2203 path_release(&oldnd
);
2208 asmlinkage
long sys_rename(const char __user
* oldname
, const char __user
* newname
)
2214 from
= getname(oldname
);
2216 return PTR_ERR(from
);
2217 to
= getname(newname
);
2218 error
= PTR_ERR(to
);
2220 error
= do_rename(from
,to
);
2227 int vfs_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
, const char *link
)
2231 len
= PTR_ERR(link
);
2236 if (len
> (unsigned) buflen
)
2238 if (copy_to_user(buffer
, link
, len
))
2245 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
2246 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
2247 * using) it for any given inode is up to filesystem.
2249 int generic_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
)
2251 struct nameidata nd
;
2254 res
= dentry
->d_inode
->i_op
->follow_link(dentry
, &nd
);
2256 res
= vfs_readlink(dentry
, buffer
, buflen
, nd_get_link(&nd
));
2257 if (dentry
->d_inode
->i_op
->put_link
)
2258 dentry
->d_inode
->i_op
->put_link(dentry
, &nd
);
2263 int vfs_follow_link(struct nameidata
*nd
, const char *link
)
2265 return __vfs_follow_link(nd
, link
);
2268 /* get the link contents into pagecache */
2269 static char *page_getlink(struct dentry
* dentry
, struct page
**ppage
)
2272 struct address_space
*mapping
= dentry
->d_inode
->i_mapping
;
2273 page
= read_cache_page(mapping
, 0, (filler_t
*)mapping
->a_ops
->readpage
,
2277 wait_on_page_locked(page
);
2278 if (!PageUptodate(page
))
2284 page_cache_release(page
);
2285 return ERR_PTR(-EIO
);
2291 int page_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
)
2293 struct page
*page
= NULL
;
2294 char *s
= page_getlink(dentry
, &page
);
2295 int res
= vfs_readlink(dentry
,buffer
,buflen
,s
);
2298 page_cache_release(page
);
2303 int page_follow_link_light(struct dentry
*dentry
, struct nameidata
*nd
)
2306 nd_set_link(nd
, page_getlink(dentry
, &page
));
2310 void page_put_link(struct dentry
*dentry
, struct nameidata
*nd
)
2312 if (!IS_ERR(nd_get_link(nd
))) {
2314 page
= find_get_page(dentry
->d_inode
->i_mapping
, 0);
2318 page_cache_release(page
);
2319 page_cache_release(page
);
2323 int page_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
2325 struct page
*page
= NULL
;
2326 char *s
= page_getlink(dentry
, &page
);
2327 int res
= __vfs_follow_link(nd
, s
);
2330 page_cache_release(page
);
2335 int page_symlink(struct inode
*inode
, const char *symname
, int len
)
2337 struct address_space
*mapping
= inode
->i_mapping
;
2338 struct page
*page
= grab_cache_page(mapping
, 0);
2344 err
= mapping
->a_ops
->prepare_write(NULL
, page
, 0, len
-1);
2347 kaddr
= kmap_atomic(page
, KM_USER0
);
2348 memcpy(kaddr
, symname
, len
-1);
2349 kunmap_atomic(kaddr
, KM_USER0
);
2350 mapping
->a_ops
->commit_write(NULL
, page
, 0, len
-1);
2352 * Notice that we are _not_ going to block here - end of page is
2353 * unmapped, so this will only try to map the rest of page, see
2354 * that it is unmapped (typically even will not look into inode -
2355 * ->i_size will be enough for everything) and zero it out.
2356 * OTOH it's obviously correct and should make the page up-to-date.
2358 if (!PageUptodate(page
)) {
2359 err
= mapping
->a_ops
->readpage(NULL
, page
);
2360 wait_on_page_locked(page
);
2364 page_cache_release(page
);
2367 mark_inode_dirty(inode
);
2371 page_cache_release(page
);
2376 struct inode_operations page_symlink_inode_operations
= {
2377 .readlink
= generic_readlink
,
2378 .follow_link
= page_follow_link_light
,
2379 .put_link
= page_put_link
,
2382 EXPORT_SYMBOL(__user_walk
);
2383 EXPORT_SYMBOL(follow_down
);
2384 EXPORT_SYMBOL(follow_up
);
2385 EXPORT_SYMBOL(get_write_access
); /* binfmt_aout */
2386 EXPORT_SYMBOL(getname
);
2387 EXPORT_SYMBOL(lock_rename
);
2388 EXPORT_SYMBOL(lookup_create
);
2389 EXPORT_SYMBOL(lookup_hash
);
2390 EXPORT_SYMBOL(lookup_one_len
);
2391 EXPORT_SYMBOL(page_follow_link
);
2392 EXPORT_SYMBOL(page_follow_link_light
);
2393 EXPORT_SYMBOL(page_put_link
);
2394 EXPORT_SYMBOL(page_readlink
);
2395 EXPORT_SYMBOL(page_symlink
);
2396 EXPORT_SYMBOL(page_symlink_inode_operations
);
2397 EXPORT_SYMBOL(path_lookup
);
2398 EXPORT_SYMBOL(path_release
);
2399 EXPORT_SYMBOL(path_walk
);
2400 EXPORT_SYMBOL(permission
);
2401 EXPORT_SYMBOL(unlock_rename
);
2402 EXPORT_SYMBOL(vfs_create
);
2403 EXPORT_SYMBOL(vfs_follow_link
);
2404 EXPORT_SYMBOL(vfs_link
);
2405 EXPORT_SYMBOL(vfs_mkdir
);
2406 EXPORT_SYMBOL(vfs_mknod
);
2407 EXPORT_SYMBOL(vfs_permission
);
2408 EXPORT_SYMBOL(vfs_readlink
);
2409 EXPORT_SYMBOL(vfs_rename
);
2410 EXPORT_SYMBOL(vfs_rmdir
);
2411 EXPORT_SYMBOL(vfs_symlink
);
2412 EXPORT_SYMBOL(vfs_unlink
);
2413 EXPORT_SYMBOL(dentry_unhash
);
2414 EXPORT_SYMBOL(generic_readlink
);