blob: ec638d27642f2016cd354dd0972022f46ee92728 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/namei.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7/*
8 * Some corrections by tytso.
9 */
10
11/* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
12 * lookup logic.
13 */
14/* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
15 */
16
17#include <linux/init.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050018#include <linux/export.h>
David S. Miller44696902012-05-23 20:12:50 -070019#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/slab.h>
21#include <linux/fs.h>
22#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/pagemap.h>
Robert Love0eeca282005-07-12 17:06:03 -040024#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/personality.h>
26#include <linux/security.h>
Mimi Zohar6146f0d2009-02-04 09:06:57 -050027#include <linux/ima.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/syscalls.h>
29#include <linux/mount.h>
30#include <linux/audit.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080031#include <linux/capability.h>
Trond Myklebust834f2a42005-10-18 14:20:16 -070032#include <linux/file.h>
Ulrich Drepper5590ff02006-01-18 17:43:53 -080033#include <linux/fcntl.h>
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070034#include <linux/device_cgroup.h>
Al Viro5ad4e532009-03-29 19:50:06 -040035#include <linux/fs_struct.h>
Linus Torvaldse77819e2011-07-22 19:30:19 -070036#include <linux/posix_acl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <asm/uaccess.h>
38
Eric Parise81e3f42009-12-04 15:47:36 -050039#include "internal.h"
Al Viroc7105362011-11-24 18:22:03 -050040#include "mount.h"
Eric Parise81e3f42009-12-04 15:47:36 -050041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042/* [Feb-1997 T. Schoebel-Theuer]
43 * Fundamental changes in the pathname lookup mechanisms (namei)
44 * were necessary because of omirr. The reason is that omirr needs
45 * to know the _real_ pathname, not the user-supplied one, in case
46 * of symlinks (and also when transname replacements occur).
47 *
48 * The new code replaces the old recursive symlink resolution with
49 * an iterative one (in case of non-nested symlink chains). It does
50 * this with calls to <fs>_follow_link().
51 * As a side effect, dir_namei(), _namei() and follow_link() are now
52 * replaced with a single function lookup_dentry() that can handle all
53 * the special cases of the former code.
54 *
55 * With the new dcache, the pathname is stored at each inode, at least as
56 * long as the refcount of the inode is positive. As a side effect, the
57 * size of the dcache depends on the inode cache and thus is dynamic.
58 *
59 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
60 * resolution to correspond with current state of the code.
61 *
62 * Note that the symlink resolution is not *completely* iterative.
63 * There is still a significant amount of tail- and mid- recursion in
64 * the algorithm. Also, note that <fs>_readlink() is not used in
65 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
66 * may return different results than <fs>_follow_link(). Many virtual
67 * filesystems (including /proc) exhibit this behavior.
68 */
69
70/* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
71 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
72 * and the name already exists in form of a symlink, try to create the new
73 * name indicated by the symlink. The old code always complained that the
74 * name already exists, due to not following the symlink even if its target
75 * is nonexistent. The new semantics affects also mknod() and link() when
Lucas De Marchi25985ed2011-03-30 22:57:33 -030076 * the name is a symlink pointing to a non-existent name.
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 *
78 * I don't know which semantics is the right one, since I have no access
79 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
80 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
81 * "old" one. Personally, I think the new semantics is much more logical.
82 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
83 * file does succeed in both HP-UX and SunOs, but not in Solaris
84 * and in the old Linux semantics.
85 */
86
87/* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
88 * semantics. See the comments in "open_namei" and "do_link" below.
89 *
90 * [10-Sep-98 Alan Modra] Another symlink change.
91 */
92
93/* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
94 * inside the path - always follow.
95 * in the last component in creation/removal/renaming - never follow.
96 * if LOOKUP_FOLLOW passed - follow.
97 * if the pathname has trailing slashes - follow.
98 * otherwise - don't follow.
99 * (applied in that order).
100 *
101 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
102 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
103 * During the 2.4 we need to fix the userland stuff depending on it -
104 * hopefully we will be able to get rid of that wart in 2.5. So far only
105 * XEmacs seems to be relying on it...
106 */
107/*
108 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
Arjan van de Vena11f3a02006-03-23 03:00:33 -0800109 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 * any extra contention...
111 */
112
113/* In order to reduce some races, while at the same time doing additional
114 * checking and hopefully speeding things up, we copy filenames to the
115 * kernel data space before using them..
116 *
117 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
118 * PATH_MAX includes the nul terminator --RR.
119 */
Jeff Layton91a27b22012-10-10 15:25:28 -0400120void final_putname(struct filename *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Jeff Layton91a27b22012-10-10 15:25:28 -0400122 __putname(name->name);
123 kfree(name);
124}
125
126static struct filename *
127getname_flags(const char __user *filename, int flags, int *empty)
128{
129 struct filename *result, *err;
130 char *kname;
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700131 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Jeff Layton91a27b22012-10-10 15:25:28 -0400133 /* FIXME: create dedicated slabcache? */
134 result = kzalloc(sizeof(*result), GFP_KERNEL);
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700135 if (unlikely(!result))
Eric Paris4043cde2012-01-03 14:23:08 -0500136 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Jeff Layton91a27b22012-10-10 15:25:28 -0400138 kname = __getname();
139 if (unlikely(!kname)) {
140 err = ERR_PTR(-ENOMEM);
141 goto error_free_name;
142 }
143
144 result->name = kname;
145 result->uptr = filename;
146 len = strncpy_from_user(kname, filename, PATH_MAX);
147 if (unlikely(len < 0)) {
148 err = ERR_PTR(len);
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700149 goto error;
Jeff Layton91a27b22012-10-10 15:25:28 -0400150 }
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700151
152 /* The empty path is special. */
153 if (unlikely(!len)) {
154 if (empty)
Eric Paris4043cde2012-01-03 14:23:08 -0500155 *empty = 1;
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700156 err = ERR_PTR(-ENOENT);
157 if (!(flags & LOOKUP_EMPTY))
158 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 }
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700160
161 err = ERR_PTR(-ENAMETOOLONG);
162 if (likely(len < PATH_MAX)) {
163 audit_getname(result);
164 return result;
165 }
166
167error:
Jeff Layton91a27b22012-10-10 15:25:28 -0400168 __putname(kname);
169error_free_name:
170 kfree(result);
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700171 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
Jeff Layton91a27b22012-10-10 15:25:28 -0400174struct filename *
175getname(const char __user * filename)
Al Virof52e0c12011-03-14 18:56:51 -0400176{
Linus Torvaldsf7493e52012-03-22 16:10:40 -0700177 return getname_flags(filename, 0, NULL);
Al Virof52e0c12011-03-14 18:56:51 -0400178}
Jeff Layton91a27b22012-10-10 15:25:28 -0400179EXPORT_SYMBOL(getname);
Al Virof52e0c12011-03-14 18:56:51 -0400180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181#ifdef CONFIG_AUDITSYSCALL
Jeff Layton91a27b22012-10-10 15:25:28 -0400182void putname(struct filename *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Al Viro5ac3a9c2006-07-16 06:38:45 -0400184 if (unlikely(!audit_dummy_context()))
Jeff Layton91a27b22012-10-10 15:25:28 -0400185 return audit_putname(name);
186 final_putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188#endif
189
Linus Torvaldse77819e2011-07-22 19:30:19 -0700190static int check_acl(struct inode *inode, int mask)
191{
Linus Torvalds84635d62011-07-25 22:47:03 -0700192#ifdef CONFIG_FS_POSIX_ACL
Linus Torvaldse77819e2011-07-22 19:30:19 -0700193 struct posix_acl *acl;
194
Linus Torvaldse77819e2011-07-22 19:30:19 -0700195 if (mask & MAY_NOT_BLOCK) {
Al Viro35678662011-08-02 21:32:13 -0400196 acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
197 if (!acl)
Linus Torvaldse77819e2011-07-22 19:30:19 -0700198 return -EAGAIN;
Al Viro35678662011-08-02 21:32:13 -0400199 /* no ->get_acl() calls in RCU mode... */
200 if (acl == ACL_NOT_CACHED)
201 return -ECHILD;
Ari Savolainen206b1d02011-08-06 19:43:07 +0300202 return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
Linus Torvaldse77819e2011-07-22 19:30:19 -0700203 }
204
205 acl = get_cached_acl(inode, ACL_TYPE_ACCESS);
206
207 /*
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200208 * A filesystem can force a ACL callback by just never filling the
209 * ACL cache. But normally you'd fill the cache either at inode
210 * instantiation time, or on the first ->get_acl call.
Linus Torvaldse77819e2011-07-22 19:30:19 -0700211 *
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200212 * If the filesystem doesn't have a get_acl() function at all, we'll
213 * just create the negative cache entry.
Linus Torvaldse77819e2011-07-22 19:30:19 -0700214 */
215 if (acl == ACL_NOT_CACHED) {
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200216 if (inode->i_op->get_acl) {
217 acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS);
218 if (IS_ERR(acl))
219 return PTR_ERR(acl);
220 } else {
221 set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
222 return -EAGAIN;
223 }
Linus Torvaldse77819e2011-07-22 19:30:19 -0700224 }
225
226 if (acl) {
227 int error = posix_acl_permission(inode, acl, mask);
228 posix_acl_release(acl);
229 return error;
230 }
Linus Torvalds84635d62011-07-25 22:47:03 -0700231#endif
Linus Torvaldse77819e2011-07-22 19:30:19 -0700232
233 return -EAGAIN;
234}
235
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700236/*
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530237 * This does the basic permission checking
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700238 */
Al Viro7e401452011-06-20 19:12:17 -0400239static int acl_permission_check(struct inode *inode, int mask)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700240{
Linus Torvalds26cf46b2011-05-13 11:51:01 -0700241 unsigned int mode = inode->i_mode;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700242
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -0800243 if (likely(uid_eq(current_fsuid(), inode->i_uid)))
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700244 mode >>= 6;
245 else {
Linus Torvaldse77819e2011-07-22 19:30:19 -0700246 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
Al Viro7e401452011-06-20 19:12:17 -0400247 int error = check_acl(inode, mask);
Nick Pigginb74c79e2011-01-07 17:49:58 +1100248 if (error != -EAGAIN)
249 return error;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700250 }
251
252 if (in_group_p(inode->i_gid))
253 mode >>= 3;
254 }
255
256 /*
257 * If the DACs are ok we don't need any capability check.
258 */
Al Viro9c2c7032011-06-20 19:06:22 -0400259 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700260 return 0;
261 return -EACCES;
262}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264/**
Nick Pigginb74c79e2011-01-07 17:49:58 +1100265 * generic_permission - check for access rights on a Posix-like filesystem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 * @inode: inode to check access rights for
Andreas Gruenbacher8fd90c82011-10-23 23:13:30 +0530267 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 *
269 * Used to check for read/write/execute permissions on a file.
270 * We use "fsuid" for this, letting us set arbitrary permissions
271 * for filesystem access without changing the "normal" uids which
Nick Pigginb74c79e2011-01-07 17:49:58 +1100272 * are used for other things.
273 *
274 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
275 * request cannot be satisfied (eg. requires blocking or too much complexity).
276 * It would then be called again in ref-walk mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 */
Al Viro2830ba72011-06-20 19:16:29 -0400278int generic_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700280 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 /*
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530283 * Do the basic permission checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 */
Al Viro7e401452011-06-20 19:12:17 -0400285 ret = acl_permission_check(inode, mask);
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700286 if (ret != -EACCES)
287 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Al Virod594e7e2011-06-20 19:55:42 -0400289 if (S_ISDIR(inode->i_mode)) {
290 /* DACs are overridable for directories */
Eric W. Biederman1a48e2a2011-11-14 16:24:06 -0800291 if (inode_capable(inode, CAP_DAC_OVERRIDE))
Al Virod594e7e2011-06-20 19:55:42 -0400292 return 0;
293 if (!(mask & MAY_WRITE))
Eric W. Biederman1a48e2a2011-11-14 16:24:06 -0800294 if (inode_capable(inode, CAP_DAC_READ_SEARCH))
Al Virod594e7e2011-06-20 19:55:42 -0400295 return 0;
296 return -EACCES;
297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 /*
299 * Read/write DACs are always overridable.
Al Virod594e7e2011-06-20 19:55:42 -0400300 * Executable DACs are overridable when there is
301 * at least one exec bit set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 */
Al Virod594e7e2011-06-20 19:55:42 -0400303 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
Eric W. Biederman1a48e2a2011-11-14 16:24:06 -0800304 if (inode_capable(inode, CAP_DAC_OVERRIDE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 return 0;
306
307 /*
308 * Searching includes executable on directories, else just read.
309 */
Serge E. Hallyn7ea66002009-12-29 14:50:19 -0600310 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
Al Virod594e7e2011-06-20 19:55:42 -0400311 if (mask == MAY_READ)
Eric W. Biederman1a48e2a2011-11-14 16:24:06 -0800312 if (inode_capable(inode, CAP_DAC_READ_SEARCH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 return 0;
314
315 return -EACCES;
316}
317
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700318/*
319 * We _really_ want to just do "generic_permission()" without
320 * even looking at the inode->i_op values. So we keep a cache
321 * flag in inode->i_opflags, that says "this has not special
322 * permission function, use the fast case".
323 */
324static inline int do_inode_permission(struct inode *inode, int mask)
325{
326 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
327 if (likely(inode->i_op->permission))
328 return inode->i_op->permission(inode, mask);
329
330 /* This gets set once for the inode lifetime */
331 spin_lock(&inode->i_lock);
332 inode->i_opflags |= IOP_FASTPERM;
333 spin_unlock(&inode->i_lock);
334 }
335 return generic_permission(inode, mask);
336}
337
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200338/**
David Howells0bdaea92012-06-25 12:55:46 +0100339 * __inode_permission - Check for access rights to a given inode
340 * @inode: Inode to check permission on
341 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200342 *
David Howells0bdaea92012-06-25 12:55:46 +0100343 * Check for read/write/execute permissions on an inode.
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530344 *
345 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
David Howells0bdaea92012-06-25 12:55:46 +0100346 *
347 * This does not check for a read-only file system. You probably want
348 * inode_permission().
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200349 */
David Howells0bdaea92012-06-25 12:55:46 +0100350int __inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
Al Viroe6305c42008-07-15 21:03:57 -0400352 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700354 if (unlikely(mask & MAY_WRITE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 /*
356 * Nobody gets write access to an immutable file.
357 */
358 if (IS_IMMUTABLE(inode))
359 return -EACCES;
360 }
361
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700362 retval = do_inode_permission(inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if (retval)
364 return retval;
365
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700366 retval = devcgroup_inode_permission(inode, mask);
367 if (retval)
368 return retval;
369
Eric Parisd09ca732010-07-23 11:43:57 -0400370 return security_inode_permission(inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
Al Virof4d6ff82011-06-19 13:14:21 -0400373/**
David Howells0bdaea92012-06-25 12:55:46 +0100374 * sb_permission - Check superblock-level permissions
375 * @sb: Superblock of inode to check permission on
Randy Dunlap55852632012-08-18 17:39:25 -0700376 * @inode: Inode to check permission on
David Howells0bdaea92012-06-25 12:55:46 +0100377 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
378 *
379 * Separate out file-system wide checks from inode-specific permission checks.
380 */
381static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
382{
383 if (unlikely(mask & MAY_WRITE)) {
384 umode_t mode = inode->i_mode;
385
386 /* Nobody gets write access to a read-only fs. */
387 if ((sb->s_flags & MS_RDONLY) &&
388 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
389 return -EROFS;
390 }
391 return 0;
392}
393
394/**
395 * inode_permission - Check for access rights to a given inode
396 * @inode: Inode to check permission on
397 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
398 *
399 * Check for read/write/execute permissions on an inode. We use fs[ug]id for
400 * this, letting us set arbitrary permissions for filesystem access without
401 * changing the "normal" UIDs which are used for other things.
402 *
403 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
404 */
405int inode_permission(struct inode *inode, int mask)
406{
407 int retval;
408
409 retval = sb_permission(inode->i_sb, inode, mask);
410 if (retval)
411 return retval;
412 return __inode_permission(inode, mask);
413}
414
415/**
Jan Blunck5dd784d02008-02-14 19:34:38 -0800416 * path_get - get a reference to a path
417 * @path: path to get the reference to
418 *
419 * Given a path increment the reference count to the dentry and the vfsmount.
420 */
421void path_get(struct path *path)
422{
423 mntget(path->mnt);
424 dget(path->dentry);
425}
426EXPORT_SYMBOL(path_get);
427
428/**
Jan Blunck1d957f92008-02-14 19:34:35 -0800429 * path_put - put a reference to a path
430 * @path: path to put the reference to
431 *
432 * Given a path decrement the reference count to the dentry and the vfsmount.
433 */
434void path_put(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Jan Blunck1d957f92008-02-14 19:34:35 -0800436 dput(path->dentry);
437 mntput(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
Jan Blunck1d957f92008-02-14 19:34:35 -0800439EXPORT_SYMBOL(path_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Al Viro19660af2011-03-25 10:32:48 -0400441/*
Nick Piggin31e6b012011-01-07 17:49:52 +1100442 * Path walking has 2 modes, rcu-walk and ref-walk (see
Al Viro19660af2011-03-25 10:32:48 -0400443 * Documentation/filesystems/path-lookup.txt). In situations when we can't
444 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
445 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
446 * mode. Refcounts are grabbed at the last known good point before rcu-walk
447 * got stuck, so ref-walk may continue from there. If this is not successful
448 * (eg. a seqcount has changed), then failure is returned and it's up to caller
449 * to restart the path walk from the beginning in ref-walk mode.
Nick Piggin31e6b012011-01-07 17:49:52 +1100450 */
Nick Piggin31e6b012011-01-07 17:49:52 +1100451
Al Viro32a79912012-07-18 20:43:19 +0400452static inline void lock_rcu_walk(void)
453{
454 br_read_lock(&vfsmount_lock);
455 rcu_read_lock();
456}
457
458static inline void unlock_rcu_walk(void)
459{
460 rcu_read_unlock();
461 br_read_unlock(&vfsmount_lock);
462}
463
Nick Piggin31e6b012011-01-07 17:49:52 +1100464/**
Al Viro19660af2011-03-25 10:32:48 -0400465 * unlazy_walk - try to switch to ref-walk mode.
466 * @nd: nameidata pathwalk data
467 * @dentry: child of nd->path.dentry or NULL
Randy Dunlap39191622011-01-08 19:36:21 -0800468 * Returns: 0 on success, -ECHILD on failure
Nick Piggin31e6b012011-01-07 17:49:52 +1100469 *
Al Viro19660af2011-03-25 10:32:48 -0400470 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
471 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
472 * @nd or NULL. Must be called from rcu-walk context.
Nick Piggin31e6b012011-01-07 17:49:52 +1100473 */
Al Viro19660af2011-03-25 10:32:48 -0400474static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
Nick Piggin31e6b012011-01-07 17:49:52 +1100475{
476 struct fs_struct *fs = current->fs;
477 struct dentry *parent = nd->path.dentry;
Al Viro5b6ca022011-03-09 23:04:47 -0500478 int want_root = 0;
Nick Piggin31e6b012011-01-07 17:49:52 +1100479
480 BUG_ON(!(nd->flags & LOOKUP_RCU));
Al Viro5b6ca022011-03-09 23:04:47 -0500481 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
482 want_root = 1;
Nick Piggin31e6b012011-01-07 17:49:52 +1100483 spin_lock(&fs->lock);
484 if (nd->root.mnt != fs->root.mnt ||
485 nd->root.dentry != fs->root.dentry)
486 goto err_root;
487 }
488 spin_lock(&parent->d_lock);
Al Viro19660af2011-03-25 10:32:48 -0400489 if (!dentry) {
490 if (!__d_rcu_to_refcount(parent, nd->seq))
491 goto err_parent;
492 BUG_ON(nd->inode != parent->d_inode);
493 } else {
Al Viro94c0d4e2011-07-12 21:40:23 -0400494 if (dentry->d_parent != parent)
495 goto err_parent;
Al Viro19660af2011-03-25 10:32:48 -0400496 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
497 if (!__d_rcu_to_refcount(dentry, nd->seq))
498 goto err_child;
499 /*
500 * If the sequence check on the child dentry passed, then
501 * the child has not been removed from its parent. This
502 * means the parent dentry must be valid and able to take
503 * a reference at this point.
504 */
505 BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
506 BUG_ON(!parent->d_count);
507 parent->d_count++;
508 spin_unlock(&dentry->d_lock);
509 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100510 spin_unlock(&parent->d_lock);
Al Viro5b6ca022011-03-09 23:04:47 -0500511 if (want_root) {
Nick Piggin31e6b012011-01-07 17:49:52 +1100512 path_get(&nd->root);
513 spin_unlock(&fs->lock);
514 }
515 mntget(nd->path.mnt);
516
Al Viro32a79912012-07-18 20:43:19 +0400517 unlock_rcu_walk();
Nick Piggin31e6b012011-01-07 17:49:52 +1100518 nd->flags &= ~LOOKUP_RCU;
519 return 0;
Al Viro19660af2011-03-25 10:32:48 -0400520
521err_child:
Nick Piggin31e6b012011-01-07 17:49:52 +1100522 spin_unlock(&dentry->d_lock);
Al Viro19660af2011-03-25 10:32:48 -0400523err_parent:
Nick Piggin31e6b012011-01-07 17:49:52 +1100524 spin_unlock(&parent->d_lock);
525err_root:
Al Viro5b6ca022011-03-09 23:04:47 -0500526 if (want_root)
Nick Piggin31e6b012011-01-07 17:49:52 +1100527 spin_unlock(&fs->lock);
528 return -ECHILD;
529}
530
Al Viro4ce16ef32012-06-10 16:10:59 -0400531static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
Nick Piggin34286d62011-01-07 17:49:57 +1100532{
Al Viro4ce16ef32012-06-10 16:10:59 -0400533 return dentry->d_op->d_revalidate(dentry, flags);
Nick Piggin34286d62011-01-07 17:49:57 +1100534}
535
Al Viro9f1fafe2011-03-25 11:00:12 -0400536/**
537 * complete_walk - successful completion of path walk
538 * @nd: pointer nameidata
Jeff Layton39159de2009-12-07 12:01:50 -0500539 *
Al Viro9f1fafe2011-03-25 11:00:12 -0400540 * If we had been in RCU mode, drop out of it and legitimize nd->path.
541 * Revalidate the final result, unless we'd already done that during
542 * the path walk or the filesystem doesn't ask for it. Return 0 on
543 * success, -error on failure. In case of failure caller does not
544 * need to drop nd->path.
Jeff Layton39159de2009-12-07 12:01:50 -0500545 */
Al Viro9f1fafe2011-03-25 11:00:12 -0400546static int complete_walk(struct nameidata *nd)
Jeff Layton39159de2009-12-07 12:01:50 -0500547{
Al Viro16c2cd72011-02-22 15:50:10 -0500548 struct dentry *dentry = nd->path.dentry;
Jeff Layton39159de2009-12-07 12:01:50 -0500549 int status;
Jeff Layton39159de2009-12-07 12:01:50 -0500550
Al Viro9f1fafe2011-03-25 11:00:12 -0400551 if (nd->flags & LOOKUP_RCU) {
552 nd->flags &= ~LOOKUP_RCU;
553 if (!(nd->flags & LOOKUP_ROOT))
554 nd->root.mnt = NULL;
555 spin_lock(&dentry->d_lock);
556 if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
557 spin_unlock(&dentry->d_lock);
Al Viro32a79912012-07-18 20:43:19 +0400558 unlock_rcu_walk();
Al Viro9f1fafe2011-03-25 11:00:12 -0400559 return -ECHILD;
560 }
561 BUG_ON(nd->inode != dentry->d_inode);
562 spin_unlock(&dentry->d_lock);
563 mntget(nd->path.mnt);
Al Viro32a79912012-07-18 20:43:19 +0400564 unlock_rcu_walk();
Al Viro9f1fafe2011-03-25 11:00:12 -0400565 }
566
Al Viro16c2cd72011-02-22 15:50:10 -0500567 if (likely(!(nd->flags & LOOKUP_JUMPED)))
Jeff Layton39159de2009-12-07 12:01:50 -0500568 return 0;
569
Al Viro16c2cd72011-02-22 15:50:10 -0500570 if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
571 return 0;
572
573 if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
574 return 0;
575
576 /* Note: we do not d_invalidate() */
Al Viro4ce16ef32012-06-10 16:10:59 -0400577 status = d_revalidate(dentry, nd->flags);
Jeff Layton39159de2009-12-07 12:01:50 -0500578 if (status > 0)
579 return 0;
580
Al Viro16c2cd72011-02-22 15:50:10 -0500581 if (!status)
Jeff Layton39159de2009-12-07 12:01:50 -0500582 status = -ESTALE;
Al Viro16c2cd72011-02-22 15:50:10 -0500583
Al Viro9f1fafe2011-03-25 11:00:12 -0400584 path_put(&nd->path);
Jeff Layton39159de2009-12-07 12:01:50 -0500585 return status;
586}
587
Al Viro2a737872009-04-07 11:49:53 -0400588static __always_inline void set_root(struct nameidata *nd)
589{
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200590 if (!nd->root.mnt)
591 get_fs_root(current->fs, &nd->root);
Al Viro2a737872009-04-07 11:49:53 -0400592}
593
Al Viro6de88d72009-08-09 01:41:57 +0400594static int link_path_walk(const char *, struct nameidata *);
595
Nick Piggin31e6b012011-01-07 17:49:52 +1100596static __always_inline void set_root_rcu(struct nameidata *nd)
597{
598 if (!nd->root.mnt) {
599 struct fs_struct *fs = current->fs;
Nick Pigginc28cc362011-01-07 17:49:53 +1100600 unsigned seq;
601
602 do {
603 seq = read_seqcount_begin(&fs->seq);
604 nd->root = fs->root;
Tim Chenc1530012011-04-15 11:39:29 -0700605 nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
Nick Pigginc28cc362011-01-07 17:49:53 +1100606 } while (read_seqcount_retry(&fs->seq, seq));
Nick Piggin31e6b012011-01-07 17:49:52 +1100607 }
608}
609
Arjan van de Venf1662352006-01-14 13:21:31 -0800610static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Nick Piggin31e6b012011-01-07 17:49:52 +1100612 int ret;
613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 if (IS_ERR(link))
615 goto fail;
616
617 if (*link == '/') {
Al Viro2a737872009-04-07 11:49:53 -0400618 set_root(nd);
Jan Blunck1d957f92008-02-14 19:34:35 -0800619 path_put(&nd->path);
Al Viro2a737872009-04-07 11:49:53 -0400620 nd->path = nd->root;
621 path_get(&nd->root);
Al Viro16c2cd72011-02-22 15:50:10 -0500622 nd->flags |= LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100624 nd->inode = nd->path.dentry->d_inode;
Christoph Hellwigb4091d52008-11-05 15:07:21 +0100625
Nick Piggin31e6b012011-01-07 17:49:52 +1100626 ret = link_path_walk(link, nd);
627 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628fail:
Jan Blunck1d957f92008-02-14 19:34:35 -0800629 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 return PTR_ERR(link);
631}
632
Jan Blunck1d957f92008-02-14 19:34:35 -0800633static void path_put_conditional(struct path *path, struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700634{
635 dput(path->dentry);
Jan Blunck4ac91372008-02-14 19:34:32 -0800636 if (path->mnt != nd->path.mnt)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700637 mntput(path->mnt);
638}
639
Nick Piggin7b9337a2011-01-14 08:42:43 +0000640static inline void path_to_nameidata(const struct path *path,
641 struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700642{
Nick Piggin31e6b012011-01-07 17:49:52 +1100643 if (!(nd->flags & LOOKUP_RCU)) {
644 dput(nd->path.dentry);
645 if (nd->path.mnt != path->mnt)
646 mntput(nd->path.mnt);
Huang Shijie9a229682010-04-02 17:37:13 +0800647 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100648 nd->path.mnt = path->mnt;
Jan Blunck4ac91372008-02-14 19:34:32 -0800649 nd->path.dentry = path->dentry;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700650}
651
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400652/*
653 * Helper to directly jump to a known parsed path from ->follow_link,
654 * caller must have taken a reference to path beforehand.
655 */
656void nd_jump_link(struct nameidata *nd, struct path *path)
657{
658 path_put(&nd->path);
659
660 nd->path = *path;
661 nd->inode = nd->path.dentry->d_inode;
662 nd->flags |= LOOKUP_JUMPED;
663
664 BUG_ON(nd->inode->i_op->follow_link);
665}
666
Al Viro574197e2011-03-14 22:20:34 -0400667static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
668{
669 struct inode *inode = link->dentry->d_inode;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400670 if (inode->i_op->put_link)
Al Viro574197e2011-03-14 22:20:34 -0400671 inode->i_op->put_link(link->dentry, nd, cookie);
672 path_put(link);
673}
674
Kees Cook800179c2012-07-25 17:29:07 -0700675int sysctl_protected_symlinks __read_mostly = 1;
676int sysctl_protected_hardlinks __read_mostly = 1;
677
678/**
679 * may_follow_link - Check symlink following for unsafe situations
680 * @link: The path of the symlink
Randy Dunlap55852632012-08-18 17:39:25 -0700681 * @nd: nameidata pathwalk data
Kees Cook800179c2012-07-25 17:29:07 -0700682 *
683 * In the case of the sysctl_protected_symlinks sysctl being enabled,
684 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
685 * in a sticky world-writable directory. This is to protect privileged
686 * processes from failing races against path names that may change out
687 * from under them by way of other users creating malicious symlinks.
688 * It will permit symlinks to be followed only when outside a sticky
689 * world-writable directory, or when the uid of the symlink and follower
690 * match, or when the directory owner matches the symlink's owner.
691 *
692 * Returns 0 if following the symlink is allowed, -ve on error.
693 */
694static inline int may_follow_link(struct path *link, struct nameidata *nd)
695{
696 const struct inode *inode;
697 const struct inode *parent;
698
699 if (!sysctl_protected_symlinks)
700 return 0;
701
702 /* Allowed if owner and follower match. */
703 inode = link->dentry->d_inode;
Eric W. Biederman81abe272012-08-03 09:38:08 -0700704 if (uid_eq(current_cred()->fsuid, inode->i_uid))
Kees Cook800179c2012-07-25 17:29:07 -0700705 return 0;
706
707 /* Allowed if parent directory not sticky and world-writable. */
708 parent = nd->path.dentry->d_inode;
709 if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
710 return 0;
711
712 /* Allowed if parent directory and link owner match. */
Eric W. Biederman81abe272012-08-03 09:38:08 -0700713 if (uid_eq(parent->i_uid, inode->i_uid))
Kees Cook800179c2012-07-25 17:29:07 -0700714 return 0;
715
Sasha Levinffd8d102012-10-04 19:56:40 -0400716 audit_log_link_denied("follow_link", link);
Kees Cook800179c2012-07-25 17:29:07 -0700717 path_put_conditional(link, nd);
718 path_put(&nd->path);
719 return -EACCES;
720}
721
722/**
723 * safe_hardlink_source - Check for safe hardlink conditions
724 * @inode: the source inode to hardlink from
725 *
726 * Return false if at least one of the following conditions:
727 * - inode is not a regular file
728 * - inode is setuid
729 * - inode is setgid and group-exec
730 * - access failure for read and write
731 *
732 * Otherwise returns true.
733 */
734static bool safe_hardlink_source(struct inode *inode)
735{
736 umode_t mode = inode->i_mode;
737
738 /* Special files should not get pinned to the filesystem. */
739 if (!S_ISREG(mode))
740 return false;
741
742 /* Setuid files should not get pinned to the filesystem. */
743 if (mode & S_ISUID)
744 return false;
745
746 /* Executable setgid files should not get pinned to the filesystem. */
747 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
748 return false;
749
750 /* Hardlinking to unreadable or unwritable sources is dangerous. */
751 if (inode_permission(inode, MAY_READ | MAY_WRITE))
752 return false;
753
754 return true;
755}
756
757/**
758 * may_linkat - Check permissions for creating a hardlink
759 * @link: the source to hardlink from
760 *
761 * Block hardlink when all of:
762 * - sysctl_protected_hardlinks enabled
763 * - fsuid does not match inode
764 * - hardlink source is unsafe (see safe_hardlink_source() above)
765 * - not CAP_FOWNER
766 *
767 * Returns 0 if successful, -ve on error.
768 */
769static int may_linkat(struct path *link)
770{
771 const struct cred *cred;
772 struct inode *inode;
773
774 if (!sysctl_protected_hardlinks)
775 return 0;
776
777 cred = current_cred();
778 inode = link->dentry->d_inode;
779
780 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
781 * otherwise, it must be a safe source.
782 */
Eric W. Biederman81abe272012-08-03 09:38:08 -0700783 if (uid_eq(cred->fsuid, inode->i_uid) || safe_hardlink_source(inode) ||
Kees Cook800179c2012-07-25 17:29:07 -0700784 capable(CAP_FOWNER))
785 return 0;
786
Kees Cooka51d9ea2012-07-25 17:29:08 -0700787 audit_log_link_denied("linkat", link);
Kees Cook800179c2012-07-25 17:29:07 -0700788 return -EPERM;
789}
790
Al Virodef4af32009-12-26 08:37:05 -0500791static __always_inline int
Al Viro574197e2011-03-14 22:20:34 -0400792follow_link(struct path *link, struct nameidata *nd, void **p)
Ian Kent051d3812006-03-27 01:14:53 -0800793{
Nick Piggin7b9337a2011-01-14 08:42:43 +0000794 struct dentry *dentry = link->dentry;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400795 int error;
796 char *s;
Ian Kent051d3812006-03-27 01:14:53 -0800797
Al Viro844a3912011-02-15 00:38:26 -0500798 BUG_ON(nd->flags & LOOKUP_RCU);
799
Al Viro0e794582011-03-16 02:45:02 -0400800 if (link->mnt == nd->path.mnt)
801 mntget(link->mnt);
802
Al Viro6d7b5aa2012-06-10 04:15:17 -0400803 error = -ELOOP;
804 if (unlikely(current->total_link_count >= 40))
805 goto out_put_nd_path;
806
Al Viro574197e2011-03-14 22:20:34 -0400807 cond_resched();
808 current->total_link_count++;
809
Al Viro68ac1232012-03-15 08:21:57 -0400810 touch_atime(link);
Ian Kent051d3812006-03-27 01:14:53 -0800811 nd_set_link(nd, NULL);
812
Al Viro36f3b4f2011-02-22 21:24:38 -0500813 error = security_inode_follow_link(link->dentry, nd);
Al Viro6d7b5aa2012-06-10 04:15:17 -0400814 if (error)
815 goto out_put_nd_path;
Al Viro36f3b4f2011-02-22 21:24:38 -0500816
Al Viro86acdca12009-12-22 23:45:11 -0500817 nd->last_type = LAST_BIND;
Al Virodef4af32009-12-26 08:37:05 -0500818 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
819 error = PTR_ERR(*p);
Al Viro6d7b5aa2012-06-10 04:15:17 -0400820 if (IS_ERR(*p))
Christoph Hellwig408ef012012-06-18 10:47:03 -0400821 goto out_put_nd_path;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400822
823 error = 0;
824 s = nd_get_link(nd);
825 if (s) {
826 error = __vfs_follow_link(nd, s);
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400827 if (unlikely(error))
828 put_link(nd, link, *p);
Ian Kent051d3812006-03-27 01:14:53 -0800829 }
Al Viro6d7b5aa2012-06-10 04:15:17 -0400830
831 return error;
832
833out_put_nd_path:
Arnd Bergmann98f6ef62012-10-11 13:20:00 +0000834 *p = NULL;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400835 path_put(&nd->path);
Al Viro6d7b5aa2012-06-10 04:15:17 -0400836 path_put(link);
Ian Kent051d3812006-03-27 01:14:53 -0800837 return error;
838}
839
Nick Piggin31e6b012011-01-07 17:49:52 +1100840static int follow_up_rcu(struct path *path)
841{
Al Viro0714a532011-11-24 22:19:58 -0500842 struct mount *mnt = real_mount(path->mnt);
843 struct mount *parent;
Nick Piggin31e6b012011-01-07 17:49:52 +1100844 struct dentry *mountpoint;
845
Al Viro0714a532011-11-24 22:19:58 -0500846 parent = mnt->mnt_parent;
847 if (&parent->mnt == path->mnt)
Nick Piggin31e6b012011-01-07 17:49:52 +1100848 return 0;
Al Viroa73324d2011-11-24 22:25:07 -0500849 mountpoint = mnt->mnt_mountpoint;
Nick Piggin31e6b012011-01-07 17:49:52 +1100850 path->dentry = mountpoint;
Al Viro0714a532011-11-24 22:19:58 -0500851 path->mnt = &parent->mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +1100852 return 1;
853}
854
David Howellsf015f1262012-06-25 12:55:28 +0100855/*
856 * follow_up - Find the mountpoint of path's vfsmount
857 *
858 * Given a path, find the mountpoint of its source file system.
859 * Replace @path with the path of the mountpoint in the parent mount.
860 * Up is towards /.
861 *
862 * Return 1 if we went up a level and 0 if we were already at the
863 * root.
864 */
Al Virobab77eb2009-04-18 03:26:48 -0400865int follow_up(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Al Viro0714a532011-11-24 22:19:58 -0500867 struct mount *mnt = real_mount(path->mnt);
868 struct mount *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 struct dentry *mountpoint;
Nick Piggin99b7db72010-08-18 04:37:39 +1000870
Andi Kleen962830d2012-05-08 13:32:02 +0930871 br_read_lock(&vfsmount_lock);
Al Viro0714a532011-11-24 22:19:58 -0500872 parent = mnt->mnt_parent;
Al Viro3c0a6162012-07-18 17:32:50 +0400873 if (parent == mnt) {
Andi Kleen962830d2012-05-08 13:32:02 +0930874 br_read_unlock(&vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 return 0;
876 }
Al Viro0714a532011-11-24 22:19:58 -0500877 mntget(&parent->mnt);
Al Viroa73324d2011-11-24 22:25:07 -0500878 mountpoint = dget(mnt->mnt_mountpoint);
Andi Kleen962830d2012-05-08 13:32:02 +0930879 br_read_unlock(&vfsmount_lock);
Al Virobab77eb2009-04-18 03:26:48 -0400880 dput(path->dentry);
881 path->dentry = mountpoint;
882 mntput(path->mnt);
Al Viro0714a532011-11-24 22:19:58 -0500883 path->mnt = &parent->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 return 1;
885}
886
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100887/*
David Howells9875cf82011-01-14 18:45:21 +0000888 * Perform an automount
889 * - return -EISDIR to tell follow_managed() to stop and return the path we
890 * were called with.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 */
David Howells9875cf82011-01-14 18:45:21 +0000892static int follow_automount(struct path *path, unsigned flags,
893 bool *need_mntput)
Nick Piggin31e6b012011-01-07 17:49:52 +1100894{
David Howells9875cf82011-01-14 18:45:21 +0000895 struct vfsmount *mnt;
David Howellsea5b7782011-01-14 19:10:03 +0000896 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +1100897
David Howells9875cf82011-01-14 18:45:21 +0000898 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
899 return -EREMOTE;
Al Viro463ffb22005-06-06 13:36:05 -0700900
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200901 /* We don't want to mount if someone's just doing a stat -
902 * unless they're stat'ing a directory and appended a '/' to
903 * the name.
904 *
905 * We do, however, want to mount if someone wants to open or
906 * create a file of any type under the mountpoint, wants to
907 * traverse through the mountpoint or wants to open the
908 * mounted directory. Also, autofs may mark negative dentries
909 * as being automount points. These will need the attentions
910 * of the daemon to instantiate them before they can be used.
David Howells9875cf82011-01-14 18:45:21 +0000911 */
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200912 if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
Linus Torvaldsd94c1772011-09-26 17:44:55 -0700913 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200914 path->dentry->d_inode)
915 return -EISDIR;
916
David Howells9875cf82011-01-14 18:45:21 +0000917 current->total_link_count++;
918 if (current->total_link_count >= 40)
919 return -ELOOP;
920
921 mnt = path->dentry->d_op->d_automount(path);
922 if (IS_ERR(mnt)) {
923 /*
924 * The filesystem is allowed to return -EISDIR here to indicate
925 * it doesn't want to automount. For instance, autofs would do
926 * this so that its userspace daemon can mount on this dentry.
927 *
928 * However, we can only permit this if it's a terminal point in
929 * the path being looked up; if it wasn't then the remainder of
930 * the path is inaccessible and we should say so.
931 */
Al Viro49084c32011-06-25 21:59:52 -0400932 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
David Howells9875cf82011-01-14 18:45:21 +0000933 return -EREMOTE;
934 return PTR_ERR(mnt);
935 }
David Howellsea5b7782011-01-14 19:10:03 +0000936
David Howells9875cf82011-01-14 18:45:21 +0000937 if (!mnt) /* mount collision */
938 return 0;
939
Al Viro8aef1882011-06-16 15:10:06 +0100940 if (!*need_mntput) {
941 /* lock_mount() may release path->mnt on error */
942 mntget(path->mnt);
943 *need_mntput = true;
944 }
Al Viro19a167a2011-01-17 01:35:23 -0500945 err = finish_automount(mnt, path);
David Howellsea5b7782011-01-14 19:10:03 +0000946
David Howellsea5b7782011-01-14 19:10:03 +0000947 switch (err) {
948 case -EBUSY:
949 /* Someone else made a mount here whilst we were busy */
Al Viro19a167a2011-01-17 01:35:23 -0500950 return 0;
David Howellsea5b7782011-01-14 19:10:03 +0000951 case 0:
Al Viro8aef1882011-06-16 15:10:06 +0100952 path_put(path);
David Howellsea5b7782011-01-14 19:10:03 +0000953 path->mnt = mnt;
954 path->dentry = dget(mnt->mnt_root);
David Howellsea5b7782011-01-14 19:10:03 +0000955 return 0;
Al Viro19a167a2011-01-17 01:35:23 -0500956 default:
957 return err;
David Howellsea5b7782011-01-14 19:10:03 +0000958 }
Al Viro19a167a2011-01-17 01:35:23 -0500959
David Howells9875cf82011-01-14 18:45:21 +0000960}
961
962/*
963 * Handle a dentry that is managed in some way.
David Howellscc53ce52011-01-14 18:45:26 +0000964 * - Flagged for transit management (autofs)
David Howells9875cf82011-01-14 18:45:21 +0000965 * - Flagged as mountpoint
966 * - Flagged as automount point
967 *
968 * This may only be called in refwalk mode.
969 *
970 * Serialization is taken care of in namespace.c
971 */
972static int follow_managed(struct path *path, unsigned flags)
973{
Al Viro8aef1882011-06-16 15:10:06 +0100974 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
David Howells9875cf82011-01-14 18:45:21 +0000975 unsigned managed;
976 bool need_mntput = false;
Al Viro8aef1882011-06-16 15:10:06 +0100977 int ret = 0;
David Howells9875cf82011-01-14 18:45:21 +0000978
979 /* Given that we're not holding a lock here, we retain the value in a
980 * local variable for each dentry as we look at it so that we don't see
981 * the components of that value change under us */
982 while (managed = ACCESS_ONCE(path->dentry->d_flags),
983 managed &= DCACHE_MANAGED_DENTRY,
984 unlikely(managed != 0)) {
David Howellscc53ce52011-01-14 18:45:26 +0000985 /* Allow the filesystem to manage the transit without i_mutex
986 * being held. */
987 if (managed & DCACHE_MANAGE_TRANSIT) {
988 BUG_ON(!path->dentry->d_op);
989 BUG_ON(!path->dentry->d_op->d_manage);
Al Viro1aed3e42011-03-18 09:09:02 -0400990 ret = path->dentry->d_op->d_manage(path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +0000991 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +0100992 break;
David Howellscc53ce52011-01-14 18:45:26 +0000993 }
994
David Howells9875cf82011-01-14 18:45:21 +0000995 /* Transit to a mounted filesystem. */
996 if (managed & DCACHE_MOUNTED) {
997 struct vfsmount *mounted = lookup_mnt(path);
998 if (mounted) {
999 dput(path->dentry);
1000 if (need_mntput)
1001 mntput(path->mnt);
1002 path->mnt = mounted;
1003 path->dentry = dget(mounted->mnt_root);
1004 need_mntput = true;
1005 continue;
1006 }
1007
1008 /* Something is mounted on this dentry in another
1009 * namespace and/or whatever was mounted there in this
1010 * namespace got unmounted before we managed to get the
1011 * vfsmount_lock */
1012 }
1013
1014 /* Handle an automount point */
1015 if (managed & DCACHE_NEED_AUTOMOUNT) {
1016 ret = follow_automount(path, flags, &need_mntput);
1017 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001018 break;
David Howells9875cf82011-01-14 18:45:21 +00001019 continue;
1020 }
1021
1022 /* We didn't change the current path point */
1023 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 }
Al Viro8aef1882011-06-16 15:10:06 +01001025
1026 if (need_mntput && path->mnt == mnt)
1027 mntput(path->mnt);
1028 if (ret == -EISDIR)
1029 ret = 0;
Al Viroa3fbbde2011-11-07 21:21:26 +00001030 return ret < 0 ? ret : need_mntput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031}
1032
David Howellscc53ce52011-01-14 18:45:26 +00001033int follow_down_one(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
1035 struct vfsmount *mounted;
1036
Al Viro1c755af2009-04-18 14:06:57 -04001037 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -04001039 dput(path->dentry);
1040 mntput(path->mnt);
1041 path->mnt = mounted;
1042 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 return 1;
1044 }
1045 return 0;
1046}
1047
Ian Kent62a73752011-03-25 01:51:02 +08001048static inline bool managed_dentry_might_block(struct dentry *dentry)
1049{
1050 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
1051 dentry->d_op->d_manage(dentry, true) < 0);
1052}
1053
David Howells9875cf82011-01-14 18:45:21 +00001054/*
Al Viro287548e2011-05-27 06:50:06 -04001055 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1056 * we meet a managed dentry that would need blocking.
David Howells9875cf82011-01-14 18:45:21 +00001057 */
1058static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
Al Viro287548e2011-05-27 06:50:06 -04001059 struct inode **inode)
David Howells9875cf82011-01-14 18:45:21 +00001060{
Ian Kent62a73752011-03-25 01:51:02 +08001061 for (;;) {
Al Viroc7105362011-11-24 18:22:03 -05001062 struct mount *mounted;
Ian Kent62a73752011-03-25 01:51:02 +08001063 /*
1064 * Don't forget we might have a non-mountpoint managed dentry
1065 * that wants to block transit.
1066 */
Al Viro287548e2011-05-27 06:50:06 -04001067 if (unlikely(managed_dentry_might_block(path->dentry)))
David Howellsab909112011-01-14 18:46:51 +00001068 return false;
Ian Kent62a73752011-03-25 01:51:02 +08001069
1070 if (!d_mountpoint(path->dentry))
1071 break;
1072
David Howells9875cf82011-01-14 18:45:21 +00001073 mounted = __lookup_mnt(path->mnt, path->dentry, 1);
1074 if (!mounted)
1075 break;
Al Viroc7105362011-11-24 18:22:03 -05001076 path->mnt = &mounted->mnt;
1077 path->dentry = mounted->mnt.mnt_root;
Al Viroa3fbbde2011-11-07 21:21:26 +00001078 nd->flags |= LOOKUP_JUMPED;
David Howells9875cf82011-01-14 18:45:21 +00001079 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
Linus Torvalds59430262011-07-18 15:43:29 -07001080 /*
1081 * Update the inode too. We don't need to re-check the
1082 * dentry sequence number here after this d_inode read,
1083 * because a mount-point is always pinned.
1084 */
1085 *inode = path->dentry->d_inode;
David Howells9875cf82011-01-14 18:45:21 +00001086 }
David Howells9875cf82011-01-14 18:45:21 +00001087 return true;
1088}
1089
Al Virodea39372011-05-27 06:53:39 -04001090static void follow_mount_rcu(struct nameidata *nd)
Al Viro287548e2011-05-27 06:50:06 -04001091{
Al Virodea39372011-05-27 06:53:39 -04001092 while (d_mountpoint(nd->path.dentry)) {
Al Viroc7105362011-11-24 18:22:03 -05001093 struct mount *mounted;
Al Virodea39372011-05-27 06:53:39 -04001094 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
Al Viro287548e2011-05-27 06:50:06 -04001095 if (!mounted)
1096 break;
Al Viroc7105362011-11-24 18:22:03 -05001097 nd->path.mnt = &mounted->mnt;
1098 nd->path.dentry = mounted->mnt.mnt_root;
Al Virodea39372011-05-27 06:53:39 -04001099 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro287548e2011-05-27 06:50:06 -04001100 }
1101}
1102
Nick Piggin31e6b012011-01-07 17:49:52 +11001103static int follow_dotdot_rcu(struct nameidata *nd)
1104{
Nick Piggin31e6b012011-01-07 17:49:52 +11001105 set_root_rcu(nd);
1106
David Howells9875cf82011-01-14 18:45:21 +00001107 while (1) {
Nick Piggin31e6b012011-01-07 17:49:52 +11001108 if (nd->path.dentry == nd->root.dentry &&
1109 nd->path.mnt == nd->root.mnt) {
1110 break;
1111 }
1112 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1113 struct dentry *old = nd->path.dentry;
1114 struct dentry *parent = old->d_parent;
1115 unsigned seq;
1116
1117 seq = read_seqcount_begin(&parent->d_seq);
1118 if (read_seqcount_retry(&old->d_seq, nd->seq))
Al Viroef7562d2011-03-04 14:35:59 -05001119 goto failed;
Nick Piggin31e6b012011-01-07 17:49:52 +11001120 nd->path.dentry = parent;
1121 nd->seq = seq;
1122 break;
1123 }
1124 if (!follow_up_rcu(&nd->path))
1125 break;
1126 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Nick Piggin31e6b012011-01-07 17:49:52 +11001127 }
Al Virodea39372011-05-27 06:53:39 -04001128 follow_mount_rcu(nd);
1129 nd->inode = nd->path.dentry->d_inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001130 return 0;
Al Viroef7562d2011-03-04 14:35:59 -05001131
1132failed:
1133 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001134 if (!(nd->flags & LOOKUP_ROOT))
1135 nd->root.mnt = NULL;
Al Viro32a79912012-07-18 20:43:19 +04001136 unlock_rcu_walk();
Al Viroef7562d2011-03-04 14:35:59 -05001137 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001138}
1139
David Howells9875cf82011-01-14 18:45:21 +00001140/*
David Howellscc53ce52011-01-14 18:45:26 +00001141 * Follow down to the covering mount currently visible to userspace. At each
1142 * point, the filesystem owning that dentry may be queried as to whether the
1143 * caller is permitted to proceed or not.
David Howellscc53ce52011-01-14 18:45:26 +00001144 */
Al Viro7cc90cc2011-03-18 09:04:20 -04001145int follow_down(struct path *path)
David Howellscc53ce52011-01-14 18:45:26 +00001146{
1147 unsigned managed;
1148 int ret;
1149
1150 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1151 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1152 /* Allow the filesystem to manage the transit without i_mutex
1153 * being held.
1154 *
1155 * We indicate to the filesystem if someone is trying to mount
1156 * something here. This gives autofs the chance to deny anyone
1157 * other than its daemon the right to mount on its
1158 * superstructure.
1159 *
1160 * The filesystem may sleep at this point.
1161 */
1162 if (managed & DCACHE_MANAGE_TRANSIT) {
1163 BUG_ON(!path->dentry->d_op);
1164 BUG_ON(!path->dentry->d_op->d_manage);
David Howellsab909112011-01-14 18:46:51 +00001165 ret = path->dentry->d_op->d_manage(
Al Viro1aed3e42011-03-18 09:09:02 -04001166 path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +00001167 if (ret < 0)
1168 return ret == -EISDIR ? 0 : ret;
1169 }
1170
1171 /* Transit to a mounted filesystem. */
1172 if (managed & DCACHE_MOUNTED) {
1173 struct vfsmount *mounted = lookup_mnt(path);
1174 if (!mounted)
1175 break;
1176 dput(path->dentry);
1177 mntput(path->mnt);
1178 path->mnt = mounted;
1179 path->dentry = dget(mounted->mnt_root);
1180 continue;
1181 }
1182
1183 /* Don't handle automount points here */
1184 break;
1185 }
1186 return 0;
1187}
1188
1189/*
David Howells9875cf82011-01-14 18:45:21 +00001190 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1191 */
1192static void follow_mount(struct path *path)
1193{
1194 while (d_mountpoint(path->dentry)) {
1195 struct vfsmount *mounted = lookup_mnt(path);
1196 if (!mounted)
1197 break;
1198 dput(path->dentry);
1199 mntput(path->mnt);
1200 path->mnt = mounted;
1201 path->dentry = dget(mounted->mnt_root);
1202 }
1203}
1204
Nick Piggin31e6b012011-01-07 17:49:52 +11001205static void follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
Al Viro2a737872009-04-07 11:49:53 -04001207 set_root(nd);
Andreas Mohre518ddb2006-09-29 02:01:22 -07001208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 while(1) {
Jan Blunck4ac91372008-02-14 19:34:32 -08001210 struct dentry *old = nd->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Al Viro2a737872009-04-07 11:49:53 -04001212 if (nd->path.dentry == nd->root.dentry &&
1213 nd->path.mnt == nd->root.mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 break;
1215 }
Jan Blunck4ac91372008-02-14 19:34:32 -08001216 if (nd->path.dentry != nd->path.mnt->mnt_root) {
Al Viro3088dd72010-01-30 15:47:29 -05001217 /* rare case of legitimate dget_parent()... */
1218 nd->path.dentry = dget_parent(nd->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 dput(old);
1220 break;
1221 }
Al Viro3088dd72010-01-30 15:47:29 -05001222 if (!follow_up(&nd->path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 }
Al Viro79ed0222009-04-18 13:59:41 -04001225 follow_mount(&nd->path);
Nick Piggin31e6b012011-01-07 17:49:52 +11001226 nd->inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227}
1228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229/*
Miklos Szeredibad61182012-03-26 12:54:24 +02001230 * This looks up the name in dcache, possibly revalidates the old dentry and
1231 * allocates a new one if not found or not valid. In the need_lookup argument
1232 * returns whether i_op->lookup is necessary.
1233 *
1234 * dir->d_inode->i_mutex must be held
Nick Pigginbaa03892010-08-18 04:37:31 +10001235 */
Miklos Szeredibad61182012-03-26 12:54:24 +02001236static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
Al Viro201f9562012-06-22 12:42:10 +04001237 unsigned int flags, bool *need_lookup)
Nick Pigginbaa03892010-08-18 04:37:31 +10001238{
Nick Pigginbaa03892010-08-18 04:37:31 +10001239 struct dentry *dentry;
Miklos Szeredibad61182012-03-26 12:54:24 +02001240 int error;
Nick Pigginbaa03892010-08-18 04:37:31 +10001241
Miklos Szeredibad61182012-03-26 12:54:24 +02001242 *need_lookup = false;
1243 dentry = d_lookup(dir, name);
1244 if (dentry) {
1245 if (d_need_lookup(dentry)) {
1246 *need_lookup = true;
1247 } else if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
Al Viro201f9562012-06-22 12:42:10 +04001248 error = d_revalidate(dentry, flags);
Miklos Szeredibad61182012-03-26 12:54:24 +02001249 if (unlikely(error <= 0)) {
1250 if (error < 0) {
1251 dput(dentry);
1252 return ERR_PTR(error);
1253 } else if (!d_invalidate(dentry)) {
1254 dput(dentry);
1255 dentry = NULL;
1256 }
1257 }
1258 }
1259 }
Nick Pigginbaa03892010-08-18 04:37:31 +10001260
Miklos Szeredibad61182012-03-26 12:54:24 +02001261 if (!dentry) {
1262 dentry = d_alloc(dir, name);
1263 if (unlikely(!dentry))
1264 return ERR_PTR(-ENOMEM);
Nick Pigginbaa03892010-08-18 04:37:31 +10001265
Miklos Szeredibad61182012-03-26 12:54:24 +02001266 *need_lookup = true;
Nick Pigginbaa03892010-08-18 04:37:31 +10001267 }
1268 return dentry;
1269}
1270
1271/*
Miklos Szeredibad61182012-03-26 12:54:24 +02001272 * Call i_op->lookup on the dentry. The dentry must be negative but may be
1273 * hashed if it was pouplated with DCACHE_NEED_LOOKUP.
1274 *
1275 * dir->d_inode->i_mutex must be held
Josef Bacik44396f42011-05-31 11:58:49 -04001276 */
Miklos Szeredibad61182012-03-26 12:54:24 +02001277static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
Al Viro72bd8662012-06-10 17:17:17 -04001278 unsigned int flags)
Josef Bacik44396f42011-05-31 11:58:49 -04001279{
Josef Bacik44396f42011-05-31 11:58:49 -04001280 struct dentry *old;
1281
1282 /* Don't create child dentry for a dead directory. */
Miklos Szeredibad61182012-03-26 12:54:24 +02001283 if (unlikely(IS_DEADDIR(dir))) {
Miklos Szeredie188dc02012-02-03 14:25:18 +01001284 dput(dentry);
Josef Bacik44396f42011-05-31 11:58:49 -04001285 return ERR_PTR(-ENOENT);
Miklos Szeredie188dc02012-02-03 14:25:18 +01001286 }
Josef Bacik44396f42011-05-31 11:58:49 -04001287
Al Viro72bd8662012-06-10 17:17:17 -04001288 old = dir->i_op->lookup(dir, dentry, flags);
Josef Bacik44396f42011-05-31 11:58:49 -04001289 if (unlikely(old)) {
1290 dput(dentry);
1291 dentry = old;
1292 }
1293 return dentry;
1294}
1295
Al Viroa3255542012-03-30 14:41:51 -04001296static struct dentry *__lookup_hash(struct qstr *name,
Al Viro72bd8662012-06-10 17:17:17 -04001297 struct dentry *base, unsigned int flags)
Al Viroa3255542012-03-30 14:41:51 -04001298{
Miklos Szeredibad61182012-03-26 12:54:24 +02001299 bool need_lookup;
Al Viroa3255542012-03-30 14:41:51 -04001300 struct dentry *dentry;
1301
Al Viro72bd8662012-06-10 17:17:17 -04001302 dentry = lookup_dcache(name, base, flags, &need_lookup);
Miklos Szeredibad61182012-03-26 12:54:24 +02001303 if (!need_lookup)
1304 return dentry;
Al Viroa3255542012-03-30 14:41:51 -04001305
Al Viro72bd8662012-06-10 17:17:17 -04001306 return lookup_real(base->d_inode, dentry, flags);
Al Viroa3255542012-03-30 14:41:51 -04001307}
1308
Josef Bacik44396f42011-05-31 11:58:49 -04001309/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 * It's more convoluted than I'd like it to be, but... it's still fairly
1311 * small and for now I'd prefer to have fast path as straight as possible.
1312 * It _is_ time-critical.
1313 */
Miklos Szeredi697f5142012-05-21 17:30:05 +02001314static int lookup_fast(struct nameidata *nd, struct qstr *name,
1315 struct path *path, struct inode **inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316{
Jan Blunck4ac91372008-02-14 19:34:32 -08001317 struct vfsmount *mnt = nd->path.mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +11001318 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro5a18fff2011-03-11 04:44:53 -05001319 int need_reval = 1;
1320 int status = 1;
David Howells9875cf82011-01-14 18:45:21 +00001321 int err;
1322
Al Viro3cac2602009-08-13 18:27:43 +04001323 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001324 * Rename seqlock is not required here because in the off chance
1325 * of a false negative due to a concurrent rename, we're going to
1326 * do the non-racy lookup, below.
1327 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001328 if (nd->flags & LOOKUP_RCU) {
1329 unsigned seq;
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001330 dentry = __d_lookup_rcu(parent, name, &seq, nd->inode);
Al Viro5a18fff2011-03-11 04:44:53 -05001331 if (!dentry)
1332 goto unlazy;
1333
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001334 /*
1335 * This sequence count validates that the inode matches
1336 * the dentry name information from lookup.
1337 */
1338 *inode = dentry->d_inode;
1339 if (read_seqcount_retry(&dentry->d_seq, seq))
1340 return -ECHILD;
1341
1342 /*
1343 * This sequence count validates that the parent had no
1344 * changes while we did the lookup of the dentry above.
1345 *
1346 * The memory barrier in read_seqcount_begin of child is
1347 * enough, we can use __read_seqcount_retry here.
1348 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001349 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1350 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001351 nd->seq = seq;
Al Viro5a18fff2011-03-11 04:44:53 -05001352
Miklos Szeredifa4ee152012-03-26 12:54:19 +02001353 if (unlikely(d_need_lookup(dentry)))
1354 goto unlazy;
Al Viro24643082011-02-15 01:26:22 -05001355 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
Al Viro4ce16ef32012-06-10 16:10:59 -04001356 status = d_revalidate(dentry, nd->flags);
Al Viro5a18fff2011-03-11 04:44:53 -05001357 if (unlikely(status <= 0)) {
1358 if (status != -ECHILD)
1359 need_reval = 0;
1360 goto unlazy;
1361 }
Al Viro24643082011-02-15 01:26:22 -05001362 }
Nick Piggin31e6b012011-01-07 17:49:52 +11001363 path->mnt = mnt;
1364 path->dentry = dentry;
Al Virod6e9bd22011-05-27 07:03:15 -04001365 if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1366 goto unlazy;
1367 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1368 goto unlazy;
1369 return 0;
Al Viro5a18fff2011-03-11 04:44:53 -05001370unlazy:
Al Viro19660af2011-03-25 10:32:48 -04001371 if (unlazy_walk(nd, dentry))
1372 return -ECHILD;
Al Viro5a18fff2011-03-11 04:44:53 -05001373 } else {
1374 dentry = __d_lookup(parent, name);
Nick Piggin31e6b012011-01-07 17:49:52 +11001375 }
Al Viro5a18fff2011-03-11 04:44:53 -05001376
Al Viro81e6f522012-03-30 14:48:04 -04001377 if (unlikely(!dentry))
1378 goto need_lookup;
Al Viro5a18fff2011-03-11 04:44:53 -05001379
Al Viro81e6f522012-03-30 14:48:04 -04001380 if (unlikely(d_need_lookup(dentry))) {
1381 dput(dentry);
1382 goto need_lookup;
Al Viro24643082011-02-15 01:26:22 -05001383 }
Al Viro81e6f522012-03-30 14:48:04 -04001384
Al Viro5a18fff2011-03-11 04:44:53 -05001385 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
Al Viro4ce16ef32012-06-10 16:10:59 -04001386 status = d_revalidate(dentry, nd->flags);
Al Viro5a18fff2011-03-11 04:44:53 -05001387 if (unlikely(status <= 0)) {
1388 if (status < 0) {
1389 dput(dentry);
1390 return status;
1391 }
1392 if (!d_invalidate(dentry)) {
1393 dput(dentry);
Al Viro81e6f522012-03-30 14:48:04 -04001394 goto need_lookup;
Al Viro5a18fff2011-03-11 04:44:53 -05001395 }
1396 }
Miklos Szeredi697f5142012-05-21 17:30:05 +02001397
David Howells9875cf82011-01-14 18:45:21 +00001398 path->mnt = mnt;
1399 path->dentry = dentry;
1400 err = follow_managed(path, nd->flags);
Ian Kent89312212011-01-18 12:06:10 +08001401 if (unlikely(err < 0)) {
1402 path_put_conditional(path, nd);
David Howells9875cf82011-01-14 18:45:21 +00001403 return err;
Ian Kent89312212011-01-18 12:06:10 +08001404 }
Al Viroa3fbbde2011-11-07 21:21:26 +00001405 if (err)
1406 nd->flags |= LOOKUP_JUMPED;
David Howells9875cf82011-01-14 18:45:21 +00001407 *inode = path->dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 return 0;
Al Viro81e6f522012-03-30 14:48:04 -04001409
1410need_lookup:
Miklos Szeredi697f5142012-05-21 17:30:05 +02001411 return 1;
1412}
1413
1414/* Fast lookup failed, do it the slow way */
1415static int lookup_slow(struct nameidata *nd, struct qstr *name,
1416 struct path *path)
1417{
1418 struct dentry *dentry, *parent;
1419 int err;
1420
1421 parent = nd->path.dentry;
Al Viro81e6f522012-03-30 14:48:04 -04001422 BUG_ON(nd->inode != parent->d_inode);
1423
1424 mutex_lock(&parent->d_inode->i_mutex);
Al Viro72bd8662012-06-10 17:17:17 -04001425 dentry = __lookup_hash(name, parent, nd->flags);
Al Viro81e6f522012-03-30 14:48:04 -04001426 mutex_unlock(&parent->d_inode->i_mutex);
1427 if (IS_ERR(dentry))
1428 return PTR_ERR(dentry);
Miklos Szeredi697f5142012-05-21 17:30:05 +02001429 path->mnt = nd->path.mnt;
1430 path->dentry = dentry;
1431 err = follow_managed(path, nd->flags);
1432 if (unlikely(err < 0)) {
1433 path_put_conditional(path, nd);
1434 return err;
1435 }
1436 if (err)
1437 nd->flags |= LOOKUP_JUMPED;
1438 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439}
1440
Al Viro52094c82011-02-21 21:34:47 -05001441static inline int may_lookup(struct nameidata *nd)
1442{
1443 if (nd->flags & LOOKUP_RCU) {
Al Viro4ad5abb2011-06-20 19:57:03 -04001444 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
Al Viro52094c82011-02-21 21:34:47 -05001445 if (err != -ECHILD)
1446 return err;
Al Viro19660af2011-03-25 10:32:48 -04001447 if (unlazy_walk(nd, NULL))
Al Viro52094c82011-02-21 21:34:47 -05001448 return -ECHILD;
1449 }
Al Viro4ad5abb2011-06-20 19:57:03 -04001450 return inode_permission(nd->inode, MAY_EXEC);
Al Viro52094c82011-02-21 21:34:47 -05001451}
1452
Al Viro9856fa12011-03-04 14:22:06 -05001453static inline int handle_dots(struct nameidata *nd, int type)
1454{
1455 if (type == LAST_DOTDOT) {
1456 if (nd->flags & LOOKUP_RCU) {
1457 if (follow_dotdot_rcu(nd))
1458 return -ECHILD;
1459 } else
1460 follow_dotdot(nd);
1461 }
1462 return 0;
1463}
1464
Al Viro951361f2011-03-04 14:44:37 -05001465static void terminate_walk(struct nameidata *nd)
1466{
1467 if (!(nd->flags & LOOKUP_RCU)) {
1468 path_put(&nd->path);
1469 } else {
1470 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001471 if (!(nd->flags & LOOKUP_ROOT))
1472 nd->root.mnt = NULL;
Al Viro32a79912012-07-18 20:43:19 +04001473 unlock_rcu_walk();
Al Viro951361f2011-03-04 14:44:37 -05001474 }
1475}
1476
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001477/*
1478 * Do we need to follow links? We _really_ want to be able
1479 * to do this check without having to look at inode->i_op,
1480 * so we keep a cache of "no, this doesn't need follow_link"
1481 * for the common case.
1482 */
Linus Torvalds7813b942011-08-07 09:53:20 -07001483static inline int should_follow_link(struct inode *inode, int follow)
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001484{
1485 if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
1486 if (likely(inode->i_op->follow_link))
1487 return follow;
1488
1489 /* This gets set once for the inode lifetime */
1490 spin_lock(&inode->i_lock);
1491 inode->i_opflags |= IOP_NOFOLLOW;
1492 spin_unlock(&inode->i_lock);
1493 }
1494 return 0;
1495}
1496
Al Viroce57dfc2011-03-13 19:58:58 -04001497static inline int walk_component(struct nameidata *nd, struct path *path,
1498 struct qstr *name, int type, int follow)
1499{
1500 struct inode *inode;
1501 int err;
1502 /*
1503 * "." and ".." are special - ".." especially so because it has
1504 * to be able to know about the current root directory and
1505 * parent relationships.
1506 */
1507 if (unlikely(type != LAST_NORM))
1508 return handle_dots(nd, type);
Miklos Szeredi697f5142012-05-21 17:30:05 +02001509 err = lookup_fast(nd, name, path, &inode);
Al Viroce57dfc2011-03-13 19:58:58 -04001510 if (unlikely(err)) {
Miklos Szeredi697f5142012-05-21 17:30:05 +02001511 if (err < 0)
1512 goto out_err;
1513
1514 err = lookup_slow(nd, name, path);
1515 if (err < 0)
1516 goto out_err;
1517
1518 inode = path->dentry->d_inode;
Al Viroce57dfc2011-03-13 19:58:58 -04001519 }
Miklos Szeredi697f5142012-05-21 17:30:05 +02001520 err = -ENOENT;
1521 if (!inode)
1522 goto out_path_put;
1523
Linus Torvalds7813b942011-08-07 09:53:20 -07001524 if (should_follow_link(inode, follow)) {
Al Viro19660af2011-03-25 10:32:48 -04001525 if (nd->flags & LOOKUP_RCU) {
1526 if (unlikely(unlazy_walk(nd, path->dentry))) {
Miklos Szeredi697f5142012-05-21 17:30:05 +02001527 err = -ECHILD;
1528 goto out_err;
Al Viro19660af2011-03-25 10:32:48 -04001529 }
1530 }
Al Viroce57dfc2011-03-13 19:58:58 -04001531 BUG_ON(inode != path->dentry->d_inode);
1532 return 1;
1533 }
1534 path_to_nameidata(path, nd);
1535 nd->inode = inode;
1536 return 0;
Miklos Szeredi697f5142012-05-21 17:30:05 +02001537
1538out_path_put:
1539 path_to_nameidata(path, nd);
1540out_err:
1541 terminate_walk(nd);
1542 return err;
Al Viroce57dfc2011-03-13 19:58:58 -04001543}
1544
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545/*
Al Virob3563792011-03-14 21:54:55 -04001546 * This limits recursive symlink follows to 8, while
1547 * limiting consecutive symlinks to 40.
1548 *
1549 * Without that kind of total limit, nasty chains of consecutive
1550 * symlinks can cause almost arbitrarily long lookups.
1551 */
1552static inline int nested_symlink(struct path *path, struct nameidata *nd)
1553{
1554 int res;
1555
Al Virob3563792011-03-14 21:54:55 -04001556 if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1557 path_put_conditional(path, nd);
1558 path_put(&nd->path);
1559 return -ELOOP;
1560 }
Erez Zadok1a4022f2011-05-21 01:19:59 -04001561 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
Al Virob3563792011-03-14 21:54:55 -04001562
1563 nd->depth++;
1564 current->link_count++;
1565
1566 do {
1567 struct path link = *path;
1568 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04001569
1570 res = follow_link(&link, nd, &cookie);
Al Viro6d7b5aa2012-06-10 04:15:17 -04001571 if (res)
1572 break;
1573 res = walk_component(nd, path, &nd->last,
1574 nd->last_type, LOOKUP_FOLLOW);
Al Viro574197e2011-03-14 22:20:34 -04001575 put_link(nd, &link, cookie);
Al Virob3563792011-03-14 21:54:55 -04001576 } while (res > 0);
1577
1578 current->link_count--;
1579 nd->depth--;
1580 return res;
1581}
1582
1583/*
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001584 * We really don't want to look at inode->i_op->lookup
1585 * when we don't have to. So we keep a cache bit in
1586 * the inode ->i_opflags field that says "yes, we can
1587 * do lookup on this inode".
1588 */
1589static inline int can_lookup(struct inode *inode)
1590{
1591 if (likely(inode->i_opflags & IOP_LOOKUP))
1592 return 1;
1593 if (likely(!inode->i_op->lookup))
1594 return 0;
1595
1596 /* We do this once for the lifetime of the inode */
1597 spin_lock(&inode->i_lock);
1598 inode->i_opflags |= IOP_LOOKUP;
1599 spin_unlock(&inode->i_lock);
1600 return 1;
1601}
1602
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001603/*
1604 * We can do the critical dentry name comparison and hashing
1605 * operations one word at a time, but we are limited to:
1606 *
1607 * - Architectures with fast unaligned word accesses. We could
1608 * do a "get_unaligned()" if this helps and is sufficiently
1609 * fast.
1610 *
1611 * - Little-endian machines (so that we can generate the mask
1612 * of low bytes efficiently). Again, we *could* do a byte
1613 * swapping load on big-endian architectures if that is not
1614 * expensive enough to make the optimization worthless.
1615 *
1616 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1617 * do not trap on the (extremely unlikely) case of a page
1618 * crossing operation.
1619 *
1620 * - Furthermore, we need an efficient 64-bit compile for the
1621 * 64-bit case in order to generate the "number of bytes in
1622 * the final mask". Again, that could be replaced with a
1623 * efficient population count instruction or similar.
1624 */
1625#ifdef CONFIG_DCACHE_WORD_ACCESS
1626
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001627#include <asm/word-at-a-time.h>
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001628
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001629#ifdef CONFIG_64BIT
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001630
1631static inline unsigned int fold_hash(unsigned long hash)
1632{
1633 hash += hash >> (8*sizeof(int));
1634 return hash;
1635}
1636
1637#else /* 32-bit case */
1638
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001639#define fold_hash(x) (x)
1640
1641#endif
1642
1643unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1644{
1645 unsigned long a, mask;
1646 unsigned long hash = 0;
1647
1648 for (;;) {
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001649 a = load_unaligned_zeropad(name);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001650 if (len < sizeof(unsigned long))
1651 break;
1652 hash += a;
Al Virof132c5b2012-03-22 21:59:52 +00001653 hash *= 9;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001654 name += sizeof(unsigned long);
1655 len -= sizeof(unsigned long);
1656 if (!len)
1657 goto done;
1658 }
1659 mask = ~(~0ul << len*8);
1660 hash += mask & a;
1661done:
1662 return fold_hash(hash);
1663}
1664EXPORT_SYMBOL(full_name_hash);
1665
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001666/*
1667 * Calculate the length and hash of the path component, and
1668 * return the length of the component;
1669 */
1670static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1671{
Linus Torvalds36126f82012-05-26 10:43:17 -07001672 unsigned long a, b, adata, bdata, mask, hash, len;
1673 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001674
1675 hash = a = 0;
1676 len = -sizeof(unsigned long);
1677 do {
1678 hash = (hash + a) * 9;
1679 len += sizeof(unsigned long);
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001680 a = load_unaligned_zeropad(name+len);
Linus Torvalds36126f82012-05-26 10:43:17 -07001681 b = a ^ REPEAT_BYTE('/');
1682 } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001683
Linus Torvalds36126f82012-05-26 10:43:17 -07001684 adata = prep_zero_mask(a, adata, &constants);
1685 bdata = prep_zero_mask(b, bdata, &constants);
1686
1687 mask = create_zero_mask(adata | bdata);
1688
1689 hash += a & zero_bytemask(mask);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001690 *hashp = fold_hash(hash);
1691
Linus Torvalds36126f82012-05-26 10:43:17 -07001692 return len + find_zero(mask);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001693}
1694
1695#else
1696
Linus Torvalds0145acc2012-03-02 14:32:59 -08001697unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1698{
1699 unsigned long hash = init_name_hash();
1700 while (len--)
1701 hash = partial_name_hash(*name++, hash);
1702 return end_name_hash(hash);
1703}
Linus Torvaldsae942ae2012-03-02 19:40:57 -08001704EXPORT_SYMBOL(full_name_hash);
Linus Torvalds0145acc2012-03-02 14:32:59 -08001705
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001706/*
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001707 * We know there's a real path component here of at least
1708 * one character.
1709 */
1710static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1711{
1712 unsigned long hash = init_name_hash();
1713 unsigned long len = 0, c;
1714
1715 c = (unsigned char)*name;
1716 do {
1717 len++;
1718 hash = partial_name_hash(c, hash);
1719 c = (unsigned char)name[len];
1720 } while (c && c != '/');
1721 *hashp = end_name_hash(hash);
1722 return len;
1723}
1724
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001725#endif
1726
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001727/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01001729 * This is the basic name resolution function, turning a pathname into
1730 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01001732 * Returns 0 and nd will have valid dentry and mnt on success.
1733 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 */
Al Viro6de88d72009-08-09 01:41:57 +04001735static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736{
1737 struct path next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
1740 while (*name=='/')
1741 name++;
1742 if (!*name)
Al Viro086e1832011-02-22 20:56:27 -05001743 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 /* At this point we know we have a real path component. */
1746 for(;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 struct qstr this;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001748 long len;
Al Virofe479a52011-02-22 15:10:03 -05001749 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Al Viro52094c82011-02-21 21:34:47 -05001751 err = may_lookup(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 if (err)
1753 break;
1754
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001755 len = hash_name(name, &this.hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 this.name = name;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001757 this.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
Al Virofe479a52011-02-22 15:10:03 -05001759 type = LAST_NORM;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001760 if (name[0] == '.') switch (len) {
Al Virofe479a52011-02-22 15:10:03 -05001761 case 2:
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001762 if (name[1] == '.') {
Al Virofe479a52011-02-22 15:10:03 -05001763 type = LAST_DOTDOT;
Al Viro16c2cd72011-02-22 15:50:10 -05001764 nd->flags |= LOOKUP_JUMPED;
1765 }
Al Virofe479a52011-02-22 15:10:03 -05001766 break;
1767 case 1:
1768 type = LAST_DOT;
1769 }
Al Viro5a202bc2011-03-08 14:17:44 -05001770 if (likely(type == LAST_NORM)) {
1771 struct dentry *parent = nd->path.dentry;
Al Viro16c2cd72011-02-22 15:50:10 -05001772 nd->flags &= ~LOOKUP_JUMPED;
Al Viro5a202bc2011-03-08 14:17:44 -05001773 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1774 err = parent->d_op->d_hash(parent, nd->inode,
1775 &this);
1776 if (err < 0)
1777 break;
1778 }
1779 }
Al Virofe479a52011-02-22 15:10:03 -05001780
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001781 if (!name[len])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 goto last_component;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001783 /*
1784 * If it wasn't NUL, we know it was '/'. Skip that
1785 * slash, and continue until no more slashes.
1786 */
1787 do {
1788 len++;
1789 } while (unlikely(name[len] == '/'));
1790 if (!name[len])
Al Virob3563792011-03-14 21:54:55 -04001791 goto last_component;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001792 name += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Al Viroce57dfc2011-03-13 19:58:58 -04001794 err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1795 if (err < 0)
1796 return err;
Al Virofe479a52011-02-22 15:10:03 -05001797
Al Viroce57dfc2011-03-13 19:58:58 -04001798 if (err) {
Al Virob3563792011-03-14 21:54:55 -04001799 err = nested_symlink(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 if (err)
Al Viroa7472ba2011-03-04 14:39:30 -05001801 return err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001802 }
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001803 if (can_lookup(nd->inode))
1804 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 err = -ENOTDIR;
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001806 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 /* here ends the main loop */
1808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809last_component:
Al Virob3563792011-03-14 21:54:55 -04001810 nd->last = this;
1811 nd->last_type = type;
Al Viro086e1832011-02-22 20:56:27 -05001812 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 }
Al Viro951361f2011-03-04 14:44:37 -05001814 terminate_walk(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 return err;
1816}
1817
Al Viro70e9b352011-03-05 21:12:22 -05001818static int path_init(int dfd, const char *name, unsigned int flags,
1819 struct nameidata *nd, struct file **fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820{
Prasanna Medaea3834d2005-04-29 16:00:17 +01001821 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
1823 nd->last_type = LAST_ROOT; /* if there are only slashes... */
Al Viro16c2cd72011-02-22 15:50:10 -05001824 nd->flags = flags | LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 nd->depth = 0;
Al Viro5b6ca022011-03-09 23:04:47 -05001826 if (flags & LOOKUP_ROOT) {
1827 struct inode *inode = nd->root.dentry->d_inode;
Al Viro73d049a2011-03-11 12:08:24 -05001828 if (*name) {
1829 if (!inode->i_op->lookup)
1830 return -ENOTDIR;
1831 retval = inode_permission(inode, MAY_EXEC);
1832 if (retval)
1833 return retval;
1834 }
Al Viro5b6ca022011-03-09 23:04:47 -05001835 nd->path = nd->root;
1836 nd->inode = inode;
1837 if (flags & LOOKUP_RCU) {
Al Viro32a79912012-07-18 20:43:19 +04001838 lock_rcu_walk();
Al Viro5b6ca022011-03-09 23:04:47 -05001839 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1840 } else {
1841 path_get(&nd->path);
1842 }
1843 return 0;
1844 }
1845
Al Viro2a737872009-04-07 11:49:53 -04001846 nd->root.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 if (*name=='/') {
Al Viroe41f7d42011-02-22 14:02:58 -05001849 if (flags & LOOKUP_RCU) {
Al Viro32a79912012-07-18 20:43:19 +04001850 lock_rcu_walk();
Al Viroe41f7d42011-02-22 14:02:58 -05001851 set_root_rcu(nd);
1852 } else {
1853 set_root(nd);
1854 path_get(&nd->root);
1855 }
Al Viro2a737872009-04-07 11:49:53 -04001856 nd->path = nd->root;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001857 } else if (dfd == AT_FDCWD) {
Al Viroe41f7d42011-02-22 14:02:58 -05001858 if (flags & LOOKUP_RCU) {
1859 struct fs_struct *fs = current->fs;
1860 unsigned seq;
1861
Al Viro32a79912012-07-18 20:43:19 +04001862 lock_rcu_walk();
Al Viroe41f7d42011-02-22 14:02:58 -05001863
1864 do {
1865 seq = read_seqcount_begin(&fs->seq);
1866 nd->path = fs->pwd;
1867 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1868 } while (read_seqcount_retry(&fs->seq, seq));
1869 } else {
1870 get_fs_pwd(current->fs, &nd->path);
1871 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001872 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001873 struct fd f = fdget_raw(dfd);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001874 struct dentry *dentry;
1875
Al Viro2903ff02012-08-28 12:52:22 -04001876 if (!f.file)
1877 return -EBADF;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001878
Al Viro2903ff02012-08-28 12:52:22 -04001879 dentry = f.file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001880
Al Virof52e0c12011-03-14 18:56:51 -04001881 if (*name) {
Al Viro2903ff02012-08-28 12:52:22 -04001882 if (!S_ISDIR(dentry->d_inode->i_mode)) {
1883 fdput(f);
1884 return -ENOTDIR;
1885 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001886
Al Viro4ad5abb2011-06-20 19:57:03 -04001887 retval = inode_permission(dentry->d_inode, MAY_EXEC);
Al Viro2903ff02012-08-28 12:52:22 -04001888 if (retval) {
1889 fdput(f);
1890 return retval;
1891 }
Al Virof52e0c12011-03-14 18:56:51 -04001892 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001893
Al Viro2903ff02012-08-28 12:52:22 -04001894 nd->path = f.file->f_path;
Al Viroe41f7d42011-02-22 14:02:58 -05001895 if (flags & LOOKUP_RCU) {
Al Viro2903ff02012-08-28 12:52:22 -04001896 if (f.need_put)
1897 *fp = f.file;
Al Viroe41f7d42011-02-22 14:02:58 -05001898 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro32a79912012-07-18 20:43:19 +04001899 lock_rcu_walk();
Al Viroe41f7d42011-02-22 14:02:58 -05001900 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001901 path_get(&nd->path);
1902 fdput(f);
Al Viroe41f7d42011-02-22 14:02:58 -05001903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 }
Al Viroe41f7d42011-02-22 14:02:58 -05001905
Nick Piggin31e6b012011-01-07 17:49:52 +11001906 nd->inode = nd->path.dentry->d_inode;
Al Viro9b4a9b12009-04-07 11:44:16 -04001907 return 0;
Al Viro9b4a9b12009-04-07 11:44:16 -04001908}
1909
Al Virobd92d7f2011-03-14 19:54:59 -04001910static inline int lookup_last(struct nameidata *nd, struct path *path)
1911{
1912 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1913 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1914
1915 nd->flags &= ~LOOKUP_PARENT;
1916 return walk_component(nd, path, &nd->last, nd->last_type,
1917 nd->flags & LOOKUP_FOLLOW);
1918}
1919
Al Viro9b4a9b12009-04-07 11:44:16 -04001920/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroee0827c2011-02-21 23:38:09 -05001921static int path_lookupat(int dfd, const char *name,
Al Viro9b4a9b12009-04-07 11:44:16 -04001922 unsigned int flags, struct nameidata *nd)
1923{
Al Viro70e9b352011-03-05 21:12:22 -05001924 struct file *base = NULL;
Al Virobd92d7f2011-03-14 19:54:59 -04001925 struct path path;
1926 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001927
1928 /*
1929 * Path walking is largely split up into 2 different synchronisation
1930 * schemes, rcu-walk and ref-walk (explained in
1931 * Documentation/filesystems/path-lookup.txt). These share much of the
1932 * path walk code, but some things particularly setup, cleanup, and
1933 * following mounts are sufficiently divergent that functions are
1934 * duplicated. Typically there is a function foo(), and its RCU
1935 * analogue, foo_rcu().
1936 *
1937 * -ECHILD is the error number of choice (just to avoid clashes) that
1938 * is returned if some aspect of an rcu-walk fails. Such an error must
1939 * be handled by restarting a traditional ref-walk (which will always
1940 * be able to complete).
1941 */
Al Virobd92d7f2011-03-14 19:54:59 -04001942 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
Al Viroee0827c2011-02-21 23:38:09 -05001943
Al Virobd92d7f2011-03-14 19:54:59 -04001944 if (unlikely(err))
1945 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001946
1947 current->total_link_count = 0;
Al Virobd92d7f2011-03-14 19:54:59 -04001948 err = link_path_walk(name, nd);
1949
1950 if (!err && !(flags & LOOKUP_PARENT)) {
Al Virobd92d7f2011-03-14 19:54:59 -04001951 err = lookup_last(nd, &path);
1952 while (err > 0) {
1953 void *cookie;
1954 struct path link = path;
Kees Cook800179c2012-07-25 17:29:07 -07001955 err = may_follow_link(&link, nd);
1956 if (unlikely(err))
1957 break;
Al Virobd92d7f2011-03-14 19:54:59 -04001958 nd->flags |= LOOKUP_PARENT;
Al Viro574197e2011-03-14 22:20:34 -04001959 err = follow_link(&link, nd, &cookie);
Al Viro6d7b5aa2012-06-10 04:15:17 -04001960 if (err)
1961 break;
1962 err = lookup_last(nd, &path);
Al Viro574197e2011-03-14 22:20:34 -04001963 put_link(nd, &link, cookie);
Al Virobd92d7f2011-03-14 19:54:59 -04001964 }
1965 }
Al Viroee0827c2011-02-21 23:38:09 -05001966
Al Viro9f1fafe2011-03-25 11:00:12 -04001967 if (!err)
1968 err = complete_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04001969
1970 if (!err && nd->flags & LOOKUP_DIRECTORY) {
1971 if (!nd->inode->i_op->lookup) {
1972 path_put(&nd->path);
Al Virobd23a532011-03-23 09:56:30 -04001973 err = -ENOTDIR;
Al Virobd92d7f2011-03-14 19:54:59 -04001974 }
1975 }
Al Viro16c2cd72011-02-22 15:50:10 -05001976
Al Viro70e9b352011-03-05 21:12:22 -05001977 if (base)
1978 fput(base);
Al Viroee0827c2011-02-21 23:38:09 -05001979
Al Viro5b6ca022011-03-09 23:04:47 -05001980 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
Al Viro2a737872009-04-07 11:49:53 -04001981 path_put(&nd->root);
1982 nd->root.mnt = NULL;
1983 }
Al Virobd92d7f2011-03-14 19:54:59 -04001984 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001985}
Nick Piggin31e6b012011-01-07 17:49:52 +11001986
Al Viroee0827c2011-02-21 23:38:09 -05001987static int do_path_lookup(int dfd, const char *name,
1988 unsigned int flags, struct nameidata *nd)
1989{
1990 int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1991 if (unlikely(retval == -ECHILD))
1992 retval = path_lookupat(dfd, name, flags, nd);
1993 if (unlikely(retval == -ESTALE))
1994 retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11001995
Jeff Laytonf78570d2012-10-10 15:25:20 -04001996 if (likely(!retval))
Jeff Laytonbfcec702012-10-10 15:25:23 -04001997 audit_inode(name, nd->path.dentry, flags & LOOKUP_PARENT);
Al Viro9b4a9b12009-04-07 11:44:16 -04001998 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999}
2000
Al Viro79714f72012-06-15 03:01:42 +04002001/* does lookup, returns the object with parent locked */
2002struct dentry *kern_path_locked(const char *name, struct path *path)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002003{
Al Viro79714f72012-06-15 03:01:42 +04002004 struct nameidata nd;
2005 struct dentry *d;
2006 int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd);
2007 if (err)
2008 return ERR_PTR(err);
2009 if (nd.last_type != LAST_NORM) {
2010 path_put(&nd.path);
2011 return ERR_PTR(-EINVAL);
2012 }
2013 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Al Viro1e0ea002012-07-22 23:46:21 +04002014 d = __lookup_hash(&nd.last, nd.path.dentry, 0);
Al Viro79714f72012-06-15 03:01:42 +04002015 if (IS_ERR(d)) {
2016 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2017 path_put(&nd.path);
2018 return d;
2019 }
2020 *path = nd.path;
2021 return d;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002022}
2023
Al Virod1811462008-08-02 00:49:18 -04002024int kern_path(const char *name, unsigned int flags, struct path *path)
2025{
2026 struct nameidata nd;
2027 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
2028 if (!res)
2029 *path = nd.path;
2030 return res;
2031}
2032
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002033/**
2034 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2035 * @dentry: pointer to dentry of the base directory
2036 * @mnt: pointer to vfs mount of the base directory
2037 * @name: pointer to file name
2038 * @flags: lookup flags
Al Viroe0a01242011-06-27 17:00:37 -04002039 * @path: pointer to struct path to fill
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002040 */
2041int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2042 const char *name, unsigned int flags,
Al Viroe0a01242011-06-27 17:00:37 -04002043 struct path *path)
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002044{
Al Viroe0a01242011-06-27 17:00:37 -04002045 struct nameidata nd;
2046 int err;
2047 nd.root.dentry = dentry;
2048 nd.root.mnt = mnt;
2049 BUG_ON(flags & LOOKUP_PARENT);
Al Viro5b6ca022011-03-09 23:04:47 -05002050 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
Al Viroe0a01242011-06-27 17:00:37 -04002051 err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
2052 if (!err)
2053 *path = nd.path;
2054 return err;
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002055}
2056
James Morris057f6c02007-04-26 00:12:05 -07002057/*
2058 * Restricted form of lookup. Doesn't follow links, single-component only,
2059 * needs parent already locked. Doesn't follow mounts.
2060 * SMP-safe.
2061 */
Adrian Bunka244e162006-03-31 02:32:11 -08002062static struct dentry *lookup_hash(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063{
Al Viro72bd8662012-06-10 17:17:17 -04002064 return __lookup_hash(&nd->last, nd->path.dentry, nd->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065}
2066
Christoph Hellwigeead1912007-10-16 23:25:38 -07002067/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07002068 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07002069 * @name: pathname component to lookup
2070 * @base: base directory to lookup from
2071 * @len: maximum length @len should be interpreted to
2072 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07002073 * Note that this routine is purely a helper for filesystem usage and should
2074 * not be called by generic code. Also note that by using this function the
Christoph Hellwigeead1912007-10-16 23:25:38 -07002075 * nameidata argument is passed to the filesystem methods and a filesystem
2076 * using this helper needs to be prepared for that.
2077 */
James Morris057f6c02007-04-26 00:12:05 -07002078struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2079{
James Morris057f6c02007-04-26 00:12:05 -07002080 struct qstr this;
Al Viro6a96ba52011-03-07 23:49:20 -05002081 unsigned int c;
Miklos Szeredicda309d2012-03-26 12:54:21 +02002082 int err;
James Morris057f6c02007-04-26 00:12:05 -07002083
David Woodhouse2f9092e2009-04-20 23:18:37 +01002084 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
2085
Al Viro6a96ba52011-03-07 23:49:20 -05002086 this.name = name;
2087 this.len = len;
Linus Torvalds0145acc2012-03-02 14:32:59 -08002088 this.hash = full_name_hash(name, len);
Al Viro6a96ba52011-03-07 23:49:20 -05002089 if (!len)
2090 return ERR_PTR(-EACCES);
2091
Al Viro6a96ba52011-03-07 23:49:20 -05002092 while (len--) {
2093 c = *(const unsigned char *)name++;
2094 if (c == '/' || c == '\0')
2095 return ERR_PTR(-EACCES);
Al Viro6a96ba52011-03-07 23:49:20 -05002096 }
Al Viro5a202bc2011-03-08 14:17:44 -05002097 /*
2098 * See if the low-level filesystem might want
2099 * to use its own hash..
2100 */
2101 if (base->d_flags & DCACHE_OP_HASH) {
2102 int err = base->d_op->d_hash(base, base->d_inode, &this);
2103 if (err < 0)
2104 return ERR_PTR(err);
2105 }
Christoph Hellwigeead1912007-10-16 23:25:38 -07002106
Miklos Szeredicda309d2012-03-26 12:54:21 +02002107 err = inode_permission(base->d_inode, MAY_EXEC);
2108 if (err)
2109 return ERR_PTR(err);
2110
Al Viro72bd8662012-06-10 17:17:17 -04002111 return __lookup_hash(&this, base, 0);
James Morris057f6c02007-04-26 00:12:05 -07002112}
2113
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002114int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2115 struct path *path, int *empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116{
Al Viro2d8f3032008-07-22 09:59:21 -04002117 struct nameidata nd;
Jeff Layton91a27b22012-10-10 15:25:28 -04002118 struct filename *tmp = getname_flags(name, flags, empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 int err = PTR_ERR(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 if (!IS_ERR(tmp)) {
Al Viro2d8f3032008-07-22 09:59:21 -04002121
2122 BUG_ON(flags & LOOKUP_PARENT);
2123
Jeff Layton91a27b22012-10-10 15:25:28 -04002124 err = do_path_lookup(dfd, tmp->name, flags, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 putname(tmp);
Al Viro2d8f3032008-07-22 09:59:21 -04002126 if (!err)
2127 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 }
2129 return err;
2130}
2131
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002132int user_path_at(int dfd, const char __user *name, unsigned flags,
2133 struct path *path)
2134{
Linus Torvaldsf7493e52012-03-22 16:10:40 -07002135 return user_path_at_empty(dfd, name, flags, path, NULL);
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002136}
2137
Jeff Layton91a27b22012-10-10 15:25:28 -04002138static struct filename *
2139user_path_parent(int dfd, const char __user *path, struct nameidata *nd)
Al Viro2ad94ae2008-07-21 09:32:51 -04002140{
Jeff Layton91a27b22012-10-10 15:25:28 -04002141 struct filename *s = getname(path);
Al Viro2ad94ae2008-07-21 09:32:51 -04002142 int error;
2143
2144 if (IS_ERR(s))
Jeff Layton91a27b22012-10-10 15:25:28 -04002145 return s;
Al Viro2ad94ae2008-07-21 09:32:51 -04002146
Jeff Layton91a27b22012-10-10 15:25:28 -04002147 error = do_path_lookup(dfd, s->name, LOOKUP_PARENT, nd);
2148 if (error) {
Al Viro2ad94ae2008-07-21 09:32:51 -04002149 putname(s);
Jeff Layton91a27b22012-10-10 15:25:28 -04002150 return ERR_PTR(error);
2151 }
Al Viro2ad94ae2008-07-21 09:32:51 -04002152
Jeff Layton91a27b22012-10-10 15:25:28 -04002153 return s;
Al Viro2ad94ae2008-07-21 09:32:51 -04002154}
2155
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156/*
2157 * It's inline, so penalty for filesystems that don't use sticky bit is
2158 * minimal.
2159 */
2160static inline int check_sticky(struct inode *dir, struct inode *inode)
2161{
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002162 kuid_t fsuid = current_fsuid();
David Howellsda9592e2008-11-14 10:39:05 +11002163
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 if (!(dir->i_mode & S_ISVTX))
2165 return 0;
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002166 if (uid_eq(inode->i_uid, fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 return 0;
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002168 if (uid_eq(dir->i_uid, fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 return 0;
Eric W. Biederman1a48e2a2011-11-14 16:24:06 -08002170 return !inode_capable(inode, CAP_FOWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171}
2172
2173/*
2174 * Check whether we can remove a link victim from directory dir, check
2175 * whether the type of victim is right.
2176 * 1. We can't do it if dir is read-only (done in permission())
2177 * 2. We should have write and exec permissions on dir
2178 * 3. We can't remove anything from append-only dir
2179 * 4. We can't do anything with immutable dir (done in permission())
2180 * 5. If the sticky bit on dir is set we should either
2181 * a. be owner of dir, or
2182 * b. be owner of victim, or
2183 * c. have CAP_FOWNER capability
2184 * 6. If the victim is append-only or immutable we can't do antyhing with
2185 * links pointing to it.
2186 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2187 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2188 * 9. We can't remove a root or mountpoint.
2189 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
2190 * nfs_async_unlink().
2191 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08002192static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193{
2194 int error;
2195
2196 if (!victim->d_inode)
2197 return -ENOENT;
2198
2199 BUG_ON(victim->d_parent->d_inode != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04002200 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
Al Virof419a2e2008-07-22 00:07:17 -04002202 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 if (error)
2204 return error;
2205 if (IS_APPEND(dir))
2206 return -EPERM;
2207 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
Hugh Dickinsf9454542008-11-19 15:36:38 -08002208 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 return -EPERM;
2210 if (isdir) {
2211 if (!S_ISDIR(victim->d_inode->i_mode))
2212 return -ENOTDIR;
2213 if (IS_ROOT(victim))
2214 return -EBUSY;
2215 } else if (S_ISDIR(victim->d_inode->i_mode))
2216 return -EISDIR;
2217 if (IS_DEADDIR(dir))
2218 return -ENOENT;
2219 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2220 return -EBUSY;
2221 return 0;
2222}
2223
2224/* Check whether we can create an object with dentry child in directory
2225 * dir.
2226 * 1. We can't do it if child already exists (open has special treatment for
2227 * this case, but since we are inlined it's OK)
2228 * 2. We can't do it if dir is read-only (done in permission())
2229 * 3. We should have write and exec permissions on dir
2230 * 4. We can't do it if dir is immutable (done in permission())
2231 */
Miklos Szeredia95164d2008-07-30 15:08:48 +02002232static inline int may_create(struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233{
2234 if (child->d_inode)
2235 return -EEXIST;
2236 if (IS_DEADDIR(dir))
2237 return -ENOENT;
Al Virof419a2e2008-07-22 00:07:17 -04002238 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239}
2240
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241/*
2242 * p1 and p2 should be directories on the same fs.
2243 */
2244struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2245{
2246 struct dentry *p;
2247
2248 if (p1 == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07002249 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 return NULL;
2251 }
2252
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002253 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002255 p = d_ancestor(p2, p1);
2256 if (p) {
2257 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2258 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
2259 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 }
2261
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002262 p = d_ancestor(p1, p2);
2263 if (p) {
2264 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2265 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2266 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 }
2268
Ingo Molnarf2eace22006-07-03 00:25:05 -07002269 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2270 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 return NULL;
2272}
2273
2274void unlock_rename(struct dentry *p1, struct dentry *p2)
2275{
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002276 mutex_unlock(&p1->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 if (p1 != p2) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002278 mutex_unlock(&p2->d_inode->i_mutex);
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002279 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 }
2281}
2282
Al Viro4acdaf22011-07-26 01:42:34 -04002283int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viro312b63f2012-06-10 18:09:36 -04002284 bool want_excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002286 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 if (error)
2288 return error;
2289
Al Viroacfa4382008-12-04 10:06:33 -05002290 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 return -EACCES; /* shouldn't it be ENOSYS? */
2292 mode &= S_IALLUGO;
2293 mode |= S_IFREG;
2294 error = security_inode_create(dir, dentry, mode);
2295 if (error)
2296 return error;
Al Viro312b63f2012-06-10 18:09:36 -04002297 error = dir->i_op->create(dir, dentry, mode, want_excl);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002298 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002299 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 return error;
2301}
2302
Al Viro73d049a2011-03-11 12:08:24 -05002303static int may_open(struct path *path, int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002305 struct dentry *dentry = path->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 struct inode *inode = dentry->d_inode;
2307 int error;
2308
Al Virobcda7652011-03-13 16:42:14 -04002309 /* O_PATH? */
2310 if (!acc_mode)
2311 return 0;
2312
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 if (!inode)
2314 return -ENOENT;
2315
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002316 switch (inode->i_mode & S_IFMT) {
2317 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002319 case S_IFDIR:
2320 if (acc_mode & MAY_WRITE)
2321 return -EISDIR;
2322 break;
2323 case S_IFBLK:
2324 case S_IFCHR:
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002325 if (path->mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002327 /*FALLTHRU*/
2328 case S_IFIFO:
2329 case S_IFSOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002331 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08002332 }
Dave Hansenb41572e2007-10-16 23:31:14 -07002333
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002334 error = inode_permission(inode, acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07002335 if (error)
2336 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05002337
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 /*
2339 * An append-only file must be opened in append mode for writing.
2340 */
2341 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05002342 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05002343 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05002345 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 }
2347
2348 /* O_NOATIME can only be set by the owner or superuser */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07002349 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
Al Viro7715b522009-12-16 03:54:00 -05002350 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
J. Bruce Fieldsf3c7691e2011-09-21 10:58:13 -04002352 return 0;
Al Viro7715b522009-12-16 03:54:00 -05002353}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
Jeff Laytone1181ee2010-12-07 16:19:50 -05002355static int handle_truncate(struct file *filp)
Al Viro7715b522009-12-16 03:54:00 -05002356{
Jeff Laytone1181ee2010-12-07 16:19:50 -05002357 struct path *path = &filp->f_path;
Al Viro7715b522009-12-16 03:54:00 -05002358 struct inode *inode = path->dentry->d_inode;
2359 int error = get_write_access(inode);
2360 if (error)
2361 return error;
2362 /*
2363 * Refuse to truncate files with mandatory locks held on them.
2364 */
2365 error = locks_verify_locked(inode);
2366 if (!error)
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +09002367 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05002368 if (!error) {
2369 error = do_truncate(path->dentry, 0,
2370 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
Jeff Laytone1181ee2010-12-07 16:19:50 -05002371 filp);
Al Viro7715b522009-12-16 03:54:00 -05002372 }
2373 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04002374 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375}
2376
Dave Hansend57999e2008-02-15 14:37:27 -08002377static inline int open_to_namei_flags(int flag)
2378{
Al Viro8a5e9292011-06-25 19:15:54 -04002379 if ((flag & O_ACCMODE) == 3)
2380 flag--;
Dave Hansend57999e2008-02-15 14:37:27 -08002381 return flag;
2382}
2383
Miklos Szeredid18e9002012-06-05 15:10:17 +02002384static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode)
2385{
2386 int error = security_path_mknod(dir, dentry, mode, 0);
2387 if (error)
2388 return error;
2389
2390 error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
2391 if (error)
2392 return error;
2393
2394 return security_inode_create(dir->dentry->d_inode, dentry, mode);
2395}
2396
David Howells1acf0af2012-06-14 16:13:46 +01002397/*
2398 * Attempt to atomically look up, create and open a file from a negative
2399 * dentry.
2400 *
2401 * Returns 0 if successful. The file will have been created and attached to
2402 * @file by the filesystem calling finish_open().
2403 *
2404 * Returns 1 if the file was looked up only or didn't need creating. The
2405 * caller will need to perform the open themselves. @path will have been
2406 * updated to point to the new dentry. This may be negative.
2407 *
2408 * Returns an error code otherwise.
2409 */
Al Viro2675a4e2012-06-22 12:41:10 +04002410static int atomic_open(struct nameidata *nd, struct dentry *dentry,
2411 struct path *path, struct file *file,
2412 const struct open_flags *op,
Al Viro64894cf2012-07-31 00:53:35 +04002413 bool got_write, bool need_lookup,
Al Viro2675a4e2012-06-22 12:41:10 +04002414 int *opened)
Miklos Szeredid18e9002012-06-05 15:10:17 +02002415{
2416 struct inode *dir = nd->path.dentry->d_inode;
2417 unsigned open_flag = open_to_namei_flags(op->open_flag);
2418 umode_t mode;
2419 int error;
2420 int acc_mode;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002421 int create_error = 0;
2422 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
2423
2424 BUG_ON(dentry->d_inode);
2425
2426 /* Don't create child dentry for a dead directory. */
2427 if (unlikely(IS_DEADDIR(dir))) {
Al Viro2675a4e2012-06-22 12:41:10 +04002428 error = -ENOENT;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002429 goto out;
2430 }
2431
Miklos Szeredi62b259d2012-08-15 13:01:24 +02002432 mode = op->mode;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002433 if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
2434 mode &= ~current_umask();
2435
Al Virof8310c52012-07-30 11:50:30 +04002436 if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT)) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002437 open_flag &= ~O_TRUNC;
Al Viro47237682012-06-10 05:01:45 -04002438 *opened |= FILE_CREATED;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002439 }
2440
2441 /*
2442 * Checking write permission is tricky, bacuse we don't know if we are
2443 * going to actually need it: O_CREAT opens should work as long as the
2444 * file exists. But checking existence breaks atomicity. The trick is
2445 * to check access and if not granted clear O_CREAT from the flags.
2446 *
2447 * Another problem is returing the "right" error value (e.g. for an
2448 * O_EXCL open we want to return EEXIST not EROFS).
2449 */
Al Viro64894cf2012-07-31 00:53:35 +04002450 if (((open_flag & (O_CREAT | O_TRUNC)) ||
2451 (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) {
2452 if (!(open_flag & O_CREAT)) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002453 /*
2454 * No O_CREATE -> atomicity not a requirement -> fall
2455 * back to lookup + open
2456 */
2457 goto no_open;
2458 } else if (open_flag & (O_EXCL | O_TRUNC)) {
2459 /* Fall back and fail with the right error */
Al Viro64894cf2012-07-31 00:53:35 +04002460 create_error = -EROFS;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002461 goto no_open;
2462 } else {
2463 /* No side effects, safe to clear O_CREAT */
Al Viro64894cf2012-07-31 00:53:35 +04002464 create_error = -EROFS;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002465 open_flag &= ~O_CREAT;
2466 }
2467 }
2468
2469 if (open_flag & O_CREAT) {
Miklos Szeredi38227f72012-08-15 13:01:24 +02002470 error = may_o_create(&nd->path, dentry, mode);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002471 if (error) {
2472 create_error = error;
2473 if (open_flag & O_EXCL)
2474 goto no_open;
2475 open_flag &= ~O_CREAT;
2476 }
2477 }
2478
2479 if (nd->flags & LOOKUP_DIRECTORY)
2480 open_flag |= O_DIRECTORY;
2481
Al Viro30d90492012-06-22 12:40:19 +04002482 file->f_path.dentry = DENTRY_NOT_SET;
2483 file->f_path.mnt = nd->path.mnt;
2484 error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode,
Al Viro47237682012-06-10 05:01:45 -04002485 opened);
Al Virod9585272012-06-22 12:39:14 +04002486 if (error < 0) {
Al Virod9585272012-06-22 12:39:14 +04002487 if (create_error && error == -ENOENT)
2488 error = create_error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002489 goto out;
2490 }
2491
2492 acc_mode = op->acc_mode;
Al Viro47237682012-06-10 05:01:45 -04002493 if (*opened & FILE_CREATED) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002494 fsnotify_create(dir, dentry);
2495 acc_mode = MAY_OPEN;
2496 }
2497
Al Virod9585272012-06-22 12:39:14 +04002498 if (error) { /* returned 1, that is */
Al Viro30d90492012-06-22 12:40:19 +04002499 if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
Al Viro2675a4e2012-06-22 12:41:10 +04002500 error = -EIO;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002501 goto out;
2502 }
Al Viro30d90492012-06-22 12:40:19 +04002503 if (file->f_path.dentry) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002504 dput(dentry);
Al Viro30d90492012-06-22 12:40:19 +04002505 dentry = file->f_path.dentry;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002506 }
Sage Weil62b2ce92012-08-15 13:30:12 -07002507 if (create_error && dentry->d_inode == NULL) {
2508 error = create_error;
2509 goto out;
2510 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02002511 goto looked_up;
2512 }
2513
2514 /*
2515 * We didn't have the inode before the open, so check open permission
2516 * here.
2517 */
Al Viro2675a4e2012-06-22 12:41:10 +04002518 error = may_open(&file->f_path, acc_mode, open_flag);
2519 if (error)
2520 fput(file);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002521
2522out:
2523 dput(dentry);
Al Viro2675a4e2012-06-22 12:41:10 +04002524 return error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002525
Miklos Szeredid18e9002012-06-05 15:10:17 +02002526no_open:
2527 if (need_lookup) {
Al Viro72bd8662012-06-10 17:17:17 -04002528 dentry = lookup_real(dir, dentry, nd->flags);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002529 if (IS_ERR(dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04002530 return PTR_ERR(dentry);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002531
2532 if (create_error) {
2533 int open_flag = op->open_flag;
2534
Al Viro2675a4e2012-06-22 12:41:10 +04002535 error = create_error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002536 if ((open_flag & O_EXCL)) {
2537 if (!dentry->d_inode)
2538 goto out;
2539 } else if (!dentry->d_inode) {
2540 goto out;
2541 } else if ((open_flag & O_TRUNC) &&
2542 S_ISREG(dentry->d_inode->i_mode)) {
2543 goto out;
2544 }
2545 /* will fail later, go on to get the right error */
2546 }
2547 }
2548looked_up:
2549 path->dentry = dentry;
2550 path->mnt = nd->path.mnt;
Al Viro2675a4e2012-06-22 12:41:10 +04002551 return 1;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002552}
2553
Nick Piggin31e6b012011-01-07 17:49:52 +11002554/*
David Howells1acf0af2012-06-14 16:13:46 +01002555 * Look up and maybe create and open the last component.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002556 *
2557 * Must be called with i_mutex held on parent.
2558 *
David Howells1acf0af2012-06-14 16:13:46 +01002559 * Returns 0 if the file was successfully atomically created (if necessary) and
2560 * opened. In this case the file will be returned attached to @file.
2561 *
2562 * Returns 1 if the file was not completely opened at this time, though lookups
2563 * and creations will have been performed and the dentry returned in @path will
2564 * be positive upon return if O_CREAT was specified. If O_CREAT wasn't
2565 * specified then a negative dentry may be returned.
2566 *
2567 * An error code is returned otherwise.
2568 *
2569 * FILE_CREATE will be set in @*opened if the dentry was created and will be
2570 * cleared otherwise prior to returning.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002571 */
Al Viro2675a4e2012-06-22 12:41:10 +04002572static int lookup_open(struct nameidata *nd, struct path *path,
2573 struct file *file,
2574 const struct open_flags *op,
Al Viro64894cf2012-07-31 00:53:35 +04002575 bool got_write, int *opened)
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002576{
2577 struct dentry *dir = nd->path.dentry;
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002578 struct inode *dir_inode = dir->d_inode;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002579 struct dentry *dentry;
2580 int error;
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002581 bool need_lookup;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002582
Al Viro47237682012-06-10 05:01:45 -04002583 *opened &= ~FILE_CREATED;
Al Viro201f9562012-06-22 12:42:10 +04002584 dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002585 if (IS_ERR(dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04002586 return PTR_ERR(dentry);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002587
Miklos Szeredid18e9002012-06-05 15:10:17 +02002588 /* Cached positive dentry: will open in f_op->open */
2589 if (!need_lookup && dentry->d_inode)
2590 goto out_no_open;
2591
2592 if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) {
Al Viro64894cf2012-07-31 00:53:35 +04002593 return atomic_open(nd, dentry, path, file, op, got_write,
Al Viro47237682012-06-10 05:01:45 -04002594 need_lookup, opened);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002595 }
2596
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002597 if (need_lookup) {
2598 BUG_ON(dentry->d_inode);
2599
Al Viro72bd8662012-06-10 17:17:17 -04002600 dentry = lookup_real(dir_inode, dentry, nd->flags);
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002601 if (IS_ERR(dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04002602 return PTR_ERR(dentry);
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002603 }
2604
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002605 /* Negative dentry, just create the file */
2606 if (!dentry->d_inode && (op->open_flag & O_CREAT)) {
2607 umode_t mode = op->mode;
2608 if (!IS_POSIXACL(dir->d_inode))
2609 mode &= ~current_umask();
2610 /*
2611 * This write is needed to ensure that a
2612 * rw->ro transition does not occur between
2613 * the time when the file is created and when
2614 * a permanent write count is taken through
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02002615 * the 'struct file' in finish_open().
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002616 */
Al Viro64894cf2012-07-31 00:53:35 +04002617 if (!got_write) {
2618 error = -EROFS;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002619 goto out_dput;
Al Viro64894cf2012-07-31 00:53:35 +04002620 }
Al Viro47237682012-06-10 05:01:45 -04002621 *opened |= FILE_CREATED;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002622 error = security_path_mknod(&nd->path, dentry, mode, 0);
2623 if (error)
2624 goto out_dput;
Al Viro312b63f2012-06-10 18:09:36 -04002625 error = vfs_create(dir->d_inode, dentry, mode,
2626 nd->flags & LOOKUP_EXCL);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002627 if (error)
2628 goto out_dput;
2629 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02002630out_no_open:
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002631 path->dentry = dentry;
2632 path->mnt = nd->path.mnt;
Al Viro2675a4e2012-06-22 12:41:10 +04002633 return 1;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002634
2635out_dput:
2636 dput(dentry);
Al Viro2675a4e2012-06-22 12:41:10 +04002637 return error;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002638}
2639
2640/*
Al Virofe2d35f2011-03-05 22:58:25 -05002641 * Handle the last step of open()
Nick Piggin31e6b012011-01-07 17:49:52 +11002642 */
Al Viro2675a4e2012-06-22 12:41:10 +04002643static int do_last(struct nameidata *nd, struct path *path,
2644 struct file *file, const struct open_flags *op,
2645 int *opened, const char *pathname)
Al Virofb1cc552009-12-24 01:58:28 -05002646{
Al Viroa1e28032009-12-24 02:12:06 -05002647 struct dentry *dir = nd->path.dentry;
Al Viroca344a892011-03-09 00:36:45 -05002648 int open_flag = op->open_flag;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002649 bool will_truncate = (open_flag & O_TRUNC) != 0;
Al Viro64894cf2012-07-31 00:53:35 +04002650 bool got_write = false;
Al Virobcda7652011-03-13 16:42:14 -04002651 int acc_mode = op->acc_mode;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02002652 struct inode *inode;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002653 bool symlink_ok = false;
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02002654 struct path save_parent = { .dentry = NULL, .mnt = NULL };
2655 bool retried = false;
Al Viro16c2cd72011-02-22 15:50:10 -05002656 int error;
Al Virofb1cc552009-12-24 01:58:28 -05002657
Al Viroc3e380b2011-02-23 13:39:45 -05002658 nd->flags &= ~LOOKUP_PARENT;
2659 nd->flags |= op->intent;
2660
Al Viro1f36f772009-12-26 10:56:19 -05002661 switch (nd->last_type) {
2662 case LAST_DOTDOT:
Neil Brown176306f2010-05-24 16:57:56 +10002663 case LAST_DOT:
Al Virofe2d35f2011-03-05 22:58:25 -05002664 error = handle_dots(nd, nd->last_type);
2665 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04002666 return error;
Al Viro1f36f772009-12-26 10:56:19 -05002667 /* fallthrough */
Al Viro1f36f772009-12-26 10:56:19 -05002668 case LAST_ROOT:
Al Viro9f1fafe2011-03-25 11:00:12 -04002669 error = complete_walk(nd);
Al Viro16c2cd72011-02-22 15:50:10 -05002670 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04002671 return error;
Jeff Laytonbfcec702012-10-10 15:25:23 -04002672 audit_inode(pathname, nd->path.dentry, 0);
Al Viroca344a892011-03-09 00:36:45 -05002673 if (open_flag & O_CREAT) {
Al Virofe2d35f2011-03-05 22:58:25 -05002674 error = -EISDIR;
Al Viro2675a4e2012-06-22 12:41:10 +04002675 goto out;
Al Virofe2d35f2011-03-05 22:58:25 -05002676 }
Miklos Szeredie83db162012-06-05 15:10:29 +02002677 goto finish_open;
Al Viro1f36f772009-12-26 10:56:19 -05002678 case LAST_BIND:
Al Viro9f1fafe2011-03-25 11:00:12 -04002679 error = complete_walk(nd);
Al Viro16c2cd72011-02-22 15:50:10 -05002680 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04002681 return error;
Jeff Laytonbfcec702012-10-10 15:25:23 -04002682 audit_inode(pathname, dir, 0);
Miklos Szeredie83db162012-06-05 15:10:29 +02002683 goto finish_open;
Al Viro1f36f772009-12-26 10:56:19 -05002684 }
Al Viro67ee3ad2009-12-26 07:01:01 -05002685
Al Viroca344a892011-03-09 00:36:45 -05002686 if (!(open_flag & O_CREAT)) {
Al Virofe2d35f2011-03-05 22:58:25 -05002687 if (nd->last.name[nd->last.len])
2688 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
Al Virobcda7652011-03-13 16:42:14 -04002689 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002690 symlink_ok = true;
Al Virofe2d35f2011-03-05 22:58:25 -05002691 /* we _can_ be in RCU mode here */
Miklos Szeredia1eb3312012-05-21 17:30:07 +02002692 error = lookup_fast(nd, &nd->last, path, &inode);
Miklos Szeredi71574862012-06-05 15:10:14 +02002693 if (likely(!error))
2694 goto finish_lookup;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02002695
Miklos Szeredi71574862012-06-05 15:10:14 +02002696 if (error < 0)
Al Viro2675a4e2012-06-22 12:41:10 +04002697 goto out;
Miklos Szeredi37d7fff2012-06-05 15:10:12 +02002698
Miklos Szeredi71574862012-06-05 15:10:14 +02002699 BUG_ON(nd->inode != dir->d_inode);
Miklos Szeredib6183df2012-06-05 15:10:13 +02002700 } else {
2701 /* create side of things */
2702 /*
2703 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
2704 * has been cleared when we got to the last component we are
2705 * about to look up
2706 */
2707 error = complete_walk(nd);
2708 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04002709 return error;
Miklos Szeredib6183df2012-06-05 15:10:13 +02002710
Jeff Laytonbfcec702012-10-10 15:25:23 -04002711 audit_inode(pathname, dir, 0);
Miklos Szeredib6183df2012-06-05 15:10:13 +02002712 error = -EISDIR;
2713 /* trailing slashes? */
2714 if (nd->last.name[nd->last.len])
Al Viro2675a4e2012-06-22 12:41:10 +04002715 goto out;
Al Virofe2d35f2011-03-05 22:58:25 -05002716 }
2717
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02002718retry_lookup:
Al Viro64894cf2012-07-31 00:53:35 +04002719 if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
2720 error = mnt_want_write(nd->path.mnt);
2721 if (!error)
2722 got_write = true;
2723 /*
2724 * do _not_ fail yet - we might not need that or fail with
2725 * a different error; let lookup_open() decide; we'll be
2726 * dropping this one anyway.
2727 */
2728 }
Al Viroa1e28032009-12-24 02:12:06 -05002729 mutex_lock(&dir->d_inode->i_mutex);
Al Viro64894cf2012-07-31 00:53:35 +04002730 error = lookup_open(nd, path, file, op, got_write, opened);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002731 mutex_unlock(&dir->d_inode->i_mutex);
Al Viroa1e28032009-12-24 02:12:06 -05002732
Al Viro2675a4e2012-06-22 12:41:10 +04002733 if (error <= 0) {
2734 if (error)
Miklos Szeredid18e9002012-06-05 15:10:17 +02002735 goto out;
2736
Al Viro47237682012-06-10 05:01:45 -04002737 if ((*opened & FILE_CREATED) ||
Al Viro2675a4e2012-06-22 12:41:10 +04002738 !S_ISREG(file->f_path.dentry->d_inode->i_mode))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002739 will_truncate = false;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002740
Jeff Laytonbfcec702012-10-10 15:25:23 -04002741 audit_inode(pathname, file->f_path.dentry, 0);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002742 goto opened;
2743 }
Al Virofb1cc552009-12-24 01:58:28 -05002744
Al Viro47237682012-06-10 05:01:45 -04002745 if (*opened & FILE_CREATED) {
Al Viro9b44f1b2011-03-09 00:17:27 -05002746 /* Don't check for write permission, don't truncate */
Al Viroca344a892011-03-09 00:36:45 -05002747 open_flag &= ~O_TRUNC;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002748 will_truncate = false;
Al Virobcda7652011-03-13 16:42:14 -04002749 acc_mode = MAY_OPEN;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002750 path_to_nameidata(path, nd);
Miklos Szeredie83db162012-06-05 15:10:29 +02002751 goto finish_open_created;
Al Virofb1cc552009-12-24 01:58:28 -05002752 }
2753
2754 /*
Jeff Layton3134f372012-07-25 10:19:47 -04002755 * create/update audit record if it already exists.
Al Virofb1cc552009-12-24 01:58:28 -05002756 */
Jeff Layton3134f372012-07-25 10:19:47 -04002757 if (path->dentry->d_inode)
Jeff Laytonbfcec702012-10-10 15:25:23 -04002758 audit_inode(pathname, path->dentry, 0);
Al Virofb1cc552009-12-24 01:58:28 -05002759
Miklos Szeredid18e9002012-06-05 15:10:17 +02002760 /*
2761 * If atomic_open() acquired write access it is dropped now due to
2762 * possible mount and symlink following (this might be optimized away if
2763 * necessary...)
2764 */
Al Viro64894cf2012-07-31 00:53:35 +04002765 if (got_write) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002766 mnt_drop_write(nd->path.mnt);
Al Viro64894cf2012-07-31 00:53:35 +04002767 got_write = false;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002768 }
2769
Al Virofb1cc552009-12-24 01:58:28 -05002770 error = -EEXIST;
Al Virof8310c52012-07-30 11:50:30 +04002771 if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))
Al Virofb1cc552009-12-24 01:58:28 -05002772 goto exit_dput;
2773
David Howells9875cf82011-01-14 18:45:21 +00002774 error = follow_managed(path, nd->flags);
2775 if (error < 0)
2776 goto exit_dput;
Al Virofb1cc552009-12-24 01:58:28 -05002777
Al Viroa3fbbde2011-11-07 21:21:26 +00002778 if (error)
2779 nd->flags |= LOOKUP_JUMPED;
2780
Miklos Szeredidecf3402012-05-21 17:30:08 +02002781 BUG_ON(nd->flags & LOOKUP_RCU);
2782 inode = path->dentry->d_inode;
Miklos Szeredi5f5daac2012-05-21 17:30:14 +02002783finish_lookup:
2784 /* we _can_ be in RCU mode here */
Al Virofb1cc552009-12-24 01:58:28 -05002785 error = -ENOENT;
Miklos Szeredi54c33e72012-05-21 17:30:10 +02002786 if (!inode) {
2787 path_to_nameidata(path, nd);
Al Viro2675a4e2012-06-22 12:41:10 +04002788 goto out;
Miklos Szeredi54c33e72012-05-21 17:30:10 +02002789 }
Al Viro9e67f362009-12-26 07:04:50 -05002790
Miklos Szeredid45ea862012-05-21 17:30:09 +02002791 if (should_follow_link(inode, !symlink_ok)) {
2792 if (nd->flags & LOOKUP_RCU) {
2793 if (unlikely(unlazy_walk(nd, path->dentry))) {
2794 error = -ECHILD;
Al Viro2675a4e2012-06-22 12:41:10 +04002795 goto out;
Miklos Szeredid45ea862012-05-21 17:30:09 +02002796 }
2797 }
2798 BUG_ON(inode != path->dentry->d_inode);
Al Viro2675a4e2012-06-22 12:41:10 +04002799 return 1;
Miklos Szeredid45ea862012-05-21 17:30:09 +02002800 }
Al Virofb1cc552009-12-24 01:58:28 -05002801
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02002802 if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) {
2803 path_to_nameidata(path, nd);
2804 } else {
2805 save_parent.dentry = nd->path.dentry;
2806 save_parent.mnt = mntget(path->mnt);
2807 nd->path.dentry = path->dentry;
2808
2809 }
Miklos Szeredidecf3402012-05-21 17:30:08 +02002810 nd->inode = inode;
Al Viroa3fbbde2011-11-07 21:21:26 +00002811 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
2812 error = complete_walk(nd);
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02002813 if (error) {
2814 path_put(&save_parent);
Al Viro2675a4e2012-06-22 12:41:10 +04002815 return error;
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02002816 }
Al Virofb1cc552009-12-24 01:58:28 -05002817 error = -EISDIR;
Miklos Szeredi050ac842012-05-21 17:30:12 +02002818 if ((open_flag & O_CREAT) && S_ISDIR(nd->inode->i_mode))
Al Viro2675a4e2012-06-22 12:41:10 +04002819 goto out;
Miklos Szerediaf2f5542012-05-21 17:30:11 +02002820 error = -ENOTDIR;
2821 if ((nd->flags & LOOKUP_DIRECTORY) && !nd->inode->i_op->lookup)
Al Viro2675a4e2012-06-22 12:41:10 +04002822 goto out;
Jeff Laytonbfcec702012-10-10 15:25:23 -04002823 audit_inode(pathname, nd->path.dentry, 0);
Miklos Szeredie83db162012-06-05 15:10:29 +02002824finish_open:
Al Viro6c0d46c2011-03-09 00:59:59 -05002825 if (!S_ISREG(nd->inode->i_mode))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002826 will_truncate = false;
Al Viro6c0d46c2011-03-09 00:59:59 -05002827
Al Viro0f9d1a12011-03-09 00:13:14 -05002828 if (will_truncate) {
2829 error = mnt_want_write(nd->path.mnt);
2830 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04002831 goto out;
Al Viro64894cf2012-07-31 00:53:35 +04002832 got_write = true;
Al Viro0f9d1a12011-03-09 00:13:14 -05002833 }
Miklos Szeredie83db162012-06-05 15:10:29 +02002834finish_open_created:
Al Virobcda7652011-03-13 16:42:14 -04002835 error = may_open(&nd->path, acc_mode, open_flag);
Al Viroca344a892011-03-09 00:36:45 -05002836 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04002837 goto out;
Al Viro30d90492012-06-22 12:40:19 +04002838 file->f_path.mnt = nd->path.mnt;
2839 error = finish_open(file, nd->path.dentry, NULL, opened);
2840 if (error) {
Al Viro30d90492012-06-22 12:40:19 +04002841 if (error == -EOPENSTALE)
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02002842 goto stale_open;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02002843 goto out;
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02002844 }
Miklos Szeredia8277b92012-06-05 15:10:32 +02002845opened:
Al Viro2675a4e2012-06-22 12:41:10 +04002846 error = open_check_o_direct(file);
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02002847 if (error)
2848 goto exit_fput;
Al Viro2675a4e2012-06-22 12:41:10 +04002849 error = ima_file_check(file, op->acc_mode);
Miklos Szerediaa4caad2012-06-05 15:10:28 +02002850 if (error)
2851 goto exit_fput;
2852
2853 if (will_truncate) {
Al Viro2675a4e2012-06-22 12:41:10 +04002854 error = handle_truncate(file);
Miklos Szerediaa4caad2012-06-05 15:10:28 +02002855 if (error)
2856 goto exit_fput;
Al Viro0f9d1a12011-03-09 00:13:14 -05002857 }
Al Viroca344a892011-03-09 00:36:45 -05002858out:
Al Viro64894cf2012-07-31 00:53:35 +04002859 if (got_write)
Al Viro0f9d1a12011-03-09 00:13:14 -05002860 mnt_drop_write(nd->path.mnt);
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02002861 path_put(&save_parent);
Miklos Szeredie276ae62012-05-21 17:30:06 +02002862 terminate_walk(nd);
Al Viro2675a4e2012-06-22 12:41:10 +04002863 return error;
Al Virofb1cc552009-12-24 01:58:28 -05002864
Al Virofb1cc552009-12-24 01:58:28 -05002865exit_dput:
2866 path_put_conditional(path, nd);
Al Viroca344a892011-03-09 00:36:45 -05002867 goto out;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02002868exit_fput:
Al Viro2675a4e2012-06-22 12:41:10 +04002869 fput(file);
2870 goto out;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02002871
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02002872stale_open:
2873 /* If no saved parent or already retried then can't retry */
2874 if (!save_parent.dentry || retried)
2875 goto out;
2876
2877 BUG_ON(save_parent.dentry != dir);
2878 path_put(&nd->path);
2879 nd->path = save_parent;
2880 nd->inode = dir->d_inode;
2881 save_parent.mnt = NULL;
2882 save_parent.dentry = NULL;
Al Viro64894cf2012-07-31 00:53:35 +04002883 if (got_write) {
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02002884 mnt_drop_write(nd->path.mnt);
Al Viro64894cf2012-07-31 00:53:35 +04002885 got_write = false;
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02002886 }
2887 retried = true;
2888 goto retry_lookup;
Al Virofb1cc552009-12-24 01:58:28 -05002889}
2890
Al Viro13aab422011-02-23 17:54:08 -05002891static struct file *path_openat(int dfd, const char *pathname,
Al Viro73d049a2011-03-11 12:08:24 -05002892 struct nameidata *nd, const struct open_flags *op, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893{
Al Virofe2d35f2011-03-05 22:58:25 -05002894 struct file *base = NULL;
Al Viro30d90492012-06-22 12:40:19 +04002895 struct file *file;
Al Viro9850c052010-01-13 15:01:15 -05002896 struct path path;
Al Viro47237682012-06-10 05:01:45 -04002897 int opened = 0;
Al Viro13aab422011-02-23 17:54:08 -05002898 int error;
Nick Piggin31e6b012011-01-07 17:49:52 +11002899
Al Viro30d90492012-06-22 12:40:19 +04002900 file = get_empty_filp();
2901 if (!file)
Nick Piggin31e6b012011-01-07 17:49:52 +11002902 return ERR_PTR(-ENFILE);
2903
Al Viro30d90492012-06-22 12:40:19 +04002904 file->f_flags = op->open_flag;
Nick Piggin31e6b012011-01-07 17:49:52 +11002905
Al Viro73d049a2011-03-11 12:08:24 -05002906 error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
Nick Piggin31e6b012011-01-07 17:49:52 +11002907 if (unlikely(error))
Al Viro2675a4e2012-06-22 12:41:10 +04002908 goto out;
Nick Piggin31e6b012011-01-07 17:49:52 +11002909
Al Virofe2d35f2011-03-05 22:58:25 -05002910 current->total_link_count = 0;
Al Viro73d049a2011-03-11 12:08:24 -05002911 error = link_path_walk(pathname, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11002912 if (unlikely(error))
Al Viro2675a4e2012-06-22 12:41:10 +04002913 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914
Al Viro2675a4e2012-06-22 12:41:10 +04002915 error = do_last(nd, &path, file, op, &opened, pathname);
2916 while (unlikely(error > 0)) { /* trailing symlink */
Nick Piggin7b9337a2011-01-14 08:42:43 +00002917 struct path link = path;
Al Virodef4af32009-12-26 08:37:05 -05002918 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04002919 if (!(nd->flags & LOOKUP_FOLLOW)) {
Al Viro73d049a2011-03-11 12:08:24 -05002920 path_put_conditional(&path, nd);
2921 path_put(&nd->path);
Al Viro2675a4e2012-06-22 12:41:10 +04002922 error = -ELOOP;
Al Viro40b39132011-03-09 16:22:18 -05002923 break;
2924 }
Kees Cook800179c2012-07-25 17:29:07 -07002925 error = may_follow_link(&link, nd);
2926 if (unlikely(error))
2927 break;
Al Viro73d049a2011-03-11 12:08:24 -05002928 nd->flags |= LOOKUP_PARENT;
2929 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
Al Viro574197e2011-03-14 22:20:34 -04002930 error = follow_link(&link, nd, &cookie);
Al Viroc3e380b2011-02-23 13:39:45 -05002931 if (unlikely(error))
Al Viro2675a4e2012-06-22 12:41:10 +04002932 break;
2933 error = do_last(nd, &path, file, op, &opened, pathname);
Al Viro574197e2011-03-14 22:20:34 -04002934 put_link(nd, &link, cookie);
Al Viro806b6812009-12-26 07:16:40 -05002935 }
Al Viro10fa8e62009-12-26 07:09:49 -05002936out:
Al Viro73d049a2011-03-11 12:08:24 -05002937 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
2938 path_put(&nd->root);
Al Virofe2d35f2011-03-05 22:58:25 -05002939 if (base)
2940 fput(base);
Al Viro2675a4e2012-06-22 12:41:10 +04002941 if (!(opened & FILE_OPENED)) {
2942 BUG_ON(!error);
Al Viro30d90492012-06-22 12:40:19 +04002943 put_filp(file);
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02002944 }
Al Viro2675a4e2012-06-22 12:41:10 +04002945 if (unlikely(error)) {
2946 if (error == -EOPENSTALE) {
2947 if (flags & LOOKUP_RCU)
2948 error = -ECHILD;
2949 else
2950 error = -ESTALE;
2951 }
2952 file = ERR_PTR(error);
2953 }
2954 return file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955}
2956
Al Viro13aab422011-02-23 17:54:08 -05002957struct file *do_filp_open(int dfd, const char *pathname,
2958 const struct open_flags *op, int flags)
2959{
Al Viro73d049a2011-03-11 12:08:24 -05002960 struct nameidata nd;
Al Viro13aab422011-02-23 17:54:08 -05002961 struct file *filp;
2962
Al Viro73d049a2011-03-11 12:08:24 -05002963 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
Al Viro13aab422011-02-23 17:54:08 -05002964 if (unlikely(filp == ERR_PTR(-ECHILD)))
Al Viro73d049a2011-03-11 12:08:24 -05002965 filp = path_openat(dfd, pathname, &nd, op, flags);
Al Viro13aab422011-02-23 17:54:08 -05002966 if (unlikely(filp == ERR_PTR(-ESTALE)))
Al Viro73d049a2011-03-11 12:08:24 -05002967 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
Al Viro13aab422011-02-23 17:54:08 -05002968 return filp;
2969}
2970
Al Viro73d049a2011-03-11 12:08:24 -05002971struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
2972 const char *name, const struct open_flags *op, int flags)
2973{
2974 struct nameidata nd;
2975 struct file *file;
2976
2977 nd.root.mnt = mnt;
2978 nd.root.dentry = dentry;
2979
2980 flags |= LOOKUP_ROOT;
2981
Al Virobcda7652011-03-13 16:42:14 -04002982 if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
Al Viro73d049a2011-03-11 12:08:24 -05002983 return ERR_PTR(-ELOOP);
2984
2985 file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
2986 if (unlikely(file == ERR_PTR(-ECHILD)))
2987 file = path_openat(-1, name, &nd, op, flags);
2988 if (unlikely(file == ERR_PTR(-ESTALE)))
2989 file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
2990 return file;
2991}
2992
Al Viroed75e952011-06-27 16:53:43 -04002993struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002995 struct dentry *dentry = ERR_PTR(-EEXIST);
Al Viroed75e952011-06-27 16:53:43 -04002996 struct nameidata nd;
Jan Karac30dabf2012-06-12 16:20:30 +02002997 int err2;
Al Viroed75e952011-06-27 16:53:43 -04002998 int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
2999 if (error)
3000 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003002 /*
3003 * Yucky last component or no last component at all?
3004 * (foo/., foo/.., /////)
3005 */
Al Viroed75e952011-06-27 16:53:43 -04003006 if (nd.last_type != LAST_NORM)
3007 goto out;
3008 nd.flags &= ~LOOKUP_PARENT;
3009 nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003010
Jan Karac30dabf2012-06-12 16:20:30 +02003011 /* don't fail immediately if it's r/o, at least try to report other errors */
3012 err2 = mnt_want_write(nd.path.mnt);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003013 /*
3014 * Do the final lookup.
3015 */
Al Viroed75e952011-06-27 16:53:43 -04003016 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3017 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 if (IS_ERR(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003019 goto unlock;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003020
Al Viroa8104a92012-07-20 02:25:00 +04003021 error = -EEXIST;
Al Viroe9baf6e2008-05-15 04:49:12 -04003022 if (dentry->d_inode)
Al Viroa8104a92012-07-20 02:25:00 +04003023 goto fail;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003024 /*
3025 * Special case - lookup gave negative, but... we had foo/bar/
3026 * From the vfs_mknod() POV we just have a negative dentry -
3027 * all is fine. Let's be bastards - you had / on the end, you've
3028 * been asking for (non-existent) directory. -ENOENT for you.
3029 */
Al Viroed75e952011-06-27 16:53:43 -04003030 if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
Al Viroa8104a92012-07-20 02:25:00 +04003031 error = -ENOENT;
Al Viroed75e952011-06-27 16:53:43 -04003032 goto fail;
Al Viroe9baf6e2008-05-15 04:49:12 -04003033 }
Jan Karac30dabf2012-06-12 16:20:30 +02003034 if (unlikely(err2)) {
3035 error = err2;
Al Viroa8104a92012-07-20 02:25:00 +04003036 goto fail;
Jan Karac30dabf2012-06-12 16:20:30 +02003037 }
Al Viroed75e952011-06-27 16:53:43 -04003038 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040fail:
Al Viroa8104a92012-07-20 02:25:00 +04003041 dput(dentry);
3042 dentry = ERR_PTR(error);
3043unlock:
Al Viroed75e952011-06-27 16:53:43 -04003044 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Karac30dabf2012-06-12 16:20:30 +02003045 if (!err2)
3046 mnt_drop_write(nd.path.mnt);
Al Viroed75e952011-06-27 16:53:43 -04003047out:
3048 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 return dentry;
3050}
Al Virodae6ad82011-06-26 11:50:15 -04003051EXPORT_SYMBOL(kern_path_create);
3052
Al Viro921a1652012-07-20 01:15:31 +04003053void done_path_create(struct path *path, struct dentry *dentry)
3054{
3055 dput(dentry);
3056 mutex_unlock(&path->dentry->d_inode->i_mutex);
Al Viroa8104a92012-07-20 02:25:00 +04003057 mnt_drop_write(path->mnt);
Al Viro921a1652012-07-20 01:15:31 +04003058 path_put(path);
3059}
3060EXPORT_SYMBOL(done_path_create);
3061
Al Virodae6ad82011-06-26 11:50:15 -04003062struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir)
3063{
Jeff Layton91a27b22012-10-10 15:25:28 -04003064 struct filename *tmp = getname(pathname);
Al Virodae6ad82011-06-26 11:50:15 -04003065 struct dentry *res;
3066 if (IS_ERR(tmp))
3067 return ERR_CAST(tmp);
Jeff Layton91a27b22012-10-10 15:25:28 -04003068 res = kern_path_create(dfd, tmp->name, path, is_dir);
Al Virodae6ad82011-06-26 11:50:15 -04003069 putname(tmp);
3070 return res;
3071}
3072EXPORT_SYMBOL(user_path_create);
3073
Al Viro1a67aaf2011-07-26 01:52:52 -04003074int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003076 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077
3078 if (error)
3079 return error;
3080
Eric W. Biederman975d6b32011-11-13 12:16:43 -08003081 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 return -EPERM;
3083
Al Viroacfa4382008-12-04 10:06:33 -05003084 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 return -EPERM;
3086
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07003087 error = devcgroup_inode_mknod(mode, dev);
3088 if (error)
3089 return error;
3090
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 error = security_inode_mknod(dir, dentry, mode, dev);
3092 if (error)
3093 return error;
3094
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003096 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003097 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 return error;
3099}
3100
Al Virof69aac02011-07-26 04:31:40 -04003101static int may_mknod(umode_t mode)
Dave Hansen463c3192008-02-15 14:37:57 -08003102{
3103 switch (mode & S_IFMT) {
3104 case S_IFREG:
3105 case S_IFCHR:
3106 case S_IFBLK:
3107 case S_IFIFO:
3108 case S_IFSOCK:
3109 case 0: /* zero mode translates to S_IFREG */
3110 return 0;
3111 case S_IFDIR:
3112 return -EPERM;
3113 default:
3114 return -EINVAL;
3115 }
3116}
3117
Al Viro8208a222011-07-25 17:32:17 -04003118SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003119 unsigned, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120{
Al Viro2ad94ae2008-07-21 09:32:51 -04003121 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003122 struct path path;
3123 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124
Al Viro8e4bfca2012-07-20 01:17:26 +04003125 error = may_mknod(mode);
3126 if (error)
3127 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128
Al Virodae6ad82011-06-26 11:50:15 -04003129 dentry = user_path_create(dfd, filename, &path, 0);
3130 if (IS_ERR(dentry))
3131 return PTR_ERR(dentry);
Al Viro2ad94ae2008-07-21 09:32:51 -04003132
Al Virodae6ad82011-06-26 11:50:15 -04003133 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003134 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003135 error = security_path_mknod(&path, dentry, mode, dev);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003136 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04003137 goto out;
Dave Hansen463c3192008-02-15 14:37:57 -08003138 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 case 0: case S_IFREG:
Al Viro312b63f2012-06-10 18:09:36 -04003140 error = vfs_create(path.dentry->d_inode,dentry,mode,true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 break;
3142 case S_IFCHR: case S_IFBLK:
Al Virodae6ad82011-06-26 11:50:15 -04003143 error = vfs_mknod(path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 new_decode_dev(dev));
3145 break;
3146 case S_IFIFO: case S_IFSOCK:
Al Virodae6ad82011-06-26 11:50:15 -04003147 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 }
Al Viroa8104a92012-07-20 02:25:00 +04003150out:
Al Viro921a1652012-07-20 01:15:31 +04003151 done_path_create(&path, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 return error;
3153}
3154
Al Viro8208a222011-07-25 17:32:17 -04003155SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003156{
3157 return sys_mknodat(AT_FDCWD, filename, mode, dev);
3158}
3159
Al Viro18bb1db2011-07-26 01:41:39 -04003160int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003162 int error = may_create(dir, dentry);
Al Viro8de52772012-02-06 12:45:27 -05003163 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164
3165 if (error)
3166 return error;
3167
Al Viroacfa4382008-12-04 10:06:33 -05003168 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 return -EPERM;
3170
3171 mode &= (S_IRWXUGO|S_ISVTX);
3172 error = security_inode_mkdir(dir, dentry, mode);
3173 if (error)
3174 return error;
3175
Al Viro8de52772012-02-06 12:45:27 -05003176 if (max_links && dir->i_nlink >= max_links)
3177 return -EMLINK;
3178
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003180 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003181 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 return error;
3183}
3184
Al Viroa218d0f2011-11-21 14:59:34 -05003185SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186{
Dave Hansen6902d922006-09-30 23:29:01 -07003187 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003188 struct path path;
3189 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190
Al Virodae6ad82011-06-26 11:50:15 -04003191 dentry = user_path_create(dfd, pathname, &path, 1);
Dave Hansen6902d922006-09-30 23:29:01 -07003192 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003193 return PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003194
Al Virodae6ad82011-06-26 11:50:15 -04003195 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003196 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003197 error = security_path_mkdir(&path, dentry, mode);
Al Viroa8104a92012-07-20 02:25:00 +04003198 if (!error)
3199 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
Al Viro921a1652012-07-20 01:15:31 +04003200 done_path_create(&path, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 return error;
3202}
3203
Al Viroa218d0f2011-11-21 14:59:34 -05003204SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003205{
3206 return sys_mkdirat(AT_FDCWD, pathname, mode);
3207}
3208
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209/*
Sage Weila71905f2011-05-24 13:06:08 -07003210 * The dentry_unhash() helper will try to drop the dentry early: we
J. Bruce Fieldsc0d02592012-02-15 11:48:40 -05003211 * should have a usage count of 1 if we're the only user of this
Sage Weila71905f2011-05-24 13:06:08 -07003212 * dentry, and if that is true (possibly after pruning the dcache),
3213 * then we drop the dentry now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 *
3215 * A low-level filesystem can, if it choses, legally
3216 * do a
3217 *
3218 * if (!d_unhashed(dentry))
3219 * return -EBUSY;
3220 *
3221 * if it cannot handle the case of removing a directory
3222 * that is still in use by something else..
3223 */
3224void dentry_unhash(struct dentry *dentry)
3225{
Vasily Averindc168422006-12-06 20:37:07 -08003226 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 spin_lock(&dentry->d_lock);
Sage Weil64252c72011-05-24 13:06:05 -07003228 if (dentry->d_count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 __d_drop(dentry);
3230 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231}
3232
3233int vfs_rmdir(struct inode *dir, struct dentry *dentry)
3234{
3235 int error = may_delete(dir, dentry, 1);
3236
3237 if (error)
3238 return error;
3239
Al Viroacfa4382008-12-04 10:06:33 -05003240 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 return -EPERM;
3242
Al Viro1d2ef592011-09-14 18:55:41 +01003243 dget(dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003244 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245
Sage Weil912dbc12011-05-24 13:06:11 -07003246 error = -EBUSY;
3247 if (d_mountpoint(dentry))
3248 goto out;
3249
3250 error = security_inode_rmdir(dir, dentry);
3251 if (error)
3252 goto out;
3253
Sage Weil3cebde22011-05-29 21:20:59 -07003254 shrink_dcache_parent(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003255 error = dir->i_op->rmdir(dir, dentry);
3256 if (error)
3257 goto out;
3258
3259 dentry->d_inode->i_flags |= S_DEAD;
3260 dont_mount(dentry);
3261
3262out:
3263 mutex_unlock(&dentry->d_inode->i_mutex);
Al Viro1d2ef592011-09-14 18:55:41 +01003264 dput(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003265 if (!error)
3266 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 return error;
3268}
3269
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003270static long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271{
3272 int error = 0;
Jeff Layton91a27b22012-10-10 15:25:28 -04003273 struct filename *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 struct dentry *dentry;
3275 struct nameidata nd;
3276
Jeff Layton91a27b22012-10-10 15:25:28 -04003277 name = user_path_parent(dfd, pathname, &nd);
3278 if (IS_ERR(name))
3279 return PTR_ERR(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280
3281 switch(nd.last_type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003282 case LAST_DOTDOT:
3283 error = -ENOTEMPTY;
3284 goto exit1;
3285 case LAST_DOT:
3286 error = -EINVAL;
3287 goto exit1;
3288 case LAST_ROOT:
3289 error = -EBUSY;
3290 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003292
3293 nd.flags &= ~LOOKUP_PARENT;
Jan Karac30dabf2012-06-12 16:20:30 +02003294 error = mnt_want_write(nd.path.mnt);
3295 if (error)
3296 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003297
Jan Blunck4ac91372008-02-14 19:34:32 -08003298 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08003299 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003301 if (IS_ERR(dentry))
3302 goto exit2;
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04003303 if (!dentry->d_inode) {
3304 error = -ENOENT;
3305 goto exit3;
3306 }
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003307 error = security_path_rmdir(&nd.path, dentry);
3308 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02003309 goto exit3;
Jan Blunck4ac91372008-02-14 19:34:32 -08003310 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
Dave Hansen06227532008-02-15 14:37:34 -08003311exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07003312 dput(dentry);
3313exit2:
Jan Blunck4ac91372008-02-14 19:34:32 -08003314 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Karac30dabf2012-06-12 16:20:30 +02003315 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003317 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 putname(name);
3319 return error;
3320}
3321
Heiko Carstens3cdad422009-01-14 14:14:22 +01003322SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003323{
3324 return do_rmdir(AT_FDCWD, pathname);
3325}
3326
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327int vfs_unlink(struct inode *dir, struct dentry *dentry)
3328{
3329 int error = may_delete(dir, dentry, 0);
3330
3331 if (error)
3332 return error;
3333
Al Viroacfa4382008-12-04 10:06:33 -05003334 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 return -EPERM;
3336
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003337 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 if (d_mountpoint(dentry))
3339 error = -EBUSY;
3340 else {
3341 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05003342 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 error = dir->i_op->unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05003344 if (!error)
Al Virod83c49f2010-04-30 17:17:09 -04003345 dont_mount(dentry);
Al Virobec10522010-03-03 14:12:08 -05003346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003348 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349
3350 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
3351 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
Jan Karaece95912008-02-06 01:37:13 -08003352 fsnotify_link_count(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 }
Robert Love0eeca282005-07-12 17:06:03 -04003355
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 return error;
3357}
3358
3359/*
3360 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003361 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 * writeout happening, and we don't want to prevent access to the directory
3363 * while waiting on the I/O.
3364 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003365static long do_unlinkat(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366{
Al Viro2ad94ae2008-07-21 09:32:51 -04003367 int error;
Jeff Layton91a27b22012-10-10 15:25:28 -04003368 struct filename *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 struct dentry *dentry;
3370 struct nameidata nd;
3371 struct inode *inode = NULL;
3372
Jeff Layton91a27b22012-10-10 15:25:28 -04003373 name = user_path_parent(dfd, pathname, &nd);
3374 if (IS_ERR(name))
3375 return PTR_ERR(name);
Al Viro2ad94ae2008-07-21 09:32:51 -04003376
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 error = -EISDIR;
3378 if (nd.last_type != LAST_NORM)
3379 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003380
3381 nd.flags &= ~LOOKUP_PARENT;
Jan Karac30dabf2012-06-12 16:20:30 +02003382 error = mnt_want_write(nd.path.mnt);
3383 if (error)
3384 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003385
Jan Blunck4ac91372008-02-14 19:34:32 -08003386 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08003387 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 error = PTR_ERR(dentry);
3389 if (!IS_ERR(dentry)) {
3390 /* Why not before? Because we want correct error value */
Török Edwin50338b82011-06-16 00:06:14 +03003391 if (nd.last.name[nd.last.len])
3392 goto slashes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 inode = dentry->d_inode;
Török Edwin50338b82011-06-16 00:06:14 +03003394 if (!inode)
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04003395 goto slashes;
3396 ihold(inode);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003397 error = security_path_unlink(&nd.path, dentry);
3398 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02003399 goto exit2;
Jan Blunck4ac91372008-02-14 19:34:32 -08003400 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
Jan Karac30dabf2012-06-12 16:20:30 +02003401exit2:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 dput(dentry);
3403 }
Jan Blunck4ac91372008-02-14 19:34:32 -08003404 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 if (inode)
3406 iput(inode); /* truncate the inode here */
Jan Karac30dabf2012-06-12 16:20:30 +02003407 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003409 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 putname(name);
3411 return error;
3412
3413slashes:
3414 error = !dentry->d_inode ? -ENOENT :
3415 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
3416 goto exit2;
3417}
3418
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003419SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003420{
3421 if ((flag & ~AT_REMOVEDIR) != 0)
3422 return -EINVAL;
3423
3424 if (flag & AT_REMOVEDIR)
3425 return do_rmdir(dfd, pathname);
3426
3427 return do_unlinkat(dfd, pathname);
3428}
3429
Heiko Carstens3480b252009-01-14 14:14:16 +01003430SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003431{
3432 return do_unlinkat(AT_FDCWD, pathname);
3433}
3434
Miklos Szeredidb2e7472008-06-24 16:50:16 +02003435int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003437 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438
3439 if (error)
3440 return error;
3441
Al Viroacfa4382008-12-04 10:06:33 -05003442 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 return -EPERM;
3444
3445 error = security_inode_symlink(dir, dentry, oldname);
3446 if (error)
3447 return error;
3448
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003450 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003451 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 return error;
3453}
3454
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003455SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
3456 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457{
Al Viro2ad94ae2008-07-21 09:32:51 -04003458 int error;
Jeff Layton91a27b22012-10-10 15:25:28 -04003459 struct filename *from;
Dave Hansen6902d922006-09-30 23:29:01 -07003460 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003461 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462
3463 from = getname(oldname);
Al Viro2ad94ae2008-07-21 09:32:51 -04003464 if (IS_ERR(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 return PTR_ERR(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04003466
Al Virodae6ad82011-06-26 11:50:15 -04003467 dentry = user_path_create(newdfd, newname, &path, 0);
Dave Hansen6902d922006-09-30 23:29:01 -07003468 error = PTR_ERR(dentry);
3469 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003470 goto out_putname;
Dave Hansen6902d922006-09-30 23:29:01 -07003471
Jeff Layton91a27b22012-10-10 15:25:28 -04003472 error = security_path_symlink(&path, dentry, from->name);
Al Viroa8104a92012-07-20 02:25:00 +04003473 if (!error)
Jeff Layton91a27b22012-10-10 15:25:28 -04003474 error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
Al Viro921a1652012-07-20 01:15:31 +04003475 done_path_create(&path, dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003476out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 putname(from);
3478 return error;
3479}
3480
Heiko Carstens3480b252009-01-14 14:14:16 +01003481SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003482{
3483 return sys_symlinkat(oldname, AT_FDCWD, newname);
3484}
3485
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
3487{
3488 struct inode *inode = old_dentry->d_inode;
Al Viro8de52772012-02-06 12:45:27 -05003489 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 int error;
3491
3492 if (!inode)
3493 return -ENOENT;
3494
Miklos Szeredia95164d2008-07-30 15:08:48 +02003495 error = may_create(dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 if (error)
3497 return error;
3498
3499 if (dir->i_sb != inode->i_sb)
3500 return -EXDEV;
3501
3502 /*
3503 * A link to an append-only or immutable file cannot be created.
3504 */
3505 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3506 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05003507 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003509 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 return -EPERM;
3511
3512 error = security_inode_link(old_dentry, dir, new_dentry);
3513 if (error)
3514 return error;
3515
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003516 mutex_lock(&inode->i_mutex);
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05303517 /* Make sure we don't allow creating hardlink to an unlinked file */
3518 if (inode->i_nlink == 0)
3519 error = -ENOENT;
Al Viro8de52772012-02-06 12:45:27 -05003520 else if (max_links && inode->i_nlink >= max_links)
3521 error = -EMLINK;
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05303522 else
3523 error = dir->i_op->link(old_dentry, dir, new_dentry);
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003524 mutex_unlock(&inode->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07003525 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003526 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 return error;
3528}
3529
3530/*
3531 * Hardlinks are often used in delicate situations. We avoid
3532 * security-related surprises by not following symlinks on the
3533 * newname. --KAB
3534 *
3535 * We don't follow them on the oldname either to be compatible
3536 * with linux 2.0, and to avoid hard-linking to directories
3537 * and other special files. --ADM
3538 */
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003539SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
3540 int, newdfd, const char __user *, newname, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541{
3542 struct dentry *new_dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003543 struct path old_path, new_path;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303544 int how = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303547 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003548 return -EINVAL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303549 /*
3550 * To use null names we require CAP_DAC_READ_SEARCH
3551 * This ensures that not everyone will be able to create
3552 * handlink using the passed filedescriptor.
3553 */
3554 if (flags & AT_EMPTY_PATH) {
3555 if (!capable(CAP_DAC_READ_SEARCH))
3556 return -ENOENT;
3557 how = LOOKUP_EMPTY;
3558 }
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003559
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303560 if (flags & AT_SYMLINK_FOLLOW)
3561 how |= LOOKUP_FOLLOW;
3562
3563 error = user_path_at(olddfd, oldname, how, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04003565 return error;
3566
Al Virodae6ad82011-06-26 11:50:15 -04003567 new_dentry = user_path_create(newdfd, newname, &new_path, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003569 if (IS_ERR(new_dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003570 goto out;
3571
3572 error = -EXDEV;
3573 if (old_path.mnt != new_path.mnt)
3574 goto out_dput;
Kees Cook800179c2012-07-25 17:29:07 -07003575 error = may_linkat(&old_path);
3576 if (unlikely(error))
3577 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04003578 error = security_path_link(old_path.dentry, &new_path, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003579 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04003580 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04003581 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
Dave Hansen75c3f292008-02-15 14:37:45 -08003582out_dput:
Al Viro921a1652012-07-20 01:15:31 +04003583 done_path_create(&new_path, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584out:
Al Viro2d8f3032008-07-22 09:59:21 -04003585 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586
3587 return error;
3588}
3589
Heiko Carstens3480b252009-01-14 14:14:16 +01003590SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003591{
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003592 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003593}
3594
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595/*
3596 * The worst of all namespace operations - renaming directory. "Perverted"
3597 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
3598 * Problems:
3599 * a) we can get into loop creation. Check is done in is_subdir().
3600 * b) race potential - two innocent renames can create a loop together.
3601 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08003602 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 * story.
3604 * c) we have to lock _three_ objects - parents and victim (if it exists).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003605 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 * whether the target exists). Solution: try to be smart with locking
3607 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08003608 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 * move will be locked. Thus we can rank directories by the tree
3610 * (ancestors first) and rank all non-directories after them.
3611 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08003612 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 * HOWEVER, it relies on the assumption that any object with ->lookup()
3614 * has no more than 1 dentry. If "hybrid" objects will ever appear,
3615 * we'd better make sure that there's no link(2) for them.
Sage Weile4eaac02011-05-24 13:06:07 -07003616 * d) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003617 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05003619 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 * locking].
3621 */
Adrian Bunk75c96f82005-05-05 16:16:09 -07003622static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
3623 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624{
3625 int error = 0;
Sage Weil9055cba2011-05-24 13:06:12 -07003626 struct inode *target = new_dentry->d_inode;
Al Viro8de52772012-02-06 12:45:27 -05003627 unsigned max_links = new_dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628
3629 /*
3630 * If we are going to change the parent - check write permissions,
3631 * we'll need to flip '..'.
3632 */
3633 if (new_dir != old_dir) {
Al Virof419a2e2008-07-22 00:07:17 -04003634 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 if (error)
3636 return error;
3637 }
3638
3639 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3640 if (error)
3641 return error;
3642
Al Viro1d2ef592011-09-14 18:55:41 +01003643 dget(new_dentry);
Al Virod83c49f2010-04-30 17:17:09 -04003644 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003645 mutex_lock(&target->i_mutex);
Sage Weil9055cba2011-05-24 13:06:12 -07003646
3647 error = -EBUSY;
3648 if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
3649 goto out;
3650
Al Viro8de52772012-02-06 12:45:27 -05003651 error = -EMLINK;
3652 if (max_links && !target && new_dir != old_dir &&
3653 new_dir->i_nlink >= max_links)
3654 goto out;
3655
Sage Weil3cebde22011-05-29 21:20:59 -07003656 if (target)
3657 shrink_dcache_parent(new_dentry);
Sage Weil9055cba2011-05-24 13:06:12 -07003658 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3659 if (error)
3660 goto out;
3661
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 if (target) {
Sage Weil9055cba2011-05-24 13:06:12 -07003663 target->i_flags |= S_DEAD;
3664 dont_mount(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 }
Sage Weil9055cba2011-05-24 13:06:12 -07003666out:
3667 if (target)
3668 mutex_unlock(&target->i_mutex);
Al Viro1d2ef592011-09-14 18:55:41 +01003669 dput(new_dentry);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07003670 if (!error)
Mark Fasheh349457c2006-09-08 14:22:21 -07003671 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3672 d_move(old_dentry,new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 return error;
3674}
3675
Adrian Bunk75c96f82005-05-05 16:16:09 -07003676static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
3677 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678{
Sage Weil51892bb2011-05-24 13:06:13 -07003679 struct inode *target = new_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 int error;
3681
3682 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3683 if (error)
3684 return error;
3685
3686 dget(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003688 mutex_lock(&target->i_mutex);
Sage Weil51892bb2011-05-24 13:06:13 -07003689
3690 error = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
Sage Weil51892bb2011-05-24 13:06:13 -07003692 goto out;
3693
3694 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3695 if (error)
3696 goto out;
3697
3698 if (target)
3699 dont_mount(new_dentry);
3700 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3701 d_move(old_dentry, new_dentry);
3702out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003704 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 dput(new_dentry);
3706 return error;
3707}
3708
3709int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
3710 struct inode *new_dir, struct dentry *new_dentry)
3711{
3712 int error;
3713 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
Eric Paris59b0df22010-02-08 12:53:52 -05003714 const unsigned char *old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715
3716 if (old_dentry->d_inode == new_dentry->d_inode)
3717 return 0;
3718
3719 error = may_delete(old_dir, old_dentry, is_dir);
3720 if (error)
3721 return error;
3722
3723 if (!new_dentry->d_inode)
Miklos Szeredia95164d2008-07-30 15:08:48 +02003724 error = may_create(new_dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 else
3726 error = may_delete(new_dir, new_dentry, is_dir);
3727 if (error)
3728 return error;
3729
Al Viroacfa4382008-12-04 10:06:33 -05003730 if (!old_dir->i_op->rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 return -EPERM;
3732
Robert Love0eeca282005-07-12 17:06:03 -04003733 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
3734
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 if (is_dir)
3736 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
3737 else
3738 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
Al Viro123df292009-12-25 04:57:57 -05003739 if (!error)
3740 fsnotify_move(old_dir, new_dir, old_name, is_dir,
Al Viro5a190ae2007-06-07 12:19:32 -04003741 new_dentry->d_inode, old_dentry);
Robert Love0eeca282005-07-12 17:06:03 -04003742 fsnotify_oldname_free(old_name);
3743
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 return error;
3745}
3746
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003747SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
3748 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749{
Al Viro2ad94ae2008-07-21 09:32:51 -04003750 struct dentry *old_dir, *new_dir;
3751 struct dentry *old_dentry, *new_dentry;
3752 struct dentry *trap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 struct nameidata oldnd, newnd;
Jeff Layton91a27b22012-10-10 15:25:28 -04003754 struct filename *from;
3755 struct filename *to;
Al Viro2ad94ae2008-07-21 09:32:51 -04003756 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757
Jeff Layton91a27b22012-10-10 15:25:28 -04003758 from = user_path_parent(olddfd, oldname, &oldnd);
3759 if (IS_ERR(from)) {
3760 error = PTR_ERR(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 goto exit;
Jeff Layton91a27b22012-10-10 15:25:28 -04003762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763
Jeff Layton91a27b22012-10-10 15:25:28 -04003764 to = user_path_parent(newdfd, newname, &newnd);
3765 if (IS_ERR(to)) {
3766 error = PTR_ERR(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 goto exit1;
Jeff Layton91a27b22012-10-10 15:25:28 -04003768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769
3770 error = -EXDEV;
Jan Blunck4ac91372008-02-14 19:34:32 -08003771 if (oldnd.path.mnt != newnd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 goto exit2;
3773
Jan Blunck4ac91372008-02-14 19:34:32 -08003774 old_dir = oldnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 error = -EBUSY;
3776 if (oldnd.last_type != LAST_NORM)
3777 goto exit2;
3778
Jan Blunck4ac91372008-02-14 19:34:32 -08003779 new_dir = newnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 if (newnd.last_type != LAST_NORM)
3781 goto exit2;
3782
Jan Karac30dabf2012-06-12 16:20:30 +02003783 error = mnt_want_write(oldnd.path.mnt);
3784 if (error)
3785 goto exit2;
3786
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003787 oldnd.flags &= ~LOOKUP_PARENT;
3788 newnd.flags &= ~LOOKUP_PARENT;
OGAWA Hirofumi4e9ed2f2008-10-16 07:50:29 +09003789 newnd.flags |= LOOKUP_RENAME_TARGET;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003790
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 trap = lock_rename(new_dir, old_dir);
3792
Christoph Hellwig49705b72005-11-08 21:35:06 -08003793 old_dentry = lookup_hash(&oldnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 error = PTR_ERR(old_dentry);
3795 if (IS_ERR(old_dentry))
3796 goto exit3;
3797 /* source must exist */
3798 error = -ENOENT;
3799 if (!old_dentry->d_inode)
3800 goto exit4;
3801 /* unless the source is a directory trailing slashes give -ENOTDIR */
3802 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
3803 error = -ENOTDIR;
3804 if (oldnd.last.name[oldnd.last.len])
3805 goto exit4;
3806 if (newnd.last.name[newnd.last.len])
3807 goto exit4;
3808 }
3809 /* source should not be ancestor of target */
3810 error = -EINVAL;
3811 if (old_dentry == trap)
3812 goto exit4;
Christoph Hellwig49705b72005-11-08 21:35:06 -08003813 new_dentry = lookup_hash(&newnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 error = PTR_ERR(new_dentry);
3815 if (IS_ERR(new_dentry))
3816 goto exit4;
3817 /* target should not be an ancestor of source */
3818 error = -ENOTEMPTY;
3819 if (new_dentry == trap)
3820 goto exit5;
3821
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003822 error = security_path_rename(&oldnd.path, old_dentry,
3823 &newnd.path, new_dentry);
3824 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02003825 goto exit5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 error = vfs_rename(old_dir->d_inode, old_dentry,
3827 new_dir->d_inode, new_dentry);
3828exit5:
3829 dput(new_dentry);
3830exit4:
3831 dput(old_dentry);
3832exit3:
3833 unlock_rename(new_dir, old_dir);
Jan Karac30dabf2012-06-12 16:20:30 +02003834 mnt_drop_write(oldnd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835exit2:
Jan Blunck1d957f92008-02-14 19:34:35 -08003836 path_put(&newnd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04003837 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003839 path_put(&oldnd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 putname(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04003841exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 return error;
3843}
3844
Heiko Carstensa26eab22009-01-14 14:14:17 +01003845SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003846{
3847 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
3848}
3849
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
3851{
3852 int len;
3853
3854 len = PTR_ERR(link);
3855 if (IS_ERR(link))
3856 goto out;
3857
3858 len = strlen(link);
3859 if (len > (unsigned) buflen)
3860 len = buflen;
3861 if (copy_to_user(buffer, link, len))
3862 len = -EFAULT;
3863out:
3864 return len;
3865}
3866
3867/*
3868 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
3869 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
3870 * using) it for any given inode is up to filesystem.
3871 */
3872int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3873{
3874 struct nameidata nd;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003875 void *cookie;
Marcin Slusarz694a1762008-06-09 16:40:37 -07003876 int res;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003877
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 nd.depth = 0;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003879 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
Marcin Slusarz694a1762008-06-09 16:40:37 -07003880 if (IS_ERR(cookie))
3881 return PTR_ERR(cookie);
3882
3883 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
3884 if (dentry->d_inode->i_op->put_link)
3885 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3886 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887}
3888
3889int vfs_follow_link(struct nameidata *nd, const char *link)
3890{
3891 return __vfs_follow_link(nd, link);
3892}
3893
3894/* get the link contents into pagecache */
3895static char *page_getlink(struct dentry * dentry, struct page **ppage)
3896{
Duane Griffinebd09ab2008-12-19 20:47:12 +00003897 char *kaddr;
3898 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 struct address_space *mapping = dentry->d_inode->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -07003900 page = read_mapping_page(mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 if (IS_ERR(page))
Nick Piggin6fe69002007-05-06 14:49:04 -07003902 return (char*)page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 *ppage = page;
Duane Griffinebd09ab2008-12-19 20:47:12 +00003904 kaddr = kmap(page);
3905 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3906 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907}
3908
3909int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3910{
3911 struct page *page = NULL;
3912 char *s = page_getlink(dentry, &page);
3913 int res = vfs_readlink(dentry,buffer,buflen,s);
3914 if (page) {
3915 kunmap(page);
3916 page_cache_release(page);
3917 }
3918 return res;
3919}
3920
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003921void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003923 struct page *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 nd_set_link(nd, page_getlink(dentry, &page));
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003925 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926}
3927
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003928void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003930 struct page *page = cookie;
3931
3932 if (page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 kunmap(page);
3934 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 }
3936}
3937
Nick Piggin54566b22009-01-04 12:00:53 -08003938/*
3939 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
3940 */
3941int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942{
3943 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003944 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07003945 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08003946 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 char *kaddr;
Nick Piggin54566b22009-01-04 12:00:53 -08003948 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
3949 if (nofs)
3950 flags |= AOP_FLAG_NOFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951
NeilBrown7e53cac2006-03-25 03:07:57 -08003952retry:
Nick Pigginafddba42007-10-16 01:25:01 -07003953 err = pagecache_write_begin(NULL, mapping, 0, len-1,
Nick Piggin54566b22009-01-04 12:00:53 -08003954 flags, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07003956 goto fail;
3957
Cong Wange8e3c3d2011-11-25 23:14:27 +08003958 kaddr = kmap_atomic(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 memcpy(kaddr, symname, len-1);
Cong Wange8e3c3d2011-11-25 23:14:27 +08003960 kunmap_atomic(kaddr);
Nick Pigginafddba42007-10-16 01:25:01 -07003961
3962 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3963 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 if (err < 0)
3965 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07003966 if (err < len-1)
3967 goto retry;
3968
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 mark_inode_dirty(inode);
3970 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971fail:
3972 return err;
3973}
3974
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003975int page_symlink(struct inode *inode, const char *symname, int len)
3976{
3977 return __page_symlink(inode, symname, len,
Nick Piggin54566b22009-01-04 12:00:53 -08003978 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003979}
3980
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003981const struct inode_operations page_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 .readlink = generic_readlink,
3983 .follow_link = page_follow_link_light,
3984 .put_link = page_put_link,
3985};
3986
Al Viro2d8f3032008-07-22 09:59:21 -04003987EXPORT_SYMBOL(user_path_at);
David Howellscc53ce52011-01-14 18:45:26 +00003988EXPORT_SYMBOL(follow_down_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989EXPORT_SYMBOL(follow_down);
3990EXPORT_SYMBOL(follow_up);
Al Virof6d2ac52012-08-26 12:55:54 -04003991EXPORT_SYMBOL(get_write_access); /* nfsd */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993EXPORT_SYMBOL(lookup_one_len);
3994EXPORT_SYMBOL(page_follow_link_light);
3995EXPORT_SYMBOL(page_put_link);
3996EXPORT_SYMBOL(page_readlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003997EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998EXPORT_SYMBOL(page_symlink);
3999EXPORT_SYMBOL(page_symlink_inode_operations);
Al Virod1811462008-08-02 00:49:18 -04004000EXPORT_SYMBOL(kern_path);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07004001EXPORT_SYMBOL(vfs_path_lookup);
Al Virof419a2e2008-07-22 00:07:17 -04004002EXPORT_SYMBOL(inode_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003EXPORT_SYMBOL(unlock_rename);
4004EXPORT_SYMBOL(vfs_create);
4005EXPORT_SYMBOL(vfs_follow_link);
4006EXPORT_SYMBOL(vfs_link);
4007EXPORT_SYMBOL(vfs_mkdir);
4008EXPORT_SYMBOL(vfs_mknod);
4009EXPORT_SYMBOL(generic_permission);
4010EXPORT_SYMBOL(vfs_readlink);
4011EXPORT_SYMBOL(vfs_rename);
4012EXPORT_SYMBOL(vfs_rmdir);
4013EXPORT_SYMBOL(vfs_symlink);
4014EXPORT_SYMBOL(vfs_unlink);
4015EXPORT_SYMBOL(dentry_unhash);
4016EXPORT_SYMBOL(generic_readlink);