blob: 8274c8d39b037ed8059b6253f3146ff140e0aab7 [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 */
Andy Lutomirski5bacea82014-06-10 12:45:42 -0700335 if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
Al Virod594e7e2011-06-20 19:55:42 -0400336 return 0;
337 if (!(mask & MAY_WRITE))
Andy Lutomirski5bacea82014-06-10 12:45:42 -0700338 if (capable_wrt_inode_uidgid(inode,
339 CAP_DAC_READ_SEARCH))
Al Virod594e7e2011-06-20 19:55:42 -0400340 return 0;
341 return -EACCES;
342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 /*
344 * Read/write DACs are always overridable.
Al Virod594e7e2011-06-20 19:55:42 -0400345 * Executable DACs are overridable when there is
346 * at least one exec bit set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 */
Al Virod594e7e2011-06-20 19:55:42 -0400348 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
Andy Lutomirski5bacea82014-06-10 12:45:42 -0700349 if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return 0;
351
352 /*
353 * Searching includes executable on directories, else just read.
354 */
Serge E. Hallyn7ea66002009-12-29 14:50:19 -0600355 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
Al Virod594e7e2011-06-20 19:55:42 -0400356 if (mask == MAY_READ)
Andy Lutomirski5bacea82014-06-10 12:45:42 -0700357 if (capable_wrt_inode_uidgid(inode, CAP_DAC_READ_SEARCH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return 0;
359
360 return -EACCES;
361}
362
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700363/*
364 * We _really_ want to just do "generic_permission()" without
365 * even looking at the inode->i_op values. So we keep a cache
366 * flag in inode->i_opflags, that says "this has not special
367 * permission function, use the fast case".
368 */
369static inline int do_inode_permission(struct inode *inode, int mask)
370{
371 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
372 if (likely(inode->i_op->permission))
373 return inode->i_op->permission(inode, mask);
374
375 /* This gets set once for the inode lifetime */
376 spin_lock(&inode->i_lock);
377 inode->i_opflags |= IOP_FASTPERM;
378 spin_unlock(&inode->i_lock);
379 }
380 return generic_permission(inode, mask);
381}
382
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200383/**
David Howells0bdaea92012-06-25 12:55:46 +0100384 * __inode_permission - Check for access rights to a given inode
385 * @inode: Inode to check permission on
386 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200387 *
David Howells0bdaea92012-06-25 12:55:46 +0100388 * Check for read/write/execute permissions on an inode.
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530389 *
390 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
David Howells0bdaea92012-06-25 12:55:46 +0100391 *
392 * This does not check for a read-only file system. You probably want
393 * inode_permission().
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200394 */
David Howells0bdaea92012-06-25 12:55:46 +0100395int __inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Al Viroe6305c42008-07-15 21:03:57 -0400397 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700399 if (unlikely(mask & MAY_WRITE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 /*
401 * Nobody gets write access to an immutable file.
402 */
403 if (IS_IMMUTABLE(inode))
404 return -EACCES;
405 }
406
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700407 retval = do_inode_permission(inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 if (retval)
409 return retval;
410
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700411 retval = devcgroup_inode_permission(inode, mask);
412 if (retval)
413 return retval;
414
Eric Parisd09ca732010-07-23 11:43:57 -0400415 return security_inode_permission(inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416}
417
Al Virof4d6ff82011-06-19 13:14:21 -0400418/**
David Howells0bdaea92012-06-25 12:55:46 +0100419 * sb_permission - Check superblock-level permissions
420 * @sb: Superblock of inode to check permission on
Randy Dunlap55852632012-08-18 17:39:25 -0700421 * @inode: Inode to check permission on
David Howells0bdaea92012-06-25 12:55:46 +0100422 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
423 *
424 * Separate out file-system wide checks from inode-specific permission checks.
425 */
426static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
427{
428 if (unlikely(mask & MAY_WRITE)) {
429 umode_t mode = inode->i_mode;
430
431 /* Nobody gets write access to a read-only fs. */
432 if ((sb->s_flags & MS_RDONLY) &&
433 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
434 return -EROFS;
435 }
436 return 0;
437}
438
439/**
440 * inode_permission - Check for access rights to a given inode
441 * @inode: Inode to check permission on
442 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
443 *
444 * Check for read/write/execute permissions on an inode. We use fs[ug]id for
445 * this, letting us set arbitrary permissions for filesystem access without
446 * changing the "normal" UIDs which are used for other things.
447 *
448 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
449 */
450int inode_permission(struct inode *inode, int mask)
451{
452 int retval;
453
454 retval = sb_permission(inode->i_sb, inode, mask);
455 if (retval)
456 return retval;
457 return __inode_permission(inode, mask);
458}
459
460/**
Jan Blunck5dd784d02008-02-14 19:34:38 -0800461 * path_get - get a reference to a path
462 * @path: path to get the reference to
463 *
464 * Given a path increment the reference count to the dentry and the vfsmount.
465 */
Al Virodcf787f2013-03-01 23:51:07 -0500466void path_get(const struct path *path)
Jan Blunck5dd784d02008-02-14 19:34:38 -0800467{
468 mntget(path->mnt);
469 dget(path->dentry);
470}
471EXPORT_SYMBOL(path_get);
472
473/**
Jan Blunck1d957f92008-02-14 19:34:35 -0800474 * path_put - put a reference to a path
475 * @path: path to put the reference to
476 *
477 * Given a path decrement the reference count to the dentry and the vfsmount.
478 */
Al Virodcf787f2013-03-01 23:51:07 -0500479void path_put(const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Jan Blunck1d957f92008-02-14 19:34:35 -0800481 dput(path->dentry);
482 mntput(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
Jan Blunck1d957f92008-02-14 19:34:35 -0800484EXPORT_SYMBOL(path_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Al Viro19660af2011-03-25 10:32:48 -0400486/*
Nick Piggin31e6b012011-01-07 17:49:52 +1100487 * Path walking has 2 modes, rcu-walk and ref-walk (see
Al Viro19660af2011-03-25 10:32:48 -0400488 * Documentation/filesystems/path-lookup.txt). In situations when we can't
489 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
490 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
491 * mode. Refcounts are grabbed at the last known good point before rcu-walk
492 * got stuck, so ref-walk may continue from there. If this is not successful
493 * (eg. a seqcount has changed), then failure is returned and it's up to caller
494 * to restart the path walk from the beginning in ref-walk mode.
Nick Piggin31e6b012011-01-07 17:49:52 +1100495 */
Nick Piggin31e6b012011-01-07 17:49:52 +1100496
497/**
Al Viro19660af2011-03-25 10:32:48 -0400498 * unlazy_walk - try to switch to ref-walk mode.
499 * @nd: nameidata pathwalk data
500 * @dentry: child of nd->path.dentry or NULL
Randy Dunlap39191622011-01-08 19:36:21 -0800501 * Returns: 0 on success, -ECHILD on failure
Nick Piggin31e6b012011-01-07 17:49:52 +1100502 *
Al Viro19660af2011-03-25 10:32:48 -0400503 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
504 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
505 * @nd or NULL. Must be called from rcu-walk context.
Nick Piggin31e6b012011-01-07 17:49:52 +1100506 */
Al Viro19660af2011-03-25 10:32:48 -0400507static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
Nick Piggin31e6b012011-01-07 17:49:52 +1100508{
509 struct fs_struct *fs = current->fs;
510 struct dentry *parent = nd->path.dentry;
511
512 BUG_ON(!(nd->flags & LOOKUP_RCU));
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700513
514 /*
Al Viro48a066e2013-09-29 22:06:07 -0400515 * After legitimizing the bastards, terminate_walk()
516 * will do the right thing for non-RCU mode, and all our
517 * subsequent exit cases should rcu_read_unlock()
518 * before returning. Do vfsmount first; if dentry
519 * can't be legitimized, just set nd->path.dentry to NULL
520 * and rely on dput(NULL) being a no-op.
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700521 */
Al Viro48a066e2013-09-29 22:06:07 -0400522 if (!legitimize_mnt(nd->path.mnt, nd->m_seq))
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700523 return -ECHILD;
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700524 nd->flags &= ~LOOKUP_RCU;
Linus Torvalds15570082013-09-02 11:38:06 -0700525
Al Viro48a066e2013-09-29 22:06:07 -0400526 if (!lockref_get_not_dead(&parent->d_lockref)) {
527 nd->path.dentry = NULL;
Al Virod870b4a2013-11-29 01:48:32 -0500528 goto out;
Al Viro48a066e2013-09-29 22:06:07 -0400529 }
530
Linus Torvalds15570082013-09-02 11:38:06 -0700531 /*
532 * For a negative lookup, the lookup sequence point is the parents
533 * sequence point, and it only needs to revalidate the parent dentry.
534 *
535 * For a positive lookup, we need to move both the parent and the
536 * dentry from the RCU domain to be properly refcounted. And the
537 * sequence number in the dentry validates *both* dentry counters,
538 * since we checked the sequence number of the parent after we got
539 * the child sequence number. So we know the parent must still
540 * be valid if the child sequence number is still valid.
541 */
Al Viro19660af2011-03-25 10:32:48 -0400542 if (!dentry) {
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700543 if (read_seqcount_retry(&parent->d_seq, nd->seq))
544 goto out;
Al Viro19660af2011-03-25 10:32:48 -0400545 BUG_ON(nd->inode != parent->d_inode);
546 } else {
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700547 if (!lockref_get_not_dead(&dentry->d_lockref))
548 goto out;
549 if (read_seqcount_retry(&dentry->d_seq, nd->seq))
550 goto drop_dentry;
Al Viro19660af2011-03-25 10:32:48 -0400551 }
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700552
553 /*
554 * Sequence counts matched. Now make sure that the root is
555 * still valid and get it if required.
556 */
557 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
558 spin_lock(&fs->lock);
559 if (nd->root.mnt != fs->root.mnt || nd->root.dentry != fs->root.dentry)
560 goto unlock_and_drop_dentry;
Nick Piggin31e6b012011-01-07 17:49:52 +1100561 path_get(&nd->root);
562 spin_unlock(&fs->lock);
563 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100564
Al Viro8b61e742013-11-08 12:45:01 -0500565 rcu_read_unlock();
Nick Piggin31e6b012011-01-07 17:49:52 +1100566 return 0;
Al Viro19660af2011-03-25 10:32:48 -0400567
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700568unlock_and_drop_dentry:
569 spin_unlock(&fs->lock);
570drop_dentry:
Al Viro8b61e742013-11-08 12:45:01 -0500571 rcu_read_unlock();
Linus Torvalds15570082013-09-02 11:38:06 -0700572 dput(dentry);
Linus Torvaldsd0d27272013-09-10 12:17:49 -0700573 goto drop_root_mnt;
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700574out:
Al Viro8b61e742013-11-08 12:45:01 -0500575 rcu_read_unlock();
Linus Torvaldsd0d27272013-09-10 12:17:49 -0700576drop_root_mnt:
577 if (!(nd->flags & LOOKUP_ROOT))
578 nd->root.mnt = NULL;
Nick Piggin31e6b012011-01-07 17:49:52 +1100579 return -ECHILD;
580}
581
Al Viro4ce16ef32012-06-10 16:10:59 -0400582static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
Nick Piggin34286d62011-01-07 17:49:57 +1100583{
Al Viro4ce16ef32012-06-10 16:10:59 -0400584 return dentry->d_op->d_revalidate(dentry, flags);
Nick Piggin34286d62011-01-07 17:49:57 +1100585}
586
Al Viro9f1fafe2011-03-25 11:00:12 -0400587/**
588 * complete_walk - successful completion of path walk
589 * @nd: pointer nameidata
Jeff Layton39159de2009-12-07 12:01:50 -0500590 *
Al Viro9f1fafe2011-03-25 11:00:12 -0400591 * If we had been in RCU mode, drop out of it and legitimize nd->path.
592 * Revalidate the final result, unless we'd already done that during
593 * the path walk or the filesystem doesn't ask for it. Return 0 on
594 * success, -error on failure. In case of failure caller does not
595 * need to drop nd->path.
Jeff Layton39159de2009-12-07 12:01:50 -0500596 */
Al Viro9f1fafe2011-03-25 11:00:12 -0400597static int complete_walk(struct nameidata *nd)
Jeff Layton39159de2009-12-07 12:01:50 -0500598{
Al Viro16c2cd72011-02-22 15:50:10 -0500599 struct dentry *dentry = nd->path.dentry;
Jeff Layton39159de2009-12-07 12:01:50 -0500600 int status;
Jeff Layton39159de2009-12-07 12:01:50 -0500601
Al Viro9f1fafe2011-03-25 11:00:12 -0400602 if (nd->flags & LOOKUP_RCU) {
603 nd->flags &= ~LOOKUP_RCU;
604 if (!(nd->flags & LOOKUP_ROOT))
605 nd->root.mnt = NULL;
Linus Torvalds15570082013-09-02 11:38:06 -0700606
Al Viro48a066e2013-09-29 22:06:07 -0400607 if (!legitimize_mnt(nd->path.mnt, nd->m_seq)) {
Al Viro8b61e742013-11-08 12:45:01 -0500608 rcu_read_unlock();
Al Viro48a066e2013-09-29 22:06:07 -0400609 return -ECHILD;
610 }
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700611 if (unlikely(!lockref_get_not_dead(&dentry->d_lockref))) {
Al Viro8b61e742013-11-08 12:45:01 -0500612 rcu_read_unlock();
Al Viro48a066e2013-09-29 22:06:07 -0400613 mntput(nd->path.mnt);
Al Viro9f1fafe2011-03-25 11:00:12 -0400614 return -ECHILD;
615 }
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700616 if (read_seqcount_retry(&dentry->d_seq, nd->seq)) {
Al Viro8b61e742013-11-08 12:45:01 -0500617 rcu_read_unlock();
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700618 dput(dentry);
Al Viro48a066e2013-09-29 22:06:07 -0400619 mntput(nd->path.mnt);
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700620 return -ECHILD;
621 }
Al Viro8b61e742013-11-08 12:45:01 -0500622 rcu_read_unlock();
Al Viro9f1fafe2011-03-25 11:00:12 -0400623 }
624
Al Viro16c2cd72011-02-22 15:50:10 -0500625 if (likely(!(nd->flags & LOOKUP_JUMPED)))
Jeff Layton39159de2009-12-07 12:01:50 -0500626 return 0;
627
Jeff Laytonecf3d1f2013-02-20 11:19:05 -0500628 if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
Al Viro16c2cd72011-02-22 15:50:10 -0500629 return 0;
630
Jeff Laytonecf3d1f2013-02-20 11:19:05 -0500631 status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
Jeff Layton39159de2009-12-07 12:01:50 -0500632 if (status > 0)
633 return 0;
634
Al Viro16c2cd72011-02-22 15:50:10 -0500635 if (!status)
Jeff Layton39159de2009-12-07 12:01:50 -0500636 status = -ESTALE;
Al Viro16c2cd72011-02-22 15:50:10 -0500637
Al Viro9f1fafe2011-03-25 11:00:12 -0400638 path_put(&nd->path);
Jeff Layton39159de2009-12-07 12:01:50 -0500639 return status;
640}
641
Al Viro2a737872009-04-07 11:49:53 -0400642static __always_inline void set_root(struct nameidata *nd)
643{
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200644 if (!nd->root.mnt)
645 get_fs_root(current->fs, &nd->root);
Al Viro2a737872009-04-07 11:49:53 -0400646}
647
Al Viro6de88d72009-08-09 01:41:57 +0400648static int link_path_walk(const char *, struct nameidata *);
649
Nick Piggin31e6b012011-01-07 17:49:52 +1100650static __always_inline void set_root_rcu(struct nameidata *nd)
651{
652 if (!nd->root.mnt) {
653 struct fs_struct *fs = current->fs;
Nick Pigginc28cc362011-01-07 17:49:53 +1100654 unsigned seq;
655
656 do {
657 seq = read_seqcount_begin(&fs->seq);
658 nd->root = fs->root;
Tim Chenc1530012011-04-15 11:39:29 -0700659 nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
Nick Pigginc28cc362011-01-07 17:49:53 +1100660 } while (read_seqcount_retry(&fs->seq, seq));
Nick Piggin31e6b012011-01-07 17:49:52 +1100661 }
662}
663
Jan Blunck1d957f92008-02-14 19:34:35 -0800664static void path_put_conditional(struct path *path, struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700665{
666 dput(path->dentry);
Jan Blunck4ac91372008-02-14 19:34:32 -0800667 if (path->mnt != nd->path.mnt)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700668 mntput(path->mnt);
669}
670
Nick Piggin7b9337a2011-01-14 08:42:43 +0000671static inline void path_to_nameidata(const struct path *path,
672 struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700673{
Nick Piggin31e6b012011-01-07 17:49:52 +1100674 if (!(nd->flags & LOOKUP_RCU)) {
675 dput(nd->path.dentry);
676 if (nd->path.mnt != path->mnt)
677 mntput(nd->path.mnt);
Huang Shijie9a229682010-04-02 17:37:13 +0800678 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100679 nd->path.mnt = path->mnt;
Jan Blunck4ac91372008-02-14 19:34:32 -0800680 nd->path.dentry = path->dentry;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700681}
682
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400683/*
684 * Helper to directly jump to a known parsed path from ->follow_link,
685 * caller must have taken a reference to path beforehand.
686 */
687void nd_jump_link(struct nameidata *nd, struct path *path)
688{
689 path_put(&nd->path);
690
691 nd->path = *path;
692 nd->inode = nd->path.dentry->d_inode;
693 nd->flags |= LOOKUP_JUMPED;
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400694}
695
Al Viro574197e2011-03-14 22:20:34 -0400696static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
697{
698 struct inode *inode = link->dentry->d_inode;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400699 if (inode->i_op->put_link)
Al Viro574197e2011-03-14 22:20:34 -0400700 inode->i_op->put_link(link->dentry, nd, cookie);
701 path_put(link);
702}
703
Linus Torvalds561ec642012-10-26 10:05:07 -0700704int sysctl_protected_symlinks __read_mostly = 0;
705int sysctl_protected_hardlinks __read_mostly = 0;
Kees Cook800179c2012-07-25 17:29:07 -0700706
707/**
708 * may_follow_link - Check symlink following for unsafe situations
709 * @link: The path of the symlink
Randy Dunlap55852632012-08-18 17:39:25 -0700710 * @nd: nameidata pathwalk data
Kees Cook800179c2012-07-25 17:29:07 -0700711 *
712 * In the case of the sysctl_protected_symlinks sysctl being enabled,
713 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
714 * in a sticky world-writable directory. This is to protect privileged
715 * processes from failing races against path names that may change out
716 * from under them by way of other users creating malicious symlinks.
717 * It will permit symlinks to be followed only when outside a sticky
718 * world-writable directory, or when the uid of the symlink and follower
719 * match, or when the directory owner matches the symlink's owner.
720 *
721 * Returns 0 if following the symlink is allowed, -ve on error.
722 */
723static inline int may_follow_link(struct path *link, struct nameidata *nd)
724{
725 const struct inode *inode;
726 const struct inode *parent;
727
728 if (!sysctl_protected_symlinks)
729 return 0;
730
731 /* Allowed if owner and follower match. */
732 inode = link->dentry->d_inode;
Eric W. Biederman81abe272012-08-03 09:38:08 -0700733 if (uid_eq(current_cred()->fsuid, inode->i_uid))
Kees Cook800179c2012-07-25 17:29:07 -0700734 return 0;
735
736 /* Allowed if parent directory not sticky and world-writable. */
737 parent = nd->path.dentry->d_inode;
738 if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
739 return 0;
740
741 /* Allowed if parent directory and link owner match. */
Eric W. Biederman81abe272012-08-03 09:38:08 -0700742 if (uid_eq(parent->i_uid, inode->i_uid))
Kees Cook800179c2012-07-25 17:29:07 -0700743 return 0;
744
Sasha Levinffd8d102012-10-04 19:56:40 -0400745 audit_log_link_denied("follow_link", link);
Kees Cook800179c2012-07-25 17:29:07 -0700746 path_put_conditional(link, nd);
747 path_put(&nd->path);
748 return -EACCES;
749}
750
751/**
752 * safe_hardlink_source - Check for safe hardlink conditions
753 * @inode: the source inode to hardlink from
754 *
755 * Return false if at least one of the following conditions:
756 * - inode is not a regular file
757 * - inode is setuid
758 * - inode is setgid and group-exec
759 * - access failure for read and write
760 *
761 * Otherwise returns true.
762 */
763static bool safe_hardlink_source(struct inode *inode)
764{
765 umode_t mode = inode->i_mode;
766
767 /* Special files should not get pinned to the filesystem. */
768 if (!S_ISREG(mode))
769 return false;
770
771 /* Setuid files should not get pinned to the filesystem. */
772 if (mode & S_ISUID)
773 return false;
774
775 /* Executable setgid files should not get pinned to the filesystem. */
776 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
777 return false;
778
779 /* Hardlinking to unreadable or unwritable sources is dangerous. */
780 if (inode_permission(inode, MAY_READ | MAY_WRITE))
781 return false;
782
783 return true;
784}
785
786/**
787 * may_linkat - Check permissions for creating a hardlink
788 * @link: the source to hardlink from
789 *
790 * Block hardlink when all of:
791 * - sysctl_protected_hardlinks enabled
792 * - fsuid does not match inode
793 * - hardlink source is unsafe (see safe_hardlink_source() above)
794 * - not CAP_FOWNER
795 *
796 * Returns 0 if successful, -ve on error.
797 */
798static int may_linkat(struct path *link)
799{
800 const struct cred *cred;
801 struct inode *inode;
802
803 if (!sysctl_protected_hardlinks)
804 return 0;
805
806 cred = current_cred();
807 inode = link->dentry->d_inode;
808
809 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
810 * otherwise, it must be a safe source.
811 */
Eric W. Biederman81abe272012-08-03 09:38:08 -0700812 if (uid_eq(cred->fsuid, inode->i_uid) || safe_hardlink_source(inode) ||
Kees Cook800179c2012-07-25 17:29:07 -0700813 capable(CAP_FOWNER))
814 return 0;
815
Kees Cooka51d9ea2012-07-25 17:29:08 -0700816 audit_log_link_denied("linkat", link);
Kees Cook800179c2012-07-25 17:29:07 -0700817 return -EPERM;
818}
819
Al Virodef4af32009-12-26 08:37:05 -0500820static __always_inline int
Al Viro574197e2011-03-14 22:20:34 -0400821follow_link(struct path *link, struct nameidata *nd, void **p)
Ian Kent051d3812006-03-27 01:14:53 -0800822{
Nick Piggin7b9337a2011-01-14 08:42:43 +0000823 struct dentry *dentry = link->dentry;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400824 int error;
825 char *s;
Ian Kent051d3812006-03-27 01:14:53 -0800826
Al Viro844a3912011-02-15 00:38:26 -0500827 BUG_ON(nd->flags & LOOKUP_RCU);
828
Al Viro0e794582011-03-16 02:45:02 -0400829 if (link->mnt == nd->path.mnt)
830 mntget(link->mnt);
831
Al Viro6d7b5aa2012-06-10 04:15:17 -0400832 error = -ELOOP;
833 if (unlikely(current->total_link_count >= 40))
834 goto out_put_nd_path;
835
Al Viro574197e2011-03-14 22:20:34 -0400836 cond_resched();
837 current->total_link_count++;
838
Al Viro68ac1232012-03-15 08:21:57 -0400839 touch_atime(link);
Ian Kent051d3812006-03-27 01:14:53 -0800840 nd_set_link(nd, NULL);
841
Al Viro36f3b4f2011-02-22 21:24:38 -0500842 error = security_inode_follow_link(link->dentry, nd);
Al Viro6d7b5aa2012-06-10 04:15:17 -0400843 if (error)
844 goto out_put_nd_path;
Al Viro36f3b4f2011-02-22 21:24:38 -0500845
Al Viro86acdca12009-12-22 23:45:11 -0500846 nd->last_type = LAST_BIND;
Al Virodef4af32009-12-26 08:37:05 -0500847 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
848 error = PTR_ERR(*p);
Al Viro6d7b5aa2012-06-10 04:15:17 -0400849 if (IS_ERR(*p))
Christoph Hellwig408ef012012-06-18 10:47:03 -0400850 goto out_put_nd_path;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400851
852 error = 0;
853 s = nd_get_link(nd);
854 if (s) {
Al Viro443ed252013-09-10 12:00:43 -0400855 if (unlikely(IS_ERR(s))) {
856 path_put(&nd->path);
857 put_link(nd, link, *p);
858 return PTR_ERR(s);
859 }
860 if (*s == '/') {
861 set_root(nd);
862 path_put(&nd->path);
863 nd->path = nd->root;
864 path_get(&nd->root);
865 nd->flags |= LOOKUP_JUMPED;
866 }
867 nd->inode = nd->path.dentry->d_inode;
868 error = link_path_walk(s, nd);
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400869 if (unlikely(error))
870 put_link(nd, link, *p);
Ian Kent051d3812006-03-27 01:14:53 -0800871 }
Al Viro6d7b5aa2012-06-10 04:15:17 -0400872
873 return error;
874
875out_put_nd_path:
Arnd Bergmann98f6ef62012-10-11 13:20:00 +0000876 *p = NULL;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400877 path_put(&nd->path);
Al Viro6d7b5aa2012-06-10 04:15:17 -0400878 path_put(link);
Ian Kent051d3812006-03-27 01:14:53 -0800879 return error;
880}
881
Nick Piggin31e6b012011-01-07 17:49:52 +1100882static int follow_up_rcu(struct path *path)
883{
Al Viro0714a532011-11-24 22:19:58 -0500884 struct mount *mnt = real_mount(path->mnt);
885 struct mount *parent;
Nick Piggin31e6b012011-01-07 17:49:52 +1100886 struct dentry *mountpoint;
887
Al Viro0714a532011-11-24 22:19:58 -0500888 parent = mnt->mnt_parent;
889 if (&parent->mnt == path->mnt)
Nick Piggin31e6b012011-01-07 17:49:52 +1100890 return 0;
Al Viroa73324d2011-11-24 22:25:07 -0500891 mountpoint = mnt->mnt_mountpoint;
Nick Piggin31e6b012011-01-07 17:49:52 +1100892 path->dentry = mountpoint;
Al Viro0714a532011-11-24 22:19:58 -0500893 path->mnt = &parent->mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +1100894 return 1;
895}
896
David Howellsf015f1262012-06-25 12:55:28 +0100897/*
898 * follow_up - Find the mountpoint of path's vfsmount
899 *
900 * Given a path, find the mountpoint of its source file system.
901 * Replace @path with the path of the mountpoint in the parent mount.
902 * Up is towards /.
903 *
904 * Return 1 if we went up a level and 0 if we were already at the
905 * root.
906 */
Al Virobab77eb2009-04-18 03:26:48 -0400907int follow_up(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
Al Viro0714a532011-11-24 22:19:58 -0500909 struct mount *mnt = real_mount(path->mnt);
910 struct mount *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 struct dentry *mountpoint;
Nick Piggin99b7db72010-08-18 04:37:39 +1000912
Al Viro48a066e2013-09-29 22:06:07 -0400913 read_seqlock_excl(&mount_lock);
Al Viro0714a532011-11-24 22:19:58 -0500914 parent = mnt->mnt_parent;
Al Viro3c0a6162012-07-18 17:32:50 +0400915 if (parent == mnt) {
Al Viro48a066e2013-09-29 22:06:07 -0400916 read_sequnlock_excl(&mount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 return 0;
918 }
Al Viro0714a532011-11-24 22:19:58 -0500919 mntget(&parent->mnt);
Al Viroa73324d2011-11-24 22:25:07 -0500920 mountpoint = dget(mnt->mnt_mountpoint);
Al Viro48a066e2013-09-29 22:06:07 -0400921 read_sequnlock_excl(&mount_lock);
Al Virobab77eb2009-04-18 03:26:48 -0400922 dput(path->dentry);
923 path->dentry = mountpoint;
924 mntput(path->mnt);
Al Viro0714a532011-11-24 22:19:58 -0500925 path->mnt = &parent->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 return 1;
927}
928
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100929/*
David Howells9875cf82011-01-14 18:45:21 +0000930 * Perform an automount
931 * - return -EISDIR to tell follow_managed() to stop and return the path we
932 * were called with.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 */
David Howells9875cf82011-01-14 18:45:21 +0000934static int follow_automount(struct path *path, unsigned flags,
935 bool *need_mntput)
Nick Piggin31e6b012011-01-07 17:49:52 +1100936{
David Howells9875cf82011-01-14 18:45:21 +0000937 struct vfsmount *mnt;
David Howellsea5b7782011-01-14 19:10:03 +0000938 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +1100939
David Howells9875cf82011-01-14 18:45:21 +0000940 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
941 return -EREMOTE;
Al Viro463ffb22005-06-06 13:36:05 -0700942
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200943 /* We don't want to mount if someone's just doing a stat -
944 * unless they're stat'ing a directory and appended a '/' to
945 * the name.
946 *
947 * We do, however, want to mount if someone wants to open or
948 * create a file of any type under the mountpoint, wants to
949 * traverse through the mountpoint or wants to open the
950 * mounted directory. Also, autofs may mark negative dentries
951 * as being automount points. These will need the attentions
952 * of the daemon to instantiate them before they can be used.
David Howells9875cf82011-01-14 18:45:21 +0000953 */
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200954 if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
Linus Torvaldsd94c1772011-09-26 17:44:55 -0700955 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200956 path->dentry->d_inode)
957 return -EISDIR;
958
David Howells9875cf82011-01-14 18:45:21 +0000959 current->total_link_count++;
960 if (current->total_link_count >= 40)
961 return -ELOOP;
962
963 mnt = path->dentry->d_op->d_automount(path);
964 if (IS_ERR(mnt)) {
965 /*
966 * The filesystem is allowed to return -EISDIR here to indicate
967 * it doesn't want to automount. For instance, autofs would do
968 * this so that its userspace daemon can mount on this dentry.
969 *
970 * However, we can only permit this if it's a terminal point in
971 * the path being looked up; if it wasn't then the remainder of
972 * the path is inaccessible and we should say so.
973 */
Al Viro49084c32011-06-25 21:59:52 -0400974 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
David Howells9875cf82011-01-14 18:45:21 +0000975 return -EREMOTE;
976 return PTR_ERR(mnt);
977 }
David Howellsea5b7782011-01-14 19:10:03 +0000978
David Howells9875cf82011-01-14 18:45:21 +0000979 if (!mnt) /* mount collision */
980 return 0;
981
Al Viro8aef1882011-06-16 15:10:06 +0100982 if (!*need_mntput) {
983 /* lock_mount() may release path->mnt on error */
984 mntget(path->mnt);
985 *need_mntput = true;
986 }
Al Viro19a167a2011-01-17 01:35:23 -0500987 err = finish_automount(mnt, path);
David Howellsea5b7782011-01-14 19:10:03 +0000988
David Howellsea5b7782011-01-14 19:10:03 +0000989 switch (err) {
990 case -EBUSY:
991 /* Someone else made a mount here whilst we were busy */
Al Viro19a167a2011-01-17 01:35:23 -0500992 return 0;
David Howellsea5b7782011-01-14 19:10:03 +0000993 case 0:
Al Viro8aef1882011-06-16 15:10:06 +0100994 path_put(path);
David Howellsea5b7782011-01-14 19:10:03 +0000995 path->mnt = mnt;
996 path->dentry = dget(mnt->mnt_root);
David Howellsea5b7782011-01-14 19:10:03 +0000997 return 0;
Al Viro19a167a2011-01-17 01:35:23 -0500998 default:
999 return err;
David Howellsea5b7782011-01-14 19:10:03 +00001000 }
Al Viro19a167a2011-01-17 01:35:23 -05001001
David Howells9875cf82011-01-14 18:45:21 +00001002}
1003
1004/*
1005 * Handle a dentry that is managed in some way.
David Howellscc53ce52011-01-14 18:45:26 +00001006 * - Flagged for transit management (autofs)
David Howells9875cf82011-01-14 18:45:21 +00001007 * - Flagged as mountpoint
1008 * - Flagged as automount point
1009 *
1010 * This may only be called in refwalk mode.
1011 *
1012 * Serialization is taken care of in namespace.c
1013 */
1014static int follow_managed(struct path *path, unsigned flags)
1015{
Al Viro8aef1882011-06-16 15:10:06 +01001016 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
David Howells9875cf82011-01-14 18:45:21 +00001017 unsigned managed;
1018 bool need_mntput = false;
Al Viro8aef1882011-06-16 15:10:06 +01001019 int ret = 0;
David Howells9875cf82011-01-14 18:45:21 +00001020
1021 /* Given that we're not holding a lock here, we retain the value in a
1022 * local variable for each dentry as we look at it so that we don't see
1023 * the components of that value change under us */
1024 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1025 managed &= DCACHE_MANAGED_DENTRY,
1026 unlikely(managed != 0)) {
David Howellscc53ce52011-01-14 18:45:26 +00001027 /* Allow the filesystem to manage the transit without i_mutex
1028 * being held. */
1029 if (managed & DCACHE_MANAGE_TRANSIT) {
1030 BUG_ON(!path->dentry->d_op);
1031 BUG_ON(!path->dentry->d_op->d_manage);
Al Viro1aed3e42011-03-18 09:09:02 -04001032 ret = path->dentry->d_op->d_manage(path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +00001033 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001034 break;
David Howellscc53ce52011-01-14 18:45:26 +00001035 }
1036
David Howells9875cf82011-01-14 18:45:21 +00001037 /* Transit to a mounted filesystem. */
1038 if (managed & DCACHE_MOUNTED) {
1039 struct vfsmount *mounted = lookup_mnt(path);
1040 if (mounted) {
1041 dput(path->dentry);
1042 if (need_mntput)
1043 mntput(path->mnt);
1044 path->mnt = mounted;
1045 path->dentry = dget(mounted->mnt_root);
1046 need_mntput = true;
1047 continue;
1048 }
1049
1050 /* Something is mounted on this dentry in another
1051 * namespace and/or whatever was mounted there in this
Al Viro48a066e2013-09-29 22:06:07 -04001052 * namespace got unmounted before lookup_mnt() could
1053 * get it */
David Howells9875cf82011-01-14 18:45:21 +00001054 }
1055
1056 /* Handle an automount point */
1057 if (managed & DCACHE_NEED_AUTOMOUNT) {
1058 ret = follow_automount(path, flags, &need_mntput);
1059 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001060 break;
David Howells9875cf82011-01-14 18:45:21 +00001061 continue;
1062 }
1063
1064 /* We didn't change the current path point */
1065 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 }
Al Viro8aef1882011-06-16 15:10:06 +01001067
1068 if (need_mntput && path->mnt == mnt)
1069 mntput(path->mnt);
1070 if (ret == -EISDIR)
1071 ret = 0;
Al Viroa3fbbde2011-11-07 21:21:26 +00001072 return ret < 0 ? ret : need_mntput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073}
1074
David Howellscc53ce52011-01-14 18:45:26 +00001075int follow_down_one(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
1077 struct vfsmount *mounted;
1078
Al Viro1c755af2009-04-18 14:06:57 -04001079 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -04001081 dput(path->dentry);
1082 mntput(path->mnt);
1083 path->mnt = mounted;
1084 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 return 1;
1086 }
1087 return 0;
1088}
1089
Ian Kent62a73752011-03-25 01:51:02 +08001090static inline bool managed_dentry_might_block(struct dentry *dentry)
1091{
1092 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
1093 dentry->d_op->d_manage(dentry, true) < 0);
1094}
1095
David Howells9875cf82011-01-14 18:45:21 +00001096/*
Al Viro287548e2011-05-27 06:50:06 -04001097 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1098 * we meet a managed dentry that would need blocking.
David Howells9875cf82011-01-14 18:45:21 +00001099 */
1100static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
Al Viro287548e2011-05-27 06:50:06 -04001101 struct inode **inode)
David Howells9875cf82011-01-14 18:45:21 +00001102{
Ian Kent62a73752011-03-25 01:51:02 +08001103 for (;;) {
Al Viroc7105362011-11-24 18:22:03 -05001104 struct mount *mounted;
Ian Kent62a73752011-03-25 01:51:02 +08001105 /*
1106 * Don't forget we might have a non-mountpoint managed dentry
1107 * that wants to block transit.
1108 */
Al Viro287548e2011-05-27 06:50:06 -04001109 if (unlikely(managed_dentry_might_block(path->dentry)))
David Howellsab909112011-01-14 18:46:51 +00001110 return false;
Ian Kent62a73752011-03-25 01:51:02 +08001111
1112 if (!d_mountpoint(path->dentry))
Al Virob37199e2014-03-20 15:18:22 -04001113 return true;
Ian Kent62a73752011-03-25 01:51:02 +08001114
Al Viro474279d2013-10-01 16:11:26 -04001115 mounted = __lookup_mnt(path->mnt, path->dentry);
David Howells9875cf82011-01-14 18:45:21 +00001116 if (!mounted)
1117 break;
Al Viroc7105362011-11-24 18:22:03 -05001118 path->mnt = &mounted->mnt;
1119 path->dentry = mounted->mnt.mnt_root;
Al Viroa3fbbde2011-11-07 21:21:26 +00001120 nd->flags |= LOOKUP_JUMPED;
David Howells9875cf82011-01-14 18:45:21 +00001121 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
Linus Torvalds59430262011-07-18 15:43:29 -07001122 /*
1123 * Update the inode too. We don't need to re-check the
1124 * dentry sequence number here after this d_inode read,
1125 * because a mount-point is always pinned.
1126 */
1127 *inode = path->dentry->d_inode;
David Howells9875cf82011-01-14 18:45:21 +00001128 }
Al Virob37199e2014-03-20 15:18:22 -04001129 return read_seqretry(&mount_lock, nd->m_seq);
Al Viro287548e2011-05-27 06:50:06 -04001130}
1131
Nick Piggin31e6b012011-01-07 17:49:52 +11001132static int follow_dotdot_rcu(struct nameidata *nd)
1133{
Nick Piggin31e6b012011-01-07 17:49:52 +11001134 set_root_rcu(nd);
1135
David Howells9875cf82011-01-14 18:45:21 +00001136 while (1) {
Nick Piggin31e6b012011-01-07 17:49:52 +11001137 if (nd->path.dentry == nd->root.dentry &&
1138 nd->path.mnt == nd->root.mnt) {
1139 break;
1140 }
1141 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1142 struct dentry *old = nd->path.dentry;
1143 struct dentry *parent = old->d_parent;
1144 unsigned seq;
1145
1146 seq = read_seqcount_begin(&parent->d_seq);
1147 if (read_seqcount_retry(&old->d_seq, nd->seq))
Al Viroef7562d2011-03-04 14:35:59 -05001148 goto failed;
Nick Piggin31e6b012011-01-07 17:49:52 +11001149 nd->path.dentry = parent;
1150 nd->seq = seq;
1151 break;
1152 }
1153 if (!follow_up_rcu(&nd->path))
1154 break;
1155 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Nick Piggin31e6b012011-01-07 17:49:52 +11001156 }
Al Virob37199e2014-03-20 15:18:22 -04001157 while (d_mountpoint(nd->path.dentry)) {
1158 struct mount *mounted;
1159 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry);
1160 if (!mounted)
1161 break;
1162 nd->path.mnt = &mounted->mnt;
1163 nd->path.dentry = mounted->mnt.mnt_root;
1164 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
1165 if (!read_seqretry(&mount_lock, nd->m_seq))
1166 goto failed;
1167 }
Al Virodea39372011-05-27 06:53:39 -04001168 nd->inode = nd->path.dentry->d_inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001169 return 0;
Al Viroef7562d2011-03-04 14:35:59 -05001170
1171failed:
1172 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001173 if (!(nd->flags & LOOKUP_ROOT))
1174 nd->root.mnt = NULL;
Al Viro8b61e742013-11-08 12:45:01 -05001175 rcu_read_unlock();
Al Viroef7562d2011-03-04 14:35:59 -05001176 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001177}
1178
David Howells9875cf82011-01-14 18:45:21 +00001179/*
David Howellscc53ce52011-01-14 18:45:26 +00001180 * Follow down to the covering mount currently visible to userspace. At each
1181 * point, the filesystem owning that dentry may be queried as to whether the
1182 * caller is permitted to proceed or not.
David Howellscc53ce52011-01-14 18:45:26 +00001183 */
Al Viro7cc90cc2011-03-18 09:04:20 -04001184int follow_down(struct path *path)
David Howellscc53ce52011-01-14 18:45:26 +00001185{
1186 unsigned managed;
1187 int ret;
1188
1189 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1190 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1191 /* Allow the filesystem to manage the transit without i_mutex
1192 * being held.
1193 *
1194 * We indicate to the filesystem if someone is trying to mount
1195 * something here. This gives autofs the chance to deny anyone
1196 * other than its daemon the right to mount on its
1197 * superstructure.
1198 *
1199 * The filesystem may sleep at this point.
1200 */
1201 if (managed & DCACHE_MANAGE_TRANSIT) {
1202 BUG_ON(!path->dentry->d_op);
1203 BUG_ON(!path->dentry->d_op->d_manage);
David Howellsab909112011-01-14 18:46:51 +00001204 ret = path->dentry->d_op->d_manage(
Al Viro1aed3e42011-03-18 09:09:02 -04001205 path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +00001206 if (ret < 0)
1207 return ret == -EISDIR ? 0 : ret;
1208 }
1209
1210 /* Transit to a mounted filesystem. */
1211 if (managed & DCACHE_MOUNTED) {
1212 struct vfsmount *mounted = lookup_mnt(path);
1213 if (!mounted)
1214 break;
1215 dput(path->dentry);
1216 mntput(path->mnt);
1217 path->mnt = mounted;
1218 path->dentry = dget(mounted->mnt_root);
1219 continue;
1220 }
1221
1222 /* Don't handle automount points here */
1223 break;
1224 }
1225 return 0;
1226}
1227
1228/*
David Howells9875cf82011-01-14 18:45:21 +00001229 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1230 */
1231static void follow_mount(struct path *path)
1232{
1233 while (d_mountpoint(path->dentry)) {
1234 struct vfsmount *mounted = lookup_mnt(path);
1235 if (!mounted)
1236 break;
1237 dput(path->dentry);
1238 mntput(path->mnt);
1239 path->mnt = mounted;
1240 path->dentry = dget(mounted->mnt_root);
1241 }
1242}
1243
Nick Piggin31e6b012011-01-07 17:49:52 +11001244static void follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245{
Al Viro2a737872009-04-07 11:49:53 -04001246 set_root(nd);
Andreas Mohre518ddb2006-09-29 02:01:22 -07001247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 while(1) {
Jan Blunck4ac91372008-02-14 19:34:32 -08001249 struct dentry *old = nd->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Al Viro2a737872009-04-07 11:49:53 -04001251 if (nd->path.dentry == nd->root.dentry &&
1252 nd->path.mnt == nd->root.mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 break;
1254 }
Jan Blunck4ac91372008-02-14 19:34:32 -08001255 if (nd->path.dentry != nd->path.mnt->mnt_root) {
Al Viro3088dd72010-01-30 15:47:29 -05001256 /* rare case of legitimate dget_parent()... */
1257 nd->path.dentry = dget_parent(nd->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 dput(old);
1259 break;
1260 }
Al Viro3088dd72010-01-30 15:47:29 -05001261 if (!follow_up(&nd->path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 }
Al Viro79ed0222009-04-18 13:59:41 -04001264 follow_mount(&nd->path);
Nick Piggin31e6b012011-01-07 17:49:52 +11001265 nd->inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266}
1267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268/*
Miklos Szeredibad61182012-03-26 12:54:24 +02001269 * This looks up the name in dcache, possibly revalidates the old dentry and
1270 * allocates a new one if not found or not valid. In the need_lookup argument
1271 * returns whether i_op->lookup is necessary.
1272 *
1273 * dir->d_inode->i_mutex must be held
Nick Pigginbaa03892010-08-18 04:37:31 +10001274 */
Miklos Szeredibad61182012-03-26 12:54:24 +02001275static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
Al Viro201f9562012-06-22 12:42:10 +04001276 unsigned int flags, bool *need_lookup)
Nick Pigginbaa03892010-08-18 04:37:31 +10001277{
Nick Pigginbaa03892010-08-18 04:37:31 +10001278 struct dentry *dentry;
Miklos Szeredibad61182012-03-26 12:54:24 +02001279 int error;
Nick Pigginbaa03892010-08-18 04:37:31 +10001280
Miklos Szeredibad61182012-03-26 12:54:24 +02001281 *need_lookup = false;
1282 dentry = d_lookup(dir, name);
1283 if (dentry) {
Jeff Layton39e3c952012-11-28 11:30:53 -05001284 if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
Al Viro201f9562012-06-22 12:42:10 +04001285 error = d_revalidate(dentry, flags);
Miklos Szeredibad61182012-03-26 12:54:24 +02001286 if (unlikely(error <= 0)) {
1287 if (error < 0) {
1288 dput(dentry);
1289 return ERR_PTR(error);
1290 } else if (!d_invalidate(dentry)) {
1291 dput(dentry);
1292 dentry = NULL;
1293 }
1294 }
1295 }
1296 }
Nick Pigginbaa03892010-08-18 04:37:31 +10001297
Miklos Szeredibad61182012-03-26 12:54:24 +02001298 if (!dentry) {
1299 dentry = d_alloc(dir, name);
1300 if (unlikely(!dentry))
1301 return ERR_PTR(-ENOMEM);
Nick Pigginbaa03892010-08-18 04:37:31 +10001302
Miklos Szeredibad61182012-03-26 12:54:24 +02001303 *need_lookup = true;
Nick Pigginbaa03892010-08-18 04:37:31 +10001304 }
1305 return dentry;
1306}
1307
1308/*
J. Bruce Fields13a2c3b2013-10-23 16:09:16 -04001309 * Call i_op->lookup on the dentry. The dentry must be negative and
1310 * unhashed.
Miklos Szeredibad61182012-03-26 12:54:24 +02001311 *
1312 * dir->d_inode->i_mutex must be held
Josef Bacik44396f42011-05-31 11:58:49 -04001313 */
Miklos Szeredibad61182012-03-26 12:54:24 +02001314static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
Al Viro72bd8662012-06-10 17:17:17 -04001315 unsigned int flags)
Josef Bacik44396f42011-05-31 11:58:49 -04001316{
Josef Bacik44396f42011-05-31 11:58:49 -04001317 struct dentry *old;
1318
1319 /* Don't create child dentry for a dead directory. */
Miklos Szeredibad61182012-03-26 12:54:24 +02001320 if (unlikely(IS_DEADDIR(dir))) {
Miklos Szeredie188dc02012-02-03 14:25:18 +01001321 dput(dentry);
Josef Bacik44396f42011-05-31 11:58:49 -04001322 return ERR_PTR(-ENOENT);
Miklos Szeredie188dc02012-02-03 14:25:18 +01001323 }
Josef Bacik44396f42011-05-31 11:58:49 -04001324
Al Viro72bd8662012-06-10 17:17:17 -04001325 old = dir->i_op->lookup(dir, dentry, flags);
Josef Bacik44396f42011-05-31 11:58:49 -04001326 if (unlikely(old)) {
1327 dput(dentry);
1328 dentry = old;
1329 }
1330 return dentry;
1331}
1332
Al Viroa3255542012-03-30 14:41:51 -04001333static struct dentry *__lookup_hash(struct qstr *name,
Al Viro72bd8662012-06-10 17:17:17 -04001334 struct dentry *base, unsigned int flags)
Al Viroa3255542012-03-30 14:41:51 -04001335{
Miklos Szeredibad61182012-03-26 12:54:24 +02001336 bool need_lookup;
Al Viroa3255542012-03-30 14:41:51 -04001337 struct dentry *dentry;
1338
Al Viro72bd8662012-06-10 17:17:17 -04001339 dentry = lookup_dcache(name, base, flags, &need_lookup);
Miklos Szeredibad61182012-03-26 12:54:24 +02001340 if (!need_lookup)
1341 return dentry;
Al Viroa3255542012-03-30 14:41:51 -04001342
Al Viro72bd8662012-06-10 17:17:17 -04001343 return lookup_real(base->d_inode, dentry, flags);
Al Viroa3255542012-03-30 14:41:51 -04001344}
1345
Josef Bacik44396f42011-05-31 11:58:49 -04001346/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 * It's more convoluted than I'd like it to be, but... it's still fairly
1348 * small and for now I'd prefer to have fast path as straight as possible.
1349 * It _is_ time-critical.
1350 */
Al Viroe97cdc82013-01-24 18:16:00 -05001351static int lookup_fast(struct nameidata *nd,
Miklos Szeredi697f5142012-05-21 17:30:05 +02001352 struct path *path, struct inode **inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
Jan Blunck4ac91372008-02-14 19:34:32 -08001354 struct vfsmount *mnt = nd->path.mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +11001355 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro5a18fff2011-03-11 04:44:53 -05001356 int need_reval = 1;
1357 int status = 1;
David Howells9875cf82011-01-14 18:45:21 +00001358 int err;
1359
Al Viro3cac2602009-08-13 18:27:43 +04001360 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001361 * Rename seqlock is not required here because in the off chance
1362 * of a false negative due to a concurrent rename, we're going to
1363 * do the non-racy lookup, below.
1364 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001365 if (nd->flags & LOOKUP_RCU) {
1366 unsigned seq;
Linus Torvaldsda53be12013-05-21 15:22:44 -07001367 dentry = __d_lookup_rcu(parent, &nd->last, &seq);
Al Viro5a18fff2011-03-11 04:44:53 -05001368 if (!dentry)
1369 goto unlazy;
1370
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001371 /*
1372 * This sequence count validates that the inode matches
1373 * the dentry name information from lookup.
1374 */
1375 *inode = dentry->d_inode;
1376 if (read_seqcount_retry(&dentry->d_seq, seq))
1377 return -ECHILD;
1378
1379 /*
1380 * This sequence count validates that the parent had no
1381 * changes while we did the lookup of the dentry above.
1382 *
1383 * The memory barrier in read_seqcount_begin of child is
1384 * enough, we can use __read_seqcount_retry here.
1385 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001386 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1387 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001388 nd->seq = seq;
Al Viro5a18fff2011-03-11 04:44:53 -05001389
Al Viro24643082011-02-15 01:26:22 -05001390 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
Al Viro4ce16ef32012-06-10 16:10:59 -04001391 status = d_revalidate(dentry, nd->flags);
Al Viro5a18fff2011-03-11 04:44:53 -05001392 if (unlikely(status <= 0)) {
1393 if (status != -ECHILD)
1394 need_reval = 0;
1395 goto unlazy;
1396 }
Al Viro24643082011-02-15 01:26:22 -05001397 }
Nick Piggin31e6b012011-01-07 17:49:52 +11001398 path->mnt = mnt;
1399 path->dentry = dentry;
Al Virod6e9bd22011-05-27 07:03:15 -04001400 if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1401 goto unlazy;
1402 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1403 goto unlazy;
1404 return 0;
Al Viro5a18fff2011-03-11 04:44:53 -05001405unlazy:
Al Viro19660af2011-03-25 10:32:48 -04001406 if (unlazy_walk(nd, dentry))
1407 return -ECHILD;
Al Viro5a18fff2011-03-11 04:44:53 -05001408 } else {
Al Viroe97cdc82013-01-24 18:16:00 -05001409 dentry = __d_lookup(parent, &nd->last);
Nick Piggin31e6b012011-01-07 17:49:52 +11001410 }
Al Viro5a18fff2011-03-11 04:44:53 -05001411
Al Viro81e6f522012-03-30 14:48:04 -04001412 if (unlikely(!dentry))
1413 goto need_lookup;
Al Viro5a18fff2011-03-11 04:44:53 -05001414
Al Viro5a18fff2011-03-11 04:44:53 -05001415 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
Al Viro4ce16ef32012-06-10 16:10:59 -04001416 status = d_revalidate(dentry, nd->flags);
Al Viro5a18fff2011-03-11 04:44:53 -05001417 if (unlikely(status <= 0)) {
1418 if (status < 0) {
1419 dput(dentry);
1420 return status;
1421 }
1422 if (!d_invalidate(dentry)) {
1423 dput(dentry);
Al Viro81e6f522012-03-30 14:48:04 -04001424 goto need_lookup;
Al Viro5a18fff2011-03-11 04:44:53 -05001425 }
1426 }
Miklos Szeredi697f5142012-05-21 17:30:05 +02001427
David Howells9875cf82011-01-14 18:45:21 +00001428 path->mnt = mnt;
1429 path->dentry = dentry;
1430 err = follow_managed(path, nd->flags);
Ian Kent89312212011-01-18 12:06:10 +08001431 if (unlikely(err < 0)) {
1432 path_put_conditional(path, nd);
David Howells9875cf82011-01-14 18:45:21 +00001433 return err;
Ian Kent89312212011-01-18 12:06:10 +08001434 }
Al Viroa3fbbde2011-11-07 21:21:26 +00001435 if (err)
1436 nd->flags |= LOOKUP_JUMPED;
David Howells9875cf82011-01-14 18:45:21 +00001437 *inode = path->dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 return 0;
Al Viro81e6f522012-03-30 14:48:04 -04001439
1440need_lookup:
Miklos Szeredi697f5142012-05-21 17:30:05 +02001441 return 1;
1442}
1443
1444/* Fast lookup failed, do it the slow way */
Al Virocc2a5272013-01-24 18:19:49 -05001445static int lookup_slow(struct nameidata *nd, struct path *path)
Miklos Szeredi697f5142012-05-21 17:30:05 +02001446{
1447 struct dentry *dentry, *parent;
1448 int err;
1449
1450 parent = nd->path.dentry;
Al Viro81e6f522012-03-30 14:48:04 -04001451 BUG_ON(nd->inode != parent->d_inode);
1452
1453 mutex_lock(&parent->d_inode->i_mutex);
Al Virocc2a5272013-01-24 18:19:49 -05001454 dentry = __lookup_hash(&nd->last, parent, nd->flags);
Al Viro81e6f522012-03-30 14:48:04 -04001455 mutex_unlock(&parent->d_inode->i_mutex);
1456 if (IS_ERR(dentry))
1457 return PTR_ERR(dentry);
Miklos Szeredi697f5142012-05-21 17:30:05 +02001458 path->mnt = nd->path.mnt;
1459 path->dentry = dentry;
1460 err = follow_managed(path, nd->flags);
1461 if (unlikely(err < 0)) {
1462 path_put_conditional(path, nd);
1463 return err;
1464 }
1465 if (err)
1466 nd->flags |= LOOKUP_JUMPED;
1467 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468}
1469
Al Viro52094c82011-02-21 21:34:47 -05001470static inline int may_lookup(struct nameidata *nd)
1471{
1472 if (nd->flags & LOOKUP_RCU) {
Al Viro4ad5abb2011-06-20 19:57:03 -04001473 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
Al Viro52094c82011-02-21 21:34:47 -05001474 if (err != -ECHILD)
1475 return err;
Al Viro19660af2011-03-25 10:32:48 -04001476 if (unlazy_walk(nd, NULL))
Al Viro52094c82011-02-21 21:34:47 -05001477 return -ECHILD;
1478 }
Al Viro4ad5abb2011-06-20 19:57:03 -04001479 return inode_permission(nd->inode, MAY_EXEC);
Al Viro52094c82011-02-21 21:34:47 -05001480}
1481
Al Viro9856fa12011-03-04 14:22:06 -05001482static inline int handle_dots(struct nameidata *nd, int type)
1483{
1484 if (type == LAST_DOTDOT) {
1485 if (nd->flags & LOOKUP_RCU) {
1486 if (follow_dotdot_rcu(nd))
1487 return -ECHILD;
1488 } else
1489 follow_dotdot(nd);
1490 }
1491 return 0;
1492}
1493
Al Viro951361f2011-03-04 14:44:37 -05001494static void terminate_walk(struct nameidata *nd)
1495{
1496 if (!(nd->flags & LOOKUP_RCU)) {
1497 path_put(&nd->path);
1498 } else {
1499 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001500 if (!(nd->flags & LOOKUP_ROOT))
1501 nd->root.mnt = NULL;
Al Viro8b61e742013-11-08 12:45:01 -05001502 rcu_read_unlock();
Al Viro951361f2011-03-04 14:44:37 -05001503 }
1504}
1505
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001506/*
1507 * Do we need to follow links? We _really_ want to be able
1508 * to do this check without having to look at inode->i_op,
1509 * so we keep a cache of "no, this doesn't need follow_link"
1510 * for the common case.
1511 */
David Howellsb18825a2013-09-12 19:22:53 +01001512static inline int should_follow_link(struct dentry *dentry, int follow)
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001513{
David Howellsb18825a2013-09-12 19:22:53 +01001514 return unlikely(d_is_symlink(dentry)) ? follow : 0;
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001515}
1516
Al Viroce57dfc2011-03-13 19:58:58 -04001517static inline int walk_component(struct nameidata *nd, struct path *path,
Al Viro21b9b072013-01-24 18:10:25 -05001518 int follow)
Al Viroce57dfc2011-03-13 19:58:58 -04001519{
1520 struct inode *inode;
1521 int err;
1522 /*
1523 * "." and ".." are special - ".." especially so because it has
1524 * to be able to know about the current root directory and
1525 * parent relationships.
1526 */
Al Viro21b9b072013-01-24 18:10:25 -05001527 if (unlikely(nd->last_type != LAST_NORM))
1528 return handle_dots(nd, nd->last_type);
Al Viroe97cdc82013-01-24 18:16:00 -05001529 err = lookup_fast(nd, path, &inode);
Al Viroce57dfc2011-03-13 19:58:58 -04001530 if (unlikely(err)) {
Miklos Szeredi697f5142012-05-21 17:30:05 +02001531 if (err < 0)
1532 goto out_err;
1533
Al Virocc2a5272013-01-24 18:19:49 -05001534 err = lookup_slow(nd, path);
Miklos Szeredi697f5142012-05-21 17:30:05 +02001535 if (err < 0)
1536 goto out_err;
1537
1538 inode = path->dentry->d_inode;
Al Viroce57dfc2011-03-13 19:58:58 -04001539 }
Miklos Szeredi697f5142012-05-21 17:30:05 +02001540 err = -ENOENT;
Al Virof0754e52014-04-19 12:30:58 -04001541 if (!inode || d_is_negative(path->dentry))
Miklos Szeredi697f5142012-05-21 17:30:05 +02001542 goto out_path_put;
1543
David Howellsb18825a2013-09-12 19:22:53 +01001544 if (should_follow_link(path->dentry, follow)) {
Al Viro19660af2011-03-25 10:32:48 -04001545 if (nd->flags & LOOKUP_RCU) {
1546 if (unlikely(unlazy_walk(nd, path->dentry))) {
Miklos Szeredi697f5142012-05-21 17:30:05 +02001547 err = -ECHILD;
1548 goto out_err;
Al Viro19660af2011-03-25 10:32:48 -04001549 }
1550 }
Al Viroce57dfc2011-03-13 19:58:58 -04001551 BUG_ON(inode != path->dentry->d_inode);
1552 return 1;
1553 }
1554 path_to_nameidata(path, nd);
1555 nd->inode = inode;
1556 return 0;
Miklos Szeredi697f5142012-05-21 17:30:05 +02001557
1558out_path_put:
1559 path_to_nameidata(path, nd);
1560out_err:
1561 terminate_walk(nd);
1562 return err;
Al Viroce57dfc2011-03-13 19:58:58 -04001563}
1564
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565/*
Al Virob3563792011-03-14 21:54:55 -04001566 * This limits recursive symlink follows to 8, while
1567 * limiting consecutive symlinks to 40.
1568 *
1569 * Without that kind of total limit, nasty chains of consecutive
1570 * symlinks can cause almost arbitrarily long lookups.
1571 */
1572static inline int nested_symlink(struct path *path, struct nameidata *nd)
1573{
1574 int res;
1575
Al Virob3563792011-03-14 21:54:55 -04001576 if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1577 path_put_conditional(path, nd);
1578 path_put(&nd->path);
1579 return -ELOOP;
1580 }
Erez Zadok1a4022f2011-05-21 01:19:59 -04001581 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
Al Virob3563792011-03-14 21:54:55 -04001582
1583 nd->depth++;
1584 current->link_count++;
1585
1586 do {
1587 struct path link = *path;
1588 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04001589
1590 res = follow_link(&link, nd, &cookie);
Al Viro6d7b5aa2012-06-10 04:15:17 -04001591 if (res)
1592 break;
Al Viro21b9b072013-01-24 18:10:25 -05001593 res = walk_component(nd, path, LOOKUP_FOLLOW);
Al Viro574197e2011-03-14 22:20:34 -04001594 put_link(nd, &link, cookie);
Al Virob3563792011-03-14 21:54:55 -04001595 } while (res > 0);
1596
1597 current->link_count--;
1598 nd->depth--;
1599 return res;
1600}
1601
1602/*
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001603 * We can do the critical dentry name comparison and hashing
1604 * operations one word at a time, but we are limited to:
1605 *
1606 * - Architectures with fast unaligned word accesses. We could
1607 * do a "get_unaligned()" if this helps and is sufficiently
1608 * fast.
1609 *
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001610 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1611 * do not trap on the (extremely unlikely) case of a page
1612 * crossing operation.
1613 *
1614 * - Furthermore, we need an efficient 64-bit compile for the
1615 * 64-bit case in order to generate the "number of bytes in
1616 * the final mask". Again, that could be replaced with a
1617 * efficient population count instruction or similar.
1618 */
1619#ifdef CONFIG_DCACHE_WORD_ACCESS
1620
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001621#include <asm/word-at-a-time.h>
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001622
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001623#ifdef CONFIG_64BIT
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001624
1625static inline unsigned int fold_hash(unsigned long hash)
1626{
1627 hash += hash >> (8*sizeof(int));
1628 return hash;
1629}
1630
1631#else /* 32-bit case */
1632
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001633#define fold_hash(x) (x)
1634
1635#endif
1636
1637unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1638{
1639 unsigned long a, mask;
1640 unsigned long hash = 0;
1641
1642 for (;;) {
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001643 a = load_unaligned_zeropad(name);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001644 if (len < sizeof(unsigned long))
1645 break;
1646 hash += a;
Al Virof132c5b2012-03-22 21:59:52 +00001647 hash *= 9;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001648 name += sizeof(unsigned long);
1649 len -= sizeof(unsigned long);
1650 if (!len)
1651 goto done;
1652 }
Will Deacona5c21dc2013-12-12 17:40:21 +00001653 mask = bytemask_from_count(len);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001654 hash += mask & a;
1655done:
1656 return fold_hash(hash);
1657}
1658EXPORT_SYMBOL(full_name_hash);
1659
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001660/*
1661 * Calculate the length and hash of the path component, and
1662 * return the length of the component;
1663 */
1664static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1665{
Linus Torvalds36126f82012-05-26 10:43:17 -07001666 unsigned long a, b, adata, bdata, mask, hash, len;
1667 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001668
1669 hash = a = 0;
1670 len = -sizeof(unsigned long);
1671 do {
1672 hash = (hash + a) * 9;
1673 len += sizeof(unsigned long);
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001674 a = load_unaligned_zeropad(name+len);
Linus Torvalds36126f82012-05-26 10:43:17 -07001675 b = a ^ REPEAT_BYTE('/');
1676 } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001677
Linus Torvalds36126f82012-05-26 10:43:17 -07001678 adata = prep_zero_mask(a, adata, &constants);
1679 bdata = prep_zero_mask(b, bdata, &constants);
1680
1681 mask = create_zero_mask(adata | bdata);
1682
1683 hash += a & zero_bytemask(mask);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001684 *hashp = fold_hash(hash);
1685
Linus Torvalds36126f82012-05-26 10:43:17 -07001686 return len + find_zero(mask);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001687}
1688
1689#else
1690
Linus Torvalds0145acc2012-03-02 14:32:59 -08001691unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1692{
1693 unsigned long hash = init_name_hash();
1694 while (len--)
1695 hash = partial_name_hash(*name++, hash);
1696 return end_name_hash(hash);
1697}
Linus Torvaldsae942ae2012-03-02 19:40:57 -08001698EXPORT_SYMBOL(full_name_hash);
Linus Torvalds0145acc2012-03-02 14:32:59 -08001699
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001700/*
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001701 * We know there's a real path component here of at least
1702 * one character.
1703 */
1704static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1705{
1706 unsigned long hash = init_name_hash();
1707 unsigned long len = 0, c;
1708
1709 c = (unsigned char)*name;
1710 do {
1711 len++;
1712 hash = partial_name_hash(c, hash);
1713 c = (unsigned char)name[len];
1714 } while (c && c != '/');
1715 *hashp = end_name_hash(hash);
1716 return len;
1717}
1718
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001719#endif
1720
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001721/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01001723 * This is the basic name resolution function, turning a pathname into
1724 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01001726 * Returns 0 and nd will have valid dentry and mnt on success.
1727 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 */
Al Viro6de88d72009-08-09 01:41:57 +04001729static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730{
1731 struct path next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
1734 while (*name=='/')
1735 name++;
1736 if (!*name)
Al Viro086e1832011-02-22 20:56:27 -05001737 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 /* At this point we know we have a real path component. */
1740 for(;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 struct qstr this;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001742 long len;
Al Virofe479a52011-02-22 15:10:03 -05001743 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
Al Viro52094c82011-02-21 21:34:47 -05001745 err = may_lookup(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 if (err)
1747 break;
1748
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001749 len = hash_name(name, &this.hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 this.name = name;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001751 this.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Al Virofe479a52011-02-22 15:10:03 -05001753 type = LAST_NORM;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001754 if (name[0] == '.') switch (len) {
Al Virofe479a52011-02-22 15:10:03 -05001755 case 2:
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001756 if (name[1] == '.') {
Al Virofe479a52011-02-22 15:10:03 -05001757 type = LAST_DOTDOT;
Al Viro16c2cd72011-02-22 15:50:10 -05001758 nd->flags |= LOOKUP_JUMPED;
1759 }
Al Virofe479a52011-02-22 15:10:03 -05001760 break;
1761 case 1:
1762 type = LAST_DOT;
1763 }
Al Viro5a202bc2011-03-08 14:17:44 -05001764 if (likely(type == LAST_NORM)) {
1765 struct dentry *parent = nd->path.dentry;
Al Viro16c2cd72011-02-22 15:50:10 -05001766 nd->flags &= ~LOOKUP_JUMPED;
Al Viro5a202bc2011-03-08 14:17:44 -05001767 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
Linus Torvaldsda53be12013-05-21 15:22:44 -07001768 err = parent->d_op->d_hash(parent, &this);
Al Viro5a202bc2011-03-08 14:17:44 -05001769 if (err < 0)
1770 break;
1771 }
1772 }
Al Virofe479a52011-02-22 15:10:03 -05001773
Al Viro5f4a6a62013-01-24 18:04:22 -05001774 nd->last = this;
1775 nd->last_type = type;
1776
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001777 if (!name[len])
Al Viro5f4a6a62013-01-24 18:04:22 -05001778 return 0;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001779 /*
1780 * If it wasn't NUL, we know it was '/'. Skip that
1781 * slash, and continue until no more slashes.
1782 */
1783 do {
1784 len++;
1785 } while (unlikely(name[len] == '/'));
1786 if (!name[len])
Al Viro5f4a6a62013-01-24 18:04:22 -05001787 return 0;
1788
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001789 name += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Al Viro21b9b072013-01-24 18:10:25 -05001791 err = walk_component(nd, &next, LOOKUP_FOLLOW);
Al Viroce57dfc2011-03-13 19:58:58 -04001792 if (err < 0)
1793 return err;
Al Virofe479a52011-02-22 15:10:03 -05001794
Al Viroce57dfc2011-03-13 19:58:58 -04001795 if (err) {
Al Virob3563792011-03-14 21:54:55 -04001796 err = nested_symlink(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 if (err)
Al Viroa7472ba2011-03-04 14:39:30 -05001798 return err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001799 }
David Howellsb18825a2013-09-12 19:22:53 +01001800 if (!d_is_directory(nd->path.dentry)) {
Al Viro5f4a6a62013-01-24 18:04:22 -05001801 err = -ENOTDIR;
1802 break;
1803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 }
Al Viro951361f2011-03-04 14:44:37 -05001805 terminate_walk(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 return err;
1807}
1808
Al Viro70e9b352011-03-05 21:12:22 -05001809static int path_init(int dfd, const char *name, unsigned int flags,
1810 struct nameidata *nd, struct file **fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811{
Prasanna Medaea3834d2005-04-29 16:00:17 +01001812 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
1814 nd->last_type = LAST_ROOT; /* if there are only slashes... */
Al Viro16c2cd72011-02-22 15:50:10 -05001815 nd->flags = flags | LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 nd->depth = 0;
Al Viro5b6ca022011-03-09 23:04:47 -05001817 if (flags & LOOKUP_ROOT) {
David Howellsb18825a2013-09-12 19:22:53 +01001818 struct dentry *root = nd->root.dentry;
1819 struct inode *inode = root->d_inode;
Al Viro73d049a2011-03-11 12:08:24 -05001820 if (*name) {
David Howellsb18825a2013-09-12 19:22:53 +01001821 if (!d_is_directory(root))
Al Viro73d049a2011-03-11 12:08:24 -05001822 return -ENOTDIR;
1823 retval = inode_permission(inode, MAY_EXEC);
1824 if (retval)
1825 return retval;
1826 }
Al Viro5b6ca022011-03-09 23:04:47 -05001827 nd->path = nd->root;
1828 nd->inode = inode;
1829 if (flags & LOOKUP_RCU) {
Al Viro8b61e742013-11-08 12:45:01 -05001830 rcu_read_lock();
Al Viro5b6ca022011-03-09 23:04:47 -05001831 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro48a066e2013-09-29 22:06:07 -04001832 nd->m_seq = read_seqbegin(&mount_lock);
Al Viro5b6ca022011-03-09 23:04:47 -05001833 } else {
1834 path_get(&nd->path);
1835 }
1836 return 0;
1837 }
1838
Al Viro2a737872009-04-07 11:49:53 -04001839 nd->root.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Al Viro48a066e2013-09-29 22:06:07 -04001841 nd->m_seq = read_seqbegin(&mount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 if (*name=='/') {
Al Viroe41f7d42011-02-22 14:02:58 -05001843 if (flags & LOOKUP_RCU) {
Al Viro8b61e742013-11-08 12:45:01 -05001844 rcu_read_lock();
Al Viroe41f7d42011-02-22 14:02:58 -05001845 set_root_rcu(nd);
1846 } else {
1847 set_root(nd);
1848 path_get(&nd->root);
1849 }
Al Viro2a737872009-04-07 11:49:53 -04001850 nd->path = nd->root;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001851 } else if (dfd == AT_FDCWD) {
Al Viroe41f7d42011-02-22 14:02:58 -05001852 if (flags & LOOKUP_RCU) {
1853 struct fs_struct *fs = current->fs;
1854 unsigned seq;
1855
Al Viro8b61e742013-11-08 12:45:01 -05001856 rcu_read_lock();
Al Viroe41f7d42011-02-22 14:02:58 -05001857
1858 do {
1859 seq = read_seqcount_begin(&fs->seq);
1860 nd->path = fs->pwd;
1861 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1862 } while (read_seqcount_retry(&fs->seq, seq));
1863 } else {
1864 get_fs_pwd(current->fs, &nd->path);
1865 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001866 } else {
Jeff Layton582aa642012-12-11 08:56:16 -05001867 /* Caller must check execute permissions on the starting path component */
Al Viro2903ff02012-08-28 12:52:22 -04001868 struct fd f = fdget_raw(dfd);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001869 struct dentry *dentry;
1870
Al Viro2903ff02012-08-28 12:52:22 -04001871 if (!f.file)
1872 return -EBADF;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001873
Al Viro2903ff02012-08-28 12:52:22 -04001874 dentry = f.file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001875
Al Virof52e0c12011-03-14 18:56:51 -04001876 if (*name) {
David Howellsb18825a2013-09-12 19:22:53 +01001877 if (!d_is_directory(dentry)) {
Al Viro2903ff02012-08-28 12:52:22 -04001878 fdput(f);
1879 return -ENOTDIR;
1880 }
Al Virof52e0c12011-03-14 18:56:51 -04001881 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001882
Al Viro2903ff02012-08-28 12:52:22 -04001883 nd->path = f.file->f_path;
Al Viroe41f7d42011-02-22 14:02:58 -05001884 if (flags & LOOKUP_RCU) {
Linus Torvalds9c225f22014-03-03 09:36:58 -08001885 if (f.flags & FDPUT_FPUT)
Al Viro2903ff02012-08-28 12:52:22 -04001886 *fp = f.file;
Al Viroe41f7d42011-02-22 14:02:58 -05001887 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro8b61e742013-11-08 12:45:01 -05001888 rcu_read_lock();
Al Viroe41f7d42011-02-22 14:02:58 -05001889 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001890 path_get(&nd->path);
1891 fdput(f);
Al Viroe41f7d42011-02-22 14:02:58 -05001892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 }
Al Viroe41f7d42011-02-22 14:02:58 -05001894
Nick Piggin31e6b012011-01-07 17:49:52 +11001895 nd->inode = nd->path.dentry->d_inode;
Al Viro9b4a9b12009-04-07 11:44:16 -04001896 return 0;
Al Viro9b4a9b12009-04-07 11:44:16 -04001897}
1898
Al Virobd92d7f2011-03-14 19:54:59 -04001899static inline int lookup_last(struct nameidata *nd, struct path *path)
1900{
1901 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1902 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1903
1904 nd->flags &= ~LOOKUP_PARENT;
Al Viro21b9b072013-01-24 18:10:25 -05001905 return walk_component(nd, path, nd->flags & LOOKUP_FOLLOW);
Al Virobd92d7f2011-03-14 19:54:59 -04001906}
1907
Al Viro9b4a9b12009-04-07 11:44:16 -04001908/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroee0827c2011-02-21 23:38:09 -05001909static int path_lookupat(int dfd, const char *name,
Al Viro9b4a9b12009-04-07 11:44:16 -04001910 unsigned int flags, struct nameidata *nd)
1911{
Al Viro70e9b352011-03-05 21:12:22 -05001912 struct file *base = NULL;
Al Virobd92d7f2011-03-14 19:54:59 -04001913 struct path path;
1914 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001915
1916 /*
1917 * Path walking is largely split up into 2 different synchronisation
1918 * schemes, rcu-walk and ref-walk (explained in
1919 * Documentation/filesystems/path-lookup.txt). These share much of the
1920 * path walk code, but some things particularly setup, cleanup, and
1921 * following mounts are sufficiently divergent that functions are
1922 * duplicated. Typically there is a function foo(), and its RCU
1923 * analogue, foo_rcu().
1924 *
1925 * -ECHILD is the error number of choice (just to avoid clashes) that
1926 * is returned if some aspect of an rcu-walk fails. Such an error must
1927 * be handled by restarting a traditional ref-walk (which will always
1928 * be able to complete).
1929 */
Al Virobd92d7f2011-03-14 19:54:59 -04001930 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
Al Viroee0827c2011-02-21 23:38:09 -05001931
Al Virobd92d7f2011-03-14 19:54:59 -04001932 if (unlikely(err))
1933 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001934
1935 current->total_link_count = 0;
Al Virobd92d7f2011-03-14 19:54:59 -04001936 err = link_path_walk(name, nd);
1937
1938 if (!err && !(flags & LOOKUP_PARENT)) {
Al Virobd92d7f2011-03-14 19:54:59 -04001939 err = lookup_last(nd, &path);
1940 while (err > 0) {
1941 void *cookie;
1942 struct path link = path;
Kees Cook800179c2012-07-25 17:29:07 -07001943 err = may_follow_link(&link, nd);
1944 if (unlikely(err))
1945 break;
Al Virobd92d7f2011-03-14 19:54:59 -04001946 nd->flags |= LOOKUP_PARENT;
Al Viro574197e2011-03-14 22:20:34 -04001947 err = follow_link(&link, nd, &cookie);
Al Viro6d7b5aa2012-06-10 04:15:17 -04001948 if (err)
1949 break;
1950 err = lookup_last(nd, &path);
Al Viro574197e2011-03-14 22:20:34 -04001951 put_link(nd, &link, cookie);
Al Virobd92d7f2011-03-14 19:54:59 -04001952 }
1953 }
Al Viroee0827c2011-02-21 23:38:09 -05001954
Al Viro9f1fafe2011-03-25 11:00:12 -04001955 if (!err)
1956 err = complete_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04001957
1958 if (!err && nd->flags & LOOKUP_DIRECTORY) {
David Howellsb18825a2013-09-12 19:22:53 +01001959 if (!d_is_directory(nd->path.dentry)) {
Al Virobd92d7f2011-03-14 19:54:59 -04001960 path_put(&nd->path);
Al Virobd23a532011-03-23 09:56:30 -04001961 err = -ENOTDIR;
Al Virobd92d7f2011-03-14 19:54:59 -04001962 }
1963 }
Al Viro16c2cd72011-02-22 15:50:10 -05001964
Al Viro70e9b352011-03-05 21:12:22 -05001965 if (base)
1966 fput(base);
Al Viroee0827c2011-02-21 23:38:09 -05001967
Al Viro5b6ca022011-03-09 23:04:47 -05001968 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
Al Viro2a737872009-04-07 11:49:53 -04001969 path_put(&nd->root);
1970 nd->root.mnt = NULL;
1971 }
Al Virobd92d7f2011-03-14 19:54:59 -04001972 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001973}
Nick Piggin31e6b012011-01-07 17:49:52 +11001974
Jeff Layton873f1ee2012-10-10 15:25:29 -04001975static int filename_lookup(int dfd, struct filename *name,
1976 unsigned int flags, struct nameidata *nd)
1977{
1978 int retval = path_lookupat(dfd, name->name, flags | LOOKUP_RCU, nd);
1979 if (unlikely(retval == -ECHILD))
1980 retval = path_lookupat(dfd, name->name, flags, nd);
1981 if (unlikely(retval == -ESTALE))
1982 retval = path_lookupat(dfd, name->name,
1983 flags | LOOKUP_REVAL, nd);
1984
1985 if (likely(!retval))
Jeff Laytonadb5c242012-10-10 16:43:13 -04001986 audit_inode(name, nd->path.dentry, flags & LOOKUP_PARENT);
Jeff Layton873f1ee2012-10-10 15:25:29 -04001987 return retval;
1988}
1989
Al Viroee0827c2011-02-21 23:38:09 -05001990static int do_path_lookup(int dfd, const char *name,
1991 unsigned int flags, struct nameidata *nd)
1992{
Jeff Layton873f1ee2012-10-10 15:25:29 -04001993 struct filename filename = { .name = name };
Nick Piggin31e6b012011-01-07 17:49:52 +11001994
Jeff Layton873f1ee2012-10-10 15:25:29 -04001995 return filename_lookup(dfd, &filename, flags, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996}
1997
Al Viro79714f72012-06-15 03:01:42 +04001998/* does lookup, returns the object with parent locked */
1999struct dentry *kern_path_locked(const char *name, struct path *path)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002000{
Al Viro79714f72012-06-15 03:01:42 +04002001 struct nameidata nd;
2002 struct dentry *d;
2003 int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd);
2004 if (err)
2005 return ERR_PTR(err);
2006 if (nd.last_type != LAST_NORM) {
2007 path_put(&nd.path);
2008 return ERR_PTR(-EINVAL);
2009 }
2010 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Al Viro1e0ea002012-07-22 23:46:21 +04002011 d = __lookup_hash(&nd.last, nd.path.dentry, 0);
Al Viro79714f72012-06-15 03:01:42 +04002012 if (IS_ERR(d)) {
2013 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2014 path_put(&nd.path);
2015 return d;
2016 }
2017 *path = nd.path;
2018 return d;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002019}
2020
Al Virod1811462008-08-02 00:49:18 -04002021int kern_path(const char *name, unsigned int flags, struct path *path)
2022{
2023 struct nameidata nd;
2024 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
2025 if (!res)
2026 *path = nd.path;
2027 return res;
2028}
2029
Josef 'Jeff' Sipek16f182002007-07-19 01:48:18 -07002030/**
2031 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2032 * @dentry: pointer to dentry of the base directory
2033 * @mnt: pointer to vfs mount of the base directory
2034 * @name: pointer to file name
2035 * @flags: lookup flags
Al Viroe0a01242011-06-27 17:00:37 -04002036 * @path: pointer to struct path to fill
Josef 'Jeff' Sipek16f182002007-07-19 01:48:18 -07002037 */
2038int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2039 const char *name, unsigned int flags,
Al Viroe0a01242011-06-27 17:00:37 -04002040 struct path *path)
Josef 'Jeff' Sipek16f182002007-07-19 01:48:18 -07002041{
Al Viroe0a01242011-06-27 17:00:37 -04002042 struct nameidata nd;
2043 int err;
2044 nd.root.dentry = dentry;
2045 nd.root.mnt = mnt;
2046 BUG_ON(flags & LOOKUP_PARENT);
Al Viro5b6ca022011-03-09 23:04:47 -05002047 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
Al Viroe0a01242011-06-27 17:00:37 -04002048 err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
2049 if (!err)
2050 *path = nd.path;
2051 return err;
Josef 'Jeff' Sipek16f182002007-07-19 01:48:18 -07002052}
2053
James Morris057f6c02007-04-26 00:12:05 -07002054/*
2055 * Restricted form of lookup. Doesn't follow links, single-component only,
2056 * needs parent already locked. Doesn't follow mounts.
2057 * SMP-safe.
2058 */
Adrian Bunka244e162006-03-31 02:32:11 -08002059static struct dentry *lookup_hash(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060{
Al Viro72bd8662012-06-10 17:17:17 -04002061 return __lookup_hash(&nd->last, nd->path.dentry, nd->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062}
2063
Christoph Hellwigeead1912007-10-16 23:25:38 -07002064/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07002065 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07002066 * @name: pathname component to lookup
2067 * @base: base directory to lookup from
2068 * @len: maximum length @len should be interpreted to
2069 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07002070 * Note that this routine is purely a helper for filesystem usage and should
2071 * not be called by generic code. Also note that by using this function the
Christoph Hellwigeead1912007-10-16 23:25:38 -07002072 * nameidata argument is passed to the filesystem methods and a filesystem
2073 * using this helper needs to be prepared for that.
2074 */
James Morris057f6c02007-04-26 00:12:05 -07002075struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2076{
James Morris057f6c02007-04-26 00:12:05 -07002077 struct qstr this;
Al Viro6a96ba52011-03-07 23:49:20 -05002078 unsigned int c;
Miklos Szeredicda309d2012-03-26 12:54:21 +02002079 int err;
James Morris057f6c02007-04-26 00:12:05 -07002080
David Woodhouse2f9092e2009-04-20 23:18:37 +01002081 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
2082
Al Viro6a96ba52011-03-07 23:49:20 -05002083 this.name = name;
2084 this.len = len;
Linus Torvalds0145acc2012-03-02 14:32:59 -08002085 this.hash = full_name_hash(name, len);
Al Viro6a96ba52011-03-07 23:49:20 -05002086 if (!len)
2087 return ERR_PTR(-EACCES);
2088
Al Viro21d8a152012-11-29 22:17:21 -05002089 if (unlikely(name[0] == '.')) {
2090 if (len < 2 || (len == 2 && name[1] == '.'))
2091 return ERR_PTR(-EACCES);
2092 }
2093
Al Viro6a96ba52011-03-07 23:49:20 -05002094 while (len--) {
2095 c = *(const unsigned char *)name++;
2096 if (c == '/' || c == '\0')
2097 return ERR_PTR(-EACCES);
Al Viro6a96ba52011-03-07 23:49:20 -05002098 }
Al Viro5a202bc2011-03-08 14:17:44 -05002099 /*
2100 * See if the low-level filesystem might want
2101 * to use its own hash..
2102 */
2103 if (base->d_flags & DCACHE_OP_HASH) {
Linus Torvaldsda53be12013-05-21 15:22:44 -07002104 int err = base->d_op->d_hash(base, &this);
Al Viro5a202bc2011-03-08 14:17:44 -05002105 if (err < 0)
2106 return ERR_PTR(err);
2107 }
Christoph Hellwigeead1912007-10-16 23:25:38 -07002108
Miklos Szeredicda309d2012-03-26 12:54:21 +02002109 err = inode_permission(base->d_inode, MAY_EXEC);
2110 if (err)
2111 return ERR_PTR(err);
2112
Al Viro72bd8662012-06-10 17:17:17 -04002113 return __lookup_hash(&this, base, 0);
James Morris057f6c02007-04-26 00:12:05 -07002114}
2115
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002116int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2117 struct path *path, int *empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118{
Al Viro2d8f3032008-07-22 09:59:21 -04002119 struct nameidata nd;
Jeff Layton91a27b22012-10-10 15:25:28 -04002120 struct filename *tmp = getname_flags(name, flags, empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 int err = PTR_ERR(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 if (!IS_ERR(tmp)) {
Al Viro2d8f3032008-07-22 09:59:21 -04002123
2124 BUG_ON(flags & LOOKUP_PARENT);
2125
Jeff Layton873f1ee2012-10-10 15:25:29 -04002126 err = filename_lookup(dfd, tmp, flags, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 putname(tmp);
Al Viro2d8f3032008-07-22 09:59:21 -04002128 if (!err)
2129 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 }
2131 return err;
2132}
2133
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002134int user_path_at(int dfd, const char __user *name, unsigned flags,
2135 struct path *path)
2136{
Linus Torvaldsf7493e52012-03-22 16:10:40 -07002137 return user_path_at_empty(dfd, name, flags, path, NULL);
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002138}
2139
Jeff Layton873f1ee2012-10-10 15:25:29 -04002140/*
2141 * NB: most callers don't do anything directly with the reference to the
2142 * to struct filename, but the nd->last pointer points into the name string
2143 * allocated by getname. So we must hold the reference to it until all
2144 * path-walking is complete.
2145 */
Jeff Layton91a27b22012-10-10 15:25:28 -04002146static struct filename *
Jeff Layton9e790bd2012-12-11 12:10:09 -05002147user_path_parent(int dfd, const char __user *path, struct nameidata *nd,
2148 unsigned int flags)
Al Viro2ad94ae2008-07-21 09:32:51 -04002149{
Jeff Layton91a27b22012-10-10 15:25:28 -04002150 struct filename *s = getname(path);
Al Viro2ad94ae2008-07-21 09:32:51 -04002151 int error;
2152
Jeff Layton9e790bd2012-12-11 12:10:09 -05002153 /* only LOOKUP_REVAL is allowed in extra flags */
2154 flags &= LOOKUP_REVAL;
2155
Al Viro2ad94ae2008-07-21 09:32:51 -04002156 if (IS_ERR(s))
Jeff Layton91a27b22012-10-10 15:25:28 -04002157 return s;
Al Viro2ad94ae2008-07-21 09:32:51 -04002158
Jeff Layton9e790bd2012-12-11 12:10:09 -05002159 error = filename_lookup(dfd, s, flags | LOOKUP_PARENT, nd);
Jeff Layton91a27b22012-10-10 15:25:28 -04002160 if (error) {
Al Viro2ad94ae2008-07-21 09:32:51 -04002161 putname(s);
Jeff Layton91a27b22012-10-10 15:25:28 -04002162 return ERR_PTR(error);
2163 }
Al Viro2ad94ae2008-07-21 09:32:51 -04002164
Jeff Layton91a27b22012-10-10 15:25:28 -04002165 return s;
Al Viro2ad94ae2008-07-21 09:32:51 -04002166}
2167
Jeff Layton80334262013-07-26 06:23:25 -04002168/**
Al Viro197df042013-09-08 14:03:27 -04002169 * mountpoint_last - look up last component for umount
Jeff Layton80334262013-07-26 06:23:25 -04002170 * @nd: pathwalk nameidata - currently pointing at parent directory of "last"
2171 * @path: pointer to container for result
2172 *
2173 * This is a special lookup_last function just for umount. In this case, we
2174 * need to resolve the path without doing any revalidation.
2175 *
2176 * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
2177 * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
2178 * in almost all cases, this lookup will be served out of the dcache. The only
2179 * cases where it won't are if nd->last refers to a symlink or the path is
2180 * bogus and it doesn't exist.
2181 *
2182 * Returns:
2183 * -error: if there was an error during lookup. This includes -ENOENT if the
2184 * lookup found a negative dentry. The nd->path reference will also be
2185 * put in this case.
2186 *
2187 * 0: if we successfully resolved nd->path and found it to not to be a
2188 * symlink that needs to be followed. "path" will also be populated.
2189 * The nd->path reference will also be put.
2190 *
2191 * 1: if we successfully resolved nd->last and found it to be a symlink
2192 * that needs to be followed. "path" will be populated with the path
2193 * to the link, and nd->path will *not* be put.
2194 */
2195static int
Al Viro197df042013-09-08 14:03:27 -04002196mountpoint_last(struct nameidata *nd, struct path *path)
Jeff Layton80334262013-07-26 06:23:25 -04002197{
2198 int error = 0;
2199 struct dentry *dentry;
2200 struct dentry *dir = nd->path.dentry;
2201
Al Viro35759522013-09-08 13:41:33 -04002202 /* If we're in rcuwalk, drop out of it to handle last component */
2203 if (nd->flags & LOOKUP_RCU) {
2204 if (unlazy_walk(nd, NULL)) {
2205 error = -ECHILD;
2206 goto out;
2207 }
Jeff Layton80334262013-07-26 06:23:25 -04002208 }
2209
2210 nd->flags &= ~LOOKUP_PARENT;
2211
2212 if (unlikely(nd->last_type != LAST_NORM)) {
2213 error = handle_dots(nd, nd->last_type);
Al Viro35759522013-09-08 13:41:33 -04002214 if (error)
2215 goto out;
2216 dentry = dget(nd->path.dentry);
2217 goto done;
Jeff Layton80334262013-07-26 06:23:25 -04002218 }
2219
2220 mutex_lock(&dir->d_inode->i_mutex);
2221 dentry = d_lookup(dir, &nd->last);
2222 if (!dentry) {
2223 /*
2224 * No cached dentry. Mounted dentries are pinned in the cache,
2225 * so that means that this dentry is probably a symlink or the
2226 * path doesn't actually point to a mounted dentry.
2227 */
2228 dentry = d_alloc(dir, &nd->last);
2229 if (!dentry) {
2230 error = -ENOMEM;
Dave Jonesbcceeeb2013-09-10 17:04:25 -04002231 mutex_unlock(&dir->d_inode->i_mutex);
Al Viro35759522013-09-08 13:41:33 -04002232 goto out;
Jeff Layton80334262013-07-26 06:23:25 -04002233 }
Al Viro35759522013-09-08 13:41:33 -04002234 dentry = lookup_real(dir->d_inode, dentry, nd->flags);
2235 error = PTR_ERR(dentry);
Dave Jonesbcceeeb2013-09-10 17:04:25 -04002236 if (IS_ERR(dentry)) {
2237 mutex_unlock(&dir->d_inode->i_mutex);
Al Viro35759522013-09-08 13:41:33 -04002238 goto out;
Dave Jonesbcceeeb2013-09-10 17:04:25 -04002239 }
Jeff Layton80334262013-07-26 06:23:25 -04002240 }
2241 mutex_unlock(&dir->d_inode->i_mutex);
2242
Al Viro35759522013-09-08 13:41:33 -04002243done:
Al Virof0754e52014-04-19 12:30:58 -04002244 if (!dentry->d_inode || d_is_negative(dentry)) {
Al Viro35759522013-09-08 13:41:33 -04002245 error = -ENOENT;
2246 dput(dentry);
2247 goto out;
Jeff Layton80334262013-07-26 06:23:25 -04002248 }
Al Viro35759522013-09-08 13:41:33 -04002249 path->dentry = dentry;
2250 path->mnt = mntget(nd->path.mnt);
David Howellsb18825a2013-09-12 19:22:53 +01002251 if (should_follow_link(dentry, nd->flags & LOOKUP_FOLLOW))
Al Viro35759522013-09-08 13:41:33 -04002252 return 1;
2253 follow_mount(path);
2254 error = 0;
2255out:
Jeff Layton80334262013-07-26 06:23:25 -04002256 terminate_walk(nd);
2257 return error;
2258}
2259
2260/**
Al Viro197df042013-09-08 14:03:27 -04002261 * path_mountpoint - look up a path to be umounted
Jeff Layton80334262013-07-26 06:23:25 -04002262 * @dfd: directory file descriptor to start walk from
2263 * @name: full pathname to walk
Randy Dunlap606d6fe2013-10-19 14:56:55 -07002264 * @path: pointer to container for result
Jeff Layton80334262013-07-26 06:23:25 -04002265 * @flags: lookup flags
Jeff Layton80334262013-07-26 06:23:25 -04002266 *
2267 * Look up the given name, but don't attempt to revalidate the last component.
Randy Dunlap606d6fe2013-10-19 14:56:55 -07002268 * Returns 0 and "path" will be valid on success; Returns error otherwise.
Jeff Layton80334262013-07-26 06:23:25 -04002269 */
2270static int
Al Viro197df042013-09-08 14:03:27 -04002271path_mountpoint(int dfd, const char *name, struct path *path, unsigned int flags)
Jeff Layton80334262013-07-26 06:23:25 -04002272{
2273 struct file *base = NULL;
2274 struct nameidata nd;
2275 int err;
2276
2277 err = path_init(dfd, name, flags | LOOKUP_PARENT, &nd, &base);
2278 if (unlikely(err))
2279 return err;
2280
2281 current->total_link_count = 0;
2282 err = link_path_walk(name, &nd);
2283 if (err)
2284 goto out;
2285
Al Viro197df042013-09-08 14:03:27 -04002286 err = mountpoint_last(&nd, path);
Jeff Layton80334262013-07-26 06:23:25 -04002287 while (err > 0) {
2288 void *cookie;
2289 struct path link = *path;
2290 err = may_follow_link(&link, &nd);
2291 if (unlikely(err))
2292 break;
2293 nd.flags |= LOOKUP_PARENT;
2294 err = follow_link(&link, &nd, &cookie);
2295 if (err)
2296 break;
Al Viro197df042013-09-08 14:03:27 -04002297 err = mountpoint_last(&nd, path);
Jeff Layton80334262013-07-26 06:23:25 -04002298 put_link(&nd, &link, cookie);
2299 }
2300out:
2301 if (base)
2302 fput(base);
2303
2304 if (nd.root.mnt && !(nd.flags & LOOKUP_ROOT))
2305 path_put(&nd.root);
2306
2307 return err;
2308}
2309
Al Viro2d864652013-09-08 20:18:44 -04002310static int
2311filename_mountpoint(int dfd, struct filename *s, struct path *path,
2312 unsigned int flags)
2313{
2314 int error = path_mountpoint(dfd, s->name, path, flags | LOOKUP_RCU);
2315 if (unlikely(error == -ECHILD))
2316 error = path_mountpoint(dfd, s->name, path, flags);
2317 if (unlikely(error == -ESTALE))
2318 error = path_mountpoint(dfd, s->name, path, flags | LOOKUP_REVAL);
2319 if (likely(!error))
2320 audit_inode(s, path->dentry, 0);
2321 return error;
2322}
2323
Jeff Layton80334262013-07-26 06:23:25 -04002324/**
Al Viro197df042013-09-08 14:03:27 -04002325 * user_path_mountpoint_at - lookup a path from userland in order to umount it
Jeff Layton80334262013-07-26 06:23:25 -04002326 * @dfd: directory file descriptor
2327 * @name: pathname from userland
2328 * @flags: lookup flags
2329 * @path: pointer to container to hold result
2330 *
2331 * A umount is a special case for path walking. We're not actually interested
2332 * in the inode in this situation, and ESTALE errors can be a problem. We
2333 * simply want track down the dentry and vfsmount attached at the mountpoint
2334 * and avoid revalidating the last component.
2335 *
2336 * Returns 0 and populates "path" on success.
2337 */
2338int
Al Viro197df042013-09-08 14:03:27 -04002339user_path_mountpoint_at(int dfd, const char __user *name, unsigned int flags,
Jeff Layton80334262013-07-26 06:23:25 -04002340 struct path *path)
2341{
2342 struct filename *s = getname(name);
2343 int error;
Jeff Layton80334262013-07-26 06:23:25 -04002344 if (IS_ERR(s))
2345 return PTR_ERR(s);
Al Viro2d864652013-09-08 20:18:44 -04002346 error = filename_mountpoint(dfd, s, path, flags);
Jeff Layton80334262013-07-26 06:23:25 -04002347 putname(s);
2348 return error;
2349}
2350
Al Viro2d864652013-09-08 20:18:44 -04002351int
2352kern_path_mountpoint(int dfd, const char *name, struct path *path,
2353 unsigned int flags)
2354{
2355 struct filename s = {.name = name};
2356 return filename_mountpoint(dfd, &s, path, flags);
2357}
2358EXPORT_SYMBOL(kern_path_mountpoint);
2359
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360/*
2361 * It's inline, so penalty for filesystems that don't use sticky bit is
2362 * minimal.
2363 */
2364static inline int check_sticky(struct inode *dir, struct inode *inode)
2365{
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002366 kuid_t fsuid = current_fsuid();
David Howellsda9592e2008-11-14 10:39:05 +11002367
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 if (!(dir->i_mode & S_ISVTX))
2369 return 0;
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002370 if (uid_eq(inode->i_uid, fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 return 0;
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002372 if (uid_eq(dir->i_uid, fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 return 0;
Andy Lutomirski5bacea82014-06-10 12:45:42 -07002374 return !capable_wrt_inode_uidgid(inode, CAP_FOWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375}
2376
2377/*
2378 * Check whether we can remove a link victim from directory dir, check
2379 * whether the type of victim is right.
2380 * 1. We can't do it if dir is read-only (done in permission())
2381 * 2. We should have write and exec permissions on dir
2382 * 3. We can't remove anything from append-only dir
2383 * 4. We can't do anything with immutable dir (done in permission())
2384 * 5. If the sticky bit on dir is set we should either
2385 * a. be owner of dir, or
2386 * b. be owner of victim, or
2387 * c. have CAP_FOWNER capability
2388 * 6. If the victim is append-only or immutable we can't do antyhing with
2389 * links pointing to it.
2390 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2391 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2392 * 9. We can't remove a root or mountpoint.
2393 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
2394 * nfs_async_unlink().
2395 */
David Howellsb18825a2013-09-12 19:22:53 +01002396static int may_delete(struct inode *dir, struct dentry *victim, bool isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397{
David Howellsb18825a2013-09-12 19:22:53 +01002398 struct inode *inode = victim->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 int error;
2400
David Howellsb18825a2013-09-12 19:22:53 +01002401 if (d_is_negative(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 return -ENOENT;
David Howellsb18825a2013-09-12 19:22:53 +01002403 BUG_ON(!inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
2405 BUG_ON(victim->d_parent->d_inode != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04002406 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
Al Virof419a2e2008-07-22 00:07:17 -04002408 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 if (error)
2410 return error;
2411 if (IS_APPEND(dir))
2412 return -EPERM;
David Howellsb18825a2013-09-12 19:22:53 +01002413
2414 if (check_sticky(dir, inode) || IS_APPEND(inode) ||
2415 IS_IMMUTABLE(inode) || IS_SWAPFILE(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 return -EPERM;
2417 if (isdir) {
David Howellsb18825a2013-09-12 19:22:53 +01002418 if (!d_is_directory(victim) && !d_is_autodir(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 return -ENOTDIR;
2420 if (IS_ROOT(victim))
2421 return -EBUSY;
David Howellsb18825a2013-09-12 19:22:53 +01002422 } else if (d_is_directory(victim) || d_is_autodir(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 return -EISDIR;
2424 if (IS_DEADDIR(dir))
2425 return -ENOENT;
2426 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2427 return -EBUSY;
2428 return 0;
2429}
2430
2431/* Check whether we can create an object with dentry child in directory
2432 * dir.
2433 * 1. We can't do it if child already exists (open has special treatment for
2434 * this case, but since we are inlined it's OK)
2435 * 2. We can't do it if dir is read-only (done in permission())
2436 * 3. We should have write and exec permissions on dir
2437 * 4. We can't do it if dir is immutable (done in permission())
2438 */
Miklos Szeredia95164d2008-07-30 15:08:48 +02002439static inline int may_create(struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440{
Jeff Layton14e972b2013-05-08 10:25:58 -04002441 audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 if (child->d_inode)
2443 return -EEXIST;
2444 if (IS_DEADDIR(dir))
2445 return -ENOENT;
Al Virof419a2e2008-07-22 00:07:17 -04002446 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447}
2448
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449/*
2450 * p1 and p2 should be directories on the same fs.
2451 */
2452struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2453{
2454 struct dentry *p;
2455
2456 if (p1 == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07002457 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 return NULL;
2459 }
2460
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002461 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002463 p = d_ancestor(p2, p1);
2464 if (p) {
2465 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2466 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
2467 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 }
2469
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002470 p = d_ancestor(p1, p2);
2471 if (p) {
2472 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2473 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2474 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 }
2476
Ingo Molnarf2eace22006-07-03 00:25:05 -07002477 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2478 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 return NULL;
2480}
2481
2482void unlock_rename(struct dentry *p1, struct dentry *p2)
2483{
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002484 mutex_unlock(&p1->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 if (p1 != p2) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002486 mutex_unlock(&p2->d_inode->i_mutex);
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002487 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 }
2489}
2490
Al Viro4acdaf22011-07-26 01:42:34 -04002491int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viro312b63f2012-06-10 18:09:36 -04002492 bool want_excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002494 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 if (error)
2496 return error;
2497
Al Viroacfa4382008-12-04 10:06:33 -05002498 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 return -EACCES; /* shouldn't it be ENOSYS? */
2500 mode &= S_IALLUGO;
2501 mode |= S_IFREG;
2502 error = security_inode_create(dir, dentry, mode);
2503 if (error)
2504 return error;
Al Viro312b63f2012-06-10 18:09:36 -04002505 error = dir->i_op->create(dir, dentry, mode, want_excl);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002506 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002507 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 return error;
2509}
2510
Al Viro73d049a2011-03-11 12:08:24 -05002511static int may_open(struct path *path, int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002513 struct dentry *dentry = path->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 struct inode *inode = dentry->d_inode;
2515 int error;
2516
Al Virobcda7652011-03-13 16:42:14 -04002517 /* O_PATH? */
2518 if (!acc_mode)
2519 return 0;
2520
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 if (!inode)
2522 return -ENOENT;
2523
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002524 switch (inode->i_mode & S_IFMT) {
2525 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002527 case S_IFDIR:
2528 if (acc_mode & MAY_WRITE)
2529 return -EISDIR;
2530 break;
2531 case S_IFBLK:
2532 case S_IFCHR:
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002533 if (path->mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002535 /*FALLTHRU*/
2536 case S_IFIFO:
2537 case S_IFSOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002539 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08002540 }
Dave Hansenb41572e2007-10-16 23:31:14 -07002541
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002542 error = inode_permission(inode, acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07002543 if (error)
2544 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05002545
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 /*
2547 * An append-only file must be opened in append mode for writing.
2548 */
2549 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05002550 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05002551 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05002553 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 }
2555
2556 /* O_NOATIME can only be set by the owner or superuser */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07002557 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
Al Viro7715b522009-12-16 03:54:00 -05002558 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
J. Bruce Fieldsf3c7691e2011-09-21 10:58:13 -04002560 return 0;
Al Viro7715b522009-12-16 03:54:00 -05002561}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562
Jeff Laytone1181ee2010-12-07 16:19:50 -05002563static int handle_truncate(struct file *filp)
Al Viro7715b522009-12-16 03:54:00 -05002564{
Jeff Laytone1181ee2010-12-07 16:19:50 -05002565 struct path *path = &filp->f_path;
Al Viro7715b522009-12-16 03:54:00 -05002566 struct inode *inode = path->dentry->d_inode;
2567 int error = get_write_access(inode);
2568 if (error)
2569 return error;
2570 /*
2571 * Refuse to truncate files with mandatory locks held on them.
2572 */
2573 error = locks_verify_locked(inode);
2574 if (!error)
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +09002575 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05002576 if (!error) {
2577 error = do_truncate(path->dentry, 0,
2578 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
Jeff Laytone1181ee2010-12-07 16:19:50 -05002579 filp);
Al Viro7715b522009-12-16 03:54:00 -05002580 }
2581 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04002582 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583}
2584
Dave Hansend57999e2008-02-15 14:37:27 -08002585static inline int open_to_namei_flags(int flag)
2586{
Al Viro8a5e9292011-06-25 19:15:54 -04002587 if ((flag & O_ACCMODE) == 3)
2588 flag--;
Dave Hansend57999e2008-02-15 14:37:27 -08002589 return flag;
2590}
2591
Miklos Szeredid18e9002012-06-05 15:10:17 +02002592static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode)
2593{
2594 int error = security_path_mknod(dir, dentry, mode, 0);
2595 if (error)
2596 return error;
2597
2598 error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
2599 if (error)
2600 return error;
2601
2602 return security_inode_create(dir->dentry->d_inode, dentry, mode);
2603}
2604
David Howells1acf0af2012-06-14 16:13:46 +01002605/*
2606 * Attempt to atomically look up, create and open a file from a negative
2607 * dentry.
2608 *
2609 * Returns 0 if successful. The file will have been created and attached to
2610 * @file by the filesystem calling finish_open().
2611 *
2612 * Returns 1 if the file was looked up only or didn't need creating. The
2613 * caller will need to perform the open themselves. @path will have been
2614 * updated to point to the new dentry. This may be negative.
2615 *
2616 * Returns an error code otherwise.
2617 */
Al Viro2675a4e2012-06-22 12:41:10 +04002618static int atomic_open(struct nameidata *nd, struct dentry *dentry,
2619 struct path *path, struct file *file,
2620 const struct open_flags *op,
Al Viro64894cf2012-07-31 00:53:35 +04002621 bool got_write, bool need_lookup,
Al Viro2675a4e2012-06-22 12:41:10 +04002622 int *opened)
Miklos Szeredid18e9002012-06-05 15:10:17 +02002623{
2624 struct inode *dir = nd->path.dentry->d_inode;
2625 unsigned open_flag = open_to_namei_flags(op->open_flag);
2626 umode_t mode;
2627 int error;
2628 int acc_mode;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002629 int create_error = 0;
2630 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
Miklos Szeredi116cc022013-09-16 14:52:05 +02002631 bool excl;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002632
2633 BUG_ON(dentry->d_inode);
2634
2635 /* Don't create child dentry for a dead directory. */
2636 if (unlikely(IS_DEADDIR(dir))) {
Al Viro2675a4e2012-06-22 12:41:10 +04002637 error = -ENOENT;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002638 goto out;
2639 }
2640
Miklos Szeredi62b259d2012-08-15 13:01:24 +02002641 mode = op->mode;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002642 if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
2643 mode &= ~current_umask();
2644
Miklos Szeredi116cc022013-09-16 14:52:05 +02002645 excl = (open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT);
2646 if (excl)
Miklos Szeredid18e9002012-06-05 15:10:17 +02002647 open_flag &= ~O_TRUNC;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002648
2649 /*
2650 * Checking write permission is tricky, bacuse we don't know if we are
2651 * going to actually need it: O_CREAT opens should work as long as the
2652 * file exists. But checking existence breaks atomicity. The trick is
2653 * to check access and if not granted clear O_CREAT from the flags.
2654 *
2655 * Another problem is returing the "right" error value (e.g. for an
2656 * O_EXCL open we want to return EEXIST not EROFS).
2657 */
Al Viro64894cf2012-07-31 00:53:35 +04002658 if (((open_flag & (O_CREAT | O_TRUNC)) ||
2659 (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) {
2660 if (!(open_flag & O_CREAT)) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002661 /*
2662 * No O_CREATE -> atomicity not a requirement -> fall
2663 * back to lookup + open
2664 */
2665 goto no_open;
2666 } else if (open_flag & (O_EXCL | O_TRUNC)) {
2667 /* Fall back and fail with the right error */
Al Viro64894cf2012-07-31 00:53:35 +04002668 create_error = -EROFS;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002669 goto no_open;
2670 } else {
2671 /* No side effects, safe to clear O_CREAT */
Al Viro64894cf2012-07-31 00:53:35 +04002672 create_error = -EROFS;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002673 open_flag &= ~O_CREAT;
2674 }
2675 }
2676
2677 if (open_flag & O_CREAT) {
Miklos Szeredi38227f72012-08-15 13:01:24 +02002678 error = may_o_create(&nd->path, dentry, mode);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002679 if (error) {
2680 create_error = error;
2681 if (open_flag & O_EXCL)
2682 goto no_open;
2683 open_flag &= ~O_CREAT;
2684 }
2685 }
2686
2687 if (nd->flags & LOOKUP_DIRECTORY)
2688 open_flag |= O_DIRECTORY;
2689
Al Viro30d90492012-06-22 12:40:19 +04002690 file->f_path.dentry = DENTRY_NOT_SET;
2691 file->f_path.mnt = nd->path.mnt;
2692 error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode,
Al Viro47237682012-06-10 05:01:45 -04002693 opened);
Al Virod9585272012-06-22 12:39:14 +04002694 if (error < 0) {
Al Virod9585272012-06-22 12:39:14 +04002695 if (create_error && error == -ENOENT)
2696 error = create_error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002697 goto out;
2698 }
2699
Al Virod9585272012-06-22 12:39:14 +04002700 if (error) { /* returned 1, that is */
Al Viro30d90492012-06-22 12:40:19 +04002701 if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
Al Viro2675a4e2012-06-22 12:41:10 +04002702 error = -EIO;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002703 goto out;
2704 }
Al Viro30d90492012-06-22 12:40:19 +04002705 if (file->f_path.dentry) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002706 dput(dentry);
Al Viro30d90492012-06-22 12:40:19 +04002707 dentry = file->f_path.dentry;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002708 }
Al Viro03da6332013-09-16 19:22:33 -04002709 if (*opened & FILE_CREATED)
2710 fsnotify_create(dir, dentry);
2711 if (!dentry->d_inode) {
2712 WARN_ON(*opened & FILE_CREATED);
2713 if (create_error) {
2714 error = create_error;
2715 goto out;
2716 }
2717 } else {
2718 if (excl && !(*opened & FILE_CREATED)) {
2719 error = -EEXIST;
2720 goto out;
2721 }
Sage Weil62b2ce92012-08-15 13:30:12 -07002722 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02002723 goto looked_up;
2724 }
2725
2726 /*
2727 * We didn't have the inode before the open, so check open permission
2728 * here.
2729 */
Al Viro03da6332013-09-16 19:22:33 -04002730 acc_mode = op->acc_mode;
2731 if (*opened & FILE_CREATED) {
2732 WARN_ON(!(open_flag & O_CREAT));
2733 fsnotify_create(dir, dentry);
2734 acc_mode = MAY_OPEN;
2735 }
Al Viro2675a4e2012-06-22 12:41:10 +04002736 error = may_open(&file->f_path, acc_mode, open_flag);
2737 if (error)
2738 fput(file);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002739
2740out:
2741 dput(dentry);
Al Viro2675a4e2012-06-22 12:41:10 +04002742 return error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002743
Miklos Szeredid18e9002012-06-05 15:10:17 +02002744no_open:
2745 if (need_lookup) {
Al Viro72bd8662012-06-10 17:17:17 -04002746 dentry = lookup_real(dir, dentry, nd->flags);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002747 if (IS_ERR(dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04002748 return PTR_ERR(dentry);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002749
2750 if (create_error) {
2751 int open_flag = op->open_flag;
2752
Al Viro2675a4e2012-06-22 12:41:10 +04002753 error = create_error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002754 if ((open_flag & O_EXCL)) {
2755 if (!dentry->d_inode)
2756 goto out;
2757 } else if (!dentry->d_inode) {
2758 goto out;
2759 } else if ((open_flag & O_TRUNC) &&
2760 S_ISREG(dentry->d_inode->i_mode)) {
2761 goto out;
2762 }
2763 /* will fail later, go on to get the right error */
2764 }
2765 }
2766looked_up:
2767 path->dentry = dentry;
2768 path->mnt = nd->path.mnt;
Al Viro2675a4e2012-06-22 12:41:10 +04002769 return 1;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002770}
2771
Nick Piggin31e6b012011-01-07 17:49:52 +11002772/*
David Howells1acf0af2012-06-14 16:13:46 +01002773 * Look up and maybe create and open the last component.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002774 *
2775 * Must be called with i_mutex held on parent.
2776 *
David Howells1acf0af2012-06-14 16:13:46 +01002777 * Returns 0 if the file was successfully atomically created (if necessary) and
2778 * opened. In this case the file will be returned attached to @file.
2779 *
2780 * Returns 1 if the file was not completely opened at this time, though lookups
2781 * and creations will have been performed and the dentry returned in @path will
2782 * be positive upon return if O_CREAT was specified. If O_CREAT wasn't
2783 * specified then a negative dentry may be returned.
2784 *
2785 * An error code is returned otherwise.
2786 *
2787 * FILE_CREATE will be set in @*opened if the dentry was created and will be
2788 * cleared otherwise prior to returning.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002789 */
Al Viro2675a4e2012-06-22 12:41:10 +04002790static int lookup_open(struct nameidata *nd, struct path *path,
2791 struct file *file,
2792 const struct open_flags *op,
Al Viro64894cf2012-07-31 00:53:35 +04002793 bool got_write, int *opened)
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002794{
2795 struct dentry *dir = nd->path.dentry;
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002796 struct inode *dir_inode = dir->d_inode;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002797 struct dentry *dentry;
2798 int error;
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002799 bool need_lookup;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002800
Al Viro47237682012-06-10 05:01:45 -04002801 *opened &= ~FILE_CREATED;
Al Viro201f9562012-06-22 12:42:10 +04002802 dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002803 if (IS_ERR(dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04002804 return PTR_ERR(dentry);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002805
Miklos Szeredid18e9002012-06-05 15:10:17 +02002806 /* Cached positive dentry: will open in f_op->open */
2807 if (!need_lookup && dentry->d_inode)
2808 goto out_no_open;
2809
2810 if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) {
Al Viro64894cf2012-07-31 00:53:35 +04002811 return atomic_open(nd, dentry, path, file, op, got_write,
Al Viro47237682012-06-10 05:01:45 -04002812 need_lookup, opened);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002813 }
2814
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002815 if (need_lookup) {
2816 BUG_ON(dentry->d_inode);
2817
Al Viro72bd8662012-06-10 17:17:17 -04002818 dentry = lookup_real(dir_inode, dentry, nd->flags);
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002819 if (IS_ERR(dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04002820 return PTR_ERR(dentry);
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002821 }
2822
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002823 /* Negative dentry, just create the file */
2824 if (!dentry->d_inode && (op->open_flag & O_CREAT)) {
2825 umode_t mode = op->mode;
2826 if (!IS_POSIXACL(dir->d_inode))
2827 mode &= ~current_umask();
2828 /*
2829 * This write is needed to ensure that a
2830 * rw->ro transition does not occur between
2831 * the time when the file is created and when
2832 * a permanent write count is taken through
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02002833 * the 'struct file' in finish_open().
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002834 */
Al Viro64894cf2012-07-31 00:53:35 +04002835 if (!got_write) {
2836 error = -EROFS;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002837 goto out_dput;
Al Viro64894cf2012-07-31 00:53:35 +04002838 }
Al Viro47237682012-06-10 05:01:45 -04002839 *opened |= FILE_CREATED;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002840 error = security_path_mknod(&nd->path, dentry, mode, 0);
2841 if (error)
2842 goto out_dput;
Al Viro312b63f2012-06-10 18:09:36 -04002843 error = vfs_create(dir->d_inode, dentry, mode,
2844 nd->flags & LOOKUP_EXCL);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002845 if (error)
2846 goto out_dput;
2847 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02002848out_no_open:
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002849 path->dentry = dentry;
2850 path->mnt = nd->path.mnt;
Al Viro2675a4e2012-06-22 12:41:10 +04002851 return 1;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002852
2853out_dput:
2854 dput(dentry);
Al Viro2675a4e2012-06-22 12:41:10 +04002855 return error;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002856}
2857
2858/*
Al Virofe2d35f2011-03-05 22:58:25 -05002859 * Handle the last step of open()
Nick Piggin31e6b012011-01-07 17:49:52 +11002860 */
Al Viro2675a4e2012-06-22 12:41:10 +04002861static int do_last(struct nameidata *nd, struct path *path,
2862 struct file *file, const struct open_flags *op,
Jeff Layton669abf42012-10-10 16:43:10 -04002863 int *opened, struct filename *name)
Al Virofb1cc552009-12-24 01:58:28 -05002864{
Al Viroa1e28032009-12-24 02:12:06 -05002865 struct dentry *dir = nd->path.dentry;
Al Viroca344a892011-03-09 00:36:45 -05002866 int open_flag = op->open_flag;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002867 bool will_truncate = (open_flag & O_TRUNC) != 0;
Al Viro64894cf2012-07-31 00:53:35 +04002868 bool got_write = false;
Al Virobcda7652011-03-13 16:42:14 -04002869 int acc_mode = op->acc_mode;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02002870 struct inode *inode;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002871 bool symlink_ok = false;
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02002872 struct path save_parent = { .dentry = NULL, .mnt = NULL };
2873 bool retried = false;
Al Viro16c2cd72011-02-22 15:50:10 -05002874 int error;
Al Virofb1cc552009-12-24 01:58:28 -05002875
Al Viroc3e380b2011-02-23 13:39:45 -05002876 nd->flags &= ~LOOKUP_PARENT;
2877 nd->flags |= op->intent;
2878
Al Virobc77daa2013-06-06 09:12:33 -04002879 if (nd->last_type != LAST_NORM) {
Al Virofe2d35f2011-03-05 22:58:25 -05002880 error = handle_dots(nd, nd->last_type);
2881 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04002882 return error;
Miklos Szeredie83db162012-06-05 15:10:29 +02002883 goto finish_open;
Al Viro1f36f772009-12-26 10:56:19 -05002884 }
Al Viro67ee3ad2009-12-26 07:01:01 -05002885
Al Viroca344a892011-03-09 00:36:45 -05002886 if (!(open_flag & O_CREAT)) {
Al Virofe2d35f2011-03-05 22:58:25 -05002887 if (nd->last.name[nd->last.len])
2888 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
Al Virobcda7652011-03-13 16:42:14 -04002889 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002890 symlink_ok = true;
Al Virofe2d35f2011-03-05 22:58:25 -05002891 /* we _can_ be in RCU mode here */
Al Viroe97cdc82013-01-24 18:16:00 -05002892 error = lookup_fast(nd, path, &inode);
Miklos Szeredi71574862012-06-05 15:10:14 +02002893 if (likely(!error))
2894 goto finish_lookup;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02002895
Miklos Szeredi71574862012-06-05 15:10:14 +02002896 if (error < 0)
Al Viro2675a4e2012-06-22 12:41:10 +04002897 goto out;
Miklos Szeredi37d7fff2012-06-05 15:10:12 +02002898
Miklos Szeredi71574862012-06-05 15:10:14 +02002899 BUG_ON(nd->inode != dir->d_inode);
Miklos Szeredib6183df2012-06-05 15:10:13 +02002900 } else {
2901 /* create side of things */
2902 /*
2903 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
2904 * has been cleared when we got to the last component we are
2905 * about to look up
2906 */
2907 error = complete_walk(nd);
2908 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04002909 return error;
Miklos Szeredib6183df2012-06-05 15:10:13 +02002910
Jeff Layton33e22082013-04-12 15:16:32 -04002911 audit_inode(name, dir, LOOKUP_PARENT);
Miklos Szeredib6183df2012-06-05 15:10:13 +02002912 error = -EISDIR;
2913 /* trailing slashes? */
2914 if (nd->last.name[nd->last.len])
Al Viro2675a4e2012-06-22 12:41:10 +04002915 goto out;
Al Virofe2d35f2011-03-05 22:58:25 -05002916 }
2917
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02002918retry_lookup:
Al Viro64894cf2012-07-31 00:53:35 +04002919 if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
2920 error = mnt_want_write(nd->path.mnt);
2921 if (!error)
2922 got_write = true;
2923 /*
2924 * do _not_ fail yet - we might not need that or fail with
2925 * a different error; let lookup_open() decide; we'll be
2926 * dropping this one anyway.
2927 */
2928 }
Al Viroa1e28032009-12-24 02:12:06 -05002929 mutex_lock(&dir->d_inode->i_mutex);
Al Viro64894cf2012-07-31 00:53:35 +04002930 error = lookup_open(nd, path, file, op, got_write, opened);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002931 mutex_unlock(&dir->d_inode->i_mutex);
Al Viroa1e28032009-12-24 02:12:06 -05002932
Al Viro2675a4e2012-06-22 12:41:10 +04002933 if (error <= 0) {
2934 if (error)
Miklos Szeredid18e9002012-06-05 15:10:17 +02002935 goto out;
2936
Al Viro47237682012-06-10 05:01:45 -04002937 if ((*opened & FILE_CREATED) ||
Al Viro496ad9a2013-01-23 17:07:38 -05002938 !S_ISREG(file_inode(file)->i_mode))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002939 will_truncate = false;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002940
Jeff Laytonadb5c242012-10-10 16:43:13 -04002941 audit_inode(name, file->f_path.dentry, 0);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002942 goto opened;
2943 }
Al Virofb1cc552009-12-24 01:58:28 -05002944
Al Viro47237682012-06-10 05:01:45 -04002945 if (*opened & FILE_CREATED) {
Al Viro9b44f1b2011-03-09 00:17:27 -05002946 /* Don't check for write permission, don't truncate */
Al Viroca344a892011-03-09 00:36:45 -05002947 open_flag &= ~O_TRUNC;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002948 will_truncate = false;
Al Virobcda7652011-03-13 16:42:14 -04002949 acc_mode = MAY_OPEN;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002950 path_to_nameidata(path, nd);
Miklos Szeredie83db162012-06-05 15:10:29 +02002951 goto finish_open_created;
Al Virofb1cc552009-12-24 01:58:28 -05002952 }
2953
2954 /*
Jeff Layton3134f372012-07-25 10:19:47 -04002955 * create/update audit record if it already exists.
Al Virofb1cc552009-12-24 01:58:28 -05002956 */
David Howellsb18825a2013-09-12 19:22:53 +01002957 if (d_is_positive(path->dentry))
Jeff Laytonadb5c242012-10-10 16:43:13 -04002958 audit_inode(name, path->dentry, 0);
Al Virofb1cc552009-12-24 01:58:28 -05002959
Miklos Szeredid18e9002012-06-05 15:10:17 +02002960 /*
2961 * If atomic_open() acquired write access it is dropped now due to
2962 * possible mount and symlink following (this might be optimized away if
2963 * necessary...)
2964 */
Al Viro64894cf2012-07-31 00:53:35 +04002965 if (got_write) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002966 mnt_drop_write(nd->path.mnt);
Al Viro64894cf2012-07-31 00:53:35 +04002967 got_write = false;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002968 }
2969
Al Virofb1cc552009-12-24 01:58:28 -05002970 error = -EEXIST;
Al Virof8310c52012-07-30 11:50:30 +04002971 if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))
Al Virofb1cc552009-12-24 01:58:28 -05002972 goto exit_dput;
2973
David Howells9875cf82011-01-14 18:45:21 +00002974 error = follow_managed(path, nd->flags);
2975 if (error < 0)
2976 goto exit_dput;
Al Virofb1cc552009-12-24 01:58:28 -05002977
Al Viroa3fbbde2011-11-07 21:21:26 +00002978 if (error)
2979 nd->flags |= LOOKUP_JUMPED;
2980
Miklos Szeredidecf3402012-05-21 17:30:08 +02002981 BUG_ON(nd->flags & LOOKUP_RCU);
2982 inode = path->dentry->d_inode;
Miklos Szeredi5f5daac2012-05-21 17:30:14 +02002983finish_lookup:
2984 /* we _can_ be in RCU mode here */
Al Virofb1cc552009-12-24 01:58:28 -05002985 error = -ENOENT;
Al Virof0754e52014-04-19 12:30:58 -04002986 if (!inode || d_is_negative(path->dentry)) {
Miklos Szeredi54c33e72012-05-21 17:30:10 +02002987 path_to_nameidata(path, nd);
Al Viro2675a4e2012-06-22 12:41:10 +04002988 goto out;
Miklos Szeredi54c33e72012-05-21 17:30:10 +02002989 }
Al Viro9e67f362009-12-26 07:04:50 -05002990
David Howellsb18825a2013-09-12 19:22:53 +01002991 if (should_follow_link(path->dentry, !symlink_ok)) {
Miklos Szeredid45ea862012-05-21 17:30:09 +02002992 if (nd->flags & LOOKUP_RCU) {
2993 if (unlikely(unlazy_walk(nd, path->dentry))) {
2994 error = -ECHILD;
Al Viro2675a4e2012-06-22 12:41:10 +04002995 goto out;
Miklos Szeredid45ea862012-05-21 17:30:09 +02002996 }
2997 }
2998 BUG_ON(inode != path->dentry->d_inode);
Al Viro2675a4e2012-06-22 12:41:10 +04002999 return 1;
Miklos Szeredid45ea862012-05-21 17:30:09 +02003000 }
Al Virofb1cc552009-12-24 01:58:28 -05003001
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003002 if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) {
3003 path_to_nameidata(path, nd);
3004 } else {
3005 save_parent.dentry = nd->path.dentry;
3006 save_parent.mnt = mntget(path->mnt);
3007 nd->path.dentry = path->dentry;
3008
3009 }
Miklos Szeredidecf3402012-05-21 17:30:08 +02003010 nd->inode = inode;
Al Viroa3fbbde2011-11-07 21:21:26 +00003011 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
Al Virobc77daa2013-06-06 09:12:33 -04003012finish_open:
Al Viroa3fbbde2011-11-07 21:21:26 +00003013 error = complete_walk(nd);
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003014 if (error) {
3015 path_put(&save_parent);
Al Viro2675a4e2012-06-22 12:41:10 +04003016 return error;
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003017 }
Al Virobc77daa2013-06-06 09:12:33 -04003018 audit_inode(name, nd->path.dentry, 0);
Al Virofb1cc552009-12-24 01:58:28 -05003019 error = -EISDIR;
David Howellsb18825a2013-09-12 19:22:53 +01003020 if ((open_flag & O_CREAT) &&
3021 (d_is_directory(nd->path.dentry) || d_is_autodir(nd->path.dentry)))
Al Viro2675a4e2012-06-22 12:41:10 +04003022 goto out;
Miklos Szerediaf2f5542012-05-21 17:30:11 +02003023 error = -ENOTDIR;
David Howellsb18825a2013-09-12 19:22:53 +01003024 if ((nd->flags & LOOKUP_DIRECTORY) && !d_is_directory(nd->path.dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04003025 goto out;
Al Viro6c0d46c2011-03-09 00:59:59 -05003026 if (!S_ISREG(nd->inode->i_mode))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003027 will_truncate = false;
Al Viro6c0d46c2011-03-09 00:59:59 -05003028
Al Viro0f9d1a12011-03-09 00:13:14 -05003029 if (will_truncate) {
3030 error = mnt_want_write(nd->path.mnt);
3031 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003032 goto out;
Al Viro64894cf2012-07-31 00:53:35 +04003033 got_write = true;
Al Viro0f9d1a12011-03-09 00:13:14 -05003034 }
Miklos Szeredie83db162012-06-05 15:10:29 +02003035finish_open_created:
Al Virobcda7652011-03-13 16:42:14 -04003036 error = may_open(&nd->path, acc_mode, open_flag);
Al Viroca344a892011-03-09 00:36:45 -05003037 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003038 goto out;
Al Viro30d90492012-06-22 12:40:19 +04003039 file->f_path.mnt = nd->path.mnt;
3040 error = finish_open(file, nd->path.dentry, NULL, opened);
3041 if (error) {
Al Viro30d90492012-06-22 12:40:19 +04003042 if (error == -EOPENSTALE)
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003043 goto stale_open;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003044 goto out;
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003045 }
Miklos Szeredia8277b92012-06-05 15:10:32 +02003046opened:
Al Viro2675a4e2012-06-22 12:41:10 +04003047 error = open_check_o_direct(file);
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003048 if (error)
3049 goto exit_fput;
Al Viro2675a4e2012-06-22 12:41:10 +04003050 error = ima_file_check(file, op->acc_mode);
Miklos Szerediaa4caad2012-06-05 15:10:28 +02003051 if (error)
3052 goto exit_fput;
3053
3054 if (will_truncate) {
Al Viro2675a4e2012-06-22 12:41:10 +04003055 error = handle_truncate(file);
Miklos Szerediaa4caad2012-06-05 15:10:28 +02003056 if (error)
3057 goto exit_fput;
Al Viro0f9d1a12011-03-09 00:13:14 -05003058 }
Al Viroca344a892011-03-09 00:36:45 -05003059out:
Al Viro64894cf2012-07-31 00:53:35 +04003060 if (got_write)
Al Viro0f9d1a12011-03-09 00:13:14 -05003061 mnt_drop_write(nd->path.mnt);
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003062 path_put(&save_parent);
Miklos Szeredie276ae62012-05-21 17:30:06 +02003063 terminate_walk(nd);
Al Viro2675a4e2012-06-22 12:41:10 +04003064 return error;
Al Virofb1cc552009-12-24 01:58:28 -05003065
Al Virofb1cc552009-12-24 01:58:28 -05003066exit_dput:
3067 path_put_conditional(path, nd);
Al Viroca344a892011-03-09 00:36:45 -05003068 goto out;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003069exit_fput:
Al Viro2675a4e2012-06-22 12:41:10 +04003070 fput(file);
3071 goto out;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003072
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003073stale_open:
3074 /* If no saved parent or already retried then can't retry */
3075 if (!save_parent.dentry || retried)
3076 goto out;
3077
3078 BUG_ON(save_parent.dentry != dir);
3079 path_put(&nd->path);
3080 nd->path = save_parent;
3081 nd->inode = dir->d_inode;
3082 save_parent.mnt = NULL;
3083 save_parent.dentry = NULL;
Al Viro64894cf2012-07-31 00:53:35 +04003084 if (got_write) {
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003085 mnt_drop_write(nd->path.mnt);
Al Viro64894cf2012-07-31 00:53:35 +04003086 got_write = false;
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003087 }
3088 retried = true;
3089 goto retry_lookup;
Al Virofb1cc552009-12-24 01:58:28 -05003090}
3091
Al Viro60545d02013-06-07 01:20:27 -04003092static int do_tmpfile(int dfd, struct filename *pathname,
3093 struct nameidata *nd, int flags,
3094 const struct open_flags *op,
3095 struct file *file, int *opened)
3096{
3097 static const struct qstr name = QSTR_INIT("/", 1);
3098 struct dentry *dentry, *child;
3099 struct inode *dir;
3100 int error = path_lookupat(dfd, pathname->name,
3101 flags | LOOKUP_DIRECTORY, nd);
3102 if (unlikely(error))
3103 return error;
3104 error = mnt_want_write(nd->path.mnt);
3105 if (unlikely(error))
3106 goto out;
3107 /* we want directory to be writable */
3108 error = inode_permission(nd->inode, MAY_WRITE | MAY_EXEC);
3109 if (error)
3110 goto out2;
3111 dentry = nd->path.dentry;
3112 dir = dentry->d_inode;
3113 if (!dir->i_op->tmpfile) {
3114 error = -EOPNOTSUPP;
3115 goto out2;
3116 }
3117 child = d_alloc(dentry, &name);
3118 if (unlikely(!child)) {
3119 error = -ENOMEM;
3120 goto out2;
3121 }
3122 nd->flags &= ~LOOKUP_DIRECTORY;
3123 nd->flags |= op->intent;
3124 dput(nd->path.dentry);
3125 nd->path.dentry = child;
3126 error = dir->i_op->tmpfile(dir, nd->path.dentry, op->mode);
3127 if (error)
3128 goto out2;
3129 audit_inode(pathname, nd->path.dentry, 0);
3130 error = may_open(&nd->path, op->acc_mode, op->open_flag);
3131 if (error)
3132 goto out2;
3133 file->f_path.mnt = nd->path.mnt;
3134 error = finish_open(file, nd->path.dentry, NULL, opened);
3135 if (error)
3136 goto out2;
3137 error = open_check_o_direct(file);
Al Virof4e0c302013-06-11 08:34:36 +04003138 if (error) {
Al Viro60545d02013-06-07 01:20:27 -04003139 fput(file);
Al Virof4e0c302013-06-11 08:34:36 +04003140 } else if (!(op->open_flag & O_EXCL)) {
3141 struct inode *inode = file_inode(file);
3142 spin_lock(&inode->i_lock);
3143 inode->i_state |= I_LINKABLE;
3144 spin_unlock(&inode->i_lock);
3145 }
Al Viro60545d02013-06-07 01:20:27 -04003146out2:
3147 mnt_drop_write(nd->path.mnt);
3148out:
3149 path_put(&nd->path);
3150 return error;
3151}
3152
Jeff Layton669abf42012-10-10 16:43:10 -04003153static struct file *path_openat(int dfd, struct filename *pathname,
Al Viro73d049a2011-03-11 12:08:24 -05003154 struct nameidata *nd, const struct open_flags *op, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155{
Al Virofe2d35f2011-03-05 22:58:25 -05003156 struct file *base = NULL;
Al Viro30d90492012-06-22 12:40:19 +04003157 struct file *file;
Al Viro9850c052010-01-13 15:01:15 -05003158 struct path path;
Al Viro47237682012-06-10 05:01:45 -04003159 int opened = 0;
Al Viro13aab422011-02-23 17:54:08 -05003160 int error;
Nick Piggin31e6b012011-01-07 17:49:52 +11003161
Al Viro30d90492012-06-22 12:40:19 +04003162 file = get_empty_filp();
Al Viro1afc99b2013-02-14 20:41:04 -05003163 if (IS_ERR(file))
3164 return file;
Nick Piggin31e6b012011-01-07 17:49:52 +11003165
Al Viro30d90492012-06-22 12:40:19 +04003166 file->f_flags = op->open_flag;
Nick Piggin31e6b012011-01-07 17:49:52 +11003167
Al Virobb458c62013-07-13 13:26:37 +04003168 if (unlikely(file->f_flags & __O_TMPFILE)) {
Al Viro60545d02013-06-07 01:20:27 -04003169 error = do_tmpfile(dfd, pathname, nd, flags, op, file, &opened);
3170 goto out;
3171 }
3172
Jeff Layton669abf42012-10-10 16:43:10 -04003173 error = path_init(dfd, pathname->name, flags | LOOKUP_PARENT, nd, &base);
Nick Piggin31e6b012011-01-07 17:49:52 +11003174 if (unlikely(error))
Al Viro2675a4e2012-06-22 12:41:10 +04003175 goto out;
Nick Piggin31e6b012011-01-07 17:49:52 +11003176
Al Virofe2d35f2011-03-05 22:58:25 -05003177 current->total_link_count = 0;
Jeff Layton669abf42012-10-10 16:43:10 -04003178 error = link_path_walk(pathname->name, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11003179 if (unlikely(error))
Al Viro2675a4e2012-06-22 12:41:10 +04003180 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181
Al Viro2675a4e2012-06-22 12:41:10 +04003182 error = do_last(nd, &path, file, op, &opened, pathname);
3183 while (unlikely(error > 0)) { /* trailing symlink */
Nick Piggin7b9337a2011-01-14 08:42:43 +00003184 struct path link = path;
Al Virodef4af32009-12-26 08:37:05 -05003185 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04003186 if (!(nd->flags & LOOKUP_FOLLOW)) {
Al Viro73d049a2011-03-11 12:08:24 -05003187 path_put_conditional(&path, nd);
3188 path_put(&nd->path);
Al Viro2675a4e2012-06-22 12:41:10 +04003189 error = -ELOOP;
Al Viro40b39132011-03-09 16:22:18 -05003190 break;
3191 }
Kees Cook800179c2012-07-25 17:29:07 -07003192 error = may_follow_link(&link, nd);
3193 if (unlikely(error))
3194 break;
Al Viro73d049a2011-03-11 12:08:24 -05003195 nd->flags |= LOOKUP_PARENT;
3196 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
Al Viro574197e2011-03-14 22:20:34 -04003197 error = follow_link(&link, nd, &cookie);
Al Viroc3e380b2011-02-23 13:39:45 -05003198 if (unlikely(error))
Al Viro2675a4e2012-06-22 12:41:10 +04003199 break;
3200 error = do_last(nd, &path, file, op, &opened, pathname);
Al Viro574197e2011-03-14 22:20:34 -04003201 put_link(nd, &link, cookie);
Al Viro806b6812009-12-26 07:16:40 -05003202 }
Al Viro10fa8e62009-12-26 07:09:49 -05003203out:
Al Viro73d049a2011-03-11 12:08:24 -05003204 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
3205 path_put(&nd->root);
Al Virofe2d35f2011-03-05 22:58:25 -05003206 if (base)
3207 fput(base);
Al Viro2675a4e2012-06-22 12:41:10 +04003208 if (!(opened & FILE_OPENED)) {
3209 BUG_ON(!error);
Al Viro30d90492012-06-22 12:40:19 +04003210 put_filp(file);
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003211 }
Al Viro2675a4e2012-06-22 12:41:10 +04003212 if (unlikely(error)) {
3213 if (error == -EOPENSTALE) {
3214 if (flags & LOOKUP_RCU)
3215 error = -ECHILD;
3216 else
3217 error = -ESTALE;
3218 }
3219 file = ERR_PTR(error);
3220 }
3221 return file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222}
3223
Jeff Layton669abf42012-10-10 16:43:10 -04003224struct file *do_filp_open(int dfd, struct filename *pathname,
Al Virof9652e12013-06-11 08:23:01 +04003225 const struct open_flags *op)
Al Viro13aab422011-02-23 17:54:08 -05003226{
Al Viro73d049a2011-03-11 12:08:24 -05003227 struct nameidata nd;
Al Virof9652e12013-06-11 08:23:01 +04003228 int flags = op->lookup_flags;
Al Viro13aab422011-02-23 17:54:08 -05003229 struct file *filp;
3230
Al Viro73d049a2011-03-11 12:08:24 -05003231 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
Al Viro13aab422011-02-23 17:54:08 -05003232 if (unlikely(filp == ERR_PTR(-ECHILD)))
Al Viro73d049a2011-03-11 12:08:24 -05003233 filp = path_openat(dfd, pathname, &nd, op, flags);
Al Viro13aab422011-02-23 17:54:08 -05003234 if (unlikely(filp == ERR_PTR(-ESTALE)))
Al Viro73d049a2011-03-11 12:08:24 -05003235 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
Al Viro13aab422011-02-23 17:54:08 -05003236 return filp;
3237}
3238
Al Viro73d049a2011-03-11 12:08:24 -05003239struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
Al Virof9652e12013-06-11 08:23:01 +04003240 const char *name, const struct open_flags *op)
Al Viro73d049a2011-03-11 12:08:24 -05003241{
3242 struct nameidata nd;
3243 struct file *file;
Jeff Layton669abf42012-10-10 16:43:10 -04003244 struct filename filename = { .name = name };
Al Virof9652e12013-06-11 08:23:01 +04003245 int flags = op->lookup_flags | LOOKUP_ROOT;
Al Viro73d049a2011-03-11 12:08:24 -05003246
3247 nd.root.mnt = mnt;
3248 nd.root.dentry = dentry;
3249
David Howellsb18825a2013-09-12 19:22:53 +01003250 if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN)
Al Viro73d049a2011-03-11 12:08:24 -05003251 return ERR_PTR(-ELOOP);
3252
Jeff Layton669abf42012-10-10 16:43:10 -04003253 file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_RCU);
Al Viro73d049a2011-03-11 12:08:24 -05003254 if (unlikely(file == ERR_PTR(-ECHILD)))
Jeff Layton669abf42012-10-10 16:43:10 -04003255 file = path_openat(-1, &filename, &nd, op, flags);
Al Viro73d049a2011-03-11 12:08:24 -05003256 if (unlikely(file == ERR_PTR(-ESTALE)))
Jeff Layton669abf42012-10-10 16:43:10 -04003257 file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_REVAL);
Al Viro73d049a2011-03-11 12:08:24 -05003258 return file;
3259}
3260
Jeff Layton1ac12b42012-12-11 12:10:06 -05003261struct dentry *kern_path_create(int dfd, const char *pathname,
3262 struct path *path, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003264 struct dentry *dentry = ERR_PTR(-EEXIST);
Al Viroed75e952011-06-27 16:53:43 -04003265 struct nameidata nd;
Jan Karac30dabf2012-06-12 16:20:30 +02003266 int err2;
Jeff Layton1ac12b42012-12-11 12:10:06 -05003267 int error;
3268 bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
3269
3270 /*
3271 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
3272 * other flags passed in are ignored!
3273 */
3274 lookup_flags &= LOOKUP_REVAL;
3275
3276 error = do_path_lookup(dfd, pathname, LOOKUP_PARENT|lookup_flags, &nd);
Al Viroed75e952011-06-27 16:53:43 -04003277 if (error)
3278 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003280 /*
3281 * Yucky last component or no last component at all?
3282 * (foo/., foo/.., /////)
3283 */
Al Viroed75e952011-06-27 16:53:43 -04003284 if (nd.last_type != LAST_NORM)
3285 goto out;
3286 nd.flags &= ~LOOKUP_PARENT;
3287 nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003288
Jan Karac30dabf2012-06-12 16:20:30 +02003289 /* don't fail immediately if it's r/o, at least try to report other errors */
3290 err2 = mnt_want_write(nd.path.mnt);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003291 /*
3292 * Do the final lookup.
3293 */
Al Viroed75e952011-06-27 16:53:43 -04003294 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3295 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 if (IS_ERR(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003297 goto unlock;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003298
Al Viroa8104a92012-07-20 02:25:00 +04003299 error = -EEXIST;
David Howellsb18825a2013-09-12 19:22:53 +01003300 if (d_is_positive(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003301 goto fail;
David Howellsb18825a2013-09-12 19:22:53 +01003302
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003303 /*
3304 * Special case - lookup gave negative, but... we had foo/bar/
3305 * From the vfs_mknod() POV we just have a negative dentry -
3306 * all is fine. Let's be bastards - you had / on the end, you've
3307 * been asking for (non-existent) directory. -ENOENT for you.
3308 */
Al Viroed75e952011-06-27 16:53:43 -04003309 if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
Al Viroa8104a92012-07-20 02:25:00 +04003310 error = -ENOENT;
Al Viroed75e952011-06-27 16:53:43 -04003311 goto fail;
Al Viroe9baf6e2008-05-15 04:49:12 -04003312 }
Jan Karac30dabf2012-06-12 16:20:30 +02003313 if (unlikely(err2)) {
3314 error = err2;
Al Viroa8104a92012-07-20 02:25:00 +04003315 goto fail;
Jan Karac30dabf2012-06-12 16:20:30 +02003316 }
Al Viroed75e952011-06-27 16:53:43 -04003317 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319fail:
Al Viroa8104a92012-07-20 02:25:00 +04003320 dput(dentry);
3321 dentry = ERR_PTR(error);
3322unlock:
Al Viroed75e952011-06-27 16:53:43 -04003323 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Karac30dabf2012-06-12 16:20:30 +02003324 if (!err2)
3325 mnt_drop_write(nd.path.mnt);
Al Viroed75e952011-06-27 16:53:43 -04003326out:
3327 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 return dentry;
3329}
Al Virodae6ad82011-06-26 11:50:15 -04003330EXPORT_SYMBOL(kern_path_create);
3331
Al Viro921a1652012-07-20 01:15:31 +04003332void done_path_create(struct path *path, struct dentry *dentry)
3333{
3334 dput(dentry);
3335 mutex_unlock(&path->dentry->d_inode->i_mutex);
Al Viroa8104a92012-07-20 02:25:00 +04003336 mnt_drop_write(path->mnt);
Al Viro921a1652012-07-20 01:15:31 +04003337 path_put(path);
3338}
3339EXPORT_SYMBOL(done_path_create);
3340
Jeff Layton1ac12b42012-12-11 12:10:06 -05003341struct dentry *user_path_create(int dfd, const char __user *pathname,
3342 struct path *path, unsigned int lookup_flags)
Al Virodae6ad82011-06-26 11:50:15 -04003343{
Jeff Layton91a27b22012-10-10 15:25:28 -04003344 struct filename *tmp = getname(pathname);
Al Virodae6ad82011-06-26 11:50:15 -04003345 struct dentry *res;
3346 if (IS_ERR(tmp))
3347 return ERR_CAST(tmp);
Jeff Layton1ac12b42012-12-11 12:10:06 -05003348 res = kern_path_create(dfd, tmp->name, path, lookup_flags);
Al Virodae6ad82011-06-26 11:50:15 -04003349 putname(tmp);
3350 return res;
3351}
3352EXPORT_SYMBOL(user_path_create);
3353
Al Viro1a67aaf2011-07-26 01:52:52 -04003354int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003356 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357
3358 if (error)
3359 return error;
3360
Eric W. Biederman975d6b32011-11-13 12:16:43 -08003361 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 return -EPERM;
3363
Al Viroacfa4382008-12-04 10:06:33 -05003364 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 return -EPERM;
3366
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07003367 error = devcgroup_inode_mknod(mode, dev);
3368 if (error)
3369 return error;
3370
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 error = security_inode_mknod(dir, dentry, mode, dev);
3372 if (error)
3373 return error;
3374
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003376 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003377 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 return error;
3379}
3380
Al Virof69aac02011-07-26 04:31:40 -04003381static int may_mknod(umode_t mode)
Dave Hansen463c3192008-02-15 14:37:57 -08003382{
3383 switch (mode & S_IFMT) {
3384 case S_IFREG:
3385 case S_IFCHR:
3386 case S_IFBLK:
3387 case S_IFIFO:
3388 case S_IFSOCK:
3389 case 0: /* zero mode translates to S_IFREG */
3390 return 0;
3391 case S_IFDIR:
3392 return -EPERM;
3393 default:
3394 return -EINVAL;
3395 }
3396}
3397
Al Viro8208a222011-07-25 17:32:17 -04003398SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003399 unsigned, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400{
Al Viro2ad94ae2008-07-21 09:32:51 -04003401 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003402 struct path path;
3403 int error;
Jeff Layton972567f2012-12-20 16:00:10 -05003404 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405
Al Viro8e4bfca2012-07-20 01:17:26 +04003406 error = may_mknod(mode);
3407 if (error)
3408 return error;
Jeff Layton972567f2012-12-20 16:00:10 -05003409retry:
3410 dentry = user_path_create(dfd, filename, &path, lookup_flags);
Al Virodae6ad82011-06-26 11:50:15 -04003411 if (IS_ERR(dentry))
3412 return PTR_ERR(dentry);
Al Viro2ad94ae2008-07-21 09:32:51 -04003413
Al Virodae6ad82011-06-26 11:50:15 -04003414 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003415 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003416 error = security_path_mknod(&path, dentry, mode, dev);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003417 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04003418 goto out;
Dave Hansen463c3192008-02-15 14:37:57 -08003419 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 case 0: case S_IFREG:
Al Viro312b63f2012-06-10 18:09:36 -04003421 error = vfs_create(path.dentry->d_inode,dentry,mode,true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 break;
3423 case S_IFCHR: case S_IFBLK:
Al Virodae6ad82011-06-26 11:50:15 -04003424 error = vfs_mknod(path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 new_decode_dev(dev));
3426 break;
3427 case S_IFIFO: case S_IFSOCK:
Al Virodae6ad82011-06-26 11:50:15 -04003428 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 }
Al Viroa8104a92012-07-20 02:25:00 +04003431out:
Al Viro921a1652012-07-20 01:15:31 +04003432 done_path_create(&path, dentry);
Jeff Layton972567f2012-12-20 16:00:10 -05003433 if (retry_estale(error, lookup_flags)) {
3434 lookup_flags |= LOOKUP_REVAL;
3435 goto retry;
3436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 return error;
3438}
3439
Al Viro8208a222011-07-25 17:32:17 -04003440SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003441{
3442 return sys_mknodat(AT_FDCWD, filename, mode, dev);
3443}
3444
Al Viro18bb1db2011-07-26 01:41:39 -04003445int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003447 int error = may_create(dir, dentry);
Al Viro8de52772012-02-06 12:45:27 -05003448 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449
3450 if (error)
3451 return error;
3452
Al Viroacfa4382008-12-04 10:06:33 -05003453 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 return -EPERM;
3455
3456 mode &= (S_IRWXUGO|S_ISVTX);
3457 error = security_inode_mkdir(dir, dentry, mode);
3458 if (error)
3459 return error;
3460
Al Viro8de52772012-02-06 12:45:27 -05003461 if (max_links && dir->i_nlink >= max_links)
3462 return -EMLINK;
3463
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003465 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003466 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 return error;
3468}
3469
Al Viroa218d0f2011-11-21 14:59:34 -05003470SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471{
Dave Hansen6902d922006-09-30 23:29:01 -07003472 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003473 struct path path;
3474 int error;
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003475 unsigned int lookup_flags = LOOKUP_DIRECTORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003477retry:
3478 dentry = user_path_create(dfd, pathname, &path, lookup_flags);
Dave Hansen6902d922006-09-30 23:29:01 -07003479 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003480 return PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003481
Al Virodae6ad82011-06-26 11:50:15 -04003482 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003483 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003484 error = security_path_mkdir(&path, dentry, mode);
Al Viroa8104a92012-07-20 02:25:00 +04003485 if (!error)
3486 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
Al Viro921a1652012-07-20 01:15:31 +04003487 done_path_create(&path, dentry);
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003488 if (retry_estale(error, lookup_flags)) {
3489 lookup_flags |= LOOKUP_REVAL;
3490 goto retry;
3491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 return error;
3493}
3494
Al Viroa218d0f2011-11-21 14:59:34 -05003495SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003496{
3497 return sys_mkdirat(AT_FDCWD, pathname, mode);
3498}
3499
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500/*
Sage Weila71905f2011-05-24 13:06:08 -07003501 * The dentry_unhash() helper will try to drop the dentry early: we
J. Bruce Fieldsc0d02592012-02-15 11:48:40 -05003502 * should have a usage count of 1 if we're the only user of this
Sage Weila71905f2011-05-24 13:06:08 -07003503 * dentry, and if that is true (possibly after pruning the dcache),
3504 * then we drop the dentry now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 *
3506 * A low-level filesystem can, if it choses, legally
3507 * do a
3508 *
3509 * if (!d_unhashed(dentry))
3510 * return -EBUSY;
3511 *
3512 * if it cannot handle the case of removing a directory
3513 * that is still in use by something else..
3514 */
3515void dentry_unhash(struct dentry *dentry)
3516{
Vasily Averindc168422006-12-06 20:37:07 -08003517 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 spin_lock(&dentry->d_lock);
Waiman Long98474232013-08-28 18:24:59 -07003519 if (dentry->d_lockref.count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 __d_drop(dentry);
3521 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522}
3523
3524int vfs_rmdir(struct inode *dir, struct dentry *dentry)
3525{
3526 int error = may_delete(dir, dentry, 1);
3527
3528 if (error)
3529 return error;
3530
Al Viroacfa4382008-12-04 10:06:33 -05003531 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 return -EPERM;
3533
Al Viro1d2ef592011-09-14 18:55:41 +01003534 dget(dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003535 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536
Sage Weil912dbc12011-05-24 13:06:11 -07003537 error = -EBUSY;
3538 if (d_mountpoint(dentry))
3539 goto out;
3540
3541 error = security_inode_rmdir(dir, dentry);
3542 if (error)
3543 goto out;
3544
Sage Weil3cebde22011-05-29 21:20:59 -07003545 shrink_dcache_parent(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003546 error = dir->i_op->rmdir(dir, dentry);
3547 if (error)
3548 goto out;
3549
3550 dentry->d_inode->i_flags |= S_DEAD;
3551 dont_mount(dentry);
3552
3553out:
3554 mutex_unlock(&dentry->d_inode->i_mutex);
Al Viro1d2ef592011-09-14 18:55:41 +01003555 dput(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003556 if (!error)
3557 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 return error;
3559}
3560
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003561static long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562{
3563 int error = 0;
Jeff Layton91a27b22012-10-10 15:25:28 -04003564 struct filename *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 struct dentry *dentry;
3566 struct nameidata nd;
Jeff Laytonc6ee9202012-12-20 16:28:33 -05003567 unsigned int lookup_flags = 0;
3568retry:
3569 name = user_path_parent(dfd, pathname, &nd, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04003570 if (IS_ERR(name))
3571 return PTR_ERR(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572
3573 switch(nd.last_type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003574 case LAST_DOTDOT:
3575 error = -ENOTEMPTY;
3576 goto exit1;
3577 case LAST_DOT:
3578 error = -EINVAL;
3579 goto exit1;
3580 case LAST_ROOT:
3581 error = -EBUSY;
3582 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003584
3585 nd.flags &= ~LOOKUP_PARENT;
Jan Karac30dabf2012-06-12 16:20:30 +02003586 error = mnt_want_write(nd.path.mnt);
3587 if (error)
3588 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003589
Jan Blunck4ac91372008-02-14 19:34:32 -08003590 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08003591 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003593 if (IS_ERR(dentry))
3594 goto exit2;
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04003595 if (!dentry->d_inode) {
3596 error = -ENOENT;
3597 goto exit3;
3598 }
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003599 error = security_path_rmdir(&nd.path, dentry);
3600 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02003601 goto exit3;
Jan Blunck4ac91372008-02-14 19:34:32 -08003602 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
Dave Hansen06227532008-02-15 14:37:34 -08003603exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07003604 dput(dentry);
3605exit2:
Jan Blunck4ac91372008-02-14 19:34:32 -08003606 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Karac30dabf2012-06-12 16:20:30 +02003607 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003609 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 putname(name);
Jeff Laytonc6ee9202012-12-20 16:28:33 -05003611 if (retry_estale(error, lookup_flags)) {
3612 lookup_flags |= LOOKUP_REVAL;
3613 goto retry;
3614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 return error;
3616}
3617
Heiko Carstens3cdad422009-01-14 14:14:22 +01003618SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003619{
3620 return do_rmdir(AT_FDCWD, pathname);
3621}
3622
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003623/**
3624 * vfs_unlink - unlink a filesystem object
3625 * @dir: parent directory
3626 * @dentry: victim
3627 * @delegated_inode: returns victim inode, if the inode is delegated.
3628 *
3629 * The caller must hold dir->i_mutex.
3630 *
3631 * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
3632 * return a reference to the inode in delegated_inode. The caller
3633 * should then break the delegation on that inode and retry. Because
3634 * breaking a delegation may take a long time, the caller should drop
3635 * dir->i_mutex before doing so.
3636 *
3637 * Alternatively, a caller may pass NULL for delegated_inode. This may
3638 * be appropriate for callers that expect the underlying filesystem not
3639 * to be NFS exported.
3640 */
3641int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642{
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003643 struct inode *target = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 int error = may_delete(dir, dentry, 0);
3645
3646 if (error)
3647 return error;
3648
Al Viroacfa4382008-12-04 10:06:33 -05003649 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 return -EPERM;
3651
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003652 mutex_lock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 if (d_mountpoint(dentry))
3654 error = -EBUSY;
3655 else {
3656 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05003657 if (!error) {
J. Bruce Fields5a146962012-08-28 07:50:40 -07003658 error = try_break_deleg(target, delegated_inode);
3659 if (error)
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003660 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 error = dir->i_op->unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05003662 if (!error)
Al Virod83c49f2010-04-30 17:17:09 -04003663 dont_mount(dentry);
Al Virobec10522010-03-03 14:12:08 -05003664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 }
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003666out:
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003667 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668
3669 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
3670 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003671 fsnotify_link_count(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 }
Robert Love0eeca282005-07-12 17:06:03 -04003674
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 return error;
3676}
3677
3678/*
3679 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003680 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 * writeout happening, and we don't want to prevent access to the directory
3682 * while waiting on the I/O.
3683 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003684static long do_unlinkat(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685{
Al Viro2ad94ae2008-07-21 09:32:51 -04003686 int error;
Jeff Layton91a27b22012-10-10 15:25:28 -04003687 struct filename *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 struct dentry *dentry;
3689 struct nameidata nd;
3690 struct inode *inode = NULL;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003691 struct inode *delegated_inode = NULL;
Jeff Layton5d18f812012-12-20 16:38:04 -05003692 unsigned int lookup_flags = 0;
3693retry:
3694 name = user_path_parent(dfd, pathname, &nd, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04003695 if (IS_ERR(name))
3696 return PTR_ERR(name);
Al Viro2ad94ae2008-07-21 09:32:51 -04003697
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 error = -EISDIR;
3699 if (nd.last_type != LAST_NORM)
3700 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003701
3702 nd.flags &= ~LOOKUP_PARENT;
Jan Karac30dabf2012-06-12 16:20:30 +02003703 error = mnt_want_write(nd.path.mnt);
3704 if (error)
3705 goto exit1;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003706retry_deleg:
Jan Blunck4ac91372008-02-14 19:34:32 -08003707 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08003708 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 error = PTR_ERR(dentry);
3710 if (!IS_ERR(dentry)) {
3711 /* Why not before? Because we want correct error value */
Török Edwin50338b82011-06-16 00:06:14 +03003712 if (nd.last.name[nd.last.len])
3713 goto slashes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 inode = dentry->d_inode;
David Howellsb18825a2013-09-12 19:22:53 +01003715 if (d_is_negative(dentry))
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04003716 goto slashes;
3717 ihold(inode);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003718 error = security_path_unlink(&nd.path, dentry);
3719 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02003720 goto exit2;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003721 error = vfs_unlink(nd.path.dentry->d_inode, dentry, &delegated_inode);
Jan Karac30dabf2012-06-12 16:20:30 +02003722exit2:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 dput(dentry);
3724 }
Jan Blunck4ac91372008-02-14 19:34:32 -08003725 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 if (inode)
3727 iput(inode); /* truncate the inode here */
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003728 inode = NULL;
3729 if (delegated_inode) {
J. Bruce Fields5a146962012-08-28 07:50:40 -07003730 error = break_deleg_wait(&delegated_inode);
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003731 if (!error)
3732 goto retry_deleg;
3733 }
Jan Karac30dabf2012-06-12 16:20:30 +02003734 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003736 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737 putname(name);
Jeff Layton5d18f812012-12-20 16:38:04 -05003738 if (retry_estale(error, lookup_flags)) {
3739 lookup_flags |= LOOKUP_REVAL;
3740 inode = NULL;
3741 goto retry;
3742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 return error;
3744
3745slashes:
David Howellsb18825a2013-09-12 19:22:53 +01003746 if (d_is_negative(dentry))
3747 error = -ENOENT;
3748 else if (d_is_directory(dentry) || d_is_autodir(dentry))
3749 error = -EISDIR;
3750 else
3751 error = -ENOTDIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 goto exit2;
3753}
3754
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003755SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003756{
3757 if ((flag & ~AT_REMOVEDIR) != 0)
3758 return -EINVAL;
3759
3760 if (flag & AT_REMOVEDIR)
3761 return do_rmdir(dfd, pathname);
3762
3763 return do_unlinkat(dfd, pathname);
3764}
3765
Heiko Carstens3480b252009-01-14 14:14:16 +01003766SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003767{
3768 return do_unlinkat(AT_FDCWD, pathname);
3769}
3770
Miklos Szeredidb2e7472008-06-24 16:50:16 +02003771int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003773 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774
3775 if (error)
3776 return error;
3777
Al Viroacfa4382008-12-04 10:06:33 -05003778 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 return -EPERM;
3780
3781 error = security_inode_symlink(dir, dentry, oldname);
3782 if (error)
3783 return error;
3784
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003786 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003787 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 return error;
3789}
3790
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003791SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
3792 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793{
Al Viro2ad94ae2008-07-21 09:32:51 -04003794 int error;
Jeff Layton91a27b22012-10-10 15:25:28 -04003795 struct filename *from;
Dave Hansen6902d922006-09-30 23:29:01 -07003796 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003797 struct path path;
Jeff Laytonf46d3562012-12-11 12:10:08 -05003798 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799
3800 from = getname(oldname);
Al Viro2ad94ae2008-07-21 09:32:51 -04003801 if (IS_ERR(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 return PTR_ERR(from);
Jeff Laytonf46d3562012-12-11 12:10:08 -05003803retry:
3804 dentry = user_path_create(newdfd, newname, &path, lookup_flags);
Dave Hansen6902d922006-09-30 23:29:01 -07003805 error = PTR_ERR(dentry);
3806 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003807 goto out_putname;
Dave Hansen6902d922006-09-30 23:29:01 -07003808
Jeff Layton91a27b22012-10-10 15:25:28 -04003809 error = security_path_symlink(&path, dentry, from->name);
Al Viroa8104a92012-07-20 02:25:00 +04003810 if (!error)
Jeff Layton91a27b22012-10-10 15:25:28 -04003811 error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
Al Viro921a1652012-07-20 01:15:31 +04003812 done_path_create(&path, dentry);
Jeff Laytonf46d3562012-12-11 12:10:08 -05003813 if (retry_estale(error, lookup_flags)) {
3814 lookup_flags |= LOOKUP_REVAL;
3815 goto retry;
3816 }
Dave Hansen6902d922006-09-30 23:29:01 -07003817out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 putname(from);
3819 return error;
3820}
3821
Heiko Carstens3480b252009-01-14 14:14:16 +01003822SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003823{
3824 return sys_symlinkat(oldname, AT_FDCWD, newname);
3825}
3826
J. Bruce Fields146a8592011-09-20 17:14:31 -04003827/**
3828 * vfs_link - create a new link
3829 * @old_dentry: object to be linked
3830 * @dir: new parent
3831 * @new_dentry: where to create the new link
3832 * @delegated_inode: returns inode needing a delegation break
3833 *
3834 * The caller must hold dir->i_mutex
3835 *
3836 * If vfs_link discovers a delegation on the to-be-linked file in need
3837 * of breaking, it will return -EWOULDBLOCK and return a reference to the
3838 * inode in delegated_inode. The caller should then break the delegation
3839 * and retry. Because breaking a delegation may take a long time, the
3840 * caller should drop the i_mutex before doing so.
3841 *
3842 * Alternatively, a caller may pass NULL for delegated_inode. This may
3843 * be appropriate for callers that expect the underlying filesystem not
3844 * to be NFS exported.
3845 */
3846int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847{
3848 struct inode *inode = old_dentry->d_inode;
Al Viro8de52772012-02-06 12:45:27 -05003849 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 int error;
3851
3852 if (!inode)
3853 return -ENOENT;
3854
Miklos Szeredia95164d2008-07-30 15:08:48 +02003855 error = may_create(dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 if (error)
3857 return error;
3858
3859 if (dir->i_sb != inode->i_sb)
3860 return -EXDEV;
3861
3862 /*
3863 * A link to an append-only or immutable file cannot be created.
3864 */
3865 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3866 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05003867 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003869 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 return -EPERM;
3871
3872 error = security_inode_link(old_dentry, dir, new_dentry);
3873 if (error)
3874 return error;
3875
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003876 mutex_lock(&inode->i_mutex);
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05303877 /* Make sure we don't allow creating hardlink to an unlinked file */
Al Virof4e0c302013-06-11 08:34:36 +04003878 if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05303879 error = -ENOENT;
Al Viro8de52772012-02-06 12:45:27 -05003880 else if (max_links && inode->i_nlink >= max_links)
3881 error = -EMLINK;
J. Bruce Fields146a8592011-09-20 17:14:31 -04003882 else {
3883 error = try_break_deleg(inode, delegated_inode);
3884 if (!error)
3885 error = dir->i_op->link(old_dentry, dir, new_dentry);
3886 }
Al Virof4e0c302013-06-11 08:34:36 +04003887
3888 if (!error && (inode->i_state & I_LINKABLE)) {
3889 spin_lock(&inode->i_lock);
3890 inode->i_state &= ~I_LINKABLE;
3891 spin_unlock(&inode->i_lock);
3892 }
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003893 mutex_unlock(&inode->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07003894 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003895 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 return error;
3897}
3898
3899/*
3900 * Hardlinks are often used in delicate situations. We avoid
3901 * security-related surprises by not following symlinks on the
3902 * newname. --KAB
3903 *
3904 * We don't follow them on the oldname either to be compatible
3905 * with linux 2.0, and to avoid hard-linking to directories
3906 * and other special files. --ADM
3907 */
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003908SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
3909 int, newdfd, const char __user *, newname, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910{
3911 struct dentry *new_dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003912 struct path old_path, new_path;
J. Bruce Fields146a8592011-09-20 17:14:31 -04003913 struct inode *delegated_inode = NULL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303914 int how = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303917 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003918 return -EINVAL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303919 /*
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07003920 * To use null names we require CAP_DAC_READ_SEARCH
3921 * This ensures that not everyone will be able to create
3922 * handlink using the passed filedescriptor.
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303923 */
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07003924 if (flags & AT_EMPTY_PATH) {
3925 if (!capable(CAP_DAC_READ_SEARCH))
3926 return -ENOENT;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303927 how = LOOKUP_EMPTY;
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07003928 }
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003929
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303930 if (flags & AT_SYMLINK_FOLLOW)
3931 how |= LOOKUP_FOLLOW;
Jeff Layton442e31c2012-12-20 16:15:38 -05003932retry:
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303933 error = user_path_at(olddfd, oldname, how, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04003935 return error;
3936
Jeff Layton442e31c2012-12-20 16:15:38 -05003937 new_dentry = user_path_create(newdfd, newname, &new_path,
3938 (how & LOOKUP_REVAL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003940 if (IS_ERR(new_dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003941 goto out;
3942
3943 error = -EXDEV;
3944 if (old_path.mnt != new_path.mnt)
3945 goto out_dput;
Kees Cook800179c2012-07-25 17:29:07 -07003946 error = may_linkat(&old_path);
3947 if (unlikely(error))
3948 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04003949 error = security_path_link(old_path.dentry, &new_path, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003950 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04003951 goto out_dput;
J. Bruce Fields146a8592011-09-20 17:14:31 -04003952 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode);
Dave Hansen75c3f292008-02-15 14:37:45 -08003953out_dput:
Al Viro921a1652012-07-20 01:15:31 +04003954 done_path_create(&new_path, new_dentry);
J. Bruce Fields146a8592011-09-20 17:14:31 -04003955 if (delegated_inode) {
3956 error = break_deleg_wait(&delegated_inode);
Oleg Drokind22e6332014-01-31 15:41:58 -05003957 if (!error) {
3958 path_put(&old_path);
J. Bruce Fields146a8592011-09-20 17:14:31 -04003959 goto retry;
Oleg Drokind22e6332014-01-31 15:41:58 -05003960 }
J. Bruce Fields146a8592011-09-20 17:14:31 -04003961 }
Jeff Layton442e31c2012-12-20 16:15:38 -05003962 if (retry_estale(error, how)) {
Oleg Drokind22e6332014-01-31 15:41:58 -05003963 path_put(&old_path);
Jeff Layton442e31c2012-12-20 16:15:38 -05003964 how |= LOOKUP_REVAL;
3965 goto retry;
3966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967out:
Al Viro2d8f3032008-07-22 09:59:21 -04003968 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969
3970 return error;
3971}
3972
Heiko Carstens3480b252009-01-14 14:14:16 +01003973SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003974{
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003975 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003976}
3977
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978/*
3979 * The worst of all namespace operations - renaming directory. "Perverted"
3980 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
3981 * Problems:
3982 * a) we can get into loop creation. Check is done in is_subdir().
3983 * b) race potential - two innocent renames can create a loop together.
3984 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08003985 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 * story.
J. Bruce Fields6cedba82012-03-05 11:40:41 -05003987 * c) we have to lock _four_ objects - parents and victim (if it exists),
3988 * and source (if it is not a directory).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003989 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 * whether the target exists). Solution: try to be smart with locking
3991 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08003992 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 * move will be locked. Thus we can rank directories by the tree
3994 * (ancestors first) and rank all non-directories after them.
3995 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08003996 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 * HOWEVER, it relies on the assumption that any object with ->lookup()
3998 * has no more than 1 dentry. If "hybrid" objects will ever appear,
3999 * we'd better make sure that there's no link(2) for them.
Sage Weile4eaac02011-05-24 13:06:07 -07004000 * d) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004001 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05004003 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 * locking].
4005 */
Adrian Bunk75c96f82005-05-05 16:16:09 -07004006static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
4007 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008{
4009 int error = 0;
Sage Weil9055cba2011-05-24 13:06:12 -07004010 struct inode *target = new_dentry->d_inode;
Al Viro8de52772012-02-06 12:45:27 -05004011 unsigned max_links = new_dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012
4013 /*
4014 * If we are going to change the parent - check write permissions,
4015 * we'll need to flip '..'.
4016 */
4017 if (new_dir != old_dir) {
Al Virof419a2e2008-07-22 00:07:17 -04004018 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 if (error)
4020 return error;
4021 }
4022
4023 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
4024 if (error)
4025 return error;
4026
Al Viro1d2ef592011-09-14 18:55:41 +01004027 dget(new_dentry);
Al Virod83c49f2010-04-30 17:17:09 -04004028 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004029 mutex_lock(&target->i_mutex);
Sage Weil9055cba2011-05-24 13:06:12 -07004030
4031 error = -EBUSY;
4032 if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
4033 goto out;
4034
Al Viro8de52772012-02-06 12:45:27 -05004035 error = -EMLINK;
4036 if (max_links && !target && new_dir != old_dir &&
4037 new_dir->i_nlink >= max_links)
4038 goto out;
4039
Sage Weil3cebde22011-05-29 21:20:59 -07004040 if (target)
4041 shrink_dcache_parent(new_dentry);
Sage Weil9055cba2011-05-24 13:06:12 -07004042 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
4043 if (error)
4044 goto out;
4045
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 if (target) {
Sage Weil9055cba2011-05-24 13:06:12 -07004047 target->i_flags |= S_DEAD;
4048 dont_mount(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 }
Sage Weil9055cba2011-05-24 13:06:12 -07004050out:
4051 if (target)
4052 mutex_unlock(&target->i_mutex);
Al Viro1d2ef592011-09-14 18:55:41 +01004053 dput(new_dentry);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07004054 if (!error)
Mark Fasheh349457c2006-09-08 14:22:21 -07004055 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
4056 d_move(old_dentry,new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 return error;
4058}
4059
Adrian Bunk75c96f82005-05-05 16:16:09 -07004060static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004061 struct inode *new_dir, struct dentry *new_dentry,
4062 struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063{
Sage Weil51892bb2011-05-24 13:06:13 -07004064 struct inode *target = new_dentry->d_inode;
J. Bruce Fields6cedba82012-03-05 11:40:41 -05004065 struct inode *source = old_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 int error;
4067
4068 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
4069 if (error)
4070 return error;
4071
4072 dget(new_dentry);
J. Bruce Fields6cedba82012-03-05 11:40:41 -05004073 lock_two_nondirectories(source, target);
Sage Weil51892bb2011-05-24 13:06:13 -07004074
4075 error = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
Sage Weil51892bb2011-05-24 13:06:13 -07004077 goto out;
4078
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004079 error = try_break_deleg(source, delegated_inode);
4080 if (error)
4081 goto out;
4082 if (target) {
4083 error = try_break_deleg(target, delegated_inode);
4084 if (error)
4085 goto out;
4086 }
Sage Weil51892bb2011-05-24 13:06:13 -07004087 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
4088 if (error)
4089 goto out;
4090
4091 if (target)
4092 dont_mount(new_dentry);
4093 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
4094 d_move(old_dentry, new_dentry);
4095out:
J. Bruce Fields6cedba82012-03-05 11:40:41 -05004096 unlock_two_nondirectories(source, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 dput(new_dentry);
4098 return error;
4099}
4100
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004101/**
4102 * vfs_rename - rename a filesystem object
4103 * @old_dir: parent of source
4104 * @old_dentry: source
4105 * @new_dir: parent of destination
4106 * @new_dentry: destination
4107 * @delegated_inode: returns an inode needing a delegation break
4108 *
4109 * The caller must hold multiple mutexes--see lock_rename()).
4110 *
4111 * If vfs_rename discovers a delegation in need of breaking at either
4112 * the source or destination, it will return -EWOULDBLOCK and return a
4113 * reference to the inode in delegated_inode. The caller should then
4114 * break the delegation and retry. Because breaking a delegation may
4115 * take a long time, the caller should drop all locks before doing
4116 * so.
4117 *
4118 * Alternatively, a caller may pass NULL for delegated_inode. This may
4119 * be appropriate for callers that expect the underlying filesystem not
4120 * to be NFS exported.
4121 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004123 struct inode *new_dir, struct dentry *new_dentry,
4124 struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125{
4126 int error;
David Howellsb18825a2013-09-12 19:22:53 +01004127 int is_dir = d_is_directory(old_dentry) || d_is_autodir(old_dentry);
Eric Paris59b0df22010-02-08 12:53:52 -05004128 const unsigned char *old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129
4130 if (old_dentry->d_inode == new_dentry->d_inode)
4131 return 0;
4132
4133 error = may_delete(old_dir, old_dentry, is_dir);
4134 if (error)
4135 return error;
4136
4137 if (!new_dentry->d_inode)
Miklos Szeredia95164d2008-07-30 15:08:48 +02004138 error = may_create(new_dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 else
4140 error = may_delete(new_dir, new_dentry, is_dir);
4141 if (error)
4142 return error;
4143
Al Viroacfa4382008-12-04 10:06:33 -05004144 if (!old_dir->i_op->rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 return -EPERM;
4146
Robert Love0eeca282005-07-12 17:06:03 -04004147 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
4148
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 if (is_dir)
4150 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
4151 else
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004152 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry,delegated_inode);
Al Viro123df292009-12-25 04:57:57 -05004153 if (!error)
4154 fsnotify_move(old_dir, new_dir, old_name, is_dir,
Al Viro5a190ae2007-06-07 12:19:32 -04004155 new_dentry->d_inode, old_dentry);
Robert Love0eeca282005-07-12 17:06:03 -04004156 fsnotify_oldname_free(old_name);
4157
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 return error;
4159}
4160
Heiko Carstens2e4d0922009-01-14 14:14:31 +01004161SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4162 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163{
Al Viro2ad94ae2008-07-21 09:32:51 -04004164 struct dentry *old_dir, *new_dir;
4165 struct dentry *old_dentry, *new_dentry;
4166 struct dentry *trap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 struct nameidata oldnd, newnd;
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004168 struct inode *delegated_inode = NULL;
Jeff Layton91a27b22012-10-10 15:25:28 -04004169 struct filename *from;
4170 struct filename *to;
Jeff Laytonc6a94282012-12-11 12:10:10 -05004171 unsigned int lookup_flags = 0;
4172 bool should_retry = false;
Al Viro2ad94ae2008-07-21 09:32:51 -04004173 int error;
Jeff Laytonc6a94282012-12-11 12:10:10 -05004174retry:
4175 from = user_path_parent(olddfd, oldname, &oldnd, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04004176 if (IS_ERR(from)) {
4177 error = PTR_ERR(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 goto exit;
Jeff Layton91a27b22012-10-10 15:25:28 -04004179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180
Jeff Laytonc6a94282012-12-11 12:10:10 -05004181 to = user_path_parent(newdfd, newname, &newnd, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04004182 if (IS_ERR(to)) {
4183 error = PTR_ERR(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 goto exit1;
Jeff Layton91a27b22012-10-10 15:25:28 -04004185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186
4187 error = -EXDEV;
Jan Blunck4ac91372008-02-14 19:34:32 -08004188 if (oldnd.path.mnt != newnd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 goto exit2;
4190
Jan Blunck4ac91372008-02-14 19:34:32 -08004191 old_dir = oldnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 error = -EBUSY;
4193 if (oldnd.last_type != LAST_NORM)
4194 goto exit2;
4195
Jan Blunck4ac91372008-02-14 19:34:32 -08004196 new_dir = newnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 if (newnd.last_type != LAST_NORM)
4198 goto exit2;
4199
Jan Karac30dabf2012-06-12 16:20:30 +02004200 error = mnt_want_write(oldnd.path.mnt);
4201 if (error)
4202 goto exit2;
4203
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004204 oldnd.flags &= ~LOOKUP_PARENT;
4205 newnd.flags &= ~LOOKUP_PARENT;
OGAWA Hirofumi4e9ed2f2008-10-16 07:50:29 +09004206 newnd.flags |= LOOKUP_RENAME_TARGET;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004207
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004208retry_deleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 trap = lock_rename(new_dir, old_dir);
4210
Christoph Hellwig49705b72005-11-08 21:35:06 -08004211 old_dentry = lookup_hash(&oldnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 error = PTR_ERR(old_dentry);
4213 if (IS_ERR(old_dentry))
4214 goto exit3;
4215 /* source must exist */
4216 error = -ENOENT;
David Howellsb18825a2013-09-12 19:22:53 +01004217 if (d_is_negative(old_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 goto exit4;
4219 /* unless the source is a directory trailing slashes give -ENOTDIR */
David Howellsb18825a2013-09-12 19:22:53 +01004220 if (!d_is_directory(old_dentry) && !d_is_autodir(old_dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 error = -ENOTDIR;
4222 if (oldnd.last.name[oldnd.last.len])
4223 goto exit4;
4224 if (newnd.last.name[newnd.last.len])
4225 goto exit4;
4226 }
4227 /* source should not be ancestor of target */
4228 error = -EINVAL;
4229 if (old_dentry == trap)
4230 goto exit4;
Christoph Hellwig49705b72005-11-08 21:35:06 -08004231 new_dentry = lookup_hash(&newnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 error = PTR_ERR(new_dentry);
4233 if (IS_ERR(new_dentry))
4234 goto exit4;
4235 /* target should not be an ancestor of source */
4236 error = -ENOTEMPTY;
4237 if (new_dentry == trap)
4238 goto exit5;
4239
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004240 error = security_path_rename(&oldnd.path, old_dentry,
4241 &newnd.path, new_dentry);
4242 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02004243 goto exit5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 error = vfs_rename(old_dir->d_inode, old_dentry,
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004245 new_dir->d_inode, new_dentry,
4246 &delegated_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247exit5:
4248 dput(new_dentry);
4249exit4:
4250 dput(old_dentry);
4251exit3:
4252 unlock_rename(new_dir, old_dir);
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004253 if (delegated_inode) {
4254 error = break_deleg_wait(&delegated_inode);
4255 if (!error)
4256 goto retry_deleg;
4257 }
Jan Karac30dabf2012-06-12 16:20:30 +02004258 mnt_drop_write(oldnd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259exit2:
Jeff Laytonc6a94282012-12-11 12:10:10 -05004260 if (retry_estale(error, lookup_flags))
4261 should_retry = true;
Jan Blunck1d957f92008-02-14 19:34:35 -08004262 path_put(&newnd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04004263 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08004265 path_put(&oldnd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 putname(from);
Jeff Laytonc6a94282012-12-11 12:10:10 -05004267 if (should_retry) {
4268 should_retry = false;
4269 lookup_flags |= LOOKUP_REVAL;
4270 goto retry;
4271 }
Al Viro2ad94ae2008-07-21 09:32:51 -04004272exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 return error;
4274}
4275
Heiko Carstensa26eab22009-01-14 14:14:17 +01004276SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004277{
4278 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
4279}
4280
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
4282{
4283 int len;
4284
4285 len = PTR_ERR(link);
4286 if (IS_ERR(link))
4287 goto out;
4288
4289 len = strlen(link);
4290 if (len > (unsigned) buflen)
4291 len = buflen;
4292 if (copy_to_user(buffer, link, len))
4293 len = -EFAULT;
4294out:
4295 return len;
4296}
4297
4298/*
4299 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
4300 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
4301 * using) it for any given inode is up to filesystem.
4302 */
4303int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4304{
4305 struct nameidata nd;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004306 void *cookie;
Marcin Slusarz694a1762008-06-09 16:40:37 -07004307 int res;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004308
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 nd.depth = 0;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004310 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
Marcin Slusarz694a1762008-06-09 16:40:37 -07004311 if (IS_ERR(cookie))
4312 return PTR_ERR(cookie);
4313
4314 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
4315 if (dentry->d_inode->i_op->put_link)
4316 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
4317 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318}
4319
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320/* get the link contents into pagecache */
4321static char *page_getlink(struct dentry * dentry, struct page **ppage)
4322{
Duane Griffinebd09ab2008-12-19 20:47:12 +00004323 char *kaddr;
4324 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 struct address_space *mapping = dentry->d_inode->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -07004326 page = read_mapping_page(mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 if (IS_ERR(page))
Nick Piggin6fe69002007-05-06 14:49:04 -07004328 return (char*)page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 *ppage = page;
Duane Griffinebd09ab2008-12-19 20:47:12 +00004330 kaddr = kmap(page);
4331 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
4332 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333}
4334
4335int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4336{
4337 struct page *page = NULL;
4338 char *s = page_getlink(dentry, &page);
4339 int res = vfs_readlink(dentry,buffer,buflen,s);
4340 if (page) {
4341 kunmap(page);
4342 page_cache_release(page);
4343 }
4344 return res;
4345}
4346
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004347void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004349 struct page *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 nd_set_link(nd, page_getlink(dentry, &page));
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004351 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352}
4353
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004354void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004356 struct page *page = cookie;
4357
4358 if (page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 kunmap(page);
4360 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 }
4362}
4363
Nick Piggin54566b22009-01-04 12:00:53 -08004364/*
4365 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
4366 */
4367int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368{
4369 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004370 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07004371 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08004372 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 char *kaddr;
Nick Piggin54566b22009-01-04 12:00:53 -08004374 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
4375 if (nofs)
4376 flags |= AOP_FLAG_NOFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377
NeilBrown7e53cac2006-03-25 03:07:57 -08004378retry:
Nick Pigginafddba42007-10-16 01:25:01 -07004379 err = pagecache_write_begin(NULL, mapping, 0, len-1,
Nick Piggin54566b22009-01-04 12:00:53 -08004380 flags, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07004382 goto fail;
4383
Cong Wange8e3c3d2011-11-25 23:14:27 +08004384 kaddr = kmap_atomic(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 memcpy(kaddr, symname, len-1);
Cong Wange8e3c3d2011-11-25 23:14:27 +08004386 kunmap_atomic(kaddr);
Nick Pigginafddba42007-10-16 01:25:01 -07004387
4388 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4389 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 if (err < 0)
4391 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07004392 if (err < len-1)
4393 goto retry;
4394
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 mark_inode_dirty(inode);
4396 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397fail:
4398 return err;
4399}
4400
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004401int page_symlink(struct inode *inode, const char *symname, int len)
4402{
4403 return __page_symlink(inode, symname, len,
Nick Piggin54566b22009-01-04 12:00:53 -08004404 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004405}
4406
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08004407const struct inode_operations page_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 .readlink = generic_readlink,
4409 .follow_link = page_follow_link_light,
4410 .put_link = page_put_link,
4411};
4412
Al Viro2d8f3032008-07-22 09:59:21 -04004413EXPORT_SYMBOL(user_path_at);
David Howellscc53ce52011-01-14 18:45:26 +00004414EXPORT_SYMBOL(follow_down_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415EXPORT_SYMBOL(follow_down);
4416EXPORT_SYMBOL(follow_up);
Al Virof6d2ac52012-08-26 12:55:54 -04004417EXPORT_SYMBOL(get_write_access); /* nfsd */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419EXPORT_SYMBOL(lookup_one_len);
4420EXPORT_SYMBOL(page_follow_link_light);
4421EXPORT_SYMBOL(page_put_link);
4422EXPORT_SYMBOL(page_readlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004423EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424EXPORT_SYMBOL(page_symlink);
4425EXPORT_SYMBOL(page_symlink_inode_operations);
Al Virod1811462008-08-02 00:49:18 -04004426EXPORT_SYMBOL(kern_path);
Josef 'Jeff' Sipek16f182002007-07-19 01:48:18 -07004427EXPORT_SYMBOL(vfs_path_lookup);
Al Virof419a2e2008-07-22 00:07:17 -04004428EXPORT_SYMBOL(inode_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429EXPORT_SYMBOL(unlock_rename);
4430EXPORT_SYMBOL(vfs_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431EXPORT_SYMBOL(vfs_link);
4432EXPORT_SYMBOL(vfs_mkdir);
4433EXPORT_SYMBOL(vfs_mknod);
4434EXPORT_SYMBOL(generic_permission);
4435EXPORT_SYMBOL(vfs_readlink);
4436EXPORT_SYMBOL(vfs_rename);
4437EXPORT_SYMBOL(vfs_rmdir);
4438EXPORT_SYMBOL(vfs_symlink);
4439EXPORT_SYMBOL(vfs_unlink);
4440EXPORT_SYMBOL(dentry_unhash);
4441EXPORT_SYMBOL(generic_readlink);