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/pagemap.h>
23 #include <linux/fsnotify.h>
24 #include <linux/personality.h>
25 #include <linux/security.h>
26 #include <linux/ima.h>
27 #include <linux/syscalls.h>
28 #include <linux/mount.h>
29 #include <linux/audit.h>
30 #include <linux/capability.h>
31 #include <linux/file.h>
32 #include <linux/fcntl.h>
33 #include <linux/device_cgroup.h>
34 #include <linux/fs_struct.h>
35 #include <asm/uaccess.h>
39 /* [Feb-1997 T. Schoebel-Theuer]
40 * Fundamental changes in the pathname lookup mechanisms (namei)
41 * were necessary because of omirr. The reason is that omirr needs
42 * to know the _real_ pathname, not the user-supplied one, in case
43 * of symlinks (and also when transname replacements occur).
45 * The new code replaces the old recursive symlink resolution with
46 * an iterative one (in case of non-nested symlink chains). It does
47 * this with calls to <fs>_follow_link().
48 * As a side effect, dir_namei(), _namei() and follow_link() are now
49 * replaced with a single function lookup_dentry() that can handle all
50 * the special cases of the former code.
52 * With the new dcache, the pathname is stored at each inode, at least as
53 * long as the refcount of the inode is positive. As a side effect, the
54 * size of the dcache depends on the inode cache and thus is dynamic.
56 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
57 * resolution to correspond with current state of the code.
59 * Note that the symlink resolution is not *completely* iterative.
60 * There is still a significant amount of tail- and mid- recursion in
61 * the algorithm. Also, note that <fs>_readlink() is not used in
62 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
63 * may return different results than <fs>_follow_link(). Many virtual
64 * filesystems (including /proc) exhibit this behavior.
67 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
68 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
69 * and the name already exists in form of a symlink, try to create the new
70 * name indicated by the symlink. The old code always complained that the
71 * name already exists, due to not following the symlink even if its target
72 * is nonexistent. The new semantics affects also mknod() and link() when
73 * the name is a symlink pointing to a non-existant name.
75 * I don't know which semantics is the right one, since I have no access
76 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
77 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
78 * "old" one. Personally, I think the new semantics is much more logical.
79 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
80 * file does succeed in both HP-UX and SunOs, but not in Solaris
81 * and in the old Linux semantics.
84 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
85 * semantics. See the comments in "open_namei" and "do_link" below.
87 * [10-Sep-98 Alan Modra] Another symlink change.
90 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
91 * inside the path - always follow.
92 * in the last component in creation/removal/renaming - never follow.
93 * if LOOKUP_FOLLOW passed - follow.
94 * if the pathname has trailing slashes - follow.
95 * otherwise - don't follow.
96 * (applied in that order).
98 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
99 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
100 * During the 2.4 we need to fix the userland stuff depending on it -
101 * hopefully we will be able to get rid of that wart in 2.5. So far only
102 * XEmacs seems to be relying on it...
105 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
106 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
107 * any extra contention...
110 /* In order to reduce some races, while at the same time doing additional
111 * checking and hopefully speeding things up, we copy filenames to the
112 * kernel data space before using them..
114 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
115 * PATH_MAX includes the nul terminator --RR.
117 static int do_getname(const char __user
*filename
, char *page
)
120 unsigned long len
= PATH_MAX
;
122 if (!segment_eq(get_fs(), KERNEL_DS
)) {
123 if ((unsigned long) filename
>= TASK_SIZE
)
125 if (TASK_SIZE
- (unsigned long) filename
< PATH_MAX
)
126 len
= TASK_SIZE
- (unsigned long) filename
;
129 retval
= strncpy_from_user(page
, filename
, len
);
133 return -ENAMETOOLONG
;
139 char * getname(const char __user
* filename
)
143 result
= ERR_PTR(-ENOMEM
);
146 int retval
= do_getname(filename
, tmp
);
151 result
= ERR_PTR(retval
);
154 audit_getname(result
);
158 #ifdef CONFIG_AUDITSYSCALL
159 void putname(const char *name
)
161 if (unlikely(!audit_dummy_context()))
166 EXPORT_SYMBOL(putname
);
170 * This does basic POSIX ACL permission checking
172 static int acl_permission_check(struct inode
*inode
, int mask
, unsigned int flags
,
173 int (*check_acl
)(struct inode
*inode
, int mask
, unsigned int flags
))
175 umode_t mode
= inode
->i_mode
;
177 mask
&= MAY_READ
| MAY_WRITE
| MAY_EXEC
;
179 if (current_fsuid() == inode
->i_uid
)
182 if (IS_POSIXACL(inode
) && (mode
& S_IRWXG
) && check_acl
) {
183 int error
= check_acl(inode
, mask
, flags
);
184 if (error
!= -EAGAIN
)
188 if (in_group_p(inode
->i_gid
))
193 * If the DACs are ok we don't need any capability check.
195 if ((mask
& ~mode
) == 0)
201 * generic_permission - check for access rights on a Posix-like filesystem
202 * @inode: inode to check access rights for
203 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
204 * @check_acl: optional callback to check for Posix ACLs
205 * @flags IPERM_FLAG_ flags.
207 * Used to check for read/write/execute permissions on a file.
208 * We use "fsuid" for this, letting us set arbitrary permissions
209 * for filesystem access without changing the "normal" uids which
210 * are used for other things.
212 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
213 * request cannot be satisfied (eg. requires blocking or too much complexity).
214 * It would then be called again in ref-walk mode.
216 int generic_permission(struct inode
*inode
, int mask
, unsigned int flags
,
217 int (*check_acl
)(struct inode
*inode
, int mask
, unsigned int flags
))
222 * Do the basic POSIX ACL permission checks.
224 ret
= acl_permission_check(inode
, mask
, flags
, check_acl
);
229 * Read/write DACs are always overridable.
230 * Executable DACs are overridable if at least one exec bit is set.
232 if (!(mask
& MAY_EXEC
) || execute_ok(inode
))
233 if (capable(CAP_DAC_OVERRIDE
))
237 * Searching includes executable on directories, else just read.
239 mask
&= MAY_READ
| MAY_WRITE
| MAY_EXEC
;
240 if (mask
== MAY_READ
|| (S_ISDIR(inode
->i_mode
) && !(mask
& MAY_WRITE
)))
241 if (capable(CAP_DAC_READ_SEARCH
))
248 * inode_permission - check for access rights to a given inode
249 * @inode: inode to check permission on
250 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
252 * Used to check for read/write/execute permissions on an inode.
253 * We use "fsuid" for this, letting us set arbitrary permissions
254 * for filesystem access without changing the "normal" uids which
255 * are used for other things.
257 int inode_permission(struct inode
*inode
, int mask
)
261 if (mask
& MAY_WRITE
) {
262 umode_t mode
= inode
->i_mode
;
265 * Nobody gets write access to a read-only fs.
267 if (IS_RDONLY(inode
) &&
268 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
)))
272 * Nobody gets write access to an immutable file.
274 if (IS_IMMUTABLE(inode
))
278 if (inode
->i_op
->permission
)
279 retval
= inode
->i_op
->permission(inode
, mask
, 0);
281 retval
= generic_permission(inode
, mask
, 0,
282 inode
->i_op
->check_acl
);
287 retval
= devcgroup_inode_permission(inode
, mask
);
291 return security_inode_permission(inode
, mask
);
295 * file_permission - check for additional access rights to a given file
296 * @file: file to check access rights for
297 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
299 * Used to check for read/write/execute permissions on an already opened
303 * Do not use this function in new code. All access checks should
304 * be done using inode_permission().
306 int file_permission(struct file
*file
, int mask
)
308 return inode_permission(file
->f_path
.dentry
->d_inode
, mask
);
312 * get_write_access() gets write permission for a file.
313 * put_write_access() releases this write permission.
314 * This is used for regular files.
315 * We cannot support write (and maybe mmap read-write shared) accesses and
316 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
317 * can have the following values:
318 * 0: no writers, no VM_DENYWRITE mappings
319 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
320 * > 0: (i_writecount) users are writing to the file.
322 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
323 * except for the cases where we don't hold i_writecount yet. Then we need to
324 * use {get,deny}_write_access() - these functions check the sign and refuse
325 * to do the change if sign is wrong. Exclusion between them is provided by
326 * the inode->i_lock spinlock.
329 int get_write_access(struct inode
* inode
)
331 spin_lock(&inode
->i_lock
);
332 if (atomic_read(&inode
->i_writecount
) < 0) {
333 spin_unlock(&inode
->i_lock
);
336 atomic_inc(&inode
->i_writecount
);
337 spin_unlock(&inode
->i_lock
);
342 int deny_write_access(struct file
* file
)
344 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
346 spin_lock(&inode
->i_lock
);
347 if (atomic_read(&inode
->i_writecount
) > 0) {
348 spin_unlock(&inode
->i_lock
);
351 atomic_dec(&inode
->i_writecount
);
352 spin_unlock(&inode
->i_lock
);
358 * path_get - get a reference to a path
359 * @path: path to get the reference to
361 * Given a path increment the reference count to the dentry and the vfsmount.
363 void path_get(struct path
*path
)
368 EXPORT_SYMBOL(path_get
);
371 * path_get_long - get a long reference to a path
372 * @path: path to get the reference to
374 * Given a path increment the reference count to the dentry and the vfsmount.
376 void path_get_long(struct path
*path
)
378 mntget_long(path
->mnt
);
383 * path_put - put a reference to a path
384 * @path: path to put the reference to
386 * Given a path decrement the reference count to the dentry and the vfsmount.
388 void path_put(struct path
*path
)
393 EXPORT_SYMBOL(path_put
);
396 * path_put_long - put a long reference to a path
397 * @path: path to put the reference to
399 * Given a path decrement the reference count to the dentry and the vfsmount.
401 void path_put_long(struct path
*path
)
404 mntput_long(path
->mnt
);
408 * nameidata_drop_rcu - drop this nameidata out of rcu-walk
409 * @nd: nameidata pathwalk data to drop
410 * @Returns: 0 on success, -ECHLID on failure
412 * Path walking has 2 modes, rcu-walk and ref-walk (see
413 * Documentation/filesystems/path-lookup.txt). __drop_rcu* functions attempt
414 * to drop out of rcu-walk mode and take normal reference counts on dentries
415 * and vfsmounts to transition to rcu-walk mode. __drop_rcu* functions take
416 * refcounts at the last known good point before rcu-walk got stuck, so
417 * ref-walk may continue from there. If this is not successful (eg. a seqcount
418 * has changed), then failure is returned and path walk restarts from the
419 * beginning in ref-walk mode.
421 * nameidata_drop_rcu attempts to drop the current nd->path and nd->root into
422 * ref-walk. Must be called from rcu-walk context.
424 static int nameidata_drop_rcu(struct nameidata
*nd
)
426 struct fs_struct
*fs
= current
->fs
;
427 struct dentry
*dentry
= nd
->path
.dentry
;
429 BUG_ON(!(nd
->flags
& LOOKUP_RCU
));
431 spin_lock(&fs
->lock
);
432 if (nd
->root
.mnt
!= fs
->root
.mnt
||
433 nd
->root
.dentry
!= fs
->root
.dentry
)
436 spin_lock(&dentry
->d_lock
);
437 if (!__d_rcu_to_refcount(dentry
, nd
->seq
))
439 BUG_ON(nd
->inode
!= dentry
->d_inode
);
440 spin_unlock(&dentry
->d_lock
);
443 spin_unlock(&fs
->lock
);
445 mntget(nd
->path
.mnt
);
448 br_read_unlock(vfsmount_lock
);
449 nd
->flags
&= ~LOOKUP_RCU
;
452 spin_unlock(&dentry
->d_lock
);
455 spin_unlock(&fs
->lock
);
459 /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing. */
460 static inline int nameidata_drop_rcu_maybe(struct nameidata
*nd
)
462 if (nd
->flags
& LOOKUP_RCU
)
463 return nameidata_drop_rcu(nd
);
468 * nameidata_dentry_drop_rcu - drop nameidata and dentry out of rcu-walk
469 * @nd: nameidata pathwalk data to drop
470 * @dentry: dentry to drop
471 * @Returns: 0 on success, -ECHLID on failure
473 * nameidata_dentry_drop_rcu attempts to drop the current nd->path and nd->root,
474 * and dentry into ref-walk. @dentry must be a path found by a do_lookup call on
475 * @nd. Must be called from rcu-walk context.
477 static int nameidata_dentry_drop_rcu(struct nameidata
*nd
, struct dentry
*dentry
)
479 struct fs_struct
*fs
= current
->fs
;
480 struct dentry
*parent
= nd
->path
.dentry
;
482 BUG_ON(!(nd
->flags
& LOOKUP_RCU
));
484 spin_lock(&fs
->lock
);
485 if (nd
->root
.mnt
!= fs
->root
.mnt
||
486 nd
->root
.dentry
!= fs
->root
.dentry
)
489 spin_lock(&parent
->d_lock
);
490 spin_lock_nested(&dentry
->d_lock
, DENTRY_D_LOCK_NESTED
);
491 if (!__d_rcu_to_refcount(dentry
, nd
->seq
))
494 * If the sequence check on the child dentry passed, then the child has
495 * not been removed from its parent. This means the parent dentry must
496 * be valid and able to take a reference at this point.
498 BUG_ON(!IS_ROOT(dentry
) && dentry
->d_parent
!= parent
);
499 BUG_ON(!parent
->d_count
);
501 spin_unlock(&dentry
->d_lock
);
502 spin_unlock(&parent
->d_lock
);
505 spin_unlock(&fs
->lock
);
507 mntget(nd
->path
.mnt
);
510 br_read_unlock(vfsmount_lock
);
511 nd
->flags
&= ~LOOKUP_RCU
;
514 spin_unlock(&dentry
->d_lock
);
515 spin_unlock(&parent
->d_lock
);
518 spin_unlock(&fs
->lock
);
522 /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing. */
523 static inline int nameidata_dentry_drop_rcu_maybe(struct nameidata
*nd
, struct dentry
*dentry
)
525 if (nd
->flags
& LOOKUP_RCU
)
526 return nameidata_dentry_drop_rcu(nd
, dentry
);
531 * nameidata_drop_rcu_last - drop nameidata ending path walk out of rcu-walk
532 * @nd: nameidata pathwalk data to drop
533 * @Returns: 0 on success, -ECHLID on failure
535 * nameidata_drop_rcu_last attempts to drop the current nd->path into ref-walk.
536 * nd->path should be the final element of the lookup, so nd->root is discarded.
537 * Must be called from rcu-walk context.
539 static int nameidata_drop_rcu_last(struct nameidata
*nd
)
541 struct dentry
*dentry
= nd
->path
.dentry
;
543 BUG_ON(!(nd
->flags
& LOOKUP_RCU
));
544 nd
->flags
&= ~LOOKUP_RCU
;
546 spin_lock(&dentry
->d_lock
);
547 if (!__d_rcu_to_refcount(dentry
, nd
->seq
))
549 BUG_ON(nd
->inode
!= dentry
->d_inode
);
550 spin_unlock(&dentry
->d_lock
);
552 mntget(nd
->path
.mnt
);
555 br_read_unlock(vfsmount_lock
);
560 spin_unlock(&dentry
->d_lock
);
562 br_read_unlock(vfsmount_lock
);
566 /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing. */
567 static inline int nameidata_drop_rcu_last_maybe(struct nameidata
*nd
)
569 if (likely(nd
->flags
& LOOKUP_RCU
))
570 return nameidata_drop_rcu_last(nd
);
575 * release_open_intent - free up open intent resources
576 * @nd: pointer to nameidata
578 void release_open_intent(struct nameidata
*nd
)
580 if (nd
->intent
.open
.file
->f_path
.dentry
== NULL
)
581 put_filp(nd
->intent
.open
.file
);
583 fput(nd
->intent
.open
.file
);
587 * Call d_revalidate and handle filesystems that request rcu-walk
588 * to be dropped. This may be called and return in rcu-walk mode,
589 * regardless of success or error. If -ECHILD is returned, the caller
590 * must return -ECHILD back up the path walk stack so path walk may
591 * be restarted in ref-walk mode.
593 static int d_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
597 status
= dentry
->d_op
->d_revalidate(dentry
, nd
);
598 if (status
== -ECHILD
) {
599 if (nameidata_dentry_drop_rcu(nd
, dentry
))
601 status
= dentry
->d_op
->d_revalidate(dentry
, nd
);
607 static inline struct dentry
*
608 do_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
612 status
= d_revalidate(dentry
, nd
);
613 if (unlikely(status
<= 0)) {
615 * The dentry failed validation.
616 * If d_revalidate returned 0 attempt to invalidate
617 * the dentry otherwise d_revalidate is asking us
618 * to return a fail status.
621 /* If we're in rcu-walk, we don't have a ref */
622 if (!(nd
->flags
& LOOKUP_RCU
))
624 dentry
= ERR_PTR(status
);
627 /* Don't d_invalidate in rcu-walk mode */
628 if (nameidata_dentry_drop_rcu_maybe(nd
, dentry
))
629 return ERR_PTR(-ECHILD
);
630 if (!d_invalidate(dentry
)) {
639 static inline int need_reval_dot(struct dentry
*dentry
)
641 if (likely(!(dentry
->d_flags
& DCACHE_OP_REVALIDATE
)))
644 if (likely(!(dentry
->d_sb
->s_type
->fs_flags
& FS_REVAL_DOT
)))
651 * force_reval_path - force revalidation of a dentry
653 * In some situations the path walking code will trust dentries without
654 * revalidating them. This causes problems for filesystems that depend on
655 * d_revalidate to handle file opens (e.g. NFSv4). When FS_REVAL_DOT is set
656 * (which indicates that it's possible for the dentry to go stale), force
657 * a d_revalidate call before proceeding.
659 * Returns 0 if the revalidation was successful. If the revalidation fails,
660 * either return the error returned by d_revalidate or -ESTALE if the
661 * revalidation it just returned 0. If d_revalidate returns 0, we attempt to
662 * invalidate the dentry. It's up to the caller to handle putting references
663 * to the path if necessary.
666 force_reval_path(struct path
*path
, struct nameidata
*nd
)
669 struct dentry
*dentry
= path
->dentry
;
672 * only check on filesystems where it's possible for the dentry to
675 if (!need_reval_dot(dentry
))
678 status
= d_revalidate(dentry
, nd
);
683 /* Don't d_invalidate in rcu-walk mode */
684 if (nameidata_drop_rcu(nd
))
686 d_invalidate(dentry
);
693 * Short-cut version of permission(), for calling on directories
694 * during pathname resolution. Combines parts of permission()
695 * and generic_permission(), and tests ONLY for MAY_EXEC permission.
697 * If appropriate, check DAC only. If not appropriate, or
698 * short-cut DAC fails, then call ->permission() to do more
699 * complete permission check.
701 static inline int exec_permission(struct inode
*inode
, unsigned int flags
)
705 if (inode
->i_op
->permission
) {
706 ret
= inode
->i_op
->permission(inode
, MAY_EXEC
, flags
);
708 ret
= acl_permission_check(inode
, MAY_EXEC
, flags
,
709 inode
->i_op
->check_acl
);
716 if (capable(CAP_DAC_OVERRIDE
) || capable(CAP_DAC_READ_SEARCH
))
721 return security_inode_exec_permission(inode
, flags
);
724 static __always_inline
void set_root(struct nameidata
*nd
)
727 get_fs_root(current
->fs
, &nd
->root
);
730 static int link_path_walk(const char *, struct nameidata
*);
732 static __always_inline
void set_root_rcu(struct nameidata
*nd
)
735 struct fs_struct
*fs
= current
->fs
;
739 seq
= read_seqcount_begin(&fs
->seq
);
741 } while (read_seqcount_retry(&fs
->seq
, seq
));
745 static __always_inline
int __vfs_follow_link(struct nameidata
*nd
, const char *link
)
758 nd
->inode
= nd
->path
.dentry
->d_inode
;
760 ret
= link_path_walk(link
, nd
);
764 return PTR_ERR(link
);
767 static void path_put_conditional(struct path
*path
, struct nameidata
*nd
)
770 if (path
->mnt
!= nd
->path
.mnt
)
774 static inline void path_to_nameidata(struct path
*path
, struct nameidata
*nd
)
776 if (!(nd
->flags
& LOOKUP_RCU
)) {
777 dput(nd
->path
.dentry
);
778 if (nd
->path
.mnt
!= path
->mnt
)
779 mntput(nd
->path
.mnt
);
781 nd
->path
.mnt
= path
->mnt
;
782 nd
->path
.dentry
= path
->dentry
;
785 static __always_inline
int
786 __do_follow_link(struct path
*path
, struct nameidata
*nd
, void **p
)
789 struct dentry
*dentry
= path
->dentry
;
791 touch_atime(path
->mnt
, dentry
);
792 nd_set_link(nd
, NULL
);
794 if (path
->mnt
!= nd
->path
.mnt
) {
795 path_to_nameidata(path
, nd
);
796 nd
->inode
= nd
->path
.dentry
->d_inode
;
801 nd
->last_type
= LAST_BIND
;
802 *p
= dentry
->d_inode
->i_op
->follow_link(dentry
, nd
);
805 char *s
= nd_get_link(nd
);
808 error
= __vfs_follow_link(nd
, s
);
809 else if (nd
->last_type
== LAST_BIND
) {
810 error
= force_reval_path(&nd
->path
, nd
);
819 * This limits recursive symlink follows to 8, while
820 * limiting consecutive symlinks to 40.
822 * Without that kind of total limit, nasty chains of consecutive
823 * symlinks can cause almost arbitrarily long lookups.
825 static inline int do_follow_link(struct path
*path
, struct nameidata
*nd
)
829 if (current
->link_count
>= MAX_NESTED_LINKS
)
831 if (current
->total_link_count
>= 40)
833 BUG_ON(nd
->depth
>= MAX_NESTED_LINKS
);
835 err
= security_inode_follow_link(path
->dentry
, nd
);
838 current
->link_count
++;
839 current
->total_link_count
++;
841 err
= __do_follow_link(path
, nd
, &cookie
);
842 if (!IS_ERR(cookie
) && path
->dentry
->d_inode
->i_op
->put_link
)
843 path
->dentry
->d_inode
->i_op
->put_link(path
->dentry
, nd
, cookie
);
845 current
->link_count
--;
849 path_put_conditional(path
, nd
);
854 static int follow_up_rcu(struct path
*path
)
856 struct vfsmount
*parent
;
857 struct dentry
*mountpoint
;
859 parent
= path
->mnt
->mnt_parent
;
860 if (parent
== path
->mnt
)
862 mountpoint
= path
->mnt
->mnt_mountpoint
;
863 path
->dentry
= mountpoint
;
868 int follow_up(struct path
*path
)
870 struct vfsmount
*parent
;
871 struct dentry
*mountpoint
;
873 br_read_lock(vfsmount_lock
);
874 parent
= path
->mnt
->mnt_parent
;
875 if (parent
== path
->mnt
) {
876 br_read_unlock(vfsmount_lock
);
880 mountpoint
= dget(path
->mnt
->mnt_mountpoint
);
881 br_read_unlock(vfsmount_lock
);
883 path
->dentry
= mountpoint
;
890 * serialization is taken care of in namespace.c
892 static void __follow_mount_rcu(struct nameidata
*nd
, struct path
*path
,
893 struct inode
**inode
)
895 while (d_mountpoint(path
->dentry
)) {
896 struct vfsmount
*mounted
;
897 mounted
= __lookup_mnt(path
->mnt
, path
->dentry
, 1);
901 path
->dentry
= mounted
->mnt_root
;
902 nd
->seq
= read_seqcount_begin(&path
->dentry
->d_seq
);
903 *inode
= path
->dentry
->d_inode
;
907 static int __follow_mount(struct path
*path
)
910 while (d_mountpoint(path
->dentry
)) {
911 struct vfsmount
*mounted
= lookup_mnt(path
);
918 path
->dentry
= dget(mounted
->mnt_root
);
924 static void follow_mount(struct path
*path
)
926 while (d_mountpoint(path
->dentry
)) {
927 struct vfsmount
*mounted
= lookup_mnt(path
);
933 path
->dentry
= dget(mounted
->mnt_root
);
937 int follow_down(struct path
*path
)
939 struct vfsmount
*mounted
;
941 mounted
= lookup_mnt(path
);
946 path
->dentry
= dget(mounted
->mnt_root
);
952 static int follow_dotdot_rcu(struct nameidata
*nd
)
954 struct inode
*inode
= nd
->inode
;
959 if (nd
->path
.dentry
== nd
->root
.dentry
&&
960 nd
->path
.mnt
== nd
->root
.mnt
) {
963 if (nd
->path
.dentry
!= nd
->path
.mnt
->mnt_root
) {
964 struct dentry
*old
= nd
->path
.dentry
;
965 struct dentry
*parent
= old
->d_parent
;
968 seq
= read_seqcount_begin(&parent
->d_seq
);
969 if (read_seqcount_retry(&old
->d_seq
, nd
->seq
))
971 inode
= parent
->d_inode
;
972 nd
->path
.dentry
= parent
;
976 if (!follow_up_rcu(&nd
->path
))
978 nd
->seq
= read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
979 inode
= nd
->path
.dentry
->d_inode
;
981 __follow_mount_rcu(nd
, &nd
->path
, &inode
);
987 static void follow_dotdot(struct nameidata
*nd
)
992 struct dentry
*old
= nd
->path
.dentry
;
994 if (nd
->path
.dentry
== nd
->root
.dentry
&&
995 nd
->path
.mnt
== nd
->root
.mnt
) {
998 if (nd
->path
.dentry
!= nd
->path
.mnt
->mnt_root
) {
999 /* rare case of legitimate dget_parent()... */
1000 nd
->path
.dentry
= dget_parent(nd
->path
.dentry
);
1004 if (!follow_up(&nd
->path
))
1007 follow_mount(&nd
->path
);
1008 nd
->inode
= nd
->path
.dentry
->d_inode
;
1012 * Allocate a dentry with name and parent, and perform a parent
1013 * directory ->lookup on it. Returns the new dentry, or ERR_PTR
1014 * on error. parent->d_inode->i_mutex must be held. d_lookup must
1015 * have verified that no child exists while under i_mutex.
1017 static struct dentry
*d_alloc_and_lookup(struct dentry
*parent
,
1018 struct qstr
*name
, struct nameidata
*nd
)
1020 struct inode
*inode
= parent
->d_inode
;
1021 struct dentry
*dentry
;
1024 /* Don't create child dentry for a dead directory. */
1025 if (unlikely(IS_DEADDIR(inode
)))
1026 return ERR_PTR(-ENOENT
);
1028 dentry
= d_alloc(parent
, name
);
1029 if (unlikely(!dentry
))
1030 return ERR_PTR(-ENOMEM
);
1032 old
= inode
->i_op
->lookup(inode
, dentry
, nd
);
1033 if (unlikely(old
)) {
1041 * It's more convoluted than I'd like it to be, but... it's still fairly
1042 * small and for now I'd prefer to have fast path as straight as possible.
1043 * It _is_ time-critical.
1045 static int do_lookup(struct nameidata
*nd
, struct qstr
*name
,
1046 struct path
*path
, struct inode
**inode
)
1048 struct vfsmount
*mnt
= nd
->path
.mnt
;
1049 struct dentry
*dentry
, *parent
= nd
->path
.dentry
;
1052 * See if the low-level filesystem might want
1053 * to use its own hash..
1055 if (unlikely(parent
->d_flags
& DCACHE_OP_HASH
)) {
1056 int err
= parent
->d_op
->d_hash(parent
, nd
->inode
, name
);
1062 * Rename seqlock is not required here because in the off chance
1063 * of a false negative due to a concurrent rename, we're going to
1064 * do the non-racy lookup, below.
1066 if (nd
->flags
& LOOKUP_RCU
) {
1070 dentry
= __d_lookup_rcu(parent
, name
, &seq
, inode
);
1072 if (nameidata_drop_rcu(nd
))
1076 /* Memory barrier in read_seqcount_begin of child is enough */
1077 if (__read_seqcount_retry(&parent
->d_seq
, nd
->seq
))
1081 if (dentry
->d_flags
& DCACHE_OP_REVALIDATE
)
1082 goto need_revalidate
;
1084 path
->dentry
= dentry
;
1085 __follow_mount_rcu(nd
, path
, inode
);
1087 dentry
= __d_lookup(parent
, name
);
1091 if (dentry
->d_flags
& DCACHE_OP_REVALIDATE
)
1092 goto need_revalidate
;
1095 path
->dentry
= dentry
;
1096 __follow_mount(path
);
1097 *inode
= path
->dentry
->d_inode
;
1102 dir
= parent
->d_inode
;
1103 BUG_ON(nd
->inode
!= dir
);
1105 mutex_lock(&dir
->i_mutex
);
1107 * First re-do the cached lookup just in case it was created
1108 * while we waited for the directory semaphore, or the first
1109 * lookup failed due to an unrelated rename.
1111 * This could use version numbering or similar to avoid unnecessary
1112 * cache lookups, but then we'd have to do the first lookup in the
1113 * non-racy way. However in the common case here, everything should
1114 * be hot in cache, so would it be a big win?
1116 dentry
= d_lookup(parent
, name
);
1117 if (likely(!dentry
)) {
1118 dentry
= d_alloc_and_lookup(parent
, name
, nd
);
1119 mutex_unlock(&dir
->i_mutex
);
1125 * Uhhuh! Nasty case: the cache was re-populated while
1126 * we waited on the semaphore. Need to revalidate.
1128 mutex_unlock(&dir
->i_mutex
);
1132 dentry
= do_revalidate(dentry
, nd
);
1140 return PTR_ERR(dentry
);
1144 * This is a temporary kludge to deal with "automount" symlinks; proper
1145 * solution is to trigger them on follow_mount(), so that do_lookup()
1146 * would DTRT. To be killed before 2.6.34-final.
1148 static inline int follow_on_final(struct inode
*inode
, unsigned lookup_flags
)
1150 return inode
&& unlikely(inode
->i_op
->follow_link
) &&
1151 ((lookup_flags
& LOOKUP_FOLLOW
) || S_ISDIR(inode
->i_mode
));
1156 * This is the basic name resolution function, turning a pathname into
1157 * the final dentry. We expect 'base' to be positive and a directory.
1159 * Returns 0 and nd will have valid dentry and mnt on success.
1160 * Returns error and drops reference to input namei data on failure.
1162 static int link_path_walk(const char *name
, struct nameidata
*nd
)
1166 unsigned int lookup_flags
= nd
->flags
;
1174 lookup_flags
= LOOKUP_FOLLOW
| (nd
->flags
& LOOKUP_CONTINUE
);
1176 /* At this point we know we have a real path component. */
1178 struct inode
*inode
;
1183 nd
->flags
|= LOOKUP_CONTINUE
;
1184 if (nd
->flags
& LOOKUP_RCU
) {
1185 err
= exec_permission(nd
->inode
, IPERM_FLAG_RCU
);
1186 if (err
== -ECHILD
) {
1187 if (nameidata_drop_rcu(nd
))
1193 err
= exec_permission(nd
->inode
, 0);
1199 c
= *(const unsigned char *)name
;
1201 hash
= init_name_hash();
1204 hash
= partial_name_hash(c
, hash
);
1205 c
= *(const unsigned char *)name
;
1206 } while (c
&& (c
!= '/'));
1207 this.len
= name
- (const char *) this.name
;
1208 this.hash
= end_name_hash(hash
);
1210 /* remove trailing slashes? */
1212 goto last_component
;
1213 while (*++name
== '/');
1215 goto last_with_slashes
;
1218 * "." and ".." are special - ".." especially so because it has
1219 * to be able to know about the current root directory and
1220 * parent relationships.
1222 if (this.name
[0] == '.') switch (this.len
) {
1226 if (this.name
[1] != '.')
1228 if (nd
->flags
& LOOKUP_RCU
) {
1229 if (follow_dotdot_rcu(nd
))
1237 /* This does the actual lookups.. */
1238 err
= do_lookup(nd
, &this, &next
, &inode
);
1245 if (inode
->i_op
->follow_link
) {
1246 /* We commonly drop rcu-walk here */
1247 if (nameidata_dentry_drop_rcu_maybe(nd
, next
.dentry
))
1249 BUG_ON(inode
!= next
.dentry
->d_inode
);
1250 err
= do_follow_link(&next
, nd
);
1253 nd
->inode
= nd
->path
.dentry
->d_inode
;
1258 path_to_nameidata(&next
, nd
);
1262 if (!nd
->inode
->i_op
->lookup
)
1265 /* here ends the main loop */
1268 lookup_flags
|= LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
;
1270 /* Clear LOOKUP_CONTINUE iff it was previously unset */
1271 nd
->flags
&= lookup_flags
| ~LOOKUP_CONTINUE
;
1272 if (lookup_flags
& LOOKUP_PARENT
)
1274 if (this.name
[0] == '.') switch (this.len
) {
1278 if (this.name
[1] != '.')
1280 if (nd
->flags
& LOOKUP_RCU
) {
1281 if (follow_dotdot_rcu(nd
))
1289 err
= do_lookup(nd
, &this, &next
, &inode
);
1292 if (follow_on_final(inode
, lookup_flags
)) {
1293 if (nameidata_dentry_drop_rcu_maybe(nd
, next
.dentry
))
1295 BUG_ON(inode
!= next
.dentry
->d_inode
);
1296 err
= do_follow_link(&next
, nd
);
1299 nd
->inode
= nd
->path
.dentry
->d_inode
;
1301 path_to_nameidata(&next
, nd
);
1307 if (lookup_flags
& LOOKUP_DIRECTORY
) {
1309 if (!nd
->inode
->i_op
->lookup
)
1315 nd
->last_type
= LAST_NORM
;
1316 if (this.name
[0] != '.')
1319 nd
->last_type
= LAST_DOT
;
1320 else if (this.len
== 2 && this.name
[1] == '.')
1321 nd
->last_type
= LAST_DOTDOT
;
1326 * We bypassed the ordinary revalidation routines.
1327 * We may need to check the cached dentry for staleness.
1329 if (need_reval_dot(nd
->path
.dentry
)) {
1330 /* Note: we do not d_invalidate() */
1331 err
= d_revalidate(nd
->path
.dentry
, nd
);
1338 if (nameidata_drop_rcu_last_maybe(nd
))
1342 if (!(nd
->flags
& LOOKUP_RCU
))
1343 path_put_conditional(&next
, nd
);
1346 if (!(nd
->flags
& LOOKUP_RCU
))
1347 path_put(&nd
->path
);
1352 static inline int path_walk_rcu(const char *name
, struct nameidata
*nd
)
1354 current
->total_link_count
= 0;
1356 return link_path_walk(name
, nd
);
1359 static inline int path_walk_simple(const char *name
, struct nameidata
*nd
)
1361 current
->total_link_count
= 0;
1363 return link_path_walk(name
, nd
);
1366 static int path_walk(const char *name
, struct nameidata
*nd
)
1368 struct path save
= nd
->path
;
1371 current
->total_link_count
= 0;
1373 /* make sure the stuff we saved doesn't go away */
1376 result
= link_path_walk(name
, nd
);
1377 if (result
== -ESTALE
) {
1378 /* nd->path had been dropped */
1379 current
->total_link_count
= 0;
1381 path_get(&nd
->path
);
1382 nd
->flags
|= LOOKUP_REVAL
;
1383 result
= link_path_walk(name
, nd
);
1391 static void path_finish_rcu(struct nameidata
*nd
)
1393 if (nd
->flags
& LOOKUP_RCU
) {
1394 /* RCU dangling. Cancel it. */
1395 nd
->flags
&= ~LOOKUP_RCU
;
1396 nd
->root
.mnt
= NULL
;
1398 br_read_unlock(vfsmount_lock
);
1404 static int path_init_rcu(int dfd
, const char *name
, unsigned int flags
, struct nameidata
*nd
)
1410 nd
->last_type
= LAST_ROOT
; /* if there are only slashes... */
1411 nd
->flags
= flags
| LOOKUP_RCU
;
1413 nd
->root
.mnt
= NULL
;
1417 struct fs_struct
*fs
= current
->fs
;
1420 br_read_lock(vfsmount_lock
);
1424 seq
= read_seqcount_begin(&fs
->seq
);
1425 nd
->root
= fs
->root
;
1426 nd
->path
= nd
->root
;
1427 nd
->seq
= __read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
1428 } while (read_seqcount_retry(&fs
->seq
, seq
));
1430 } else if (dfd
== AT_FDCWD
) {
1431 struct fs_struct
*fs
= current
->fs
;
1434 br_read_lock(vfsmount_lock
);
1438 seq
= read_seqcount_begin(&fs
->seq
);
1440 nd
->seq
= __read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
1441 } while (read_seqcount_retry(&fs
->seq
, seq
));
1444 struct dentry
*dentry
;
1446 file
= fget_light(dfd
, &fput_needed
);
1451 dentry
= file
->f_path
.dentry
;
1454 if (!S_ISDIR(dentry
->d_inode
->i_mode
))
1457 retval
= file_permission(file
, MAY_EXEC
);
1461 nd
->path
= file
->f_path
;
1465 nd
->seq
= __read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
1466 br_read_lock(vfsmount_lock
);
1469 nd
->inode
= nd
->path
.dentry
->d_inode
;
1473 fput_light(file
, fput_needed
);
1478 static int path_init(int dfd
, const char *name
, unsigned int flags
, struct nameidata
*nd
)
1484 nd
->last_type
= LAST_ROOT
; /* if there are only slashes... */
1487 nd
->root
.mnt
= NULL
;
1491 nd
->path
= nd
->root
;
1492 path_get(&nd
->root
);
1493 } else if (dfd
== AT_FDCWD
) {
1494 get_fs_pwd(current
->fs
, &nd
->path
);
1496 struct dentry
*dentry
;
1498 file
= fget_light(dfd
, &fput_needed
);
1503 dentry
= file
->f_path
.dentry
;
1506 if (!S_ISDIR(dentry
->d_inode
->i_mode
))
1509 retval
= file_permission(file
, MAY_EXEC
);
1513 nd
->path
= file
->f_path
;
1514 path_get(&file
->f_path
);
1516 fput_light(file
, fput_needed
);
1518 nd
->inode
= nd
->path
.dentry
->d_inode
;
1522 fput_light(file
, fput_needed
);
1527 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1528 static int do_path_lookup(int dfd
, const char *name
,
1529 unsigned int flags
, struct nameidata
*nd
)
1534 * Path walking is largely split up into 2 different synchronisation
1535 * schemes, rcu-walk and ref-walk (explained in
1536 * Documentation/filesystems/path-lookup.txt). These share much of the
1537 * path walk code, but some things particularly setup, cleanup, and
1538 * following mounts are sufficiently divergent that functions are
1539 * duplicated. Typically there is a function foo(), and its RCU
1540 * analogue, foo_rcu().
1542 * -ECHILD is the error number of choice (just to avoid clashes) that
1543 * is returned if some aspect of an rcu-walk fails. Such an error must
1544 * be handled by restarting a traditional ref-walk (which will always
1545 * be able to complete).
1547 retval
= path_init_rcu(dfd
, name
, flags
, nd
);
1548 if (unlikely(retval
))
1550 retval
= path_walk_rcu(name
, nd
);
1551 path_finish_rcu(nd
);
1553 path_put(&nd
->root
);
1554 nd
->root
.mnt
= NULL
;
1557 if (unlikely(retval
== -ECHILD
|| retval
== -ESTALE
)) {
1558 /* slower, locked walk */
1559 if (retval
== -ESTALE
)
1560 flags
|= LOOKUP_REVAL
;
1561 retval
= path_init(dfd
, name
, flags
, nd
);
1562 if (unlikely(retval
))
1564 retval
= path_walk(name
, nd
);
1566 path_put(&nd
->root
);
1567 nd
->root
.mnt
= NULL
;
1571 if (likely(!retval
)) {
1572 if (unlikely(!audit_dummy_context())) {
1573 if (nd
->path
.dentry
&& nd
->inode
)
1574 audit_inode(name
, nd
->path
.dentry
);
1581 int path_lookup(const char *name
, unsigned int flags
,
1582 struct nameidata
*nd
)
1584 return do_path_lookup(AT_FDCWD
, name
, flags
, nd
);
1587 int kern_path(const char *name
, unsigned int flags
, struct path
*path
)
1589 struct nameidata nd
;
1590 int res
= do_path_lookup(AT_FDCWD
, name
, flags
, &nd
);
1597 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1598 * @dentry: pointer to dentry of the base directory
1599 * @mnt: pointer to vfs mount of the base directory
1600 * @name: pointer to file name
1601 * @flags: lookup flags
1602 * @nd: pointer to nameidata
1604 int vfs_path_lookup(struct dentry
*dentry
, struct vfsmount
*mnt
,
1605 const char *name
, unsigned int flags
,
1606 struct nameidata
*nd
)
1610 /* same as do_path_lookup */
1611 nd
->last_type
= LAST_ROOT
;
1615 nd
->path
.dentry
= dentry
;
1617 path_get(&nd
->path
);
1618 nd
->root
= nd
->path
;
1619 path_get(&nd
->root
);
1620 nd
->inode
= nd
->path
.dentry
->d_inode
;
1622 retval
= path_walk(name
, nd
);
1623 if (unlikely(!retval
&& !audit_dummy_context() && nd
->path
.dentry
&&
1625 audit_inode(name
, nd
->path
.dentry
);
1627 path_put(&nd
->root
);
1628 nd
->root
.mnt
= NULL
;
1633 static struct dentry
*__lookup_hash(struct qstr
*name
,
1634 struct dentry
*base
, struct nameidata
*nd
)
1636 struct inode
*inode
= base
->d_inode
;
1637 struct dentry
*dentry
;
1640 err
= exec_permission(inode
, 0);
1642 return ERR_PTR(err
);
1645 * See if the low-level filesystem might want
1646 * to use its own hash..
1648 if (base
->d_flags
& DCACHE_OP_HASH
) {
1649 err
= base
->d_op
->d_hash(base
, inode
, name
);
1650 dentry
= ERR_PTR(err
);
1656 * Don't bother with __d_lookup: callers are for creat as
1657 * well as unlink, so a lot of the time it would cost
1660 dentry
= d_lookup(base
, name
);
1662 if (dentry
&& (dentry
->d_flags
& DCACHE_OP_REVALIDATE
))
1663 dentry
= do_revalidate(dentry
, nd
);
1666 dentry
= d_alloc_and_lookup(base
, name
, nd
);
1672 * Restricted form of lookup. Doesn't follow links, single-component only,
1673 * needs parent already locked. Doesn't follow mounts.
1676 static struct dentry
*lookup_hash(struct nameidata
*nd
)
1678 return __lookup_hash(&nd
->last
, nd
->path
.dentry
, nd
);
1681 static int __lookup_one_len(const char *name
, struct qstr
*this,
1682 struct dentry
*base
, int len
)
1692 hash
= init_name_hash();
1694 c
= *(const unsigned char *)name
++;
1695 if (c
== '/' || c
== '\0')
1697 hash
= partial_name_hash(c
, hash
);
1699 this->hash
= end_name_hash(hash
);
1704 * lookup_one_len - filesystem helper to lookup single pathname component
1705 * @name: pathname component to lookup
1706 * @base: base directory to lookup from
1707 * @len: maximum length @len should be interpreted to
1709 * Note that this routine is purely a helper for filesystem usage and should
1710 * not be called by generic code. Also note that by using this function the
1711 * nameidata argument is passed to the filesystem methods and a filesystem
1712 * using this helper needs to be prepared for that.
1714 struct dentry
*lookup_one_len(const char *name
, struct dentry
*base
, int len
)
1719 WARN_ON_ONCE(!mutex_is_locked(&base
->d_inode
->i_mutex
));
1721 err
= __lookup_one_len(name
, &this, base
, len
);
1723 return ERR_PTR(err
);
1725 return __lookup_hash(&this, base
, NULL
);
1728 int user_path_at(int dfd
, const char __user
*name
, unsigned flags
,
1731 struct nameidata nd
;
1732 char *tmp
= getname(name
);
1733 int err
= PTR_ERR(tmp
);
1736 BUG_ON(flags
& LOOKUP_PARENT
);
1738 err
= do_path_lookup(dfd
, tmp
, flags
, &nd
);
1746 static int user_path_parent(int dfd
, const char __user
*path
,
1747 struct nameidata
*nd
, char **name
)
1749 char *s
= getname(path
);
1755 error
= do_path_lookup(dfd
, s
, LOOKUP_PARENT
, nd
);
1765 * It's inline, so penalty for filesystems that don't use sticky bit is
1768 static inline int check_sticky(struct inode
*dir
, struct inode
*inode
)
1770 uid_t fsuid
= current_fsuid();
1772 if (!(dir
->i_mode
& S_ISVTX
))
1774 if (inode
->i_uid
== fsuid
)
1776 if (dir
->i_uid
== fsuid
)
1778 return !capable(CAP_FOWNER
);
1782 * Check whether we can remove a link victim from directory dir, check
1783 * whether the type of victim is right.
1784 * 1. We can't do it if dir is read-only (done in permission())
1785 * 2. We should have write and exec permissions on dir
1786 * 3. We can't remove anything from append-only dir
1787 * 4. We can't do anything with immutable dir (done in permission())
1788 * 5. If the sticky bit on dir is set we should either
1789 * a. be owner of dir, or
1790 * b. be owner of victim, or
1791 * c. have CAP_FOWNER capability
1792 * 6. If the victim is append-only or immutable we can't do antyhing with
1793 * links pointing to it.
1794 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1795 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1796 * 9. We can't remove a root or mountpoint.
1797 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1798 * nfs_async_unlink().
1800 static int may_delete(struct inode
*dir
,struct dentry
*victim
,int isdir
)
1804 if (!victim
->d_inode
)
1807 BUG_ON(victim
->d_parent
->d_inode
!= dir
);
1808 audit_inode_child(victim
, dir
);
1810 error
= inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
1815 if (check_sticky(dir
, victim
->d_inode
)||IS_APPEND(victim
->d_inode
)||
1816 IS_IMMUTABLE(victim
->d_inode
) || IS_SWAPFILE(victim
->d_inode
))
1819 if (!S_ISDIR(victim
->d_inode
->i_mode
))
1821 if (IS_ROOT(victim
))
1823 } else if (S_ISDIR(victim
->d_inode
->i_mode
))
1825 if (IS_DEADDIR(dir
))
1827 if (victim
->d_flags
& DCACHE_NFSFS_RENAMED
)
1832 /* Check whether we can create an object with dentry child in directory
1834 * 1. We can't do it if child already exists (open has special treatment for
1835 * this case, but since we are inlined it's OK)
1836 * 2. We can't do it if dir is read-only (done in permission())
1837 * 3. We should have write and exec permissions on dir
1838 * 4. We can't do it if dir is immutable (done in permission())
1840 static inline int may_create(struct inode
*dir
, struct dentry
*child
)
1844 if (IS_DEADDIR(dir
))
1846 return inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
1850 * p1 and p2 should be directories on the same fs.
1852 struct dentry
*lock_rename(struct dentry
*p1
, struct dentry
*p2
)
1857 mutex_lock_nested(&p1
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
1861 mutex_lock(&p1
->d_inode
->i_sb
->s_vfs_rename_mutex
);
1863 p
= d_ancestor(p2
, p1
);
1865 mutex_lock_nested(&p2
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
1866 mutex_lock_nested(&p1
->d_inode
->i_mutex
, I_MUTEX_CHILD
);
1870 p
= d_ancestor(p1
, p2
);
1872 mutex_lock_nested(&p1
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
1873 mutex_lock_nested(&p2
->d_inode
->i_mutex
, I_MUTEX_CHILD
);
1877 mutex_lock_nested(&p1
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
1878 mutex_lock_nested(&p2
->d_inode
->i_mutex
, I_MUTEX_CHILD
);
1882 void unlock_rename(struct dentry
*p1
, struct dentry
*p2
)
1884 mutex_unlock(&p1
->d_inode
->i_mutex
);
1886 mutex_unlock(&p2
->d_inode
->i_mutex
);
1887 mutex_unlock(&p1
->d_inode
->i_sb
->s_vfs_rename_mutex
);
1891 int vfs_create(struct inode
*dir
, struct dentry
*dentry
, int mode
,
1892 struct nameidata
*nd
)
1894 int error
= may_create(dir
, dentry
);
1899 if (!dir
->i_op
->create
)
1900 return -EACCES
; /* shouldn't it be ENOSYS? */
1903 error
= security_inode_create(dir
, dentry
, mode
);
1906 error
= dir
->i_op
->create(dir
, dentry
, mode
, nd
);
1908 fsnotify_create(dir
, dentry
);
1912 int may_open(struct path
*path
, int acc_mode
, int flag
)
1914 struct dentry
*dentry
= path
->dentry
;
1915 struct inode
*inode
= dentry
->d_inode
;
1921 switch (inode
->i_mode
& S_IFMT
) {
1925 if (acc_mode
& MAY_WRITE
)
1930 if (path
->mnt
->mnt_flags
& MNT_NODEV
)
1939 error
= inode_permission(inode
, acc_mode
);
1944 * An append-only file must be opened in append mode for writing.
1946 if (IS_APPEND(inode
)) {
1947 if ((flag
& O_ACCMODE
) != O_RDONLY
&& !(flag
& O_APPEND
))
1953 /* O_NOATIME can only be set by the owner or superuser */
1954 if (flag
& O_NOATIME
&& !is_owner_or_cap(inode
))
1958 * Ensure there are no outstanding leases on the file.
1960 return break_lease(inode
, flag
);
1963 static int handle_truncate(struct path
*path
)
1965 struct inode
*inode
= path
->dentry
->d_inode
;
1966 int error
= get_write_access(inode
);
1970 * Refuse to truncate files with mandatory locks held on them.
1972 error
= locks_verify_locked(inode
);
1974 error
= security_path_truncate(path
);
1976 error
= do_truncate(path
->dentry
, 0,
1977 ATTR_MTIME
|ATTR_CTIME
|ATTR_OPEN
,
1980 put_write_access(inode
);
1985 * Be careful about ever adding any more callers of this
1986 * function. Its flags must be in the namei format, not
1987 * what get passed to sys_open().
1989 static int __open_namei_create(struct nameidata
*nd
, struct path
*path
,
1990 int open_flag
, int mode
)
1993 struct dentry
*dir
= nd
->path
.dentry
;
1995 if (!IS_POSIXACL(dir
->d_inode
))
1996 mode
&= ~current_umask();
1997 error
= security_path_mknod(&nd
->path
, path
->dentry
, mode
, 0);
2000 error
= vfs_create(dir
->d_inode
, path
->dentry
, mode
, nd
);
2002 mutex_unlock(&dir
->d_inode
->i_mutex
);
2003 dput(nd
->path
.dentry
);
2004 nd
->path
.dentry
= path
->dentry
;
2008 /* Don't check for write permission, don't truncate */
2009 return may_open(&nd
->path
, 0, open_flag
& ~O_TRUNC
);
2013 * Note that while the flag value (low two bits) for sys_open means:
2018 * it is changed into
2019 * 00 - no permissions needed
2020 * 01 - read-permission
2021 * 10 - write-permission
2023 * for the internal routines (ie open_namei()/follow_link() etc)
2024 * This is more logical, and also allows the 00 "no perm needed"
2025 * to be used for symlinks (where the permissions are checked
2029 static inline int open_to_namei_flags(int flag
)
2031 if ((flag
+1) & O_ACCMODE
)
2036 static int open_will_truncate(int flag
, struct inode
*inode
)
2039 * We'll never write to the fs underlying
2042 if (special_file(inode
->i_mode
))
2044 return (flag
& O_TRUNC
);
2047 static struct file
*finish_open(struct nameidata
*nd
,
2048 int open_flag
, int acc_mode
)
2054 will_truncate
= open_will_truncate(open_flag
, nd
->path
.dentry
->d_inode
);
2055 if (will_truncate
) {
2056 error
= mnt_want_write(nd
->path
.mnt
);
2060 error
= may_open(&nd
->path
, acc_mode
, open_flag
);
2063 mnt_drop_write(nd
->path
.mnt
);
2066 filp
= nameidata_to_filp(nd
);
2067 if (!IS_ERR(filp
)) {
2068 error
= ima_file_check(filp
, acc_mode
);
2071 filp
= ERR_PTR(error
);
2074 if (!IS_ERR(filp
)) {
2075 if (will_truncate
) {
2076 error
= handle_truncate(&nd
->path
);
2079 filp
= ERR_PTR(error
);
2084 * It is now safe to drop the mnt write
2085 * because the filp has had a write taken
2089 mnt_drop_write(nd
->path
.mnt
);
2090 path_put(&nd
->path
);
2094 if (!IS_ERR(nd
->intent
.open
.file
))
2095 release_open_intent(nd
);
2096 path_put(&nd
->path
);
2097 return ERR_PTR(error
);
2101 * Handle O_CREAT case for do_filp_open
2103 static struct file
*do_last(struct nameidata
*nd
, struct path
*path
,
2104 int open_flag
, int acc_mode
,
2105 int mode
, const char *pathname
)
2107 struct dentry
*dir
= nd
->path
.dentry
;
2109 int error
= -EISDIR
;
2111 switch (nd
->last_type
) {
2114 dir
= nd
->path
.dentry
;
2116 if (need_reval_dot(dir
)) {
2117 error
= d_revalidate(nd
->path
.dentry
, nd
);
2127 audit_inode(pathname
, dir
);
2131 /* trailing slashes? */
2132 if (nd
->last
.name
[nd
->last
.len
])
2135 mutex_lock(&dir
->d_inode
->i_mutex
);
2137 path
->dentry
= lookup_hash(nd
);
2138 path
->mnt
= nd
->path
.mnt
;
2140 error
= PTR_ERR(path
->dentry
);
2141 if (IS_ERR(path
->dentry
)) {
2142 mutex_unlock(&dir
->d_inode
->i_mutex
);
2146 if (IS_ERR(nd
->intent
.open
.file
)) {
2147 error
= PTR_ERR(nd
->intent
.open
.file
);
2148 goto exit_mutex_unlock
;
2151 /* Negative dentry, just create the file */
2152 if (!path
->dentry
->d_inode
) {
2154 * This write is needed to ensure that a
2155 * ro->rw transition does not occur between
2156 * the time when the file is created and when
2157 * a permanent write count is taken through
2158 * the 'struct file' in nameidata_to_filp().
2160 error
= mnt_want_write(nd
->path
.mnt
);
2162 goto exit_mutex_unlock
;
2163 error
= __open_namei_create(nd
, path
, open_flag
, mode
);
2165 mnt_drop_write(nd
->path
.mnt
);
2168 filp
= nameidata_to_filp(nd
);
2169 mnt_drop_write(nd
->path
.mnt
);
2170 path_put(&nd
->path
);
2171 if (!IS_ERR(filp
)) {
2172 error
= ima_file_check(filp
, acc_mode
);
2175 filp
= ERR_PTR(error
);
2182 * It already exists.
2184 mutex_unlock(&dir
->d_inode
->i_mutex
);
2185 audit_inode(pathname
, path
->dentry
);
2188 if (open_flag
& O_EXCL
)
2191 if (__follow_mount(path
)) {
2193 if (open_flag
& O_NOFOLLOW
)
2198 if (!path
->dentry
->d_inode
)
2201 if (path
->dentry
->d_inode
->i_op
->follow_link
)
2204 path_to_nameidata(path
, nd
);
2205 nd
->inode
= path
->dentry
->d_inode
;
2207 if (S_ISDIR(nd
->inode
->i_mode
))
2210 filp
= finish_open(nd
, open_flag
, acc_mode
);
2214 mutex_unlock(&dir
->d_inode
->i_mutex
);
2216 path_put_conditional(path
, nd
);
2218 if (!IS_ERR(nd
->intent
.open
.file
))
2219 release_open_intent(nd
);
2220 path_put(&nd
->path
);
2221 return ERR_PTR(error
);
2225 * Note that the low bits of the passed in "open_flag"
2226 * are not the same as in the local variable "flag". See
2227 * open_to_namei_flags() for more details.
2229 struct file
*do_filp_open(int dfd
, const char *pathname
,
2230 int open_flag
, int mode
, int acc_mode
)
2233 struct nameidata nd
;
2237 int flag
= open_to_namei_flags(open_flag
);
2240 if (!(open_flag
& O_CREAT
))
2243 /* Must never be set by userspace */
2244 open_flag
&= ~FMODE_NONOTIFY
;
2247 * O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only
2248 * check for O_DSYNC if the need any syncing at all we enforce it's
2249 * always set instead of having to deal with possibly weird behaviour
2250 * for malicious applications setting only __O_SYNC.
2252 if (open_flag
& __O_SYNC
)
2253 open_flag
|= O_DSYNC
;
2256 acc_mode
= MAY_OPEN
| ACC_MODE(open_flag
);
2258 /* O_TRUNC implies we need access checks for write permissions */
2259 if (open_flag
& O_TRUNC
)
2260 acc_mode
|= MAY_WRITE
;
2262 /* Allow the LSM permission hook to distinguish append
2263 access from general write access. */
2264 if (open_flag
& O_APPEND
)
2265 acc_mode
|= MAY_APPEND
;
2267 flags
= LOOKUP_OPEN
;
2268 if (open_flag
& O_CREAT
) {
2269 flags
|= LOOKUP_CREATE
;
2270 if (open_flag
& O_EXCL
)
2271 flags
|= LOOKUP_EXCL
;
2273 if (open_flag
& O_DIRECTORY
)
2274 flags
|= LOOKUP_DIRECTORY
;
2275 if (!(open_flag
& O_NOFOLLOW
))
2276 flags
|= LOOKUP_FOLLOW
;
2278 filp
= get_empty_filp();
2280 return ERR_PTR(-ENFILE
);
2282 filp
->f_flags
= open_flag
;
2283 nd
.intent
.open
.file
= filp
;
2284 nd
.intent
.open
.flags
= flag
;
2285 nd
.intent
.open
.create_mode
= mode
;
2287 if (open_flag
& O_CREAT
)
2290 /* !O_CREAT, simple open */
2291 error
= do_path_lookup(dfd
, pathname
, flags
, &nd
);
2292 if (unlikely(error
))
2295 if (!(nd
.flags
& LOOKUP_FOLLOW
)) {
2296 if (nd
.inode
->i_op
->follow_link
)
2300 if (nd
.flags
& LOOKUP_DIRECTORY
) {
2301 if (!nd
.inode
->i_op
->lookup
)
2304 audit_inode(pathname
, nd
.path
.dentry
);
2305 filp
= finish_open(&nd
, open_flag
, acc_mode
);
2309 /* OK, have to create the file. Find the parent. */
2310 error
= path_init_rcu(dfd
, pathname
,
2311 LOOKUP_PARENT
| (flags
& LOOKUP_REVAL
), &nd
);
2314 error
= path_walk_rcu(pathname
, &nd
);
2315 path_finish_rcu(&nd
);
2316 if (unlikely(error
== -ECHILD
|| error
== -ESTALE
)) {
2317 /* slower, locked walk */
2318 if (error
== -ESTALE
) {
2320 flags
|= LOOKUP_REVAL
;
2322 error
= path_init(dfd
, pathname
,
2323 LOOKUP_PARENT
| (flags
& LOOKUP_REVAL
), &nd
);
2327 error
= path_walk_simple(pathname
, &nd
);
2329 if (unlikely(error
))
2331 if (unlikely(!audit_dummy_context()))
2332 audit_inode(pathname
, nd
.path
.dentry
);
2335 * We have the parent and last component.
2338 filp
= do_last(&nd
, &path
, open_flag
, acc_mode
, mode
, pathname
);
2339 while (unlikely(!filp
)) { /* trailing symlink */
2343 /* S_ISDIR part is a temporary automount kludge */
2344 if (!(nd
.flags
& LOOKUP_FOLLOW
) && !S_ISDIR(nd
.inode
->i_mode
))
2349 * This is subtle. Instead of calling do_follow_link() we do
2350 * the thing by hands. The reason is that this way we have zero
2351 * link_count and path_walk() (called from ->follow_link)
2352 * honoring LOOKUP_PARENT. After that we have the parent and
2353 * last component, i.e. we are in the same situation as after
2354 * the first path_walk(). Well, almost - if the last component
2355 * is normal we get its copy stored in nd->last.name and we will
2356 * have to putname() it when we are done. Procfs-like symlinks
2357 * just set LAST_BIND.
2359 nd
.flags
|= LOOKUP_PARENT
;
2360 error
= security_inode_follow_link(path
.dentry
, &nd
);
2363 error
= __do_follow_link(&path
, &nd
, &cookie
);
2364 if (unlikely(error
)) {
2365 if (!IS_ERR(cookie
) && nd
.inode
->i_op
->put_link
)
2366 nd
.inode
->i_op
->put_link(path
.dentry
, &nd
, cookie
);
2367 /* nd.path had been dropped */
2372 nd
.flags
&= ~LOOKUP_PARENT
;
2373 filp
= do_last(&nd
, &path
, open_flag
, acc_mode
, mode
, pathname
);
2374 if (nd
.inode
->i_op
->put_link
)
2375 nd
.inode
->i_op
->put_link(holder
.dentry
, &nd
, cookie
);
2381 if (filp
== ERR_PTR(-ESTALE
) && !(flags
& LOOKUP_REVAL
))
2386 path_put_conditional(&path
, &nd
);
2390 if (!IS_ERR(nd
.intent
.open
.file
))
2391 release_open_intent(&nd
);
2392 filp
= ERR_PTR(error
);
2397 * filp_open - open file and return file pointer
2399 * @filename: path to open
2400 * @flags: open flags as per the open(2) second argument
2401 * @mode: mode for the new file if O_CREAT is set, else ignored
2403 * This is the helper to open a file from kernelspace if you really
2404 * have to. But in generally you should not do this, so please move
2405 * along, nothing to see here..
2407 struct file
*filp_open(const char *filename
, int flags
, int mode
)
2409 return do_filp_open(AT_FDCWD
, filename
, flags
, mode
, 0);
2411 EXPORT_SYMBOL(filp_open
);
2414 * lookup_create - lookup a dentry, creating it if it doesn't exist
2415 * @nd: nameidata info
2416 * @is_dir: directory flag
2418 * Simple function to lookup and return a dentry and create it
2419 * if it doesn't exist. Is SMP-safe.
2421 * Returns with nd->path.dentry->d_inode->i_mutex locked.
2423 struct dentry
*lookup_create(struct nameidata
*nd
, int is_dir
)
2425 struct dentry
*dentry
= ERR_PTR(-EEXIST
);
2427 mutex_lock_nested(&nd
->path
.dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
2429 * Yucky last component or no last component at all?
2430 * (foo/., foo/.., /////)
2432 if (nd
->last_type
!= LAST_NORM
)
2434 nd
->flags
&= ~LOOKUP_PARENT
;
2435 nd
->flags
|= LOOKUP_CREATE
| LOOKUP_EXCL
;
2436 nd
->intent
.open
.flags
= O_EXCL
;
2439 * Do the final lookup.
2441 dentry
= lookup_hash(nd
);
2445 if (dentry
->d_inode
)
2448 * Special case - lookup gave negative, but... we had foo/bar/
2449 * From the vfs_mknod() POV we just have a negative dentry -
2450 * all is fine. Let's be bastards - you had / on the end, you've
2451 * been asking for (non-existent) directory. -ENOENT for you.
2453 if (unlikely(!is_dir
&& nd
->last
.name
[nd
->last
.len
])) {
2455 dentry
= ERR_PTR(-ENOENT
);
2460 dentry
= ERR_PTR(-EEXIST
);
2464 EXPORT_SYMBOL_GPL(lookup_create
);
2466 int vfs_mknod(struct inode
*dir
, struct dentry
*dentry
, int mode
, dev_t dev
)
2468 int error
= may_create(dir
, dentry
);
2473 if ((S_ISCHR(mode
) || S_ISBLK(mode
)) && !capable(CAP_MKNOD
))
2476 if (!dir
->i_op
->mknod
)
2479 error
= devcgroup_inode_mknod(mode
, dev
);
2483 error
= security_inode_mknod(dir
, dentry
, mode
, dev
);
2487 error
= dir
->i_op
->mknod(dir
, dentry
, mode
, dev
);
2489 fsnotify_create(dir
, dentry
);
2493 static int may_mknod(mode_t mode
)
2495 switch (mode
& S_IFMT
) {
2501 case 0: /* zero mode translates to S_IFREG */
2510 SYSCALL_DEFINE4(mknodat
, int, dfd
, const char __user
*, filename
, int, mode
,
2515 struct dentry
*dentry
;
2516 struct nameidata nd
;
2521 error
= user_path_parent(dfd
, filename
, &nd
, &tmp
);
2525 dentry
= lookup_create(&nd
, 0);
2526 if (IS_ERR(dentry
)) {
2527 error
= PTR_ERR(dentry
);
2530 if (!IS_POSIXACL(nd
.path
.dentry
->d_inode
))
2531 mode
&= ~current_umask();
2532 error
= may_mknod(mode
);
2535 error
= mnt_want_write(nd
.path
.mnt
);
2538 error
= security_path_mknod(&nd
.path
, dentry
, mode
, dev
);
2540 goto out_drop_write
;
2541 switch (mode
& S_IFMT
) {
2542 case 0: case S_IFREG
:
2543 error
= vfs_create(nd
.path
.dentry
->d_inode
,dentry
,mode
,&nd
);
2545 case S_IFCHR
: case S_IFBLK
:
2546 error
= vfs_mknod(nd
.path
.dentry
->d_inode
,dentry
,mode
,
2547 new_decode_dev(dev
));
2549 case S_IFIFO
: case S_IFSOCK
:
2550 error
= vfs_mknod(nd
.path
.dentry
->d_inode
,dentry
,mode
,0);
2554 mnt_drop_write(nd
.path
.mnt
);
2558 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
2565 SYSCALL_DEFINE3(mknod
, const char __user
*, filename
, int, mode
, unsigned, dev
)
2567 return sys_mknodat(AT_FDCWD
, filename
, mode
, dev
);
2570 int vfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
2572 int error
= may_create(dir
, dentry
);
2577 if (!dir
->i_op
->mkdir
)
2580 mode
&= (S_IRWXUGO
|S_ISVTX
);
2581 error
= security_inode_mkdir(dir
, dentry
, mode
);
2585 error
= dir
->i_op
->mkdir(dir
, dentry
, mode
);
2587 fsnotify_mkdir(dir
, dentry
);
2591 SYSCALL_DEFINE3(mkdirat
, int, dfd
, const char __user
*, pathname
, int, mode
)
2595 struct dentry
*dentry
;
2596 struct nameidata nd
;
2598 error
= user_path_parent(dfd
, pathname
, &nd
, &tmp
);
2602 dentry
= lookup_create(&nd
, 1);
2603 error
= PTR_ERR(dentry
);
2607 if (!IS_POSIXACL(nd
.path
.dentry
->d_inode
))
2608 mode
&= ~current_umask();
2609 error
= mnt_want_write(nd
.path
.mnt
);
2612 error
= security_path_mkdir(&nd
.path
, dentry
, mode
);
2614 goto out_drop_write
;
2615 error
= vfs_mkdir(nd
.path
.dentry
->d_inode
, dentry
, mode
);
2617 mnt_drop_write(nd
.path
.mnt
);
2621 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
2628 SYSCALL_DEFINE2(mkdir
, const char __user
*, pathname
, int, mode
)
2630 return sys_mkdirat(AT_FDCWD
, pathname
, mode
);
2634 * We try to drop the dentry early: we should have
2635 * a usage count of 2 if we're the only user of this
2636 * dentry, and if that is true (possibly after pruning
2637 * the dcache), then we drop the dentry now.
2639 * A low-level filesystem can, if it choses, legally
2642 * if (!d_unhashed(dentry))
2645 * if it cannot handle the case of removing a directory
2646 * that is still in use by something else..
2648 void dentry_unhash(struct dentry
*dentry
)
2651 shrink_dcache_parent(dentry
);
2652 spin_lock(&dentry
->d_lock
);
2653 if (dentry
->d_count
== 2)
2655 spin_unlock(&dentry
->d_lock
);
2658 int vfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
2660 int error
= may_delete(dir
, dentry
, 1);
2665 if (!dir
->i_op
->rmdir
)
2668 mutex_lock(&dentry
->d_inode
->i_mutex
);
2669 dentry_unhash(dentry
);
2670 if (d_mountpoint(dentry
))
2673 error
= security_inode_rmdir(dir
, dentry
);
2675 error
= dir
->i_op
->rmdir(dir
, dentry
);
2677 dentry
->d_inode
->i_flags
|= S_DEAD
;
2682 mutex_unlock(&dentry
->d_inode
->i_mutex
);
2691 static long do_rmdir(int dfd
, const char __user
*pathname
)
2695 struct dentry
*dentry
;
2696 struct nameidata nd
;
2698 error
= user_path_parent(dfd
, pathname
, &nd
, &name
);
2702 switch(nd
.last_type
) {
2714 nd
.flags
&= ~LOOKUP_PARENT
;
2716 mutex_lock_nested(&nd
.path
.dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
2717 dentry
= lookup_hash(&nd
);
2718 error
= PTR_ERR(dentry
);
2721 error
= mnt_want_write(nd
.path
.mnt
);
2724 error
= security_path_rmdir(&nd
.path
, dentry
);
2727 error
= vfs_rmdir(nd
.path
.dentry
->d_inode
, dentry
);
2729 mnt_drop_write(nd
.path
.mnt
);
2733 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
2740 SYSCALL_DEFINE1(rmdir
, const char __user
*, pathname
)
2742 return do_rmdir(AT_FDCWD
, pathname
);
2745 int vfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
2747 int error
= may_delete(dir
, dentry
, 0);
2752 if (!dir
->i_op
->unlink
)
2755 mutex_lock(&dentry
->d_inode
->i_mutex
);
2756 if (d_mountpoint(dentry
))
2759 error
= security_inode_unlink(dir
, dentry
);
2761 error
= dir
->i_op
->unlink(dir
, dentry
);
2766 mutex_unlock(&dentry
->d_inode
->i_mutex
);
2768 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2769 if (!error
&& !(dentry
->d_flags
& DCACHE_NFSFS_RENAMED
)) {
2770 fsnotify_link_count(dentry
->d_inode
);
2778 * Make sure that the actual truncation of the file will occur outside its
2779 * directory's i_mutex. Truncate can take a long time if there is a lot of
2780 * writeout happening, and we don't want to prevent access to the directory
2781 * while waiting on the I/O.
2783 static long do_unlinkat(int dfd
, const char __user
*pathname
)
2787 struct dentry
*dentry
;
2788 struct nameidata nd
;
2789 struct inode
*inode
= NULL
;
2791 error
= user_path_parent(dfd
, pathname
, &nd
, &name
);
2796 if (nd
.last_type
!= LAST_NORM
)
2799 nd
.flags
&= ~LOOKUP_PARENT
;
2801 mutex_lock_nested(&nd
.path
.dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
2802 dentry
= lookup_hash(&nd
);
2803 error
= PTR_ERR(dentry
);
2804 if (!IS_ERR(dentry
)) {
2805 /* Why not before? Because we want correct error value */
2806 if (nd
.last
.name
[nd
.last
.len
])
2808 inode
= dentry
->d_inode
;
2811 error
= mnt_want_write(nd
.path
.mnt
);
2814 error
= security_path_unlink(&nd
.path
, dentry
);
2817 error
= vfs_unlink(nd
.path
.dentry
->d_inode
, dentry
);
2819 mnt_drop_write(nd
.path
.mnt
);
2823 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
2825 iput(inode
); /* truncate the inode here */
2832 error
= !dentry
->d_inode
? -ENOENT
:
2833 S_ISDIR(dentry
->d_inode
->i_mode
) ? -EISDIR
: -ENOTDIR
;
2837 SYSCALL_DEFINE3(unlinkat
, int, dfd
, const char __user
*, pathname
, int, flag
)
2839 if ((flag
& ~AT_REMOVEDIR
) != 0)
2842 if (flag
& AT_REMOVEDIR
)
2843 return do_rmdir(dfd
, pathname
);
2845 return do_unlinkat(dfd
, pathname
);
2848 SYSCALL_DEFINE1(unlink
, const char __user
*, pathname
)
2850 return do_unlinkat(AT_FDCWD
, pathname
);
2853 int vfs_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *oldname
)
2855 int error
= may_create(dir
, dentry
);
2860 if (!dir
->i_op
->symlink
)
2863 error
= security_inode_symlink(dir
, dentry
, oldname
);
2867 error
= dir
->i_op
->symlink(dir
, dentry
, oldname
);
2869 fsnotify_create(dir
, dentry
);
2873 SYSCALL_DEFINE3(symlinkat
, const char __user
*, oldname
,
2874 int, newdfd
, const char __user
*, newname
)
2879 struct dentry
*dentry
;
2880 struct nameidata nd
;
2882 from
= getname(oldname
);
2884 return PTR_ERR(from
);
2886 error
= user_path_parent(newdfd
, newname
, &nd
, &to
);
2890 dentry
= lookup_create(&nd
, 0);
2891 error
= PTR_ERR(dentry
);
2895 error
= mnt_want_write(nd
.path
.mnt
);
2898 error
= security_path_symlink(&nd
.path
, dentry
, from
);
2900 goto out_drop_write
;
2901 error
= vfs_symlink(nd
.path
.dentry
->d_inode
, dentry
, from
);
2903 mnt_drop_write(nd
.path
.mnt
);
2907 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
2915 SYSCALL_DEFINE2(symlink
, const char __user
*, oldname
, const char __user
*, newname
)
2917 return sys_symlinkat(oldname
, AT_FDCWD
, newname
);
2920 int vfs_link(struct dentry
*old_dentry
, struct inode
*dir
, struct dentry
*new_dentry
)
2922 struct inode
*inode
= old_dentry
->d_inode
;
2928 error
= may_create(dir
, new_dentry
);
2932 if (dir
->i_sb
!= inode
->i_sb
)
2936 * A link to an append-only or immutable file cannot be created.
2938 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
2940 if (!dir
->i_op
->link
)
2942 if (S_ISDIR(inode
->i_mode
))
2945 error
= security_inode_link(old_dentry
, dir
, new_dentry
);
2949 mutex_lock(&inode
->i_mutex
);
2950 error
= dir
->i_op
->link(old_dentry
, dir
, new_dentry
);
2951 mutex_unlock(&inode
->i_mutex
);
2953 fsnotify_link(dir
, inode
, new_dentry
);
2958 * Hardlinks are often used in delicate situations. We avoid
2959 * security-related surprises by not following symlinks on the
2962 * We don't follow them on the oldname either to be compatible
2963 * with linux 2.0, and to avoid hard-linking to directories
2964 * and other special files. --ADM
2966 SYSCALL_DEFINE5(linkat
, int, olddfd
, const char __user
*, oldname
,
2967 int, newdfd
, const char __user
*, newname
, int, flags
)
2969 struct dentry
*new_dentry
;
2970 struct nameidata nd
;
2971 struct path old_path
;
2975 if ((flags
& ~AT_SYMLINK_FOLLOW
) != 0)
2978 error
= user_path_at(olddfd
, oldname
,
2979 flags
& AT_SYMLINK_FOLLOW
? LOOKUP_FOLLOW
: 0,
2984 error
= user_path_parent(newdfd
, newname
, &nd
, &to
);
2988 if (old_path
.mnt
!= nd
.path
.mnt
)
2990 new_dentry
= lookup_create(&nd
, 0);
2991 error
= PTR_ERR(new_dentry
);
2992 if (IS_ERR(new_dentry
))
2994 error
= mnt_want_write(nd
.path
.mnt
);
2997 error
= security_path_link(old_path
.dentry
, &nd
.path
, new_dentry
);
2999 goto out_drop_write
;
3000 error
= vfs_link(old_path
.dentry
, nd
.path
.dentry
->d_inode
, new_dentry
);
3002 mnt_drop_write(nd
.path
.mnt
);
3006 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
3011 path_put(&old_path
);
3016 SYSCALL_DEFINE2(link
, const char __user
*, oldname
, const char __user
*, newname
)
3018 return sys_linkat(AT_FDCWD
, oldname
, AT_FDCWD
, newname
, 0);
3022 * The worst of all namespace operations - renaming directory. "Perverted"
3023 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
3025 * a) we can get into loop creation. Check is done in is_subdir().
3026 * b) race potential - two innocent renames can create a loop together.
3027 * That's where 4.4 screws up. Current fix: serialization on
3028 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
3030 * c) we have to lock _three_ objects - parents and victim (if it exists).
3031 * And that - after we got ->i_mutex on parents (until then we don't know
3032 * whether the target exists). Solution: try to be smart with locking
3033 * order for inodes. We rely on the fact that tree topology may change
3034 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
3035 * move will be locked. Thus we can rank directories by the tree
3036 * (ancestors first) and rank all non-directories after them.
3037 * That works since everybody except rename does "lock parent, lookup,
3038 * lock child" and rename is under ->s_vfs_rename_mutex.
3039 * HOWEVER, it relies on the assumption that any object with ->lookup()
3040 * has no more than 1 dentry. If "hybrid" objects will ever appear,
3041 * we'd better make sure that there's no link(2) for them.
3042 * d) some filesystems don't support opened-but-unlinked directories,
3043 * either because of layout or because they are not ready to deal with
3044 * all cases correctly. The latter will be fixed (taking this sort of
3045 * stuff into VFS), but the former is not going away. Solution: the same
3046 * trick as in rmdir().
3047 * e) conversion from fhandle to dentry may come in the wrong moment - when
3048 * we are removing the target. Solution: we will have to grab ->i_mutex
3049 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
3050 * ->i_mutex on parents, which works but leads to some truly excessive
3053 static int vfs_rename_dir(struct inode
*old_dir
, struct dentry
*old_dentry
,
3054 struct inode
*new_dir
, struct dentry
*new_dentry
)
3057 struct inode
*target
;
3060 * If we are going to change the parent - check write permissions,
3061 * we'll need to flip '..'.
3063 if (new_dir
!= old_dir
) {
3064 error
= inode_permission(old_dentry
->d_inode
, MAY_WRITE
);
3069 error
= security_inode_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
3073 target
= new_dentry
->d_inode
;
3075 mutex_lock(&target
->i_mutex
);
3076 if (d_mountpoint(old_dentry
)||d_mountpoint(new_dentry
))
3080 dentry_unhash(new_dentry
);
3081 error
= old_dir
->i_op
->rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
3085 target
->i_flags
|= S_DEAD
;
3086 dont_mount(new_dentry
);
3088 mutex_unlock(&target
->i_mutex
);
3089 if (d_unhashed(new_dentry
))
3090 d_rehash(new_dentry
);
3094 if (!(old_dir
->i_sb
->s_type
->fs_flags
& FS_RENAME_DOES_D_MOVE
))
3095 d_move(old_dentry
,new_dentry
);
3099 static int vfs_rename_other(struct inode
*old_dir
, struct dentry
*old_dentry
,
3100 struct inode
*new_dir
, struct dentry
*new_dentry
)
3102 struct inode
*target
;
3105 error
= security_inode_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
3110 target
= new_dentry
->d_inode
;
3112 mutex_lock(&target
->i_mutex
);
3113 if (d_mountpoint(old_dentry
)||d_mountpoint(new_dentry
))
3116 error
= old_dir
->i_op
->rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
3119 dont_mount(new_dentry
);
3120 if (!(old_dir
->i_sb
->s_type
->fs_flags
& FS_RENAME_DOES_D_MOVE
))
3121 d_move(old_dentry
, new_dentry
);
3124 mutex_unlock(&target
->i_mutex
);
3129 int vfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
3130 struct inode
*new_dir
, struct dentry
*new_dentry
)
3133 int is_dir
= S_ISDIR(old_dentry
->d_inode
->i_mode
);
3134 const unsigned char *old_name
;
3136 if (old_dentry
->d_inode
== new_dentry
->d_inode
)
3139 error
= may_delete(old_dir
, old_dentry
, is_dir
);
3143 if (!new_dentry
->d_inode
)
3144 error
= may_create(new_dir
, new_dentry
);
3146 error
= may_delete(new_dir
, new_dentry
, is_dir
);
3150 if (!old_dir
->i_op
->rename
)
3153 old_name
= fsnotify_oldname_init(old_dentry
->d_name
.name
);
3156 error
= vfs_rename_dir(old_dir
,old_dentry
,new_dir
,new_dentry
);
3158 error
= vfs_rename_other(old_dir
,old_dentry
,new_dir
,new_dentry
);
3160 fsnotify_move(old_dir
, new_dir
, old_name
, is_dir
,
3161 new_dentry
->d_inode
, old_dentry
);
3162 fsnotify_oldname_free(old_name
);
3167 SYSCALL_DEFINE4(renameat
, int, olddfd
, const char __user
*, oldname
,
3168 int, newdfd
, const char __user
*, newname
)
3170 struct dentry
*old_dir
, *new_dir
;
3171 struct dentry
*old_dentry
, *new_dentry
;
3172 struct dentry
*trap
;
3173 struct nameidata oldnd
, newnd
;
3178 error
= user_path_parent(olddfd
, oldname
, &oldnd
, &from
);
3182 error
= user_path_parent(newdfd
, newname
, &newnd
, &to
);
3187 if (oldnd
.path
.mnt
!= newnd
.path
.mnt
)
3190 old_dir
= oldnd
.path
.dentry
;
3192 if (oldnd
.last_type
!= LAST_NORM
)
3195 new_dir
= newnd
.path
.dentry
;
3196 if (newnd
.last_type
!= LAST_NORM
)
3199 oldnd
.flags
&= ~LOOKUP_PARENT
;
3200 newnd
.flags
&= ~LOOKUP_PARENT
;
3201 newnd
.flags
|= LOOKUP_RENAME_TARGET
;
3203 trap
= lock_rename(new_dir
, old_dir
);
3205 old_dentry
= lookup_hash(&oldnd
);
3206 error
= PTR_ERR(old_dentry
);
3207 if (IS_ERR(old_dentry
))
3209 /* source must exist */
3211 if (!old_dentry
->d_inode
)
3213 /* unless the source is a directory trailing slashes give -ENOTDIR */
3214 if (!S_ISDIR(old_dentry
->d_inode
->i_mode
)) {
3216 if (oldnd
.last
.name
[oldnd
.last
.len
])
3218 if (newnd
.last
.name
[newnd
.last
.len
])
3221 /* source should not be ancestor of target */
3223 if (old_dentry
== trap
)
3225 new_dentry
= lookup_hash(&newnd
);
3226 error
= PTR_ERR(new_dentry
);
3227 if (IS_ERR(new_dentry
))
3229 /* target should not be an ancestor of source */
3231 if (new_dentry
== trap
)
3234 error
= mnt_want_write(oldnd
.path
.mnt
);
3237 error
= security_path_rename(&oldnd
.path
, old_dentry
,
3238 &newnd
.path
, new_dentry
);
3241 error
= vfs_rename(old_dir
->d_inode
, old_dentry
,
3242 new_dir
->d_inode
, new_dentry
);
3244 mnt_drop_write(oldnd
.path
.mnt
);
3250 unlock_rename(new_dir
, old_dir
);
3252 path_put(&newnd
.path
);
3255 path_put(&oldnd
.path
);
3261 SYSCALL_DEFINE2(rename
, const char __user
*, oldname
, const char __user
*, newname
)
3263 return sys_renameat(AT_FDCWD
, oldname
, AT_FDCWD
, newname
);
3266 int vfs_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
, const char *link
)
3270 len
= PTR_ERR(link
);
3275 if (len
> (unsigned) buflen
)
3277 if (copy_to_user(buffer
, link
, len
))
3284 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
3285 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
3286 * using) it for any given inode is up to filesystem.
3288 int generic_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
)
3290 struct nameidata nd
;
3295 cookie
= dentry
->d_inode
->i_op
->follow_link(dentry
, &nd
);
3297 return PTR_ERR(cookie
);
3299 res
= vfs_readlink(dentry
, buffer
, buflen
, nd_get_link(&nd
));
3300 if (dentry
->d_inode
->i_op
->put_link
)
3301 dentry
->d_inode
->i_op
->put_link(dentry
, &nd
, cookie
);
3305 int vfs_follow_link(struct nameidata
*nd
, const char *link
)
3307 return __vfs_follow_link(nd
, link
);
3310 /* get the link contents into pagecache */
3311 static char *page_getlink(struct dentry
* dentry
, struct page
**ppage
)
3315 struct address_space
*mapping
= dentry
->d_inode
->i_mapping
;
3316 page
= read_mapping_page(mapping
, 0, NULL
);
3321 nd_terminate_link(kaddr
, dentry
->d_inode
->i_size
, PAGE_SIZE
- 1);
3325 int page_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
)
3327 struct page
*page
= NULL
;
3328 char *s
= page_getlink(dentry
, &page
);
3329 int res
= vfs_readlink(dentry
,buffer
,buflen
,s
);
3332 page_cache_release(page
);
3337 void *page_follow_link_light(struct dentry
*dentry
, struct nameidata
*nd
)
3339 struct page
*page
= NULL
;
3340 nd_set_link(nd
, page_getlink(dentry
, &page
));
3344 void page_put_link(struct dentry
*dentry
, struct nameidata
*nd
, void *cookie
)
3346 struct page
*page
= cookie
;
3350 page_cache_release(page
);
3355 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
3357 int __page_symlink(struct inode
*inode
, const char *symname
, int len
, int nofs
)
3359 struct address_space
*mapping
= inode
->i_mapping
;
3364 unsigned int flags
= AOP_FLAG_UNINTERRUPTIBLE
;
3366 flags
|= AOP_FLAG_NOFS
;
3369 err
= pagecache_write_begin(NULL
, mapping
, 0, len
-1,
3370 flags
, &page
, &fsdata
);
3374 kaddr
= kmap_atomic(page
, KM_USER0
);
3375 memcpy(kaddr
, symname
, len
-1);
3376 kunmap_atomic(kaddr
, KM_USER0
);
3378 err
= pagecache_write_end(NULL
, mapping
, 0, len
-1, len
-1,
3385 mark_inode_dirty(inode
);
3391 int page_symlink(struct inode
*inode
, const char *symname
, int len
)
3393 return __page_symlink(inode
, symname
, len
,
3394 !(mapping_gfp_mask(inode
->i_mapping
) & __GFP_FS
));
3397 const struct inode_operations page_symlink_inode_operations
= {
3398 .readlink
= generic_readlink
,
3399 .follow_link
= page_follow_link_light
,
3400 .put_link
= page_put_link
,
3403 EXPORT_SYMBOL(user_path_at
);
3404 EXPORT_SYMBOL(follow_down
);
3405 EXPORT_SYMBOL(follow_up
);
3406 EXPORT_SYMBOL(get_write_access
); /* binfmt_aout */
3407 EXPORT_SYMBOL(getname
);
3408 EXPORT_SYMBOL(lock_rename
);
3409 EXPORT_SYMBOL(lookup_one_len
);
3410 EXPORT_SYMBOL(page_follow_link_light
);
3411 EXPORT_SYMBOL(page_put_link
);
3412 EXPORT_SYMBOL(page_readlink
);
3413 EXPORT_SYMBOL(__page_symlink
);
3414 EXPORT_SYMBOL(page_symlink
);
3415 EXPORT_SYMBOL(page_symlink_inode_operations
);
3416 EXPORT_SYMBOL(path_lookup
);
3417 EXPORT_SYMBOL(kern_path
);
3418 EXPORT_SYMBOL(vfs_path_lookup
);
3419 EXPORT_SYMBOL(inode_permission
);
3420 EXPORT_SYMBOL(file_permission
);
3421 EXPORT_SYMBOL(unlock_rename
);
3422 EXPORT_SYMBOL(vfs_create
);
3423 EXPORT_SYMBOL(vfs_follow_link
);
3424 EXPORT_SYMBOL(vfs_link
);
3425 EXPORT_SYMBOL(vfs_mkdir
);
3426 EXPORT_SYMBOL(vfs_mknod
);
3427 EXPORT_SYMBOL(generic_permission
);
3428 EXPORT_SYMBOL(vfs_readlink
);
3429 EXPORT_SYMBOL(vfs_rename
);
3430 EXPORT_SYMBOL(vfs_rmdir
);
3431 EXPORT_SYMBOL(vfs_symlink
);
3432 EXPORT_SYMBOL(vfs_unlink
);
3433 EXPORT_SYMBOL(dentry_unhash
);
3434 EXPORT_SYMBOL(generic_readlink
);