blob: 385f7817bfccbd12fbc352c39827586d4cf953d7 [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 Layton7950e382012-10-10 16:43:13 -0400122 if (name->separate) {
123 __putname(name->name);
124 kfree(name);
125 } else {
126 __putname(name);
127 }
Jeff Layton91a27b22012-10-10 15:25:28 -0400128}
129
Jeff Layton7950e382012-10-10 16:43:13 -0400130#define EMBEDDED_NAME_MAX (PATH_MAX - sizeof(struct filename))
131
Jeff Layton91a27b22012-10-10 15:25:28 -0400132static struct filename *
133getname_flags(const char __user *filename, int flags, int *empty)
134{
135 struct filename *result, *err;
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700136 int len;
Jeff Layton7950e382012-10-10 16:43:13 -0400137 long max;
138 char *kname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Jeff Layton7ac86262012-10-10 15:25:28 -0400140 result = audit_reusename(filename);
141 if (result)
142 return result;
143
Jeff Layton7950e382012-10-10 16:43:13 -0400144 result = __getname();
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700145 if (unlikely(!result))
Eric Paris4043cde2012-01-03 14:23:08 -0500146 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Jeff Layton7950e382012-10-10 16:43:13 -0400148 /*
149 * First, try to embed the struct filename inside the names_cache
150 * allocation
151 */
152 kname = (char *)result + sizeof(*result);
Jeff Layton91a27b22012-10-10 15:25:28 -0400153 result->name = kname;
Jeff Layton7950e382012-10-10 16:43:13 -0400154 result->separate = false;
155 max = EMBEDDED_NAME_MAX;
156
157recopy:
158 len = strncpy_from_user(kname, filename, max);
Jeff Layton91a27b22012-10-10 15:25:28 -0400159 if (unlikely(len < 0)) {
160 err = ERR_PTR(len);
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700161 goto error;
Jeff Layton91a27b22012-10-10 15:25:28 -0400162 }
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700163
Jeff Layton7950e382012-10-10 16:43:13 -0400164 /*
165 * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
166 * separate struct filename so we can dedicate the entire
167 * names_cache allocation for the pathname, and re-do the copy from
168 * userland.
169 */
170 if (len == EMBEDDED_NAME_MAX && max == EMBEDDED_NAME_MAX) {
171 kname = (char *)result;
172
173 result = kzalloc(sizeof(*result), GFP_KERNEL);
174 if (!result) {
175 err = ERR_PTR(-ENOMEM);
176 result = (struct filename *)kname;
177 goto error;
178 }
179 result->name = kname;
180 result->separate = true;
181 max = PATH_MAX;
182 goto recopy;
183 }
184
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700185 /* The empty path is special. */
186 if (unlikely(!len)) {
187 if (empty)
Eric Paris4043cde2012-01-03 14:23:08 -0500188 *empty = 1;
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700189 err = ERR_PTR(-ENOENT);
190 if (!(flags & LOOKUP_EMPTY))
191 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 }
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700193
194 err = ERR_PTR(-ENAMETOOLONG);
Jeff Layton7950e382012-10-10 16:43:13 -0400195 if (unlikely(len >= PATH_MAX))
196 goto error;
197
198 result->uptr = filename;
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800199 result->aname = NULL;
Jeff Layton7950e382012-10-10 16:43:13 -0400200 audit_getname(result);
201 return result;
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700202
203error:
Jeff Layton7950e382012-10-10 16:43:13 -0400204 final_putname(result);
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700205 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
Jeff Layton91a27b22012-10-10 15:25:28 -0400208struct filename *
209getname(const char __user * filename)
Al Virof52e0c12011-03-14 18:56:51 -0400210{
Linus Torvaldsf7493e52012-03-22 16:10:40 -0700211 return getname_flags(filename, 0, NULL);
Al Virof52e0c12011-03-14 18:56:51 -0400212}
213
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800214/*
215 * The "getname_kernel()" interface doesn't do pathnames longer
216 * than EMBEDDED_NAME_MAX. Deal with it - you're a kernel user.
217 */
218struct filename *
219getname_kernel(const char * filename)
220{
221 struct filename *result;
222 char *kname;
223 int len;
224
225 len = strlen(filename);
226 if (len >= EMBEDDED_NAME_MAX)
227 return ERR_PTR(-ENAMETOOLONG);
228
229 result = __getname();
230 if (unlikely(!result))
231 return ERR_PTR(-ENOMEM);
232
233 kname = (char *)result + sizeof(*result);
234 result->name = kname;
235 result->uptr = NULL;
236 result->aname = NULL;
237 result->separate = false;
238
239 strlcpy(kname, filename, EMBEDDED_NAME_MAX);
240 return result;
241}
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243#ifdef CONFIG_AUDITSYSCALL
Jeff Layton91a27b22012-10-10 15:25:28 -0400244void putname(struct filename *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
Al Viro5ac3a9c2006-07-16 06:38:45 -0400246 if (unlikely(!audit_dummy_context()))
Jeff Layton91a27b22012-10-10 15:25:28 -0400247 return audit_putname(name);
248 final_putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250#endif
251
Linus Torvaldse77819e2011-07-22 19:30:19 -0700252static int check_acl(struct inode *inode, int mask)
253{
Linus Torvalds84635d62011-07-25 22:47:03 -0700254#ifdef CONFIG_FS_POSIX_ACL
Linus Torvaldse77819e2011-07-22 19:30:19 -0700255 struct posix_acl *acl;
256
Linus Torvaldse77819e2011-07-22 19:30:19 -0700257 if (mask & MAY_NOT_BLOCK) {
Al Viro35678662011-08-02 21:32:13 -0400258 acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
259 if (!acl)
Linus Torvaldse77819e2011-07-22 19:30:19 -0700260 return -EAGAIN;
Al Viro35678662011-08-02 21:32:13 -0400261 /* no ->get_acl() calls in RCU mode... */
262 if (acl == ACL_NOT_CACHED)
263 return -ECHILD;
Ari Savolainen206b1d02011-08-06 19:43:07 +0300264 return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
Linus Torvaldse77819e2011-07-22 19:30:19 -0700265 }
266
Christoph Hellwig2982baa2013-12-20 05:16:38 -0800267 acl = get_acl(inode, ACL_TYPE_ACCESS);
268 if (IS_ERR(acl))
269 return PTR_ERR(acl);
Linus Torvaldse77819e2011-07-22 19:30:19 -0700270 if (acl) {
271 int error = posix_acl_permission(inode, acl, mask);
272 posix_acl_release(acl);
273 return error;
274 }
Linus Torvalds84635d62011-07-25 22:47:03 -0700275#endif
Linus Torvaldse77819e2011-07-22 19:30:19 -0700276
277 return -EAGAIN;
278}
279
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700280/*
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530281 * This does the basic permission checking
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700282 */
Al Viro7e401452011-06-20 19:12:17 -0400283static int acl_permission_check(struct inode *inode, int mask)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700284{
Linus Torvalds26cf46b2011-05-13 11:51:01 -0700285 unsigned int mode = inode->i_mode;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700286
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -0800287 if (likely(uid_eq(current_fsuid(), inode->i_uid)))
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700288 mode >>= 6;
289 else {
Linus Torvaldse77819e2011-07-22 19:30:19 -0700290 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
Al Viro7e401452011-06-20 19:12:17 -0400291 int error = check_acl(inode, mask);
Nick Pigginb74c79e2011-01-07 17:49:58 +1100292 if (error != -EAGAIN)
293 return error;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700294 }
295
296 if (in_group_p(inode->i_gid))
297 mode >>= 3;
298 }
299
300 /*
301 * If the DACs are ok we don't need any capability check.
302 */
Al Viro9c2c7032011-06-20 19:06:22 -0400303 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700304 return 0;
305 return -EACCES;
306}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308/**
Nick Pigginb74c79e2011-01-07 17:49:58 +1100309 * generic_permission - check for access rights on a Posix-like filesystem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 * @inode: inode to check access rights for
Andreas Gruenbacher8fd90c82011-10-23 23:13:30 +0530311 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 *
313 * Used to check for read/write/execute permissions on a file.
314 * We use "fsuid" for this, letting us set arbitrary permissions
315 * for filesystem access without changing the "normal" uids which
Nick Pigginb74c79e2011-01-07 17:49:58 +1100316 * are used for other things.
317 *
318 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
319 * request cannot be satisfied (eg. requires blocking or too much complexity).
320 * It would then be called again in ref-walk mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 */
Al Viro2830ba72011-06-20 19:16:29 -0400322int generic_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700324 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 /*
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530327 * Do the basic permission checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 */
Al Viro7e401452011-06-20 19:12:17 -0400329 ret = acl_permission_check(inode, mask);
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700330 if (ret != -EACCES)
331 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Al Virod594e7e2011-06-20 19:55:42 -0400333 if (S_ISDIR(inode->i_mode)) {
334 /* DACs are overridable for directories */
Eric W. Biederman1a48e2a2011-11-14 16:24:06 -0800335 if (inode_capable(inode, CAP_DAC_OVERRIDE))
Al Virod594e7e2011-06-20 19:55:42 -0400336 return 0;
337 if (!(mask & MAY_WRITE))
Eric W. Biederman1a48e2a2011-11-14 16:24:06 -0800338 if (inode_capable(inode, CAP_DAC_READ_SEARCH))
Al Virod594e7e2011-06-20 19:55:42 -0400339 return 0;
340 return -EACCES;
341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 /*
343 * Read/write DACs are always overridable.
Al Virod594e7e2011-06-20 19:55:42 -0400344 * Executable DACs are overridable when there is
345 * at least one exec bit set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 */
Al Virod594e7e2011-06-20 19:55:42 -0400347 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
Eric W. Biederman1a48e2a2011-11-14 16:24:06 -0800348 if (inode_capable(inode, CAP_DAC_OVERRIDE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return 0;
350
351 /*
352 * Searching includes executable on directories, else just read.
353 */
Serge E. Hallyn7ea66002009-12-29 14:50:19 -0600354 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
Al Virod594e7e2011-06-20 19:55:42 -0400355 if (mask == MAY_READ)
Eric W. Biederman1a48e2a2011-11-14 16:24:06 -0800356 if (inode_capable(inode, CAP_DAC_READ_SEARCH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return 0;
358
359 return -EACCES;
360}
361
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700362/*
363 * We _really_ want to just do "generic_permission()" without
364 * even looking at the inode->i_op values. So we keep a cache
365 * flag in inode->i_opflags, that says "this has not special
366 * permission function, use the fast case".
367 */
368static inline int do_inode_permission(struct inode *inode, int mask)
369{
370 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
371 if (likely(inode->i_op->permission))
372 return inode->i_op->permission(inode, mask);
373
374 /* This gets set once for the inode lifetime */
375 spin_lock(&inode->i_lock);
376 inode->i_opflags |= IOP_FASTPERM;
377 spin_unlock(&inode->i_lock);
378 }
379 return generic_permission(inode, mask);
380}
381
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200382/**
David Howells0bdaea92012-06-25 12:55:46 +0100383 * __inode_permission - Check for access rights to a given inode
384 * @inode: Inode to check permission on
385 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200386 *
David Howells0bdaea92012-06-25 12:55:46 +0100387 * Check for read/write/execute permissions on an inode.
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530388 *
389 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
David Howells0bdaea92012-06-25 12:55:46 +0100390 *
391 * This does not check for a read-only file system. You probably want
392 * inode_permission().
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200393 */
David Howells0bdaea92012-06-25 12:55:46 +0100394int __inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
Al Viroe6305c42008-07-15 21:03:57 -0400396 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700398 if (unlikely(mask & MAY_WRITE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 /*
400 * Nobody gets write access to an immutable file.
401 */
402 if (IS_IMMUTABLE(inode))
403 return -EACCES;
404 }
405
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700406 retval = do_inode_permission(inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (retval)
408 return retval;
409
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700410 retval = devcgroup_inode_permission(inode, mask);
411 if (retval)
412 return retval;
413
Eric Parisd09ca732010-07-23 11:43:57 -0400414 return security_inode_permission(inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
Al Virof4d6ff82011-06-19 13:14:21 -0400417/**
David Howells0bdaea92012-06-25 12:55:46 +0100418 * sb_permission - Check superblock-level permissions
419 * @sb: Superblock of inode to check permission on
Randy Dunlap55852632012-08-18 17:39:25 -0700420 * @inode: Inode to check permission on
David Howells0bdaea92012-06-25 12:55:46 +0100421 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
422 *
423 * Separate out file-system wide checks from inode-specific permission checks.
424 */
425static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
426{
427 if (unlikely(mask & MAY_WRITE)) {
428 umode_t mode = inode->i_mode;
429
430 /* Nobody gets write access to a read-only fs. */
431 if ((sb->s_flags & MS_RDONLY) &&
432 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
433 return -EROFS;
434 }
435 return 0;
436}
437
438/**
439 * inode_permission - Check for access rights to a given inode
440 * @inode: Inode to check permission on
441 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
442 *
443 * Check for read/write/execute permissions on an inode. We use fs[ug]id for
444 * this, letting us set arbitrary permissions for filesystem access without
445 * changing the "normal" UIDs which are used for other things.
446 *
447 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
448 */
449int inode_permission(struct inode *inode, int mask)
450{
451 int retval;
452
453 retval = sb_permission(inode->i_sb, inode, mask);
454 if (retval)
455 return retval;
456 return __inode_permission(inode, mask);
457}
458
459/**
Jan Blunck5dd784d02008-02-14 19:34:38 -0800460 * path_get - get a reference to a path
461 * @path: path to get the reference to
462 *
463 * Given a path increment the reference count to the dentry and the vfsmount.
464 */
Al Virodcf787f2013-03-01 23:51:07 -0500465void path_get(const struct path *path)
Jan Blunck5dd784d02008-02-14 19:34:38 -0800466{
467 mntget(path->mnt);
468 dget(path->dentry);
469}
470EXPORT_SYMBOL(path_get);
471
472/**
Jan Blunck1d957f92008-02-14 19:34:35 -0800473 * path_put - put a reference to a path
474 * @path: path to put the reference to
475 *
476 * Given a path decrement the reference count to the dentry and the vfsmount.
477 */
Al Virodcf787f2013-03-01 23:51:07 -0500478void path_put(const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
Jan Blunck1d957f92008-02-14 19:34:35 -0800480 dput(path->dentry);
481 mntput(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
Jan Blunck1d957f92008-02-14 19:34:35 -0800483EXPORT_SYMBOL(path_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Al Viro19660af2011-03-25 10:32:48 -0400485/*
Nick Piggin31e6b012011-01-07 17:49:52 +1100486 * Path walking has 2 modes, rcu-walk and ref-walk (see
Al Viro19660af2011-03-25 10:32:48 -0400487 * Documentation/filesystems/path-lookup.txt). In situations when we can't
488 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
489 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
490 * mode. Refcounts are grabbed at the last known good point before rcu-walk
491 * got stuck, so ref-walk may continue from there. If this is not successful
492 * (eg. a seqcount has changed), then failure is returned and it's up to caller
493 * to restart the path walk from the beginning in ref-walk mode.
Nick Piggin31e6b012011-01-07 17:49:52 +1100494 */
Nick Piggin31e6b012011-01-07 17:49:52 +1100495
496/**
Al Viro19660af2011-03-25 10:32:48 -0400497 * unlazy_walk - try to switch to ref-walk mode.
498 * @nd: nameidata pathwalk data
499 * @dentry: child of nd->path.dentry or NULL
Randy Dunlap39191622011-01-08 19:36:21 -0800500 * Returns: 0 on success, -ECHILD on failure
Nick Piggin31e6b012011-01-07 17:49:52 +1100501 *
Al Viro19660af2011-03-25 10:32:48 -0400502 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
503 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
504 * @nd or NULL. Must be called from rcu-walk context.
Nick Piggin31e6b012011-01-07 17:49:52 +1100505 */
Al Viro19660af2011-03-25 10:32:48 -0400506static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
Nick Piggin31e6b012011-01-07 17:49:52 +1100507{
508 struct fs_struct *fs = current->fs;
509 struct dentry *parent = nd->path.dentry;
510
511 BUG_ON(!(nd->flags & LOOKUP_RCU));
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700512
513 /*
Al Viro48a066e2013-09-29 22:06:07 -0400514 * After legitimizing the bastards, terminate_walk()
515 * will do the right thing for non-RCU mode, and all our
516 * subsequent exit cases should rcu_read_unlock()
517 * before returning. Do vfsmount first; if dentry
518 * can't be legitimized, just set nd->path.dentry to NULL
519 * and rely on dput(NULL) being a no-op.
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700520 */
Al Viro48a066e2013-09-29 22:06:07 -0400521 if (!legitimize_mnt(nd->path.mnt, nd->m_seq))
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700522 return -ECHILD;
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700523 nd->flags &= ~LOOKUP_RCU;
Linus Torvalds15570082013-09-02 11:38:06 -0700524
Al Viro48a066e2013-09-29 22:06:07 -0400525 if (!lockref_get_not_dead(&parent->d_lockref)) {
526 nd->path.dentry = NULL;
Al Virod870b4a2013-11-29 01:48:32 -0500527 goto out;
Al Viro48a066e2013-09-29 22:06:07 -0400528 }
529
Linus Torvalds15570082013-09-02 11:38:06 -0700530 /*
531 * For a negative lookup, the lookup sequence point is the parents
532 * sequence point, and it only needs to revalidate the parent dentry.
533 *
534 * For a positive lookup, we need to move both the parent and the
535 * dentry from the RCU domain to be properly refcounted. And the
536 * sequence number in the dentry validates *both* dentry counters,
537 * since we checked the sequence number of the parent after we got
538 * the child sequence number. So we know the parent must still
539 * be valid if the child sequence number is still valid.
540 */
Al Viro19660af2011-03-25 10:32:48 -0400541 if (!dentry) {
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700542 if (read_seqcount_retry(&parent->d_seq, nd->seq))
543 goto out;
Al Viro19660af2011-03-25 10:32:48 -0400544 BUG_ON(nd->inode != parent->d_inode);
545 } else {
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700546 if (!lockref_get_not_dead(&dentry->d_lockref))
547 goto out;
548 if (read_seqcount_retry(&dentry->d_seq, nd->seq))
549 goto drop_dentry;
Al Viro19660af2011-03-25 10:32:48 -0400550 }
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700551
552 /*
553 * Sequence counts matched. Now make sure that the root is
554 * still valid and get it if required.
555 */
556 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
557 spin_lock(&fs->lock);
558 if (nd->root.mnt != fs->root.mnt || nd->root.dentry != fs->root.dentry)
559 goto unlock_and_drop_dentry;
Nick Piggin31e6b012011-01-07 17:49:52 +1100560 path_get(&nd->root);
561 spin_unlock(&fs->lock);
562 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100563
Al Viro8b61e742013-11-08 12:45:01 -0500564 rcu_read_unlock();
Nick Piggin31e6b012011-01-07 17:49:52 +1100565 return 0;
Al Viro19660af2011-03-25 10:32:48 -0400566
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700567unlock_and_drop_dentry:
568 spin_unlock(&fs->lock);
569drop_dentry:
Al Viro8b61e742013-11-08 12:45:01 -0500570 rcu_read_unlock();
Linus Torvalds15570082013-09-02 11:38:06 -0700571 dput(dentry);
Linus Torvaldsd0d27272013-09-10 12:17:49 -0700572 goto drop_root_mnt;
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700573out:
Al Viro8b61e742013-11-08 12:45:01 -0500574 rcu_read_unlock();
Linus Torvaldsd0d27272013-09-10 12:17:49 -0700575drop_root_mnt:
576 if (!(nd->flags & LOOKUP_ROOT))
577 nd->root.mnt = NULL;
Nick Piggin31e6b012011-01-07 17:49:52 +1100578 return -ECHILD;
579}
580
Al Viro4ce16ef32012-06-10 16:10:59 -0400581static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
Nick Piggin34286d62011-01-07 17:49:57 +1100582{
Al Viro4ce16ef32012-06-10 16:10:59 -0400583 return dentry->d_op->d_revalidate(dentry, flags);
Nick Piggin34286d62011-01-07 17:49:57 +1100584}
585
Al Viro9f1fafe2011-03-25 11:00:12 -0400586/**
587 * complete_walk - successful completion of path walk
588 * @nd: pointer nameidata
Jeff Layton39159de2009-12-07 12:01:50 -0500589 *
Al Viro9f1fafe2011-03-25 11:00:12 -0400590 * If we had been in RCU mode, drop out of it and legitimize nd->path.
591 * Revalidate the final result, unless we'd already done that during
592 * the path walk or the filesystem doesn't ask for it. Return 0 on
593 * success, -error on failure. In case of failure caller does not
594 * need to drop nd->path.
Jeff Layton39159de2009-12-07 12:01:50 -0500595 */
Al Viro9f1fafe2011-03-25 11:00:12 -0400596static int complete_walk(struct nameidata *nd)
Jeff Layton39159de2009-12-07 12:01:50 -0500597{
Al Viro16c2cd72011-02-22 15:50:10 -0500598 struct dentry *dentry = nd->path.dentry;
Jeff Layton39159de2009-12-07 12:01:50 -0500599 int status;
Jeff Layton39159de2009-12-07 12:01:50 -0500600
Al Viro9f1fafe2011-03-25 11:00:12 -0400601 if (nd->flags & LOOKUP_RCU) {
602 nd->flags &= ~LOOKUP_RCU;
603 if (!(nd->flags & LOOKUP_ROOT))
604 nd->root.mnt = NULL;
Linus Torvalds15570082013-09-02 11:38:06 -0700605
Al Viro48a066e2013-09-29 22:06:07 -0400606 if (!legitimize_mnt(nd->path.mnt, nd->m_seq)) {
Al Viro8b61e742013-11-08 12:45:01 -0500607 rcu_read_unlock();
Al Viro48a066e2013-09-29 22:06:07 -0400608 return -ECHILD;
609 }
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700610 if (unlikely(!lockref_get_not_dead(&dentry->d_lockref))) {
Al Viro8b61e742013-11-08 12:45:01 -0500611 rcu_read_unlock();
Al Viro48a066e2013-09-29 22:06:07 -0400612 mntput(nd->path.mnt);
Al Viro9f1fafe2011-03-25 11:00:12 -0400613 return -ECHILD;
614 }
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700615 if (read_seqcount_retry(&dentry->d_seq, nd->seq)) {
Al Viro8b61e742013-11-08 12:45:01 -0500616 rcu_read_unlock();
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700617 dput(dentry);
Al Viro48a066e2013-09-29 22:06:07 -0400618 mntput(nd->path.mnt);
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700619 return -ECHILD;
620 }
Al Viro8b61e742013-11-08 12:45:01 -0500621 rcu_read_unlock();
Al Viro9f1fafe2011-03-25 11:00:12 -0400622 }
623
Al Viro16c2cd72011-02-22 15:50:10 -0500624 if (likely(!(nd->flags & LOOKUP_JUMPED)))
Jeff Layton39159de2009-12-07 12:01:50 -0500625 return 0;
626
Jeff Laytonecf3d1f2013-02-20 11:19:05 -0500627 if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
Al Viro16c2cd72011-02-22 15:50:10 -0500628 return 0;
629
Jeff Laytonecf3d1f2013-02-20 11:19:05 -0500630 status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
Jeff Layton39159de2009-12-07 12:01:50 -0500631 if (status > 0)
632 return 0;
633
Al Viro16c2cd72011-02-22 15:50:10 -0500634 if (!status)
Jeff Layton39159de2009-12-07 12:01:50 -0500635 status = -ESTALE;
Al Viro16c2cd72011-02-22 15:50:10 -0500636
Al Viro9f1fafe2011-03-25 11:00:12 -0400637 path_put(&nd->path);
Jeff Layton39159de2009-12-07 12:01:50 -0500638 return status;
639}
640
Al Viro2a737872009-04-07 11:49:53 -0400641static __always_inline void set_root(struct nameidata *nd)
642{
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200643 if (!nd->root.mnt)
644 get_fs_root(current->fs, &nd->root);
Al Viro2a737872009-04-07 11:49:53 -0400645}
646
Al Viro6de88d72009-08-09 01:41:57 +0400647static int link_path_walk(const char *, struct nameidata *);
648
Nick Piggin31e6b012011-01-07 17:49:52 +1100649static __always_inline void set_root_rcu(struct nameidata *nd)
650{
651 if (!nd->root.mnt) {
652 struct fs_struct *fs = current->fs;
Nick Pigginc28cc362011-01-07 17:49:53 +1100653 unsigned seq;
654
655 do {
656 seq = read_seqcount_begin(&fs->seq);
657 nd->root = fs->root;
Tim Chenc1530012011-04-15 11:39:29 -0700658 nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
Nick Pigginc28cc362011-01-07 17:49:53 +1100659 } while (read_seqcount_retry(&fs->seq, seq));
Nick Piggin31e6b012011-01-07 17:49:52 +1100660 }
661}
662
Jan Blunck1d957f92008-02-14 19:34:35 -0800663static void path_put_conditional(struct path *path, struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700664{
665 dput(path->dentry);
Jan Blunck4ac91372008-02-14 19:34:32 -0800666 if (path->mnt != nd->path.mnt)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700667 mntput(path->mnt);
668}
669
Nick Piggin7b9337a2011-01-14 08:42:43 +0000670static inline void path_to_nameidata(const struct path *path,
671 struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700672{
Nick Piggin31e6b012011-01-07 17:49:52 +1100673 if (!(nd->flags & LOOKUP_RCU)) {
674 dput(nd->path.dentry);
675 if (nd->path.mnt != path->mnt)
676 mntput(nd->path.mnt);
Huang Shijie9a229682010-04-02 17:37:13 +0800677 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100678 nd->path.mnt = path->mnt;
Jan Blunck4ac91372008-02-14 19:34:32 -0800679 nd->path.dentry = path->dentry;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700680}
681
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400682/*
683 * Helper to directly jump to a known parsed path from ->follow_link,
684 * caller must have taken a reference to path beforehand.
685 */
686void nd_jump_link(struct nameidata *nd, struct path *path)
687{
688 path_put(&nd->path);
689
690 nd->path = *path;
691 nd->inode = nd->path.dentry->d_inode;
692 nd->flags |= LOOKUP_JUMPED;
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400693}
694
Al Viro574197e2011-03-14 22:20:34 -0400695static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
696{
697 struct inode *inode = link->dentry->d_inode;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400698 if (inode->i_op->put_link)
Al Viro574197e2011-03-14 22:20:34 -0400699 inode->i_op->put_link(link->dentry, nd, cookie);
700 path_put(link);
701}
702
Linus Torvalds561ec642012-10-26 10:05:07 -0700703int sysctl_protected_symlinks __read_mostly = 0;
704int sysctl_protected_hardlinks __read_mostly = 0;
Kees Cook800179c2012-07-25 17:29:07 -0700705
706/**
707 * may_follow_link - Check symlink following for unsafe situations
708 * @link: The path of the symlink
Randy Dunlap55852632012-08-18 17:39:25 -0700709 * @nd: nameidata pathwalk data
Kees Cook800179c2012-07-25 17:29:07 -0700710 *
711 * In the case of the sysctl_protected_symlinks sysctl being enabled,
712 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
713 * in a sticky world-writable directory. This is to protect privileged
714 * processes from failing races against path names that may change out
715 * from under them by way of other users creating malicious symlinks.
716 * It will permit symlinks to be followed only when outside a sticky
717 * world-writable directory, or when the uid of the symlink and follower
718 * match, or when the directory owner matches the symlink's owner.
719 *
720 * Returns 0 if following the symlink is allowed, -ve on error.
721 */
722static inline int may_follow_link(struct path *link, struct nameidata *nd)
723{
724 const struct inode *inode;
725 const struct inode *parent;
726
727 if (!sysctl_protected_symlinks)
728 return 0;
729
730 /* Allowed if owner and follower match. */
731 inode = link->dentry->d_inode;
Eric W. Biederman81abe272012-08-03 09:38:08 -0700732 if (uid_eq(current_cred()->fsuid, inode->i_uid))
Kees Cook800179c2012-07-25 17:29:07 -0700733 return 0;
734
735 /* Allowed if parent directory not sticky and world-writable. */
736 parent = nd->path.dentry->d_inode;
737 if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
738 return 0;
739
740 /* Allowed if parent directory and link owner match. */
Eric W. Biederman81abe272012-08-03 09:38:08 -0700741 if (uid_eq(parent->i_uid, inode->i_uid))
Kees Cook800179c2012-07-25 17:29:07 -0700742 return 0;
743
Sasha Levinffd8d102012-10-04 19:56:40 -0400744 audit_log_link_denied("follow_link", link);
Kees Cook800179c2012-07-25 17:29:07 -0700745 path_put_conditional(link, nd);
746 path_put(&nd->path);
747 return -EACCES;
748}
749
750/**
751 * safe_hardlink_source - Check for safe hardlink conditions
752 * @inode: the source inode to hardlink from
753 *
754 * Return false if at least one of the following conditions:
755 * - inode is not a regular file
756 * - inode is setuid
757 * - inode is setgid and group-exec
758 * - access failure for read and write
759 *
760 * Otherwise returns true.
761 */
762static bool safe_hardlink_source(struct inode *inode)
763{
764 umode_t mode = inode->i_mode;
765
766 /* Special files should not get pinned to the filesystem. */
767 if (!S_ISREG(mode))
768 return false;
769
770 /* Setuid files should not get pinned to the filesystem. */
771 if (mode & S_ISUID)
772 return false;
773
774 /* Executable setgid files should not get pinned to the filesystem. */
775 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
776 return false;
777
778 /* Hardlinking to unreadable or unwritable sources is dangerous. */
779 if (inode_permission(inode, MAY_READ | MAY_WRITE))
780 return false;
781
782 return true;
783}
784
785/**
786 * may_linkat - Check permissions for creating a hardlink
787 * @link: the source to hardlink from
788 *
789 * Block hardlink when all of:
790 * - sysctl_protected_hardlinks enabled
791 * - fsuid does not match inode
792 * - hardlink source is unsafe (see safe_hardlink_source() above)
793 * - not CAP_FOWNER
794 *
795 * Returns 0 if successful, -ve on error.
796 */
797static int may_linkat(struct path *link)
798{
799 const struct cred *cred;
800 struct inode *inode;
801
802 if (!sysctl_protected_hardlinks)
803 return 0;
804
805 cred = current_cred();
806 inode = link->dentry->d_inode;
807
808 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
809 * otherwise, it must be a safe source.
810 */
Eric W. Biederman81abe272012-08-03 09:38:08 -0700811 if (uid_eq(cred->fsuid, inode->i_uid) || safe_hardlink_source(inode) ||
Kees Cook800179c2012-07-25 17:29:07 -0700812 capable(CAP_FOWNER))
813 return 0;
814
Kees Cooka51d9ea2012-07-25 17:29:08 -0700815 audit_log_link_denied("linkat", link);
Kees Cook800179c2012-07-25 17:29:07 -0700816 return -EPERM;
817}
818
Al Virodef4af32009-12-26 08:37:05 -0500819static __always_inline int
Al Viro574197e2011-03-14 22:20:34 -0400820follow_link(struct path *link, struct nameidata *nd, void **p)
Ian Kent051d3812006-03-27 01:14:53 -0800821{
Nick Piggin7b9337a2011-01-14 08:42:43 +0000822 struct dentry *dentry = link->dentry;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400823 int error;
824 char *s;
Ian Kent051d3812006-03-27 01:14:53 -0800825
Al Viro844a3912011-02-15 00:38:26 -0500826 BUG_ON(nd->flags & LOOKUP_RCU);
827
Al Viro0e794582011-03-16 02:45:02 -0400828 if (link->mnt == nd->path.mnt)
829 mntget(link->mnt);
830
Al Viro6d7b5aa2012-06-10 04:15:17 -0400831 error = -ELOOP;
832 if (unlikely(current->total_link_count >= 40))
833 goto out_put_nd_path;
834
Al Viro574197e2011-03-14 22:20:34 -0400835 cond_resched();
836 current->total_link_count++;
837
Al Viro68ac1232012-03-15 08:21:57 -0400838 touch_atime(link);
Ian Kent051d3812006-03-27 01:14:53 -0800839 nd_set_link(nd, NULL);
840
Al Viro36f3b4f2011-02-22 21:24:38 -0500841 error = security_inode_follow_link(link->dentry, nd);
Al Viro6d7b5aa2012-06-10 04:15:17 -0400842 if (error)
843 goto out_put_nd_path;
Al Viro36f3b4f2011-02-22 21:24:38 -0500844
Al Viro86acdca12009-12-22 23:45:11 -0500845 nd->last_type = LAST_BIND;
Al Virodef4af32009-12-26 08:37:05 -0500846 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
847 error = PTR_ERR(*p);
Al Viro6d7b5aa2012-06-10 04:15:17 -0400848 if (IS_ERR(*p))
Christoph Hellwig408ef012012-06-18 10:47:03 -0400849 goto out_put_nd_path;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400850
851 error = 0;
852 s = nd_get_link(nd);
853 if (s) {
Al Viro443ed252013-09-10 12:00:43 -0400854 if (unlikely(IS_ERR(s))) {
855 path_put(&nd->path);
856 put_link(nd, link, *p);
857 return PTR_ERR(s);
858 }
859 if (*s == '/') {
860 set_root(nd);
861 path_put(&nd->path);
862 nd->path = nd->root;
863 path_get(&nd->root);
864 nd->flags |= LOOKUP_JUMPED;
865 }
866 nd->inode = nd->path.dentry->d_inode;
867 error = link_path_walk(s, nd);
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400868 if (unlikely(error))
869 put_link(nd, link, *p);
Ian Kent051d3812006-03-27 01:14:53 -0800870 }
Al Viro6d7b5aa2012-06-10 04:15:17 -0400871
872 return error;
873
874out_put_nd_path:
Arnd Bergmann98f6ef62012-10-11 13:20:00 +0000875 *p = NULL;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400876 path_put(&nd->path);
Al Viro6d7b5aa2012-06-10 04:15:17 -0400877 path_put(link);
Ian Kent051d3812006-03-27 01:14:53 -0800878 return error;
879}
880
Nick Piggin31e6b012011-01-07 17:49:52 +1100881static int follow_up_rcu(struct path *path)
882{
Al Viro0714a532011-11-24 22:19:58 -0500883 struct mount *mnt = real_mount(path->mnt);
884 struct mount *parent;
Nick Piggin31e6b012011-01-07 17:49:52 +1100885 struct dentry *mountpoint;
886
Al Viro0714a532011-11-24 22:19:58 -0500887 parent = mnt->mnt_parent;
888 if (&parent->mnt == path->mnt)
Nick Piggin31e6b012011-01-07 17:49:52 +1100889 return 0;
Al Viroa73324d2011-11-24 22:25:07 -0500890 mountpoint = mnt->mnt_mountpoint;
Nick Piggin31e6b012011-01-07 17:49:52 +1100891 path->dentry = mountpoint;
Al Viro0714a532011-11-24 22:19:58 -0500892 path->mnt = &parent->mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +1100893 return 1;
894}
895
David Howellsf015f1262012-06-25 12:55:28 +0100896/*
897 * follow_up - Find the mountpoint of path's vfsmount
898 *
899 * Given a path, find the mountpoint of its source file system.
900 * Replace @path with the path of the mountpoint in the parent mount.
901 * Up is towards /.
902 *
903 * Return 1 if we went up a level and 0 if we were already at the
904 * root.
905 */
Al Virobab77eb2009-04-18 03:26:48 -0400906int follow_up(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Al Viro0714a532011-11-24 22:19:58 -0500908 struct mount *mnt = real_mount(path->mnt);
909 struct mount *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 struct dentry *mountpoint;
Nick Piggin99b7db72010-08-18 04:37:39 +1000911
Al Viro48a066e2013-09-29 22:06:07 -0400912 read_seqlock_excl(&mount_lock);
Al Viro0714a532011-11-24 22:19:58 -0500913 parent = mnt->mnt_parent;
Al Viro3c0a6162012-07-18 17:32:50 +0400914 if (parent == mnt) {
Al Viro48a066e2013-09-29 22:06:07 -0400915 read_sequnlock_excl(&mount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 return 0;
917 }
Al Viro0714a532011-11-24 22:19:58 -0500918 mntget(&parent->mnt);
Al Viroa73324d2011-11-24 22:25:07 -0500919 mountpoint = dget(mnt->mnt_mountpoint);
Al Viro48a066e2013-09-29 22:06:07 -0400920 read_sequnlock_excl(&mount_lock);
Al Virobab77eb2009-04-18 03:26:48 -0400921 dput(path->dentry);
922 path->dentry = mountpoint;
923 mntput(path->mnt);
Al Viro0714a532011-11-24 22:19:58 -0500924 path->mnt = &parent->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 return 1;
926}
927
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100928/*
David Howells9875cf82011-01-14 18:45:21 +0000929 * Perform an automount
930 * - return -EISDIR to tell follow_managed() to stop and return the path we
931 * were called with.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 */
David Howells9875cf82011-01-14 18:45:21 +0000933static int follow_automount(struct path *path, unsigned flags,
934 bool *need_mntput)
Nick Piggin31e6b012011-01-07 17:49:52 +1100935{
David Howells9875cf82011-01-14 18:45:21 +0000936 struct vfsmount *mnt;
David Howellsea5b7782011-01-14 19:10:03 +0000937 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +1100938
David Howells9875cf82011-01-14 18:45:21 +0000939 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
940 return -EREMOTE;
Al Viro463ffb22005-06-06 13:36:05 -0700941
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200942 /* We don't want to mount if someone's just doing a stat -
943 * unless they're stat'ing a directory and appended a '/' to
944 * the name.
945 *
946 * We do, however, want to mount if someone wants to open or
947 * create a file of any type under the mountpoint, wants to
948 * traverse through the mountpoint or wants to open the
949 * mounted directory. Also, autofs may mark negative dentries
950 * as being automount points. These will need the attentions
951 * of the daemon to instantiate them before they can be used.
David Howells9875cf82011-01-14 18:45:21 +0000952 */
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200953 if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
Linus Torvaldsd94c1772011-09-26 17:44:55 -0700954 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200955 path->dentry->d_inode)
956 return -EISDIR;
957
David Howells9875cf82011-01-14 18:45:21 +0000958 current->total_link_count++;
959 if (current->total_link_count >= 40)
960 return -ELOOP;
961
962 mnt = path->dentry->d_op->d_automount(path);
963 if (IS_ERR(mnt)) {
964 /*
965 * The filesystem is allowed to return -EISDIR here to indicate
966 * it doesn't want to automount. For instance, autofs would do
967 * this so that its userspace daemon can mount on this dentry.
968 *
969 * However, we can only permit this if it's a terminal point in
970 * the path being looked up; if it wasn't then the remainder of
971 * the path is inaccessible and we should say so.
972 */
Al Viro49084c32011-06-25 21:59:52 -0400973 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
David Howells9875cf82011-01-14 18:45:21 +0000974 return -EREMOTE;
975 return PTR_ERR(mnt);
976 }
David Howellsea5b7782011-01-14 19:10:03 +0000977
David Howells9875cf82011-01-14 18:45:21 +0000978 if (!mnt) /* mount collision */
979 return 0;
980
Al Viro8aef1882011-06-16 15:10:06 +0100981 if (!*need_mntput) {
982 /* lock_mount() may release path->mnt on error */
983 mntget(path->mnt);
984 *need_mntput = true;
985 }
Al Viro19a167a2011-01-17 01:35:23 -0500986 err = finish_automount(mnt, path);
David Howellsea5b7782011-01-14 19:10:03 +0000987
David Howellsea5b7782011-01-14 19:10:03 +0000988 switch (err) {
989 case -EBUSY:
990 /* Someone else made a mount here whilst we were busy */
Al Viro19a167a2011-01-17 01:35:23 -0500991 return 0;
David Howellsea5b7782011-01-14 19:10:03 +0000992 case 0:
Al Viro8aef1882011-06-16 15:10:06 +0100993 path_put(path);
David Howellsea5b7782011-01-14 19:10:03 +0000994 path->mnt = mnt;
995 path->dentry = dget(mnt->mnt_root);
David Howellsea5b7782011-01-14 19:10:03 +0000996 return 0;
Al Viro19a167a2011-01-17 01:35:23 -0500997 default:
998 return err;
David Howellsea5b7782011-01-14 19:10:03 +0000999 }
Al Viro19a167a2011-01-17 01:35:23 -05001000
David Howells9875cf82011-01-14 18:45:21 +00001001}
1002
1003/*
1004 * Handle a dentry that is managed in some way.
David Howellscc53ce52011-01-14 18:45:26 +00001005 * - Flagged for transit management (autofs)
David Howells9875cf82011-01-14 18:45:21 +00001006 * - Flagged as mountpoint
1007 * - Flagged as automount point
1008 *
1009 * This may only be called in refwalk mode.
1010 *
1011 * Serialization is taken care of in namespace.c
1012 */
1013static int follow_managed(struct path *path, unsigned flags)
1014{
Al Viro8aef1882011-06-16 15:10:06 +01001015 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
David Howells9875cf82011-01-14 18:45:21 +00001016 unsigned managed;
1017 bool need_mntput = false;
Al Viro8aef1882011-06-16 15:10:06 +01001018 int ret = 0;
David Howells9875cf82011-01-14 18:45:21 +00001019
1020 /* Given that we're not holding a lock here, we retain the value in a
1021 * local variable for each dentry as we look at it so that we don't see
1022 * the components of that value change under us */
1023 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1024 managed &= DCACHE_MANAGED_DENTRY,
1025 unlikely(managed != 0)) {
David Howellscc53ce52011-01-14 18:45:26 +00001026 /* Allow the filesystem to manage the transit without i_mutex
1027 * being held. */
1028 if (managed & DCACHE_MANAGE_TRANSIT) {
1029 BUG_ON(!path->dentry->d_op);
1030 BUG_ON(!path->dentry->d_op->d_manage);
Al Viro1aed3e42011-03-18 09:09:02 -04001031 ret = path->dentry->d_op->d_manage(path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +00001032 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001033 break;
David Howellscc53ce52011-01-14 18:45:26 +00001034 }
1035
David Howells9875cf82011-01-14 18:45:21 +00001036 /* Transit to a mounted filesystem. */
1037 if (managed & DCACHE_MOUNTED) {
1038 struct vfsmount *mounted = lookup_mnt(path);
1039 if (mounted) {
1040 dput(path->dentry);
1041 if (need_mntput)
1042 mntput(path->mnt);
1043 path->mnt = mounted;
1044 path->dentry = dget(mounted->mnt_root);
1045 need_mntput = true;
1046 continue;
1047 }
1048
1049 /* Something is mounted on this dentry in another
1050 * namespace and/or whatever was mounted there in this
Al Viro48a066e2013-09-29 22:06:07 -04001051 * namespace got unmounted before lookup_mnt() could
1052 * get it */
David Howells9875cf82011-01-14 18:45:21 +00001053 }
1054
1055 /* Handle an automount point */
1056 if (managed & DCACHE_NEED_AUTOMOUNT) {
1057 ret = follow_automount(path, flags, &need_mntput);
1058 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001059 break;
David Howells9875cf82011-01-14 18:45:21 +00001060 continue;
1061 }
1062
1063 /* We didn't change the current path point */
1064 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 }
Al Viro8aef1882011-06-16 15:10:06 +01001066
1067 if (need_mntput && path->mnt == mnt)
1068 mntput(path->mnt);
1069 if (ret == -EISDIR)
1070 ret = 0;
Al Viroa3fbbde2011-11-07 21:21:26 +00001071 return ret < 0 ? ret : need_mntput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072}
1073
David Howellscc53ce52011-01-14 18:45:26 +00001074int follow_down_one(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
1076 struct vfsmount *mounted;
1077
Al Viro1c755af2009-04-18 14:06:57 -04001078 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -04001080 dput(path->dentry);
1081 mntput(path->mnt);
1082 path->mnt = mounted;
1083 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 return 1;
1085 }
1086 return 0;
1087}
1088
Ian Kent62a73752011-03-25 01:51:02 +08001089static inline bool managed_dentry_might_block(struct dentry *dentry)
1090{
1091 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
1092 dentry->d_op->d_manage(dentry, true) < 0);
1093}
1094
David Howells9875cf82011-01-14 18:45:21 +00001095/*
Al Viro287548e2011-05-27 06:50:06 -04001096 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1097 * we meet a managed dentry that would need blocking.
David Howells9875cf82011-01-14 18:45:21 +00001098 */
1099static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
Al Viro287548e2011-05-27 06:50:06 -04001100 struct inode **inode)
David Howells9875cf82011-01-14 18:45:21 +00001101{
Ian Kent62a73752011-03-25 01:51:02 +08001102 for (;;) {
Al Viroc7105362011-11-24 18:22:03 -05001103 struct mount *mounted;
Ian Kent62a73752011-03-25 01:51:02 +08001104 /*
1105 * Don't forget we might have a non-mountpoint managed dentry
1106 * that wants to block transit.
1107 */
Al Viro287548e2011-05-27 06:50:06 -04001108 if (unlikely(managed_dentry_might_block(path->dentry)))
David Howellsab909112011-01-14 18:46:51 +00001109 return false;
Ian Kent62a73752011-03-25 01:51:02 +08001110
1111 if (!d_mountpoint(path->dentry))
1112 break;
1113
Al Viro474279d2013-10-01 16:11:26 -04001114 mounted = __lookup_mnt(path->mnt, path->dentry);
David Howells9875cf82011-01-14 18:45:21 +00001115 if (!mounted)
1116 break;
Al Viroc7105362011-11-24 18:22:03 -05001117 path->mnt = &mounted->mnt;
1118 path->dentry = mounted->mnt.mnt_root;
Al Viroa3fbbde2011-11-07 21:21:26 +00001119 nd->flags |= LOOKUP_JUMPED;
David Howells9875cf82011-01-14 18:45:21 +00001120 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
Linus Torvalds59430262011-07-18 15:43:29 -07001121 /*
1122 * Update the inode too. We don't need to re-check the
1123 * dentry sequence number here after this d_inode read,
1124 * because a mount-point is always pinned.
1125 */
1126 *inode = path->dentry->d_inode;
David Howells9875cf82011-01-14 18:45:21 +00001127 }
David Howells9875cf82011-01-14 18:45:21 +00001128 return true;
1129}
1130
Al Virodea39372011-05-27 06:53:39 -04001131static void follow_mount_rcu(struct nameidata *nd)
Al Viro287548e2011-05-27 06:50:06 -04001132{
Al Virodea39372011-05-27 06:53:39 -04001133 while (d_mountpoint(nd->path.dentry)) {
Al Viroc7105362011-11-24 18:22:03 -05001134 struct mount *mounted;
Al Viro474279d2013-10-01 16:11:26 -04001135 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry);
Al Viro287548e2011-05-27 06:50:06 -04001136 if (!mounted)
1137 break;
Al Viroc7105362011-11-24 18:22:03 -05001138 nd->path.mnt = &mounted->mnt;
1139 nd->path.dentry = mounted->mnt.mnt_root;
Al Virodea39372011-05-27 06:53:39 -04001140 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro287548e2011-05-27 06:50:06 -04001141 }
1142}
1143
Nick Piggin31e6b012011-01-07 17:49:52 +11001144static int follow_dotdot_rcu(struct nameidata *nd)
1145{
Nick Piggin31e6b012011-01-07 17:49:52 +11001146 set_root_rcu(nd);
1147
David Howells9875cf82011-01-14 18:45:21 +00001148 while (1) {
Nick Piggin31e6b012011-01-07 17:49:52 +11001149 if (nd->path.dentry == nd->root.dentry &&
1150 nd->path.mnt == nd->root.mnt) {
1151 break;
1152 }
1153 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1154 struct dentry *old = nd->path.dentry;
1155 struct dentry *parent = old->d_parent;
1156 unsigned seq;
1157
1158 seq = read_seqcount_begin(&parent->d_seq);
1159 if (read_seqcount_retry(&old->d_seq, nd->seq))
Al Viroef7562d2011-03-04 14:35:59 -05001160 goto failed;
Nick Piggin31e6b012011-01-07 17:49:52 +11001161 nd->path.dentry = parent;
1162 nd->seq = seq;
1163 break;
1164 }
1165 if (!follow_up_rcu(&nd->path))
1166 break;
1167 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Nick Piggin31e6b012011-01-07 17:49:52 +11001168 }
Al Virodea39372011-05-27 06:53:39 -04001169 follow_mount_rcu(nd);
1170 nd->inode = nd->path.dentry->d_inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001171 return 0;
Al Viroef7562d2011-03-04 14:35:59 -05001172
1173failed:
1174 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001175 if (!(nd->flags & LOOKUP_ROOT))
1176 nd->root.mnt = NULL;
Al Viro8b61e742013-11-08 12:45:01 -05001177 rcu_read_unlock();
Al Viroef7562d2011-03-04 14:35:59 -05001178 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001179}
1180
David Howells9875cf82011-01-14 18:45:21 +00001181/*
David Howellscc53ce52011-01-14 18:45:26 +00001182 * Follow down to the covering mount currently visible to userspace. At each
1183 * point, the filesystem owning that dentry may be queried as to whether the
1184 * caller is permitted to proceed or not.
David Howellscc53ce52011-01-14 18:45:26 +00001185 */
Al Viro7cc90cc2011-03-18 09:04:20 -04001186int follow_down(struct path *path)
David Howellscc53ce52011-01-14 18:45:26 +00001187{
1188 unsigned managed;
1189 int ret;
1190
1191 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1192 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1193 /* Allow the filesystem to manage the transit without i_mutex
1194 * being held.
1195 *
1196 * We indicate to the filesystem if someone is trying to mount
1197 * something here. This gives autofs the chance to deny anyone
1198 * other than its daemon the right to mount on its
1199 * superstructure.
1200 *
1201 * The filesystem may sleep at this point.
1202 */
1203 if (managed & DCACHE_MANAGE_TRANSIT) {
1204 BUG_ON(!path->dentry->d_op);
1205 BUG_ON(!path->dentry->d_op->d_manage);
David Howellsab909112011-01-14 18:46:51 +00001206 ret = path->dentry->d_op->d_manage(
Al Viro1aed3e42011-03-18 09:09:02 -04001207 path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +00001208 if (ret < 0)
1209 return ret == -EISDIR ? 0 : ret;
1210 }
1211
1212 /* Transit to a mounted filesystem. */
1213 if (managed & DCACHE_MOUNTED) {
1214 struct vfsmount *mounted = lookup_mnt(path);
1215 if (!mounted)
1216 break;
1217 dput(path->dentry);
1218 mntput(path->mnt);
1219 path->mnt = mounted;
1220 path->dentry = dget(mounted->mnt_root);
1221 continue;
1222 }
1223
1224 /* Don't handle automount points here */
1225 break;
1226 }
1227 return 0;
1228}
1229
1230/*
David Howells9875cf82011-01-14 18:45:21 +00001231 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1232 */
1233static void follow_mount(struct path *path)
1234{
1235 while (d_mountpoint(path->dentry)) {
1236 struct vfsmount *mounted = lookup_mnt(path);
1237 if (!mounted)
1238 break;
1239 dput(path->dentry);
1240 mntput(path->mnt);
1241 path->mnt = mounted;
1242 path->dentry = dget(mounted->mnt_root);
1243 }
1244}
1245
Nick Piggin31e6b012011-01-07 17:49:52 +11001246static void follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
Al Viro2a737872009-04-07 11:49:53 -04001248 set_root(nd);
Andreas Mohre518ddb2006-09-29 02:01:22 -07001249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 while(1) {
Jan Blunck4ac91372008-02-14 19:34:32 -08001251 struct dentry *old = nd->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Al Viro2a737872009-04-07 11:49:53 -04001253 if (nd->path.dentry == nd->root.dentry &&
1254 nd->path.mnt == nd->root.mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 break;
1256 }
Jan Blunck4ac91372008-02-14 19:34:32 -08001257 if (nd->path.dentry != nd->path.mnt->mnt_root) {
Al Viro3088dd72010-01-30 15:47:29 -05001258 /* rare case of legitimate dget_parent()... */
1259 nd->path.dentry = dget_parent(nd->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 dput(old);
1261 break;
1262 }
Al Viro3088dd72010-01-30 15:47:29 -05001263 if (!follow_up(&nd->path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
Al Viro79ed0222009-04-18 13:59:41 -04001266 follow_mount(&nd->path);
Nick Piggin31e6b012011-01-07 17:49:52 +11001267 nd->inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268}
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270/*
Miklos Szeredibad61182012-03-26 12:54:24 +02001271 * This looks up the name in dcache, possibly revalidates the old dentry and
1272 * allocates a new one if not found or not valid. In the need_lookup argument
1273 * returns whether i_op->lookup is necessary.
1274 *
1275 * dir->d_inode->i_mutex must be held
Nick Pigginbaa03892010-08-18 04:37:31 +10001276 */
Miklos Szeredibad61182012-03-26 12:54:24 +02001277static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
Al Viro201f9562012-06-22 12:42:10 +04001278 unsigned int flags, bool *need_lookup)
Nick Pigginbaa03892010-08-18 04:37:31 +10001279{
Nick Pigginbaa03892010-08-18 04:37:31 +10001280 struct dentry *dentry;
Miklos Szeredibad61182012-03-26 12:54:24 +02001281 int error;
Nick Pigginbaa03892010-08-18 04:37:31 +10001282
Miklos Szeredibad61182012-03-26 12:54:24 +02001283 *need_lookup = false;
1284 dentry = d_lookup(dir, name);
1285 if (dentry) {
Jeff Layton39e3c952012-11-28 11:30:53 -05001286 if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
Al Viro201f9562012-06-22 12:42:10 +04001287 error = d_revalidate(dentry, flags);
Miklos Szeredibad61182012-03-26 12:54:24 +02001288 if (unlikely(error <= 0)) {
1289 if (error < 0) {
1290 dput(dentry);
1291 return ERR_PTR(error);
1292 } else if (!d_invalidate(dentry)) {
1293 dput(dentry);
1294 dentry = NULL;
1295 }
1296 }
1297 }
1298 }
Nick Pigginbaa03892010-08-18 04:37:31 +10001299
Miklos Szeredibad61182012-03-26 12:54:24 +02001300 if (!dentry) {
1301 dentry = d_alloc(dir, name);
1302 if (unlikely(!dentry))
1303 return ERR_PTR(-ENOMEM);
Nick Pigginbaa03892010-08-18 04:37:31 +10001304
Miklos Szeredibad61182012-03-26 12:54:24 +02001305 *need_lookup = true;
Nick Pigginbaa03892010-08-18 04:37:31 +10001306 }
1307 return dentry;
1308}
1309
1310/*
J. Bruce Fields13a2c3b2013-10-23 16:09:16 -04001311 * Call i_op->lookup on the dentry. The dentry must be negative and
1312 * unhashed.
Miklos Szeredibad61182012-03-26 12:54:24 +02001313 *
1314 * dir->d_inode->i_mutex must be held
Josef Bacik44396f42011-05-31 11:58:49 -04001315 */
Miklos Szeredibad61182012-03-26 12:54:24 +02001316static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
Al Viro72bd8662012-06-10 17:17:17 -04001317 unsigned int flags)
Josef Bacik44396f42011-05-31 11:58:49 -04001318{
Josef Bacik44396f42011-05-31 11:58:49 -04001319 struct dentry *old;
1320
1321 /* Don't create child dentry for a dead directory. */
Miklos Szeredibad61182012-03-26 12:54:24 +02001322 if (unlikely(IS_DEADDIR(dir))) {
Miklos Szeredie188dc02012-02-03 14:25:18 +01001323 dput(dentry);
Josef Bacik44396f42011-05-31 11:58:49 -04001324 return ERR_PTR(-ENOENT);
Miklos Szeredie188dc02012-02-03 14:25:18 +01001325 }
Josef Bacik44396f42011-05-31 11:58:49 -04001326
Al Viro72bd8662012-06-10 17:17:17 -04001327 old = dir->i_op->lookup(dir, dentry, flags);
Josef Bacik44396f42011-05-31 11:58:49 -04001328 if (unlikely(old)) {
1329 dput(dentry);
1330 dentry = old;
1331 }
1332 return dentry;
1333}
1334
Al Viroa3255542012-03-30 14:41:51 -04001335static struct dentry *__lookup_hash(struct qstr *name,
Al Viro72bd8662012-06-10 17:17:17 -04001336 struct dentry *base, unsigned int flags)
Al Viroa3255542012-03-30 14:41:51 -04001337{
Miklos Szeredibad61182012-03-26 12:54:24 +02001338 bool need_lookup;
Al Viroa3255542012-03-30 14:41:51 -04001339 struct dentry *dentry;
1340
Al Viro72bd8662012-06-10 17:17:17 -04001341 dentry = lookup_dcache(name, base, flags, &need_lookup);
Miklos Szeredibad61182012-03-26 12:54:24 +02001342 if (!need_lookup)
1343 return dentry;
Al Viroa3255542012-03-30 14:41:51 -04001344
Al Viro72bd8662012-06-10 17:17:17 -04001345 return lookup_real(base->d_inode, dentry, flags);
Al Viroa3255542012-03-30 14:41:51 -04001346}
1347
Josef Bacik44396f42011-05-31 11:58:49 -04001348/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 * It's more convoluted than I'd like it to be, but... it's still fairly
1350 * small and for now I'd prefer to have fast path as straight as possible.
1351 * It _is_ time-critical.
1352 */
Al Viroe97cdc82013-01-24 18:16:00 -05001353static int lookup_fast(struct nameidata *nd,
Miklos Szeredi697f5142012-05-21 17:30:05 +02001354 struct path *path, struct inode **inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355{
Jan Blunck4ac91372008-02-14 19:34:32 -08001356 struct vfsmount *mnt = nd->path.mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +11001357 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro5a18fff2011-03-11 04:44:53 -05001358 int need_reval = 1;
1359 int status = 1;
David Howells9875cf82011-01-14 18:45:21 +00001360 int err;
1361
Al Viro3cac2602009-08-13 18:27:43 +04001362 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001363 * Rename seqlock is not required here because in the off chance
1364 * of a false negative due to a concurrent rename, we're going to
1365 * do the non-racy lookup, below.
1366 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001367 if (nd->flags & LOOKUP_RCU) {
1368 unsigned seq;
Linus Torvaldsda53be12013-05-21 15:22:44 -07001369 dentry = __d_lookup_rcu(parent, &nd->last, &seq);
Al Viro5a18fff2011-03-11 04:44:53 -05001370 if (!dentry)
1371 goto unlazy;
1372
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001373 /*
1374 * This sequence count validates that the inode matches
1375 * the dentry name information from lookup.
1376 */
1377 *inode = dentry->d_inode;
1378 if (read_seqcount_retry(&dentry->d_seq, seq))
1379 return -ECHILD;
1380
1381 /*
1382 * This sequence count validates that the parent had no
1383 * changes while we did the lookup of the dentry above.
1384 *
1385 * The memory barrier in read_seqcount_begin of child is
1386 * enough, we can use __read_seqcount_retry here.
1387 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001388 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1389 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001390 nd->seq = seq;
Al Viro5a18fff2011-03-11 04:44:53 -05001391
Al Viro24643082011-02-15 01:26:22 -05001392 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
Al Viro4ce16ef32012-06-10 16:10:59 -04001393 status = d_revalidate(dentry, nd->flags);
Al Viro5a18fff2011-03-11 04:44:53 -05001394 if (unlikely(status <= 0)) {
1395 if (status != -ECHILD)
1396 need_reval = 0;
1397 goto unlazy;
1398 }
Al Viro24643082011-02-15 01:26:22 -05001399 }
Nick Piggin31e6b012011-01-07 17:49:52 +11001400 path->mnt = mnt;
1401 path->dentry = dentry;
Al Virod6e9bd22011-05-27 07:03:15 -04001402 if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1403 goto unlazy;
1404 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1405 goto unlazy;
1406 return 0;
Al Viro5a18fff2011-03-11 04:44:53 -05001407unlazy:
Al Viro19660af2011-03-25 10:32:48 -04001408 if (unlazy_walk(nd, dentry))
1409 return -ECHILD;
Al Viro5a18fff2011-03-11 04:44:53 -05001410 } else {
Al Viroe97cdc82013-01-24 18:16:00 -05001411 dentry = __d_lookup(parent, &nd->last);
Nick Piggin31e6b012011-01-07 17:49:52 +11001412 }
Al Viro5a18fff2011-03-11 04:44:53 -05001413
Al Viro81e6f522012-03-30 14:48:04 -04001414 if (unlikely(!dentry))
1415 goto need_lookup;
Al Viro5a18fff2011-03-11 04:44:53 -05001416
Al Viro5a18fff2011-03-11 04:44:53 -05001417 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
Al Viro4ce16ef32012-06-10 16:10:59 -04001418 status = d_revalidate(dentry, nd->flags);
Al Viro5a18fff2011-03-11 04:44:53 -05001419 if (unlikely(status <= 0)) {
1420 if (status < 0) {
1421 dput(dentry);
1422 return status;
1423 }
1424 if (!d_invalidate(dentry)) {
1425 dput(dentry);
Al Viro81e6f522012-03-30 14:48:04 -04001426 goto need_lookup;
Al Viro5a18fff2011-03-11 04:44:53 -05001427 }
1428 }
Miklos Szeredi697f5142012-05-21 17:30:05 +02001429
David Howells9875cf82011-01-14 18:45:21 +00001430 path->mnt = mnt;
1431 path->dentry = dentry;
1432 err = follow_managed(path, nd->flags);
Ian Kent89312212011-01-18 12:06:10 +08001433 if (unlikely(err < 0)) {
1434 path_put_conditional(path, nd);
David Howells9875cf82011-01-14 18:45:21 +00001435 return err;
Ian Kent89312212011-01-18 12:06:10 +08001436 }
Al Viroa3fbbde2011-11-07 21:21:26 +00001437 if (err)
1438 nd->flags |= LOOKUP_JUMPED;
David Howells9875cf82011-01-14 18:45:21 +00001439 *inode = path->dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 return 0;
Al Viro81e6f522012-03-30 14:48:04 -04001441
1442need_lookup:
Miklos Szeredi697f5142012-05-21 17:30:05 +02001443 return 1;
1444}
1445
1446/* Fast lookup failed, do it the slow way */
Al Virocc2a5272013-01-24 18:19:49 -05001447static int lookup_slow(struct nameidata *nd, struct path *path)
Miklos Szeredi697f5142012-05-21 17:30:05 +02001448{
1449 struct dentry *dentry, *parent;
1450 int err;
1451
1452 parent = nd->path.dentry;
Al Viro81e6f522012-03-30 14:48:04 -04001453 BUG_ON(nd->inode != parent->d_inode);
1454
1455 mutex_lock(&parent->d_inode->i_mutex);
Al Virocc2a5272013-01-24 18:19:49 -05001456 dentry = __lookup_hash(&nd->last, parent, nd->flags);
Al Viro81e6f522012-03-30 14:48:04 -04001457 mutex_unlock(&parent->d_inode->i_mutex);
1458 if (IS_ERR(dentry))
1459 return PTR_ERR(dentry);
Miklos Szeredi697f5142012-05-21 17:30:05 +02001460 path->mnt = nd->path.mnt;
1461 path->dentry = dentry;
1462 err = follow_managed(path, nd->flags);
1463 if (unlikely(err < 0)) {
1464 path_put_conditional(path, nd);
1465 return err;
1466 }
1467 if (err)
1468 nd->flags |= LOOKUP_JUMPED;
1469 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470}
1471
Al Viro52094c82011-02-21 21:34:47 -05001472static inline int may_lookup(struct nameidata *nd)
1473{
1474 if (nd->flags & LOOKUP_RCU) {
Al Viro4ad5abb2011-06-20 19:57:03 -04001475 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
Al Viro52094c82011-02-21 21:34:47 -05001476 if (err != -ECHILD)
1477 return err;
Al Viro19660af2011-03-25 10:32:48 -04001478 if (unlazy_walk(nd, NULL))
Al Viro52094c82011-02-21 21:34:47 -05001479 return -ECHILD;
1480 }
Al Viro4ad5abb2011-06-20 19:57:03 -04001481 return inode_permission(nd->inode, MAY_EXEC);
Al Viro52094c82011-02-21 21:34:47 -05001482}
1483
Al Viro9856fa12011-03-04 14:22:06 -05001484static inline int handle_dots(struct nameidata *nd, int type)
1485{
1486 if (type == LAST_DOTDOT) {
1487 if (nd->flags & LOOKUP_RCU) {
1488 if (follow_dotdot_rcu(nd))
1489 return -ECHILD;
1490 } else
1491 follow_dotdot(nd);
1492 }
1493 return 0;
1494}
1495
Al Viro951361f2011-03-04 14:44:37 -05001496static void terminate_walk(struct nameidata *nd)
1497{
1498 if (!(nd->flags & LOOKUP_RCU)) {
1499 path_put(&nd->path);
1500 } else {
1501 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001502 if (!(nd->flags & LOOKUP_ROOT))
1503 nd->root.mnt = NULL;
Al Viro8b61e742013-11-08 12:45:01 -05001504 rcu_read_unlock();
Al Viro951361f2011-03-04 14:44:37 -05001505 }
1506}
1507
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001508/*
1509 * Do we need to follow links? We _really_ want to be able
1510 * to do this check without having to look at inode->i_op,
1511 * so we keep a cache of "no, this doesn't need follow_link"
1512 * for the common case.
1513 */
David Howellsb18825a2013-09-12 19:22:53 +01001514static inline int should_follow_link(struct dentry *dentry, int follow)
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001515{
David Howellsb18825a2013-09-12 19:22:53 +01001516 return unlikely(d_is_symlink(dentry)) ? follow : 0;
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001517}
1518
Al Viroce57dfc2011-03-13 19:58:58 -04001519static inline int walk_component(struct nameidata *nd, struct path *path,
Al Viro21b9b072013-01-24 18:10:25 -05001520 int follow)
Al Viroce57dfc2011-03-13 19:58:58 -04001521{
1522 struct inode *inode;
1523 int err;
1524 /*
1525 * "." and ".." are special - ".." especially so because it has
1526 * to be able to know about the current root directory and
1527 * parent relationships.
1528 */
Al Viro21b9b072013-01-24 18:10:25 -05001529 if (unlikely(nd->last_type != LAST_NORM))
1530 return handle_dots(nd, nd->last_type);
Al Viroe97cdc82013-01-24 18:16:00 -05001531 err = lookup_fast(nd, path, &inode);
Al Viroce57dfc2011-03-13 19:58:58 -04001532 if (unlikely(err)) {
Miklos Szeredi697f5142012-05-21 17:30:05 +02001533 if (err < 0)
1534 goto out_err;
1535
Al Virocc2a5272013-01-24 18:19:49 -05001536 err = lookup_slow(nd, path);
Miklos Szeredi697f5142012-05-21 17:30:05 +02001537 if (err < 0)
1538 goto out_err;
1539
1540 inode = path->dentry->d_inode;
Al Viroce57dfc2011-03-13 19:58:58 -04001541 }
Miklos Szeredi697f5142012-05-21 17:30:05 +02001542 err = -ENOENT;
1543 if (!inode)
1544 goto out_path_put;
1545
David Howellsb18825a2013-09-12 19:22:53 +01001546 if (should_follow_link(path->dentry, follow)) {
Al Viro19660af2011-03-25 10:32:48 -04001547 if (nd->flags & LOOKUP_RCU) {
1548 if (unlikely(unlazy_walk(nd, path->dentry))) {
Miklos Szeredi697f5142012-05-21 17:30:05 +02001549 err = -ECHILD;
1550 goto out_err;
Al Viro19660af2011-03-25 10:32:48 -04001551 }
1552 }
Al Viroce57dfc2011-03-13 19:58:58 -04001553 BUG_ON(inode != path->dentry->d_inode);
1554 return 1;
1555 }
1556 path_to_nameidata(path, nd);
1557 nd->inode = inode;
1558 return 0;
Miklos Szeredi697f5142012-05-21 17:30:05 +02001559
1560out_path_put:
1561 path_to_nameidata(path, nd);
1562out_err:
1563 terminate_walk(nd);
1564 return err;
Al Viroce57dfc2011-03-13 19:58:58 -04001565}
1566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567/*
Al Virob3563792011-03-14 21:54:55 -04001568 * This limits recursive symlink follows to 8, while
1569 * limiting consecutive symlinks to 40.
1570 *
1571 * Without that kind of total limit, nasty chains of consecutive
1572 * symlinks can cause almost arbitrarily long lookups.
1573 */
1574static inline int nested_symlink(struct path *path, struct nameidata *nd)
1575{
1576 int res;
1577
Al Virob3563792011-03-14 21:54:55 -04001578 if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1579 path_put_conditional(path, nd);
1580 path_put(&nd->path);
1581 return -ELOOP;
1582 }
Erez Zadok1a4022f2011-05-21 01:19:59 -04001583 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
Al Virob3563792011-03-14 21:54:55 -04001584
1585 nd->depth++;
1586 current->link_count++;
1587
1588 do {
1589 struct path link = *path;
1590 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04001591
1592 res = follow_link(&link, nd, &cookie);
Al Viro6d7b5aa2012-06-10 04:15:17 -04001593 if (res)
1594 break;
Al Viro21b9b072013-01-24 18:10:25 -05001595 res = walk_component(nd, path, LOOKUP_FOLLOW);
Al Viro574197e2011-03-14 22:20:34 -04001596 put_link(nd, &link, cookie);
Al Virob3563792011-03-14 21:54:55 -04001597 } while (res > 0);
1598
1599 current->link_count--;
1600 nd->depth--;
1601 return res;
1602}
1603
1604/*
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001605 * We can do the critical dentry name comparison and hashing
1606 * operations one word at a time, but we are limited to:
1607 *
1608 * - Architectures with fast unaligned word accesses. We could
1609 * do a "get_unaligned()" if this helps and is sufficiently
1610 * fast.
1611 *
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001612 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1613 * do not trap on the (extremely unlikely) case of a page
1614 * crossing operation.
1615 *
1616 * - Furthermore, we need an efficient 64-bit compile for the
1617 * 64-bit case in order to generate the "number of bytes in
1618 * the final mask". Again, that could be replaced with a
1619 * efficient population count instruction or similar.
1620 */
1621#ifdef CONFIG_DCACHE_WORD_ACCESS
1622
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001623#include <asm/word-at-a-time.h>
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001624
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001625#ifdef CONFIG_64BIT
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001626
1627static inline unsigned int fold_hash(unsigned long hash)
1628{
1629 hash += hash >> (8*sizeof(int));
1630 return hash;
1631}
1632
1633#else /* 32-bit case */
1634
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001635#define fold_hash(x) (x)
1636
1637#endif
1638
1639unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1640{
1641 unsigned long a, mask;
1642 unsigned long hash = 0;
1643
1644 for (;;) {
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001645 a = load_unaligned_zeropad(name);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001646 if (len < sizeof(unsigned long))
1647 break;
1648 hash += a;
Al Virof132c5b2012-03-22 21:59:52 +00001649 hash *= 9;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001650 name += sizeof(unsigned long);
1651 len -= sizeof(unsigned long);
1652 if (!len)
1653 goto done;
1654 }
Will Deacona5c21dc2013-12-12 17:40:21 +00001655 mask = bytemask_from_count(len);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001656 hash += mask & a;
1657done:
1658 return fold_hash(hash);
1659}
1660EXPORT_SYMBOL(full_name_hash);
1661
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001662/*
1663 * Calculate the length and hash of the path component, and
1664 * return the length of the component;
1665 */
1666static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1667{
Linus Torvalds36126f82012-05-26 10:43:17 -07001668 unsigned long a, b, adata, bdata, mask, hash, len;
1669 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001670
1671 hash = a = 0;
1672 len = -sizeof(unsigned long);
1673 do {
1674 hash = (hash + a) * 9;
1675 len += sizeof(unsigned long);
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001676 a = load_unaligned_zeropad(name+len);
Linus Torvalds36126f82012-05-26 10:43:17 -07001677 b = a ^ REPEAT_BYTE('/');
1678 } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001679
Linus Torvalds36126f82012-05-26 10:43:17 -07001680 adata = prep_zero_mask(a, adata, &constants);
1681 bdata = prep_zero_mask(b, bdata, &constants);
1682
1683 mask = create_zero_mask(adata | bdata);
1684
1685 hash += a & zero_bytemask(mask);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001686 *hashp = fold_hash(hash);
1687
Linus Torvalds36126f82012-05-26 10:43:17 -07001688 return len + find_zero(mask);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001689}
1690
1691#else
1692
Linus Torvalds0145acc2012-03-02 14:32:59 -08001693unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1694{
1695 unsigned long hash = init_name_hash();
1696 while (len--)
1697 hash = partial_name_hash(*name++, hash);
1698 return end_name_hash(hash);
1699}
Linus Torvaldsae942ae2012-03-02 19:40:57 -08001700EXPORT_SYMBOL(full_name_hash);
Linus Torvalds0145acc2012-03-02 14:32:59 -08001701
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001702/*
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001703 * We know there's a real path component here of at least
1704 * one character.
1705 */
1706static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1707{
1708 unsigned long hash = init_name_hash();
1709 unsigned long len = 0, c;
1710
1711 c = (unsigned char)*name;
1712 do {
1713 len++;
1714 hash = partial_name_hash(c, hash);
1715 c = (unsigned char)name[len];
1716 } while (c && c != '/');
1717 *hashp = end_name_hash(hash);
1718 return len;
1719}
1720
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001721#endif
1722
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001723/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01001725 * This is the basic name resolution function, turning a pathname into
1726 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01001728 * Returns 0 and nd will have valid dentry and mnt on success.
1729 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 */
Al Viro6de88d72009-08-09 01:41:57 +04001731static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732{
1733 struct path next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
1736 while (*name=='/')
1737 name++;
1738 if (!*name)
Al Viro086e1832011-02-22 20:56:27 -05001739 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 /* At this point we know we have a real path component. */
1742 for(;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 struct qstr this;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001744 long len;
Al Virofe479a52011-02-22 15:10:03 -05001745 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Al Viro52094c82011-02-21 21:34:47 -05001747 err = may_lookup(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 if (err)
1749 break;
1750
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001751 len = hash_name(name, &this.hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 this.name = name;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001753 this.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Al Virofe479a52011-02-22 15:10:03 -05001755 type = LAST_NORM;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001756 if (name[0] == '.') switch (len) {
Al Virofe479a52011-02-22 15:10:03 -05001757 case 2:
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001758 if (name[1] == '.') {
Al Virofe479a52011-02-22 15:10:03 -05001759 type = LAST_DOTDOT;
Al Viro16c2cd72011-02-22 15:50:10 -05001760 nd->flags |= LOOKUP_JUMPED;
1761 }
Al Virofe479a52011-02-22 15:10:03 -05001762 break;
1763 case 1:
1764 type = LAST_DOT;
1765 }
Al Viro5a202bc2011-03-08 14:17:44 -05001766 if (likely(type == LAST_NORM)) {
1767 struct dentry *parent = nd->path.dentry;
Al Viro16c2cd72011-02-22 15:50:10 -05001768 nd->flags &= ~LOOKUP_JUMPED;
Al Viro5a202bc2011-03-08 14:17:44 -05001769 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
Linus Torvaldsda53be12013-05-21 15:22:44 -07001770 err = parent->d_op->d_hash(parent, &this);
Al Viro5a202bc2011-03-08 14:17:44 -05001771 if (err < 0)
1772 break;
1773 }
1774 }
Al Virofe479a52011-02-22 15:10:03 -05001775
Al Viro5f4a6a62013-01-24 18:04:22 -05001776 nd->last = this;
1777 nd->last_type = type;
1778
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001779 if (!name[len])
Al Viro5f4a6a62013-01-24 18:04:22 -05001780 return 0;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001781 /*
1782 * If it wasn't NUL, we know it was '/'. Skip that
1783 * slash, and continue until no more slashes.
1784 */
1785 do {
1786 len++;
1787 } while (unlikely(name[len] == '/'));
1788 if (!name[len])
Al Viro5f4a6a62013-01-24 18:04:22 -05001789 return 0;
1790
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001791 name += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Al Viro21b9b072013-01-24 18:10:25 -05001793 err = walk_component(nd, &next, LOOKUP_FOLLOW);
Al Viroce57dfc2011-03-13 19:58:58 -04001794 if (err < 0)
1795 return err;
Al Virofe479a52011-02-22 15:10:03 -05001796
Al Viroce57dfc2011-03-13 19:58:58 -04001797 if (err) {
Al Virob3563792011-03-14 21:54:55 -04001798 err = nested_symlink(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 if (err)
Al Viroa7472ba2011-03-04 14:39:30 -05001800 return err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001801 }
David Howellsb18825a2013-09-12 19:22:53 +01001802 if (!d_is_directory(nd->path.dentry)) {
Al Viro5f4a6a62013-01-24 18:04:22 -05001803 err = -ENOTDIR;
1804 break;
1805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 }
Al Viro951361f2011-03-04 14:44:37 -05001807 terminate_walk(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 return err;
1809}
1810
Al Viro70e9b352011-03-05 21:12:22 -05001811static int path_init(int dfd, const char *name, unsigned int flags,
1812 struct nameidata *nd, struct file **fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813{
Prasanna Medaea3834d2005-04-29 16:00:17 +01001814 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
1816 nd->last_type = LAST_ROOT; /* if there are only slashes... */
Al Viro16c2cd72011-02-22 15:50:10 -05001817 nd->flags = flags | LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 nd->depth = 0;
Al Viro5b6ca022011-03-09 23:04:47 -05001819 if (flags & LOOKUP_ROOT) {
David Howellsb18825a2013-09-12 19:22:53 +01001820 struct dentry *root = nd->root.dentry;
1821 struct inode *inode = root->d_inode;
Al Viro73d049a2011-03-11 12:08:24 -05001822 if (*name) {
David Howellsb18825a2013-09-12 19:22:53 +01001823 if (!d_is_directory(root))
Al Viro73d049a2011-03-11 12:08:24 -05001824 return -ENOTDIR;
1825 retval = inode_permission(inode, MAY_EXEC);
1826 if (retval)
1827 return retval;
1828 }
Al Viro5b6ca022011-03-09 23:04:47 -05001829 nd->path = nd->root;
1830 nd->inode = inode;
1831 if (flags & LOOKUP_RCU) {
Al Viro8b61e742013-11-08 12:45:01 -05001832 rcu_read_lock();
Al Viro5b6ca022011-03-09 23:04:47 -05001833 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro48a066e2013-09-29 22:06:07 -04001834 nd->m_seq = read_seqbegin(&mount_lock);
Al Viro5b6ca022011-03-09 23:04:47 -05001835 } else {
1836 path_get(&nd->path);
1837 }
1838 return 0;
1839 }
1840
Al Viro2a737872009-04-07 11:49:53 -04001841 nd->root.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
Al Viro48a066e2013-09-29 22:06:07 -04001843 nd->m_seq = read_seqbegin(&mount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 if (*name=='/') {
Al Viroe41f7d42011-02-22 14:02:58 -05001845 if (flags & LOOKUP_RCU) {
Al Viro8b61e742013-11-08 12:45:01 -05001846 rcu_read_lock();
Al Viroe41f7d42011-02-22 14:02:58 -05001847 set_root_rcu(nd);
1848 } else {
1849 set_root(nd);
1850 path_get(&nd->root);
1851 }
Al Viro2a737872009-04-07 11:49:53 -04001852 nd->path = nd->root;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001853 } else if (dfd == AT_FDCWD) {
Al Viroe41f7d42011-02-22 14:02:58 -05001854 if (flags & LOOKUP_RCU) {
1855 struct fs_struct *fs = current->fs;
1856 unsigned seq;
1857
Al Viro8b61e742013-11-08 12:45:01 -05001858 rcu_read_lock();
Al Viroe41f7d42011-02-22 14:02:58 -05001859
1860 do {
1861 seq = read_seqcount_begin(&fs->seq);
1862 nd->path = fs->pwd;
1863 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1864 } while (read_seqcount_retry(&fs->seq, seq));
1865 } else {
1866 get_fs_pwd(current->fs, &nd->path);
1867 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001868 } else {
Jeff Layton582aa642012-12-11 08:56:16 -05001869 /* Caller must check execute permissions on the starting path component */
Al Viro2903ff02012-08-28 12:52:22 -04001870 struct fd f = fdget_raw(dfd);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001871 struct dentry *dentry;
1872
Al Viro2903ff02012-08-28 12:52:22 -04001873 if (!f.file)
1874 return -EBADF;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001875
Al Viro2903ff02012-08-28 12:52:22 -04001876 dentry = f.file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001877
Al Virof52e0c12011-03-14 18:56:51 -04001878 if (*name) {
David Howellsb18825a2013-09-12 19:22:53 +01001879 if (!d_is_directory(dentry)) {
Al Viro2903ff02012-08-28 12:52:22 -04001880 fdput(f);
1881 return -ENOTDIR;
1882 }
Al Virof52e0c12011-03-14 18:56:51 -04001883 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001884
Al Viro2903ff02012-08-28 12:52:22 -04001885 nd->path = f.file->f_path;
Al Viroe41f7d42011-02-22 14:02:58 -05001886 if (flags & LOOKUP_RCU) {
Al Viro2903ff02012-08-28 12:52:22 -04001887 if (f.need_put)
1888 *fp = f.file;
Al Viroe41f7d42011-02-22 14:02:58 -05001889 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro8b61e742013-11-08 12:45:01 -05001890 rcu_read_lock();
Al Viroe41f7d42011-02-22 14:02:58 -05001891 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001892 path_get(&nd->path);
1893 fdput(f);
Al Viroe41f7d42011-02-22 14:02:58 -05001894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 }
Al Viroe41f7d42011-02-22 14:02:58 -05001896
Nick Piggin31e6b012011-01-07 17:49:52 +11001897 nd->inode = nd->path.dentry->d_inode;
Al Viro9b4a9b12009-04-07 11:44:16 -04001898 return 0;
Al Viro9b4a9b12009-04-07 11:44:16 -04001899}
1900
Al Virobd92d7f2011-03-14 19:54:59 -04001901static inline int lookup_last(struct nameidata *nd, struct path *path)
1902{
1903 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1904 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1905
1906 nd->flags &= ~LOOKUP_PARENT;
Al Viro21b9b072013-01-24 18:10:25 -05001907 return walk_component(nd, path, nd->flags & LOOKUP_FOLLOW);
Al Virobd92d7f2011-03-14 19:54:59 -04001908}
1909
Al Viro9b4a9b12009-04-07 11:44:16 -04001910/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroee0827c2011-02-21 23:38:09 -05001911static int path_lookupat(int dfd, const char *name,
Al Viro9b4a9b12009-04-07 11:44:16 -04001912 unsigned int flags, struct nameidata *nd)
1913{
Al Viro70e9b352011-03-05 21:12:22 -05001914 struct file *base = NULL;
Al Virobd92d7f2011-03-14 19:54:59 -04001915 struct path path;
1916 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001917
1918 /*
1919 * Path walking is largely split up into 2 different synchronisation
1920 * schemes, rcu-walk and ref-walk (explained in
1921 * Documentation/filesystems/path-lookup.txt). These share much of the
1922 * path walk code, but some things particularly setup, cleanup, and
1923 * following mounts are sufficiently divergent that functions are
1924 * duplicated. Typically there is a function foo(), and its RCU
1925 * analogue, foo_rcu().
1926 *
1927 * -ECHILD is the error number of choice (just to avoid clashes) that
1928 * is returned if some aspect of an rcu-walk fails. Such an error must
1929 * be handled by restarting a traditional ref-walk (which will always
1930 * be able to complete).
1931 */
Al Virobd92d7f2011-03-14 19:54:59 -04001932 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
Al Viroee0827c2011-02-21 23:38:09 -05001933
Al Virobd92d7f2011-03-14 19:54:59 -04001934 if (unlikely(err))
1935 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001936
1937 current->total_link_count = 0;
Al Virobd92d7f2011-03-14 19:54:59 -04001938 err = link_path_walk(name, nd);
1939
1940 if (!err && !(flags & LOOKUP_PARENT)) {
Al Virobd92d7f2011-03-14 19:54:59 -04001941 err = lookup_last(nd, &path);
1942 while (err > 0) {
1943 void *cookie;
1944 struct path link = path;
Kees Cook800179c2012-07-25 17:29:07 -07001945 err = may_follow_link(&link, nd);
1946 if (unlikely(err))
1947 break;
Al Virobd92d7f2011-03-14 19:54:59 -04001948 nd->flags |= LOOKUP_PARENT;
Al Viro574197e2011-03-14 22:20:34 -04001949 err = follow_link(&link, nd, &cookie);
Al Viro6d7b5aa2012-06-10 04:15:17 -04001950 if (err)
1951 break;
1952 err = lookup_last(nd, &path);
Al Viro574197e2011-03-14 22:20:34 -04001953 put_link(nd, &link, cookie);
Al Virobd92d7f2011-03-14 19:54:59 -04001954 }
1955 }
Al Viroee0827c2011-02-21 23:38:09 -05001956
Al Viro9f1fafe2011-03-25 11:00:12 -04001957 if (!err)
1958 err = complete_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04001959
1960 if (!err && nd->flags & LOOKUP_DIRECTORY) {
David Howellsb18825a2013-09-12 19:22:53 +01001961 if (!d_is_directory(nd->path.dentry)) {
Al Virobd92d7f2011-03-14 19:54:59 -04001962 path_put(&nd->path);
Al Virobd23a532011-03-23 09:56:30 -04001963 err = -ENOTDIR;
Al Virobd92d7f2011-03-14 19:54:59 -04001964 }
1965 }
Al Viro16c2cd72011-02-22 15:50:10 -05001966
Al Viro70e9b352011-03-05 21:12:22 -05001967 if (base)
1968 fput(base);
Al Viroee0827c2011-02-21 23:38:09 -05001969
Al Viro5b6ca022011-03-09 23:04:47 -05001970 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
Al Viro2a737872009-04-07 11:49:53 -04001971 path_put(&nd->root);
1972 nd->root.mnt = NULL;
1973 }
Al Virobd92d7f2011-03-14 19:54:59 -04001974 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001975}
Nick Piggin31e6b012011-01-07 17:49:52 +11001976
Jeff Layton873f1ee2012-10-10 15:25:29 -04001977static int filename_lookup(int dfd, struct filename *name,
1978 unsigned int flags, struct nameidata *nd)
1979{
1980 int retval = path_lookupat(dfd, name->name, flags | LOOKUP_RCU, nd);
1981 if (unlikely(retval == -ECHILD))
1982 retval = path_lookupat(dfd, name->name, flags, nd);
1983 if (unlikely(retval == -ESTALE))
1984 retval = path_lookupat(dfd, name->name,
1985 flags | LOOKUP_REVAL, nd);
1986
1987 if (likely(!retval))
Jeff Laytonadb5c242012-10-10 16:43:13 -04001988 audit_inode(name, nd->path.dentry, flags & LOOKUP_PARENT);
Jeff Layton873f1ee2012-10-10 15:25:29 -04001989 return retval;
1990}
1991
Al Viroee0827c2011-02-21 23:38:09 -05001992static int do_path_lookup(int dfd, const char *name,
1993 unsigned int flags, struct nameidata *nd)
1994{
Jeff Layton873f1ee2012-10-10 15:25:29 -04001995 struct filename filename = { .name = name };
Nick Piggin31e6b012011-01-07 17:49:52 +11001996
Jeff Layton873f1ee2012-10-10 15:25:29 -04001997 return filename_lookup(dfd, &filename, flags, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998}
1999
Al Viro79714f72012-06-15 03:01:42 +04002000/* does lookup, returns the object with parent locked */
2001struct dentry *kern_path_locked(const char *name, struct path *path)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002002{
Al Viro79714f72012-06-15 03:01:42 +04002003 struct nameidata nd;
2004 struct dentry *d;
2005 int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd);
2006 if (err)
2007 return ERR_PTR(err);
2008 if (nd.last_type != LAST_NORM) {
2009 path_put(&nd.path);
2010 return ERR_PTR(-EINVAL);
2011 }
2012 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Al Viro1e0ea002012-07-22 23:46:21 +04002013 d = __lookup_hash(&nd.last, nd.path.dentry, 0);
Al Viro79714f72012-06-15 03:01:42 +04002014 if (IS_ERR(d)) {
2015 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2016 path_put(&nd.path);
2017 return d;
2018 }
2019 *path = nd.path;
2020 return d;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002021}
2022
Al Virod1811462008-08-02 00:49:18 -04002023int kern_path(const char *name, unsigned int flags, struct path *path)
2024{
2025 struct nameidata nd;
2026 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
2027 if (!res)
2028 *path = nd.path;
2029 return res;
2030}
2031
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002032/**
2033 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2034 * @dentry: pointer to dentry of the base directory
2035 * @mnt: pointer to vfs mount of the base directory
2036 * @name: pointer to file name
2037 * @flags: lookup flags
Al Viroe0a01242011-06-27 17:00:37 -04002038 * @path: pointer to struct path to fill
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002039 */
2040int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2041 const char *name, unsigned int flags,
Al Viroe0a01242011-06-27 17:00:37 -04002042 struct path *path)
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002043{
Al Viroe0a01242011-06-27 17:00:37 -04002044 struct nameidata nd;
2045 int err;
2046 nd.root.dentry = dentry;
2047 nd.root.mnt = mnt;
2048 BUG_ON(flags & LOOKUP_PARENT);
Al Viro5b6ca022011-03-09 23:04:47 -05002049 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
Al Viroe0a01242011-06-27 17:00:37 -04002050 err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
2051 if (!err)
2052 *path = nd.path;
2053 return err;
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002054}
2055
James Morris057f6c02007-04-26 00:12:05 -07002056/*
2057 * Restricted form of lookup. Doesn't follow links, single-component only,
2058 * needs parent already locked. Doesn't follow mounts.
2059 * SMP-safe.
2060 */
Adrian Bunka244e162006-03-31 02:32:11 -08002061static struct dentry *lookup_hash(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062{
Al Viro72bd8662012-06-10 17:17:17 -04002063 return __lookup_hash(&nd->last, nd->path.dentry, nd->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064}
2065
Christoph Hellwigeead1912007-10-16 23:25:38 -07002066/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07002067 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07002068 * @name: pathname component to lookup
2069 * @base: base directory to lookup from
2070 * @len: maximum length @len should be interpreted to
2071 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07002072 * Note that this routine is purely a helper for filesystem usage and should
2073 * not be called by generic code. Also note that by using this function the
Christoph Hellwigeead1912007-10-16 23:25:38 -07002074 * nameidata argument is passed to the filesystem methods and a filesystem
2075 * using this helper needs to be prepared for that.
2076 */
James Morris057f6c02007-04-26 00:12:05 -07002077struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2078{
James Morris057f6c02007-04-26 00:12:05 -07002079 struct qstr this;
Al Viro6a96ba52011-03-07 23:49:20 -05002080 unsigned int c;
Miklos Szeredicda309d2012-03-26 12:54:21 +02002081 int err;
James Morris057f6c02007-04-26 00:12:05 -07002082
David Woodhouse2f9092e2009-04-20 23:18:37 +01002083 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
2084
Al Viro6a96ba52011-03-07 23:49:20 -05002085 this.name = name;
2086 this.len = len;
Linus Torvalds0145acc2012-03-02 14:32:59 -08002087 this.hash = full_name_hash(name, len);
Al Viro6a96ba52011-03-07 23:49:20 -05002088 if (!len)
2089 return ERR_PTR(-EACCES);
2090
Al Viro21d8a152012-11-29 22:17:21 -05002091 if (unlikely(name[0] == '.')) {
2092 if (len < 2 || (len == 2 && name[1] == '.'))
2093 return ERR_PTR(-EACCES);
2094 }
2095
Al Viro6a96ba52011-03-07 23:49:20 -05002096 while (len--) {
2097 c = *(const unsigned char *)name++;
2098 if (c == '/' || c == '\0')
2099 return ERR_PTR(-EACCES);
Al Viro6a96ba52011-03-07 23:49:20 -05002100 }
Al Viro5a202bc2011-03-08 14:17:44 -05002101 /*
2102 * See if the low-level filesystem might want
2103 * to use its own hash..
2104 */
2105 if (base->d_flags & DCACHE_OP_HASH) {
Linus Torvaldsda53be12013-05-21 15:22:44 -07002106 int err = base->d_op->d_hash(base, &this);
Al Viro5a202bc2011-03-08 14:17:44 -05002107 if (err < 0)
2108 return ERR_PTR(err);
2109 }
Christoph Hellwigeead1912007-10-16 23:25:38 -07002110
Miklos Szeredicda309d2012-03-26 12:54:21 +02002111 err = inode_permission(base->d_inode, MAY_EXEC);
2112 if (err)
2113 return ERR_PTR(err);
2114
Al Viro72bd8662012-06-10 17:17:17 -04002115 return __lookup_hash(&this, base, 0);
James Morris057f6c02007-04-26 00:12:05 -07002116}
2117
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002118int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2119 struct path *path, int *empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120{
Al Viro2d8f3032008-07-22 09:59:21 -04002121 struct nameidata nd;
Jeff Layton91a27b22012-10-10 15:25:28 -04002122 struct filename *tmp = getname_flags(name, flags, empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 int err = PTR_ERR(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 if (!IS_ERR(tmp)) {
Al Viro2d8f3032008-07-22 09:59:21 -04002125
2126 BUG_ON(flags & LOOKUP_PARENT);
2127
Jeff Layton873f1ee2012-10-10 15:25:29 -04002128 err = filename_lookup(dfd, tmp, flags, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 putname(tmp);
Al Viro2d8f3032008-07-22 09:59:21 -04002130 if (!err)
2131 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 }
2133 return err;
2134}
2135
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002136int user_path_at(int dfd, const char __user *name, unsigned flags,
2137 struct path *path)
2138{
Linus Torvaldsf7493e52012-03-22 16:10:40 -07002139 return user_path_at_empty(dfd, name, flags, path, NULL);
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002140}
2141
Jeff Layton873f1ee2012-10-10 15:25:29 -04002142/*
2143 * NB: most callers don't do anything directly with the reference to the
2144 * to struct filename, but the nd->last pointer points into the name string
2145 * allocated by getname. So we must hold the reference to it until all
2146 * path-walking is complete.
2147 */
Jeff Layton91a27b22012-10-10 15:25:28 -04002148static struct filename *
Jeff Layton9e790bd2012-12-11 12:10:09 -05002149user_path_parent(int dfd, const char __user *path, struct nameidata *nd,
2150 unsigned int flags)
Al Viro2ad94ae2008-07-21 09:32:51 -04002151{
Jeff Layton91a27b22012-10-10 15:25:28 -04002152 struct filename *s = getname(path);
Al Viro2ad94ae2008-07-21 09:32:51 -04002153 int error;
2154
Jeff Layton9e790bd2012-12-11 12:10:09 -05002155 /* only LOOKUP_REVAL is allowed in extra flags */
2156 flags &= LOOKUP_REVAL;
2157
Al Viro2ad94ae2008-07-21 09:32:51 -04002158 if (IS_ERR(s))
Jeff Layton91a27b22012-10-10 15:25:28 -04002159 return s;
Al Viro2ad94ae2008-07-21 09:32:51 -04002160
Jeff Layton9e790bd2012-12-11 12:10:09 -05002161 error = filename_lookup(dfd, s, flags | LOOKUP_PARENT, nd);
Jeff Layton91a27b22012-10-10 15:25:28 -04002162 if (error) {
Al Viro2ad94ae2008-07-21 09:32:51 -04002163 putname(s);
Jeff Layton91a27b22012-10-10 15:25:28 -04002164 return ERR_PTR(error);
2165 }
Al Viro2ad94ae2008-07-21 09:32:51 -04002166
Jeff Layton91a27b22012-10-10 15:25:28 -04002167 return s;
Al Viro2ad94ae2008-07-21 09:32:51 -04002168}
2169
Jeff Layton80334262013-07-26 06:23:25 -04002170/**
Al Viro197df042013-09-08 14:03:27 -04002171 * mountpoint_last - look up last component for umount
Jeff Layton80334262013-07-26 06:23:25 -04002172 * @nd: pathwalk nameidata - currently pointing at parent directory of "last"
2173 * @path: pointer to container for result
2174 *
2175 * This is a special lookup_last function just for umount. In this case, we
2176 * need to resolve the path without doing any revalidation.
2177 *
2178 * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
2179 * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
2180 * in almost all cases, this lookup will be served out of the dcache. The only
2181 * cases where it won't are if nd->last refers to a symlink or the path is
2182 * bogus and it doesn't exist.
2183 *
2184 * Returns:
2185 * -error: if there was an error during lookup. This includes -ENOENT if the
2186 * lookup found a negative dentry. The nd->path reference will also be
2187 * put in this case.
2188 *
2189 * 0: if we successfully resolved nd->path and found it to not to be a
2190 * symlink that needs to be followed. "path" will also be populated.
2191 * The nd->path reference will also be put.
2192 *
2193 * 1: if we successfully resolved nd->last and found it to be a symlink
2194 * that needs to be followed. "path" will be populated with the path
2195 * to the link, and nd->path will *not* be put.
2196 */
2197static int
Al Viro197df042013-09-08 14:03:27 -04002198mountpoint_last(struct nameidata *nd, struct path *path)
Jeff Layton80334262013-07-26 06:23:25 -04002199{
2200 int error = 0;
2201 struct dentry *dentry;
2202 struct dentry *dir = nd->path.dentry;
2203
Al Viro35759522013-09-08 13:41:33 -04002204 /* If we're in rcuwalk, drop out of it to handle last component */
2205 if (nd->flags & LOOKUP_RCU) {
2206 if (unlazy_walk(nd, NULL)) {
2207 error = -ECHILD;
2208 goto out;
2209 }
Jeff Layton80334262013-07-26 06:23:25 -04002210 }
2211
2212 nd->flags &= ~LOOKUP_PARENT;
2213
2214 if (unlikely(nd->last_type != LAST_NORM)) {
2215 error = handle_dots(nd, nd->last_type);
Al Viro35759522013-09-08 13:41:33 -04002216 if (error)
2217 goto out;
2218 dentry = dget(nd->path.dentry);
2219 goto done;
Jeff Layton80334262013-07-26 06:23:25 -04002220 }
2221
2222 mutex_lock(&dir->d_inode->i_mutex);
2223 dentry = d_lookup(dir, &nd->last);
2224 if (!dentry) {
2225 /*
2226 * No cached dentry. Mounted dentries are pinned in the cache,
2227 * so that means that this dentry is probably a symlink or the
2228 * path doesn't actually point to a mounted dentry.
2229 */
2230 dentry = d_alloc(dir, &nd->last);
2231 if (!dentry) {
2232 error = -ENOMEM;
Dave Jonesbcceeeb2013-09-10 17:04:25 -04002233 mutex_unlock(&dir->d_inode->i_mutex);
Al Viro35759522013-09-08 13:41:33 -04002234 goto out;
Jeff Layton80334262013-07-26 06:23:25 -04002235 }
Al Viro35759522013-09-08 13:41:33 -04002236 dentry = lookup_real(dir->d_inode, dentry, nd->flags);
2237 error = PTR_ERR(dentry);
Dave Jonesbcceeeb2013-09-10 17:04:25 -04002238 if (IS_ERR(dentry)) {
2239 mutex_unlock(&dir->d_inode->i_mutex);
Al Viro35759522013-09-08 13:41:33 -04002240 goto out;
Dave Jonesbcceeeb2013-09-10 17:04:25 -04002241 }
Jeff Layton80334262013-07-26 06:23:25 -04002242 }
2243 mutex_unlock(&dir->d_inode->i_mutex);
2244
Al Viro35759522013-09-08 13:41:33 -04002245done:
2246 if (!dentry->d_inode) {
2247 error = -ENOENT;
2248 dput(dentry);
2249 goto out;
Jeff Layton80334262013-07-26 06:23:25 -04002250 }
Al Viro35759522013-09-08 13:41:33 -04002251 path->dentry = dentry;
2252 path->mnt = mntget(nd->path.mnt);
David Howellsb18825a2013-09-12 19:22:53 +01002253 if (should_follow_link(dentry, nd->flags & LOOKUP_FOLLOW))
Al Viro35759522013-09-08 13:41:33 -04002254 return 1;
2255 follow_mount(path);
2256 error = 0;
2257out:
Jeff Layton80334262013-07-26 06:23:25 -04002258 terminate_walk(nd);
2259 return error;
2260}
2261
2262/**
Al Viro197df042013-09-08 14:03:27 -04002263 * path_mountpoint - look up a path to be umounted
Jeff Layton80334262013-07-26 06:23:25 -04002264 * @dfd: directory file descriptor to start walk from
2265 * @name: full pathname to walk
Randy Dunlap606d6fe2013-10-19 14:56:55 -07002266 * @path: pointer to container for result
Jeff Layton80334262013-07-26 06:23:25 -04002267 * @flags: lookup flags
Jeff Layton80334262013-07-26 06:23:25 -04002268 *
2269 * Look up the given name, but don't attempt to revalidate the last component.
Randy Dunlap606d6fe2013-10-19 14:56:55 -07002270 * Returns 0 and "path" will be valid on success; Returns error otherwise.
Jeff Layton80334262013-07-26 06:23:25 -04002271 */
2272static int
Al Viro197df042013-09-08 14:03:27 -04002273path_mountpoint(int dfd, const char *name, struct path *path, unsigned int flags)
Jeff Layton80334262013-07-26 06:23:25 -04002274{
2275 struct file *base = NULL;
2276 struct nameidata nd;
2277 int err;
2278
2279 err = path_init(dfd, name, flags | LOOKUP_PARENT, &nd, &base);
2280 if (unlikely(err))
2281 return err;
2282
2283 current->total_link_count = 0;
2284 err = link_path_walk(name, &nd);
2285 if (err)
2286 goto out;
2287
Al Viro197df042013-09-08 14:03:27 -04002288 err = mountpoint_last(&nd, path);
Jeff Layton80334262013-07-26 06:23:25 -04002289 while (err > 0) {
2290 void *cookie;
2291 struct path link = *path;
2292 err = may_follow_link(&link, &nd);
2293 if (unlikely(err))
2294 break;
2295 nd.flags |= LOOKUP_PARENT;
2296 err = follow_link(&link, &nd, &cookie);
2297 if (err)
2298 break;
Al Viro197df042013-09-08 14:03:27 -04002299 err = mountpoint_last(&nd, path);
Jeff Layton80334262013-07-26 06:23:25 -04002300 put_link(&nd, &link, cookie);
2301 }
2302out:
2303 if (base)
2304 fput(base);
2305
2306 if (nd.root.mnt && !(nd.flags & LOOKUP_ROOT))
2307 path_put(&nd.root);
2308
2309 return err;
2310}
2311
Al Viro2d864652013-09-08 20:18:44 -04002312static int
2313filename_mountpoint(int dfd, struct filename *s, struct path *path,
2314 unsigned int flags)
2315{
2316 int error = path_mountpoint(dfd, s->name, path, flags | LOOKUP_RCU);
2317 if (unlikely(error == -ECHILD))
2318 error = path_mountpoint(dfd, s->name, path, flags);
2319 if (unlikely(error == -ESTALE))
2320 error = path_mountpoint(dfd, s->name, path, flags | LOOKUP_REVAL);
2321 if (likely(!error))
2322 audit_inode(s, path->dentry, 0);
2323 return error;
2324}
2325
Jeff Layton80334262013-07-26 06:23:25 -04002326/**
Al Viro197df042013-09-08 14:03:27 -04002327 * user_path_mountpoint_at - lookup a path from userland in order to umount it
Jeff Layton80334262013-07-26 06:23:25 -04002328 * @dfd: directory file descriptor
2329 * @name: pathname from userland
2330 * @flags: lookup flags
2331 * @path: pointer to container to hold result
2332 *
2333 * A umount is a special case for path walking. We're not actually interested
2334 * in the inode in this situation, and ESTALE errors can be a problem. We
2335 * simply want track down the dentry and vfsmount attached at the mountpoint
2336 * and avoid revalidating the last component.
2337 *
2338 * Returns 0 and populates "path" on success.
2339 */
2340int
Al Viro197df042013-09-08 14:03:27 -04002341user_path_mountpoint_at(int dfd, const char __user *name, unsigned int flags,
Jeff Layton80334262013-07-26 06:23:25 -04002342 struct path *path)
2343{
2344 struct filename *s = getname(name);
2345 int error;
Jeff Layton80334262013-07-26 06:23:25 -04002346 if (IS_ERR(s))
2347 return PTR_ERR(s);
Al Viro2d864652013-09-08 20:18:44 -04002348 error = filename_mountpoint(dfd, s, path, flags);
Jeff Layton80334262013-07-26 06:23:25 -04002349 putname(s);
2350 return error;
2351}
2352
Al Viro2d864652013-09-08 20:18:44 -04002353int
2354kern_path_mountpoint(int dfd, const char *name, struct path *path,
2355 unsigned int flags)
2356{
2357 struct filename s = {.name = name};
2358 return filename_mountpoint(dfd, &s, path, flags);
2359}
2360EXPORT_SYMBOL(kern_path_mountpoint);
2361
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362/*
2363 * It's inline, so penalty for filesystems that don't use sticky bit is
2364 * minimal.
2365 */
2366static inline int check_sticky(struct inode *dir, struct inode *inode)
2367{
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002368 kuid_t fsuid = current_fsuid();
David Howellsda9592e2008-11-14 10:39:05 +11002369
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 if (!(dir->i_mode & S_ISVTX))
2371 return 0;
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002372 if (uid_eq(inode->i_uid, fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 return 0;
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002374 if (uid_eq(dir->i_uid, fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 return 0;
Eric W. Biederman1a48e2a2011-11-14 16:24:06 -08002376 return !inode_capable(inode, CAP_FOWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377}
2378
2379/*
2380 * Check whether we can remove a link victim from directory dir, check
2381 * whether the type of victim is right.
2382 * 1. We can't do it if dir is read-only (done in permission())
2383 * 2. We should have write and exec permissions on dir
2384 * 3. We can't remove anything from append-only dir
2385 * 4. We can't do anything with immutable dir (done in permission())
2386 * 5. If the sticky bit on dir is set we should either
2387 * a. be owner of dir, or
2388 * b. be owner of victim, or
2389 * c. have CAP_FOWNER capability
2390 * 6. If the victim is append-only or immutable we can't do antyhing with
2391 * links pointing to it.
2392 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2393 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2394 * 9. We can't remove a root or mountpoint.
2395 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
2396 * nfs_async_unlink().
2397 */
David Howellsb18825a2013-09-12 19:22:53 +01002398static int may_delete(struct inode *dir, struct dentry *victim, bool isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399{
David Howellsb18825a2013-09-12 19:22:53 +01002400 struct inode *inode = victim->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 int error;
2402
David Howellsb18825a2013-09-12 19:22:53 +01002403 if (d_is_negative(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 return -ENOENT;
David Howellsb18825a2013-09-12 19:22:53 +01002405 BUG_ON(!inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
2407 BUG_ON(victim->d_parent->d_inode != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04002408 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
Al Virof419a2e2008-07-22 00:07:17 -04002410 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 if (error)
2412 return error;
2413 if (IS_APPEND(dir))
2414 return -EPERM;
David Howellsb18825a2013-09-12 19:22:53 +01002415
2416 if (check_sticky(dir, inode) || IS_APPEND(inode) ||
2417 IS_IMMUTABLE(inode) || IS_SWAPFILE(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 return -EPERM;
2419 if (isdir) {
David Howellsb18825a2013-09-12 19:22:53 +01002420 if (!d_is_directory(victim) && !d_is_autodir(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 return -ENOTDIR;
2422 if (IS_ROOT(victim))
2423 return -EBUSY;
David Howellsb18825a2013-09-12 19:22:53 +01002424 } else if (d_is_directory(victim) || d_is_autodir(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 return -EISDIR;
2426 if (IS_DEADDIR(dir))
2427 return -ENOENT;
2428 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2429 return -EBUSY;
2430 return 0;
2431}
2432
2433/* Check whether we can create an object with dentry child in directory
2434 * dir.
2435 * 1. We can't do it if child already exists (open has special treatment for
2436 * this case, but since we are inlined it's OK)
2437 * 2. We can't do it if dir is read-only (done in permission())
2438 * 3. We should have write and exec permissions on dir
2439 * 4. We can't do it if dir is immutable (done in permission())
2440 */
Miklos Szeredia95164d2008-07-30 15:08:48 +02002441static inline int may_create(struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442{
Jeff Layton14e972b2013-05-08 10:25:58 -04002443 audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 if (child->d_inode)
2445 return -EEXIST;
2446 if (IS_DEADDIR(dir))
2447 return -ENOENT;
Al Virof419a2e2008-07-22 00:07:17 -04002448 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449}
2450
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451/*
2452 * p1 and p2 should be directories on the same fs.
2453 */
2454struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2455{
2456 struct dentry *p;
2457
2458 if (p1 == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07002459 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 return NULL;
2461 }
2462
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002463 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002465 p = d_ancestor(p2, p1);
2466 if (p) {
2467 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2468 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
2469 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 }
2471
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002472 p = d_ancestor(p1, p2);
2473 if (p) {
2474 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2475 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2476 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 }
2478
Ingo Molnarf2eace22006-07-03 00:25:05 -07002479 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2480 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 return NULL;
2482}
2483
2484void unlock_rename(struct dentry *p1, struct dentry *p2)
2485{
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002486 mutex_unlock(&p1->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 if (p1 != p2) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002488 mutex_unlock(&p2->d_inode->i_mutex);
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002489 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 }
2491}
2492
Al Viro4acdaf22011-07-26 01:42:34 -04002493int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viro312b63f2012-06-10 18:09:36 -04002494 bool want_excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002496 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 if (error)
2498 return error;
2499
Al Viroacfa4382008-12-04 10:06:33 -05002500 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 return -EACCES; /* shouldn't it be ENOSYS? */
2502 mode &= S_IALLUGO;
2503 mode |= S_IFREG;
2504 error = security_inode_create(dir, dentry, mode);
2505 if (error)
2506 return error;
Al Viro312b63f2012-06-10 18:09:36 -04002507 error = dir->i_op->create(dir, dentry, mode, want_excl);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002508 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002509 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 return error;
2511}
2512
Al Viro73d049a2011-03-11 12:08:24 -05002513static int may_open(struct path *path, int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002515 struct dentry *dentry = path->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 struct inode *inode = dentry->d_inode;
2517 int error;
2518
Al Virobcda7652011-03-13 16:42:14 -04002519 /* O_PATH? */
2520 if (!acc_mode)
2521 return 0;
2522
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 if (!inode)
2524 return -ENOENT;
2525
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002526 switch (inode->i_mode & S_IFMT) {
2527 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002529 case S_IFDIR:
2530 if (acc_mode & MAY_WRITE)
2531 return -EISDIR;
2532 break;
2533 case S_IFBLK:
2534 case S_IFCHR:
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002535 if (path->mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002537 /*FALLTHRU*/
2538 case S_IFIFO:
2539 case S_IFSOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002541 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08002542 }
Dave Hansenb41572e2007-10-16 23:31:14 -07002543
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002544 error = inode_permission(inode, acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07002545 if (error)
2546 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05002547
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 /*
2549 * An append-only file must be opened in append mode for writing.
2550 */
2551 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05002552 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05002553 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05002555 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 }
2557
2558 /* O_NOATIME can only be set by the owner or superuser */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07002559 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
Al Viro7715b522009-12-16 03:54:00 -05002560 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561
J. Bruce Fieldsf3c7691e2011-09-21 10:58:13 -04002562 return 0;
Al Viro7715b522009-12-16 03:54:00 -05002563}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
Jeff Laytone1181ee2010-12-07 16:19:50 -05002565static int handle_truncate(struct file *filp)
Al Viro7715b522009-12-16 03:54:00 -05002566{
Jeff Laytone1181ee2010-12-07 16:19:50 -05002567 struct path *path = &filp->f_path;
Al Viro7715b522009-12-16 03:54:00 -05002568 struct inode *inode = path->dentry->d_inode;
2569 int error = get_write_access(inode);
2570 if (error)
2571 return error;
2572 /*
2573 * Refuse to truncate files with mandatory locks held on them.
2574 */
2575 error = locks_verify_locked(inode);
2576 if (!error)
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +09002577 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05002578 if (!error) {
2579 error = do_truncate(path->dentry, 0,
2580 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
Jeff Laytone1181ee2010-12-07 16:19:50 -05002581 filp);
Al Viro7715b522009-12-16 03:54:00 -05002582 }
2583 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04002584 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585}
2586
Dave Hansend57999e2008-02-15 14:37:27 -08002587static inline int open_to_namei_flags(int flag)
2588{
Al Viro8a5e9292011-06-25 19:15:54 -04002589 if ((flag & O_ACCMODE) == 3)
2590 flag--;
Dave Hansend57999e2008-02-15 14:37:27 -08002591 return flag;
2592}
2593
Miklos Szeredid18e9002012-06-05 15:10:17 +02002594static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode)
2595{
2596 int error = security_path_mknod(dir, dentry, mode, 0);
2597 if (error)
2598 return error;
2599
2600 error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
2601 if (error)
2602 return error;
2603
2604 return security_inode_create(dir->dentry->d_inode, dentry, mode);
2605}
2606
David Howells1acf0af2012-06-14 16:13:46 +01002607/*
2608 * Attempt to atomically look up, create and open a file from a negative
2609 * dentry.
2610 *
2611 * Returns 0 if successful. The file will have been created and attached to
2612 * @file by the filesystem calling finish_open().
2613 *
2614 * Returns 1 if the file was looked up only or didn't need creating. The
2615 * caller will need to perform the open themselves. @path will have been
2616 * updated to point to the new dentry. This may be negative.
2617 *
2618 * Returns an error code otherwise.
2619 */
Al Viro2675a4e2012-06-22 12:41:10 +04002620static int atomic_open(struct nameidata *nd, struct dentry *dentry,
2621 struct path *path, struct file *file,
2622 const struct open_flags *op,
Al Viro64894cf2012-07-31 00:53:35 +04002623 bool got_write, bool need_lookup,
Al Viro2675a4e2012-06-22 12:41:10 +04002624 int *opened)
Miklos Szeredid18e9002012-06-05 15:10:17 +02002625{
2626 struct inode *dir = nd->path.dentry->d_inode;
2627 unsigned open_flag = open_to_namei_flags(op->open_flag);
2628 umode_t mode;
2629 int error;
2630 int acc_mode;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002631 int create_error = 0;
2632 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
Miklos Szeredi116cc022013-09-16 14:52:05 +02002633 bool excl;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002634
2635 BUG_ON(dentry->d_inode);
2636
2637 /* Don't create child dentry for a dead directory. */
2638 if (unlikely(IS_DEADDIR(dir))) {
Al Viro2675a4e2012-06-22 12:41:10 +04002639 error = -ENOENT;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002640 goto out;
2641 }
2642
Miklos Szeredi62b259d2012-08-15 13:01:24 +02002643 mode = op->mode;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002644 if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
2645 mode &= ~current_umask();
2646
Miklos Szeredi116cc022013-09-16 14:52:05 +02002647 excl = (open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT);
2648 if (excl)
Miklos Szeredid18e9002012-06-05 15:10:17 +02002649 open_flag &= ~O_TRUNC;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002650
2651 /*
2652 * Checking write permission is tricky, bacuse we don't know if we are
2653 * going to actually need it: O_CREAT opens should work as long as the
2654 * file exists. But checking existence breaks atomicity. The trick is
2655 * to check access and if not granted clear O_CREAT from the flags.
2656 *
2657 * Another problem is returing the "right" error value (e.g. for an
2658 * O_EXCL open we want to return EEXIST not EROFS).
2659 */
Al Viro64894cf2012-07-31 00:53:35 +04002660 if (((open_flag & (O_CREAT | O_TRUNC)) ||
2661 (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) {
2662 if (!(open_flag & O_CREAT)) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002663 /*
2664 * No O_CREATE -> atomicity not a requirement -> fall
2665 * back to lookup + open
2666 */
2667 goto no_open;
2668 } else if (open_flag & (O_EXCL | O_TRUNC)) {
2669 /* Fall back and fail with the right error */
Al Viro64894cf2012-07-31 00:53:35 +04002670 create_error = -EROFS;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002671 goto no_open;
2672 } else {
2673 /* No side effects, safe to clear O_CREAT */
Al Viro64894cf2012-07-31 00:53:35 +04002674 create_error = -EROFS;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002675 open_flag &= ~O_CREAT;
2676 }
2677 }
2678
2679 if (open_flag & O_CREAT) {
Miklos Szeredi38227f72012-08-15 13:01:24 +02002680 error = may_o_create(&nd->path, dentry, mode);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002681 if (error) {
2682 create_error = error;
2683 if (open_flag & O_EXCL)
2684 goto no_open;
2685 open_flag &= ~O_CREAT;
2686 }
2687 }
2688
2689 if (nd->flags & LOOKUP_DIRECTORY)
2690 open_flag |= O_DIRECTORY;
2691
Al Viro30d90492012-06-22 12:40:19 +04002692 file->f_path.dentry = DENTRY_NOT_SET;
2693 file->f_path.mnt = nd->path.mnt;
2694 error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode,
Al Viro47237682012-06-10 05:01:45 -04002695 opened);
Al Virod9585272012-06-22 12:39:14 +04002696 if (error < 0) {
Al Virod9585272012-06-22 12:39:14 +04002697 if (create_error && error == -ENOENT)
2698 error = create_error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002699 goto out;
2700 }
2701
Al Virod9585272012-06-22 12:39:14 +04002702 if (error) { /* returned 1, that is */
Al Viro30d90492012-06-22 12:40:19 +04002703 if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
Al Viro2675a4e2012-06-22 12:41:10 +04002704 error = -EIO;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002705 goto out;
2706 }
Al Viro30d90492012-06-22 12:40:19 +04002707 if (file->f_path.dentry) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002708 dput(dentry);
Al Viro30d90492012-06-22 12:40:19 +04002709 dentry = file->f_path.dentry;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002710 }
Al Viro03da6332013-09-16 19:22:33 -04002711 if (*opened & FILE_CREATED)
2712 fsnotify_create(dir, dentry);
2713 if (!dentry->d_inode) {
2714 WARN_ON(*opened & FILE_CREATED);
2715 if (create_error) {
2716 error = create_error;
2717 goto out;
2718 }
2719 } else {
2720 if (excl && !(*opened & FILE_CREATED)) {
2721 error = -EEXIST;
2722 goto out;
2723 }
Sage Weil62b2ce92012-08-15 13:30:12 -07002724 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02002725 goto looked_up;
2726 }
2727
2728 /*
2729 * We didn't have the inode before the open, so check open permission
2730 * here.
2731 */
Al Viro03da6332013-09-16 19:22:33 -04002732 acc_mode = op->acc_mode;
2733 if (*opened & FILE_CREATED) {
2734 WARN_ON(!(open_flag & O_CREAT));
2735 fsnotify_create(dir, dentry);
2736 acc_mode = MAY_OPEN;
2737 }
Al Viro2675a4e2012-06-22 12:41:10 +04002738 error = may_open(&file->f_path, acc_mode, open_flag);
2739 if (error)
2740 fput(file);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002741
2742out:
2743 dput(dentry);
Al Viro2675a4e2012-06-22 12:41:10 +04002744 return error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002745
Miklos Szeredid18e9002012-06-05 15:10:17 +02002746no_open:
2747 if (need_lookup) {
Al Viro72bd8662012-06-10 17:17:17 -04002748 dentry = lookup_real(dir, dentry, nd->flags);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002749 if (IS_ERR(dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04002750 return PTR_ERR(dentry);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002751
2752 if (create_error) {
2753 int open_flag = op->open_flag;
2754
Al Viro2675a4e2012-06-22 12:41:10 +04002755 error = create_error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002756 if ((open_flag & O_EXCL)) {
2757 if (!dentry->d_inode)
2758 goto out;
2759 } else if (!dentry->d_inode) {
2760 goto out;
2761 } else if ((open_flag & O_TRUNC) &&
2762 S_ISREG(dentry->d_inode->i_mode)) {
2763 goto out;
2764 }
2765 /* will fail later, go on to get the right error */
2766 }
2767 }
2768looked_up:
2769 path->dentry = dentry;
2770 path->mnt = nd->path.mnt;
Al Viro2675a4e2012-06-22 12:41:10 +04002771 return 1;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002772}
2773
Nick Piggin31e6b012011-01-07 17:49:52 +11002774/*
David Howells1acf0af2012-06-14 16:13:46 +01002775 * Look up and maybe create and open the last component.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002776 *
2777 * Must be called with i_mutex held on parent.
2778 *
David Howells1acf0af2012-06-14 16:13:46 +01002779 * Returns 0 if the file was successfully atomically created (if necessary) and
2780 * opened. In this case the file will be returned attached to @file.
2781 *
2782 * Returns 1 if the file was not completely opened at this time, though lookups
2783 * and creations will have been performed and the dentry returned in @path will
2784 * be positive upon return if O_CREAT was specified. If O_CREAT wasn't
2785 * specified then a negative dentry may be returned.
2786 *
2787 * An error code is returned otherwise.
2788 *
2789 * FILE_CREATE will be set in @*opened if the dentry was created and will be
2790 * cleared otherwise prior to returning.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002791 */
Al Viro2675a4e2012-06-22 12:41:10 +04002792static int lookup_open(struct nameidata *nd, struct path *path,
2793 struct file *file,
2794 const struct open_flags *op,
Al Viro64894cf2012-07-31 00:53:35 +04002795 bool got_write, int *opened)
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002796{
2797 struct dentry *dir = nd->path.dentry;
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002798 struct inode *dir_inode = dir->d_inode;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002799 struct dentry *dentry;
2800 int error;
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002801 bool need_lookup;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002802
Al Viro47237682012-06-10 05:01:45 -04002803 *opened &= ~FILE_CREATED;
Al Viro201f9562012-06-22 12:42:10 +04002804 dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002805 if (IS_ERR(dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04002806 return PTR_ERR(dentry);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002807
Miklos Szeredid18e9002012-06-05 15:10:17 +02002808 /* Cached positive dentry: will open in f_op->open */
2809 if (!need_lookup && dentry->d_inode)
2810 goto out_no_open;
2811
2812 if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) {
Al Viro64894cf2012-07-31 00:53:35 +04002813 return atomic_open(nd, dentry, path, file, op, got_write,
Al Viro47237682012-06-10 05:01:45 -04002814 need_lookup, opened);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002815 }
2816
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002817 if (need_lookup) {
2818 BUG_ON(dentry->d_inode);
2819
Al Viro72bd8662012-06-10 17:17:17 -04002820 dentry = lookup_real(dir_inode, dentry, nd->flags);
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002821 if (IS_ERR(dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04002822 return PTR_ERR(dentry);
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002823 }
2824
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002825 /* Negative dentry, just create the file */
2826 if (!dentry->d_inode && (op->open_flag & O_CREAT)) {
2827 umode_t mode = op->mode;
2828 if (!IS_POSIXACL(dir->d_inode))
2829 mode &= ~current_umask();
2830 /*
2831 * This write is needed to ensure that a
2832 * rw->ro transition does not occur between
2833 * the time when the file is created and when
2834 * a permanent write count is taken through
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02002835 * the 'struct file' in finish_open().
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002836 */
Al Viro64894cf2012-07-31 00:53:35 +04002837 if (!got_write) {
2838 error = -EROFS;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002839 goto out_dput;
Al Viro64894cf2012-07-31 00:53:35 +04002840 }
Al Viro47237682012-06-10 05:01:45 -04002841 *opened |= FILE_CREATED;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002842 error = security_path_mknod(&nd->path, dentry, mode, 0);
2843 if (error)
2844 goto out_dput;
Al Viro312b63f2012-06-10 18:09:36 -04002845 error = vfs_create(dir->d_inode, dentry, mode,
2846 nd->flags & LOOKUP_EXCL);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002847 if (error)
2848 goto out_dput;
2849 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02002850out_no_open:
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002851 path->dentry = dentry;
2852 path->mnt = nd->path.mnt;
Al Viro2675a4e2012-06-22 12:41:10 +04002853 return 1;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002854
2855out_dput:
2856 dput(dentry);
Al Viro2675a4e2012-06-22 12:41:10 +04002857 return error;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002858}
2859
2860/*
Al Virofe2d35f2011-03-05 22:58:25 -05002861 * Handle the last step of open()
Nick Piggin31e6b012011-01-07 17:49:52 +11002862 */
Al Viro2675a4e2012-06-22 12:41:10 +04002863static int do_last(struct nameidata *nd, struct path *path,
2864 struct file *file, const struct open_flags *op,
Jeff Layton669abf42012-10-10 16:43:10 -04002865 int *opened, struct filename *name)
Al Virofb1cc552009-12-24 01:58:28 -05002866{
Al Viroa1e28032009-12-24 02:12:06 -05002867 struct dentry *dir = nd->path.dentry;
Al Viroca344a892011-03-09 00:36:45 -05002868 int open_flag = op->open_flag;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002869 bool will_truncate = (open_flag & O_TRUNC) != 0;
Al Viro64894cf2012-07-31 00:53:35 +04002870 bool got_write = false;
Al Virobcda7652011-03-13 16:42:14 -04002871 int acc_mode = op->acc_mode;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02002872 struct inode *inode;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002873 bool symlink_ok = false;
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02002874 struct path save_parent = { .dentry = NULL, .mnt = NULL };
2875 bool retried = false;
Al Viro16c2cd72011-02-22 15:50:10 -05002876 int error;
Al Virofb1cc552009-12-24 01:58:28 -05002877
Al Viroc3e380b2011-02-23 13:39:45 -05002878 nd->flags &= ~LOOKUP_PARENT;
2879 nd->flags |= op->intent;
2880
Al Virobc77daa2013-06-06 09:12:33 -04002881 if (nd->last_type != LAST_NORM) {
Al Virofe2d35f2011-03-05 22:58:25 -05002882 error = handle_dots(nd, nd->last_type);
2883 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04002884 return error;
Miklos Szeredie83db162012-06-05 15:10:29 +02002885 goto finish_open;
Al Viro1f36f772009-12-26 10:56:19 -05002886 }
Al Viro67ee3ad2009-12-26 07:01:01 -05002887
Al Viroca344a892011-03-09 00:36:45 -05002888 if (!(open_flag & O_CREAT)) {
Al Virofe2d35f2011-03-05 22:58:25 -05002889 if (nd->last.name[nd->last.len])
2890 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
Al Virobcda7652011-03-13 16:42:14 -04002891 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002892 symlink_ok = true;
Al Virofe2d35f2011-03-05 22:58:25 -05002893 /* we _can_ be in RCU mode here */
Al Viroe97cdc82013-01-24 18:16:00 -05002894 error = lookup_fast(nd, path, &inode);
Miklos Szeredi71574862012-06-05 15:10:14 +02002895 if (likely(!error))
2896 goto finish_lookup;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02002897
Miklos Szeredi71574862012-06-05 15:10:14 +02002898 if (error < 0)
Al Viro2675a4e2012-06-22 12:41:10 +04002899 goto out;
Miklos Szeredi37d7fff2012-06-05 15:10:12 +02002900
Miklos Szeredi71574862012-06-05 15:10:14 +02002901 BUG_ON(nd->inode != dir->d_inode);
Miklos Szeredib6183df2012-06-05 15:10:13 +02002902 } else {
2903 /* create side of things */
2904 /*
2905 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
2906 * has been cleared when we got to the last component we are
2907 * about to look up
2908 */
2909 error = complete_walk(nd);
2910 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04002911 return error;
Miklos Szeredib6183df2012-06-05 15:10:13 +02002912
Jeff Layton33e22082013-04-12 15:16:32 -04002913 audit_inode(name, dir, LOOKUP_PARENT);
Miklos Szeredib6183df2012-06-05 15:10:13 +02002914 error = -EISDIR;
2915 /* trailing slashes? */
2916 if (nd->last.name[nd->last.len])
Al Viro2675a4e2012-06-22 12:41:10 +04002917 goto out;
Al Virofe2d35f2011-03-05 22:58:25 -05002918 }
2919
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02002920retry_lookup:
Al Viro64894cf2012-07-31 00:53:35 +04002921 if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
2922 error = mnt_want_write(nd->path.mnt);
2923 if (!error)
2924 got_write = true;
2925 /*
2926 * do _not_ fail yet - we might not need that or fail with
2927 * a different error; let lookup_open() decide; we'll be
2928 * dropping this one anyway.
2929 */
2930 }
Al Viroa1e28032009-12-24 02:12:06 -05002931 mutex_lock(&dir->d_inode->i_mutex);
Al Viro64894cf2012-07-31 00:53:35 +04002932 error = lookup_open(nd, path, file, op, got_write, opened);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002933 mutex_unlock(&dir->d_inode->i_mutex);
Al Viroa1e28032009-12-24 02:12:06 -05002934
Al Viro2675a4e2012-06-22 12:41:10 +04002935 if (error <= 0) {
2936 if (error)
Miklos Szeredid18e9002012-06-05 15:10:17 +02002937 goto out;
2938
Al Viro47237682012-06-10 05:01:45 -04002939 if ((*opened & FILE_CREATED) ||
Al Viro496ad9a2013-01-23 17:07:38 -05002940 !S_ISREG(file_inode(file)->i_mode))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002941 will_truncate = false;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002942
Jeff Laytonadb5c242012-10-10 16:43:13 -04002943 audit_inode(name, file->f_path.dentry, 0);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002944 goto opened;
2945 }
Al Virofb1cc552009-12-24 01:58:28 -05002946
Al Viro47237682012-06-10 05:01:45 -04002947 if (*opened & FILE_CREATED) {
Al Viro9b44f1b2011-03-09 00:17:27 -05002948 /* Don't check for write permission, don't truncate */
Al Viroca344a892011-03-09 00:36:45 -05002949 open_flag &= ~O_TRUNC;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002950 will_truncate = false;
Al Virobcda7652011-03-13 16:42:14 -04002951 acc_mode = MAY_OPEN;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002952 path_to_nameidata(path, nd);
Miklos Szeredie83db162012-06-05 15:10:29 +02002953 goto finish_open_created;
Al Virofb1cc552009-12-24 01:58:28 -05002954 }
2955
2956 /*
Jeff Layton3134f372012-07-25 10:19:47 -04002957 * create/update audit record if it already exists.
Al Virofb1cc552009-12-24 01:58:28 -05002958 */
David Howellsb18825a2013-09-12 19:22:53 +01002959 if (d_is_positive(path->dentry))
Jeff Laytonadb5c242012-10-10 16:43:13 -04002960 audit_inode(name, path->dentry, 0);
Al Virofb1cc552009-12-24 01:58:28 -05002961
Miklos Szeredid18e9002012-06-05 15:10:17 +02002962 /*
2963 * If atomic_open() acquired write access it is dropped now due to
2964 * possible mount and symlink following (this might be optimized away if
2965 * necessary...)
2966 */
Al Viro64894cf2012-07-31 00:53:35 +04002967 if (got_write) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002968 mnt_drop_write(nd->path.mnt);
Al Viro64894cf2012-07-31 00:53:35 +04002969 got_write = false;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002970 }
2971
Al Virofb1cc552009-12-24 01:58:28 -05002972 error = -EEXIST;
Al Virof8310c52012-07-30 11:50:30 +04002973 if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))
Al Virofb1cc552009-12-24 01:58:28 -05002974 goto exit_dput;
2975
David Howells9875cf82011-01-14 18:45:21 +00002976 error = follow_managed(path, nd->flags);
2977 if (error < 0)
2978 goto exit_dput;
Al Virofb1cc552009-12-24 01:58:28 -05002979
Al Viroa3fbbde2011-11-07 21:21:26 +00002980 if (error)
2981 nd->flags |= LOOKUP_JUMPED;
2982
Miklos Szeredidecf3402012-05-21 17:30:08 +02002983 BUG_ON(nd->flags & LOOKUP_RCU);
2984 inode = path->dentry->d_inode;
Miklos Szeredi5f5daac2012-05-21 17:30:14 +02002985finish_lookup:
2986 /* we _can_ be in RCU mode here */
Al Virofb1cc552009-12-24 01:58:28 -05002987 error = -ENOENT;
David Howellsb18825a2013-09-12 19:22:53 +01002988 if (d_is_negative(path->dentry)) {
Miklos Szeredi54c33e72012-05-21 17:30:10 +02002989 path_to_nameidata(path, nd);
Al Viro2675a4e2012-06-22 12:41:10 +04002990 goto out;
Miklos Szeredi54c33e72012-05-21 17:30:10 +02002991 }
Al Viro9e67f362009-12-26 07:04:50 -05002992
David Howellsb18825a2013-09-12 19:22:53 +01002993 if (should_follow_link(path->dentry, !symlink_ok)) {
Miklos Szeredid45ea862012-05-21 17:30:09 +02002994 if (nd->flags & LOOKUP_RCU) {
2995 if (unlikely(unlazy_walk(nd, path->dentry))) {
2996 error = -ECHILD;
Al Viro2675a4e2012-06-22 12:41:10 +04002997 goto out;
Miklos Szeredid45ea862012-05-21 17:30:09 +02002998 }
2999 }
3000 BUG_ON(inode != path->dentry->d_inode);
Al Viro2675a4e2012-06-22 12:41:10 +04003001 return 1;
Miklos Szeredid45ea862012-05-21 17:30:09 +02003002 }
Al Virofb1cc552009-12-24 01:58:28 -05003003
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003004 if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) {
3005 path_to_nameidata(path, nd);
3006 } else {
3007 save_parent.dentry = nd->path.dentry;
3008 save_parent.mnt = mntget(path->mnt);
3009 nd->path.dentry = path->dentry;
3010
3011 }
Miklos Szeredidecf3402012-05-21 17:30:08 +02003012 nd->inode = inode;
Al Viroa3fbbde2011-11-07 21:21:26 +00003013 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
Al Virobc77daa2013-06-06 09:12:33 -04003014finish_open:
Al Viroa3fbbde2011-11-07 21:21:26 +00003015 error = complete_walk(nd);
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003016 if (error) {
3017 path_put(&save_parent);
Al Viro2675a4e2012-06-22 12:41:10 +04003018 return error;
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003019 }
Al Virobc77daa2013-06-06 09:12:33 -04003020 audit_inode(name, nd->path.dentry, 0);
Al Virofb1cc552009-12-24 01:58:28 -05003021 error = -EISDIR;
David Howellsb18825a2013-09-12 19:22:53 +01003022 if ((open_flag & O_CREAT) &&
3023 (d_is_directory(nd->path.dentry) || d_is_autodir(nd->path.dentry)))
Al Viro2675a4e2012-06-22 12:41:10 +04003024 goto out;
Miklos Szerediaf2f5542012-05-21 17:30:11 +02003025 error = -ENOTDIR;
David Howellsb18825a2013-09-12 19:22:53 +01003026 if ((nd->flags & LOOKUP_DIRECTORY) && !d_is_directory(nd->path.dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04003027 goto out;
Al Viro6c0d46c2011-03-09 00:59:59 -05003028 if (!S_ISREG(nd->inode->i_mode))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003029 will_truncate = false;
Al Viro6c0d46c2011-03-09 00:59:59 -05003030
Al Viro0f9d1a12011-03-09 00:13:14 -05003031 if (will_truncate) {
3032 error = mnt_want_write(nd->path.mnt);
3033 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003034 goto out;
Al Viro64894cf2012-07-31 00:53:35 +04003035 got_write = true;
Al Viro0f9d1a12011-03-09 00:13:14 -05003036 }
Miklos Szeredie83db162012-06-05 15:10:29 +02003037finish_open_created:
Al Virobcda7652011-03-13 16:42:14 -04003038 error = may_open(&nd->path, acc_mode, open_flag);
Al Viroca344a892011-03-09 00:36:45 -05003039 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003040 goto out;
Al Viro30d90492012-06-22 12:40:19 +04003041 file->f_path.mnt = nd->path.mnt;
3042 error = finish_open(file, nd->path.dentry, NULL, opened);
3043 if (error) {
Al Viro30d90492012-06-22 12:40:19 +04003044 if (error == -EOPENSTALE)
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003045 goto stale_open;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003046 goto out;
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003047 }
Miklos Szeredia8277b92012-06-05 15:10:32 +02003048opened:
Al Viro2675a4e2012-06-22 12:41:10 +04003049 error = open_check_o_direct(file);
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003050 if (error)
3051 goto exit_fput;
Al Viro2675a4e2012-06-22 12:41:10 +04003052 error = ima_file_check(file, op->acc_mode);
Miklos Szerediaa4caad2012-06-05 15:10:28 +02003053 if (error)
3054 goto exit_fput;
3055
3056 if (will_truncate) {
Al Viro2675a4e2012-06-22 12:41:10 +04003057 error = handle_truncate(file);
Miklos Szerediaa4caad2012-06-05 15:10:28 +02003058 if (error)
3059 goto exit_fput;
Al Viro0f9d1a12011-03-09 00:13:14 -05003060 }
Al Viroca344a892011-03-09 00:36:45 -05003061out:
Al Viro64894cf2012-07-31 00:53:35 +04003062 if (got_write)
Al Viro0f9d1a12011-03-09 00:13:14 -05003063 mnt_drop_write(nd->path.mnt);
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003064 path_put(&save_parent);
Miklos Szeredie276ae62012-05-21 17:30:06 +02003065 terminate_walk(nd);
Al Viro2675a4e2012-06-22 12:41:10 +04003066 return error;
Al Virofb1cc552009-12-24 01:58:28 -05003067
Al Virofb1cc552009-12-24 01:58:28 -05003068exit_dput:
3069 path_put_conditional(path, nd);
Al Viroca344a892011-03-09 00:36:45 -05003070 goto out;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003071exit_fput:
Al Viro2675a4e2012-06-22 12:41:10 +04003072 fput(file);
3073 goto out;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003074
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003075stale_open:
3076 /* If no saved parent or already retried then can't retry */
3077 if (!save_parent.dentry || retried)
3078 goto out;
3079
3080 BUG_ON(save_parent.dentry != dir);
3081 path_put(&nd->path);
3082 nd->path = save_parent;
3083 nd->inode = dir->d_inode;
3084 save_parent.mnt = NULL;
3085 save_parent.dentry = NULL;
Al Viro64894cf2012-07-31 00:53:35 +04003086 if (got_write) {
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003087 mnt_drop_write(nd->path.mnt);
Al Viro64894cf2012-07-31 00:53:35 +04003088 got_write = false;
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003089 }
3090 retried = true;
3091 goto retry_lookup;
Al Virofb1cc552009-12-24 01:58:28 -05003092}
3093
Al Viro60545d02013-06-07 01:20:27 -04003094static int do_tmpfile(int dfd, struct filename *pathname,
3095 struct nameidata *nd, int flags,
3096 const struct open_flags *op,
3097 struct file *file, int *opened)
3098{
3099 static const struct qstr name = QSTR_INIT("/", 1);
3100 struct dentry *dentry, *child;
3101 struct inode *dir;
3102 int error = path_lookupat(dfd, pathname->name,
3103 flags | LOOKUP_DIRECTORY, nd);
3104 if (unlikely(error))
3105 return error;
3106 error = mnt_want_write(nd->path.mnt);
3107 if (unlikely(error))
3108 goto out;
3109 /* we want directory to be writable */
3110 error = inode_permission(nd->inode, MAY_WRITE | MAY_EXEC);
3111 if (error)
3112 goto out2;
3113 dentry = nd->path.dentry;
3114 dir = dentry->d_inode;
3115 if (!dir->i_op->tmpfile) {
3116 error = -EOPNOTSUPP;
3117 goto out2;
3118 }
3119 child = d_alloc(dentry, &name);
3120 if (unlikely(!child)) {
3121 error = -ENOMEM;
3122 goto out2;
3123 }
3124 nd->flags &= ~LOOKUP_DIRECTORY;
3125 nd->flags |= op->intent;
3126 dput(nd->path.dentry);
3127 nd->path.dentry = child;
3128 error = dir->i_op->tmpfile(dir, nd->path.dentry, op->mode);
3129 if (error)
3130 goto out2;
3131 audit_inode(pathname, nd->path.dentry, 0);
3132 error = may_open(&nd->path, op->acc_mode, op->open_flag);
3133 if (error)
3134 goto out2;
3135 file->f_path.mnt = nd->path.mnt;
3136 error = finish_open(file, nd->path.dentry, NULL, opened);
3137 if (error)
3138 goto out2;
3139 error = open_check_o_direct(file);
Al Virof4e0c302013-06-11 08:34:36 +04003140 if (error) {
Al Viro60545d02013-06-07 01:20:27 -04003141 fput(file);
Al Virof4e0c302013-06-11 08:34:36 +04003142 } else if (!(op->open_flag & O_EXCL)) {
3143 struct inode *inode = file_inode(file);
3144 spin_lock(&inode->i_lock);
3145 inode->i_state |= I_LINKABLE;
3146 spin_unlock(&inode->i_lock);
3147 }
Al Viro60545d02013-06-07 01:20:27 -04003148out2:
3149 mnt_drop_write(nd->path.mnt);
3150out:
3151 path_put(&nd->path);
3152 return error;
3153}
3154
Jeff Layton669abf42012-10-10 16:43:10 -04003155static struct file *path_openat(int dfd, struct filename *pathname,
Al Viro73d049a2011-03-11 12:08:24 -05003156 struct nameidata *nd, const struct open_flags *op, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157{
Al Virofe2d35f2011-03-05 22:58:25 -05003158 struct file *base = NULL;
Al Viro30d90492012-06-22 12:40:19 +04003159 struct file *file;
Al Viro9850c052010-01-13 15:01:15 -05003160 struct path path;
Al Viro47237682012-06-10 05:01:45 -04003161 int opened = 0;
Al Viro13aab422011-02-23 17:54:08 -05003162 int error;
Nick Piggin31e6b012011-01-07 17:49:52 +11003163
Al Viro30d90492012-06-22 12:40:19 +04003164 file = get_empty_filp();
Al Viro1afc99b2013-02-14 20:41:04 -05003165 if (IS_ERR(file))
3166 return file;
Nick Piggin31e6b012011-01-07 17:49:52 +11003167
Al Viro30d90492012-06-22 12:40:19 +04003168 file->f_flags = op->open_flag;
Nick Piggin31e6b012011-01-07 17:49:52 +11003169
Al Virobb458c62013-07-13 13:26:37 +04003170 if (unlikely(file->f_flags & __O_TMPFILE)) {
Al Viro60545d02013-06-07 01:20:27 -04003171 error = do_tmpfile(dfd, pathname, nd, flags, op, file, &opened);
3172 goto out;
3173 }
3174
Jeff Layton669abf42012-10-10 16:43:10 -04003175 error = path_init(dfd, pathname->name, flags | LOOKUP_PARENT, nd, &base);
Nick Piggin31e6b012011-01-07 17:49:52 +11003176 if (unlikely(error))
Al Viro2675a4e2012-06-22 12:41:10 +04003177 goto out;
Nick Piggin31e6b012011-01-07 17:49:52 +11003178
Al Virofe2d35f2011-03-05 22:58:25 -05003179 current->total_link_count = 0;
Jeff Layton669abf42012-10-10 16:43:10 -04003180 error = link_path_walk(pathname->name, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11003181 if (unlikely(error))
Al Viro2675a4e2012-06-22 12:41:10 +04003182 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183
Al Viro2675a4e2012-06-22 12:41:10 +04003184 error = do_last(nd, &path, file, op, &opened, pathname);
3185 while (unlikely(error > 0)) { /* trailing symlink */
Nick Piggin7b9337a2011-01-14 08:42:43 +00003186 struct path link = path;
Al Virodef4af32009-12-26 08:37:05 -05003187 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04003188 if (!(nd->flags & LOOKUP_FOLLOW)) {
Al Viro73d049a2011-03-11 12:08:24 -05003189 path_put_conditional(&path, nd);
3190 path_put(&nd->path);
Al Viro2675a4e2012-06-22 12:41:10 +04003191 error = -ELOOP;
Al Viro40b39132011-03-09 16:22:18 -05003192 break;
3193 }
Kees Cook800179c2012-07-25 17:29:07 -07003194 error = may_follow_link(&link, nd);
3195 if (unlikely(error))
3196 break;
Al Viro73d049a2011-03-11 12:08:24 -05003197 nd->flags |= LOOKUP_PARENT;
3198 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
Al Viro574197e2011-03-14 22:20:34 -04003199 error = follow_link(&link, nd, &cookie);
Al Viroc3e380b2011-02-23 13:39:45 -05003200 if (unlikely(error))
Al Viro2675a4e2012-06-22 12:41:10 +04003201 break;
3202 error = do_last(nd, &path, file, op, &opened, pathname);
Al Viro574197e2011-03-14 22:20:34 -04003203 put_link(nd, &link, cookie);
Al Viro806b6812009-12-26 07:16:40 -05003204 }
Al Viro10fa8e62009-12-26 07:09:49 -05003205out:
Al Viro73d049a2011-03-11 12:08:24 -05003206 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
3207 path_put(&nd->root);
Al Virofe2d35f2011-03-05 22:58:25 -05003208 if (base)
3209 fput(base);
Al Viro2675a4e2012-06-22 12:41:10 +04003210 if (!(opened & FILE_OPENED)) {
3211 BUG_ON(!error);
Al Viro30d90492012-06-22 12:40:19 +04003212 put_filp(file);
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003213 }
Al Viro2675a4e2012-06-22 12:41:10 +04003214 if (unlikely(error)) {
3215 if (error == -EOPENSTALE) {
3216 if (flags & LOOKUP_RCU)
3217 error = -ECHILD;
3218 else
3219 error = -ESTALE;
3220 }
3221 file = ERR_PTR(error);
3222 }
3223 return file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224}
3225
Jeff Layton669abf42012-10-10 16:43:10 -04003226struct file *do_filp_open(int dfd, struct filename *pathname,
Al Virof9652e12013-06-11 08:23:01 +04003227 const struct open_flags *op)
Al Viro13aab422011-02-23 17:54:08 -05003228{
Al Viro73d049a2011-03-11 12:08:24 -05003229 struct nameidata nd;
Al Virof9652e12013-06-11 08:23:01 +04003230 int flags = op->lookup_flags;
Al Viro13aab422011-02-23 17:54:08 -05003231 struct file *filp;
3232
Al Viro73d049a2011-03-11 12:08:24 -05003233 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
Al Viro13aab422011-02-23 17:54:08 -05003234 if (unlikely(filp == ERR_PTR(-ECHILD)))
Al Viro73d049a2011-03-11 12:08:24 -05003235 filp = path_openat(dfd, pathname, &nd, op, flags);
Al Viro13aab422011-02-23 17:54:08 -05003236 if (unlikely(filp == ERR_PTR(-ESTALE)))
Al Viro73d049a2011-03-11 12:08:24 -05003237 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
Al Viro13aab422011-02-23 17:54:08 -05003238 return filp;
3239}
3240
Al Viro73d049a2011-03-11 12:08:24 -05003241struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
Al Virof9652e12013-06-11 08:23:01 +04003242 const char *name, const struct open_flags *op)
Al Viro73d049a2011-03-11 12:08:24 -05003243{
3244 struct nameidata nd;
3245 struct file *file;
Jeff Layton669abf42012-10-10 16:43:10 -04003246 struct filename filename = { .name = name };
Al Virof9652e12013-06-11 08:23:01 +04003247 int flags = op->lookup_flags | LOOKUP_ROOT;
Al Viro73d049a2011-03-11 12:08:24 -05003248
3249 nd.root.mnt = mnt;
3250 nd.root.dentry = dentry;
3251
David Howellsb18825a2013-09-12 19:22:53 +01003252 if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN)
Al Viro73d049a2011-03-11 12:08:24 -05003253 return ERR_PTR(-ELOOP);
3254
Jeff Layton669abf42012-10-10 16:43:10 -04003255 file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_RCU);
Al Viro73d049a2011-03-11 12:08:24 -05003256 if (unlikely(file == ERR_PTR(-ECHILD)))
Jeff Layton669abf42012-10-10 16:43:10 -04003257 file = path_openat(-1, &filename, &nd, op, flags);
Al Viro73d049a2011-03-11 12:08:24 -05003258 if (unlikely(file == ERR_PTR(-ESTALE)))
Jeff Layton669abf42012-10-10 16:43:10 -04003259 file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_REVAL);
Al Viro73d049a2011-03-11 12:08:24 -05003260 return file;
3261}
3262
Jeff Layton1ac12b42012-12-11 12:10:06 -05003263struct dentry *kern_path_create(int dfd, const char *pathname,
3264 struct path *path, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003266 struct dentry *dentry = ERR_PTR(-EEXIST);
Al Viroed75e952011-06-27 16:53:43 -04003267 struct nameidata nd;
Jan Karac30dabf2012-06-12 16:20:30 +02003268 int err2;
Jeff Layton1ac12b42012-12-11 12:10:06 -05003269 int error;
3270 bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
3271
3272 /*
3273 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
3274 * other flags passed in are ignored!
3275 */
3276 lookup_flags &= LOOKUP_REVAL;
3277
3278 error = do_path_lookup(dfd, pathname, LOOKUP_PARENT|lookup_flags, &nd);
Al Viroed75e952011-06-27 16:53:43 -04003279 if (error)
3280 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003282 /*
3283 * Yucky last component or no last component at all?
3284 * (foo/., foo/.., /////)
3285 */
Al Viroed75e952011-06-27 16:53:43 -04003286 if (nd.last_type != LAST_NORM)
3287 goto out;
3288 nd.flags &= ~LOOKUP_PARENT;
3289 nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003290
Jan Karac30dabf2012-06-12 16:20:30 +02003291 /* don't fail immediately if it's r/o, at least try to report other errors */
3292 err2 = mnt_want_write(nd.path.mnt);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003293 /*
3294 * Do the final lookup.
3295 */
Al Viroed75e952011-06-27 16:53:43 -04003296 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3297 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 if (IS_ERR(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003299 goto unlock;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003300
Al Viroa8104a92012-07-20 02:25:00 +04003301 error = -EEXIST;
David Howellsb18825a2013-09-12 19:22:53 +01003302 if (d_is_positive(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003303 goto fail;
David Howellsb18825a2013-09-12 19:22:53 +01003304
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003305 /*
3306 * Special case - lookup gave negative, but... we had foo/bar/
3307 * From the vfs_mknod() POV we just have a negative dentry -
3308 * all is fine. Let's be bastards - you had / on the end, you've
3309 * been asking for (non-existent) directory. -ENOENT for you.
3310 */
Al Viroed75e952011-06-27 16:53:43 -04003311 if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
Al Viroa8104a92012-07-20 02:25:00 +04003312 error = -ENOENT;
Al Viroed75e952011-06-27 16:53:43 -04003313 goto fail;
Al Viroe9baf6e2008-05-15 04:49:12 -04003314 }
Jan Karac30dabf2012-06-12 16:20:30 +02003315 if (unlikely(err2)) {
3316 error = err2;
Al Viroa8104a92012-07-20 02:25:00 +04003317 goto fail;
Jan Karac30dabf2012-06-12 16:20:30 +02003318 }
Al Viroed75e952011-06-27 16:53:43 -04003319 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321fail:
Al Viroa8104a92012-07-20 02:25:00 +04003322 dput(dentry);
3323 dentry = ERR_PTR(error);
3324unlock:
Al Viroed75e952011-06-27 16:53:43 -04003325 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Karac30dabf2012-06-12 16:20:30 +02003326 if (!err2)
3327 mnt_drop_write(nd.path.mnt);
Al Viroed75e952011-06-27 16:53:43 -04003328out:
3329 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 return dentry;
3331}
Al Virodae6ad82011-06-26 11:50:15 -04003332EXPORT_SYMBOL(kern_path_create);
3333
Al Viro921a1652012-07-20 01:15:31 +04003334void done_path_create(struct path *path, struct dentry *dentry)
3335{
3336 dput(dentry);
3337 mutex_unlock(&path->dentry->d_inode->i_mutex);
Al Viroa8104a92012-07-20 02:25:00 +04003338 mnt_drop_write(path->mnt);
Al Viro921a1652012-07-20 01:15:31 +04003339 path_put(path);
3340}
3341EXPORT_SYMBOL(done_path_create);
3342
Jeff Layton1ac12b42012-12-11 12:10:06 -05003343struct dentry *user_path_create(int dfd, const char __user *pathname,
3344 struct path *path, unsigned int lookup_flags)
Al Virodae6ad82011-06-26 11:50:15 -04003345{
Jeff Layton91a27b22012-10-10 15:25:28 -04003346 struct filename *tmp = getname(pathname);
Al Virodae6ad82011-06-26 11:50:15 -04003347 struct dentry *res;
3348 if (IS_ERR(tmp))
3349 return ERR_CAST(tmp);
Jeff Layton1ac12b42012-12-11 12:10:06 -05003350 res = kern_path_create(dfd, tmp->name, path, lookup_flags);
Al Virodae6ad82011-06-26 11:50:15 -04003351 putname(tmp);
3352 return res;
3353}
3354EXPORT_SYMBOL(user_path_create);
3355
Al Viro1a67aaf2011-07-26 01:52:52 -04003356int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003358 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359
3360 if (error)
3361 return error;
3362
Eric W. Biederman975d6b32011-11-13 12:16:43 -08003363 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 return -EPERM;
3365
Al Viroacfa4382008-12-04 10:06:33 -05003366 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 return -EPERM;
3368
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07003369 error = devcgroup_inode_mknod(mode, dev);
3370 if (error)
3371 return error;
3372
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 error = security_inode_mknod(dir, dentry, mode, dev);
3374 if (error)
3375 return error;
3376
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003378 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003379 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 return error;
3381}
3382
Al Virof69aac02011-07-26 04:31:40 -04003383static int may_mknod(umode_t mode)
Dave Hansen463c3192008-02-15 14:37:57 -08003384{
3385 switch (mode & S_IFMT) {
3386 case S_IFREG:
3387 case S_IFCHR:
3388 case S_IFBLK:
3389 case S_IFIFO:
3390 case S_IFSOCK:
3391 case 0: /* zero mode translates to S_IFREG */
3392 return 0;
3393 case S_IFDIR:
3394 return -EPERM;
3395 default:
3396 return -EINVAL;
3397 }
3398}
3399
Al Viro8208a222011-07-25 17:32:17 -04003400SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003401 unsigned, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402{
Al Viro2ad94ae2008-07-21 09:32:51 -04003403 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003404 struct path path;
3405 int error;
Jeff Layton972567f2012-12-20 16:00:10 -05003406 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407
Al Viro8e4bfca2012-07-20 01:17:26 +04003408 error = may_mknod(mode);
3409 if (error)
3410 return error;
Jeff Layton972567f2012-12-20 16:00:10 -05003411retry:
3412 dentry = user_path_create(dfd, filename, &path, lookup_flags);
Al Virodae6ad82011-06-26 11:50:15 -04003413 if (IS_ERR(dentry))
3414 return PTR_ERR(dentry);
Al Viro2ad94ae2008-07-21 09:32:51 -04003415
Al Virodae6ad82011-06-26 11:50:15 -04003416 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003417 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003418 error = security_path_mknod(&path, dentry, mode, dev);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003419 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04003420 goto out;
Dave Hansen463c3192008-02-15 14:37:57 -08003421 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 case 0: case S_IFREG:
Al Viro312b63f2012-06-10 18:09:36 -04003423 error = vfs_create(path.dentry->d_inode,dentry,mode,true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 break;
3425 case S_IFCHR: case S_IFBLK:
Al Virodae6ad82011-06-26 11:50:15 -04003426 error = vfs_mknod(path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 new_decode_dev(dev));
3428 break;
3429 case S_IFIFO: case S_IFSOCK:
Al Virodae6ad82011-06-26 11:50:15 -04003430 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 }
Al Viroa8104a92012-07-20 02:25:00 +04003433out:
Al Viro921a1652012-07-20 01:15:31 +04003434 done_path_create(&path, dentry);
Jeff Layton972567f2012-12-20 16:00:10 -05003435 if (retry_estale(error, lookup_flags)) {
3436 lookup_flags |= LOOKUP_REVAL;
3437 goto retry;
3438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 return error;
3440}
3441
Al Viro8208a222011-07-25 17:32:17 -04003442SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003443{
3444 return sys_mknodat(AT_FDCWD, filename, mode, dev);
3445}
3446
Al Viro18bb1db2011-07-26 01:41:39 -04003447int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003449 int error = may_create(dir, dentry);
Al Viro8de52772012-02-06 12:45:27 -05003450 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451
3452 if (error)
3453 return error;
3454
Al Viroacfa4382008-12-04 10:06:33 -05003455 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 return -EPERM;
3457
3458 mode &= (S_IRWXUGO|S_ISVTX);
3459 error = security_inode_mkdir(dir, dentry, mode);
3460 if (error)
3461 return error;
3462
Al Viro8de52772012-02-06 12:45:27 -05003463 if (max_links && dir->i_nlink >= max_links)
3464 return -EMLINK;
3465
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003467 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003468 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 return error;
3470}
3471
Al Viroa218d0f2011-11-21 14:59:34 -05003472SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473{
Dave Hansen6902d922006-09-30 23:29:01 -07003474 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003475 struct path path;
3476 int error;
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003477 unsigned int lookup_flags = LOOKUP_DIRECTORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003479retry:
3480 dentry = user_path_create(dfd, pathname, &path, lookup_flags);
Dave Hansen6902d922006-09-30 23:29:01 -07003481 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003482 return PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003483
Al Virodae6ad82011-06-26 11:50:15 -04003484 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003485 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003486 error = security_path_mkdir(&path, dentry, mode);
Al Viroa8104a92012-07-20 02:25:00 +04003487 if (!error)
3488 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
Al Viro921a1652012-07-20 01:15:31 +04003489 done_path_create(&path, dentry);
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003490 if (retry_estale(error, lookup_flags)) {
3491 lookup_flags |= LOOKUP_REVAL;
3492 goto retry;
3493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 return error;
3495}
3496
Al Viroa218d0f2011-11-21 14:59:34 -05003497SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003498{
3499 return sys_mkdirat(AT_FDCWD, pathname, mode);
3500}
3501
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502/*
Sage Weila71905f2011-05-24 13:06:08 -07003503 * The dentry_unhash() helper will try to drop the dentry early: we
J. Bruce Fieldsc0d02592012-02-15 11:48:40 -05003504 * should have a usage count of 1 if we're the only user of this
Sage Weila71905f2011-05-24 13:06:08 -07003505 * dentry, and if that is true (possibly after pruning the dcache),
3506 * then we drop the dentry now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 *
3508 * A low-level filesystem can, if it choses, legally
3509 * do a
3510 *
3511 * if (!d_unhashed(dentry))
3512 * return -EBUSY;
3513 *
3514 * if it cannot handle the case of removing a directory
3515 * that is still in use by something else..
3516 */
3517void dentry_unhash(struct dentry *dentry)
3518{
Vasily Averindc168422006-12-06 20:37:07 -08003519 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 spin_lock(&dentry->d_lock);
Waiman Long98474232013-08-28 18:24:59 -07003521 if (dentry->d_lockref.count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 __d_drop(dentry);
3523 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524}
3525
3526int vfs_rmdir(struct inode *dir, struct dentry *dentry)
3527{
3528 int error = may_delete(dir, dentry, 1);
3529
3530 if (error)
3531 return error;
3532
Al Viroacfa4382008-12-04 10:06:33 -05003533 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 return -EPERM;
3535
Al Viro1d2ef592011-09-14 18:55:41 +01003536 dget(dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003537 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538
Sage Weil912dbc12011-05-24 13:06:11 -07003539 error = -EBUSY;
3540 if (d_mountpoint(dentry))
3541 goto out;
3542
3543 error = security_inode_rmdir(dir, dentry);
3544 if (error)
3545 goto out;
3546
Sage Weil3cebde22011-05-29 21:20:59 -07003547 shrink_dcache_parent(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003548 error = dir->i_op->rmdir(dir, dentry);
3549 if (error)
3550 goto out;
3551
3552 dentry->d_inode->i_flags |= S_DEAD;
3553 dont_mount(dentry);
3554
3555out:
3556 mutex_unlock(&dentry->d_inode->i_mutex);
Al Viro1d2ef592011-09-14 18:55:41 +01003557 dput(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003558 if (!error)
3559 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 return error;
3561}
3562
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003563static long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564{
3565 int error = 0;
Jeff Layton91a27b22012-10-10 15:25:28 -04003566 struct filename *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 struct dentry *dentry;
3568 struct nameidata nd;
Jeff Laytonc6ee9202012-12-20 16:28:33 -05003569 unsigned int lookup_flags = 0;
3570retry:
3571 name = user_path_parent(dfd, pathname, &nd, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04003572 if (IS_ERR(name))
3573 return PTR_ERR(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574
3575 switch(nd.last_type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003576 case LAST_DOTDOT:
3577 error = -ENOTEMPTY;
3578 goto exit1;
3579 case LAST_DOT:
3580 error = -EINVAL;
3581 goto exit1;
3582 case LAST_ROOT:
3583 error = -EBUSY;
3584 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003586
3587 nd.flags &= ~LOOKUP_PARENT;
Jan Karac30dabf2012-06-12 16:20:30 +02003588 error = mnt_want_write(nd.path.mnt);
3589 if (error)
3590 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003591
Jan Blunck4ac91372008-02-14 19:34:32 -08003592 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08003593 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003595 if (IS_ERR(dentry))
3596 goto exit2;
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04003597 if (!dentry->d_inode) {
3598 error = -ENOENT;
3599 goto exit3;
3600 }
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003601 error = security_path_rmdir(&nd.path, dentry);
3602 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02003603 goto exit3;
Jan Blunck4ac91372008-02-14 19:34:32 -08003604 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
Dave Hansen06227532008-02-15 14:37:34 -08003605exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07003606 dput(dentry);
3607exit2:
Jan Blunck4ac91372008-02-14 19:34:32 -08003608 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Karac30dabf2012-06-12 16:20:30 +02003609 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003611 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 putname(name);
Jeff Laytonc6ee9202012-12-20 16:28:33 -05003613 if (retry_estale(error, lookup_flags)) {
3614 lookup_flags |= LOOKUP_REVAL;
3615 goto retry;
3616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 return error;
3618}
3619
Heiko Carstens3cdad422009-01-14 14:14:22 +01003620SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003621{
3622 return do_rmdir(AT_FDCWD, pathname);
3623}
3624
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003625/**
3626 * vfs_unlink - unlink a filesystem object
3627 * @dir: parent directory
3628 * @dentry: victim
3629 * @delegated_inode: returns victim inode, if the inode is delegated.
3630 *
3631 * The caller must hold dir->i_mutex.
3632 *
3633 * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
3634 * return a reference to the inode in delegated_inode. The caller
3635 * should then break the delegation on that inode and retry. Because
3636 * breaking a delegation may take a long time, the caller should drop
3637 * dir->i_mutex before doing so.
3638 *
3639 * Alternatively, a caller may pass NULL for delegated_inode. This may
3640 * be appropriate for callers that expect the underlying filesystem not
3641 * to be NFS exported.
3642 */
3643int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644{
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003645 struct inode *target = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 int error = may_delete(dir, dentry, 0);
3647
3648 if (error)
3649 return error;
3650
Al Viroacfa4382008-12-04 10:06:33 -05003651 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 return -EPERM;
3653
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003654 mutex_lock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 if (d_mountpoint(dentry))
3656 error = -EBUSY;
3657 else {
3658 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05003659 if (!error) {
J. Bruce Fields5a146962012-08-28 07:50:40 -07003660 error = try_break_deleg(target, delegated_inode);
3661 if (error)
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003662 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 error = dir->i_op->unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05003664 if (!error)
Al Virod83c49f2010-04-30 17:17:09 -04003665 dont_mount(dentry);
Al Virobec10522010-03-03 14:12:08 -05003666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 }
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003668out:
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003669 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670
3671 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
3672 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003673 fsnotify_link_count(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 }
Robert Love0eeca282005-07-12 17:06:03 -04003676
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 return error;
3678}
3679
3680/*
3681 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003682 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 * writeout happening, and we don't want to prevent access to the directory
3684 * while waiting on the I/O.
3685 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003686static long do_unlinkat(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687{
Al Viro2ad94ae2008-07-21 09:32:51 -04003688 int error;
Jeff Layton91a27b22012-10-10 15:25:28 -04003689 struct filename *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 struct dentry *dentry;
3691 struct nameidata nd;
3692 struct inode *inode = NULL;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003693 struct inode *delegated_inode = NULL;
Jeff Layton5d18f812012-12-20 16:38:04 -05003694 unsigned int lookup_flags = 0;
3695retry:
3696 name = user_path_parent(dfd, pathname, &nd, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04003697 if (IS_ERR(name))
3698 return PTR_ERR(name);
Al Viro2ad94ae2008-07-21 09:32:51 -04003699
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 error = -EISDIR;
3701 if (nd.last_type != LAST_NORM)
3702 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003703
3704 nd.flags &= ~LOOKUP_PARENT;
Jan Karac30dabf2012-06-12 16:20:30 +02003705 error = mnt_want_write(nd.path.mnt);
3706 if (error)
3707 goto exit1;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003708retry_deleg:
Jan Blunck4ac91372008-02-14 19:34:32 -08003709 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08003710 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 error = PTR_ERR(dentry);
3712 if (!IS_ERR(dentry)) {
3713 /* Why not before? Because we want correct error value */
Török Edwin50338b82011-06-16 00:06:14 +03003714 if (nd.last.name[nd.last.len])
3715 goto slashes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 inode = dentry->d_inode;
David Howellsb18825a2013-09-12 19:22:53 +01003717 if (d_is_negative(dentry))
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04003718 goto slashes;
3719 ihold(inode);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003720 error = security_path_unlink(&nd.path, dentry);
3721 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02003722 goto exit2;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003723 error = vfs_unlink(nd.path.dentry->d_inode, dentry, &delegated_inode);
Jan Karac30dabf2012-06-12 16:20:30 +02003724exit2:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 dput(dentry);
3726 }
Jan Blunck4ac91372008-02-14 19:34:32 -08003727 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 if (inode)
3729 iput(inode); /* truncate the inode here */
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003730 inode = NULL;
3731 if (delegated_inode) {
J. Bruce Fields5a146962012-08-28 07:50:40 -07003732 error = break_deleg_wait(&delegated_inode);
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003733 if (!error)
3734 goto retry_deleg;
3735 }
Jan Karac30dabf2012-06-12 16:20:30 +02003736 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003738 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 putname(name);
Jeff Layton5d18f812012-12-20 16:38:04 -05003740 if (retry_estale(error, lookup_flags)) {
3741 lookup_flags |= LOOKUP_REVAL;
3742 inode = NULL;
3743 goto retry;
3744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 return error;
3746
3747slashes:
David Howellsb18825a2013-09-12 19:22:53 +01003748 if (d_is_negative(dentry))
3749 error = -ENOENT;
3750 else if (d_is_directory(dentry) || d_is_autodir(dentry))
3751 error = -EISDIR;
3752 else
3753 error = -ENOTDIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 goto exit2;
3755}
3756
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003757SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003758{
3759 if ((flag & ~AT_REMOVEDIR) != 0)
3760 return -EINVAL;
3761
3762 if (flag & AT_REMOVEDIR)
3763 return do_rmdir(dfd, pathname);
3764
3765 return do_unlinkat(dfd, pathname);
3766}
3767
Heiko Carstens3480b252009-01-14 14:14:16 +01003768SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003769{
3770 return do_unlinkat(AT_FDCWD, pathname);
3771}
3772
Miklos Szeredidb2e7472008-06-24 16:50:16 +02003773int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003775 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776
3777 if (error)
3778 return error;
3779
Al Viroacfa4382008-12-04 10:06:33 -05003780 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 return -EPERM;
3782
3783 error = security_inode_symlink(dir, dentry, oldname);
3784 if (error)
3785 return error;
3786
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003788 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003789 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 return error;
3791}
3792
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003793SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
3794 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795{
Al Viro2ad94ae2008-07-21 09:32:51 -04003796 int error;
Jeff Layton91a27b22012-10-10 15:25:28 -04003797 struct filename *from;
Dave Hansen6902d922006-09-30 23:29:01 -07003798 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003799 struct path path;
Jeff Laytonf46d3562012-12-11 12:10:08 -05003800 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801
3802 from = getname(oldname);
Al Viro2ad94ae2008-07-21 09:32:51 -04003803 if (IS_ERR(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 return PTR_ERR(from);
Jeff Laytonf46d3562012-12-11 12:10:08 -05003805retry:
3806 dentry = user_path_create(newdfd, newname, &path, lookup_flags);
Dave Hansen6902d922006-09-30 23:29:01 -07003807 error = PTR_ERR(dentry);
3808 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003809 goto out_putname;
Dave Hansen6902d922006-09-30 23:29:01 -07003810
Jeff Layton91a27b22012-10-10 15:25:28 -04003811 error = security_path_symlink(&path, dentry, from->name);
Al Viroa8104a92012-07-20 02:25:00 +04003812 if (!error)
Jeff Layton91a27b22012-10-10 15:25:28 -04003813 error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
Al Viro921a1652012-07-20 01:15:31 +04003814 done_path_create(&path, dentry);
Jeff Laytonf46d3562012-12-11 12:10:08 -05003815 if (retry_estale(error, lookup_flags)) {
3816 lookup_flags |= LOOKUP_REVAL;
3817 goto retry;
3818 }
Dave Hansen6902d922006-09-30 23:29:01 -07003819out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 putname(from);
3821 return error;
3822}
3823
Heiko Carstens3480b252009-01-14 14:14:16 +01003824SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003825{
3826 return sys_symlinkat(oldname, AT_FDCWD, newname);
3827}
3828
J. Bruce Fields146a8592011-09-20 17:14:31 -04003829/**
3830 * vfs_link - create a new link
3831 * @old_dentry: object to be linked
3832 * @dir: new parent
3833 * @new_dentry: where to create the new link
3834 * @delegated_inode: returns inode needing a delegation break
3835 *
3836 * The caller must hold dir->i_mutex
3837 *
3838 * If vfs_link discovers a delegation on the to-be-linked file in need
3839 * of breaking, it will return -EWOULDBLOCK and return a reference to the
3840 * inode in delegated_inode. The caller should then break the delegation
3841 * and retry. Because breaking a delegation may take a long time, the
3842 * caller should drop the i_mutex before doing so.
3843 *
3844 * Alternatively, a caller may pass NULL for delegated_inode. This may
3845 * be appropriate for callers that expect the underlying filesystem not
3846 * to be NFS exported.
3847 */
3848int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849{
3850 struct inode *inode = old_dentry->d_inode;
Al Viro8de52772012-02-06 12:45:27 -05003851 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 int error;
3853
3854 if (!inode)
3855 return -ENOENT;
3856
Miklos Szeredia95164d2008-07-30 15:08:48 +02003857 error = may_create(dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 if (error)
3859 return error;
3860
3861 if (dir->i_sb != inode->i_sb)
3862 return -EXDEV;
3863
3864 /*
3865 * A link to an append-only or immutable file cannot be created.
3866 */
3867 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3868 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05003869 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003871 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 return -EPERM;
3873
3874 error = security_inode_link(old_dentry, dir, new_dentry);
3875 if (error)
3876 return error;
3877
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003878 mutex_lock(&inode->i_mutex);
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05303879 /* Make sure we don't allow creating hardlink to an unlinked file */
Al Virof4e0c302013-06-11 08:34:36 +04003880 if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05303881 error = -ENOENT;
Al Viro8de52772012-02-06 12:45:27 -05003882 else if (max_links && inode->i_nlink >= max_links)
3883 error = -EMLINK;
J. Bruce Fields146a8592011-09-20 17:14:31 -04003884 else {
3885 error = try_break_deleg(inode, delegated_inode);
3886 if (!error)
3887 error = dir->i_op->link(old_dentry, dir, new_dentry);
3888 }
Al Virof4e0c302013-06-11 08:34:36 +04003889
3890 if (!error && (inode->i_state & I_LINKABLE)) {
3891 spin_lock(&inode->i_lock);
3892 inode->i_state &= ~I_LINKABLE;
3893 spin_unlock(&inode->i_lock);
3894 }
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003895 mutex_unlock(&inode->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07003896 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003897 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898 return error;
3899}
3900
3901/*
3902 * Hardlinks are often used in delicate situations. We avoid
3903 * security-related surprises by not following symlinks on the
3904 * newname. --KAB
3905 *
3906 * We don't follow them on the oldname either to be compatible
3907 * with linux 2.0, and to avoid hard-linking to directories
3908 * and other special files. --ADM
3909 */
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003910SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
3911 int, newdfd, const char __user *, newname, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912{
3913 struct dentry *new_dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003914 struct path old_path, new_path;
J. Bruce Fields146a8592011-09-20 17:14:31 -04003915 struct inode *delegated_inode = NULL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303916 int how = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303919 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003920 return -EINVAL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303921 /*
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07003922 * To use null names we require CAP_DAC_READ_SEARCH
3923 * This ensures that not everyone will be able to create
3924 * handlink using the passed filedescriptor.
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303925 */
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07003926 if (flags & AT_EMPTY_PATH) {
3927 if (!capable(CAP_DAC_READ_SEARCH))
3928 return -ENOENT;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303929 how = LOOKUP_EMPTY;
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07003930 }
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003931
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303932 if (flags & AT_SYMLINK_FOLLOW)
3933 how |= LOOKUP_FOLLOW;
Jeff Layton442e31c2012-12-20 16:15:38 -05003934retry:
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303935 error = user_path_at(olddfd, oldname, how, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04003937 return error;
3938
Jeff Layton442e31c2012-12-20 16:15:38 -05003939 new_dentry = user_path_create(newdfd, newname, &new_path,
3940 (how & LOOKUP_REVAL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003942 if (IS_ERR(new_dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003943 goto out;
3944
3945 error = -EXDEV;
3946 if (old_path.mnt != new_path.mnt)
3947 goto out_dput;
Kees Cook800179c2012-07-25 17:29:07 -07003948 error = may_linkat(&old_path);
3949 if (unlikely(error))
3950 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04003951 error = security_path_link(old_path.dentry, &new_path, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003952 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04003953 goto out_dput;
J. Bruce Fields146a8592011-09-20 17:14:31 -04003954 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode);
Dave Hansen75c3f292008-02-15 14:37:45 -08003955out_dput:
Al Viro921a1652012-07-20 01:15:31 +04003956 done_path_create(&new_path, new_dentry);
J. Bruce Fields146a8592011-09-20 17:14:31 -04003957 if (delegated_inode) {
3958 error = break_deleg_wait(&delegated_inode);
Oleg Drokind22e6332014-01-31 15:41:58 -05003959 if (!error) {
3960 path_put(&old_path);
J. Bruce Fields146a8592011-09-20 17:14:31 -04003961 goto retry;
Oleg Drokind22e6332014-01-31 15:41:58 -05003962 }
J. Bruce Fields146a8592011-09-20 17:14:31 -04003963 }
Jeff Layton442e31c2012-12-20 16:15:38 -05003964 if (retry_estale(error, how)) {
Oleg Drokind22e6332014-01-31 15:41:58 -05003965 path_put(&old_path);
Jeff Layton442e31c2012-12-20 16:15:38 -05003966 how |= LOOKUP_REVAL;
3967 goto retry;
3968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969out:
Al Viro2d8f3032008-07-22 09:59:21 -04003970 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971
3972 return error;
3973}
3974
Heiko Carstens3480b252009-01-14 14:14:16 +01003975SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003976{
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003977 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003978}
3979
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980/*
3981 * The worst of all namespace operations - renaming directory. "Perverted"
3982 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
3983 * Problems:
3984 * a) we can get into loop creation. Check is done in is_subdir().
3985 * b) race potential - two innocent renames can create a loop together.
3986 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08003987 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 * story.
J. Bruce Fields6cedba82012-03-05 11:40:41 -05003989 * c) we have to lock _four_ objects - parents and victim (if it exists),
3990 * and source (if it is not a directory).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003991 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 * whether the target exists). Solution: try to be smart with locking
3993 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08003994 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 * move will be locked. Thus we can rank directories by the tree
3996 * (ancestors first) and rank all non-directories after them.
3997 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08003998 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 * HOWEVER, it relies on the assumption that any object with ->lookup()
4000 * has no more than 1 dentry. If "hybrid" objects will ever appear,
4001 * we'd better make sure that there's no link(2) for them.
Sage Weile4eaac02011-05-24 13:06:07 -07004002 * d) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004003 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05004005 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 * locking].
4007 */
Adrian Bunk75c96f82005-05-05 16:16:09 -07004008static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
4009 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010{
4011 int error = 0;
Sage Weil9055cba2011-05-24 13:06:12 -07004012 struct inode *target = new_dentry->d_inode;
Al Viro8de52772012-02-06 12:45:27 -05004013 unsigned max_links = new_dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014
4015 /*
4016 * If we are going to change the parent - check write permissions,
4017 * we'll need to flip '..'.
4018 */
4019 if (new_dir != old_dir) {
Al Virof419a2e2008-07-22 00:07:17 -04004020 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 if (error)
4022 return error;
4023 }
4024
4025 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
4026 if (error)
4027 return error;
4028
Al Viro1d2ef592011-09-14 18:55:41 +01004029 dget(new_dentry);
Al Virod83c49f2010-04-30 17:17:09 -04004030 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004031 mutex_lock(&target->i_mutex);
Sage Weil9055cba2011-05-24 13:06:12 -07004032
4033 error = -EBUSY;
4034 if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
4035 goto out;
4036
Al Viro8de52772012-02-06 12:45:27 -05004037 error = -EMLINK;
4038 if (max_links && !target && new_dir != old_dir &&
4039 new_dir->i_nlink >= max_links)
4040 goto out;
4041
Sage Weil3cebde22011-05-29 21:20:59 -07004042 if (target)
4043 shrink_dcache_parent(new_dentry);
Sage Weil9055cba2011-05-24 13:06:12 -07004044 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
4045 if (error)
4046 goto out;
4047
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 if (target) {
Sage Weil9055cba2011-05-24 13:06:12 -07004049 target->i_flags |= S_DEAD;
4050 dont_mount(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 }
Sage Weil9055cba2011-05-24 13:06:12 -07004052out:
4053 if (target)
4054 mutex_unlock(&target->i_mutex);
Al Viro1d2ef592011-09-14 18:55:41 +01004055 dput(new_dentry);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07004056 if (!error)
Mark Fasheh349457c2006-09-08 14:22:21 -07004057 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
4058 d_move(old_dentry,new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 return error;
4060}
4061
Adrian Bunk75c96f82005-05-05 16:16:09 -07004062static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004063 struct inode *new_dir, struct dentry *new_dentry,
4064 struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065{
Sage Weil51892bb2011-05-24 13:06:13 -07004066 struct inode *target = new_dentry->d_inode;
J. Bruce Fields6cedba82012-03-05 11:40:41 -05004067 struct inode *source = old_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 int error;
4069
4070 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
4071 if (error)
4072 return error;
4073
4074 dget(new_dentry);
J. Bruce Fields6cedba82012-03-05 11:40:41 -05004075 lock_two_nondirectories(source, target);
Sage Weil51892bb2011-05-24 13:06:13 -07004076
4077 error = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
Sage Weil51892bb2011-05-24 13:06:13 -07004079 goto out;
4080
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004081 error = try_break_deleg(source, delegated_inode);
4082 if (error)
4083 goto out;
4084 if (target) {
4085 error = try_break_deleg(target, delegated_inode);
4086 if (error)
4087 goto out;
4088 }
Sage Weil51892bb2011-05-24 13:06:13 -07004089 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
4090 if (error)
4091 goto out;
4092
4093 if (target)
4094 dont_mount(new_dentry);
4095 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
4096 d_move(old_dentry, new_dentry);
4097out:
J. Bruce Fields6cedba82012-03-05 11:40:41 -05004098 unlock_two_nondirectories(source, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 dput(new_dentry);
4100 return error;
4101}
4102
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004103/**
4104 * vfs_rename - rename a filesystem object
4105 * @old_dir: parent of source
4106 * @old_dentry: source
4107 * @new_dir: parent of destination
4108 * @new_dentry: destination
4109 * @delegated_inode: returns an inode needing a delegation break
4110 *
4111 * The caller must hold multiple mutexes--see lock_rename()).
4112 *
4113 * If vfs_rename discovers a delegation in need of breaking at either
4114 * the source or destination, it will return -EWOULDBLOCK and return a
4115 * reference to the inode in delegated_inode. The caller should then
4116 * break the delegation and retry. Because breaking a delegation may
4117 * take a long time, the caller should drop all locks before doing
4118 * so.
4119 *
4120 * Alternatively, a caller may pass NULL for delegated_inode. This may
4121 * be appropriate for callers that expect the underlying filesystem not
4122 * to be NFS exported.
4123 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004125 struct inode *new_dir, struct dentry *new_dentry,
4126 struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127{
4128 int error;
David Howellsb18825a2013-09-12 19:22:53 +01004129 int is_dir = d_is_directory(old_dentry) || d_is_autodir(old_dentry);
Eric Paris59b0df22010-02-08 12:53:52 -05004130 const unsigned char *old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131
4132 if (old_dentry->d_inode == new_dentry->d_inode)
4133 return 0;
4134
4135 error = may_delete(old_dir, old_dentry, is_dir);
4136 if (error)
4137 return error;
4138
4139 if (!new_dentry->d_inode)
Miklos Szeredia95164d2008-07-30 15:08:48 +02004140 error = may_create(new_dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 else
4142 error = may_delete(new_dir, new_dentry, is_dir);
4143 if (error)
4144 return error;
4145
Al Viroacfa4382008-12-04 10:06:33 -05004146 if (!old_dir->i_op->rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 return -EPERM;
4148
Robert Love0eeca282005-07-12 17:06:03 -04004149 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
4150
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 if (is_dir)
4152 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
4153 else
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004154 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry,delegated_inode);
Al Viro123df292009-12-25 04:57:57 -05004155 if (!error)
4156 fsnotify_move(old_dir, new_dir, old_name, is_dir,
Al Viro5a190ae2007-06-07 12:19:32 -04004157 new_dentry->d_inode, old_dentry);
Robert Love0eeca282005-07-12 17:06:03 -04004158 fsnotify_oldname_free(old_name);
4159
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 return error;
4161}
4162
Heiko Carstens2e4d0922009-01-14 14:14:31 +01004163SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4164 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165{
Al Viro2ad94ae2008-07-21 09:32:51 -04004166 struct dentry *old_dir, *new_dir;
4167 struct dentry *old_dentry, *new_dentry;
4168 struct dentry *trap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 struct nameidata oldnd, newnd;
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004170 struct inode *delegated_inode = NULL;
Jeff Layton91a27b22012-10-10 15:25:28 -04004171 struct filename *from;
4172 struct filename *to;
Jeff Laytonc6a94282012-12-11 12:10:10 -05004173 unsigned int lookup_flags = 0;
4174 bool should_retry = false;
Al Viro2ad94ae2008-07-21 09:32:51 -04004175 int error;
Jeff Laytonc6a94282012-12-11 12:10:10 -05004176retry:
4177 from = user_path_parent(olddfd, oldname, &oldnd, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04004178 if (IS_ERR(from)) {
4179 error = PTR_ERR(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 goto exit;
Jeff Layton91a27b22012-10-10 15:25:28 -04004181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182
Jeff Laytonc6a94282012-12-11 12:10:10 -05004183 to = user_path_parent(newdfd, newname, &newnd, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04004184 if (IS_ERR(to)) {
4185 error = PTR_ERR(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 goto exit1;
Jeff Layton91a27b22012-10-10 15:25:28 -04004187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188
4189 error = -EXDEV;
Jan Blunck4ac91372008-02-14 19:34:32 -08004190 if (oldnd.path.mnt != newnd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 goto exit2;
4192
Jan Blunck4ac91372008-02-14 19:34:32 -08004193 old_dir = oldnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 error = -EBUSY;
4195 if (oldnd.last_type != LAST_NORM)
4196 goto exit2;
4197
Jan Blunck4ac91372008-02-14 19:34:32 -08004198 new_dir = newnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 if (newnd.last_type != LAST_NORM)
4200 goto exit2;
4201
Jan Karac30dabf2012-06-12 16:20:30 +02004202 error = mnt_want_write(oldnd.path.mnt);
4203 if (error)
4204 goto exit2;
4205
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004206 oldnd.flags &= ~LOOKUP_PARENT;
4207 newnd.flags &= ~LOOKUP_PARENT;
OGAWA Hirofumi4e9ed2f2008-10-16 07:50:29 +09004208 newnd.flags |= LOOKUP_RENAME_TARGET;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004209
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004210retry_deleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 trap = lock_rename(new_dir, old_dir);
4212
Christoph Hellwig49705b72005-11-08 21:35:06 -08004213 old_dentry = lookup_hash(&oldnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 error = PTR_ERR(old_dentry);
4215 if (IS_ERR(old_dentry))
4216 goto exit3;
4217 /* source must exist */
4218 error = -ENOENT;
David Howellsb18825a2013-09-12 19:22:53 +01004219 if (d_is_negative(old_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 goto exit4;
4221 /* unless the source is a directory trailing slashes give -ENOTDIR */
David Howellsb18825a2013-09-12 19:22:53 +01004222 if (!d_is_directory(old_dentry) && !d_is_autodir(old_dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 error = -ENOTDIR;
4224 if (oldnd.last.name[oldnd.last.len])
4225 goto exit4;
4226 if (newnd.last.name[newnd.last.len])
4227 goto exit4;
4228 }
4229 /* source should not be ancestor of target */
4230 error = -EINVAL;
4231 if (old_dentry == trap)
4232 goto exit4;
Christoph Hellwig49705b72005-11-08 21:35:06 -08004233 new_dentry = lookup_hash(&newnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 error = PTR_ERR(new_dentry);
4235 if (IS_ERR(new_dentry))
4236 goto exit4;
4237 /* target should not be an ancestor of source */
4238 error = -ENOTEMPTY;
4239 if (new_dentry == trap)
4240 goto exit5;
4241
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004242 error = security_path_rename(&oldnd.path, old_dentry,
4243 &newnd.path, new_dentry);
4244 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02004245 goto exit5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 error = vfs_rename(old_dir->d_inode, old_dentry,
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004247 new_dir->d_inode, new_dentry,
4248 &delegated_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249exit5:
4250 dput(new_dentry);
4251exit4:
4252 dput(old_dentry);
4253exit3:
4254 unlock_rename(new_dir, old_dir);
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004255 if (delegated_inode) {
4256 error = break_deleg_wait(&delegated_inode);
4257 if (!error)
4258 goto retry_deleg;
4259 }
Jan Karac30dabf2012-06-12 16:20:30 +02004260 mnt_drop_write(oldnd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261exit2:
Jeff Laytonc6a94282012-12-11 12:10:10 -05004262 if (retry_estale(error, lookup_flags))
4263 should_retry = true;
Jan Blunck1d957f92008-02-14 19:34:35 -08004264 path_put(&newnd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04004265 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08004267 path_put(&oldnd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 putname(from);
Jeff Laytonc6a94282012-12-11 12:10:10 -05004269 if (should_retry) {
4270 should_retry = false;
4271 lookup_flags |= LOOKUP_REVAL;
4272 goto retry;
4273 }
Al Viro2ad94ae2008-07-21 09:32:51 -04004274exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 return error;
4276}
4277
Heiko Carstensa26eab22009-01-14 14:14:17 +01004278SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004279{
4280 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
4281}
4282
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
4284{
4285 int len;
4286
4287 len = PTR_ERR(link);
4288 if (IS_ERR(link))
4289 goto out;
4290
4291 len = strlen(link);
4292 if (len > (unsigned) buflen)
4293 len = buflen;
4294 if (copy_to_user(buffer, link, len))
4295 len = -EFAULT;
4296out:
4297 return len;
4298}
4299
4300/*
4301 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
4302 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
4303 * using) it for any given inode is up to filesystem.
4304 */
4305int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4306{
4307 struct nameidata nd;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004308 void *cookie;
Marcin Slusarz694a1762008-06-09 16:40:37 -07004309 int res;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004310
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 nd.depth = 0;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004312 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
Marcin Slusarz694a1762008-06-09 16:40:37 -07004313 if (IS_ERR(cookie))
4314 return PTR_ERR(cookie);
4315
4316 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
4317 if (dentry->d_inode->i_op->put_link)
4318 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
4319 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320}
4321
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322/* get the link contents into pagecache */
4323static char *page_getlink(struct dentry * dentry, struct page **ppage)
4324{
Duane Griffinebd09ab2008-12-19 20:47:12 +00004325 char *kaddr;
4326 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 struct address_space *mapping = dentry->d_inode->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -07004328 page = read_mapping_page(mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 if (IS_ERR(page))
Nick Piggin6fe69002007-05-06 14:49:04 -07004330 return (char*)page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 *ppage = page;
Duane Griffinebd09ab2008-12-19 20:47:12 +00004332 kaddr = kmap(page);
4333 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
4334 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335}
4336
4337int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4338{
4339 struct page *page = NULL;
4340 char *s = page_getlink(dentry, &page);
4341 int res = vfs_readlink(dentry,buffer,buflen,s);
4342 if (page) {
4343 kunmap(page);
4344 page_cache_release(page);
4345 }
4346 return res;
4347}
4348
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004349void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004351 struct page *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 nd_set_link(nd, page_getlink(dentry, &page));
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004353 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354}
4355
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004356void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004358 struct page *page = cookie;
4359
4360 if (page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 kunmap(page);
4362 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 }
4364}
4365
Nick Piggin54566b22009-01-04 12:00:53 -08004366/*
4367 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
4368 */
4369int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370{
4371 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004372 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07004373 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08004374 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 char *kaddr;
Nick Piggin54566b22009-01-04 12:00:53 -08004376 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
4377 if (nofs)
4378 flags |= AOP_FLAG_NOFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379
NeilBrown7e53cac2006-03-25 03:07:57 -08004380retry:
Nick Pigginafddba42007-10-16 01:25:01 -07004381 err = pagecache_write_begin(NULL, mapping, 0, len-1,
Nick Piggin54566b22009-01-04 12:00:53 -08004382 flags, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07004384 goto fail;
4385
Cong Wange8e3c3d2011-11-25 23:14:27 +08004386 kaddr = kmap_atomic(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 memcpy(kaddr, symname, len-1);
Cong Wange8e3c3d2011-11-25 23:14:27 +08004388 kunmap_atomic(kaddr);
Nick Pigginafddba42007-10-16 01:25:01 -07004389
4390 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4391 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 if (err < 0)
4393 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07004394 if (err < len-1)
4395 goto retry;
4396
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 mark_inode_dirty(inode);
4398 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399fail:
4400 return err;
4401}
4402
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004403int page_symlink(struct inode *inode, const char *symname, int len)
4404{
4405 return __page_symlink(inode, symname, len,
Nick Piggin54566b22009-01-04 12:00:53 -08004406 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004407}
4408
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08004409const struct inode_operations page_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 .readlink = generic_readlink,
4411 .follow_link = page_follow_link_light,
4412 .put_link = page_put_link,
4413};
4414
Al Viro2d8f3032008-07-22 09:59:21 -04004415EXPORT_SYMBOL(user_path_at);
David Howellscc53ce52011-01-14 18:45:26 +00004416EXPORT_SYMBOL(follow_down_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417EXPORT_SYMBOL(follow_down);
4418EXPORT_SYMBOL(follow_up);
Al Virof6d2ac52012-08-26 12:55:54 -04004419EXPORT_SYMBOL(get_write_access); /* nfsd */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421EXPORT_SYMBOL(lookup_one_len);
4422EXPORT_SYMBOL(page_follow_link_light);
4423EXPORT_SYMBOL(page_put_link);
4424EXPORT_SYMBOL(page_readlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004425EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426EXPORT_SYMBOL(page_symlink);
4427EXPORT_SYMBOL(page_symlink_inode_operations);
Al Virod1811462008-08-02 00:49:18 -04004428EXPORT_SYMBOL(kern_path);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07004429EXPORT_SYMBOL(vfs_path_lookup);
Al Virof419a2e2008-07-22 00:07:17 -04004430EXPORT_SYMBOL(inode_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431EXPORT_SYMBOL(unlock_rename);
4432EXPORT_SYMBOL(vfs_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433EXPORT_SYMBOL(vfs_link);
4434EXPORT_SYMBOL(vfs_mkdir);
4435EXPORT_SYMBOL(vfs_mknod);
4436EXPORT_SYMBOL(generic_permission);
4437EXPORT_SYMBOL(vfs_readlink);
4438EXPORT_SYMBOL(vfs_rename);
4439EXPORT_SYMBOL(vfs_rmdir);
4440EXPORT_SYMBOL(vfs_symlink);
4441EXPORT_SYMBOL(vfs_unlink);
4442EXPORT_SYMBOL(dentry_unhash);
4443EXPORT_SYMBOL(generic_readlink);