4 * Copyright (C) 1991, 1992 Linus Torvalds
8 * Some corrections by tytso.
11 /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
14 /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
21 #include <linux/namei.h>
22 #include <linux/quotaops.h>
23 #include <linux/pagemap.h>
24 #include <linux/dnotify.h>
25 #include <linux/smp_lock.h>
26 #include <linux/personality.h>
27 #include <linux/security.h>
28 #include <linux/mount.h>
29 #include <asm/namei.h>
30 #include <asm/uaccess.h>
32 #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
34 /* [Feb-1997 T. Schoebel-Theuer]
35 * Fundamental changes in the pathname lookup mechanisms (namei)
36 * were necessary because of omirr. The reason is that omirr needs
37 * to know the _real_ pathname, not the user-supplied one, in case
38 * of symlinks (and also when transname replacements occur).
40 * The new code replaces the old recursive symlink resolution with
41 * an iterative one (in case of non-nested symlink chains). It does
42 * this with calls to <fs>_follow_link().
43 * As a side effect, dir_namei(), _namei() and follow_link() are now
44 * replaced with a single function lookup_dentry() that can handle all
45 * the special cases of the former code.
47 * With the new dcache, the pathname is stored at each inode, at least as
48 * long as the refcount of the inode is positive. As a side effect, the
49 * size of the dcache depends on the inode cache and thus is dynamic.
51 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
52 * resolution to correspond with current state of the code.
54 * Note that the symlink resolution is not *completely* iterative.
55 * There is still a significant amount of tail- and mid- recursion in
56 * the algorithm. Also, note that <fs>_readlink() is not used in
57 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
58 * may return different results than <fs>_follow_link(). Many virtual
59 * filesystems (including /proc) exhibit this behavior.
62 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
63 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
64 * and the name already exists in form of a symlink, try to create the new
65 * name indicated by the symlink. The old code always complained that the
66 * name already exists, due to not following the symlink even if its target
67 * is nonexistent. The new semantics affects also mknod() and link() when
68 * the name is a symlink pointing to a non-existant name.
70 * I don't know which semantics is the right one, since I have no access
71 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
72 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
73 * "old" one. Personally, I think the new semantics is much more logical.
74 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
75 * file does succeed in both HP-UX and SunOs, but not in Solaris
76 * and in the old Linux semantics.
79 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
80 * semantics. See the comments in "open_namei" and "do_link" below.
82 * [10-Sep-98 Alan Modra] Another symlink change.
85 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
86 * inside the path - always follow.
87 * in the last component in creation/removal/renaming - never follow.
88 * if LOOKUP_FOLLOW passed - follow.
89 * if the pathname has trailing slashes - follow.
90 * otherwise - don't follow.
91 * (applied in that order).
93 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
94 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
95 * During the 2.4 we need to fix the userland stuff depending on it -
96 * hopefully we will be able to get rid of that wart in 2.5. So far only
97 * XEmacs seems to be relying on it...
100 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
101 * implemented. Let's see if raised priority of ->s_vfs_rename_sem gives
102 * any extra contention...
105 /* In order to reduce some races, while at the same time doing additional
106 * checking and hopefully speeding things up, we copy filenames to the
107 * kernel data space before using them..
109 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
110 * PATH_MAX includes the nul terminator --RR.
112 static inline int do_getname(const char __user
*filename
, char *page
)
115 unsigned long len
= PATH_MAX
;
117 if ((unsigned long) filename
>= TASK_SIZE
) {
118 if (!segment_eq(get_fs(), KERNEL_DS
))
120 } else if (TASK_SIZE
- (unsigned long) filename
< PATH_MAX
)
121 len
= TASK_SIZE
- (unsigned long) filename
;
123 retval
= strncpy_from_user((char *)page
, filename
, len
);
127 return -ENAMETOOLONG
;
133 char * getname(const char __user
* filename
)
137 result
= ERR_PTR(-ENOMEM
);
140 int retval
= do_getname(filename
, tmp
);
145 result
= ERR_PTR(retval
);
154 * is used to check for read/write/execute permissions on a file.
155 * We use "fsuid" for this, letting us set arbitrary permissions
156 * for filesystem access without changing the "normal" uids which
157 * are used for other things..
159 int vfs_permission(struct inode
* inode
, int mask
)
161 umode_t mode
= inode
->i_mode
;
163 if (mask
& MAY_WRITE
) {
165 * Nobody gets write access to a read-only fs.
167 if (IS_RDONLY(inode
) &&
168 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
)))
172 * Nobody gets write access to an immutable file.
174 if (IS_IMMUTABLE(inode
))
178 if (current
->fsuid
== inode
->i_uid
)
180 else if (in_group_p(inode
->i_gid
))
184 * If the DACs are ok we don't need any capability check.
186 if (((mode
& mask
& (MAY_READ
|MAY_WRITE
|MAY_EXEC
)) == mask
))
190 * Read/write DACs are always overridable.
191 * Executable DACs are overridable if at least one exec bit is set.
193 if ((mask
& (MAY_READ
|MAY_WRITE
)) || (inode
->i_mode
& S_IXUGO
))
194 if (capable(CAP_DAC_OVERRIDE
))
198 * Searching includes executable on directories, else just read.
200 if (mask
== MAY_READ
|| (S_ISDIR(inode
->i_mode
) && !(mask
& MAY_WRITE
)))
201 if (capable(CAP_DAC_READ_SEARCH
))
207 int permission(struct inode
* inode
,int mask
, struct nameidata
*nd
)
212 /* Ordinary permission routines do not understand MAY_APPEND. */
213 submask
= mask
& ~MAY_APPEND
;
215 if (inode
->i_op
&& inode
->i_op
->permission
)
216 retval
= inode
->i_op
->permission(inode
, submask
, nd
);
218 retval
= vfs_permission(inode
, submask
);
222 return security_inode_permission(inode
, mask
, nd
);
226 * get_write_access() gets write permission for a file.
227 * put_write_access() releases this write permission.
228 * This is used for regular files.
229 * We cannot support write (and maybe mmap read-write shared) accesses and
230 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
231 * can have the following values:
232 * 0: no writers, no VM_DENYWRITE mappings
233 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
234 * > 0: (i_writecount) users are writing to the file.
236 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
237 * except for the cases where we don't hold i_writecount yet. Then we need to
238 * use {get,deny}_write_access() - these functions check the sign and refuse
239 * to do the change if sign is wrong. Exclusion between them is provided by
240 * spinlock (arbitration_lock) and I'll rip the second arsehole to the first
241 * who will try to move it in struct inode - just leave it here.
243 static spinlock_t arbitration_lock
= SPIN_LOCK_UNLOCKED
;
244 int get_write_access(struct inode
* inode
)
246 spin_lock(&arbitration_lock
);
247 if (atomic_read(&inode
->i_writecount
) < 0) {
248 spin_unlock(&arbitration_lock
);
251 atomic_inc(&inode
->i_writecount
);
252 spin_unlock(&arbitration_lock
);
255 int deny_write_access(struct file
* file
)
257 spin_lock(&arbitration_lock
);
258 if (atomic_read(&file
->f_dentry
->d_inode
->i_writecount
) > 0) {
259 spin_unlock(&arbitration_lock
);
262 atomic_dec(&file
->f_dentry
->d_inode
->i_writecount
);
263 spin_unlock(&arbitration_lock
);
267 void path_release(struct nameidata
*nd
)
274 * Internal lookup() using the new generic dcache.
277 static struct dentry
* cached_lookup(struct dentry
* parent
, struct qstr
* name
, struct nameidata
*nd
)
279 struct dentry
* dentry
= __d_lookup(parent
, name
);
281 /* lockess __d_lookup may fail due to concurrent d_move()
282 * in some unrelated directory, so try with d_lookup
285 dentry
= d_lookup(parent
, name
);
287 if (dentry
&& dentry
->d_op
&& dentry
->d_op
->d_revalidate
) {
288 if (!dentry
->d_op
->d_revalidate(dentry
, nd
) && !d_invalidate(dentry
)) {
297 * Short-cut version of permission(), for calling by
298 * path_walk(), when dcache lock is held. Combines parts
299 * of permission() and vfs_permission(), and tests ONLY for
300 * MAY_EXEC permission.
302 * If appropriate, check DAC only. If not appropriate, or
303 * short-cut DAC fails, then call permission() to do more
304 * complete permission check.
306 static inline int exec_permission_lite(struct inode
*inode
,
307 struct nameidata
*nd
)
309 umode_t mode
= inode
->i_mode
;
311 if ((inode
->i_op
&& inode
->i_op
->permission
))
314 if (current
->fsuid
== inode
->i_uid
)
316 else if (in_group_p(inode
->i_gid
))
322 if ((inode
->i_mode
& S_IXUGO
) && capable(CAP_DAC_OVERRIDE
))
325 if (S_ISDIR(inode
->i_mode
) && capable(CAP_DAC_READ_SEARCH
))
330 return security_inode_permission(inode
, MAY_EXEC
, nd
);
334 * This is called when everything else fails, and we actually have
335 * to go to the low-level filesystem to find out what we should do..
337 * We get the directory semaphore, and after getting that we also
338 * make sure that nobody added the entry to the dcache in the meantime..
341 static struct dentry
* real_lookup(struct dentry
* parent
, struct qstr
* name
, struct nameidata
*nd
)
343 struct dentry
* result
;
344 struct inode
*dir
= parent
->d_inode
;
348 * First re-do the cached lookup just in case it was created
349 * while we waited for the directory semaphore..
351 * FIXME! This could use version numbering or similar to
352 * avoid unnecessary cache lookups.
354 * The "dcache_lock" is purely to protect the RCU list walker
355 * from concurrent renames at this point (we mustn't get false
356 * negatives from the RCU list walk here, unlike the optimistic
359 * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
361 result
= d_lookup(parent
, name
);
363 struct dentry
* dentry
= d_alloc(parent
, name
);
364 result
= ERR_PTR(-ENOMEM
);
366 result
= dir
->i_op
->lookup(dir
, dentry
, nd
);
377 * Uhhuh! Nasty case: the cache was re-populated while
378 * we waited on the semaphore. Need to revalidate.
381 if (result
->d_op
&& result
->d_op
->d_revalidate
) {
382 if (!result
->d_op
->d_revalidate(result
, nd
) && !d_invalidate(result
)) {
384 result
= ERR_PTR(-ENOENT
);
391 * This limits recursive symlink follows to 8, while
392 * limiting consecutive symlinks to 40.
394 * Without that kind of total limit, nasty chains of consecutive
395 * symlinks can cause almost arbitrarily long lookups.
397 static inline int do_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
400 if (current
->link_count
>= 5)
402 if (current
->total_link_count
>= 40)
405 err
= security_inode_follow_link(dentry
, nd
);
408 current
->link_count
++;
409 current
->total_link_count
++;
410 update_atime(dentry
->d_inode
);
411 err
= dentry
->d_inode
->i_op
->follow_link(dentry
, nd
);
412 current
->link_count
--;
419 int follow_up(struct vfsmount
**mnt
, struct dentry
**dentry
)
421 struct vfsmount
*parent
;
422 struct dentry
*mountpoint
;
423 spin_lock(&dcache_lock
);
424 parent
=(*mnt
)->mnt_parent
;
425 if (parent
== *mnt
) {
426 spin_unlock(&dcache_lock
);
430 mountpoint
=dget((*mnt
)->mnt_mountpoint
);
431 spin_unlock(&dcache_lock
);
433 *dentry
= mountpoint
;
439 /* no need for dcache_lock, as serialization is taken care in
442 static int follow_mount(struct vfsmount
**mnt
, struct dentry
**dentry
)
445 while (d_mountpoint(*dentry
)) {
446 struct vfsmount
*mounted
= lookup_mnt(*mnt
, *dentry
);
451 mntput(mounted
->mnt_parent
);
452 *dentry
= dget(mounted
->mnt_root
);
458 /* no need for dcache_lock, as serialization is taken care in
461 static inline int __follow_down(struct vfsmount
**mnt
, struct dentry
**dentry
)
463 struct vfsmount
*mounted
;
465 mounted
= lookup_mnt(*mnt
, *dentry
);
469 mntput(mounted
->mnt_parent
);
470 *dentry
= dget(mounted
->mnt_root
);
476 int follow_down(struct vfsmount
**mnt
, struct dentry
**dentry
)
478 return __follow_down(mnt
,dentry
);
481 static inline void follow_dotdot(struct vfsmount
**mnt
, struct dentry
**dentry
)
484 struct vfsmount
*parent
;
485 struct dentry
*old
= *dentry
;
487 read_lock(¤t
->fs
->lock
);
488 if (*dentry
== current
->fs
->root
&&
489 *mnt
== current
->fs
->rootmnt
) {
490 read_unlock(¤t
->fs
->lock
);
493 read_unlock(¤t
->fs
->lock
);
494 spin_lock(&dcache_lock
);
495 if (*dentry
!= (*mnt
)->mnt_root
) {
496 *dentry
= dget((*dentry
)->d_parent
);
497 spin_unlock(&dcache_lock
);
501 parent
= (*mnt
)->mnt_parent
;
502 if (parent
== *mnt
) {
503 spin_unlock(&dcache_lock
);
507 *dentry
= dget((*mnt
)->mnt_mountpoint
);
508 spin_unlock(&dcache_lock
);
513 follow_mount(mnt
, dentry
);
517 struct vfsmount
*mnt
;
518 struct dentry
*dentry
;
522 * It's more convoluted than I'd like it to be, but... it's still fairly
523 * small and for now I'd prefer to have fast path as straight as possible.
524 * It _is_ time-critical.
526 static int do_lookup(struct nameidata
*nd
, struct qstr
*name
,
529 struct vfsmount
*mnt
= nd
->mnt
;
530 struct dentry
*dentry
= __d_lookup(nd
->dentry
, name
);
534 if (dentry
->d_op
&& dentry
->d_op
->d_revalidate
)
535 goto need_revalidate
;
538 path
->dentry
= dentry
;
542 dentry
= real_lookup(nd
->dentry
, name
, nd
);
548 if (dentry
->d_op
->d_revalidate(dentry
, nd
))
550 if (d_invalidate(dentry
))
556 return PTR_ERR(dentry
);
562 * This is the basic name resolution function, turning a pathname
563 * into the final dentry.
565 * We expect 'base' to be positive and a directory.
567 int link_path_walk(const char * name
, struct nameidata
*nd
)
572 unsigned int lookup_flags
= nd
->flags
;
579 inode
= nd
->dentry
->d_inode
;
580 if (current
->link_count
)
581 lookup_flags
= LOOKUP_FOLLOW
;
583 /* At this point we know we have a real path component. */
589 err
= exec_permission_lite(inode
, nd
);
590 if (err
== -EAGAIN
) {
591 err
= permission(inode
, MAY_EXEC
, nd
);
597 c
= *(const unsigned char *)name
;
599 hash
= init_name_hash();
602 hash
= partial_name_hash(c
, hash
);
603 c
= *(const unsigned char *)name
;
604 } while (c
&& (c
!= '/'));
605 this.len
= name
- (const char *) this.name
;
606 this.hash
= end_name_hash(hash
);
608 /* remove trailing slashes? */
611 while (*++name
== '/');
613 goto last_with_slashes
;
616 * "." and ".." are special - ".." especially so because it has
617 * to be able to know about the current root directory and
618 * parent relationships.
620 if (this.name
[0] == '.') switch (this.len
) {
624 if (this.name
[1] != '.')
626 follow_dotdot(&nd
->mnt
, &nd
->dentry
);
627 inode
= nd
->dentry
->d_inode
;
633 * See if the low-level filesystem might want
634 * to use its own hash..
636 if (nd
->dentry
->d_op
&& nd
->dentry
->d_op
->d_hash
) {
637 err
= nd
->dentry
->d_op
->d_hash(nd
->dentry
, &this);
641 nd
->flags
|= LOOKUP_CONTINUE
;
642 /* This does the actual lookups.. */
643 err
= do_lookup(nd
, &this, &next
);
646 /* Check mountpoints.. */
647 follow_mount(&next
.mnt
, &next
.dentry
);
650 inode
= next
.dentry
->d_inode
;
657 if (inode
->i_op
->follow_link
) {
659 err
= do_follow_link(next
.dentry
, nd
);
665 inode
= nd
->dentry
->d_inode
;
674 nd
->dentry
= next
.dentry
;
677 if (!inode
->i_op
->lookup
)
680 /* here ends the main loop */
683 lookup_flags
|= LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
;
685 nd
->flags
&= ~LOOKUP_CONTINUE
;
686 if (lookup_flags
& LOOKUP_PARENT
)
688 if (this.name
[0] == '.') switch (this.len
) {
692 if (this.name
[1] != '.')
694 follow_dotdot(&nd
->mnt
, &nd
->dentry
);
695 inode
= nd
->dentry
->d_inode
;
700 if (nd
->dentry
->d_op
&& nd
->dentry
->d_op
->d_hash
) {
701 err
= nd
->dentry
->d_op
->d_hash(nd
->dentry
, &this);
705 err
= do_lookup(nd
, &this, &next
);
708 follow_mount(&next
.mnt
, &next
.dentry
);
709 inode
= next
.dentry
->d_inode
;
710 if ((lookup_flags
& LOOKUP_FOLLOW
)
711 && inode
&& inode
->i_op
&& inode
->i_op
->follow_link
) {
713 err
= do_follow_link(next
.dentry
, nd
);
718 inode
= nd
->dentry
->d_inode
;
722 nd
->dentry
= next
.dentry
;
727 if (lookup_flags
& LOOKUP_DIRECTORY
) {
729 if (!inode
->i_op
|| !inode
->i_op
->lookup
)
735 nd
->last_type
= LAST_NORM
;
736 if (this.name
[0] != '.')
739 nd
->last_type
= LAST_DOT
;
740 else if (this.len
== 2 && this.name
[1] == '.')
741 nd
->last_type
= LAST_DOTDOT
;
746 * We bypassed the ordinary revalidation routines.
747 * We may need to check the cached dentry for staleness.
749 if (nd
->dentry
&& nd
->dentry
->d_sb
&&
750 (nd
->dentry
->d_sb
->s_type
->fs_flags
& FS_REVAL_DOT
)) {
752 /* Note: we do not d_invalidate() */
753 if (!nd
->dentry
->d_op
->d_revalidate(nd
->dentry
, nd
))
767 int path_walk(const char * name
, struct nameidata
*nd
)
769 current
->total_link_count
= 0;
770 return link_path_walk(name
, nd
);
774 /* returns 1 if everything is done */
775 static int __emul_lookup_dentry(const char *name
, struct nameidata
*nd
)
777 if (path_walk(name
, nd
))
778 return 0; /* something went wrong... */
780 if (!nd
->dentry
->d_inode
|| S_ISDIR(nd
->dentry
->d_inode
->i_mode
)) {
781 struct nameidata nd_root
;
783 * NAME was not found in alternate root or it's a directory. Try to find
784 * it in the normal root:
786 nd_root
.last_type
= LAST_ROOT
;
787 nd_root
.flags
= nd
->flags
;
788 memcpy(&nd_root
.intent
, &nd
->intent
, sizeof(nd_root
.intent
));
789 read_lock(¤t
->fs
->lock
);
790 nd_root
.mnt
= mntget(current
->fs
->rootmnt
);
791 nd_root
.dentry
= dget(current
->fs
->root
);
792 read_unlock(¤t
->fs
->lock
);
793 if (path_walk(name
, &nd_root
))
795 if (nd_root
.dentry
->d_inode
) {
797 nd
->dentry
= nd_root
.dentry
;
798 nd
->mnt
= nd_root
.mnt
;
799 nd
->last
= nd_root
.last
;
802 path_release(&nd_root
);
807 void set_fs_altroot(void)
809 char *emul
= __emul_prefix();
811 struct vfsmount
*mnt
= NULL
, *oldmnt
;
812 struct dentry
*dentry
= NULL
, *olddentry
;
817 err
= path_lookup(emul
, LOOKUP_FOLLOW
|LOOKUP_DIRECTORY
|LOOKUP_NOALT
, &nd
);
823 write_lock(¤t
->fs
->lock
);
824 oldmnt
= current
->fs
->altrootmnt
;
825 olddentry
= current
->fs
->altroot
;
826 current
->fs
->altrootmnt
= mnt
;
827 current
->fs
->altroot
= dentry
;
828 write_unlock(¤t
->fs
->lock
);
837 walk_init_root(const char *name
, struct nameidata
*nd
)
839 read_lock(¤t
->fs
->lock
);
840 if (current
->fs
->altroot
&& !(nd
->flags
& LOOKUP_NOALT
)) {
841 nd
->mnt
= mntget(current
->fs
->altrootmnt
);
842 nd
->dentry
= dget(current
->fs
->altroot
);
843 read_unlock(¤t
->fs
->lock
);
844 if (__emul_lookup_dentry(name
,nd
))
846 read_lock(¤t
->fs
->lock
);
848 nd
->mnt
= mntget(current
->fs
->rootmnt
);
849 nd
->dentry
= dget(current
->fs
->root
);
850 read_unlock(¤t
->fs
->lock
);
854 int path_lookup(const char *name
, unsigned int flags
, struct nameidata
*nd
)
856 nd
->last_type
= LAST_ROOT
; /* if there are only slashes... */
859 read_lock(¤t
->fs
->lock
);
861 if (current
->fs
->altroot
&& !(nd
->flags
& LOOKUP_NOALT
)) {
862 nd
->mnt
= mntget(current
->fs
->altrootmnt
);
863 nd
->dentry
= dget(current
->fs
->altroot
);
864 read_unlock(¤t
->fs
->lock
);
865 if (__emul_lookup_dentry(name
,nd
))
867 read_lock(¤t
->fs
->lock
);
869 nd
->mnt
= mntget(current
->fs
->rootmnt
);
870 nd
->dentry
= dget(current
->fs
->root
);
873 nd
->mnt
= mntget(current
->fs
->pwdmnt
);
874 nd
->dentry
= dget(current
->fs
->pwd
);
876 read_unlock(¤t
->fs
->lock
);
877 current
->total_link_count
= 0;
878 return link_path_walk(name
, nd
);
882 * Restricted form of lookup. Doesn't follow links, single-component only,
883 * needs parent already locked. Doesn't follow mounts.
886 static struct dentry
* __lookup_hash(struct qstr
*name
, struct dentry
* base
, struct nameidata
*nd
)
888 struct dentry
* dentry
;
892 inode
= base
->d_inode
;
893 err
= permission(inode
, MAY_EXEC
, nd
);
894 dentry
= ERR_PTR(err
);
899 * See if the low-level filesystem might want
900 * to use its own hash..
902 if (base
->d_op
&& base
->d_op
->d_hash
) {
903 err
= base
->d_op
->d_hash(base
, name
);
904 dentry
= ERR_PTR(err
);
909 dentry
= cached_lookup(base
, name
, nd
);
911 struct dentry
*new = d_alloc(base
, name
);
912 dentry
= ERR_PTR(-ENOMEM
);
915 dentry
= inode
->i_op
->lookup(inode
, new, nd
);
925 struct dentry
* lookup_hash(struct qstr
*name
, struct dentry
* base
)
927 return __lookup_hash(name
, base
, NULL
);
931 struct dentry
* lookup_one_len(const char * name
, struct dentry
* base
, int len
)
942 hash
= init_name_hash();
944 c
= *(const unsigned char *)name
++;
945 if (c
== '/' || c
== '\0')
947 hash
= partial_name_hash(c
, hash
);
949 this.hash
= end_name_hash(hash
);
951 return lookup_hash(&this, base
);
953 return ERR_PTR(-EACCES
);
959 * is used by most simple commands to get the inode of a specified name.
960 * Open, link etc use their own routines, but this is enough for things
963 * namei exists in two versions: namei/lnamei. The only difference is
964 * that namei follows links, while lnamei does not.
967 int __user_walk(const char __user
*name
, unsigned flags
, struct nameidata
*nd
)
969 char *tmp
= getname(name
);
970 int err
= PTR_ERR(tmp
);
973 err
= path_lookup(tmp
, flags
, nd
);
980 * It's inline, so penalty for filesystems that don't use sticky bit is
983 static inline int check_sticky(struct inode
*dir
, struct inode
*inode
)
985 if (!(dir
->i_mode
& S_ISVTX
))
987 if (inode
->i_uid
== current
->fsuid
)
989 if (dir
->i_uid
== current
->fsuid
)
991 return !capable(CAP_FOWNER
);
995 * Check whether we can remove a link victim from directory dir, check
996 * whether the type of victim is right.
997 * 1. We can't do it if dir is read-only (done in permission())
998 * 2. We should have write and exec permissions on dir
999 * 3. We can't remove anything from append-only dir
1000 * 4. We can't do anything with immutable dir (done in permission())
1001 * 5. If the sticky bit on dir is set we should either
1002 * a. be owner of dir, or
1003 * b. be owner of victim, or
1004 * c. have CAP_FOWNER capability
1005 * 6. If the victim is append-only or immutable we can't do antyhing with
1006 * links pointing to it.
1007 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1008 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1009 * 9. We can't remove a root or mountpoint.
1010 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1011 * nfs_async_unlink().
1013 static inline int may_delete(struct inode
*dir
,struct dentry
*victim
,int isdir
)
1016 if (!victim
->d_inode
|| victim
->d_parent
->d_inode
!= dir
)
1018 error
= permission(dir
,MAY_WRITE
| MAY_EXEC
, NULL
);
1023 if (check_sticky(dir
, victim
->d_inode
)||IS_APPEND(victim
->d_inode
)||
1024 IS_IMMUTABLE(victim
->d_inode
))
1027 if (!S_ISDIR(victim
->d_inode
->i_mode
))
1029 if (IS_ROOT(victim
))
1031 } else if (S_ISDIR(victim
->d_inode
->i_mode
))
1033 if (IS_DEADDIR(dir
))
1035 if (victim
->d_flags
& DCACHE_NFSFS_RENAMED
)
1040 /* Check whether we can create an object with dentry child in directory
1042 * 1. We can't do it if child already exists (open has special treatment for
1043 * this case, but since we are inlined it's OK)
1044 * 2. We can't do it if dir is read-only (done in permission())
1045 * 3. We should have write and exec permissions on dir
1046 * 4. We can't do it if dir is immutable (done in permission())
1048 static inline int may_create(struct inode
*dir
, struct dentry
*child
,
1049 struct nameidata
*nd
)
1053 if (IS_DEADDIR(dir
))
1055 return permission(dir
,MAY_WRITE
| MAY_EXEC
, nd
);
1059 * Special case: O_CREAT|O_EXCL implies O_NOFOLLOW for security
1062 * O_DIRECTORY translates into forcing a directory lookup.
1064 static inline int lookup_flags(unsigned int f
)
1066 unsigned long retval
= LOOKUP_FOLLOW
;
1069 retval
&= ~LOOKUP_FOLLOW
;
1071 if ((f
& (O_CREAT
|O_EXCL
)) == (O_CREAT
|O_EXCL
))
1072 retval
&= ~LOOKUP_FOLLOW
;
1074 if (f
& O_DIRECTORY
)
1075 retval
|= LOOKUP_DIRECTORY
;
1081 * p1 and p2 should be directories on the same fs.
1083 struct dentry
*lock_rename(struct dentry
*p1
, struct dentry
*p2
)
1088 down(&p1
->d_inode
->i_sem
);
1092 down(&p1
->d_inode
->i_sb
->s_vfs_rename_sem
);
1094 for (p
= p1
; p
->d_parent
!= p
; p
= p
->d_parent
) {
1095 if (p
->d_parent
== p2
) {
1096 down(&p2
->d_inode
->i_sem
);
1097 down(&p1
->d_inode
->i_sem
);
1102 for (p
= p2
; p
->d_parent
!= p
; p
= p
->d_parent
) {
1103 if (p
->d_parent
== p1
) {
1104 down(&p1
->d_inode
->i_sem
);
1105 down(&p2
->d_inode
->i_sem
);
1110 down(&p1
->d_inode
->i_sem
);
1111 down(&p2
->d_inode
->i_sem
);
1115 void unlock_rename(struct dentry
*p1
, struct dentry
*p2
)
1117 up(&p1
->d_inode
->i_sem
);
1119 up(&p2
->d_inode
->i_sem
);
1120 up(&p1
->d_inode
->i_sb
->s_vfs_rename_sem
);
1124 int vfs_create(struct inode
*dir
, struct dentry
*dentry
, int mode
,
1125 struct nameidata
*nd
)
1127 int error
= may_create(dir
, dentry
, nd
);
1132 if (!dir
->i_op
|| !dir
->i_op
->create
)
1133 return -EACCES
; /* shouldn't it be ENOSYS? */
1136 error
= security_inode_create(dir
, dentry
, mode
);
1140 error
= dir
->i_op
->create(dir
, dentry
, mode
, nd
);
1142 inode_dir_notify(dir
, DN_CREATE
);
1143 security_inode_post_create(dir
, dentry
, mode
);
1148 int may_open(struct nameidata
*nd
, int acc_mode
, int flag
)
1150 struct dentry
*dentry
= nd
->dentry
;
1151 struct inode
*inode
= dentry
->d_inode
;
1157 if (S_ISLNK(inode
->i_mode
))
1160 if (S_ISDIR(inode
->i_mode
) && (flag
& FMODE_WRITE
))
1163 error
= permission(inode
, acc_mode
, nd
);
1168 * FIFO's, sockets and device files are special: they don't
1169 * actually live on the filesystem itself, and as such you
1170 * can write to them even if the filesystem is read-only.
1172 if (S_ISFIFO(inode
->i_mode
) || S_ISSOCK(inode
->i_mode
)) {
1174 } else if (S_ISBLK(inode
->i_mode
) || S_ISCHR(inode
->i_mode
)) {
1175 if (nd
->mnt
->mnt_flags
& MNT_NODEV
)
1179 } else if (IS_RDONLY(inode
) && (flag
& FMODE_WRITE
))
1182 * An append-only file must be opened in append mode for writing.
1184 if (IS_APPEND(inode
)) {
1185 if ((flag
& FMODE_WRITE
) && !(flag
& O_APPEND
))
1192 * Ensure there are no outstanding leases on the file.
1194 error
= break_lease(inode
, flag
);
1198 if (flag
& O_TRUNC
) {
1199 error
= get_write_access(inode
);
1204 * Refuse to truncate files with mandatory locks held on them.
1206 error
= locks_verify_locked(inode
);
1210 error
= do_truncate(dentry
, 0);
1212 put_write_access(inode
);
1216 if (flag
& FMODE_WRITE
)
1225 * namei for open - this is in fact almost the whole open-routine.
1227 * Note that the low bits of "flag" aren't the same as in the open
1228 * system call - they are 00 - no permissions needed
1229 * 01 - read permission needed
1230 * 10 - write permission needed
1231 * 11 - read/write permissions needed
1232 * which is a lot more logical, and also allows the "no perm" needed
1233 * for symlinks (where the permissions are checked later).
1236 int open_namei(const char * pathname
, int flag
, int mode
, struct nameidata
*nd
)
1238 int acc_mode
, error
= 0;
1239 struct dentry
*dentry
;
1243 acc_mode
= ACC_MODE(flag
);
1245 /* Allow the LSM permission hook to distinguish append
1246 access from general write access. */
1247 if (flag
& O_APPEND
)
1248 acc_mode
|= MAY_APPEND
;
1250 /* Fill in the open() intent data */
1251 nd
->intent
.open
.flags
= flag
;
1252 nd
->intent
.open
.create_mode
= mode
;
1255 * The simplest case - just a plain lookup.
1257 if (!(flag
& O_CREAT
)) {
1258 error
= path_lookup(pathname
, lookup_flags(flag
)|LOOKUP_OPEN
, nd
);
1261 dentry
= nd
->dentry
;
1266 * Create - we need to know the parent.
1268 error
= path_lookup(pathname
, LOOKUP_PARENT
|LOOKUP_OPEN
|LOOKUP_CREATE
, nd
);
1273 * We have the parent and last component. First of all, check
1274 * that we are not asked to creat(2) an obvious directory - that
1278 if (nd
->last_type
!= LAST_NORM
|| nd
->last
.name
[nd
->last
.len
])
1282 nd
->flags
&= ~LOOKUP_PARENT
;
1283 down(&dir
->d_inode
->i_sem
);
1284 dentry
= __lookup_hash(&nd
->last
, nd
->dentry
, nd
);
1287 error
= PTR_ERR(dentry
);
1288 if (IS_ERR(dentry
)) {
1289 up(&dir
->d_inode
->i_sem
);
1293 /* Negative dentry, just create the file */
1294 if (!dentry
->d_inode
) {
1295 if (!IS_POSIXACL(dir
->d_inode
))
1296 mode
&= ~current
->fs
->umask
;
1297 error
= vfs_create(dir
->d_inode
, dentry
, mode
, nd
);
1298 up(&dir
->d_inode
->i_sem
);
1300 nd
->dentry
= dentry
;
1303 /* Don't check for write permission, don't truncate */
1310 * It already exists.
1312 up(&dir
->d_inode
->i_sem
);
1318 if (d_mountpoint(dentry
)) {
1320 if (flag
& O_NOFOLLOW
)
1322 while (__follow_down(&nd
->mnt
,&dentry
) && d_mountpoint(dentry
));
1325 if (!dentry
->d_inode
)
1327 if (dentry
->d_inode
->i_op
&& dentry
->d_inode
->i_op
->follow_link
)
1331 nd
->dentry
= dentry
;
1333 if (dentry
->d_inode
&& S_ISDIR(dentry
->d_inode
->i_mode
))
1336 error
= may_open(nd
, acc_mode
, flag
);
1349 if (flag
& O_NOFOLLOW
)
1352 * This is subtle. Instead of calling do_follow_link() we do the
1353 * thing by hands. The reason is that this way we have zero link_count
1354 * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1355 * After that we have the parent and last component, i.e.
1356 * we are in the same situation as after the first path_walk().
1357 * Well, almost - if the last component is normal we get its copy
1358 * stored in nd->last.name and we will have to putname() it when we
1359 * are done. Procfs-like symlinks just set LAST_BIND.
1361 nd
->flags
|= LOOKUP_PARENT
;
1362 error
= security_inode_follow_link(dentry
, nd
);
1365 update_atime(dentry
->d_inode
);
1366 error
= dentry
->d_inode
->i_op
->follow_link(dentry
, nd
);
1370 nd
->flags
&= ~LOOKUP_PARENT
;
1371 if (nd
->last_type
== LAST_BIND
) {
1372 dentry
= nd
->dentry
;
1376 if (nd
->last_type
!= LAST_NORM
)
1378 if (nd
->last
.name
[nd
->last
.len
]) {
1379 putname(nd
->last
.name
);
1384 putname(nd
->last
.name
);
1388 down(&dir
->d_inode
->i_sem
);
1389 dentry
= __lookup_hash(&nd
->last
, nd
->dentry
, nd
);
1390 putname(nd
->last
.name
);
1395 * lookup_create - lookup a dentry, creating it if it doesn't exist
1396 * @nd: nameidata info
1397 * @is_dir: directory flag
1399 * Simple function to lookup and return a dentry and create it
1400 * if it doesn't exist. Is SMP-safe.
1402 struct dentry
*lookup_create(struct nameidata
*nd
, int is_dir
)
1404 struct dentry
*dentry
;
1406 down(&nd
->dentry
->d_inode
->i_sem
);
1407 dentry
= ERR_PTR(-EEXIST
);
1408 if (nd
->last_type
!= LAST_NORM
)
1410 nd
->flags
&= ~LOOKUP_PARENT
;
1411 dentry
= lookup_hash(&nd
->last
, nd
->dentry
);
1414 if (!is_dir
&& nd
->last
.name
[nd
->last
.len
] && !dentry
->d_inode
)
1419 dentry
= ERR_PTR(-ENOENT
);
1424 int vfs_mknod(struct inode
*dir
, struct dentry
*dentry
, int mode
, dev_t dev
)
1426 int error
= may_create(dir
, dentry
, NULL
);
1431 if ((S_ISCHR(mode
) || S_ISBLK(mode
)) && !capable(CAP_MKNOD
))
1434 if (!dir
->i_op
|| !dir
->i_op
->mknod
)
1437 error
= security_inode_mknod(dir
, dentry
, mode
, dev
);
1442 error
= dir
->i_op
->mknod(dir
, dentry
, mode
, dev
);
1444 inode_dir_notify(dir
, DN_CREATE
);
1445 security_inode_post_mknod(dir
, dentry
, mode
, dev
);
1450 asmlinkage
long sys_mknod(const char __user
* filename
, int mode
, unsigned dev
)
1454 struct dentry
* dentry
;
1455 struct nameidata nd
;
1459 tmp
= getname(filename
);
1461 return PTR_ERR(tmp
);
1463 error
= path_lookup(tmp
, LOOKUP_PARENT
, &nd
);
1466 dentry
= lookup_create(&nd
, 0);
1467 error
= PTR_ERR(dentry
);
1469 if (!IS_POSIXACL(nd
.dentry
->d_inode
))
1470 mode
&= ~current
->fs
->umask
;
1471 if (!IS_ERR(dentry
)) {
1472 switch (mode
& S_IFMT
) {
1473 case 0: case S_IFREG
:
1474 error
= vfs_create(nd
.dentry
->d_inode
,dentry
,mode
,&nd
);
1476 case S_IFCHR
: case S_IFBLK
:
1477 error
= vfs_mknod(nd
.dentry
->d_inode
,dentry
,mode
,
1478 new_decode_dev(dev
));
1480 case S_IFIFO
: case S_IFSOCK
:
1481 error
= vfs_mknod(nd
.dentry
->d_inode
,dentry
,mode
,0);
1491 up(&nd
.dentry
->d_inode
->i_sem
);
1499 int vfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
1501 int error
= may_create(dir
, dentry
, NULL
);
1506 if (!dir
->i_op
|| !dir
->i_op
->mkdir
)
1509 mode
&= (S_IRWXUGO
|S_ISVTX
);
1510 error
= security_inode_mkdir(dir
, dentry
, mode
);
1515 error
= dir
->i_op
->mkdir(dir
, dentry
, mode
);
1517 inode_dir_notify(dir
, DN_CREATE
);
1518 security_inode_post_mkdir(dir
,dentry
, mode
);
1523 asmlinkage
long sys_mkdir(const char __user
* pathname
, int mode
)
1528 tmp
= getname(pathname
);
1529 error
= PTR_ERR(tmp
);
1531 struct dentry
*dentry
;
1532 struct nameidata nd
;
1534 error
= path_lookup(tmp
, LOOKUP_PARENT
, &nd
);
1537 dentry
= lookup_create(&nd
, 1);
1538 error
= PTR_ERR(dentry
);
1539 if (!IS_ERR(dentry
)) {
1540 if (!IS_POSIXACL(nd
.dentry
->d_inode
))
1541 mode
&= ~current
->fs
->umask
;
1542 error
= vfs_mkdir(nd
.dentry
->d_inode
, dentry
, mode
);
1545 up(&nd
.dentry
->d_inode
->i_sem
);
1555 * We try to drop the dentry early: we should have
1556 * a usage count of 2 if we're the only user of this
1557 * dentry, and if that is true (possibly after pruning
1558 * the dcache), then we drop the dentry now.
1560 * A low-level filesystem can, if it choses, legally
1563 * if (!d_unhashed(dentry))
1566 * if it cannot handle the case of removing a directory
1567 * that is still in use by something else..
1569 static void d_unhash(struct dentry
*dentry
)
1572 spin_lock(&dcache_lock
);
1573 switch (atomic_read(&dentry
->d_count
)) {
1575 spin_unlock(&dcache_lock
);
1576 shrink_dcache_parent(dentry
);
1577 spin_lock(&dcache_lock
);
1578 if (atomic_read(&dentry
->d_count
) != 2)
1583 spin_unlock(&dcache_lock
);
1586 int vfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
1588 int error
= may_delete(dir
, dentry
, 1);
1593 if (!dir
->i_op
|| !dir
->i_op
->rmdir
)
1598 down(&dentry
->d_inode
->i_sem
);
1600 if (d_mountpoint(dentry
))
1603 error
= security_inode_rmdir(dir
, dentry
);
1605 error
= dir
->i_op
->rmdir(dir
, dentry
);
1607 dentry
->d_inode
->i_flags
|= S_DEAD
;
1610 up(&dentry
->d_inode
->i_sem
);
1612 inode_dir_notify(dir
, DN_DELETE
);
1620 asmlinkage
long sys_rmdir(const char __user
* pathname
)
1624 struct dentry
*dentry
;
1625 struct nameidata nd
;
1627 name
= getname(pathname
);
1629 return PTR_ERR(name
);
1631 error
= path_lookup(name
, LOOKUP_PARENT
, &nd
);
1635 switch(nd
.last_type
) {
1646 down(&nd
.dentry
->d_inode
->i_sem
);
1647 dentry
= lookup_hash(&nd
.last
, nd
.dentry
);
1648 error
= PTR_ERR(dentry
);
1649 if (!IS_ERR(dentry
)) {
1650 error
= vfs_rmdir(nd
.dentry
->d_inode
, dentry
);
1653 up(&nd
.dentry
->d_inode
->i_sem
);
1661 int vfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
1663 int error
= may_delete(dir
, dentry
, 0);
1668 if (!dir
->i_op
|| !dir
->i_op
->unlink
)
1673 down(&dentry
->d_inode
->i_sem
);
1674 if (d_mountpoint(dentry
))
1677 error
= security_inode_unlink(dir
, dentry
);
1679 error
= dir
->i_op
->unlink(dir
, dentry
);
1681 up(&dentry
->d_inode
->i_sem
);
1683 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
1684 if (!error
&& !(dentry
->d_flags
& DCACHE_NFSFS_RENAMED
)) {
1686 inode_dir_notify(dir
, DN_DELETE
);
1692 * Make sure that the actual truncation of the file will occur outside its
1693 * directory's i_sem. Truncate can take a long time if there is a lot of
1694 * writeout happening, and we don't want to prevent access to the directory
1695 * while waiting on the I/O.
1697 asmlinkage
long sys_unlink(const char __user
* pathname
)
1701 struct dentry
*dentry
;
1702 struct nameidata nd
;
1703 struct inode
*inode
= NULL
;
1705 name
= getname(pathname
);
1707 return PTR_ERR(name
);
1709 error
= path_lookup(name
, LOOKUP_PARENT
, &nd
);
1713 if (nd
.last_type
!= LAST_NORM
)
1715 down(&nd
.dentry
->d_inode
->i_sem
);
1716 dentry
= lookup_hash(&nd
.last
, nd
.dentry
);
1717 error
= PTR_ERR(dentry
);
1718 if (!IS_ERR(dentry
)) {
1719 /* Why not before? Because we want correct error value */
1720 if (nd
.last
.name
[nd
.last
.len
])
1722 inode
= dentry
->d_inode
;
1724 atomic_inc(&inode
->i_count
);
1725 error
= vfs_unlink(nd
.dentry
->d_inode
, dentry
);
1729 up(&nd
.dentry
->d_inode
->i_sem
);
1736 iput(inode
); /* truncate the inode here */
1740 error
= !dentry
->d_inode
? -ENOENT
:
1741 S_ISDIR(dentry
->d_inode
->i_mode
) ? -EISDIR
: -ENOTDIR
;
1745 int vfs_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *oldname
)
1747 int error
= may_create(dir
, dentry
, NULL
);
1752 if (!dir
->i_op
|| !dir
->i_op
->symlink
)
1755 error
= security_inode_symlink(dir
, dentry
, oldname
);
1760 error
= dir
->i_op
->symlink(dir
, dentry
, oldname
);
1762 inode_dir_notify(dir
, DN_CREATE
);
1763 security_inode_post_symlink(dir
, dentry
, oldname
);
1768 asmlinkage
long sys_symlink(const char __user
* oldname
, const char __user
* newname
)
1774 from
= getname(oldname
);
1776 return PTR_ERR(from
);
1777 to
= getname(newname
);
1778 error
= PTR_ERR(to
);
1780 struct dentry
*dentry
;
1781 struct nameidata nd
;
1783 error
= path_lookup(to
, LOOKUP_PARENT
, &nd
);
1786 dentry
= lookup_create(&nd
, 0);
1787 error
= PTR_ERR(dentry
);
1788 if (!IS_ERR(dentry
)) {
1789 error
= vfs_symlink(nd
.dentry
->d_inode
, dentry
, from
);
1792 up(&nd
.dentry
->d_inode
->i_sem
);
1801 int vfs_link(struct dentry
*old_dentry
, struct inode
*dir
, struct dentry
*new_dentry
)
1803 struct inode
*inode
= old_dentry
->d_inode
;
1809 error
= may_create(dir
, new_dentry
, NULL
);
1813 if (dir
->i_sb
!= inode
->i_sb
)
1817 * A link to an append-only or immutable file cannot be created.
1819 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
1821 if (!dir
->i_op
|| !dir
->i_op
->link
)
1823 if (S_ISDIR(old_dentry
->d_inode
->i_mode
))
1826 error
= security_inode_link(old_dentry
, dir
, new_dentry
);
1830 down(&old_dentry
->d_inode
->i_sem
);
1832 error
= dir
->i_op
->link(old_dentry
, dir
, new_dentry
);
1833 up(&old_dentry
->d_inode
->i_sem
);
1835 inode_dir_notify(dir
, DN_CREATE
);
1836 security_inode_post_link(old_dentry
, dir
, new_dentry
);
1842 * Hardlinks are often used in delicate situations. We avoid
1843 * security-related surprises by not following symlinks on the
1846 * We don't follow them on the oldname either to be compatible
1847 * with linux 2.0, and to avoid hard-linking to directories
1848 * and other special files. --ADM
1850 asmlinkage
long sys_link(const char __user
* oldname
, const char __user
* newname
)
1852 struct dentry
*new_dentry
;
1853 struct nameidata nd
, old_nd
;
1857 to
= getname(newname
);
1861 error
= __user_walk(oldname
, 0, &old_nd
);
1864 error
= path_lookup(to
, LOOKUP_PARENT
, &nd
);
1868 if (old_nd
.mnt
!= nd
.mnt
)
1870 new_dentry
= lookup_create(&nd
, 0);
1871 error
= PTR_ERR(new_dentry
);
1872 if (!IS_ERR(new_dentry
)) {
1873 error
= vfs_link(old_nd
.dentry
, nd
.dentry
->d_inode
, new_dentry
);
1876 up(&nd
.dentry
->d_inode
->i_sem
);
1880 path_release(&old_nd
);
1888 * The worst of all namespace operations - renaming directory. "Perverted"
1889 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
1891 * a) we can get into loop creation. Check is done in is_subdir().
1892 * b) race potential - two innocent renames can create a loop together.
1893 * That's where 4.4 screws up. Current fix: serialization on
1894 * sb->s_vfs_rename_sem. We might be more accurate, but that's another
1896 * c) we have to lock _three_ objects - parents and victim (if it exists).
1897 * And that - after we got ->i_sem on parents (until then we don't know
1898 * whether the target exists). Solution: try to be smart with locking
1899 * order for inodes. We rely on the fact that tree topology may change
1900 * only under ->s_vfs_rename_sem _and_ that parent of the object we
1901 * move will be locked. Thus we can rank directories by the tree
1902 * (ancestors first) and rank all non-directories after them.
1903 * That works since everybody except rename does "lock parent, lookup,
1904 * lock child" and rename is under ->s_vfs_rename_sem.
1905 * HOWEVER, it relies on the assumption that any object with ->lookup()
1906 * has no more than 1 dentry. If "hybrid" objects will ever appear,
1907 * we'd better make sure that there's no link(2) for them.
1908 * d) some filesystems don't support opened-but-unlinked directories,
1909 * either because of layout or because they are not ready to deal with
1910 * all cases correctly. The latter will be fixed (taking this sort of
1911 * stuff into VFS), but the former is not going away. Solution: the same
1912 * trick as in rmdir().
1913 * e) conversion from fhandle to dentry may come in the wrong moment - when
1914 * we are removing the target. Solution: we will have to grab ->i_sem
1915 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
1916 * ->i_sem on parents, which works but leads to some truely excessive
1919 int vfs_rename_dir(struct inode
*old_dir
, struct dentry
*old_dentry
,
1920 struct inode
*new_dir
, struct dentry
*new_dentry
)
1923 struct inode
*target
;
1926 * If we are going to change the parent - check write permissions,
1927 * we'll need to flip '..'.
1929 if (new_dir
!= old_dir
) {
1930 error
= permission(old_dentry
->d_inode
, MAY_WRITE
, NULL
);
1935 error
= security_inode_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
1939 target
= new_dentry
->d_inode
;
1941 down(&target
->i_sem
);
1942 d_unhash(new_dentry
);
1944 if (d_mountpoint(old_dentry
)||d_mountpoint(new_dentry
))
1947 error
= old_dir
->i_op
->rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
1950 target
->i_flags
|= S_DEAD
;
1952 if (d_unhashed(new_dentry
))
1953 d_rehash(new_dentry
);
1957 d_move(old_dentry
,new_dentry
);
1958 security_inode_post_rename(old_dir
, old_dentry
,
1959 new_dir
, new_dentry
);
1964 int vfs_rename_other(struct inode
*old_dir
, struct dentry
*old_dentry
,
1965 struct inode
*new_dir
, struct dentry
*new_dentry
)
1967 struct inode
*target
;
1970 error
= security_inode_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
1975 target
= new_dentry
->d_inode
;
1977 down(&target
->i_sem
);
1978 if (d_mountpoint(old_dentry
)||d_mountpoint(new_dentry
))
1981 error
= old_dir
->i_op
->rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
1983 /* The following d_move() should become unconditional */
1984 if (!(old_dir
->i_sb
->s_type
->fs_flags
& FS_ODD_RENAME
))
1985 d_move(old_dentry
, new_dentry
);
1986 security_inode_post_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
1994 int vfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1995 struct inode
*new_dir
, struct dentry
*new_dentry
)
1998 int is_dir
= S_ISDIR(old_dentry
->d_inode
->i_mode
);
2000 if (old_dentry
->d_inode
== new_dentry
->d_inode
)
2003 error
= may_delete(old_dir
, old_dentry
, is_dir
);
2007 if (!new_dentry
->d_inode
)
2008 error
= may_create(new_dir
, new_dentry
, NULL
);
2010 error
= may_delete(new_dir
, new_dentry
, is_dir
);
2014 if (!old_dir
->i_op
|| !old_dir
->i_op
->rename
)
2017 DQUOT_INIT(old_dir
);
2018 DQUOT_INIT(new_dir
);
2021 error
= vfs_rename_dir(old_dir
,old_dentry
,new_dir
,new_dentry
);
2023 error
= vfs_rename_other(old_dir
,old_dentry
,new_dir
,new_dentry
);
2025 if (old_dir
== new_dir
)
2026 inode_dir_notify(old_dir
, DN_RENAME
);
2028 inode_dir_notify(old_dir
, DN_DELETE
);
2029 inode_dir_notify(new_dir
, DN_CREATE
);
2035 static inline int do_rename(const char * oldname
, const char * newname
)
2038 struct dentry
* old_dir
, * new_dir
;
2039 struct dentry
* old_dentry
, *new_dentry
;
2040 struct dentry
* trap
;
2041 struct nameidata oldnd
, newnd
;
2043 error
= path_lookup(oldname
, LOOKUP_PARENT
, &oldnd
);
2047 error
= path_lookup(newname
, LOOKUP_PARENT
, &newnd
);
2052 if (oldnd
.mnt
!= newnd
.mnt
)
2055 old_dir
= oldnd
.dentry
;
2057 if (oldnd
.last_type
!= LAST_NORM
)
2060 new_dir
= newnd
.dentry
;
2061 if (newnd
.last_type
!= LAST_NORM
)
2064 trap
= lock_rename(new_dir
, old_dir
);
2066 old_dentry
= lookup_hash(&oldnd
.last
, old_dir
);
2067 error
= PTR_ERR(old_dentry
);
2068 if (IS_ERR(old_dentry
))
2070 /* source must exist */
2072 if (!old_dentry
->d_inode
)
2074 /* unless the source is a directory trailing slashes give -ENOTDIR */
2075 if (!S_ISDIR(old_dentry
->d_inode
->i_mode
)) {
2077 if (oldnd
.last
.name
[oldnd
.last
.len
])
2079 if (newnd
.last
.name
[newnd
.last
.len
])
2082 /* source should not be ancestor of target */
2084 if (old_dentry
== trap
)
2086 new_dentry
= lookup_hash(&newnd
.last
, new_dir
);
2087 error
= PTR_ERR(new_dentry
);
2088 if (IS_ERR(new_dentry
))
2090 /* target should not be an ancestor of source */
2092 if (new_dentry
== trap
)
2095 error
= vfs_rename(old_dir
->d_inode
, old_dentry
,
2096 new_dir
->d_inode
, new_dentry
);
2102 unlock_rename(new_dir
, old_dir
);
2104 path_release(&newnd
);
2106 path_release(&oldnd
);
2111 asmlinkage
long sys_rename(const char __user
* oldname
, const char __user
* newname
)
2117 from
= getname(oldname
);
2119 return PTR_ERR(from
);
2120 to
= getname(newname
);
2121 error
= PTR_ERR(to
);
2123 error
= do_rename(from
,to
);
2130 int vfs_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
, const char *link
)
2134 len
= PTR_ERR(link
);
2139 if (len
> (unsigned) buflen
)
2141 if (copy_to_user(buffer
, link
, len
))
2148 __vfs_follow_link(struct nameidata
*nd
, const char *link
)
2157 if (!walk_init_root(link
, nd
))
2158 /* weird __emul_prefix() stuff did it */
2161 res
= link_path_walk(link
, nd
);
2163 if (current
->link_count
|| res
|| nd
->last_type
!=LAST_NORM
)
2166 * If it is an iterative symlinks resolution in open_namei() we
2167 * have to copy the last component. And all that crap because of
2168 * bloody create() on broken symlinks. Furrfu...
2171 if (unlikely(!name
)) {
2175 strcpy(name
, nd
->last
.name
);
2176 nd
->last
.name
= name
;
2180 return PTR_ERR(link
);
2183 int vfs_follow_link(struct nameidata
*nd
, const char *link
)
2185 return __vfs_follow_link(nd
, link
);
2188 /* get the link contents into pagecache */
2189 static char *page_getlink(struct dentry
* dentry
, struct page
**ppage
)
2192 struct address_space
*mapping
= dentry
->d_inode
->i_mapping
;
2193 page
= read_cache_page(mapping
, 0, (filler_t
*)mapping
->a_ops
->readpage
,
2197 wait_on_page_locked(page
);
2198 if (!PageUptodate(page
))
2204 page_cache_release(page
);
2205 return ERR_PTR(-EIO
);
2211 int page_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
)
2213 struct page
*page
= NULL
;
2214 char *s
= page_getlink(dentry
, &page
);
2215 int res
= vfs_readlink(dentry
,buffer
,buflen
,s
);
2218 page_cache_release(page
);
2223 int page_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
2225 struct page
*page
= NULL
;
2226 char *s
= page_getlink(dentry
, &page
);
2227 int res
= __vfs_follow_link(nd
, s
);
2230 page_cache_release(page
);
2235 int page_symlink(struct inode
*inode
, const char *symname
, int len
)
2237 struct address_space
*mapping
= inode
->i_mapping
;
2238 struct page
*page
= grab_cache_page(mapping
, 0);
2244 err
= mapping
->a_ops
->prepare_write(NULL
, page
, 0, len
-1);
2247 kaddr
= kmap_atomic(page
, KM_USER0
);
2248 memcpy(kaddr
, symname
, len
-1);
2249 kunmap_atomic(kaddr
, KM_USER0
);
2250 mapping
->a_ops
->commit_write(NULL
, page
, 0, len
-1);
2252 * Notice that we are _not_ going to block here - end of page is
2253 * unmapped, so this will only try to map the rest of page, see
2254 * that it is unmapped (typically even will not look into inode -
2255 * ->i_size will be enough for everything) and zero it out.
2256 * OTOH it's obviously correct and should make the page up-to-date.
2258 if (!PageUptodate(page
)) {
2259 err
= mapping
->a_ops
->readpage(NULL
, page
);
2260 wait_on_page_locked(page
);
2264 page_cache_release(page
);
2267 mark_inode_dirty(inode
);
2271 page_cache_release(page
);
2276 struct inode_operations page_symlink_inode_operations
= {
2277 .readlink
= page_readlink
,
2278 .follow_link
= page_follow_link
,
2281 EXPORT_SYMBOL(__user_walk
);
2282 EXPORT_SYMBOL(follow_down
);
2283 EXPORT_SYMBOL(follow_up
);
2284 EXPORT_SYMBOL(get_write_access
); /* binfmt_aout */
2285 EXPORT_SYMBOL(getname
);
2286 EXPORT_SYMBOL(lock_rename
);
2287 EXPORT_SYMBOL(lookup_create
);
2288 EXPORT_SYMBOL(lookup_hash
);
2289 EXPORT_SYMBOL(lookup_one_len
);
2290 EXPORT_SYMBOL(page_follow_link
);
2291 EXPORT_SYMBOL(page_readlink
);
2292 EXPORT_SYMBOL(page_symlink
);
2293 EXPORT_SYMBOL(page_symlink_inode_operations
);
2294 EXPORT_SYMBOL(path_lookup
);
2295 EXPORT_SYMBOL(path_release
);
2296 EXPORT_SYMBOL(path_walk
);
2297 EXPORT_SYMBOL(permission
);
2298 EXPORT_SYMBOL(unlock_rename
);
2299 EXPORT_SYMBOL(vfs_create
);
2300 EXPORT_SYMBOL(vfs_follow_link
);
2301 EXPORT_SYMBOL(vfs_link
);
2302 EXPORT_SYMBOL(vfs_mkdir
);
2303 EXPORT_SYMBOL(vfs_mknod
);
2304 EXPORT_SYMBOL(vfs_permission
);
2305 EXPORT_SYMBOL(vfs_readlink
);
2306 EXPORT_SYMBOL(vfs_rename
);
2307 EXPORT_SYMBOL(vfs_rmdir
);
2308 EXPORT_SYMBOL(vfs_symlink
);
2309 EXPORT_SYMBOL(vfs_unlink
);