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>
20 #include <asm/uaccess.h>
21 #include <asm/unaligned.h>
22 #include <asm/semaphore.h>
24 #include <asm/pgtable.h>
26 #include <asm/namei.h>
28 /* This can be removed after the beta phase. */
29 #define CACHE_SUPERVISE /* debug the correctness of dcache entries */
30 #undef DEBUG /* some other debugging */
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 /* In order to reduce some races, while at the same time doing additional
87 * checking and hopefully speeding things up, we copy filenames to the
88 * kernel data space before using them..
90 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
92 static inline int do_getname(const char *filename
, char *page
)
95 unsigned long len
= PAGE_SIZE
;
97 if ((unsigned long) filename
>= TASK_SIZE
) {
98 if (!segment_eq(get_fs(), KERNEL_DS
))
100 } else if (TASK_SIZE
- (unsigned long) filename
< PAGE_SIZE
)
101 len
= TASK_SIZE
- (unsigned long) filename
;
103 retval
= strncpy_from_user((char *)page
, filename
, len
);
107 return -ENAMETOOLONG
;
113 char * getname(const char * filename
)
117 result
= ERR_PTR(-ENOMEM
);
120 int retval
= do_getname(filename
, tmp
);
125 result
= ERR_PTR(retval
);
134 * is used to check for read/write/execute permissions on a file.
135 * We use "fsuid" for this, letting us set arbitrary permissions
136 * for filesystem access without changing the "normal" uids which
137 * are used for other things..
139 int permission(struct inode
* inode
,int mask
)
141 int mode
= inode
->i_mode
;
143 if (inode
->i_op
&& inode
->i_op
->permission
)
144 return inode
->i_op
->permission(inode
, mask
);
145 else if ((mask
& S_IWOTH
) && IS_RDONLY(inode
) &&
146 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
)))
147 return -EROFS
; /* Nobody gets write access to a read-only fs */
148 else if ((mask
& S_IWOTH
) && IS_IMMUTABLE(inode
))
149 return -EACCES
; /* Nobody gets write access to an immutable file */
150 else if (current
->fsuid
== inode
->i_uid
)
152 else if (in_group_p(inode
->i_gid
))
154 if (((mode
& mask
& S_IRWXO
) == mask
) || capable(CAP_DAC_OVERRIDE
))
156 /* read and search access */
157 if ((mask
== S_IROTH
) ||
158 (S_ISDIR(mode
) && !(mask
& ~(S_IROTH
| S_IXOTH
))))
159 if (capable(CAP_DAC_READ_SEARCH
))
165 * get_write_access() gets write permission for a file.
166 * put_write_access() releases this write permission.
167 * This is used for regular files.
168 * We cannot support write (and maybe mmap read-write shared) accesses and
169 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
170 * can have the following values:
171 * 0: no writers, no VM_DENYWRITE mappings
172 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
173 * > 0: (i_writecount) users are writing to the file.
175 int get_write_access(struct inode
* inode
)
177 if (inode
->i_writecount
< 0)
179 inode
->i_writecount
++;
183 void put_write_access(struct inode
* inode
)
185 inode
->i_writecount
--;
189 * "." and ".." are special - ".." especially so because it has to be able
190 * to know about the current root directory and parent relationships
192 static struct dentry
* reserved_lookup(struct dentry
* parent
, struct qstr
* name
)
194 struct dentry
*result
= NULL
;
195 if (name
->name
[0] == '.') {
200 if (name
->name
[1] != '.')
203 if (parent
!= current
->fs
->root
)
204 parent
= parent
->d_covers
->d_parent
;
214 * Internal lookup() using the new generic dcache.
216 static struct dentry
* cached_lookup(struct dentry
* parent
, struct qstr
* name
, int flags
)
218 struct dentry
* dentry
= d_lookup(parent
, name
);
220 if (dentry
&& dentry
->d_op
&& dentry
->d_op
->d_revalidate
) {
221 if (!dentry
->d_op
->d_revalidate(dentry
, flags
) && !d_invalidate(dentry
)) {
230 * This is called when everything else fails, and we actually have
231 * to go to the low-level filesystem to find out what we should do..
233 * We get the directory semaphore, and after getting that we also
234 * make sure that nobody added the entry to the dcache in the meantime..
236 static struct dentry
* real_lookup(struct dentry
* parent
, struct qstr
* name
, int flags
)
238 struct dentry
* result
;
239 struct inode
*dir
= parent
->d_inode
;
243 * First re-do the cached lookup just in case it was created
244 * while we waited for the directory semaphore..
246 * FIXME! This could use version numbering or similar to
247 * avoid unnecessary cache lookups.
249 result
= cached_lookup(parent
, name
, flags
);
251 struct dentry
* dentry
= d_alloc(parent
, name
);
252 result
= ERR_PTR(-ENOMEM
);
254 result
= dir
->i_op
->lookup(dir
, dentry
);
265 static struct dentry
* do_follow_link(struct dentry
*base
, struct dentry
*dentry
, unsigned int follow
)
267 struct inode
* inode
= dentry
->d_inode
;
269 if ((follow
& LOOKUP_FOLLOW
)
270 && inode
&& inode
->i_op
&& inode
->i_op
->follow_link
) {
271 if (current
->link_count
< 5) {
272 struct dentry
* result
;
274 current
->link_count
++;
275 /* This eats the base */
276 result
= inode
->i_op
->follow_link(dentry
, base
, follow
);
277 current
->link_count
--;
282 dentry
= ERR_PTR(-ELOOP
);
288 static inline struct dentry
* follow_mount(struct dentry
* dentry
)
290 struct dentry
* mnt
= dentry
->d_mounts
;
303 * This is the basic name resolution function, turning a pathname
304 * into the final dentry.
306 struct dentry
* lookup_dentry(const char * name
, struct dentry
* base
, unsigned int lookup_flags
)
308 struct dentry
* dentry
;
316 } while (*name
== '/');
317 __prefix_lookup_dentry(name
, lookup_flags
);
318 base
= dget(current
->fs
->root
);
320 base
= dget(current
->fs
->pwd
);
326 inode
= base
->d_inode
;
327 lookup_flags
&= LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
| LOOKUP_SLASHOK
;
329 /* At this point we know we have a real path component. */
337 err
= permission(inode
, MAY_EXEC
);
338 dentry
= ERR_PTR(err
);
343 c
= *(const unsigned char *)name
;
345 hash
= init_name_hash();
348 hash
= partial_name_hash(c
, hash
);
349 c
= *(const unsigned char *)name
;
350 } while (c
&& (c
!= '/'));
351 this.len
= name
- (const char *) this.name
;
352 this.hash
= end_name_hash(hash
);
354 /* remove trailing slashes? */
355 flags
= lookup_flags
;
359 flags
|= LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
;
362 } while (tmp
== '/');
364 flags
|= LOOKUP_CONTINUE
;
368 * See if the low-level filesystem might want
369 * to use its own hash..
371 if (base
->d_op
&& base
->d_op
->d_hash
) {
373 error
= base
->d_op
->d_hash(base
, &this);
375 dentry
= ERR_PTR(error
);
380 /* This does the actual lookups.. */
381 dentry
= reserved_lookup(base
, &this);
383 dentry
= cached_lookup(base
, &this, flags
);
385 dentry
= real_lookup(base
, &this, flags
);
391 /* Check mountpoints.. */
392 dentry
= follow_mount(dentry
);
394 base
= do_follow_link(base
, dentry
, flags
);
398 inode
= base
->d_inode
;
399 if (flags
& LOOKUP_DIRECTORY
) {
402 dentry
= ERR_PTR(-ENOTDIR
);
403 if (!inode
->i_op
|| !inode
->i_op
->lookup
)
405 if (flags
& LOOKUP_CONTINUE
)
411 * The case of a nonexisting file is special.
413 * In the middle of a pathname lookup (ie when
414 * LOOKUP_CONTINUE is set), it's an obvious
415 * error and returns ENOENT.
417 * At the end of a pathname lookup it's legal,
418 * and we return a negative dentry. However, we
419 * get here only if there were trailing slashes,
420 * which is legal only if we know it's supposed
421 * to be a directory (ie "mkdir"). Thus the
422 * LOOKUP_SLASHOK flag.
425 dentry
= ERR_PTR(-ENOENT
);
426 if (flags
& LOOKUP_CONTINUE
)
428 if (flags
& LOOKUP_SLASHOK
)
439 * is used by most simple commands to get the inode of a specified name.
440 * Open, link etc use their own routines, but this is enough for things
443 * namei exists in two versions: namei/lnamei. The only difference is
444 * that namei follows links, while lnamei does not.
446 struct dentry
* __namei(const char *pathname
, unsigned int lookup_flags
)
449 struct dentry
*dentry
;
451 name
= getname(pathname
);
452 dentry
= (struct dentry
*) name
;
454 dentry
= lookup_dentry(name
, NULL
, lookup_flags
);
456 if (!IS_ERR(dentry
)) {
457 if (!dentry
->d_inode
) {
459 dentry
= ERR_PTR(-ENOENT
);
467 * It's inline, so penalty for filesystems that don't use sticky bit is
470 static inline int check_sticky(struct inode
*dir
, struct inode
*inode
)
472 if (!(dir
->i_mode
& S_ISVTX
))
474 if (inode
->i_uid
== current
->fsuid
)
476 if (dir
->i_uid
== current
->fsuid
)
478 return !capable(CAP_FOWNER
);
482 * Check whether we can remove a link victim from directory dir, check
483 * whether the type of victim is right.
484 * 1. We can't do it if dir is read-only (done in permission())
485 * 2. We should have write and exec permissions on dir
486 * 3. We can't remove anything from append-only dir
487 * 4. We can't do anything with immutable dir (done in permission())
488 * 5. If the sticky bit on dir is set we should either
489 * a. be owner of dir, or
490 * b. be owner of victim, or
491 * c. have CAP_FOWNER capability
492 * 6. If the victim is append-only or immutable we can't do antyhing with
493 * links pointing to it.
494 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
495 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
496 * 9. We can't remove a root or mountpoint.
498 static inline int may_delete(struct inode
*dir
,struct dentry
*victim
, int isdir
)
501 if (!victim
->d_inode
|| victim
->d_parent
->d_inode
!= dir
)
503 error
= permission(dir
,MAY_WRITE
| MAY_EXEC
);
508 if (check_sticky(dir
, victim
->d_inode
)||IS_APPEND(victim
->d_inode
)||
509 IS_IMMUTABLE(victim
->d_inode
))
512 if (!S_ISDIR(victim
->d_inode
->i_mode
))
516 if (victim
->d_mounts
!= victim
->d_covers
)
518 } else if (S_ISDIR(victim
->d_inode
->i_mode
))
523 /* Check whether we can create an object with dentry child in directory
525 * 1. We can't do it if child already exists (open has special treatment for
526 * this case, but since we are inlined it's OK)
527 * 2. We can't do it if dir is read-only (done in permission())
528 * 3. We should have write and exec permissions on dir
529 * 4. We can't do it if dir is immutable (done in permission())
531 static inline int may_create(struct inode
*dir
, struct dentry
*child
) {
534 return permission(dir
,MAY_WRITE
| MAY_EXEC
);
537 static inline struct dentry
*get_parent(struct dentry
*dentry
)
539 return dget(dentry
->d_parent
);
542 static inline void unlock_dir(struct dentry
*dir
)
544 up(&dir
->d_inode
->i_sem
);
549 * We need to do a check-parent every time
550 * after we have locked the parent - to verify
551 * that the parent is still our parent and
552 * that we are still hashed onto it..
554 * This is requied in case two processes race
555 * on removing (or moving) the same entry: the
556 * parent lock will serialize them, but the
557 * other process will be too late..
559 #define check_parent(dir, dentry) \
560 ((dir) == (dentry)->d_parent && !list_empty(&dentry->d_hash))
563 * Locking the parent is needed to:
564 * - serialize directory operations
565 * - make sure the parent doesn't change from
566 * under us in the middle of an operation.
568 * NOTE! Right now we'd rather use a "struct inode"
569 * for this, but as I expect things to move toward
570 * using dentries instead for most things it is
571 * probably better to start with the conceptually
572 * better interface of relying on a path of dentries.
574 static inline struct dentry
*lock_parent(struct dentry
*dentry
)
576 struct dentry
*dir
= dget(dentry
->d_parent
);
578 down(&dir
->d_inode
->i_sem
);
583 * Whee.. Deadlock country. Happily there are only two VFS
584 * operations that do this..
586 static inline void double_lock(struct dentry
*d1
, struct dentry
*d2
)
588 struct semaphore
*s1
= &d1
->d_inode
->i_sem
;
589 struct semaphore
*s2
= &d2
->d_inode
->i_sem
;
592 if ((unsigned long) s1
< (unsigned long) s2
) {
593 struct semaphore
*tmp
= s2
;
601 static inline void double_unlock(struct dentry
*d1
, struct dentry
*d2
)
603 struct semaphore
*s1
= &d1
->d_inode
->i_sem
;
604 struct semaphore
*s2
= &d2
->d_inode
->i_sem
;
615 * Special case: O_CREAT|O_EXCL implies O_NOFOLLOW for security
618 * O_DIRECTORY translates into forcing a directory lookup.
620 static inline int lookup_flags(unsigned int f
)
622 unsigned long retval
= LOOKUP_FOLLOW
;
625 retval
&= ~LOOKUP_FOLLOW
;
627 if ((f
& (O_CREAT
|O_EXCL
)) == (O_CREAT
|O_EXCL
))
628 retval
&= ~LOOKUP_FOLLOW
;
631 retval
|= LOOKUP_DIRECTORY
;
636 int vfs_create(struct inode
*dir
, struct dentry
*dentry
, int mode
)
640 error
= may_create(dir
, dentry
);
644 error
= -EACCES
; /* shouldn't it be ENOSYS? */
645 if (!dir
->i_op
|| !dir
->i_op
->create
)
649 error
= dir
->i_op
->create(dir
, dentry
, mode
);
657 * namei for open - this is in fact almost the whole open-routine.
659 * Note that the low bits of "flag" aren't the same as in the open
660 * system call - they are 00 - no permissions needed
661 * 01 - read permission needed
662 * 10 - write permission needed
663 * 11 - read/write permissions needed
664 * which is a lot more logical, and also allows the "no perm" needed
665 * for symlinks (where the permissions are checked later).
667 struct dentry
* open_namei(const char * pathname
, int flag
, int mode
)
671 struct dentry
*dentry
;
673 mode
&= S_IALLUGO
& ~current
->fs
->umask
;
676 dentry
= lookup_dentry(pathname
, NULL
, lookup_flags(flag
));
680 acc_mode
= ACC_MODE(flag
);
681 if (flag
& O_CREAT
) {
684 if (dentry
->d_inode
) {
685 if (!(flag
& O_EXCL
))
691 dir
= lock_parent(dentry
);
692 if (!check_parent(dir
, dentry
)) {
694 * Really nasty race happened. What's the
695 * right error code? We had a dentry, but
696 * before we could use it it was removed
697 * by somebody else. We could just re-try
698 * everything, I guess.
700 * ENOENT is definitely wrong.
708 * Somebody might have created the file while we
709 * waited for the directory lock.. So we have to
710 * re-do the existence test.
712 if (dentry
->d_inode
) {
717 error
= vfs_create(dir
->d_inode
, dentry
,mode
);
718 /* Don't check for write permission, don't truncate */
729 inode
= dentry
->d_inode
;
734 if (S_ISLNK(inode
->i_mode
))
738 if (S_ISDIR(inode
->i_mode
) && (flag
& FMODE_WRITE
))
741 error
= permission(inode
,acc_mode
);
746 * FIFO's, sockets and device files are special: they don't
747 * actually live on the filesystem itself, and as such you
748 * can write to them even if the filesystem is read-only.
750 if (S_ISFIFO(inode
->i_mode
) || S_ISSOCK(inode
->i_mode
)) {
752 } else if (S_ISBLK(inode
->i_mode
) || S_ISCHR(inode
->i_mode
)) {
760 if (IS_RDONLY(inode
) && (flag
& 2))
764 * An append-only file must be opened in append mode for writing.
767 if (IS_APPEND(inode
)) {
768 if ((flag
& FMODE_WRITE
) && !(flag
& O_APPEND
))
774 if (flag
& O_TRUNC
) {
775 error
= get_write_access(inode
);
780 * Refuse to truncate files with mandatory locks held on them.
782 error
= locks_verify_locked(inode
);
786 error
= do_truncate(dentry
, 0);
788 put_write_access(inode
);
792 if (flag
& FMODE_WRITE
)
799 return ERR_PTR(error
);
802 struct dentry
* do_mknod(const char * filename
, int mode
, dev_t dev
)
806 struct dentry
*dentry
, *retval
;
808 mode
&= ~current
->fs
->umask
;
809 dentry
= lookup_dentry(filename
, NULL
, LOOKUP_FOLLOW
);
813 dir
= lock_parent(dentry
);
815 if (!check_parent(dir
, dentry
))
818 error
= may_create(dir
->d_inode
, dentry
);
823 if (!dir
->d_inode
->i_op
|| !dir
->d_inode
->i_op
->mknod
)
826 DQUOT_INIT(dir
->d_inode
);
827 error
= dir
->d_inode
->i_op
->mknod(dir
->d_inode
, dentry
, mode
, dev
);
829 retval
= ERR_PTR(error
);
831 retval
= dget(dentry
);
837 asmlinkage
int sys_mknod(const char * filename
, int mode
, dev_t dev
)
841 struct dentry
* dentry
;
845 if (S_ISDIR(mode
) || (!S_ISFIFO(mode
) && !capable(CAP_SYS_ADMIN
)))
847 tmp
= getname(filename
);
848 error
= PTR_ERR(tmp
);
853 switch (mode
& S_IFMT
) {
855 mode
|= S_IFREG
; /* fallthrough */
857 mode
&= ~current
->fs
->umask
;
858 dentry
= lookup_dentry(filename
, NULL
, LOOKUP_FOLLOW
);
860 error
= PTR_ERR(dentry
);
862 struct dentry
*dir
= lock_parent(dentry
);
864 if (check_parent(dir
, dentry
))
865 error
= vfs_create(dir
->d_inode
, dentry
, mode
);
869 case S_IFCHR
: case S_IFBLK
: case S_IFIFO
: case S_IFSOCK
:
870 dentry
= do_mknod(tmp
,mode
,dev
);
871 error
= PTR_ERR(dentry
);
872 if (!IS_ERR(dentry
)) {
885 static inline int do_mkdir(const char * pathname
, int mode
)
889 struct dentry
*dentry
;
891 dentry
= lookup_dentry(pathname
, NULL
, LOOKUP_SLASHOK
);
892 error
= PTR_ERR(dentry
);
897 * EEXIST is kind of a strange error code to
898 * return, but basically if the dentry was moved
899 * or unlinked while we locked the parent, we
900 * do know that it _did_ exist before, and as
901 * such it makes perfect sense.. In contrast,
902 * ENOENT doesn't make sense for mkdir.
904 dir
= lock_parent(dentry
);
906 if (!check_parent(dir
, dentry
))
909 error
= may_create(dir
->d_inode
, dentry
);
914 if (!dir
->d_inode
->i_op
|| !dir
->d_inode
->i_op
->mkdir
)
917 DQUOT_INIT(dir
->d_inode
);
918 mode
&= (S_IRWXUGO
|S_ISVTX
) & ~current
->fs
->umask
;
919 error
= dir
->d_inode
->i_op
->mkdir(dir
->d_inode
, dentry
, mode
);
928 asmlinkage
int sys_mkdir(const char * pathname
, int mode
)
934 tmp
= getname(pathname
);
935 error
= PTR_ERR(tmp
);
937 error
= do_mkdir(tmp
,mode
);
944 int vfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
948 error
= may_delete(dir
, dentry
, 1);
952 if (!dir
->i_op
|| !dir
->i_op
->rmdir
)
958 * We try to drop the dentry early: we should have
959 * a usage count of 2 if we're the only user of this
960 * dentry, and if that is true (possibly after pruning
961 * the dcache), then we drop the dentry now.
963 * A low-level filesystem can, if it choses, legally
966 * if (!list_empty(&dentry->d_hash))
969 * if it cannot handle the case of removing a directory
970 * that is still in use by something else..
972 switch (dentry
->d_count
) {
974 shrink_dcache_parent(dentry
);
975 if (dentry
->d_count
!= 2)
981 error
= dir
->i_op
->rmdir(dir
, dentry
);
986 static inline int do_rmdir(const char * name
)
990 struct dentry
*dentry
;
992 dentry
= lookup_dentry(name
, NULL
, 0);
993 error
= PTR_ERR(dentry
);
998 if (!dentry
->d_inode
)
1001 dir
= dget(dentry
->d_parent
);
1004 * The dentry->d_count stuff confuses d_delete() enough to
1005 * not kill the inode from under us while it is locked. This
1006 * wouldn't be needed, except the dentry semaphore is really
1007 * in the inode, not in the dentry..
1010 double_lock(dir
, dentry
);
1013 if (check_parent(dir
, dentry
))
1014 error
= vfs_rmdir(dir
->d_inode
, dentry
);
1016 double_unlock(dentry
, dir
);
1023 asmlinkage
int sys_rmdir(const char * pathname
)
1029 tmp
= getname(pathname
);
1030 error
= PTR_ERR(tmp
);
1032 error
= do_rmdir(tmp
);
1039 int vfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
1043 error
= may_delete(dir
, dentry
, 0);
1046 if (dir
->i_op
&& dir
->i_op
->unlink
) {
1048 error
= dir
->i_op
->unlink(dir
, dentry
);
1054 static inline int do_unlink(const char * name
)
1058 struct dentry
*dentry
;
1060 dentry
= lookup_dentry(name
, NULL
, 0);
1061 error
= PTR_ERR(dentry
);
1065 dir
= lock_parent(dentry
);
1067 if (check_parent(dir
, dentry
))
1068 error
= vfs_unlink(dir
->d_inode
, dentry
);
1076 asmlinkage
int sys_unlink(const char * pathname
)
1082 tmp
= getname(pathname
);
1083 error
= PTR_ERR(tmp
);
1085 error
= do_unlink(tmp
);
1092 static inline int do_symlink(const char * oldname
, const char * newname
)
1096 struct dentry
*dentry
;
1098 dentry
= lookup_dentry(newname
, NULL
, 0);
1100 error
= PTR_ERR(dentry
);
1104 dir
= lock_parent(dentry
);
1106 if (!check_parent(dir
, dentry
))
1109 error
= may_create(dir
->d_inode
, dentry
);
1114 if (!dir
->d_inode
->i_op
|| !dir
->d_inode
->i_op
->symlink
)
1117 DQUOT_INIT(dir
->d_inode
);
1118 error
= dir
->d_inode
->i_op
->symlink(dir
->d_inode
, dentry
, oldname
);
1127 asmlinkage
int sys_symlink(const char * oldname
, const char * newname
)
1133 from
= getname(oldname
);
1134 error
= PTR_ERR(from
);
1135 if (!IS_ERR(from
)) {
1137 to
= getname(newname
);
1138 error
= PTR_ERR(to
);
1140 error
= do_symlink(from
,to
);
1149 static inline int do_link(const char * oldname
, const char * newname
)
1151 struct dentry
*old_dentry
, *new_dentry
, *dir
;
1152 struct inode
*inode
;
1156 * Hardlinks are often used in delicate situations. We avoid
1157 * security-related surprises by not following symlinks on the
1160 * We don't follow them on the oldname either to be compatible
1161 * with linux 2.0, and to avoid hard-linking to directories
1162 * and other special files. --ADM
1164 old_dentry
= lookup_dentry(oldname
, NULL
, 0);
1165 error
= PTR_ERR(old_dentry
);
1166 if (IS_ERR(old_dentry
))
1169 new_dentry
= lookup_dentry(newname
, NULL
, 0);
1170 error
= PTR_ERR(new_dentry
);
1171 if (IS_ERR(new_dentry
))
1174 dir
= lock_parent(new_dentry
);
1176 if (!check_parent(dir
, new_dentry
))
1180 inode
= old_dentry
->d_inode
;
1184 error
= may_create(dir
->d_inode
, new_dentry
);
1189 if (dir
->d_inode
->i_dev
!= inode
->i_dev
)
1193 * A link to an append-only or immutable file cannot be created.
1196 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
1200 if (!dir
->d_inode
->i_op
|| !dir
->d_inode
->i_op
->link
)
1203 DQUOT_INIT(dir
->d_inode
);
1204 error
= dir
->d_inode
->i_op
->link(old_dentry
, dir
->d_inode
, new_dentry
);
1215 asmlinkage
int sys_link(const char * oldname
, const char * newname
)
1221 from
= getname(oldname
);
1222 error
= PTR_ERR(from
);
1223 if (!IS_ERR(from
)) {
1225 to
= getname(newname
);
1226 error
= PTR_ERR(to
);
1228 error
= do_link(from
,to
);
1237 int vfs_rename_dir(struct inode
*old_dir
, struct dentry
*old_dentry
,
1238 struct inode
*new_dir
, struct dentry
*new_dentry
)
1241 int need_rehash
= 0;
1243 if (old_dentry
->d_inode
== new_dentry
->d_inode
)
1246 error
= may_delete(old_dir
, old_dentry
, 1);
1250 if (new_dir
->i_dev
!= old_dir
->i_dev
)
1253 if (!new_dentry
->d_inode
)
1254 error
= may_create(new_dir
, new_dentry
);
1256 error
= may_delete(new_dir
, new_dentry
, 1);
1260 if (!old_dir
->i_op
|| !old_dir
->i_op
->rename
)
1264 * If we are going to change the parent - check write permissions,
1265 * we'll need to flip '..'.
1267 if (new_dir
!= old_dir
) {
1268 error
= permission(old_dentry
->d_inode
, MAY_WRITE
);
1273 DQUOT_INIT(old_dir
);
1274 DQUOT_INIT(new_dir
);
1275 down(&old_dir
->i_sb
->s_vfs_rename_sem
);
1277 if (is_subdir(new_dentry
, old_dentry
))
1279 if (new_dentry
->d_inode
) {
1281 if (d_invalidate(new_dentry
)<0)
1285 error
= old_dir
->i_op
->rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
1287 d_rehash(new_dentry
);
1289 d_move(old_dentry
,new_dentry
);
1291 up(&old_dir
->i_sb
->s_vfs_rename_sem
);
1295 int vfs_rename_other(struct inode
*old_dir
, struct dentry
*old_dentry
,
1296 struct inode
*new_dir
, struct dentry
*new_dentry
)
1300 if (old_dentry
->d_inode
== new_dentry
->d_inode
)
1303 error
= may_delete(old_dir
, old_dentry
, 0);
1307 if (new_dir
->i_dev
!= old_dir
->i_dev
)
1310 if (!new_dentry
->d_inode
)
1311 error
= may_create(new_dir
, new_dentry
);
1313 error
= may_delete(new_dir
, new_dentry
, 0);
1317 if (!old_dir
->i_op
|| !old_dir
->i_op
->rename
)
1320 DQUOT_INIT(old_dir
);
1321 DQUOT_INIT(new_dir
);
1322 error
= old_dir
->i_op
->rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
1325 /* The following d_move() should become unconditional */
1326 if (!(old_dir
->i_sb
->s_flags
& MS_ODD_RENAME
)) {
1327 d_move(old_dentry
, new_dentry
);
1332 int vfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1333 struct inode
*new_dir
, struct dentry
*new_dentry
)
1335 if (S_ISDIR(old_dentry
->d_inode
->i_mode
))
1336 return vfs_rename_dir(old_dir
,old_dentry
,new_dir
,new_dentry
);
1338 return vfs_rename_other(old_dir
,old_dentry
,new_dir
,new_dentry
);
1341 static inline int do_rename(const char * oldname
, const char * newname
)
1344 struct dentry
* old_dir
, * new_dir
;
1345 struct dentry
* old_dentry
, *new_dentry
;
1347 old_dentry
= lookup_dentry(oldname
, NULL
, 0);
1349 error
= PTR_ERR(old_dentry
);
1350 if (IS_ERR(old_dentry
))
1354 if (!old_dentry
->d_inode
)
1358 unsigned int flags
= 0;
1359 if (S_ISDIR(old_dentry
->d_inode
->i_mode
))
1360 flags
= LOOKUP_SLASHOK
;
1361 new_dentry
= lookup_dentry(newname
, NULL
, flags
);
1364 error
= PTR_ERR(new_dentry
);
1365 if (IS_ERR(new_dentry
))
1368 new_dir
= get_parent(new_dentry
);
1369 old_dir
= get_parent(old_dentry
);
1371 double_lock(new_dir
, old_dir
);
1374 if (check_parent(old_dir
, old_dentry
) && check_parent(new_dir
, new_dentry
))
1375 error
= vfs_rename(old_dir
->d_inode
, old_dentry
,
1376 new_dir
->d_inode
, new_dentry
);
1378 double_unlock(new_dir
, old_dir
);
1386 asmlinkage
int sys_rename(const char * oldname
, const char * newname
)
1392 from
= getname(oldname
);
1393 error
= PTR_ERR(from
);
1394 if (!IS_ERR(from
)) {
1396 to
= getname(newname
);
1397 error
= PTR_ERR(to
);
1399 error
= do_rename(from
,to
);