4 * Copyright (C) 1991, 1992 Linus Torvalds
8 * Some corrections by tytso.
11 /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
16 #include <linux/proc_fs.h>
17 #include <linux/smp_lock.h>
18 #include <linux/quotaops.h>
19 #include <linux/pagemap.h>
20 #include <linux/dcache.h>
22 #include <asm/uaccess.h>
23 #include <asm/unaligned.h>
24 #include <asm/semaphore.h>
26 #include <asm/pgtable.h>
28 #include <asm/namei.h>
30 /* This can be removed after the beta phase. */
31 #define CACHE_SUPERVISE /* debug the correctness of dcache entries */
32 #undef DEBUG /* some other debugging */
35 #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
37 /* [Feb-1997 T. Schoebel-Theuer]
38 * Fundamental changes in the pathname lookup mechanisms (namei)
39 * were necessary because of omirr. The reason is that omirr needs
40 * to know the _real_ pathname, not the user-supplied one, in case
41 * of symlinks (and also when transname replacements occur).
43 * The new code replaces the old recursive symlink resolution with
44 * an iterative one (in case of non-nested symlink chains). It does
45 * this with calls to <fs>_follow_link().
46 * As a side effect, dir_namei(), _namei() and follow_link() are now
47 * replaced with a single function lookup_dentry() that can handle all
48 * the special cases of the former code.
50 * With the new dcache, the pathname is stored at each inode, at least as
51 * long as the refcount of the inode is positive. As a side effect, the
52 * size of the dcache depends on the inode cache and thus is dynamic.
54 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
55 * resolution to correspond with current state of the code.
57 * Note that the symlink resolution is not *completely* iterative.
58 * There is still a significant amount of tail- and mid- recursion in
59 * the algorithm. Also, note that <fs>_readlink() is not used in
60 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
61 * may return different results than <fs>_follow_link(). Many virtual
62 * filesystems (including /proc) exhibit this behavior.
65 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
66 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
67 * and the name already exists in form of a symlink, try to create the new
68 * name indicated by the symlink. The old code always complained that the
69 * name already exists, due to not following the symlink even if its target
70 * is nonexistent. The new semantics affects also mknod() and link() when
71 * the name is a symlink pointing to a non-existant name.
73 * I don't know which semantics is the right one, since I have no access
74 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
75 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
76 * "old" one. Personally, I think the new semantics is much more logical.
77 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
78 * file does succeed in both HP-UX and SunOs, but not in Solaris
79 * and in the old Linux semantics.
82 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
83 * semantics. See the comments in "open_namei" and "do_link" below.
85 * [10-Sep-98 Alan Modra] Another symlink change.
88 /* In order to reduce some races, while at the same time doing additional
89 * checking and hopefully speeding things up, we copy filenames to the
90 * kernel data space before using them..
92 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
94 static inline int do_getname(const char *filename
, char *page
)
97 unsigned long len
= PAGE_SIZE
;
99 if ((unsigned long) filename
>= TASK_SIZE
) {
100 if (!segment_eq(get_fs(), KERNEL_DS
))
102 } else if (TASK_SIZE
- (unsigned long) filename
< PAGE_SIZE
)
103 len
= TASK_SIZE
- (unsigned long) filename
;
105 retval
= strncpy_from_user((char *)page
, filename
, len
);
109 return -ENAMETOOLONG
;
115 char * getname(const char * filename
)
119 result
= ERR_PTR(-ENOMEM
);
122 int retval
= do_getname(filename
, tmp
);
127 result
= ERR_PTR(retval
);
136 * is used to check for read/write/execute permissions on a file.
137 * We use "fsuid" for this, letting us set arbitrary permissions
138 * for filesystem access without changing the "normal" uids which
139 * are used for other things..
141 int permission(struct inode
* inode
,int mask
)
143 int mode
= inode
->i_mode
;
145 if (inode
->i_op
&& inode
->i_op
->permission
)
146 return inode
->i_op
->permission(inode
, mask
);
147 else if ((mask
& S_IWOTH
) && IS_RDONLY(inode
) &&
148 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
)))
149 return -EROFS
; /* Nobody gets write access to a read-only fs */
150 else if ((mask
& S_IWOTH
) && IS_IMMUTABLE(inode
))
151 return -EACCES
; /* Nobody gets write access to an immutable file */
152 else if (current
->fsuid
== inode
->i_uid
)
154 else if (in_group_p(inode
->i_gid
))
156 if (((mode
& mask
& S_IRWXO
) == mask
) || capable(CAP_DAC_OVERRIDE
))
158 /* read and search access */
159 if ((mask
== S_IROTH
) ||
160 (S_ISDIR(mode
) && !(mask
& ~(S_IROTH
| S_IXOTH
))))
161 if (capable(CAP_DAC_READ_SEARCH
))
167 * get_write_access() gets write permission for a file.
168 * put_write_access() releases this write permission.
169 * This is used for regular files.
170 * We cannot support write (and maybe mmap read-write shared) accesses and
171 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
172 * can have the following values:
173 * 0: no writers, no VM_DENYWRITE mappings
174 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
175 * > 0: (i_writecount) users are writing to the file.
177 * WARNING: as soon as we will move get_write_access(), do_mmap() or
178 * prepare_binfmt() out of the big lock we will need a spinlock protecting
179 * the checks in all 3. For the time being it is not needed.
181 int get_write_access(struct inode
* inode
)
183 if (atomic_read(&inode
->i_writecount
) < 0)
185 atomic_inc(&inode
->i_writecount
);
189 void put_write_access(struct inode
* inode
)
191 atomic_dec(&inode
->i_writecount
);
195 * Internal lookup() using the new generic dcache.
197 static struct dentry
* cached_lookup(struct dentry
* parent
, struct qstr
* name
, int flags
)
199 struct dentry
* dentry
= d_lookup(parent
, name
);
201 if (dentry
&& dentry
->d_op
&& dentry
->d_op
->d_revalidate
) {
202 if (!dentry
->d_op
->d_revalidate(dentry
, flags
) && !d_invalidate(dentry
)) {
211 * This is called when everything else fails, and we actually have
212 * to go to the low-level filesystem to find out what we should do..
214 * We get the directory semaphore, and after getting that we also
215 * make sure that nobody added the entry to the dcache in the meantime..
217 static struct dentry
* real_lookup(struct dentry
* parent
, struct qstr
* name
, int flags
)
219 struct dentry
* result
;
220 struct inode
*dir
= parent
->d_inode
;
224 * First re-do the cached lookup just in case it was created
225 * while we waited for the directory semaphore..
227 * FIXME! This could use version numbering or similar to
228 * avoid unnecessary cache lookups.
230 result
= d_lookup(parent
, name
);
232 struct dentry
* dentry
= d_alloc(parent
, name
);
233 result
= ERR_PTR(-ENOMEM
);
235 result
= dir
->i_op
->lookup(dir
, dentry
);
246 * Uhhuh! Nasty case: the cache was re-populated while
247 * we waited on the semaphore. Need to revalidate, but
248 * we're going to return this entry regardless (same
249 * as if it was busy).
252 if (result
->d_op
&& result
->d_op
->d_revalidate
)
253 result
->d_op
->d_revalidate(result
, flags
);
257 static inline int do_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
260 if (current
->link_count
>= 32)
262 current
->link_count
++;
263 UPDATE_ATIME(dentry
->d_inode
);
264 err
= dentry
->d_inode
->i_op
->follow_link(dentry
, nd
);
265 current
->link_count
--;
273 static inline int follow_down(struct dentry
** dentry
, struct vfsmount
**mnt
)
275 struct dentry
* parent
= dget((*dentry
)->d_mounts
);
284 * This is the basic name resolution function, turning a pathname
285 * into the final dentry.
287 * We expect 'base' to be positive and a directory.
289 int walk_name(const char * name
, unsigned lookup_flags
, struct nameidata
*nd
)
291 struct dentry
*dentry
;
300 inode
= nd
->dentry
->d_inode
;
301 if (current
->link_count
)
302 lookup_flags
= LOOKUP_FOLLOW
;
304 lookup_flags
&= LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
|
305 LOOKUP_SLASHOK
| LOOKUP_POSITIVE
| LOOKUP_PARENT
;
307 /* At this point we know we have a real path component. */
313 err
= permission(inode
, MAY_EXEC
);
314 dentry
= ERR_PTR(err
);
319 c
= *(const unsigned char *)name
;
321 hash
= init_name_hash();
324 hash
= partial_name_hash(c
, hash
);
325 c
= *(const unsigned char *)name
;
326 } while (c
&& (c
!= '/'));
327 this.len
= name
- (const char *) this.name
;
328 this.hash
= end_name_hash(hash
);
330 /* remove trailing slashes? */
333 while (*++name
== '/');
335 goto last_with_slashes
;
338 * "." and ".." are special - ".." especially so because it has
339 * to be able to know about the current root directory and
340 * parent relationships.
342 if (this.name
[0] == '.') switch (this.len
) {
346 if (this.name
[1] != '.')
348 if (nd
->dentry
!= current
->fs
->root
) {
349 dentry
= dget(nd
->dentry
->d_covers
->d_parent
);
352 inode
= dentry
->d_inode
;
359 * See if the low-level filesystem might want
360 * to use its own hash..
362 if (nd
->dentry
->d_op
&& nd
->dentry
->d_op
->d_hash
) {
363 err
= nd
->dentry
->d_op
->d_hash(nd
->dentry
, &this);
367 /* This does the actual lookups.. */
368 dentry
= cached_lookup(nd
->dentry
, &this, LOOKUP_CONTINUE
);
370 dentry
= real_lookup(nd
->dentry
, &this, LOOKUP_CONTINUE
);
371 err
= PTR_ERR(dentry
);
375 /* Check mountpoints.. */
376 while (d_mountpoint(dentry
) && follow_down(&dentry
, &nd
->mnt
))
380 inode
= dentry
->d_inode
;
387 if (inode
->i_op
->follow_link
) {
388 err
= do_follow_link(dentry
, nd
);
393 inode
= nd
->dentry
->d_inode
;
404 if (!inode
->i_op
->lookup
)
407 /* here ends the main loop */
410 lookup_flags
|= LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
;
412 if (lookup_flags
& LOOKUP_PARENT
)
414 if (this.name
[0] == '.') switch (this.len
) {
418 if (this.name
[1] != '.')
420 if (nd
->dentry
!= current
->fs
->root
) {
421 dentry
= dget(nd
->dentry
->d_covers
->d_parent
);
424 inode
= dentry
->d_inode
;
430 if (nd
->dentry
->d_op
&& nd
->dentry
->d_op
->d_hash
) {
431 err
= nd
->dentry
->d_op
->d_hash(nd
->dentry
, &this);
435 dentry
= cached_lookup(nd
->dentry
, &this, 0);
437 dentry
= real_lookup(nd
->dentry
, &this, 0);
438 err
= PTR_ERR(dentry
);
442 while (d_mountpoint(dentry
) && follow_down(&dentry
, &nd
->mnt
))
444 inode
= dentry
->d_inode
;
445 if ((lookup_flags
& LOOKUP_FOLLOW
)
446 && inode
&& inode
->i_op
&& inode
->i_op
->follow_link
) {
447 err
= do_follow_link(dentry
, nd
);
451 inode
= nd
->dentry
->d_inode
;
459 if (lookup_flags
& LOOKUP_DIRECTORY
) {
461 if (!inode
->i_op
|| !inode
->i_op
->lookup
)
467 if (lookup_flags
& LOOKUP_POSITIVE
)
469 if (lookup_flags
& LOOKUP_DIRECTORY
)
470 if (!(lookup_flags
& LOOKUP_SLASHOK
))
484 /* returns 1 if everything is done */
485 static int __emul_lookup_dentry(const char *name
, int lookup_flags
,
486 struct nameidata
*nd
)
488 char *emul
= __emul_prefix();
493 nd
->mnt
= mntget(current
->fs
->rootmnt
);
494 nd
->dentry
= dget(current
->fs
->root
);
495 if (walk_name(emul
,LOOKUP_FOLLOW
|LOOKUP_DIRECTORY
|LOOKUP_POSITIVE
,nd
))
497 if (walk_name(name
, lookup_flags
, nd
))
500 if (!nd
->dentry
->d_inode
) {
501 struct nameidata nd_root
;
502 nd_root
.last
.len
= 0;
503 nd_root
.mnt
= mntget(current
->fs
->rootmnt
);
504 nd_root
.dentry
= dget(current
->fs
->root
);
505 if (walk_name(name
, lookup_flags
, &nd_root
))
507 if (nd_root
.dentry
->d_inode
) {
510 nd
->dentry
= nd_root
.dentry
;
511 nd
->mnt
= nd_root
.mnt
;
512 nd
->last
= nd_root
.last
;
515 dput(nd_root
.dentry
);
522 walk_init_root(const char *name
, unsigned flags
, struct nameidata
*nd
)
524 if (current
->personality
!= PER_LINUX
)
525 if (__emul_lookup_dentry(name
,flags
,nd
));
527 nd
->mnt
= mntget(current
->fs
->rootmnt
);
528 nd
->dentry
= dget(current
->fs
->root
);
532 int walk_init(const char *name
,unsigned int flags
,struct nameidata
*nd
)
536 return walk_init_root(name
,flags
,nd
);
537 nd
->mnt
= mntget(current
->fs
->pwdmnt
);
538 nd
->dentry
= dget(current
->fs
->pwd
);
542 struct dentry
* lookup_dentry(const char * name
, unsigned int lookup_flags
)
547 if (walk_init(name
, lookup_flags
, &nd
))
548 err
= walk_name(name
, lookup_flags
, &nd
);
557 * Restricted form of lookup. Doesn't follow links, single-component only,
558 * needs parent already locked. Doesn't follow mounts.
560 static inline struct dentry
* lookup_hash(struct qstr
*name
, struct dentry
* base
)
562 struct dentry
* dentry
;
566 inode
= base
->d_inode
;
567 err
= permission(inode
, MAY_EXEC
);
568 dentry
= ERR_PTR(err
);
573 * See if the low-level filesystem might want
574 * to use its own hash..
576 if (base
->d_op
&& base
->d_op
->d_hash
) {
577 err
= base
->d_op
->d_hash(base
, name
);
578 dentry
= ERR_PTR(err
);
583 dentry
= cached_lookup(base
, name
, 0);
585 struct dentry
*new = d_alloc(base
, name
);
586 dentry
= ERR_PTR(-ENOMEM
);
589 dentry
= inode
->i_op
->lookup(inode
, new);
604 struct dentry
* lookup_one(const char * name
, struct dentry
* base
)
611 c
= *(const unsigned char *)name
;
615 hash
= init_name_hash();
620 hash
= partial_name_hash(c
, hash
);
621 c
= *(const unsigned char *)name
;
623 this.len
= name
- (const char *) this.name
;
624 this.hash
= end_name_hash(hash
);
626 return lookup_hash(&this, base
);
628 return ERR_PTR(-EACCES
);
634 * is used by most simple commands to get the inode of a specified name.
635 * Open, link etc use their own routines, but this is enough for things
638 * namei exists in two versions: namei/lnamei. The only difference is
639 * that namei follows links, while lnamei does not.
641 struct dentry
* __namei(const char *pathname
, unsigned int lookup_flags
)
644 struct dentry
*dentry
;
646 name
= getname(pathname
);
647 dentry
= (struct dentry
*) name
;
649 dentry
= lookup_dentry(name
,lookup_flags
|LOOKUP_POSITIVE
);
656 * It's inline, so penalty for filesystems that don't use sticky bit is
659 static inline int check_sticky(struct inode
*dir
, struct inode
*inode
)
661 if (!(dir
->i_mode
& S_ISVTX
))
663 if (inode
->i_uid
== current
->fsuid
)
665 if (dir
->i_uid
== current
->fsuid
)
667 return !capable(CAP_FOWNER
);
671 * Check whether we can remove a link victim from directory dir, check
672 * whether the type of victim is right.
673 * 1. We can't do it if dir is read-only (done in permission())
674 * 2. We should have write and exec permissions on dir
675 * 3. We can't remove anything from append-only dir
676 * 4. We can't do anything with immutable dir (done in permission())
677 * 5. If the sticky bit on dir is set we should either
678 * a. be owner of dir, or
679 * b. be owner of victim, or
680 * c. have CAP_FOWNER capability
681 * 6. If the victim is append-only or immutable we can't do antyhing with
682 * links pointing to it.
683 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
684 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
685 * 9. We can't remove a root or mountpoint.
687 static inline int may_delete(struct inode
*dir
,struct dentry
*victim
, int isdir
)
690 if (!victim
->d_inode
|| victim
->d_parent
->d_inode
!= dir
)
692 error
= permission(dir
,MAY_WRITE
| MAY_EXEC
);
697 if (check_sticky(dir
, victim
->d_inode
)||IS_APPEND(victim
->d_inode
)||
698 IS_IMMUTABLE(victim
->d_inode
))
701 if (!S_ISDIR(victim
->d_inode
->i_mode
))
705 if (d_mountpoint(victim
))
707 } else if (S_ISDIR(victim
->d_inode
->i_mode
))
712 /* Check whether we can create an object with dentry child in directory
714 * 1. We can't do it if child already exists (open has special treatment for
715 * this case, but since we are inlined it's OK)
716 * 2. We can't do it if dir is read-only (done in permission())
717 * 3. We should have write and exec permissions on dir
718 * 4. We can't do it if dir is immutable (done in permission())
720 static inline int may_create(struct inode
*dir
, struct dentry
*child
) {
723 return permission(dir
,MAY_WRITE
| MAY_EXEC
);
727 * Special case: O_CREAT|O_EXCL implies O_NOFOLLOW for security
730 * O_DIRECTORY translates into forcing a directory lookup.
732 static inline int lookup_flags(unsigned int f
)
734 unsigned long retval
= LOOKUP_FOLLOW
;
737 retval
&= ~LOOKUP_FOLLOW
;
739 if ((f
& (O_CREAT
|O_EXCL
)) == (O_CREAT
|O_EXCL
))
740 retval
&= ~LOOKUP_FOLLOW
;
743 retval
|= LOOKUP_DIRECTORY
;
748 int vfs_create(struct inode
*dir
, struct dentry
*dentry
, int mode
)
752 mode
&= S_IALLUGO
& ~current
->fs
->umask
;
755 down(&dir
->i_zombie
);
756 error
= may_create(dir
, dentry
);
760 error
= -EACCES
; /* shouldn't it be ENOSYS? */
761 if (!dir
->i_op
|| !dir
->i_op
->create
)
765 error
= dir
->i_op
->create(dir
, dentry
, mode
);
774 * namei for open - this is in fact almost the whole open-routine.
776 * Note that the low bits of "flag" aren't the same as in the open
777 * system call - they are 00 - no permissions needed
778 * 01 - read permission needed
779 * 10 - write permission needed
780 * 11 - read/write permissions needed
781 * which is a lot more logical, and also allows the "no perm" needed
782 * for symlinks (where the permissions are checked later).
784 int open_namei(const char * pathname
, int flag
, int mode
, struct nameidata
*nd
)
786 int acc_mode
, error
= 0;
788 struct dentry
*dentry
;
790 acc_mode
= ACC_MODE(flag
);
791 if (!(flag
& O_CREAT
)) {
792 if (walk_init(pathname
, lookup_flags(flag
), nd
))
793 error
= walk_name(pathname
, lookup_flags(flag
), nd
);
801 if (walk_init(pathname
, LOOKUP_PARENT
, nd
))
802 error
= walk_name(pathname
, LOOKUP_PARENT
, nd
);
806 * It's not obvious that open(".", O_CREAT, foo) should
807 * fail, but it's even less obvious that it should succeed.
808 * Since O_CREAT means an intention to create the thing and
809 * open(2) had never created directories, count it as caller's
810 * luserdom and let him sod off - -EISDIR it is.
813 if (!nd
->last
.len
|| (nd
->last
.name
[0] == '.' &&
814 (nd
->last
.len
== 1 ||
815 (nd
->last
.name
[1] == '.' && nd
->last
.len
== 2))))
818 if (nd
->last
.name
[nd
->last
.len
])
821 dir
= dget(nd
->dentry
);
822 down(&dir
->d_inode
->i_sem
);
824 dentry
= lookup_hash(&nd
->last
, dget(nd
->dentry
));
825 error
= PTR_ERR(dentry
);
826 if (IS_ERR(dentry
)) {
827 up(&dir
->d_inode
->i_sem
);
832 if (dentry
->d_inode
) {
833 up(&dir
->d_inode
->i_sem
);
838 if (dentry
->d_inode
->i_op
&&
839 dentry
->d_inode
->i_op
->follow_link
) {
841 * With O_EXCL it would be -EEXIST.
842 * If symlink is a dangling one it's -ENOENT.
843 * Otherwise we open the object it points to.
845 error
= do_follow_link(dentry
, nd
);
855 if (dentry
->d_inode
&& S_ISDIR(dentry
->d_inode
->i_mode
))
858 error
= vfs_create(dir
->d_inode
, dentry
, mode
);
859 /* Don't check for write permission, don't truncate */
871 inode
= dentry
->d_inode
;
876 if (S_ISLNK(inode
->i_mode
))
880 if (S_ISDIR(inode
->i_mode
) && (flag
& FMODE_WRITE
))
883 error
= permission(inode
,acc_mode
);
888 * FIFO's, sockets and device files are special: they don't
889 * actually live on the filesystem itself, and as such you
890 * can write to them even if the filesystem is read-only.
892 if (S_ISFIFO(inode
->i_mode
) || S_ISSOCK(inode
->i_mode
)) {
894 } else if (S_ISBLK(inode
->i_mode
) || S_ISCHR(inode
->i_mode
)) {
902 if (IS_RDONLY(inode
) && (flag
& 2))
906 * An append-only file must be opened in append mode for writing.
909 if (IS_APPEND(inode
)) {
910 if ((flag
& FMODE_WRITE
) && !(flag
& O_APPEND
))
916 if (flag
& O_TRUNC
) {
917 error
= get_write_access(inode
);
922 * Refuse to truncate files with mandatory locks held on them.
924 error
= locks_verify_locked(inode
);
928 error
= do_truncate(dentry
, 0);
930 put_write_access(inode
);
934 if (flag
& FMODE_WRITE
)
945 static struct dentry
*lookup_create(const char *name
, int is_dir
)
948 struct dentry
*dentry
;
950 if (walk_init(name
, LOOKUP_PARENT
, &nd
))
951 err
= walk_name(name
, LOOKUP_PARENT
, &nd
);
952 dentry
= ERR_PTR(err
);
955 down(&nd
.dentry
->d_inode
->i_sem
);
956 dentry
= ERR_PTR(-EEXIST
);
957 if (!nd
.last
.len
|| (nd
.last
.name
[0] == '.' &&
958 (nd
.last
.len
== 1 || (nd
.last
.name
[1] == '.' && nd
.last
.len
== 2))))
960 dentry
= lookup_hash(&nd
.last
, dget(nd
.dentry
));
963 if (!is_dir
&& nd
.last
.name
[nd
.last
.len
] && !dentry
->d_inode
)
972 dentry
= ERR_PTR(-ENOENT
);
974 up(&nd
.dentry
->d_inode
->i_sem
);
978 int vfs_mknod(struct inode
*dir
, struct dentry
*dentry
, int mode
, dev_t dev
)
982 mode
&= ~current
->fs
->umask
;
984 down(&dir
->i_zombie
);
985 if ((S_ISCHR(mode
) || S_ISBLK(mode
)) && !capable(CAP_MKNOD
))
988 error
= may_create(dir
, dentry
);
993 if (!dir
->i_op
|| !dir
->i_op
->mknod
)
997 error
= dir
->i_op
->mknod(dir
, dentry
, mode
, dev
);
1003 struct dentry
* do_mknod(const char * filename
, int mode
, dev_t dev
)
1007 struct dentry
*dentry
, *retval
;
1009 dentry
= lookup_create(filename
, 0);
1013 dir
= dget(dentry
->d_parent
);
1015 error
= vfs_mknod(dir
->d_inode
, dentry
, mode
, dev
);
1017 retval
= ERR_PTR(error
);
1019 retval
= dget(dentry
);
1025 asmlinkage
long sys_mknod(const char * filename
, int mode
, dev_t dev
)
1029 struct dentry
* dentry
, *dir
;
1033 tmp
= getname(filename
);
1035 return PTR_ERR(tmp
);
1038 dentry
= lookup_create(tmp
, 0);
1039 error
= PTR_ERR(dentry
);
1042 dir
= dget(dentry
->d_parent
);
1043 switch (mode
& S_IFMT
) {
1044 case 0: case S_IFREG
:
1045 error
= vfs_create(dir
->d_inode
, dentry
, mode
);
1047 case S_IFCHR
: case S_IFBLK
: case S_IFIFO
: case S_IFSOCK
:
1048 error
= vfs_mknod(dir
->d_inode
, dentry
, mode
, dev
);
1065 int vfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
1069 down(&dir
->i_zombie
);
1070 error
= may_create(dir
, dentry
);
1075 if (!dir
->i_op
|| !dir
->i_op
->mkdir
)
1079 mode
&= (S_IRWXUGO
|S_ISVTX
) & ~current
->fs
->umask
;
1080 error
= dir
->i_op
->mkdir(dir
, dentry
, mode
);
1087 asmlinkage
long sys_mkdir(const char * pathname
, int mode
)
1092 tmp
= getname(pathname
);
1093 error
= PTR_ERR(tmp
);
1096 struct dentry
*dentry
;
1099 dentry
= lookup_create(tmp
, 1);
1100 error
= PTR_ERR(dentry
);
1101 if (!IS_ERR(dentry
)) {
1102 dir
= dget(dentry
->d_parent
);
1103 error
= vfs_mkdir(dir
->d_inode
, dentry
, mode
);
1115 * We try to drop the dentry early: we should have
1116 * a usage count of 2 if we're the only user of this
1117 * dentry, and if that is true (possibly after pruning
1118 * the dcache), then we drop the dentry now.
1120 * A low-level filesystem can, if it choses, legally
1123 * if (!d_unhashed(dentry))
1126 * if it cannot handle the case of removing a directory
1127 * that is still in use by something else..
1129 static void d_unhash(struct dentry
*dentry
)
1132 switch (dentry
->d_count
) {
1134 shrink_dcache_parent(dentry
);
1135 if (dentry
->d_count
!= 2)
1142 int vfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
1146 error
= may_delete(dir
, dentry
, 1);
1150 if (!dir
->i_op
|| !dir
->i_op
->rmdir
)
1155 double_down(&dir
->i_zombie
, &dentry
->d_inode
->i_zombie
);
1157 error
= dir
->i_op
->rmdir(dir
, dentry
);
1158 double_up(&dir
->i_zombie
, &dentry
->d_inode
->i_zombie
);
1164 static inline int do_rmdir(const char * name
)
1168 struct dentry
*dentry
;
1170 dentry
= lookup_dentry(name
, LOOKUP_POSITIVE
);
1171 error
= PTR_ERR(dentry
);
1175 dir
= lock_parent(dentry
);
1177 if (check_parent(dir
, dentry
))
1178 error
= vfs_rmdir(dir
->d_inode
, dentry
);
1185 asmlinkage
long sys_rmdir(const char * pathname
)
1190 tmp
= getname(pathname
);
1192 return PTR_ERR(tmp
);
1194 error
= do_rmdir(tmp
);
1202 int vfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
1206 down(&dir
->i_zombie
);
1207 error
= may_delete(dir
, dentry
, 0);
1210 if (dir
->i_op
&& dir
->i_op
->unlink
) {
1212 error
= dir
->i_op
->unlink(dir
, dentry
);
1219 static int do_unlink(const char * name
)
1223 struct dentry
*dentry
;
1225 dentry
= lookup_dentry(name
, LOOKUP_POSITIVE
);
1226 error
= PTR_ERR(dentry
);
1230 dir
= lock_parent(dentry
);
1232 if (check_parent(dir
, dentry
))
1233 error
= vfs_unlink(dir
->d_inode
, dentry
);
1241 asmlinkage
long sys_unlink(const char * pathname
)
1246 tmp
= getname(pathname
);
1248 return PTR_ERR(tmp
);
1250 error
= do_unlink(tmp
);
1257 int vfs_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *oldname
)
1261 down(&dir
->i_zombie
);
1262 error
= may_create(dir
, dentry
);
1267 if (!dir
->i_op
|| !dir
->i_op
->symlink
)
1271 error
= dir
->i_op
->symlink(dir
, dentry
, oldname
);
1278 asmlinkage
long sys_symlink(const char * oldname
, const char * newname
)
1284 from
= getname(oldname
);
1286 return PTR_ERR(from
);
1287 to
= getname(newname
);
1288 error
= PTR_ERR(to
);
1291 struct dentry
*dentry
;
1294 dentry
= lookup_create(to
, 0);
1295 error
= PTR_ERR(dentry
);
1296 if (!IS_ERR(dentry
)) {
1297 dir
= dget(dentry
->d_parent
);
1298 error
= vfs_symlink(dir
->d_inode
, dentry
, from
);
1309 int vfs_link(struct dentry
*old_dentry
, struct inode
*dir
, struct dentry
*new_dentry
)
1311 struct inode
*inode
;
1314 down(&dir
->i_zombie
);
1316 inode
= old_dentry
->d_inode
;
1320 error
= may_create(dir
, new_dentry
);
1325 if (dir
->i_dev
!= inode
->i_dev
)
1329 * A link to an append-only or immutable file cannot be created.
1332 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
1334 if (!dir
->i_op
|| !dir
->i_op
->link
)
1338 error
= dir
->i_op
->link(old_dentry
, dir
, new_dentry
);
1346 * Hardlinks are often used in delicate situations. We avoid
1347 * security-related surprises by not following symlinks on the
1350 * We don't follow them on the oldname either to be compatible
1351 * with linux 2.0, and to avoid hard-linking to directories
1352 * and other special files. --ADM
1354 asmlinkage
long sys_link(const char * oldname
, const char * newname
)
1360 from
= getname(oldname
);
1362 return PTR_ERR(from
);
1363 to
= getname(newname
);
1364 error
= PTR_ERR(to
);
1366 struct dentry
*old_dentry
, *new_dentry
, *dir
;
1369 old_dentry
= lookup_dentry(from
, LOOKUP_POSITIVE
);
1370 error
= PTR_ERR(old_dentry
);
1371 if (IS_ERR(old_dentry
))
1374 new_dentry
= lookup_create(to
, 0);
1375 error
= PTR_ERR(new_dentry
);
1376 if (!IS_ERR(new_dentry
)) {
1377 dir
= dget(new_dentry
->d_parent
);
1378 error
= vfs_link(old_dentry
, dir
->d_inode
, new_dentry
);
1393 * The worst of all namespace operations - renaming directory. "Perverted"
1394 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
1396 * a) we can get into loop creation. Check is done in is_subdir().
1397 * b) race potential - two innocent renames can create a loop together.
1398 * That's where 4.4 screws up. Current fix: serialization on
1399 * sb->s_vfs_rename_sem. We might be more accurate, but that's another
1401 * c) we have to lock _three_ objects - parents and victim (if it exists).
1402 * And that - after we got ->i_sem on parents (until then we don't know
1403 * whether the target exists at all, let alone whether it is a directory
1404 * or not). Solution: ->i_zombie. Taken only after ->i_sem. Always taken
1405 * on link creation/removal of any kind. And taken (without ->i_sem) on
1406 * directory that will be removed (both in rmdir() and here).
1407 * d) some filesystems don't support opened-but-unlinked directories,
1408 * either because of layout or because they are not ready to deal with
1409 * all cases correctly. The latter will be fixed (taking this sort of
1410 * stuff into VFS), but the former is not going away. Solution: the same
1411 * trick as in rmdir().
1412 * e) conversion from fhandle to dentry may come in the wrong moment - when
1413 * we are removing the target. Solution: we will have to grab ->i_zombie
1414 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
1415 * ->i_sem on parents, which works but leads to some truely excessive
1418 int vfs_rename_dir(struct inode
*old_dir
, struct dentry
*old_dentry
,
1419 struct inode
*new_dir
, struct dentry
*new_dentry
)
1422 struct inode
*target
;
1424 if (old_dentry
->d_inode
== new_dentry
->d_inode
)
1427 error
= may_delete(old_dir
, old_dentry
, 1);
1431 if (new_dir
->i_dev
!= old_dir
->i_dev
)
1434 if (!new_dentry
->d_inode
)
1435 error
= may_create(new_dir
, new_dentry
);
1437 error
= may_delete(new_dir
, new_dentry
, 1);
1441 if (!old_dir
->i_op
|| !old_dir
->i_op
->rename
)
1445 * If we are going to change the parent - check write permissions,
1446 * we'll need to flip '..'.
1448 if (new_dir
!= old_dir
) {
1449 error
= permission(old_dentry
->d_inode
, MAY_WRITE
);
1454 DQUOT_INIT(old_dir
);
1455 DQUOT_INIT(new_dir
);
1456 down(&old_dir
->i_sb
->s_vfs_rename_sem
);
1458 if (is_subdir(new_dentry
, old_dentry
))
1460 target
= new_dentry
->d_inode
;
1461 if (target
) { /* Hastur! Hastur! Hastur! */
1462 triple_down(&old_dir
->i_zombie
,
1465 d_unhash(new_dentry
);
1467 double_down(&old_dir
->i_zombie
,
1468 &new_dir
->i_zombie
);
1469 error
= old_dir
->i_op
->rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
1471 triple_up(&old_dir
->i_zombie
,
1474 d_rehash(new_dentry
);
1477 double_up(&old_dir
->i_zombie
,
1478 &new_dir
->i_zombie
);
1481 d_move(old_dentry
,new_dentry
);
1483 up(&old_dir
->i_sb
->s_vfs_rename_sem
);
1487 int vfs_rename_other(struct inode
*old_dir
, struct dentry
*old_dentry
,
1488 struct inode
*new_dir
, struct dentry
*new_dentry
)
1492 if (old_dentry
->d_inode
== new_dentry
->d_inode
)
1495 error
= may_delete(old_dir
, old_dentry
, 0);
1499 if (new_dir
->i_dev
!= old_dir
->i_dev
)
1502 if (!new_dentry
->d_inode
)
1503 error
= may_create(new_dir
, new_dentry
);
1505 error
= may_delete(new_dir
, new_dentry
, 0);
1509 if (!old_dir
->i_op
|| !old_dir
->i_op
->rename
)
1512 DQUOT_INIT(old_dir
);
1513 DQUOT_INIT(new_dir
);
1514 double_down(&old_dir
->i_zombie
, &new_dir
->i_zombie
);
1515 error
= old_dir
->i_op
->rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
1516 double_up(&old_dir
->i_zombie
, &new_dir
->i_zombie
);
1519 /* The following d_move() should become unconditional */
1520 if (!(old_dir
->i_sb
->s_flags
& MS_ODD_RENAME
)) {
1521 d_move(old_dentry
, new_dentry
);
1526 int vfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1527 struct inode
*new_dir
, struct dentry
*new_dentry
)
1529 if (S_ISDIR(old_dentry
->d_inode
->i_mode
))
1530 return vfs_rename_dir(old_dir
,old_dentry
,new_dir
,new_dentry
);
1532 return vfs_rename_other(old_dir
,old_dentry
,new_dir
,new_dentry
);
1535 static inline int do_rename(const char * oldname
, const char * newname
)
1538 struct dentry
* old_dir
, * new_dir
;
1539 struct dentry
* old_dentry
, *new_dentry
;
1541 old_dentry
= lookup_dentry(oldname
, LOOKUP_POSITIVE
);
1543 error
= PTR_ERR(old_dentry
);
1544 if (IS_ERR(old_dentry
))
1548 unsigned int flags
= 0;
1549 if (S_ISDIR(old_dentry
->d_inode
->i_mode
))
1550 flags
= LOOKUP_SLASHOK
;
1551 new_dentry
= lookup_dentry(newname
, flags
);
1554 error
= PTR_ERR(new_dentry
);
1555 if (IS_ERR(new_dentry
))
1558 new_dir
= get_parent(new_dentry
);
1559 old_dir
= get_parent(old_dentry
);
1561 double_lock(new_dir
, old_dir
);
1564 if (check_parent(old_dir
, old_dentry
) && check_parent(new_dir
, new_dentry
))
1565 error
= vfs_rename(old_dir
->d_inode
, old_dentry
,
1566 new_dir
->d_inode
, new_dentry
);
1568 double_unlock(new_dir
, old_dir
);
1576 asmlinkage
long sys_rename(const char * oldname
, const char * newname
)
1582 from
= getname(oldname
);
1584 return PTR_ERR(from
);
1585 to
= getname(newname
);
1586 error
= PTR_ERR(to
);
1589 error
= do_rename(from
,to
);
1597 int vfs_readlink(struct dentry
*dentry
, char *buffer
, int buflen
, const char *link
)
1601 len
= PTR_ERR(link
);
1606 if (len
> (unsigned) buflen
)
1608 if (copy_to_user(buffer
, link
, len
))
1615 __vfs_follow_link(struct nameidata
*nd
, const char *link
)
1623 if (!walk_init_root(link
, LOOKUP_FOLLOW
, nd
))
1624 /* weird __emul_prefix() stuff did it */
1627 return walk_name(link
, LOOKUP_FOLLOW
, nd
);
1632 return PTR_ERR(link
);
1635 int vfs_follow_link(struct nameidata
*nd
, const char *link
)
1637 return __vfs_follow_link(nd
, link
);
1640 /* get the link contents into pagecache */
1641 static char *page_getlink(struct dentry
* dentry
, struct page
**ppage
)
1644 struct address_space
*mapping
= dentry
->d_inode
->i_mapping
;
1645 page
= read_cache_page(mapping
, 0, (filler_t
*)mapping
->a_ops
->readpage
,
1650 if (!Page_Uptodate(page
))
1653 return (char*) kmap(page
);
1656 page_cache_release(page
);
1657 return ERR_PTR(-EIO
);
1663 int page_readlink(struct dentry
*dentry
, char *buffer
, int buflen
)
1665 struct page
*page
= NULL
;
1666 char *s
= page_getlink(dentry
, &page
);
1667 int res
= vfs_readlink(dentry
,buffer
,buflen
,s
);
1670 page_cache_release(page
);
1675 int page_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1677 struct page
*page
= NULL
;
1678 char *s
= page_getlink(dentry
, &page
);
1679 int res
= __vfs_follow_link(nd
, s
);
1682 page_cache_release(page
);
1687 struct inode_operations page_symlink_inode_operations
= {
1688 readlink
: page_readlink
,
1689 follow_link
: page_follow_link
,