blob: bcb838e2e52f24593da609abba67dc5ab5336947 [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;
199 audit_getname(result);
200 return result;
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700201
202error:
Jeff Layton7950e382012-10-10 16:43:13 -0400203 final_putname(result);
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700204 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206
Jeff Layton91a27b22012-10-10 15:25:28 -0400207struct filename *
208getname(const char __user * filename)
Al Virof52e0c12011-03-14 18:56:51 -0400209{
Linus Torvaldsf7493e52012-03-22 16:10:40 -0700210 return getname_flags(filename, 0, NULL);
Al Virof52e0c12011-03-14 18:56:51 -0400211}
Jeff Layton91a27b22012-10-10 15:25:28 -0400212EXPORT_SYMBOL(getname);
Al Virof52e0c12011-03-14 18:56:51 -0400213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214#ifdef CONFIG_AUDITSYSCALL
Jeff Layton91a27b22012-10-10 15:25:28 -0400215void putname(struct filename *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
Al Viro5ac3a9c2006-07-16 06:38:45 -0400217 if (unlikely(!audit_dummy_context()))
Jeff Layton91a27b22012-10-10 15:25:28 -0400218 return audit_putname(name);
219 final_putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221#endif
222
Linus Torvaldse77819e2011-07-22 19:30:19 -0700223static int check_acl(struct inode *inode, int mask)
224{
Linus Torvalds84635d62011-07-25 22:47:03 -0700225#ifdef CONFIG_FS_POSIX_ACL
Linus Torvaldse77819e2011-07-22 19:30:19 -0700226 struct posix_acl *acl;
227
Linus Torvaldse77819e2011-07-22 19:30:19 -0700228 if (mask & MAY_NOT_BLOCK) {
Al Viro35678662011-08-02 21:32:13 -0400229 acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
230 if (!acl)
Linus Torvaldse77819e2011-07-22 19:30:19 -0700231 return -EAGAIN;
Al Viro35678662011-08-02 21:32:13 -0400232 /* no ->get_acl() calls in RCU mode... */
233 if (acl == ACL_NOT_CACHED)
234 return -ECHILD;
Ari Savolainen206b1d02011-08-06 19:43:07 +0300235 return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
Linus Torvaldse77819e2011-07-22 19:30:19 -0700236 }
237
Christoph Hellwig2982baa2013-12-20 05:16:38 -0800238 acl = get_acl(inode, ACL_TYPE_ACCESS);
239 if (IS_ERR(acl))
240 return PTR_ERR(acl);
Linus Torvaldse77819e2011-07-22 19:30:19 -0700241 if (acl) {
242 int error = posix_acl_permission(inode, acl, mask);
243 posix_acl_release(acl);
244 return error;
245 }
Linus Torvalds84635d62011-07-25 22:47:03 -0700246#endif
Linus Torvaldse77819e2011-07-22 19:30:19 -0700247
248 return -EAGAIN;
249}
250
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700251/*
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530252 * This does the basic permission checking
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700253 */
Al Viro7e401452011-06-20 19:12:17 -0400254static int acl_permission_check(struct inode *inode, int mask)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700255{
Linus Torvalds26cf46b2011-05-13 11:51:01 -0700256 unsigned int mode = inode->i_mode;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700257
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -0800258 if (likely(uid_eq(current_fsuid(), inode->i_uid)))
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700259 mode >>= 6;
260 else {
Linus Torvaldse77819e2011-07-22 19:30:19 -0700261 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
Al Viro7e401452011-06-20 19:12:17 -0400262 int error = check_acl(inode, mask);
Nick Pigginb74c79e2011-01-07 17:49:58 +1100263 if (error != -EAGAIN)
264 return error;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700265 }
266
267 if (in_group_p(inode->i_gid))
268 mode >>= 3;
269 }
270
271 /*
272 * If the DACs are ok we don't need any capability check.
273 */
Al Viro9c2c7032011-06-20 19:06:22 -0400274 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700275 return 0;
276 return -EACCES;
277}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279/**
Nick Pigginb74c79e2011-01-07 17:49:58 +1100280 * generic_permission - check for access rights on a Posix-like filesystem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 * @inode: inode to check access rights for
Andreas Gruenbacher8fd90c82011-10-23 23:13:30 +0530282 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 *
284 * Used to check for read/write/execute permissions on a file.
285 * We use "fsuid" for this, letting us set arbitrary permissions
286 * for filesystem access without changing the "normal" uids which
Nick Pigginb74c79e2011-01-07 17:49:58 +1100287 * are used for other things.
288 *
289 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
290 * request cannot be satisfied (eg. requires blocking or too much complexity).
291 * It would then be called again in ref-walk mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 */
Al Viro2830ba72011-06-20 19:16:29 -0400293int generic_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700295 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 /*
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530298 * Do the basic permission checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 */
Al Viro7e401452011-06-20 19:12:17 -0400300 ret = acl_permission_check(inode, mask);
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700301 if (ret != -EACCES)
302 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Al Virod594e7e2011-06-20 19:55:42 -0400304 if (S_ISDIR(inode->i_mode)) {
305 /* DACs are overridable for directories */
Eric W. Biederman1a48e2a2011-11-14 16:24:06 -0800306 if (inode_capable(inode, CAP_DAC_OVERRIDE))
Al Virod594e7e2011-06-20 19:55:42 -0400307 return 0;
308 if (!(mask & MAY_WRITE))
Eric W. Biederman1a48e2a2011-11-14 16:24:06 -0800309 if (inode_capable(inode, CAP_DAC_READ_SEARCH))
Al Virod594e7e2011-06-20 19:55:42 -0400310 return 0;
311 return -EACCES;
312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 /*
314 * Read/write DACs are always overridable.
Al Virod594e7e2011-06-20 19:55:42 -0400315 * Executable DACs are overridable when there is
316 * at least one exec bit set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 */
Al Virod594e7e2011-06-20 19:55:42 -0400318 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
Eric W. Biederman1a48e2a2011-11-14 16:24:06 -0800319 if (inode_capable(inode, CAP_DAC_OVERRIDE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return 0;
321
322 /*
323 * Searching includes executable on directories, else just read.
324 */
Serge E. Hallyn7ea66002009-12-29 14:50:19 -0600325 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
Al Virod594e7e2011-06-20 19:55:42 -0400326 if (mask == MAY_READ)
Eric W. Biederman1a48e2a2011-11-14 16:24:06 -0800327 if (inode_capable(inode, CAP_DAC_READ_SEARCH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 return 0;
329
330 return -EACCES;
331}
332
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700333/*
334 * We _really_ want to just do "generic_permission()" without
335 * even looking at the inode->i_op values. So we keep a cache
336 * flag in inode->i_opflags, that says "this has not special
337 * permission function, use the fast case".
338 */
339static inline int do_inode_permission(struct inode *inode, int mask)
340{
341 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
342 if (likely(inode->i_op->permission))
343 return inode->i_op->permission(inode, mask);
344
345 /* This gets set once for the inode lifetime */
346 spin_lock(&inode->i_lock);
347 inode->i_opflags |= IOP_FASTPERM;
348 spin_unlock(&inode->i_lock);
349 }
350 return generic_permission(inode, mask);
351}
352
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200353/**
David Howells0bdaea92012-06-25 12:55:46 +0100354 * __inode_permission - Check for access rights to a given inode
355 * @inode: Inode to check permission on
356 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200357 *
David Howells0bdaea92012-06-25 12:55:46 +0100358 * Check for read/write/execute permissions on an inode.
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530359 *
360 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
David Howells0bdaea92012-06-25 12:55:46 +0100361 *
362 * This does not check for a read-only file system. You probably want
363 * inode_permission().
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200364 */
David Howells0bdaea92012-06-25 12:55:46 +0100365int __inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
Al Viroe6305c42008-07-15 21:03:57 -0400367 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700369 if (unlikely(mask & MAY_WRITE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 /*
371 * Nobody gets write access to an immutable file.
372 */
373 if (IS_IMMUTABLE(inode))
374 return -EACCES;
375 }
376
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700377 retval = do_inode_permission(inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if (retval)
379 return retval;
380
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700381 retval = devcgroup_inode_permission(inode, mask);
382 if (retval)
383 return retval;
384
Eric Parisd09ca732010-07-23 11:43:57 -0400385 return security_inode_permission(inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
Al Virof4d6ff82011-06-19 13:14:21 -0400388/**
David Howells0bdaea92012-06-25 12:55:46 +0100389 * sb_permission - Check superblock-level permissions
390 * @sb: Superblock of inode to check permission on
Randy Dunlap55852632012-08-18 17:39:25 -0700391 * @inode: Inode to check permission on
David Howells0bdaea92012-06-25 12:55:46 +0100392 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
393 *
394 * Separate out file-system wide checks from inode-specific permission checks.
395 */
396static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
397{
398 if (unlikely(mask & MAY_WRITE)) {
399 umode_t mode = inode->i_mode;
400
401 /* Nobody gets write access to a read-only fs. */
402 if ((sb->s_flags & MS_RDONLY) &&
403 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
404 return -EROFS;
405 }
406 return 0;
407}
408
409/**
410 * inode_permission - Check for access rights to a given inode
411 * @inode: Inode to check permission on
412 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
413 *
414 * Check for read/write/execute permissions on an inode. We use fs[ug]id for
415 * this, letting us set arbitrary permissions for filesystem access without
416 * changing the "normal" UIDs which are used for other things.
417 *
418 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
419 */
420int inode_permission(struct inode *inode, int mask)
421{
422 int retval;
423
424 retval = sb_permission(inode->i_sb, inode, mask);
425 if (retval)
426 return retval;
427 return __inode_permission(inode, mask);
428}
429
430/**
Jan Blunck5dd784d02008-02-14 19:34:38 -0800431 * path_get - get a reference to a path
432 * @path: path to get the reference to
433 *
434 * Given a path increment the reference count to the dentry and the vfsmount.
435 */
Al Virodcf787f2013-03-01 23:51:07 -0500436void path_get(const struct path *path)
Jan Blunck5dd784d02008-02-14 19:34:38 -0800437{
438 mntget(path->mnt);
439 dget(path->dentry);
440}
441EXPORT_SYMBOL(path_get);
442
443/**
Jan Blunck1d957f92008-02-14 19:34:35 -0800444 * path_put - put a reference to a path
445 * @path: path to put the reference to
446 *
447 * Given a path decrement the reference count to the dentry and the vfsmount.
448 */
Al Virodcf787f2013-03-01 23:51:07 -0500449void path_put(const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
Jan Blunck1d957f92008-02-14 19:34:35 -0800451 dput(path->dentry);
452 mntput(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
Jan Blunck1d957f92008-02-14 19:34:35 -0800454EXPORT_SYMBOL(path_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Al Viro19660af2011-03-25 10:32:48 -0400456/*
Nick Piggin31e6b012011-01-07 17:49:52 +1100457 * Path walking has 2 modes, rcu-walk and ref-walk (see
Al Viro19660af2011-03-25 10:32:48 -0400458 * Documentation/filesystems/path-lookup.txt). In situations when we can't
459 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
460 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
461 * mode. Refcounts are grabbed at the last known good point before rcu-walk
462 * got stuck, so ref-walk may continue from there. If this is not successful
463 * (eg. a seqcount has changed), then failure is returned and it's up to caller
464 * to restart the path walk from the beginning in ref-walk mode.
Nick Piggin31e6b012011-01-07 17:49:52 +1100465 */
Nick Piggin31e6b012011-01-07 17:49:52 +1100466
467/**
Al Viro19660af2011-03-25 10:32:48 -0400468 * unlazy_walk - try to switch to ref-walk mode.
469 * @nd: nameidata pathwalk data
470 * @dentry: child of nd->path.dentry or NULL
Randy Dunlap39191622011-01-08 19:36:21 -0800471 * Returns: 0 on success, -ECHILD on failure
Nick Piggin31e6b012011-01-07 17:49:52 +1100472 *
Al Viro19660af2011-03-25 10:32:48 -0400473 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
474 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
475 * @nd or NULL. Must be called from rcu-walk context.
Nick Piggin31e6b012011-01-07 17:49:52 +1100476 */
Al Viro19660af2011-03-25 10:32:48 -0400477static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
Nick Piggin31e6b012011-01-07 17:49:52 +1100478{
479 struct fs_struct *fs = current->fs;
480 struct dentry *parent = nd->path.dentry;
481
482 BUG_ON(!(nd->flags & LOOKUP_RCU));
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700483
484 /*
Al Viro48a066e2013-09-29 22:06:07 -0400485 * After legitimizing the bastards, terminate_walk()
486 * will do the right thing for non-RCU mode, and all our
487 * subsequent exit cases should rcu_read_unlock()
488 * before returning. Do vfsmount first; if dentry
489 * can't be legitimized, just set nd->path.dentry to NULL
490 * and rely on dput(NULL) being a no-op.
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700491 */
Al Viro48a066e2013-09-29 22:06:07 -0400492 if (!legitimize_mnt(nd->path.mnt, nd->m_seq))
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700493 return -ECHILD;
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700494 nd->flags &= ~LOOKUP_RCU;
Linus Torvalds15570082013-09-02 11:38:06 -0700495
Al Viro48a066e2013-09-29 22:06:07 -0400496 if (!lockref_get_not_dead(&parent->d_lockref)) {
497 nd->path.dentry = NULL;
Al Virod870b4a2013-11-29 01:48:32 -0500498 goto out;
Al Viro48a066e2013-09-29 22:06:07 -0400499 }
500
Linus Torvalds15570082013-09-02 11:38:06 -0700501 /*
502 * For a negative lookup, the lookup sequence point is the parents
503 * sequence point, and it only needs to revalidate the parent dentry.
504 *
505 * For a positive lookup, we need to move both the parent and the
506 * dentry from the RCU domain to be properly refcounted. And the
507 * sequence number in the dentry validates *both* dentry counters,
508 * since we checked the sequence number of the parent after we got
509 * the child sequence number. So we know the parent must still
510 * be valid if the child sequence number is still valid.
511 */
Al Viro19660af2011-03-25 10:32:48 -0400512 if (!dentry) {
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700513 if (read_seqcount_retry(&parent->d_seq, nd->seq))
514 goto out;
Al Viro19660af2011-03-25 10:32:48 -0400515 BUG_ON(nd->inode != parent->d_inode);
516 } else {
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700517 if (!lockref_get_not_dead(&dentry->d_lockref))
518 goto out;
519 if (read_seqcount_retry(&dentry->d_seq, nd->seq))
520 goto drop_dentry;
Al Viro19660af2011-03-25 10:32:48 -0400521 }
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700522
523 /*
524 * Sequence counts matched. Now make sure that the root is
525 * still valid and get it if required.
526 */
527 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
528 spin_lock(&fs->lock);
529 if (nd->root.mnt != fs->root.mnt || nd->root.dentry != fs->root.dentry)
530 goto unlock_and_drop_dentry;
Nick Piggin31e6b012011-01-07 17:49:52 +1100531 path_get(&nd->root);
532 spin_unlock(&fs->lock);
533 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100534
Al Viro8b61e742013-11-08 12:45:01 -0500535 rcu_read_unlock();
Nick Piggin31e6b012011-01-07 17:49:52 +1100536 return 0;
Al Viro19660af2011-03-25 10:32:48 -0400537
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700538unlock_and_drop_dentry:
539 spin_unlock(&fs->lock);
540drop_dentry:
Al Viro8b61e742013-11-08 12:45:01 -0500541 rcu_read_unlock();
Linus Torvalds15570082013-09-02 11:38:06 -0700542 dput(dentry);
Linus Torvaldsd0d27272013-09-10 12:17:49 -0700543 goto drop_root_mnt;
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700544out:
Al Viro8b61e742013-11-08 12:45:01 -0500545 rcu_read_unlock();
Linus Torvaldsd0d27272013-09-10 12:17:49 -0700546drop_root_mnt:
547 if (!(nd->flags & LOOKUP_ROOT))
548 nd->root.mnt = NULL;
Nick Piggin31e6b012011-01-07 17:49:52 +1100549 return -ECHILD;
550}
551
Al Viro4ce16ef32012-06-10 16:10:59 -0400552static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
Nick Piggin34286d62011-01-07 17:49:57 +1100553{
Al Viro4ce16ef32012-06-10 16:10:59 -0400554 return dentry->d_op->d_revalidate(dentry, flags);
Nick Piggin34286d62011-01-07 17:49:57 +1100555}
556
Al Viro9f1fafe2011-03-25 11:00:12 -0400557/**
558 * complete_walk - successful completion of path walk
559 * @nd: pointer nameidata
Jeff Layton39159de2009-12-07 12:01:50 -0500560 *
Al Viro9f1fafe2011-03-25 11:00:12 -0400561 * If we had been in RCU mode, drop out of it and legitimize nd->path.
562 * Revalidate the final result, unless we'd already done that during
563 * the path walk or the filesystem doesn't ask for it. Return 0 on
564 * success, -error on failure. In case of failure caller does not
565 * need to drop nd->path.
Jeff Layton39159de2009-12-07 12:01:50 -0500566 */
Al Viro9f1fafe2011-03-25 11:00:12 -0400567static int complete_walk(struct nameidata *nd)
Jeff Layton39159de2009-12-07 12:01:50 -0500568{
Al Viro16c2cd72011-02-22 15:50:10 -0500569 struct dentry *dentry = nd->path.dentry;
Jeff Layton39159de2009-12-07 12:01:50 -0500570 int status;
Jeff Layton39159de2009-12-07 12:01:50 -0500571
Al Viro9f1fafe2011-03-25 11:00:12 -0400572 if (nd->flags & LOOKUP_RCU) {
573 nd->flags &= ~LOOKUP_RCU;
574 if (!(nd->flags & LOOKUP_ROOT))
575 nd->root.mnt = NULL;
Linus Torvalds15570082013-09-02 11:38:06 -0700576
Al Viro48a066e2013-09-29 22:06:07 -0400577 if (!legitimize_mnt(nd->path.mnt, nd->m_seq)) {
Al Viro8b61e742013-11-08 12:45:01 -0500578 rcu_read_unlock();
Al Viro48a066e2013-09-29 22:06:07 -0400579 return -ECHILD;
580 }
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700581 if (unlikely(!lockref_get_not_dead(&dentry->d_lockref))) {
Al Viro8b61e742013-11-08 12:45:01 -0500582 rcu_read_unlock();
Al Viro48a066e2013-09-29 22:06:07 -0400583 mntput(nd->path.mnt);
Al Viro9f1fafe2011-03-25 11:00:12 -0400584 return -ECHILD;
585 }
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700586 if (read_seqcount_retry(&dentry->d_seq, nd->seq)) {
Al Viro8b61e742013-11-08 12:45:01 -0500587 rcu_read_unlock();
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700588 dput(dentry);
Al Viro48a066e2013-09-29 22:06:07 -0400589 mntput(nd->path.mnt);
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700590 return -ECHILD;
591 }
Al Viro8b61e742013-11-08 12:45:01 -0500592 rcu_read_unlock();
Al Viro9f1fafe2011-03-25 11:00:12 -0400593 }
594
Al Viro16c2cd72011-02-22 15:50:10 -0500595 if (likely(!(nd->flags & LOOKUP_JUMPED)))
Jeff Layton39159de2009-12-07 12:01:50 -0500596 return 0;
597
Jeff Laytonecf3d1f2013-02-20 11:19:05 -0500598 if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
Al Viro16c2cd72011-02-22 15:50:10 -0500599 return 0;
600
Jeff Laytonecf3d1f2013-02-20 11:19:05 -0500601 status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
Jeff Layton39159de2009-12-07 12:01:50 -0500602 if (status > 0)
603 return 0;
604
Al Viro16c2cd72011-02-22 15:50:10 -0500605 if (!status)
Jeff Layton39159de2009-12-07 12:01:50 -0500606 status = -ESTALE;
Al Viro16c2cd72011-02-22 15:50:10 -0500607
Al Viro9f1fafe2011-03-25 11:00:12 -0400608 path_put(&nd->path);
Jeff Layton39159de2009-12-07 12:01:50 -0500609 return status;
610}
611
Al Viro2a737872009-04-07 11:49:53 -0400612static __always_inline void set_root(struct nameidata *nd)
613{
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200614 if (!nd->root.mnt)
615 get_fs_root(current->fs, &nd->root);
Al Viro2a737872009-04-07 11:49:53 -0400616}
617
Al Viro6de88d72009-08-09 01:41:57 +0400618static int link_path_walk(const char *, struct nameidata *);
619
Nick Piggin31e6b012011-01-07 17:49:52 +1100620static __always_inline void set_root_rcu(struct nameidata *nd)
621{
622 if (!nd->root.mnt) {
623 struct fs_struct *fs = current->fs;
Nick Pigginc28cc362011-01-07 17:49:53 +1100624 unsigned seq;
625
626 do {
627 seq = read_seqcount_begin(&fs->seq);
628 nd->root = fs->root;
Tim Chenc1530012011-04-15 11:39:29 -0700629 nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
Nick Pigginc28cc362011-01-07 17:49:53 +1100630 } while (read_seqcount_retry(&fs->seq, seq));
Nick Piggin31e6b012011-01-07 17:49:52 +1100631 }
632}
633
Jan Blunck1d957f92008-02-14 19:34:35 -0800634static void path_put_conditional(struct path *path, struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700635{
636 dput(path->dentry);
Jan Blunck4ac91372008-02-14 19:34:32 -0800637 if (path->mnt != nd->path.mnt)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700638 mntput(path->mnt);
639}
640
Nick Piggin7b9337a2011-01-14 08:42:43 +0000641static inline void path_to_nameidata(const struct path *path,
642 struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700643{
Nick Piggin31e6b012011-01-07 17:49:52 +1100644 if (!(nd->flags & LOOKUP_RCU)) {
645 dput(nd->path.dentry);
646 if (nd->path.mnt != path->mnt)
647 mntput(nd->path.mnt);
Huang Shijie9a229682010-04-02 17:37:13 +0800648 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100649 nd->path.mnt = path->mnt;
Jan Blunck4ac91372008-02-14 19:34:32 -0800650 nd->path.dentry = path->dentry;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700651}
652
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400653/*
654 * Helper to directly jump to a known parsed path from ->follow_link,
655 * caller must have taken a reference to path beforehand.
656 */
657void nd_jump_link(struct nameidata *nd, struct path *path)
658{
659 path_put(&nd->path);
660
661 nd->path = *path;
662 nd->inode = nd->path.dentry->d_inode;
663 nd->flags |= LOOKUP_JUMPED;
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400664}
665
Al Viro574197e2011-03-14 22:20:34 -0400666static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
667{
668 struct inode *inode = link->dentry->d_inode;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400669 if (inode->i_op->put_link)
Al Viro574197e2011-03-14 22:20:34 -0400670 inode->i_op->put_link(link->dentry, nd, cookie);
671 path_put(link);
672}
673
Linus Torvalds561ec642012-10-26 10:05:07 -0700674int sysctl_protected_symlinks __read_mostly = 0;
675int sysctl_protected_hardlinks __read_mostly = 0;
Kees Cook800179c2012-07-25 17:29:07 -0700676
677/**
678 * may_follow_link - Check symlink following for unsafe situations
679 * @link: The path of the symlink
Randy Dunlap55852632012-08-18 17:39:25 -0700680 * @nd: nameidata pathwalk data
Kees Cook800179c2012-07-25 17:29:07 -0700681 *
682 * In the case of the sysctl_protected_symlinks sysctl being enabled,
683 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
684 * in a sticky world-writable directory. This is to protect privileged
685 * processes from failing races against path names that may change out
686 * from under them by way of other users creating malicious symlinks.
687 * It will permit symlinks to be followed only when outside a sticky
688 * world-writable directory, or when the uid of the symlink and follower
689 * match, or when the directory owner matches the symlink's owner.
690 *
691 * Returns 0 if following the symlink is allowed, -ve on error.
692 */
693static inline int may_follow_link(struct path *link, struct nameidata *nd)
694{
695 const struct inode *inode;
696 const struct inode *parent;
697
698 if (!sysctl_protected_symlinks)
699 return 0;
700
701 /* Allowed if owner and follower match. */
702 inode = link->dentry->d_inode;
Eric W. Biederman81abe272012-08-03 09:38:08 -0700703 if (uid_eq(current_cred()->fsuid, inode->i_uid))
Kees Cook800179c2012-07-25 17:29:07 -0700704 return 0;
705
706 /* Allowed if parent directory not sticky and world-writable. */
707 parent = nd->path.dentry->d_inode;
708 if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
709 return 0;
710
711 /* Allowed if parent directory and link owner match. */
Eric W. Biederman81abe272012-08-03 09:38:08 -0700712 if (uid_eq(parent->i_uid, inode->i_uid))
Kees Cook800179c2012-07-25 17:29:07 -0700713 return 0;
714
Sasha Levinffd8d102012-10-04 19:56:40 -0400715 audit_log_link_denied("follow_link", link);
Kees Cook800179c2012-07-25 17:29:07 -0700716 path_put_conditional(link, nd);
717 path_put(&nd->path);
718 return -EACCES;
719}
720
721/**
722 * safe_hardlink_source - Check for safe hardlink conditions
723 * @inode: the source inode to hardlink from
724 *
725 * Return false if at least one of the following conditions:
726 * - inode is not a regular file
727 * - inode is setuid
728 * - inode is setgid and group-exec
729 * - access failure for read and write
730 *
731 * Otherwise returns true.
732 */
733static bool safe_hardlink_source(struct inode *inode)
734{
735 umode_t mode = inode->i_mode;
736
737 /* Special files should not get pinned to the filesystem. */
738 if (!S_ISREG(mode))
739 return false;
740
741 /* Setuid files should not get pinned to the filesystem. */
742 if (mode & S_ISUID)
743 return false;
744
745 /* Executable setgid files should not get pinned to the filesystem. */
746 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
747 return false;
748
749 /* Hardlinking to unreadable or unwritable sources is dangerous. */
750 if (inode_permission(inode, MAY_READ | MAY_WRITE))
751 return false;
752
753 return true;
754}
755
756/**
757 * may_linkat - Check permissions for creating a hardlink
758 * @link: the source to hardlink from
759 *
760 * Block hardlink when all of:
761 * - sysctl_protected_hardlinks enabled
762 * - fsuid does not match inode
763 * - hardlink source is unsafe (see safe_hardlink_source() above)
764 * - not CAP_FOWNER
765 *
766 * Returns 0 if successful, -ve on error.
767 */
768static int may_linkat(struct path *link)
769{
770 const struct cred *cred;
771 struct inode *inode;
772
773 if (!sysctl_protected_hardlinks)
774 return 0;
775
776 cred = current_cred();
777 inode = link->dentry->d_inode;
778
779 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
780 * otherwise, it must be a safe source.
781 */
Eric W. Biederman81abe272012-08-03 09:38:08 -0700782 if (uid_eq(cred->fsuid, inode->i_uid) || safe_hardlink_source(inode) ||
Kees Cook800179c2012-07-25 17:29:07 -0700783 capable(CAP_FOWNER))
784 return 0;
785
Kees Cooka51d9ea2012-07-25 17:29:08 -0700786 audit_log_link_denied("linkat", link);
Kees Cook800179c2012-07-25 17:29:07 -0700787 return -EPERM;
788}
789
Al Virodef4af32009-12-26 08:37:05 -0500790static __always_inline int
Al Viro574197e2011-03-14 22:20:34 -0400791follow_link(struct path *link, struct nameidata *nd, void **p)
Ian Kent051d3812006-03-27 01:14:53 -0800792{
Nick Piggin7b9337a2011-01-14 08:42:43 +0000793 struct dentry *dentry = link->dentry;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400794 int error;
795 char *s;
Ian Kent051d3812006-03-27 01:14:53 -0800796
Al Viro844a3912011-02-15 00:38:26 -0500797 BUG_ON(nd->flags & LOOKUP_RCU);
798
Al Viro0e794582011-03-16 02:45:02 -0400799 if (link->mnt == nd->path.mnt)
800 mntget(link->mnt);
801
Al Viro6d7b5aa2012-06-10 04:15:17 -0400802 error = -ELOOP;
803 if (unlikely(current->total_link_count >= 40))
804 goto out_put_nd_path;
805
Al Viro574197e2011-03-14 22:20:34 -0400806 cond_resched();
807 current->total_link_count++;
808
Al Viro68ac1232012-03-15 08:21:57 -0400809 touch_atime(link);
Ian Kent051d3812006-03-27 01:14:53 -0800810 nd_set_link(nd, NULL);
811
Al Viro36f3b4f2011-02-22 21:24:38 -0500812 error = security_inode_follow_link(link->dentry, nd);
Al Viro6d7b5aa2012-06-10 04:15:17 -0400813 if (error)
814 goto out_put_nd_path;
Al Viro36f3b4f2011-02-22 21:24:38 -0500815
Al Viro86acdca12009-12-22 23:45:11 -0500816 nd->last_type = LAST_BIND;
Al Virodef4af32009-12-26 08:37:05 -0500817 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
818 error = PTR_ERR(*p);
Al Viro6d7b5aa2012-06-10 04:15:17 -0400819 if (IS_ERR(*p))
Christoph Hellwig408ef012012-06-18 10:47:03 -0400820 goto out_put_nd_path;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400821
822 error = 0;
823 s = nd_get_link(nd);
824 if (s) {
Al Viro443ed252013-09-10 12:00:43 -0400825 if (unlikely(IS_ERR(s))) {
826 path_put(&nd->path);
827 put_link(nd, link, *p);
828 return PTR_ERR(s);
829 }
830 if (*s == '/') {
831 set_root(nd);
832 path_put(&nd->path);
833 nd->path = nd->root;
834 path_get(&nd->root);
835 nd->flags |= LOOKUP_JUMPED;
836 }
837 nd->inode = nd->path.dentry->d_inode;
838 error = link_path_walk(s, nd);
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400839 if (unlikely(error))
840 put_link(nd, link, *p);
Ian Kent051d3812006-03-27 01:14:53 -0800841 }
Al Viro6d7b5aa2012-06-10 04:15:17 -0400842
843 return error;
844
845out_put_nd_path:
Arnd Bergmann98f6ef62012-10-11 13:20:00 +0000846 *p = NULL;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400847 path_put(&nd->path);
Al Viro6d7b5aa2012-06-10 04:15:17 -0400848 path_put(link);
Ian Kent051d3812006-03-27 01:14:53 -0800849 return error;
850}
851
Nick Piggin31e6b012011-01-07 17:49:52 +1100852static int follow_up_rcu(struct path *path)
853{
Al Viro0714a532011-11-24 22:19:58 -0500854 struct mount *mnt = real_mount(path->mnt);
855 struct mount *parent;
Nick Piggin31e6b012011-01-07 17:49:52 +1100856 struct dentry *mountpoint;
857
Al Viro0714a532011-11-24 22:19:58 -0500858 parent = mnt->mnt_parent;
859 if (&parent->mnt == path->mnt)
Nick Piggin31e6b012011-01-07 17:49:52 +1100860 return 0;
Al Viroa73324d2011-11-24 22:25:07 -0500861 mountpoint = mnt->mnt_mountpoint;
Nick Piggin31e6b012011-01-07 17:49:52 +1100862 path->dentry = mountpoint;
Al Viro0714a532011-11-24 22:19:58 -0500863 path->mnt = &parent->mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +1100864 return 1;
865}
866
David Howellsf015f1262012-06-25 12:55:28 +0100867/*
868 * follow_up - Find the mountpoint of path's vfsmount
869 *
870 * Given a path, find the mountpoint of its source file system.
871 * Replace @path with the path of the mountpoint in the parent mount.
872 * Up is towards /.
873 *
874 * Return 1 if we went up a level and 0 if we were already at the
875 * root.
876 */
Al Virobab77eb2009-04-18 03:26:48 -0400877int follow_up(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
Al Viro0714a532011-11-24 22:19:58 -0500879 struct mount *mnt = real_mount(path->mnt);
880 struct mount *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 struct dentry *mountpoint;
Nick Piggin99b7db72010-08-18 04:37:39 +1000882
Al Viro48a066e2013-09-29 22:06:07 -0400883 read_seqlock_excl(&mount_lock);
Al Viro0714a532011-11-24 22:19:58 -0500884 parent = mnt->mnt_parent;
Al Viro3c0a6162012-07-18 17:32:50 +0400885 if (parent == mnt) {
Al Viro48a066e2013-09-29 22:06:07 -0400886 read_sequnlock_excl(&mount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return 0;
888 }
Al Viro0714a532011-11-24 22:19:58 -0500889 mntget(&parent->mnt);
Al Viroa73324d2011-11-24 22:25:07 -0500890 mountpoint = dget(mnt->mnt_mountpoint);
Al Viro48a066e2013-09-29 22:06:07 -0400891 read_sequnlock_excl(&mount_lock);
Al Virobab77eb2009-04-18 03:26:48 -0400892 dput(path->dentry);
893 path->dentry = mountpoint;
894 mntput(path->mnt);
Al Viro0714a532011-11-24 22:19:58 -0500895 path->mnt = &parent->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return 1;
897}
898
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100899/*
David Howells9875cf82011-01-14 18:45:21 +0000900 * Perform an automount
901 * - return -EISDIR to tell follow_managed() to stop and return the path we
902 * were called with.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 */
David Howells9875cf82011-01-14 18:45:21 +0000904static int follow_automount(struct path *path, unsigned flags,
905 bool *need_mntput)
Nick Piggin31e6b012011-01-07 17:49:52 +1100906{
David Howells9875cf82011-01-14 18:45:21 +0000907 struct vfsmount *mnt;
David Howellsea5b7782011-01-14 19:10:03 +0000908 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +1100909
David Howells9875cf82011-01-14 18:45:21 +0000910 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
911 return -EREMOTE;
Al Viro463ffb22005-06-06 13:36:05 -0700912
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200913 /* We don't want to mount if someone's just doing a stat -
914 * unless they're stat'ing a directory and appended a '/' to
915 * the name.
916 *
917 * We do, however, want to mount if someone wants to open or
918 * create a file of any type under the mountpoint, wants to
919 * traverse through the mountpoint or wants to open the
920 * mounted directory. Also, autofs may mark negative dentries
921 * as being automount points. These will need the attentions
922 * of the daemon to instantiate them before they can be used.
David Howells9875cf82011-01-14 18:45:21 +0000923 */
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200924 if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
Linus Torvaldsd94c1772011-09-26 17:44:55 -0700925 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200926 path->dentry->d_inode)
927 return -EISDIR;
928
David Howells9875cf82011-01-14 18:45:21 +0000929 current->total_link_count++;
930 if (current->total_link_count >= 40)
931 return -ELOOP;
932
933 mnt = path->dentry->d_op->d_automount(path);
934 if (IS_ERR(mnt)) {
935 /*
936 * The filesystem is allowed to return -EISDIR here to indicate
937 * it doesn't want to automount. For instance, autofs would do
938 * this so that its userspace daemon can mount on this dentry.
939 *
940 * However, we can only permit this if it's a terminal point in
941 * the path being looked up; if it wasn't then the remainder of
942 * the path is inaccessible and we should say so.
943 */
Al Viro49084c32011-06-25 21:59:52 -0400944 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
David Howells9875cf82011-01-14 18:45:21 +0000945 return -EREMOTE;
946 return PTR_ERR(mnt);
947 }
David Howellsea5b7782011-01-14 19:10:03 +0000948
David Howells9875cf82011-01-14 18:45:21 +0000949 if (!mnt) /* mount collision */
950 return 0;
951
Al Viro8aef1882011-06-16 15:10:06 +0100952 if (!*need_mntput) {
953 /* lock_mount() may release path->mnt on error */
954 mntget(path->mnt);
955 *need_mntput = true;
956 }
Al Viro19a167a2011-01-17 01:35:23 -0500957 err = finish_automount(mnt, path);
David Howellsea5b7782011-01-14 19:10:03 +0000958
David Howellsea5b7782011-01-14 19:10:03 +0000959 switch (err) {
960 case -EBUSY:
961 /* Someone else made a mount here whilst we were busy */
Al Viro19a167a2011-01-17 01:35:23 -0500962 return 0;
David Howellsea5b7782011-01-14 19:10:03 +0000963 case 0:
Al Viro8aef1882011-06-16 15:10:06 +0100964 path_put(path);
David Howellsea5b7782011-01-14 19:10:03 +0000965 path->mnt = mnt;
966 path->dentry = dget(mnt->mnt_root);
David Howellsea5b7782011-01-14 19:10:03 +0000967 return 0;
Al Viro19a167a2011-01-17 01:35:23 -0500968 default:
969 return err;
David Howellsea5b7782011-01-14 19:10:03 +0000970 }
Al Viro19a167a2011-01-17 01:35:23 -0500971
David Howells9875cf82011-01-14 18:45:21 +0000972}
973
974/*
975 * Handle a dentry that is managed in some way.
David Howellscc53ce52011-01-14 18:45:26 +0000976 * - Flagged for transit management (autofs)
David Howells9875cf82011-01-14 18:45:21 +0000977 * - Flagged as mountpoint
978 * - Flagged as automount point
979 *
980 * This may only be called in refwalk mode.
981 *
982 * Serialization is taken care of in namespace.c
983 */
984static int follow_managed(struct path *path, unsigned flags)
985{
Al Viro8aef1882011-06-16 15:10:06 +0100986 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
David Howells9875cf82011-01-14 18:45:21 +0000987 unsigned managed;
988 bool need_mntput = false;
Al Viro8aef1882011-06-16 15:10:06 +0100989 int ret = 0;
David Howells9875cf82011-01-14 18:45:21 +0000990
991 /* Given that we're not holding a lock here, we retain the value in a
992 * local variable for each dentry as we look at it so that we don't see
993 * the components of that value change under us */
994 while (managed = ACCESS_ONCE(path->dentry->d_flags),
995 managed &= DCACHE_MANAGED_DENTRY,
996 unlikely(managed != 0)) {
David Howellscc53ce52011-01-14 18:45:26 +0000997 /* Allow the filesystem to manage the transit without i_mutex
998 * being held. */
999 if (managed & DCACHE_MANAGE_TRANSIT) {
1000 BUG_ON(!path->dentry->d_op);
1001 BUG_ON(!path->dentry->d_op->d_manage);
Al Viro1aed3e42011-03-18 09:09:02 -04001002 ret = path->dentry->d_op->d_manage(path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +00001003 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001004 break;
David Howellscc53ce52011-01-14 18:45:26 +00001005 }
1006
David Howells9875cf82011-01-14 18:45:21 +00001007 /* Transit to a mounted filesystem. */
1008 if (managed & DCACHE_MOUNTED) {
1009 struct vfsmount *mounted = lookup_mnt(path);
1010 if (mounted) {
1011 dput(path->dentry);
1012 if (need_mntput)
1013 mntput(path->mnt);
1014 path->mnt = mounted;
1015 path->dentry = dget(mounted->mnt_root);
1016 need_mntput = true;
1017 continue;
1018 }
1019
1020 /* Something is mounted on this dentry in another
1021 * namespace and/or whatever was mounted there in this
Al Viro48a066e2013-09-29 22:06:07 -04001022 * namespace got unmounted before lookup_mnt() could
1023 * get it */
David Howells9875cf82011-01-14 18:45:21 +00001024 }
1025
1026 /* Handle an automount point */
1027 if (managed & DCACHE_NEED_AUTOMOUNT) {
1028 ret = follow_automount(path, flags, &need_mntput);
1029 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001030 break;
David Howells9875cf82011-01-14 18:45:21 +00001031 continue;
1032 }
1033
1034 /* We didn't change the current path point */
1035 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 }
Al Viro8aef1882011-06-16 15:10:06 +01001037
1038 if (need_mntput && path->mnt == mnt)
1039 mntput(path->mnt);
1040 if (ret == -EISDIR)
1041 ret = 0;
Al Viroa3fbbde2011-11-07 21:21:26 +00001042 return ret < 0 ? ret : need_mntput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043}
1044
David Howellscc53ce52011-01-14 18:45:26 +00001045int follow_down_one(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
1047 struct vfsmount *mounted;
1048
Al Viro1c755af2009-04-18 14:06:57 -04001049 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -04001051 dput(path->dentry);
1052 mntput(path->mnt);
1053 path->mnt = mounted;
1054 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 return 1;
1056 }
1057 return 0;
1058}
1059
Ian Kent62a73752011-03-25 01:51:02 +08001060static inline bool managed_dentry_might_block(struct dentry *dentry)
1061{
1062 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
1063 dentry->d_op->d_manage(dentry, true) < 0);
1064}
1065
David Howells9875cf82011-01-14 18:45:21 +00001066/*
Al Viro287548e2011-05-27 06:50:06 -04001067 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1068 * we meet a managed dentry that would need blocking.
David Howells9875cf82011-01-14 18:45:21 +00001069 */
1070static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
Al Viro287548e2011-05-27 06:50:06 -04001071 struct inode **inode)
David Howells9875cf82011-01-14 18:45:21 +00001072{
Ian Kent62a73752011-03-25 01:51:02 +08001073 for (;;) {
Al Viroc7105362011-11-24 18:22:03 -05001074 struct mount *mounted;
Ian Kent62a73752011-03-25 01:51:02 +08001075 /*
1076 * Don't forget we might have a non-mountpoint managed dentry
1077 * that wants to block transit.
1078 */
Al Viro287548e2011-05-27 06:50:06 -04001079 if (unlikely(managed_dentry_might_block(path->dentry)))
David Howellsab909112011-01-14 18:46:51 +00001080 return false;
Ian Kent62a73752011-03-25 01:51:02 +08001081
1082 if (!d_mountpoint(path->dentry))
1083 break;
1084
Al Viro474279d2013-10-01 16:11:26 -04001085 mounted = __lookup_mnt(path->mnt, path->dentry);
David Howells9875cf82011-01-14 18:45:21 +00001086 if (!mounted)
1087 break;
Al Viroc7105362011-11-24 18:22:03 -05001088 path->mnt = &mounted->mnt;
1089 path->dentry = mounted->mnt.mnt_root;
Al Viroa3fbbde2011-11-07 21:21:26 +00001090 nd->flags |= LOOKUP_JUMPED;
David Howells9875cf82011-01-14 18:45:21 +00001091 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
Linus Torvalds59430262011-07-18 15:43:29 -07001092 /*
1093 * Update the inode too. We don't need to re-check the
1094 * dentry sequence number here after this d_inode read,
1095 * because a mount-point is always pinned.
1096 */
1097 *inode = path->dentry->d_inode;
David Howells9875cf82011-01-14 18:45:21 +00001098 }
David Howells9875cf82011-01-14 18:45:21 +00001099 return true;
1100}
1101
Al Virodea39372011-05-27 06:53:39 -04001102static void follow_mount_rcu(struct nameidata *nd)
Al Viro287548e2011-05-27 06:50:06 -04001103{
Al Virodea39372011-05-27 06:53:39 -04001104 while (d_mountpoint(nd->path.dentry)) {
Al Viroc7105362011-11-24 18:22:03 -05001105 struct mount *mounted;
Al Viro474279d2013-10-01 16:11:26 -04001106 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry);
Al Viro287548e2011-05-27 06:50:06 -04001107 if (!mounted)
1108 break;
Al Viroc7105362011-11-24 18:22:03 -05001109 nd->path.mnt = &mounted->mnt;
1110 nd->path.dentry = mounted->mnt.mnt_root;
Al Virodea39372011-05-27 06:53:39 -04001111 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro287548e2011-05-27 06:50:06 -04001112 }
1113}
1114
Nick Piggin31e6b012011-01-07 17:49:52 +11001115static int follow_dotdot_rcu(struct nameidata *nd)
1116{
Nick Piggin31e6b012011-01-07 17:49:52 +11001117 set_root_rcu(nd);
1118
David Howells9875cf82011-01-14 18:45:21 +00001119 while (1) {
Nick Piggin31e6b012011-01-07 17:49:52 +11001120 if (nd->path.dentry == nd->root.dentry &&
1121 nd->path.mnt == nd->root.mnt) {
1122 break;
1123 }
1124 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1125 struct dentry *old = nd->path.dentry;
1126 struct dentry *parent = old->d_parent;
1127 unsigned seq;
1128
1129 seq = read_seqcount_begin(&parent->d_seq);
1130 if (read_seqcount_retry(&old->d_seq, nd->seq))
Al Viroef7562d2011-03-04 14:35:59 -05001131 goto failed;
Nick Piggin31e6b012011-01-07 17:49:52 +11001132 nd->path.dentry = parent;
1133 nd->seq = seq;
1134 break;
1135 }
1136 if (!follow_up_rcu(&nd->path))
1137 break;
1138 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Nick Piggin31e6b012011-01-07 17:49:52 +11001139 }
Al Virodea39372011-05-27 06:53:39 -04001140 follow_mount_rcu(nd);
1141 nd->inode = nd->path.dentry->d_inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001142 return 0;
Al Viroef7562d2011-03-04 14:35:59 -05001143
1144failed:
1145 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001146 if (!(nd->flags & LOOKUP_ROOT))
1147 nd->root.mnt = NULL;
Al Viro8b61e742013-11-08 12:45:01 -05001148 rcu_read_unlock();
Al Viroef7562d2011-03-04 14:35:59 -05001149 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001150}
1151
David Howells9875cf82011-01-14 18:45:21 +00001152/*
David Howellscc53ce52011-01-14 18:45:26 +00001153 * Follow down to the covering mount currently visible to userspace. At each
1154 * point, the filesystem owning that dentry may be queried as to whether the
1155 * caller is permitted to proceed or not.
David Howellscc53ce52011-01-14 18:45:26 +00001156 */
Al Viro7cc90cc2011-03-18 09:04:20 -04001157int follow_down(struct path *path)
David Howellscc53ce52011-01-14 18:45:26 +00001158{
1159 unsigned managed;
1160 int ret;
1161
1162 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1163 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1164 /* Allow the filesystem to manage the transit without i_mutex
1165 * being held.
1166 *
1167 * We indicate to the filesystem if someone is trying to mount
1168 * something here. This gives autofs the chance to deny anyone
1169 * other than its daemon the right to mount on its
1170 * superstructure.
1171 *
1172 * The filesystem may sleep at this point.
1173 */
1174 if (managed & DCACHE_MANAGE_TRANSIT) {
1175 BUG_ON(!path->dentry->d_op);
1176 BUG_ON(!path->dentry->d_op->d_manage);
David Howellsab909112011-01-14 18:46:51 +00001177 ret = path->dentry->d_op->d_manage(
Al Viro1aed3e42011-03-18 09:09:02 -04001178 path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +00001179 if (ret < 0)
1180 return ret == -EISDIR ? 0 : ret;
1181 }
1182
1183 /* Transit to a mounted filesystem. */
1184 if (managed & DCACHE_MOUNTED) {
1185 struct vfsmount *mounted = lookup_mnt(path);
1186 if (!mounted)
1187 break;
1188 dput(path->dentry);
1189 mntput(path->mnt);
1190 path->mnt = mounted;
1191 path->dentry = dget(mounted->mnt_root);
1192 continue;
1193 }
1194
1195 /* Don't handle automount points here */
1196 break;
1197 }
1198 return 0;
1199}
1200
1201/*
David Howells9875cf82011-01-14 18:45:21 +00001202 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1203 */
1204static void follow_mount(struct path *path)
1205{
1206 while (d_mountpoint(path->dentry)) {
1207 struct vfsmount *mounted = lookup_mnt(path);
1208 if (!mounted)
1209 break;
1210 dput(path->dentry);
1211 mntput(path->mnt);
1212 path->mnt = mounted;
1213 path->dentry = dget(mounted->mnt_root);
1214 }
1215}
1216
Nick Piggin31e6b012011-01-07 17:49:52 +11001217static void follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
Al Viro2a737872009-04-07 11:49:53 -04001219 set_root(nd);
Andreas Mohre518ddb2006-09-29 02:01:22 -07001220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 while(1) {
Jan Blunck4ac91372008-02-14 19:34:32 -08001222 struct dentry *old = nd->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Al Viro2a737872009-04-07 11:49:53 -04001224 if (nd->path.dentry == nd->root.dentry &&
1225 nd->path.mnt == nd->root.mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 break;
1227 }
Jan Blunck4ac91372008-02-14 19:34:32 -08001228 if (nd->path.dentry != nd->path.mnt->mnt_root) {
Al Viro3088dd72010-01-30 15:47:29 -05001229 /* rare case of legitimate dget_parent()... */
1230 nd->path.dentry = dget_parent(nd->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 dput(old);
1232 break;
1233 }
Al Viro3088dd72010-01-30 15:47:29 -05001234 if (!follow_up(&nd->path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 }
Al Viro79ed0222009-04-18 13:59:41 -04001237 follow_mount(&nd->path);
Nick Piggin31e6b012011-01-07 17:49:52 +11001238 nd->inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239}
1240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241/*
Miklos Szeredibad61182012-03-26 12:54:24 +02001242 * This looks up the name in dcache, possibly revalidates the old dentry and
1243 * allocates a new one if not found or not valid. In the need_lookup argument
1244 * returns whether i_op->lookup is necessary.
1245 *
1246 * dir->d_inode->i_mutex must be held
Nick Pigginbaa03892010-08-18 04:37:31 +10001247 */
Miklos Szeredibad61182012-03-26 12:54:24 +02001248static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
Al Viro201f9562012-06-22 12:42:10 +04001249 unsigned int flags, bool *need_lookup)
Nick Pigginbaa03892010-08-18 04:37:31 +10001250{
Nick Pigginbaa03892010-08-18 04:37:31 +10001251 struct dentry *dentry;
Miklos Szeredibad61182012-03-26 12:54:24 +02001252 int error;
Nick Pigginbaa03892010-08-18 04:37:31 +10001253
Miklos Szeredibad61182012-03-26 12:54:24 +02001254 *need_lookup = false;
1255 dentry = d_lookup(dir, name);
1256 if (dentry) {
Jeff Layton39e3c952012-11-28 11:30:53 -05001257 if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
Al Viro201f9562012-06-22 12:42:10 +04001258 error = d_revalidate(dentry, flags);
Miklos Szeredibad61182012-03-26 12:54:24 +02001259 if (unlikely(error <= 0)) {
1260 if (error < 0) {
1261 dput(dentry);
1262 return ERR_PTR(error);
1263 } else if (!d_invalidate(dentry)) {
1264 dput(dentry);
1265 dentry = NULL;
1266 }
1267 }
1268 }
1269 }
Nick Pigginbaa03892010-08-18 04:37:31 +10001270
Miklos Szeredibad61182012-03-26 12:54:24 +02001271 if (!dentry) {
1272 dentry = d_alloc(dir, name);
1273 if (unlikely(!dentry))
1274 return ERR_PTR(-ENOMEM);
Nick Pigginbaa03892010-08-18 04:37:31 +10001275
Miklos Szeredibad61182012-03-26 12:54:24 +02001276 *need_lookup = true;
Nick Pigginbaa03892010-08-18 04:37:31 +10001277 }
1278 return dentry;
1279}
1280
1281/*
J. Bruce Fields13a2c3b2013-10-23 16:09:16 -04001282 * Call i_op->lookup on the dentry. The dentry must be negative and
1283 * unhashed.
Miklos Szeredibad61182012-03-26 12:54:24 +02001284 *
1285 * dir->d_inode->i_mutex must be held
Josef Bacik44396f42011-05-31 11:58:49 -04001286 */
Miklos Szeredibad61182012-03-26 12:54:24 +02001287static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
Al Viro72bd8662012-06-10 17:17:17 -04001288 unsigned int flags)
Josef Bacik44396f42011-05-31 11:58:49 -04001289{
Josef Bacik44396f42011-05-31 11:58:49 -04001290 struct dentry *old;
1291
1292 /* Don't create child dentry for a dead directory. */
Miklos Szeredibad61182012-03-26 12:54:24 +02001293 if (unlikely(IS_DEADDIR(dir))) {
Miklos Szeredie188dc02012-02-03 14:25:18 +01001294 dput(dentry);
Josef Bacik44396f42011-05-31 11:58:49 -04001295 return ERR_PTR(-ENOENT);
Miklos Szeredie188dc02012-02-03 14:25:18 +01001296 }
Josef Bacik44396f42011-05-31 11:58:49 -04001297
Al Viro72bd8662012-06-10 17:17:17 -04001298 old = dir->i_op->lookup(dir, dentry, flags);
Josef Bacik44396f42011-05-31 11:58:49 -04001299 if (unlikely(old)) {
1300 dput(dentry);
1301 dentry = old;
1302 }
1303 return dentry;
1304}
1305
Al Viroa3255542012-03-30 14:41:51 -04001306static struct dentry *__lookup_hash(struct qstr *name,
Al Viro72bd8662012-06-10 17:17:17 -04001307 struct dentry *base, unsigned int flags)
Al Viroa3255542012-03-30 14:41:51 -04001308{
Miklos Szeredibad61182012-03-26 12:54:24 +02001309 bool need_lookup;
Al Viroa3255542012-03-30 14:41:51 -04001310 struct dentry *dentry;
1311
Al Viro72bd8662012-06-10 17:17:17 -04001312 dentry = lookup_dcache(name, base, flags, &need_lookup);
Miklos Szeredibad61182012-03-26 12:54:24 +02001313 if (!need_lookup)
1314 return dentry;
Al Viroa3255542012-03-30 14:41:51 -04001315
Al Viro72bd8662012-06-10 17:17:17 -04001316 return lookup_real(base->d_inode, dentry, flags);
Al Viroa3255542012-03-30 14:41:51 -04001317}
1318
Josef Bacik44396f42011-05-31 11:58:49 -04001319/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 * It's more convoluted than I'd like it to be, but... it's still fairly
1321 * small and for now I'd prefer to have fast path as straight as possible.
1322 * It _is_ time-critical.
1323 */
Al Viroe97cdc82013-01-24 18:16:00 -05001324static int lookup_fast(struct nameidata *nd,
Miklos Szeredi697f5142012-05-21 17:30:05 +02001325 struct path *path, struct inode **inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
Jan Blunck4ac91372008-02-14 19:34:32 -08001327 struct vfsmount *mnt = nd->path.mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +11001328 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro5a18fff2011-03-11 04:44:53 -05001329 int need_reval = 1;
1330 int status = 1;
David Howells9875cf82011-01-14 18:45:21 +00001331 int err;
1332
Al Viro3cac2602009-08-13 18:27:43 +04001333 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001334 * Rename seqlock is not required here because in the off chance
1335 * of a false negative due to a concurrent rename, we're going to
1336 * do the non-racy lookup, below.
1337 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001338 if (nd->flags & LOOKUP_RCU) {
1339 unsigned seq;
Linus Torvaldsda53be12013-05-21 15:22:44 -07001340 dentry = __d_lookup_rcu(parent, &nd->last, &seq);
Al Viro5a18fff2011-03-11 04:44:53 -05001341 if (!dentry)
1342 goto unlazy;
1343
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001344 /*
1345 * This sequence count validates that the inode matches
1346 * the dentry name information from lookup.
1347 */
1348 *inode = dentry->d_inode;
1349 if (read_seqcount_retry(&dentry->d_seq, seq))
1350 return -ECHILD;
1351
1352 /*
1353 * This sequence count validates that the parent had no
1354 * changes while we did the lookup of the dentry above.
1355 *
1356 * The memory barrier in read_seqcount_begin of child is
1357 * enough, we can use __read_seqcount_retry here.
1358 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001359 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1360 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001361 nd->seq = seq;
Al Viro5a18fff2011-03-11 04:44:53 -05001362
Al Viro24643082011-02-15 01:26:22 -05001363 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
Al Viro4ce16ef32012-06-10 16:10:59 -04001364 status = d_revalidate(dentry, nd->flags);
Al Viro5a18fff2011-03-11 04:44:53 -05001365 if (unlikely(status <= 0)) {
1366 if (status != -ECHILD)
1367 need_reval = 0;
1368 goto unlazy;
1369 }
Al Viro24643082011-02-15 01:26:22 -05001370 }
Nick Piggin31e6b012011-01-07 17:49:52 +11001371 path->mnt = mnt;
1372 path->dentry = dentry;
Al Virod6e9bd22011-05-27 07:03:15 -04001373 if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1374 goto unlazy;
1375 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1376 goto unlazy;
1377 return 0;
Al Viro5a18fff2011-03-11 04:44:53 -05001378unlazy:
Al Viro19660af2011-03-25 10:32:48 -04001379 if (unlazy_walk(nd, dentry))
1380 return -ECHILD;
Al Viro5a18fff2011-03-11 04:44:53 -05001381 } else {
Al Viroe97cdc82013-01-24 18:16:00 -05001382 dentry = __d_lookup(parent, &nd->last);
Nick Piggin31e6b012011-01-07 17:49:52 +11001383 }
Al Viro5a18fff2011-03-11 04:44:53 -05001384
Al Viro81e6f522012-03-30 14:48:04 -04001385 if (unlikely(!dentry))
1386 goto need_lookup;
Al Viro5a18fff2011-03-11 04:44:53 -05001387
Al Viro5a18fff2011-03-11 04:44:53 -05001388 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
Al Viro4ce16ef32012-06-10 16:10:59 -04001389 status = d_revalidate(dentry, nd->flags);
Al Viro5a18fff2011-03-11 04:44:53 -05001390 if (unlikely(status <= 0)) {
1391 if (status < 0) {
1392 dput(dentry);
1393 return status;
1394 }
1395 if (!d_invalidate(dentry)) {
1396 dput(dentry);
Al Viro81e6f522012-03-30 14:48:04 -04001397 goto need_lookup;
Al Viro5a18fff2011-03-11 04:44:53 -05001398 }
1399 }
Miklos Szeredi697f5142012-05-21 17:30:05 +02001400
David Howells9875cf82011-01-14 18:45:21 +00001401 path->mnt = mnt;
1402 path->dentry = dentry;
1403 err = follow_managed(path, nd->flags);
Ian Kent89312212011-01-18 12:06:10 +08001404 if (unlikely(err < 0)) {
1405 path_put_conditional(path, nd);
David Howells9875cf82011-01-14 18:45:21 +00001406 return err;
Ian Kent89312212011-01-18 12:06:10 +08001407 }
Al Viroa3fbbde2011-11-07 21:21:26 +00001408 if (err)
1409 nd->flags |= LOOKUP_JUMPED;
David Howells9875cf82011-01-14 18:45:21 +00001410 *inode = path->dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 return 0;
Al Viro81e6f522012-03-30 14:48:04 -04001412
1413need_lookup:
Miklos Szeredi697f5142012-05-21 17:30:05 +02001414 return 1;
1415}
1416
1417/* Fast lookup failed, do it the slow way */
Al Virocc2a5272013-01-24 18:19:49 -05001418static int lookup_slow(struct nameidata *nd, struct path *path)
Miklos Szeredi697f5142012-05-21 17:30:05 +02001419{
1420 struct dentry *dentry, *parent;
1421 int err;
1422
1423 parent = nd->path.dentry;
Al Viro81e6f522012-03-30 14:48:04 -04001424 BUG_ON(nd->inode != parent->d_inode);
1425
1426 mutex_lock(&parent->d_inode->i_mutex);
Al Virocc2a5272013-01-24 18:19:49 -05001427 dentry = __lookup_hash(&nd->last, parent, nd->flags);
Al Viro81e6f522012-03-30 14:48:04 -04001428 mutex_unlock(&parent->d_inode->i_mutex);
1429 if (IS_ERR(dentry))
1430 return PTR_ERR(dentry);
Miklos Szeredi697f5142012-05-21 17:30:05 +02001431 path->mnt = nd->path.mnt;
1432 path->dentry = dentry;
1433 err = follow_managed(path, nd->flags);
1434 if (unlikely(err < 0)) {
1435 path_put_conditional(path, nd);
1436 return err;
1437 }
1438 if (err)
1439 nd->flags |= LOOKUP_JUMPED;
1440 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441}
1442
Al Viro52094c82011-02-21 21:34:47 -05001443static inline int may_lookup(struct nameidata *nd)
1444{
1445 if (nd->flags & LOOKUP_RCU) {
Al Viro4ad5abb2011-06-20 19:57:03 -04001446 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
Al Viro52094c82011-02-21 21:34:47 -05001447 if (err != -ECHILD)
1448 return err;
Al Viro19660af2011-03-25 10:32:48 -04001449 if (unlazy_walk(nd, NULL))
Al Viro52094c82011-02-21 21:34:47 -05001450 return -ECHILD;
1451 }
Al Viro4ad5abb2011-06-20 19:57:03 -04001452 return inode_permission(nd->inode, MAY_EXEC);
Al Viro52094c82011-02-21 21:34:47 -05001453}
1454
Al Viro9856fa12011-03-04 14:22:06 -05001455static inline int handle_dots(struct nameidata *nd, int type)
1456{
1457 if (type == LAST_DOTDOT) {
1458 if (nd->flags & LOOKUP_RCU) {
1459 if (follow_dotdot_rcu(nd))
1460 return -ECHILD;
1461 } else
1462 follow_dotdot(nd);
1463 }
1464 return 0;
1465}
1466
Al Viro951361f2011-03-04 14:44:37 -05001467static void terminate_walk(struct nameidata *nd)
1468{
1469 if (!(nd->flags & LOOKUP_RCU)) {
1470 path_put(&nd->path);
1471 } else {
1472 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001473 if (!(nd->flags & LOOKUP_ROOT))
1474 nd->root.mnt = NULL;
Al Viro8b61e742013-11-08 12:45:01 -05001475 rcu_read_unlock();
Al Viro951361f2011-03-04 14:44:37 -05001476 }
1477}
1478
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001479/*
1480 * Do we need to follow links? We _really_ want to be able
1481 * to do this check without having to look at inode->i_op,
1482 * so we keep a cache of "no, this doesn't need follow_link"
1483 * for the common case.
1484 */
David Howellsb18825a2013-09-12 19:22:53 +01001485static inline int should_follow_link(struct dentry *dentry, int follow)
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001486{
David Howellsb18825a2013-09-12 19:22:53 +01001487 return unlikely(d_is_symlink(dentry)) ? follow : 0;
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001488}
1489
Al Viroce57dfc2011-03-13 19:58:58 -04001490static inline int walk_component(struct nameidata *nd, struct path *path,
Al Viro21b9b072013-01-24 18:10:25 -05001491 int follow)
Al Viroce57dfc2011-03-13 19:58:58 -04001492{
1493 struct inode *inode;
1494 int err;
1495 /*
1496 * "." and ".." are special - ".." especially so because it has
1497 * to be able to know about the current root directory and
1498 * parent relationships.
1499 */
Al Viro21b9b072013-01-24 18:10:25 -05001500 if (unlikely(nd->last_type != LAST_NORM))
1501 return handle_dots(nd, nd->last_type);
Al Viroe97cdc82013-01-24 18:16:00 -05001502 err = lookup_fast(nd, path, &inode);
Al Viroce57dfc2011-03-13 19:58:58 -04001503 if (unlikely(err)) {
Miklos Szeredi697f5142012-05-21 17:30:05 +02001504 if (err < 0)
1505 goto out_err;
1506
Al Virocc2a5272013-01-24 18:19:49 -05001507 err = lookup_slow(nd, path);
Miklos Szeredi697f5142012-05-21 17:30:05 +02001508 if (err < 0)
1509 goto out_err;
1510
1511 inode = path->dentry->d_inode;
Al Viroce57dfc2011-03-13 19:58:58 -04001512 }
Miklos Szeredi697f5142012-05-21 17:30:05 +02001513 err = -ENOENT;
1514 if (!inode)
1515 goto out_path_put;
1516
David Howellsb18825a2013-09-12 19:22:53 +01001517 if (should_follow_link(path->dentry, follow)) {
Al Viro19660af2011-03-25 10:32:48 -04001518 if (nd->flags & LOOKUP_RCU) {
1519 if (unlikely(unlazy_walk(nd, path->dentry))) {
Miklos Szeredi697f5142012-05-21 17:30:05 +02001520 err = -ECHILD;
1521 goto out_err;
Al Viro19660af2011-03-25 10:32:48 -04001522 }
1523 }
Al Viroce57dfc2011-03-13 19:58:58 -04001524 BUG_ON(inode != path->dentry->d_inode);
1525 return 1;
1526 }
1527 path_to_nameidata(path, nd);
1528 nd->inode = inode;
1529 return 0;
Miklos Szeredi697f5142012-05-21 17:30:05 +02001530
1531out_path_put:
1532 path_to_nameidata(path, nd);
1533out_err:
1534 terminate_walk(nd);
1535 return err;
Al Viroce57dfc2011-03-13 19:58:58 -04001536}
1537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538/*
Al Virob3563792011-03-14 21:54:55 -04001539 * This limits recursive symlink follows to 8, while
1540 * limiting consecutive symlinks to 40.
1541 *
1542 * Without that kind of total limit, nasty chains of consecutive
1543 * symlinks can cause almost arbitrarily long lookups.
1544 */
1545static inline int nested_symlink(struct path *path, struct nameidata *nd)
1546{
1547 int res;
1548
Al Virob3563792011-03-14 21:54:55 -04001549 if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1550 path_put_conditional(path, nd);
1551 path_put(&nd->path);
1552 return -ELOOP;
1553 }
Erez Zadok1a4022f2011-05-21 01:19:59 -04001554 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
Al Virob3563792011-03-14 21:54:55 -04001555
1556 nd->depth++;
1557 current->link_count++;
1558
1559 do {
1560 struct path link = *path;
1561 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04001562
1563 res = follow_link(&link, nd, &cookie);
Al Viro6d7b5aa2012-06-10 04:15:17 -04001564 if (res)
1565 break;
Al Viro21b9b072013-01-24 18:10:25 -05001566 res = walk_component(nd, path, LOOKUP_FOLLOW);
Al Viro574197e2011-03-14 22:20:34 -04001567 put_link(nd, &link, cookie);
Al Virob3563792011-03-14 21:54:55 -04001568 } while (res > 0);
1569
1570 current->link_count--;
1571 nd->depth--;
1572 return res;
1573}
1574
1575/*
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001576 * We can do the critical dentry name comparison and hashing
1577 * operations one word at a time, but we are limited to:
1578 *
1579 * - Architectures with fast unaligned word accesses. We could
1580 * do a "get_unaligned()" if this helps and is sufficiently
1581 * fast.
1582 *
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001583 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1584 * do not trap on the (extremely unlikely) case of a page
1585 * crossing operation.
1586 *
1587 * - Furthermore, we need an efficient 64-bit compile for the
1588 * 64-bit case in order to generate the "number of bytes in
1589 * the final mask". Again, that could be replaced with a
1590 * efficient population count instruction or similar.
1591 */
1592#ifdef CONFIG_DCACHE_WORD_ACCESS
1593
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001594#include <asm/word-at-a-time.h>
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001595
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001596#ifdef CONFIG_64BIT
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001597
1598static inline unsigned int fold_hash(unsigned long hash)
1599{
1600 hash += hash >> (8*sizeof(int));
1601 return hash;
1602}
1603
1604#else /* 32-bit case */
1605
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001606#define fold_hash(x) (x)
1607
1608#endif
1609
1610unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1611{
1612 unsigned long a, mask;
1613 unsigned long hash = 0;
1614
1615 for (;;) {
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001616 a = load_unaligned_zeropad(name);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001617 if (len < sizeof(unsigned long))
1618 break;
1619 hash += a;
Al Virof132c5b2012-03-22 21:59:52 +00001620 hash *= 9;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001621 name += sizeof(unsigned long);
1622 len -= sizeof(unsigned long);
1623 if (!len)
1624 goto done;
1625 }
Will Deacona5c21dc2013-12-12 17:40:21 +00001626 mask = bytemask_from_count(len);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001627 hash += mask & a;
1628done:
1629 return fold_hash(hash);
1630}
1631EXPORT_SYMBOL(full_name_hash);
1632
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001633/*
1634 * Calculate the length and hash of the path component, and
1635 * return the length of the component;
1636 */
1637static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1638{
Linus Torvalds36126f82012-05-26 10:43:17 -07001639 unsigned long a, b, adata, bdata, mask, hash, len;
1640 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001641
1642 hash = a = 0;
1643 len = -sizeof(unsigned long);
1644 do {
1645 hash = (hash + a) * 9;
1646 len += sizeof(unsigned long);
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001647 a = load_unaligned_zeropad(name+len);
Linus Torvalds36126f82012-05-26 10:43:17 -07001648 b = a ^ REPEAT_BYTE('/');
1649 } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001650
Linus Torvalds36126f82012-05-26 10:43:17 -07001651 adata = prep_zero_mask(a, adata, &constants);
1652 bdata = prep_zero_mask(b, bdata, &constants);
1653
1654 mask = create_zero_mask(adata | bdata);
1655
1656 hash += a & zero_bytemask(mask);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001657 *hashp = fold_hash(hash);
1658
Linus Torvalds36126f82012-05-26 10:43:17 -07001659 return len + find_zero(mask);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001660}
1661
1662#else
1663
Linus Torvalds0145acc2012-03-02 14:32:59 -08001664unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1665{
1666 unsigned long hash = init_name_hash();
1667 while (len--)
1668 hash = partial_name_hash(*name++, hash);
1669 return end_name_hash(hash);
1670}
Linus Torvaldsae942ae2012-03-02 19:40:57 -08001671EXPORT_SYMBOL(full_name_hash);
Linus Torvalds0145acc2012-03-02 14:32:59 -08001672
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001673/*
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001674 * We know there's a real path component here of at least
1675 * one character.
1676 */
1677static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1678{
1679 unsigned long hash = init_name_hash();
1680 unsigned long len = 0, c;
1681
1682 c = (unsigned char)*name;
1683 do {
1684 len++;
1685 hash = partial_name_hash(c, hash);
1686 c = (unsigned char)name[len];
1687 } while (c && c != '/');
1688 *hashp = end_name_hash(hash);
1689 return len;
1690}
1691
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001692#endif
1693
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001694/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01001696 * This is the basic name resolution function, turning a pathname into
1697 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01001699 * Returns 0 and nd will have valid dentry and mnt on success.
1700 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 */
Al Viro6de88d72009-08-09 01:41:57 +04001702static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703{
1704 struct path next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
1707 while (*name=='/')
1708 name++;
1709 if (!*name)
Al Viro086e1832011-02-22 20:56:27 -05001710 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 /* At this point we know we have a real path component. */
1713 for(;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 struct qstr this;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001715 long len;
Al Virofe479a52011-02-22 15:10:03 -05001716 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Al Viro52094c82011-02-21 21:34:47 -05001718 err = may_lookup(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 if (err)
1720 break;
1721
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001722 len = hash_name(name, &this.hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 this.name = name;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001724 this.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
Al Virofe479a52011-02-22 15:10:03 -05001726 type = LAST_NORM;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001727 if (name[0] == '.') switch (len) {
Al Virofe479a52011-02-22 15:10:03 -05001728 case 2:
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001729 if (name[1] == '.') {
Al Virofe479a52011-02-22 15:10:03 -05001730 type = LAST_DOTDOT;
Al Viro16c2cd72011-02-22 15:50:10 -05001731 nd->flags |= LOOKUP_JUMPED;
1732 }
Al Virofe479a52011-02-22 15:10:03 -05001733 break;
1734 case 1:
1735 type = LAST_DOT;
1736 }
Al Viro5a202bc2011-03-08 14:17:44 -05001737 if (likely(type == LAST_NORM)) {
1738 struct dentry *parent = nd->path.dentry;
Al Viro16c2cd72011-02-22 15:50:10 -05001739 nd->flags &= ~LOOKUP_JUMPED;
Al Viro5a202bc2011-03-08 14:17:44 -05001740 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
Linus Torvaldsda53be12013-05-21 15:22:44 -07001741 err = parent->d_op->d_hash(parent, &this);
Al Viro5a202bc2011-03-08 14:17:44 -05001742 if (err < 0)
1743 break;
1744 }
1745 }
Al Virofe479a52011-02-22 15:10:03 -05001746
Al Viro5f4a6a62013-01-24 18:04:22 -05001747 nd->last = this;
1748 nd->last_type = type;
1749
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001750 if (!name[len])
Al Viro5f4a6a62013-01-24 18:04:22 -05001751 return 0;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001752 /*
1753 * If it wasn't NUL, we know it was '/'. Skip that
1754 * slash, and continue until no more slashes.
1755 */
1756 do {
1757 len++;
1758 } while (unlikely(name[len] == '/'));
1759 if (!name[len])
Al Viro5f4a6a62013-01-24 18:04:22 -05001760 return 0;
1761
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001762 name += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
Al Viro21b9b072013-01-24 18:10:25 -05001764 err = walk_component(nd, &next, LOOKUP_FOLLOW);
Al Viroce57dfc2011-03-13 19:58:58 -04001765 if (err < 0)
1766 return err;
Al Virofe479a52011-02-22 15:10:03 -05001767
Al Viroce57dfc2011-03-13 19:58:58 -04001768 if (err) {
Al Virob3563792011-03-14 21:54:55 -04001769 err = nested_symlink(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 if (err)
Al Viroa7472ba2011-03-04 14:39:30 -05001771 return err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001772 }
David Howellsb18825a2013-09-12 19:22:53 +01001773 if (!d_is_directory(nd->path.dentry)) {
Al Viro5f4a6a62013-01-24 18:04:22 -05001774 err = -ENOTDIR;
1775 break;
1776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 }
Al Viro951361f2011-03-04 14:44:37 -05001778 terminate_walk(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 return err;
1780}
1781
Al Viro70e9b352011-03-05 21:12:22 -05001782static int path_init(int dfd, const char *name, unsigned int flags,
1783 struct nameidata *nd, struct file **fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784{
Prasanna Medaea3834d2005-04-29 16:00:17 +01001785 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
1787 nd->last_type = LAST_ROOT; /* if there are only slashes... */
Al Viro16c2cd72011-02-22 15:50:10 -05001788 nd->flags = flags | LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 nd->depth = 0;
Al Viro5b6ca022011-03-09 23:04:47 -05001790 if (flags & LOOKUP_ROOT) {
David Howellsb18825a2013-09-12 19:22:53 +01001791 struct dentry *root = nd->root.dentry;
1792 struct inode *inode = root->d_inode;
Al Viro73d049a2011-03-11 12:08:24 -05001793 if (*name) {
David Howellsb18825a2013-09-12 19:22:53 +01001794 if (!d_is_directory(root))
Al Viro73d049a2011-03-11 12:08:24 -05001795 return -ENOTDIR;
1796 retval = inode_permission(inode, MAY_EXEC);
1797 if (retval)
1798 return retval;
1799 }
Al Viro5b6ca022011-03-09 23:04:47 -05001800 nd->path = nd->root;
1801 nd->inode = inode;
1802 if (flags & LOOKUP_RCU) {
Al Viro8b61e742013-11-08 12:45:01 -05001803 rcu_read_lock();
Al Viro5b6ca022011-03-09 23:04:47 -05001804 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro48a066e2013-09-29 22:06:07 -04001805 nd->m_seq = read_seqbegin(&mount_lock);
Al Viro5b6ca022011-03-09 23:04:47 -05001806 } else {
1807 path_get(&nd->path);
1808 }
1809 return 0;
1810 }
1811
Al Viro2a737872009-04-07 11:49:53 -04001812 nd->root.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
Al Viro48a066e2013-09-29 22:06:07 -04001814 nd->m_seq = read_seqbegin(&mount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 if (*name=='/') {
Al Viroe41f7d42011-02-22 14:02:58 -05001816 if (flags & LOOKUP_RCU) {
Al Viro8b61e742013-11-08 12:45:01 -05001817 rcu_read_lock();
Al Viroe41f7d42011-02-22 14:02:58 -05001818 set_root_rcu(nd);
1819 } else {
1820 set_root(nd);
1821 path_get(&nd->root);
1822 }
Al Viro2a737872009-04-07 11:49:53 -04001823 nd->path = nd->root;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001824 } else if (dfd == AT_FDCWD) {
Al Viroe41f7d42011-02-22 14:02:58 -05001825 if (flags & LOOKUP_RCU) {
1826 struct fs_struct *fs = current->fs;
1827 unsigned seq;
1828
Al Viro8b61e742013-11-08 12:45:01 -05001829 rcu_read_lock();
Al Viroe41f7d42011-02-22 14:02:58 -05001830
1831 do {
1832 seq = read_seqcount_begin(&fs->seq);
1833 nd->path = fs->pwd;
1834 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1835 } while (read_seqcount_retry(&fs->seq, seq));
1836 } else {
1837 get_fs_pwd(current->fs, &nd->path);
1838 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001839 } else {
Jeff Layton582aa642012-12-11 08:56:16 -05001840 /* Caller must check execute permissions on the starting path component */
Al Viro2903ff02012-08-28 12:52:22 -04001841 struct fd f = fdget_raw(dfd);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001842 struct dentry *dentry;
1843
Al Viro2903ff02012-08-28 12:52:22 -04001844 if (!f.file)
1845 return -EBADF;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001846
Al Viro2903ff02012-08-28 12:52:22 -04001847 dentry = f.file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001848
Al Virof52e0c12011-03-14 18:56:51 -04001849 if (*name) {
David Howellsb18825a2013-09-12 19:22:53 +01001850 if (!d_is_directory(dentry)) {
Al Viro2903ff02012-08-28 12:52:22 -04001851 fdput(f);
1852 return -ENOTDIR;
1853 }
Al Virof52e0c12011-03-14 18:56:51 -04001854 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001855
Al Viro2903ff02012-08-28 12:52:22 -04001856 nd->path = f.file->f_path;
Al Viroe41f7d42011-02-22 14:02:58 -05001857 if (flags & LOOKUP_RCU) {
Al Viro2903ff02012-08-28 12:52:22 -04001858 if (f.need_put)
1859 *fp = f.file;
Al Viroe41f7d42011-02-22 14:02:58 -05001860 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro8b61e742013-11-08 12:45:01 -05001861 rcu_read_lock();
Al Viroe41f7d42011-02-22 14:02:58 -05001862 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001863 path_get(&nd->path);
1864 fdput(f);
Al Viroe41f7d42011-02-22 14:02:58 -05001865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 }
Al Viroe41f7d42011-02-22 14:02:58 -05001867
Nick Piggin31e6b012011-01-07 17:49:52 +11001868 nd->inode = nd->path.dentry->d_inode;
Al Viro9b4a9b12009-04-07 11:44:16 -04001869 return 0;
Al Viro9b4a9b12009-04-07 11:44:16 -04001870}
1871
Al Virobd92d7f2011-03-14 19:54:59 -04001872static inline int lookup_last(struct nameidata *nd, struct path *path)
1873{
1874 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1875 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1876
1877 nd->flags &= ~LOOKUP_PARENT;
Al Viro21b9b072013-01-24 18:10:25 -05001878 return walk_component(nd, path, nd->flags & LOOKUP_FOLLOW);
Al Virobd92d7f2011-03-14 19:54:59 -04001879}
1880
Al Viro9b4a9b12009-04-07 11:44:16 -04001881/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroee0827c2011-02-21 23:38:09 -05001882static int path_lookupat(int dfd, const char *name,
Al Viro9b4a9b12009-04-07 11:44:16 -04001883 unsigned int flags, struct nameidata *nd)
1884{
Al Viro70e9b352011-03-05 21:12:22 -05001885 struct file *base = NULL;
Al Virobd92d7f2011-03-14 19:54:59 -04001886 struct path path;
1887 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001888
1889 /*
1890 * Path walking is largely split up into 2 different synchronisation
1891 * schemes, rcu-walk and ref-walk (explained in
1892 * Documentation/filesystems/path-lookup.txt). These share much of the
1893 * path walk code, but some things particularly setup, cleanup, and
1894 * following mounts are sufficiently divergent that functions are
1895 * duplicated. Typically there is a function foo(), and its RCU
1896 * analogue, foo_rcu().
1897 *
1898 * -ECHILD is the error number of choice (just to avoid clashes) that
1899 * is returned if some aspect of an rcu-walk fails. Such an error must
1900 * be handled by restarting a traditional ref-walk (which will always
1901 * be able to complete).
1902 */
Al Virobd92d7f2011-03-14 19:54:59 -04001903 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
Al Viroee0827c2011-02-21 23:38:09 -05001904
Al Virobd92d7f2011-03-14 19:54:59 -04001905 if (unlikely(err))
1906 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001907
1908 current->total_link_count = 0;
Al Virobd92d7f2011-03-14 19:54:59 -04001909 err = link_path_walk(name, nd);
1910
1911 if (!err && !(flags & LOOKUP_PARENT)) {
Al Virobd92d7f2011-03-14 19:54:59 -04001912 err = lookup_last(nd, &path);
1913 while (err > 0) {
1914 void *cookie;
1915 struct path link = path;
Kees Cook800179c2012-07-25 17:29:07 -07001916 err = may_follow_link(&link, nd);
1917 if (unlikely(err))
1918 break;
Al Virobd92d7f2011-03-14 19:54:59 -04001919 nd->flags |= LOOKUP_PARENT;
Al Viro574197e2011-03-14 22:20:34 -04001920 err = follow_link(&link, nd, &cookie);
Al Viro6d7b5aa2012-06-10 04:15:17 -04001921 if (err)
1922 break;
1923 err = lookup_last(nd, &path);
Al Viro574197e2011-03-14 22:20:34 -04001924 put_link(nd, &link, cookie);
Al Virobd92d7f2011-03-14 19:54:59 -04001925 }
1926 }
Al Viroee0827c2011-02-21 23:38:09 -05001927
Al Viro9f1fafe2011-03-25 11:00:12 -04001928 if (!err)
1929 err = complete_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04001930
1931 if (!err && nd->flags & LOOKUP_DIRECTORY) {
David Howellsb18825a2013-09-12 19:22:53 +01001932 if (!d_is_directory(nd->path.dentry)) {
Al Virobd92d7f2011-03-14 19:54:59 -04001933 path_put(&nd->path);
Al Virobd23a532011-03-23 09:56:30 -04001934 err = -ENOTDIR;
Al Virobd92d7f2011-03-14 19:54:59 -04001935 }
1936 }
Al Viro16c2cd72011-02-22 15:50:10 -05001937
Al Viro70e9b352011-03-05 21:12:22 -05001938 if (base)
1939 fput(base);
Al Viroee0827c2011-02-21 23:38:09 -05001940
Al Viro5b6ca022011-03-09 23:04:47 -05001941 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
Al Viro2a737872009-04-07 11:49:53 -04001942 path_put(&nd->root);
1943 nd->root.mnt = NULL;
1944 }
Al Virobd92d7f2011-03-14 19:54:59 -04001945 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001946}
Nick Piggin31e6b012011-01-07 17:49:52 +11001947
Jeff Layton873f1ee2012-10-10 15:25:29 -04001948static int filename_lookup(int dfd, struct filename *name,
1949 unsigned int flags, struct nameidata *nd)
1950{
1951 int retval = path_lookupat(dfd, name->name, flags | LOOKUP_RCU, nd);
1952 if (unlikely(retval == -ECHILD))
1953 retval = path_lookupat(dfd, name->name, flags, nd);
1954 if (unlikely(retval == -ESTALE))
1955 retval = path_lookupat(dfd, name->name,
1956 flags | LOOKUP_REVAL, nd);
1957
1958 if (likely(!retval))
Jeff Laytonadb5c242012-10-10 16:43:13 -04001959 audit_inode(name, nd->path.dentry, flags & LOOKUP_PARENT);
Jeff Layton873f1ee2012-10-10 15:25:29 -04001960 return retval;
1961}
1962
Al Viroee0827c2011-02-21 23:38:09 -05001963static int do_path_lookup(int dfd, const char *name,
1964 unsigned int flags, struct nameidata *nd)
1965{
Jeff Layton873f1ee2012-10-10 15:25:29 -04001966 struct filename filename = { .name = name };
Nick Piggin31e6b012011-01-07 17:49:52 +11001967
Jeff Layton873f1ee2012-10-10 15:25:29 -04001968 return filename_lookup(dfd, &filename, flags, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969}
1970
Al Viro79714f72012-06-15 03:01:42 +04001971/* does lookup, returns the object with parent locked */
1972struct dentry *kern_path_locked(const char *name, struct path *path)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001973{
Al Viro79714f72012-06-15 03:01:42 +04001974 struct nameidata nd;
1975 struct dentry *d;
1976 int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd);
1977 if (err)
1978 return ERR_PTR(err);
1979 if (nd.last_type != LAST_NORM) {
1980 path_put(&nd.path);
1981 return ERR_PTR(-EINVAL);
1982 }
1983 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Al Viro1e0ea002012-07-22 23:46:21 +04001984 d = __lookup_hash(&nd.last, nd.path.dentry, 0);
Al Viro79714f72012-06-15 03:01:42 +04001985 if (IS_ERR(d)) {
1986 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
1987 path_put(&nd.path);
1988 return d;
1989 }
1990 *path = nd.path;
1991 return d;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001992}
1993
Al Virod1811462008-08-02 00:49:18 -04001994int kern_path(const char *name, unsigned int flags, struct path *path)
1995{
1996 struct nameidata nd;
1997 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1998 if (!res)
1999 *path = nd.path;
2000 return res;
2001}
2002
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002003/**
2004 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2005 * @dentry: pointer to dentry of the base directory
2006 * @mnt: pointer to vfs mount of the base directory
2007 * @name: pointer to file name
2008 * @flags: lookup flags
Al Viroe0a01242011-06-27 17:00:37 -04002009 * @path: pointer to struct path to fill
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002010 */
2011int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2012 const char *name, unsigned int flags,
Al Viroe0a01242011-06-27 17:00:37 -04002013 struct path *path)
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002014{
Al Viroe0a01242011-06-27 17:00:37 -04002015 struct nameidata nd;
2016 int err;
2017 nd.root.dentry = dentry;
2018 nd.root.mnt = mnt;
2019 BUG_ON(flags & LOOKUP_PARENT);
Al Viro5b6ca022011-03-09 23:04:47 -05002020 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
Al Viroe0a01242011-06-27 17:00:37 -04002021 err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
2022 if (!err)
2023 *path = nd.path;
2024 return err;
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002025}
2026
James Morris057f6c02007-04-26 00:12:05 -07002027/*
2028 * Restricted form of lookup. Doesn't follow links, single-component only,
2029 * needs parent already locked. Doesn't follow mounts.
2030 * SMP-safe.
2031 */
Adrian Bunka244e162006-03-31 02:32:11 -08002032static struct dentry *lookup_hash(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033{
Al Viro72bd8662012-06-10 17:17:17 -04002034 return __lookup_hash(&nd->last, nd->path.dentry, nd->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035}
2036
Christoph Hellwigeead1912007-10-16 23:25:38 -07002037/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07002038 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07002039 * @name: pathname component to lookup
2040 * @base: base directory to lookup from
2041 * @len: maximum length @len should be interpreted to
2042 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07002043 * Note that this routine is purely a helper for filesystem usage and should
2044 * not be called by generic code. Also note that by using this function the
Christoph Hellwigeead1912007-10-16 23:25:38 -07002045 * nameidata argument is passed to the filesystem methods and a filesystem
2046 * using this helper needs to be prepared for that.
2047 */
James Morris057f6c02007-04-26 00:12:05 -07002048struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2049{
James Morris057f6c02007-04-26 00:12:05 -07002050 struct qstr this;
Al Viro6a96ba52011-03-07 23:49:20 -05002051 unsigned int c;
Miklos Szeredicda309d2012-03-26 12:54:21 +02002052 int err;
James Morris057f6c02007-04-26 00:12:05 -07002053
David Woodhouse2f9092e2009-04-20 23:18:37 +01002054 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
2055
Al Viro6a96ba52011-03-07 23:49:20 -05002056 this.name = name;
2057 this.len = len;
Linus Torvalds0145acc2012-03-02 14:32:59 -08002058 this.hash = full_name_hash(name, len);
Al Viro6a96ba52011-03-07 23:49:20 -05002059 if (!len)
2060 return ERR_PTR(-EACCES);
2061
Al Viro21d8a152012-11-29 22:17:21 -05002062 if (unlikely(name[0] == '.')) {
2063 if (len < 2 || (len == 2 && name[1] == '.'))
2064 return ERR_PTR(-EACCES);
2065 }
2066
Al Viro6a96ba52011-03-07 23:49:20 -05002067 while (len--) {
2068 c = *(const unsigned char *)name++;
2069 if (c == '/' || c == '\0')
2070 return ERR_PTR(-EACCES);
Al Viro6a96ba52011-03-07 23:49:20 -05002071 }
Al Viro5a202bc2011-03-08 14:17:44 -05002072 /*
2073 * See if the low-level filesystem might want
2074 * to use its own hash..
2075 */
2076 if (base->d_flags & DCACHE_OP_HASH) {
Linus Torvaldsda53be12013-05-21 15:22:44 -07002077 int err = base->d_op->d_hash(base, &this);
Al Viro5a202bc2011-03-08 14:17:44 -05002078 if (err < 0)
2079 return ERR_PTR(err);
2080 }
Christoph Hellwigeead1912007-10-16 23:25:38 -07002081
Miklos Szeredicda309d2012-03-26 12:54:21 +02002082 err = inode_permission(base->d_inode, MAY_EXEC);
2083 if (err)
2084 return ERR_PTR(err);
2085
Al Viro72bd8662012-06-10 17:17:17 -04002086 return __lookup_hash(&this, base, 0);
James Morris057f6c02007-04-26 00:12:05 -07002087}
2088
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002089int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2090 struct path *path, int *empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091{
Al Viro2d8f3032008-07-22 09:59:21 -04002092 struct nameidata nd;
Jeff Layton91a27b22012-10-10 15:25:28 -04002093 struct filename *tmp = getname_flags(name, flags, empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 int err = PTR_ERR(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 if (!IS_ERR(tmp)) {
Al Viro2d8f3032008-07-22 09:59:21 -04002096
2097 BUG_ON(flags & LOOKUP_PARENT);
2098
Jeff Layton873f1ee2012-10-10 15:25:29 -04002099 err = filename_lookup(dfd, tmp, flags, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 putname(tmp);
Al Viro2d8f3032008-07-22 09:59:21 -04002101 if (!err)
2102 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 }
2104 return err;
2105}
2106
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002107int user_path_at(int dfd, const char __user *name, unsigned flags,
2108 struct path *path)
2109{
Linus Torvaldsf7493e52012-03-22 16:10:40 -07002110 return user_path_at_empty(dfd, name, flags, path, NULL);
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002111}
2112
Jeff Layton873f1ee2012-10-10 15:25:29 -04002113/*
2114 * NB: most callers don't do anything directly with the reference to the
2115 * to struct filename, but the nd->last pointer points into the name string
2116 * allocated by getname. So we must hold the reference to it until all
2117 * path-walking is complete.
2118 */
Jeff Layton91a27b22012-10-10 15:25:28 -04002119static struct filename *
Jeff Layton9e790bd2012-12-11 12:10:09 -05002120user_path_parent(int dfd, const char __user *path, struct nameidata *nd,
2121 unsigned int flags)
Al Viro2ad94ae2008-07-21 09:32:51 -04002122{
Jeff Layton91a27b22012-10-10 15:25:28 -04002123 struct filename *s = getname(path);
Al Viro2ad94ae2008-07-21 09:32:51 -04002124 int error;
2125
Jeff Layton9e790bd2012-12-11 12:10:09 -05002126 /* only LOOKUP_REVAL is allowed in extra flags */
2127 flags &= LOOKUP_REVAL;
2128
Al Viro2ad94ae2008-07-21 09:32:51 -04002129 if (IS_ERR(s))
Jeff Layton91a27b22012-10-10 15:25:28 -04002130 return s;
Al Viro2ad94ae2008-07-21 09:32:51 -04002131
Jeff Layton9e790bd2012-12-11 12:10:09 -05002132 error = filename_lookup(dfd, s, flags | LOOKUP_PARENT, nd);
Jeff Layton91a27b22012-10-10 15:25:28 -04002133 if (error) {
Al Viro2ad94ae2008-07-21 09:32:51 -04002134 putname(s);
Jeff Layton91a27b22012-10-10 15:25:28 -04002135 return ERR_PTR(error);
2136 }
Al Viro2ad94ae2008-07-21 09:32:51 -04002137
Jeff Layton91a27b22012-10-10 15:25:28 -04002138 return s;
Al Viro2ad94ae2008-07-21 09:32:51 -04002139}
2140
Jeff Layton80334262013-07-26 06:23:25 -04002141/**
Al Viro197df042013-09-08 14:03:27 -04002142 * mountpoint_last - look up last component for umount
Jeff Layton80334262013-07-26 06:23:25 -04002143 * @nd: pathwalk nameidata - currently pointing at parent directory of "last"
2144 * @path: pointer to container for result
2145 *
2146 * This is a special lookup_last function just for umount. In this case, we
2147 * need to resolve the path without doing any revalidation.
2148 *
2149 * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
2150 * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
2151 * in almost all cases, this lookup will be served out of the dcache. The only
2152 * cases where it won't are if nd->last refers to a symlink or the path is
2153 * bogus and it doesn't exist.
2154 *
2155 * Returns:
2156 * -error: if there was an error during lookup. This includes -ENOENT if the
2157 * lookup found a negative dentry. The nd->path reference will also be
2158 * put in this case.
2159 *
2160 * 0: if we successfully resolved nd->path and found it to not to be a
2161 * symlink that needs to be followed. "path" will also be populated.
2162 * The nd->path reference will also be put.
2163 *
2164 * 1: if we successfully resolved nd->last and found it to be a symlink
2165 * that needs to be followed. "path" will be populated with the path
2166 * to the link, and nd->path will *not* be put.
2167 */
2168static int
Al Viro197df042013-09-08 14:03:27 -04002169mountpoint_last(struct nameidata *nd, struct path *path)
Jeff Layton80334262013-07-26 06:23:25 -04002170{
2171 int error = 0;
2172 struct dentry *dentry;
2173 struct dentry *dir = nd->path.dentry;
2174
Al Viro35759522013-09-08 13:41:33 -04002175 /* If we're in rcuwalk, drop out of it to handle last component */
2176 if (nd->flags & LOOKUP_RCU) {
2177 if (unlazy_walk(nd, NULL)) {
2178 error = -ECHILD;
2179 goto out;
2180 }
Jeff Layton80334262013-07-26 06:23:25 -04002181 }
2182
2183 nd->flags &= ~LOOKUP_PARENT;
2184
2185 if (unlikely(nd->last_type != LAST_NORM)) {
2186 error = handle_dots(nd, nd->last_type);
Al Viro35759522013-09-08 13:41:33 -04002187 if (error)
2188 goto out;
2189 dentry = dget(nd->path.dentry);
2190 goto done;
Jeff Layton80334262013-07-26 06:23:25 -04002191 }
2192
2193 mutex_lock(&dir->d_inode->i_mutex);
2194 dentry = d_lookup(dir, &nd->last);
2195 if (!dentry) {
2196 /*
2197 * No cached dentry. Mounted dentries are pinned in the cache,
2198 * so that means that this dentry is probably a symlink or the
2199 * path doesn't actually point to a mounted dentry.
2200 */
2201 dentry = d_alloc(dir, &nd->last);
2202 if (!dentry) {
2203 error = -ENOMEM;
Dave Jonesbcceeeb2013-09-10 17:04:25 -04002204 mutex_unlock(&dir->d_inode->i_mutex);
Al Viro35759522013-09-08 13:41:33 -04002205 goto out;
Jeff Layton80334262013-07-26 06:23:25 -04002206 }
Al Viro35759522013-09-08 13:41:33 -04002207 dentry = lookup_real(dir->d_inode, dentry, nd->flags);
2208 error = PTR_ERR(dentry);
Dave Jonesbcceeeb2013-09-10 17:04:25 -04002209 if (IS_ERR(dentry)) {
2210 mutex_unlock(&dir->d_inode->i_mutex);
Al Viro35759522013-09-08 13:41:33 -04002211 goto out;
Dave Jonesbcceeeb2013-09-10 17:04:25 -04002212 }
Jeff Layton80334262013-07-26 06:23:25 -04002213 }
2214 mutex_unlock(&dir->d_inode->i_mutex);
2215
Al Viro35759522013-09-08 13:41:33 -04002216done:
2217 if (!dentry->d_inode) {
2218 error = -ENOENT;
2219 dput(dentry);
2220 goto out;
Jeff Layton80334262013-07-26 06:23:25 -04002221 }
Al Viro35759522013-09-08 13:41:33 -04002222 path->dentry = dentry;
2223 path->mnt = mntget(nd->path.mnt);
David Howellsb18825a2013-09-12 19:22:53 +01002224 if (should_follow_link(dentry, nd->flags & LOOKUP_FOLLOW))
Al Viro35759522013-09-08 13:41:33 -04002225 return 1;
2226 follow_mount(path);
2227 error = 0;
2228out:
Jeff Layton80334262013-07-26 06:23:25 -04002229 terminate_walk(nd);
2230 return error;
2231}
2232
2233/**
Al Viro197df042013-09-08 14:03:27 -04002234 * path_mountpoint - look up a path to be umounted
Jeff Layton80334262013-07-26 06:23:25 -04002235 * @dfd: directory file descriptor to start walk from
2236 * @name: full pathname to walk
Randy Dunlap606d6fe2013-10-19 14:56:55 -07002237 * @path: pointer to container for result
Jeff Layton80334262013-07-26 06:23:25 -04002238 * @flags: lookup flags
Jeff Layton80334262013-07-26 06:23:25 -04002239 *
2240 * Look up the given name, but don't attempt to revalidate the last component.
Randy Dunlap606d6fe2013-10-19 14:56:55 -07002241 * Returns 0 and "path" will be valid on success; Returns error otherwise.
Jeff Layton80334262013-07-26 06:23:25 -04002242 */
2243static int
Al Viro197df042013-09-08 14:03:27 -04002244path_mountpoint(int dfd, const char *name, struct path *path, unsigned int flags)
Jeff Layton80334262013-07-26 06:23:25 -04002245{
2246 struct file *base = NULL;
2247 struct nameidata nd;
2248 int err;
2249
2250 err = path_init(dfd, name, flags | LOOKUP_PARENT, &nd, &base);
2251 if (unlikely(err))
2252 return err;
2253
2254 current->total_link_count = 0;
2255 err = link_path_walk(name, &nd);
2256 if (err)
2257 goto out;
2258
Al Viro197df042013-09-08 14:03:27 -04002259 err = mountpoint_last(&nd, path);
Jeff Layton80334262013-07-26 06:23:25 -04002260 while (err > 0) {
2261 void *cookie;
2262 struct path link = *path;
2263 err = may_follow_link(&link, &nd);
2264 if (unlikely(err))
2265 break;
2266 nd.flags |= LOOKUP_PARENT;
2267 err = follow_link(&link, &nd, &cookie);
2268 if (err)
2269 break;
Al Viro197df042013-09-08 14:03:27 -04002270 err = mountpoint_last(&nd, path);
Jeff Layton80334262013-07-26 06:23:25 -04002271 put_link(&nd, &link, cookie);
2272 }
2273out:
2274 if (base)
2275 fput(base);
2276
2277 if (nd.root.mnt && !(nd.flags & LOOKUP_ROOT))
2278 path_put(&nd.root);
2279
2280 return err;
2281}
2282
Al Viro2d864652013-09-08 20:18:44 -04002283static int
2284filename_mountpoint(int dfd, struct filename *s, struct path *path,
2285 unsigned int flags)
2286{
2287 int error = path_mountpoint(dfd, s->name, path, flags | LOOKUP_RCU);
2288 if (unlikely(error == -ECHILD))
2289 error = path_mountpoint(dfd, s->name, path, flags);
2290 if (unlikely(error == -ESTALE))
2291 error = path_mountpoint(dfd, s->name, path, flags | LOOKUP_REVAL);
2292 if (likely(!error))
2293 audit_inode(s, path->dentry, 0);
2294 return error;
2295}
2296
Jeff Layton80334262013-07-26 06:23:25 -04002297/**
Al Viro197df042013-09-08 14:03:27 -04002298 * user_path_mountpoint_at - lookup a path from userland in order to umount it
Jeff Layton80334262013-07-26 06:23:25 -04002299 * @dfd: directory file descriptor
2300 * @name: pathname from userland
2301 * @flags: lookup flags
2302 * @path: pointer to container to hold result
2303 *
2304 * A umount is a special case for path walking. We're not actually interested
2305 * in the inode in this situation, and ESTALE errors can be a problem. We
2306 * simply want track down the dentry and vfsmount attached at the mountpoint
2307 * and avoid revalidating the last component.
2308 *
2309 * Returns 0 and populates "path" on success.
2310 */
2311int
Al Viro197df042013-09-08 14:03:27 -04002312user_path_mountpoint_at(int dfd, const char __user *name, unsigned int flags,
Jeff Layton80334262013-07-26 06:23:25 -04002313 struct path *path)
2314{
2315 struct filename *s = getname(name);
2316 int error;
Jeff Layton80334262013-07-26 06:23:25 -04002317 if (IS_ERR(s))
2318 return PTR_ERR(s);
Al Viro2d864652013-09-08 20:18:44 -04002319 error = filename_mountpoint(dfd, s, path, flags);
Jeff Layton80334262013-07-26 06:23:25 -04002320 putname(s);
2321 return error;
2322}
2323
Al Viro2d864652013-09-08 20:18:44 -04002324int
2325kern_path_mountpoint(int dfd, const char *name, struct path *path,
2326 unsigned int flags)
2327{
2328 struct filename s = {.name = name};
2329 return filename_mountpoint(dfd, &s, path, flags);
2330}
2331EXPORT_SYMBOL(kern_path_mountpoint);
2332
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333/*
2334 * It's inline, so penalty for filesystems that don't use sticky bit is
2335 * minimal.
2336 */
2337static inline int check_sticky(struct inode *dir, struct inode *inode)
2338{
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002339 kuid_t fsuid = current_fsuid();
David Howellsda9592e2008-11-14 10:39:05 +11002340
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 if (!(dir->i_mode & S_ISVTX))
2342 return 0;
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002343 if (uid_eq(inode->i_uid, fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 return 0;
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002345 if (uid_eq(dir->i_uid, fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 return 0;
Eric W. Biederman1a48e2a2011-11-14 16:24:06 -08002347 return !inode_capable(inode, CAP_FOWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348}
2349
2350/*
2351 * Check whether we can remove a link victim from directory dir, check
2352 * whether the type of victim is right.
2353 * 1. We can't do it if dir is read-only (done in permission())
2354 * 2. We should have write and exec permissions on dir
2355 * 3. We can't remove anything from append-only dir
2356 * 4. We can't do anything with immutable dir (done in permission())
2357 * 5. If the sticky bit on dir is set we should either
2358 * a. be owner of dir, or
2359 * b. be owner of victim, or
2360 * c. have CAP_FOWNER capability
2361 * 6. If the victim is append-only or immutable we can't do antyhing with
2362 * links pointing to it.
2363 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2364 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2365 * 9. We can't remove a root or mountpoint.
2366 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
2367 * nfs_async_unlink().
2368 */
David Howellsb18825a2013-09-12 19:22:53 +01002369static int may_delete(struct inode *dir, struct dentry *victim, bool isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370{
David Howellsb18825a2013-09-12 19:22:53 +01002371 struct inode *inode = victim->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 int error;
2373
David Howellsb18825a2013-09-12 19:22:53 +01002374 if (d_is_negative(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 return -ENOENT;
David Howellsb18825a2013-09-12 19:22:53 +01002376 BUG_ON(!inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
2378 BUG_ON(victim->d_parent->d_inode != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04002379 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
Al Virof419a2e2008-07-22 00:07:17 -04002381 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 if (error)
2383 return error;
2384 if (IS_APPEND(dir))
2385 return -EPERM;
David Howellsb18825a2013-09-12 19:22:53 +01002386
2387 if (check_sticky(dir, inode) || IS_APPEND(inode) ||
2388 IS_IMMUTABLE(inode) || IS_SWAPFILE(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 return -EPERM;
2390 if (isdir) {
David Howellsb18825a2013-09-12 19:22:53 +01002391 if (!d_is_directory(victim) && !d_is_autodir(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 return -ENOTDIR;
2393 if (IS_ROOT(victim))
2394 return -EBUSY;
David Howellsb18825a2013-09-12 19:22:53 +01002395 } else if (d_is_directory(victim) || d_is_autodir(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 return -EISDIR;
2397 if (IS_DEADDIR(dir))
2398 return -ENOENT;
2399 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2400 return -EBUSY;
2401 return 0;
2402}
2403
2404/* Check whether we can create an object with dentry child in directory
2405 * dir.
2406 * 1. We can't do it if child already exists (open has special treatment for
2407 * this case, but since we are inlined it's OK)
2408 * 2. We can't do it if dir is read-only (done in permission())
2409 * 3. We should have write and exec permissions on dir
2410 * 4. We can't do it if dir is immutable (done in permission())
2411 */
Miklos Szeredia95164d2008-07-30 15:08:48 +02002412static inline int may_create(struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413{
Jeff Layton14e972b2013-05-08 10:25:58 -04002414 audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 if (child->d_inode)
2416 return -EEXIST;
2417 if (IS_DEADDIR(dir))
2418 return -ENOENT;
Al Virof419a2e2008-07-22 00:07:17 -04002419 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420}
2421
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422/*
2423 * p1 and p2 should be directories on the same fs.
2424 */
2425struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2426{
2427 struct dentry *p;
2428
2429 if (p1 == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07002430 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 return NULL;
2432 }
2433
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002434 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002436 p = d_ancestor(p2, p1);
2437 if (p) {
2438 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2439 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
2440 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 }
2442
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002443 p = d_ancestor(p1, p2);
2444 if (p) {
2445 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2446 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2447 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 }
2449
Ingo Molnarf2eace22006-07-03 00:25:05 -07002450 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2451 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 return NULL;
2453}
2454
2455void unlock_rename(struct dentry *p1, struct dentry *p2)
2456{
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002457 mutex_unlock(&p1->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 if (p1 != p2) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002459 mutex_unlock(&p2->d_inode->i_mutex);
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002460 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 }
2462}
2463
Al Viro4acdaf22011-07-26 01:42:34 -04002464int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viro312b63f2012-06-10 18:09:36 -04002465 bool want_excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002467 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 if (error)
2469 return error;
2470
Al Viroacfa4382008-12-04 10:06:33 -05002471 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 return -EACCES; /* shouldn't it be ENOSYS? */
2473 mode &= S_IALLUGO;
2474 mode |= S_IFREG;
2475 error = security_inode_create(dir, dentry, mode);
2476 if (error)
2477 return error;
Al Viro312b63f2012-06-10 18:09:36 -04002478 error = dir->i_op->create(dir, dentry, mode, want_excl);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002479 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002480 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 return error;
2482}
2483
Al Viro73d049a2011-03-11 12:08:24 -05002484static int may_open(struct path *path, int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002486 struct dentry *dentry = path->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 struct inode *inode = dentry->d_inode;
2488 int error;
2489
Al Virobcda7652011-03-13 16:42:14 -04002490 /* O_PATH? */
2491 if (!acc_mode)
2492 return 0;
2493
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 if (!inode)
2495 return -ENOENT;
2496
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002497 switch (inode->i_mode & S_IFMT) {
2498 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002500 case S_IFDIR:
2501 if (acc_mode & MAY_WRITE)
2502 return -EISDIR;
2503 break;
2504 case S_IFBLK:
2505 case S_IFCHR:
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002506 if (path->mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002508 /*FALLTHRU*/
2509 case S_IFIFO:
2510 case S_IFSOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002512 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08002513 }
Dave Hansenb41572e2007-10-16 23:31:14 -07002514
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002515 error = inode_permission(inode, acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07002516 if (error)
2517 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05002518
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 /*
2520 * An append-only file must be opened in append mode for writing.
2521 */
2522 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05002523 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05002524 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05002526 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 }
2528
2529 /* O_NOATIME can only be set by the owner or superuser */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07002530 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
Al Viro7715b522009-12-16 03:54:00 -05002531 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532
J. Bruce Fieldsf3c7691e2011-09-21 10:58:13 -04002533 return 0;
Al Viro7715b522009-12-16 03:54:00 -05002534}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535
Jeff Laytone1181ee2010-12-07 16:19:50 -05002536static int handle_truncate(struct file *filp)
Al Viro7715b522009-12-16 03:54:00 -05002537{
Jeff Laytone1181ee2010-12-07 16:19:50 -05002538 struct path *path = &filp->f_path;
Al Viro7715b522009-12-16 03:54:00 -05002539 struct inode *inode = path->dentry->d_inode;
2540 int error = get_write_access(inode);
2541 if (error)
2542 return error;
2543 /*
2544 * Refuse to truncate files with mandatory locks held on them.
2545 */
2546 error = locks_verify_locked(inode);
2547 if (!error)
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +09002548 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05002549 if (!error) {
2550 error = do_truncate(path->dentry, 0,
2551 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
Jeff Laytone1181ee2010-12-07 16:19:50 -05002552 filp);
Al Viro7715b522009-12-16 03:54:00 -05002553 }
2554 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04002555 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556}
2557
Dave Hansend57999e2008-02-15 14:37:27 -08002558static inline int open_to_namei_flags(int flag)
2559{
Al Viro8a5e9292011-06-25 19:15:54 -04002560 if ((flag & O_ACCMODE) == 3)
2561 flag--;
Dave Hansend57999e2008-02-15 14:37:27 -08002562 return flag;
2563}
2564
Miklos Szeredid18e9002012-06-05 15:10:17 +02002565static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode)
2566{
2567 int error = security_path_mknod(dir, dentry, mode, 0);
2568 if (error)
2569 return error;
2570
2571 error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
2572 if (error)
2573 return error;
2574
2575 return security_inode_create(dir->dentry->d_inode, dentry, mode);
2576}
2577
David Howells1acf0af2012-06-14 16:13:46 +01002578/*
2579 * Attempt to atomically look up, create and open a file from a negative
2580 * dentry.
2581 *
2582 * Returns 0 if successful. The file will have been created and attached to
2583 * @file by the filesystem calling finish_open().
2584 *
2585 * Returns 1 if the file was looked up only or didn't need creating. The
2586 * caller will need to perform the open themselves. @path will have been
2587 * updated to point to the new dentry. This may be negative.
2588 *
2589 * Returns an error code otherwise.
2590 */
Al Viro2675a4e2012-06-22 12:41:10 +04002591static int atomic_open(struct nameidata *nd, struct dentry *dentry,
2592 struct path *path, struct file *file,
2593 const struct open_flags *op,
Al Viro64894cf2012-07-31 00:53:35 +04002594 bool got_write, bool need_lookup,
Al Viro2675a4e2012-06-22 12:41:10 +04002595 int *opened)
Miklos Szeredid18e9002012-06-05 15:10:17 +02002596{
2597 struct inode *dir = nd->path.dentry->d_inode;
2598 unsigned open_flag = open_to_namei_flags(op->open_flag);
2599 umode_t mode;
2600 int error;
2601 int acc_mode;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002602 int create_error = 0;
2603 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
Miklos Szeredi116cc022013-09-16 14:52:05 +02002604 bool excl;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002605
2606 BUG_ON(dentry->d_inode);
2607
2608 /* Don't create child dentry for a dead directory. */
2609 if (unlikely(IS_DEADDIR(dir))) {
Al Viro2675a4e2012-06-22 12:41:10 +04002610 error = -ENOENT;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002611 goto out;
2612 }
2613
Miklos Szeredi62b259d2012-08-15 13:01:24 +02002614 mode = op->mode;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002615 if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
2616 mode &= ~current_umask();
2617
Miklos Szeredi116cc022013-09-16 14:52:05 +02002618 excl = (open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT);
2619 if (excl)
Miklos Szeredid18e9002012-06-05 15:10:17 +02002620 open_flag &= ~O_TRUNC;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002621
2622 /*
2623 * Checking write permission is tricky, bacuse we don't know if we are
2624 * going to actually need it: O_CREAT opens should work as long as the
2625 * file exists. But checking existence breaks atomicity. The trick is
2626 * to check access and if not granted clear O_CREAT from the flags.
2627 *
2628 * Another problem is returing the "right" error value (e.g. for an
2629 * O_EXCL open we want to return EEXIST not EROFS).
2630 */
Al Viro64894cf2012-07-31 00:53:35 +04002631 if (((open_flag & (O_CREAT | O_TRUNC)) ||
2632 (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) {
2633 if (!(open_flag & O_CREAT)) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002634 /*
2635 * No O_CREATE -> atomicity not a requirement -> fall
2636 * back to lookup + open
2637 */
2638 goto no_open;
2639 } else if (open_flag & (O_EXCL | O_TRUNC)) {
2640 /* Fall back and fail with the right error */
Al Viro64894cf2012-07-31 00:53:35 +04002641 create_error = -EROFS;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002642 goto no_open;
2643 } else {
2644 /* No side effects, safe to clear O_CREAT */
Al Viro64894cf2012-07-31 00:53:35 +04002645 create_error = -EROFS;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002646 open_flag &= ~O_CREAT;
2647 }
2648 }
2649
2650 if (open_flag & O_CREAT) {
Miklos Szeredi38227f72012-08-15 13:01:24 +02002651 error = may_o_create(&nd->path, dentry, mode);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002652 if (error) {
2653 create_error = error;
2654 if (open_flag & O_EXCL)
2655 goto no_open;
2656 open_flag &= ~O_CREAT;
2657 }
2658 }
2659
2660 if (nd->flags & LOOKUP_DIRECTORY)
2661 open_flag |= O_DIRECTORY;
2662
Al Viro30d90492012-06-22 12:40:19 +04002663 file->f_path.dentry = DENTRY_NOT_SET;
2664 file->f_path.mnt = nd->path.mnt;
2665 error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode,
Al Viro47237682012-06-10 05:01:45 -04002666 opened);
Al Virod9585272012-06-22 12:39:14 +04002667 if (error < 0) {
Al Virod9585272012-06-22 12:39:14 +04002668 if (create_error && error == -ENOENT)
2669 error = create_error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002670 goto out;
2671 }
2672
Al Virod9585272012-06-22 12:39:14 +04002673 if (error) { /* returned 1, that is */
Al Viro30d90492012-06-22 12:40:19 +04002674 if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
Al Viro2675a4e2012-06-22 12:41:10 +04002675 error = -EIO;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002676 goto out;
2677 }
Al Viro30d90492012-06-22 12:40:19 +04002678 if (file->f_path.dentry) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002679 dput(dentry);
Al Viro30d90492012-06-22 12:40:19 +04002680 dentry = file->f_path.dentry;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002681 }
Al Viro03da6332013-09-16 19:22:33 -04002682 if (*opened & FILE_CREATED)
2683 fsnotify_create(dir, dentry);
2684 if (!dentry->d_inode) {
2685 WARN_ON(*opened & FILE_CREATED);
2686 if (create_error) {
2687 error = create_error;
2688 goto out;
2689 }
2690 } else {
2691 if (excl && !(*opened & FILE_CREATED)) {
2692 error = -EEXIST;
2693 goto out;
2694 }
Sage Weil62b2ce92012-08-15 13:30:12 -07002695 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02002696 goto looked_up;
2697 }
2698
2699 /*
2700 * We didn't have the inode before the open, so check open permission
2701 * here.
2702 */
Al Viro03da6332013-09-16 19:22:33 -04002703 acc_mode = op->acc_mode;
2704 if (*opened & FILE_CREATED) {
2705 WARN_ON(!(open_flag & O_CREAT));
2706 fsnotify_create(dir, dentry);
2707 acc_mode = MAY_OPEN;
2708 }
Al Viro2675a4e2012-06-22 12:41:10 +04002709 error = may_open(&file->f_path, acc_mode, open_flag);
2710 if (error)
2711 fput(file);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002712
2713out:
2714 dput(dentry);
Al Viro2675a4e2012-06-22 12:41:10 +04002715 return error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002716
Miklos Szeredid18e9002012-06-05 15:10:17 +02002717no_open:
2718 if (need_lookup) {
Al Viro72bd8662012-06-10 17:17:17 -04002719 dentry = lookup_real(dir, dentry, nd->flags);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002720 if (IS_ERR(dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04002721 return PTR_ERR(dentry);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002722
2723 if (create_error) {
2724 int open_flag = op->open_flag;
2725
Al Viro2675a4e2012-06-22 12:41:10 +04002726 error = create_error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002727 if ((open_flag & O_EXCL)) {
2728 if (!dentry->d_inode)
2729 goto out;
2730 } else if (!dentry->d_inode) {
2731 goto out;
2732 } else if ((open_flag & O_TRUNC) &&
2733 S_ISREG(dentry->d_inode->i_mode)) {
2734 goto out;
2735 }
2736 /* will fail later, go on to get the right error */
2737 }
2738 }
2739looked_up:
2740 path->dentry = dentry;
2741 path->mnt = nd->path.mnt;
Al Viro2675a4e2012-06-22 12:41:10 +04002742 return 1;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002743}
2744
Nick Piggin31e6b012011-01-07 17:49:52 +11002745/*
David Howells1acf0af2012-06-14 16:13:46 +01002746 * Look up and maybe create and open the last component.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002747 *
2748 * Must be called with i_mutex held on parent.
2749 *
David Howells1acf0af2012-06-14 16:13:46 +01002750 * Returns 0 if the file was successfully atomically created (if necessary) and
2751 * opened. In this case the file will be returned attached to @file.
2752 *
2753 * Returns 1 if the file was not completely opened at this time, though lookups
2754 * and creations will have been performed and the dentry returned in @path will
2755 * be positive upon return if O_CREAT was specified. If O_CREAT wasn't
2756 * specified then a negative dentry may be returned.
2757 *
2758 * An error code is returned otherwise.
2759 *
2760 * FILE_CREATE will be set in @*opened if the dentry was created and will be
2761 * cleared otherwise prior to returning.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002762 */
Al Viro2675a4e2012-06-22 12:41:10 +04002763static int lookup_open(struct nameidata *nd, struct path *path,
2764 struct file *file,
2765 const struct open_flags *op,
Al Viro64894cf2012-07-31 00:53:35 +04002766 bool got_write, int *opened)
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002767{
2768 struct dentry *dir = nd->path.dentry;
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002769 struct inode *dir_inode = dir->d_inode;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002770 struct dentry *dentry;
2771 int error;
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002772 bool need_lookup;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002773
Al Viro47237682012-06-10 05:01:45 -04002774 *opened &= ~FILE_CREATED;
Al Viro201f9562012-06-22 12:42:10 +04002775 dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002776 if (IS_ERR(dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04002777 return PTR_ERR(dentry);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002778
Miklos Szeredid18e9002012-06-05 15:10:17 +02002779 /* Cached positive dentry: will open in f_op->open */
2780 if (!need_lookup && dentry->d_inode)
2781 goto out_no_open;
2782
2783 if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) {
Al Viro64894cf2012-07-31 00:53:35 +04002784 return atomic_open(nd, dentry, path, file, op, got_write,
Al Viro47237682012-06-10 05:01:45 -04002785 need_lookup, opened);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002786 }
2787
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002788 if (need_lookup) {
2789 BUG_ON(dentry->d_inode);
2790
Al Viro72bd8662012-06-10 17:17:17 -04002791 dentry = lookup_real(dir_inode, dentry, nd->flags);
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002792 if (IS_ERR(dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04002793 return PTR_ERR(dentry);
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002794 }
2795
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002796 /* Negative dentry, just create the file */
2797 if (!dentry->d_inode && (op->open_flag & O_CREAT)) {
2798 umode_t mode = op->mode;
2799 if (!IS_POSIXACL(dir->d_inode))
2800 mode &= ~current_umask();
2801 /*
2802 * This write is needed to ensure that a
2803 * rw->ro transition does not occur between
2804 * the time when the file is created and when
2805 * a permanent write count is taken through
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02002806 * the 'struct file' in finish_open().
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002807 */
Al Viro64894cf2012-07-31 00:53:35 +04002808 if (!got_write) {
2809 error = -EROFS;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002810 goto out_dput;
Al Viro64894cf2012-07-31 00:53:35 +04002811 }
Al Viro47237682012-06-10 05:01:45 -04002812 *opened |= FILE_CREATED;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002813 error = security_path_mknod(&nd->path, dentry, mode, 0);
2814 if (error)
2815 goto out_dput;
Al Viro312b63f2012-06-10 18:09:36 -04002816 error = vfs_create(dir->d_inode, dentry, mode,
2817 nd->flags & LOOKUP_EXCL);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002818 if (error)
2819 goto out_dput;
2820 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02002821out_no_open:
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002822 path->dentry = dentry;
2823 path->mnt = nd->path.mnt;
Al Viro2675a4e2012-06-22 12:41:10 +04002824 return 1;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002825
2826out_dput:
2827 dput(dentry);
Al Viro2675a4e2012-06-22 12:41:10 +04002828 return error;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002829}
2830
2831/*
Al Virofe2d35f2011-03-05 22:58:25 -05002832 * Handle the last step of open()
Nick Piggin31e6b012011-01-07 17:49:52 +11002833 */
Al Viro2675a4e2012-06-22 12:41:10 +04002834static int do_last(struct nameidata *nd, struct path *path,
2835 struct file *file, const struct open_flags *op,
Jeff Layton669abf42012-10-10 16:43:10 -04002836 int *opened, struct filename *name)
Al Virofb1cc552009-12-24 01:58:28 -05002837{
Al Viroa1e28032009-12-24 02:12:06 -05002838 struct dentry *dir = nd->path.dentry;
Al Viroca344a892011-03-09 00:36:45 -05002839 int open_flag = op->open_flag;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002840 bool will_truncate = (open_flag & O_TRUNC) != 0;
Al Viro64894cf2012-07-31 00:53:35 +04002841 bool got_write = false;
Al Virobcda7652011-03-13 16:42:14 -04002842 int acc_mode = op->acc_mode;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02002843 struct inode *inode;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002844 bool symlink_ok = false;
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02002845 struct path save_parent = { .dentry = NULL, .mnt = NULL };
2846 bool retried = false;
Al Viro16c2cd72011-02-22 15:50:10 -05002847 int error;
Al Virofb1cc552009-12-24 01:58:28 -05002848
Al Viroc3e380b2011-02-23 13:39:45 -05002849 nd->flags &= ~LOOKUP_PARENT;
2850 nd->flags |= op->intent;
2851
Al Virobc77daa2013-06-06 09:12:33 -04002852 if (nd->last_type != LAST_NORM) {
Al Virofe2d35f2011-03-05 22:58:25 -05002853 error = handle_dots(nd, nd->last_type);
2854 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04002855 return error;
Miklos Szeredie83db162012-06-05 15:10:29 +02002856 goto finish_open;
Al Viro1f36f772009-12-26 10:56:19 -05002857 }
Al Viro67ee3ad2009-12-26 07:01:01 -05002858
Al Viroca344a892011-03-09 00:36:45 -05002859 if (!(open_flag & O_CREAT)) {
Al Virofe2d35f2011-03-05 22:58:25 -05002860 if (nd->last.name[nd->last.len])
2861 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
Al Virobcda7652011-03-13 16:42:14 -04002862 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002863 symlink_ok = true;
Al Virofe2d35f2011-03-05 22:58:25 -05002864 /* we _can_ be in RCU mode here */
Al Viroe97cdc82013-01-24 18:16:00 -05002865 error = lookup_fast(nd, path, &inode);
Miklos Szeredi71574862012-06-05 15:10:14 +02002866 if (likely(!error))
2867 goto finish_lookup;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02002868
Miklos Szeredi71574862012-06-05 15:10:14 +02002869 if (error < 0)
Al Viro2675a4e2012-06-22 12:41:10 +04002870 goto out;
Miklos Szeredi37d7fff2012-06-05 15:10:12 +02002871
Miklos Szeredi71574862012-06-05 15:10:14 +02002872 BUG_ON(nd->inode != dir->d_inode);
Miklos Szeredib6183df2012-06-05 15:10:13 +02002873 } else {
2874 /* create side of things */
2875 /*
2876 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
2877 * has been cleared when we got to the last component we are
2878 * about to look up
2879 */
2880 error = complete_walk(nd);
2881 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04002882 return error;
Miklos Szeredib6183df2012-06-05 15:10:13 +02002883
Jeff Layton33e22082013-04-12 15:16:32 -04002884 audit_inode(name, dir, LOOKUP_PARENT);
Miklos Szeredib6183df2012-06-05 15:10:13 +02002885 error = -EISDIR;
2886 /* trailing slashes? */
2887 if (nd->last.name[nd->last.len])
Al Viro2675a4e2012-06-22 12:41:10 +04002888 goto out;
Al Virofe2d35f2011-03-05 22:58:25 -05002889 }
2890
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02002891retry_lookup:
Al Viro64894cf2012-07-31 00:53:35 +04002892 if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
2893 error = mnt_want_write(nd->path.mnt);
2894 if (!error)
2895 got_write = true;
2896 /*
2897 * do _not_ fail yet - we might not need that or fail with
2898 * a different error; let lookup_open() decide; we'll be
2899 * dropping this one anyway.
2900 */
2901 }
Al Viroa1e28032009-12-24 02:12:06 -05002902 mutex_lock(&dir->d_inode->i_mutex);
Al Viro64894cf2012-07-31 00:53:35 +04002903 error = lookup_open(nd, path, file, op, got_write, opened);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002904 mutex_unlock(&dir->d_inode->i_mutex);
Al Viroa1e28032009-12-24 02:12:06 -05002905
Al Viro2675a4e2012-06-22 12:41:10 +04002906 if (error <= 0) {
2907 if (error)
Miklos Szeredid18e9002012-06-05 15:10:17 +02002908 goto out;
2909
Al Viro47237682012-06-10 05:01:45 -04002910 if ((*opened & FILE_CREATED) ||
Al Viro496ad9a2013-01-23 17:07:38 -05002911 !S_ISREG(file_inode(file)->i_mode))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002912 will_truncate = false;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002913
Jeff Laytonadb5c242012-10-10 16:43:13 -04002914 audit_inode(name, file->f_path.dentry, 0);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002915 goto opened;
2916 }
Al Virofb1cc552009-12-24 01:58:28 -05002917
Al Viro47237682012-06-10 05:01:45 -04002918 if (*opened & FILE_CREATED) {
Al Viro9b44f1b2011-03-09 00:17:27 -05002919 /* Don't check for write permission, don't truncate */
Al Viroca344a892011-03-09 00:36:45 -05002920 open_flag &= ~O_TRUNC;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002921 will_truncate = false;
Al Virobcda7652011-03-13 16:42:14 -04002922 acc_mode = MAY_OPEN;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002923 path_to_nameidata(path, nd);
Miklos Szeredie83db162012-06-05 15:10:29 +02002924 goto finish_open_created;
Al Virofb1cc552009-12-24 01:58:28 -05002925 }
2926
2927 /*
Jeff Layton3134f372012-07-25 10:19:47 -04002928 * create/update audit record if it already exists.
Al Virofb1cc552009-12-24 01:58:28 -05002929 */
David Howellsb18825a2013-09-12 19:22:53 +01002930 if (d_is_positive(path->dentry))
Jeff Laytonadb5c242012-10-10 16:43:13 -04002931 audit_inode(name, path->dentry, 0);
Al Virofb1cc552009-12-24 01:58:28 -05002932
Miklos Szeredid18e9002012-06-05 15:10:17 +02002933 /*
2934 * If atomic_open() acquired write access it is dropped now due to
2935 * possible mount and symlink following (this might be optimized away if
2936 * necessary...)
2937 */
Al Viro64894cf2012-07-31 00:53:35 +04002938 if (got_write) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002939 mnt_drop_write(nd->path.mnt);
Al Viro64894cf2012-07-31 00:53:35 +04002940 got_write = false;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002941 }
2942
Al Virofb1cc552009-12-24 01:58:28 -05002943 error = -EEXIST;
Al Virof8310c52012-07-30 11:50:30 +04002944 if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))
Al Virofb1cc552009-12-24 01:58:28 -05002945 goto exit_dput;
2946
David Howells9875cf82011-01-14 18:45:21 +00002947 error = follow_managed(path, nd->flags);
2948 if (error < 0)
2949 goto exit_dput;
Al Virofb1cc552009-12-24 01:58:28 -05002950
Al Viroa3fbbde2011-11-07 21:21:26 +00002951 if (error)
2952 nd->flags |= LOOKUP_JUMPED;
2953
Miklos Szeredidecf3402012-05-21 17:30:08 +02002954 BUG_ON(nd->flags & LOOKUP_RCU);
2955 inode = path->dentry->d_inode;
Miklos Szeredi5f5daac2012-05-21 17:30:14 +02002956finish_lookup:
2957 /* we _can_ be in RCU mode here */
Al Virofb1cc552009-12-24 01:58:28 -05002958 error = -ENOENT;
David Howellsb18825a2013-09-12 19:22:53 +01002959 if (d_is_negative(path->dentry)) {
Miklos Szeredi54c33e72012-05-21 17:30:10 +02002960 path_to_nameidata(path, nd);
Al Viro2675a4e2012-06-22 12:41:10 +04002961 goto out;
Miklos Szeredi54c33e72012-05-21 17:30:10 +02002962 }
Al Viro9e67f362009-12-26 07:04:50 -05002963
David Howellsb18825a2013-09-12 19:22:53 +01002964 if (should_follow_link(path->dentry, !symlink_ok)) {
Miklos Szeredid45ea862012-05-21 17:30:09 +02002965 if (nd->flags & LOOKUP_RCU) {
2966 if (unlikely(unlazy_walk(nd, path->dentry))) {
2967 error = -ECHILD;
Al Viro2675a4e2012-06-22 12:41:10 +04002968 goto out;
Miklos Szeredid45ea862012-05-21 17:30:09 +02002969 }
2970 }
2971 BUG_ON(inode != path->dentry->d_inode);
Al Viro2675a4e2012-06-22 12:41:10 +04002972 return 1;
Miklos Szeredid45ea862012-05-21 17:30:09 +02002973 }
Al Virofb1cc552009-12-24 01:58:28 -05002974
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02002975 if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) {
2976 path_to_nameidata(path, nd);
2977 } else {
2978 save_parent.dentry = nd->path.dentry;
2979 save_parent.mnt = mntget(path->mnt);
2980 nd->path.dentry = path->dentry;
2981
2982 }
Miklos Szeredidecf3402012-05-21 17:30:08 +02002983 nd->inode = inode;
Al Viroa3fbbde2011-11-07 21:21:26 +00002984 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
Al Virobc77daa2013-06-06 09:12:33 -04002985finish_open:
Al Viroa3fbbde2011-11-07 21:21:26 +00002986 error = complete_walk(nd);
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02002987 if (error) {
2988 path_put(&save_parent);
Al Viro2675a4e2012-06-22 12:41:10 +04002989 return error;
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02002990 }
Al Virobc77daa2013-06-06 09:12:33 -04002991 audit_inode(name, nd->path.dentry, 0);
Al Virofb1cc552009-12-24 01:58:28 -05002992 error = -EISDIR;
David Howellsb18825a2013-09-12 19:22:53 +01002993 if ((open_flag & O_CREAT) &&
2994 (d_is_directory(nd->path.dentry) || d_is_autodir(nd->path.dentry)))
Al Viro2675a4e2012-06-22 12:41:10 +04002995 goto out;
Miklos Szerediaf2f5542012-05-21 17:30:11 +02002996 error = -ENOTDIR;
David Howellsb18825a2013-09-12 19:22:53 +01002997 if ((nd->flags & LOOKUP_DIRECTORY) && !d_is_directory(nd->path.dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04002998 goto out;
Al Viro6c0d46c2011-03-09 00:59:59 -05002999 if (!S_ISREG(nd->inode->i_mode))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003000 will_truncate = false;
Al Viro6c0d46c2011-03-09 00:59:59 -05003001
Al Viro0f9d1a12011-03-09 00:13:14 -05003002 if (will_truncate) {
3003 error = mnt_want_write(nd->path.mnt);
3004 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003005 goto out;
Al Viro64894cf2012-07-31 00:53:35 +04003006 got_write = true;
Al Viro0f9d1a12011-03-09 00:13:14 -05003007 }
Miklos Szeredie83db162012-06-05 15:10:29 +02003008finish_open_created:
Al Virobcda7652011-03-13 16:42:14 -04003009 error = may_open(&nd->path, acc_mode, open_flag);
Al Viroca344a892011-03-09 00:36:45 -05003010 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003011 goto out;
Al Viro30d90492012-06-22 12:40:19 +04003012 file->f_path.mnt = nd->path.mnt;
3013 error = finish_open(file, nd->path.dentry, NULL, opened);
3014 if (error) {
Al Viro30d90492012-06-22 12:40:19 +04003015 if (error == -EOPENSTALE)
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003016 goto stale_open;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003017 goto out;
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003018 }
Miklos Szeredia8277b92012-06-05 15:10:32 +02003019opened:
Al Viro2675a4e2012-06-22 12:41:10 +04003020 error = open_check_o_direct(file);
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003021 if (error)
3022 goto exit_fput;
Al Viro2675a4e2012-06-22 12:41:10 +04003023 error = ima_file_check(file, op->acc_mode);
Miklos Szerediaa4caad2012-06-05 15:10:28 +02003024 if (error)
3025 goto exit_fput;
3026
3027 if (will_truncate) {
Al Viro2675a4e2012-06-22 12:41:10 +04003028 error = handle_truncate(file);
Miklos Szerediaa4caad2012-06-05 15:10:28 +02003029 if (error)
3030 goto exit_fput;
Al Viro0f9d1a12011-03-09 00:13:14 -05003031 }
Al Viroca344a892011-03-09 00:36:45 -05003032out:
Al Viro64894cf2012-07-31 00:53:35 +04003033 if (got_write)
Al Viro0f9d1a12011-03-09 00:13:14 -05003034 mnt_drop_write(nd->path.mnt);
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003035 path_put(&save_parent);
Miklos Szeredie276ae62012-05-21 17:30:06 +02003036 terminate_walk(nd);
Al Viro2675a4e2012-06-22 12:41:10 +04003037 return error;
Al Virofb1cc552009-12-24 01:58:28 -05003038
Al Virofb1cc552009-12-24 01:58:28 -05003039exit_dput:
3040 path_put_conditional(path, nd);
Al Viroca344a892011-03-09 00:36:45 -05003041 goto out;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003042exit_fput:
Al Viro2675a4e2012-06-22 12:41:10 +04003043 fput(file);
3044 goto out;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003045
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003046stale_open:
3047 /* If no saved parent or already retried then can't retry */
3048 if (!save_parent.dentry || retried)
3049 goto out;
3050
3051 BUG_ON(save_parent.dentry != dir);
3052 path_put(&nd->path);
3053 nd->path = save_parent;
3054 nd->inode = dir->d_inode;
3055 save_parent.mnt = NULL;
3056 save_parent.dentry = NULL;
Al Viro64894cf2012-07-31 00:53:35 +04003057 if (got_write) {
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003058 mnt_drop_write(nd->path.mnt);
Al Viro64894cf2012-07-31 00:53:35 +04003059 got_write = false;
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003060 }
3061 retried = true;
3062 goto retry_lookup;
Al Virofb1cc552009-12-24 01:58:28 -05003063}
3064
Al Viro60545d02013-06-07 01:20:27 -04003065static int do_tmpfile(int dfd, struct filename *pathname,
3066 struct nameidata *nd, int flags,
3067 const struct open_flags *op,
3068 struct file *file, int *opened)
3069{
3070 static const struct qstr name = QSTR_INIT("/", 1);
3071 struct dentry *dentry, *child;
3072 struct inode *dir;
3073 int error = path_lookupat(dfd, pathname->name,
3074 flags | LOOKUP_DIRECTORY, nd);
3075 if (unlikely(error))
3076 return error;
3077 error = mnt_want_write(nd->path.mnt);
3078 if (unlikely(error))
3079 goto out;
3080 /* we want directory to be writable */
3081 error = inode_permission(nd->inode, MAY_WRITE | MAY_EXEC);
3082 if (error)
3083 goto out2;
3084 dentry = nd->path.dentry;
3085 dir = dentry->d_inode;
3086 if (!dir->i_op->tmpfile) {
3087 error = -EOPNOTSUPP;
3088 goto out2;
3089 }
3090 child = d_alloc(dentry, &name);
3091 if (unlikely(!child)) {
3092 error = -ENOMEM;
3093 goto out2;
3094 }
3095 nd->flags &= ~LOOKUP_DIRECTORY;
3096 nd->flags |= op->intent;
3097 dput(nd->path.dentry);
3098 nd->path.dentry = child;
3099 error = dir->i_op->tmpfile(dir, nd->path.dentry, op->mode);
3100 if (error)
3101 goto out2;
3102 audit_inode(pathname, nd->path.dentry, 0);
3103 error = may_open(&nd->path, op->acc_mode, op->open_flag);
3104 if (error)
3105 goto out2;
3106 file->f_path.mnt = nd->path.mnt;
3107 error = finish_open(file, nd->path.dentry, NULL, opened);
3108 if (error)
3109 goto out2;
3110 error = open_check_o_direct(file);
Al Virof4e0c302013-06-11 08:34:36 +04003111 if (error) {
Al Viro60545d02013-06-07 01:20:27 -04003112 fput(file);
Al Virof4e0c302013-06-11 08:34:36 +04003113 } else if (!(op->open_flag & O_EXCL)) {
3114 struct inode *inode = file_inode(file);
3115 spin_lock(&inode->i_lock);
3116 inode->i_state |= I_LINKABLE;
3117 spin_unlock(&inode->i_lock);
3118 }
Al Viro60545d02013-06-07 01:20:27 -04003119out2:
3120 mnt_drop_write(nd->path.mnt);
3121out:
3122 path_put(&nd->path);
3123 return error;
3124}
3125
Jeff Layton669abf42012-10-10 16:43:10 -04003126static struct file *path_openat(int dfd, struct filename *pathname,
Al Viro73d049a2011-03-11 12:08:24 -05003127 struct nameidata *nd, const struct open_flags *op, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128{
Al Virofe2d35f2011-03-05 22:58:25 -05003129 struct file *base = NULL;
Al Viro30d90492012-06-22 12:40:19 +04003130 struct file *file;
Al Viro9850c052010-01-13 15:01:15 -05003131 struct path path;
Al Viro47237682012-06-10 05:01:45 -04003132 int opened = 0;
Al Viro13aab422011-02-23 17:54:08 -05003133 int error;
Nick Piggin31e6b012011-01-07 17:49:52 +11003134
Al Viro30d90492012-06-22 12:40:19 +04003135 file = get_empty_filp();
Al Viro1afc99b2013-02-14 20:41:04 -05003136 if (IS_ERR(file))
3137 return file;
Nick Piggin31e6b012011-01-07 17:49:52 +11003138
Al Viro30d90492012-06-22 12:40:19 +04003139 file->f_flags = op->open_flag;
Nick Piggin31e6b012011-01-07 17:49:52 +11003140
Al Virobb458c62013-07-13 13:26:37 +04003141 if (unlikely(file->f_flags & __O_TMPFILE)) {
Al Viro60545d02013-06-07 01:20:27 -04003142 error = do_tmpfile(dfd, pathname, nd, flags, op, file, &opened);
3143 goto out;
3144 }
3145
Jeff Layton669abf42012-10-10 16:43:10 -04003146 error = path_init(dfd, pathname->name, flags | LOOKUP_PARENT, nd, &base);
Nick Piggin31e6b012011-01-07 17:49:52 +11003147 if (unlikely(error))
Al Viro2675a4e2012-06-22 12:41:10 +04003148 goto out;
Nick Piggin31e6b012011-01-07 17:49:52 +11003149
Al Virofe2d35f2011-03-05 22:58:25 -05003150 current->total_link_count = 0;
Jeff Layton669abf42012-10-10 16:43:10 -04003151 error = link_path_walk(pathname->name, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11003152 if (unlikely(error))
Al Viro2675a4e2012-06-22 12:41:10 +04003153 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154
Al Viro2675a4e2012-06-22 12:41:10 +04003155 error = do_last(nd, &path, file, op, &opened, pathname);
3156 while (unlikely(error > 0)) { /* trailing symlink */
Nick Piggin7b9337a2011-01-14 08:42:43 +00003157 struct path link = path;
Al Virodef4af32009-12-26 08:37:05 -05003158 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04003159 if (!(nd->flags & LOOKUP_FOLLOW)) {
Al Viro73d049a2011-03-11 12:08:24 -05003160 path_put_conditional(&path, nd);
3161 path_put(&nd->path);
Al Viro2675a4e2012-06-22 12:41:10 +04003162 error = -ELOOP;
Al Viro40b39132011-03-09 16:22:18 -05003163 break;
3164 }
Kees Cook800179c2012-07-25 17:29:07 -07003165 error = may_follow_link(&link, nd);
3166 if (unlikely(error))
3167 break;
Al Viro73d049a2011-03-11 12:08:24 -05003168 nd->flags |= LOOKUP_PARENT;
3169 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
Al Viro574197e2011-03-14 22:20:34 -04003170 error = follow_link(&link, nd, &cookie);
Al Viroc3e380b2011-02-23 13:39:45 -05003171 if (unlikely(error))
Al Viro2675a4e2012-06-22 12:41:10 +04003172 break;
3173 error = do_last(nd, &path, file, op, &opened, pathname);
Al Viro574197e2011-03-14 22:20:34 -04003174 put_link(nd, &link, cookie);
Al Viro806b6812009-12-26 07:16:40 -05003175 }
Al Viro10fa8e62009-12-26 07:09:49 -05003176out:
Al Viro73d049a2011-03-11 12:08:24 -05003177 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
3178 path_put(&nd->root);
Al Virofe2d35f2011-03-05 22:58:25 -05003179 if (base)
3180 fput(base);
Al Viro2675a4e2012-06-22 12:41:10 +04003181 if (!(opened & FILE_OPENED)) {
3182 BUG_ON(!error);
Al Viro30d90492012-06-22 12:40:19 +04003183 put_filp(file);
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003184 }
Al Viro2675a4e2012-06-22 12:41:10 +04003185 if (unlikely(error)) {
3186 if (error == -EOPENSTALE) {
3187 if (flags & LOOKUP_RCU)
3188 error = -ECHILD;
3189 else
3190 error = -ESTALE;
3191 }
3192 file = ERR_PTR(error);
3193 }
3194 return file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195}
3196
Jeff Layton669abf42012-10-10 16:43:10 -04003197struct file *do_filp_open(int dfd, struct filename *pathname,
Al Virof9652e12013-06-11 08:23:01 +04003198 const struct open_flags *op)
Al Viro13aab422011-02-23 17:54:08 -05003199{
Al Viro73d049a2011-03-11 12:08:24 -05003200 struct nameidata nd;
Al Virof9652e12013-06-11 08:23:01 +04003201 int flags = op->lookup_flags;
Al Viro13aab422011-02-23 17:54:08 -05003202 struct file *filp;
3203
Al Viro73d049a2011-03-11 12:08:24 -05003204 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
Al Viro13aab422011-02-23 17:54:08 -05003205 if (unlikely(filp == ERR_PTR(-ECHILD)))
Al Viro73d049a2011-03-11 12:08:24 -05003206 filp = path_openat(dfd, pathname, &nd, op, flags);
Al Viro13aab422011-02-23 17:54:08 -05003207 if (unlikely(filp == ERR_PTR(-ESTALE)))
Al Viro73d049a2011-03-11 12:08:24 -05003208 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
Al Viro13aab422011-02-23 17:54:08 -05003209 return filp;
3210}
3211
Al Viro73d049a2011-03-11 12:08:24 -05003212struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
Al Virof9652e12013-06-11 08:23:01 +04003213 const char *name, const struct open_flags *op)
Al Viro73d049a2011-03-11 12:08:24 -05003214{
3215 struct nameidata nd;
3216 struct file *file;
Jeff Layton669abf42012-10-10 16:43:10 -04003217 struct filename filename = { .name = name };
Al Virof9652e12013-06-11 08:23:01 +04003218 int flags = op->lookup_flags | LOOKUP_ROOT;
Al Viro73d049a2011-03-11 12:08:24 -05003219
3220 nd.root.mnt = mnt;
3221 nd.root.dentry = dentry;
3222
David Howellsb18825a2013-09-12 19:22:53 +01003223 if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN)
Al Viro73d049a2011-03-11 12:08:24 -05003224 return ERR_PTR(-ELOOP);
3225
Jeff Layton669abf42012-10-10 16:43:10 -04003226 file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_RCU);
Al Viro73d049a2011-03-11 12:08:24 -05003227 if (unlikely(file == ERR_PTR(-ECHILD)))
Jeff Layton669abf42012-10-10 16:43:10 -04003228 file = path_openat(-1, &filename, &nd, op, flags);
Al Viro73d049a2011-03-11 12:08:24 -05003229 if (unlikely(file == ERR_PTR(-ESTALE)))
Jeff Layton669abf42012-10-10 16:43:10 -04003230 file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_REVAL);
Al Viro73d049a2011-03-11 12:08:24 -05003231 return file;
3232}
3233
Jeff Layton1ac12b42012-12-11 12:10:06 -05003234struct dentry *kern_path_create(int dfd, const char *pathname,
3235 struct path *path, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003237 struct dentry *dentry = ERR_PTR(-EEXIST);
Al Viroed75e952011-06-27 16:53:43 -04003238 struct nameidata nd;
Jan Karac30dabf2012-06-12 16:20:30 +02003239 int err2;
Jeff Layton1ac12b42012-12-11 12:10:06 -05003240 int error;
3241 bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
3242
3243 /*
3244 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
3245 * other flags passed in are ignored!
3246 */
3247 lookup_flags &= LOOKUP_REVAL;
3248
3249 error = do_path_lookup(dfd, pathname, LOOKUP_PARENT|lookup_flags, &nd);
Al Viroed75e952011-06-27 16:53:43 -04003250 if (error)
3251 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003253 /*
3254 * Yucky last component or no last component at all?
3255 * (foo/., foo/.., /////)
3256 */
Al Viroed75e952011-06-27 16:53:43 -04003257 if (nd.last_type != LAST_NORM)
3258 goto out;
3259 nd.flags &= ~LOOKUP_PARENT;
3260 nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003261
Jan Karac30dabf2012-06-12 16:20:30 +02003262 /* don't fail immediately if it's r/o, at least try to report other errors */
3263 err2 = mnt_want_write(nd.path.mnt);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003264 /*
3265 * Do the final lookup.
3266 */
Al Viroed75e952011-06-27 16:53:43 -04003267 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3268 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 if (IS_ERR(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003270 goto unlock;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003271
Al Viroa8104a92012-07-20 02:25:00 +04003272 error = -EEXIST;
David Howellsb18825a2013-09-12 19:22:53 +01003273 if (d_is_positive(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003274 goto fail;
David Howellsb18825a2013-09-12 19:22:53 +01003275
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003276 /*
3277 * Special case - lookup gave negative, but... we had foo/bar/
3278 * From the vfs_mknod() POV we just have a negative dentry -
3279 * all is fine. Let's be bastards - you had / on the end, you've
3280 * been asking for (non-existent) directory. -ENOENT for you.
3281 */
Al Viroed75e952011-06-27 16:53:43 -04003282 if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
Al Viroa8104a92012-07-20 02:25:00 +04003283 error = -ENOENT;
Al Viroed75e952011-06-27 16:53:43 -04003284 goto fail;
Al Viroe9baf6e2008-05-15 04:49:12 -04003285 }
Jan Karac30dabf2012-06-12 16:20:30 +02003286 if (unlikely(err2)) {
3287 error = err2;
Al Viroa8104a92012-07-20 02:25:00 +04003288 goto fail;
Jan Karac30dabf2012-06-12 16:20:30 +02003289 }
Al Viroed75e952011-06-27 16:53:43 -04003290 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292fail:
Al Viroa8104a92012-07-20 02:25:00 +04003293 dput(dentry);
3294 dentry = ERR_PTR(error);
3295unlock:
Al Viroed75e952011-06-27 16:53:43 -04003296 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Karac30dabf2012-06-12 16:20:30 +02003297 if (!err2)
3298 mnt_drop_write(nd.path.mnt);
Al Viroed75e952011-06-27 16:53:43 -04003299out:
3300 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 return dentry;
3302}
Al Virodae6ad82011-06-26 11:50:15 -04003303EXPORT_SYMBOL(kern_path_create);
3304
Al Viro921a1652012-07-20 01:15:31 +04003305void done_path_create(struct path *path, struct dentry *dentry)
3306{
3307 dput(dentry);
3308 mutex_unlock(&path->dentry->d_inode->i_mutex);
Al Viroa8104a92012-07-20 02:25:00 +04003309 mnt_drop_write(path->mnt);
Al Viro921a1652012-07-20 01:15:31 +04003310 path_put(path);
3311}
3312EXPORT_SYMBOL(done_path_create);
3313
Jeff Layton1ac12b42012-12-11 12:10:06 -05003314struct dentry *user_path_create(int dfd, const char __user *pathname,
3315 struct path *path, unsigned int lookup_flags)
Al Virodae6ad82011-06-26 11:50:15 -04003316{
Jeff Layton91a27b22012-10-10 15:25:28 -04003317 struct filename *tmp = getname(pathname);
Al Virodae6ad82011-06-26 11:50:15 -04003318 struct dentry *res;
3319 if (IS_ERR(tmp))
3320 return ERR_CAST(tmp);
Jeff Layton1ac12b42012-12-11 12:10:06 -05003321 res = kern_path_create(dfd, tmp->name, path, lookup_flags);
Al Virodae6ad82011-06-26 11:50:15 -04003322 putname(tmp);
3323 return res;
3324}
3325EXPORT_SYMBOL(user_path_create);
3326
Al Viro1a67aaf2011-07-26 01:52:52 -04003327int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003329 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330
3331 if (error)
3332 return error;
3333
Eric W. Biederman975d6b32011-11-13 12:16:43 -08003334 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 return -EPERM;
3336
Al Viroacfa4382008-12-04 10:06:33 -05003337 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 return -EPERM;
3339
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07003340 error = devcgroup_inode_mknod(mode, dev);
3341 if (error)
3342 return error;
3343
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 error = security_inode_mknod(dir, dentry, mode, dev);
3345 if (error)
3346 return error;
3347
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003349 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003350 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 return error;
3352}
3353
Al Virof69aac02011-07-26 04:31:40 -04003354static int may_mknod(umode_t mode)
Dave Hansen463c3192008-02-15 14:37:57 -08003355{
3356 switch (mode & S_IFMT) {
3357 case S_IFREG:
3358 case S_IFCHR:
3359 case S_IFBLK:
3360 case S_IFIFO:
3361 case S_IFSOCK:
3362 case 0: /* zero mode translates to S_IFREG */
3363 return 0;
3364 case S_IFDIR:
3365 return -EPERM;
3366 default:
3367 return -EINVAL;
3368 }
3369}
3370
Al Viro8208a222011-07-25 17:32:17 -04003371SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003372 unsigned, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373{
Al Viro2ad94ae2008-07-21 09:32:51 -04003374 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003375 struct path path;
3376 int error;
Jeff Layton972567f2012-12-20 16:00:10 -05003377 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378
Al Viro8e4bfca2012-07-20 01:17:26 +04003379 error = may_mknod(mode);
3380 if (error)
3381 return error;
Jeff Layton972567f2012-12-20 16:00:10 -05003382retry:
3383 dentry = user_path_create(dfd, filename, &path, lookup_flags);
Al Virodae6ad82011-06-26 11:50:15 -04003384 if (IS_ERR(dentry))
3385 return PTR_ERR(dentry);
Al Viro2ad94ae2008-07-21 09:32:51 -04003386
Al Virodae6ad82011-06-26 11:50:15 -04003387 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003388 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003389 error = security_path_mknod(&path, dentry, mode, dev);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003390 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04003391 goto out;
Dave Hansen463c3192008-02-15 14:37:57 -08003392 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 case 0: case S_IFREG:
Al Viro312b63f2012-06-10 18:09:36 -04003394 error = vfs_create(path.dentry->d_inode,dentry,mode,true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 break;
3396 case S_IFCHR: case S_IFBLK:
Al Virodae6ad82011-06-26 11:50:15 -04003397 error = vfs_mknod(path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 new_decode_dev(dev));
3399 break;
3400 case S_IFIFO: case S_IFSOCK:
Al Virodae6ad82011-06-26 11:50:15 -04003401 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 }
Al Viroa8104a92012-07-20 02:25:00 +04003404out:
Al Viro921a1652012-07-20 01:15:31 +04003405 done_path_create(&path, dentry);
Jeff Layton972567f2012-12-20 16:00:10 -05003406 if (retry_estale(error, lookup_flags)) {
3407 lookup_flags |= LOOKUP_REVAL;
3408 goto retry;
3409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 return error;
3411}
3412
Al Viro8208a222011-07-25 17:32:17 -04003413SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003414{
3415 return sys_mknodat(AT_FDCWD, filename, mode, dev);
3416}
3417
Al Viro18bb1db2011-07-26 01:41:39 -04003418int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003420 int error = may_create(dir, dentry);
Al Viro8de52772012-02-06 12:45:27 -05003421 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422
3423 if (error)
3424 return error;
3425
Al Viroacfa4382008-12-04 10:06:33 -05003426 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 return -EPERM;
3428
3429 mode &= (S_IRWXUGO|S_ISVTX);
3430 error = security_inode_mkdir(dir, dentry, mode);
3431 if (error)
3432 return error;
3433
Al Viro8de52772012-02-06 12:45:27 -05003434 if (max_links && dir->i_nlink >= max_links)
3435 return -EMLINK;
3436
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003438 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003439 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 return error;
3441}
3442
Al Viroa218d0f2011-11-21 14:59:34 -05003443SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444{
Dave Hansen6902d922006-09-30 23:29:01 -07003445 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003446 struct path path;
3447 int error;
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003448 unsigned int lookup_flags = LOOKUP_DIRECTORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003450retry:
3451 dentry = user_path_create(dfd, pathname, &path, lookup_flags);
Dave Hansen6902d922006-09-30 23:29:01 -07003452 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003453 return PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003454
Al Virodae6ad82011-06-26 11:50:15 -04003455 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003456 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003457 error = security_path_mkdir(&path, dentry, mode);
Al Viroa8104a92012-07-20 02:25:00 +04003458 if (!error)
3459 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
Al Viro921a1652012-07-20 01:15:31 +04003460 done_path_create(&path, dentry);
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003461 if (retry_estale(error, lookup_flags)) {
3462 lookup_flags |= LOOKUP_REVAL;
3463 goto retry;
3464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 return error;
3466}
3467
Al Viroa218d0f2011-11-21 14:59:34 -05003468SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003469{
3470 return sys_mkdirat(AT_FDCWD, pathname, mode);
3471}
3472
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473/*
Sage Weila71905f2011-05-24 13:06:08 -07003474 * The dentry_unhash() helper will try to drop the dentry early: we
J. Bruce Fieldsc0d02592012-02-15 11:48:40 -05003475 * should have a usage count of 1 if we're the only user of this
Sage Weila71905f2011-05-24 13:06:08 -07003476 * dentry, and if that is true (possibly after pruning the dcache),
3477 * then we drop the dentry now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 *
3479 * A low-level filesystem can, if it choses, legally
3480 * do a
3481 *
3482 * if (!d_unhashed(dentry))
3483 * return -EBUSY;
3484 *
3485 * if it cannot handle the case of removing a directory
3486 * that is still in use by something else..
3487 */
3488void dentry_unhash(struct dentry *dentry)
3489{
Vasily Averindc168422006-12-06 20:37:07 -08003490 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 spin_lock(&dentry->d_lock);
Waiman Long98474232013-08-28 18:24:59 -07003492 if (dentry->d_lockref.count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 __d_drop(dentry);
3494 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495}
3496
3497int vfs_rmdir(struct inode *dir, struct dentry *dentry)
3498{
3499 int error = may_delete(dir, dentry, 1);
3500
3501 if (error)
3502 return error;
3503
Al Viroacfa4382008-12-04 10:06:33 -05003504 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 return -EPERM;
3506
Al Viro1d2ef592011-09-14 18:55:41 +01003507 dget(dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003508 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509
Sage Weil912dbc12011-05-24 13:06:11 -07003510 error = -EBUSY;
3511 if (d_mountpoint(dentry))
3512 goto out;
3513
3514 error = security_inode_rmdir(dir, dentry);
3515 if (error)
3516 goto out;
3517
Sage Weil3cebde22011-05-29 21:20:59 -07003518 shrink_dcache_parent(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003519 error = dir->i_op->rmdir(dir, dentry);
3520 if (error)
3521 goto out;
3522
3523 dentry->d_inode->i_flags |= S_DEAD;
3524 dont_mount(dentry);
3525
3526out:
3527 mutex_unlock(&dentry->d_inode->i_mutex);
Al Viro1d2ef592011-09-14 18:55:41 +01003528 dput(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003529 if (!error)
3530 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 return error;
3532}
3533
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003534static long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535{
3536 int error = 0;
Jeff Layton91a27b22012-10-10 15:25:28 -04003537 struct filename *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 struct dentry *dentry;
3539 struct nameidata nd;
Jeff Laytonc6ee9202012-12-20 16:28:33 -05003540 unsigned int lookup_flags = 0;
3541retry:
3542 name = user_path_parent(dfd, pathname, &nd, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04003543 if (IS_ERR(name))
3544 return PTR_ERR(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545
3546 switch(nd.last_type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003547 case LAST_DOTDOT:
3548 error = -ENOTEMPTY;
3549 goto exit1;
3550 case LAST_DOT:
3551 error = -EINVAL;
3552 goto exit1;
3553 case LAST_ROOT:
3554 error = -EBUSY;
3555 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003557
3558 nd.flags &= ~LOOKUP_PARENT;
Jan Karac30dabf2012-06-12 16:20:30 +02003559 error = mnt_want_write(nd.path.mnt);
3560 if (error)
3561 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003562
Jan Blunck4ac91372008-02-14 19:34:32 -08003563 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08003564 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003566 if (IS_ERR(dentry))
3567 goto exit2;
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04003568 if (!dentry->d_inode) {
3569 error = -ENOENT;
3570 goto exit3;
3571 }
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003572 error = security_path_rmdir(&nd.path, dentry);
3573 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02003574 goto exit3;
Jan Blunck4ac91372008-02-14 19:34:32 -08003575 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
Dave Hansen06227532008-02-15 14:37:34 -08003576exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07003577 dput(dentry);
3578exit2:
Jan Blunck4ac91372008-02-14 19:34:32 -08003579 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Karac30dabf2012-06-12 16:20:30 +02003580 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003582 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 putname(name);
Jeff Laytonc6ee9202012-12-20 16:28:33 -05003584 if (retry_estale(error, lookup_flags)) {
3585 lookup_flags |= LOOKUP_REVAL;
3586 goto retry;
3587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 return error;
3589}
3590
Heiko Carstens3cdad422009-01-14 14:14:22 +01003591SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003592{
3593 return do_rmdir(AT_FDCWD, pathname);
3594}
3595
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003596/**
3597 * vfs_unlink - unlink a filesystem object
3598 * @dir: parent directory
3599 * @dentry: victim
3600 * @delegated_inode: returns victim inode, if the inode is delegated.
3601 *
3602 * The caller must hold dir->i_mutex.
3603 *
3604 * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
3605 * return a reference to the inode in delegated_inode. The caller
3606 * should then break the delegation on that inode and retry. Because
3607 * breaking a delegation may take a long time, the caller should drop
3608 * dir->i_mutex before doing so.
3609 *
3610 * Alternatively, a caller may pass NULL for delegated_inode. This may
3611 * be appropriate for callers that expect the underlying filesystem not
3612 * to be NFS exported.
3613 */
3614int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615{
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003616 struct inode *target = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 int error = may_delete(dir, dentry, 0);
3618
3619 if (error)
3620 return error;
3621
Al Viroacfa4382008-12-04 10:06:33 -05003622 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 return -EPERM;
3624
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003625 mutex_lock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 if (d_mountpoint(dentry))
3627 error = -EBUSY;
3628 else {
3629 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05003630 if (!error) {
J. Bruce Fields5a146962012-08-28 07:50:40 -07003631 error = try_break_deleg(target, delegated_inode);
3632 if (error)
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003633 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 error = dir->i_op->unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05003635 if (!error)
Al Virod83c49f2010-04-30 17:17:09 -04003636 dont_mount(dentry);
Al Virobec10522010-03-03 14:12:08 -05003637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 }
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003639out:
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003640 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641
3642 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
3643 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003644 fsnotify_link_count(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 }
Robert Love0eeca282005-07-12 17:06:03 -04003647
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 return error;
3649}
3650
3651/*
3652 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003653 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 * writeout happening, and we don't want to prevent access to the directory
3655 * while waiting on the I/O.
3656 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003657static long do_unlinkat(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658{
Al Viro2ad94ae2008-07-21 09:32:51 -04003659 int error;
Jeff Layton91a27b22012-10-10 15:25:28 -04003660 struct filename *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 struct dentry *dentry;
3662 struct nameidata nd;
3663 struct inode *inode = NULL;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003664 struct inode *delegated_inode = NULL;
Jeff Layton5d18f812012-12-20 16:38:04 -05003665 unsigned int lookup_flags = 0;
3666retry:
3667 name = user_path_parent(dfd, pathname, &nd, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04003668 if (IS_ERR(name))
3669 return PTR_ERR(name);
Al Viro2ad94ae2008-07-21 09:32:51 -04003670
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 error = -EISDIR;
3672 if (nd.last_type != LAST_NORM)
3673 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003674
3675 nd.flags &= ~LOOKUP_PARENT;
Jan Karac30dabf2012-06-12 16:20:30 +02003676 error = mnt_want_write(nd.path.mnt);
3677 if (error)
3678 goto exit1;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003679retry_deleg:
Jan Blunck4ac91372008-02-14 19:34:32 -08003680 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08003681 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 error = PTR_ERR(dentry);
3683 if (!IS_ERR(dentry)) {
3684 /* Why not before? Because we want correct error value */
Török Edwin50338b82011-06-16 00:06:14 +03003685 if (nd.last.name[nd.last.len])
3686 goto slashes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 inode = dentry->d_inode;
David Howellsb18825a2013-09-12 19:22:53 +01003688 if (d_is_negative(dentry))
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04003689 goto slashes;
3690 ihold(inode);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003691 error = security_path_unlink(&nd.path, dentry);
3692 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02003693 goto exit2;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003694 error = vfs_unlink(nd.path.dentry->d_inode, dentry, &delegated_inode);
Jan Karac30dabf2012-06-12 16:20:30 +02003695exit2:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 dput(dentry);
3697 }
Jan Blunck4ac91372008-02-14 19:34:32 -08003698 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 if (inode)
3700 iput(inode); /* truncate the inode here */
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003701 inode = NULL;
3702 if (delegated_inode) {
J. Bruce Fields5a146962012-08-28 07:50:40 -07003703 error = break_deleg_wait(&delegated_inode);
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003704 if (!error)
3705 goto retry_deleg;
3706 }
Jan Karac30dabf2012-06-12 16:20:30 +02003707 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003709 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 putname(name);
Jeff Layton5d18f812012-12-20 16:38:04 -05003711 if (retry_estale(error, lookup_flags)) {
3712 lookup_flags |= LOOKUP_REVAL;
3713 inode = NULL;
3714 goto retry;
3715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 return error;
3717
3718slashes:
David Howellsb18825a2013-09-12 19:22:53 +01003719 if (d_is_negative(dentry))
3720 error = -ENOENT;
3721 else if (d_is_directory(dentry) || d_is_autodir(dentry))
3722 error = -EISDIR;
3723 else
3724 error = -ENOTDIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 goto exit2;
3726}
3727
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003728SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003729{
3730 if ((flag & ~AT_REMOVEDIR) != 0)
3731 return -EINVAL;
3732
3733 if (flag & AT_REMOVEDIR)
3734 return do_rmdir(dfd, pathname);
3735
3736 return do_unlinkat(dfd, pathname);
3737}
3738
Heiko Carstens3480b252009-01-14 14:14:16 +01003739SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003740{
3741 return do_unlinkat(AT_FDCWD, pathname);
3742}
3743
Miklos Szeredidb2e7472008-06-24 16:50:16 +02003744int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003746 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747
3748 if (error)
3749 return error;
3750
Al Viroacfa4382008-12-04 10:06:33 -05003751 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 return -EPERM;
3753
3754 error = security_inode_symlink(dir, dentry, oldname);
3755 if (error)
3756 return error;
3757
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003759 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003760 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 return error;
3762}
3763
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003764SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
3765 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766{
Al Viro2ad94ae2008-07-21 09:32:51 -04003767 int error;
Jeff Layton91a27b22012-10-10 15:25:28 -04003768 struct filename *from;
Dave Hansen6902d922006-09-30 23:29:01 -07003769 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003770 struct path path;
Jeff Laytonf46d3562012-12-11 12:10:08 -05003771 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772
3773 from = getname(oldname);
Al Viro2ad94ae2008-07-21 09:32:51 -04003774 if (IS_ERR(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 return PTR_ERR(from);
Jeff Laytonf46d3562012-12-11 12:10:08 -05003776retry:
3777 dentry = user_path_create(newdfd, newname, &path, lookup_flags);
Dave Hansen6902d922006-09-30 23:29:01 -07003778 error = PTR_ERR(dentry);
3779 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003780 goto out_putname;
Dave Hansen6902d922006-09-30 23:29:01 -07003781
Jeff Layton91a27b22012-10-10 15:25:28 -04003782 error = security_path_symlink(&path, dentry, from->name);
Al Viroa8104a92012-07-20 02:25:00 +04003783 if (!error)
Jeff Layton91a27b22012-10-10 15:25:28 -04003784 error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
Al Viro921a1652012-07-20 01:15:31 +04003785 done_path_create(&path, dentry);
Jeff Laytonf46d3562012-12-11 12:10:08 -05003786 if (retry_estale(error, lookup_flags)) {
3787 lookup_flags |= LOOKUP_REVAL;
3788 goto retry;
3789 }
Dave Hansen6902d922006-09-30 23:29:01 -07003790out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 putname(from);
3792 return error;
3793}
3794
Heiko Carstens3480b252009-01-14 14:14:16 +01003795SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003796{
3797 return sys_symlinkat(oldname, AT_FDCWD, newname);
3798}
3799
J. Bruce Fields146a8592011-09-20 17:14:31 -04003800/**
3801 * vfs_link - create a new link
3802 * @old_dentry: object to be linked
3803 * @dir: new parent
3804 * @new_dentry: where to create the new link
3805 * @delegated_inode: returns inode needing a delegation break
3806 *
3807 * The caller must hold dir->i_mutex
3808 *
3809 * If vfs_link discovers a delegation on the to-be-linked file in need
3810 * of breaking, it will return -EWOULDBLOCK and return a reference to the
3811 * inode in delegated_inode. The caller should then break the delegation
3812 * and retry. Because breaking a delegation may take a long time, the
3813 * caller should drop the i_mutex before doing so.
3814 *
3815 * Alternatively, a caller may pass NULL for delegated_inode. This may
3816 * be appropriate for callers that expect the underlying filesystem not
3817 * to be NFS exported.
3818 */
3819int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820{
3821 struct inode *inode = old_dentry->d_inode;
Al Viro8de52772012-02-06 12:45:27 -05003822 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 int error;
3824
3825 if (!inode)
3826 return -ENOENT;
3827
Miklos Szeredia95164d2008-07-30 15:08:48 +02003828 error = may_create(dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 if (error)
3830 return error;
3831
3832 if (dir->i_sb != inode->i_sb)
3833 return -EXDEV;
3834
3835 /*
3836 * A link to an append-only or immutable file cannot be created.
3837 */
3838 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3839 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05003840 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003842 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 return -EPERM;
3844
3845 error = security_inode_link(old_dentry, dir, new_dentry);
3846 if (error)
3847 return error;
3848
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003849 mutex_lock(&inode->i_mutex);
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05303850 /* Make sure we don't allow creating hardlink to an unlinked file */
Al Virof4e0c302013-06-11 08:34:36 +04003851 if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05303852 error = -ENOENT;
Al Viro8de52772012-02-06 12:45:27 -05003853 else if (max_links && inode->i_nlink >= max_links)
3854 error = -EMLINK;
J. Bruce Fields146a8592011-09-20 17:14:31 -04003855 else {
3856 error = try_break_deleg(inode, delegated_inode);
3857 if (!error)
3858 error = dir->i_op->link(old_dentry, dir, new_dentry);
3859 }
Al Virof4e0c302013-06-11 08:34:36 +04003860
3861 if (!error && (inode->i_state & I_LINKABLE)) {
3862 spin_lock(&inode->i_lock);
3863 inode->i_state &= ~I_LINKABLE;
3864 spin_unlock(&inode->i_lock);
3865 }
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003866 mutex_unlock(&inode->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07003867 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003868 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 return error;
3870}
3871
3872/*
3873 * Hardlinks are often used in delicate situations. We avoid
3874 * security-related surprises by not following symlinks on the
3875 * newname. --KAB
3876 *
3877 * We don't follow them on the oldname either to be compatible
3878 * with linux 2.0, and to avoid hard-linking to directories
3879 * and other special files. --ADM
3880 */
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003881SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
3882 int, newdfd, const char __user *, newname, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883{
3884 struct dentry *new_dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003885 struct path old_path, new_path;
J. Bruce Fields146a8592011-09-20 17:14:31 -04003886 struct inode *delegated_inode = NULL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303887 int how = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303890 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003891 return -EINVAL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303892 /*
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07003893 * To use null names we require CAP_DAC_READ_SEARCH
3894 * This ensures that not everyone will be able to create
3895 * handlink using the passed filedescriptor.
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303896 */
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07003897 if (flags & AT_EMPTY_PATH) {
3898 if (!capable(CAP_DAC_READ_SEARCH))
3899 return -ENOENT;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303900 how = LOOKUP_EMPTY;
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07003901 }
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003902
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303903 if (flags & AT_SYMLINK_FOLLOW)
3904 how |= LOOKUP_FOLLOW;
Jeff Layton442e31c2012-12-20 16:15:38 -05003905retry:
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303906 error = user_path_at(olddfd, oldname, how, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04003908 return error;
3909
Jeff Layton442e31c2012-12-20 16:15:38 -05003910 new_dentry = user_path_create(newdfd, newname, &new_path,
3911 (how & LOOKUP_REVAL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003913 if (IS_ERR(new_dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003914 goto out;
3915
3916 error = -EXDEV;
3917 if (old_path.mnt != new_path.mnt)
3918 goto out_dput;
Kees Cook800179c2012-07-25 17:29:07 -07003919 error = may_linkat(&old_path);
3920 if (unlikely(error))
3921 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04003922 error = security_path_link(old_path.dentry, &new_path, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003923 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04003924 goto out_dput;
J. Bruce Fields146a8592011-09-20 17:14:31 -04003925 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode);
Dave Hansen75c3f292008-02-15 14:37:45 -08003926out_dput:
Al Viro921a1652012-07-20 01:15:31 +04003927 done_path_create(&new_path, new_dentry);
J. Bruce Fields146a8592011-09-20 17:14:31 -04003928 if (delegated_inode) {
3929 error = break_deleg_wait(&delegated_inode);
3930 if (!error)
3931 goto retry;
3932 }
Jeff Layton442e31c2012-12-20 16:15:38 -05003933 if (retry_estale(error, how)) {
3934 how |= LOOKUP_REVAL;
3935 goto retry;
3936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937out:
Al Viro2d8f3032008-07-22 09:59:21 -04003938 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939
3940 return error;
3941}
3942
Heiko Carstens3480b252009-01-14 14:14:16 +01003943SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003944{
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003945 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003946}
3947
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948/*
3949 * The worst of all namespace operations - renaming directory. "Perverted"
3950 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
3951 * Problems:
3952 * a) we can get into loop creation. Check is done in is_subdir().
3953 * b) race potential - two innocent renames can create a loop together.
3954 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08003955 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 * story.
J. Bruce Fields6cedba82012-03-05 11:40:41 -05003957 * c) we have to lock _four_ objects - parents and victim (if it exists),
3958 * and source (if it is not a directory).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003959 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 * whether the target exists). Solution: try to be smart with locking
3961 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08003962 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 * move will be locked. Thus we can rank directories by the tree
3964 * (ancestors first) and rank all non-directories after them.
3965 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08003966 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 * HOWEVER, it relies on the assumption that any object with ->lookup()
3968 * has no more than 1 dentry. If "hybrid" objects will ever appear,
3969 * we'd better make sure that there's no link(2) for them.
Sage Weile4eaac02011-05-24 13:06:07 -07003970 * d) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003971 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05003973 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 * locking].
3975 */
Adrian Bunk75c96f82005-05-05 16:16:09 -07003976static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
3977 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978{
3979 int error = 0;
Sage Weil9055cba2011-05-24 13:06:12 -07003980 struct inode *target = new_dentry->d_inode;
Al Viro8de52772012-02-06 12:45:27 -05003981 unsigned max_links = new_dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982
3983 /*
3984 * If we are going to change the parent - check write permissions,
3985 * we'll need to flip '..'.
3986 */
3987 if (new_dir != old_dir) {
Al Virof419a2e2008-07-22 00:07:17 -04003988 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 if (error)
3990 return error;
3991 }
3992
3993 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3994 if (error)
3995 return error;
3996
Al Viro1d2ef592011-09-14 18:55:41 +01003997 dget(new_dentry);
Al Virod83c49f2010-04-30 17:17:09 -04003998 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003999 mutex_lock(&target->i_mutex);
Sage Weil9055cba2011-05-24 13:06:12 -07004000
4001 error = -EBUSY;
4002 if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
4003 goto out;
4004
Al Viro8de52772012-02-06 12:45:27 -05004005 error = -EMLINK;
4006 if (max_links && !target && new_dir != old_dir &&
4007 new_dir->i_nlink >= max_links)
4008 goto out;
4009
Sage Weil3cebde22011-05-29 21:20:59 -07004010 if (target)
4011 shrink_dcache_parent(new_dentry);
Sage Weil9055cba2011-05-24 13:06:12 -07004012 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
4013 if (error)
4014 goto out;
4015
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 if (target) {
Sage Weil9055cba2011-05-24 13:06:12 -07004017 target->i_flags |= S_DEAD;
4018 dont_mount(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 }
Sage Weil9055cba2011-05-24 13:06:12 -07004020out:
4021 if (target)
4022 mutex_unlock(&target->i_mutex);
Al Viro1d2ef592011-09-14 18:55:41 +01004023 dput(new_dentry);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07004024 if (!error)
Mark Fasheh349457c2006-09-08 14:22:21 -07004025 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
4026 d_move(old_dentry,new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 return error;
4028}
4029
Adrian Bunk75c96f82005-05-05 16:16:09 -07004030static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004031 struct inode *new_dir, struct dentry *new_dentry,
4032 struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033{
Sage Weil51892bb2011-05-24 13:06:13 -07004034 struct inode *target = new_dentry->d_inode;
J. Bruce Fields6cedba82012-03-05 11:40:41 -05004035 struct inode *source = old_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 int error;
4037
4038 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
4039 if (error)
4040 return error;
4041
4042 dget(new_dentry);
J. Bruce Fields6cedba82012-03-05 11:40:41 -05004043 lock_two_nondirectories(source, target);
Sage Weil51892bb2011-05-24 13:06:13 -07004044
4045 error = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
Sage Weil51892bb2011-05-24 13:06:13 -07004047 goto out;
4048
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004049 error = try_break_deleg(source, delegated_inode);
4050 if (error)
4051 goto out;
4052 if (target) {
4053 error = try_break_deleg(target, delegated_inode);
4054 if (error)
4055 goto out;
4056 }
Sage Weil51892bb2011-05-24 13:06:13 -07004057 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
4058 if (error)
4059 goto out;
4060
4061 if (target)
4062 dont_mount(new_dentry);
4063 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
4064 d_move(old_dentry, new_dentry);
4065out:
J. Bruce Fields6cedba82012-03-05 11:40:41 -05004066 unlock_two_nondirectories(source, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 dput(new_dentry);
4068 return error;
4069}
4070
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004071/**
4072 * vfs_rename - rename a filesystem object
4073 * @old_dir: parent of source
4074 * @old_dentry: source
4075 * @new_dir: parent of destination
4076 * @new_dentry: destination
4077 * @delegated_inode: returns an inode needing a delegation break
4078 *
4079 * The caller must hold multiple mutexes--see lock_rename()).
4080 *
4081 * If vfs_rename discovers a delegation in need of breaking at either
4082 * the source or destination, it will return -EWOULDBLOCK and return a
4083 * reference to the inode in delegated_inode. The caller should then
4084 * break the delegation and retry. Because breaking a delegation may
4085 * take a long time, the caller should drop all locks before doing
4086 * so.
4087 *
4088 * Alternatively, a caller may pass NULL for delegated_inode. This may
4089 * be appropriate for callers that expect the underlying filesystem not
4090 * to be NFS exported.
4091 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004093 struct inode *new_dir, struct dentry *new_dentry,
4094 struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095{
4096 int error;
David Howellsb18825a2013-09-12 19:22:53 +01004097 int is_dir = d_is_directory(old_dentry) || d_is_autodir(old_dentry);
Eric Paris59b0df22010-02-08 12:53:52 -05004098 const unsigned char *old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099
4100 if (old_dentry->d_inode == new_dentry->d_inode)
4101 return 0;
4102
4103 error = may_delete(old_dir, old_dentry, is_dir);
4104 if (error)
4105 return error;
4106
4107 if (!new_dentry->d_inode)
Miklos Szeredia95164d2008-07-30 15:08:48 +02004108 error = may_create(new_dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 else
4110 error = may_delete(new_dir, new_dentry, is_dir);
4111 if (error)
4112 return error;
4113
Al Viroacfa4382008-12-04 10:06:33 -05004114 if (!old_dir->i_op->rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 return -EPERM;
4116
Robert Love0eeca282005-07-12 17:06:03 -04004117 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
4118
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 if (is_dir)
4120 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
4121 else
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004122 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry,delegated_inode);
Al Viro123df292009-12-25 04:57:57 -05004123 if (!error)
4124 fsnotify_move(old_dir, new_dir, old_name, is_dir,
Al Viro5a190ae2007-06-07 12:19:32 -04004125 new_dentry->d_inode, old_dentry);
Robert Love0eeca282005-07-12 17:06:03 -04004126 fsnotify_oldname_free(old_name);
4127
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 return error;
4129}
4130
Heiko Carstens2e4d0922009-01-14 14:14:31 +01004131SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4132 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133{
Al Viro2ad94ae2008-07-21 09:32:51 -04004134 struct dentry *old_dir, *new_dir;
4135 struct dentry *old_dentry, *new_dentry;
4136 struct dentry *trap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 struct nameidata oldnd, newnd;
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004138 struct inode *delegated_inode = NULL;
Jeff Layton91a27b22012-10-10 15:25:28 -04004139 struct filename *from;
4140 struct filename *to;
Jeff Laytonc6a94282012-12-11 12:10:10 -05004141 unsigned int lookup_flags = 0;
4142 bool should_retry = false;
Al Viro2ad94ae2008-07-21 09:32:51 -04004143 int error;
Jeff Laytonc6a94282012-12-11 12:10:10 -05004144retry:
4145 from = user_path_parent(olddfd, oldname, &oldnd, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04004146 if (IS_ERR(from)) {
4147 error = PTR_ERR(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 goto exit;
Jeff Layton91a27b22012-10-10 15:25:28 -04004149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150
Jeff Laytonc6a94282012-12-11 12:10:10 -05004151 to = user_path_parent(newdfd, newname, &newnd, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04004152 if (IS_ERR(to)) {
4153 error = PTR_ERR(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 goto exit1;
Jeff Layton91a27b22012-10-10 15:25:28 -04004155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156
4157 error = -EXDEV;
Jan Blunck4ac91372008-02-14 19:34:32 -08004158 if (oldnd.path.mnt != newnd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 goto exit2;
4160
Jan Blunck4ac91372008-02-14 19:34:32 -08004161 old_dir = oldnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 error = -EBUSY;
4163 if (oldnd.last_type != LAST_NORM)
4164 goto exit2;
4165
Jan Blunck4ac91372008-02-14 19:34:32 -08004166 new_dir = newnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 if (newnd.last_type != LAST_NORM)
4168 goto exit2;
4169
Jan Karac30dabf2012-06-12 16:20:30 +02004170 error = mnt_want_write(oldnd.path.mnt);
4171 if (error)
4172 goto exit2;
4173
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004174 oldnd.flags &= ~LOOKUP_PARENT;
4175 newnd.flags &= ~LOOKUP_PARENT;
OGAWA Hirofumi4e9ed2f2008-10-16 07:50:29 +09004176 newnd.flags |= LOOKUP_RENAME_TARGET;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004177
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004178retry_deleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 trap = lock_rename(new_dir, old_dir);
4180
Christoph Hellwig49705b72005-11-08 21:35:06 -08004181 old_dentry = lookup_hash(&oldnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 error = PTR_ERR(old_dentry);
4183 if (IS_ERR(old_dentry))
4184 goto exit3;
4185 /* source must exist */
4186 error = -ENOENT;
David Howellsb18825a2013-09-12 19:22:53 +01004187 if (d_is_negative(old_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 goto exit4;
4189 /* unless the source is a directory trailing slashes give -ENOTDIR */
David Howellsb18825a2013-09-12 19:22:53 +01004190 if (!d_is_directory(old_dentry) && !d_is_autodir(old_dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 error = -ENOTDIR;
4192 if (oldnd.last.name[oldnd.last.len])
4193 goto exit4;
4194 if (newnd.last.name[newnd.last.len])
4195 goto exit4;
4196 }
4197 /* source should not be ancestor of target */
4198 error = -EINVAL;
4199 if (old_dentry == trap)
4200 goto exit4;
Christoph Hellwig49705b72005-11-08 21:35:06 -08004201 new_dentry = lookup_hash(&newnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 error = PTR_ERR(new_dentry);
4203 if (IS_ERR(new_dentry))
4204 goto exit4;
4205 /* target should not be an ancestor of source */
4206 error = -ENOTEMPTY;
4207 if (new_dentry == trap)
4208 goto exit5;
4209
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004210 error = security_path_rename(&oldnd.path, old_dentry,
4211 &newnd.path, new_dentry);
4212 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02004213 goto exit5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 error = vfs_rename(old_dir->d_inode, old_dentry,
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004215 new_dir->d_inode, new_dentry,
4216 &delegated_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217exit5:
4218 dput(new_dentry);
4219exit4:
4220 dput(old_dentry);
4221exit3:
4222 unlock_rename(new_dir, old_dir);
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004223 if (delegated_inode) {
4224 error = break_deleg_wait(&delegated_inode);
4225 if (!error)
4226 goto retry_deleg;
4227 }
Jan Karac30dabf2012-06-12 16:20:30 +02004228 mnt_drop_write(oldnd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229exit2:
Jeff Laytonc6a94282012-12-11 12:10:10 -05004230 if (retry_estale(error, lookup_flags))
4231 should_retry = true;
Jan Blunck1d957f92008-02-14 19:34:35 -08004232 path_put(&newnd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04004233 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08004235 path_put(&oldnd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 putname(from);
Jeff Laytonc6a94282012-12-11 12:10:10 -05004237 if (should_retry) {
4238 should_retry = false;
4239 lookup_flags |= LOOKUP_REVAL;
4240 goto retry;
4241 }
Al Viro2ad94ae2008-07-21 09:32:51 -04004242exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 return error;
4244}
4245
Heiko Carstensa26eab22009-01-14 14:14:17 +01004246SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004247{
4248 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
4249}
4250
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
4252{
4253 int len;
4254
4255 len = PTR_ERR(link);
4256 if (IS_ERR(link))
4257 goto out;
4258
4259 len = strlen(link);
4260 if (len > (unsigned) buflen)
4261 len = buflen;
4262 if (copy_to_user(buffer, link, len))
4263 len = -EFAULT;
4264out:
4265 return len;
4266}
4267
4268/*
4269 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
4270 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
4271 * using) it for any given inode is up to filesystem.
4272 */
4273int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4274{
4275 struct nameidata nd;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004276 void *cookie;
Marcin Slusarz694a1762008-06-09 16:40:37 -07004277 int res;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004278
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 nd.depth = 0;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004280 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
Marcin Slusarz694a1762008-06-09 16:40:37 -07004281 if (IS_ERR(cookie))
4282 return PTR_ERR(cookie);
4283
4284 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
4285 if (dentry->d_inode->i_op->put_link)
4286 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
4287 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288}
4289
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290/* get the link contents into pagecache */
4291static char *page_getlink(struct dentry * dentry, struct page **ppage)
4292{
Duane Griffinebd09ab2008-12-19 20:47:12 +00004293 char *kaddr;
4294 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 struct address_space *mapping = dentry->d_inode->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -07004296 page = read_mapping_page(mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 if (IS_ERR(page))
Nick Piggin6fe69002007-05-06 14:49:04 -07004298 return (char*)page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 *ppage = page;
Duane Griffinebd09ab2008-12-19 20:47:12 +00004300 kaddr = kmap(page);
4301 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
4302 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303}
4304
4305int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4306{
4307 struct page *page = NULL;
4308 char *s = page_getlink(dentry, &page);
4309 int res = vfs_readlink(dentry,buffer,buflen,s);
4310 if (page) {
4311 kunmap(page);
4312 page_cache_release(page);
4313 }
4314 return res;
4315}
4316
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004317void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004319 struct page *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 nd_set_link(nd, page_getlink(dentry, &page));
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004321 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322}
4323
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004324void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004326 struct page *page = cookie;
4327
4328 if (page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 kunmap(page);
4330 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 }
4332}
4333
Nick Piggin54566b22009-01-04 12:00:53 -08004334/*
4335 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
4336 */
4337int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338{
4339 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004340 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07004341 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08004342 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 char *kaddr;
Nick Piggin54566b22009-01-04 12:00:53 -08004344 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
4345 if (nofs)
4346 flags |= AOP_FLAG_NOFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347
NeilBrown7e53cac2006-03-25 03:07:57 -08004348retry:
Nick Pigginafddba42007-10-16 01:25:01 -07004349 err = pagecache_write_begin(NULL, mapping, 0, len-1,
Nick Piggin54566b22009-01-04 12:00:53 -08004350 flags, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07004352 goto fail;
4353
Cong Wange8e3c3d2011-11-25 23:14:27 +08004354 kaddr = kmap_atomic(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 memcpy(kaddr, symname, len-1);
Cong Wange8e3c3d2011-11-25 23:14:27 +08004356 kunmap_atomic(kaddr);
Nick Pigginafddba42007-10-16 01:25:01 -07004357
4358 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4359 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 if (err < 0)
4361 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07004362 if (err < len-1)
4363 goto retry;
4364
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 mark_inode_dirty(inode);
4366 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367fail:
4368 return err;
4369}
4370
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004371int page_symlink(struct inode *inode, const char *symname, int len)
4372{
4373 return __page_symlink(inode, symname, len,
Nick Piggin54566b22009-01-04 12:00:53 -08004374 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004375}
4376
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08004377const struct inode_operations page_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 .readlink = generic_readlink,
4379 .follow_link = page_follow_link_light,
4380 .put_link = page_put_link,
4381};
4382
Al Viro2d8f3032008-07-22 09:59:21 -04004383EXPORT_SYMBOL(user_path_at);
David Howellscc53ce52011-01-14 18:45:26 +00004384EXPORT_SYMBOL(follow_down_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385EXPORT_SYMBOL(follow_down);
4386EXPORT_SYMBOL(follow_up);
Al Virof6d2ac52012-08-26 12:55:54 -04004387EXPORT_SYMBOL(get_write_access); /* nfsd */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389EXPORT_SYMBOL(lookup_one_len);
4390EXPORT_SYMBOL(page_follow_link_light);
4391EXPORT_SYMBOL(page_put_link);
4392EXPORT_SYMBOL(page_readlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004393EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394EXPORT_SYMBOL(page_symlink);
4395EXPORT_SYMBOL(page_symlink_inode_operations);
Al Virod1811462008-08-02 00:49:18 -04004396EXPORT_SYMBOL(kern_path);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07004397EXPORT_SYMBOL(vfs_path_lookup);
Al Virof419a2e2008-07-22 00:07:17 -04004398EXPORT_SYMBOL(inode_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399EXPORT_SYMBOL(unlock_rename);
4400EXPORT_SYMBOL(vfs_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401EXPORT_SYMBOL(vfs_link);
4402EXPORT_SYMBOL(vfs_mkdir);
4403EXPORT_SYMBOL(vfs_mknod);
4404EXPORT_SYMBOL(generic_permission);
4405EXPORT_SYMBOL(vfs_readlink);
4406EXPORT_SYMBOL(vfs_rename);
4407EXPORT_SYMBOL(vfs_rmdir);
4408EXPORT_SYMBOL(vfs_symlink);
4409EXPORT_SYMBOL(vfs_unlink);
4410EXPORT_SYMBOL(dentry_unhash);
4411EXPORT_SYMBOL(generic_readlink);