blob: 9071bb1bd18458d81dd3db61fd33339d44ecc2aa [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 Torvalds99d263d2014-09-13 11:30:10 -070037#include <linux/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/uaccess.h>
39
Eric Parise81e3f42009-12-04 15:47:36 -050040#include "internal.h"
Al Viroc7105362011-11-24 18:22:03 -050041#include "mount.h"
Eric Parise81e3f42009-12-04 15:47:36 -050042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/* [Feb-1997 T. Schoebel-Theuer]
44 * Fundamental changes in the pathname lookup mechanisms (namei)
45 * were necessary because of omirr. The reason is that omirr needs
46 * to know the _real_ pathname, not the user-supplied one, in case
47 * of symlinks (and also when transname replacements occur).
48 *
49 * The new code replaces the old recursive symlink resolution with
50 * an iterative one (in case of non-nested symlink chains). It does
51 * this with calls to <fs>_follow_link().
52 * As a side effect, dir_namei(), _namei() and follow_link() are now
53 * replaced with a single function lookup_dentry() that can handle all
54 * the special cases of the former code.
55 *
56 * With the new dcache, the pathname is stored at each inode, at least as
57 * long as the refcount of the inode is positive. As a side effect, the
58 * size of the dcache depends on the inode cache and thus is dynamic.
59 *
60 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
61 * resolution to correspond with current state of the code.
62 *
63 * Note that the symlink resolution is not *completely* iterative.
64 * There is still a significant amount of tail- and mid- recursion in
65 * the algorithm. Also, note that <fs>_readlink() is not used in
66 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
67 * may return different results than <fs>_follow_link(). Many virtual
68 * filesystems (including /proc) exhibit this behavior.
69 */
70
71/* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
72 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
73 * and the name already exists in form of a symlink, try to create the new
74 * name indicated by the symlink. The old code always complained that the
75 * name already exists, due to not following the symlink even if its target
76 * is nonexistent. The new semantics affects also mknod() and link() when
Lucas De Marchi25985ed2011-03-30 22:57:33 -030077 * the name is a symlink pointing to a non-existent name.
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 *
79 * I don't know which semantics is the right one, since I have no access
80 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
81 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
82 * "old" one. Personally, I think the new semantics is much more logical.
83 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
84 * file does succeed in both HP-UX and SunOs, but not in Solaris
85 * and in the old Linux semantics.
86 */
87
88/* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
89 * semantics. See the comments in "open_namei" and "do_link" below.
90 *
91 * [10-Sep-98 Alan Modra] Another symlink change.
92 */
93
94/* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
95 * inside the path - always follow.
96 * in the last component in creation/removal/renaming - never follow.
97 * if LOOKUP_FOLLOW passed - follow.
98 * if the pathname has trailing slashes - follow.
99 * otherwise - don't follow.
100 * (applied in that order).
101 *
102 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
103 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
104 * During the 2.4 we need to fix the userland stuff depending on it -
105 * hopefully we will be able to get rid of that wart in 2.5. So far only
106 * XEmacs seems to be relying on it...
107 */
108/*
109 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
Arjan van de Vena11f3a02006-03-23 03:00:33 -0800110 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 * any extra contention...
112 */
113
114/* In order to reduce some races, while at the same time doing additional
115 * checking and hopefully speeding things up, we copy filenames to the
116 * kernel data space before using them..
117 *
118 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
119 * PATH_MAX includes the nul terminator --RR.
120 */
Jeff Layton91a27b22012-10-10 15:25:28 -0400121
Al Virofd2f7cb2015-02-22 20:07:13 -0500122#define EMBEDDED_NAME_MAX (PATH_MAX - offsetof(struct filename, iname))
Jeff Layton7950e382012-10-10 16:43:13 -0400123
David Drysdale51f39a12014-12-12 16:57:29 -0800124struct filename *
Jeff Layton91a27b22012-10-10 15:25:28 -0400125getname_flags(const char __user *filename, int flags, int *empty)
126{
Al Viro94b5d262015-02-22 19:38:03 -0500127 struct filename *result;
Jeff Layton7950e382012-10-10 16:43:13 -0400128 char *kname;
Al Viro94b5d262015-02-22 19:38:03 -0500129 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Jeff Layton7ac86262012-10-10 15:25:28 -0400131 result = audit_reusename(filename);
132 if (result)
133 return result;
134
Jeff Layton7950e382012-10-10 16:43:13 -0400135 result = __getname();
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700136 if (unlikely(!result))
Eric Paris4043cde2012-01-03 14:23:08 -0500137 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Jeff Layton7950e382012-10-10 16:43:13 -0400139 /*
140 * First, try to embed the struct filename inside the names_cache
141 * allocation
142 */
Al Virofd2f7cb2015-02-22 20:07:13 -0500143 kname = (char *)result->iname;
Jeff Layton91a27b22012-10-10 15:25:28 -0400144 result->name = kname;
Jeff Layton7950e382012-10-10 16:43:13 -0400145
Al Viro94b5d262015-02-22 19:38:03 -0500146 len = strncpy_from_user(kname, filename, EMBEDDED_NAME_MAX);
Jeff Layton91a27b22012-10-10 15:25:28 -0400147 if (unlikely(len < 0)) {
Al Viro94b5d262015-02-22 19:38:03 -0500148 __putname(result);
149 return ERR_PTR(len);
Jeff Layton91a27b22012-10-10 15:25:28 -0400150 }
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700151
Jeff Layton7950e382012-10-10 16:43:13 -0400152 /*
153 * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
154 * separate struct filename so we can dedicate the entire
155 * names_cache allocation for the pathname, and re-do the copy from
156 * userland.
157 */
Al Viro94b5d262015-02-22 19:38:03 -0500158 if (unlikely(len == EMBEDDED_NAME_MAX)) {
Al Virofd2f7cb2015-02-22 20:07:13 -0500159 const size_t size = offsetof(struct filename, iname[1]);
Jeff Layton7950e382012-10-10 16:43:13 -0400160 kname = (char *)result;
161
Al Virofd2f7cb2015-02-22 20:07:13 -0500162 /*
163 * size is chosen that way we to guarantee that
164 * result->iname[0] is within the same object and that
165 * kname can't be equal to result->iname, no matter what.
166 */
167 result = kzalloc(size, GFP_KERNEL);
Al Viro94b5d262015-02-22 19:38:03 -0500168 if (unlikely(!result)) {
169 __putname(kname);
170 return ERR_PTR(-ENOMEM);
Jeff Layton7950e382012-10-10 16:43:13 -0400171 }
172 result->name = kname;
Al Viro94b5d262015-02-22 19:38:03 -0500173 len = strncpy_from_user(kname, filename, PATH_MAX);
174 if (unlikely(len < 0)) {
175 __putname(kname);
176 kfree(result);
177 return ERR_PTR(len);
178 }
179 if (unlikely(len == PATH_MAX)) {
180 __putname(kname);
181 kfree(result);
182 return ERR_PTR(-ENAMETOOLONG);
183 }
Jeff Layton7950e382012-10-10 16:43:13 -0400184 }
185
Al Viro94b5d262015-02-22 19:38:03 -0500186 result->refcnt = 1;
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700187 /* The empty path is special. */
188 if (unlikely(!len)) {
189 if (empty)
Eric Paris4043cde2012-01-03 14:23:08 -0500190 *empty = 1;
Al Viro94b5d262015-02-22 19:38:03 -0500191 if (!(flags & LOOKUP_EMPTY)) {
192 putname(result);
193 return ERR_PTR(-ENOENT);
194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 }
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700196
Jeff Layton7950e382012-10-10 16:43:13 -0400197 result->uptr = filename;
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800198 result->aname = NULL;
Jeff Layton7950e382012-10-10 16:43:13 -0400199 audit_getname(result);
200 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
202
Jeff Layton91a27b22012-10-10 15:25:28 -0400203struct filename *
204getname(const char __user * filename)
Al Virof52e0c12011-03-14 18:56:51 -0400205{
Linus Torvaldsf7493e52012-03-22 16:10:40 -0700206 return getname_flags(filename, 0, NULL);
Al Virof52e0c12011-03-14 18:56:51 -0400207}
208
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800209struct filename *
210getname_kernel(const char * filename)
211{
212 struct filename *result;
Paul Moore08518542015-01-21 23:59:56 -0500213 int len = strlen(filename) + 1;
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800214
215 result = __getname();
216 if (unlikely(!result))
217 return ERR_PTR(-ENOMEM);
218
Paul Moore08518542015-01-21 23:59:56 -0500219 if (len <= EMBEDDED_NAME_MAX) {
Al Virofd2f7cb2015-02-22 20:07:13 -0500220 result->name = (char *)result->iname;
Paul Moore08518542015-01-21 23:59:56 -0500221 } else if (len <= PATH_MAX) {
Al Viro5da48802018-04-08 11:57:10 -0400222 const size_t size = offsetof(struct filename, iname[1]);
Paul Moore08518542015-01-21 23:59:56 -0500223 struct filename *tmp;
224
Al Viro5da48802018-04-08 11:57:10 -0400225 tmp = kmalloc(size, GFP_KERNEL);
Paul Moore08518542015-01-21 23:59:56 -0500226 if (unlikely(!tmp)) {
227 __putname(result);
228 return ERR_PTR(-ENOMEM);
229 }
230 tmp->name = (char *)result;
Paul Moore08518542015-01-21 23:59:56 -0500231 result = tmp;
232 } else {
233 __putname(result);
234 return ERR_PTR(-ENAMETOOLONG);
235 }
236 memcpy((char *)result->name, filename, len);
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800237 result->uptr = NULL;
238 result->aname = NULL;
Paul Moore55422d02015-01-22 00:00:23 -0500239 result->refcnt = 1;
Paul Moorefd3522f2015-01-22 00:00:10 -0500240 audit_getname(result);
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800241
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800242 return result;
243}
244
Jeff Layton91a27b22012-10-10 15:25:28 -0400245void putname(struct filename *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
Paul Moore55422d02015-01-22 00:00:23 -0500247 BUG_ON(name->refcnt <= 0);
248
249 if (--name->refcnt > 0)
250 return;
251
Al Virofd2f7cb2015-02-22 20:07:13 -0500252 if (name->name != name->iname) {
Paul Moore55422d02015-01-22 00:00:23 -0500253 __putname(name->name);
254 kfree(name);
255 } else
256 __putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Linus Torvaldse77819e2011-07-22 19:30:19 -0700259static int check_acl(struct inode *inode, int mask)
260{
Linus Torvalds84635d62011-07-25 22:47:03 -0700261#ifdef CONFIG_FS_POSIX_ACL
Linus Torvaldse77819e2011-07-22 19:30:19 -0700262 struct posix_acl *acl;
263
Linus Torvaldse77819e2011-07-22 19:30:19 -0700264 if (mask & MAY_NOT_BLOCK) {
Al Viro35678662011-08-02 21:32:13 -0400265 acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
266 if (!acl)
Linus Torvaldse77819e2011-07-22 19:30:19 -0700267 return -EAGAIN;
Al Viro35678662011-08-02 21:32:13 -0400268 /* no ->get_acl() calls in RCU mode... */
269 if (acl == ACL_NOT_CACHED)
270 return -ECHILD;
Ari Savolainen206b1d02011-08-06 19:43:07 +0300271 return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
Linus Torvaldse77819e2011-07-22 19:30:19 -0700272 }
273
Christoph Hellwig2982baa2013-12-20 05:16:38 -0800274 acl = get_acl(inode, ACL_TYPE_ACCESS);
275 if (IS_ERR(acl))
276 return PTR_ERR(acl);
Linus Torvaldse77819e2011-07-22 19:30:19 -0700277 if (acl) {
278 int error = posix_acl_permission(inode, acl, mask);
279 posix_acl_release(acl);
280 return error;
281 }
Linus Torvalds84635d62011-07-25 22:47:03 -0700282#endif
Linus Torvaldse77819e2011-07-22 19:30:19 -0700283
284 return -EAGAIN;
285}
286
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700287/*
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530288 * This does the basic permission checking
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700289 */
Al Viro7e401452011-06-20 19:12:17 -0400290static int acl_permission_check(struct inode *inode, int mask)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700291{
Linus Torvalds26cf46b2011-05-13 11:51:01 -0700292 unsigned int mode = inode->i_mode;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700293
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -0800294 if (likely(uid_eq(current_fsuid(), inode->i_uid)))
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700295 mode >>= 6;
296 else {
Linus Torvaldse77819e2011-07-22 19:30:19 -0700297 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
Al Viro7e401452011-06-20 19:12:17 -0400298 int error = check_acl(inode, mask);
Nick Pigginb74c79e2011-01-07 17:49:58 +1100299 if (error != -EAGAIN)
300 return error;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700301 }
302
303 if (in_group_p(inode->i_gid))
304 mode >>= 3;
305 }
306
307 /*
308 * If the DACs are ok we don't need any capability check.
309 */
Al Viro9c2c7032011-06-20 19:06:22 -0400310 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700311 return 0;
312 return -EACCES;
313}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315/**
Nick Pigginb74c79e2011-01-07 17:49:58 +1100316 * generic_permission - check for access rights on a Posix-like filesystem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 * @inode: inode to check access rights for
Andreas Gruenbacher8fd90c82011-10-23 23:13:30 +0530318 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 *
320 * Used to check for read/write/execute permissions on a file.
321 * We use "fsuid" for this, letting us set arbitrary permissions
322 * for filesystem access without changing the "normal" uids which
Nick Pigginb74c79e2011-01-07 17:49:58 +1100323 * are used for other things.
324 *
325 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
326 * request cannot be satisfied (eg. requires blocking or too much complexity).
327 * It would then be called again in ref-walk mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 */
Al Viro2830ba72011-06-20 19:16:29 -0400329int generic_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700331 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 /*
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530334 * Do the basic permission checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 */
Al Viro7e401452011-06-20 19:12:17 -0400336 ret = acl_permission_check(inode, mask);
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700337 if (ret != -EACCES)
338 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Al Virod594e7e2011-06-20 19:55:42 -0400340 if (S_ISDIR(inode->i_mode)) {
341 /* DACs are overridable for directories */
Andy Lutomirski23adbe12014-06-10 12:45:42 -0700342 if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
Al Virod594e7e2011-06-20 19:55:42 -0400343 return 0;
344 if (!(mask & MAY_WRITE))
Andy Lutomirski23adbe12014-06-10 12:45:42 -0700345 if (capable_wrt_inode_uidgid(inode,
346 CAP_DAC_READ_SEARCH))
Al Virod594e7e2011-06-20 19:55:42 -0400347 return 0;
348 return -EACCES;
349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 /*
351 * Read/write DACs are always overridable.
Al Virod594e7e2011-06-20 19:55:42 -0400352 * Executable DACs are overridable when there is
353 * at least one exec bit set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 */
Al Virod594e7e2011-06-20 19:55:42 -0400355 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
Andy Lutomirski23adbe12014-06-10 12:45:42 -0700356 if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return 0;
358
359 /*
360 * Searching includes executable on directories, else just read.
361 */
Serge E. Hallyn7ea66002009-12-29 14:50:19 -0600362 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
Al Virod594e7e2011-06-20 19:55:42 -0400363 if (mask == MAY_READ)
Andy Lutomirski23adbe12014-06-10 12:45:42 -0700364 if (capable_wrt_inode_uidgid(inode, CAP_DAC_READ_SEARCH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return 0;
366
367 return -EACCES;
368}
Al Viro4d359502014-03-14 12:20:17 -0400369EXPORT_SYMBOL(generic_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700371/*
372 * We _really_ want to just do "generic_permission()" without
373 * even looking at the inode->i_op values. So we keep a cache
374 * flag in inode->i_opflags, that says "this has not special
375 * permission function, use the fast case".
376 */
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -0700377static inline int do_inode_permission(struct vfsmount *mnt, struct inode *inode, int mask)
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700378{
379 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -0700380 if (likely(mnt && inode->i_op->permission2))
381 return inode->i_op->permission2(mnt, inode, mask);
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700382 if (likely(inode->i_op->permission))
383 return inode->i_op->permission(inode, mask);
384
385 /* This gets set once for the inode lifetime */
386 spin_lock(&inode->i_lock);
387 inode->i_opflags |= IOP_FASTPERM;
388 spin_unlock(&inode->i_lock);
389 }
390 return generic_permission(inode, mask);
391}
392
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200393/**
David Howells0bdaea92012-06-25 12:55:46 +0100394 * __inode_permission - Check for access rights to a given inode
395 * @inode: Inode to check permission on
396 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200397 *
David Howells0bdaea92012-06-25 12:55:46 +0100398 * Check for read/write/execute permissions on an inode.
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530399 *
400 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
David Howells0bdaea92012-06-25 12:55:46 +0100401 *
402 * This does not check for a read-only file system. You probably want
403 * inode_permission().
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200404 */
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -0700405int __inode_permission2(struct vfsmount *mnt, struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
Al Viroe6305c42008-07-15 21:03:57 -0400407 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700409 if (unlikely(mask & MAY_WRITE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 /*
411 * Nobody gets write access to an immutable file.
412 */
413 if (IS_IMMUTABLE(inode))
414 return -EACCES;
415 }
416
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -0700417 retval = do_inode_permission(mnt, inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if (retval)
419 return retval;
420
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700421 retval = devcgroup_inode_permission(inode, mask);
422 if (retval)
423 return retval;
424
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -0700425 retval = security_inode_permission(inode, mask);
426 return retval;
427}
428EXPORT_SYMBOL(__inode_permission2);
429
430int __inode_permission(struct inode *inode, int mask)
431{
432 return __inode_permission2(NULL, inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
Miklos Szeredibd5d0852014-10-24 00:14:35 +0200434EXPORT_SYMBOL(__inode_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Al Virof4d6ff82011-06-19 13:14:21 -0400436/**
David Howells0bdaea92012-06-25 12:55:46 +0100437 * sb_permission - Check superblock-level permissions
438 * @sb: Superblock of inode to check permission on
Randy Dunlap55852632012-08-18 17:39:25 -0700439 * @inode: Inode to check permission on
David Howells0bdaea92012-06-25 12:55:46 +0100440 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
441 *
442 * Separate out file-system wide checks from inode-specific permission checks.
443 */
444static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
445{
446 if (unlikely(mask & MAY_WRITE)) {
447 umode_t mode = inode->i_mode;
448
449 /* Nobody gets write access to a read-only fs. */
450 if ((sb->s_flags & MS_RDONLY) &&
451 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
452 return -EROFS;
453 }
454 return 0;
455}
456
457/**
458 * inode_permission - Check for access rights to a given inode
459 * @inode: Inode to check permission on
460 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
461 *
462 * Check for read/write/execute permissions on an inode. We use fs[ug]id for
463 * this, letting us set arbitrary permissions for filesystem access without
464 * changing the "normal" UIDs which are used for other things.
465 *
466 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
467 */
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -0700468int inode_permission2(struct vfsmount *mnt, struct inode *inode, int mask)
David Howells0bdaea92012-06-25 12:55:46 +0100469{
470 int retval;
471
472 retval = sb_permission(inode->i_sb, inode, mask);
473 if (retval)
474 return retval;
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -0700475 return __inode_permission2(mnt, inode, mask);
476}
477EXPORT_SYMBOL(inode_permission2);
478
479int inode_permission(struct inode *inode, int mask)
480{
481 return inode_permission2(NULL, inode, mask);
David Howells0bdaea92012-06-25 12:55:46 +0100482}
Al Viro4d359502014-03-14 12:20:17 -0400483EXPORT_SYMBOL(inode_permission);
David Howells0bdaea92012-06-25 12:55:46 +0100484
485/**
Jan Blunck5dd784d02008-02-14 19:34:38 -0800486 * path_get - get a reference to a path
487 * @path: path to get the reference to
488 *
489 * Given a path increment the reference count to the dentry and the vfsmount.
490 */
Al Virodcf787f2013-03-01 23:51:07 -0500491void path_get(const struct path *path)
Jan Blunck5dd784d02008-02-14 19:34:38 -0800492{
493 mntget(path->mnt);
494 dget(path->dentry);
495}
496EXPORT_SYMBOL(path_get);
497
498/**
Jan Blunck1d957f92008-02-14 19:34:35 -0800499 * path_put - put a reference to a path
500 * @path: path to put the reference to
501 *
502 * Given a path decrement the reference count to the dentry and the vfsmount.
503 */
Al Virodcf787f2013-03-01 23:51:07 -0500504void path_put(const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
Jan Blunck1d957f92008-02-14 19:34:35 -0800506 dput(path->dentry);
507 mntput(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508}
Jan Blunck1d957f92008-02-14 19:34:35 -0800509EXPORT_SYMBOL(path_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Al Viro894bc8c2015-05-02 07:16:16 -0400511#define EMBEDDED_LEVELS 2
Al Viro1f55a6e2014-11-01 19:30:41 -0400512struct nameidata {
513 struct path path;
Al Viro1cf26652015-05-06 16:01:56 -0400514 struct qstr last;
Al Viro1f55a6e2014-11-01 19:30:41 -0400515 struct path root;
516 struct inode *inode; /* path.dentry.d_inode */
517 unsigned int flags;
Al Viro9883d182015-05-13 07:28:08 -0400518 unsigned seq, m_seq;
Al Viro1f55a6e2014-11-01 19:30:41 -0400519 int last_type;
520 unsigned depth;
NeilBrown756daf22015-03-23 13:37:38 +1100521 int total_link_count;
Al Viro697fc6c2015-05-02 19:38:35 -0400522 struct saved {
523 struct path link;
524 void *cookie;
525 const char *name;
Al Viro237d8b32015-05-07 09:21:14 -0400526 struct inode *inode;
Al Viro0450b2d2015-05-08 13:23:53 -0400527 unsigned seq;
Al Viro894bc8c2015-05-02 07:16:16 -0400528 } *stack, internal[EMBEDDED_LEVELS];
Al Viro9883d182015-05-13 07:28:08 -0400529 struct filename *name;
530 struct nameidata *saved;
531 unsigned root_seq;
532 int dfd;
Al Viro1f55a6e2014-11-01 19:30:41 -0400533};
534
Al Viro9883d182015-05-13 07:28:08 -0400535static void set_nameidata(struct nameidata *p, int dfd, struct filename *name)
Al Viro894bc8c2015-05-02 07:16:16 -0400536{
NeilBrown756daf22015-03-23 13:37:38 +1100537 struct nameidata *old = current->nameidata;
538 p->stack = p->internal;
Al Viroc8a53ee2015-05-12 18:43:07 -0400539 p->dfd = dfd;
540 p->name = name;
NeilBrown756daf22015-03-23 13:37:38 +1100541 p->total_link_count = old ? old->total_link_count : 0;
Al Viro9883d182015-05-13 07:28:08 -0400542 p->saved = old;
NeilBrown756daf22015-03-23 13:37:38 +1100543 current->nameidata = p;
Al Viro894bc8c2015-05-02 07:16:16 -0400544}
545
Al Viro9883d182015-05-13 07:28:08 -0400546static void restore_nameidata(void)
Al Viro894bc8c2015-05-02 07:16:16 -0400547{
Al Viro9883d182015-05-13 07:28:08 -0400548 struct nameidata *now = current->nameidata, *old = now->saved;
NeilBrown756daf22015-03-23 13:37:38 +1100549
550 current->nameidata = old;
551 if (old)
552 old->total_link_count = now->total_link_count;
553 if (now->stack != now->internal) {
554 kfree(now->stack);
555 now->stack = now->internal;
Al Viro894bc8c2015-05-02 07:16:16 -0400556 }
557}
558
559static int __nd_alloc_stack(struct nameidata *nd)
560{
Al Virobc40aee2015-05-09 13:04:24 -0400561 struct saved *p;
562
563 if (nd->flags & LOOKUP_RCU) {
564 p= kmalloc(MAXSYMLINKS * sizeof(struct saved),
565 GFP_ATOMIC);
566 if (unlikely(!p))
567 return -ECHILD;
568 } else {
569 p= kmalloc(MAXSYMLINKS * sizeof(struct saved),
Al Viro894bc8c2015-05-02 07:16:16 -0400570 GFP_KERNEL);
Al Virobc40aee2015-05-09 13:04:24 -0400571 if (unlikely(!p))
572 return -ENOMEM;
573 }
Al Viro894bc8c2015-05-02 07:16:16 -0400574 memcpy(p, nd->internal, sizeof(nd->internal));
575 nd->stack = p;
576 return 0;
577}
578
Eric W. Biederman397d4252015-08-15 20:27:13 -0500579/**
580 * path_connected - Verify that a path->dentry is below path->mnt.mnt_root
581 * @path: nameidate to verify
582 *
583 * Rename can sometimes move a file or directory outside of a bind
584 * mount, path_connected allows those cases to be detected.
585 */
586static bool path_connected(const struct path *path)
587{
588 struct vfsmount *mnt = path->mnt;
Eric W. Biedermanc8c71402018-03-14 18:20:29 -0500589 struct super_block *sb = mnt->mnt_sb;
Eric W. Biederman397d4252015-08-15 20:27:13 -0500590
Eric W. Biedermanc8c71402018-03-14 18:20:29 -0500591 /* Bind mounts and multi-root filesystems can have disconnected paths */
592 if (!(sb->s_iflags & SB_I_MULTIROOT) && (mnt->mnt_root == sb->s_root))
Eric W. Biederman397d4252015-08-15 20:27:13 -0500593 return true;
594
595 return is_subdir(path->dentry, mnt->mnt_root);
596}
597
Al Viro894bc8c2015-05-02 07:16:16 -0400598static inline int nd_alloc_stack(struct nameidata *nd)
599{
Al Viroda4e0be2015-05-03 20:52:15 -0400600 if (likely(nd->depth != EMBEDDED_LEVELS))
Al Viro894bc8c2015-05-02 07:16:16 -0400601 return 0;
602 if (likely(nd->stack != nd->internal))
603 return 0;
604 return __nd_alloc_stack(nd);
605}
606
Al Viro79733872015-05-09 12:55:43 -0400607static void drop_links(struct nameidata *nd)
608{
609 int i = nd->depth;
610 while (i--) {
611 struct saved *last = nd->stack + i;
612 struct inode *inode = last->inode;
613 if (last->cookie && inode->i_op->put_link) {
614 inode->i_op->put_link(inode, last->cookie);
615 last->cookie = NULL;
616 }
617 }
618}
619
620static void terminate_walk(struct nameidata *nd)
621{
622 drop_links(nd);
623 if (!(nd->flags & LOOKUP_RCU)) {
624 int i;
625 path_put(&nd->path);
626 for (i = 0; i < nd->depth; i++)
627 path_put(&nd->stack[i].link);
Al Viro102b8af2015-05-12 17:35:52 -0400628 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
629 path_put(&nd->root);
630 nd->root.mnt = NULL;
631 }
Al Viro79733872015-05-09 12:55:43 -0400632 } else {
633 nd->flags &= ~LOOKUP_RCU;
634 if (!(nd->flags & LOOKUP_ROOT))
635 nd->root.mnt = NULL;
636 rcu_read_unlock();
637 }
638 nd->depth = 0;
639}
640
641/* path_put is needed afterwards regardless of success or failure */
642static bool legitimize_path(struct nameidata *nd,
643 struct path *path, unsigned seq)
644{
645 int res = __legitimize_mnt(path->mnt, nd->m_seq);
646 if (unlikely(res)) {
647 if (res > 0)
648 path->mnt = NULL;
649 path->dentry = NULL;
650 return false;
651 }
652 if (unlikely(!lockref_get_not_dead(&path->dentry->d_lockref))) {
653 path->dentry = NULL;
654 return false;
655 }
656 return !read_seqcount_retry(&path->dentry->d_seq, seq);
657}
658
659static bool legitimize_links(struct nameidata *nd)
660{
661 int i;
662 for (i = 0; i < nd->depth; i++) {
663 struct saved *last = nd->stack + i;
664 if (unlikely(!legitimize_path(nd, &last->link, last->seq))) {
665 drop_links(nd);
666 nd->depth = i + 1;
667 return false;
668 }
669 }
670 return true;
671}
672
Al Viro19660af2011-03-25 10:32:48 -0400673/*
Nick Piggin31e6b012011-01-07 17:49:52 +1100674 * Path walking has 2 modes, rcu-walk and ref-walk (see
Al Viro19660af2011-03-25 10:32:48 -0400675 * Documentation/filesystems/path-lookup.txt). In situations when we can't
676 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
677 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
678 * mode. Refcounts are grabbed at the last known good point before rcu-walk
679 * got stuck, so ref-walk may continue from there. If this is not successful
680 * (eg. a seqcount has changed), then failure is returned and it's up to caller
681 * to restart the path walk from the beginning in ref-walk mode.
Nick Piggin31e6b012011-01-07 17:49:52 +1100682 */
Nick Piggin31e6b012011-01-07 17:49:52 +1100683
684/**
Al Viro19660af2011-03-25 10:32:48 -0400685 * unlazy_walk - try to switch to ref-walk mode.
686 * @nd: nameidata pathwalk data
687 * @dentry: child of nd->path.dentry or NULL
Al Viro6e9918b2015-05-05 09:26:05 -0400688 * @seq: seq number to check dentry against
Randy Dunlap39191622011-01-08 19:36:21 -0800689 * Returns: 0 on success, -ECHILD on failure
Nick Piggin31e6b012011-01-07 17:49:52 +1100690 *
Al Viro19660af2011-03-25 10:32:48 -0400691 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
692 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
693 * @nd or NULL. Must be called from rcu-walk context.
Al Viro79733872015-05-09 12:55:43 -0400694 * Nothing should touch nameidata between unlazy_walk() failure and
695 * terminate_walk().
Nick Piggin31e6b012011-01-07 17:49:52 +1100696 */
Al Viro6e9918b2015-05-05 09:26:05 -0400697static int unlazy_walk(struct nameidata *nd, struct dentry *dentry, unsigned seq)
Nick Piggin31e6b012011-01-07 17:49:52 +1100698{
Nick Piggin31e6b012011-01-07 17:49:52 +1100699 struct dentry *parent = nd->path.dentry;
700
701 BUG_ON(!(nd->flags & LOOKUP_RCU));
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700702
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700703 nd->flags &= ~LOOKUP_RCU;
Al Viro79733872015-05-09 12:55:43 -0400704 if (unlikely(!legitimize_links(nd)))
705 goto out2;
706 if (unlikely(!legitimize_mnt(nd->path.mnt, nd->m_seq)))
707 goto out2;
708 if (unlikely(!lockref_get_not_dead(&parent->d_lockref)))
709 goto out1;
Al Viro48a066e2013-09-29 22:06:07 -0400710
Linus Torvalds15570082013-09-02 11:38:06 -0700711 /*
712 * For a negative lookup, the lookup sequence point is the parents
713 * sequence point, and it only needs to revalidate the parent dentry.
714 *
715 * For a positive lookup, we need to move both the parent and the
716 * dentry from the RCU domain to be properly refcounted. And the
717 * sequence number in the dentry validates *both* dentry counters,
718 * since we checked the sequence number of the parent after we got
719 * the child sequence number. So we know the parent must still
720 * be valid if the child sequence number is still valid.
721 */
Al Viro19660af2011-03-25 10:32:48 -0400722 if (!dentry) {
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700723 if (read_seqcount_retry(&parent->d_seq, nd->seq))
724 goto out;
Al Viro19660af2011-03-25 10:32:48 -0400725 BUG_ON(nd->inode != parent->d_inode);
726 } else {
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700727 if (!lockref_get_not_dead(&dentry->d_lockref))
728 goto out;
Al Viro6e9918b2015-05-05 09:26:05 -0400729 if (read_seqcount_retry(&dentry->d_seq, seq))
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700730 goto drop_dentry;
Al Viro19660af2011-03-25 10:32:48 -0400731 }
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700732
733 /*
734 * Sequence counts matched. Now make sure that the root is
735 * still valid and get it if required.
736 */
737 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
Al Viro5a8d87e2015-05-12 00:10:18 -0400738 if (unlikely(!legitimize_path(nd, &nd->root, nd->root_seq))) {
739 rcu_read_unlock();
740 dput(dentry);
741 return -ECHILD;
Al Viro79733872015-05-09 12:55:43 -0400742 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100743 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100744
Al Viro8b61e742013-11-08 12:45:01 -0500745 rcu_read_unlock();
Nick Piggin31e6b012011-01-07 17:49:52 +1100746 return 0;
Al Viro19660af2011-03-25 10:32:48 -0400747
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700748drop_dentry:
Al Viro8b61e742013-11-08 12:45:01 -0500749 rcu_read_unlock();
Linus Torvalds15570082013-09-02 11:38:06 -0700750 dput(dentry);
Linus Torvaldsd0d27272013-09-10 12:17:49 -0700751 goto drop_root_mnt;
Al Viro79733872015-05-09 12:55:43 -0400752out2:
753 nd->path.mnt = NULL;
754out1:
755 nd->path.dentry = NULL;
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700756out:
Al Viro8b61e742013-11-08 12:45:01 -0500757 rcu_read_unlock();
Linus Torvaldsd0d27272013-09-10 12:17:49 -0700758drop_root_mnt:
759 if (!(nd->flags & LOOKUP_ROOT))
760 nd->root.mnt = NULL;
Nick Piggin31e6b012011-01-07 17:49:52 +1100761 return -ECHILD;
762}
763
Al Viro79733872015-05-09 12:55:43 -0400764static int unlazy_link(struct nameidata *nd, struct path *link, unsigned seq)
765{
766 if (unlikely(!legitimize_path(nd, link, seq))) {
767 drop_links(nd);
768 nd->depth = 0;
769 nd->flags &= ~LOOKUP_RCU;
770 nd->path.mnt = NULL;
771 nd->path.dentry = NULL;
772 if (!(nd->flags & LOOKUP_ROOT))
773 nd->root.mnt = NULL;
774 rcu_read_unlock();
775 } else if (likely(unlazy_walk(nd, NULL, 0)) == 0) {
776 return 0;
777 }
778 path_put(link);
779 return -ECHILD;
780}
781
Al Viro4ce16ef32012-06-10 16:10:59 -0400782static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
Nick Piggin34286d62011-01-07 17:49:57 +1100783{
Al Viro4ce16ef32012-06-10 16:10:59 -0400784 return dentry->d_op->d_revalidate(dentry, flags);
Nick Piggin34286d62011-01-07 17:49:57 +1100785}
786
Al Viro9f1fafe2011-03-25 11:00:12 -0400787/**
788 * complete_walk - successful completion of path walk
789 * @nd: pointer nameidata
Jeff Layton39159de2009-12-07 12:01:50 -0500790 *
Al Viro9f1fafe2011-03-25 11:00:12 -0400791 * If we had been in RCU mode, drop out of it and legitimize nd->path.
792 * Revalidate the final result, unless we'd already done that during
793 * the path walk or the filesystem doesn't ask for it. Return 0 on
794 * success, -error on failure. In case of failure caller does not
795 * need to drop nd->path.
Jeff Layton39159de2009-12-07 12:01:50 -0500796 */
Al Viro9f1fafe2011-03-25 11:00:12 -0400797static int complete_walk(struct nameidata *nd)
Jeff Layton39159de2009-12-07 12:01:50 -0500798{
Al Viro16c2cd72011-02-22 15:50:10 -0500799 struct dentry *dentry = nd->path.dentry;
Jeff Layton39159de2009-12-07 12:01:50 -0500800 int status;
Jeff Layton39159de2009-12-07 12:01:50 -0500801
Al Viro9f1fafe2011-03-25 11:00:12 -0400802 if (nd->flags & LOOKUP_RCU) {
Al Viro9f1fafe2011-03-25 11:00:12 -0400803 if (!(nd->flags & LOOKUP_ROOT))
804 nd->root.mnt = NULL;
Al Viro6e9918b2015-05-05 09:26:05 -0400805 if (unlikely(unlazy_walk(nd, NULL, 0)))
Al Viro48a066e2013-09-29 22:06:07 -0400806 return -ECHILD;
Al Viro9f1fafe2011-03-25 11:00:12 -0400807 }
808
Al Viro16c2cd72011-02-22 15:50:10 -0500809 if (likely(!(nd->flags & LOOKUP_JUMPED)))
Jeff Layton39159de2009-12-07 12:01:50 -0500810 return 0;
811
Jeff Laytonecf3d1f2013-02-20 11:19:05 -0500812 if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
Al Viro16c2cd72011-02-22 15:50:10 -0500813 return 0;
814
Jeff Laytonecf3d1f2013-02-20 11:19:05 -0500815 status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
Jeff Layton39159de2009-12-07 12:01:50 -0500816 if (status > 0)
817 return 0;
818
Al Viro16c2cd72011-02-22 15:50:10 -0500819 if (!status)
Jeff Layton39159de2009-12-07 12:01:50 -0500820 status = -ESTALE;
Al Viro16c2cd72011-02-22 15:50:10 -0500821
Jeff Layton39159de2009-12-07 12:01:50 -0500822 return status;
823}
824
Al Viro18d8c862015-05-12 16:32:34 -0400825static void set_root(struct nameidata *nd)
Al Viro2a737872009-04-07 11:49:53 -0400826{
Al Viro7bd88372014-09-13 21:55:46 -0400827 get_fs_root(current->fs, &nd->root);
Al Viro2a737872009-04-07 11:49:53 -0400828}
829
Al Viro06d71372015-06-29 12:07:04 -0400830static void set_root_rcu(struct nameidata *nd)
Nick Piggin31e6b012011-01-07 17:49:52 +1100831{
Al Viro7bd88372014-09-13 21:55:46 -0400832 struct fs_struct *fs = current->fs;
Al Viro8f47a0162015-05-09 19:02:01 -0400833 unsigned seq;
Nick Pigginc28cc362011-01-07 17:49:53 +1100834
Al Viro7bd88372014-09-13 21:55:46 -0400835 do {
836 seq = read_seqcount_begin(&fs->seq);
837 nd->root = fs->root;
Al Viro8f47a0162015-05-09 19:02:01 -0400838 nd->root_seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
Al Viro7bd88372014-09-13 21:55:46 -0400839 } while (read_seqcount_retry(&fs->seq, seq));
Nick Piggin31e6b012011-01-07 17:49:52 +1100840}
841
Jan Blunck1d957f92008-02-14 19:34:35 -0800842static void path_put_conditional(struct path *path, struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700843{
844 dput(path->dentry);
Jan Blunck4ac91372008-02-14 19:34:32 -0800845 if (path->mnt != nd->path.mnt)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700846 mntput(path->mnt);
847}
848
Nick Piggin7b9337a2011-01-14 08:42:43 +0000849static inline void path_to_nameidata(const struct path *path,
850 struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700851{
Nick Piggin31e6b012011-01-07 17:49:52 +1100852 if (!(nd->flags & LOOKUP_RCU)) {
853 dput(nd->path.dentry);
854 if (nd->path.mnt != path->mnt)
855 mntput(nd->path.mnt);
Huang Shijie9a229682010-04-02 17:37:13 +0800856 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100857 nd->path.mnt = path->mnt;
Jan Blunck4ac91372008-02-14 19:34:32 -0800858 nd->path.dentry = path->dentry;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700859}
860
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400861/*
862 * Helper to directly jump to a known parsed path from ->follow_link,
863 * caller must have taken a reference to path beforehand.
864 */
Al Viro6e771372015-05-02 13:37:52 -0400865void nd_jump_link(struct path *path)
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400866{
Al Viro6e771372015-05-02 13:37:52 -0400867 struct nameidata *nd = current->nameidata;
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400868 path_put(&nd->path);
869
870 nd->path = *path;
871 nd->inode = nd->path.dentry->d_inode;
872 nd->flags |= LOOKUP_JUMPED;
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400873}
874
Al Virob9ff4422015-05-02 20:19:23 -0400875static inline void put_link(struct nameidata *nd)
Al Viro574197e2011-03-14 22:20:34 -0400876{
Al Viro21c30032015-05-03 21:06:24 -0400877 struct saved *last = nd->stack + --nd->depth;
Al Viro237d8b32015-05-07 09:21:14 -0400878 struct inode *inode = last->inode;
Al Virob9ff4422015-05-02 20:19:23 -0400879 if (last->cookie && inode->i_op->put_link)
Al Viro5f2c4172015-05-07 11:14:26 -0400880 inode->i_op->put_link(inode, last->cookie);
Al Viro6548fae2015-05-07 20:32:22 -0400881 if (!(nd->flags & LOOKUP_RCU))
882 path_put(&last->link);
Al Viro574197e2011-03-14 22:20:34 -0400883}
884
Linus Torvalds561ec642012-10-26 10:05:07 -0700885int sysctl_protected_symlinks __read_mostly = 0;
886int sysctl_protected_hardlinks __read_mostly = 0;
Salvatore Mesoraca3658ccb2018-08-23 17:00:35 -0700887int sysctl_protected_fifos __read_mostly;
888int sysctl_protected_regular __read_mostly;
Kees Cook800179c2012-07-25 17:29:07 -0700889
890/**
891 * may_follow_link - Check symlink following for unsafe situations
Randy Dunlap55852632012-08-18 17:39:25 -0700892 * @nd: nameidata pathwalk data
Kees Cook800179c2012-07-25 17:29:07 -0700893 *
894 * In the case of the sysctl_protected_symlinks sysctl being enabled,
895 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
896 * in a sticky world-writable directory. This is to protect privileged
897 * processes from failing races against path names that may change out
898 * from under them by way of other users creating malicious symlinks.
899 * It will permit symlinks to be followed only when outside a sticky
900 * world-writable directory, or when the uid of the symlink and follower
901 * match, or when the directory owner matches the symlink's owner.
902 *
903 * Returns 0 if following the symlink is allowed, -ve on error.
904 */
Al Virofec2fa22015-05-06 15:58:18 -0400905static inline int may_follow_link(struct nameidata *nd)
Kees Cook800179c2012-07-25 17:29:07 -0700906{
907 const struct inode *inode;
908 const struct inode *parent;
Seth Forshee6a90aa42016-08-30 00:35:00 -0400909 kuid_t puid;
Kees Cook800179c2012-07-25 17:29:07 -0700910
911 if (!sysctl_protected_symlinks)
912 return 0;
913
914 /* Allowed if owner and follower match. */
Al Viro237d8b32015-05-07 09:21:14 -0400915 inode = nd->stack[0].inode;
Eric W. Biederman81abe272012-08-03 09:38:08 -0700916 if (uid_eq(current_cred()->fsuid, inode->i_uid))
Kees Cook800179c2012-07-25 17:29:07 -0700917 return 0;
918
919 /* Allowed if parent directory not sticky and world-writable. */
Al Viroaa65fa32015-08-04 23:23:50 -0400920 parent = nd->inode;
Kees Cook800179c2012-07-25 17:29:07 -0700921 if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
922 return 0;
923
924 /* Allowed if parent directory and link owner match. */
Seth Forshee6a90aa42016-08-30 00:35:00 -0400925 puid = parent->i_uid;
926 if (uid_valid(puid) && uid_eq(puid, inode->i_uid))
Kees Cook800179c2012-07-25 17:29:07 -0700927 return 0;
928
Al Viro319565022015-05-07 20:37:40 -0400929 if (nd->flags & LOOKUP_RCU)
930 return -ECHILD;
931
Al Viro1cf26652015-05-06 16:01:56 -0400932 audit_log_link_denied("follow_link", &nd->stack[0].link);
Kees Cook800179c2012-07-25 17:29:07 -0700933 return -EACCES;
934}
935
936/**
937 * safe_hardlink_source - Check for safe hardlink conditions
938 * @inode: the source inode to hardlink from
939 *
940 * Return false if at least one of the following conditions:
941 * - inode is not a regular file
942 * - inode is setuid
943 * - inode is setgid and group-exec
944 * - access failure for read and write
945 *
946 * Otherwise returns true.
947 */
948static bool safe_hardlink_source(struct inode *inode)
949{
950 umode_t mode = inode->i_mode;
951
952 /* Special files should not get pinned to the filesystem. */
953 if (!S_ISREG(mode))
954 return false;
955
956 /* Setuid files should not get pinned to the filesystem. */
957 if (mode & S_ISUID)
958 return false;
959
960 /* Executable setgid files should not get pinned to the filesystem. */
961 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
962 return false;
963
964 /* Hardlinking to unreadable or unwritable sources is dangerous. */
965 if (inode_permission(inode, MAY_READ | MAY_WRITE))
966 return false;
967
968 return true;
969}
970
971/**
972 * may_linkat - Check permissions for creating a hardlink
973 * @link: the source to hardlink from
974 *
975 * Block hardlink when all of:
976 * - sysctl_protected_hardlinks enabled
977 * - fsuid does not match inode
978 * - hardlink source is unsafe (see safe_hardlink_source() above)
Dirk Steinmetzf2ca3792015-10-20 16:09:19 +0200979 * - not CAP_FOWNER in a namespace with the inode owner uid mapped
Kees Cook800179c2012-07-25 17:29:07 -0700980 *
981 * Returns 0 if successful, -ve on error.
982 */
983static int may_linkat(struct path *link)
984{
Kees Cook800179c2012-07-25 17:29:07 -0700985 struct inode *inode;
986
987 if (!sysctl_protected_hardlinks)
988 return 0;
989
Kees Cook800179c2012-07-25 17:29:07 -0700990 inode = link->dentry->d_inode;
991
992 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
993 * otherwise, it must be a safe source.
994 */
Dirk Steinmetzf2ca3792015-10-20 16:09:19 +0200995 if (inode_owner_or_capable(inode) || safe_hardlink_source(inode))
Kees Cook800179c2012-07-25 17:29:07 -0700996 return 0;
997
Kees Cooka51d9ea2012-07-25 17:29:08 -0700998 audit_log_link_denied("linkat", link);
Kees Cook800179c2012-07-25 17:29:07 -0700999 return -EPERM;
1000}
1001
Salvatore Mesoraca3658ccb2018-08-23 17:00:35 -07001002/**
1003 * may_create_in_sticky - Check whether an O_CREAT open in a sticky directory
1004 * should be allowed, or not, on files that already
1005 * exist.
1006 * @dir: the sticky parent directory
1007 * @inode: the inode of the file to open
1008 *
1009 * Block an O_CREAT open of a FIFO (or a regular file) when:
1010 * - sysctl_protected_fifos (or sysctl_protected_regular) is enabled
1011 * - the file already exists
1012 * - we are in a sticky directory
1013 * - we don't own the file
1014 * - the owner of the directory doesn't own the file
1015 * - the directory is world writable
1016 * If the sysctl_protected_fifos (or sysctl_protected_regular) is set to 2
1017 * the directory doesn't have to be world writable: being group writable will
1018 * be enough.
1019 *
1020 * Returns 0 if the open is allowed, -ve on error.
1021 */
1022static int may_create_in_sticky(struct dentry * const dir,
1023 struct inode * const inode)
1024{
1025 if ((!sysctl_protected_fifos && S_ISFIFO(inode->i_mode)) ||
1026 (!sysctl_protected_regular && S_ISREG(inode->i_mode)) ||
1027 likely(!(dir->d_inode->i_mode & S_ISVTX)) ||
1028 uid_eq(inode->i_uid, dir->d_inode->i_uid) ||
1029 uid_eq(current_fsuid(), inode->i_uid))
1030 return 0;
1031
1032 if (likely(dir->d_inode->i_mode & 0002) ||
1033 (dir->d_inode->i_mode & 0020 &&
1034 ((sysctl_protected_fifos >= 2 && S_ISFIFO(inode->i_mode)) ||
1035 (sysctl_protected_regular >= 2 && S_ISREG(inode->i_mode))))) {
1036 return -EACCES;
1037 }
1038 return 0;
1039}
1040
Al Viro3b2e7f72015-04-19 00:53:50 -04001041static __always_inline
1042const char *get_link(struct nameidata *nd)
Ian Kent051d3812006-03-27 01:14:53 -08001043{
Al Viroab104922015-05-10 11:50:01 -04001044 struct saved *last = nd->stack + nd->depth - 1;
Al Viro1cf26652015-05-06 16:01:56 -04001045 struct dentry *dentry = last->link.dentry;
Al Viro237d8b32015-05-07 09:21:14 -04001046 struct inode *inode = last->inode;
Al Viro6d7b5aa2012-06-10 04:15:17 -04001047 int error;
Al Viro0a959df2015-04-18 18:23:41 -04001048 const char *res;
Ian Kent051d3812006-03-27 01:14:53 -08001049
NeilBrown8fa9dd22015-03-23 13:37:40 +11001050 if (!(nd->flags & LOOKUP_RCU)) {
1051 touch_atime(&last->link);
1052 cond_resched();
1053 } else if (atime_needs_update(&last->link, inode)) {
1054 if (unlikely(unlazy_walk(nd, NULL, 0)))
1055 return ERR_PTR(-ECHILD);
1056 touch_atime(&last->link);
1057 }
1058
NeilBrownbda0be72015-03-23 13:37:39 +11001059 error = security_inode_follow_link(dentry, inode,
1060 nd->flags & LOOKUP_RCU);
1061 if (unlikely(error))
Al Viro6920a442015-05-10 10:43:46 -04001062 return ERR_PTR(error);
Al Viro36f3b4f2011-02-22 21:24:38 -05001063
Al Viro86acdca12009-12-22 23:45:11 -05001064 nd->last_type = LAST_BIND;
Al Virod4dee482015-04-30 20:08:02 -04001065 res = inode->i_link;
1066 if (!res) {
Al Viro8c1b4562015-05-09 18:15:21 -04001067 if (nd->flags & LOOKUP_RCU) {
1068 if (unlikely(unlazy_walk(nd, NULL, 0)))
1069 return ERR_PTR(-ECHILD);
1070 }
Al Viro6e771372015-05-02 13:37:52 -04001071 res = inode->i_op->follow_link(dentry, &last->cookie);
Al Virofab51e82015-05-10 11:01:00 -04001072 if (IS_ERR_OR_NULL(res)) {
Al Viro6920a442015-05-10 10:43:46 -04001073 last->cookie = NULL;
Al Virofab51e82015-05-10 11:01:00 -04001074 return res;
1075 }
Ian Kent051d3812006-03-27 01:14:53 -08001076 }
Al Virofab51e82015-05-10 11:01:00 -04001077 if (*res == '/') {
Al Viro8c1b4562015-05-09 18:15:21 -04001078 if (nd->flags & LOOKUP_RCU) {
Al Viro8f47a0162015-05-09 19:02:01 -04001079 struct dentry *d;
1080 if (!nd->root.mnt)
1081 set_root_rcu(nd);
1082 nd->path = nd->root;
1083 d = nd->path.dentry;
1084 nd->inode = d->d_inode;
1085 nd->seq = nd->root_seq;
1086 if (unlikely(read_seqcount_retry(&d->d_seq, nd->seq)))
Al Viro8c1b4562015-05-09 18:15:21 -04001087 return ERR_PTR(-ECHILD);
Al Viro8f47a0162015-05-09 19:02:01 -04001088 } else {
1089 if (!nd->root.mnt)
1090 set_root(nd);
1091 path_put(&nd->path);
1092 nd->path = nd->root;
1093 path_get(&nd->root);
1094 nd->inode = nd->path.dentry->d_inode;
Al Viro8c1b4562015-05-09 18:15:21 -04001095 }
Al Virofab51e82015-05-10 11:01:00 -04001096 nd->flags |= LOOKUP_JUMPED;
1097 while (unlikely(*++res == '/'))
1098 ;
1099 }
1100 if (!*res)
1101 res = NULL;
Al Viro0a959df2015-04-18 18:23:41 -04001102 return res;
1103}
Al Viro6d7b5aa2012-06-10 04:15:17 -04001104
David Howellsf015f1262012-06-25 12:55:28 +01001105/*
1106 * follow_up - Find the mountpoint of path's vfsmount
1107 *
1108 * Given a path, find the mountpoint of its source file system.
1109 * Replace @path with the path of the mountpoint in the parent mount.
1110 * Up is towards /.
1111 *
1112 * Return 1 if we went up a level and 0 if we were already at the
1113 * root.
1114 */
Al Virobab77eb2009-04-18 03:26:48 -04001115int follow_up(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
Al Viro0714a532011-11-24 22:19:58 -05001117 struct mount *mnt = real_mount(path->mnt);
1118 struct mount *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 struct dentry *mountpoint;
Nick Piggin99b7db72010-08-18 04:37:39 +10001120
Al Viro48a066e2013-09-29 22:06:07 -04001121 read_seqlock_excl(&mount_lock);
Al Viro0714a532011-11-24 22:19:58 -05001122 parent = mnt->mnt_parent;
Al Viro3c0a6162012-07-18 17:32:50 +04001123 if (parent == mnt) {
Al Viro48a066e2013-09-29 22:06:07 -04001124 read_sequnlock_excl(&mount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 return 0;
1126 }
Al Viro0714a532011-11-24 22:19:58 -05001127 mntget(&parent->mnt);
Al Viroa73324d2011-11-24 22:25:07 -05001128 mountpoint = dget(mnt->mnt_mountpoint);
Al Viro48a066e2013-09-29 22:06:07 -04001129 read_sequnlock_excl(&mount_lock);
Al Virobab77eb2009-04-18 03:26:48 -04001130 dput(path->dentry);
1131 path->dentry = mountpoint;
1132 mntput(path->mnt);
Al Viro0714a532011-11-24 22:19:58 -05001133 path->mnt = &parent->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 return 1;
1135}
Al Viro4d359502014-03-14 12:20:17 -04001136EXPORT_SYMBOL(follow_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Nick Pigginb5c84bf2011-01-07 17:49:38 +11001138/*
David Howells9875cf82011-01-14 18:45:21 +00001139 * Perform an automount
1140 * - return -EISDIR to tell follow_managed() to stop and return the path we
1141 * were called with.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 */
NeilBrown756daf22015-03-23 13:37:38 +11001143static int follow_automount(struct path *path, struct nameidata *nd,
David Howells9875cf82011-01-14 18:45:21 +00001144 bool *need_mntput)
Nick Piggin31e6b012011-01-07 17:49:52 +11001145{
David Howells9875cf82011-01-14 18:45:21 +00001146 struct vfsmount *mnt;
David Howellsea5b7782011-01-14 19:10:03 +00001147 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001148
David Howells9875cf82011-01-14 18:45:21 +00001149 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
1150 return -EREMOTE;
Al Viro463ffb22005-06-06 13:36:05 -07001151
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +02001152 /* We don't want to mount if someone's just doing a stat -
1153 * unless they're stat'ing a directory and appended a '/' to
1154 * the name.
1155 *
1156 * We do, however, want to mount if someone wants to open or
1157 * create a file of any type under the mountpoint, wants to
1158 * traverse through the mountpoint or wants to open the
1159 * mounted directory. Also, autofs may mark negative dentries
1160 * as being automount points. These will need the attentions
1161 * of the daemon to instantiate them before they can be used.
David Howells9875cf82011-01-14 18:45:21 +00001162 */
NeilBrown756daf22015-03-23 13:37:38 +11001163 if (!(nd->flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
1164 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +02001165 path->dentry->d_inode)
1166 return -EISDIR;
1167
NeilBrown756daf22015-03-23 13:37:38 +11001168 nd->total_link_count++;
1169 if (nd->total_link_count >= 40)
David Howells9875cf82011-01-14 18:45:21 +00001170 return -ELOOP;
1171
1172 mnt = path->dentry->d_op->d_automount(path);
1173 if (IS_ERR(mnt)) {
1174 /*
1175 * The filesystem is allowed to return -EISDIR here to indicate
1176 * it doesn't want to automount. For instance, autofs would do
1177 * this so that its userspace daemon can mount on this dentry.
1178 *
1179 * However, we can only permit this if it's a terminal point in
1180 * the path being looked up; if it wasn't then the remainder of
1181 * the path is inaccessible and we should say so.
1182 */
NeilBrown756daf22015-03-23 13:37:38 +11001183 if (PTR_ERR(mnt) == -EISDIR && (nd->flags & LOOKUP_PARENT))
David Howells9875cf82011-01-14 18:45:21 +00001184 return -EREMOTE;
1185 return PTR_ERR(mnt);
1186 }
David Howellsea5b7782011-01-14 19:10:03 +00001187
David Howells9875cf82011-01-14 18:45:21 +00001188 if (!mnt) /* mount collision */
1189 return 0;
1190
Al Viro8aef1882011-06-16 15:10:06 +01001191 if (!*need_mntput) {
1192 /* lock_mount() may release path->mnt on error */
1193 mntget(path->mnt);
1194 *need_mntput = true;
1195 }
Al Viro19a167a2011-01-17 01:35:23 -05001196 err = finish_automount(mnt, path);
David Howellsea5b7782011-01-14 19:10:03 +00001197
David Howellsea5b7782011-01-14 19:10:03 +00001198 switch (err) {
1199 case -EBUSY:
1200 /* Someone else made a mount here whilst we were busy */
Al Viro19a167a2011-01-17 01:35:23 -05001201 return 0;
David Howellsea5b7782011-01-14 19:10:03 +00001202 case 0:
Al Viro8aef1882011-06-16 15:10:06 +01001203 path_put(path);
David Howellsea5b7782011-01-14 19:10:03 +00001204 path->mnt = mnt;
1205 path->dentry = dget(mnt->mnt_root);
David Howellsea5b7782011-01-14 19:10:03 +00001206 return 0;
Al Viro19a167a2011-01-17 01:35:23 -05001207 default:
1208 return err;
David Howellsea5b7782011-01-14 19:10:03 +00001209 }
Al Viro19a167a2011-01-17 01:35:23 -05001210
David Howells9875cf82011-01-14 18:45:21 +00001211}
1212
1213/*
1214 * Handle a dentry that is managed in some way.
David Howellscc53ce52011-01-14 18:45:26 +00001215 * - Flagged for transit management (autofs)
David Howells9875cf82011-01-14 18:45:21 +00001216 * - Flagged as mountpoint
1217 * - Flagged as automount point
1218 *
1219 * This may only be called in refwalk mode.
1220 *
1221 * Serialization is taken care of in namespace.c
1222 */
NeilBrown756daf22015-03-23 13:37:38 +11001223static int follow_managed(struct path *path, struct nameidata *nd)
David Howells9875cf82011-01-14 18:45:21 +00001224{
Al Viro8aef1882011-06-16 15:10:06 +01001225 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
David Howells9875cf82011-01-14 18:45:21 +00001226 unsigned managed;
1227 bool need_mntput = false;
Al Viro8aef1882011-06-16 15:10:06 +01001228 int ret = 0;
David Howells9875cf82011-01-14 18:45:21 +00001229
1230 /* Given that we're not holding a lock here, we retain the value in a
1231 * local variable for each dentry as we look at it so that we don't see
1232 * the components of that value change under us */
1233 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1234 managed &= DCACHE_MANAGED_DENTRY,
1235 unlikely(managed != 0)) {
David Howellscc53ce52011-01-14 18:45:26 +00001236 /* Allow the filesystem to manage the transit without i_mutex
1237 * being held. */
1238 if (managed & DCACHE_MANAGE_TRANSIT) {
1239 BUG_ON(!path->dentry->d_op);
1240 BUG_ON(!path->dentry->d_op->d_manage);
Al Viro1aed3e42011-03-18 09:09:02 -04001241 ret = path->dentry->d_op->d_manage(path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +00001242 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001243 break;
David Howellscc53ce52011-01-14 18:45:26 +00001244 }
1245
David Howells9875cf82011-01-14 18:45:21 +00001246 /* Transit to a mounted filesystem. */
1247 if (managed & DCACHE_MOUNTED) {
1248 struct vfsmount *mounted = lookup_mnt(path);
1249 if (mounted) {
1250 dput(path->dentry);
1251 if (need_mntput)
1252 mntput(path->mnt);
1253 path->mnt = mounted;
1254 path->dentry = dget(mounted->mnt_root);
1255 need_mntput = true;
1256 continue;
1257 }
1258
1259 /* Something is mounted on this dentry in another
1260 * namespace and/or whatever was mounted there in this
Al Viro48a066e2013-09-29 22:06:07 -04001261 * namespace got unmounted before lookup_mnt() could
1262 * get it */
David Howells9875cf82011-01-14 18:45:21 +00001263 }
1264
1265 /* Handle an automount point */
1266 if (managed & DCACHE_NEED_AUTOMOUNT) {
NeilBrown756daf22015-03-23 13:37:38 +11001267 ret = follow_automount(path, nd, &need_mntput);
David Howells9875cf82011-01-14 18:45:21 +00001268 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001269 break;
David Howells9875cf82011-01-14 18:45:21 +00001270 continue;
1271 }
1272
1273 /* We didn't change the current path point */
1274 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 }
Al Viro8aef1882011-06-16 15:10:06 +01001276
1277 if (need_mntput && path->mnt == mnt)
1278 mntput(path->mnt);
1279 if (ret == -EISDIR)
1280 ret = 0;
Al Viro84027522015-04-22 10:30:08 -04001281 if (need_mntput)
1282 nd->flags |= LOOKUP_JUMPED;
1283 if (unlikely(ret < 0))
1284 path_put_conditional(path, nd);
1285 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286}
1287
David Howellscc53ce52011-01-14 18:45:26 +00001288int follow_down_one(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289{
1290 struct vfsmount *mounted;
1291
Al Viro1c755af2009-04-18 14:06:57 -04001292 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -04001294 dput(path->dentry);
1295 mntput(path->mnt);
1296 path->mnt = mounted;
1297 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 return 1;
1299 }
1300 return 0;
1301}
Al Viro4d359502014-03-14 12:20:17 -04001302EXPORT_SYMBOL(follow_down_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
NeilBrownb8faf032014-08-04 17:06:29 +10001304static inline int managed_dentry_rcu(struct dentry *dentry)
Ian Kent62a73752011-03-25 01:51:02 +08001305{
NeilBrownb8faf032014-08-04 17:06:29 +10001306 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT) ?
1307 dentry->d_op->d_manage(dentry, true) : 0;
Ian Kent62a73752011-03-25 01:51:02 +08001308}
1309
David Howells9875cf82011-01-14 18:45:21 +00001310/*
Al Viro287548e2011-05-27 06:50:06 -04001311 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1312 * we meet a managed dentry that would need blocking.
David Howells9875cf82011-01-14 18:45:21 +00001313 */
1314static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
Al Viro254cf582015-05-05 09:40:46 -04001315 struct inode **inode, unsigned *seqp)
David Howells9875cf82011-01-14 18:45:21 +00001316{
Ian Kent62a73752011-03-25 01:51:02 +08001317 for (;;) {
Al Viroc7105362011-11-24 18:22:03 -05001318 struct mount *mounted;
Ian Kent62a73752011-03-25 01:51:02 +08001319 /*
1320 * Don't forget we might have a non-mountpoint managed dentry
1321 * that wants to block transit.
1322 */
NeilBrownb8faf032014-08-04 17:06:29 +10001323 switch (managed_dentry_rcu(path->dentry)) {
1324 case -ECHILD:
1325 default:
David Howellsab909112011-01-14 18:46:51 +00001326 return false;
NeilBrownb8faf032014-08-04 17:06:29 +10001327 case -EISDIR:
1328 return true;
1329 case 0:
1330 break;
1331 }
Ian Kent62a73752011-03-25 01:51:02 +08001332
1333 if (!d_mountpoint(path->dentry))
NeilBrownb8faf032014-08-04 17:06:29 +10001334 return !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
Ian Kent62a73752011-03-25 01:51:02 +08001335
Al Viro474279d2013-10-01 16:11:26 -04001336 mounted = __lookup_mnt(path->mnt, path->dentry);
David Howells9875cf82011-01-14 18:45:21 +00001337 if (!mounted)
1338 break;
Al Viroc7105362011-11-24 18:22:03 -05001339 path->mnt = &mounted->mnt;
1340 path->dentry = mounted->mnt.mnt_root;
Al Viroa3fbbde2011-11-07 21:21:26 +00001341 nd->flags |= LOOKUP_JUMPED;
Al Viro254cf582015-05-05 09:40:46 -04001342 *seqp = read_seqcount_begin(&path->dentry->d_seq);
Linus Torvalds59430262011-07-18 15:43:29 -07001343 /*
1344 * Update the inode too. We don't need to re-check the
1345 * dentry sequence number here after this d_inode read,
1346 * because a mount-point is always pinned.
1347 */
1348 *inode = path->dentry->d_inode;
David Howells9875cf82011-01-14 18:45:21 +00001349 }
Al Virof5be3e29122014-09-13 21:50:45 -04001350 return !read_seqretry(&mount_lock, nd->m_seq) &&
NeilBrownb8faf032014-08-04 17:06:29 +10001351 !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
Al Viro287548e2011-05-27 06:50:06 -04001352}
1353
Nick Piggin31e6b012011-01-07 17:49:52 +11001354static int follow_dotdot_rcu(struct nameidata *nd)
1355{
Al Viro4023bfc2014-09-13 21:59:43 -04001356 struct inode *inode = nd->inode;
Al Viro7bd88372014-09-13 21:55:46 -04001357 if (!nd->root.mnt)
1358 set_root_rcu(nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11001359
David Howells9875cf82011-01-14 18:45:21 +00001360 while (1) {
Al Viroaed434a2015-05-12 12:22:47 -04001361 if (path_equal(&nd->path, &nd->root))
Nick Piggin31e6b012011-01-07 17:49:52 +11001362 break;
Nick Piggin31e6b012011-01-07 17:49:52 +11001363 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1364 struct dentry *old = nd->path.dentry;
1365 struct dentry *parent = old->d_parent;
1366 unsigned seq;
1367
Al Viro4023bfc2014-09-13 21:59:43 -04001368 inode = parent->d_inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001369 seq = read_seqcount_begin(&parent->d_seq);
Al Viroaed434a2015-05-12 12:22:47 -04001370 if (unlikely(read_seqcount_retry(&old->d_seq, nd->seq)))
1371 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001372 nd->path.dentry = parent;
1373 nd->seq = seq;
Eric W. Biederman397d4252015-08-15 20:27:13 -05001374 if (unlikely(!path_connected(&nd->path)))
1375 return -ENOENT;
Nick Piggin31e6b012011-01-07 17:49:52 +11001376 break;
Al Viroaed434a2015-05-12 12:22:47 -04001377 } else {
1378 struct mount *mnt = real_mount(nd->path.mnt);
1379 struct mount *mparent = mnt->mnt_parent;
1380 struct dentry *mountpoint = mnt->mnt_mountpoint;
1381 struct inode *inode2 = mountpoint->d_inode;
1382 unsigned seq = read_seqcount_begin(&mountpoint->d_seq);
1383 if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1384 return -ECHILD;
1385 if (&mparent->mnt == nd->path.mnt)
1386 break;
1387 /* we know that mountpoint was pinned */
1388 nd->path.dentry = mountpoint;
1389 nd->path.mnt = &mparent->mnt;
1390 inode = inode2;
1391 nd->seq = seq;
Nick Piggin31e6b012011-01-07 17:49:52 +11001392 }
Nick Piggin31e6b012011-01-07 17:49:52 +11001393 }
Al Viroaed434a2015-05-12 12:22:47 -04001394 while (unlikely(d_mountpoint(nd->path.dentry))) {
Al Virob37199e2014-03-20 15:18:22 -04001395 struct mount *mounted;
1396 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry);
Al Viroaed434a2015-05-12 12:22:47 -04001397 if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1398 return -ECHILD;
Al Virob37199e2014-03-20 15:18:22 -04001399 if (!mounted)
1400 break;
1401 nd->path.mnt = &mounted->mnt;
1402 nd->path.dentry = mounted->mnt.mnt_root;
Al Viro4023bfc2014-09-13 21:59:43 -04001403 inode = nd->path.dentry->d_inode;
Al Virob37199e2014-03-20 15:18:22 -04001404 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Virob37199e2014-03-20 15:18:22 -04001405 }
Al Viro4023bfc2014-09-13 21:59:43 -04001406 nd->inode = inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001407 return 0;
1408}
1409
David Howells9875cf82011-01-14 18:45:21 +00001410/*
David Howellscc53ce52011-01-14 18:45:26 +00001411 * Follow down to the covering mount currently visible to userspace. At each
1412 * point, the filesystem owning that dentry may be queried as to whether the
1413 * caller is permitted to proceed or not.
David Howellscc53ce52011-01-14 18:45:26 +00001414 */
Al Viro7cc90cc2011-03-18 09:04:20 -04001415int follow_down(struct path *path)
David Howellscc53ce52011-01-14 18:45:26 +00001416{
1417 unsigned managed;
1418 int ret;
1419
1420 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1421 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1422 /* Allow the filesystem to manage the transit without i_mutex
1423 * being held.
1424 *
1425 * We indicate to the filesystem if someone is trying to mount
1426 * something here. This gives autofs the chance to deny anyone
1427 * other than its daemon the right to mount on its
1428 * superstructure.
1429 *
1430 * The filesystem may sleep at this point.
1431 */
1432 if (managed & DCACHE_MANAGE_TRANSIT) {
1433 BUG_ON(!path->dentry->d_op);
1434 BUG_ON(!path->dentry->d_op->d_manage);
David Howellsab909112011-01-14 18:46:51 +00001435 ret = path->dentry->d_op->d_manage(
Al Viro1aed3e42011-03-18 09:09:02 -04001436 path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +00001437 if (ret < 0)
1438 return ret == -EISDIR ? 0 : ret;
1439 }
1440
1441 /* Transit to a mounted filesystem. */
1442 if (managed & DCACHE_MOUNTED) {
1443 struct vfsmount *mounted = lookup_mnt(path);
1444 if (!mounted)
1445 break;
1446 dput(path->dentry);
1447 mntput(path->mnt);
1448 path->mnt = mounted;
1449 path->dentry = dget(mounted->mnt_root);
1450 continue;
1451 }
1452
1453 /* Don't handle automount points here */
1454 break;
1455 }
1456 return 0;
1457}
Al Viro4d359502014-03-14 12:20:17 -04001458EXPORT_SYMBOL(follow_down);
David Howellscc53ce52011-01-14 18:45:26 +00001459
1460/*
David Howells9875cf82011-01-14 18:45:21 +00001461 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1462 */
1463static void follow_mount(struct path *path)
1464{
1465 while (d_mountpoint(path->dentry)) {
1466 struct vfsmount *mounted = lookup_mnt(path);
1467 if (!mounted)
1468 break;
1469 dput(path->dentry);
1470 mntput(path->mnt);
1471 path->mnt = mounted;
1472 path->dentry = dget(mounted->mnt_root);
1473 }
1474}
1475
Eric W. Biederman397d4252015-08-15 20:27:13 -05001476static int follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477{
Al Viro7bd88372014-09-13 21:55:46 -04001478 if (!nd->root.mnt)
1479 set_root(nd);
Andreas Mohre518ddb2006-09-29 02:01:22 -07001480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 while(1) {
Jan Blunck4ac91372008-02-14 19:34:32 -08001482 struct dentry *old = nd->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Al Viro2a737872009-04-07 11:49:53 -04001484 if (nd->path.dentry == nd->root.dentry &&
1485 nd->path.mnt == nd->root.mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 break;
1487 }
Jan Blunck4ac91372008-02-14 19:34:32 -08001488 if (nd->path.dentry != nd->path.mnt->mnt_root) {
Al Viro3088dd72010-01-30 15:47:29 -05001489 /* rare case of legitimate dget_parent()... */
1490 nd->path.dentry = dget_parent(nd->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 dput(old);
Eric W. Biederman397d4252015-08-15 20:27:13 -05001492 if (unlikely(!path_connected(&nd->path)))
1493 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 break;
1495 }
Al Viro3088dd72010-01-30 15:47:29 -05001496 if (!follow_up(&nd->path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 }
Al Viro79ed0222009-04-18 13:59:41 -04001499 follow_mount(&nd->path);
Nick Piggin31e6b012011-01-07 17:49:52 +11001500 nd->inode = nd->path.dentry->d_inode;
Eric W. Biederman397d4252015-08-15 20:27:13 -05001501 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502}
1503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504/*
Miklos Szeredibad61182012-03-26 12:54:24 +02001505 * This looks up the name in dcache, possibly revalidates the old dentry and
1506 * allocates a new one if not found or not valid. In the need_lookup argument
1507 * returns whether i_op->lookup is necessary.
1508 *
1509 * dir->d_inode->i_mutex must be held
Nick Pigginbaa03892010-08-18 04:37:31 +10001510 */
Miklos Szeredibad61182012-03-26 12:54:24 +02001511static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
Al Viro201f9562012-06-22 12:42:10 +04001512 unsigned int flags, bool *need_lookup)
Nick Pigginbaa03892010-08-18 04:37:31 +10001513{
Nick Pigginbaa03892010-08-18 04:37:31 +10001514 struct dentry *dentry;
Miklos Szeredibad61182012-03-26 12:54:24 +02001515 int error;
Nick Pigginbaa03892010-08-18 04:37:31 +10001516
Miklos Szeredibad61182012-03-26 12:54:24 +02001517 *need_lookup = false;
1518 dentry = d_lookup(dir, name);
1519 if (dentry) {
Jeff Layton39e3c952012-11-28 11:30:53 -05001520 if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
Al Viro201f9562012-06-22 12:42:10 +04001521 error = d_revalidate(dentry, flags);
Miklos Szeredibad61182012-03-26 12:54:24 +02001522 if (unlikely(error <= 0)) {
1523 if (error < 0) {
1524 dput(dentry);
1525 return ERR_PTR(error);
Eric W. Biederman5542aa22014-02-13 09:46:25 -08001526 } else {
1527 d_invalidate(dentry);
Miklos Szeredibad61182012-03-26 12:54:24 +02001528 dput(dentry);
1529 dentry = NULL;
1530 }
1531 }
1532 }
1533 }
Nick Pigginbaa03892010-08-18 04:37:31 +10001534
Miklos Szeredibad61182012-03-26 12:54:24 +02001535 if (!dentry) {
1536 dentry = d_alloc(dir, name);
1537 if (unlikely(!dentry))
1538 return ERR_PTR(-ENOMEM);
Nick Pigginbaa03892010-08-18 04:37:31 +10001539
Miklos Szeredibad61182012-03-26 12:54:24 +02001540 *need_lookup = true;
Nick Pigginbaa03892010-08-18 04:37:31 +10001541 }
1542 return dentry;
1543}
1544
1545/*
J. Bruce Fields13a2c3b2013-10-23 16:09:16 -04001546 * Call i_op->lookup on the dentry. The dentry must be negative and
1547 * unhashed.
Miklos Szeredibad61182012-03-26 12:54:24 +02001548 *
1549 * dir->d_inode->i_mutex must be held
Josef Bacik44396f42011-05-31 11:58:49 -04001550 */
Miklos Szeredibad61182012-03-26 12:54:24 +02001551static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
Al Viro72bd8662012-06-10 17:17:17 -04001552 unsigned int flags)
Josef Bacik44396f42011-05-31 11:58:49 -04001553{
Josef Bacik44396f42011-05-31 11:58:49 -04001554 struct dentry *old;
1555
1556 /* Don't create child dentry for a dead directory. */
Miklos Szeredibad61182012-03-26 12:54:24 +02001557 if (unlikely(IS_DEADDIR(dir))) {
Miklos Szeredie188dc02012-02-03 14:25:18 +01001558 dput(dentry);
Josef Bacik44396f42011-05-31 11:58:49 -04001559 return ERR_PTR(-ENOENT);
Miklos Szeredie188dc02012-02-03 14:25:18 +01001560 }
Josef Bacik44396f42011-05-31 11:58:49 -04001561
Al Viro72bd8662012-06-10 17:17:17 -04001562 old = dir->i_op->lookup(dir, dentry, flags);
Josef Bacik44396f42011-05-31 11:58:49 -04001563 if (unlikely(old)) {
1564 dput(dentry);
1565 dentry = old;
1566 }
1567 return dentry;
1568}
1569
Al Viroa3255542012-03-30 14:41:51 -04001570static struct dentry *__lookup_hash(struct qstr *name,
Al Viro72bd8662012-06-10 17:17:17 -04001571 struct dentry *base, unsigned int flags)
Al Viroa3255542012-03-30 14:41:51 -04001572{
Miklos Szeredibad61182012-03-26 12:54:24 +02001573 bool need_lookup;
Al Viroa3255542012-03-30 14:41:51 -04001574 struct dentry *dentry;
1575
Al Viro72bd8662012-06-10 17:17:17 -04001576 dentry = lookup_dcache(name, base, flags, &need_lookup);
Miklos Szeredibad61182012-03-26 12:54:24 +02001577 if (!need_lookup)
1578 return dentry;
Al Viroa3255542012-03-30 14:41:51 -04001579
Al Viro72bd8662012-06-10 17:17:17 -04001580 return lookup_real(base->d_inode, dentry, flags);
Al Viroa3255542012-03-30 14:41:51 -04001581}
1582
Josef Bacik44396f42011-05-31 11:58:49 -04001583/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 * It's more convoluted than I'd like it to be, but... it's still fairly
1585 * small and for now I'd prefer to have fast path as straight as possible.
1586 * It _is_ time-critical.
1587 */
Al Viroe97cdc82013-01-24 18:16:00 -05001588static int lookup_fast(struct nameidata *nd,
Al Viro254cf582015-05-05 09:40:46 -04001589 struct path *path, struct inode **inode,
1590 unsigned *seqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
Jan Blunck4ac91372008-02-14 19:34:32 -08001592 struct vfsmount *mnt = nd->path.mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +11001593 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro5a18fff2011-03-11 04:44:53 -05001594 int need_reval = 1;
1595 int status = 1;
David Howells9875cf82011-01-14 18:45:21 +00001596 int err;
1597
Al Viro3cac2602009-08-13 18:27:43 +04001598 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001599 * Rename seqlock is not required here because in the off chance
1600 * of a false negative due to a concurrent rename, we're going to
1601 * do the non-racy lookup, below.
1602 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001603 if (nd->flags & LOOKUP_RCU) {
1604 unsigned seq;
Al Viro766c4cb2015-05-07 19:24:57 -04001605 bool negative;
Linus Torvaldsda53be12013-05-21 15:22:44 -07001606 dentry = __d_lookup_rcu(parent, &nd->last, &seq);
Al Viro5a18fff2011-03-11 04:44:53 -05001607 if (!dentry)
1608 goto unlazy;
1609
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001610 /*
1611 * This sequence count validates that the inode matches
1612 * the dentry name information from lookup.
1613 */
David Howells63afdfc2015-05-06 15:59:00 +01001614 *inode = d_backing_inode(dentry);
Al Viro766c4cb2015-05-07 19:24:57 -04001615 negative = d_is_negative(dentry);
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001616 if (read_seqcount_retry(&dentry->d_seq, seq))
1617 return -ECHILD;
1618
1619 /*
1620 * This sequence count validates that the parent had no
1621 * changes while we did the lookup of the dentry above.
1622 *
1623 * The memory barrier in read_seqcount_begin of child is
1624 * enough, we can use __read_seqcount_retry here.
1625 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001626 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1627 return -ECHILD;
Al Viro5a18fff2011-03-11 04:44:53 -05001628
Al Viro254cf582015-05-05 09:40:46 -04001629 *seqp = seq;
Al Viro24643082011-02-15 01:26:22 -05001630 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
Al Viro4ce16ef32012-06-10 16:10:59 -04001631 status = d_revalidate(dentry, nd->flags);
Al Viro5a18fff2011-03-11 04:44:53 -05001632 if (unlikely(status <= 0)) {
1633 if (status != -ECHILD)
1634 need_reval = 0;
1635 goto unlazy;
1636 }
Al Viro24643082011-02-15 01:26:22 -05001637 }
Trond Myklebustdaf37612015-10-09 13:44:34 -04001638 /*
1639 * Note: do negative dentry check after revalidation in
1640 * case that drops it.
1641 */
1642 if (negative)
1643 return -ENOENT;
Nick Piggin31e6b012011-01-07 17:49:52 +11001644 path->mnt = mnt;
1645 path->dentry = dentry;
Al Viro254cf582015-05-05 09:40:46 -04001646 if (likely(__follow_mount_rcu(nd, path, inode, seqp)))
NeilBrownb8faf032014-08-04 17:06:29 +10001647 return 0;
Al Viro5a18fff2011-03-11 04:44:53 -05001648unlazy:
Al Viro254cf582015-05-05 09:40:46 -04001649 if (unlazy_walk(nd, dentry, seq))
Al Viro19660af2011-03-25 10:32:48 -04001650 return -ECHILD;
Al Viro5a18fff2011-03-11 04:44:53 -05001651 } else {
Al Viroe97cdc82013-01-24 18:16:00 -05001652 dentry = __d_lookup(parent, &nd->last);
Nick Piggin31e6b012011-01-07 17:49:52 +11001653 }
Al Viro5a18fff2011-03-11 04:44:53 -05001654
Al Viro81e6f522012-03-30 14:48:04 -04001655 if (unlikely(!dentry))
1656 goto need_lookup;
Al Viro5a18fff2011-03-11 04:44:53 -05001657
Al Viro5a18fff2011-03-11 04:44:53 -05001658 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
Al Viro4ce16ef32012-06-10 16:10:59 -04001659 status = d_revalidate(dentry, nd->flags);
Al Viro5a18fff2011-03-11 04:44:53 -05001660 if (unlikely(status <= 0)) {
1661 if (status < 0) {
1662 dput(dentry);
1663 return status;
1664 }
Eric W. Biederman5542aa22014-02-13 09:46:25 -08001665 d_invalidate(dentry);
1666 dput(dentry);
1667 goto need_lookup;
Al Viro5a18fff2011-03-11 04:44:53 -05001668 }
Miklos Szeredi697f5142012-05-21 17:30:05 +02001669
Al Viro766c4cb2015-05-07 19:24:57 -04001670 if (unlikely(d_is_negative(dentry))) {
1671 dput(dentry);
1672 return -ENOENT;
1673 }
David Howells9875cf82011-01-14 18:45:21 +00001674 path->mnt = mnt;
1675 path->dentry = dentry;
NeilBrown756daf22015-03-23 13:37:38 +11001676 err = follow_managed(path, nd);
Al Viro84027522015-04-22 10:30:08 -04001677 if (likely(!err))
David Howells63afdfc2015-05-06 15:59:00 +01001678 *inode = d_backing_inode(path->dentry);
Al Viro84027522015-04-22 10:30:08 -04001679 return err;
Al Viro81e6f522012-03-30 14:48:04 -04001680
1681need_lookup:
Miklos Szeredi697f5142012-05-21 17:30:05 +02001682 return 1;
1683}
1684
1685/* Fast lookup failed, do it the slow way */
Al Virocc2a5272013-01-24 18:19:49 -05001686static int lookup_slow(struct nameidata *nd, struct path *path)
Miklos Szeredi697f5142012-05-21 17:30:05 +02001687{
1688 struct dentry *dentry, *parent;
Miklos Szeredi697f5142012-05-21 17:30:05 +02001689
1690 parent = nd->path.dentry;
Al Viro81e6f522012-03-30 14:48:04 -04001691 BUG_ON(nd->inode != parent->d_inode);
1692
1693 mutex_lock(&parent->d_inode->i_mutex);
Al Virocc2a5272013-01-24 18:19:49 -05001694 dentry = __lookup_hash(&nd->last, parent, nd->flags);
Al Viro81e6f522012-03-30 14:48:04 -04001695 mutex_unlock(&parent->d_inode->i_mutex);
1696 if (IS_ERR(dentry))
1697 return PTR_ERR(dentry);
Miklos Szeredi697f5142012-05-21 17:30:05 +02001698 path->mnt = nd->path.mnt;
1699 path->dentry = dentry;
Al Viro84027522015-04-22 10:30:08 -04001700 return follow_managed(path, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701}
1702
Al Viro52094c82011-02-21 21:34:47 -05001703static inline int may_lookup(struct nameidata *nd)
1704{
1705 if (nd->flags & LOOKUP_RCU) {
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07001706 int err = inode_permission2(nd->path.mnt, nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
Al Viro52094c82011-02-21 21:34:47 -05001707 if (err != -ECHILD)
1708 return err;
Al Viro6e9918b2015-05-05 09:26:05 -04001709 if (unlazy_walk(nd, NULL, 0))
Al Viro52094c82011-02-21 21:34:47 -05001710 return -ECHILD;
1711 }
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07001712 return inode_permission2(nd->path.mnt, nd->inode, MAY_EXEC);
Al Viro52094c82011-02-21 21:34:47 -05001713}
1714
Al Viro9856fa12011-03-04 14:22:06 -05001715static inline int handle_dots(struct nameidata *nd, int type)
1716{
1717 if (type == LAST_DOTDOT) {
1718 if (nd->flags & LOOKUP_RCU) {
Al Viro70291ae2015-05-04 07:53:00 -04001719 return follow_dotdot_rcu(nd);
Al Viro9856fa12011-03-04 14:22:06 -05001720 } else
Eric W. Biederman397d4252015-08-15 20:27:13 -05001721 return follow_dotdot(nd);
Al Viro9856fa12011-03-04 14:22:06 -05001722 }
1723 return 0;
1724}
1725
Al Viro181548c2015-05-07 19:54:34 -04001726static int pick_link(struct nameidata *nd, struct path *link,
1727 struct inode *inode, unsigned seq)
Al Virod63ff282015-05-04 18:13:23 -04001728{
Al Viro626de992015-05-04 18:26:59 -04001729 int error;
Al Viro1cf26652015-05-06 16:01:56 -04001730 struct saved *last;
NeilBrown756daf22015-03-23 13:37:38 +11001731 if (unlikely(nd->total_link_count++ >= MAXSYMLINKS)) {
Al Viro626de992015-05-04 18:26:59 -04001732 path_to_nameidata(link, nd);
1733 return -ELOOP;
1734 }
Al Virobc40aee2015-05-09 13:04:24 -04001735 if (!(nd->flags & LOOKUP_RCU)) {
Al Viro79733872015-05-09 12:55:43 -04001736 if (link->mnt == nd->path.mnt)
1737 mntget(link->mnt);
Al Virod63ff282015-05-04 18:13:23 -04001738 }
Al Viro626de992015-05-04 18:26:59 -04001739 error = nd_alloc_stack(nd);
1740 if (unlikely(error)) {
Al Virobc40aee2015-05-09 13:04:24 -04001741 if (error == -ECHILD) {
1742 if (unlikely(unlazy_link(nd, link, seq)))
1743 return -ECHILD;
1744 error = nd_alloc_stack(nd);
1745 }
1746 if (error) {
1747 path_put(link);
1748 return error;
1749 }
Al Viro626de992015-05-04 18:26:59 -04001750 }
1751
Al Viroab104922015-05-10 11:50:01 -04001752 last = nd->stack + nd->depth++;
Al Viro1cf26652015-05-06 16:01:56 -04001753 last->link = *link;
Al Viroab104922015-05-10 11:50:01 -04001754 last->cookie = NULL;
Al Viro181548c2015-05-07 19:54:34 -04001755 last->inode = inode;
Al Viro0450b2d2015-05-08 13:23:53 -04001756 last->seq = seq;
Al Virod63ff282015-05-04 18:13:23 -04001757 return 1;
1758}
1759
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001760/*
1761 * Do we need to follow links? We _really_ want to be able
1762 * to do this check without having to look at inode->i_op,
1763 * so we keep a cache of "no, this doesn't need follow_link"
1764 * for the common case.
1765 */
Al Viro254cf582015-05-05 09:40:46 -04001766static inline int should_follow_link(struct nameidata *nd, struct path *link,
Al Viro181548c2015-05-07 19:54:34 -04001767 int follow,
1768 struct inode *inode, unsigned seq)
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001769{
Al Virod63ff282015-05-04 18:13:23 -04001770 if (likely(!d_is_symlink(link->dentry)))
1771 return 0;
1772 if (!follow)
1773 return 0;
Al Viro6f4b3522016-02-27 19:31:01 -05001774 /* make sure that d_is_symlink above matches inode */
1775 if (nd->flags & LOOKUP_RCU) {
1776 if (read_seqcount_retry(&link->dentry->d_seq, seq))
1777 return -ECHILD;
1778 }
Al Viro181548c2015-05-07 19:54:34 -04001779 return pick_link(nd, link, inode, seq);
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001780}
1781
Al Viro4693a542015-05-04 17:47:11 -04001782enum {WALK_GET = 1, WALK_PUT = 2};
1783
1784static int walk_component(struct nameidata *nd, int flags)
Al Viroce57dfc2011-03-13 19:58:58 -04001785{
Al Virocaa85632015-04-22 17:52:47 -04001786 struct path path;
Al Viroce57dfc2011-03-13 19:58:58 -04001787 struct inode *inode;
Al Viro254cf582015-05-05 09:40:46 -04001788 unsigned seq;
Al Viroce57dfc2011-03-13 19:58:58 -04001789 int err;
1790 /*
1791 * "." and ".." are special - ".." especially so because it has
1792 * to be able to know about the current root directory and
1793 * parent relationships.
1794 */
Al Viro4693a542015-05-04 17:47:11 -04001795 if (unlikely(nd->last_type != LAST_NORM)) {
1796 err = handle_dots(nd, nd->last_type);
1797 if (flags & WALK_PUT)
1798 put_link(nd);
1799 return err;
1800 }
Al Viro254cf582015-05-05 09:40:46 -04001801 err = lookup_fast(nd, &path, &inode, &seq);
Al Viroce57dfc2011-03-13 19:58:58 -04001802 if (unlikely(err)) {
Miklos Szeredi697f5142012-05-21 17:30:05 +02001803 if (err < 0)
Al Virof0a9ba72015-05-04 07:59:30 -04001804 return err;
Miklos Szeredi697f5142012-05-21 17:30:05 +02001805
Al Virocaa85632015-04-22 17:52:47 -04001806 err = lookup_slow(nd, &path);
Miklos Szeredi697f5142012-05-21 17:30:05 +02001807 if (err < 0)
Al Virof0a9ba72015-05-04 07:59:30 -04001808 return err;
Miklos Szeredi697f5142012-05-21 17:30:05 +02001809
Al Viro254cf582015-05-05 09:40:46 -04001810 seq = 0; /* we are already out of RCU mode */
Al Viro766c4cb2015-05-07 19:24:57 -04001811 err = -ENOENT;
Al Virocaa85632015-04-22 17:52:47 -04001812 if (d_is_negative(path.dentry))
Al Viro766c4cb2015-05-07 19:24:57 -04001813 goto out_path_put;
Al Viroaaf15692016-02-27 19:23:16 -05001814 inode = d_backing_inode(path.dentry);
Al Viroce57dfc2011-03-13 19:58:58 -04001815 }
Miklos Szeredi697f5142012-05-21 17:30:05 +02001816
Al Viro4693a542015-05-04 17:47:11 -04001817 if (flags & WALK_PUT)
1818 put_link(nd);
Al Viro181548c2015-05-07 19:54:34 -04001819 err = should_follow_link(nd, &path, flags & WALK_GET, inode, seq);
Al Virod63ff282015-05-04 18:13:23 -04001820 if (unlikely(err))
1821 return err;
Al Virocaa85632015-04-22 17:52:47 -04001822 path_to_nameidata(&path, nd);
Al Viroce57dfc2011-03-13 19:58:58 -04001823 nd->inode = inode;
Al Viro254cf582015-05-05 09:40:46 -04001824 nd->seq = seq;
Al Viroce57dfc2011-03-13 19:58:58 -04001825 return 0;
Miklos Szeredi697f5142012-05-21 17:30:05 +02001826
1827out_path_put:
Al Virocaa85632015-04-22 17:52:47 -04001828 path_to_nameidata(&path, nd);
Miklos Szeredi697f5142012-05-21 17:30:05 +02001829 return err;
Al Viroce57dfc2011-03-13 19:58:58 -04001830}
1831
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832/*
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001833 * We can do the critical dentry name comparison and hashing
1834 * operations one word at a time, but we are limited to:
1835 *
1836 * - Architectures with fast unaligned word accesses. We could
1837 * do a "get_unaligned()" if this helps and is sufficiently
1838 * fast.
1839 *
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001840 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1841 * do not trap on the (extremely unlikely) case of a page
1842 * crossing operation.
1843 *
1844 * - Furthermore, we need an efficient 64-bit compile for the
1845 * 64-bit case in order to generate the "number of bytes in
1846 * the final mask". Again, that could be replaced with a
1847 * efficient population count instruction or similar.
1848 */
1849#ifdef CONFIG_DCACHE_WORD_ACCESS
1850
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001851#include <asm/word-at-a-time.h>
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001852
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001853#ifdef CONFIG_64BIT
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001854
1855static inline unsigned int fold_hash(unsigned long hash)
1856{
Linus Torvalds99d263d2014-09-13 11:30:10 -07001857 return hash_64(hash, 32);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001858}
1859
1860#else /* 32-bit case */
1861
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001862#define fold_hash(x) (x)
1863
1864#endif
1865
1866unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1867{
1868 unsigned long a, mask;
1869 unsigned long hash = 0;
1870
1871 for (;;) {
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001872 a = load_unaligned_zeropad(name);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001873 if (len < sizeof(unsigned long))
1874 break;
1875 hash += a;
Al Virof132c5b2012-03-22 21:59:52 +00001876 hash *= 9;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001877 name += sizeof(unsigned long);
1878 len -= sizeof(unsigned long);
1879 if (!len)
1880 goto done;
1881 }
Will Deacona5c21dc2013-12-12 17:40:21 +00001882 mask = bytemask_from_count(len);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001883 hash += mask & a;
1884done:
1885 return fold_hash(hash);
1886}
1887EXPORT_SYMBOL(full_name_hash);
1888
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001889/*
1890 * Calculate the length and hash of the path component, and
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07001891 * return the "hash_len" as the result.
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001892 */
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07001893static inline u64 hash_name(const char *name)
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001894{
Linus Torvalds36126f82012-05-26 10:43:17 -07001895 unsigned long a, b, adata, bdata, mask, hash, len;
1896 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001897
1898 hash = a = 0;
1899 len = -sizeof(unsigned long);
1900 do {
1901 hash = (hash + a) * 9;
1902 len += sizeof(unsigned long);
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001903 a = load_unaligned_zeropad(name+len);
Linus Torvalds36126f82012-05-26 10:43:17 -07001904 b = a ^ REPEAT_BYTE('/');
1905 } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001906
Linus Torvalds36126f82012-05-26 10:43:17 -07001907 adata = prep_zero_mask(a, adata, &constants);
1908 bdata = prep_zero_mask(b, bdata, &constants);
1909
1910 mask = create_zero_mask(adata | bdata);
1911
1912 hash += a & zero_bytemask(mask);
Linus Torvalds9226b5b2014-09-14 17:28:32 -07001913 len += find_zero(mask);
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07001914 return hashlen_create(fold_hash(hash), len);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001915}
1916
1917#else
1918
Linus Torvalds0145acc2012-03-02 14:32:59 -08001919unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1920{
1921 unsigned long hash = init_name_hash();
1922 while (len--)
1923 hash = partial_name_hash(*name++, hash);
1924 return end_name_hash(hash);
1925}
Linus Torvaldsae942ae2012-03-02 19:40:57 -08001926EXPORT_SYMBOL(full_name_hash);
Linus Torvalds0145acc2012-03-02 14:32:59 -08001927
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001928/*
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001929 * We know there's a real path component here of at least
1930 * one character.
1931 */
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07001932static inline u64 hash_name(const char *name)
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001933{
1934 unsigned long hash = init_name_hash();
1935 unsigned long len = 0, c;
1936
1937 c = (unsigned char)*name;
1938 do {
1939 len++;
1940 hash = partial_name_hash(c, hash);
1941 c = (unsigned char)name[len];
1942 } while (c && c != '/');
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07001943 return hashlen_create(end_name_hash(hash), len);
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001944}
1945
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001946#endif
1947
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001948/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01001950 * This is the basic name resolution function, turning a pathname into
1951 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01001953 * Returns 0 and nd will have valid dentry and mnt on success.
1954 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 */
Al Viro6de88d72009-08-09 01:41:57 +04001956static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 int err;
Al Viro32cd7462015-04-18 20:30:49 -04001959
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 while (*name=='/')
1961 name++;
1962 if (!*name)
Al Viro9e18f102015-04-18 20:44:34 -04001963 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 /* At this point we know we have a real path component. */
1966 for(;;) {
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07001967 u64 hash_len;
Al Virofe479a52011-02-22 15:10:03 -05001968 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
Al Viro52094c82011-02-21 21:34:47 -05001970 err = may_lookup(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 if (err)
Al Viro3595e232015-05-09 16:54:45 -04001972 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07001974 hash_len = hash_name(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
Al Virofe479a52011-02-22 15:10:03 -05001976 type = LAST_NORM;
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07001977 if (name[0] == '.') switch (hashlen_len(hash_len)) {
Al Virofe479a52011-02-22 15:10:03 -05001978 case 2:
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001979 if (name[1] == '.') {
Al Virofe479a52011-02-22 15:10:03 -05001980 type = LAST_DOTDOT;
Al Viro16c2cd72011-02-22 15:50:10 -05001981 nd->flags |= LOOKUP_JUMPED;
1982 }
Al Virofe479a52011-02-22 15:10:03 -05001983 break;
1984 case 1:
1985 type = LAST_DOT;
1986 }
Al Viro5a202bc2011-03-08 14:17:44 -05001987 if (likely(type == LAST_NORM)) {
1988 struct dentry *parent = nd->path.dentry;
Al Viro16c2cd72011-02-22 15:50:10 -05001989 nd->flags &= ~LOOKUP_JUMPED;
Al Viro5a202bc2011-03-08 14:17:44 -05001990 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
James Hogana060dc52014-09-16 13:07:35 +01001991 struct qstr this = { { .hash_len = hash_len }, .name = name };
Linus Torvaldsda53be12013-05-21 15:22:44 -07001992 err = parent->d_op->d_hash(parent, &this);
Al Viro5a202bc2011-03-08 14:17:44 -05001993 if (err < 0)
Al Viro3595e232015-05-09 16:54:45 -04001994 return err;
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07001995 hash_len = this.hash_len;
1996 name = this.name;
Al Viro5a202bc2011-03-08 14:17:44 -05001997 }
1998 }
Al Virofe479a52011-02-22 15:10:03 -05001999
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002000 nd->last.hash_len = hash_len;
2001 nd->last.name = name;
Al Viro5f4a6a62013-01-24 18:04:22 -05002002 nd->last_type = type;
2003
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002004 name += hashlen_len(hash_len);
2005 if (!*name)
Al Virobdf6cbf2015-04-18 20:21:40 -04002006 goto OK;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002007 /*
2008 * If it wasn't NUL, we know it was '/'. Skip that
2009 * slash, and continue until no more slashes.
2010 */
2011 do {
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002012 name++;
2013 } while (unlikely(*name == '/'));
Al Viro8620c232015-05-04 08:58:35 -04002014 if (unlikely(!*name)) {
2015OK:
Al Viro368ee9b2015-05-08 17:19:59 -04002016 /* pathname body, done */
Al Viro8620c232015-05-04 08:58:35 -04002017 if (!nd->depth)
2018 return 0;
2019 name = nd->stack[nd->depth - 1].name;
Al Viro368ee9b2015-05-08 17:19:59 -04002020 /* trailing symlink, done */
Al Viro8620c232015-05-04 08:58:35 -04002021 if (!name)
2022 return 0;
2023 /* last component of nested symlink */
Al Viro4693a542015-05-04 17:47:11 -04002024 err = walk_component(nd, WALK_GET | WALK_PUT);
Al Viro8620c232015-05-04 08:58:35 -04002025 } else {
Al Viro4693a542015-05-04 17:47:11 -04002026 err = walk_component(nd, WALK_GET);
Al Viro8620c232015-05-04 08:58:35 -04002027 }
Al Viroce57dfc2011-03-13 19:58:58 -04002028 if (err < 0)
Al Viro3595e232015-05-09 16:54:45 -04002029 return err;
Al Virofe479a52011-02-22 15:10:03 -05002030
Al Viroce57dfc2011-03-13 19:58:58 -04002031 if (err) {
Al Viro626de992015-05-04 18:26:59 -04002032 const char *s = get_link(nd);
Al Viro5a460272015-04-17 23:44:45 -04002033
Viresh Kumara1c83682015-08-12 15:59:44 +05302034 if (IS_ERR(s))
Al Viro3595e232015-05-09 16:54:45 -04002035 return PTR_ERR(s);
Al Virod40bcc02015-04-18 20:03:03 -04002036 err = 0;
2037 if (unlikely(!s)) {
2038 /* jumped */
Al Virob9ff4422015-05-02 20:19:23 -04002039 put_link(nd);
Al Virod40bcc02015-04-18 20:03:03 -04002040 } else {
Al Virofab51e82015-05-10 11:01:00 -04002041 nd->stack[nd->depth - 1].name = name;
2042 name = s;
2043 continue;
Al Virod40bcc02015-04-18 20:03:03 -04002044 }
Nick Piggin31e6b012011-01-07 17:49:52 +11002045 }
Al Viro97242f92015-08-01 19:59:28 -04002046 if (unlikely(!d_can_lookup(nd->path.dentry))) {
2047 if (nd->flags & LOOKUP_RCU) {
2048 if (unlazy_walk(nd, NULL, 0))
2049 return -ECHILD;
2050 }
Al Viro3595e232015-05-09 16:54:45 -04002051 return -ENOTDIR;
Al Viro97242f92015-08-01 19:59:28 -04002052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054}
2055
Al Viroc8a53ee2015-05-12 18:43:07 -04002056static const char *path_init(struct nameidata *nd, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057{
Prasanna Medaea3834d2005-04-29 16:00:17 +01002058 int retval = 0;
Al Viroc8a53ee2015-05-12 18:43:07 -04002059 const char *s = nd->name->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
Linus Torvalds42e9b082017-04-02 17:10:08 -07002061 if (!*s)
2062 flags &= ~LOOKUP_RCU;
2063
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 nd->last_type = LAST_ROOT; /* if there are only slashes... */
Al Viro980f3ea2014-11-20 14:20:24 -05002065 nd->flags = flags | LOOKUP_JUMPED | LOOKUP_PARENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 nd->depth = 0;
Al Viro5b6ca022011-03-09 23:04:47 -05002067 if (flags & LOOKUP_ROOT) {
David Howellsb18825a2013-09-12 19:22:53 +01002068 struct dentry *root = nd->root.dentry;
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07002069 struct vfsmount *mnt = nd->root.mnt;
David Howellsb18825a2013-09-12 19:22:53 +01002070 struct inode *inode = root->d_inode;
Al Virofd2f7cb2015-02-22 20:07:13 -05002071 if (*s) {
Miklos Szeredi44b1d532014-04-01 17:08:41 +02002072 if (!d_can_lookup(root))
Al Viro368ee9b2015-05-08 17:19:59 -04002073 return ERR_PTR(-ENOTDIR);
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07002074 retval = inode_permission2(mnt, inode, MAY_EXEC);
Al Viro73d049a2011-03-11 12:08:24 -05002075 if (retval)
Al Viro368ee9b2015-05-08 17:19:59 -04002076 return ERR_PTR(retval);
Al Viro73d049a2011-03-11 12:08:24 -05002077 }
Al Viro5b6ca022011-03-09 23:04:47 -05002078 nd->path = nd->root;
2079 nd->inode = inode;
2080 if (flags & LOOKUP_RCU) {
Al Viro8b61e742013-11-08 12:45:01 -05002081 rcu_read_lock();
Al Viro5b6ca022011-03-09 23:04:47 -05002082 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro8f47a0162015-05-09 19:02:01 -04002083 nd->root_seq = nd->seq;
Al Viro48a066e2013-09-29 22:06:07 -04002084 nd->m_seq = read_seqbegin(&mount_lock);
Al Viro5b6ca022011-03-09 23:04:47 -05002085 } else {
2086 path_get(&nd->path);
2087 }
Al Viro368ee9b2015-05-08 17:19:59 -04002088 return s;
Al Viro5b6ca022011-03-09 23:04:47 -05002089 }
2090
Al Viro2a737872009-04-07 11:49:53 -04002091 nd->root.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
Al Viro48a066e2013-09-29 22:06:07 -04002093 nd->m_seq = read_seqbegin(&mount_lock);
Al Virofd2f7cb2015-02-22 20:07:13 -05002094 if (*s == '/') {
Al Viroe41f7d42011-02-22 14:02:58 -05002095 if (flags & LOOKUP_RCU) {
Al Viro8b61e742013-11-08 12:45:01 -05002096 rcu_read_lock();
Al Viro06d71372015-06-29 12:07:04 -04002097 set_root_rcu(nd);
2098 nd->seq = nd->root_seq;
Al Viroe41f7d42011-02-22 14:02:58 -05002099 } else {
2100 set_root(nd);
2101 path_get(&nd->root);
2102 }
Al Viro2a737872009-04-07 11:49:53 -04002103 nd->path = nd->root;
Al Viroc8a53ee2015-05-12 18:43:07 -04002104 } else if (nd->dfd == AT_FDCWD) {
Al Viroe41f7d42011-02-22 14:02:58 -05002105 if (flags & LOOKUP_RCU) {
2106 struct fs_struct *fs = current->fs;
2107 unsigned seq;
2108
Al Viro8b61e742013-11-08 12:45:01 -05002109 rcu_read_lock();
Al Viroe41f7d42011-02-22 14:02:58 -05002110
2111 do {
2112 seq = read_seqcount_begin(&fs->seq);
2113 nd->path = fs->pwd;
2114 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
2115 } while (read_seqcount_retry(&fs->seq, seq));
2116 } else {
2117 get_fs_pwd(current->fs, &nd->path);
2118 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002119 } else {
Jeff Layton582aa642012-12-11 08:56:16 -05002120 /* Caller must check execute permissions on the starting path component */
Al Viroc8a53ee2015-05-12 18:43:07 -04002121 struct fd f = fdget_raw(nd->dfd);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002122 struct dentry *dentry;
2123
Al Viro2903ff02012-08-28 12:52:22 -04002124 if (!f.file)
Al Viro368ee9b2015-05-08 17:19:59 -04002125 return ERR_PTR(-EBADF);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002126
Al Viro2903ff02012-08-28 12:52:22 -04002127 dentry = f.file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002128
Al Virofd2f7cb2015-02-22 20:07:13 -05002129 if (*s) {
Miklos Szeredi44b1d532014-04-01 17:08:41 +02002130 if (!d_can_lookup(dentry)) {
Al Viro2903ff02012-08-28 12:52:22 -04002131 fdput(f);
Al Viro368ee9b2015-05-08 17:19:59 -04002132 return ERR_PTR(-ENOTDIR);
Al Viro2903ff02012-08-28 12:52:22 -04002133 }
Al Virof52e0c12011-03-14 18:56:51 -04002134 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002135
Al Viro2903ff02012-08-28 12:52:22 -04002136 nd->path = f.file->f_path;
Al Viroe41f7d42011-02-22 14:02:58 -05002137 if (flags & LOOKUP_RCU) {
Al Viro8b61e742013-11-08 12:45:01 -05002138 rcu_read_lock();
Al Viro34a26b92015-05-11 08:05:05 -04002139 nd->inode = nd->path.dentry->d_inode;
2140 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viroe41f7d42011-02-22 14:02:58 -05002141 } else {
Al Viro2903ff02012-08-28 12:52:22 -04002142 path_get(&nd->path);
Al Viro34a26b92015-05-11 08:05:05 -04002143 nd->inode = nd->path.dentry->d_inode;
Al Viroe41f7d42011-02-22 14:02:58 -05002144 }
Al Viro34a26b92015-05-11 08:05:05 -04002145 fdput(f);
Al Viro368ee9b2015-05-08 17:19:59 -04002146 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 }
Al Viroe41f7d42011-02-22 14:02:58 -05002148
Nick Piggin31e6b012011-01-07 17:49:52 +11002149 nd->inode = nd->path.dentry->d_inode;
Al Viro4023bfc2014-09-13 21:59:43 -04002150 if (!(flags & LOOKUP_RCU))
Al Viro368ee9b2015-05-08 17:19:59 -04002151 return s;
Al Viro4023bfc2014-09-13 21:59:43 -04002152 if (likely(!read_seqcount_retry(&nd->path.dentry->d_seq, nd->seq)))
Al Viro368ee9b2015-05-08 17:19:59 -04002153 return s;
Al Viro4023bfc2014-09-13 21:59:43 -04002154 if (!(nd->flags & LOOKUP_ROOT))
2155 nd->root.mnt = NULL;
2156 rcu_read_unlock();
Al Viro368ee9b2015-05-08 17:19:59 -04002157 return ERR_PTR(-ECHILD);
Al Viro9b4a9b12009-04-07 11:44:16 -04002158}
2159
Al Viro3bdba282015-05-08 17:37:07 -04002160static const char *trailing_symlink(struct nameidata *nd)
Al Viro95fa25d2015-04-22 13:46:57 -04002161{
2162 const char *s;
Al Virofec2fa22015-05-06 15:58:18 -04002163 int error = may_follow_link(nd);
Al Virodeb106c2015-05-08 18:05:21 -04002164 if (unlikely(error))
Al Viro3bdba282015-05-08 17:37:07 -04002165 return ERR_PTR(error);
Al Viro95fa25d2015-04-22 13:46:57 -04002166 nd->flags |= LOOKUP_PARENT;
Al Virofab51e82015-05-10 11:01:00 -04002167 nd->stack[0].name = NULL;
Al Viro3b2e7f72015-04-19 00:53:50 -04002168 s = get_link(nd);
Al Virodeb106c2015-05-08 18:05:21 -04002169 return s ? s : "";
Al Viro95fa25d2015-04-22 13:46:57 -04002170}
2171
Al Virocaa85632015-04-22 17:52:47 -04002172static inline int lookup_last(struct nameidata *nd)
Al Virobd92d7f2011-03-14 19:54:59 -04002173{
2174 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
2175 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2176
2177 nd->flags &= ~LOOKUP_PARENT;
Al Virodeb106c2015-05-08 18:05:21 -04002178 return walk_component(nd,
Al Viro4693a542015-05-04 17:47:11 -04002179 nd->flags & LOOKUP_FOLLOW
2180 ? nd->depth
2181 ? WALK_PUT | WALK_GET
2182 : WALK_GET
2183 : 0);
Al Virobd92d7f2011-03-14 19:54:59 -04002184}
2185
Al Viro9b4a9b12009-04-07 11:44:16 -04002186/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroc8a53ee2015-05-12 18:43:07 -04002187static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path)
Al Viro9b4a9b12009-04-07 11:44:16 -04002188{
Al Viroc8a53ee2015-05-12 18:43:07 -04002189 const char *s = path_init(nd, flags);
Al Virobd92d7f2011-03-14 19:54:59 -04002190 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11002191
Al Viro368ee9b2015-05-08 17:19:59 -04002192 if (IS_ERR(s))
2193 return PTR_ERR(s);
Al Viro3bdba282015-05-08 17:37:07 -04002194 while (!(err = link_path_walk(s, nd))
2195 && ((err = lookup_last(nd)) > 0)) {
2196 s = trailing_symlink(nd);
2197 if (IS_ERR(s)) {
2198 err = PTR_ERR(s);
2199 break;
Al Virobd92d7f2011-03-14 19:54:59 -04002200 }
2201 }
Al Viro9f1fafe2011-03-25 11:00:12 -04002202 if (!err)
2203 err = complete_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04002204
Al Virodeb106c2015-05-08 18:05:21 -04002205 if (!err && nd->flags & LOOKUP_DIRECTORY)
2206 if (!d_can_lookup(nd->path.dentry))
Al Virobd23a532011-03-23 09:56:30 -04002207 err = -ENOTDIR;
Al Viro625b6d12015-05-12 16:36:12 -04002208 if (!err) {
2209 *path = nd->path;
2210 nd->path.mnt = NULL;
2211 nd->path.dentry = NULL;
2212 }
2213 terminate_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04002214 return err;
Al Viroee0827c2011-02-21 23:38:09 -05002215}
Nick Piggin31e6b012011-01-07 17:49:52 +11002216
Al Viro625b6d12015-05-12 16:36:12 -04002217static int filename_lookup(int dfd, struct filename *name, unsigned flags,
Al Viro9ad1aaa2015-05-12 16:44:39 -04002218 struct path *path, struct path *root)
Jeff Layton873f1ee2012-10-10 15:25:29 -04002219{
Al Viro894bc8c2015-05-02 07:16:16 -04002220 int retval;
Al Viro9883d182015-05-13 07:28:08 -04002221 struct nameidata nd;
Al Viroabc9f5b2015-05-12 16:53:42 -04002222 if (IS_ERR(name))
2223 return PTR_ERR(name);
Al Viro9ad1aaa2015-05-12 16:44:39 -04002224 if (unlikely(root)) {
2225 nd.root = *root;
2226 flags |= LOOKUP_ROOT;
2227 }
Al Viro9883d182015-05-13 07:28:08 -04002228 set_nameidata(&nd, dfd, name);
Al Viroc8a53ee2015-05-12 18:43:07 -04002229 retval = path_lookupat(&nd, flags | LOOKUP_RCU, path);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002230 if (unlikely(retval == -ECHILD))
Al Viroc8a53ee2015-05-12 18:43:07 -04002231 retval = path_lookupat(&nd, flags, path);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002232 if (unlikely(retval == -ESTALE))
Al Viroc8a53ee2015-05-12 18:43:07 -04002233 retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002234
2235 if (likely(!retval))
Al Viro625b6d12015-05-12 16:36:12 -04002236 audit_inode(name, path->dentry, flags & LOOKUP_PARENT);
Al Viro9883d182015-05-13 07:28:08 -04002237 restore_nameidata();
Al Viroe4bd1c12015-05-12 16:40:39 -04002238 putname(name);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002239 return retval;
2240}
2241
Al Viro8bcb77f2015-05-08 16:59:20 -04002242/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroc8a53ee2015-05-12 18:43:07 -04002243static int path_parentat(struct nameidata *nd, unsigned flags,
Al Viro391172c2015-05-09 11:19:16 -04002244 struct path *parent)
Al Viro8bcb77f2015-05-08 16:59:20 -04002245{
Al Viroc8a53ee2015-05-12 18:43:07 -04002246 const char *s = path_init(nd, flags);
Al Viro368ee9b2015-05-08 17:19:59 -04002247 int err;
2248 if (IS_ERR(s))
2249 return PTR_ERR(s);
2250 err = link_path_walk(s, nd);
Al Viro8bcb77f2015-05-08 16:59:20 -04002251 if (!err)
2252 err = complete_walk(nd);
Al Viro391172c2015-05-09 11:19:16 -04002253 if (!err) {
2254 *parent = nd->path;
2255 nd->path.mnt = NULL;
2256 nd->path.dentry = NULL;
2257 }
2258 terminate_walk(nd);
Al Viro8bcb77f2015-05-08 16:59:20 -04002259 return err;
2260}
2261
Al Viro5c31b6c2015-05-12 17:32:54 -04002262static struct filename *filename_parentat(int dfd, struct filename *name,
Al Viro391172c2015-05-09 11:19:16 -04002263 unsigned int flags, struct path *parent,
2264 struct qstr *last, int *type)
Al Viro8bcb77f2015-05-08 16:59:20 -04002265{
2266 int retval;
Al Viro9883d182015-05-13 07:28:08 -04002267 struct nameidata nd;
Al Viro8bcb77f2015-05-08 16:59:20 -04002268
Al Viro5c31b6c2015-05-12 17:32:54 -04002269 if (IS_ERR(name))
2270 return name;
Al Viro9883d182015-05-13 07:28:08 -04002271 set_nameidata(&nd, dfd, name);
Al Viroc8a53ee2015-05-12 18:43:07 -04002272 retval = path_parentat(&nd, flags | LOOKUP_RCU, parent);
Al Viro8bcb77f2015-05-08 16:59:20 -04002273 if (unlikely(retval == -ECHILD))
Al Viroc8a53ee2015-05-12 18:43:07 -04002274 retval = path_parentat(&nd, flags, parent);
Al Viro8bcb77f2015-05-08 16:59:20 -04002275 if (unlikely(retval == -ESTALE))
Al Viroc8a53ee2015-05-12 18:43:07 -04002276 retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent);
Al Viro391172c2015-05-09 11:19:16 -04002277 if (likely(!retval)) {
2278 *last = nd.last;
2279 *type = nd.last_type;
2280 audit_inode(name, parent->dentry, LOOKUP_PARENT);
Al Viro5c31b6c2015-05-12 17:32:54 -04002281 } else {
2282 putname(name);
2283 name = ERR_PTR(retval);
Al Viro391172c2015-05-09 11:19:16 -04002284 }
Al Viro9883d182015-05-13 07:28:08 -04002285 restore_nameidata();
Al Viro5c31b6c2015-05-12 17:32:54 -04002286 return name;
Al Viro8bcb77f2015-05-08 16:59:20 -04002287}
2288
Al Viro79714f72012-06-15 03:01:42 +04002289/* does lookup, returns the object with parent locked */
2290struct dentry *kern_path_locked(const char *name, struct path *path)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002291{
Al Viro5c31b6c2015-05-12 17:32:54 -04002292 struct filename *filename;
2293 struct dentry *d;
Al Viro391172c2015-05-09 11:19:16 -04002294 struct qstr last;
2295 int type;
Paul Moore51689102015-01-22 00:00:03 -05002296
Al Viro5c31b6c2015-05-12 17:32:54 -04002297 filename = filename_parentat(AT_FDCWD, getname_kernel(name), 0, path,
2298 &last, &type);
Paul Moore51689102015-01-22 00:00:03 -05002299 if (IS_ERR(filename))
2300 return ERR_CAST(filename);
Al Viro5c31b6c2015-05-12 17:32:54 -04002301 if (unlikely(type != LAST_NORM)) {
Al Viro391172c2015-05-09 11:19:16 -04002302 path_put(path);
Al Viro5c31b6c2015-05-12 17:32:54 -04002303 putname(filename);
2304 return ERR_PTR(-EINVAL);
Al Viro79714f72012-06-15 03:01:42 +04002305 }
Al Viro391172c2015-05-09 11:19:16 -04002306 mutex_lock_nested(&path->dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2307 d = __lookup_hash(&last, path->dentry, 0);
Al Viro79714f72012-06-15 03:01:42 +04002308 if (IS_ERR(d)) {
Al Viro391172c2015-05-09 11:19:16 -04002309 mutex_unlock(&path->dentry->d_inode->i_mutex);
2310 path_put(path);
Al Viro79714f72012-06-15 03:01:42 +04002311 }
Paul Moore51689102015-01-22 00:00:03 -05002312 putname(filename);
Al Viro79714f72012-06-15 03:01:42 +04002313 return d;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002314}
2315
Al Virod1811462008-08-02 00:49:18 -04002316int kern_path(const char *name, unsigned int flags, struct path *path)
2317{
Al Viroabc9f5b2015-05-12 16:53:42 -04002318 return filename_lookup(AT_FDCWD, getname_kernel(name),
2319 flags, path, NULL);
Al Virod1811462008-08-02 00:49:18 -04002320}
Al Viro4d359502014-03-14 12:20:17 -04002321EXPORT_SYMBOL(kern_path);
Al Virod1811462008-08-02 00:49:18 -04002322
Josef 'Jeff' Sipek16f182002007-07-19 01:48:18 -07002323/**
2324 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2325 * @dentry: pointer to dentry of the base directory
2326 * @mnt: pointer to vfs mount of the base directory
2327 * @name: pointer to file name
2328 * @flags: lookup flags
Al Viroe0a01242011-06-27 17:00:37 -04002329 * @path: pointer to struct path to fill
Josef 'Jeff' Sipek16f182002007-07-19 01:48:18 -07002330 */
2331int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2332 const char *name, unsigned int flags,
Al Viroe0a01242011-06-27 17:00:37 -04002333 struct path *path)
Josef 'Jeff' Sipek16f182002007-07-19 01:48:18 -07002334{
Al Viro9ad1aaa2015-05-12 16:44:39 -04002335 struct path root = {.mnt = mnt, .dentry = dentry};
Al Viro9ad1aaa2015-05-12 16:44:39 -04002336 /* the first argument of filename_lookup() is ignored with root */
Al Viroabc9f5b2015-05-12 16:53:42 -04002337 return filename_lookup(AT_FDCWD, getname_kernel(name),
2338 flags , path, &root);
Josef 'Jeff' Sipek16f182002007-07-19 01:48:18 -07002339}
Al Viro4d359502014-03-14 12:20:17 -04002340EXPORT_SYMBOL(vfs_path_lookup);
Josef 'Jeff' Sipek16f182002007-07-19 01:48:18 -07002341
Christoph Hellwigeead1912007-10-16 23:25:38 -07002342/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07002343 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07002344 * @name: pathname component to lookup
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07002345 * @mnt: mount we are looking up on
Christoph Hellwigeead1912007-10-16 23:25:38 -07002346 * @base: base directory to lookup from
2347 * @len: maximum length @len should be interpreted to
2348 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07002349 * Note that this routine is purely a helper for filesystem usage and should
Al Viro9e7543e2015-02-23 02:49:48 -05002350 * not be called by generic code.
Christoph Hellwigeead1912007-10-16 23:25:38 -07002351 */
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07002352struct dentry *lookup_one_len2(const char *name, struct vfsmount *mnt, struct dentry *base, int len)
James Morris057f6c02007-04-26 00:12:05 -07002353{
James Morris057f6c02007-04-26 00:12:05 -07002354 struct qstr this;
Al Viro6a96ba52011-03-07 23:49:20 -05002355 unsigned int c;
Miklos Szeredicda309d2012-03-26 12:54:21 +02002356 int err;
James Morris057f6c02007-04-26 00:12:05 -07002357
David Woodhouse2f9092e2009-04-20 23:18:37 +01002358 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
2359
Al Viro6a96ba52011-03-07 23:49:20 -05002360 this.name = name;
2361 this.len = len;
Linus Torvalds0145acc2012-03-02 14:32:59 -08002362 this.hash = full_name_hash(name, len);
Al Viro6a96ba52011-03-07 23:49:20 -05002363 if (!len)
2364 return ERR_PTR(-EACCES);
2365
Al Viro21d8a152012-11-29 22:17:21 -05002366 if (unlikely(name[0] == '.')) {
2367 if (len < 2 || (len == 2 && name[1] == '.'))
2368 return ERR_PTR(-EACCES);
2369 }
2370
Al Viro6a96ba52011-03-07 23:49:20 -05002371 while (len--) {
2372 c = *(const unsigned char *)name++;
2373 if (c == '/' || c == '\0')
2374 return ERR_PTR(-EACCES);
Al Viro6a96ba52011-03-07 23:49:20 -05002375 }
Al Viro5a202bc2011-03-08 14:17:44 -05002376 /*
2377 * See if the low-level filesystem might want
2378 * to use its own hash..
2379 */
2380 if (base->d_flags & DCACHE_OP_HASH) {
Linus Torvaldsda53be12013-05-21 15:22:44 -07002381 int err = base->d_op->d_hash(base, &this);
Al Viro5a202bc2011-03-08 14:17:44 -05002382 if (err < 0)
2383 return ERR_PTR(err);
2384 }
Christoph Hellwigeead1912007-10-16 23:25:38 -07002385
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07002386 err = inode_permission2(mnt, base->d_inode, MAY_EXEC);
Miklos Szeredicda309d2012-03-26 12:54:21 +02002387 if (err)
2388 return ERR_PTR(err);
2389
Al Viro72bd8662012-06-10 17:17:17 -04002390 return __lookup_hash(&this, base, 0);
James Morris057f6c02007-04-26 00:12:05 -07002391}
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07002392EXPORT_SYMBOL(lookup_one_len2);
2393
2394struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2395{
2396 return lookup_one_len2(name, NULL, base, len);
2397}
Al Viro4d359502014-03-14 12:20:17 -04002398EXPORT_SYMBOL(lookup_one_len);
James Morris057f6c02007-04-26 00:12:05 -07002399
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002400int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2401 struct path *path, int *empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402{
Al Viroabc9f5b2015-05-12 16:53:42 -04002403 return filename_lookup(dfd, getname_flags(name, flags, empty),
2404 flags, path, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405}
Al Virob853a162015-05-13 09:12:02 -04002406EXPORT_SYMBOL(user_path_at_empty);
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002407
Jeff Layton873f1ee2012-10-10 15:25:29 -04002408/*
2409 * NB: most callers don't do anything directly with the reference to the
2410 * to struct filename, but the nd->last pointer points into the name string
2411 * allocated by getname. So we must hold the reference to it until all
2412 * path-walking is complete.
2413 */
Al Viroa2ec4a22015-05-13 06:57:49 -04002414static inline struct filename *
Al Virof5beed72015-04-30 16:09:11 -04002415user_path_parent(int dfd, const char __user *path,
2416 struct path *parent,
2417 struct qstr *last,
2418 int *type,
Jeff Layton9e790bd2012-12-11 12:10:09 -05002419 unsigned int flags)
Al Viro2ad94ae2008-07-21 09:32:51 -04002420{
Jeff Layton9e790bd2012-12-11 12:10:09 -05002421 /* only LOOKUP_REVAL is allowed in extra flags */
Al Viro5c31b6c2015-05-12 17:32:54 -04002422 return filename_parentat(dfd, getname(path), flags & LOOKUP_REVAL,
2423 parent, last, type);
Al Viro2ad94ae2008-07-21 09:32:51 -04002424}
2425
Jeff Layton80334262013-07-26 06:23:25 -04002426/**
Al Viro197df042013-09-08 14:03:27 -04002427 * mountpoint_last - look up last component for umount
Jeff Layton80334262013-07-26 06:23:25 -04002428 * @nd: pathwalk nameidata - currently pointing at parent directory of "last"
2429 * @path: pointer to container for result
2430 *
2431 * This is a special lookup_last function just for umount. In this case, we
2432 * need to resolve the path without doing any revalidation.
2433 *
2434 * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
2435 * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
2436 * in almost all cases, this lookup will be served out of the dcache. The only
2437 * cases where it won't are if nd->last refers to a symlink or the path is
2438 * bogus and it doesn't exist.
2439 *
2440 * Returns:
2441 * -error: if there was an error during lookup. This includes -ENOENT if the
2442 * lookup found a negative dentry. The nd->path reference will also be
2443 * put in this case.
2444 *
2445 * 0: if we successfully resolved nd->path and found it to not to be a
2446 * symlink that needs to be followed. "path" will also be populated.
2447 * The nd->path reference will also be put.
2448 *
2449 * 1: if we successfully resolved nd->last and found it to be a symlink
2450 * that needs to be followed. "path" will be populated with the path
2451 * to the link, and nd->path will *not* be put.
2452 */
2453static int
Al Viro197df042013-09-08 14:03:27 -04002454mountpoint_last(struct nameidata *nd, struct path *path)
Jeff Layton80334262013-07-26 06:23:25 -04002455{
2456 int error = 0;
2457 struct dentry *dentry;
2458 struct dentry *dir = nd->path.dentry;
2459
Al Viro35759522013-09-08 13:41:33 -04002460 /* If we're in rcuwalk, drop out of it to handle last component */
2461 if (nd->flags & LOOKUP_RCU) {
Al Viro6e9918b2015-05-05 09:26:05 -04002462 if (unlazy_walk(nd, NULL, 0))
Al Virodeb106c2015-05-08 18:05:21 -04002463 return -ECHILD;
Jeff Layton80334262013-07-26 06:23:25 -04002464 }
2465
2466 nd->flags &= ~LOOKUP_PARENT;
2467
2468 if (unlikely(nd->last_type != LAST_NORM)) {
2469 error = handle_dots(nd, nd->last_type);
Al Viro35759522013-09-08 13:41:33 -04002470 if (error)
Al Virodeb106c2015-05-08 18:05:21 -04002471 return error;
Al Viro35759522013-09-08 13:41:33 -04002472 dentry = dget(nd->path.dentry);
2473 goto done;
Jeff Layton80334262013-07-26 06:23:25 -04002474 }
2475
2476 mutex_lock(&dir->d_inode->i_mutex);
2477 dentry = d_lookup(dir, &nd->last);
2478 if (!dentry) {
2479 /*
2480 * No cached dentry. Mounted dentries are pinned in the cache,
2481 * so that means that this dentry is probably a symlink or the
2482 * path doesn't actually point to a mounted dentry.
2483 */
2484 dentry = d_alloc(dir, &nd->last);
2485 if (!dentry) {
Dave Jonesbcceeeb2013-09-10 17:04:25 -04002486 mutex_unlock(&dir->d_inode->i_mutex);
Al Virodeb106c2015-05-08 18:05:21 -04002487 return -ENOMEM;
Jeff Layton80334262013-07-26 06:23:25 -04002488 }
Al Viro35759522013-09-08 13:41:33 -04002489 dentry = lookup_real(dir->d_inode, dentry, nd->flags);
Dave Jonesbcceeeb2013-09-10 17:04:25 -04002490 if (IS_ERR(dentry)) {
2491 mutex_unlock(&dir->d_inode->i_mutex);
Al Virodeb106c2015-05-08 18:05:21 -04002492 return PTR_ERR(dentry);
Dave Jonesbcceeeb2013-09-10 17:04:25 -04002493 }
Jeff Layton80334262013-07-26 06:23:25 -04002494 }
2495 mutex_unlock(&dir->d_inode->i_mutex);
2496
Al Viro35759522013-09-08 13:41:33 -04002497done:
David Howells698934d2015-03-17 17:33:52 +00002498 if (d_is_negative(dentry)) {
Al Viro35759522013-09-08 13:41:33 -04002499 dput(dentry);
Al Virodeb106c2015-05-08 18:05:21 -04002500 return -ENOENT;
Jeff Layton80334262013-07-26 06:23:25 -04002501 }
Al Viro191d7f72015-05-04 08:26:45 -04002502 if (nd->depth)
2503 put_link(nd);
Al Viro35759522013-09-08 13:41:33 -04002504 path->dentry = dentry;
Vasily Averin295dc392014-07-21 12:30:23 +04002505 path->mnt = nd->path.mnt;
Al Viro181548c2015-05-07 19:54:34 -04002506 error = should_follow_link(nd, path, nd->flags & LOOKUP_FOLLOW,
2507 d_backing_inode(dentry), 0);
Al Virodeb106c2015-05-08 18:05:21 -04002508 if (unlikely(error))
Al Virod63ff282015-05-04 18:13:23 -04002509 return error;
Vasily Averin295dc392014-07-21 12:30:23 +04002510 mntget(path->mnt);
Al Viro35759522013-09-08 13:41:33 -04002511 follow_mount(path);
Al Virodeb106c2015-05-08 18:05:21 -04002512 return 0;
Jeff Layton80334262013-07-26 06:23:25 -04002513}
2514
2515/**
Al Viro197df042013-09-08 14:03:27 -04002516 * path_mountpoint - look up a path to be umounted
Masanari Iida2a78b852015-09-09 15:39:23 -07002517 * @nd: lookup context
Jeff Layton80334262013-07-26 06:23:25 -04002518 * @flags: lookup flags
Al Viroc8a53ee2015-05-12 18:43:07 -04002519 * @path: pointer to container for result
Jeff Layton80334262013-07-26 06:23:25 -04002520 *
2521 * Look up the given name, but don't attempt to revalidate the last component.
Randy Dunlap606d6fe2013-10-19 14:56:55 -07002522 * Returns 0 and "path" will be valid on success; Returns error otherwise.
Jeff Layton80334262013-07-26 06:23:25 -04002523 */
2524static int
Al Viroc8a53ee2015-05-12 18:43:07 -04002525path_mountpoint(struct nameidata *nd, unsigned flags, struct path *path)
Jeff Layton80334262013-07-26 06:23:25 -04002526{
Al Viroc8a53ee2015-05-12 18:43:07 -04002527 const char *s = path_init(nd, flags);
Al Viro368ee9b2015-05-08 17:19:59 -04002528 int err;
2529 if (IS_ERR(s))
2530 return PTR_ERR(s);
Al Viro3bdba282015-05-08 17:37:07 -04002531 while (!(err = link_path_walk(s, nd)) &&
2532 (err = mountpoint_last(nd, path)) > 0) {
2533 s = trailing_symlink(nd);
2534 if (IS_ERR(s)) {
2535 err = PTR_ERR(s);
Jeff Layton80334262013-07-26 06:23:25 -04002536 break;
Al Viro3bdba282015-05-08 17:37:07 -04002537 }
Jeff Layton80334262013-07-26 06:23:25 -04002538 }
Al Virodeb106c2015-05-08 18:05:21 -04002539 terminate_walk(nd);
Jeff Layton80334262013-07-26 06:23:25 -04002540 return err;
2541}
2542
Al Viro2d864652013-09-08 20:18:44 -04002543static int
Al Viro668696d2015-02-22 19:44:00 -05002544filename_mountpoint(int dfd, struct filename *name, struct path *path,
Al Viro2d864652013-09-08 20:18:44 -04002545 unsigned int flags)
2546{
Al Viro9883d182015-05-13 07:28:08 -04002547 struct nameidata nd;
Al Virocbaab2d2015-01-22 02:49:00 -05002548 int error;
Al Viro668696d2015-02-22 19:44:00 -05002549 if (IS_ERR(name))
2550 return PTR_ERR(name);
Al Viro9883d182015-05-13 07:28:08 -04002551 set_nameidata(&nd, dfd, name);
Al Viroc8a53ee2015-05-12 18:43:07 -04002552 error = path_mountpoint(&nd, flags | LOOKUP_RCU, path);
Al Viro2d864652013-09-08 20:18:44 -04002553 if (unlikely(error == -ECHILD))
Al Viroc8a53ee2015-05-12 18:43:07 -04002554 error = path_mountpoint(&nd, flags, path);
Al Viro2d864652013-09-08 20:18:44 -04002555 if (unlikely(error == -ESTALE))
Al Viroc8a53ee2015-05-12 18:43:07 -04002556 error = path_mountpoint(&nd, flags | LOOKUP_REVAL, path);
Al Viro2d864652013-09-08 20:18:44 -04002557 if (likely(!error))
Al Viro668696d2015-02-22 19:44:00 -05002558 audit_inode(name, path->dentry, 0);
Al Viro9883d182015-05-13 07:28:08 -04002559 restore_nameidata();
Al Viro668696d2015-02-22 19:44:00 -05002560 putname(name);
Al Viro2d864652013-09-08 20:18:44 -04002561 return error;
2562}
2563
Jeff Layton80334262013-07-26 06:23:25 -04002564/**
Al Viro197df042013-09-08 14:03:27 -04002565 * user_path_mountpoint_at - lookup a path from userland in order to umount it
Jeff Layton80334262013-07-26 06:23:25 -04002566 * @dfd: directory file descriptor
2567 * @name: pathname from userland
2568 * @flags: lookup flags
2569 * @path: pointer to container to hold result
2570 *
2571 * A umount is a special case for path walking. We're not actually interested
2572 * in the inode in this situation, and ESTALE errors can be a problem. We
2573 * simply want track down the dentry and vfsmount attached at the mountpoint
2574 * and avoid revalidating the last component.
2575 *
2576 * Returns 0 and populates "path" on success.
2577 */
2578int
Al Viro197df042013-09-08 14:03:27 -04002579user_path_mountpoint_at(int dfd, const char __user *name, unsigned int flags,
Jeff Layton80334262013-07-26 06:23:25 -04002580 struct path *path)
2581{
Al Virocbaab2d2015-01-22 02:49:00 -05002582 return filename_mountpoint(dfd, getname(name), path, flags);
Jeff Layton80334262013-07-26 06:23:25 -04002583}
2584
Al Viro2d864652013-09-08 20:18:44 -04002585int
2586kern_path_mountpoint(int dfd, const char *name, struct path *path,
2587 unsigned int flags)
2588{
Al Virocbaab2d2015-01-22 02:49:00 -05002589 return filename_mountpoint(dfd, getname_kernel(name), path, flags);
Al Viro2d864652013-09-08 20:18:44 -04002590}
2591EXPORT_SYMBOL(kern_path_mountpoint);
2592
Miklos Szeredicbdf35b2014-10-24 00:14:36 +02002593int __check_sticky(struct inode *dir, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594{
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002595 kuid_t fsuid = current_fsuid();
David Howellsda9592e2008-11-14 10:39:05 +11002596
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002597 if (uid_eq(inode->i_uid, fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 return 0;
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002599 if (uid_eq(dir->i_uid, fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 return 0;
Andy Lutomirski23adbe12014-06-10 12:45:42 -07002601 return !capable_wrt_inode_uidgid(inode, CAP_FOWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602}
Miklos Szeredicbdf35b2014-10-24 00:14:36 +02002603EXPORT_SYMBOL(__check_sticky);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
2605/*
2606 * Check whether we can remove a link victim from directory dir, check
2607 * whether the type of victim is right.
2608 * 1. We can't do it if dir is read-only (done in permission())
2609 * 2. We should have write and exec permissions on dir
2610 * 3. We can't remove anything from append-only dir
2611 * 4. We can't do anything with immutable dir (done in permission())
2612 * 5. If the sticky bit on dir is set we should either
2613 * a. be owner of dir, or
2614 * b. be owner of victim, or
2615 * c. have CAP_FOWNER capability
2616 * 6. If the victim is append-only or immutable we can't do antyhing with
2617 * links pointing to it.
2618 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2619 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2620 * 9. We can't remove a root or mountpoint.
2621 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
2622 * nfs_async_unlink().
2623 */
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07002624static int may_delete(struct vfsmount *mnt, struct inode *dir, struct dentry *victim, bool isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625{
David Howells63afdfc2015-05-06 15:59:00 +01002626 struct inode *inode = d_backing_inode(victim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 int error;
2628
David Howellsb18825a2013-09-12 19:22:53 +01002629 if (d_is_negative(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 return -ENOENT;
David Howellsb18825a2013-09-12 19:22:53 +01002631 BUG_ON(!inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632
2633 BUG_ON(victim->d_parent->d_inode != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04002634 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07002636 error = inode_permission2(mnt, dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 if (error)
2638 return error;
2639 if (IS_APPEND(dir))
2640 return -EPERM;
David Howellsb18825a2013-09-12 19:22:53 +01002641
2642 if (check_sticky(dir, inode) || IS_APPEND(inode) ||
2643 IS_IMMUTABLE(inode) || IS_SWAPFILE(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 return -EPERM;
2645 if (isdir) {
Miklos Szeredi44b1d532014-04-01 17:08:41 +02002646 if (!d_is_dir(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 return -ENOTDIR;
2648 if (IS_ROOT(victim))
2649 return -EBUSY;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02002650 } else if (d_is_dir(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 return -EISDIR;
2652 if (IS_DEADDIR(dir))
2653 return -ENOENT;
2654 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2655 return -EBUSY;
2656 return 0;
2657}
2658
2659/* Check whether we can create an object with dentry child in directory
2660 * dir.
2661 * 1. We can't do it if child already exists (open has special treatment for
2662 * this case, but since we are inlined it's OK)
2663 * 2. We can't do it if dir is read-only (done in permission())
2664 * 3. We should have write and exec permissions on dir
2665 * 4. We can't do it if dir is immutable (done in permission())
2666 */
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07002667static inline int may_create(struct vfsmount *mnt, struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668{
Jeff Layton14e972b2013-05-08 10:25:58 -04002669 audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 if (child->d_inode)
2671 return -EEXIST;
2672 if (IS_DEADDIR(dir))
2673 return -ENOENT;
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07002674 return inode_permission2(mnt, dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675}
2676
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677/*
2678 * p1 and p2 should be directories on the same fs.
2679 */
2680struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2681{
2682 struct dentry *p;
2683
2684 if (p1 == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07002685 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 return NULL;
2687 }
2688
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002689 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002691 p = d_ancestor(p2, p1);
2692 if (p) {
2693 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2694 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
2695 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 }
2697
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002698 p = d_ancestor(p1, p2);
2699 if (p) {
2700 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2701 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2702 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 }
2704
Ingo Molnarf2eace22006-07-03 00:25:05 -07002705 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Miklos Szeredid1b72cc2014-10-27 15:42:01 +01002706 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 return NULL;
2708}
Al Viro4d359502014-03-14 12:20:17 -04002709EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710
2711void unlock_rename(struct dentry *p1, struct dentry *p2)
2712{
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002713 mutex_unlock(&p1->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 if (p1 != p2) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002715 mutex_unlock(&p2->d_inode->i_mutex);
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002716 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 }
2718}
Al Viro4d359502014-03-14 12:20:17 -04002719EXPORT_SYMBOL(unlock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07002721int vfs_create2(struct vfsmount *mnt, struct inode *dir, struct dentry *dentry,
2722 umode_t mode, bool want_excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723{
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07002724 int error = may_create(mnt, dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 if (error)
2726 return error;
2727
Al Viroacfa4382008-12-04 10:06:33 -05002728 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 return -EACCES; /* shouldn't it be ENOSYS? */
2730 mode &= S_IALLUGO;
2731 mode |= S_IFREG;
2732 error = security_inode_create(dir, dentry, mode);
2733 if (error)
2734 return error;
Al Viro312b63f2012-06-10 18:09:36 -04002735 error = dir->i_op->create(dir, dentry, mode, want_excl);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002736 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002737 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 return error;
2739}
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07002740EXPORT_SYMBOL(vfs_create2);
2741
2742int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2743 bool want_excl)
2744{
2745 return vfs_create2(NULL, dir, dentry, mode, want_excl);
2746}
Al Viro4d359502014-03-14 12:20:17 -04002747EXPORT_SYMBOL(vfs_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748
Al Viro73d049a2011-03-11 12:08:24 -05002749static int may_open(struct path *path, int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002751 struct dentry *dentry = path->dentry;
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07002752 struct vfsmount *mnt = path->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 struct inode *inode = dentry->d_inode;
2754 int error;
2755
Al Virobcda7652011-03-13 16:42:14 -04002756 /* O_PATH? */
2757 if (!acc_mode)
2758 return 0;
2759
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 if (!inode)
2761 return -ENOENT;
2762
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002763 switch (inode->i_mode & S_IFMT) {
2764 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002766 case S_IFDIR:
2767 if (acc_mode & MAY_WRITE)
2768 return -EISDIR;
2769 break;
2770 case S_IFBLK:
2771 case S_IFCHR:
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002772 if (path->mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002774 /*FALLTHRU*/
2775 case S_IFIFO:
2776 case S_IFSOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002778 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08002779 }
Dave Hansenb41572e2007-10-16 23:31:14 -07002780
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07002781 error = inode_permission2(mnt, inode, acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07002782 if (error)
2783 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05002784
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 /*
2786 * An append-only file must be opened in append mode for writing.
2787 */
2788 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05002789 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05002790 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05002792 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 }
2794
2795 /* O_NOATIME can only be set by the owner or superuser */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07002796 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
Al Viro7715b522009-12-16 03:54:00 -05002797 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798
J. Bruce Fieldsf3c7691e2011-09-21 10:58:13 -04002799 return 0;
Al Viro7715b522009-12-16 03:54:00 -05002800}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801
Jeff Laytone1181ee2010-12-07 16:19:50 -05002802static int handle_truncate(struct file *filp)
Al Viro7715b522009-12-16 03:54:00 -05002803{
Jeff Laytone1181ee2010-12-07 16:19:50 -05002804 struct path *path = &filp->f_path;
Al Viro7715b522009-12-16 03:54:00 -05002805 struct inode *inode = path->dentry->d_inode;
2806 int error = get_write_access(inode);
2807 if (error)
2808 return error;
2809 /*
2810 * Refuse to truncate files with mandatory locks held on them.
2811 */
Jeff Laytond7a06982014-03-10 09:54:15 -04002812 error = locks_verify_locked(filp);
Al Viro7715b522009-12-16 03:54:00 -05002813 if (!error)
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +09002814 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05002815 if (!error) {
Daniel Rosenberge5eeaaf2016-10-26 16:33:11 -07002816 error = do_truncate2(path->mnt, path->dentry, 0,
Al Viro7715b522009-12-16 03:54:00 -05002817 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
Jeff Laytone1181ee2010-12-07 16:19:50 -05002818 filp);
Al Viro7715b522009-12-16 03:54:00 -05002819 }
2820 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04002821 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822}
2823
Dave Hansend57999e2008-02-15 14:37:27 -08002824static inline int open_to_namei_flags(int flag)
2825{
Al Viro8a5e9292011-06-25 19:15:54 -04002826 if ((flag & O_ACCMODE) == 3)
2827 flag--;
Dave Hansend57999e2008-02-15 14:37:27 -08002828 return flag;
2829}
2830
Miklos Szeredid18e9002012-06-05 15:10:17 +02002831static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode)
2832{
2833 int error = security_path_mknod(dir, dentry, mode, 0);
2834 if (error)
2835 return error;
2836
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07002837 error = inode_permission2(dir->mnt, dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002838 if (error)
2839 return error;
2840
2841 return security_inode_create(dir->dentry->d_inode, dentry, mode);
2842}
2843
David Howells1acf0af2012-06-14 16:13:46 +01002844/*
2845 * Attempt to atomically look up, create and open a file from a negative
2846 * dentry.
2847 *
2848 * Returns 0 if successful. The file will have been created and attached to
2849 * @file by the filesystem calling finish_open().
2850 *
2851 * Returns 1 if the file was looked up only or didn't need creating. The
2852 * caller will need to perform the open themselves. @path will have been
2853 * updated to point to the new dentry. This may be negative.
2854 *
2855 * Returns an error code otherwise.
2856 */
Al Viro2675a4e2012-06-22 12:41:10 +04002857static int atomic_open(struct nameidata *nd, struct dentry *dentry,
2858 struct path *path, struct file *file,
2859 const struct open_flags *op,
Al Viro64894cf2012-07-31 00:53:35 +04002860 bool got_write, bool need_lookup,
Al Viro2675a4e2012-06-22 12:41:10 +04002861 int *opened)
Miklos Szeredid18e9002012-06-05 15:10:17 +02002862{
2863 struct inode *dir = nd->path.dentry->d_inode;
2864 unsigned open_flag = open_to_namei_flags(op->open_flag);
2865 umode_t mode;
2866 int error;
2867 int acc_mode;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002868 int create_error = 0;
2869 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
Miklos Szeredi116cc022013-09-16 14:52:05 +02002870 bool excl;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002871
2872 BUG_ON(dentry->d_inode);
2873
2874 /* Don't create child dentry for a dead directory. */
2875 if (unlikely(IS_DEADDIR(dir))) {
Al Viro2675a4e2012-06-22 12:41:10 +04002876 error = -ENOENT;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002877 goto out;
2878 }
2879
Miklos Szeredi62b259d2012-08-15 13:01:24 +02002880 mode = op->mode;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002881 if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
2882 mode &= ~current_umask();
2883
Miklos Szeredi116cc022013-09-16 14:52:05 +02002884 excl = (open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT);
2885 if (excl)
Miklos Szeredid18e9002012-06-05 15:10:17 +02002886 open_flag &= ~O_TRUNC;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002887
2888 /*
2889 * Checking write permission is tricky, bacuse we don't know if we are
2890 * going to actually need it: O_CREAT opens should work as long as the
2891 * file exists. But checking existence breaks atomicity. The trick is
2892 * to check access and if not granted clear O_CREAT from the flags.
2893 *
2894 * Another problem is returing the "right" error value (e.g. for an
2895 * O_EXCL open we want to return EEXIST not EROFS).
2896 */
Al Viro64894cf2012-07-31 00:53:35 +04002897 if (((open_flag & (O_CREAT | O_TRUNC)) ||
2898 (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) {
2899 if (!(open_flag & O_CREAT)) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002900 /*
2901 * No O_CREATE -> atomicity not a requirement -> fall
2902 * back to lookup + open
2903 */
2904 goto no_open;
2905 } else if (open_flag & (O_EXCL | O_TRUNC)) {
2906 /* Fall back and fail with the right error */
Al Viro64894cf2012-07-31 00:53:35 +04002907 create_error = -EROFS;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002908 goto no_open;
2909 } else {
2910 /* No side effects, safe to clear O_CREAT */
Al Viro64894cf2012-07-31 00:53:35 +04002911 create_error = -EROFS;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002912 open_flag &= ~O_CREAT;
2913 }
2914 }
2915
2916 if (open_flag & O_CREAT) {
Miklos Szeredi38227f72012-08-15 13:01:24 +02002917 error = may_o_create(&nd->path, dentry, mode);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002918 if (error) {
2919 create_error = error;
2920 if (open_flag & O_EXCL)
2921 goto no_open;
2922 open_flag &= ~O_CREAT;
2923 }
2924 }
2925
2926 if (nd->flags & LOOKUP_DIRECTORY)
2927 open_flag |= O_DIRECTORY;
2928
Al Viro30d90492012-06-22 12:40:19 +04002929 file->f_path.dentry = DENTRY_NOT_SET;
2930 file->f_path.mnt = nd->path.mnt;
2931 error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode,
Al Viro47237682012-06-10 05:01:45 -04002932 opened);
Al Virod9585272012-06-22 12:39:14 +04002933 if (error < 0) {
Al Virod9585272012-06-22 12:39:14 +04002934 if (create_error && error == -ENOENT)
2935 error = create_error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002936 goto out;
2937 }
2938
Al Virod9585272012-06-22 12:39:14 +04002939 if (error) { /* returned 1, that is */
Al Viro30d90492012-06-22 12:40:19 +04002940 if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
Al Viro2675a4e2012-06-22 12:41:10 +04002941 error = -EIO;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002942 goto out;
2943 }
Al Viro30d90492012-06-22 12:40:19 +04002944 if (file->f_path.dentry) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002945 dput(dentry);
Al Viro30d90492012-06-22 12:40:19 +04002946 dentry = file->f_path.dentry;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002947 }
Al Viro03da6332013-09-16 19:22:33 -04002948 if (*opened & FILE_CREATED)
2949 fsnotify_create(dir, dentry);
2950 if (!dentry->d_inode) {
2951 WARN_ON(*opened & FILE_CREATED);
2952 if (create_error) {
2953 error = create_error;
2954 goto out;
2955 }
2956 } else {
2957 if (excl && !(*opened & FILE_CREATED)) {
2958 error = -EEXIST;
2959 goto out;
2960 }
Sage Weil62b2ce92012-08-15 13:30:12 -07002961 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02002962 goto looked_up;
2963 }
2964
2965 /*
2966 * We didn't have the inode before the open, so check open permission
2967 * here.
2968 */
Al Viro03da6332013-09-16 19:22:33 -04002969 acc_mode = op->acc_mode;
2970 if (*opened & FILE_CREATED) {
2971 WARN_ON(!(open_flag & O_CREAT));
2972 fsnotify_create(dir, dentry);
2973 acc_mode = MAY_OPEN;
2974 }
Al Viro2675a4e2012-06-22 12:41:10 +04002975 error = may_open(&file->f_path, acc_mode, open_flag);
2976 if (error)
2977 fput(file);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002978
2979out:
2980 dput(dentry);
Al Viro2675a4e2012-06-22 12:41:10 +04002981 return error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002982
Miklos Szeredid18e9002012-06-05 15:10:17 +02002983no_open:
2984 if (need_lookup) {
Al Viro72bd8662012-06-10 17:17:17 -04002985 dentry = lookup_real(dir, dentry, nd->flags);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002986 if (IS_ERR(dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04002987 return PTR_ERR(dentry);
Al Viro4549fc72016-04-27 01:11:55 -04002988 }
2989 if (create_error && !dentry->d_inode) {
2990 error = create_error;
2991 goto out;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002992 }
2993looked_up:
2994 path->dentry = dentry;
2995 path->mnt = nd->path.mnt;
Al Viro2675a4e2012-06-22 12:41:10 +04002996 return 1;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002997}
2998
Nick Piggin31e6b012011-01-07 17:49:52 +11002999/*
David Howells1acf0af2012-06-14 16:13:46 +01003000 * Look up and maybe create and open the last component.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003001 *
3002 * Must be called with i_mutex held on parent.
3003 *
David Howells1acf0af2012-06-14 16:13:46 +01003004 * Returns 0 if the file was successfully atomically created (if necessary) and
3005 * opened. In this case the file will be returned attached to @file.
3006 *
3007 * Returns 1 if the file was not completely opened at this time, though lookups
3008 * and creations will have been performed and the dentry returned in @path will
3009 * be positive upon return if O_CREAT was specified. If O_CREAT wasn't
3010 * specified then a negative dentry may be returned.
3011 *
3012 * An error code is returned otherwise.
3013 *
3014 * FILE_CREATE will be set in @*opened if the dentry was created and will be
3015 * cleared otherwise prior to returning.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003016 */
Al Viro2675a4e2012-06-22 12:41:10 +04003017static int lookup_open(struct nameidata *nd, struct path *path,
3018 struct file *file,
3019 const struct open_flags *op,
Al Viro64894cf2012-07-31 00:53:35 +04003020 bool got_write, int *opened)
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003021{
3022 struct dentry *dir = nd->path.dentry;
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07003023 struct vfsmount *mnt = nd->path.mnt;
Miklos Szeredi54ef4872012-06-05 15:10:16 +02003024 struct inode *dir_inode = dir->d_inode;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003025 struct dentry *dentry;
3026 int error;
Miklos Szeredi54ef4872012-06-05 15:10:16 +02003027 bool need_lookup;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003028
Al Viro47237682012-06-10 05:01:45 -04003029 *opened &= ~FILE_CREATED;
Al Viro201f9562012-06-22 12:42:10 +04003030 dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003031 if (IS_ERR(dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04003032 return PTR_ERR(dentry);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003033
Miklos Szeredid18e9002012-06-05 15:10:17 +02003034 /* Cached positive dentry: will open in f_op->open */
3035 if (!need_lookup && dentry->d_inode)
3036 goto out_no_open;
3037
3038 if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) {
Al Viro64894cf2012-07-31 00:53:35 +04003039 return atomic_open(nd, dentry, path, file, op, got_write,
Al Viro47237682012-06-10 05:01:45 -04003040 need_lookup, opened);
Miklos Szeredid18e9002012-06-05 15:10:17 +02003041 }
3042
Miklos Szeredi54ef4872012-06-05 15:10:16 +02003043 if (need_lookup) {
3044 BUG_ON(dentry->d_inode);
3045
Al Viro72bd8662012-06-10 17:17:17 -04003046 dentry = lookup_real(dir_inode, dentry, nd->flags);
Miklos Szeredi54ef4872012-06-05 15:10:16 +02003047 if (IS_ERR(dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04003048 return PTR_ERR(dentry);
Miklos Szeredi54ef4872012-06-05 15:10:16 +02003049 }
3050
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003051 /* Negative dentry, just create the file */
3052 if (!dentry->d_inode && (op->open_flag & O_CREAT)) {
3053 umode_t mode = op->mode;
3054 if (!IS_POSIXACL(dir->d_inode))
3055 mode &= ~current_umask();
3056 /*
3057 * This write is needed to ensure that a
3058 * rw->ro transition does not occur between
3059 * the time when the file is created and when
3060 * a permanent write count is taken through
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003061 * the 'struct file' in finish_open().
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003062 */
Al Viro64894cf2012-07-31 00:53:35 +04003063 if (!got_write) {
3064 error = -EROFS;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003065 goto out_dput;
Al Viro64894cf2012-07-31 00:53:35 +04003066 }
Al Viro47237682012-06-10 05:01:45 -04003067 *opened |= FILE_CREATED;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003068 error = security_path_mknod(&nd->path, dentry, mode, 0);
3069 if (error)
3070 goto out_dput;
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07003071 error = vfs_create2(mnt, dir->d_inode, dentry, mode,
Al Viro312b63f2012-06-10 18:09:36 -04003072 nd->flags & LOOKUP_EXCL);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003073 if (error)
3074 goto out_dput;
3075 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02003076out_no_open:
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003077 path->dentry = dentry;
3078 path->mnt = nd->path.mnt;
Al Viro2675a4e2012-06-22 12:41:10 +04003079 return 1;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003080
3081out_dput:
3082 dput(dentry);
Al Viro2675a4e2012-06-22 12:41:10 +04003083 return error;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003084}
3085
3086/*
Al Virofe2d35f2011-03-05 22:58:25 -05003087 * Handle the last step of open()
Nick Piggin31e6b012011-01-07 17:49:52 +11003088 */
Al Viro896475d2015-04-22 18:02:17 -04003089static int do_last(struct nameidata *nd,
Al Viro2675a4e2012-06-22 12:41:10 +04003090 struct file *file, const struct open_flags *op,
Al Viro76ae2a52015-05-12 18:44:32 -04003091 int *opened)
Al Virofb1cc552009-12-24 01:58:28 -05003092{
Al Viroa1e28032009-12-24 02:12:06 -05003093 struct dentry *dir = nd->path.dentry;
Al Viroca344a892011-03-09 00:36:45 -05003094 int open_flag = op->open_flag;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003095 bool will_truncate = (open_flag & O_TRUNC) != 0;
Al Viro64894cf2012-07-31 00:53:35 +04003096 bool got_write = false;
Al Virobcda7652011-03-13 16:42:14 -04003097 int acc_mode = op->acc_mode;
Al Viro254cf582015-05-05 09:40:46 -04003098 unsigned seq;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02003099 struct inode *inode;
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003100 struct path save_parent = { .dentry = NULL, .mnt = NULL };
Al Viro896475d2015-04-22 18:02:17 -04003101 struct path path;
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003102 bool retried = false;
Al Viro16c2cd72011-02-22 15:50:10 -05003103 int error;
Al Virofb1cc552009-12-24 01:58:28 -05003104
Al Viroc3e380b2011-02-23 13:39:45 -05003105 nd->flags &= ~LOOKUP_PARENT;
3106 nd->flags |= op->intent;
3107
Al Virobc77daa2013-06-06 09:12:33 -04003108 if (nd->last_type != LAST_NORM) {
Al Virofe2d35f2011-03-05 22:58:25 -05003109 error = handle_dots(nd, nd->last_type);
Al Virodeb106c2015-05-08 18:05:21 -04003110 if (unlikely(error))
Al Viro2675a4e2012-06-22 12:41:10 +04003111 return error;
Miklos Szeredie83db162012-06-05 15:10:29 +02003112 goto finish_open;
Al Viro1f36f772009-12-26 10:56:19 -05003113 }
Al Viro67ee3ad2009-12-26 07:01:01 -05003114
Al Viroca344a892011-03-09 00:36:45 -05003115 if (!(open_flag & O_CREAT)) {
Al Virofe2d35f2011-03-05 22:58:25 -05003116 if (nd->last.name[nd->last.len])
3117 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
3118 /* we _can_ be in RCU mode here */
Al Viro254cf582015-05-05 09:40:46 -04003119 error = lookup_fast(nd, &path, &inode, &seq);
Miklos Szeredi71574862012-06-05 15:10:14 +02003120 if (likely(!error))
3121 goto finish_lookup;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02003122
Miklos Szeredi71574862012-06-05 15:10:14 +02003123 if (error < 0)
Al Virodeb106c2015-05-08 18:05:21 -04003124 return error;
Miklos Szeredi37d7fff2012-06-05 15:10:12 +02003125
Miklos Szeredi71574862012-06-05 15:10:14 +02003126 BUG_ON(nd->inode != dir->d_inode);
Miklos Szeredib6183df2012-06-05 15:10:13 +02003127 } else {
3128 /* create side of things */
3129 /*
3130 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
3131 * has been cleared when we got to the last component we are
3132 * about to look up
3133 */
3134 error = complete_walk(nd);
Al Viroe8bb73d2015-05-08 16:28:42 -04003135 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003136 return error;
Miklos Szeredib6183df2012-06-05 15:10:13 +02003137
Al Viro76ae2a52015-05-12 18:44:32 -04003138 audit_inode(nd->name, dir, LOOKUP_PARENT);
Miklos Szeredib6183df2012-06-05 15:10:13 +02003139 /* trailing slashes? */
Al Virodeb106c2015-05-08 18:05:21 -04003140 if (unlikely(nd->last.name[nd->last.len]))
3141 return -EISDIR;
Al Virofe2d35f2011-03-05 22:58:25 -05003142 }
3143
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003144retry_lookup:
Al Viro64894cf2012-07-31 00:53:35 +04003145 if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
3146 error = mnt_want_write(nd->path.mnt);
3147 if (!error)
3148 got_write = true;
3149 /*
3150 * do _not_ fail yet - we might not need that or fail with
3151 * a different error; let lookup_open() decide; we'll be
3152 * dropping this one anyway.
3153 */
3154 }
Al Viroa1e28032009-12-24 02:12:06 -05003155 mutex_lock(&dir->d_inode->i_mutex);
Al Viro896475d2015-04-22 18:02:17 -04003156 error = lookup_open(nd, &path, file, op, got_write, opened);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003157 mutex_unlock(&dir->d_inode->i_mutex);
Al Viroa1e28032009-12-24 02:12:06 -05003158
Al Viro2675a4e2012-06-22 12:41:10 +04003159 if (error <= 0) {
3160 if (error)
Miklos Szeredid18e9002012-06-05 15:10:17 +02003161 goto out;
3162
Al Viro47237682012-06-10 05:01:45 -04003163 if ((*opened & FILE_CREATED) ||
Al Viro496ad9a2013-01-23 17:07:38 -05003164 !S_ISREG(file_inode(file)->i_mode))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003165 will_truncate = false;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003166
Al Viro76ae2a52015-05-12 18:44:32 -04003167 audit_inode(nd->name, file->f_path.dentry, 0);
Miklos Szeredid18e9002012-06-05 15:10:17 +02003168 goto opened;
3169 }
Al Virofb1cc552009-12-24 01:58:28 -05003170
Al Viro47237682012-06-10 05:01:45 -04003171 if (*opened & FILE_CREATED) {
Al Viro9b44f1b2011-03-09 00:17:27 -05003172 /* Don't check for write permission, don't truncate */
Al Viroca344a892011-03-09 00:36:45 -05003173 open_flag &= ~O_TRUNC;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003174 will_truncate = false;
Al Virobcda7652011-03-13 16:42:14 -04003175 acc_mode = MAY_OPEN;
Al Viro896475d2015-04-22 18:02:17 -04003176 path_to_nameidata(&path, nd);
Miklos Szeredie83db162012-06-05 15:10:29 +02003177 goto finish_open_created;
Al Virofb1cc552009-12-24 01:58:28 -05003178 }
3179
3180 /*
Jeff Layton3134f372012-07-25 10:19:47 -04003181 * create/update audit record if it already exists.
Al Virofb1cc552009-12-24 01:58:28 -05003182 */
Al Viro896475d2015-04-22 18:02:17 -04003183 if (d_is_positive(path.dentry))
Al Viro76ae2a52015-05-12 18:44:32 -04003184 audit_inode(nd->name, path.dentry, 0);
Al Virofb1cc552009-12-24 01:58:28 -05003185
Miklos Szeredid18e9002012-06-05 15:10:17 +02003186 /*
3187 * If atomic_open() acquired write access it is dropped now due to
3188 * possible mount and symlink following (this might be optimized away if
3189 * necessary...)
3190 */
Al Viro64894cf2012-07-31 00:53:35 +04003191 if (got_write) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02003192 mnt_drop_write(nd->path.mnt);
Al Viro64894cf2012-07-31 00:53:35 +04003193 got_write = false;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003194 }
3195
Al Virodeb106c2015-05-08 18:05:21 -04003196 if (unlikely((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))) {
3197 path_to_nameidata(&path, nd);
3198 return -EEXIST;
3199 }
Al Virofb1cc552009-12-24 01:58:28 -05003200
NeilBrown756daf22015-03-23 13:37:38 +11003201 error = follow_managed(&path, nd);
Al Virodeb106c2015-05-08 18:05:21 -04003202 if (unlikely(error < 0))
3203 return error;
Al Viroa3fbbde2011-11-07 21:21:26 +00003204
Miklos Szeredidecf3402012-05-21 17:30:08 +02003205 BUG_ON(nd->flags & LOOKUP_RCU);
Al Viro254cf582015-05-05 09:40:46 -04003206 seq = 0; /* out of RCU mode, so the value doesn't matter */
Al Virodeb106c2015-05-08 18:05:21 -04003207 if (unlikely(d_is_negative(path.dentry))) {
Al Viro896475d2015-04-22 18:02:17 -04003208 path_to_nameidata(&path, nd);
Al Virodeb106c2015-05-08 18:05:21 -04003209 return -ENOENT;
Miklos Szeredi54c33e72012-05-21 17:30:10 +02003210 }
Al Viroaaf15692016-02-27 19:23:16 -05003211 inode = d_backing_inode(path.dentry);
Al Viro766c4cb2015-05-07 19:24:57 -04003212finish_lookup:
Al Virod63ff282015-05-04 18:13:23 -04003213 if (nd->depth)
3214 put_link(nd);
Al Viro181548c2015-05-07 19:54:34 -04003215 error = should_follow_link(nd, &path, nd->flags & LOOKUP_FOLLOW,
3216 inode, seq);
Al Virodeb106c2015-05-08 18:05:21 -04003217 if (unlikely(error))
Al Virod63ff282015-05-04 18:13:23 -04003218 return error;
Al Virofb1cc552009-12-24 01:58:28 -05003219
Al Viro896475d2015-04-22 18:02:17 -04003220 if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path.mnt) {
3221 path_to_nameidata(&path, nd);
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003222 } else {
3223 save_parent.dentry = nd->path.dentry;
Al Viro896475d2015-04-22 18:02:17 -04003224 save_parent.mnt = mntget(path.mnt);
3225 nd->path.dentry = path.dentry;
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003226
3227 }
Miklos Szeredidecf3402012-05-21 17:30:08 +02003228 nd->inode = inode;
Al Viro254cf582015-05-05 09:40:46 -04003229 nd->seq = seq;
Al Viroa3fbbde2011-11-07 21:21:26 +00003230 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
Al Virobc77daa2013-06-06 09:12:33 -04003231finish_open:
Al Viroa3fbbde2011-11-07 21:21:26 +00003232 error = complete_walk(nd);
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003233 if (error) {
3234 path_put(&save_parent);
Al Viro2675a4e2012-06-22 12:41:10 +04003235 return error;
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003236 }
Al Viro76ae2a52015-05-12 18:44:32 -04003237 audit_inode(nd->name, nd->path.dentry, 0);
Al Virod57c0472016-02-27 19:37:37 -05003238 if (unlikely(d_is_symlink(nd->path.dentry)) && !(open_flag & O_PATH)) {
3239 error = -ELOOP;
3240 goto out;
3241 }
Salvatore Mesoraca3658ccb2018-08-23 17:00:35 -07003242 if (open_flag & O_CREAT) {
3243 error = -EISDIR;
3244 if (d_is_dir(nd->path.dentry))
3245 goto out;
3246 error = may_create_in_sticky(dir,
3247 d_backing_inode(nd->path.dentry));
3248 if (unlikely(error))
3249 goto out;
3250 }
Miklos Szerediaf2f5542012-05-21 17:30:11 +02003251 error = -ENOTDIR;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02003252 if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04003253 goto out;
David Howells4bbcbd3b2015-03-17 22:16:40 +00003254 if (!d_is_reg(nd->path.dentry))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003255 will_truncate = false;
Al Viro6c0d46c2011-03-09 00:59:59 -05003256
Al Viro0f9d1a12011-03-09 00:13:14 -05003257 if (will_truncate) {
3258 error = mnt_want_write(nd->path.mnt);
3259 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003260 goto out;
Al Viro64894cf2012-07-31 00:53:35 +04003261 got_write = true;
Al Viro0f9d1a12011-03-09 00:13:14 -05003262 }
Miklos Szeredie83db162012-06-05 15:10:29 +02003263finish_open_created:
Al Virobcda7652011-03-13 16:42:14 -04003264 error = may_open(&nd->path, acc_mode, open_flag);
Al Viroca344a892011-03-09 00:36:45 -05003265 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003266 goto out;
Miklos Szeredi4aa7c632014-10-24 00:14:35 +02003267
3268 BUG_ON(*opened & FILE_OPENED); /* once it's opened, it's opened */
3269 error = vfs_open(&nd->path, file, current_cred());
3270 if (!error) {
3271 *opened |= FILE_OPENED;
3272 } else {
Al Viro30d90492012-06-22 12:40:19 +04003273 if (error == -EOPENSTALE)
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003274 goto stale_open;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003275 goto out;
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003276 }
Miklos Szeredia8277b92012-06-05 15:10:32 +02003277opened:
Al Viro2675a4e2012-06-22 12:41:10 +04003278 error = open_check_o_direct(file);
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003279 if (error)
3280 goto exit_fput;
Dmitry Kasatkin3034a142014-06-27 18:15:44 +03003281 error = ima_file_check(file, op->acc_mode, *opened);
Miklos Szerediaa4caad2012-06-05 15:10:28 +02003282 if (error)
3283 goto exit_fput;
3284
3285 if (will_truncate) {
Al Viro2675a4e2012-06-22 12:41:10 +04003286 error = handle_truncate(file);
Miklos Szerediaa4caad2012-06-05 15:10:28 +02003287 if (error)
3288 goto exit_fput;
Al Viro0f9d1a12011-03-09 00:13:14 -05003289 }
Al Viroca344a892011-03-09 00:36:45 -05003290out:
Al Viro80d18c02016-02-27 19:17:33 -05003291 if (unlikely(error > 0)) {
3292 WARN_ON(1);
3293 error = -EINVAL;
3294 }
Al Viro64894cf2012-07-31 00:53:35 +04003295 if (got_write)
Al Viro0f9d1a12011-03-09 00:13:14 -05003296 mnt_drop_write(nd->path.mnt);
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003297 path_put(&save_parent);
Al Viro2675a4e2012-06-22 12:41:10 +04003298 return error;
Al Virofb1cc552009-12-24 01:58:28 -05003299
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003300exit_fput:
Al Viro2675a4e2012-06-22 12:41:10 +04003301 fput(file);
3302 goto out;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003303
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003304stale_open:
3305 /* If no saved parent or already retried then can't retry */
3306 if (!save_parent.dentry || retried)
3307 goto out;
3308
3309 BUG_ON(save_parent.dentry != dir);
3310 path_put(&nd->path);
3311 nd->path = save_parent;
3312 nd->inode = dir->d_inode;
3313 save_parent.mnt = NULL;
3314 save_parent.dentry = NULL;
Al Viro64894cf2012-07-31 00:53:35 +04003315 if (got_write) {
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003316 mnt_drop_write(nd->path.mnt);
Al Viro64894cf2012-07-31 00:53:35 +04003317 got_write = false;
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003318 }
3319 retried = true;
3320 goto retry_lookup;
Al Virofb1cc552009-12-24 01:58:28 -05003321}
3322
Al Viroc8a53ee2015-05-12 18:43:07 -04003323static int do_tmpfile(struct nameidata *nd, unsigned flags,
Al Viro60545d02013-06-07 01:20:27 -04003324 const struct open_flags *op,
3325 struct file *file, int *opened)
3326{
3327 static const struct qstr name = QSTR_INIT("/", 1);
Al Viro625b6d12015-05-12 16:36:12 -04003328 struct dentry *child;
Al Viro60545d02013-06-07 01:20:27 -04003329 struct inode *dir;
Al Viro625b6d12015-05-12 16:36:12 -04003330 struct path path;
Al Viroc8a53ee2015-05-12 18:43:07 -04003331 int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path);
Al Viro60545d02013-06-07 01:20:27 -04003332 if (unlikely(error))
3333 return error;
Al Viro625b6d12015-05-12 16:36:12 -04003334 error = mnt_want_write(path.mnt);
Al Viro60545d02013-06-07 01:20:27 -04003335 if (unlikely(error))
3336 goto out;
Al Viro625b6d12015-05-12 16:36:12 -04003337 dir = path.dentry->d_inode;
Al Viro60545d02013-06-07 01:20:27 -04003338 /* we want directory to be writable */
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07003339 error = inode_permission2(path.mnt, dir, MAY_WRITE | MAY_EXEC);
Al Viro60545d02013-06-07 01:20:27 -04003340 if (error)
3341 goto out2;
Al Viro60545d02013-06-07 01:20:27 -04003342 if (!dir->i_op->tmpfile) {
3343 error = -EOPNOTSUPP;
3344 goto out2;
3345 }
Al Viro625b6d12015-05-12 16:36:12 -04003346 child = d_alloc(path.dentry, &name);
Al Viro60545d02013-06-07 01:20:27 -04003347 if (unlikely(!child)) {
3348 error = -ENOMEM;
3349 goto out2;
3350 }
Al Viro625b6d12015-05-12 16:36:12 -04003351 dput(path.dentry);
3352 path.dentry = child;
3353 error = dir->i_op->tmpfile(dir, child, op->mode);
Al Viro60545d02013-06-07 01:20:27 -04003354 if (error)
3355 goto out2;
Al Viroc8a53ee2015-05-12 18:43:07 -04003356 audit_inode(nd->name, child, 0);
Eric Rannaud69a91c22014-10-30 01:51:01 -07003357 /* Don't check for other permissions, the inode was just created */
Al Viro625b6d12015-05-12 16:36:12 -04003358 error = may_open(&path, MAY_OPEN, op->open_flag);
Al Viro60545d02013-06-07 01:20:27 -04003359 if (error)
3360 goto out2;
Al Viro625b6d12015-05-12 16:36:12 -04003361 file->f_path.mnt = path.mnt;
3362 error = finish_open(file, child, NULL, opened);
Al Viro60545d02013-06-07 01:20:27 -04003363 if (error)
3364 goto out2;
3365 error = open_check_o_direct(file);
Al Virof4e0c302013-06-11 08:34:36 +04003366 if (error) {
Al Viro60545d02013-06-07 01:20:27 -04003367 fput(file);
Al Virof4e0c302013-06-11 08:34:36 +04003368 } else if (!(op->open_flag & O_EXCL)) {
3369 struct inode *inode = file_inode(file);
3370 spin_lock(&inode->i_lock);
3371 inode->i_state |= I_LINKABLE;
3372 spin_unlock(&inode->i_lock);
3373 }
Al Viro60545d02013-06-07 01:20:27 -04003374out2:
Al Viro625b6d12015-05-12 16:36:12 -04003375 mnt_drop_write(path.mnt);
Al Viro60545d02013-06-07 01:20:27 -04003376out:
Al Viro625b6d12015-05-12 16:36:12 -04003377 path_put(&path);
Al Viro60545d02013-06-07 01:20:27 -04003378 return error;
3379}
3380
Al Viroc8a53ee2015-05-12 18:43:07 -04003381static struct file *path_openat(struct nameidata *nd,
3382 const struct open_flags *op, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383{
Al Viro368ee9b2015-05-08 17:19:59 -04003384 const char *s;
Al Viro30d90492012-06-22 12:40:19 +04003385 struct file *file;
Al Viro47237682012-06-10 05:01:45 -04003386 int opened = 0;
Al Viro13aab422011-02-23 17:54:08 -05003387 int error;
Nick Piggin31e6b012011-01-07 17:49:52 +11003388
Al Viro30d90492012-06-22 12:40:19 +04003389 file = get_empty_filp();
Al Viro1afc99b2013-02-14 20:41:04 -05003390 if (IS_ERR(file))
3391 return file;
Nick Piggin31e6b012011-01-07 17:49:52 +11003392
Al Viro30d90492012-06-22 12:40:19 +04003393 file->f_flags = op->open_flag;
Nick Piggin31e6b012011-01-07 17:49:52 +11003394
Al Virobb458c62013-07-13 13:26:37 +04003395 if (unlikely(file->f_flags & __O_TMPFILE)) {
Al Viroc8a53ee2015-05-12 18:43:07 -04003396 error = do_tmpfile(nd, flags, op, file, &opened);
Al Virof15133d2015-05-08 22:53:15 -04003397 goto out2;
Al Viro60545d02013-06-07 01:20:27 -04003398 }
3399
Al Viroc8a53ee2015-05-12 18:43:07 -04003400 s = path_init(nd, flags);
Al Viro368ee9b2015-05-08 17:19:59 -04003401 if (IS_ERR(s)) {
3402 put_filp(file);
3403 return ERR_CAST(s);
3404 }
Al Viro3bdba282015-05-08 17:37:07 -04003405 while (!(error = link_path_walk(s, nd)) &&
Al Viro76ae2a52015-05-12 18:44:32 -04003406 (error = do_last(nd, file, op, &opened)) > 0) {
Al Viro73d049a2011-03-11 12:08:24 -05003407 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
Al Viro3bdba282015-05-08 17:37:07 -04003408 s = trailing_symlink(nd);
3409 if (IS_ERR(s)) {
3410 error = PTR_ERR(s);
Al Viro2675a4e2012-06-22 12:41:10 +04003411 break;
Al Viro3bdba282015-05-08 17:37:07 -04003412 }
Al Viro806b6812009-12-26 07:16:40 -05003413 }
Al Virodeb106c2015-05-08 18:05:21 -04003414 terminate_walk(nd);
Al Virof15133d2015-05-08 22:53:15 -04003415out2:
Al Viro2675a4e2012-06-22 12:41:10 +04003416 if (!(opened & FILE_OPENED)) {
3417 BUG_ON(!error);
Al Viro30d90492012-06-22 12:40:19 +04003418 put_filp(file);
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003419 }
Al Viro2675a4e2012-06-22 12:41:10 +04003420 if (unlikely(error)) {
3421 if (error == -EOPENSTALE) {
3422 if (flags & LOOKUP_RCU)
3423 error = -ECHILD;
3424 else
3425 error = -ESTALE;
3426 }
3427 file = ERR_PTR(error);
3428 }
3429 return file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430}
3431
Jeff Layton669abf42012-10-10 16:43:10 -04003432struct file *do_filp_open(int dfd, struct filename *pathname,
Al Virof9652e12013-06-11 08:23:01 +04003433 const struct open_flags *op)
Al Viro13aab422011-02-23 17:54:08 -05003434{
Al Viro9883d182015-05-13 07:28:08 -04003435 struct nameidata nd;
Al Virof9652e12013-06-11 08:23:01 +04003436 int flags = op->lookup_flags;
Al Viro13aab422011-02-23 17:54:08 -05003437 struct file *filp;
3438
Al Viro9883d182015-05-13 07:28:08 -04003439 set_nameidata(&nd, dfd, pathname);
Al Viroc8a53ee2015-05-12 18:43:07 -04003440 filp = path_openat(&nd, op, flags | LOOKUP_RCU);
Al Viro13aab422011-02-23 17:54:08 -05003441 if (unlikely(filp == ERR_PTR(-ECHILD)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003442 filp = path_openat(&nd, op, flags);
Al Viro13aab422011-02-23 17:54:08 -05003443 if (unlikely(filp == ERR_PTR(-ESTALE)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003444 filp = path_openat(&nd, op, flags | LOOKUP_REVAL);
Al Viro9883d182015-05-13 07:28:08 -04003445 restore_nameidata();
Al Viro13aab422011-02-23 17:54:08 -05003446 return filp;
3447}
3448
Al Viro73d049a2011-03-11 12:08:24 -05003449struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
Al Virof9652e12013-06-11 08:23:01 +04003450 const char *name, const struct open_flags *op)
Al Viro73d049a2011-03-11 12:08:24 -05003451{
Al Viro9883d182015-05-13 07:28:08 -04003452 struct nameidata nd;
Al Viro73d049a2011-03-11 12:08:24 -05003453 struct file *file;
Paul Moore51689102015-01-22 00:00:03 -05003454 struct filename *filename;
Al Virof9652e12013-06-11 08:23:01 +04003455 int flags = op->lookup_flags | LOOKUP_ROOT;
Al Viro73d049a2011-03-11 12:08:24 -05003456
3457 nd.root.mnt = mnt;
3458 nd.root.dentry = dentry;
3459
David Howellsb18825a2013-09-12 19:22:53 +01003460 if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN)
Al Viro73d049a2011-03-11 12:08:24 -05003461 return ERR_PTR(-ELOOP);
3462
Paul Moore51689102015-01-22 00:00:03 -05003463 filename = getname_kernel(name);
Viresh Kumara1c83682015-08-12 15:59:44 +05303464 if (IS_ERR(filename))
Paul Moore51689102015-01-22 00:00:03 -05003465 return ERR_CAST(filename);
3466
Al Viro9883d182015-05-13 07:28:08 -04003467 set_nameidata(&nd, -1, filename);
Al Viroc8a53ee2015-05-12 18:43:07 -04003468 file = path_openat(&nd, op, flags | LOOKUP_RCU);
Al Viro73d049a2011-03-11 12:08:24 -05003469 if (unlikely(file == ERR_PTR(-ECHILD)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003470 file = path_openat(&nd, op, flags);
Al Viro73d049a2011-03-11 12:08:24 -05003471 if (unlikely(file == ERR_PTR(-ESTALE)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003472 file = path_openat(&nd, op, flags | LOOKUP_REVAL);
Al Viro9883d182015-05-13 07:28:08 -04003473 restore_nameidata();
Paul Moore51689102015-01-22 00:00:03 -05003474 putname(filename);
Al Viro73d049a2011-03-11 12:08:24 -05003475 return file;
3476}
3477
Al Virofa14a0b2015-01-22 02:16:49 -05003478static struct dentry *filename_create(int dfd, struct filename *name,
Jeff Layton1ac12b42012-12-11 12:10:06 -05003479 struct path *path, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003481 struct dentry *dentry = ERR_PTR(-EEXIST);
Al Viro391172c2015-05-09 11:19:16 -04003482 struct qstr last;
3483 int type;
Jan Karac30dabf2012-06-12 16:20:30 +02003484 int err2;
Jeff Layton1ac12b42012-12-11 12:10:06 -05003485 int error;
3486 bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
3487
3488 /*
3489 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
3490 * other flags passed in are ignored!
3491 */
3492 lookup_flags &= LOOKUP_REVAL;
3493
Al Viro5c31b6c2015-05-12 17:32:54 -04003494 name = filename_parentat(dfd, name, lookup_flags, path, &last, &type);
3495 if (IS_ERR(name))
3496 return ERR_CAST(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003498 /*
3499 * Yucky last component or no last component at all?
3500 * (foo/., foo/.., /////)
3501 */
Al Viro5c31b6c2015-05-12 17:32:54 -04003502 if (unlikely(type != LAST_NORM))
Al Viroed75e952011-06-27 16:53:43 -04003503 goto out;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003504
Jan Karac30dabf2012-06-12 16:20:30 +02003505 /* don't fail immediately if it's r/o, at least try to report other errors */
Al Viro391172c2015-05-09 11:19:16 -04003506 err2 = mnt_want_write(path->mnt);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003507 /*
3508 * Do the final lookup.
3509 */
Al Viro391172c2015-05-09 11:19:16 -04003510 lookup_flags |= LOOKUP_CREATE | LOOKUP_EXCL;
3511 mutex_lock_nested(&path->dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3512 dentry = __lookup_hash(&last, path->dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 if (IS_ERR(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003514 goto unlock;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003515
Al Viroa8104a92012-07-20 02:25:00 +04003516 error = -EEXIST;
David Howellsb18825a2013-09-12 19:22:53 +01003517 if (d_is_positive(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003518 goto fail;
David Howellsb18825a2013-09-12 19:22:53 +01003519
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003520 /*
3521 * Special case - lookup gave negative, but... we had foo/bar/
3522 * From the vfs_mknod() POV we just have a negative dentry -
3523 * all is fine. Let's be bastards - you had / on the end, you've
3524 * been asking for (non-existent) directory. -ENOENT for you.
3525 */
Al Viro391172c2015-05-09 11:19:16 -04003526 if (unlikely(!is_dir && last.name[last.len])) {
Al Viroa8104a92012-07-20 02:25:00 +04003527 error = -ENOENT;
Al Viroed75e952011-06-27 16:53:43 -04003528 goto fail;
Al Viroe9baf6e2008-05-15 04:49:12 -04003529 }
Jan Karac30dabf2012-06-12 16:20:30 +02003530 if (unlikely(err2)) {
3531 error = err2;
Al Viroa8104a92012-07-20 02:25:00 +04003532 goto fail;
Jan Karac30dabf2012-06-12 16:20:30 +02003533 }
Al Viro181c37b2015-05-12 17:21:25 -04003534 putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536fail:
Al Viroa8104a92012-07-20 02:25:00 +04003537 dput(dentry);
3538 dentry = ERR_PTR(error);
3539unlock:
Al Viro391172c2015-05-09 11:19:16 -04003540 mutex_unlock(&path->dentry->d_inode->i_mutex);
Jan Karac30dabf2012-06-12 16:20:30 +02003541 if (!err2)
Al Viro391172c2015-05-09 11:19:16 -04003542 mnt_drop_write(path->mnt);
Al Viroed75e952011-06-27 16:53:43 -04003543out:
Al Viro391172c2015-05-09 11:19:16 -04003544 path_put(path);
Al Viro181c37b2015-05-12 17:21:25 -04003545 putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 return dentry;
3547}
Al Virofa14a0b2015-01-22 02:16:49 -05003548
3549struct dentry *kern_path_create(int dfd, const char *pathname,
3550 struct path *path, unsigned int lookup_flags)
3551{
Al Viro181c37b2015-05-12 17:21:25 -04003552 return filename_create(dfd, getname_kernel(pathname),
3553 path, lookup_flags);
Al Virofa14a0b2015-01-22 02:16:49 -05003554}
Al Virodae6ad82011-06-26 11:50:15 -04003555EXPORT_SYMBOL(kern_path_create);
3556
Al Viro921a1652012-07-20 01:15:31 +04003557void done_path_create(struct path *path, struct dentry *dentry)
3558{
3559 dput(dentry);
3560 mutex_unlock(&path->dentry->d_inode->i_mutex);
Al Viroa8104a92012-07-20 02:25:00 +04003561 mnt_drop_write(path->mnt);
Al Viro921a1652012-07-20 01:15:31 +04003562 path_put(path);
3563}
3564EXPORT_SYMBOL(done_path_create);
3565
Al Viro520ae682015-05-13 07:00:28 -04003566inline struct dentry *user_path_create(int dfd, const char __user *pathname,
Jeff Layton1ac12b42012-12-11 12:10:06 -05003567 struct path *path, unsigned int lookup_flags)
Al Virodae6ad82011-06-26 11:50:15 -04003568{
Al Viro181c37b2015-05-12 17:21:25 -04003569 return filename_create(dfd, getname(pathname), path, lookup_flags);
Al Virodae6ad82011-06-26 11:50:15 -04003570}
3571EXPORT_SYMBOL(user_path_create);
3572
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07003573int vfs_mknod2(struct vfsmount *mnt, struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574{
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07003575 int error = may_create(mnt, dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576
3577 if (error)
3578 return error;
3579
Eric W. Biederman975d6b32011-11-13 12:16:43 -08003580 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 return -EPERM;
3582
Al Viroacfa4382008-12-04 10:06:33 -05003583 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 return -EPERM;
3585
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07003586 error = devcgroup_inode_mknod(mode, dev);
3587 if (error)
3588 return error;
3589
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 error = security_inode_mknod(dir, dentry, mode, dev);
3591 if (error)
3592 return error;
3593
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003595 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003596 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 return error;
3598}
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07003599EXPORT_SYMBOL(vfs_mknod2);
3600
3601int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
3602{
3603 return vfs_mknod2(NULL, dir, dentry, mode, dev);
3604}
Al Viro4d359502014-03-14 12:20:17 -04003605EXPORT_SYMBOL(vfs_mknod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606
Al Virof69aac02011-07-26 04:31:40 -04003607static int may_mknod(umode_t mode)
Dave Hansen463c3192008-02-15 14:37:57 -08003608{
3609 switch (mode & S_IFMT) {
3610 case S_IFREG:
3611 case S_IFCHR:
3612 case S_IFBLK:
3613 case S_IFIFO:
3614 case S_IFSOCK:
3615 case 0: /* zero mode translates to S_IFREG */
3616 return 0;
3617 case S_IFDIR:
3618 return -EPERM;
3619 default:
3620 return -EINVAL;
3621 }
3622}
3623
Al Viro8208a222011-07-25 17:32:17 -04003624SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003625 unsigned, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626{
Al Viro2ad94ae2008-07-21 09:32:51 -04003627 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003628 struct path path;
3629 int error;
Jeff Layton972567f2012-12-20 16:00:10 -05003630 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631
Al Viro8e4bfca2012-07-20 01:17:26 +04003632 error = may_mknod(mode);
3633 if (error)
3634 return error;
Jeff Layton972567f2012-12-20 16:00:10 -05003635retry:
3636 dentry = user_path_create(dfd, filename, &path, lookup_flags);
Al Virodae6ad82011-06-26 11:50:15 -04003637 if (IS_ERR(dentry))
3638 return PTR_ERR(dentry);
Al Viro2ad94ae2008-07-21 09:32:51 -04003639
Al Virodae6ad82011-06-26 11:50:15 -04003640 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003641 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003642 error = security_path_mknod(&path, dentry, mode, dev);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003643 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04003644 goto out;
Dave Hansen463c3192008-02-15 14:37:57 -08003645 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 case 0: case S_IFREG:
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07003647 error = vfs_create2(path.mnt, path.dentry->d_inode,dentry,mode,true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 break;
3649 case S_IFCHR: case S_IFBLK:
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07003650 error = vfs_mknod2(path.mnt, path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 new_decode_dev(dev));
3652 break;
3653 case S_IFIFO: case S_IFSOCK:
Al Virodae6ad82011-06-26 11:50:15 -04003654 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 }
Al Viroa8104a92012-07-20 02:25:00 +04003657out:
Al Viro921a1652012-07-20 01:15:31 +04003658 done_path_create(&path, dentry);
Jeff Layton972567f2012-12-20 16:00:10 -05003659 if (retry_estale(error, lookup_flags)) {
3660 lookup_flags |= LOOKUP_REVAL;
3661 goto retry;
3662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 return error;
3664}
3665
Al Viro8208a222011-07-25 17:32:17 -04003666SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003667{
3668 return sys_mknodat(AT_FDCWD, filename, mode, dev);
3669}
3670
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07003671int vfs_mkdir2(struct vfsmount *mnt, struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672{
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07003673 int error = may_create(mnt, dir, dentry);
Al Viro8de52772012-02-06 12:45:27 -05003674 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675
3676 if (error)
3677 return error;
3678
Al Viroacfa4382008-12-04 10:06:33 -05003679 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 return -EPERM;
3681
3682 mode &= (S_IRWXUGO|S_ISVTX);
3683 error = security_inode_mkdir(dir, dentry, mode);
3684 if (error)
3685 return error;
3686
Al Viro8de52772012-02-06 12:45:27 -05003687 if (max_links && dir->i_nlink >= max_links)
3688 return -EMLINK;
3689
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003691 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003692 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 return error;
3694}
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07003695EXPORT_SYMBOL(vfs_mkdir2);
3696
3697int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
3698{
3699 return vfs_mkdir2(NULL, dir, dentry, mode);
3700}
Al Viro4d359502014-03-14 12:20:17 -04003701EXPORT_SYMBOL(vfs_mkdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702
Al Viroa218d0f2011-11-21 14:59:34 -05003703SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704{
Dave Hansen6902d922006-09-30 23:29:01 -07003705 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003706 struct path path;
3707 int error;
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003708 unsigned int lookup_flags = LOOKUP_DIRECTORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003710retry:
3711 dentry = user_path_create(dfd, pathname, &path, lookup_flags);
Dave Hansen6902d922006-09-30 23:29:01 -07003712 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003713 return PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003714
Al Virodae6ad82011-06-26 11:50:15 -04003715 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003716 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003717 error = security_path_mkdir(&path, dentry, mode);
Al Viroa8104a92012-07-20 02:25:00 +04003718 if (!error)
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07003719 error = vfs_mkdir2(path.mnt, path.dentry->d_inode, dentry, mode);
Al Viro921a1652012-07-20 01:15:31 +04003720 done_path_create(&path, dentry);
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003721 if (retry_estale(error, lookup_flags)) {
3722 lookup_flags |= LOOKUP_REVAL;
3723 goto retry;
3724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 return error;
3726}
3727
Al Viroa218d0f2011-11-21 14:59:34 -05003728SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003729{
3730 return sys_mkdirat(AT_FDCWD, pathname, mode);
3731}
3732
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733/*
Sage Weila71905f2011-05-24 13:06:08 -07003734 * The dentry_unhash() helper will try to drop the dentry early: we
J. Bruce Fieldsc0d02592012-02-15 11:48:40 -05003735 * should have a usage count of 1 if we're the only user of this
Sage Weila71905f2011-05-24 13:06:08 -07003736 * dentry, and if that is true (possibly after pruning the dcache),
3737 * then we drop the dentry now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 *
3739 * A low-level filesystem can, if it choses, legally
3740 * do a
3741 *
3742 * if (!d_unhashed(dentry))
3743 * return -EBUSY;
3744 *
3745 * if it cannot handle the case of removing a directory
3746 * that is still in use by something else..
3747 */
3748void dentry_unhash(struct dentry *dentry)
3749{
Vasily Averindc168422006-12-06 20:37:07 -08003750 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 spin_lock(&dentry->d_lock);
Waiman Long98474232013-08-28 18:24:59 -07003752 if (dentry->d_lockref.count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 __d_drop(dentry);
3754 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755}
Al Viro4d359502014-03-14 12:20:17 -04003756EXPORT_SYMBOL(dentry_unhash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07003758int vfs_rmdir2(struct vfsmount *mnt, struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759{
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07003760 int error = may_delete(mnt, dir, dentry, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761
3762 if (error)
3763 return error;
3764
Al Viroacfa4382008-12-04 10:06:33 -05003765 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 return -EPERM;
3767
Al Viro1d2ef592011-09-14 18:55:41 +01003768 dget(dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003769 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770
Sage Weil912dbc12011-05-24 13:06:11 -07003771 error = -EBUSY;
Eric W. Biederman7af13642013-10-04 19:15:13 -07003772 if (is_local_mountpoint(dentry))
Sage Weil912dbc12011-05-24 13:06:11 -07003773 goto out;
3774
3775 error = security_inode_rmdir(dir, dentry);
3776 if (error)
3777 goto out;
3778
Sage Weil3cebde22011-05-29 21:20:59 -07003779 shrink_dcache_parent(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003780 error = dir->i_op->rmdir(dir, dentry);
3781 if (error)
3782 goto out;
3783
3784 dentry->d_inode->i_flags |= S_DEAD;
3785 dont_mount(dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07003786 detach_mounts(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003787
3788out:
3789 mutex_unlock(&dentry->d_inode->i_mutex);
Al Viro1d2ef592011-09-14 18:55:41 +01003790 dput(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003791 if (!error)
3792 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 return error;
3794}
Guenter Roeck431cd3e2017-01-30 12:29:00 -08003795EXPORT_SYMBOL(vfs_rmdir2);
3796
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07003797int vfs_rmdir(struct inode *dir, struct dentry *dentry)
3798{
3799 return vfs_rmdir2(NULL, dir, dentry);
3800}
Al Viro4d359502014-03-14 12:20:17 -04003801EXPORT_SYMBOL(vfs_rmdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003803static long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804{
3805 int error = 0;
Jeff Layton91a27b22012-10-10 15:25:28 -04003806 struct filename *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 struct dentry *dentry;
Al Virof5beed72015-04-30 16:09:11 -04003808 struct path path;
3809 struct qstr last;
3810 int type;
Jeff Laytonc6ee9202012-12-20 16:28:33 -05003811 unsigned int lookup_flags = 0;
3812retry:
Al Virof5beed72015-04-30 16:09:11 -04003813 name = user_path_parent(dfd, pathname,
3814 &path, &last, &type, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04003815 if (IS_ERR(name))
3816 return PTR_ERR(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817
Al Virof5beed72015-04-30 16:09:11 -04003818 switch (type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003819 case LAST_DOTDOT:
3820 error = -ENOTEMPTY;
3821 goto exit1;
3822 case LAST_DOT:
3823 error = -EINVAL;
3824 goto exit1;
3825 case LAST_ROOT:
3826 error = -EBUSY;
3827 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003829
Al Virof5beed72015-04-30 16:09:11 -04003830 error = mnt_want_write(path.mnt);
Jan Karac30dabf2012-06-12 16:20:30 +02003831 if (error)
3832 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003833
Al Virof5beed72015-04-30 16:09:11 -04003834 mutex_lock_nested(&path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3835 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003837 if (IS_ERR(dentry))
3838 goto exit2;
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04003839 if (!dentry->d_inode) {
3840 error = -ENOENT;
3841 goto exit3;
3842 }
Al Virof5beed72015-04-30 16:09:11 -04003843 error = security_path_rmdir(&path, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003844 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02003845 goto exit3;
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07003846 error = vfs_rmdir2(path.mnt, path.dentry->d_inode, dentry);
Dave Hansen06227532008-02-15 14:37:34 -08003847exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07003848 dput(dentry);
3849exit2:
Al Virof5beed72015-04-30 16:09:11 -04003850 mutex_unlock(&path.dentry->d_inode->i_mutex);
3851 mnt_drop_write(path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852exit1:
Al Virof5beed72015-04-30 16:09:11 -04003853 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 putname(name);
Jeff Laytonc6ee9202012-12-20 16:28:33 -05003855 if (retry_estale(error, lookup_flags)) {
3856 lookup_flags |= LOOKUP_REVAL;
3857 goto retry;
3858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859 return error;
3860}
3861
Heiko Carstens3cdad422009-01-14 14:14:22 +01003862SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003863{
3864 return do_rmdir(AT_FDCWD, pathname);
3865}
3866
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003867/**
3868 * vfs_unlink - unlink a filesystem object
3869 * @dir: parent directory
3870 * @dentry: victim
3871 * @delegated_inode: returns victim inode, if the inode is delegated.
3872 *
3873 * The caller must hold dir->i_mutex.
3874 *
3875 * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
3876 * return a reference to the inode in delegated_inode. The caller
3877 * should then break the delegation on that inode and retry. Because
3878 * breaking a delegation may take a long time, the caller should drop
3879 * dir->i_mutex before doing so.
3880 *
3881 * Alternatively, a caller may pass NULL for delegated_inode. This may
3882 * be appropriate for callers that expect the underlying filesystem not
3883 * to be NFS exported.
3884 */
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07003885int vfs_unlink2(struct vfsmount *mnt, struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886{
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003887 struct inode *target = dentry->d_inode;
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07003888 int error = may_delete(mnt, dir, dentry, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889
3890 if (error)
3891 return error;
3892
Al Viroacfa4382008-12-04 10:06:33 -05003893 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894 return -EPERM;
3895
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003896 mutex_lock(&target->i_mutex);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07003897 if (is_local_mountpoint(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898 error = -EBUSY;
3899 else {
3900 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05003901 if (!error) {
J. Bruce Fields5a146962012-08-28 07:50:40 -07003902 error = try_break_deleg(target, delegated_inode);
3903 if (error)
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003904 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 error = dir->i_op->unlink(dir, dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07003906 if (!error) {
Al Virod83c49f2010-04-30 17:17:09 -04003907 dont_mount(dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07003908 detach_mounts(dentry);
3909 }
Al Virobec10522010-03-03 14:12:08 -05003910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911 }
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003912out:
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003913 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914
3915 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
3916 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003917 fsnotify_link_count(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 }
Robert Love0eeca282005-07-12 17:06:03 -04003920
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 return error;
3922}
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07003923EXPORT_SYMBOL(vfs_unlink2);
3924
3925int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
3926{
3927 return vfs_unlink2(NULL, dir, dentry, delegated_inode);
3928}
Al Viro4d359502014-03-14 12:20:17 -04003929EXPORT_SYMBOL(vfs_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930
3931/*
3932 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003933 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 * writeout happening, and we don't want to prevent access to the directory
3935 * while waiting on the I/O.
3936 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003937static long do_unlinkat(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938{
Al Viro2ad94ae2008-07-21 09:32:51 -04003939 int error;
Jeff Layton91a27b22012-10-10 15:25:28 -04003940 struct filename *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 struct dentry *dentry;
Al Virof5beed72015-04-30 16:09:11 -04003942 struct path path;
3943 struct qstr last;
3944 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 struct inode *inode = NULL;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003946 struct inode *delegated_inode = NULL;
Jeff Layton5d18f812012-12-20 16:38:04 -05003947 unsigned int lookup_flags = 0;
3948retry:
Al Virof5beed72015-04-30 16:09:11 -04003949 name = user_path_parent(dfd, pathname,
3950 &path, &last, &type, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04003951 if (IS_ERR(name))
3952 return PTR_ERR(name);
Al Viro2ad94ae2008-07-21 09:32:51 -04003953
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 error = -EISDIR;
Al Virof5beed72015-04-30 16:09:11 -04003955 if (type != LAST_NORM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003957
Al Virof5beed72015-04-30 16:09:11 -04003958 error = mnt_want_write(path.mnt);
Jan Karac30dabf2012-06-12 16:20:30 +02003959 if (error)
3960 goto exit1;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003961retry_deleg:
Al Virof5beed72015-04-30 16:09:11 -04003962 mutex_lock_nested(&path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3963 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 error = PTR_ERR(dentry);
3965 if (!IS_ERR(dentry)) {
3966 /* Why not before? Because we want correct error value */
Al Virof5beed72015-04-30 16:09:11 -04003967 if (last.name[last.len])
Török Edwin50338b82011-06-16 00:06:14 +03003968 goto slashes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 inode = dentry->d_inode;
David Howellsb18825a2013-09-12 19:22:53 +01003970 if (d_is_negative(dentry))
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04003971 goto slashes;
3972 ihold(inode);
Al Virof5beed72015-04-30 16:09:11 -04003973 error = security_path_unlink(&path, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003974 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02003975 goto exit2;
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07003976 error = vfs_unlink2(path.mnt, path.dentry->d_inode, dentry, &delegated_inode);
Jan Karac30dabf2012-06-12 16:20:30 +02003977exit2:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 dput(dentry);
3979 }
Al Virof5beed72015-04-30 16:09:11 -04003980 mutex_unlock(&path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 if (inode)
3982 iput(inode); /* truncate the inode here */
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003983 inode = NULL;
3984 if (delegated_inode) {
J. Bruce Fields5a146962012-08-28 07:50:40 -07003985 error = break_deleg_wait(&delegated_inode);
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003986 if (!error)
3987 goto retry_deleg;
3988 }
Al Virof5beed72015-04-30 16:09:11 -04003989 mnt_drop_write(path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990exit1:
Al Virof5beed72015-04-30 16:09:11 -04003991 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 putname(name);
Jeff Layton5d18f812012-12-20 16:38:04 -05003993 if (retry_estale(error, lookup_flags)) {
3994 lookup_flags |= LOOKUP_REVAL;
3995 inode = NULL;
3996 goto retry;
3997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 return error;
3999
4000slashes:
David Howellsb18825a2013-09-12 19:22:53 +01004001 if (d_is_negative(dentry))
4002 error = -ENOENT;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02004003 else if (d_is_dir(dentry))
David Howellsb18825a2013-09-12 19:22:53 +01004004 error = -EISDIR;
4005 else
4006 error = -ENOTDIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 goto exit2;
4008}
4009
Heiko Carstens2e4d0922009-01-14 14:14:31 +01004010SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004011{
4012 if ((flag & ~AT_REMOVEDIR) != 0)
4013 return -EINVAL;
4014
4015 if (flag & AT_REMOVEDIR)
4016 return do_rmdir(dfd, pathname);
4017
4018 return do_unlinkat(dfd, pathname);
4019}
4020
Heiko Carstens3480b252009-01-14 14:14:16 +01004021SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004022{
4023 return do_unlinkat(AT_FDCWD, pathname);
4024}
4025
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07004026int vfs_symlink2(struct vfsmount *mnt, struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027{
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07004028 int error = may_create(mnt, dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029
4030 if (error)
4031 return error;
4032
Al Viroacfa4382008-12-04 10:06:33 -05004033 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 return -EPERM;
4035
4036 error = security_inode_symlink(dir, dentry, oldname);
4037 if (error)
4038 return error;
4039
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07004041 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00004042 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 return error;
4044}
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07004045EXPORT_SYMBOL(vfs_symlink2);
4046
4047int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
4048{
4049 return vfs_symlink2(NULL, dir, dentry, oldname);
4050}
Al Viro4d359502014-03-14 12:20:17 -04004051EXPORT_SYMBOL(vfs_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052
Heiko Carstens2e4d0922009-01-14 14:14:31 +01004053SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
4054 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055{
Al Viro2ad94ae2008-07-21 09:32:51 -04004056 int error;
Jeff Layton91a27b22012-10-10 15:25:28 -04004057 struct filename *from;
Dave Hansen6902d922006-09-30 23:29:01 -07004058 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04004059 struct path path;
Jeff Laytonf46d3562012-12-11 12:10:08 -05004060 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061
4062 from = getname(oldname);
Al Viro2ad94ae2008-07-21 09:32:51 -04004063 if (IS_ERR(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 return PTR_ERR(from);
Jeff Laytonf46d3562012-12-11 12:10:08 -05004065retry:
4066 dentry = user_path_create(newdfd, newname, &path, lookup_flags);
Dave Hansen6902d922006-09-30 23:29:01 -07004067 error = PTR_ERR(dentry);
4068 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04004069 goto out_putname;
Dave Hansen6902d922006-09-30 23:29:01 -07004070
Jeff Layton91a27b22012-10-10 15:25:28 -04004071 error = security_path_symlink(&path, dentry, from->name);
Al Viroa8104a92012-07-20 02:25:00 +04004072 if (!error)
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07004073 error = vfs_symlink2(path.mnt, path.dentry->d_inode, dentry, from->name);
Al Viro921a1652012-07-20 01:15:31 +04004074 done_path_create(&path, dentry);
Jeff Laytonf46d3562012-12-11 12:10:08 -05004075 if (retry_estale(error, lookup_flags)) {
4076 lookup_flags |= LOOKUP_REVAL;
4077 goto retry;
4078 }
Dave Hansen6902d922006-09-30 23:29:01 -07004079out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 putname(from);
4081 return error;
4082}
4083
Heiko Carstens3480b252009-01-14 14:14:16 +01004084SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004085{
4086 return sys_symlinkat(oldname, AT_FDCWD, newname);
4087}
4088
J. Bruce Fields146a8592011-09-20 17:14:31 -04004089/**
4090 * vfs_link - create a new link
4091 * @old_dentry: object to be linked
4092 * @dir: new parent
4093 * @new_dentry: where to create the new link
4094 * @delegated_inode: returns inode needing a delegation break
4095 *
4096 * The caller must hold dir->i_mutex
4097 *
4098 * If vfs_link discovers a delegation on the to-be-linked file in need
4099 * of breaking, it will return -EWOULDBLOCK and return a reference to the
4100 * inode in delegated_inode. The caller should then break the delegation
4101 * and retry. Because breaking a delegation may take a long time, the
4102 * caller should drop the i_mutex before doing so.
4103 *
4104 * Alternatively, a caller may pass NULL for delegated_inode. This may
4105 * be appropriate for callers that expect the underlying filesystem not
4106 * to be NFS exported.
4107 */
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07004108int vfs_link2(struct vfsmount *mnt, struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109{
4110 struct inode *inode = old_dentry->d_inode;
Al Viro8de52772012-02-06 12:45:27 -05004111 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 int error;
4113
4114 if (!inode)
4115 return -ENOENT;
4116
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07004117 error = may_create(mnt, dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 if (error)
4119 return error;
4120
4121 if (dir->i_sb != inode->i_sb)
4122 return -EXDEV;
4123
4124 /*
4125 * A link to an append-only or immutable file cannot be created.
4126 */
4127 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4128 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05004129 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02004131 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 return -EPERM;
4133
4134 error = security_inode_link(old_dentry, dir, new_dentry);
4135 if (error)
4136 return error;
4137
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02004138 mutex_lock(&inode->i_mutex);
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05304139 /* Make sure we don't allow creating hardlink to an unlinked file */
Al Virof4e0c302013-06-11 08:34:36 +04004140 if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05304141 error = -ENOENT;
Al Viro8de52772012-02-06 12:45:27 -05004142 else if (max_links && inode->i_nlink >= max_links)
4143 error = -EMLINK;
J. Bruce Fields146a8592011-09-20 17:14:31 -04004144 else {
4145 error = try_break_deleg(inode, delegated_inode);
4146 if (!error)
4147 error = dir->i_op->link(old_dentry, dir, new_dentry);
4148 }
Al Virof4e0c302013-06-11 08:34:36 +04004149
4150 if (!error && (inode->i_state & I_LINKABLE)) {
4151 spin_lock(&inode->i_lock);
4152 inode->i_state &= ~I_LINKABLE;
4153 spin_unlock(&inode->i_lock);
4154 }
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02004155 mutex_unlock(&inode->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07004156 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02004157 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 return error;
4159}
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07004160EXPORT_SYMBOL(vfs_link2);
4161
4162int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
4163{
4164 return vfs_link2(NULL, old_dentry, dir, new_dentry, delegated_inode);
4165}
Al Viro4d359502014-03-14 12:20:17 -04004166EXPORT_SYMBOL(vfs_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167
4168/*
4169 * Hardlinks are often used in delicate situations. We avoid
4170 * security-related surprises by not following symlinks on the
4171 * newname. --KAB
4172 *
4173 * We don't follow them on the oldname either to be compatible
4174 * with linux 2.0, and to avoid hard-linking to directories
4175 * and other special files. --ADM
4176 */
Heiko Carstens2e4d0922009-01-14 14:14:31 +01004177SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
4178 int, newdfd, const char __user *, newname, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179{
4180 struct dentry *new_dentry;
Al Virodae6ad82011-06-26 11:50:15 -04004181 struct path old_path, new_path;
J. Bruce Fields146a8592011-09-20 17:14:31 -04004182 struct inode *delegated_inode = NULL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304183 int how = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304186 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08004187 return -EINVAL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304188 /*
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07004189 * To use null names we require CAP_DAC_READ_SEARCH
4190 * This ensures that not everyone will be able to create
4191 * handlink using the passed filedescriptor.
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304192 */
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07004193 if (flags & AT_EMPTY_PATH) {
4194 if (!capable(CAP_DAC_READ_SEARCH))
4195 return -ENOENT;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304196 how = LOOKUP_EMPTY;
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07004197 }
Ulrich Drepperc04030e2006-02-24 13:04:21 -08004198
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304199 if (flags & AT_SYMLINK_FOLLOW)
4200 how |= LOOKUP_FOLLOW;
Jeff Layton442e31c2012-12-20 16:15:38 -05004201retry:
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304202 error = user_path_at(olddfd, oldname, how, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04004204 return error;
4205
Jeff Layton442e31c2012-12-20 16:15:38 -05004206 new_dentry = user_path_create(newdfd, newname, &new_path,
4207 (how & LOOKUP_REVAL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07004209 if (IS_ERR(new_dentry))
Al Virodae6ad82011-06-26 11:50:15 -04004210 goto out;
4211
4212 error = -EXDEV;
4213 if (old_path.mnt != new_path.mnt)
4214 goto out_dput;
Kees Cook800179c2012-07-25 17:29:07 -07004215 error = may_linkat(&old_path);
4216 if (unlikely(error))
4217 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04004218 error = security_path_link(old_path.dentry, &new_path, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004219 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04004220 goto out_dput;
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07004221 error = vfs_link2(old_path.mnt, old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode);
Dave Hansen75c3f292008-02-15 14:37:45 -08004222out_dput:
Al Viro921a1652012-07-20 01:15:31 +04004223 done_path_create(&new_path, new_dentry);
J. Bruce Fields146a8592011-09-20 17:14:31 -04004224 if (delegated_inode) {
4225 error = break_deleg_wait(&delegated_inode);
Oleg Drokind22e6332014-01-31 15:41:58 -05004226 if (!error) {
4227 path_put(&old_path);
J. Bruce Fields146a8592011-09-20 17:14:31 -04004228 goto retry;
Oleg Drokind22e6332014-01-31 15:41:58 -05004229 }
J. Bruce Fields146a8592011-09-20 17:14:31 -04004230 }
Jeff Layton442e31c2012-12-20 16:15:38 -05004231 if (retry_estale(error, how)) {
Oleg Drokind22e6332014-01-31 15:41:58 -05004232 path_put(&old_path);
Jeff Layton442e31c2012-12-20 16:15:38 -05004233 how |= LOOKUP_REVAL;
4234 goto retry;
4235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236out:
Al Viro2d8f3032008-07-22 09:59:21 -04004237 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238
4239 return error;
4240}
4241
Heiko Carstens3480b252009-01-14 14:14:16 +01004242SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004243{
Ulrich Drepperc04030e2006-02-24 13:04:21 -08004244 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004245}
4246
Miklos Szeredibc270272014-04-01 17:08:42 +02004247/**
4248 * vfs_rename - rename a filesystem object
4249 * @old_dir: parent of source
4250 * @old_dentry: source
4251 * @new_dir: parent of destination
4252 * @new_dentry: destination
4253 * @delegated_inode: returns an inode needing a delegation break
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004254 * @flags: rename flags
Miklos Szeredibc270272014-04-01 17:08:42 +02004255 *
4256 * The caller must hold multiple mutexes--see lock_rename()).
4257 *
4258 * If vfs_rename discovers a delegation in need of breaking at either
4259 * the source or destination, it will return -EWOULDBLOCK and return a
4260 * reference to the inode in delegated_inode. The caller should then
4261 * break the delegation and retry. Because breaking a delegation may
4262 * take a long time, the caller should drop all locks before doing
4263 * so.
4264 *
4265 * Alternatively, a caller may pass NULL for delegated_inode. This may
4266 * be appropriate for callers that expect the underlying filesystem not
4267 * to be NFS exported.
4268 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 * The worst of all namespace operations - renaming directory. "Perverted"
4270 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
4271 * Problems:
J. Bruce Fieldsd03b29a2014-02-17 16:52:33 -05004272 * a) we can get into loop creation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 * b) race potential - two innocent renames can create a loop together.
4274 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004275 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 * story.
J. Bruce Fields6cedba82012-03-05 11:40:41 -05004277 * c) we have to lock _four_ objects - parents and victim (if it exists),
4278 * and source (if it is not a directory).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004279 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 * whether the target exists). Solution: try to be smart with locking
4281 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004282 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 * move will be locked. Thus we can rank directories by the tree
4284 * (ancestors first) and rank all non-directories after them.
4285 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004286 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 * HOWEVER, it relies on the assumption that any object with ->lookup()
4288 * has no more than 1 dentry. If "hybrid" objects will ever appear,
4289 * we'd better make sure that there's no link(2) for them.
Sage Weile4eaac02011-05-24 13:06:07 -07004290 * d) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004291 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05004293 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 * locking].
4295 */
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07004296int vfs_rename2(struct vfsmount *mnt,
4297 struct inode *old_dir, struct dentry *old_dentry,
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004298 struct inode *new_dir, struct dentry *new_dentry,
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004299 struct inode **delegated_inode, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300{
4301 int error;
Miklos Szeredibc270272014-04-01 17:08:42 +02004302 bool is_dir = d_is_dir(old_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004303 struct inode *source = old_dentry->d_inode;
4304 struct inode *target = new_dentry->d_inode;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004305 bool new_is_dir = false;
4306 unsigned max_links = new_dir->i_sb->s_max_links;
Al Viro407669f2017-07-07 14:51:19 -04004307 struct name_snapshot old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308
Miklos Szeredi8e5bb3c2016-05-11 01:16:37 +02004309 /*
4310 * Check source == target.
4311 * On overlayfs need to look at underlying inodes.
4312 */
4313 if (vfs_select_inode(old_dentry, 0) == vfs_select_inode(new_dentry, 0))
Miklos Szeredibc270272014-04-01 17:08:42 +02004314 return 0;
4315
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07004316 error = may_delete(mnt, old_dir, old_dentry, is_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 if (error)
4318 return error;
4319
Miklos Szeredida1ce062014-04-01 17:08:43 +02004320 if (!target) {
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07004321 error = may_create(mnt, new_dir, new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004322 } else {
4323 new_is_dir = d_is_dir(new_dentry);
4324
4325 if (!(flags & RENAME_EXCHANGE))
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07004326 error = may_delete(mnt, new_dir, new_dentry, is_dir);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004327 else
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07004328 error = may_delete(mnt, new_dir, new_dentry, new_is_dir);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 if (error)
4331 return error;
4332
Miklos Szeredi7177a9c2014-07-23 15:15:30 +02004333 if (!old_dir->i_op->rename && !old_dir->i_op->rename2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 return -EPERM;
4335
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004336 if (flags && !old_dir->i_op->rename2)
4337 return -EINVAL;
4338
Miklos Szeredibc270272014-04-01 17:08:42 +02004339 /*
4340 * If we are going to change the parent - check write permissions,
4341 * we'll need to flip '..'.
4342 */
Miklos Szeredida1ce062014-04-01 17:08:43 +02004343 if (new_dir != old_dir) {
4344 if (is_dir) {
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07004345 error = inode_permission2(mnt, source, MAY_WRITE);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004346 if (error)
4347 return error;
4348 }
4349 if ((flags & RENAME_EXCHANGE) && new_is_dir) {
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07004350 error = inode_permission2(mnt, target, MAY_WRITE);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004351 if (error)
4352 return error;
4353 }
Miklos Szeredibc270272014-04-01 17:08:42 +02004354 }
Robert Love0eeca282005-07-12 17:06:03 -04004355
Miklos Szeredi0b3974e2014-04-01 17:08:43 +02004356 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry,
4357 flags);
Miklos Szeredibc270272014-04-01 17:08:42 +02004358 if (error)
4359 return error;
4360
Al Viro407669f2017-07-07 14:51:19 -04004361 take_dentry_name_snapshot(&old_name, old_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004362 dget(new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004363 if (!is_dir || (flags & RENAME_EXCHANGE))
Miklos Szeredibc270272014-04-01 17:08:42 +02004364 lock_two_nondirectories(source, target);
4365 else if (target)
4366 mutex_lock(&target->i_mutex);
4367
4368 error = -EBUSY;
Eric W. Biederman7af13642013-10-04 19:15:13 -07004369 if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry))
Miklos Szeredibc270272014-04-01 17:08:42 +02004370 goto out;
4371
Miklos Szeredida1ce062014-04-01 17:08:43 +02004372 if (max_links && new_dir != old_dir) {
Miklos Szeredibc270272014-04-01 17:08:42 +02004373 error = -EMLINK;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004374 if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links)
Miklos Szeredibc270272014-04-01 17:08:42 +02004375 goto out;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004376 if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir &&
4377 old_dir->i_nlink >= max_links)
4378 goto out;
4379 }
4380 if (is_dir && !(flags & RENAME_EXCHANGE) && target)
4381 shrink_dcache_parent(new_dentry);
4382 if (!is_dir) {
Miklos Szeredibc270272014-04-01 17:08:42 +02004383 error = try_break_deleg(source, delegated_inode);
4384 if (error)
4385 goto out;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004386 }
4387 if (target && !new_is_dir) {
4388 error = try_break_deleg(target, delegated_inode);
4389 if (error)
4390 goto out;
Miklos Szeredibc270272014-04-01 17:08:42 +02004391 }
Miklos Szeredi7177a9c2014-07-23 15:15:30 +02004392 if (!old_dir->i_op->rename2) {
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004393 error = old_dir->i_op->rename(old_dir, old_dentry,
4394 new_dir, new_dentry);
4395 } else {
Miklos Szeredi7177a9c2014-07-23 15:15:30 +02004396 WARN_ON(old_dir->i_op->rename != NULL);
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004397 error = old_dir->i_op->rename2(old_dir, old_dentry,
4398 new_dir, new_dentry, flags);
4399 }
Miklos Szeredibc270272014-04-01 17:08:42 +02004400 if (error)
4401 goto out;
4402
Miklos Szeredida1ce062014-04-01 17:08:43 +02004403 if (!(flags & RENAME_EXCHANGE) && target) {
Miklos Szeredibc270272014-04-01 17:08:42 +02004404 if (is_dir)
4405 target->i_flags |= S_DEAD;
4406 dont_mount(new_dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07004407 detach_mounts(new_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004408 }
Miklos Szeredida1ce062014-04-01 17:08:43 +02004409 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) {
4410 if (!(flags & RENAME_EXCHANGE))
4411 d_move(old_dentry, new_dentry);
4412 else
4413 d_exchange(old_dentry, new_dentry);
4414 }
Miklos Szeredibc270272014-04-01 17:08:42 +02004415out:
Miklos Szeredida1ce062014-04-01 17:08:43 +02004416 if (!is_dir || (flags & RENAME_EXCHANGE))
Miklos Szeredibc270272014-04-01 17:08:42 +02004417 unlock_two_nondirectories(source, target);
4418 else if (target)
4419 mutex_unlock(&target->i_mutex);
4420 dput(new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004421 if (!error) {
Al Viro407669f2017-07-07 14:51:19 -04004422 fsnotify_move(old_dir, new_dir, old_name.name, is_dir,
Miklos Szeredida1ce062014-04-01 17:08:43 +02004423 !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
4424 if (flags & RENAME_EXCHANGE) {
4425 fsnotify_move(new_dir, old_dir, old_dentry->d_name.name,
4426 new_is_dir, NULL, new_dentry);
4427 }
4428 }
Al Viro407669f2017-07-07 14:51:19 -04004429 release_dentry_name_snapshot(&old_name);
Robert Love0eeca282005-07-12 17:06:03 -04004430
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 return error;
4432}
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07004433EXPORT_SYMBOL(vfs_rename2);
4434
4435int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
4436 struct inode *new_dir, struct dentry *new_dentry,
4437 struct inode **delegated_inode, unsigned int flags)
4438{
4439 return vfs_rename2(NULL, old_dir, old_dentry, new_dir, new_dentry, delegated_inode, flags);
4440}
Al Viro4d359502014-03-14 12:20:17 -04004441EXPORT_SYMBOL(vfs_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004443SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
4444 int, newdfd, const char __user *, newname, unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445{
Al Viro2ad94ae2008-07-21 09:32:51 -04004446 struct dentry *old_dentry, *new_dentry;
4447 struct dentry *trap;
Al Virof5beed72015-04-30 16:09:11 -04004448 struct path old_path, new_path;
4449 struct qstr old_last, new_last;
4450 int old_type, new_type;
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004451 struct inode *delegated_inode = NULL;
Jeff Layton91a27b22012-10-10 15:25:28 -04004452 struct filename *from;
4453 struct filename *to;
Al Virof5beed72015-04-30 16:09:11 -04004454 unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET;
Jeff Laytonc6a94282012-12-11 12:10:10 -05004455 bool should_retry = false;
Al Viro2ad94ae2008-07-21 09:32:51 -04004456 int error;
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004457
Miklos Szeredi0d7a8552014-10-24 00:14:37 +02004458 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredida1ce062014-04-01 17:08:43 +02004459 return -EINVAL;
4460
Miklos Szeredi0d7a8552014-10-24 00:14:37 +02004461 if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) &&
4462 (flags & RENAME_EXCHANGE))
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004463 return -EINVAL;
4464
Miklos Szeredi0d7a8552014-10-24 00:14:37 +02004465 if ((flags & RENAME_WHITEOUT) && !capable(CAP_MKNOD))
4466 return -EPERM;
4467
Al Virof5beed72015-04-30 16:09:11 -04004468 if (flags & RENAME_EXCHANGE)
4469 target_flags = 0;
4470
Jeff Laytonc6a94282012-12-11 12:10:10 -05004471retry:
Al Virof5beed72015-04-30 16:09:11 -04004472 from = user_path_parent(olddfd, oldname,
4473 &old_path, &old_last, &old_type, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04004474 if (IS_ERR(from)) {
4475 error = PTR_ERR(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 goto exit;
Jeff Layton91a27b22012-10-10 15:25:28 -04004477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478
Al Virof5beed72015-04-30 16:09:11 -04004479 to = user_path_parent(newdfd, newname,
4480 &new_path, &new_last, &new_type, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04004481 if (IS_ERR(to)) {
4482 error = PTR_ERR(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 goto exit1;
Jeff Layton91a27b22012-10-10 15:25:28 -04004484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485
4486 error = -EXDEV;
Al Virof5beed72015-04-30 16:09:11 -04004487 if (old_path.mnt != new_path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 goto exit2;
4489
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 error = -EBUSY;
Al Virof5beed72015-04-30 16:09:11 -04004491 if (old_type != LAST_NORM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 goto exit2;
4493
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004494 if (flags & RENAME_NOREPLACE)
4495 error = -EEXIST;
Al Virof5beed72015-04-30 16:09:11 -04004496 if (new_type != LAST_NORM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 goto exit2;
4498
Al Virof5beed72015-04-30 16:09:11 -04004499 error = mnt_want_write(old_path.mnt);
Jan Karac30dabf2012-06-12 16:20:30 +02004500 if (error)
4501 goto exit2;
4502
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004503retry_deleg:
Al Virof5beed72015-04-30 16:09:11 -04004504 trap = lock_rename(new_path.dentry, old_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505
Al Virof5beed72015-04-30 16:09:11 -04004506 old_dentry = __lookup_hash(&old_last, old_path.dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 error = PTR_ERR(old_dentry);
4508 if (IS_ERR(old_dentry))
4509 goto exit3;
4510 /* source must exist */
4511 error = -ENOENT;
David Howellsb18825a2013-09-12 19:22:53 +01004512 if (d_is_negative(old_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 goto exit4;
Al Virof5beed72015-04-30 16:09:11 -04004514 new_dentry = __lookup_hash(&new_last, new_path.dentry, lookup_flags | target_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 error = PTR_ERR(new_dentry);
4516 if (IS_ERR(new_dentry))
4517 goto exit4;
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004518 error = -EEXIST;
4519 if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry))
4520 goto exit5;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004521 if (flags & RENAME_EXCHANGE) {
4522 error = -ENOENT;
4523 if (d_is_negative(new_dentry))
4524 goto exit5;
4525
4526 if (!d_is_dir(new_dentry)) {
4527 error = -ENOTDIR;
Al Virof5beed72015-04-30 16:09:11 -04004528 if (new_last.name[new_last.len])
Miklos Szeredida1ce062014-04-01 17:08:43 +02004529 goto exit5;
4530 }
4531 }
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004532 /* unless the source is a directory trailing slashes give -ENOTDIR */
4533 if (!d_is_dir(old_dentry)) {
4534 error = -ENOTDIR;
Al Virof5beed72015-04-30 16:09:11 -04004535 if (old_last.name[old_last.len])
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004536 goto exit5;
Al Virof5beed72015-04-30 16:09:11 -04004537 if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len])
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004538 goto exit5;
4539 }
4540 /* source should not be ancestor of target */
4541 error = -EINVAL;
4542 if (old_dentry == trap)
4543 goto exit5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 /* target should not be an ancestor of source */
Miklos Szeredida1ce062014-04-01 17:08:43 +02004545 if (!(flags & RENAME_EXCHANGE))
4546 error = -ENOTEMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 if (new_dentry == trap)
4548 goto exit5;
4549
Al Virof5beed72015-04-30 16:09:11 -04004550 error = security_path_rename(&old_path, old_dentry,
4551 &new_path, new_dentry, flags);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004552 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02004553 goto exit5;
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07004554 error = vfs_rename2(old_path.mnt, old_path.dentry->d_inode, old_dentry,
Al Virof5beed72015-04-30 16:09:11 -04004555 new_path.dentry->d_inode, new_dentry,
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004556 &delegated_inode, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557exit5:
4558 dput(new_dentry);
4559exit4:
4560 dput(old_dentry);
4561exit3:
Al Virof5beed72015-04-30 16:09:11 -04004562 unlock_rename(new_path.dentry, old_path.dentry);
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004563 if (delegated_inode) {
4564 error = break_deleg_wait(&delegated_inode);
4565 if (!error)
4566 goto retry_deleg;
4567 }
Al Virof5beed72015-04-30 16:09:11 -04004568 mnt_drop_write(old_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569exit2:
Jeff Laytonc6a94282012-12-11 12:10:10 -05004570 if (retry_estale(error, lookup_flags))
4571 should_retry = true;
Al Virof5beed72015-04-30 16:09:11 -04004572 path_put(&new_path);
Al Viro2ad94ae2008-07-21 09:32:51 -04004573 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574exit1:
Al Virof5beed72015-04-30 16:09:11 -04004575 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 putname(from);
Jeff Laytonc6a94282012-12-11 12:10:10 -05004577 if (should_retry) {
4578 should_retry = false;
4579 lookup_flags |= LOOKUP_REVAL;
4580 goto retry;
4581 }
Al Viro2ad94ae2008-07-21 09:32:51 -04004582exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 return error;
4584}
4585
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004586SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4587 int, newdfd, const char __user *, newname)
4588{
4589 return sys_renameat2(olddfd, oldname, newdfd, newname, 0);
4590}
4591
Heiko Carstensa26eab22009-01-14 14:14:17 +01004592SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004593{
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004594 return sys_renameat2(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004595}
4596
Miklos Szeredi787fb6b2014-10-24 00:14:36 +02004597int vfs_whiteout(struct inode *dir, struct dentry *dentry)
4598{
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -07004599 int error = may_create(NULL, dir, dentry);
Miklos Szeredi787fb6b2014-10-24 00:14:36 +02004600 if (error)
4601 return error;
4602
4603 if (!dir->i_op->mknod)
4604 return -EPERM;
4605
4606 return dir->i_op->mknod(dir, dentry,
4607 S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
4608}
4609EXPORT_SYMBOL(vfs_whiteout);
4610
Al Viro5d826c82014-03-14 13:42:45 -04004611int readlink_copy(char __user *buffer, int buflen, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612{
Al Viro5d826c82014-03-14 13:42:45 -04004613 int len = PTR_ERR(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 if (IS_ERR(link))
4615 goto out;
4616
4617 len = strlen(link);
4618 if (len > (unsigned) buflen)
4619 len = buflen;
4620 if (copy_to_user(buffer, link, len))
4621 len = -EFAULT;
4622out:
4623 return len;
4624}
Al Viro5d826c82014-03-14 13:42:45 -04004625EXPORT_SYMBOL(readlink_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626
4627/*
4628 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
4629 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
4630 * using) it for any given inode is up to filesystem.
4631 */
4632int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4633{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004634 void *cookie;
Al Viro5f2c4172015-05-07 11:14:26 -04004635 struct inode *inode = d_inode(dentry);
4636 const char *link = inode->i_link;
Marcin Slusarz694a1762008-06-09 16:40:37 -07004637 int res;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004638
Al Virod4dee482015-04-30 20:08:02 -04004639 if (!link) {
Al Viro5f2c4172015-05-07 11:14:26 -04004640 link = inode->i_op->follow_link(dentry, &cookie);
Al Virod4dee482015-04-30 20:08:02 -04004641 if (IS_ERR(link))
4642 return PTR_ERR(link);
4643 }
Al Viro680baac2015-05-02 13:32:22 -04004644 res = readlink_copy(buffer, buflen, link);
Al Viro5f2c4172015-05-07 11:14:26 -04004645 if (inode->i_op->put_link)
4646 inode->i_op->put_link(inode, cookie);
Marcin Slusarz694a1762008-06-09 16:40:37 -07004647 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648}
Al Viro4d359502014-03-14 12:20:17 -04004649EXPORT_SYMBOL(generic_readlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651/* get the link contents into pagecache */
4652static char *page_getlink(struct dentry * dentry, struct page **ppage)
4653{
Duane Griffinebd09ab2008-12-19 20:47:12 +00004654 char *kaddr;
4655 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 struct address_space *mapping = dentry->d_inode->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -07004657 page = read_mapping_page(mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 if (IS_ERR(page))
Nick Piggin6fe69002007-05-06 14:49:04 -07004659 return (char*)page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660 *ppage = page;
Duane Griffinebd09ab2008-12-19 20:47:12 +00004661 kaddr = kmap(page);
4662 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
4663 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664}
4665
4666int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4667{
4668 struct page *page = NULL;
Al Viro5d826c82014-03-14 13:42:45 -04004669 int res = readlink_copy(buffer, buflen, page_getlink(dentry, &page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 if (page) {
4671 kunmap(page);
4672 page_cache_release(page);
4673 }
4674 return res;
4675}
Al Viro4d359502014-03-14 12:20:17 -04004676EXPORT_SYMBOL(page_readlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677
Al Viro6e771372015-05-02 13:37:52 -04004678const char *page_follow_link_light(struct dentry *dentry, void **cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004680 struct page *page = NULL;
Al Viro680baac2015-05-02 13:32:22 -04004681 char *res = page_getlink(dentry, &page);
4682 if (!IS_ERR(res))
4683 *cookie = page;
4684 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685}
Al Viro4d359502014-03-14 12:20:17 -04004686EXPORT_SYMBOL(page_follow_link_light);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687
Al Viro5f2c4172015-05-07 11:14:26 -04004688void page_put_link(struct inode *unused, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004690 struct page *page = cookie;
Al Viro680baac2015-05-02 13:32:22 -04004691 kunmap(page);
4692 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693}
Al Viro4d359502014-03-14 12:20:17 -04004694EXPORT_SYMBOL(page_put_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695
Nick Piggin54566b22009-01-04 12:00:53 -08004696/*
4697 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
4698 */
4699int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700{
4701 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004702 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07004703 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08004704 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 char *kaddr;
Nick Piggin54566b22009-01-04 12:00:53 -08004706 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
4707 if (nofs)
4708 flags |= AOP_FLAG_NOFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709
NeilBrown7e53cac2006-03-25 03:07:57 -08004710retry:
Nick Pigginafddba42007-10-16 01:25:01 -07004711 err = pagecache_write_begin(NULL, mapping, 0, len-1,
Nick Piggin54566b22009-01-04 12:00:53 -08004712 flags, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07004714 goto fail;
4715
Cong Wange8e3c3d2011-11-25 23:14:27 +08004716 kaddr = kmap_atomic(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 memcpy(kaddr, symname, len-1);
Cong Wange8e3c3d2011-11-25 23:14:27 +08004718 kunmap_atomic(kaddr);
Nick Pigginafddba42007-10-16 01:25:01 -07004719
4720 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4721 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 if (err < 0)
4723 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07004724 if (err < len-1)
4725 goto retry;
4726
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727 mark_inode_dirty(inode);
4728 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729fail:
4730 return err;
4731}
Al Viro4d359502014-03-14 12:20:17 -04004732EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004734int page_symlink(struct inode *inode, const char *symname, int len)
4735{
4736 return __page_symlink(inode, symname, len,
Michal Hockoc62d2552015-11-06 16:28:49 -08004737 !mapping_gfp_constraint(inode->i_mapping, __GFP_FS));
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004738}
Al Viro4d359502014-03-14 12:20:17 -04004739EXPORT_SYMBOL(page_symlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004740
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08004741const struct inode_operations page_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 .readlink = generic_readlink,
4743 .follow_link = page_follow_link_light,
4744 .put_link = page_put_link,
4745};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746EXPORT_SYMBOL(page_symlink_inode_operations);