blob: 1a117c0d13c5cbeca8bd329aa4b1cb134dd75f0d [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) {
222 struct filename *tmp;
223
224 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
225 if (unlikely(!tmp)) {
226 __putname(result);
227 return ERR_PTR(-ENOMEM);
228 }
229 tmp->name = (char *)result;
Paul Moore08518542015-01-21 23:59:56 -0500230 result = tmp;
231 } else {
232 __putname(result);
233 return ERR_PTR(-ENAMETOOLONG);
234 }
235 memcpy((char *)result->name, filename, len);
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800236 result->uptr = NULL;
237 result->aname = NULL;
Paul Moore55422d02015-01-22 00:00:23 -0500238 result->refcnt = 1;
Paul Moorefd3522f2015-01-22 00:00:10 -0500239 audit_getname(result);
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800240
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800241 return result;
242}
243
Jeff Layton91a27b22012-10-10 15:25:28 -0400244void putname(struct filename *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
Paul Moore55422d02015-01-22 00:00:23 -0500246 BUG_ON(name->refcnt <= 0);
247
248 if (--name->refcnt > 0)
249 return;
250
Al Virofd2f7cb2015-02-22 20:07:13 -0500251 if (name->name != name->iname) {
Paul Moore55422d02015-01-22 00:00:23 -0500252 __putname(name->name);
253 kfree(name);
254 } else
255 __putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Linus Torvaldse77819e2011-07-22 19:30:19 -0700258static int check_acl(struct inode *inode, int mask)
259{
Linus Torvalds84635d62011-07-25 22:47:03 -0700260#ifdef CONFIG_FS_POSIX_ACL
Linus Torvaldse77819e2011-07-22 19:30:19 -0700261 struct posix_acl *acl;
262
Linus Torvaldse77819e2011-07-22 19:30:19 -0700263 if (mask & MAY_NOT_BLOCK) {
Al Viro35678662011-08-02 21:32:13 -0400264 acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
265 if (!acl)
Linus Torvaldse77819e2011-07-22 19:30:19 -0700266 return -EAGAIN;
Al Viro35678662011-08-02 21:32:13 -0400267 /* no ->get_acl() calls in RCU mode... */
268 if (acl == ACL_NOT_CACHED)
269 return -ECHILD;
Ari Savolainen206b1d02011-08-06 19:43:07 +0300270 return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
Linus Torvaldse77819e2011-07-22 19:30:19 -0700271 }
272
Christoph Hellwig2982baa2013-12-20 05:16:38 -0800273 acl = get_acl(inode, ACL_TYPE_ACCESS);
274 if (IS_ERR(acl))
275 return PTR_ERR(acl);
Linus Torvaldse77819e2011-07-22 19:30:19 -0700276 if (acl) {
277 int error = posix_acl_permission(inode, acl, mask);
278 posix_acl_release(acl);
279 return error;
280 }
Linus Torvalds84635d62011-07-25 22:47:03 -0700281#endif
Linus Torvaldse77819e2011-07-22 19:30:19 -0700282
283 return -EAGAIN;
284}
285
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700286/*
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530287 * This does the basic permission checking
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700288 */
Al Viro7e401452011-06-20 19:12:17 -0400289static int acl_permission_check(struct inode *inode, int mask)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700290{
Linus Torvalds26cf46b2011-05-13 11:51:01 -0700291 unsigned int mode = inode->i_mode;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700292
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -0800293 if (likely(uid_eq(current_fsuid(), inode->i_uid)))
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700294 mode >>= 6;
295 else {
Linus Torvaldse77819e2011-07-22 19:30:19 -0700296 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
Al Viro7e401452011-06-20 19:12:17 -0400297 int error = check_acl(inode, mask);
Nick Pigginb74c79e2011-01-07 17:49:58 +1100298 if (error != -EAGAIN)
299 return error;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700300 }
301
302 if (in_group_p(inode->i_gid))
303 mode >>= 3;
304 }
305
306 /*
307 * If the DACs are ok we don't need any capability check.
308 */
Al Viro9c2c7032011-06-20 19:06:22 -0400309 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700310 return 0;
311 return -EACCES;
312}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314/**
Nick Pigginb74c79e2011-01-07 17:49:58 +1100315 * generic_permission - check for access rights on a Posix-like filesystem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 * @inode: inode to check access rights for
Andreas Gruenbacher8fd90c82011-10-23 23:13:30 +0530317 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 *
319 * Used to check for read/write/execute permissions on a file.
320 * We use "fsuid" for this, letting us set arbitrary permissions
321 * for filesystem access without changing the "normal" uids which
Nick Pigginb74c79e2011-01-07 17:49:58 +1100322 * are used for other things.
323 *
324 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
325 * request cannot be satisfied (eg. requires blocking or too much complexity).
326 * It would then be called again in ref-walk mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 */
Al Viro2830ba72011-06-20 19:16:29 -0400328int generic_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700330 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 /*
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530333 * Do the basic permission checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 */
Al Viro7e401452011-06-20 19:12:17 -0400335 ret = acl_permission_check(inode, mask);
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700336 if (ret != -EACCES)
337 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Al Virod594e7e2011-06-20 19:55:42 -0400339 if (S_ISDIR(inode->i_mode)) {
340 /* DACs are overridable for directories */
Andy Lutomirski23adbe12014-06-10 12:45:42 -0700341 if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
Al Virod594e7e2011-06-20 19:55:42 -0400342 return 0;
343 if (!(mask & MAY_WRITE))
Andy Lutomirski23adbe12014-06-10 12:45:42 -0700344 if (capable_wrt_inode_uidgid(inode,
345 CAP_DAC_READ_SEARCH))
Al Virod594e7e2011-06-20 19:55:42 -0400346 return 0;
347 return -EACCES;
348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 /*
350 * Read/write DACs are always overridable.
Al Virod594e7e2011-06-20 19:55:42 -0400351 * Executable DACs are overridable when there is
352 * at least one exec bit set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 */
Al Virod594e7e2011-06-20 19:55:42 -0400354 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
Andy Lutomirski23adbe12014-06-10 12:45:42 -0700355 if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return 0;
357
358 /*
359 * Searching includes executable on directories, else just read.
360 */
Serge E. Hallyn7ea66002009-12-29 14:50:19 -0600361 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
Al Virod594e7e2011-06-20 19:55:42 -0400362 if (mask == MAY_READ)
Andy Lutomirski23adbe12014-06-10 12:45:42 -0700363 if (capable_wrt_inode_uidgid(inode, CAP_DAC_READ_SEARCH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return 0;
365
366 return -EACCES;
367}
Al Viro4d359502014-03-14 12:20:17 -0400368EXPORT_SYMBOL(generic_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700370/*
371 * We _really_ want to just do "generic_permission()" without
372 * even looking at the inode->i_op values. So we keep a cache
373 * flag in inode->i_opflags, that says "this has not special
374 * permission function, use the fast case".
375 */
376static inline int do_inode_permission(struct inode *inode, int mask)
377{
378 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
379 if (likely(inode->i_op->permission))
380 return inode->i_op->permission(inode, mask);
381
382 /* This gets set once for the inode lifetime */
383 spin_lock(&inode->i_lock);
384 inode->i_opflags |= IOP_FASTPERM;
385 spin_unlock(&inode->i_lock);
386 }
387 return generic_permission(inode, mask);
388}
389
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200390/**
David Howells0bdaea92012-06-25 12:55:46 +0100391 * __inode_permission - Check for access rights to a given inode
392 * @inode: Inode to check permission on
393 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200394 *
David Howells0bdaea92012-06-25 12:55:46 +0100395 * Check for read/write/execute permissions on an inode.
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530396 *
397 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
David Howells0bdaea92012-06-25 12:55:46 +0100398 *
399 * This does not check for a read-only file system. You probably want
400 * inode_permission().
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200401 */
David Howells0bdaea92012-06-25 12:55:46 +0100402int __inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Al Viroe6305c42008-07-15 21:03:57 -0400404 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700406 if (unlikely(mask & MAY_WRITE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 /*
408 * Nobody gets write access to an immutable file.
409 */
410 if (IS_IMMUTABLE(inode))
411 return -EACCES;
412 }
413
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700414 retval = do_inode_permission(inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if (retval)
416 return retval;
417
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700418 retval = devcgroup_inode_permission(inode, mask);
419 if (retval)
420 return retval;
421
Eric Parisd09ca732010-07-23 11:43:57 -0400422 return security_inode_permission(inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
Miklos Szeredibd5d0852014-10-24 00:14:35 +0200424EXPORT_SYMBOL(__inode_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Al Virof4d6ff82011-06-19 13:14:21 -0400426/**
David Howells0bdaea92012-06-25 12:55:46 +0100427 * sb_permission - Check superblock-level permissions
428 * @sb: Superblock of inode to check permission on
Randy Dunlap55852632012-08-18 17:39:25 -0700429 * @inode: Inode to check permission on
David Howells0bdaea92012-06-25 12:55:46 +0100430 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
431 *
432 * Separate out file-system wide checks from inode-specific permission checks.
433 */
434static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
435{
436 if (unlikely(mask & MAY_WRITE)) {
437 umode_t mode = inode->i_mode;
438
439 /* Nobody gets write access to a read-only fs. */
440 if ((sb->s_flags & MS_RDONLY) &&
441 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
442 return -EROFS;
443 }
444 return 0;
445}
446
447/**
448 * inode_permission - Check for access rights to a given inode
449 * @inode: Inode to check permission on
450 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
451 *
452 * Check for read/write/execute permissions on an inode. We use fs[ug]id for
453 * this, letting us set arbitrary permissions for filesystem access without
454 * changing the "normal" UIDs which are used for other things.
455 *
456 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
457 */
458int inode_permission(struct inode *inode, int mask)
459{
460 int retval;
461
462 retval = sb_permission(inode->i_sb, inode, mask);
463 if (retval)
464 return retval;
465 return __inode_permission(inode, mask);
466}
Al Viro4d359502014-03-14 12:20:17 -0400467EXPORT_SYMBOL(inode_permission);
David Howells0bdaea92012-06-25 12:55:46 +0100468
469/**
Jan Blunck5dd784d2008-02-14 19:34:38 -0800470 * path_get - get a reference to a path
471 * @path: path to get the reference to
472 *
473 * Given a path increment the reference count to the dentry and the vfsmount.
474 */
Al Virodcf787f2013-03-01 23:51:07 -0500475void path_get(const struct path *path)
Jan Blunck5dd784d2008-02-14 19:34:38 -0800476{
477 mntget(path->mnt);
478 dget(path->dentry);
479}
480EXPORT_SYMBOL(path_get);
481
482/**
Jan Blunck1d957f92008-02-14 19:34:35 -0800483 * path_put - put a reference to a path
484 * @path: path to put the reference to
485 *
486 * Given a path decrement the reference count to the dentry and the vfsmount.
487 */
Al Virodcf787f2013-03-01 23:51:07 -0500488void path_put(const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Jan Blunck1d957f92008-02-14 19:34:35 -0800490 dput(path->dentry);
491 mntput(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
Jan Blunck1d957f92008-02-14 19:34:35 -0800493EXPORT_SYMBOL(path_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Al Viro894bc8c2015-05-02 07:16:16 -0400495#define EMBEDDED_LEVELS 2
Al Viro1f55a6e2014-11-01 19:30:41 -0400496struct nameidata {
497 struct path path;
Al Viro1cf26652015-05-06 16:01:56 -0400498 struct qstr last;
Al Viro1f55a6e2014-11-01 19:30:41 -0400499 struct path root;
500 struct inode *inode; /* path.dentry.d_inode */
501 unsigned int flags;
Al Viro9883d182015-05-13 07:28:08 -0400502 unsigned seq, m_seq;
Al Viro1f55a6e2014-11-01 19:30:41 -0400503 int last_type;
504 unsigned depth;
NeilBrown756daf22015-03-23 13:37:38 +1100505 int total_link_count;
Al Viro697fc6c2015-05-02 19:38:35 -0400506 struct saved {
507 struct path link;
508 void *cookie;
509 const char *name;
Al Viro237d8b32015-05-07 09:21:14 -0400510 struct inode *inode;
Al Viro0450b2d2015-05-08 13:23:53 -0400511 unsigned seq;
Al Viro894bc8c2015-05-02 07:16:16 -0400512 } *stack, internal[EMBEDDED_LEVELS];
Al Viro9883d182015-05-13 07:28:08 -0400513 struct filename *name;
514 struct nameidata *saved;
515 unsigned root_seq;
516 int dfd;
Al Viro1f55a6e2014-11-01 19:30:41 -0400517};
518
Al Viro9883d182015-05-13 07:28:08 -0400519static void set_nameidata(struct nameidata *p, int dfd, struct filename *name)
Al Viro894bc8c2015-05-02 07:16:16 -0400520{
NeilBrown756daf22015-03-23 13:37:38 +1100521 struct nameidata *old = current->nameidata;
522 p->stack = p->internal;
Al Viroc8a53ee2015-05-12 18:43:07 -0400523 p->dfd = dfd;
524 p->name = name;
NeilBrown756daf22015-03-23 13:37:38 +1100525 p->total_link_count = old ? old->total_link_count : 0;
Al Viro9883d182015-05-13 07:28:08 -0400526 p->saved = old;
NeilBrown756daf22015-03-23 13:37:38 +1100527 current->nameidata = p;
Al Viro894bc8c2015-05-02 07:16:16 -0400528}
529
Al Viro9883d182015-05-13 07:28:08 -0400530static void restore_nameidata(void)
Al Viro894bc8c2015-05-02 07:16:16 -0400531{
Al Viro9883d182015-05-13 07:28:08 -0400532 struct nameidata *now = current->nameidata, *old = now->saved;
NeilBrown756daf22015-03-23 13:37:38 +1100533
534 current->nameidata = old;
535 if (old)
536 old->total_link_count = now->total_link_count;
537 if (now->stack != now->internal) {
538 kfree(now->stack);
539 now->stack = now->internal;
Al Viro894bc8c2015-05-02 07:16:16 -0400540 }
541}
542
543static int __nd_alloc_stack(struct nameidata *nd)
544{
Al Virobc40aee2015-05-09 13:04:24 -0400545 struct saved *p;
546
547 if (nd->flags & LOOKUP_RCU) {
548 p= kmalloc(MAXSYMLINKS * sizeof(struct saved),
549 GFP_ATOMIC);
550 if (unlikely(!p))
551 return -ECHILD;
552 } else {
553 p= kmalloc(MAXSYMLINKS * sizeof(struct saved),
Al Viro894bc8c2015-05-02 07:16:16 -0400554 GFP_KERNEL);
Al Virobc40aee2015-05-09 13:04:24 -0400555 if (unlikely(!p))
556 return -ENOMEM;
557 }
Al Viro894bc8c2015-05-02 07:16:16 -0400558 memcpy(p, nd->internal, sizeof(nd->internal));
559 nd->stack = p;
560 return 0;
561}
562
563static inline int nd_alloc_stack(struct nameidata *nd)
564{
Al Viroda4e0be2015-05-03 20:52:15 -0400565 if (likely(nd->depth != EMBEDDED_LEVELS))
Al Viro894bc8c2015-05-02 07:16:16 -0400566 return 0;
567 if (likely(nd->stack != nd->internal))
568 return 0;
569 return __nd_alloc_stack(nd);
570}
571
Al Viro79733872015-05-09 12:55:43 -0400572static void drop_links(struct nameidata *nd)
573{
574 int i = nd->depth;
575 while (i--) {
576 struct saved *last = nd->stack + i;
577 struct inode *inode = last->inode;
578 if (last->cookie && inode->i_op->put_link) {
579 inode->i_op->put_link(inode, last->cookie);
580 last->cookie = NULL;
581 }
582 }
583}
584
585static void terminate_walk(struct nameidata *nd)
586{
587 drop_links(nd);
588 if (!(nd->flags & LOOKUP_RCU)) {
589 int i;
590 path_put(&nd->path);
591 for (i = 0; i < nd->depth; i++)
592 path_put(&nd->stack[i].link);
Al Viro102b8af2015-05-12 17:35:52 -0400593 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
594 path_put(&nd->root);
595 nd->root.mnt = NULL;
596 }
Al Viro79733872015-05-09 12:55:43 -0400597 } else {
598 nd->flags &= ~LOOKUP_RCU;
599 if (!(nd->flags & LOOKUP_ROOT))
600 nd->root.mnt = NULL;
601 rcu_read_unlock();
602 }
603 nd->depth = 0;
604}
605
606/* path_put is needed afterwards regardless of success or failure */
607static bool legitimize_path(struct nameidata *nd,
608 struct path *path, unsigned seq)
609{
610 int res = __legitimize_mnt(path->mnt, nd->m_seq);
611 if (unlikely(res)) {
612 if (res > 0)
613 path->mnt = NULL;
614 path->dentry = NULL;
615 return false;
616 }
617 if (unlikely(!lockref_get_not_dead(&path->dentry->d_lockref))) {
618 path->dentry = NULL;
619 return false;
620 }
621 return !read_seqcount_retry(&path->dentry->d_seq, seq);
622}
623
624static bool legitimize_links(struct nameidata *nd)
625{
626 int i;
627 for (i = 0; i < nd->depth; i++) {
628 struct saved *last = nd->stack + i;
629 if (unlikely(!legitimize_path(nd, &last->link, last->seq))) {
630 drop_links(nd);
631 nd->depth = i + 1;
632 return false;
633 }
634 }
635 return true;
636}
637
Al Viro19660af2011-03-25 10:32:48 -0400638/*
Nick Piggin31e6b012011-01-07 17:49:52 +1100639 * Path walking has 2 modes, rcu-walk and ref-walk (see
Al Viro19660af2011-03-25 10:32:48 -0400640 * Documentation/filesystems/path-lookup.txt). In situations when we can't
641 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
642 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
643 * mode. Refcounts are grabbed at the last known good point before rcu-walk
644 * got stuck, so ref-walk may continue from there. If this is not successful
645 * (eg. a seqcount has changed), then failure is returned and it's up to caller
646 * to restart the path walk from the beginning in ref-walk mode.
Nick Piggin31e6b012011-01-07 17:49:52 +1100647 */
Nick Piggin31e6b012011-01-07 17:49:52 +1100648
649/**
Al Viro19660af2011-03-25 10:32:48 -0400650 * unlazy_walk - try to switch to ref-walk mode.
651 * @nd: nameidata pathwalk data
652 * @dentry: child of nd->path.dentry or NULL
Al Viro6e9918b2015-05-05 09:26:05 -0400653 * @seq: seq number to check dentry against
Randy Dunlap39191622011-01-08 19:36:21 -0800654 * Returns: 0 on success, -ECHILD on failure
Nick Piggin31e6b012011-01-07 17:49:52 +1100655 *
Al Viro19660af2011-03-25 10:32:48 -0400656 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
657 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
658 * @nd or NULL. Must be called from rcu-walk context.
Al Viro79733872015-05-09 12:55:43 -0400659 * Nothing should touch nameidata between unlazy_walk() failure and
660 * terminate_walk().
Nick Piggin31e6b012011-01-07 17:49:52 +1100661 */
Al Viro6e9918b2015-05-05 09:26:05 -0400662static int unlazy_walk(struct nameidata *nd, struct dentry *dentry, unsigned seq)
Nick Piggin31e6b012011-01-07 17:49:52 +1100663{
Nick Piggin31e6b012011-01-07 17:49:52 +1100664 struct dentry *parent = nd->path.dentry;
665
666 BUG_ON(!(nd->flags & LOOKUP_RCU));
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700667
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700668 nd->flags &= ~LOOKUP_RCU;
Al Viro79733872015-05-09 12:55:43 -0400669 if (unlikely(!legitimize_links(nd)))
670 goto out2;
671 if (unlikely(!legitimize_mnt(nd->path.mnt, nd->m_seq)))
672 goto out2;
673 if (unlikely(!lockref_get_not_dead(&parent->d_lockref)))
674 goto out1;
Al Viro48a066e2013-09-29 22:06:07 -0400675
Linus Torvalds15570082013-09-02 11:38:06 -0700676 /*
677 * For a negative lookup, the lookup sequence point is the parents
678 * sequence point, and it only needs to revalidate the parent dentry.
679 *
680 * For a positive lookup, we need to move both the parent and the
681 * dentry from the RCU domain to be properly refcounted. And the
682 * sequence number in the dentry validates *both* dentry counters,
683 * since we checked the sequence number of the parent after we got
684 * the child sequence number. So we know the parent must still
685 * be valid if the child sequence number is still valid.
686 */
Al Viro19660af2011-03-25 10:32:48 -0400687 if (!dentry) {
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700688 if (read_seqcount_retry(&parent->d_seq, nd->seq))
689 goto out;
Al Viro19660af2011-03-25 10:32:48 -0400690 BUG_ON(nd->inode != parent->d_inode);
691 } else {
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700692 if (!lockref_get_not_dead(&dentry->d_lockref))
693 goto out;
Al Viro6e9918b2015-05-05 09:26:05 -0400694 if (read_seqcount_retry(&dentry->d_seq, seq))
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700695 goto drop_dentry;
Al Viro19660af2011-03-25 10:32:48 -0400696 }
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700697
698 /*
699 * Sequence counts matched. Now make sure that the root is
700 * still valid and get it if required.
701 */
702 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
Al Viro5a8d87e2015-05-12 00:10:18 -0400703 if (unlikely(!legitimize_path(nd, &nd->root, nd->root_seq))) {
704 rcu_read_unlock();
705 dput(dentry);
706 return -ECHILD;
Al Viro79733872015-05-09 12:55:43 -0400707 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100708 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100709
Al Viro8b61e742013-11-08 12:45:01 -0500710 rcu_read_unlock();
Nick Piggin31e6b012011-01-07 17:49:52 +1100711 return 0;
Al Viro19660af2011-03-25 10:32:48 -0400712
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700713drop_dentry:
Al Viro8b61e742013-11-08 12:45:01 -0500714 rcu_read_unlock();
Linus Torvalds15570082013-09-02 11:38:06 -0700715 dput(dentry);
Linus Torvaldsd0d27272013-09-10 12:17:49 -0700716 goto drop_root_mnt;
Al Viro79733872015-05-09 12:55:43 -0400717out2:
718 nd->path.mnt = NULL;
719out1:
720 nd->path.dentry = NULL;
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700721out:
Al Viro8b61e742013-11-08 12:45:01 -0500722 rcu_read_unlock();
Linus Torvaldsd0d27272013-09-10 12:17:49 -0700723drop_root_mnt:
724 if (!(nd->flags & LOOKUP_ROOT))
725 nd->root.mnt = NULL;
Nick Piggin31e6b012011-01-07 17:49:52 +1100726 return -ECHILD;
727}
728
Al Viro79733872015-05-09 12:55:43 -0400729static int unlazy_link(struct nameidata *nd, struct path *link, unsigned seq)
730{
731 if (unlikely(!legitimize_path(nd, link, seq))) {
732 drop_links(nd);
733 nd->depth = 0;
734 nd->flags &= ~LOOKUP_RCU;
735 nd->path.mnt = NULL;
736 nd->path.dentry = NULL;
737 if (!(nd->flags & LOOKUP_ROOT))
738 nd->root.mnt = NULL;
739 rcu_read_unlock();
740 } else if (likely(unlazy_walk(nd, NULL, 0)) == 0) {
741 return 0;
742 }
743 path_put(link);
744 return -ECHILD;
745}
746
Al Viro4ce16ef32012-06-10 16:10:59 -0400747static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
Nick Piggin34286d62011-01-07 17:49:57 +1100748{
Al Viro4ce16ef32012-06-10 16:10:59 -0400749 return dentry->d_op->d_revalidate(dentry, flags);
Nick Piggin34286d62011-01-07 17:49:57 +1100750}
751
Al Viro9f1fafe2011-03-25 11:00:12 -0400752/**
753 * complete_walk - successful completion of path walk
754 * @nd: pointer nameidata
Jeff Layton39159de2009-12-07 12:01:50 -0500755 *
Al Viro9f1fafe2011-03-25 11:00:12 -0400756 * If we had been in RCU mode, drop out of it and legitimize nd->path.
757 * Revalidate the final result, unless we'd already done that during
758 * the path walk or the filesystem doesn't ask for it. Return 0 on
759 * success, -error on failure. In case of failure caller does not
760 * need to drop nd->path.
Jeff Layton39159de2009-12-07 12:01:50 -0500761 */
Al Viro9f1fafe2011-03-25 11:00:12 -0400762static int complete_walk(struct nameidata *nd)
Jeff Layton39159de2009-12-07 12:01:50 -0500763{
Al Viro16c2cd72011-02-22 15:50:10 -0500764 struct dentry *dentry = nd->path.dentry;
Jeff Layton39159de2009-12-07 12:01:50 -0500765 int status;
Jeff Layton39159de2009-12-07 12:01:50 -0500766
Al Viro9f1fafe2011-03-25 11:00:12 -0400767 if (nd->flags & LOOKUP_RCU) {
Al Viro9f1fafe2011-03-25 11:00:12 -0400768 if (!(nd->flags & LOOKUP_ROOT))
769 nd->root.mnt = NULL;
Al Viro6e9918b2015-05-05 09:26:05 -0400770 if (unlikely(unlazy_walk(nd, NULL, 0)))
Al Viro48a066e2013-09-29 22:06:07 -0400771 return -ECHILD;
Al Viro9f1fafe2011-03-25 11:00:12 -0400772 }
773
Al Viro16c2cd72011-02-22 15:50:10 -0500774 if (likely(!(nd->flags & LOOKUP_JUMPED)))
Jeff Layton39159de2009-12-07 12:01:50 -0500775 return 0;
776
Jeff Laytonecf3d1f2013-02-20 11:19:05 -0500777 if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
Al Viro16c2cd72011-02-22 15:50:10 -0500778 return 0;
779
Jeff Laytonecf3d1f2013-02-20 11:19:05 -0500780 status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
Jeff Layton39159de2009-12-07 12:01:50 -0500781 if (status > 0)
782 return 0;
783
Al Viro16c2cd72011-02-22 15:50:10 -0500784 if (!status)
Jeff Layton39159de2009-12-07 12:01:50 -0500785 status = -ESTALE;
Al Viro16c2cd72011-02-22 15:50:10 -0500786
Jeff Layton39159de2009-12-07 12:01:50 -0500787 return status;
788}
789
Al Viro18d8c862015-05-12 16:32:34 -0400790static void set_root(struct nameidata *nd)
Al Viro2a737872009-04-07 11:49:53 -0400791{
Al Viro7bd88372014-09-13 21:55:46 -0400792 get_fs_root(current->fs, &nd->root);
Al Viro2a737872009-04-07 11:49:53 -0400793}
794
Al Viro18d8c862015-05-12 16:32:34 -0400795static unsigned set_root_rcu(struct nameidata *nd)
Nick Piggin31e6b012011-01-07 17:49:52 +1100796{
Al Viro7bd88372014-09-13 21:55:46 -0400797 struct fs_struct *fs = current->fs;
Al Viro8f47a0162015-05-09 19:02:01 -0400798 unsigned seq;
Nick Pigginc28cc362011-01-07 17:49:53 +1100799
Al Viro7bd88372014-09-13 21:55:46 -0400800 do {
801 seq = read_seqcount_begin(&fs->seq);
802 nd->root = fs->root;
Al Viro8f47a0162015-05-09 19:02:01 -0400803 nd->root_seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
Al Viro7bd88372014-09-13 21:55:46 -0400804 } while (read_seqcount_retry(&fs->seq, seq));
Al Viro8f47a0162015-05-09 19:02:01 -0400805 return nd->root_seq;
Nick Piggin31e6b012011-01-07 17:49:52 +1100806}
807
Jan Blunck1d957f92008-02-14 19:34:35 -0800808static void path_put_conditional(struct path *path, struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700809{
810 dput(path->dentry);
Jan Blunck4ac91372008-02-14 19:34:32 -0800811 if (path->mnt != nd->path.mnt)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700812 mntput(path->mnt);
813}
814
Nick Piggin7b9337a2011-01-14 08:42:43 +0000815static inline void path_to_nameidata(const struct path *path,
816 struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700817{
Nick Piggin31e6b012011-01-07 17:49:52 +1100818 if (!(nd->flags & LOOKUP_RCU)) {
819 dput(nd->path.dentry);
820 if (nd->path.mnt != path->mnt)
821 mntput(nd->path.mnt);
Huang Shijie9a229682010-04-02 17:37:13 +0800822 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100823 nd->path.mnt = path->mnt;
Jan Blunck4ac91372008-02-14 19:34:32 -0800824 nd->path.dentry = path->dentry;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700825}
826
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400827/*
828 * Helper to directly jump to a known parsed path from ->follow_link,
829 * caller must have taken a reference to path beforehand.
830 */
Al Viro6e771372015-05-02 13:37:52 -0400831void nd_jump_link(struct path *path)
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400832{
Al Viro6e771372015-05-02 13:37:52 -0400833 struct nameidata *nd = current->nameidata;
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400834 path_put(&nd->path);
835
836 nd->path = *path;
837 nd->inode = nd->path.dentry->d_inode;
838 nd->flags |= LOOKUP_JUMPED;
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400839}
840
Al Virob9ff4422015-05-02 20:19:23 -0400841static inline void put_link(struct nameidata *nd)
Al Viro574197e2011-03-14 22:20:34 -0400842{
Al Viro21c30032015-05-03 21:06:24 -0400843 struct saved *last = nd->stack + --nd->depth;
Al Viro237d8b32015-05-07 09:21:14 -0400844 struct inode *inode = last->inode;
Al Virob9ff4422015-05-02 20:19:23 -0400845 if (last->cookie && inode->i_op->put_link)
Al Viro5f2c4172015-05-07 11:14:26 -0400846 inode->i_op->put_link(inode, last->cookie);
Al Viro6548fae2015-05-07 20:32:22 -0400847 if (!(nd->flags & LOOKUP_RCU))
848 path_put(&last->link);
Al Viro574197e2011-03-14 22:20:34 -0400849}
850
Linus Torvalds561ec642012-10-26 10:05:07 -0700851int sysctl_protected_symlinks __read_mostly = 0;
852int sysctl_protected_hardlinks __read_mostly = 0;
Kees Cook800179c2012-07-25 17:29:07 -0700853
854/**
855 * may_follow_link - Check symlink following for unsafe situations
Randy Dunlap55852632012-08-18 17:39:25 -0700856 * @nd: nameidata pathwalk data
Kees Cook800179c2012-07-25 17:29:07 -0700857 *
858 * In the case of the sysctl_protected_symlinks sysctl being enabled,
859 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
860 * in a sticky world-writable directory. This is to protect privileged
861 * processes from failing races against path names that may change out
862 * from under them by way of other users creating malicious symlinks.
863 * It will permit symlinks to be followed only when outside a sticky
864 * world-writable directory, or when the uid of the symlink and follower
865 * match, or when the directory owner matches the symlink's owner.
866 *
867 * Returns 0 if following the symlink is allowed, -ve on error.
868 */
Al Virofec2fa22015-05-06 15:58:18 -0400869static inline int may_follow_link(struct nameidata *nd)
Kees Cook800179c2012-07-25 17:29:07 -0700870{
871 const struct inode *inode;
872 const struct inode *parent;
873
874 if (!sysctl_protected_symlinks)
875 return 0;
876
877 /* Allowed if owner and follower match. */
Al Viro237d8b32015-05-07 09:21:14 -0400878 inode = nd->stack[0].inode;
Eric W. Biederman81abe272012-08-03 09:38:08 -0700879 if (uid_eq(current_cred()->fsuid, inode->i_uid))
Kees Cook800179c2012-07-25 17:29:07 -0700880 return 0;
881
882 /* Allowed if parent directory not sticky and world-writable. */
883 parent = nd->path.dentry->d_inode;
884 if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
885 return 0;
886
887 /* Allowed if parent directory and link owner match. */
Eric W. Biederman81abe272012-08-03 09:38:08 -0700888 if (uid_eq(parent->i_uid, inode->i_uid))
Kees Cook800179c2012-07-25 17:29:07 -0700889 return 0;
890
Al Viro319565022015-05-07 20:37:40 -0400891 if (nd->flags & LOOKUP_RCU)
892 return -ECHILD;
893
Al Viro1cf26652015-05-06 16:01:56 -0400894 audit_log_link_denied("follow_link", &nd->stack[0].link);
Kees Cook800179c2012-07-25 17:29:07 -0700895 return -EACCES;
896}
897
898/**
899 * safe_hardlink_source - Check for safe hardlink conditions
900 * @inode: the source inode to hardlink from
901 *
902 * Return false if at least one of the following conditions:
903 * - inode is not a regular file
904 * - inode is setuid
905 * - inode is setgid and group-exec
906 * - access failure for read and write
907 *
908 * Otherwise returns true.
909 */
910static bool safe_hardlink_source(struct inode *inode)
911{
912 umode_t mode = inode->i_mode;
913
914 /* Special files should not get pinned to the filesystem. */
915 if (!S_ISREG(mode))
916 return false;
917
918 /* Setuid files should not get pinned to the filesystem. */
919 if (mode & S_ISUID)
920 return false;
921
922 /* Executable setgid files should not get pinned to the filesystem. */
923 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
924 return false;
925
926 /* Hardlinking to unreadable or unwritable sources is dangerous. */
927 if (inode_permission(inode, MAY_READ | MAY_WRITE))
928 return false;
929
930 return true;
931}
932
933/**
934 * may_linkat - Check permissions for creating a hardlink
935 * @link: the source to hardlink from
936 *
937 * Block hardlink when all of:
938 * - sysctl_protected_hardlinks enabled
939 * - fsuid does not match inode
940 * - hardlink source is unsafe (see safe_hardlink_source() above)
941 * - not CAP_FOWNER
942 *
943 * Returns 0 if successful, -ve on error.
944 */
945static int may_linkat(struct path *link)
946{
947 const struct cred *cred;
948 struct inode *inode;
949
950 if (!sysctl_protected_hardlinks)
951 return 0;
952
953 cred = current_cred();
954 inode = link->dentry->d_inode;
955
956 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
957 * otherwise, it must be a safe source.
958 */
Eric W. Biederman81abe272012-08-03 09:38:08 -0700959 if (uid_eq(cred->fsuid, inode->i_uid) || safe_hardlink_source(inode) ||
Kees Cook800179c2012-07-25 17:29:07 -0700960 capable(CAP_FOWNER))
961 return 0;
962
Kees Cooka51d9ea2012-07-25 17:29:08 -0700963 audit_log_link_denied("linkat", link);
Kees Cook800179c2012-07-25 17:29:07 -0700964 return -EPERM;
965}
966
Al Viro3b2e7f72015-04-19 00:53:50 -0400967static __always_inline
968const char *get_link(struct nameidata *nd)
Ian Kent051d3812006-03-27 01:14:53 -0800969{
Al Viroab104922015-05-10 11:50:01 -0400970 struct saved *last = nd->stack + nd->depth - 1;
Al Viro1cf26652015-05-06 16:01:56 -0400971 struct dentry *dentry = last->link.dentry;
Al Viro237d8b32015-05-07 09:21:14 -0400972 struct inode *inode = last->inode;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400973 int error;
Al Viro0a959df2015-04-18 18:23:41 -0400974 const char *res;
Ian Kent051d3812006-03-27 01:14:53 -0800975
NeilBrown8fa9dd22015-03-23 13:37:40 +1100976 if (!(nd->flags & LOOKUP_RCU)) {
977 touch_atime(&last->link);
978 cond_resched();
979 } else if (atime_needs_update(&last->link, inode)) {
980 if (unlikely(unlazy_walk(nd, NULL, 0)))
981 return ERR_PTR(-ECHILD);
982 touch_atime(&last->link);
983 }
984
NeilBrownbda0be72015-03-23 13:37:39 +1100985 error = security_inode_follow_link(dentry, inode,
986 nd->flags & LOOKUP_RCU);
987 if (unlikely(error))
Al Viro6920a442015-05-10 10:43:46 -0400988 return ERR_PTR(error);
Al Viro36f3b4f2011-02-22 21:24:38 -0500989
Al Viro86acdca12009-12-22 23:45:11 -0500990 nd->last_type = LAST_BIND;
Al Virod4dee482015-04-30 20:08:02 -0400991 res = inode->i_link;
992 if (!res) {
Al Viro8c1b4562015-05-09 18:15:21 -0400993 if (nd->flags & LOOKUP_RCU) {
994 if (unlikely(unlazy_walk(nd, NULL, 0)))
995 return ERR_PTR(-ECHILD);
996 }
Al Viro6e771372015-05-02 13:37:52 -0400997 res = inode->i_op->follow_link(dentry, &last->cookie);
Al Virofab51e82015-05-10 11:01:00 -0400998 if (IS_ERR_OR_NULL(res)) {
Al Viro6920a442015-05-10 10:43:46 -0400999 last->cookie = NULL;
Al Virofab51e82015-05-10 11:01:00 -04001000 return res;
1001 }
Ian Kent051d3812006-03-27 01:14:53 -08001002 }
Al Virofab51e82015-05-10 11:01:00 -04001003 if (*res == '/') {
Al Viro8c1b4562015-05-09 18:15:21 -04001004 if (nd->flags & LOOKUP_RCU) {
Al Viro8f47a0162015-05-09 19:02:01 -04001005 struct dentry *d;
1006 if (!nd->root.mnt)
1007 set_root_rcu(nd);
1008 nd->path = nd->root;
1009 d = nd->path.dentry;
1010 nd->inode = d->d_inode;
1011 nd->seq = nd->root_seq;
1012 if (unlikely(read_seqcount_retry(&d->d_seq, nd->seq)))
Al Viro8c1b4562015-05-09 18:15:21 -04001013 return ERR_PTR(-ECHILD);
Al Viro8f47a0162015-05-09 19:02:01 -04001014 } else {
1015 if (!nd->root.mnt)
1016 set_root(nd);
1017 path_put(&nd->path);
1018 nd->path = nd->root;
1019 path_get(&nd->root);
1020 nd->inode = nd->path.dentry->d_inode;
Al Viro8c1b4562015-05-09 18:15:21 -04001021 }
Al Virofab51e82015-05-10 11:01:00 -04001022 nd->flags |= LOOKUP_JUMPED;
1023 while (unlikely(*++res == '/'))
1024 ;
1025 }
1026 if (!*res)
1027 res = NULL;
Al Viro0a959df2015-04-18 18:23:41 -04001028 return res;
1029}
Al Viro6d7b5aa2012-06-10 04:15:17 -04001030
David Howellsf015f1262012-06-25 12:55:28 +01001031/*
1032 * follow_up - Find the mountpoint of path's vfsmount
1033 *
1034 * Given a path, find the mountpoint of its source file system.
1035 * Replace @path with the path of the mountpoint in the parent mount.
1036 * Up is towards /.
1037 *
1038 * Return 1 if we went up a level and 0 if we were already at the
1039 * root.
1040 */
Al Virobab77eb2009-04-18 03:26:48 -04001041int follow_up(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
Al Viro0714a532011-11-24 22:19:58 -05001043 struct mount *mnt = real_mount(path->mnt);
1044 struct mount *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 struct dentry *mountpoint;
Nick Piggin99b7db72010-08-18 04:37:39 +10001046
Al Viro48a066e2013-09-29 22:06:07 -04001047 read_seqlock_excl(&mount_lock);
Al Viro0714a532011-11-24 22:19:58 -05001048 parent = mnt->mnt_parent;
Al Viro3c0a6162012-07-18 17:32:50 +04001049 if (parent == mnt) {
Al Viro48a066e2013-09-29 22:06:07 -04001050 read_sequnlock_excl(&mount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 return 0;
1052 }
Al Viro0714a532011-11-24 22:19:58 -05001053 mntget(&parent->mnt);
Al Viroa73324d2011-11-24 22:25:07 -05001054 mountpoint = dget(mnt->mnt_mountpoint);
Al Viro48a066e2013-09-29 22:06:07 -04001055 read_sequnlock_excl(&mount_lock);
Al Virobab77eb2009-04-18 03:26:48 -04001056 dput(path->dentry);
1057 path->dentry = mountpoint;
1058 mntput(path->mnt);
Al Viro0714a532011-11-24 22:19:58 -05001059 path->mnt = &parent->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 return 1;
1061}
Al Viro4d359502014-03-14 12:20:17 -04001062EXPORT_SYMBOL(follow_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Nick Pigginb5c84bf2011-01-07 17:49:38 +11001064/*
David Howells9875cf82011-01-14 18:45:21 +00001065 * Perform an automount
1066 * - return -EISDIR to tell follow_managed() to stop and return the path we
1067 * were called with.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 */
NeilBrown756daf22015-03-23 13:37:38 +11001069static int follow_automount(struct path *path, struct nameidata *nd,
David Howells9875cf82011-01-14 18:45:21 +00001070 bool *need_mntput)
Nick Piggin31e6b012011-01-07 17:49:52 +11001071{
David Howells9875cf82011-01-14 18:45:21 +00001072 struct vfsmount *mnt;
David Howellsea5b7782011-01-14 19:10:03 +00001073 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001074
David Howells9875cf82011-01-14 18:45:21 +00001075 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
1076 return -EREMOTE;
Al Viro463ffb22005-06-06 13:36:05 -07001077
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +02001078 /* We don't want to mount if someone's just doing a stat -
1079 * unless they're stat'ing a directory and appended a '/' to
1080 * the name.
1081 *
1082 * We do, however, want to mount if someone wants to open or
1083 * create a file of any type under the mountpoint, wants to
1084 * traverse through the mountpoint or wants to open the
1085 * mounted directory. Also, autofs may mark negative dentries
1086 * as being automount points. These will need the attentions
1087 * of the daemon to instantiate them before they can be used.
David Howells9875cf82011-01-14 18:45:21 +00001088 */
NeilBrown756daf22015-03-23 13:37:38 +11001089 if (!(nd->flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
1090 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +02001091 path->dentry->d_inode)
1092 return -EISDIR;
1093
NeilBrown756daf22015-03-23 13:37:38 +11001094 nd->total_link_count++;
1095 if (nd->total_link_count >= 40)
David Howells9875cf82011-01-14 18:45:21 +00001096 return -ELOOP;
1097
1098 mnt = path->dentry->d_op->d_automount(path);
1099 if (IS_ERR(mnt)) {
1100 /*
1101 * The filesystem is allowed to return -EISDIR here to indicate
1102 * it doesn't want to automount. For instance, autofs would do
1103 * this so that its userspace daemon can mount on this dentry.
1104 *
1105 * However, we can only permit this if it's a terminal point in
1106 * the path being looked up; if it wasn't then the remainder of
1107 * the path is inaccessible and we should say so.
1108 */
NeilBrown756daf22015-03-23 13:37:38 +11001109 if (PTR_ERR(mnt) == -EISDIR && (nd->flags & LOOKUP_PARENT))
David Howells9875cf82011-01-14 18:45:21 +00001110 return -EREMOTE;
1111 return PTR_ERR(mnt);
1112 }
David Howellsea5b7782011-01-14 19:10:03 +00001113
David Howells9875cf82011-01-14 18:45:21 +00001114 if (!mnt) /* mount collision */
1115 return 0;
1116
Al Viro8aef1882011-06-16 15:10:06 +01001117 if (!*need_mntput) {
1118 /* lock_mount() may release path->mnt on error */
1119 mntget(path->mnt);
1120 *need_mntput = true;
1121 }
Al Viro19a167a2011-01-17 01:35:23 -05001122 err = finish_automount(mnt, path);
David Howellsea5b7782011-01-14 19:10:03 +00001123
David Howellsea5b7782011-01-14 19:10:03 +00001124 switch (err) {
1125 case -EBUSY:
1126 /* Someone else made a mount here whilst we were busy */
Al Viro19a167a2011-01-17 01:35:23 -05001127 return 0;
David Howellsea5b7782011-01-14 19:10:03 +00001128 case 0:
Al Viro8aef1882011-06-16 15:10:06 +01001129 path_put(path);
David Howellsea5b7782011-01-14 19:10:03 +00001130 path->mnt = mnt;
1131 path->dentry = dget(mnt->mnt_root);
David Howellsea5b7782011-01-14 19:10:03 +00001132 return 0;
Al Viro19a167a2011-01-17 01:35:23 -05001133 default:
1134 return err;
David Howellsea5b7782011-01-14 19:10:03 +00001135 }
Al Viro19a167a2011-01-17 01:35:23 -05001136
David Howells9875cf82011-01-14 18:45:21 +00001137}
1138
1139/*
1140 * Handle a dentry that is managed in some way.
David Howellscc53ce52011-01-14 18:45:26 +00001141 * - Flagged for transit management (autofs)
David Howells9875cf82011-01-14 18:45:21 +00001142 * - Flagged as mountpoint
1143 * - Flagged as automount point
1144 *
1145 * This may only be called in refwalk mode.
1146 *
1147 * Serialization is taken care of in namespace.c
1148 */
NeilBrown756daf22015-03-23 13:37:38 +11001149static int follow_managed(struct path *path, struct nameidata *nd)
David Howells9875cf82011-01-14 18:45:21 +00001150{
Al Viro8aef1882011-06-16 15:10:06 +01001151 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
David Howells9875cf82011-01-14 18:45:21 +00001152 unsigned managed;
1153 bool need_mntput = false;
Al Viro8aef1882011-06-16 15:10:06 +01001154 int ret = 0;
David Howells9875cf82011-01-14 18:45:21 +00001155
1156 /* Given that we're not holding a lock here, we retain the value in a
1157 * local variable for each dentry as we look at it so that we don't see
1158 * the components of that value change under us */
1159 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1160 managed &= DCACHE_MANAGED_DENTRY,
1161 unlikely(managed != 0)) {
David Howellscc53ce52011-01-14 18:45:26 +00001162 /* Allow the filesystem to manage the transit without i_mutex
1163 * being held. */
1164 if (managed & DCACHE_MANAGE_TRANSIT) {
1165 BUG_ON(!path->dentry->d_op);
1166 BUG_ON(!path->dentry->d_op->d_manage);
Al Viro1aed3e42011-03-18 09:09:02 -04001167 ret = path->dentry->d_op->d_manage(path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +00001168 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001169 break;
David Howellscc53ce52011-01-14 18:45:26 +00001170 }
1171
David Howells9875cf82011-01-14 18:45:21 +00001172 /* Transit to a mounted filesystem. */
1173 if (managed & DCACHE_MOUNTED) {
1174 struct vfsmount *mounted = lookup_mnt(path);
1175 if (mounted) {
1176 dput(path->dentry);
1177 if (need_mntput)
1178 mntput(path->mnt);
1179 path->mnt = mounted;
1180 path->dentry = dget(mounted->mnt_root);
1181 need_mntput = true;
1182 continue;
1183 }
1184
1185 /* Something is mounted on this dentry in another
1186 * namespace and/or whatever was mounted there in this
Al Viro48a066e2013-09-29 22:06:07 -04001187 * namespace got unmounted before lookup_mnt() could
1188 * get it */
David Howells9875cf82011-01-14 18:45:21 +00001189 }
1190
1191 /* Handle an automount point */
1192 if (managed & DCACHE_NEED_AUTOMOUNT) {
NeilBrown756daf22015-03-23 13:37:38 +11001193 ret = follow_automount(path, nd, &need_mntput);
David Howells9875cf82011-01-14 18:45:21 +00001194 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001195 break;
David Howells9875cf82011-01-14 18:45:21 +00001196 continue;
1197 }
1198
1199 /* We didn't change the current path point */
1200 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 }
Al Viro8aef1882011-06-16 15:10:06 +01001202
1203 if (need_mntput && path->mnt == mnt)
1204 mntput(path->mnt);
1205 if (ret == -EISDIR)
1206 ret = 0;
Al Viro84027522015-04-22 10:30:08 -04001207 if (need_mntput)
1208 nd->flags |= LOOKUP_JUMPED;
1209 if (unlikely(ret < 0))
1210 path_put_conditional(path, nd);
1211 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212}
1213
David Howellscc53ce52011-01-14 18:45:26 +00001214int follow_down_one(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
1216 struct vfsmount *mounted;
1217
Al Viro1c755af2009-04-18 14:06:57 -04001218 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -04001220 dput(path->dentry);
1221 mntput(path->mnt);
1222 path->mnt = mounted;
1223 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 return 1;
1225 }
1226 return 0;
1227}
Al Viro4d359502014-03-14 12:20:17 -04001228EXPORT_SYMBOL(follow_down_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
NeilBrownb8faf032014-08-04 17:06:29 +10001230static inline int managed_dentry_rcu(struct dentry *dentry)
Ian Kent62a73752011-03-25 01:51:02 +08001231{
NeilBrownb8faf032014-08-04 17:06:29 +10001232 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT) ?
1233 dentry->d_op->d_manage(dentry, true) : 0;
Ian Kent62a73752011-03-25 01:51:02 +08001234}
1235
David Howells9875cf82011-01-14 18:45:21 +00001236/*
Al Viro287548e2011-05-27 06:50:06 -04001237 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1238 * we meet a managed dentry that would need blocking.
David Howells9875cf82011-01-14 18:45:21 +00001239 */
1240static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
Al Viro254cf582015-05-05 09:40:46 -04001241 struct inode **inode, unsigned *seqp)
David Howells9875cf82011-01-14 18:45:21 +00001242{
Ian Kent62a73752011-03-25 01:51:02 +08001243 for (;;) {
Al Viroc7105362011-11-24 18:22:03 -05001244 struct mount *mounted;
Ian Kent62a73752011-03-25 01:51:02 +08001245 /*
1246 * Don't forget we might have a non-mountpoint managed dentry
1247 * that wants to block transit.
1248 */
NeilBrownb8faf032014-08-04 17:06:29 +10001249 switch (managed_dentry_rcu(path->dentry)) {
1250 case -ECHILD:
1251 default:
David Howellsab909112011-01-14 18:46:51 +00001252 return false;
NeilBrownb8faf032014-08-04 17:06:29 +10001253 case -EISDIR:
1254 return true;
1255 case 0:
1256 break;
1257 }
Ian Kent62a73752011-03-25 01:51:02 +08001258
1259 if (!d_mountpoint(path->dentry))
NeilBrownb8faf032014-08-04 17:06:29 +10001260 return !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
Ian Kent62a73752011-03-25 01:51:02 +08001261
Al Viro474279d2013-10-01 16:11:26 -04001262 mounted = __lookup_mnt(path->mnt, path->dentry);
David Howells9875cf82011-01-14 18:45:21 +00001263 if (!mounted)
1264 break;
Al Viroc7105362011-11-24 18:22:03 -05001265 path->mnt = &mounted->mnt;
1266 path->dentry = mounted->mnt.mnt_root;
Al Viroa3fbbde2011-11-07 21:21:26 +00001267 nd->flags |= LOOKUP_JUMPED;
Al Viro254cf582015-05-05 09:40:46 -04001268 *seqp = read_seqcount_begin(&path->dentry->d_seq);
Linus Torvalds59430262011-07-18 15:43:29 -07001269 /*
1270 * Update the inode too. We don't need to re-check the
1271 * dentry sequence number here after this d_inode read,
1272 * because a mount-point is always pinned.
1273 */
1274 *inode = path->dentry->d_inode;
David Howells9875cf82011-01-14 18:45:21 +00001275 }
Al Virof5be3e29122014-09-13 21:50:45 -04001276 return !read_seqretry(&mount_lock, nd->m_seq) &&
NeilBrownb8faf032014-08-04 17:06:29 +10001277 !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
Al Viro287548e2011-05-27 06:50:06 -04001278}
1279
Nick Piggin31e6b012011-01-07 17:49:52 +11001280static int follow_dotdot_rcu(struct nameidata *nd)
1281{
Al Viro4023bfc2014-09-13 21:59:43 -04001282 struct inode *inode = nd->inode;
Al Viro7bd88372014-09-13 21:55:46 -04001283 if (!nd->root.mnt)
1284 set_root_rcu(nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11001285
David Howells9875cf82011-01-14 18:45:21 +00001286 while (1) {
Al Viroaed434a2015-05-12 12:22:47 -04001287 if (path_equal(&nd->path, &nd->root))
Nick Piggin31e6b012011-01-07 17:49:52 +11001288 break;
Nick Piggin31e6b012011-01-07 17:49:52 +11001289 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1290 struct dentry *old = nd->path.dentry;
1291 struct dentry *parent = old->d_parent;
1292 unsigned seq;
1293
Al Viro4023bfc2014-09-13 21:59:43 -04001294 inode = parent->d_inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001295 seq = read_seqcount_begin(&parent->d_seq);
Al Viroaed434a2015-05-12 12:22:47 -04001296 if (unlikely(read_seqcount_retry(&old->d_seq, nd->seq)))
1297 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001298 nd->path.dentry = parent;
1299 nd->seq = seq;
1300 break;
Al Viroaed434a2015-05-12 12:22:47 -04001301 } else {
1302 struct mount *mnt = real_mount(nd->path.mnt);
1303 struct mount *mparent = mnt->mnt_parent;
1304 struct dentry *mountpoint = mnt->mnt_mountpoint;
1305 struct inode *inode2 = mountpoint->d_inode;
1306 unsigned seq = read_seqcount_begin(&mountpoint->d_seq);
1307 if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1308 return -ECHILD;
1309 if (&mparent->mnt == nd->path.mnt)
1310 break;
1311 /* we know that mountpoint was pinned */
1312 nd->path.dentry = mountpoint;
1313 nd->path.mnt = &mparent->mnt;
1314 inode = inode2;
1315 nd->seq = seq;
Nick Piggin31e6b012011-01-07 17:49:52 +11001316 }
Nick Piggin31e6b012011-01-07 17:49:52 +11001317 }
Al Viroaed434a2015-05-12 12:22:47 -04001318 while (unlikely(d_mountpoint(nd->path.dentry))) {
Al Virob37199e2014-03-20 15:18:22 -04001319 struct mount *mounted;
1320 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry);
Al Viroaed434a2015-05-12 12:22:47 -04001321 if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1322 return -ECHILD;
Al Virob37199e2014-03-20 15:18:22 -04001323 if (!mounted)
1324 break;
1325 nd->path.mnt = &mounted->mnt;
1326 nd->path.dentry = mounted->mnt.mnt_root;
Al Viro4023bfc2014-09-13 21:59:43 -04001327 inode = nd->path.dentry->d_inode;
Al Virob37199e2014-03-20 15:18:22 -04001328 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Virob37199e2014-03-20 15:18:22 -04001329 }
Al Viro4023bfc2014-09-13 21:59:43 -04001330 nd->inode = inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001331 return 0;
1332}
1333
David Howells9875cf82011-01-14 18:45:21 +00001334/*
David Howellscc53ce52011-01-14 18:45:26 +00001335 * Follow down to the covering mount currently visible to userspace. At each
1336 * point, the filesystem owning that dentry may be queried as to whether the
1337 * caller is permitted to proceed or not.
David Howellscc53ce52011-01-14 18:45:26 +00001338 */
Al Viro7cc90cc2011-03-18 09:04:20 -04001339int follow_down(struct path *path)
David Howellscc53ce52011-01-14 18:45:26 +00001340{
1341 unsigned managed;
1342 int ret;
1343
1344 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1345 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1346 /* Allow the filesystem to manage the transit without i_mutex
1347 * being held.
1348 *
1349 * We indicate to the filesystem if someone is trying to mount
1350 * something here. This gives autofs the chance to deny anyone
1351 * other than its daemon the right to mount on its
1352 * superstructure.
1353 *
1354 * The filesystem may sleep at this point.
1355 */
1356 if (managed & DCACHE_MANAGE_TRANSIT) {
1357 BUG_ON(!path->dentry->d_op);
1358 BUG_ON(!path->dentry->d_op->d_manage);
David Howellsab909112011-01-14 18:46:51 +00001359 ret = path->dentry->d_op->d_manage(
Al Viro1aed3e42011-03-18 09:09:02 -04001360 path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +00001361 if (ret < 0)
1362 return ret == -EISDIR ? 0 : ret;
1363 }
1364
1365 /* Transit to a mounted filesystem. */
1366 if (managed & DCACHE_MOUNTED) {
1367 struct vfsmount *mounted = lookup_mnt(path);
1368 if (!mounted)
1369 break;
1370 dput(path->dentry);
1371 mntput(path->mnt);
1372 path->mnt = mounted;
1373 path->dentry = dget(mounted->mnt_root);
1374 continue;
1375 }
1376
1377 /* Don't handle automount points here */
1378 break;
1379 }
1380 return 0;
1381}
Al Viro4d359502014-03-14 12:20:17 -04001382EXPORT_SYMBOL(follow_down);
David Howellscc53ce52011-01-14 18:45:26 +00001383
1384/*
David Howells9875cf82011-01-14 18:45:21 +00001385 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1386 */
1387static void follow_mount(struct path *path)
1388{
1389 while (d_mountpoint(path->dentry)) {
1390 struct vfsmount *mounted = lookup_mnt(path);
1391 if (!mounted)
1392 break;
1393 dput(path->dentry);
1394 mntput(path->mnt);
1395 path->mnt = mounted;
1396 path->dentry = dget(mounted->mnt_root);
1397 }
1398}
1399
Nick Piggin31e6b012011-01-07 17:49:52 +11001400static void follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
Al Viro7bd88372014-09-13 21:55:46 -04001402 if (!nd->root.mnt)
1403 set_root(nd);
Andreas Mohre518ddb2006-09-29 02:01:22 -07001404
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 while(1) {
Jan Blunck4ac91372008-02-14 19:34:32 -08001406 struct dentry *old = nd->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Al Viro2a737872009-04-07 11:49:53 -04001408 if (nd->path.dentry == nd->root.dentry &&
1409 nd->path.mnt == nd->root.mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 break;
1411 }
Jan Blunck4ac91372008-02-14 19:34:32 -08001412 if (nd->path.dentry != nd->path.mnt->mnt_root) {
Al Viro3088dd72010-01-30 15:47:29 -05001413 /* rare case of legitimate dget_parent()... */
1414 nd->path.dentry = dget_parent(nd->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 dput(old);
1416 break;
1417 }
Al Viro3088dd72010-01-30 15:47:29 -05001418 if (!follow_up(&nd->path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 }
Al Viro79ed0222009-04-18 13:59:41 -04001421 follow_mount(&nd->path);
Nick Piggin31e6b012011-01-07 17:49:52 +11001422 nd->inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423}
1424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425/*
Miklos Szeredibad61182012-03-26 12:54:24 +02001426 * This looks up the name in dcache, possibly revalidates the old dentry and
1427 * allocates a new one if not found or not valid. In the need_lookup argument
1428 * returns whether i_op->lookup is necessary.
1429 *
1430 * dir->d_inode->i_mutex must be held
Nick Pigginbaa03892010-08-18 04:37:31 +10001431 */
Miklos Szeredibad61182012-03-26 12:54:24 +02001432static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
Al Viro201f9562012-06-22 12:42:10 +04001433 unsigned int flags, bool *need_lookup)
Nick Pigginbaa03892010-08-18 04:37:31 +10001434{
Nick Pigginbaa03892010-08-18 04:37:31 +10001435 struct dentry *dentry;
Miklos Szeredibad61182012-03-26 12:54:24 +02001436 int error;
Nick Pigginbaa03892010-08-18 04:37:31 +10001437
Miklos Szeredibad61182012-03-26 12:54:24 +02001438 *need_lookup = false;
1439 dentry = d_lookup(dir, name);
1440 if (dentry) {
Jeff Layton39e3c952012-11-28 11:30:53 -05001441 if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
Al Viro201f9562012-06-22 12:42:10 +04001442 error = d_revalidate(dentry, flags);
Miklos Szeredibad61182012-03-26 12:54:24 +02001443 if (unlikely(error <= 0)) {
1444 if (error < 0) {
1445 dput(dentry);
1446 return ERR_PTR(error);
Eric W. Biederman5542aa22014-02-13 09:46:25 -08001447 } else {
1448 d_invalidate(dentry);
Miklos Szeredibad61182012-03-26 12:54:24 +02001449 dput(dentry);
1450 dentry = NULL;
1451 }
1452 }
1453 }
1454 }
Nick Pigginbaa03892010-08-18 04:37:31 +10001455
Miklos Szeredibad61182012-03-26 12:54:24 +02001456 if (!dentry) {
1457 dentry = d_alloc(dir, name);
1458 if (unlikely(!dentry))
1459 return ERR_PTR(-ENOMEM);
Nick Pigginbaa03892010-08-18 04:37:31 +10001460
Miklos Szeredibad61182012-03-26 12:54:24 +02001461 *need_lookup = true;
Nick Pigginbaa03892010-08-18 04:37:31 +10001462 }
1463 return dentry;
1464}
1465
1466/*
J. Bruce Fields13a2c3b2013-10-23 16:09:16 -04001467 * Call i_op->lookup on the dentry. The dentry must be negative and
1468 * unhashed.
Miklos Szeredibad61182012-03-26 12:54:24 +02001469 *
1470 * dir->d_inode->i_mutex must be held
Josef Bacik44396f42011-05-31 11:58:49 -04001471 */
Miklos Szeredibad61182012-03-26 12:54:24 +02001472static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
Al Viro72bd8662012-06-10 17:17:17 -04001473 unsigned int flags)
Josef Bacik44396f42011-05-31 11:58:49 -04001474{
Josef Bacik44396f42011-05-31 11:58:49 -04001475 struct dentry *old;
1476
1477 /* Don't create child dentry for a dead directory. */
Miklos Szeredibad61182012-03-26 12:54:24 +02001478 if (unlikely(IS_DEADDIR(dir))) {
Miklos Szeredie188dc02012-02-03 14:25:18 +01001479 dput(dentry);
Josef Bacik44396f42011-05-31 11:58:49 -04001480 return ERR_PTR(-ENOENT);
Miklos Szeredie188dc02012-02-03 14:25:18 +01001481 }
Josef Bacik44396f42011-05-31 11:58:49 -04001482
Al Viro72bd8662012-06-10 17:17:17 -04001483 old = dir->i_op->lookup(dir, dentry, flags);
Josef Bacik44396f42011-05-31 11:58:49 -04001484 if (unlikely(old)) {
1485 dput(dentry);
1486 dentry = old;
1487 }
1488 return dentry;
1489}
1490
Al Viroa3255542012-03-30 14:41:51 -04001491static struct dentry *__lookup_hash(struct qstr *name,
Al Viro72bd8662012-06-10 17:17:17 -04001492 struct dentry *base, unsigned int flags)
Al Viroa3255542012-03-30 14:41:51 -04001493{
Miklos Szeredibad61182012-03-26 12:54:24 +02001494 bool need_lookup;
Al Viroa3255542012-03-30 14:41:51 -04001495 struct dentry *dentry;
1496
Al Viro72bd8662012-06-10 17:17:17 -04001497 dentry = lookup_dcache(name, base, flags, &need_lookup);
Miklos Szeredibad61182012-03-26 12:54:24 +02001498 if (!need_lookup)
1499 return dentry;
Al Viroa3255542012-03-30 14:41:51 -04001500
Al Viro72bd8662012-06-10 17:17:17 -04001501 return lookup_real(base->d_inode, dentry, flags);
Al Viroa3255542012-03-30 14:41:51 -04001502}
1503
Josef Bacik44396f42011-05-31 11:58:49 -04001504/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 * It's more convoluted than I'd like it to be, but... it's still fairly
1506 * small and for now I'd prefer to have fast path as straight as possible.
1507 * It _is_ time-critical.
1508 */
Al Viroe97cdc82013-01-24 18:16:00 -05001509static int lookup_fast(struct nameidata *nd,
Al Viro254cf582015-05-05 09:40:46 -04001510 struct path *path, struct inode **inode,
1511 unsigned *seqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512{
Jan Blunck4ac91372008-02-14 19:34:32 -08001513 struct vfsmount *mnt = nd->path.mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +11001514 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro5a18fff2011-03-11 04:44:53 -05001515 int need_reval = 1;
1516 int status = 1;
David Howells9875cf82011-01-14 18:45:21 +00001517 int err;
1518
Al Viro3cac2602009-08-13 18:27:43 +04001519 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001520 * Rename seqlock is not required here because in the off chance
1521 * of a false negative due to a concurrent rename, we're going to
1522 * do the non-racy lookup, below.
1523 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001524 if (nd->flags & LOOKUP_RCU) {
1525 unsigned seq;
Al Viro766c4cb2015-05-07 19:24:57 -04001526 bool negative;
Linus Torvaldsda53be12013-05-21 15:22:44 -07001527 dentry = __d_lookup_rcu(parent, &nd->last, &seq);
Al Viro5a18fff2011-03-11 04:44:53 -05001528 if (!dentry)
1529 goto unlazy;
1530
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001531 /*
1532 * This sequence count validates that the inode matches
1533 * the dentry name information from lookup.
1534 */
David Howells63afdfc2015-05-06 15:59:00 +01001535 *inode = d_backing_inode(dentry);
Al Viro766c4cb2015-05-07 19:24:57 -04001536 negative = d_is_negative(dentry);
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001537 if (read_seqcount_retry(&dentry->d_seq, seq))
1538 return -ECHILD;
Al Viro766c4cb2015-05-07 19:24:57 -04001539 if (negative)
1540 return -ENOENT;
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001541
1542 /*
1543 * This sequence count validates that the parent had no
1544 * changes while we did the lookup of the dentry above.
1545 *
1546 * The memory barrier in read_seqcount_begin of child is
1547 * enough, we can use __read_seqcount_retry here.
1548 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001549 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1550 return -ECHILD;
Al Viro5a18fff2011-03-11 04:44:53 -05001551
Al Viro254cf582015-05-05 09:40:46 -04001552 *seqp = seq;
Al Viro24643082011-02-15 01:26:22 -05001553 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
Al Viro4ce16ef32012-06-10 16:10:59 -04001554 status = d_revalidate(dentry, nd->flags);
Al Viro5a18fff2011-03-11 04:44:53 -05001555 if (unlikely(status <= 0)) {
1556 if (status != -ECHILD)
1557 need_reval = 0;
1558 goto unlazy;
1559 }
Al Viro24643082011-02-15 01:26:22 -05001560 }
Nick Piggin31e6b012011-01-07 17:49:52 +11001561 path->mnt = mnt;
1562 path->dentry = dentry;
Al Viro254cf582015-05-05 09:40:46 -04001563 if (likely(__follow_mount_rcu(nd, path, inode, seqp)))
NeilBrownb8faf032014-08-04 17:06:29 +10001564 return 0;
Al Viro5a18fff2011-03-11 04:44:53 -05001565unlazy:
Al Viro254cf582015-05-05 09:40:46 -04001566 if (unlazy_walk(nd, dentry, seq))
Al Viro19660af2011-03-25 10:32:48 -04001567 return -ECHILD;
Al Viro5a18fff2011-03-11 04:44:53 -05001568 } else {
Al Viroe97cdc82013-01-24 18:16:00 -05001569 dentry = __d_lookup(parent, &nd->last);
Nick Piggin31e6b012011-01-07 17:49:52 +11001570 }
Al Viro5a18fff2011-03-11 04:44:53 -05001571
Al Viro81e6f522012-03-30 14:48:04 -04001572 if (unlikely(!dentry))
1573 goto need_lookup;
Al Viro5a18fff2011-03-11 04:44:53 -05001574
Al Viro5a18fff2011-03-11 04:44:53 -05001575 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
Al Viro4ce16ef32012-06-10 16:10:59 -04001576 status = d_revalidate(dentry, nd->flags);
Al Viro5a18fff2011-03-11 04:44:53 -05001577 if (unlikely(status <= 0)) {
1578 if (status < 0) {
1579 dput(dentry);
1580 return status;
1581 }
Eric W. Biederman5542aa22014-02-13 09:46:25 -08001582 d_invalidate(dentry);
1583 dput(dentry);
1584 goto need_lookup;
Al Viro5a18fff2011-03-11 04:44:53 -05001585 }
Miklos Szeredi697f5142012-05-21 17:30:05 +02001586
Al Viro766c4cb2015-05-07 19:24:57 -04001587 if (unlikely(d_is_negative(dentry))) {
1588 dput(dentry);
1589 return -ENOENT;
1590 }
David Howells9875cf82011-01-14 18:45:21 +00001591 path->mnt = mnt;
1592 path->dentry = dentry;
NeilBrown756daf22015-03-23 13:37:38 +11001593 err = follow_managed(path, nd);
Al Viro84027522015-04-22 10:30:08 -04001594 if (likely(!err))
David Howells63afdfc2015-05-06 15:59:00 +01001595 *inode = d_backing_inode(path->dentry);
Al Viro84027522015-04-22 10:30:08 -04001596 return err;
Al Viro81e6f522012-03-30 14:48:04 -04001597
1598need_lookup:
Miklos Szeredi697f5142012-05-21 17:30:05 +02001599 return 1;
1600}
1601
1602/* Fast lookup failed, do it the slow way */
Al Virocc2a5272013-01-24 18:19:49 -05001603static int lookup_slow(struct nameidata *nd, struct path *path)
Miklos Szeredi697f5142012-05-21 17:30:05 +02001604{
1605 struct dentry *dentry, *parent;
Miklos Szeredi697f5142012-05-21 17:30:05 +02001606
1607 parent = nd->path.dentry;
Al Viro81e6f522012-03-30 14:48:04 -04001608 BUG_ON(nd->inode != parent->d_inode);
1609
1610 mutex_lock(&parent->d_inode->i_mutex);
Al Virocc2a5272013-01-24 18:19:49 -05001611 dentry = __lookup_hash(&nd->last, parent, nd->flags);
Al Viro81e6f522012-03-30 14:48:04 -04001612 mutex_unlock(&parent->d_inode->i_mutex);
1613 if (IS_ERR(dentry))
1614 return PTR_ERR(dentry);
Miklos Szeredi697f5142012-05-21 17:30:05 +02001615 path->mnt = nd->path.mnt;
1616 path->dentry = dentry;
Al Viro84027522015-04-22 10:30:08 -04001617 return follow_managed(path, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618}
1619
Al Viro52094c82011-02-21 21:34:47 -05001620static inline int may_lookup(struct nameidata *nd)
1621{
1622 if (nd->flags & LOOKUP_RCU) {
Al Viro4ad5abb2011-06-20 19:57:03 -04001623 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
Al Viro52094c82011-02-21 21:34:47 -05001624 if (err != -ECHILD)
1625 return err;
Al Viro6e9918b2015-05-05 09:26:05 -04001626 if (unlazy_walk(nd, NULL, 0))
Al Viro52094c82011-02-21 21:34:47 -05001627 return -ECHILD;
1628 }
Al Viro4ad5abb2011-06-20 19:57:03 -04001629 return inode_permission(nd->inode, MAY_EXEC);
Al Viro52094c82011-02-21 21:34:47 -05001630}
1631
Al Viro9856fa12011-03-04 14:22:06 -05001632static inline int handle_dots(struct nameidata *nd, int type)
1633{
1634 if (type == LAST_DOTDOT) {
1635 if (nd->flags & LOOKUP_RCU) {
Al Viro70291ae2015-05-04 07:53:00 -04001636 return follow_dotdot_rcu(nd);
Al Viro9856fa12011-03-04 14:22:06 -05001637 } else
1638 follow_dotdot(nd);
1639 }
1640 return 0;
1641}
1642
Al Viro181548c2015-05-07 19:54:34 -04001643static int pick_link(struct nameidata *nd, struct path *link,
1644 struct inode *inode, unsigned seq)
Al Virod63ff282015-05-04 18:13:23 -04001645{
Al Viro626de992015-05-04 18:26:59 -04001646 int error;
Al Viro1cf26652015-05-06 16:01:56 -04001647 struct saved *last;
NeilBrown756daf22015-03-23 13:37:38 +11001648 if (unlikely(nd->total_link_count++ >= MAXSYMLINKS)) {
Al Viro626de992015-05-04 18:26:59 -04001649 path_to_nameidata(link, nd);
1650 return -ELOOP;
1651 }
Al Virobc40aee2015-05-09 13:04:24 -04001652 if (!(nd->flags & LOOKUP_RCU)) {
Al Viro79733872015-05-09 12:55:43 -04001653 if (link->mnt == nd->path.mnt)
1654 mntget(link->mnt);
Al Virod63ff282015-05-04 18:13:23 -04001655 }
Al Viro626de992015-05-04 18:26:59 -04001656 error = nd_alloc_stack(nd);
1657 if (unlikely(error)) {
Al Virobc40aee2015-05-09 13:04:24 -04001658 if (error == -ECHILD) {
1659 if (unlikely(unlazy_link(nd, link, seq)))
1660 return -ECHILD;
1661 error = nd_alloc_stack(nd);
1662 }
1663 if (error) {
1664 path_put(link);
1665 return error;
1666 }
Al Viro626de992015-05-04 18:26:59 -04001667 }
1668
Al Viroab104922015-05-10 11:50:01 -04001669 last = nd->stack + nd->depth++;
Al Viro1cf26652015-05-06 16:01:56 -04001670 last->link = *link;
Al Viroab104922015-05-10 11:50:01 -04001671 last->cookie = NULL;
Al Viro181548c2015-05-07 19:54:34 -04001672 last->inode = inode;
Al Viro0450b2d2015-05-08 13:23:53 -04001673 last->seq = seq;
Al Virod63ff282015-05-04 18:13:23 -04001674 return 1;
1675}
1676
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001677/*
1678 * Do we need to follow links? We _really_ want to be able
1679 * to do this check without having to look at inode->i_op,
1680 * so we keep a cache of "no, this doesn't need follow_link"
1681 * for the common case.
1682 */
Al Viro254cf582015-05-05 09:40:46 -04001683static inline int should_follow_link(struct nameidata *nd, struct path *link,
Al Viro181548c2015-05-07 19:54:34 -04001684 int follow,
1685 struct inode *inode, unsigned seq)
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001686{
Al Virod63ff282015-05-04 18:13:23 -04001687 if (likely(!d_is_symlink(link->dentry)))
1688 return 0;
1689 if (!follow)
1690 return 0;
Al Viro181548c2015-05-07 19:54:34 -04001691 return pick_link(nd, link, inode, seq);
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001692}
1693
Al Viro4693a542015-05-04 17:47:11 -04001694enum {WALK_GET = 1, WALK_PUT = 2};
1695
1696static int walk_component(struct nameidata *nd, int flags)
Al Viroce57dfc2011-03-13 19:58:58 -04001697{
Al Virocaa85632015-04-22 17:52:47 -04001698 struct path path;
Al Viroce57dfc2011-03-13 19:58:58 -04001699 struct inode *inode;
Al Viro254cf582015-05-05 09:40:46 -04001700 unsigned seq;
Al Viroce57dfc2011-03-13 19:58:58 -04001701 int err;
1702 /*
1703 * "." and ".." are special - ".." especially so because it has
1704 * to be able to know about the current root directory and
1705 * parent relationships.
1706 */
Al Viro4693a542015-05-04 17:47:11 -04001707 if (unlikely(nd->last_type != LAST_NORM)) {
1708 err = handle_dots(nd, nd->last_type);
1709 if (flags & WALK_PUT)
1710 put_link(nd);
1711 return err;
1712 }
Al Viro254cf582015-05-05 09:40:46 -04001713 err = lookup_fast(nd, &path, &inode, &seq);
Al Viroce57dfc2011-03-13 19:58:58 -04001714 if (unlikely(err)) {
Miklos Szeredi697f5142012-05-21 17:30:05 +02001715 if (err < 0)
Al Virof0a9ba72015-05-04 07:59:30 -04001716 return err;
Miklos Szeredi697f5142012-05-21 17:30:05 +02001717
Al Virocaa85632015-04-22 17:52:47 -04001718 err = lookup_slow(nd, &path);
Miklos Szeredi697f5142012-05-21 17:30:05 +02001719 if (err < 0)
Al Virof0a9ba72015-05-04 07:59:30 -04001720 return err;
Miklos Szeredi697f5142012-05-21 17:30:05 +02001721
David Howells63afdfc2015-05-06 15:59:00 +01001722 inode = d_backing_inode(path.dentry);
Al Viro254cf582015-05-05 09:40:46 -04001723 seq = 0; /* we are already out of RCU mode */
Al Viro766c4cb2015-05-07 19:24:57 -04001724 err = -ENOENT;
Al Virocaa85632015-04-22 17:52:47 -04001725 if (d_is_negative(path.dentry))
Al Viro766c4cb2015-05-07 19:24:57 -04001726 goto out_path_put;
Al Viroce57dfc2011-03-13 19:58:58 -04001727 }
Miklos Szeredi697f5142012-05-21 17:30:05 +02001728
Al Viro4693a542015-05-04 17:47:11 -04001729 if (flags & WALK_PUT)
1730 put_link(nd);
Al Viro181548c2015-05-07 19:54:34 -04001731 err = should_follow_link(nd, &path, flags & WALK_GET, inode, seq);
Al Virod63ff282015-05-04 18:13:23 -04001732 if (unlikely(err))
1733 return err;
Al Virocaa85632015-04-22 17:52:47 -04001734 path_to_nameidata(&path, nd);
Al Viroce57dfc2011-03-13 19:58:58 -04001735 nd->inode = inode;
Al Viro254cf582015-05-05 09:40:46 -04001736 nd->seq = seq;
Al Viroce57dfc2011-03-13 19:58:58 -04001737 return 0;
Miklos Szeredi697f5142012-05-21 17:30:05 +02001738
1739out_path_put:
Al Virocaa85632015-04-22 17:52:47 -04001740 path_to_nameidata(&path, nd);
Miklos Szeredi697f5142012-05-21 17:30:05 +02001741 return err;
Al Viroce57dfc2011-03-13 19:58:58 -04001742}
1743
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744/*
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001745 * We can do the critical dentry name comparison and hashing
1746 * operations one word at a time, but we are limited to:
1747 *
1748 * - Architectures with fast unaligned word accesses. We could
1749 * do a "get_unaligned()" if this helps and is sufficiently
1750 * fast.
1751 *
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001752 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1753 * do not trap on the (extremely unlikely) case of a page
1754 * crossing operation.
1755 *
1756 * - Furthermore, we need an efficient 64-bit compile for the
1757 * 64-bit case in order to generate the "number of bytes in
1758 * the final mask". Again, that could be replaced with a
1759 * efficient population count instruction or similar.
1760 */
1761#ifdef CONFIG_DCACHE_WORD_ACCESS
1762
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001763#include <asm/word-at-a-time.h>
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001764
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001765#ifdef CONFIG_64BIT
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001766
1767static inline unsigned int fold_hash(unsigned long hash)
1768{
Linus Torvalds99d263d2014-09-13 11:30:10 -07001769 return hash_64(hash, 32);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001770}
1771
1772#else /* 32-bit case */
1773
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001774#define fold_hash(x) (x)
1775
1776#endif
1777
1778unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1779{
1780 unsigned long a, mask;
1781 unsigned long hash = 0;
1782
1783 for (;;) {
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001784 a = load_unaligned_zeropad(name);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001785 if (len < sizeof(unsigned long))
1786 break;
1787 hash += a;
Al Virof132c5b2012-03-22 21:59:52 +00001788 hash *= 9;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001789 name += sizeof(unsigned long);
1790 len -= sizeof(unsigned long);
1791 if (!len)
1792 goto done;
1793 }
Will Deacona5c21dc2013-12-12 17:40:21 +00001794 mask = bytemask_from_count(len);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001795 hash += mask & a;
1796done:
1797 return fold_hash(hash);
1798}
1799EXPORT_SYMBOL(full_name_hash);
1800
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001801/*
1802 * Calculate the length and hash of the path component, and
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07001803 * return the "hash_len" as the result.
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001804 */
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07001805static inline u64 hash_name(const char *name)
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001806{
Linus Torvalds36126f82012-05-26 10:43:17 -07001807 unsigned long a, b, adata, bdata, mask, hash, len;
1808 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001809
1810 hash = a = 0;
1811 len = -sizeof(unsigned long);
1812 do {
1813 hash = (hash + a) * 9;
1814 len += sizeof(unsigned long);
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001815 a = load_unaligned_zeropad(name+len);
Linus Torvalds36126f82012-05-26 10:43:17 -07001816 b = a ^ REPEAT_BYTE('/');
1817 } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001818
Linus Torvalds36126f82012-05-26 10:43:17 -07001819 adata = prep_zero_mask(a, adata, &constants);
1820 bdata = prep_zero_mask(b, bdata, &constants);
1821
1822 mask = create_zero_mask(adata | bdata);
1823
1824 hash += a & zero_bytemask(mask);
Linus Torvalds9226b5b2014-09-14 17:28:32 -07001825 len += find_zero(mask);
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07001826 return hashlen_create(fold_hash(hash), len);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001827}
1828
1829#else
1830
Linus Torvalds0145acc2012-03-02 14:32:59 -08001831unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1832{
1833 unsigned long hash = init_name_hash();
1834 while (len--)
1835 hash = partial_name_hash(*name++, hash);
1836 return end_name_hash(hash);
1837}
Linus Torvaldsae942ae2012-03-02 19:40:57 -08001838EXPORT_SYMBOL(full_name_hash);
Linus Torvalds0145acc2012-03-02 14:32:59 -08001839
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001840/*
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001841 * We know there's a real path component here of at least
1842 * one character.
1843 */
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07001844static inline u64 hash_name(const char *name)
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001845{
1846 unsigned long hash = init_name_hash();
1847 unsigned long len = 0, c;
1848
1849 c = (unsigned char)*name;
1850 do {
1851 len++;
1852 hash = partial_name_hash(c, hash);
1853 c = (unsigned char)name[len];
1854 } while (c && c != '/');
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07001855 return hashlen_create(end_name_hash(hash), len);
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001856}
1857
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001858#endif
1859
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001860/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01001862 * This is the basic name resolution function, turning a pathname into
1863 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01001865 * Returns 0 and nd will have valid dentry and mnt on success.
1866 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 */
Al Viro6de88d72009-08-09 01:41:57 +04001868static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 int err;
Al Viro32cd7462015-04-18 20:30:49 -04001871
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 while (*name=='/')
1873 name++;
1874 if (!*name)
Al Viro9e18f102015-04-18 20:44:34 -04001875 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 /* At this point we know we have a real path component. */
1878 for(;;) {
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07001879 u64 hash_len;
Al Virofe479a52011-02-22 15:10:03 -05001880 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Al Viro52094c82011-02-21 21:34:47 -05001882 err = may_lookup(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 if (err)
Al Viro3595e232015-05-09 16:54:45 -04001884 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07001886 hash_len = hash_name(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
Al Virofe479a52011-02-22 15:10:03 -05001888 type = LAST_NORM;
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07001889 if (name[0] == '.') switch (hashlen_len(hash_len)) {
Al Virofe479a52011-02-22 15:10:03 -05001890 case 2:
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001891 if (name[1] == '.') {
Al Virofe479a52011-02-22 15:10:03 -05001892 type = LAST_DOTDOT;
Al Viro16c2cd72011-02-22 15:50:10 -05001893 nd->flags |= LOOKUP_JUMPED;
1894 }
Al Virofe479a52011-02-22 15:10:03 -05001895 break;
1896 case 1:
1897 type = LAST_DOT;
1898 }
Al Viro5a202bc2011-03-08 14:17:44 -05001899 if (likely(type == LAST_NORM)) {
1900 struct dentry *parent = nd->path.dentry;
Al Viro16c2cd72011-02-22 15:50:10 -05001901 nd->flags &= ~LOOKUP_JUMPED;
Al Viro5a202bc2011-03-08 14:17:44 -05001902 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
James Hogana060dc52014-09-16 13:07:35 +01001903 struct qstr this = { { .hash_len = hash_len }, .name = name };
Linus Torvaldsda53be12013-05-21 15:22:44 -07001904 err = parent->d_op->d_hash(parent, &this);
Al Viro5a202bc2011-03-08 14:17:44 -05001905 if (err < 0)
Al Viro3595e232015-05-09 16:54:45 -04001906 return err;
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07001907 hash_len = this.hash_len;
1908 name = this.name;
Al Viro5a202bc2011-03-08 14:17:44 -05001909 }
1910 }
Al Virofe479a52011-02-22 15:10:03 -05001911
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07001912 nd->last.hash_len = hash_len;
1913 nd->last.name = name;
Al Viro5f4a6a62013-01-24 18:04:22 -05001914 nd->last_type = type;
1915
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07001916 name += hashlen_len(hash_len);
1917 if (!*name)
Al Virobdf6cbf2015-04-18 20:21:40 -04001918 goto OK;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001919 /*
1920 * If it wasn't NUL, we know it was '/'. Skip that
1921 * slash, and continue until no more slashes.
1922 */
1923 do {
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07001924 name++;
1925 } while (unlikely(*name == '/'));
Al Viro8620c232015-05-04 08:58:35 -04001926 if (unlikely(!*name)) {
1927OK:
Al Viro368ee9b2015-05-08 17:19:59 -04001928 /* pathname body, done */
Al Viro8620c232015-05-04 08:58:35 -04001929 if (!nd->depth)
1930 return 0;
1931 name = nd->stack[nd->depth - 1].name;
Al Viro368ee9b2015-05-08 17:19:59 -04001932 /* trailing symlink, done */
Al Viro8620c232015-05-04 08:58:35 -04001933 if (!name)
1934 return 0;
1935 /* last component of nested symlink */
Al Viro4693a542015-05-04 17:47:11 -04001936 err = walk_component(nd, WALK_GET | WALK_PUT);
Al Viro8620c232015-05-04 08:58:35 -04001937 } else {
Al Viro4693a542015-05-04 17:47:11 -04001938 err = walk_component(nd, WALK_GET);
Al Viro8620c232015-05-04 08:58:35 -04001939 }
Al Viroce57dfc2011-03-13 19:58:58 -04001940 if (err < 0)
Al Viro3595e232015-05-09 16:54:45 -04001941 return err;
Al Virofe479a52011-02-22 15:10:03 -05001942
Al Viroce57dfc2011-03-13 19:58:58 -04001943 if (err) {
Al Viro626de992015-05-04 18:26:59 -04001944 const char *s = get_link(nd);
Al Viro5a460272015-04-17 23:44:45 -04001945
Al Viro3595e232015-05-09 16:54:45 -04001946 if (unlikely(IS_ERR(s)))
1947 return PTR_ERR(s);
Al Virod40bcc02015-04-18 20:03:03 -04001948 err = 0;
1949 if (unlikely(!s)) {
1950 /* jumped */
Al Virob9ff4422015-05-02 20:19:23 -04001951 put_link(nd);
Al Virod40bcc02015-04-18 20:03:03 -04001952 } else {
Al Virofab51e82015-05-10 11:01:00 -04001953 nd->stack[nd->depth - 1].name = name;
1954 name = s;
1955 continue;
Al Virod40bcc02015-04-18 20:03:03 -04001956 }
Nick Piggin31e6b012011-01-07 17:49:52 +11001957 }
Al Viro3595e232015-05-09 16:54:45 -04001958 if (unlikely(!d_can_lookup(nd->path.dentry)))
1959 return -ENOTDIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961}
1962
Al Viroc8a53ee2015-05-12 18:43:07 -04001963static const char *path_init(struct nameidata *nd, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964{
Prasanna Medaea3834d2005-04-29 16:00:17 +01001965 int retval = 0;
Al Viroc8a53ee2015-05-12 18:43:07 -04001966 const char *s = nd->name->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
1968 nd->last_type = LAST_ROOT; /* if there are only slashes... */
Al Viro980f3ea2014-11-20 14:20:24 -05001969 nd->flags = flags | LOOKUP_JUMPED | LOOKUP_PARENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 nd->depth = 0;
Al Viro368ee9b2015-05-08 17:19:59 -04001971 nd->total_link_count = 0;
Al Viro5b6ca022011-03-09 23:04:47 -05001972 if (flags & LOOKUP_ROOT) {
David Howellsb18825a2013-09-12 19:22:53 +01001973 struct dentry *root = nd->root.dentry;
1974 struct inode *inode = root->d_inode;
Al Virofd2f7cb2015-02-22 20:07:13 -05001975 if (*s) {
Miklos Szeredi44b1d532014-04-01 17:08:41 +02001976 if (!d_can_lookup(root))
Al Viro368ee9b2015-05-08 17:19:59 -04001977 return ERR_PTR(-ENOTDIR);
Al Viro73d049a2011-03-11 12:08:24 -05001978 retval = inode_permission(inode, MAY_EXEC);
1979 if (retval)
Al Viro368ee9b2015-05-08 17:19:59 -04001980 return ERR_PTR(retval);
Al Viro73d049a2011-03-11 12:08:24 -05001981 }
Al Viro5b6ca022011-03-09 23:04:47 -05001982 nd->path = nd->root;
1983 nd->inode = inode;
1984 if (flags & LOOKUP_RCU) {
Al Viro8b61e742013-11-08 12:45:01 -05001985 rcu_read_lock();
Al Viro5b6ca022011-03-09 23:04:47 -05001986 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro8f47a0162015-05-09 19:02:01 -04001987 nd->root_seq = nd->seq;
Al Viro48a066e2013-09-29 22:06:07 -04001988 nd->m_seq = read_seqbegin(&mount_lock);
Al Viro5b6ca022011-03-09 23:04:47 -05001989 } else {
1990 path_get(&nd->path);
1991 }
Al Viro368ee9b2015-05-08 17:19:59 -04001992 return s;
Al Viro5b6ca022011-03-09 23:04:47 -05001993 }
1994
Al Viro2a737872009-04-07 11:49:53 -04001995 nd->root.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
Al Viro48a066e2013-09-29 22:06:07 -04001997 nd->m_seq = read_seqbegin(&mount_lock);
Al Virofd2f7cb2015-02-22 20:07:13 -05001998 if (*s == '/') {
Al Viroe41f7d42011-02-22 14:02:58 -05001999 if (flags & LOOKUP_RCU) {
Al Viro8b61e742013-11-08 12:45:01 -05002000 rcu_read_lock();
Al Viro7bd88372014-09-13 21:55:46 -04002001 nd->seq = set_root_rcu(nd);
Al Viroe41f7d42011-02-22 14:02:58 -05002002 } else {
2003 set_root(nd);
2004 path_get(&nd->root);
2005 }
Al Viro2a737872009-04-07 11:49:53 -04002006 nd->path = nd->root;
Al Viroc8a53ee2015-05-12 18:43:07 -04002007 } else if (nd->dfd == AT_FDCWD) {
Al Viroe41f7d42011-02-22 14:02:58 -05002008 if (flags & LOOKUP_RCU) {
2009 struct fs_struct *fs = current->fs;
2010 unsigned seq;
2011
Al Viro8b61e742013-11-08 12:45:01 -05002012 rcu_read_lock();
Al Viroe41f7d42011-02-22 14:02:58 -05002013
2014 do {
2015 seq = read_seqcount_begin(&fs->seq);
2016 nd->path = fs->pwd;
2017 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
2018 } while (read_seqcount_retry(&fs->seq, seq));
2019 } else {
2020 get_fs_pwd(current->fs, &nd->path);
2021 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002022 } else {
Jeff Layton582aa642012-12-11 08:56:16 -05002023 /* Caller must check execute permissions on the starting path component */
Al Viroc8a53ee2015-05-12 18:43:07 -04002024 struct fd f = fdget_raw(nd->dfd);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002025 struct dentry *dentry;
2026
Al Viro2903ff02012-08-28 12:52:22 -04002027 if (!f.file)
Al Viro368ee9b2015-05-08 17:19:59 -04002028 return ERR_PTR(-EBADF);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002029
Al Viro2903ff02012-08-28 12:52:22 -04002030 dentry = f.file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002031
Al Virofd2f7cb2015-02-22 20:07:13 -05002032 if (*s) {
Miklos Szeredi44b1d532014-04-01 17:08:41 +02002033 if (!d_can_lookup(dentry)) {
Al Viro2903ff02012-08-28 12:52:22 -04002034 fdput(f);
Al Viro368ee9b2015-05-08 17:19:59 -04002035 return ERR_PTR(-ENOTDIR);
Al Viro2903ff02012-08-28 12:52:22 -04002036 }
Al Virof52e0c12011-03-14 18:56:51 -04002037 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002038
Al Viro2903ff02012-08-28 12:52:22 -04002039 nd->path = f.file->f_path;
Al Viroe41f7d42011-02-22 14:02:58 -05002040 if (flags & LOOKUP_RCU) {
Al Viro8b61e742013-11-08 12:45:01 -05002041 rcu_read_lock();
Al Viro34a26b92015-05-11 08:05:05 -04002042 nd->inode = nd->path.dentry->d_inode;
2043 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viroe41f7d42011-02-22 14:02:58 -05002044 } else {
Al Viro2903ff02012-08-28 12:52:22 -04002045 path_get(&nd->path);
Al Viro34a26b92015-05-11 08:05:05 -04002046 nd->inode = nd->path.dentry->d_inode;
Al Viroe41f7d42011-02-22 14:02:58 -05002047 }
Al Viro34a26b92015-05-11 08:05:05 -04002048 fdput(f);
Al Viro368ee9b2015-05-08 17:19:59 -04002049 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 }
Al Viroe41f7d42011-02-22 14:02:58 -05002051
Nick Piggin31e6b012011-01-07 17:49:52 +11002052 nd->inode = nd->path.dentry->d_inode;
Al Viro4023bfc2014-09-13 21:59:43 -04002053 if (!(flags & LOOKUP_RCU))
Al Viro368ee9b2015-05-08 17:19:59 -04002054 return s;
Al Viro4023bfc2014-09-13 21:59:43 -04002055 if (likely(!read_seqcount_retry(&nd->path.dentry->d_seq, nd->seq)))
Al Viro368ee9b2015-05-08 17:19:59 -04002056 return s;
Al Viro4023bfc2014-09-13 21:59:43 -04002057 if (!(nd->flags & LOOKUP_ROOT))
2058 nd->root.mnt = NULL;
2059 rcu_read_unlock();
Al Viro368ee9b2015-05-08 17:19:59 -04002060 return ERR_PTR(-ECHILD);
Al Viro9b4a9b12009-04-07 11:44:16 -04002061}
2062
Al Viro3bdba282015-05-08 17:37:07 -04002063static const char *trailing_symlink(struct nameidata *nd)
Al Viro95fa25d2015-04-22 13:46:57 -04002064{
2065 const char *s;
Al Virofec2fa22015-05-06 15:58:18 -04002066 int error = may_follow_link(nd);
Al Virodeb106c2015-05-08 18:05:21 -04002067 if (unlikely(error))
Al Viro3bdba282015-05-08 17:37:07 -04002068 return ERR_PTR(error);
Al Viro95fa25d2015-04-22 13:46:57 -04002069 nd->flags |= LOOKUP_PARENT;
Al Virofab51e82015-05-10 11:01:00 -04002070 nd->stack[0].name = NULL;
Al Viro3b2e7f72015-04-19 00:53:50 -04002071 s = get_link(nd);
Al Virodeb106c2015-05-08 18:05:21 -04002072 return s ? s : "";
Al Viro95fa25d2015-04-22 13:46:57 -04002073}
2074
Al Virocaa85632015-04-22 17:52:47 -04002075static inline int lookup_last(struct nameidata *nd)
Al Virobd92d7f2011-03-14 19:54:59 -04002076{
2077 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
2078 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2079
2080 nd->flags &= ~LOOKUP_PARENT;
Al Virodeb106c2015-05-08 18:05:21 -04002081 return walk_component(nd,
Al Viro4693a542015-05-04 17:47:11 -04002082 nd->flags & LOOKUP_FOLLOW
2083 ? nd->depth
2084 ? WALK_PUT | WALK_GET
2085 : WALK_GET
2086 : 0);
Al Virobd92d7f2011-03-14 19:54:59 -04002087}
2088
Al Viro9b4a9b12009-04-07 11:44:16 -04002089/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroc8a53ee2015-05-12 18:43:07 -04002090static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path)
Al Viro9b4a9b12009-04-07 11:44:16 -04002091{
Al Viroc8a53ee2015-05-12 18:43:07 -04002092 const char *s = path_init(nd, flags);
Al Virobd92d7f2011-03-14 19:54:59 -04002093 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11002094
Al Viro368ee9b2015-05-08 17:19:59 -04002095 if (IS_ERR(s))
2096 return PTR_ERR(s);
Al Viro3bdba282015-05-08 17:37:07 -04002097 while (!(err = link_path_walk(s, nd))
2098 && ((err = lookup_last(nd)) > 0)) {
2099 s = trailing_symlink(nd);
2100 if (IS_ERR(s)) {
2101 err = PTR_ERR(s);
2102 break;
Al Virobd92d7f2011-03-14 19:54:59 -04002103 }
2104 }
Al Viro9f1fafe2011-03-25 11:00:12 -04002105 if (!err)
2106 err = complete_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04002107
Al Virodeb106c2015-05-08 18:05:21 -04002108 if (!err && nd->flags & LOOKUP_DIRECTORY)
2109 if (!d_can_lookup(nd->path.dentry))
Al Virobd23a532011-03-23 09:56:30 -04002110 err = -ENOTDIR;
Al Viro625b6d12015-05-12 16:36:12 -04002111 if (!err) {
2112 *path = nd->path;
2113 nd->path.mnt = NULL;
2114 nd->path.dentry = NULL;
2115 }
2116 terminate_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04002117 return err;
Al Viroee0827c2011-02-21 23:38:09 -05002118}
Nick Piggin31e6b012011-01-07 17:49:52 +11002119
Al Viro625b6d12015-05-12 16:36:12 -04002120static int filename_lookup(int dfd, struct filename *name, unsigned flags,
Al Viro9ad1aaa2015-05-12 16:44:39 -04002121 struct path *path, struct path *root)
Jeff Layton873f1ee2012-10-10 15:25:29 -04002122{
Al Viro894bc8c2015-05-02 07:16:16 -04002123 int retval;
Al Viro9883d182015-05-13 07:28:08 -04002124 struct nameidata nd;
Al Viroabc9f5b2015-05-12 16:53:42 -04002125 if (IS_ERR(name))
2126 return PTR_ERR(name);
Al Viro9ad1aaa2015-05-12 16:44:39 -04002127 if (unlikely(root)) {
2128 nd.root = *root;
2129 flags |= LOOKUP_ROOT;
2130 }
Al Viro9883d182015-05-13 07:28:08 -04002131 set_nameidata(&nd, dfd, name);
Al Viroc8a53ee2015-05-12 18:43:07 -04002132 retval = path_lookupat(&nd, flags | LOOKUP_RCU, path);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002133 if (unlikely(retval == -ECHILD))
Al Viroc8a53ee2015-05-12 18:43:07 -04002134 retval = path_lookupat(&nd, flags, path);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002135 if (unlikely(retval == -ESTALE))
Al Viroc8a53ee2015-05-12 18:43:07 -04002136 retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002137
2138 if (likely(!retval))
Al Viro625b6d12015-05-12 16:36:12 -04002139 audit_inode(name, path->dentry, flags & LOOKUP_PARENT);
Al Viro9883d182015-05-13 07:28:08 -04002140 restore_nameidata();
Al Viroe4bd1c12015-05-12 16:40:39 -04002141 putname(name);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002142 return retval;
2143}
2144
Al Viro8bcb77f2015-05-08 16:59:20 -04002145/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroc8a53ee2015-05-12 18:43:07 -04002146static int path_parentat(struct nameidata *nd, unsigned flags,
Al Viro391172c2015-05-09 11:19:16 -04002147 struct path *parent)
Al Viro8bcb77f2015-05-08 16:59:20 -04002148{
Al Viroc8a53ee2015-05-12 18:43:07 -04002149 const char *s = path_init(nd, flags);
Al Viro368ee9b2015-05-08 17:19:59 -04002150 int err;
2151 if (IS_ERR(s))
2152 return PTR_ERR(s);
2153 err = link_path_walk(s, nd);
Al Viro8bcb77f2015-05-08 16:59:20 -04002154 if (!err)
2155 err = complete_walk(nd);
Al Viro391172c2015-05-09 11:19:16 -04002156 if (!err) {
2157 *parent = nd->path;
2158 nd->path.mnt = NULL;
2159 nd->path.dentry = NULL;
2160 }
2161 terminate_walk(nd);
Al Viro8bcb77f2015-05-08 16:59:20 -04002162 return err;
2163}
2164
Al Viro5c31b6c2015-05-12 17:32:54 -04002165static struct filename *filename_parentat(int dfd, struct filename *name,
Al Viro391172c2015-05-09 11:19:16 -04002166 unsigned int flags, struct path *parent,
2167 struct qstr *last, int *type)
Al Viro8bcb77f2015-05-08 16:59:20 -04002168{
2169 int retval;
Al Viro9883d182015-05-13 07:28:08 -04002170 struct nameidata nd;
Al Viro8bcb77f2015-05-08 16:59:20 -04002171
Al Viro5c31b6c2015-05-12 17:32:54 -04002172 if (IS_ERR(name))
2173 return name;
Al Viro9883d182015-05-13 07:28:08 -04002174 set_nameidata(&nd, dfd, name);
Al Viroc8a53ee2015-05-12 18:43:07 -04002175 retval = path_parentat(&nd, flags | LOOKUP_RCU, parent);
Al Viro8bcb77f2015-05-08 16:59:20 -04002176 if (unlikely(retval == -ECHILD))
Al Viroc8a53ee2015-05-12 18:43:07 -04002177 retval = path_parentat(&nd, flags, parent);
Al Viro8bcb77f2015-05-08 16:59:20 -04002178 if (unlikely(retval == -ESTALE))
Al Viroc8a53ee2015-05-12 18:43:07 -04002179 retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent);
Al Viro391172c2015-05-09 11:19:16 -04002180 if (likely(!retval)) {
2181 *last = nd.last;
2182 *type = nd.last_type;
2183 audit_inode(name, parent->dentry, LOOKUP_PARENT);
Al Viro5c31b6c2015-05-12 17:32:54 -04002184 } else {
2185 putname(name);
2186 name = ERR_PTR(retval);
Al Viro391172c2015-05-09 11:19:16 -04002187 }
Al Viro9883d182015-05-13 07:28:08 -04002188 restore_nameidata();
Al Viro5c31b6c2015-05-12 17:32:54 -04002189 return name;
Al Viro8bcb77f2015-05-08 16:59:20 -04002190}
2191
Al Viro79714f72012-06-15 03:01:42 +04002192/* does lookup, returns the object with parent locked */
2193struct dentry *kern_path_locked(const char *name, struct path *path)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002194{
Al Viro5c31b6c2015-05-12 17:32:54 -04002195 struct filename *filename;
2196 struct dentry *d;
Al Viro391172c2015-05-09 11:19:16 -04002197 struct qstr last;
2198 int type;
Paul Moore51689102015-01-22 00:00:03 -05002199
Al Viro5c31b6c2015-05-12 17:32:54 -04002200 filename = filename_parentat(AT_FDCWD, getname_kernel(name), 0, path,
2201 &last, &type);
Paul Moore51689102015-01-22 00:00:03 -05002202 if (IS_ERR(filename))
2203 return ERR_CAST(filename);
Al Viro5c31b6c2015-05-12 17:32:54 -04002204 if (unlikely(type != LAST_NORM)) {
Al Viro391172c2015-05-09 11:19:16 -04002205 path_put(path);
Al Viro5c31b6c2015-05-12 17:32:54 -04002206 putname(filename);
2207 return ERR_PTR(-EINVAL);
Al Viro79714f72012-06-15 03:01:42 +04002208 }
Al Viro391172c2015-05-09 11:19:16 -04002209 mutex_lock_nested(&path->dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2210 d = __lookup_hash(&last, path->dentry, 0);
Al Viro79714f72012-06-15 03:01:42 +04002211 if (IS_ERR(d)) {
Al Viro391172c2015-05-09 11:19:16 -04002212 mutex_unlock(&path->dentry->d_inode->i_mutex);
2213 path_put(path);
Al Viro79714f72012-06-15 03:01:42 +04002214 }
Paul Moore51689102015-01-22 00:00:03 -05002215 putname(filename);
Al Viro79714f72012-06-15 03:01:42 +04002216 return d;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002217}
2218
Al Virod1811462008-08-02 00:49:18 -04002219int kern_path(const char *name, unsigned int flags, struct path *path)
2220{
Al Viroabc9f5b2015-05-12 16:53:42 -04002221 return filename_lookup(AT_FDCWD, getname_kernel(name),
2222 flags, path, NULL);
Al Virod1811462008-08-02 00:49:18 -04002223}
Al Viro4d359502014-03-14 12:20:17 -04002224EXPORT_SYMBOL(kern_path);
Al Virod1811462008-08-02 00:49:18 -04002225
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002226/**
2227 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2228 * @dentry: pointer to dentry of the base directory
2229 * @mnt: pointer to vfs mount of the base directory
2230 * @name: pointer to file name
2231 * @flags: lookup flags
Al Viroe0a01242011-06-27 17:00:37 -04002232 * @path: pointer to struct path to fill
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002233 */
2234int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2235 const char *name, unsigned int flags,
Al Viroe0a01242011-06-27 17:00:37 -04002236 struct path *path)
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002237{
Al Viro9ad1aaa2015-05-12 16:44:39 -04002238 struct path root = {.mnt = mnt, .dentry = dentry};
Al Viro9ad1aaa2015-05-12 16:44:39 -04002239 /* the first argument of filename_lookup() is ignored with root */
Al Viroabc9f5b2015-05-12 16:53:42 -04002240 return filename_lookup(AT_FDCWD, getname_kernel(name),
2241 flags , path, &root);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002242}
Al Viro4d359502014-03-14 12:20:17 -04002243EXPORT_SYMBOL(vfs_path_lookup);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002244
Christoph Hellwigeead1912007-10-16 23:25:38 -07002245/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07002246 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07002247 * @name: pathname component to lookup
2248 * @base: base directory to lookup from
2249 * @len: maximum length @len should be interpreted to
2250 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07002251 * Note that this routine is purely a helper for filesystem usage and should
Al Viro9e7543e2015-02-23 02:49:48 -05002252 * not be called by generic code.
Christoph Hellwigeead1912007-10-16 23:25:38 -07002253 */
James Morris057f6c02007-04-26 00:12:05 -07002254struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2255{
James Morris057f6c02007-04-26 00:12:05 -07002256 struct qstr this;
Al Viro6a96ba52011-03-07 23:49:20 -05002257 unsigned int c;
Miklos Szeredicda309d2012-03-26 12:54:21 +02002258 int err;
James Morris057f6c02007-04-26 00:12:05 -07002259
David Woodhouse2f9092e2009-04-20 23:18:37 +01002260 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
2261
Al Viro6a96ba52011-03-07 23:49:20 -05002262 this.name = name;
2263 this.len = len;
Linus Torvalds0145acc2012-03-02 14:32:59 -08002264 this.hash = full_name_hash(name, len);
Al Viro6a96ba52011-03-07 23:49:20 -05002265 if (!len)
2266 return ERR_PTR(-EACCES);
2267
Al Viro21d8a152012-11-29 22:17:21 -05002268 if (unlikely(name[0] == '.')) {
2269 if (len < 2 || (len == 2 && name[1] == '.'))
2270 return ERR_PTR(-EACCES);
2271 }
2272
Al Viro6a96ba52011-03-07 23:49:20 -05002273 while (len--) {
2274 c = *(const unsigned char *)name++;
2275 if (c == '/' || c == '\0')
2276 return ERR_PTR(-EACCES);
Al Viro6a96ba52011-03-07 23:49:20 -05002277 }
Al Viro5a202bc2011-03-08 14:17:44 -05002278 /*
2279 * See if the low-level filesystem might want
2280 * to use its own hash..
2281 */
2282 if (base->d_flags & DCACHE_OP_HASH) {
Linus Torvaldsda53be12013-05-21 15:22:44 -07002283 int err = base->d_op->d_hash(base, &this);
Al Viro5a202bc2011-03-08 14:17:44 -05002284 if (err < 0)
2285 return ERR_PTR(err);
2286 }
Christoph Hellwigeead1912007-10-16 23:25:38 -07002287
Miklos Szeredicda309d2012-03-26 12:54:21 +02002288 err = inode_permission(base->d_inode, MAY_EXEC);
2289 if (err)
2290 return ERR_PTR(err);
2291
Al Viro72bd8662012-06-10 17:17:17 -04002292 return __lookup_hash(&this, base, 0);
James Morris057f6c02007-04-26 00:12:05 -07002293}
Al Viro4d359502014-03-14 12:20:17 -04002294EXPORT_SYMBOL(lookup_one_len);
James Morris057f6c02007-04-26 00:12:05 -07002295
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002296int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2297 struct path *path, int *empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298{
Al Viroabc9f5b2015-05-12 16:53:42 -04002299 return filename_lookup(dfd, getname_flags(name, flags, empty),
2300 flags, path, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301}
2302
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002303int user_path_at(int dfd, const char __user *name, unsigned flags,
2304 struct path *path)
2305{
Linus Torvaldsf7493e52012-03-22 16:10:40 -07002306 return user_path_at_empty(dfd, name, flags, path, NULL);
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002307}
Al Viro4d359502014-03-14 12:20:17 -04002308EXPORT_SYMBOL(user_path_at);
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002309
Jeff Layton873f1ee2012-10-10 15:25:29 -04002310/*
2311 * NB: most callers don't do anything directly with the reference to the
2312 * to struct filename, but the nd->last pointer points into the name string
2313 * allocated by getname. So we must hold the reference to it until all
2314 * path-walking is complete.
2315 */
Al Viroa2ec4a22015-05-13 06:57:49 -04002316static inline struct filename *
Al Virof5beed72015-04-30 16:09:11 -04002317user_path_parent(int dfd, const char __user *path,
2318 struct path *parent,
2319 struct qstr *last,
2320 int *type,
Jeff Layton9e790bd2012-12-11 12:10:09 -05002321 unsigned int flags)
Al Viro2ad94ae2008-07-21 09:32:51 -04002322{
Jeff Layton9e790bd2012-12-11 12:10:09 -05002323 /* only LOOKUP_REVAL is allowed in extra flags */
Al Viro5c31b6c2015-05-12 17:32:54 -04002324 return filename_parentat(dfd, getname(path), flags & LOOKUP_REVAL,
2325 parent, last, type);
Al Viro2ad94ae2008-07-21 09:32:51 -04002326}
2327
Jeff Layton80334262013-07-26 06:23:25 -04002328/**
Al Viro197df042013-09-08 14:03:27 -04002329 * mountpoint_last - look up last component for umount
Jeff Layton80334262013-07-26 06:23:25 -04002330 * @nd: pathwalk nameidata - currently pointing at parent directory of "last"
2331 * @path: pointer to container for result
2332 *
2333 * This is a special lookup_last function just for umount. In this case, we
2334 * need to resolve the path without doing any revalidation.
2335 *
2336 * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
2337 * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
2338 * in almost all cases, this lookup will be served out of the dcache. The only
2339 * cases where it won't are if nd->last refers to a symlink or the path is
2340 * bogus and it doesn't exist.
2341 *
2342 * Returns:
2343 * -error: if there was an error during lookup. This includes -ENOENT if the
2344 * lookup found a negative dentry. The nd->path reference will also be
2345 * put in this case.
2346 *
2347 * 0: if we successfully resolved nd->path and found it to not to be a
2348 * symlink that needs to be followed. "path" will also be populated.
2349 * The nd->path reference will also be put.
2350 *
2351 * 1: if we successfully resolved nd->last and found it to be a symlink
2352 * that needs to be followed. "path" will be populated with the path
2353 * to the link, and nd->path will *not* be put.
2354 */
2355static int
Al Viro197df042013-09-08 14:03:27 -04002356mountpoint_last(struct nameidata *nd, struct path *path)
Jeff Layton80334262013-07-26 06:23:25 -04002357{
2358 int error = 0;
2359 struct dentry *dentry;
2360 struct dentry *dir = nd->path.dentry;
2361
Al Viro35759522013-09-08 13:41:33 -04002362 /* If we're in rcuwalk, drop out of it to handle last component */
2363 if (nd->flags & LOOKUP_RCU) {
Al Viro6e9918b2015-05-05 09:26:05 -04002364 if (unlazy_walk(nd, NULL, 0))
Al Virodeb106c2015-05-08 18:05:21 -04002365 return -ECHILD;
Jeff Layton80334262013-07-26 06:23:25 -04002366 }
2367
2368 nd->flags &= ~LOOKUP_PARENT;
2369
2370 if (unlikely(nd->last_type != LAST_NORM)) {
2371 error = handle_dots(nd, nd->last_type);
Al Viro35759522013-09-08 13:41:33 -04002372 if (error)
Al Virodeb106c2015-05-08 18:05:21 -04002373 return error;
Al Viro35759522013-09-08 13:41:33 -04002374 dentry = dget(nd->path.dentry);
2375 goto done;
Jeff Layton80334262013-07-26 06:23:25 -04002376 }
2377
2378 mutex_lock(&dir->d_inode->i_mutex);
2379 dentry = d_lookup(dir, &nd->last);
2380 if (!dentry) {
2381 /*
2382 * No cached dentry. Mounted dentries are pinned in the cache,
2383 * so that means that this dentry is probably a symlink or the
2384 * path doesn't actually point to a mounted dentry.
2385 */
2386 dentry = d_alloc(dir, &nd->last);
2387 if (!dentry) {
Dave Jonesbcceeeb2013-09-10 17:04:25 -04002388 mutex_unlock(&dir->d_inode->i_mutex);
Al Virodeb106c2015-05-08 18:05:21 -04002389 return -ENOMEM;
Jeff Layton80334262013-07-26 06:23:25 -04002390 }
Al Viro35759522013-09-08 13:41:33 -04002391 dentry = lookup_real(dir->d_inode, dentry, nd->flags);
Dave Jonesbcceeeb2013-09-10 17:04:25 -04002392 if (IS_ERR(dentry)) {
2393 mutex_unlock(&dir->d_inode->i_mutex);
Al Virodeb106c2015-05-08 18:05:21 -04002394 return PTR_ERR(dentry);
Dave Jonesbcceeeb2013-09-10 17:04:25 -04002395 }
Jeff Layton80334262013-07-26 06:23:25 -04002396 }
2397 mutex_unlock(&dir->d_inode->i_mutex);
2398
Al Viro35759522013-09-08 13:41:33 -04002399done:
David Howells698934d2015-03-17 17:33:52 +00002400 if (d_is_negative(dentry)) {
Al Viro35759522013-09-08 13:41:33 -04002401 dput(dentry);
Al Virodeb106c2015-05-08 18:05:21 -04002402 return -ENOENT;
Jeff Layton80334262013-07-26 06:23:25 -04002403 }
Al Viro191d7f72015-05-04 08:26:45 -04002404 if (nd->depth)
2405 put_link(nd);
Al Viro35759522013-09-08 13:41:33 -04002406 path->dentry = dentry;
Vasily Averin295dc392014-07-21 12:30:23 +04002407 path->mnt = nd->path.mnt;
Al Viro181548c2015-05-07 19:54:34 -04002408 error = should_follow_link(nd, path, nd->flags & LOOKUP_FOLLOW,
2409 d_backing_inode(dentry), 0);
Al Virodeb106c2015-05-08 18:05:21 -04002410 if (unlikely(error))
Al Virod63ff282015-05-04 18:13:23 -04002411 return error;
Vasily Averin295dc392014-07-21 12:30:23 +04002412 mntget(path->mnt);
Al Viro35759522013-09-08 13:41:33 -04002413 follow_mount(path);
Al Virodeb106c2015-05-08 18:05:21 -04002414 return 0;
Jeff Layton80334262013-07-26 06:23:25 -04002415}
2416
2417/**
Al Viro197df042013-09-08 14:03:27 -04002418 * path_mountpoint - look up a path to be umounted
Al Viroc8a53ee2015-05-12 18:43:07 -04002419 * @nameidata: lookup context
Jeff Layton80334262013-07-26 06:23:25 -04002420 * @flags: lookup flags
Al Viroc8a53ee2015-05-12 18:43:07 -04002421 * @path: pointer to container for result
Jeff Layton80334262013-07-26 06:23:25 -04002422 *
2423 * Look up the given name, but don't attempt to revalidate the last component.
Randy Dunlap606d6fe2013-10-19 14:56:55 -07002424 * Returns 0 and "path" will be valid on success; Returns error otherwise.
Jeff Layton80334262013-07-26 06:23:25 -04002425 */
2426static int
Al Viroc8a53ee2015-05-12 18:43:07 -04002427path_mountpoint(struct nameidata *nd, unsigned flags, struct path *path)
Jeff Layton80334262013-07-26 06:23:25 -04002428{
Al Viroc8a53ee2015-05-12 18:43:07 -04002429 const char *s = path_init(nd, flags);
Al Viro368ee9b2015-05-08 17:19:59 -04002430 int err;
2431 if (IS_ERR(s))
2432 return PTR_ERR(s);
Al Viro3bdba282015-05-08 17:37:07 -04002433 while (!(err = link_path_walk(s, nd)) &&
2434 (err = mountpoint_last(nd, path)) > 0) {
2435 s = trailing_symlink(nd);
2436 if (IS_ERR(s)) {
2437 err = PTR_ERR(s);
Jeff Layton80334262013-07-26 06:23:25 -04002438 break;
Al Viro3bdba282015-05-08 17:37:07 -04002439 }
Jeff Layton80334262013-07-26 06:23:25 -04002440 }
Al Virodeb106c2015-05-08 18:05:21 -04002441 terminate_walk(nd);
Jeff Layton80334262013-07-26 06:23:25 -04002442 return err;
2443}
2444
Al Viro2d864652013-09-08 20:18:44 -04002445static int
Al Viro668696d2015-02-22 19:44:00 -05002446filename_mountpoint(int dfd, struct filename *name, struct path *path,
Al Viro2d864652013-09-08 20:18:44 -04002447 unsigned int flags)
2448{
Al Viro9883d182015-05-13 07:28:08 -04002449 struct nameidata nd;
Al Virocbaab2d2015-01-22 02:49:00 -05002450 int error;
Al Viro668696d2015-02-22 19:44:00 -05002451 if (IS_ERR(name))
2452 return PTR_ERR(name);
Al Viro9883d182015-05-13 07:28:08 -04002453 set_nameidata(&nd, dfd, name);
Al Viroc8a53ee2015-05-12 18:43:07 -04002454 error = path_mountpoint(&nd, flags | LOOKUP_RCU, path);
Al Viro2d864652013-09-08 20:18:44 -04002455 if (unlikely(error == -ECHILD))
Al Viroc8a53ee2015-05-12 18:43:07 -04002456 error = path_mountpoint(&nd, flags, path);
Al Viro2d864652013-09-08 20:18:44 -04002457 if (unlikely(error == -ESTALE))
Al Viroc8a53ee2015-05-12 18:43:07 -04002458 error = path_mountpoint(&nd, flags | LOOKUP_REVAL, path);
Al Viro2d864652013-09-08 20:18:44 -04002459 if (likely(!error))
Al Viro668696d2015-02-22 19:44:00 -05002460 audit_inode(name, path->dentry, 0);
Al Viro9883d182015-05-13 07:28:08 -04002461 restore_nameidata();
Al Viro668696d2015-02-22 19:44:00 -05002462 putname(name);
Al Viro2d864652013-09-08 20:18:44 -04002463 return error;
2464}
2465
Jeff Layton80334262013-07-26 06:23:25 -04002466/**
Al Viro197df042013-09-08 14:03:27 -04002467 * user_path_mountpoint_at - lookup a path from userland in order to umount it
Jeff Layton80334262013-07-26 06:23:25 -04002468 * @dfd: directory file descriptor
2469 * @name: pathname from userland
2470 * @flags: lookup flags
2471 * @path: pointer to container to hold result
2472 *
2473 * A umount is a special case for path walking. We're not actually interested
2474 * in the inode in this situation, and ESTALE errors can be a problem. We
2475 * simply want track down the dentry and vfsmount attached at the mountpoint
2476 * and avoid revalidating the last component.
2477 *
2478 * Returns 0 and populates "path" on success.
2479 */
2480int
Al Viro197df042013-09-08 14:03:27 -04002481user_path_mountpoint_at(int dfd, const char __user *name, unsigned int flags,
Jeff Layton80334262013-07-26 06:23:25 -04002482 struct path *path)
2483{
Al Virocbaab2d2015-01-22 02:49:00 -05002484 return filename_mountpoint(dfd, getname(name), path, flags);
Jeff Layton80334262013-07-26 06:23:25 -04002485}
2486
Al Viro2d864652013-09-08 20:18:44 -04002487int
2488kern_path_mountpoint(int dfd, const char *name, struct path *path,
2489 unsigned int flags)
2490{
Al Virocbaab2d2015-01-22 02:49:00 -05002491 return filename_mountpoint(dfd, getname_kernel(name), path, flags);
Al Viro2d864652013-09-08 20:18:44 -04002492}
2493EXPORT_SYMBOL(kern_path_mountpoint);
2494
Miklos Szeredicbdf35b2014-10-24 00:14:36 +02002495int __check_sticky(struct inode *dir, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496{
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002497 kuid_t fsuid = current_fsuid();
David Howellsda9592e2008-11-14 10:39:05 +11002498
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002499 if (uid_eq(inode->i_uid, fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 return 0;
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002501 if (uid_eq(dir->i_uid, fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 return 0;
Andy Lutomirski23adbe12014-06-10 12:45:42 -07002503 return !capable_wrt_inode_uidgid(inode, CAP_FOWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504}
Miklos Szeredicbdf35b2014-10-24 00:14:36 +02002505EXPORT_SYMBOL(__check_sticky);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506
2507/*
2508 * Check whether we can remove a link victim from directory dir, check
2509 * whether the type of victim is right.
2510 * 1. We can't do it if dir is read-only (done in permission())
2511 * 2. We should have write and exec permissions on dir
2512 * 3. We can't remove anything from append-only dir
2513 * 4. We can't do anything with immutable dir (done in permission())
2514 * 5. If the sticky bit on dir is set we should either
2515 * a. be owner of dir, or
2516 * b. be owner of victim, or
2517 * c. have CAP_FOWNER capability
2518 * 6. If the victim is append-only or immutable we can't do antyhing with
2519 * links pointing to it.
2520 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2521 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2522 * 9. We can't remove a root or mountpoint.
2523 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
2524 * nfs_async_unlink().
2525 */
David Howellsb18825a2013-09-12 19:22:53 +01002526static int may_delete(struct inode *dir, struct dentry *victim, bool isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527{
David Howells63afdfc2015-05-06 15:59:00 +01002528 struct inode *inode = d_backing_inode(victim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 int error;
2530
David Howellsb18825a2013-09-12 19:22:53 +01002531 if (d_is_negative(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 return -ENOENT;
David Howellsb18825a2013-09-12 19:22:53 +01002533 BUG_ON(!inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534
2535 BUG_ON(victim->d_parent->d_inode != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04002536 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
Al Virof419a2e2008-07-22 00:07:17 -04002538 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 if (error)
2540 return error;
2541 if (IS_APPEND(dir))
2542 return -EPERM;
David Howellsb18825a2013-09-12 19:22:53 +01002543
2544 if (check_sticky(dir, inode) || IS_APPEND(inode) ||
2545 IS_IMMUTABLE(inode) || IS_SWAPFILE(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 return -EPERM;
2547 if (isdir) {
Miklos Szeredi44b1d532014-04-01 17:08:41 +02002548 if (!d_is_dir(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 return -ENOTDIR;
2550 if (IS_ROOT(victim))
2551 return -EBUSY;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02002552 } else if (d_is_dir(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 return -EISDIR;
2554 if (IS_DEADDIR(dir))
2555 return -ENOENT;
2556 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2557 return -EBUSY;
2558 return 0;
2559}
2560
2561/* Check whether we can create an object with dentry child in directory
2562 * dir.
2563 * 1. We can't do it if child already exists (open has special treatment for
2564 * this case, but since we are inlined it's OK)
2565 * 2. We can't do it if dir is read-only (done in permission())
2566 * 3. We should have write and exec permissions on dir
2567 * 4. We can't do it if dir is immutable (done in permission())
2568 */
Miklos Szeredia95164d2008-07-30 15:08:48 +02002569static inline int may_create(struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570{
Jeff Layton14e972b2013-05-08 10:25:58 -04002571 audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 if (child->d_inode)
2573 return -EEXIST;
2574 if (IS_DEADDIR(dir))
2575 return -ENOENT;
Al Virof419a2e2008-07-22 00:07:17 -04002576 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577}
2578
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579/*
2580 * p1 and p2 should be directories on the same fs.
2581 */
2582struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2583{
2584 struct dentry *p;
2585
2586 if (p1 == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07002587 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 return NULL;
2589 }
2590
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002591 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002593 p = d_ancestor(p2, p1);
2594 if (p) {
2595 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2596 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
2597 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 }
2599
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002600 p = d_ancestor(p1, p2);
2601 if (p) {
2602 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2603 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2604 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 }
2606
Ingo Molnarf2eace22006-07-03 00:25:05 -07002607 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Miklos Szeredid1b72cc2014-10-27 15:42:01 +01002608 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 return NULL;
2610}
Al Viro4d359502014-03-14 12:20:17 -04002611EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612
2613void unlock_rename(struct dentry *p1, struct dentry *p2)
2614{
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002615 mutex_unlock(&p1->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 if (p1 != p2) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002617 mutex_unlock(&p2->d_inode->i_mutex);
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002618 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 }
2620}
Al Viro4d359502014-03-14 12:20:17 -04002621EXPORT_SYMBOL(unlock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
Al Viro4acdaf22011-07-26 01:42:34 -04002623int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viro312b63f2012-06-10 18:09:36 -04002624 bool want_excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002626 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 if (error)
2628 return error;
2629
Al Viroacfa4382008-12-04 10:06:33 -05002630 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 return -EACCES; /* shouldn't it be ENOSYS? */
2632 mode &= S_IALLUGO;
2633 mode |= S_IFREG;
2634 error = security_inode_create(dir, dentry, mode);
2635 if (error)
2636 return error;
Al Viro312b63f2012-06-10 18:09:36 -04002637 error = dir->i_op->create(dir, dentry, mode, want_excl);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002638 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002639 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 return error;
2641}
Al Viro4d359502014-03-14 12:20:17 -04002642EXPORT_SYMBOL(vfs_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643
Al Viro73d049a2011-03-11 12:08:24 -05002644static int may_open(struct path *path, int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002646 struct dentry *dentry = path->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 struct inode *inode = dentry->d_inode;
2648 int error;
2649
Al Virobcda7652011-03-13 16:42:14 -04002650 /* O_PATH? */
2651 if (!acc_mode)
2652 return 0;
2653
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 if (!inode)
2655 return -ENOENT;
2656
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002657 switch (inode->i_mode & S_IFMT) {
2658 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002660 case S_IFDIR:
2661 if (acc_mode & MAY_WRITE)
2662 return -EISDIR;
2663 break;
2664 case S_IFBLK:
2665 case S_IFCHR:
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002666 if (path->mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002668 /*FALLTHRU*/
2669 case S_IFIFO:
2670 case S_IFSOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002672 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08002673 }
Dave Hansenb41572e2007-10-16 23:31:14 -07002674
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002675 error = inode_permission(inode, acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07002676 if (error)
2677 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05002678
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 /*
2680 * An append-only file must be opened in append mode for writing.
2681 */
2682 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05002683 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05002684 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05002686 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 }
2688
2689 /* O_NOATIME can only be set by the owner or superuser */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07002690 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
Al Viro7715b522009-12-16 03:54:00 -05002691 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692
J. Bruce Fieldsf3c7691e2011-09-21 10:58:13 -04002693 return 0;
Al Viro7715b522009-12-16 03:54:00 -05002694}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695
Jeff Laytone1181ee2010-12-07 16:19:50 -05002696static int handle_truncate(struct file *filp)
Al Viro7715b522009-12-16 03:54:00 -05002697{
Jeff Laytone1181ee2010-12-07 16:19:50 -05002698 struct path *path = &filp->f_path;
Al Viro7715b522009-12-16 03:54:00 -05002699 struct inode *inode = path->dentry->d_inode;
2700 int error = get_write_access(inode);
2701 if (error)
2702 return error;
2703 /*
2704 * Refuse to truncate files with mandatory locks held on them.
2705 */
Jeff Laytond7a06982014-03-10 09:54:15 -04002706 error = locks_verify_locked(filp);
Al Viro7715b522009-12-16 03:54:00 -05002707 if (!error)
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +09002708 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05002709 if (!error) {
2710 error = do_truncate(path->dentry, 0,
2711 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
Jeff Laytone1181ee2010-12-07 16:19:50 -05002712 filp);
Al Viro7715b522009-12-16 03:54:00 -05002713 }
2714 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04002715 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716}
2717
Dave Hansend57999e2008-02-15 14:37:27 -08002718static inline int open_to_namei_flags(int flag)
2719{
Al Viro8a5e9292011-06-25 19:15:54 -04002720 if ((flag & O_ACCMODE) == 3)
2721 flag--;
Dave Hansend57999e2008-02-15 14:37:27 -08002722 return flag;
2723}
2724
Miklos Szeredid18e9002012-06-05 15:10:17 +02002725static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode)
2726{
2727 int error = security_path_mknod(dir, dentry, mode, 0);
2728 if (error)
2729 return error;
2730
2731 error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
2732 if (error)
2733 return error;
2734
2735 return security_inode_create(dir->dentry->d_inode, dentry, mode);
2736}
2737
David Howells1acf0af2012-06-14 16:13:46 +01002738/*
2739 * Attempt to atomically look up, create and open a file from a negative
2740 * dentry.
2741 *
2742 * Returns 0 if successful. The file will have been created and attached to
2743 * @file by the filesystem calling finish_open().
2744 *
2745 * Returns 1 if the file was looked up only or didn't need creating. The
2746 * caller will need to perform the open themselves. @path will have been
2747 * updated to point to the new dentry. This may be negative.
2748 *
2749 * Returns an error code otherwise.
2750 */
Al Viro2675a4e2012-06-22 12:41:10 +04002751static int atomic_open(struct nameidata *nd, struct dentry *dentry,
2752 struct path *path, struct file *file,
2753 const struct open_flags *op,
Al Viro64894cf2012-07-31 00:53:35 +04002754 bool got_write, bool need_lookup,
Al Viro2675a4e2012-06-22 12:41:10 +04002755 int *opened)
Miklos Szeredid18e9002012-06-05 15:10:17 +02002756{
2757 struct inode *dir = nd->path.dentry->d_inode;
2758 unsigned open_flag = open_to_namei_flags(op->open_flag);
2759 umode_t mode;
2760 int error;
2761 int acc_mode;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002762 int create_error = 0;
2763 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
Miklos Szeredi116cc022013-09-16 14:52:05 +02002764 bool excl;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002765
2766 BUG_ON(dentry->d_inode);
2767
2768 /* Don't create child dentry for a dead directory. */
2769 if (unlikely(IS_DEADDIR(dir))) {
Al Viro2675a4e2012-06-22 12:41:10 +04002770 error = -ENOENT;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002771 goto out;
2772 }
2773
Miklos Szeredi62b259d2012-08-15 13:01:24 +02002774 mode = op->mode;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002775 if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
2776 mode &= ~current_umask();
2777
Miklos Szeredi116cc022013-09-16 14:52:05 +02002778 excl = (open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT);
2779 if (excl)
Miklos Szeredid18e9002012-06-05 15:10:17 +02002780 open_flag &= ~O_TRUNC;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002781
2782 /*
2783 * Checking write permission is tricky, bacuse we don't know if we are
2784 * going to actually need it: O_CREAT opens should work as long as the
2785 * file exists. But checking existence breaks atomicity. The trick is
2786 * to check access and if not granted clear O_CREAT from the flags.
2787 *
2788 * Another problem is returing the "right" error value (e.g. for an
2789 * O_EXCL open we want to return EEXIST not EROFS).
2790 */
Al Viro64894cf2012-07-31 00:53:35 +04002791 if (((open_flag & (O_CREAT | O_TRUNC)) ||
2792 (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) {
2793 if (!(open_flag & O_CREAT)) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002794 /*
2795 * No O_CREATE -> atomicity not a requirement -> fall
2796 * back to lookup + open
2797 */
2798 goto no_open;
2799 } else if (open_flag & (O_EXCL | O_TRUNC)) {
2800 /* Fall back and fail with the right error */
Al Viro64894cf2012-07-31 00:53:35 +04002801 create_error = -EROFS;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002802 goto no_open;
2803 } else {
2804 /* No side effects, safe to clear O_CREAT */
Al Viro64894cf2012-07-31 00:53:35 +04002805 create_error = -EROFS;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002806 open_flag &= ~O_CREAT;
2807 }
2808 }
2809
2810 if (open_flag & O_CREAT) {
Miklos Szeredi38227f72012-08-15 13:01:24 +02002811 error = may_o_create(&nd->path, dentry, mode);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002812 if (error) {
2813 create_error = error;
2814 if (open_flag & O_EXCL)
2815 goto no_open;
2816 open_flag &= ~O_CREAT;
2817 }
2818 }
2819
2820 if (nd->flags & LOOKUP_DIRECTORY)
2821 open_flag |= O_DIRECTORY;
2822
Al Viro30d90492012-06-22 12:40:19 +04002823 file->f_path.dentry = DENTRY_NOT_SET;
2824 file->f_path.mnt = nd->path.mnt;
2825 error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode,
Al Viro47237682012-06-10 05:01:45 -04002826 opened);
Al Virod9585272012-06-22 12:39:14 +04002827 if (error < 0) {
Al Virod9585272012-06-22 12:39:14 +04002828 if (create_error && error == -ENOENT)
2829 error = create_error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002830 goto out;
2831 }
2832
Al Virod9585272012-06-22 12:39:14 +04002833 if (error) { /* returned 1, that is */
Al Viro30d90492012-06-22 12:40:19 +04002834 if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
Al Viro2675a4e2012-06-22 12:41:10 +04002835 error = -EIO;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002836 goto out;
2837 }
Al Viro30d90492012-06-22 12:40:19 +04002838 if (file->f_path.dentry) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002839 dput(dentry);
Al Viro30d90492012-06-22 12:40:19 +04002840 dentry = file->f_path.dentry;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002841 }
Al Viro03da6332013-09-16 19:22:33 -04002842 if (*opened & FILE_CREATED)
2843 fsnotify_create(dir, dentry);
2844 if (!dentry->d_inode) {
2845 WARN_ON(*opened & FILE_CREATED);
2846 if (create_error) {
2847 error = create_error;
2848 goto out;
2849 }
2850 } else {
2851 if (excl && !(*opened & FILE_CREATED)) {
2852 error = -EEXIST;
2853 goto out;
2854 }
Sage Weil62b2ce92012-08-15 13:30:12 -07002855 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02002856 goto looked_up;
2857 }
2858
2859 /*
2860 * We didn't have the inode before the open, so check open permission
2861 * here.
2862 */
Al Viro03da6332013-09-16 19:22:33 -04002863 acc_mode = op->acc_mode;
2864 if (*opened & FILE_CREATED) {
2865 WARN_ON(!(open_flag & O_CREAT));
2866 fsnotify_create(dir, dentry);
2867 acc_mode = MAY_OPEN;
2868 }
Al Viro2675a4e2012-06-22 12:41:10 +04002869 error = may_open(&file->f_path, acc_mode, open_flag);
2870 if (error)
2871 fput(file);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002872
2873out:
2874 dput(dentry);
Al Viro2675a4e2012-06-22 12:41:10 +04002875 return error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002876
Miklos Szeredid18e9002012-06-05 15:10:17 +02002877no_open:
2878 if (need_lookup) {
Al Viro72bd8662012-06-10 17:17:17 -04002879 dentry = lookup_real(dir, dentry, nd->flags);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002880 if (IS_ERR(dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04002881 return PTR_ERR(dentry);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002882
2883 if (create_error) {
2884 int open_flag = op->open_flag;
2885
Al Viro2675a4e2012-06-22 12:41:10 +04002886 error = create_error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002887 if ((open_flag & O_EXCL)) {
2888 if (!dentry->d_inode)
2889 goto out;
2890 } else if (!dentry->d_inode) {
2891 goto out;
2892 } else if ((open_flag & O_TRUNC) &&
David Howellse36cb0b2015-01-29 12:02:35 +00002893 d_is_reg(dentry)) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002894 goto out;
2895 }
2896 /* will fail later, go on to get the right error */
2897 }
2898 }
2899looked_up:
2900 path->dentry = dentry;
2901 path->mnt = nd->path.mnt;
Al Viro2675a4e2012-06-22 12:41:10 +04002902 return 1;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002903}
2904
Nick Piggin31e6b012011-01-07 17:49:52 +11002905/*
David Howells1acf0af2012-06-14 16:13:46 +01002906 * Look up and maybe create and open the last component.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002907 *
2908 * Must be called with i_mutex held on parent.
2909 *
David Howells1acf0af2012-06-14 16:13:46 +01002910 * Returns 0 if the file was successfully atomically created (if necessary) and
2911 * opened. In this case the file will be returned attached to @file.
2912 *
2913 * Returns 1 if the file was not completely opened at this time, though lookups
2914 * and creations will have been performed and the dentry returned in @path will
2915 * be positive upon return if O_CREAT was specified. If O_CREAT wasn't
2916 * specified then a negative dentry may be returned.
2917 *
2918 * An error code is returned otherwise.
2919 *
2920 * FILE_CREATE will be set in @*opened if the dentry was created and will be
2921 * cleared otherwise prior to returning.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002922 */
Al Viro2675a4e2012-06-22 12:41:10 +04002923static int lookup_open(struct nameidata *nd, struct path *path,
2924 struct file *file,
2925 const struct open_flags *op,
Al Viro64894cf2012-07-31 00:53:35 +04002926 bool got_write, int *opened)
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002927{
2928 struct dentry *dir = nd->path.dentry;
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002929 struct inode *dir_inode = dir->d_inode;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002930 struct dentry *dentry;
2931 int error;
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002932 bool need_lookup;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002933
Al Viro47237682012-06-10 05:01:45 -04002934 *opened &= ~FILE_CREATED;
Al Viro201f9562012-06-22 12:42:10 +04002935 dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002936 if (IS_ERR(dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04002937 return PTR_ERR(dentry);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002938
Miklos Szeredid18e9002012-06-05 15:10:17 +02002939 /* Cached positive dentry: will open in f_op->open */
2940 if (!need_lookup && dentry->d_inode)
2941 goto out_no_open;
2942
2943 if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) {
Al Viro64894cf2012-07-31 00:53:35 +04002944 return atomic_open(nd, dentry, path, file, op, got_write,
Al Viro47237682012-06-10 05:01:45 -04002945 need_lookup, opened);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002946 }
2947
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002948 if (need_lookup) {
2949 BUG_ON(dentry->d_inode);
2950
Al Viro72bd8662012-06-10 17:17:17 -04002951 dentry = lookup_real(dir_inode, dentry, nd->flags);
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002952 if (IS_ERR(dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04002953 return PTR_ERR(dentry);
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002954 }
2955
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002956 /* Negative dentry, just create the file */
2957 if (!dentry->d_inode && (op->open_flag & O_CREAT)) {
2958 umode_t mode = op->mode;
2959 if (!IS_POSIXACL(dir->d_inode))
2960 mode &= ~current_umask();
2961 /*
2962 * This write is needed to ensure that a
2963 * rw->ro transition does not occur between
2964 * the time when the file is created and when
2965 * a permanent write count is taken through
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02002966 * the 'struct file' in finish_open().
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002967 */
Al Viro64894cf2012-07-31 00:53:35 +04002968 if (!got_write) {
2969 error = -EROFS;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002970 goto out_dput;
Al Viro64894cf2012-07-31 00:53:35 +04002971 }
Al Viro47237682012-06-10 05:01:45 -04002972 *opened |= FILE_CREATED;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002973 error = security_path_mknod(&nd->path, dentry, mode, 0);
2974 if (error)
2975 goto out_dput;
Al Viro312b63f2012-06-10 18:09:36 -04002976 error = vfs_create(dir->d_inode, dentry, mode,
2977 nd->flags & LOOKUP_EXCL);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002978 if (error)
2979 goto out_dput;
2980 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02002981out_no_open:
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002982 path->dentry = dentry;
2983 path->mnt = nd->path.mnt;
Al Viro2675a4e2012-06-22 12:41:10 +04002984 return 1;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002985
2986out_dput:
2987 dput(dentry);
Al Viro2675a4e2012-06-22 12:41:10 +04002988 return error;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002989}
2990
2991/*
Al Virofe2d35f2011-03-05 22:58:25 -05002992 * Handle the last step of open()
Nick Piggin31e6b012011-01-07 17:49:52 +11002993 */
Al Viro896475d2015-04-22 18:02:17 -04002994static int do_last(struct nameidata *nd,
Al Viro2675a4e2012-06-22 12:41:10 +04002995 struct file *file, const struct open_flags *op,
Al Viro76ae2a52015-05-12 18:44:32 -04002996 int *opened)
Al Virofb1cc552009-12-24 01:58:28 -05002997{
Al Viroa1e28032009-12-24 02:12:06 -05002998 struct dentry *dir = nd->path.dentry;
Al Viroca344a892011-03-09 00:36:45 -05002999 int open_flag = op->open_flag;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003000 bool will_truncate = (open_flag & O_TRUNC) != 0;
Al Viro64894cf2012-07-31 00:53:35 +04003001 bool got_write = false;
Al Virobcda7652011-03-13 16:42:14 -04003002 int acc_mode = op->acc_mode;
Al Viro254cf582015-05-05 09:40:46 -04003003 unsigned seq;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02003004 struct inode *inode;
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003005 struct path save_parent = { .dentry = NULL, .mnt = NULL };
Al Viro896475d2015-04-22 18:02:17 -04003006 struct path path;
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003007 bool retried = false;
Al Viro16c2cd72011-02-22 15:50:10 -05003008 int error;
Al Virofb1cc552009-12-24 01:58:28 -05003009
Al Viroc3e380b2011-02-23 13:39:45 -05003010 nd->flags &= ~LOOKUP_PARENT;
3011 nd->flags |= op->intent;
3012
Al Virobc77daa2013-06-06 09:12:33 -04003013 if (nd->last_type != LAST_NORM) {
Al Virofe2d35f2011-03-05 22:58:25 -05003014 error = handle_dots(nd, nd->last_type);
Al Virodeb106c2015-05-08 18:05:21 -04003015 if (unlikely(error))
Al Viro2675a4e2012-06-22 12:41:10 +04003016 return error;
Miklos Szeredie83db162012-06-05 15:10:29 +02003017 goto finish_open;
Al Viro1f36f772009-12-26 10:56:19 -05003018 }
Al Viro67ee3ad2009-12-26 07:01:01 -05003019
Al Viroca344a892011-03-09 00:36:45 -05003020 if (!(open_flag & O_CREAT)) {
Al Virofe2d35f2011-03-05 22:58:25 -05003021 if (nd->last.name[nd->last.len])
3022 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
3023 /* we _can_ be in RCU mode here */
Al Viro254cf582015-05-05 09:40:46 -04003024 error = lookup_fast(nd, &path, &inode, &seq);
Miklos Szeredi71574862012-06-05 15:10:14 +02003025 if (likely(!error))
3026 goto finish_lookup;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02003027
Miklos Szeredi71574862012-06-05 15:10:14 +02003028 if (error < 0)
Al Virodeb106c2015-05-08 18:05:21 -04003029 return error;
Miklos Szeredi37d7fff2012-06-05 15:10:12 +02003030
Miklos Szeredi71574862012-06-05 15:10:14 +02003031 BUG_ON(nd->inode != dir->d_inode);
Miklos Szeredib6183df2012-06-05 15:10:13 +02003032 } else {
3033 /* create side of things */
3034 /*
3035 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
3036 * has been cleared when we got to the last component we are
3037 * about to look up
3038 */
3039 error = complete_walk(nd);
Al Viroe8bb73d2015-05-08 16:28:42 -04003040 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003041 return error;
Miklos Szeredib6183df2012-06-05 15:10:13 +02003042
Al Viro76ae2a52015-05-12 18:44:32 -04003043 audit_inode(nd->name, dir, LOOKUP_PARENT);
Miklos Szeredib6183df2012-06-05 15:10:13 +02003044 /* trailing slashes? */
Al Virodeb106c2015-05-08 18:05:21 -04003045 if (unlikely(nd->last.name[nd->last.len]))
3046 return -EISDIR;
Al Virofe2d35f2011-03-05 22:58:25 -05003047 }
3048
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003049retry_lookup:
Al Viro64894cf2012-07-31 00:53:35 +04003050 if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
3051 error = mnt_want_write(nd->path.mnt);
3052 if (!error)
3053 got_write = true;
3054 /*
3055 * do _not_ fail yet - we might not need that or fail with
3056 * a different error; let lookup_open() decide; we'll be
3057 * dropping this one anyway.
3058 */
3059 }
Al Viroa1e28032009-12-24 02:12:06 -05003060 mutex_lock(&dir->d_inode->i_mutex);
Al Viro896475d2015-04-22 18:02:17 -04003061 error = lookup_open(nd, &path, file, op, got_write, opened);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003062 mutex_unlock(&dir->d_inode->i_mutex);
Al Viroa1e28032009-12-24 02:12:06 -05003063
Al Viro2675a4e2012-06-22 12:41:10 +04003064 if (error <= 0) {
3065 if (error)
Miklos Szeredid18e9002012-06-05 15:10:17 +02003066 goto out;
3067
Al Viro47237682012-06-10 05:01:45 -04003068 if ((*opened & FILE_CREATED) ||
Al Viro496ad9a2013-01-23 17:07:38 -05003069 !S_ISREG(file_inode(file)->i_mode))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003070 will_truncate = false;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003071
Al Viro76ae2a52015-05-12 18:44:32 -04003072 audit_inode(nd->name, file->f_path.dentry, 0);
Miklos Szeredid18e9002012-06-05 15:10:17 +02003073 goto opened;
3074 }
Al Virofb1cc552009-12-24 01:58:28 -05003075
Al Viro47237682012-06-10 05:01:45 -04003076 if (*opened & FILE_CREATED) {
Al Viro9b44f1b2011-03-09 00:17:27 -05003077 /* Don't check for write permission, don't truncate */
Al Viroca344a892011-03-09 00:36:45 -05003078 open_flag &= ~O_TRUNC;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003079 will_truncate = false;
Al Virobcda7652011-03-13 16:42:14 -04003080 acc_mode = MAY_OPEN;
Al Viro896475d2015-04-22 18:02:17 -04003081 path_to_nameidata(&path, nd);
Miklos Szeredie83db162012-06-05 15:10:29 +02003082 goto finish_open_created;
Al Virofb1cc552009-12-24 01:58:28 -05003083 }
3084
3085 /*
Jeff Layton3134f372012-07-25 10:19:47 -04003086 * create/update audit record if it already exists.
Al Virofb1cc552009-12-24 01:58:28 -05003087 */
Al Viro896475d2015-04-22 18:02:17 -04003088 if (d_is_positive(path.dentry))
Al Viro76ae2a52015-05-12 18:44:32 -04003089 audit_inode(nd->name, path.dentry, 0);
Al Virofb1cc552009-12-24 01:58:28 -05003090
Miklos Szeredid18e9002012-06-05 15:10:17 +02003091 /*
3092 * If atomic_open() acquired write access it is dropped now due to
3093 * possible mount and symlink following (this might be optimized away if
3094 * necessary...)
3095 */
Al Viro64894cf2012-07-31 00:53:35 +04003096 if (got_write) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02003097 mnt_drop_write(nd->path.mnt);
Al Viro64894cf2012-07-31 00:53:35 +04003098 got_write = false;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003099 }
3100
Al Virodeb106c2015-05-08 18:05:21 -04003101 if (unlikely((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))) {
3102 path_to_nameidata(&path, nd);
3103 return -EEXIST;
3104 }
Al Virofb1cc552009-12-24 01:58:28 -05003105
NeilBrown756daf22015-03-23 13:37:38 +11003106 error = follow_managed(&path, nd);
Al Virodeb106c2015-05-08 18:05:21 -04003107 if (unlikely(error < 0))
3108 return error;
Al Viroa3fbbde2011-11-07 21:21:26 +00003109
Miklos Szeredidecf3402012-05-21 17:30:08 +02003110 BUG_ON(nd->flags & LOOKUP_RCU);
David Howells63afdfc2015-05-06 15:59:00 +01003111 inode = d_backing_inode(path.dentry);
Al Viro254cf582015-05-05 09:40:46 -04003112 seq = 0; /* out of RCU mode, so the value doesn't matter */
Al Virodeb106c2015-05-08 18:05:21 -04003113 if (unlikely(d_is_negative(path.dentry))) {
Al Viro896475d2015-04-22 18:02:17 -04003114 path_to_nameidata(&path, nd);
Al Virodeb106c2015-05-08 18:05:21 -04003115 return -ENOENT;
Miklos Szeredi54c33e72012-05-21 17:30:10 +02003116 }
Al Viro766c4cb2015-05-07 19:24:57 -04003117finish_lookup:
Al Virod63ff282015-05-04 18:13:23 -04003118 if (nd->depth)
3119 put_link(nd);
Al Viro181548c2015-05-07 19:54:34 -04003120 error = should_follow_link(nd, &path, nd->flags & LOOKUP_FOLLOW,
3121 inode, seq);
Al Virodeb106c2015-05-08 18:05:21 -04003122 if (unlikely(error))
Al Virod63ff282015-05-04 18:13:23 -04003123 return error;
Al Virofb1cc552009-12-24 01:58:28 -05003124
Al Viro896475d2015-04-22 18:02:17 -04003125 if (unlikely(d_is_symlink(path.dentry)) && !(open_flag & O_PATH)) {
3126 path_to_nameidata(&path, nd);
Al Virodeb106c2015-05-08 18:05:21 -04003127 return -ELOOP;
Al Viroa5cfe2d2015-04-22 12:10:45 -04003128 }
3129
Al Viro896475d2015-04-22 18:02:17 -04003130 if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path.mnt) {
3131 path_to_nameidata(&path, nd);
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003132 } else {
3133 save_parent.dentry = nd->path.dentry;
Al Viro896475d2015-04-22 18:02:17 -04003134 save_parent.mnt = mntget(path.mnt);
3135 nd->path.dentry = path.dentry;
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003136
3137 }
Miklos Szeredidecf3402012-05-21 17:30:08 +02003138 nd->inode = inode;
Al Viro254cf582015-05-05 09:40:46 -04003139 nd->seq = seq;
Al Viroa3fbbde2011-11-07 21:21:26 +00003140 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
Al Virobc77daa2013-06-06 09:12:33 -04003141finish_open:
Al Viroa3fbbde2011-11-07 21:21:26 +00003142 error = complete_walk(nd);
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003143 if (error) {
3144 path_put(&save_parent);
Al Viro2675a4e2012-06-22 12:41:10 +04003145 return error;
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003146 }
Al Viro76ae2a52015-05-12 18:44:32 -04003147 audit_inode(nd->name, nd->path.dentry, 0);
Al Virofb1cc552009-12-24 01:58:28 -05003148 error = -EISDIR;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02003149 if ((open_flag & O_CREAT) && d_is_dir(nd->path.dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04003150 goto out;
Miklos Szerediaf2f5542012-05-21 17:30:11 +02003151 error = -ENOTDIR;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02003152 if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04003153 goto out;
David Howells4bbcbd32015-03-17 22:16:40 +00003154 if (!d_is_reg(nd->path.dentry))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003155 will_truncate = false;
Al Viro6c0d46c2011-03-09 00:59:59 -05003156
Al Viro0f9d1a12011-03-09 00:13:14 -05003157 if (will_truncate) {
3158 error = mnt_want_write(nd->path.mnt);
3159 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003160 goto out;
Al Viro64894cf2012-07-31 00:53:35 +04003161 got_write = true;
Al Viro0f9d1a12011-03-09 00:13:14 -05003162 }
Miklos Szeredie83db162012-06-05 15:10:29 +02003163finish_open_created:
Al Virobcda7652011-03-13 16:42:14 -04003164 error = may_open(&nd->path, acc_mode, open_flag);
Al Viroca344a892011-03-09 00:36:45 -05003165 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003166 goto out;
Miklos Szeredi4aa7c632014-10-24 00:14:35 +02003167
3168 BUG_ON(*opened & FILE_OPENED); /* once it's opened, it's opened */
3169 error = vfs_open(&nd->path, file, current_cred());
3170 if (!error) {
3171 *opened |= FILE_OPENED;
3172 } else {
Al Viro30d90492012-06-22 12:40:19 +04003173 if (error == -EOPENSTALE)
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003174 goto stale_open;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003175 goto out;
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003176 }
Miklos Szeredia8277b92012-06-05 15:10:32 +02003177opened:
Al Viro2675a4e2012-06-22 12:41:10 +04003178 error = open_check_o_direct(file);
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003179 if (error)
3180 goto exit_fput;
Dmitry Kasatkin3034a142014-06-27 18:15:44 +03003181 error = ima_file_check(file, op->acc_mode, *opened);
Miklos Szerediaa4caad2012-06-05 15:10:28 +02003182 if (error)
3183 goto exit_fput;
3184
3185 if (will_truncate) {
Al Viro2675a4e2012-06-22 12:41:10 +04003186 error = handle_truncate(file);
Miklos Szerediaa4caad2012-06-05 15:10:28 +02003187 if (error)
3188 goto exit_fput;
Al Viro0f9d1a12011-03-09 00:13:14 -05003189 }
Al Viroca344a892011-03-09 00:36:45 -05003190out:
Al Viro64894cf2012-07-31 00:53:35 +04003191 if (got_write)
Al Viro0f9d1a12011-03-09 00:13:14 -05003192 mnt_drop_write(nd->path.mnt);
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003193 path_put(&save_parent);
Al Viro2675a4e2012-06-22 12:41:10 +04003194 return error;
Al Virofb1cc552009-12-24 01:58:28 -05003195
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003196exit_fput:
Al Viro2675a4e2012-06-22 12:41:10 +04003197 fput(file);
3198 goto out;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003199
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003200stale_open:
3201 /* If no saved parent or already retried then can't retry */
3202 if (!save_parent.dentry || retried)
3203 goto out;
3204
3205 BUG_ON(save_parent.dentry != dir);
3206 path_put(&nd->path);
3207 nd->path = save_parent;
3208 nd->inode = dir->d_inode;
3209 save_parent.mnt = NULL;
3210 save_parent.dentry = NULL;
Al Viro64894cf2012-07-31 00:53:35 +04003211 if (got_write) {
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003212 mnt_drop_write(nd->path.mnt);
Al Viro64894cf2012-07-31 00:53:35 +04003213 got_write = false;
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003214 }
3215 retried = true;
3216 goto retry_lookup;
Al Virofb1cc552009-12-24 01:58:28 -05003217}
3218
Al Viroc8a53ee2015-05-12 18:43:07 -04003219static int do_tmpfile(struct nameidata *nd, unsigned flags,
Al Viro60545d02013-06-07 01:20:27 -04003220 const struct open_flags *op,
3221 struct file *file, int *opened)
3222{
3223 static const struct qstr name = QSTR_INIT("/", 1);
Al Viro625b6d12015-05-12 16:36:12 -04003224 struct dentry *child;
Al Viro60545d02013-06-07 01:20:27 -04003225 struct inode *dir;
Al Viro625b6d12015-05-12 16:36:12 -04003226 struct path path;
Al Viroc8a53ee2015-05-12 18:43:07 -04003227 int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path);
Al Viro60545d02013-06-07 01:20:27 -04003228 if (unlikely(error))
3229 return error;
Al Viro625b6d12015-05-12 16:36:12 -04003230 error = mnt_want_write(path.mnt);
Al Viro60545d02013-06-07 01:20:27 -04003231 if (unlikely(error))
3232 goto out;
Al Viro625b6d12015-05-12 16:36:12 -04003233 dir = path.dentry->d_inode;
Al Viro60545d02013-06-07 01:20:27 -04003234 /* we want directory to be writable */
Al Viro625b6d12015-05-12 16:36:12 -04003235 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
Al Viro60545d02013-06-07 01:20:27 -04003236 if (error)
3237 goto out2;
Al Viro60545d02013-06-07 01:20:27 -04003238 if (!dir->i_op->tmpfile) {
3239 error = -EOPNOTSUPP;
3240 goto out2;
3241 }
Al Viro625b6d12015-05-12 16:36:12 -04003242 child = d_alloc(path.dentry, &name);
Al Viro60545d02013-06-07 01:20:27 -04003243 if (unlikely(!child)) {
3244 error = -ENOMEM;
3245 goto out2;
3246 }
Al Viro625b6d12015-05-12 16:36:12 -04003247 dput(path.dentry);
3248 path.dentry = child;
3249 error = dir->i_op->tmpfile(dir, child, op->mode);
Al Viro60545d02013-06-07 01:20:27 -04003250 if (error)
3251 goto out2;
Al Viroc8a53ee2015-05-12 18:43:07 -04003252 audit_inode(nd->name, child, 0);
Eric Rannaud69a91c22014-10-30 01:51:01 -07003253 /* Don't check for other permissions, the inode was just created */
Al Viro625b6d12015-05-12 16:36:12 -04003254 error = may_open(&path, MAY_OPEN, op->open_flag);
Al Viro60545d02013-06-07 01:20:27 -04003255 if (error)
3256 goto out2;
Al Viro625b6d12015-05-12 16:36:12 -04003257 file->f_path.mnt = path.mnt;
3258 error = finish_open(file, child, NULL, opened);
Al Viro60545d02013-06-07 01:20:27 -04003259 if (error)
3260 goto out2;
3261 error = open_check_o_direct(file);
Al Virof4e0c302013-06-11 08:34:36 +04003262 if (error) {
Al Viro60545d02013-06-07 01:20:27 -04003263 fput(file);
Al Virof4e0c302013-06-11 08:34:36 +04003264 } else if (!(op->open_flag & O_EXCL)) {
3265 struct inode *inode = file_inode(file);
3266 spin_lock(&inode->i_lock);
3267 inode->i_state |= I_LINKABLE;
3268 spin_unlock(&inode->i_lock);
3269 }
Al Viro60545d02013-06-07 01:20:27 -04003270out2:
Al Viro625b6d12015-05-12 16:36:12 -04003271 mnt_drop_write(path.mnt);
Al Viro60545d02013-06-07 01:20:27 -04003272out:
Al Viro625b6d12015-05-12 16:36:12 -04003273 path_put(&path);
Al Viro60545d02013-06-07 01:20:27 -04003274 return error;
3275}
3276
Al Viroc8a53ee2015-05-12 18:43:07 -04003277static struct file *path_openat(struct nameidata *nd,
3278 const struct open_flags *op, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279{
Al Viro368ee9b2015-05-08 17:19:59 -04003280 const char *s;
Al Viro30d90492012-06-22 12:40:19 +04003281 struct file *file;
Al Viro47237682012-06-10 05:01:45 -04003282 int opened = 0;
Al Viro13aab422011-02-23 17:54:08 -05003283 int error;
Nick Piggin31e6b012011-01-07 17:49:52 +11003284
Al Viro30d90492012-06-22 12:40:19 +04003285 file = get_empty_filp();
Al Viro1afc99b2013-02-14 20:41:04 -05003286 if (IS_ERR(file))
3287 return file;
Nick Piggin31e6b012011-01-07 17:49:52 +11003288
Al Viro30d90492012-06-22 12:40:19 +04003289 file->f_flags = op->open_flag;
Nick Piggin31e6b012011-01-07 17:49:52 +11003290
Al Virobb458c62013-07-13 13:26:37 +04003291 if (unlikely(file->f_flags & __O_TMPFILE)) {
Al Viroc8a53ee2015-05-12 18:43:07 -04003292 error = do_tmpfile(nd, flags, op, file, &opened);
Al Virof15133d2015-05-08 22:53:15 -04003293 goto out2;
Al Viro60545d02013-06-07 01:20:27 -04003294 }
3295
Al Viroc8a53ee2015-05-12 18:43:07 -04003296 s = path_init(nd, flags);
Al Viro368ee9b2015-05-08 17:19:59 -04003297 if (IS_ERR(s)) {
3298 put_filp(file);
3299 return ERR_CAST(s);
3300 }
Al Viro3bdba282015-05-08 17:37:07 -04003301 while (!(error = link_path_walk(s, nd)) &&
Al Viro76ae2a52015-05-12 18:44:32 -04003302 (error = do_last(nd, file, op, &opened)) > 0) {
Al Viro73d049a2011-03-11 12:08:24 -05003303 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
Al Viro3bdba282015-05-08 17:37:07 -04003304 s = trailing_symlink(nd);
3305 if (IS_ERR(s)) {
3306 error = PTR_ERR(s);
Al Viro2675a4e2012-06-22 12:41:10 +04003307 break;
Al Viro3bdba282015-05-08 17:37:07 -04003308 }
Al Viro806b6812009-12-26 07:16:40 -05003309 }
Al Virodeb106c2015-05-08 18:05:21 -04003310 terminate_walk(nd);
Al Virof15133d2015-05-08 22:53:15 -04003311out2:
Al Viro2675a4e2012-06-22 12:41:10 +04003312 if (!(opened & FILE_OPENED)) {
3313 BUG_ON(!error);
Al Viro30d90492012-06-22 12:40:19 +04003314 put_filp(file);
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003315 }
Al Viro2675a4e2012-06-22 12:41:10 +04003316 if (unlikely(error)) {
3317 if (error == -EOPENSTALE) {
3318 if (flags & LOOKUP_RCU)
3319 error = -ECHILD;
3320 else
3321 error = -ESTALE;
3322 }
3323 file = ERR_PTR(error);
3324 }
3325 return file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326}
3327
Jeff Layton669abf42012-10-10 16:43:10 -04003328struct file *do_filp_open(int dfd, struct filename *pathname,
Al Virof9652e12013-06-11 08:23:01 +04003329 const struct open_flags *op)
Al Viro13aab422011-02-23 17:54:08 -05003330{
Al Viro9883d182015-05-13 07:28:08 -04003331 struct nameidata nd;
Al Virof9652e12013-06-11 08:23:01 +04003332 int flags = op->lookup_flags;
Al Viro13aab422011-02-23 17:54:08 -05003333 struct file *filp;
3334
Al Viro9883d182015-05-13 07:28:08 -04003335 set_nameidata(&nd, dfd, pathname);
Al Viroc8a53ee2015-05-12 18:43:07 -04003336 filp = path_openat(&nd, op, flags | LOOKUP_RCU);
Al Viro13aab422011-02-23 17:54:08 -05003337 if (unlikely(filp == ERR_PTR(-ECHILD)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003338 filp = path_openat(&nd, op, flags);
Al Viro13aab422011-02-23 17:54:08 -05003339 if (unlikely(filp == ERR_PTR(-ESTALE)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003340 filp = path_openat(&nd, op, flags | LOOKUP_REVAL);
Al Viro9883d182015-05-13 07:28:08 -04003341 restore_nameidata();
Al Viro13aab422011-02-23 17:54:08 -05003342 return filp;
3343}
3344
Al Viro73d049a2011-03-11 12:08:24 -05003345struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
Al Virof9652e12013-06-11 08:23:01 +04003346 const char *name, const struct open_flags *op)
Al Viro73d049a2011-03-11 12:08:24 -05003347{
Al Viro9883d182015-05-13 07:28:08 -04003348 struct nameidata nd;
Al Viro73d049a2011-03-11 12:08:24 -05003349 struct file *file;
Paul Moore51689102015-01-22 00:00:03 -05003350 struct filename *filename;
Al Virof9652e12013-06-11 08:23:01 +04003351 int flags = op->lookup_flags | LOOKUP_ROOT;
Al Viro73d049a2011-03-11 12:08:24 -05003352
3353 nd.root.mnt = mnt;
3354 nd.root.dentry = dentry;
3355
David Howellsb18825a2013-09-12 19:22:53 +01003356 if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN)
Al Viro73d049a2011-03-11 12:08:24 -05003357 return ERR_PTR(-ELOOP);
3358
Paul Moore51689102015-01-22 00:00:03 -05003359 filename = getname_kernel(name);
3360 if (unlikely(IS_ERR(filename)))
3361 return ERR_CAST(filename);
3362
Al Viro9883d182015-05-13 07:28:08 -04003363 set_nameidata(&nd, -1, filename);
Al Viroc8a53ee2015-05-12 18:43:07 -04003364 file = path_openat(&nd, op, flags | LOOKUP_RCU);
Al Viro73d049a2011-03-11 12:08:24 -05003365 if (unlikely(file == ERR_PTR(-ECHILD)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003366 file = path_openat(&nd, op, flags);
Al Viro73d049a2011-03-11 12:08:24 -05003367 if (unlikely(file == ERR_PTR(-ESTALE)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003368 file = path_openat(&nd, op, flags | LOOKUP_REVAL);
Al Viro9883d182015-05-13 07:28:08 -04003369 restore_nameidata();
Paul Moore51689102015-01-22 00:00:03 -05003370 putname(filename);
Al Viro73d049a2011-03-11 12:08:24 -05003371 return file;
3372}
3373
Al Virofa14a0b2015-01-22 02:16:49 -05003374static struct dentry *filename_create(int dfd, struct filename *name,
Jeff Layton1ac12b42012-12-11 12:10:06 -05003375 struct path *path, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003377 struct dentry *dentry = ERR_PTR(-EEXIST);
Al Viro391172c2015-05-09 11:19:16 -04003378 struct qstr last;
3379 int type;
Jan Karac30dabf2012-06-12 16:20:30 +02003380 int err2;
Jeff Layton1ac12b42012-12-11 12:10:06 -05003381 int error;
3382 bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
3383
3384 /*
3385 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
3386 * other flags passed in are ignored!
3387 */
3388 lookup_flags &= LOOKUP_REVAL;
3389
Al Viro5c31b6c2015-05-12 17:32:54 -04003390 name = filename_parentat(dfd, name, lookup_flags, path, &last, &type);
3391 if (IS_ERR(name))
3392 return ERR_CAST(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003394 /*
3395 * Yucky last component or no last component at all?
3396 * (foo/., foo/.., /////)
3397 */
Al Viro5c31b6c2015-05-12 17:32:54 -04003398 if (unlikely(type != LAST_NORM))
Al Viroed75e952011-06-27 16:53:43 -04003399 goto out;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003400
Jan Karac30dabf2012-06-12 16:20:30 +02003401 /* don't fail immediately if it's r/o, at least try to report other errors */
Al Viro391172c2015-05-09 11:19:16 -04003402 err2 = mnt_want_write(path->mnt);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003403 /*
3404 * Do the final lookup.
3405 */
Al Viro391172c2015-05-09 11:19:16 -04003406 lookup_flags |= LOOKUP_CREATE | LOOKUP_EXCL;
3407 mutex_lock_nested(&path->dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3408 dentry = __lookup_hash(&last, path->dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 if (IS_ERR(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003410 goto unlock;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003411
Al Viroa8104a92012-07-20 02:25:00 +04003412 error = -EEXIST;
David Howellsb18825a2013-09-12 19:22:53 +01003413 if (d_is_positive(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003414 goto fail;
David Howellsb18825a2013-09-12 19:22:53 +01003415
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003416 /*
3417 * Special case - lookup gave negative, but... we had foo/bar/
3418 * From the vfs_mknod() POV we just have a negative dentry -
3419 * all is fine. Let's be bastards - you had / on the end, you've
3420 * been asking for (non-existent) directory. -ENOENT for you.
3421 */
Al Viro391172c2015-05-09 11:19:16 -04003422 if (unlikely(!is_dir && last.name[last.len])) {
Al Viroa8104a92012-07-20 02:25:00 +04003423 error = -ENOENT;
Al Viroed75e952011-06-27 16:53:43 -04003424 goto fail;
Al Viroe9baf6e2008-05-15 04:49:12 -04003425 }
Jan Karac30dabf2012-06-12 16:20:30 +02003426 if (unlikely(err2)) {
3427 error = err2;
Al Viroa8104a92012-07-20 02:25:00 +04003428 goto fail;
Jan Karac30dabf2012-06-12 16:20:30 +02003429 }
Al Viro181c37b2015-05-12 17:21:25 -04003430 putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432fail:
Al Viroa8104a92012-07-20 02:25:00 +04003433 dput(dentry);
3434 dentry = ERR_PTR(error);
3435unlock:
Al Viro391172c2015-05-09 11:19:16 -04003436 mutex_unlock(&path->dentry->d_inode->i_mutex);
Jan Karac30dabf2012-06-12 16:20:30 +02003437 if (!err2)
Al Viro391172c2015-05-09 11:19:16 -04003438 mnt_drop_write(path->mnt);
Al Viroed75e952011-06-27 16:53:43 -04003439out:
Al Viro391172c2015-05-09 11:19:16 -04003440 path_put(path);
Al Viro181c37b2015-05-12 17:21:25 -04003441 putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 return dentry;
3443}
Al Virofa14a0b2015-01-22 02:16:49 -05003444
3445struct dentry *kern_path_create(int dfd, const char *pathname,
3446 struct path *path, unsigned int lookup_flags)
3447{
Al Viro181c37b2015-05-12 17:21:25 -04003448 return filename_create(dfd, getname_kernel(pathname),
3449 path, lookup_flags);
Al Virofa14a0b2015-01-22 02:16:49 -05003450}
Al Virodae6ad82011-06-26 11:50:15 -04003451EXPORT_SYMBOL(kern_path_create);
3452
Al Viro921a1652012-07-20 01:15:31 +04003453void done_path_create(struct path *path, struct dentry *dentry)
3454{
3455 dput(dentry);
3456 mutex_unlock(&path->dentry->d_inode->i_mutex);
Al Viroa8104a92012-07-20 02:25:00 +04003457 mnt_drop_write(path->mnt);
Al Viro921a1652012-07-20 01:15:31 +04003458 path_put(path);
3459}
3460EXPORT_SYMBOL(done_path_create);
3461
Al Viro520ae682015-05-13 07:00:28 -04003462inline struct dentry *user_path_create(int dfd, const char __user *pathname,
Jeff Layton1ac12b42012-12-11 12:10:06 -05003463 struct path *path, unsigned int lookup_flags)
Al Virodae6ad82011-06-26 11:50:15 -04003464{
Al Viro181c37b2015-05-12 17:21:25 -04003465 return filename_create(dfd, getname(pathname), path, lookup_flags);
Al Virodae6ad82011-06-26 11:50:15 -04003466}
3467EXPORT_SYMBOL(user_path_create);
3468
Al Viro1a67aaf2011-07-26 01:52:52 -04003469int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003471 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472
3473 if (error)
3474 return error;
3475
Eric W. Biederman975d6b32011-11-13 12:16:43 -08003476 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 return -EPERM;
3478
Al Viroacfa4382008-12-04 10:06:33 -05003479 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 return -EPERM;
3481
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07003482 error = devcgroup_inode_mknod(mode, dev);
3483 if (error)
3484 return error;
3485
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 error = security_inode_mknod(dir, dentry, mode, dev);
3487 if (error)
3488 return error;
3489
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003491 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003492 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 return error;
3494}
Al Viro4d359502014-03-14 12:20:17 -04003495EXPORT_SYMBOL(vfs_mknod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496
Al Virof69aac02011-07-26 04:31:40 -04003497static int may_mknod(umode_t mode)
Dave Hansen463c3192008-02-15 14:37:57 -08003498{
3499 switch (mode & S_IFMT) {
3500 case S_IFREG:
3501 case S_IFCHR:
3502 case S_IFBLK:
3503 case S_IFIFO:
3504 case S_IFSOCK:
3505 case 0: /* zero mode translates to S_IFREG */
3506 return 0;
3507 case S_IFDIR:
3508 return -EPERM;
3509 default:
3510 return -EINVAL;
3511 }
3512}
3513
Al Viro8208a222011-07-25 17:32:17 -04003514SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003515 unsigned, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516{
Al Viro2ad94ae2008-07-21 09:32:51 -04003517 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003518 struct path path;
3519 int error;
Jeff Layton972567f2012-12-20 16:00:10 -05003520 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521
Al Viro8e4bfca2012-07-20 01:17:26 +04003522 error = may_mknod(mode);
3523 if (error)
3524 return error;
Jeff Layton972567f2012-12-20 16:00:10 -05003525retry:
3526 dentry = user_path_create(dfd, filename, &path, lookup_flags);
Al Virodae6ad82011-06-26 11:50:15 -04003527 if (IS_ERR(dentry))
3528 return PTR_ERR(dentry);
Al Viro2ad94ae2008-07-21 09:32:51 -04003529
Al Virodae6ad82011-06-26 11:50:15 -04003530 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003531 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003532 error = security_path_mknod(&path, dentry, mode, dev);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003533 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04003534 goto out;
Dave Hansen463c3192008-02-15 14:37:57 -08003535 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 case 0: case S_IFREG:
Al Viro312b63f2012-06-10 18:09:36 -04003537 error = vfs_create(path.dentry->d_inode,dentry,mode,true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 break;
3539 case S_IFCHR: case S_IFBLK:
Al Virodae6ad82011-06-26 11:50:15 -04003540 error = vfs_mknod(path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 new_decode_dev(dev));
3542 break;
3543 case S_IFIFO: case S_IFSOCK:
Al Virodae6ad82011-06-26 11:50:15 -04003544 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 }
Al Viroa8104a92012-07-20 02:25:00 +04003547out:
Al Viro921a1652012-07-20 01:15:31 +04003548 done_path_create(&path, dentry);
Jeff Layton972567f2012-12-20 16:00:10 -05003549 if (retry_estale(error, lookup_flags)) {
3550 lookup_flags |= LOOKUP_REVAL;
3551 goto retry;
3552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 return error;
3554}
3555
Al Viro8208a222011-07-25 17:32:17 -04003556SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003557{
3558 return sys_mknodat(AT_FDCWD, filename, mode, dev);
3559}
3560
Al Viro18bb1db2011-07-26 01:41:39 -04003561int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003563 int error = may_create(dir, dentry);
Al Viro8de52772012-02-06 12:45:27 -05003564 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565
3566 if (error)
3567 return error;
3568
Al Viroacfa4382008-12-04 10:06:33 -05003569 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 return -EPERM;
3571
3572 mode &= (S_IRWXUGO|S_ISVTX);
3573 error = security_inode_mkdir(dir, dentry, mode);
3574 if (error)
3575 return error;
3576
Al Viro8de52772012-02-06 12:45:27 -05003577 if (max_links && dir->i_nlink >= max_links)
3578 return -EMLINK;
3579
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003581 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003582 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 return error;
3584}
Al Viro4d359502014-03-14 12:20:17 -04003585EXPORT_SYMBOL(vfs_mkdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586
Al Viroa218d0f2011-11-21 14:59:34 -05003587SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588{
Dave Hansen6902d922006-09-30 23:29:01 -07003589 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003590 struct path path;
3591 int error;
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003592 unsigned int lookup_flags = LOOKUP_DIRECTORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003594retry:
3595 dentry = user_path_create(dfd, pathname, &path, lookup_flags);
Dave Hansen6902d922006-09-30 23:29:01 -07003596 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003597 return PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003598
Al Virodae6ad82011-06-26 11:50:15 -04003599 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003600 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003601 error = security_path_mkdir(&path, dentry, mode);
Al Viroa8104a92012-07-20 02:25:00 +04003602 if (!error)
3603 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
Al Viro921a1652012-07-20 01:15:31 +04003604 done_path_create(&path, dentry);
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003605 if (retry_estale(error, lookup_flags)) {
3606 lookup_flags |= LOOKUP_REVAL;
3607 goto retry;
3608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 return error;
3610}
3611
Al Viroa218d0f2011-11-21 14:59:34 -05003612SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003613{
3614 return sys_mkdirat(AT_FDCWD, pathname, mode);
3615}
3616
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617/*
Sage Weila71905f2011-05-24 13:06:08 -07003618 * The dentry_unhash() helper will try to drop the dentry early: we
J. Bruce Fieldsc0d02592012-02-15 11:48:40 -05003619 * should have a usage count of 1 if we're the only user of this
Sage Weila71905f2011-05-24 13:06:08 -07003620 * dentry, and if that is true (possibly after pruning the dcache),
3621 * then we drop the dentry now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 *
3623 * A low-level filesystem can, if it choses, legally
3624 * do a
3625 *
3626 * if (!d_unhashed(dentry))
3627 * return -EBUSY;
3628 *
3629 * if it cannot handle the case of removing a directory
3630 * that is still in use by something else..
3631 */
3632void dentry_unhash(struct dentry *dentry)
3633{
Vasily Averindc168422006-12-06 20:37:07 -08003634 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 spin_lock(&dentry->d_lock);
Waiman Long98474232013-08-28 18:24:59 -07003636 if (dentry->d_lockref.count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 __d_drop(dentry);
3638 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639}
Al Viro4d359502014-03-14 12:20:17 -04003640EXPORT_SYMBOL(dentry_unhash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641
3642int vfs_rmdir(struct inode *dir, struct dentry *dentry)
3643{
3644 int error = may_delete(dir, dentry, 1);
3645
3646 if (error)
3647 return error;
3648
Al Viroacfa4382008-12-04 10:06:33 -05003649 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 return -EPERM;
3651
Al Viro1d2ef592011-09-14 18:55:41 +01003652 dget(dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003653 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654
Sage Weil912dbc12011-05-24 13:06:11 -07003655 error = -EBUSY;
Eric W. Biederman7af13642013-10-04 19:15:13 -07003656 if (is_local_mountpoint(dentry))
Sage Weil912dbc12011-05-24 13:06:11 -07003657 goto out;
3658
3659 error = security_inode_rmdir(dir, dentry);
3660 if (error)
3661 goto out;
3662
Sage Weil3cebde22011-05-29 21:20:59 -07003663 shrink_dcache_parent(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003664 error = dir->i_op->rmdir(dir, dentry);
3665 if (error)
3666 goto out;
3667
3668 dentry->d_inode->i_flags |= S_DEAD;
3669 dont_mount(dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07003670 detach_mounts(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003671
3672out:
3673 mutex_unlock(&dentry->d_inode->i_mutex);
Al Viro1d2ef592011-09-14 18:55:41 +01003674 dput(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003675 if (!error)
3676 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 return error;
3678}
Al Viro4d359502014-03-14 12:20:17 -04003679EXPORT_SYMBOL(vfs_rmdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003681static long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682{
3683 int error = 0;
Jeff Layton91a27b22012-10-10 15:25:28 -04003684 struct filename *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 struct dentry *dentry;
Al Virof5beed72015-04-30 16:09:11 -04003686 struct path path;
3687 struct qstr last;
3688 int type;
Jeff Laytonc6ee9202012-12-20 16:28:33 -05003689 unsigned int lookup_flags = 0;
3690retry:
Al Virof5beed72015-04-30 16:09:11 -04003691 name = user_path_parent(dfd, pathname,
3692 &path, &last, &type, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04003693 if (IS_ERR(name))
3694 return PTR_ERR(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695
Al Virof5beed72015-04-30 16:09:11 -04003696 switch (type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003697 case LAST_DOTDOT:
3698 error = -ENOTEMPTY;
3699 goto exit1;
3700 case LAST_DOT:
3701 error = -EINVAL;
3702 goto exit1;
3703 case LAST_ROOT:
3704 error = -EBUSY;
3705 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003707
Al Virof5beed72015-04-30 16:09:11 -04003708 error = mnt_want_write(path.mnt);
Jan Karac30dabf2012-06-12 16:20:30 +02003709 if (error)
3710 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003711
Al Virof5beed72015-04-30 16:09:11 -04003712 mutex_lock_nested(&path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3713 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003715 if (IS_ERR(dentry))
3716 goto exit2;
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04003717 if (!dentry->d_inode) {
3718 error = -ENOENT;
3719 goto exit3;
3720 }
Al Virof5beed72015-04-30 16:09:11 -04003721 error = security_path_rmdir(&path, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003722 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02003723 goto exit3;
Al Virof5beed72015-04-30 16:09:11 -04003724 error = vfs_rmdir(path.dentry->d_inode, dentry);
Dave Hansen06227532008-02-15 14:37:34 -08003725exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07003726 dput(dentry);
3727exit2:
Al Virof5beed72015-04-30 16:09:11 -04003728 mutex_unlock(&path.dentry->d_inode->i_mutex);
3729 mnt_drop_write(path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730exit1:
Al Virof5beed72015-04-30 16:09:11 -04003731 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 putname(name);
Jeff Laytonc6ee9202012-12-20 16:28:33 -05003733 if (retry_estale(error, lookup_flags)) {
3734 lookup_flags |= LOOKUP_REVAL;
3735 goto retry;
3736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737 return error;
3738}
3739
Heiko Carstens3cdad422009-01-14 14:14:22 +01003740SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003741{
3742 return do_rmdir(AT_FDCWD, pathname);
3743}
3744
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003745/**
3746 * vfs_unlink - unlink a filesystem object
3747 * @dir: parent directory
3748 * @dentry: victim
3749 * @delegated_inode: returns victim inode, if the inode is delegated.
3750 *
3751 * The caller must hold dir->i_mutex.
3752 *
3753 * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
3754 * return a reference to the inode in delegated_inode. The caller
3755 * should then break the delegation on that inode and retry. Because
3756 * breaking a delegation may take a long time, the caller should drop
3757 * dir->i_mutex before doing so.
3758 *
3759 * Alternatively, a caller may pass NULL for delegated_inode. This may
3760 * be appropriate for callers that expect the underlying filesystem not
3761 * to be NFS exported.
3762 */
3763int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764{
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003765 struct inode *target = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 int error = may_delete(dir, dentry, 0);
3767
3768 if (error)
3769 return error;
3770
Al Viroacfa4382008-12-04 10:06:33 -05003771 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 return -EPERM;
3773
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003774 mutex_lock(&target->i_mutex);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07003775 if (is_local_mountpoint(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 error = -EBUSY;
3777 else {
3778 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05003779 if (!error) {
J. Bruce Fields5a146962012-08-28 07:50:40 -07003780 error = try_break_deleg(target, delegated_inode);
3781 if (error)
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003782 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 error = dir->i_op->unlink(dir, dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07003784 if (!error) {
Al Virod83c49f2010-04-30 17:17:09 -04003785 dont_mount(dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07003786 detach_mounts(dentry);
3787 }
Al Virobec10522010-03-03 14:12:08 -05003788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 }
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003790out:
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003791 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792
3793 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
3794 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003795 fsnotify_link_count(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 }
Robert Love0eeca282005-07-12 17:06:03 -04003798
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 return error;
3800}
Al Viro4d359502014-03-14 12:20:17 -04003801EXPORT_SYMBOL(vfs_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802
3803/*
3804 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003805 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 * writeout happening, and we don't want to prevent access to the directory
3807 * while waiting on the I/O.
3808 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003809static long do_unlinkat(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810{
Al Viro2ad94ae2008-07-21 09:32:51 -04003811 int error;
Jeff Layton91a27b22012-10-10 15:25:28 -04003812 struct filename *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 struct dentry *dentry;
Al Virof5beed72015-04-30 16:09:11 -04003814 struct path path;
3815 struct qstr last;
3816 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 struct inode *inode = NULL;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003818 struct inode *delegated_inode = NULL;
Jeff Layton5d18f812012-12-20 16:38:04 -05003819 unsigned int lookup_flags = 0;
3820retry:
Al Virof5beed72015-04-30 16:09:11 -04003821 name = user_path_parent(dfd, pathname,
3822 &path, &last, &type, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04003823 if (IS_ERR(name))
3824 return PTR_ERR(name);
Al Viro2ad94ae2008-07-21 09:32:51 -04003825
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 error = -EISDIR;
Al Virof5beed72015-04-30 16:09:11 -04003827 if (type != LAST_NORM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 goto exit1;
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;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003833retry_deleg:
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);
3837 if (!IS_ERR(dentry)) {
3838 /* Why not before? Because we want correct error value */
Al Virof5beed72015-04-30 16:09:11 -04003839 if (last.name[last.len])
Török Edwin50338b82011-06-16 00:06:14 +03003840 goto slashes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 inode = dentry->d_inode;
David Howellsb18825a2013-09-12 19:22:53 +01003842 if (d_is_negative(dentry))
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04003843 goto slashes;
3844 ihold(inode);
Al Virof5beed72015-04-30 16:09:11 -04003845 error = security_path_unlink(&path, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003846 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02003847 goto exit2;
Al Virof5beed72015-04-30 16:09:11 -04003848 error = vfs_unlink(path.dentry->d_inode, dentry, &delegated_inode);
Jan Karac30dabf2012-06-12 16:20:30 +02003849exit2:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 dput(dentry);
3851 }
Al Virof5beed72015-04-30 16:09:11 -04003852 mutex_unlock(&path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 if (inode)
3854 iput(inode); /* truncate the inode here */
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003855 inode = NULL;
3856 if (delegated_inode) {
J. Bruce Fields5a146962012-08-28 07:50:40 -07003857 error = break_deleg_wait(&delegated_inode);
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003858 if (!error)
3859 goto retry_deleg;
3860 }
Al Virof5beed72015-04-30 16:09:11 -04003861 mnt_drop_write(path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862exit1:
Al Virof5beed72015-04-30 16:09:11 -04003863 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 putname(name);
Jeff Layton5d18f812012-12-20 16:38:04 -05003865 if (retry_estale(error, lookup_flags)) {
3866 lookup_flags |= LOOKUP_REVAL;
3867 inode = NULL;
3868 goto retry;
3869 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 return error;
3871
3872slashes:
David Howellsb18825a2013-09-12 19:22:53 +01003873 if (d_is_negative(dentry))
3874 error = -ENOENT;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02003875 else if (d_is_dir(dentry))
David Howellsb18825a2013-09-12 19:22:53 +01003876 error = -EISDIR;
3877 else
3878 error = -ENOTDIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 goto exit2;
3880}
3881
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003882SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003883{
3884 if ((flag & ~AT_REMOVEDIR) != 0)
3885 return -EINVAL;
3886
3887 if (flag & AT_REMOVEDIR)
3888 return do_rmdir(dfd, pathname);
3889
3890 return do_unlinkat(dfd, pathname);
3891}
3892
Heiko Carstens3480b252009-01-14 14:14:16 +01003893SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003894{
3895 return do_unlinkat(AT_FDCWD, pathname);
3896}
3897
Miklos Szeredidb2e7472008-06-24 16:50:16 +02003898int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003900 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901
3902 if (error)
3903 return error;
3904
Al Viroacfa4382008-12-04 10:06:33 -05003905 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 return -EPERM;
3907
3908 error = security_inode_symlink(dir, dentry, oldname);
3909 if (error)
3910 return error;
3911
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003913 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003914 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 return error;
3916}
Al Viro4d359502014-03-14 12:20:17 -04003917EXPORT_SYMBOL(vfs_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003919SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
3920 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921{
Al Viro2ad94ae2008-07-21 09:32:51 -04003922 int error;
Jeff Layton91a27b22012-10-10 15:25:28 -04003923 struct filename *from;
Dave Hansen6902d922006-09-30 23:29:01 -07003924 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003925 struct path path;
Jeff Laytonf46d3562012-12-11 12:10:08 -05003926 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927
3928 from = getname(oldname);
Al Viro2ad94ae2008-07-21 09:32:51 -04003929 if (IS_ERR(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 return PTR_ERR(from);
Jeff Laytonf46d3562012-12-11 12:10:08 -05003931retry:
3932 dentry = user_path_create(newdfd, newname, &path, lookup_flags);
Dave Hansen6902d922006-09-30 23:29:01 -07003933 error = PTR_ERR(dentry);
3934 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003935 goto out_putname;
Dave Hansen6902d922006-09-30 23:29:01 -07003936
Jeff Layton91a27b22012-10-10 15:25:28 -04003937 error = security_path_symlink(&path, dentry, from->name);
Al Viroa8104a92012-07-20 02:25:00 +04003938 if (!error)
Jeff Layton91a27b22012-10-10 15:25:28 -04003939 error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
Al Viro921a1652012-07-20 01:15:31 +04003940 done_path_create(&path, dentry);
Jeff Laytonf46d3562012-12-11 12:10:08 -05003941 if (retry_estale(error, lookup_flags)) {
3942 lookup_flags |= LOOKUP_REVAL;
3943 goto retry;
3944 }
Dave Hansen6902d922006-09-30 23:29:01 -07003945out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 putname(from);
3947 return error;
3948}
3949
Heiko Carstens3480b252009-01-14 14:14:16 +01003950SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003951{
3952 return sys_symlinkat(oldname, AT_FDCWD, newname);
3953}
3954
J. Bruce Fields146a8592011-09-20 17:14:31 -04003955/**
3956 * vfs_link - create a new link
3957 * @old_dentry: object to be linked
3958 * @dir: new parent
3959 * @new_dentry: where to create the new link
3960 * @delegated_inode: returns inode needing a delegation break
3961 *
3962 * The caller must hold dir->i_mutex
3963 *
3964 * If vfs_link discovers a delegation on the to-be-linked file in need
3965 * of breaking, it will return -EWOULDBLOCK and return a reference to the
3966 * inode in delegated_inode. The caller should then break the delegation
3967 * and retry. Because breaking a delegation may take a long time, the
3968 * caller should drop the i_mutex before doing so.
3969 *
3970 * Alternatively, a caller may pass NULL for delegated_inode. This may
3971 * be appropriate for callers that expect the underlying filesystem not
3972 * to be NFS exported.
3973 */
3974int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975{
3976 struct inode *inode = old_dentry->d_inode;
Al Viro8de52772012-02-06 12:45:27 -05003977 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 int error;
3979
3980 if (!inode)
3981 return -ENOENT;
3982
Miklos Szeredia95164d2008-07-30 15:08:48 +02003983 error = may_create(dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 if (error)
3985 return error;
3986
3987 if (dir->i_sb != inode->i_sb)
3988 return -EXDEV;
3989
3990 /*
3991 * A link to an append-only or immutable file cannot be created.
3992 */
3993 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3994 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05003995 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003997 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 return -EPERM;
3999
4000 error = security_inode_link(old_dentry, dir, new_dentry);
4001 if (error)
4002 return error;
4003
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02004004 mutex_lock(&inode->i_mutex);
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05304005 /* Make sure we don't allow creating hardlink to an unlinked file */
Al Virof4e0c302013-06-11 08:34:36 +04004006 if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05304007 error = -ENOENT;
Al Viro8de52772012-02-06 12:45:27 -05004008 else if (max_links && inode->i_nlink >= max_links)
4009 error = -EMLINK;
J. Bruce Fields146a8592011-09-20 17:14:31 -04004010 else {
4011 error = try_break_deleg(inode, delegated_inode);
4012 if (!error)
4013 error = dir->i_op->link(old_dentry, dir, new_dentry);
4014 }
Al Virof4e0c302013-06-11 08:34:36 +04004015
4016 if (!error && (inode->i_state & I_LINKABLE)) {
4017 spin_lock(&inode->i_lock);
4018 inode->i_state &= ~I_LINKABLE;
4019 spin_unlock(&inode->i_lock);
4020 }
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02004021 mutex_unlock(&inode->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07004022 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02004023 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 return error;
4025}
Al Viro4d359502014-03-14 12:20:17 -04004026EXPORT_SYMBOL(vfs_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027
4028/*
4029 * Hardlinks are often used in delicate situations. We avoid
4030 * security-related surprises by not following symlinks on the
4031 * newname. --KAB
4032 *
4033 * We don't follow them on the oldname either to be compatible
4034 * with linux 2.0, and to avoid hard-linking to directories
4035 * and other special files. --ADM
4036 */
Heiko Carstens2e4d0922009-01-14 14:14:31 +01004037SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
4038 int, newdfd, const char __user *, newname, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039{
4040 struct dentry *new_dentry;
Al Virodae6ad82011-06-26 11:50:15 -04004041 struct path old_path, new_path;
J. Bruce Fields146a8592011-09-20 17:14:31 -04004042 struct inode *delegated_inode = NULL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304043 int how = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304046 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08004047 return -EINVAL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304048 /*
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07004049 * To use null names we require CAP_DAC_READ_SEARCH
4050 * This ensures that not everyone will be able to create
4051 * handlink using the passed filedescriptor.
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304052 */
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07004053 if (flags & AT_EMPTY_PATH) {
4054 if (!capable(CAP_DAC_READ_SEARCH))
4055 return -ENOENT;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304056 how = LOOKUP_EMPTY;
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07004057 }
Ulrich Drepperc04030e2006-02-24 13:04:21 -08004058
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304059 if (flags & AT_SYMLINK_FOLLOW)
4060 how |= LOOKUP_FOLLOW;
Jeff Layton442e31c2012-12-20 16:15:38 -05004061retry:
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304062 error = user_path_at(olddfd, oldname, how, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04004064 return error;
4065
Jeff Layton442e31c2012-12-20 16:15:38 -05004066 new_dentry = user_path_create(newdfd, newname, &new_path,
4067 (how & LOOKUP_REVAL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07004069 if (IS_ERR(new_dentry))
Al Virodae6ad82011-06-26 11:50:15 -04004070 goto out;
4071
4072 error = -EXDEV;
4073 if (old_path.mnt != new_path.mnt)
4074 goto out_dput;
Kees Cook800179c2012-07-25 17:29:07 -07004075 error = may_linkat(&old_path);
4076 if (unlikely(error))
4077 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04004078 error = security_path_link(old_path.dentry, &new_path, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004079 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04004080 goto out_dput;
J. Bruce Fields146a8592011-09-20 17:14:31 -04004081 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode);
Dave Hansen75c3f292008-02-15 14:37:45 -08004082out_dput:
Al Viro921a1652012-07-20 01:15:31 +04004083 done_path_create(&new_path, new_dentry);
J. Bruce Fields146a8592011-09-20 17:14:31 -04004084 if (delegated_inode) {
4085 error = break_deleg_wait(&delegated_inode);
Oleg Drokind22e6332014-01-31 15:41:58 -05004086 if (!error) {
4087 path_put(&old_path);
J. Bruce Fields146a8592011-09-20 17:14:31 -04004088 goto retry;
Oleg Drokind22e6332014-01-31 15:41:58 -05004089 }
J. Bruce Fields146a8592011-09-20 17:14:31 -04004090 }
Jeff Layton442e31c2012-12-20 16:15:38 -05004091 if (retry_estale(error, how)) {
Oleg Drokind22e6332014-01-31 15:41:58 -05004092 path_put(&old_path);
Jeff Layton442e31c2012-12-20 16:15:38 -05004093 how |= LOOKUP_REVAL;
4094 goto retry;
4095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096out:
Al Viro2d8f3032008-07-22 09:59:21 -04004097 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098
4099 return error;
4100}
4101
Heiko Carstens3480b252009-01-14 14:14:16 +01004102SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004103{
Ulrich Drepperc04030e2006-02-24 13:04:21 -08004104 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004105}
4106
Miklos Szeredibc270272014-04-01 17:08:42 +02004107/**
4108 * vfs_rename - rename a filesystem object
4109 * @old_dir: parent of source
4110 * @old_dentry: source
4111 * @new_dir: parent of destination
4112 * @new_dentry: destination
4113 * @delegated_inode: returns an inode needing a delegation break
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004114 * @flags: rename flags
Miklos Szeredibc270272014-04-01 17:08:42 +02004115 *
4116 * The caller must hold multiple mutexes--see lock_rename()).
4117 *
4118 * If vfs_rename discovers a delegation in need of breaking at either
4119 * the source or destination, it will return -EWOULDBLOCK and return a
4120 * reference to the inode in delegated_inode. The caller should then
4121 * break the delegation and retry. Because breaking a delegation may
4122 * take a long time, the caller should drop all locks before doing
4123 * so.
4124 *
4125 * Alternatively, a caller may pass NULL for delegated_inode. This may
4126 * be appropriate for callers that expect the underlying filesystem not
4127 * to be NFS exported.
4128 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 * The worst of all namespace operations - renaming directory. "Perverted"
4130 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
4131 * Problems:
J. Bruce Fieldsd03b29a2014-02-17 16:52:33 -05004132 * a) we can get into loop creation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 * b) race potential - two innocent renames can create a loop together.
4134 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004135 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 * story.
J. Bruce Fields6cedba82012-03-05 11:40:41 -05004137 * c) we have to lock _four_ objects - parents and victim (if it exists),
4138 * and source (if it is not a directory).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004139 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 * whether the target exists). Solution: try to be smart with locking
4141 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004142 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 * move will be locked. Thus we can rank directories by the tree
4144 * (ancestors first) and rank all non-directories after them.
4145 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004146 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 * HOWEVER, it relies on the assumption that any object with ->lookup()
4148 * has no more than 1 dentry. If "hybrid" objects will ever appear,
4149 * we'd better make sure that there's no link(2) for them.
Sage Weile4eaac02011-05-24 13:06:07 -07004150 * d) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004151 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05004153 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 * locking].
4155 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004157 struct inode *new_dir, struct dentry *new_dentry,
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004158 struct inode **delegated_inode, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159{
4160 int error;
Miklos Szeredibc270272014-04-01 17:08:42 +02004161 bool is_dir = d_is_dir(old_dentry);
Eric Paris59b0df22010-02-08 12:53:52 -05004162 const unsigned char *old_name;
Miklos Szeredibc270272014-04-01 17:08:42 +02004163 struct inode *source = old_dentry->d_inode;
4164 struct inode *target = new_dentry->d_inode;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004165 bool new_is_dir = false;
4166 unsigned max_links = new_dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167
Miklos Szeredibc270272014-04-01 17:08:42 +02004168 if (source == target)
4169 return 0;
4170
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 error = may_delete(old_dir, old_dentry, is_dir);
4172 if (error)
4173 return error;
4174
Miklos Szeredida1ce062014-04-01 17:08:43 +02004175 if (!target) {
Miklos Szeredia95164d2008-07-30 15:08:48 +02004176 error = may_create(new_dir, new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004177 } else {
4178 new_is_dir = d_is_dir(new_dentry);
4179
4180 if (!(flags & RENAME_EXCHANGE))
4181 error = may_delete(new_dir, new_dentry, is_dir);
4182 else
4183 error = may_delete(new_dir, new_dentry, new_is_dir);
4184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 if (error)
4186 return error;
4187
Miklos Szeredi7177a9c2014-07-23 15:15:30 +02004188 if (!old_dir->i_op->rename && !old_dir->i_op->rename2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 return -EPERM;
4190
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004191 if (flags && !old_dir->i_op->rename2)
4192 return -EINVAL;
4193
Miklos Szeredibc270272014-04-01 17:08:42 +02004194 /*
4195 * If we are going to change the parent - check write permissions,
4196 * we'll need to flip '..'.
4197 */
Miklos Szeredida1ce062014-04-01 17:08:43 +02004198 if (new_dir != old_dir) {
4199 if (is_dir) {
4200 error = inode_permission(source, MAY_WRITE);
4201 if (error)
4202 return error;
4203 }
4204 if ((flags & RENAME_EXCHANGE) && new_is_dir) {
4205 error = inode_permission(target, MAY_WRITE);
4206 if (error)
4207 return error;
4208 }
Miklos Szeredibc270272014-04-01 17:08:42 +02004209 }
Robert Love0eeca282005-07-12 17:06:03 -04004210
Miklos Szeredi0b3974e2014-04-01 17:08:43 +02004211 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry,
4212 flags);
Miklos Szeredibc270272014-04-01 17:08:42 +02004213 if (error)
4214 return error;
4215
4216 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
4217 dget(new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004218 if (!is_dir || (flags & RENAME_EXCHANGE))
Miklos Szeredibc270272014-04-01 17:08:42 +02004219 lock_two_nondirectories(source, target);
4220 else if (target)
4221 mutex_lock(&target->i_mutex);
4222
4223 error = -EBUSY;
Eric W. Biederman7af13642013-10-04 19:15:13 -07004224 if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry))
Miklos Szeredibc270272014-04-01 17:08:42 +02004225 goto out;
4226
Miklos Szeredida1ce062014-04-01 17:08:43 +02004227 if (max_links && new_dir != old_dir) {
Miklos Szeredibc270272014-04-01 17:08:42 +02004228 error = -EMLINK;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004229 if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links)
Miklos Szeredibc270272014-04-01 17:08:42 +02004230 goto out;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004231 if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir &&
4232 old_dir->i_nlink >= max_links)
4233 goto out;
4234 }
4235 if (is_dir && !(flags & RENAME_EXCHANGE) && target)
4236 shrink_dcache_parent(new_dentry);
4237 if (!is_dir) {
Miklos Szeredibc270272014-04-01 17:08:42 +02004238 error = try_break_deleg(source, delegated_inode);
4239 if (error)
4240 goto out;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004241 }
4242 if (target && !new_is_dir) {
4243 error = try_break_deleg(target, delegated_inode);
4244 if (error)
4245 goto out;
Miklos Szeredibc270272014-04-01 17:08:42 +02004246 }
Miklos Szeredi7177a9c2014-07-23 15:15:30 +02004247 if (!old_dir->i_op->rename2) {
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004248 error = old_dir->i_op->rename(old_dir, old_dentry,
4249 new_dir, new_dentry);
4250 } else {
Miklos Szeredi7177a9c2014-07-23 15:15:30 +02004251 WARN_ON(old_dir->i_op->rename != NULL);
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004252 error = old_dir->i_op->rename2(old_dir, old_dentry,
4253 new_dir, new_dentry, flags);
4254 }
Miklos Szeredibc270272014-04-01 17:08:42 +02004255 if (error)
4256 goto out;
4257
Miklos Szeredida1ce062014-04-01 17:08:43 +02004258 if (!(flags & RENAME_EXCHANGE) && target) {
Miklos Szeredibc270272014-04-01 17:08:42 +02004259 if (is_dir)
4260 target->i_flags |= S_DEAD;
4261 dont_mount(new_dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07004262 detach_mounts(new_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004263 }
Miklos Szeredida1ce062014-04-01 17:08:43 +02004264 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) {
4265 if (!(flags & RENAME_EXCHANGE))
4266 d_move(old_dentry, new_dentry);
4267 else
4268 d_exchange(old_dentry, new_dentry);
4269 }
Miklos Szeredibc270272014-04-01 17:08:42 +02004270out:
Miklos Szeredida1ce062014-04-01 17:08:43 +02004271 if (!is_dir || (flags & RENAME_EXCHANGE))
Miklos Szeredibc270272014-04-01 17:08:42 +02004272 unlock_two_nondirectories(source, target);
4273 else if (target)
4274 mutex_unlock(&target->i_mutex);
4275 dput(new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004276 if (!error) {
Al Viro123df292009-12-25 04:57:57 -05004277 fsnotify_move(old_dir, new_dir, old_name, is_dir,
Miklos Szeredida1ce062014-04-01 17:08:43 +02004278 !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
4279 if (flags & RENAME_EXCHANGE) {
4280 fsnotify_move(new_dir, old_dir, old_dentry->d_name.name,
4281 new_is_dir, NULL, new_dentry);
4282 }
4283 }
Robert Love0eeca282005-07-12 17:06:03 -04004284 fsnotify_oldname_free(old_name);
4285
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 return error;
4287}
Al Viro4d359502014-03-14 12:20:17 -04004288EXPORT_SYMBOL(vfs_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004290SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
4291 int, newdfd, const char __user *, newname, unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292{
Al Viro2ad94ae2008-07-21 09:32:51 -04004293 struct dentry *old_dentry, *new_dentry;
4294 struct dentry *trap;
Al Virof5beed72015-04-30 16:09:11 -04004295 struct path old_path, new_path;
4296 struct qstr old_last, new_last;
4297 int old_type, new_type;
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004298 struct inode *delegated_inode = NULL;
Jeff Layton91a27b22012-10-10 15:25:28 -04004299 struct filename *from;
4300 struct filename *to;
Al Virof5beed72015-04-30 16:09:11 -04004301 unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET;
Jeff Laytonc6a94282012-12-11 12:10:10 -05004302 bool should_retry = false;
Al Viro2ad94ae2008-07-21 09:32:51 -04004303 int error;
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004304
Miklos Szeredi0d7a8552014-10-24 00:14:37 +02004305 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredida1ce062014-04-01 17:08:43 +02004306 return -EINVAL;
4307
Miklos Szeredi0d7a8552014-10-24 00:14:37 +02004308 if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) &&
4309 (flags & RENAME_EXCHANGE))
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004310 return -EINVAL;
4311
Miklos Szeredi0d7a8552014-10-24 00:14:37 +02004312 if ((flags & RENAME_WHITEOUT) && !capable(CAP_MKNOD))
4313 return -EPERM;
4314
Al Virof5beed72015-04-30 16:09:11 -04004315 if (flags & RENAME_EXCHANGE)
4316 target_flags = 0;
4317
Jeff Laytonc6a94282012-12-11 12:10:10 -05004318retry:
Al Virof5beed72015-04-30 16:09:11 -04004319 from = user_path_parent(olddfd, oldname,
4320 &old_path, &old_last, &old_type, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04004321 if (IS_ERR(from)) {
4322 error = PTR_ERR(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 goto exit;
Jeff Layton91a27b22012-10-10 15:25:28 -04004324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325
Al Virof5beed72015-04-30 16:09:11 -04004326 to = user_path_parent(newdfd, newname,
4327 &new_path, &new_last, &new_type, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04004328 if (IS_ERR(to)) {
4329 error = PTR_ERR(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 goto exit1;
Jeff Layton91a27b22012-10-10 15:25:28 -04004331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332
4333 error = -EXDEV;
Al Virof5beed72015-04-30 16:09:11 -04004334 if (old_path.mnt != new_path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 goto exit2;
4336
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 error = -EBUSY;
Al Virof5beed72015-04-30 16:09:11 -04004338 if (old_type != LAST_NORM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 goto exit2;
4340
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004341 if (flags & RENAME_NOREPLACE)
4342 error = -EEXIST;
Al Virof5beed72015-04-30 16:09:11 -04004343 if (new_type != LAST_NORM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 goto exit2;
4345
Al Virof5beed72015-04-30 16:09:11 -04004346 error = mnt_want_write(old_path.mnt);
Jan Karac30dabf2012-06-12 16:20:30 +02004347 if (error)
4348 goto exit2;
4349
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004350retry_deleg:
Al Virof5beed72015-04-30 16:09:11 -04004351 trap = lock_rename(new_path.dentry, old_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352
Al Virof5beed72015-04-30 16:09:11 -04004353 old_dentry = __lookup_hash(&old_last, old_path.dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 error = PTR_ERR(old_dentry);
4355 if (IS_ERR(old_dentry))
4356 goto exit3;
4357 /* source must exist */
4358 error = -ENOENT;
David Howellsb18825a2013-09-12 19:22:53 +01004359 if (d_is_negative(old_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 goto exit4;
Al Virof5beed72015-04-30 16:09:11 -04004361 new_dentry = __lookup_hash(&new_last, new_path.dentry, lookup_flags | target_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 error = PTR_ERR(new_dentry);
4363 if (IS_ERR(new_dentry))
4364 goto exit4;
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004365 error = -EEXIST;
4366 if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry))
4367 goto exit5;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004368 if (flags & RENAME_EXCHANGE) {
4369 error = -ENOENT;
4370 if (d_is_negative(new_dentry))
4371 goto exit5;
4372
4373 if (!d_is_dir(new_dentry)) {
4374 error = -ENOTDIR;
Al Virof5beed72015-04-30 16:09:11 -04004375 if (new_last.name[new_last.len])
Miklos Szeredida1ce062014-04-01 17:08:43 +02004376 goto exit5;
4377 }
4378 }
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004379 /* unless the source is a directory trailing slashes give -ENOTDIR */
4380 if (!d_is_dir(old_dentry)) {
4381 error = -ENOTDIR;
Al Virof5beed72015-04-30 16:09:11 -04004382 if (old_last.name[old_last.len])
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004383 goto exit5;
Al Virof5beed72015-04-30 16:09:11 -04004384 if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len])
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004385 goto exit5;
4386 }
4387 /* source should not be ancestor of target */
4388 error = -EINVAL;
4389 if (old_dentry == trap)
4390 goto exit5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 /* target should not be an ancestor of source */
Miklos Szeredida1ce062014-04-01 17:08:43 +02004392 if (!(flags & RENAME_EXCHANGE))
4393 error = -ENOTEMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 if (new_dentry == trap)
4395 goto exit5;
4396
Al Virof5beed72015-04-30 16:09:11 -04004397 error = security_path_rename(&old_path, old_dentry,
4398 &new_path, new_dentry, flags);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004399 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02004400 goto exit5;
Al Virof5beed72015-04-30 16:09:11 -04004401 error = vfs_rename(old_path.dentry->d_inode, old_dentry,
4402 new_path.dentry->d_inode, new_dentry,
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004403 &delegated_inode, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404exit5:
4405 dput(new_dentry);
4406exit4:
4407 dput(old_dentry);
4408exit3:
Al Virof5beed72015-04-30 16:09:11 -04004409 unlock_rename(new_path.dentry, old_path.dentry);
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004410 if (delegated_inode) {
4411 error = break_deleg_wait(&delegated_inode);
4412 if (!error)
4413 goto retry_deleg;
4414 }
Al Virof5beed72015-04-30 16:09:11 -04004415 mnt_drop_write(old_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416exit2:
Jeff Laytonc6a94282012-12-11 12:10:10 -05004417 if (retry_estale(error, lookup_flags))
4418 should_retry = true;
Al Virof5beed72015-04-30 16:09:11 -04004419 path_put(&new_path);
Al Viro2ad94ae2008-07-21 09:32:51 -04004420 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421exit1:
Al Virof5beed72015-04-30 16:09:11 -04004422 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 putname(from);
Jeff Laytonc6a94282012-12-11 12:10:10 -05004424 if (should_retry) {
4425 should_retry = false;
4426 lookup_flags |= LOOKUP_REVAL;
4427 goto retry;
4428 }
Al Viro2ad94ae2008-07-21 09:32:51 -04004429exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 return error;
4431}
4432
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004433SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4434 int, newdfd, const char __user *, newname)
4435{
4436 return sys_renameat2(olddfd, oldname, newdfd, newname, 0);
4437}
4438
Heiko Carstensa26eab22009-01-14 14:14:17 +01004439SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004440{
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004441 return sys_renameat2(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004442}
4443
Miklos Szeredi787fb6b2014-10-24 00:14:36 +02004444int vfs_whiteout(struct inode *dir, struct dentry *dentry)
4445{
4446 int error = may_create(dir, dentry);
4447 if (error)
4448 return error;
4449
4450 if (!dir->i_op->mknod)
4451 return -EPERM;
4452
4453 return dir->i_op->mknod(dir, dentry,
4454 S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
4455}
4456EXPORT_SYMBOL(vfs_whiteout);
4457
Al Viro5d826c82014-03-14 13:42:45 -04004458int readlink_copy(char __user *buffer, int buflen, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459{
Al Viro5d826c82014-03-14 13:42:45 -04004460 int len = PTR_ERR(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 if (IS_ERR(link))
4462 goto out;
4463
4464 len = strlen(link);
4465 if (len > (unsigned) buflen)
4466 len = buflen;
4467 if (copy_to_user(buffer, link, len))
4468 len = -EFAULT;
4469out:
4470 return len;
4471}
Al Viro5d826c82014-03-14 13:42:45 -04004472EXPORT_SYMBOL(readlink_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473
4474/*
4475 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
4476 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
4477 * using) it for any given inode is up to filesystem.
4478 */
4479int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4480{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004481 void *cookie;
Al Viro5f2c4172015-05-07 11:14:26 -04004482 struct inode *inode = d_inode(dentry);
4483 const char *link = inode->i_link;
Marcin Slusarz694a1762008-06-09 16:40:37 -07004484 int res;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004485
Al Virod4dee482015-04-30 20:08:02 -04004486 if (!link) {
Al Viro5f2c4172015-05-07 11:14:26 -04004487 link = inode->i_op->follow_link(dentry, &cookie);
Al Virod4dee482015-04-30 20:08:02 -04004488 if (IS_ERR(link))
4489 return PTR_ERR(link);
4490 }
Al Viro680baac2015-05-02 13:32:22 -04004491 res = readlink_copy(buffer, buflen, link);
Al Viro5f2c4172015-05-07 11:14:26 -04004492 if (inode->i_op->put_link)
4493 inode->i_op->put_link(inode, cookie);
Marcin Slusarz694a1762008-06-09 16:40:37 -07004494 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495}
Al Viro4d359502014-03-14 12:20:17 -04004496EXPORT_SYMBOL(generic_readlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498/* get the link contents into pagecache */
4499static char *page_getlink(struct dentry * dentry, struct page **ppage)
4500{
Duane Griffinebd09ab2008-12-19 20:47:12 +00004501 char *kaddr;
4502 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 struct address_space *mapping = dentry->d_inode->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -07004504 page = read_mapping_page(mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 if (IS_ERR(page))
Nick Piggin6fe69002007-05-06 14:49:04 -07004506 return (char*)page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 *ppage = page;
Duane Griffinebd09ab2008-12-19 20:47:12 +00004508 kaddr = kmap(page);
4509 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
4510 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511}
4512
4513int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4514{
4515 struct page *page = NULL;
Al Viro5d826c82014-03-14 13:42:45 -04004516 int res = readlink_copy(buffer, buflen, page_getlink(dentry, &page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517 if (page) {
4518 kunmap(page);
4519 page_cache_release(page);
4520 }
4521 return res;
4522}
Al Viro4d359502014-03-14 12:20:17 -04004523EXPORT_SYMBOL(page_readlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524
Al Viro6e771372015-05-02 13:37:52 -04004525const char *page_follow_link_light(struct dentry *dentry, void **cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004527 struct page *page = NULL;
Al Viro680baac2015-05-02 13:32:22 -04004528 char *res = page_getlink(dentry, &page);
4529 if (!IS_ERR(res))
4530 *cookie = page;
4531 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532}
Al Viro4d359502014-03-14 12:20:17 -04004533EXPORT_SYMBOL(page_follow_link_light);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534
Al Viro5f2c4172015-05-07 11:14:26 -04004535void page_put_link(struct inode *unused, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004537 struct page *page = cookie;
Al Viro680baac2015-05-02 13:32:22 -04004538 kunmap(page);
4539 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540}
Al Viro4d359502014-03-14 12:20:17 -04004541EXPORT_SYMBOL(page_put_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542
Nick Piggin54566b22009-01-04 12:00:53 -08004543/*
4544 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
4545 */
4546int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547{
4548 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004549 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07004550 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08004551 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 char *kaddr;
Nick Piggin54566b22009-01-04 12:00:53 -08004553 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
4554 if (nofs)
4555 flags |= AOP_FLAG_NOFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556
NeilBrown7e53cac2006-03-25 03:07:57 -08004557retry:
Nick Pigginafddba42007-10-16 01:25:01 -07004558 err = pagecache_write_begin(NULL, mapping, 0, len-1,
Nick Piggin54566b22009-01-04 12:00:53 -08004559 flags, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07004561 goto fail;
4562
Cong Wange8e3c3d2011-11-25 23:14:27 +08004563 kaddr = kmap_atomic(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 memcpy(kaddr, symname, len-1);
Cong Wange8e3c3d2011-11-25 23:14:27 +08004565 kunmap_atomic(kaddr);
Nick Pigginafddba42007-10-16 01:25:01 -07004566
4567 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4568 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 if (err < 0)
4570 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07004571 if (err < len-1)
4572 goto retry;
4573
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 mark_inode_dirty(inode);
4575 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576fail:
4577 return err;
4578}
Al Viro4d359502014-03-14 12:20:17 -04004579EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004581int page_symlink(struct inode *inode, const char *symname, int len)
4582{
4583 return __page_symlink(inode, symname, len,
Nick Piggin54566b22009-01-04 12:00:53 -08004584 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004585}
Al Viro4d359502014-03-14 12:20:17 -04004586EXPORT_SYMBOL(page_symlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004587
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08004588const struct inode_operations page_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 .readlink = generic_readlink,
4590 .follow_link = page_follow_link_light,
4591 .put_link = page_put_link,
4592};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593EXPORT_SYMBOL(page_symlink_inode_operations);