blob: fe30d3be43a8b381d3b9ac3016b28531996f91b7 [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 Blunck5dd784d02008-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 Blunck5dd784d02008-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 Viro1f55a6e2014-11-01 19:30:41 -0400495struct nameidata {
496 struct path path;
497 struct qstr last;
498 struct path root;
499 struct inode *inode; /* path.dentry.d_inode */
500 unsigned int flags;
501 unsigned seq, m_seq;
502 int last_type;
503 unsigned depth;
Al Viro5e530842014-11-20 14:14:42 -0500504 struct file *base;
Al Viro1f55a6e2014-11-01 19:30:41 -0400505 char *saved_names[MAX_NESTED_LINKS + 1];
506};
507
Al Viro19660af2011-03-25 10:32:48 -0400508/*
Nick Piggin31e6b012011-01-07 17:49:52 +1100509 * Path walking has 2 modes, rcu-walk and ref-walk (see
Al Viro19660af2011-03-25 10:32:48 -0400510 * Documentation/filesystems/path-lookup.txt). In situations when we can't
511 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
512 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
513 * mode. Refcounts are grabbed at the last known good point before rcu-walk
514 * got stuck, so ref-walk may continue from there. If this is not successful
515 * (eg. a seqcount has changed), then failure is returned and it's up to caller
516 * to restart the path walk from the beginning in ref-walk mode.
Nick Piggin31e6b012011-01-07 17:49:52 +1100517 */
Nick Piggin31e6b012011-01-07 17:49:52 +1100518
519/**
Al Viro19660af2011-03-25 10:32:48 -0400520 * unlazy_walk - try to switch to ref-walk mode.
521 * @nd: nameidata pathwalk data
522 * @dentry: child of nd->path.dentry or NULL
Randy Dunlap39191622011-01-08 19:36:21 -0800523 * Returns: 0 on success, -ECHILD on failure
Nick Piggin31e6b012011-01-07 17:49:52 +1100524 *
Al Viro19660af2011-03-25 10:32:48 -0400525 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
526 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
527 * @nd or NULL. Must be called from rcu-walk context.
Nick Piggin31e6b012011-01-07 17:49:52 +1100528 */
Al Viro19660af2011-03-25 10:32:48 -0400529static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
Nick Piggin31e6b012011-01-07 17:49:52 +1100530{
531 struct fs_struct *fs = current->fs;
532 struct dentry *parent = nd->path.dentry;
533
534 BUG_ON(!(nd->flags & LOOKUP_RCU));
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700535
536 /*
Al Viro48a066e2013-09-29 22:06:07 -0400537 * After legitimizing the bastards, terminate_walk()
538 * will do the right thing for non-RCU mode, and all our
539 * subsequent exit cases should rcu_read_unlock()
540 * before returning. Do vfsmount first; if dentry
541 * can't be legitimized, just set nd->path.dentry to NULL
542 * and rely on dput(NULL) being a no-op.
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700543 */
Al Viro48a066e2013-09-29 22:06:07 -0400544 if (!legitimize_mnt(nd->path.mnt, nd->m_seq))
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700545 return -ECHILD;
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700546 nd->flags &= ~LOOKUP_RCU;
Linus Torvalds15570082013-09-02 11:38:06 -0700547
Al Viro48a066e2013-09-29 22:06:07 -0400548 if (!lockref_get_not_dead(&parent->d_lockref)) {
549 nd->path.dentry = NULL;
Al Virod870b4a2013-11-29 01:48:32 -0500550 goto out;
Al Viro48a066e2013-09-29 22:06:07 -0400551 }
552
Linus Torvalds15570082013-09-02 11:38:06 -0700553 /*
554 * For a negative lookup, the lookup sequence point is the parents
555 * sequence point, and it only needs to revalidate the parent dentry.
556 *
557 * For a positive lookup, we need to move both the parent and the
558 * dentry from the RCU domain to be properly refcounted. And the
559 * sequence number in the dentry validates *both* dentry counters,
560 * since we checked the sequence number of the parent after we got
561 * the child sequence number. So we know the parent must still
562 * be valid if the child sequence number is still valid.
563 */
Al Viro19660af2011-03-25 10:32:48 -0400564 if (!dentry) {
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700565 if (read_seqcount_retry(&parent->d_seq, nd->seq))
566 goto out;
Al Viro19660af2011-03-25 10:32:48 -0400567 BUG_ON(nd->inode != parent->d_inode);
568 } else {
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700569 if (!lockref_get_not_dead(&dentry->d_lockref))
570 goto out;
571 if (read_seqcount_retry(&dentry->d_seq, nd->seq))
572 goto drop_dentry;
Al Viro19660af2011-03-25 10:32:48 -0400573 }
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700574
575 /*
576 * Sequence counts matched. Now make sure that the root is
577 * still valid and get it if required.
578 */
579 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
580 spin_lock(&fs->lock);
581 if (nd->root.mnt != fs->root.mnt || nd->root.dentry != fs->root.dentry)
582 goto unlock_and_drop_dentry;
Nick Piggin31e6b012011-01-07 17:49:52 +1100583 path_get(&nd->root);
584 spin_unlock(&fs->lock);
585 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100586
Al Viro8b61e742013-11-08 12:45:01 -0500587 rcu_read_unlock();
Nick Piggin31e6b012011-01-07 17:49:52 +1100588 return 0;
Al Viro19660af2011-03-25 10:32:48 -0400589
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700590unlock_and_drop_dentry:
591 spin_unlock(&fs->lock);
592drop_dentry:
Al Viro8b61e742013-11-08 12:45:01 -0500593 rcu_read_unlock();
Linus Torvalds15570082013-09-02 11:38:06 -0700594 dput(dentry);
Linus Torvaldsd0d27272013-09-10 12:17:49 -0700595 goto drop_root_mnt;
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700596out:
Al Viro8b61e742013-11-08 12:45:01 -0500597 rcu_read_unlock();
Linus Torvaldsd0d27272013-09-10 12:17:49 -0700598drop_root_mnt:
599 if (!(nd->flags & LOOKUP_ROOT))
600 nd->root.mnt = NULL;
Nick Piggin31e6b012011-01-07 17:49:52 +1100601 return -ECHILD;
602}
603
Al Viro4ce16ef32012-06-10 16:10:59 -0400604static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
Nick Piggin34286d62011-01-07 17:49:57 +1100605{
Al Viro4ce16ef32012-06-10 16:10:59 -0400606 return dentry->d_op->d_revalidate(dentry, flags);
Nick Piggin34286d62011-01-07 17:49:57 +1100607}
608
Al Viro9f1fafe2011-03-25 11:00:12 -0400609/**
610 * complete_walk - successful completion of path walk
611 * @nd: pointer nameidata
Jeff Layton39159de2009-12-07 12:01:50 -0500612 *
Al Viro9f1fafe2011-03-25 11:00:12 -0400613 * If we had been in RCU mode, drop out of it and legitimize nd->path.
614 * Revalidate the final result, unless we'd already done that during
615 * the path walk or the filesystem doesn't ask for it. Return 0 on
616 * success, -error on failure. In case of failure caller does not
617 * need to drop nd->path.
Jeff Layton39159de2009-12-07 12:01:50 -0500618 */
Al Viro9f1fafe2011-03-25 11:00:12 -0400619static int complete_walk(struct nameidata *nd)
Jeff Layton39159de2009-12-07 12:01:50 -0500620{
Al Viro16c2cd72011-02-22 15:50:10 -0500621 struct dentry *dentry = nd->path.dentry;
Jeff Layton39159de2009-12-07 12:01:50 -0500622 int status;
Jeff Layton39159de2009-12-07 12:01:50 -0500623
Al Viro9f1fafe2011-03-25 11:00:12 -0400624 if (nd->flags & LOOKUP_RCU) {
625 nd->flags &= ~LOOKUP_RCU;
626 if (!(nd->flags & LOOKUP_ROOT))
627 nd->root.mnt = NULL;
Linus Torvalds15570082013-09-02 11:38:06 -0700628
Al Viro48a066e2013-09-29 22:06:07 -0400629 if (!legitimize_mnt(nd->path.mnt, nd->m_seq)) {
Al Viro8b61e742013-11-08 12:45:01 -0500630 rcu_read_unlock();
Al Viro48a066e2013-09-29 22:06:07 -0400631 return -ECHILD;
632 }
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700633 if (unlikely(!lockref_get_not_dead(&dentry->d_lockref))) {
Al Viro8b61e742013-11-08 12:45:01 -0500634 rcu_read_unlock();
Al Viro48a066e2013-09-29 22:06:07 -0400635 mntput(nd->path.mnt);
Al Viro9f1fafe2011-03-25 11:00:12 -0400636 return -ECHILD;
637 }
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700638 if (read_seqcount_retry(&dentry->d_seq, nd->seq)) {
Al Viro8b61e742013-11-08 12:45:01 -0500639 rcu_read_unlock();
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700640 dput(dentry);
Al Viro48a066e2013-09-29 22:06:07 -0400641 mntput(nd->path.mnt);
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700642 return -ECHILD;
643 }
Al Viro8b61e742013-11-08 12:45:01 -0500644 rcu_read_unlock();
Al Viro9f1fafe2011-03-25 11:00:12 -0400645 }
646
Al Viro16c2cd72011-02-22 15:50:10 -0500647 if (likely(!(nd->flags & LOOKUP_JUMPED)))
Jeff Layton39159de2009-12-07 12:01:50 -0500648 return 0;
649
Jeff Laytonecf3d1f2013-02-20 11:19:05 -0500650 if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
Al Viro16c2cd72011-02-22 15:50:10 -0500651 return 0;
652
Jeff Laytonecf3d1f2013-02-20 11:19:05 -0500653 status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
Jeff Layton39159de2009-12-07 12:01:50 -0500654 if (status > 0)
655 return 0;
656
Al Viro16c2cd72011-02-22 15:50:10 -0500657 if (!status)
Jeff Layton39159de2009-12-07 12:01:50 -0500658 status = -ESTALE;
Al Viro16c2cd72011-02-22 15:50:10 -0500659
Al Viro9f1fafe2011-03-25 11:00:12 -0400660 path_put(&nd->path);
Jeff Layton39159de2009-12-07 12:01:50 -0500661 return status;
662}
663
Al Viro2a737872009-04-07 11:49:53 -0400664static __always_inline void set_root(struct nameidata *nd)
665{
Al Viro7bd88372014-09-13 21:55:46 -0400666 get_fs_root(current->fs, &nd->root);
Al Viro2a737872009-04-07 11:49:53 -0400667}
668
Al Viro6de88d72009-08-09 01:41:57 +0400669static int link_path_walk(const char *, struct nameidata *);
670
Al Viro7bd88372014-09-13 21:55:46 -0400671static __always_inline unsigned set_root_rcu(struct nameidata *nd)
Nick Piggin31e6b012011-01-07 17:49:52 +1100672{
Al Viro7bd88372014-09-13 21:55:46 -0400673 struct fs_struct *fs = current->fs;
674 unsigned seq, res;
Nick Pigginc28cc362011-01-07 17:49:53 +1100675
Al Viro7bd88372014-09-13 21:55:46 -0400676 do {
677 seq = read_seqcount_begin(&fs->seq);
678 nd->root = fs->root;
679 res = __read_seqcount_begin(&nd->root.dentry->d_seq);
680 } while (read_seqcount_retry(&fs->seq, seq));
681 return res;
Nick Piggin31e6b012011-01-07 17:49:52 +1100682}
683
Jan Blunck1d957f92008-02-14 19:34:35 -0800684static void path_put_conditional(struct path *path, struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700685{
686 dput(path->dentry);
Jan Blunck4ac91372008-02-14 19:34:32 -0800687 if (path->mnt != nd->path.mnt)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700688 mntput(path->mnt);
689}
690
Nick Piggin7b9337a2011-01-14 08:42:43 +0000691static inline void path_to_nameidata(const struct path *path,
692 struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700693{
Nick Piggin31e6b012011-01-07 17:49:52 +1100694 if (!(nd->flags & LOOKUP_RCU)) {
695 dput(nd->path.dentry);
696 if (nd->path.mnt != path->mnt)
697 mntput(nd->path.mnt);
Huang Shijie9a229682010-04-02 17:37:13 +0800698 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100699 nd->path.mnt = path->mnt;
Jan Blunck4ac91372008-02-14 19:34:32 -0800700 nd->path.dentry = path->dentry;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700701}
702
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400703/*
704 * Helper to directly jump to a known parsed path from ->follow_link,
705 * caller must have taken a reference to path beforehand.
706 */
707void nd_jump_link(struct nameidata *nd, struct path *path)
708{
709 path_put(&nd->path);
710
711 nd->path = *path;
712 nd->inode = nd->path.dentry->d_inode;
713 nd->flags |= LOOKUP_JUMPED;
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400714}
715
Al Viro1f55a6e2014-11-01 19:30:41 -0400716void nd_set_link(struct nameidata *nd, char *path)
717{
718 nd->saved_names[nd->depth] = path;
719}
720EXPORT_SYMBOL(nd_set_link);
721
722char *nd_get_link(struct nameidata *nd)
723{
724 return nd->saved_names[nd->depth];
725}
726EXPORT_SYMBOL(nd_get_link);
727
Al Viro574197e2011-03-14 22:20:34 -0400728static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
729{
730 struct inode *inode = link->dentry->d_inode;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400731 if (inode->i_op->put_link)
Al Viro574197e2011-03-14 22:20:34 -0400732 inode->i_op->put_link(link->dentry, nd, cookie);
733 path_put(link);
734}
735
Linus Torvalds561ec642012-10-26 10:05:07 -0700736int sysctl_protected_symlinks __read_mostly = 0;
737int sysctl_protected_hardlinks __read_mostly = 0;
Kees Cook800179c2012-07-25 17:29:07 -0700738
739/**
740 * may_follow_link - Check symlink following for unsafe situations
741 * @link: The path of the symlink
Randy Dunlap55852632012-08-18 17:39:25 -0700742 * @nd: nameidata pathwalk data
Kees Cook800179c2012-07-25 17:29:07 -0700743 *
744 * In the case of the sysctl_protected_symlinks sysctl being enabled,
745 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
746 * in a sticky world-writable directory. This is to protect privileged
747 * processes from failing races against path names that may change out
748 * from under them by way of other users creating malicious symlinks.
749 * It will permit symlinks to be followed only when outside a sticky
750 * world-writable directory, or when the uid of the symlink and follower
751 * match, or when the directory owner matches the symlink's owner.
752 *
753 * Returns 0 if following the symlink is allowed, -ve on error.
754 */
755static inline int may_follow_link(struct path *link, struct nameidata *nd)
756{
757 const struct inode *inode;
758 const struct inode *parent;
759
760 if (!sysctl_protected_symlinks)
761 return 0;
762
763 /* Allowed if owner and follower match. */
764 inode = link->dentry->d_inode;
Eric W. Biederman81abe272012-08-03 09:38:08 -0700765 if (uid_eq(current_cred()->fsuid, inode->i_uid))
Kees Cook800179c2012-07-25 17:29:07 -0700766 return 0;
767
768 /* Allowed if parent directory not sticky and world-writable. */
769 parent = nd->path.dentry->d_inode;
770 if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
771 return 0;
772
773 /* Allowed if parent directory and link owner match. */
Eric W. Biederman81abe272012-08-03 09:38:08 -0700774 if (uid_eq(parent->i_uid, inode->i_uid))
Kees Cook800179c2012-07-25 17:29:07 -0700775 return 0;
776
Sasha Levinffd8d102012-10-04 19:56:40 -0400777 audit_log_link_denied("follow_link", link);
Kees Cook800179c2012-07-25 17:29:07 -0700778 path_put_conditional(link, nd);
779 path_put(&nd->path);
780 return -EACCES;
781}
782
783/**
784 * safe_hardlink_source - Check for safe hardlink conditions
785 * @inode: the source inode to hardlink from
786 *
787 * Return false if at least one of the following conditions:
788 * - inode is not a regular file
789 * - inode is setuid
790 * - inode is setgid and group-exec
791 * - access failure for read and write
792 *
793 * Otherwise returns true.
794 */
795static bool safe_hardlink_source(struct inode *inode)
796{
797 umode_t mode = inode->i_mode;
798
799 /* Special files should not get pinned to the filesystem. */
800 if (!S_ISREG(mode))
801 return false;
802
803 /* Setuid files should not get pinned to the filesystem. */
804 if (mode & S_ISUID)
805 return false;
806
807 /* Executable setgid files should not get pinned to the filesystem. */
808 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
809 return false;
810
811 /* Hardlinking to unreadable or unwritable sources is dangerous. */
812 if (inode_permission(inode, MAY_READ | MAY_WRITE))
813 return false;
814
815 return true;
816}
817
818/**
819 * may_linkat - Check permissions for creating a hardlink
820 * @link: the source to hardlink from
821 *
822 * Block hardlink when all of:
823 * - sysctl_protected_hardlinks enabled
824 * - fsuid does not match inode
825 * - hardlink source is unsafe (see safe_hardlink_source() above)
826 * - not CAP_FOWNER
827 *
828 * Returns 0 if successful, -ve on error.
829 */
830static int may_linkat(struct path *link)
831{
832 const struct cred *cred;
833 struct inode *inode;
834
835 if (!sysctl_protected_hardlinks)
836 return 0;
837
838 cred = current_cred();
839 inode = link->dentry->d_inode;
840
841 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
842 * otherwise, it must be a safe source.
843 */
Eric W. Biederman81abe272012-08-03 09:38:08 -0700844 if (uid_eq(cred->fsuid, inode->i_uid) || safe_hardlink_source(inode) ||
Kees Cook800179c2012-07-25 17:29:07 -0700845 capable(CAP_FOWNER))
846 return 0;
847
Kees Cooka51d9ea2012-07-25 17:29:08 -0700848 audit_log_link_denied("linkat", link);
Kees Cook800179c2012-07-25 17:29:07 -0700849 return -EPERM;
850}
851
Al Virodef4af32009-12-26 08:37:05 -0500852static __always_inline int
Al Viro574197e2011-03-14 22:20:34 -0400853follow_link(struct path *link, struct nameidata *nd, void **p)
Ian Kent051d3812006-03-27 01:14:53 -0800854{
Nick Piggin7b9337a2011-01-14 08:42:43 +0000855 struct dentry *dentry = link->dentry;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400856 int error;
857 char *s;
Ian Kent051d3812006-03-27 01:14:53 -0800858
Al Viro844a3912011-02-15 00:38:26 -0500859 BUG_ON(nd->flags & LOOKUP_RCU);
860
Al Viro0e794582011-03-16 02:45:02 -0400861 if (link->mnt == nd->path.mnt)
862 mntget(link->mnt);
863
Al Viro6d7b5aa2012-06-10 04:15:17 -0400864 error = -ELOOP;
865 if (unlikely(current->total_link_count >= 40))
866 goto out_put_nd_path;
867
Al Viro574197e2011-03-14 22:20:34 -0400868 cond_resched();
869 current->total_link_count++;
870
Al Viro68ac1232012-03-15 08:21:57 -0400871 touch_atime(link);
Ian Kent051d3812006-03-27 01:14:53 -0800872 nd_set_link(nd, NULL);
873
Al Viro36f3b4f2011-02-22 21:24:38 -0500874 error = security_inode_follow_link(link->dentry, nd);
Al Viro6d7b5aa2012-06-10 04:15:17 -0400875 if (error)
876 goto out_put_nd_path;
Al Viro36f3b4f2011-02-22 21:24:38 -0500877
Al Viro86acdca12009-12-22 23:45:11 -0500878 nd->last_type = LAST_BIND;
Al Virodef4af32009-12-26 08:37:05 -0500879 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
880 error = PTR_ERR(*p);
Al Viro6d7b5aa2012-06-10 04:15:17 -0400881 if (IS_ERR(*p))
Christoph Hellwig408ef012012-06-18 10:47:03 -0400882 goto out_put_nd_path;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400883
884 error = 0;
885 s = nd_get_link(nd);
886 if (s) {
Al Viro443ed252013-09-10 12:00:43 -0400887 if (unlikely(IS_ERR(s))) {
888 path_put(&nd->path);
889 put_link(nd, link, *p);
890 return PTR_ERR(s);
891 }
892 if (*s == '/') {
Al Viro7bd88372014-09-13 21:55:46 -0400893 if (!nd->root.mnt)
894 set_root(nd);
Al Viro443ed252013-09-10 12:00:43 -0400895 path_put(&nd->path);
896 nd->path = nd->root;
897 path_get(&nd->root);
898 nd->flags |= LOOKUP_JUMPED;
899 }
900 nd->inode = nd->path.dentry->d_inode;
901 error = link_path_walk(s, nd);
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400902 if (unlikely(error))
903 put_link(nd, link, *p);
Ian Kent051d3812006-03-27 01:14:53 -0800904 }
Al Viro6d7b5aa2012-06-10 04:15:17 -0400905
906 return error;
907
908out_put_nd_path:
Arnd Bergmann98f6ef62012-10-11 13:20:00 +0000909 *p = NULL;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400910 path_put(&nd->path);
Al Viro6d7b5aa2012-06-10 04:15:17 -0400911 path_put(link);
Ian Kent051d3812006-03-27 01:14:53 -0800912 return error;
913}
914
Nick Piggin31e6b012011-01-07 17:49:52 +1100915static int follow_up_rcu(struct path *path)
916{
Al Viro0714a532011-11-24 22:19:58 -0500917 struct mount *mnt = real_mount(path->mnt);
918 struct mount *parent;
Nick Piggin31e6b012011-01-07 17:49:52 +1100919 struct dentry *mountpoint;
920
Al Viro0714a532011-11-24 22:19:58 -0500921 parent = mnt->mnt_parent;
922 if (&parent->mnt == path->mnt)
Nick Piggin31e6b012011-01-07 17:49:52 +1100923 return 0;
Al Viroa73324d2011-11-24 22:25:07 -0500924 mountpoint = mnt->mnt_mountpoint;
Nick Piggin31e6b012011-01-07 17:49:52 +1100925 path->dentry = mountpoint;
Al Viro0714a532011-11-24 22:19:58 -0500926 path->mnt = &parent->mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +1100927 return 1;
928}
929
David Howellsf015f1262012-06-25 12:55:28 +0100930/*
931 * follow_up - Find the mountpoint of path's vfsmount
932 *
933 * Given a path, find the mountpoint of its source file system.
934 * Replace @path with the path of the mountpoint in the parent mount.
935 * Up is towards /.
936 *
937 * Return 1 if we went up a level and 0 if we were already at the
938 * root.
939 */
Al Virobab77eb2009-04-18 03:26:48 -0400940int follow_up(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
Al Viro0714a532011-11-24 22:19:58 -0500942 struct mount *mnt = real_mount(path->mnt);
943 struct mount *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 struct dentry *mountpoint;
Nick Piggin99b7db72010-08-18 04:37:39 +1000945
Al Viro48a066e2013-09-29 22:06:07 -0400946 read_seqlock_excl(&mount_lock);
Al Viro0714a532011-11-24 22:19:58 -0500947 parent = mnt->mnt_parent;
Al Viro3c0a6162012-07-18 17:32:50 +0400948 if (parent == mnt) {
Al Viro48a066e2013-09-29 22:06:07 -0400949 read_sequnlock_excl(&mount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 return 0;
951 }
Al Viro0714a532011-11-24 22:19:58 -0500952 mntget(&parent->mnt);
Al Viroa73324d2011-11-24 22:25:07 -0500953 mountpoint = dget(mnt->mnt_mountpoint);
Al Viro48a066e2013-09-29 22:06:07 -0400954 read_sequnlock_excl(&mount_lock);
Al Virobab77eb2009-04-18 03:26:48 -0400955 dput(path->dentry);
956 path->dentry = mountpoint;
957 mntput(path->mnt);
Al Viro0714a532011-11-24 22:19:58 -0500958 path->mnt = &parent->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 return 1;
960}
Al Viro4d359502014-03-14 12:20:17 -0400961EXPORT_SYMBOL(follow_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100963/*
David Howells9875cf82011-01-14 18:45:21 +0000964 * Perform an automount
965 * - return -EISDIR to tell follow_managed() to stop and return the path we
966 * were called with.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 */
David Howells9875cf82011-01-14 18:45:21 +0000968static int follow_automount(struct path *path, unsigned flags,
969 bool *need_mntput)
Nick Piggin31e6b012011-01-07 17:49:52 +1100970{
David Howells9875cf82011-01-14 18:45:21 +0000971 struct vfsmount *mnt;
David Howellsea5b7782011-01-14 19:10:03 +0000972 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +1100973
David Howells9875cf82011-01-14 18:45:21 +0000974 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
975 return -EREMOTE;
Al Viro463ffb22005-06-06 13:36:05 -0700976
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200977 /* We don't want to mount if someone's just doing a stat -
978 * unless they're stat'ing a directory and appended a '/' to
979 * the name.
980 *
981 * We do, however, want to mount if someone wants to open or
982 * create a file of any type under the mountpoint, wants to
983 * traverse through the mountpoint or wants to open the
984 * mounted directory. Also, autofs may mark negative dentries
985 * as being automount points. These will need the attentions
986 * of the daemon to instantiate them before they can be used.
David Howells9875cf82011-01-14 18:45:21 +0000987 */
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200988 if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
Linus Torvaldsd94c1772011-09-26 17:44:55 -0700989 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200990 path->dentry->d_inode)
991 return -EISDIR;
992
David Howells9875cf82011-01-14 18:45:21 +0000993 current->total_link_count++;
994 if (current->total_link_count >= 40)
995 return -ELOOP;
996
997 mnt = path->dentry->d_op->d_automount(path);
998 if (IS_ERR(mnt)) {
999 /*
1000 * The filesystem is allowed to return -EISDIR here to indicate
1001 * it doesn't want to automount. For instance, autofs would do
1002 * this so that its userspace daemon can mount on this dentry.
1003 *
1004 * However, we can only permit this if it's a terminal point in
1005 * the path being looked up; if it wasn't then the remainder of
1006 * the path is inaccessible and we should say so.
1007 */
Al Viro49084c32011-06-25 21:59:52 -04001008 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
David Howells9875cf82011-01-14 18:45:21 +00001009 return -EREMOTE;
1010 return PTR_ERR(mnt);
1011 }
David Howellsea5b7782011-01-14 19:10:03 +00001012
David Howells9875cf82011-01-14 18:45:21 +00001013 if (!mnt) /* mount collision */
1014 return 0;
1015
Al Viro8aef1882011-06-16 15:10:06 +01001016 if (!*need_mntput) {
1017 /* lock_mount() may release path->mnt on error */
1018 mntget(path->mnt);
1019 *need_mntput = true;
1020 }
Al Viro19a167a2011-01-17 01:35:23 -05001021 err = finish_automount(mnt, path);
David Howellsea5b7782011-01-14 19:10:03 +00001022
David Howellsea5b7782011-01-14 19:10:03 +00001023 switch (err) {
1024 case -EBUSY:
1025 /* Someone else made a mount here whilst we were busy */
Al Viro19a167a2011-01-17 01:35:23 -05001026 return 0;
David Howellsea5b7782011-01-14 19:10:03 +00001027 case 0:
Al Viro8aef1882011-06-16 15:10:06 +01001028 path_put(path);
David Howellsea5b7782011-01-14 19:10:03 +00001029 path->mnt = mnt;
1030 path->dentry = dget(mnt->mnt_root);
David Howellsea5b7782011-01-14 19:10:03 +00001031 return 0;
Al Viro19a167a2011-01-17 01:35:23 -05001032 default:
1033 return err;
David Howellsea5b7782011-01-14 19:10:03 +00001034 }
Al Viro19a167a2011-01-17 01:35:23 -05001035
David Howells9875cf82011-01-14 18:45:21 +00001036}
1037
1038/*
1039 * Handle a dentry that is managed in some way.
David Howellscc53ce52011-01-14 18:45:26 +00001040 * - Flagged for transit management (autofs)
David Howells9875cf82011-01-14 18:45:21 +00001041 * - Flagged as mountpoint
1042 * - Flagged as automount point
1043 *
1044 * This may only be called in refwalk mode.
1045 *
1046 * Serialization is taken care of in namespace.c
1047 */
1048static int follow_managed(struct path *path, unsigned flags)
1049{
Al Viro8aef1882011-06-16 15:10:06 +01001050 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
David Howells9875cf82011-01-14 18:45:21 +00001051 unsigned managed;
1052 bool need_mntput = false;
Al Viro8aef1882011-06-16 15:10:06 +01001053 int ret = 0;
David Howells9875cf82011-01-14 18:45:21 +00001054
1055 /* Given that we're not holding a lock here, we retain the value in a
1056 * local variable for each dentry as we look at it so that we don't see
1057 * the components of that value change under us */
1058 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1059 managed &= DCACHE_MANAGED_DENTRY,
1060 unlikely(managed != 0)) {
David Howellscc53ce52011-01-14 18:45:26 +00001061 /* Allow the filesystem to manage the transit without i_mutex
1062 * being held. */
1063 if (managed & DCACHE_MANAGE_TRANSIT) {
1064 BUG_ON(!path->dentry->d_op);
1065 BUG_ON(!path->dentry->d_op->d_manage);
Al Viro1aed3e42011-03-18 09:09:02 -04001066 ret = path->dentry->d_op->d_manage(path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +00001067 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001068 break;
David Howellscc53ce52011-01-14 18:45:26 +00001069 }
1070
David Howells9875cf82011-01-14 18:45:21 +00001071 /* Transit to a mounted filesystem. */
1072 if (managed & DCACHE_MOUNTED) {
1073 struct vfsmount *mounted = lookup_mnt(path);
1074 if (mounted) {
1075 dput(path->dentry);
1076 if (need_mntput)
1077 mntput(path->mnt);
1078 path->mnt = mounted;
1079 path->dentry = dget(mounted->mnt_root);
1080 need_mntput = true;
1081 continue;
1082 }
1083
1084 /* Something is mounted on this dentry in another
1085 * namespace and/or whatever was mounted there in this
Al Viro48a066e2013-09-29 22:06:07 -04001086 * namespace got unmounted before lookup_mnt() could
1087 * get it */
David Howells9875cf82011-01-14 18:45:21 +00001088 }
1089
1090 /* Handle an automount point */
1091 if (managed & DCACHE_NEED_AUTOMOUNT) {
1092 ret = follow_automount(path, flags, &need_mntput);
1093 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001094 break;
David Howells9875cf82011-01-14 18:45:21 +00001095 continue;
1096 }
1097
1098 /* We didn't change the current path point */
1099 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 }
Al Viro8aef1882011-06-16 15:10:06 +01001101
1102 if (need_mntput && path->mnt == mnt)
1103 mntput(path->mnt);
1104 if (ret == -EISDIR)
1105 ret = 0;
Al Viroa3fbbde2011-11-07 21:21:26 +00001106 return ret < 0 ? ret : need_mntput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107}
1108
David Howellscc53ce52011-01-14 18:45:26 +00001109int follow_down_one(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
1111 struct vfsmount *mounted;
1112
Al Viro1c755af2009-04-18 14:06:57 -04001113 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -04001115 dput(path->dentry);
1116 mntput(path->mnt);
1117 path->mnt = mounted;
1118 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 return 1;
1120 }
1121 return 0;
1122}
Al Viro4d359502014-03-14 12:20:17 -04001123EXPORT_SYMBOL(follow_down_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
NeilBrownb8faf032014-08-04 17:06:29 +10001125static inline int managed_dentry_rcu(struct dentry *dentry)
Ian Kent62a73752011-03-25 01:51:02 +08001126{
NeilBrownb8faf032014-08-04 17:06:29 +10001127 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT) ?
1128 dentry->d_op->d_manage(dentry, true) : 0;
Ian Kent62a73752011-03-25 01:51:02 +08001129}
1130
David Howells9875cf82011-01-14 18:45:21 +00001131/*
Al Viro287548e2011-05-27 06:50:06 -04001132 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1133 * we meet a managed dentry that would need blocking.
David Howells9875cf82011-01-14 18:45:21 +00001134 */
1135static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
Al Viro287548e2011-05-27 06:50:06 -04001136 struct inode **inode)
David Howells9875cf82011-01-14 18:45:21 +00001137{
Ian Kent62a73752011-03-25 01:51:02 +08001138 for (;;) {
Al Viroc7105362011-11-24 18:22:03 -05001139 struct mount *mounted;
Ian Kent62a73752011-03-25 01:51:02 +08001140 /*
1141 * Don't forget we might have a non-mountpoint managed dentry
1142 * that wants to block transit.
1143 */
NeilBrownb8faf032014-08-04 17:06:29 +10001144 switch (managed_dentry_rcu(path->dentry)) {
1145 case -ECHILD:
1146 default:
David Howellsab909112011-01-14 18:46:51 +00001147 return false;
NeilBrownb8faf032014-08-04 17:06:29 +10001148 case -EISDIR:
1149 return true;
1150 case 0:
1151 break;
1152 }
Ian Kent62a73752011-03-25 01:51:02 +08001153
1154 if (!d_mountpoint(path->dentry))
NeilBrownb8faf032014-08-04 17:06:29 +10001155 return !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
Ian Kent62a73752011-03-25 01:51:02 +08001156
Al Viro474279d2013-10-01 16:11:26 -04001157 mounted = __lookup_mnt(path->mnt, path->dentry);
David Howells9875cf82011-01-14 18:45:21 +00001158 if (!mounted)
1159 break;
Al Viroc7105362011-11-24 18:22:03 -05001160 path->mnt = &mounted->mnt;
1161 path->dentry = mounted->mnt.mnt_root;
Al Viroa3fbbde2011-11-07 21:21:26 +00001162 nd->flags |= LOOKUP_JUMPED;
David Howells9875cf82011-01-14 18:45:21 +00001163 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
Linus Torvalds59430262011-07-18 15:43:29 -07001164 /*
1165 * Update the inode too. We don't need to re-check the
1166 * dentry sequence number here after this d_inode read,
1167 * because a mount-point is always pinned.
1168 */
1169 *inode = path->dentry->d_inode;
David Howells9875cf82011-01-14 18:45:21 +00001170 }
Al Virof5be3e29122014-09-13 21:50:45 -04001171 return !read_seqretry(&mount_lock, nd->m_seq) &&
NeilBrownb8faf032014-08-04 17:06:29 +10001172 !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
Al Viro287548e2011-05-27 06:50:06 -04001173}
1174
Nick Piggin31e6b012011-01-07 17:49:52 +11001175static int follow_dotdot_rcu(struct nameidata *nd)
1176{
Al Viro4023bfc2014-09-13 21:59:43 -04001177 struct inode *inode = nd->inode;
Al Viro7bd88372014-09-13 21:55:46 -04001178 if (!nd->root.mnt)
1179 set_root_rcu(nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11001180
David Howells9875cf82011-01-14 18:45:21 +00001181 while (1) {
Nick Piggin31e6b012011-01-07 17:49:52 +11001182 if (nd->path.dentry == nd->root.dentry &&
1183 nd->path.mnt == nd->root.mnt) {
1184 break;
1185 }
1186 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1187 struct dentry *old = nd->path.dentry;
1188 struct dentry *parent = old->d_parent;
1189 unsigned seq;
1190
Al Viro4023bfc2014-09-13 21:59:43 -04001191 inode = parent->d_inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001192 seq = read_seqcount_begin(&parent->d_seq);
1193 if (read_seqcount_retry(&old->d_seq, nd->seq))
Al Viroef7562d2011-03-04 14:35:59 -05001194 goto failed;
Nick Piggin31e6b012011-01-07 17:49:52 +11001195 nd->path.dentry = parent;
1196 nd->seq = seq;
1197 break;
1198 }
1199 if (!follow_up_rcu(&nd->path))
1200 break;
Al Viro4023bfc2014-09-13 21:59:43 -04001201 inode = nd->path.dentry->d_inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001202 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Nick Piggin31e6b012011-01-07 17:49:52 +11001203 }
Al Virob37199e2014-03-20 15:18:22 -04001204 while (d_mountpoint(nd->path.dentry)) {
1205 struct mount *mounted;
1206 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry);
1207 if (!mounted)
1208 break;
1209 nd->path.mnt = &mounted->mnt;
1210 nd->path.dentry = mounted->mnt.mnt_root;
Al Viro4023bfc2014-09-13 21:59:43 -04001211 inode = nd->path.dentry->d_inode;
Al Virob37199e2014-03-20 15:18:22 -04001212 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Virof5be3e29122014-09-13 21:50:45 -04001213 if (read_seqretry(&mount_lock, nd->m_seq))
Al Virob37199e2014-03-20 15:18:22 -04001214 goto failed;
1215 }
Al Viro4023bfc2014-09-13 21:59:43 -04001216 nd->inode = inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001217 return 0;
Al Viroef7562d2011-03-04 14:35:59 -05001218
1219failed:
1220 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001221 if (!(nd->flags & LOOKUP_ROOT))
1222 nd->root.mnt = NULL;
Al Viro8b61e742013-11-08 12:45:01 -05001223 rcu_read_unlock();
Al Viroef7562d2011-03-04 14:35:59 -05001224 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001225}
1226
David Howells9875cf82011-01-14 18:45:21 +00001227/*
David Howellscc53ce52011-01-14 18:45:26 +00001228 * Follow down to the covering mount currently visible to userspace. At each
1229 * point, the filesystem owning that dentry may be queried as to whether the
1230 * caller is permitted to proceed or not.
David Howellscc53ce52011-01-14 18:45:26 +00001231 */
Al Viro7cc90cc2011-03-18 09:04:20 -04001232int follow_down(struct path *path)
David Howellscc53ce52011-01-14 18:45:26 +00001233{
1234 unsigned managed;
1235 int ret;
1236
1237 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1238 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1239 /* Allow the filesystem to manage the transit without i_mutex
1240 * being held.
1241 *
1242 * We indicate to the filesystem if someone is trying to mount
1243 * something here. This gives autofs the chance to deny anyone
1244 * other than its daemon the right to mount on its
1245 * superstructure.
1246 *
1247 * The filesystem may sleep at this point.
1248 */
1249 if (managed & DCACHE_MANAGE_TRANSIT) {
1250 BUG_ON(!path->dentry->d_op);
1251 BUG_ON(!path->dentry->d_op->d_manage);
David Howellsab909112011-01-14 18:46:51 +00001252 ret = path->dentry->d_op->d_manage(
Al Viro1aed3e42011-03-18 09:09:02 -04001253 path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +00001254 if (ret < 0)
1255 return ret == -EISDIR ? 0 : ret;
1256 }
1257
1258 /* Transit to a mounted filesystem. */
1259 if (managed & DCACHE_MOUNTED) {
1260 struct vfsmount *mounted = lookup_mnt(path);
1261 if (!mounted)
1262 break;
1263 dput(path->dentry);
1264 mntput(path->mnt);
1265 path->mnt = mounted;
1266 path->dentry = dget(mounted->mnt_root);
1267 continue;
1268 }
1269
1270 /* Don't handle automount points here */
1271 break;
1272 }
1273 return 0;
1274}
Al Viro4d359502014-03-14 12:20:17 -04001275EXPORT_SYMBOL(follow_down);
David Howellscc53ce52011-01-14 18:45:26 +00001276
1277/*
David Howells9875cf82011-01-14 18:45:21 +00001278 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1279 */
1280static void follow_mount(struct path *path)
1281{
1282 while (d_mountpoint(path->dentry)) {
1283 struct vfsmount *mounted = lookup_mnt(path);
1284 if (!mounted)
1285 break;
1286 dput(path->dentry);
1287 mntput(path->mnt);
1288 path->mnt = mounted;
1289 path->dentry = dget(mounted->mnt_root);
1290 }
1291}
1292
Nick Piggin31e6b012011-01-07 17:49:52 +11001293static void follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294{
Al Viro7bd88372014-09-13 21:55:46 -04001295 if (!nd->root.mnt)
1296 set_root(nd);
Andreas Mohre518ddb2006-09-29 02:01:22 -07001297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 while(1) {
Jan Blunck4ac91372008-02-14 19:34:32 -08001299 struct dentry *old = nd->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Al Viro2a737872009-04-07 11:49:53 -04001301 if (nd->path.dentry == nd->root.dentry &&
1302 nd->path.mnt == nd->root.mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 break;
1304 }
Jan Blunck4ac91372008-02-14 19:34:32 -08001305 if (nd->path.dentry != nd->path.mnt->mnt_root) {
Al Viro3088dd72010-01-30 15:47:29 -05001306 /* rare case of legitimate dget_parent()... */
1307 nd->path.dentry = dget_parent(nd->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 dput(old);
1309 break;
1310 }
Al Viro3088dd72010-01-30 15:47:29 -05001311 if (!follow_up(&nd->path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 }
Al Viro79ed0222009-04-18 13:59:41 -04001314 follow_mount(&nd->path);
Nick Piggin31e6b012011-01-07 17:49:52 +11001315 nd->inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316}
1317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318/*
Miklos Szeredibad61182012-03-26 12:54:24 +02001319 * This looks up the name in dcache, possibly revalidates the old dentry and
1320 * allocates a new one if not found or not valid. In the need_lookup argument
1321 * returns whether i_op->lookup is necessary.
1322 *
1323 * dir->d_inode->i_mutex must be held
Nick Pigginbaa03892010-08-18 04:37:31 +10001324 */
Miklos Szeredibad61182012-03-26 12:54:24 +02001325static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
Al Viro201f9562012-06-22 12:42:10 +04001326 unsigned int flags, bool *need_lookup)
Nick Pigginbaa03892010-08-18 04:37:31 +10001327{
Nick Pigginbaa03892010-08-18 04:37:31 +10001328 struct dentry *dentry;
Miklos Szeredibad61182012-03-26 12:54:24 +02001329 int error;
Nick Pigginbaa03892010-08-18 04:37:31 +10001330
Miklos Szeredibad61182012-03-26 12:54:24 +02001331 *need_lookup = false;
1332 dentry = d_lookup(dir, name);
1333 if (dentry) {
Jeff Layton39e3c952012-11-28 11:30:53 -05001334 if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
Al Viro201f9562012-06-22 12:42:10 +04001335 error = d_revalidate(dentry, flags);
Miklos Szeredibad61182012-03-26 12:54:24 +02001336 if (unlikely(error <= 0)) {
1337 if (error < 0) {
1338 dput(dentry);
1339 return ERR_PTR(error);
Eric W. Biederman5542aa22014-02-13 09:46:25 -08001340 } else {
1341 d_invalidate(dentry);
Miklos Szeredibad61182012-03-26 12:54:24 +02001342 dput(dentry);
1343 dentry = NULL;
1344 }
1345 }
1346 }
1347 }
Nick Pigginbaa03892010-08-18 04:37:31 +10001348
Miklos Szeredibad61182012-03-26 12:54:24 +02001349 if (!dentry) {
1350 dentry = d_alloc(dir, name);
1351 if (unlikely(!dentry))
1352 return ERR_PTR(-ENOMEM);
Nick Pigginbaa03892010-08-18 04:37:31 +10001353
Miklos Szeredibad61182012-03-26 12:54:24 +02001354 *need_lookup = true;
Nick Pigginbaa03892010-08-18 04:37:31 +10001355 }
1356 return dentry;
1357}
1358
1359/*
J. Bruce Fields13a2c3b2013-10-23 16:09:16 -04001360 * Call i_op->lookup on the dentry. The dentry must be negative and
1361 * unhashed.
Miklos Szeredibad61182012-03-26 12:54:24 +02001362 *
1363 * dir->d_inode->i_mutex must be held
Josef Bacik44396f42011-05-31 11:58:49 -04001364 */
Miklos Szeredibad61182012-03-26 12:54:24 +02001365static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
Al Viro72bd8662012-06-10 17:17:17 -04001366 unsigned int flags)
Josef Bacik44396f42011-05-31 11:58:49 -04001367{
Josef Bacik44396f42011-05-31 11:58:49 -04001368 struct dentry *old;
1369
1370 /* Don't create child dentry for a dead directory. */
Miklos Szeredibad61182012-03-26 12:54:24 +02001371 if (unlikely(IS_DEADDIR(dir))) {
Miklos Szeredie188dc02012-02-03 14:25:18 +01001372 dput(dentry);
Josef Bacik44396f42011-05-31 11:58:49 -04001373 return ERR_PTR(-ENOENT);
Miklos Szeredie188dc02012-02-03 14:25:18 +01001374 }
Josef Bacik44396f42011-05-31 11:58:49 -04001375
Al Viro72bd8662012-06-10 17:17:17 -04001376 old = dir->i_op->lookup(dir, dentry, flags);
Josef Bacik44396f42011-05-31 11:58:49 -04001377 if (unlikely(old)) {
1378 dput(dentry);
1379 dentry = old;
1380 }
1381 return dentry;
1382}
1383
Al Viroa3255542012-03-30 14:41:51 -04001384static struct dentry *__lookup_hash(struct qstr *name,
Al Viro72bd8662012-06-10 17:17:17 -04001385 struct dentry *base, unsigned int flags)
Al Viroa3255542012-03-30 14:41:51 -04001386{
Miklos Szeredibad61182012-03-26 12:54:24 +02001387 bool need_lookup;
Al Viroa3255542012-03-30 14:41:51 -04001388 struct dentry *dentry;
1389
Al Viro72bd8662012-06-10 17:17:17 -04001390 dentry = lookup_dcache(name, base, flags, &need_lookup);
Miklos Szeredibad61182012-03-26 12:54:24 +02001391 if (!need_lookup)
1392 return dentry;
Al Viroa3255542012-03-30 14:41:51 -04001393
Al Viro72bd8662012-06-10 17:17:17 -04001394 return lookup_real(base->d_inode, dentry, flags);
Al Viroa3255542012-03-30 14:41:51 -04001395}
1396
Josef Bacik44396f42011-05-31 11:58:49 -04001397/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 * It's more convoluted than I'd like it to be, but... it's still fairly
1399 * small and for now I'd prefer to have fast path as straight as possible.
1400 * It _is_ time-critical.
1401 */
Al Viroe97cdc82013-01-24 18:16:00 -05001402static int lookup_fast(struct nameidata *nd,
Miklos Szeredi697f5142012-05-21 17:30:05 +02001403 struct path *path, struct inode **inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
Jan Blunck4ac91372008-02-14 19:34:32 -08001405 struct vfsmount *mnt = nd->path.mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +11001406 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro5a18fff2011-03-11 04:44:53 -05001407 int need_reval = 1;
1408 int status = 1;
David Howells9875cf82011-01-14 18:45:21 +00001409 int err;
1410
Al Viro3cac2602009-08-13 18:27:43 +04001411 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001412 * Rename seqlock is not required here because in the off chance
1413 * of a false negative due to a concurrent rename, we're going to
1414 * do the non-racy lookup, below.
1415 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001416 if (nd->flags & LOOKUP_RCU) {
1417 unsigned seq;
Al Viro766c4cb2015-05-07 19:24:57 -04001418 bool negative;
Linus Torvaldsda53be12013-05-21 15:22:44 -07001419 dentry = __d_lookup_rcu(parent, &nd->last, &seq);
Al Viro5a18fff2011-03-11 04:44:53 -05001420 if (!dentry)
1421 goto unlazy;
1422
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001423 /*
1424 * This sequence count validates that the inode matches
1425 * the dentry name information from lookup.
1426 */
1427 *inode = dentry->d_inode;
Al Viro766c4cb2015-05-07 19:24:57 -04001428 negative = d_is_negative(dentry);
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001429 if (read_seqcount_retry(&dentry->d_seq, seq))
1430 return -ECHILD;
Al Viro766c4cb2015-05-07 19:24:57 -04001431 if (negative)
1432 return -ENOENT;
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001433
1434 /*
1435 * This sequence count validates that the parent had no
1436 * changes while we did the lookup of the dentry above.
1437 *
1438 * The memory barrier in read_seqcount_begin of child is
1439 * enough, we can use __read_seqcount_retry here.
1440 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001441 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1442 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001443 nd->seq = seq;
Al Viro5a18fff2011-03-11 04:44:53 -05001444
Al Viro24643082011-02-15 01:26:22 -05001445 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
Al Viro4ce16ef32012-06-10 16:10:59 -04001446 status = d_revalidate(dentry, nd->flags);
Al Viro5a18fff2011-03-11 04:44:53 -05001447 if (unlikely(status <= 0)) {
1448 if (status != -ECHILD)
1449 need_reval = 0;
1450 goto unlazy;
1451 }
Al Viro24643082011-02-15 01:26:22 -05001452 }
Nick Piggin31e6b012011-01-07 17:49:52 +11001453 path->mnt = mnt;
1454 path->dentry = dentry;
NeilBrownb8faf032014-08-04 17:06:29 +10001455 if (likely(__follow_mount_rcu(nd, path, inode)))
1456 return 0;
Al Viro5a18fff2011-03-11 04:44:53 -05001457unlazy:
Al Viro19660af2011-03-25 10:32:48 -04001458 if (unlazy_walk(nd, dentry))
1459 return -ECHILD;
Al Viro5a18fff2011-03-11 04:44:53 -05001460 } else {
Al Viroe97cdc82013-01-24 18:16:00 -05001461 dentry = __d_lookup(parent, &nd->last);
Nick Piggin31e6b012011-01-07 17:49:52 +11001462 }
Al Viro5a18fff2011-03-11 04:44:53 -05001463
Al Viro81e6f522012-03-30 14:48:04 -04001464 if (unlikely(!dentry))
1465 goto need_lookup;
Al Viro5a18fff2011-03-11 04:44:53 -05001466
Al Viro5a18fff2011-03-11 04:44:53 -05001467 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
Al Viro4ce16ef32012-06-10 16:10:59 -04001468 status = d_revalidate(dentry, nd->flags);
Al Viro5a18fff2011-03-11 04:44:53 -05001469 if (unlikely(status <= 0)) {
1470 if (status < 0) {
1471 dput(dentry);
1472 return status;
1473 }
Eric W. Biederman5542aa22014-02-13 09:46:25 -08001474 d_invalidate(dentry);
1475 dput(dentry);
1476 goto need_lookup;
Al Viro5a18fff2011-03-11 04:44:53 -05001477 }
Miklos Szeredi697f5142012-05-21 17:30:05 +02001478
Al Viro766c4cb2015-05-07 19:24:57 -04001479 if (unlikely(d_is_negative(dentry))) {
1480 dput(dentry);
1481 return -ENOENT;
1482 }
David Howells9875cf82011-01-14 18:45:21 +00001483 path->mnt = mnt;
1484 path->dentry = dentry;
1485 err = follow_managed(path, nd->flags);
Ian Kent89312212011-01-18 12:06:10 +08001486 if (unlikely(err < 0)) {
1487 path_put_conditional(path, nd);
David Howells9875cf82011-01-14 18:45:21 +00001488 return err;
Ian Kent89312212011-01-18 12:06:10 +08001489 }
Al Viroa3fbbde2011-11-07 21:21:26 +00001490 if (err)
1491 nd->flags |= LOOKUP_JUMPED;
David Howells9875cf82011-01-14 18:45:21 +00001492 *inode = path->dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 return 0;
Al Viro81e6f522012-03-30 14:48:04 -04001494
1495need_lookup:
Miklos Szeredi697f5142012-05-21 17:30:05 +02001496 return 1;
1497}
1498
1499/* Fast lookup failed, do it the slow way */
Al Virocc2a5272013-01-24 18:19:49 -05001500static int lookup_slow(struct nameidata *nd, struct path *path)
Miklos Szeredi697f5142012-05-21 17:30:05 +02001501{
1502 struct dentry *dentry, *parent;
1503 int err;
1504
1505 parent = nd->path.dentry;
Al Viro81e6f522012-03-30 14:48:04 -04001506 BUG_ON(nd->inode != parent->d_inode);
1507
1508 mutex_lock(&parent->d_inode->i_mutex);
Al Virocc2a5272013-01-24 18:19:49 -05001509 dentry = __lookup_hash(&nd->last, parent, nd->flags);
Al Viro81e6f522012-03-30 14:48:04 -04001510 mutex_unlock(&parent->d_inode->i_mutex);
1511 if (IS_ERR(dentry))
1512 return PTR_ERR(dentry);
Miklos Szeredi697f5142012-05-21 17:30:05 +02001513 path->mnt = nd->path.mnt;
1514 path->dentry = dentry;
1515 err = follow_managed(path, nd->flags);
1516 if (unlikely(err < 0)) {
1517 path_put_conditional(path, nd);
1518 return err;
1519 }
1520 if (err)
1521 nd->flags |= LOOKUP_JUMPED;
1522 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523}
1524
Al Viro52094c82011-02-21 21:34:47 -05001525static inline int may_lookup(struct nameidata *nd)
1526{
1527 if (nd->flags & LOOKUP_RCU) {
Al Viro4ad5abb2011-06-20 19:57:03 -04001528 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
Al Viro52094c82011-02-21 21:34:47 -05001529 if (err != -ECHILD)
1530 return err;
Al Viro19660af2011-03-25 10:32:48 -04001531 if (unlazy_walk(nd, NULL))
Al Viro52094c82011-02-21 21:34:47 -05001532 return -ECHILD;
1533 }
Al Viro4ad5abb2011-06-20 19:57:03 -04001534 return inode_permission(nd->inode, MAY_EXEC);
Al Viro52094c82011-02-21 21:34:47 -05001535}
1536
Al Viro9856fa12011-03-04 14:22:06 -05001537static inline int handle_dots(struct nameidata *nd, int type)
1538{
1539 if (type == LAST_DOTDOT) {
1540 if (nd->flags & LOOKUP_RCU) {
1541 if (follow_dotdot_rcu(nd))
1542 return -ECHILD;
1543 } else
1544 follow_dotdot(nd);
1545 }
1546 return 0;
1547}
1548
Al Viro951361f2011-03-04 14:44:37 -05001549static void terminate_walk(struct nameidata *nd)
1550{
1551 if (!(nd->flags & LOOKUP_RCU)) {
1552 path_put(&nd->path);
1553 } else {
1554 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001555 if (!(nd->flags & LOOKUP_ROOT))
1556 nd->root.mnt = NULL;
Al Viro8b61e742013-11-08 12:45:01 -05001557 rcu_read_unlock();
Al Viro951361f2011-03-04 14:44:37 -05001558 }
1559}
1560
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001561/*
1562 * Do we need to follow links? We _really_ want to be able
1563 * to do this check without having to look at inode->i_op,
1564 * so we keep a cache of "no, this doesn't need follow_link"
1565 * for the common case.
1566 */
David Howellsb18825a2013-09-12 19:22:53 +01001567static inline int should_follow_link(struct dentry *dentry, int follow)
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001568{
David Howellsb18825a2013-09-12 19:22:53 +01001569 return unlikely(d_is_symlink(dentry)) ? follow : 0;
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001570}
1571
Al Viroce57dfc2011-03-13 19:58:58 -04001572static inline int walk_component(struct nameidata *nd, struct path *path,
Al Viro21b9b072013-01-24 18:10:25 -05001573 int follow)
Al Viroce57dfc2011-03-13 19:58:58 -04001574{
1575 struct inode *inode;
1576 int err;
1577 /*
1578 * "." and ".." are special - ".." especially so because it has
1579 * to be able to know about the current root directory and
1580 * parent relationships.
1581 */
Al Viro21b9b072013-01-24 18:10:25 -05001582 if (unlikely(nd->last_type != LAST_NORM))
1583 return handle_dots(nd, nd->last_type);
Al Viroe97cdc82013-01-24 18:16:00 -05001584 err = lookup_fast(nd, path, &inode);
Al Viroce57dfc2011-03-13 19:58:58 -04001585 if (unlikely(err)) {
Miklos Szeredi697f5142012-05-21 17:30:05 +02001586 if (err < 0)
1587 goto out_err;
1588
Al Virocc2a5272013-01-24 18:19:49 -05001589 err = lookup_slow(nd, path);
Miklos Szeredi697f5142012-05-21 17:30:05 +02001590 if (err < 0)
1591 goto out_err;
1592
1593 inode = path->dentry->d_inode;
Al Viro766c4cb2015-05-07 19:24:57 -04001594 err = -ENOENT;
1595 if (d_is_negative(path->dentry))
1596 goto out_path_put;
Al Viroce57dfc2011-03-13 19:58:58 -04001597 }
Miklos Szeredi697f5142012-05-21 17:30:05 +02001598
David Howellsb18825a2013-09-12 19:22:53 +01001599 if (should_follow_link(path->dentry, follow)) {
Al Viro19660af2011-03-25 10:32:48 -04001600 if (nd->flags & LOOKUP_RCU) {
Al Viro3cab9892015-04-24 15:47:07 -04001601 if (unlikely(nd->path.mnt != path->mnt ||
1602 unlazy_walk(nd, path->dentry))) {
Miklos Szeredi697f5142012-05-21 17:30:05 +02001603 err = -ECHILD;
1604 goto out_err;
Al Viro19660af2011-03-25 10:32:48 -04001605 }
1606 }
Al Viroce57dfc2011-03-13 19:58:58 -04001607 BUG_ON(inode != path->dentry->d_inode);
1608 return 1;
1609 }
1610 path_to_nameidata(path, nd);
1611 nd->inode = inode;
1612 return 0;
Miklos Szeredi697f5142012-05-21 17:30:05 +02001613
1614out_path_put:
1615 path_to_nameidata(path, nd);
1616out_err:
1617 terminate_walk(nd);
1618 return err;
Al Viroce57dfc2011-03-13 19:58:58 -04001619}
1620
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621/*
Al Virob3563792011-03-14 21:54:55 -04001622 * This limits recursive symlink follows to 8, while
1623 * limiting consecutive symlinks to 40.
1624 *
1625 * Without that kind of total limit, nasty chains of consecutive
1626 * symlinks can cause almost arbitrarily long lookups.
1627 */
1628static inline int nested_symlink(struct path *path, struct nameidata *nd)
1629{
1630 int res;
1631
Al Virob3563792011-03-14 21:54:55 -04001632 if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1633 path_put_conditional(path, nd);
1634 path_put(&nd->path);
1635 return -ELOOP;
1636 }
Erez Zadok1a4022f2011-05-21 01:19:59 -04001637 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
Al Virob3563792011-03-14 21:54:55 -04001638
1639 nd->depth++;
1640 current->link_count++;
1641
1642 do {
1643 struct path link = *path;
1644 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04001645
1646 res = follow_link(&link, nd, &cookie);
Al Viro6d7b5aa2012-06-10 04:15:17 -04001647 if (res)
1648 break;
Al Viro21b9b072013-01-24 18:10:25 -05001649 res = walk_component(nd, path, LOOKUP_FOLLOW);
Al Viro574197e2011-03-14 22:20:34 -04001650 put_link(nd, &link, cookie);
Al Virob3563792011-03-14 21:54:55 -04001651 } while (res > 0);
1652
1653 current->link_count--;
1654 nd->depth--;
1655 return res;
1656}
1657
1658/*
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001659 * We can do the critical dentry name comparison and hashing
1660 * operations one word at a time, but we are limited to:
1661 *
1662 * - Architectures with fast unaligned word accesses. We could
1663 * do a "get_unaligned()" if this helps and is sufficiently
1664 * fast.
1665 *
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001666 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1667 * do not trap on the (extremely unlikely) case of a page
1668 * crossing operation.
1669 *
1670 * - Furthermore, we need an efficient 64-bit compile for the
1671 * 64-bit case in order to generate the "number of bytes in
1672 * the final mask". Again, that could be replaced with a
1673 * efficient population count instruction or similar.
1674 */
1675#ifdef CONFIG_DCACHE_WORD_ACCESS
1676
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001677#include <asm/word-at-a-time.h>
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001678
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001679#ifdef CONFIG_64BIT
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001680
1681static inline unsigned int fold_hash(unsigned long hash)
1682{
Linus Torvalds99d263d2014-09-13 11:30:10 -07001683 return hash_64(hash, 32);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001684}
1685
1686#else /* 32-bit case */
1687
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001688#define fold_hash(x) (x)
1689
1690#endif
1691
1692unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1693{
1694 unsigned long a, mask;
1695 unsigned long hash = 0;
1696
1697 for (;;) {
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001698 a = load_unaligned_zeropad(name);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001699 if (len < sizeof(unsigned long))
1700 break;
1701 hash += a;
Al Virof132c5b2012-03-22 21:59:52 +00001702 hash *= 9;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001703 name += sizeof(unsigned long);
1704 len -= sizeof(unsigned long);
1705 if (!len)
1706 goto done;
1707 }
Will Deacona5c21dc2013-12-12 17:40:21 +00001708 mask = bytemask_from_count(len);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001709 hash += mask & a;
1710done:
1711 return fold_hash(hash);
1712}
1713EXPORT_SYMBOL(full_name_hash);
1714
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001715/*
1716 * Calculate the length and hash of the path component, and
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07001717 * return the "hash_len" as the result.
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001718 */
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07001719static inline u64 hash_name(const char *name)
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001720{
Linus Torvalds36126f82012-05-26 10:43:17 -07001721 unsigned long a, b, adata, bdata, mask, hash, len;
1722 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001723
1724 hash = a = 0;
1725 len = -sizeof(unsigned long);
1726 do {
1727 hash = (hash + a) * 9;
1728 len += sizeof(unsigned long);
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001729 a = load_unaligned_zeropad(name+len);
Linus Torvalds36126f82012-05-26 10:43:17 -07001730 b = a ^ REPEAT_BYTE('/');
1731 } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001732
Linus Torvalds36126f82012-05-26 10:43:17 -07001733 adata = prep_zero_mask(a, adata, &constants);
1734 bdata = prep_zero_mask(b, bdata, &constants);
1735
1736 mask = create_zero_mask(adata | bdata);
1737
1738 hash += a & zero_bytemask(mask);
Linus Torvalds9226b5b2014-09-14 17:28:32 -07001739 len += find_zero(mask);
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07001740 return hashlen_create(fold_hash(hash), len);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001741}
1742
1743#else
1744
Linus Torvalds0145acc2012-03-02 14:32:59 -08001745unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1746{
1747 unsigned long hash = init_name_hash();
1748 while (len--)
1749 hash = partial_name_hash(*name++, hash);
1750 return end_name_hash(hash);
1751}
Linus Torvaldsae942ae2012-03-02 19:40:57 -08001752EXPORT_SYMBOL(full_name_hash);
Linus Torvalds0145acc2012-03-02 14:32:59 -08001753
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001754/*
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001755 * We know there's a real path component here of at least
1756 * one character.
1757 */
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07001758static inline u64 hash_name(const char *name)
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001759{
1760 unsigned long hash = init_name_hash();
1761 unsigned long len = 0, c;
1762
1763 c = (unsigned char)*name;
1764 do {
1765 len++;
1766 hash = partial_name_hash(c, hash);
1767 c = (unsigned char)name[len];
1768 } while (c && c != '/');
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07001769 return hashlen_create(end_name_hash(hash), len);
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001770}
1771
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001772#endif
1773
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001774/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01001776 * This is the basic name resolution function, turning a pathname into
1777 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01001779 * Returns 0 and nd will have valid dentry and mnt on success.
1780 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 */
Al Viro6de88d72009-08-09 01:41:57 +04001782static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783{
1784 struct path next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
1787 while (*name=='/')
1788 name++;
1789 if (!*name)
Al Viro086e1832011-02-22 20:56:27 -05001790 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 /* At this point we know we have a real path component. */
1793 for(;;) {
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07001794 u64 hash_len;
Al Virofe479a52011-02-22 15:10:03 -05001795 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Al Viro52094c82011-02-21 21:34:47 -05001797 err = may_lookup(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 if (err)
1799 break;
1800
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07001801 hash_len = hash_name(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
Al Virofe479a52011-02-22 15:10:03 -05001803 type = LAST_NORM;
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07001804 if (name[0] == '.') switch (hashlen_len(hash_len)) {
Al Virofe479a52011-02-22 15:10:03 -05001805 case 2:
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001806 if (name[1] == '.') {
Al Virofe479a52011-02-22 15:10:03 -05001807 type = LAST_DOTDOT;
Al Viro16c2cd72011-02-22 15:50:10 -05001808 nd->flags |= LOOKUP_JUMPED;
1809 }
Al Virofe479a52011-02-22 15:10:03 -05001810 break;
1811 case 1:
1812 type = LAST_DOT;
1813 }
Al Viro5a202bc2011-03-08 14:17:44 -05001814 if (likely(type == LAST_NORM)) {
1815 struct dentry *parent = nd->path.dentry;
Al Viro16c2cd72011-02-22 15:50:10 -05001816 nd->flags &= ~LOOKUP_JUMPED;
Al Viro5a202bc2011-03-08 14:17:44 -05001817 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
James Hogana060dc52014-09-16 13:07:35 +01001818 struct qstr this = { { .hash_len = hash_len }, .name = name };
Linus Torvaldsda53be12013-05-21 15:22:44 -07001819 err = parent->d_op->d_hash(parent, &this);
Al Viro5a202bc2011-03-08 14:17:44 -05001820 if (err < 0)
1821 break;
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07001822 hash_len = this.hash_len;
1823 name = this.name;
Al Viro5a202bc2011-03-08 14:17:44 -05001824 }
1825 }
Al Virofe479a52011-02-22 15:10:03 -05001826
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07001827 nd->last.hash_len = hash_len;
1828 nd->last.name = name;
Al Viro5f4a6a62013-01-24 18:04:22 -05001829 nd->last_type = type;
1830
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07001831 name += hashlen_len(hash_len);
1832 if (!*name)
Al Viro5f4a6a62013-01-24 18:04:22 -05001833 return 0;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001834 /*
1835 * If it wasn't NUL, we know it was '/'. Skip that
1836 * slash, and continue until no more slashes.
1837 */
1838 do {
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07001839 name++;
1840 } while (unlikely(*name == '/'));
1841 if (!*name)
Al Viro5f4a6a62013-01-24 18:04:22 -05001842 return 0;
1843
Al Viro21b9b072013-01-24 18:10:25 -05001844 err = walk_component(nd, &next, LOOKUP_FOLLOW);
Al Viroce57dfc2011-03-13 19:58:58 -04001845 if (err < 0)
1846 return err;
Al Virofe479a52011-02-22 15:10:03 -05001847
Al Viroce57dfc2011-03-13 19:58:58 -04001848 if (err) {
Al Virob3563792011-03-14 21:54:55 -04001849 err = nested_symlink(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 if (err)
Al Viroa7472ba2011-03-04 14:39:30 -05001851 return err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001852 }
Miklos Szeredi44b1d532014-04-01 17:08:41 +02001853 if (!d_can_lookup(nd->path.dentry)) {
Al Viro5f4a6a62013-01-24 18:04:22 -05001854 err = -ENOTDIR;
1855 break;
1856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 }
Al Viro951361f2011-03-04 14:44:37 -05001858 terminate_walk(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 return err;
1860}
1861
Al Viro6e8a1f82015-02-22 19:46:04 -05001862static int path_init(int dfd, const struct filename *name, unsigned int flags,
Al Viro5e530842014-11-20 14:14:42 -05001863 struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864{
Prasanna Medaea3834d2005-04-29 16:00:17 +01001865 int retval = 0;
Al Virofd2f7cb2015-02-22 20:07:13 -05001866 const char *s = name->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
1868 nd->last_type = LAST_ROOT; /* if there are only slashes... */
Al Viro980f3ea2014-11-20 14:20:24 -05001869 nd->flags = flags | LOOKUP_JUMPED | LOOKUP_PARENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 nd->depth = 0;
Al Viro5e530842014-11-20 14:14:42 -05001871 nd->base = NULL;
Al Viro5b6ca022011-03-09 23:04:47 -05001872 if (flags & LOOKUP_ROOT) {
David Howellsb18825a2013-09-12 19:22:53 +01001873 struct dentry *root = nd->root.dentry;
1874 struct inode *inode = root->d_inode;
Al Virofd2f7cb2015-02-22 20:07:13 -05001875 if (*s) {
Miklos Szeredi44b1d532014-04-01 17:08:41 +02001876 if (!d_can_lookup(root))
Al Viro73d049a2011-03-11 12:08:24 -05001877 return -ENOTDIR;
1878 retval = inode_permission(inode, MAY_EXEC);
1879 if (retval)
1880 return retval;
1881 }
Al Viro5b6ca022011-03-09 23:04:47 -05001882 nd->path = nd->root;
1883 nd->inode = inode;
1884 if (flags & LOOKUP_RCU) {
Al Viro8b61e742013-11-08 12:45:01 -05001885 rcu_read_lock();
Al Viro5b6ca022011-03-09 23:04:47 -05001886 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro48a066e2013-09-29 22:06:07 -04001887 nd->m_seq = read_seqbegin(&mount_lock);
Al Viro5b6ca022011-03-09 23:04:47 -05001888 } else {
1889 path_get(&nd->path);
1890 }
Al Virod4658872014-11-20 14:23:33 -05001891 goto done;
Al Viro5b6ca022011-03-09 23:04:47 -05001892 }
1893
Al Viro2a737872009-04-07 11:49:53 -04001894 nd->root.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Al Viro48a066e2013-09-29 22:06:07 -04001896 nd->m_seq = read_seqbegin(&mount_lock);
Al Virofd2f7cb2015-02-22 20:07:13 -05001897 if (*s == '/') {
Al Viroe41f7d42011-02-22 14:02:58 -05001898 if (flags & LOOKUP_RCU) {
Al Viro8b61e742013-11-08 12:45:01 -05001899 rcu_read_lock();
Al Viro7bd88372014-09-13 21:55:46 -04001900 nd->seq = set_root_rcu(nd);
Al Viroe41f7d42011-02-22 14:02:58 -05001901 } else {
1902 set_root(nd);
1903 path_get(&nd->root);
1904 }
Al Viro2a737872009-04-07 11:49:53 -04001905 nd->path = nd->root;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001906 } else if (dfd == AT_FDCWD) {
Al Viroe41f7d42011-02-22 14:02:58 -05001907 if (flags & LOOKUP_RCU) {
1908 struct fs_struct *fs = current->fs;
1909 unsigned seq;
1910
Al Viro8b61e742013-11-08 12:45:01 -05001911 rcu_read_lock();
Al Viroe41f7d42011-02-22 14:02:58 -05001912
1913 do {
1914 seq = read_seqcount_begin(&fs->seq);
1915 nd->path = fs->pwd;
1916 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1917 } while (read_seqcount_retry(&fs->seq, seq));
1918 } else {
1919 get_fs_pwd(current->fs, &nd->path);
1920 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001921 } else {
Jeff Layton582aa642012-12-11 08:56:16 -05001922 /* Caller must check execute permissions on the starting path component */
Al Viro2903ff02012-08-28 12:52:22 -04001923 struct fd f = fdget_raw(dfd);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001924 struct dentry *dentry;
1925
Al Viro2903ff02012-08-28 12:52:22 -04001926 if (!f.file)
1927 return -EBADF;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001928
Al Viro2903ff02012-08-28 12:52:22 -04001929 dentry = f.file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001930
Al Virofd2f7cb2015-02-22 20:07:13 -05001931 if (*s) {
Miklos Szeredi44b1d532014-04-01 17:08:41 +02001932 if (!d_can_lookup(dentry)) {
Al Viro2903ff02012-08-28 12:52:22 -04001933 fdput(f);
1934 return -ENOTDIR;
1935 }
Al Virof52e0c12011-03-14 18:56:51 -04001936 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001937
Al Viro2903ff02012-08-28 12:52:22 -04001938 nd->path = f.file->f_path;
Al Viroe41f7d42011-02-22 14:02:58 -05001939 if (flags & LOOKUP_RCU) {
Linus Torvalds9c225f22014-03-03 09:36:58 -08001940 if (f.flags & FDPUT_FPUT)
Al Viro5e530842014-11-20 14:14:42 -05001941 nd->base = f.file;
Al Viroe41f7d42011-02-22 14:02:58 -05001942 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro8b61e742013-11-08 12:45:01 -05001943 rcu_read_lock();
Al Viroe41f7d42011-02-22 14:02:58 -05001944 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001945 path_get(&nd->path);
1946 fdput(f);
Al Viroe41f7d42011-02-22 14:02:58 -05001947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 }
Al Viroe41f7d42011-02-22 14:02:58 -05001949
Nick Piggin31e6b012011-01-07 17:49:52 +11001950 nd->inode = nd->path.dentry->d_inode;
Al Viro4023bfc2014-09-13 21:59:43 -04001951 if (!(flags & LOOKUP_RCU))
Al Virod4658872014-11-20 14:23:33 -05001952 goto done;
Al Viro4023bfc2014-09-13 21:59:43 -04001953 if (likely(!read_seqcount_retry(&nd->path.dentry->d_seq, nd->seq)))
Al Virod4658872014-11-20 14:23:33 -05001954 goto done;
Al Viro4023bfc2014-09-13 21:59:43 -04001955 if (!(nd->flags & LOOKUP_ROOT))
1956 nd->root.mnt = NULL;
1957 rcu_read_unlock();
1958 return -ECHILD;
Al Virod4658872014-11-20 14:23:33 -05001959done:
1960 current->total_link_count = 0;
Al Virofd2f7cb2015-02-22 20:07:13 -05001961 return link_path_walk(s, nd);
Al Viro9b4a9b12009-04-07 11:44:16 -04001962}
1963
Al Viro893b7772014-11-20 14:18:09 -05001964static void path_cleanup(struct nameidata *nd)
1965{
1966 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
1967 path_put(&nd->root);
1968 nd->root.mnt = NULL;
1969 }
1970 if (unlikely(nd->base))
1971 fput(nd->base);
Al Viro9b4a9b12009-04-07 11:44:16 -04001972}
1973
Al Virobd92d7f2011-03-14 19:54:59 -04001974static inline int lookup_last(struct nameidata *nd, struct path *path)
1975{
1976 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1977 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1978
1979 nd->flags &= ~LOOKUP_PARENT;
Al Viro21b9b072013-01-24 18:10:25 -05001980 return walk_component(nd, path, nd->flags & LOOKUP_FOLLOW);
Al Virobd92d7f2011-03-14 19:54:59 -04001981}
1982
Al Viro9b4a9b12009-04-07 11:44:16 -04001983/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viro5eb6b492015-02-22 19:40:53 -05001984static int path_lookupat(int dfd, const struct filename *name,
Al Viro9b4a9b12009-04-07 11:44:16 -04001985 unsigned int flags, struct nameidata *nd)
1986{
Al Virobd92d7f2011-03-14 19:54:59 -04001987 struct path path;
1988 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001989
1990 /*
1991 * Path walking is largely split up into 2 different synchronisation
1992 * schemes, rcu-walk and ref-walk (explained in
1993 * Documentation/filesystems/path-lookup.txt). These share much of the
1994 * path walk code, but some things particularly setup, cleanup, and
1995 * following mounts are sufficiently divergent that functions are
1996 * duplicated. Typically there is a function foo(), and its RCU
1997 * analogue, foo_rcu().
1998 *
1999 * -ECHILD is the error number of choice (just to avoid clashes) that
2000 * is returned if some aspect of an rcu-walk fails. Such an error must
2001 * be handled by restarting a traditional ref-walk (which will always
2002 * be able to complete).
2003 */
Al Viro6e8a1f82015-02-22 19:46:04 -05002004 err = path_init(dfd, name, flags, nd);
Al Virobd92d7f2011-03-14 19:54:59 -04002005 if (!err && !(flags & LOOKUP_PARENT)) {
Al Virobd92d7f2011-03-14 19:54:59 -04002006 err = lookup_last(nd, &path);
2007 while (err > 0) {
2008 void *cookie;
2009 struct path link = path;
Kees Cook800179c2012-07-25 17:29:07 -07002010 err = may_follow_link(&link, nd);
2011 if (unlikely(err))
2012 break;
Al Virobd92d7f2011-03-14 19:54:59 -04002013 nd->flags |= LOOKUP_PARENT;
Al Viro574197e2011-03-14 22:20:34 -04002014 err = follow_link(&link, nd, &cookie);
Al Viro6d7b5aa2012-06-10 04:15:17 -04002015 if (err)
2016 break;
2017 err = lookup_last(nd, &path);
Al Viro574197e2011-03-14 22:20:34 -04002018 put_link(nd, &link, cookie);
Al Virobd92d7f2011-03-14 19:54:59 -04002019 }
2020 }
Al Viroee0827c2011-02-21 23:38:09 -05002021
Al Viro9f1fafe2011-03-25 11:00:12 -04002022 if (!err)
2023 err = complete_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04002024
2025 if (!err && nd->flags & LOOKUP_DIRECTORY) {
Miklos Szeredi44b1d532014-04-01 17:08:41 +02002026 if (!d_can_lookup(nd->path.dentry)) {
Al Virobd92d7f2011-03-14 19:54:59 -04002027 path_put(&nd->path);
Al Virobd23a532011-03-23 09:56:30 -04002028 err = -ENOTDIR;
Al Virobd92d7f2011-03-14 19:54:59 -04002029 }
2030 }
Al Viro16c2cd72011-02-22 15:50:10 -05002031
Al Viro893b7772014-11-20 14:18:09 -05002032 path_cleanup(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04002033 return err;
Al Viroee0827c2011-02-21 23:38:09 -05002034}
Nick Piggin31e6b012011-01-07 17:49:52 +11002035
Jeff Layton873f1ee2012-10-10 15:25:29 -04002036static int filename_lookup(int dfd, struct filename *name,
2037 unsigned int flags, struct nameidata *nd)
2038{
Al Viro5eb6b492015-02-22 19:40:53 -05002039 int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002040 if (unlikely(retval == -ECHILD))
Al Viro5eb6b492015-02-22 19:40:53 -05002041 retval = path_lookupat(dfd, name, flags, nd);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002042 if (unlikely(retval == -ESTALE))
Al Viro5eb6b492015-02-22 19:40:53 -05002043 retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002044
2045 if (likely(!retval))
Jeff Laytonadb5c242012-10-10 16:43:13 -04002046 audit_inode(name, nd->path.dentry, flags & LOOKUP_PARENT);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002047 return retval;
2048}
2049
Al Viro79714f72012-06-15 03:01:42 +04002050/* does lookup, returns the object with parent locked */
2051struct dentry *kern_path_locked(const char *name, struct path *path)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002052{
Paul Moore51689102015-01-22 00:00:03 -05002053 struct filename *filename = getname_kernel(name);
Al Viro79714f72012-06-15 03:01:42 +04002054 struct nameidata nd;
2055 struct dentry *d;
Paul Moore51689102015-01-22 00:00:03 -05002056 int err;
2057
2058 if (IS_ERR(filename))
2059 return ERR_CAST(filename);
2060
2061 err = filename_lookup(AT_FDCWD, filename, LOOKUP_PARENT, &nd);
2062 if (err) {
2063 d = ERR_PTR(err);
2064 goto out;
2065 }
Al Viro79714f72012-06-15 03:01:42 +04002066 if (nd.last_type != LAST_NORM) {
2067 path_put(&nd.path);
Paul Moore51689102015-01-22 00:00:03 -05002068 d = ERR_PTR(-EINVAL);
2069 goto out;
Al Viro79714f72012-06-15 03:01:42 +04002070 }
2071 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Al Viro1e0ea002012-07-22 23:46:21 +04002072 d = __lookup_hash(&nd.last, nd.path.dentry, 0);
Al Viro79714f72012-06-15 03:01:42 +04002073 if (IS_ERR(d)) {
2074 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2075 path_put(&nd.path);
Paul Moore51689102015-01-22 00:00:03 -05002076 goto out;
Al Viro79714f72012-06-15 03:01:42 +04002077 }
2078 *path = nd.path;
Paul Moore51689102015-01-22 00:00:03 -05002079out:
2080 putname(filename);
Al Viro79714f72012-06-15 03:01:42 +04002081 return d;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002082}
2083
Al Virod1811462008-08-02 00:49:18 -04002084int kern_path(const char *name, unsigned int flags, struct path *path)
2085{
2086 struct nameidata nd;
Al Viro74eb8cc2015-02-23 02:44:36 -05002087 struct filename *filename = getname_kernel(name);
2088 int res = PTR_ERR(filename);
2089
2090 if (!IS_ERR(filename)) {
2091 res = filename_lookup(AT_FDCWD, filename, flags, &nd);
2092 putname(filename);
2093 if (!res)
2094 *path = nd.path;
2095 }
Al Virod1811462008-08-02 00:49:18 -04002096 return res;
2097}
Al Viro4d359502014-03-14 12:20:17 -04002098EXPORT_SYMBOL(kern_path);
Al Virod1811462008-08-02 00:49:18 -04002099
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002100/**
2101 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2102 * @dentry: pointer to dentry of the base directory
2103 * @mnt: pointer to vfs mount of the base directory
2104 * @name: pointer to file name
2105 * @flags: lookup flags
Al Viroe0a01242011-06-27 17:00:37 -04002106 * @path: pointer to struct path to fill
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002107 */
2108int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2109 const char *name, unsigned int flags,
Al Viroe0a01242011-06-27 17:00:37 -04002110 struct path *path)
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002111{
Al Viro74eb8cc2015-02-23 02:44:36 -05002112 struct filename *filename = getname_kernel(name);
2113 int err = PTR_ERR(filename);
2114
Al Viroe0a01242011-06-27 17:00:37 -04002115 BUG_ON(flags & LOOKUP_PARENT);
Al Viro74eb8cc2015-02-23 02:44:36 -05002116
2117 /* the first argument of filename_lookup() is ignored with LOOKUP_ROOT */
2118 if (!IS_ERR(filename)) {
2119 struct nameidata nd;
2120 nd.root.dentry = dentry;
2121 nd.root.mnt = mnt;
2122 err = filename_lookup(AT_FDCWD, filename,
2123 flags | LOOKUP_ROOT, &nd);
2124 if (!err)
2125 *path = nd.path;
2126 putname(filename);
2127 }
Al Viroe0a01242011-06-27 17:00:37 -04002128 return err;
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002129}
Al Viro4d359502014-03-14 12:20:17 -04002130EXPORT_SYMBOL(vfs_path_lookup);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002131
James Morris057f6c02007-04-26 00:12:05 -07002132/*
2133 * Restricted form of lookup. Doesn't follow links, single-component only,
2134 * needs parent already locked. Doesn't follow mounts.
2135 * SMP-safe.
2136 */
Adrian Bunka244e162006-03-31 02:32:11 -08002137static struct dentry *lookup_hash(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138{
Al Viro72bd8662012-06-10 17:17:17 -04002139 return __lookup_hash(&nd->last, nd->path.dentry, nd->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140}
2141
Christoph Hellwigeead1912007-10-16 23:25:38 -07002142/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07002143 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07002144 * @name: pathname component to lookup
2145 * @base: base directory to lookup from
2146 * @len: maximum length @len should be interpreted to
2147 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07002148 * Note that this routine is purely a helper for filesystem usage and should
Al Viro9e7543e2015-02-23 02:49:48 -05002149 * not be called by generic code.
Christoph Hellwigeead1912007-10-16 23:25:38 -07002150 */
James Morris057f6c02007-04-26 00:12:05 -07002151struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2152{
James Morris057f6c02007-04-26 00:12:05 -07002153 struct qstr this;
Al Viro6a96ba52011-03-07 23:49:20 -05002154 unsigned int c;
Miklos Szeredicda309d2012-03-26 12:54:21 +02002155 int err;
James Morris057f6c02007-04-26 00:12:05 -07002156
David Woodhouse2f9092e2009-04-20 23:18:37 +01002157 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
2158
Al Viro6a96ba52011-03-07 23:49:20 -05002159 this.name = name;
2160 this.len = len;
Linus Torvalds0145acc2012-03-02 14:32:59 -08002161 this.hash = full_name_hash(name, len);
Al Viro6a96ba52011-03-07 23:49:20 -05002162 if (!len)
2163 return ERR_PTR(-EACCES);
2164
Al Viro21d8a152012-11-29 22:17:21 -05002165 if (unlikely(name[0] == '.')) {
2166 if (len < 2 || (len == 2 && name[1] == '.'))
2167 return ERR_PTR(-EACCES);
2168 }
2169
Al Viro6a96ba52011-03-07 23:49:20 -05002170 while (len--) {
2171 c = *(const unsigned char *)name++;
2172 if (c == '/' || c == '\0')
2173 return ERR_PTR(-EACCES);
Al Viro6a96ba52011-03-07 23:49:20 -05002174 }
Al Viro5a202bc2011-03-08 14:17:44 -05002175 /*
2176 * See if the low-level filesystem might want
2177 * to use its own hash..
2178 */
2179 if (base->d_flags & DCACHE_OP_HASH) {
Linus Torvaldsda53be12013-05-21 15:22:44 -07002180 int err = base->d_op->d_hash(base, &this);
Al Viro5a202bc2011-03-08 14:17:44 -05002181 if (err < 0)
2182 return ERR_PTR(err);
2183 }
Christoph Hellwigeead1912007-10-16 23:25:38 -07002184
Miklos Szeredicda309d2012-03-26 12:54:21 +02002185 err = inode_permission(base->d_inode, MAY_EXEC);
2186 if (err)
2187 return ERR_PTR(err);
2188
Al Viro72bd8662012-06-10 17:17:17 -04002189 return __lookup_hash(&this, base, 0);
James Morris057f6c02007-04-26 00:12:05 -07002190}
Al Viro4d359502014-03-14 12:20:17 -04002191EXPORT_SYMBOL(lookup_one_len);
James Morris057f6c02007-04-26 00:12:05 -07002192
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002193int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2194 struct path *path, int *empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195{
Al Viro2d8f3032008-07-22 09:59:21 -04002196 struct nameidata nd;
Jeff Layton91a27b22012-10-10 15:25:28 -04002197 struct filename *tmp = getname_flags(name, flags, empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 int err = PTR_ERR(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 if (!IS_ERR(tmp)) {
Al Viro2d8f3032008-07-22 09:59:21 -04002200
2201 BUG_ON(flags & LOOKUP_PARENT);
2202
Jeff Layton873f1ee2012-10-10 15:25:29 -04002203 err = filename_lookup(dfd, tmp, flags, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 putname(tmp);
Al Viro2d8f3032008-07-22 09:59:21 -04002205 if (!err)
2206 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 }
2208 return err;
2209}
2210
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002211int user_path_at(int dfd, const char __user *name, unsigned flags,
2212 struct path *path)
2213{
Linus Torvaldsf7493e52012-03-22 16:10:40 -07002214 return user_path_at_empty(dfd, name, flags, path, NULL);
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002215}
Al Viro4d359502014-03-14 12:20:17 -04002216EXPORT_SYMBOL(user_path_at);
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002217
Jeff Layton873f1ee2012-10-10 15:25:29 -04002218/*
2219 * NB: most callers don't do anything directly with the reference to the
2220 * to struct filename, but the nd->last pointer points into the name string
2221 * allocated by getname. So we must hold the reference to it until all
2222 * path-walking is complete.
2223 */
Jeff Layton91a27b22012-10-10 15:25:28 -04002224static struct filename *
Jeff Layton9e790bd2012-12-11 12:10:09 -05002225user_path_parent(int dfd, const char __user *path, struct nameidata *nd,
2226 unsigned int flags)
Al Viro2ad94ae2008-07-21 09:32:51 -04002227{
Jeff Layton91a27b22012-10-10 15:25:28 -04002228 struct filename *s = getname(path);
Al Viro2ad94ae2008-07-21 09:32:51 -04002229 int error;
2230
Jeff Layton9e790bd2012-12-11 12:10:09 -05002231 /* only LOOKUP_REVAL is allowed in extra flags */
2232 flags &= LOOKUP_REVAL;
2233
Al Viro2ad94ae2008-07-21 09:32:51 -04002234 if (IS_ERR(s))
Jeff Layton91a27b22012-10-10 15:25:28 -04002235 return s;
Al Viro2ad94ae2008-07-21 09:32:51 -04002236
Jeff Layton9e790bd2012-12-11 12:10:09 -05002237 error = filename_lookup(dfd, s, flags | LOOKUP_PARENT, nd);
Jeff Layton91a27b22012-10-10 15:25:28 -04002238 if (error) {
Al Viro2ad94ae2008-07-21 09:32:51 -04002239 putname(s);
Jeff Layton91a27b22012-10-10 15:25:28 -04002240 return ERR_PTR(error);
2241 }
Al Viro2ad94ae2008-07-21 09:32:51 -04002242
Jeff Layton91a27b22012-10-10 15:25:28 -04002243 return s;
Al Viro2ad94ae2008-07-21 09:32:51 -04002244}
2245
Jeff Layton80334262013-07-26 06:23:25 -04002246/**
Al Viro197df042013-09-08 14:03:27 -04002247 * mountpoint_last - look up last component for umount
Jeff Layton80334262013-07-26 06:23:25 -04002248 * @nd: pathwalk nameidata - currently pointing at parent directory of "last"
2249 * @path: pointer to container for result
2250 *
2251 * This is a special lookup_last function just for umount. In this case, we
2252 * need to resolve the path without doing any revalidation.
2253 *
2254 * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
2255 * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
2256 * in almost all cases, this lookup will be served out of the dcache. The only
2257 * cases where it won't are if nd->last refers to a symlink or the path is
2258 * bogus and it doesn't exist.
2259 *
2260 * Returns:
2261 * -error: if there was an error during lookup. This includes -ENOENT if the
2262 * lookup found a negative dentry. The nd->path reference will also be
2263 * put in this case.
2264 *
2265 * 0: if we successfully resolved nd->path and found it to not to be a
2266 * symlink that needs to be followed. "path" will also be populated.
2267 * The nd->path reference will also be put.
2268 *
2269 * 1: if we successfully resolved nd->last and found it to be a symlink
2270 * that needs to be followed. "path" will be populated with the path
2271 * to the link, and nd->path will *not* be put.
2272 */
2273static int
Al Viro197df042013-09-08 14:03:27 -04002274mountpoint_last(struct nameidata *nd, struct path *path)
Jeff Layton80334262013-07-26 06:23:25 -04002275{
2276 int error = 0;
2277 struct dentry *dentry;
2278 struct dentry *dir = nd->path.dentry;
2279
Al Viro35759522013-09-08 13:41:33 -04002280 /* If we're in rcuwalk, drop out of it to handle last component */
2281 if (nd->flags & LOOKUP_RCU) {
2282 if (unlazy_walk(nd, NULL)) {
2283 error = -ECHILD;
2284 goto out;
2285 }
Jeff Layton80334262013-07-26 06:23:25 -04002286 }
2287
2288 nd->flags &= ~LOOKUP_PARENT;
2289
2290 if (unlikely(nd->last_type != LAST_NORM)) {
2291 error = handle_dots(nd, nd->last_type);
Al Viro35759522013-09-08 13:41:33 -04002292 if (error)
2293 goto out;
2294 dentry = dget(nd->path.dentry);
2295 goto done;
Jeff Layton80334262013-07-26 06:23:25 -04002296 }
2297
2298 mutex_lock(&dir->d_inode->i_mutex);
2299 dentry = d_lookup(dir, &nd->last);
2300 if (!dentry) {
2301 /*
2302 * No cached dentry. Mounted dentries are pinned in the cache,
2303 * so that means that this dentry is probably a symlink or the
2304 * path doesn't actually point to a mounted dentry.
2305 */
2306 dentry = d_alloc(dir, &nd->last);
2307 if (!dentry) {
2308 error = -ENOMEM;
Dave Jonesbcceeeb2013-09-10 17:04:25 -04002309 mutex_unlock(&dir->d_inode->i_mutex);
Al Viro35759522013-09-08 13:41:33 -04002310 goto out;
Jeff Layton80334262013-07-26 06:23:25 -04002311 }
Al Viro35759522013-09-08 13:41:33 -04002312 dentry = lookup_real(dir->d_inode, dentry, nd->flags);
2313 error = PTR_ERR(dentry);
Dave Jonesbcceeeb2013-09-10 17:04:25 -04002314 if (IS_ERR(dentry)) {
2315 mutex_unlock(&dir->d_inode->i_mutex);
Al Viro35759522013-09-08 13:41:33 -04002316 goto out;
Dave Jonesbcceeeb2013-09-10 17:04:25 -04002317 }
Jeff Layton80334262013-07-26 06:23:25 -04002318 }
2319 mutex_unlock(&dir->d_inode->i_mutex);
2320
Al Viro35759522013-09-08 13:41:33 -04002321done:
David Howells698934d2015-03-17 17:33:52 +00002322 if (d_is_negative(dentry)) {
Al Viro35759522013-09-08 13:41:33 -04002323 error = -ENOENT;
2324 dput(dentry);
2325 goto out;
Jeff Layton80334262013-07-26 06:23:25 -04002326 }
Al Viro35759522013-09-08 13:41:33 -04002327 path->dentry = dentry;
Vasily Averin295dc392014-07-21 12:30:23 +04002328 path->mnt = nd->path.mnt;
David Howellsb18825a2013-09-12 19:22:53 +01002329 if (should_follow_link(dentry, nd->flags & LOOKUP_FOLLOW))
Al Viro35759522013-09-08 13:41:33 -04002330 return 1;
Vasily Averin295dc392014-07-21 12:30:23 +04002331 mntget(path->mnt);
Al Viro35759522013-09-08 13:41:33 -04002332 follow_mount(path);
2333 error = 0;
2334out:
Jeff Layton80334262013-07-26 06:23:25 -04002335 terminate_walk(nd);
2336 return error;
2337}
2338
2339/**
Al Viro197df042013-09-08 14:03:27 -04002340 * path_mountpoint - look up a path to be umounted
Jeff Layton80334262013-07-26 06:23:25 -04002341 * @dfd: directory file descriptor to start walk from
2342 * @name: full pathname to walk
Randy Dunlap606d6fe2013-10-19 14:56:55 -07002343 * @path: pointer to container for result
Jeff Layton80334262013-07-26 06:23:25 -04002344 * @flags: lookup flags
Jeff Layton80334262013-07-26 06:23:25 -04002345 *
2346 * Look up the given name, but don't attempt to revalidate the last component.
Randy Dunlap606d6fe2013-10-19 14:56:55 -07002347 * Returns 0 and "path" will be valid on success; Returns error otherwise.
Jeff Layton80334262013-07-26 06:23:25 -04002348 */
2349static int
Al Viro668696d2015-02-22 19:44:00 -05002350path_mountpoint(int dfd, const struct filename *name, struct path *path,
2351 unsigned int flags)
Jeff Layton80334262013-07-26 06:23:25 -04002352{
Jeff Layton80334262013-07-26 06:23:25 -04002353 struct nameidata nd;
2354 int err;
2355
Al Viro6e8a1f82015-02-22 19:46:04 -05002356 err = path_init(dfd, name, flags, &nd);
Jeff Layton80334262013-07-26 06:23:25 -04002357 if (unlikely(err))
Al Viro115cbfd2014-10-11 23:05:52 -04002358 goto out;
Jeff Layton80334262013-07-26 06:23:25 -04002359
Al Viro197df042013-09-08 14:03:27 -04002360 err = mountpoint_last(&nd, path);
Jeff Layton80334262013-07-26 06:23:25 -04002361 while (err > 0) {
2362 void *cookie;
2363 struct path link = *path;
2364 err = may_follow_link(&link, &nd);
2365 if (unlikely(err))
2366 break;
2367 nd.flags |= LOOKUP_PARENT;
2368 err = follow_link(&link, &nd, &cookie);
2369 if (err)
2370 break;
Al Viro197df042013-09-08 14:03:27 -04002371 err = mountpoint_last(&nd, path);
Jeff Layton80334262013-07-26 06:23:25 -04002372 put_link(&nd, &link, cookie);
2373 }
2374out:
Al Viro893b7772014-11-20 14:18:09 -05002375 path_cleanup(&nd);
Jeff Layton80334262013-07-26 06:23:25 -04002376 return err;
2377}
2378
Al Viro2d864652013-09-08 20:18:44 -04002379static int
Al Viro668696d2015-02-22 19:44:00 -05002380filename_mountpoint(int dfd, struct filename *name, struct path *path,
Al Viro2d864652013-09-08 20:18:44 -04002381 unsigned int flags)
2382{
Al Virocbaab2d2015-01-22 02:49:00 -05002383 int error;
Al Viro668696d2015-02-22 19:44:00 -05002384 if (IS_ERR(name))
2385 return PTR_ERR(name);
2386 error = path_mountpoint(dfd, name, path, flags | LOOKUP_RCU);
Al Viro2d864652013-09-08 20:18:44 -04002387 if (unlikely(error == -ECHILD))
Al Viro668696d2015-02-22 19:44:00 -05002388 error = path_mountpoint(dfd, name, path, flags);
Al Viro2d864652013-09-08 20:18:44 -04002389 if (unlikely(error == -ESTALE))
Al Viro668696d2015-02-22 19:44:00 -05002390 error = path_mountpoint(dfd, name, path, flags | LOOKUP_REVAL);
Al Viro2d864652013-09-08 20:18:44 -04002391 if (likely(!error))
Al Viro668696d2015-02-22 19:44:00 -05002392 audit_inode(name, path->dentry, 0);
2393 putname(name);
Al Viro2d864652013-09-08 20:18:44 -04002394 return error;
2395}
2396
Jeff Layton80334262013-07-26 06:23:25 -04002397/**
Al Viro197df042013-09-08 14:03:27 -04002398 * user_path_mountpoint_at - lookup a path from userland in order to umount it
Jeff Layton80334262013-07-26 06:23:25 -04002399 * @dfd: directory file descriptor
2400 * @name: pathname from userland
2401 * @flags: lookup flags
2402 * @path: pointer to container to hold result
2403 *
2404 * A umount is a special case for path walking. We're not actually interested
2405 * in the inode in this situation, and ESTALE errors can be a problem. We
2406 * simply want track down the dentry and vfsmount attached at the mountpoint
2407 * and avoid revalidating the last component.
2408 *
2409 * Returns 0 and populates "path" on success.
2410 */
2411int
Al Viro197df042013-09-08 14:03:27 -04002412user_path_mountpoint_at(int dfd, const char __user *name, unsigned int flags,
Jeff Layton80334262013-07-26 06:23:25 -04002413 struct path *path)
2414{
Al Virocbaab2d2015-01-22 02:49:00 -05002415 return filename_mountpoint(dfd, getname(name), path, flags);
Jeff Layton80334262013-07-26 06:23:25 -04002416}
2417
Al Viro2d864652013-09-08 20:18:44 -04002418int
2419kern_path_mountpoint(int dfd, const char *name, struct path *path,
2420 unsigned int flags)
2421{
Al Virocbaab2d2015-01-22 02:49:00 -05002422 return filename_mountpoint(dfd, getname_kernel(name), path, flags);
Al Viro2d864652013-09-08 20:18:44 -04002423}
2424EXPORT_SYMBOL(kern_path_mountpoint);
2425
Miklos Szeredicbdf35b2014-10-24 00:14:36 +02002426int __check_sticky(struct inode *dir, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427{
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002428 kuid_t fsuid = current_fsuid();
David Howellsda9592e2008-11-14 10:39:05 +11002429
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002430 if (uid_eq(inode->i_uid, fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 return 0;
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002432 if (uid_eq(dir->i_uid, fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 return 0;
Andy Lutomirski23adbe12014-06-10 12:45:42 -07002434 return !capable_wrt_inode_uidgid(inode, CAP_FOWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435}
Miklos Szeredicbdf35b2014-10-24 00:14:36 +02002436EXPORT_SYMBOL(__check_sticky);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
2438/*
2439 * Check whether we can remove a link victim from directory dir, check
2440 * whether the type of victim is right.
2441 * 1. We can't do it if dir is read-only (done in permission())
2442 * 2. We should have write and exec permissions on dir
2443 * 3. We can't remove anything from append-only dir
2444 * 4. We can't do anything with immutable dir (done in permission())
2445 * 5. If the sticky bit on dir is set we should either
2446 * a. be owner of dir, or
2447 * b. be owner of victim, or
2448 * c. have CAP_FOWNER capability
2449 * 6. If the victim is append-only or immutable we can't do antyhing with
2450 * links pointing to it.
2451 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2452 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2453 * 9. We can't remove a root or mountpoint.
2454 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
2455 * nfs_async_unlink().
2456 */
David Howellsb18825a2013-09-12 19:22:53 +01002457static int may_delete(struct inode *dir, struct dentry *victim, bool isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458{
David Howellsb18825a2013-09-12 19:22:53 +01002459 struct inode *inode = victim->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 int error;
2461
David Howellsb18825a2013-09-12 19:22:53 +01002462 if (d_is_negative(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 return -ENOENT;
David Howellsb18825a2013-09-12 19:22:53 +01002464 BUG_ON(!inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
2466 BUG_ON(victim->d_parent->d_inode != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04002467 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
Al Virof419a2e2008-07-22 00:07:17 -04002469 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 if (error)
2471 return error;
2472 if (IS_APPEND(dir))
2473 return -EPERM;
David Howellsb18825a2013-09-12 19:22:53 +01002474
2475 if (check_sticky(dir, inode) || IS_APPEND(inode) ||
2476 IS_IMMUTABLE(inode) || IS_SWAPFILE(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 return -EPERM;
2478 if (isdir) {
Miklos Szeredi44b1d532014-04-01 17:08:41 +02002479 if (!d_is_dir(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 return -ENOTDIR;
2481 if (IS_ROOT(victim))
2482 return -EBUSY;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02002483 } else if (d_is_dir(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 return -EISDIR;
2485 if (IS_DEADDIR(dir))
2486 return -ENOENT;
2487 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2488 return -EBUSY;
2489 return 0;
2490}
2491
2492/* Check whether we can create an object with dentry child in directory
2493 * dir.
2494 * 1. We can't do it if child already exists (open has special treatment for
2495 * this case, but since we are inlined it's OK)
2496 * 2. We can't do it if dir is read-only (done in permission())
2497 * 3. We should have write and exec permissions on dir
2498 * 4. We can't do it if dir is immutable (done in permission())
2499 */
Miklos Szeredia95164d2008-07-30 15:08:48 +02002500static inline int may_create(struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501{
Jeff Layton14e972b2013-05-08 10:25:58 -04002502 audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 if (child->d_inode)
2504 return -EEXIST;
2505 if (IS_DEADDIR(dir))
2506 return -ENOENT;
Al Virof419a2e2008-07-22 00:07:17 -04002507 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508}
2509
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510/*
2511 * p1 and p2 should be directories on the same fs.
2512 */
2513struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2514{
2515 struct dentry *p;
2516
2517 if (p1 == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07002518 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 return NULL;
2520 }
2521
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002522 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002524 p = d_ancestor(p2, p1);
2525 if (p) {
2526 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2527 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
2528 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 }
2530
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002531 p = d_ancestor(p1, p2);
2532 if (p) {
2533 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2534 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2535 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 }
2537
Ingo Molnarf2eace22006-07-03 00:25:05 -07002538 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Miklos Szeredid1b72cc2014-10-27 15:42:01 +01002539 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 return NULL;
2541}
Al Viro4d359502014-03-14 12:20:17 -04002542EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543
2544void unlock_rename(struct dentry *p1, struct dentry *p2)
2545{
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002546 mutex_unlock(&p1->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 if (p1 != p2) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002548 mutex_unlock(&p2->d_inode->i_mutex);
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002549 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 }
2551}
Al Viro4d359502014-03-14 12:20:17 -04002552EXPORT_SYMBOL(unlock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553
Al Viro4acdaf22011-07-26 01:42:34 -04002554int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viro312b63f2012-06-10 18:09:36 -04002555 bool want_excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002557 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 if (error)
2559 return error;
2560
Al Viroacfa4382008-12-04 10:06:33 -05002561 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 return -EACCES; /* shouldn't it be ENOSYS? */
2563 mode &= S_IALLUGO;
2564 mode |= S_IFREG;
2565 error = security_inode_create(dir, dentry, mode);
2566 if (error)
2567 return error;
Al Viro312b63f2012-06-10 18:09:36 -04002568 error = dir->i_op->create(dir, dentry, mode, want_excl);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002569 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002570 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 return error;
2572}
Al Viro4d359502014-03-14 12:20:17 -04002573EXPORT_SYMBOL(vfs_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
Al Viro73d049a2011-03-11 12:08:24 -05002575static int may_open(struct path *path, int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002577 struct dentry *dentry = path->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 struct inode *inode = dentry->d_inode;
2579 int error;
2580
Al Virobcda7652011-03-13 16:42:14 -04002581 /* O_PATH? */
2582 if (!acc_mode)
2583 return 0;
2584
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 if (!inode)
2586 return -ENOENT;
2587
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002588 switch (inode->i_mode & S_IFMT) {
2589 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002591 case S_IFDIR:
2592 if (acc_mode & MAY_WRITE)
2593 return -EISDIR;
2594 break;
2595 case S_IFBLK:
2596 case S_IFCHR:
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002597 if (path->mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002599 /*FALLTHRU*/
2600 case S_IFIFO:
2601 case S_IFSOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002603 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08002604 }
Dave Hansenb41572e2007-10-16 23:31:14 -07002605
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002606 error = inode_permission(inode, acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07002607 if (error)
2608 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05002609
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 /*
2611 * An append-only file must be opened in append mode for writing.
2612 */
2613 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05002614 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05002615 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05002617 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 }
2619
2620 /* O_NOATIME can only be set by the owner or superuser */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07002621 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
Al Viro7715b522009-12-16 03:54:00 -05002622 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
J. Bruce Fieldsf3c7691e2011-09-21 10:58:13 -04002624 return 0;
Al Viro7715b522009-12-16 03:54:00 -05002625}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626
Jeff Laytone1181ee2010-12-07 16:19:50 -05002627static int handle_truncate(struct file *filp)
Al Viro7715b522009-12-16 03:54:00 -05002628{
Jeff Laytone1181ee2010-12-07 16:19:50 -05002629 struct path *path = &filp->f_path;
Al Viro7715b522009-12-16 03:54:00 -05002630 struct inode *inode = path->dentry->d_inode;
2631 int error = get_write_access(inode);
2632 if (error)
2633 return error;
2634 /*
2635 * Refuse to truncate files with mandatory locks held on them.
2636 */
Jeff Laytond7a06982014-03-10 09:54:15 -04002637 error = locks_verify_locked(filp);
Al Viro7715b522009-12-16 03:54:00 -05002638 if (!error)
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +09002639 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05002640 if (!error) {
2641 error = do_truncate(path->dentry, 0,
2642 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
Jeff Laytone1181ee2010-12-07 16:19:50 -05002643 filp);
Al Viro7715b522009-12-16 03:54:00 -05002644 }
2645 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04002646 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647}
2648
Dave Hansend57999e2008-02-15 14:37:27 -08002649static inline int open_to_namei_flags(int flag)
2650{
Al Viro8a5e9292011-06-25 19:15:54 -04002651 if ((flag & O_ACCMODE) == 3)
2652 flag--;
Dave Hansend57999e2008-02-15 14:37:27 -08002653 return flag;
2654}
2655
Miklos Szeredid18e9002012-06-05 15:10:17 +02002656static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode)
2657{
2658 int error = security_path_mknod(dir, dentry, mode, 0);
2659 if (error)
2660 return error;
2661
2662 error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
2663 if (error)
2664 return error;
2665
2666 return security_inode_create(dir->dentry->d_inode, dentry, mode);
2667}
2668
David Howells1acf0af2012-06-14 16:13:46 +01002669/*
2670 * Attempt to atomically look up, create and open a file from a negative
2671 * dentry.
2672 *
2673 * Returns 0 if successful. The file will have been created and attached to
2674 * @file by the filesystem calling finish_open().
2675 *
2676 * Returns 1 if the file was looked up only or didn't need creating. The
2677 * caller will need to perform the open themselves. @path will have been
2678 * updated to point to the new dentry. This may be negative.
2679 *
2680 * Returns an error code otherwise.
2681 */
Al Viro2675a4e2012-06-22 12:41:10 +04002682static int atomic_open(struct nameidata *nd, struct dentry *dentry,
2683 struct path *path, struct file *file,
2684 const struct open_flags *op,
Al Viro64894cf2012-07-31 00:53:35 +04002685 bool got_write, bool need_lookup,
Al Viro2675a4e2012-06-22 12:41:10 +04002686 int *opened)
Miklos Szeredid18e9002012-06-05 15:10:17 +02002687{
2688 struct inode *dir = nd->path.dentry->d_inode;
2689 unsigned open_flag = open_to_namei_flags(op->open_flag);
2690 umode_t mode;
2691 int error;
2692 int acc_mode;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002693 int create_error = 0;
2694 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
Miklos Szeredi116cc022013-09-16 14:52:05 +02002695 bool excl;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002696
2697 BUG_ON(dentry->d_inode);
2698
2699 /* Don't create child dentry for a dead directory. */
2700 if (unlikely(IS_DEADDIR(dir))) {
Al Viro2675a4e2012-06-22 12:41:10 +04002701 error = -ENOENT;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002702 goto out;
2703 }
2704
Miklos Szeredi62b259d2012-08-15 13:01:24 +02002705 mode = op->mode;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002706 if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
2707 mode &= ~current_umask();
2708
Miklos Szeredi116cc022013-09-16 14:52:05 +02002709 excl = (open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT);
2710 if (excl)
Miklos Szeredid18e9002012-06-05 15:10:17 +02002711 open_flag &= ~O_TRUNC;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002712
2713 /*
2714 * Checking write permission is tricky, bacuse we don't know if we are
2715 * going to actually need it: O_CREAT opens should work as long as the
2716 * file exists. But checking existence breaks atomicity. The trick is
2717 * to check access and if not granted clear O_CREAT from the flags.
2718 *
2719 * Another problem is returing the "right" error value (e.g. for an
2720 * O_EXCL open we want to return EEXIST not EROFS).
2721 */
Al Viro64894cf2012-07-31 00:53:35 +04002722 if (((open_flag & (O_CREAT | O_TRUNC)) ||
2723 (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) {
2724 if (!(open_flag & O_CREAT)) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002725 /*
2726 * No O_CREATE -> atomicity not a requirement -> fall
2727 * back to lookup + open
2728 */
2729 goto no_open;
2730 } else if (open_flag & (O_EXCL | O_TRUNC)) {
2731 /* Fall back and fail with the right error */
Al Viro64894cf2012-07-31 00:53:35 +04002732 create_error = -EROFS;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002733 goto no_open;
2734 } else {
2735 /* No side effects, safe to clear O_CREAT */
Al Viro64894cf2012-07-31 00:53:35 +04002736 create_error = -EROFS;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002737 open_flag &= ~O_CREAT;
2738 }
2739 }
2740
2741 if (open_flag & O_CREAT) {
Miklos Szeredi38227f72012-08-15 13:01:24 +02002742 error = may_o_create(&nd->path, dentry, mode);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002743 if (error) {
2744 create_error = error;
2745 if (open_flag & O_EXCL)
2746 goto no_open;
2747 open_flag &= ~O_CREAT;
2748 }
2749 }
2750
2751 if (nd->flags & LOOKUP_DIRECTORY)
2752 open_flag |= O_DIRECTORY;
2753
Al Viro30d90492012-06-22 12:40:19 +04002754 file->f_path.dentry = DENTRY_NOT_SET;
2755 file->f_path.mnt = nd->path.mnt;
2756 error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode,
Al Viro47237682012-06-10 05:01:45 -04002757 opened);
Al Virod9585272012-06-22 12:39:14 +04002758 if (error < 0) {
Al Virod9585272012-06-22 12:39:14 +04002759 if (create_error && error == -ENOENT)
2760 error = create_error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002761 goto out;
2762 }
2763
Al Virod9585272012-06-22 12:39:14 +04002764 if (error) { /* returned 1, that is */
Al Viro30d90492012-06-22 12:40:19 +04002765 if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
Al Viro2675a4e2012-06-22 12:41:10 +04002766 error = -EIO;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002767 goto out;
2768 }
Al Viro30d90492012-06-22 12:40:19 +04002769 if (file->f_path.dentry) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002770 dput(dentry);
Al Viro30d90492012-06-22 12:40:19 +04002771 dentry = file->f_path.dentry;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002772 }
Al Viro03da6332013-09-16 19:22:33 -04002773 if (*opened & FILE_CREATED)
2774 fsnotify_create(dir, dentry);
2775 if (!dentry->d_inode) {
2776 WARN_ON(*opened & FILE_CREATED);
2777 if (create_error) {
2778 error = create_error;
2779 goto out;
2780 }
2781 } else {
2782 if (excl && !(*opened & FILE_CREATED)) {
2783 error = -EEXIST;
2784 goto out;
2785 }
Sage Weil62b2ce92012-08-15 13:30:12 -07002786 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02002787 goto looked_up;
2788 }
2789
2790 /*
2791 * We didn't have the inode before the open, so check open permission
2792 * here.
2793 */
Al Viro03da6332013-09-16 19:22:33 -04002794 acc_mode = op->acc_mode;
2795 if (*opened & FILE_CREATED) {
2796 WARN_ON(!(open_flag & O_CREAT));
2797 fsnotify_create(dir, dentry);
2798 acc_mode = MAY_OPEN;
2799 }
Al Viro2675a4e2012-06-22 12:41:10 +04002800 error = may_open(&file->f_path, acc_mode, open_flag);
2801 if (error)
2802 fput(file);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002803
2804out:
2805 dput(dentry);
Al Viro2675a4e2012-06-22 12:41:10 +04002806 return error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002807
Miklos Szeredid18e9002012-06-05 15:10:17 +02002808no_open:
2809 if (need_lookup) {
Al Viro72bd8662012-06-10 17:17:17 -04002810 dentry = lookup_real(dir, dentry, nd->flags);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002811 if (IS_ERR(dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04002812 return PTR_ERR(dentry);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002813
2814 if (create_error) {
2815 int open_flag = op->open_flag;
2816
Al Viro2675a4e2012-06-22 12:41:10 +04002817 error = create_error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002818 if ((open_flag & O_EXCL)) {
2819 if (!dentry->d_inode)
2820 goto out;
2821 } else if (!dentry->d_inode) {
2822 goto out;
2823 } else if ((open_flag & O_TRUNC) &&
David Howellse36cb0b2015-01-29 12:02:35 +00002824 d_is_reg(dentry)) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002825 goto out;
2826 }
2827 /* will fail later, go on to get the right error */
2828 }
2829 }
2830looked_up:
2831 path->dentry = dentry;
2832 path->mnt = nd->path.mnt;
Al Viro2675a4e2012-06-22 12:41:10 +04002833 return 1;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002834}
2835
Nick Piggin31e6b012011-01-07 17:49:52 +11002836/*
David Howells1acf0af2012-06-14 16:13:46 +01002837 * Look up and maybe create and open the last component.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002838 *
2839 * Must be called with i_mutex held on parent.
2840 *
David Howells1acf0af2012-06-14 16:13:46 +01002841 * Returns 0 if the file was successfully atomically created (if necessary) and
2842 * opened. In this case the file will be returned attached to @file.
2843 *
2844 * Returns 1 if the file was not completely opened at this time, though lookups
2845 * and creations will have been performed and the dentry returned in @path will
2846 * be positive upon return if O_CREAT was specified. If O_CREAT wasn't
2847 * specified then a negative dentry may be returned.
2848 *
2849 * An error code is returned otherwise.
2850 *
2851 * FILE_CREATE will be set in @*opened if the dentry was created and will be
2852 * cleared otherwise prior to returning.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002853 */
Al Viro2675a4e2012-06-22 12:41:10 +04002854static int lookup_open(struct nameidata *nd, struct path *path,
2855 struct file *file,
2856 const struct open_flags *op,
Al Viro64894cf2012-07-31 00:53:35 +04002857 bool got_write, int *opened)
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002858{
2859 struct dentry *dir = nd->path.dentry;
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002860 struct inode *dir_inode = dir->d_inode;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002861 struct dentry *dentry;
2862 int error;
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002863 bool need_lookup;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002864
Al Viro47237682012-06-10 05:01:45 -04002865 *opened &= ~FILE_CREATED;
Al Viro201f9562012-06-22 12:42:10 +04002866 dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002867 if (IS_ERR(dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04002868 return PTR_ERR(dentry);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002869
Miklos Szeredid18e9002012-06-05 15:10:17 +02002870 /* Cached positive dentry: will open in f_op->open */
2871 if (!need_lookup && dentry->d_inode)
2872 goto out_no_open;
2873
2874 if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) {
Al Viro64894cf2012-07-31 00:53:35 +04002875 return atomic_open(nd, dentry, path, file, op, got_write,
Al Viro47237682012-06-10 05:01:45 -04002876 need_lookup, opened);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002877 }
2878
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002879 if (need_lookup) {
2880 BUG_ON(dentry->d_inode);
2881
Al Viro72bd8662012-06-10 17:17:17 -04002882 dentry = lookup_real(dir_inode, dentry, nd->flags);
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002883 if (IS_ERR(dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04002884 return PTR_ERR(dentry);
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002885 }
2886
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002887 /* Negative dentry, just create the file */
2888 if (!dentry->d_inode && (op->open_flag & O_CREAT)) {
2889 umode_t mode = op->mode;
2890 if (!IS_POSIXACL(dir->d_inode))
2891 mode &= ~current_umask();
2892 /*
2893 * This write is needed to ensure that a
2894 * rw->ro transition does not occur between
2895 * the time when the file is created and when
2896 * a permanent write count is taken through
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02002897 * the 'struct file' in finish_open().
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002898 */
Al Viro64894cf2012-07-31 00:53:35 +04002899 if (!got_write) {
2900 error = -EROFS;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002901 goto out_dput;
Al Viro64894cf2012-07-31 00:53:35 +04002902 }
Al Viro47237682012-06-10 05:01:45 -04002903 *opened |= FILE_CREATED;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002904 error = security_path_mknod(&nd->path, dentry, mode, 0);
2905 if (error)
2906 goto out_dput;
Al Viro312b63f2012-06-10 18:09:36 -04002907 error = vfs_create(dir->d_inode, dentry, mode,
2908 nd->flags & LOOKUP_EXCL);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002909 if (error)
2910 goto out_dput;
2911 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02002912out_no_open:
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002913 path->dentry = dentry;
2914 path->mnt = nd->path.mnt;
Al Viro2675a4e2012-06-22 12:41:10 +04002915 return 1;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002916
2917out_dput:
2918 dput(dentry);
Al Viro2675a4e2012-06-22 12:41:10 +04002919 return error;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002920}
2921
2922/*
Al Virofe2d35f2011-03-05 22:58:25 -05002923 * Handle the last step of open()
Nick Piggin31e6b012011-01-07 17:49:52 +11002924 */
Al Viro2675a4e2012-06-22 12:41:10 +04002925static int do_last(struct nameidata *nd, struct path *path,
2926 struct file *file, const struct open_flags *op,
Jeff Layton669abf42012-10-10 16:43:10 -04002927 int *opened, struct filename *name)
Al Virofb1cc552009-12-24 01:58:28 -05002928{
Al Viroa1e28032009-12-24 02:12:06 -05002929 struct dentry *dir = nd->path.dentry;
Al Viroca344a892011-03-09 00:36:45 -05002930 int open_flag = op->open_flag;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002931 bool will_truncate = (open_flag & O_TRUNC) != 0;
Al Viro64894cf2012-07-31 00:53:35 +04002932 bool got_write = false;
Al Virobcda7652011-03-13 16:42:14 -04002933 int acc_mode = op->acc_mode;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02002934 struct inode *inode;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002935 bool symlink_ok = false;
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02002936 struct path save_parent = { .dentry = NULL, .mnt = NULL };
2937 bool retried = false;
Al Viro16c2cd72011-02-22 15:50:10 -05002938 int error;
Al Virofb1cc552009-12-24 01:58:28 -05002939
Al Viroc3e380b2011-02-23 13:39:45 -05002940 nd->flags &= ~LOOKUP_PARENT;
2941 nd->flags |= op->intent;
2942
Al Virobc77daa2013-06-06 09:12:33 -04002943 if (nd->last_type != LAST_NORM) {
Al Virofe2d35f2011-03-05 22:58:25 -05002944 error = handle_dots(nd, nd->last_type);
2945 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04002946 return error;
Miklos Szeredie83db162012-06-05 15:10:29 +02002947 goto finish_open;
Al Viro1f36f772009-12-26 10:56:19 -05002948 }
Al Viro67ee3ad2009-12-26 07:01:01 -05002949
Al Viroca344a892011-03-09 00:36:45 -05002950 if (!(open_flag & O_CREAT)) {
Al Virofe2d35f2011-03-05 22:58:25 -05002951 if (nd->last.name[nd->last.len])
2952 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
Al Virobcda7652011-03-13 16:42:14 -04002953 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002954 symlink_ok = true;
Al Virofe2d35f2011-03-05 22:58:25 -05002955 /* we _can_ be in RCU mode here */
Al Viroe97cdc82013-01-24 18:16:00 -05002956 error = lookup_fast(nd, path, &inode);
Miklos Szeredi71574862012-06-05 15:10:14 +02002957 if (likely(!error))
2958 goto finish_lookup;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02002959
Miklos Szeredi71574862012-06-05 15:10:14 +02002960 if (error < 0)
Al Viro2675a4e2012-06-22 12:41:10 +04002961 goto out;
Miklos Szeredi37d7fff2012-06-05 15:10:12 +02002962
Miklos Szeredi71574862012-06-05 15:10:14 +02002963 BUG_ON(nd->inode != dir->d_inode);
Miklos Szeredib6183df2012-06-05 15:10:13 +02002964 } else {
2965 /* create side of things */
2966 /*
2967 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
2968 * has been cleared when we got to the last component we are
2969 * about to look up
2970 */
2971 error = complete_walk(nd);
2972 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04002973 return error;
Miklos Szeredib6183df2012-06-05 15:10:13 +02002974
Jeff Layton33e22082013-04-12 15:16:32 -04002975 audit_inode(name, dir, LOOKUP_PARENT);
Miklos Szeredib6183df2012-06-05 15:10:13 +02002976 error = -EISDIR;
2977 /* trailing slashes? */
2978 if (nd->last.name[nd->last.len])
Al Viro2675a4e2012-06-22 12:41:10 +04002979 goto out;
Al Virofe2d35f2011-03-05 22:58:25 -05002980 }
2981
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02002982retry_lookup:
Al Viro64894cf2012-07-31 00:53:35 +04002983 if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
2984 error = mnt_want_write(nd->path.mnt);
2985 if (!error)
2986 got_write = true;
2987 /*
2988 * do _not_ fail yet - we might not need that or fail with
2989 * a different error; let lookup_open() decide; we'll be
2990 * dropping this one anyway.
2991 */
2992 }
Al Viroa1e28032009-12-24 02:12:06 -05002993 mutex_lock(&dir->d_inode->i_mutex);
Al Viro64894cf2012-07-31 00:53:35 +04002994 error = lookup_open(nd, path, file, op, got_write, opened);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002995 mutex_unlock(&dir->d_inode->i_mutex);
Al Viroa1e28032009-12-24 02:12:06 -05002996
Al Viro2675a4e2012-06-22 12:41:10 +04002997 if (error <= 0) {
2998 if (error)
Miklos Szeredid18e9002012-06-05 15:10:17 +02002999 goto out;
3000
Al Viro47237682012-06-10 05:01:45 -04003001 if ((*opened & FILE_CREATED) ||
Al Viro496ad9a2013-01-23 17:07:38 -05003002 !S_ISREG(file_inode(file)->i_mode))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003003 will_truncate = false;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003004
Jeff Laytonadb5c242012-10-10 16:43:13 -04003005 audit_inode(name, file->f_path.dentry, 0);
Miklos Szeredid18e9002012-06-05 15:10:17 +02003006 goto opened;
3007 }
Al Virofb1cc552009-12-24 01:58:28 -05003008
Al Viro47237682012-06-10 05:01:45 -04003009 if (*opened & FILE_CREATED) {
Al Viro9b44f1b2011-03-09 00:17:27 -05003010 /* Don't check for write permission, don't truncate */
Al Viroca344a892011-03-09 00:36:45 -05003011 open_flag &= ~O_TRUNC;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003012 will_truncate = false;
Al Virobcda7652011-03-13 16:42:14 -04003013 acc_mode = MAY_OPEN;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003014 path_to_nameidata(path, nd);
Miklos Szeredie83db162012-06-05 15:10:29 +02003015 goto finish_open_created;
Al Virofb1cc552009-12-24 01:58:28 -05003016 }
3017
3018 /*
Jeff Layton3134f372012-07-25 10:19:47 -04003019 * create/update audit record if it already exists.
Al Virofb1cc552009-12-24 01:58:28 -05003020 */
David Howellsb18825a2013-09-12 19:22:53 +01003021 if (d_is_positive(path->dentry))
Jeff Laytonadb5c242012-10-10 16:43:13 -04003022 audit_inode(name, path->dentry, 0);
Al Virofb1cc552009-12-24 01:58:28 -05003023
Miklos Szeredid18e9002012-06-05 15:10:17 +02003024 /*
3025 * If atomic_open() acquired write access it is dropped now due to
3026 * possible mount and symlink following (this might be optimized away if
3027 * necessary...)
3028 */
Al Viro64894cf2012-07-31 00:53:35 +04003029 if (got_write) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02003030 mnt_drop_write(nd->path.mnt);
Al Viro64894cf2012-07-31 00:53:35 +04003031 got_write = false;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003032 }
3033
Al Virofb1cc552009-12-24 01:58:28 -05003034 error = -EEXIST;
Al Virof8310c52012-07-30 11:50:30 +04003035 if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))
Al Virofb1cc552009-12-24 01:58:28 -05003036 goto exit_dput;
3037
David Howells9875cf82011-01-14 18:45:21 +00003038 error = follow_managed(path, nd->flags);
3039 if (error < 0)
3040 goto exit_dput;
Al Virofb1cc552009-12-24 01:58:28 -05003041
Al Viroa3fbbde2011-11-07 21:21:26 +00003042 if (error)
3043 nd->flags |= LOOKUP_JUMPED;
3044
Miklos Szeredidecf3402012-05-21 17:30:08 +02003045 BUG_ON(nd->flags & LOOKUP_RCU);
3046 inode = path->dentry->d_inode;
Al Virofb1cc552009-12-24 01:58:28 -05003047 error = -ENOENT;
David Howells698934d2015-03-17 17:33:52 +00003048 if (d_is_negative(path->dentry)) {
Miklos Szeredi54c33e72012-05-21 17:30:10 +02003049 path_to_nameidata(path, nd);
Al Viro2675a4e2012-06-22 12:41:10 +04003050 goto out;
Miklos Szeredi54c33e72012-05-21 17:30:10 +02003051 }
Al Viro766c4cb2015-05-07 19:24:57 -04003052finish_lookup:
3053 /* we _can_ be in RCU mode here */
David Howellsb18825a2013-09-12 19:22:53 +01003054 if (should_follow_link(path->dentry, !symlink_ok)) {
Miklos Szeredid45ea862012-05-21 17:30:09 +02003055 if (nd->flags & LOOKUP_RCU) {
Al Viro3cab9892015-04-24 15:47:07 -04003056 if (unlikely(nd->path.mnt != path->mnt ||
3057 unlazy_walk(nd, path->dentry))) {
Miklos Szeredid45ea862012-05-21 17:30:09 +02003058 error = -ECHILD;
Al Viro2675a4e2012-06-22 12:41:10 +04003059 goto out;
Miklos Szeredid45ea862012-05-21 17:30:09 +02003060 }
3061 }
3062 BUG_ON(inode != path->dentry->d_inode);
Al Viro2675a4e2012-06-22 12:41:10 +04003063 return 1;
Miklos Szeredid45ea862012-05-21 17:30:09 +02003064 }
Al Virofb1cc552009-12-24 01:58:28 -05003065
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003066 if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) {
3067 path_to_nameidata(path, nd);
3068 } else {
3069 save_parent.dentry = nd->path.dentry;
3070 save_parent.mnt = mntget(path->mnt);
3071 nd->path.dentry = path->dentry;
3072
3073 }
Miklos Szeredidecf3402012-05-21 17:30:08 +02003074 nd->inode = inode;
Al Viroa3fbbde2011-11-07 21:21:26 +00003075 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
Al Virobc77daa2013-06-06 09:12:33 -04003076finish_open:
Al Viroa3fbbde2011-11-07 21:21:26 +00003077 error = complete_walk(nd);
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003078 if (error) {
3079 path_put(&save_parent);
Al Viro2675a4e2012-06-22 12:41:10 +04003080 return error;
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003081 }
Al Virobc77daa2013-06-06 09:12:33 -04003082 audit_inode(name, nd->path.dentry, 0);
Al Virofb1cc552009-12-24 01:58:28 -05003083 error = -EISDIR;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02003084 if ((open_flag & O_CREAT) && d_is_dir(nd->path.dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04003085 goto out;
Miklos Szerediaf2f5542012-05-21 17:30:11 +02003086 error = -ENOTDIR;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02003087 if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04003088 goto out;
David Howells4bbcbd3b2015-03-17 22:16:40 +00003089 if (!d_is_reg(nd->path.dentry))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003090 will_truncate = false;
Al Viro6c0d46c2011-03-09 00:59:59 -05003091
Al Viro0f9d1a12011-03-09 00:13:14 -05003092 if (will_truncate) {
3093 error = mnt_want_write(nd->path.mnt);
3094 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003095 goto out;
Al Viro64894cf2012-07-31 00:53:35 +04003096 got_write = true;
Al Viro0f9d1a12011-03-09 00:13:14 -05003097 }
Miklos Szeredie83db162012-06-05 15:10:29 +02003098finish_open_created:
Al Virobcda7652011-03-13 16:42:14 -04003099 error = may_open(&nd->path, acc_mode, open_flag);
Al Viroca344a892011-03-09 00:36:45 -05003100 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003101 goto out;
Miklos Szeredi4aa7c632014-10-24 00:14:35 +02003102
3103 BUG_ON(*opened & FILE_OPENED); /* once it's opened, it's opened */
3104 error = vfs_open(&nd->path, file, current_cred());
3105 if (!error) {
3106 *opened |= FILE_OPENED;
3107 } else {
Al Viro30d90492012-06-22 12:40:19 +04003108 if (error == -EOPENSTALE)
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003109 goto stale_open;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003110 goto out;
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003111 }
Miklos Szeredia8277b92012-06-05 15:10:32 +02003112opened:
Al Viro2675a4e2012-06-22 12:41:10 +04003113 error = open_check_o_direct(file);
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003114 if (error)
3115 goto exit_fput;
Dmitry Kasatkin3034a142014-06-27 18:15:44 +03003116 error = ima_file_check(file, op->acc_mode, *opened);
Miklos Szerediaa4caad2012-06-05 15:10:28 +02003117 if (error)
3118 goto exit_fput;
3119
3120 if (will_truncate) {
Al Viro2675a4e2012-06-22 12:41:10 +04003121 error = handle_truncate(file);
Miklos Szerediaa4caad2012-06-05 15:10:28 +02003122 if (error)
3123 goto exit_fput;
Al Viro0f9d1a12011-03-09 00:13:14 -05003124 }
Al Viroca344a892011-03-09 00:36:45 -05003125out:
Al Viro64894cf2012-07-31 00:53:35 +04003126 if (got_write)
Al Viro0f9d1a12011-03-09 00:13:14 -05003127 mnt_drop_write(nd->path.mnt);
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003128 path_put(&save_parent);
Miklos Szeredie276ae62012-05-21 17:30:06 +02003129 terminate_walk(nd);
Al Viro2675a4e2012-06-22 12:41:10 +04003130 return error;
Al Virofb1cc552009-12-24 01:58:28 -05003131
Al Virofb1cc552009-12-24 01:58:28 -05003132exit_dput:
3133 path_put_conditional(path, nd);
Al Viroca344a892011-03-09 00:36:45 -05003134 goto out;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003135exit_fput:
Al Viro2675a4e2012-06-22 12:41:10 +04003136 fput(file);
3137 goto out;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003138
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003139stale_open:
3140 /* If no saved parent or already retried then can't retry */
3141 if (!save_parent.dentry || retried)
3142 goto out;
3143
3144 BUG_ON(save_parent.dentry != dir);
3145 path_put(&nd->path);
3146 nd->path = save_parent;
3147 nd->inode = dir->d_inode;
3148 save_parent.mnt = NULL;
3149 save_parent.dentry = NULL;
Al Viro64894cf2012-07-31 00:53:35 +04003150 if (got_write) {
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003151 mnt_drop_write(nd->path.mnt);
Al Viro64894cf2012-07-31 00:53:35 +04003152 got_write = false;
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003153 }
3154 retried = true;
3155 goto retry_lookup;
Al Virofb1cc552009-12-24 01:58:28 -05003156}
3157
Al Viro60545d02013-06-07 01:20:27 -04003158static int do_tmpfile(int dfd, struct filename *pathname,
3159 struct nameidata *nd, int flags,
3160 const struct open_flags *op,
3161 struct file *file, int *opened)
3162{
3163 static const struct qstr name = QSTR_INIT("/", 1);
3164 struct dentry *dentry, *child;
3165 struct inode *dir;
Al Viro5eb6b492015-02-22 19:40:53 -05003166 int error = path_lookupat(dfd, pathname,
Al Viro60545d02013-06-07 01:20:27 -04003167 flags | LOOKUP_DIRECTORY, nd);
3168 if (unlikely(error))
3169 return error;
3170 error = mnt_want_write(nd->path.mnt);
3171 if (unlikely(error))
3172 goto out;
3173 /* we want directory to be writable */
3174 error = inode_permission(nd->inode, MAY_WRITE | MAY_EXEC);
3175 if (error)
3176 goto out2;
3177 dentry = nd->path.dentry;
3178 dir = dentry->d_inode;
3179 if (!dir->i_op->tmpfile) {
3180 error = -EOPNOTSUPP;
3181 goto out2;
3182 }
3183 child = d_alloc(dentry, &name);
3184 if (unlikely(!child)) {
3185 error = -ENOMEM;
3186 goto out2;
3187 }
3188 nd->flags &= ~LOOKUP_DIRECTORY;
3189 nd->flags |= op->intent;
3190 dput(nd->path.dentry);
3191 nd->path.dentry = child;
3192 error = dir->i_op->tmpfile(dir, nd->path.dentry, op->mode);
3193 if (error)
3194 goto out2;
3195 audit_inode(pathname, nd->path.dentry, 0);
Eric Rannaud69a91c22014-10-30 01:51:01 -07003196 /* Don't check for other permissions, the inode was just created */
3197 error = may_open(&nd->path, MAY_OPEN, op->open_flag);
Al Viro60545d02013-06-07 01:20:27 -04003198 if (error)
3199 goto out2;
3200 file->f_path.mnt = nd->path.mnt;
3201 error = finish_open(file, nd->path.dentry, NULL, opened);
3202 if (error)
3203 goto out2;
3204 error = open_check_o_direct(file);
Al Virof4e0c302013-06-11 08:34:36 +04003205 if (error) {
Al Viro60545d02013-06-07 01:20:27 -04003206 fput(file);
Al Virof4e0c302013-06-11 08:34:36 +04003207 } else if (!(op->open_flag & O_EXCL)) {
3208 struct inode *inode = file_inode(file);
3209 spin_lock(&inode->i_lock);
3210 inode->i_state |= I_LINKABLE;
3211 spin_unlock(&inode->i_lock);
3212 }
Al Viro60545d02013-06-07 01:20:27 -04003213out2:
3214 mnt_drop_write(nd->path.mnt);
3215out:
3216 path_put(&nd->path);
3217 return error;
3218}
3219
Jeff Layton669abf42012-10-10 16:43:10 -04003220static struct file *path_openat(int dfd, struct filename *pathname,
Al Viro73d049a2011-03-11 12:08:24 -05003221 struct nameidata *nd, const struct open_flags *op, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222{
Al Viro30d90492012-06-22 12:40:19 +04003223 struct file *file;
Al Viro9850c052010-01-13 15:01:15 -05003224 struct path path;
Al Viro47237682012-06-10 05:01:45 -04003225 int opened = 0;
Al Viro13aab422011-02-23 17:54:08 -05003226 int error;
Nick Piggin31e6b012011-01-07 17:49:52 +11003227
Al Viro30d90492012-06-22 12:40:19 +04003228 file = get_empty_filp();
Al Viro1afc99b2013-02-14 20:41:04 -05003229 if (IS_ERR(file))
3230 return file;
Nick Piggin31e6b012011-01-07 17:49:52 +11003231
Al Viro30d90492012-06-22 12:40:19 +04003232 file->f_flags = op->open_flag;
Nick Piggin31e6b012011-01-07 17:49:52 +11003233
Al Virobb458c62013-07-13 13:26:37 +04003234 if (unlikely(file->f_flags & __O_TMPFILE)) {
Al Viro60545d02013-06-07 01:20:27 -04003235 error = do_tmpfile(dfd, pathname, nd, flags, op, file, &opened);
Al Virof15133d2015-05-08 22:53:15 -04003236 goto out2;
Al Viro60545d02013-06-07 01:20:27 -04003237 }
3238
Al Viro6e8a1f82015-02-22 19:46:04 -05003239 error = path_init(dfd, pathname, flags, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11003240 if (unlikely(error))
Al Viro2675a4e2012-06-22 12:41:10 +04003241 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242
Al Viro2675a4e2012-06-22 12:41:10 +04003243 error = do_last(nd, &path, file, op, &opened, pathname);
3244 while (unlikely(error > 0)) { /* trailing symlink */
Nick Piggin7b9337a2011-01-14 08:42:43 +00003245 struct path link = path;
Al Virodef4af32009-12-26 08:37:05 -05003246 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04003247 if (!(nd->flags & LOOKUP_FOLLOW)) {
Al Viro73d049a2011-03-11 12:08:24 -05003248 path_put_conditional(&path, nd);
3249 path_put(&nd->path);
Al Viro2675a4e2012-06-22 12:41:10 +04003250 error = -ELOOP;
Al Viro40b39132011-03-09 16:22:18 -05003251 break;
3252 }
Kees Cook800179c2012-07-25 17:29:07 -07003253 error = may_follow_link(&link, nd);
3254 if (unlikely(error))
3255 break;
Al Viro73d049a2011-03-11 12:08:24 -05003256 nd->flags |= LOOKUP_PARENT;
3257 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
Al Viro574197e2011-03-14 22:20:34 -04003258 error = follow_link(&link, nd, &cookie);
Al Viroc3e380b2011-02-23 13:39:45 -05003259 if (unlikely(error))
Al Viro2675a4e2012-06-22 12:41:10 +04003260 break;
3261 error = do_last(nd, &path, file, op, &opened, pathname);
Al Viro574197e2011-03-14 22:20:34 -04003262 put_link(nd, &link, cookie);
Al Viro806b6812009-12-26 07:16:40 -05003263 }
Al Viro10fa8e62009-12-26 07:09:49 -05003264out:
Al Viro893b7772014-11-20 14:18:09 -05003265 path_cleanup(nd);
Al Virof15133d2015-05-08 22:53:15 -04003266out2:
Al Viro2675a4e2012-06-22 12:41:10 +04003267 if (!(opened & FILE_OPENED)) {
3268 BUG_ON(!error);
Al Viro30d90492012-06-22 12:40:19 +04003269 put_filp(file);
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003270 }
Al Viro2675a4e2012-06-22 12:41:10 +04003271 if (unlikely(error)) {
3272 if (error == -EOPENSTALE) {
3273 if (flags & LOOKUP_RCU)
3274 error = -ECHILD;
3275 else
3276 error = -ESTALE;
3277 }
3278 file = ERR_PTR(error);
3279 }
3280 return file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281}
3282
Jeff Layton669abf42012-10-10 16:43:10 -04003283struct file *do_filp_open(int dfd, struct filename *pathname,
Al Virof9652e12013-06-11 08:23:01 +04003284 const struct open_flags *op)
Al Viro13aab422011-02-23 17:54:08 -05003285{
Al Viro73d049a2011-03-11 12:08:24 -05003286 struct nameidata nd;
Al Virof9652e12013-06-11 08:23:01 +04003287 int flags = op->lookup_flags;
Al Viro13aab422011-02-23 17:54:08 -05003288 struct file *filp;
3289
Al Viro73d049a2011-03-11 12:08:24 -05003290 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
Al Viro13aab422011-02-23 17:54:08 -05003291 if (unlikely(filp == ERR_PTR(-ECHILD)))
Al Viro73d049a2011-03-11 12:08:24 -05003292 filp = path_openat(dfd, pathname, &nd, op, flags);
Al Viro13aab422011-02-23 17:54:08 -05003293 if (unlikely(filp == ERR_PTR(-ESTALE)))
Al Viro73d049a2011-03-11 12:08:24 -05003294 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
Al Viro13aab422011-02-23 17:54:08 -05003295 return filp;
3296}
3297
Al Viro73d049a2011-03-11 12:08:24 -05003298struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
Al Virof9652e12013-06-11 08:23:01 +04003299 const char *name, const struct open_flags *op)
Al Viro73d049a2011-03-11 12:08:24 -05003300{
3301 struct nameidata nd;
3302 struct file *file;
Paul Moore51689102015-01-22 00:00:03 -05003303 struct filename *filename;
Al Virof9652e12013-06-11 08:23:01 +04003304 int flags = op->lookup_flags | LOOKUP_ROOT;
Al Viro73d049a2011-03-11 12:08:24 -05003305
3306 nd.root.mnt = mnt;
3307 nd.root.dentry = dentry;
3308
David Howellsb18825a2013-09-12 19:22:53 +01003309 if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN)
Al Viro73d049a2011-03-11 12:08:24 -05003310 return ERR_PTR(-ELOOP);
3311
Paul Moore51689102015-01-22 00:00:03 -05003312 filename = getname_kernel(name);
3313 if (unlikely(IS_ERR(filename)))
3314 return ERR_CAST(filename);
3315
3316 file = path_openat(-1, filename, &nd, op, flags | LOOKUP_RCU);
Al Viro73d049a2011-03-11 12:08:24 -05003317 if (unlikely(file == ERR_PTR(-ECHILD)))
Paul Moore51689102015-01-22 00:00:03 -05003318 file = path_openat(-1, filename, &nd, op, flags);
Al Viro73d049a2011-03-11 12:08:24 -05003319 if (unlikely(file == ERR_PTR(-ESTALE)))
Paul Moore51689102015-01-22 00:00:03 -05003320 file = path_openat(-1, filename, &nd, op, flags | LOOKUP_REVAL);
3321 putname(filename);
Al Viro73d049a2011-03-11 12:08:24 -05003322 return file;
3323}
3324
Al Virofa14a0b2015-01-22 02:16:49 -05003325static struct dentry *filename_create(int dfd, struct filename *name,
Jeff Layton1ac12b42012-12-11 12:10:06 -05003326 struct path *path, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003328 struct dentry *dentry = ERR_PTR(-EEXIST);
Al Viroed75e952011-06-27 16:53:43 -04003329 struct nameidata nd;
Jan Karac30dabf2012-06-12 16:20:30 +02003330 int err2;
Jeff Layton1ac12b42012-12-11 12:10:06 -05003331 int error;
3332 bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
3333
3334 /*
3335 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
3336 * other flags passed in are ignored!
3337 */
3338 lookup_flags &= LOOKUP_REVAL;
3339
Al Virofa14a0b2015-01-22 02:16:49 -05003340 error = filename_lookup(dfd, name, LOOKUP_PARENT|lookup_flags, &nd);
Al Viroed75e952011-06-27 16:53:43 -04003341 if (error)
3342 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003344 /*
3345 * Yucky last component or no last component at all?
3346 * (foo/., foo/.., /////)
3347 */
Al Viroed75e952011-06-27 16:53:43 -04003348 if (nd.last_type != LAST_NORM)
3349 goto out;
3350 nd.flags &= ~LOOKUP_PARENT;
3351 nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003352
Jan Karac30dabf2012-06-12 16:20:30 +02003353 /* don't fail immediately if it's r/o, at least try to report other errors */
3354 err2 = mnt_want_write(nd.path.mnt);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003355 /*
3356 * Do the final lookup.
3357 */
Al Viroed75e952011-06-27 16:53:43 -04003358 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3359 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 if (IS_ERR(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003361 goto unlock;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003362
Al Viroa8104a92012-07-20 02:25:00 +04003363 error = -EEXIST;
David Howellsb18825a2013-09-12 19:22:53 +01003364 if (d_is_positive(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003365 goto fail;
David Howellsb18825a2013-09-12 19:22:53 +01003366
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003367 /*
3368 * Special case - lookup gave negative, but... we had foo/bar/
3369 * From the vfs_mknod() POV we just have a negative dentry -
3370 * all is fine. Let's be bastards - you had / on the end, you've
3371 * been asking for (non-existent) directory. -ENOENT for you.
3372 */
Al Viroed75e952011-06-27 16:53:43 -04003373 if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
Al Viroa8104a92012-07-20 02:25:00 +04003374 error = -ENOENT;
Al Viroed75e952011-06-27 16:53:43 -04003375 goto fail;
Al Viroe9baf6e2008-05-15 04:49:12 -04003376 }
Jan Karac30dabf2012-06-12 16:20:30 +02003377 if (unlikely(err2)) {
3378 error = err2;
Al Viroa8104a92012-07-20 02:25:00 +04003379 goto fail;
Jan Karac30dabf2012-06-12 16:20:30 +02003380 }
Al Viroed75e952011-06-27 16:53:43 -04003381 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383fail:
Al Viroa8104a92012-07-20 02:25:00 +04003384 dput(dentry);
3385 dentry = ERR_PTR(error);
3386unlock:
Al Viroed75e952011-06-27 16:53:43 -04003387 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Karac30dabf2012-06-12 16:20:30 +02003388 if (!err2)
3389 mnt_drop_write(nd.path.mnt);
Al Viroed75e952011-06-27 16:53:43 -04003390out:
3391 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 return dentry;
3393}
Al Virofa14a0b2015-01-22 02:16:49 -05003394
3395struct dentry *kern_path_create(int dfd, const char *pathname,
3396 struct path *path, unsigned int lookup_flags)
3397{
Paul Moore51689102015-01-22 00:00:03 -05003398 struct filename *filename = getname_kernel(pathname);
3399 struct dentry *res;
3400
3401 if (IS_ERR(filename))
3402 return ERR_CAST(filename);
3403 res = filename_create(dfd, filename, path, lookup_flags);
3404 putname(filename);
3405 return res;
Al Virofa14a0b2015-01-22 02:16:49 -05003406}
Al Virodae6ad82011-06-26 11:50:15 -04003407EXPORT_SYMBOL(kern_path_create);
3408
Al Viro921a1652012-07-20 01:15:31 +04003409void done_path_create(struct path *path, struct dentry *dentry)
3410{
3411 dput(dentry);
3412 mutex_unlock(&path->dentry->d_inode->i_mutex);
Al Viroa8104a92012-07-20 02:25:00 +04003413 mnt_drop_write(path->mnt);
Al Viro921a1652012-07-20 01:15:31 +04003414 path_put(path);
3415}
3416EXPORT_SYMBOL(done_path_create);
3417
Jeff Layton1ac12b42012-12-11 12:10:06 -05003418struct dentry *user_path_create(int dfd, const char __user *pathname,
3419 struct path *path, unsigned int lookup_flags)
Al Virodae6ad82011-06-26 11:50:15 -04003420{
Jeff Layton91a27b22012-10-10 15:25:28 -04003421 struct filename *tmp = getname(pathname);
Al Virodae6ad82011-06-26 11:50:15 -04003422 struct dentry *res;
3423 if (IS_ERR(tmp))
3424 return ERR_CAST(tmp);
Al Virofa14a0b2015-01-22 02:16:49 -05003425 res = filename_create(dfd, tmp, path, lookup_flags);
Al Virodae6ad82011-06-26 11:50:15 -04003426 putname(tmp);
3427 return res;
3428}
3429EXPORT_SYMBOL(user_path_create);
3430
Al Viro1a67aaf2011-07-26 01:52:52 -04003431int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003433 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434
3435 if (error)
3436 return error;
3437
Eric W. Biederman975d6b32011-11-13 12:16:43 -08003438 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 return -EPERM;
3440
Al Viroacfa4382008-12-04 10:06:33 -05003441 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 return -EPERM;
3443
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07003444 error = devcgroup_inode_mknod(mode, dev);
3445 if (error)
3446 return error;
3447
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 error = security_inode_mknod(dir, dentry, mode, dev);
3449 if (error)
3450 return error;
3451
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003453 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003454 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 return error;
3456}
Al Viro4d359502014-03-14 12:20:17 -04003457EXPORT_SYMBOL(vfs_mknod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458
Al Virof69aac02011-07-26 04:31:40 -04003459static int may_mknod(umode_t mode)
Dave Hansen463c3192008-02-15 14:37:57 -08003460{
3461 switch (mode & S_IFMT) {
3462 case S_IFREG:
3463 case S_IFCHR:
3464 case S_IFBLK:
3465 case S_IFIFO:
3466 case S_IFSOCK:
3467 case 0: /* zero mode translates to S_IFREG */
3468 return 0;
3469 case S_IFDIR:
3470 return -EPERM;
3471 default:
3472 return -EINVAL;
3473 }
3474}
3475
Al Viro8208a222011-07-25 17:32:17 -04003476SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003477 unsigned, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478{
Al Viro2ad94ae2008-07-21 09:32:51 -04003479 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003480 struct path path;
3481 int error;
Jeff Layton972567f2012-12-20 16:00:10 -05003482 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483
Al Viro8e4bfca2012-07-20 01:17:26 +04003484 error = may_mknod(mode);
3485 if (error)
3486 return error;
Jeff Layton972567f2012-12-20 16:00:10 -05003487retry:
3488 dentry = user_path_create(dfd, filename, &path, lookup_flags);
Al Virodae6ad82011-06-26 11:50:15 -04003489 if (IS_ERR(dentry))
3490 return PTR_ERR(dentry);
Al Viro2ad94ae2008-07-21 09:32:51 -04003491
Al Virodae6ad82011-06-26 11:50:15 -04003492 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003493 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003494 error = security_path_mknod(&path, dentry, mode, dev);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003495 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04003496 goto out;
Dave Hansen463c3192008-02-15 14:37:57 -08003497 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 case 0: case S_IFREG:
Al Viro312b63f2012-06-10 18:09:36 -04003499 error = vfs_create(path.dentry->d_inode,dentry,mode,true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 break;
3501 case S_IFCHR: case S_IFBLK:
Al Virodae6ad82011-06-26 11:50:15 -04003502 error = vfs_mknod(path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 new_decode_dev(dev));
3504 break;
3505 case S_IFIFO: case S_IFSOCK:
Al Virodae6ad82011-06-26 11:50:15 -04003506 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 }
Al Viroa8104a92012-07-20 02:25:00 +04003509out:
Al Viro921a1652012-07-20 01:15:31 +04003510 done_path_create(&path, dentry);
Jeff Layton972567f2012-12-20 16:00:10 -05003511 if (retry_estale(error, lookup_flags)) {
3512 lookup_flags |= LOOKUP_REVAL;
3513 goto retry;
3514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 return error;
3516}
3517
Al Viro8208a222011-07-25 17:32:17 -04003518SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003519{
3520 return sys_mknodat(AT_FDCWD, filename, mode, dev);
3521}
3522
Al Viro18bb1db2011-07-26 01:41:39 -04003523int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003525 int error = may_create(dir, dentry);
Al Viro8de52772012-02-06 12:45:27 -05003526 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527
3528 if (error)
3529 return error;
3530
Al Viroacfa4382008-12-04 10:06:33 -05003531 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 return -EPERM;
3533
3534 mode &= (S_IRWXUGO|S_ISVTX);
3535 error = security_inode_mkdir(dir, dentry, mode);
3536 if (error)
3537 return error;
3538
Al Viro8de52772012-02-06 12:45:27 -05003539 if (max_links && dir->i_nlink >= max_links)
3540 return -EMLINK;
3541
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003543 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003544 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 return error;
3546}
Al Viro4d359502014-03-14 12:20:17 -04003547EXPORT_SYMBOL(vfs_mkdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548
Al Viroa218d0f2011-11-21 14:59:34 -05003549SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550{
Dave Hansen6902d922006-09-30 23:29:01 -07003551 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003552 struct path path;
3553 int error;
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003554 unsigned int lookup_flags = LOOKUP_DIRECTORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003556retry:
3557 dentry = user_path_create(dfd, pathname, &path, lookup_flags);
Dave Hansen6902d922006-09-30 23:29:01 -07003558 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003559 return PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003560
Al Virodae6ad82011-06-26 11:50:15 -04003561 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003562 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003563 error = security_path_mkdir(&path, dentry, mode);
Al Viroa8104a92012-07-20 02:25:00 +04003564 if (!error)
3565 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
Al Viro921a1652012-07-20 01:15:31 +04003566 done_path_create(&path, dentry);
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003567 if (retry_estale(error, lookup_flags)) {
3568 lookup_flags |= LOOKUP_REVAL;
3569 goto retry;
3570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 return error;
3572}
3573
Al Viroa218d0f2011-11-21 14:59:34 -05003574SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003575{
3576 return sys_mkdirat(AT_FDCWD, pathname, mode);
3577}
3578
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579/*
Sage Weila71905f2011-05-24 13:06:08 -07003580 * The dentry_unhash() helper will try to drop the dentry early: we
J. Bruce Fieldsc0d02592012-02-15 11:48:40 -05003581 * should have a usage count of 1 if we're the only user of this
Sage Weila71905f2011-05-24 13:06:08 -07003582 * dentry, and if that is true (possibly after pruning the dcache),
3583 * then we drop the dentry now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 *
3585 * A low-level filesystem can, if it choses, legally
3586 * do a
3587 *
3588 * if (!d_unhashed(dentry))
3589 * return -EBUSY;
3590 *
3591 * if it cannot handle the case of removing a directory
3592 * that is still in use by something else..
3593 */
3594void dentry_unhash(struct dentry *dentry)
3595{
Vasily Averindc168422006-12-06 20:37:07 -08003596 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 spin_lock(&dentry->d_lock);
Waiman Long98474232013-08-28 18:24:59 -07003598 if (dentry->d_lockref.count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 __d_drop(dentry);
3600 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601}
Al Viro4d359502014-03-14 12:20:17 -04003602EXPORT_SYMBOL(dentry_unhash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603
3604int vfs_rmdir(struct inode *dir, struct dentry *dentry)
3605{
3606 int error = may_delete(dir, dentry, 1);
3607
3608 if (error)
3609 return error;
3610
Al Viroacfa4382008-12-04 10:06:33 -05003611 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 return -EPERM;
3613
Al Viro1d2ef592011-09-14 18:55:41 +01003614 dget(dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003615 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616
Sage Weil912dbc12011-05-24 13:06:11 -07003617 error = -EBUSY;
Eric W. Biederman7af13642013-10-04 19:15:13 -07003618 if (is_local_mountpoint(dentry))
Sage Weil912dbc12011-05-24 13:06:11 -07003619 goto out;
3620
3621 error = security_inode_rmdir(dir, dentry);
3622 if (error)
3623 goto out;
3624
Sage Weil3cebde22011-05-29 21:20:59 -07003625 shrink_dcache_parent(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003626 error = dir->i_op->rmdir(dir, dentry);
3627 if (error)
3628 goto out;
3629
3630 dentry->d_inode->i_flags |= S_DEAD;
3631 dont_mount(dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07003632 detach_mounts(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003633
3634out:
3635 mutex_unlock(&dentry->d_inode->i_mutex);
Al Viro1d2ef592011-09-14 18:55:41 +01003636 dput(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003637 if (!error)
3638 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 return error;
3640}
Al Viro4d359502014-03-14 12:20:17 -04003641EXPORT_SYMBOL(vfs_rmdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003643static long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644{
3645 int error = 0;
Jeff Layton91a27b22012-10-10 15:25:28 -04003646 struct filename *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 struct dentry *dentry;
3648 struct nameidata nd;
Jeff Laytonc6ee9202012-12-20 16:28:33 -05003649 unsigned int lookup_flags = 0;
3650retry:
3651 name = user_path_parent(dfd, pathname, &nd, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04003652 if (IS_ERR(name))
3653 return PTR_ERR(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654
3655 switch(nd.last_type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003656 case LAST_DOTDOT:
3657 error = -ENOTEMPTY;
3658 goto exit1;
3659 case LAST_DOT:
3660 error = -EINVAL;
3661 goto exit1;
3662 case LAST_ROOT:
3663 error = -EBUSY;
3664 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003666
3667 nd.flags &= ~LOOKUP_PARENT;
Jan Karac30dabf2012-06-12 16:20:30 +02003668 error = mnt_want_write(nd.path.mnt);
3669 if (error)
3670 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003671
Jan Blunck4ac91372008-02-14 19:34:32 -08003672 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08003673 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003675 if (IS_ERR(dentry))
3676 goto exit2;
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04003677 if (!dentry->d_inode) {
3678 error = -ENOENT;
3679 goto exit3;
3680 }
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003681 error = security_path_rmdir(&nd.path, dentry);
3682 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02003683 goto exit3;
Jan Blunck4ac91372008-02-14 19:34:32 -08003684 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
Dave Hansen06227532008-02-15 14:37:34 -08003685exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07003686 dput(dentry);
3687exit2:
Jan Blunck4ac91372008-02-14 19:34:32 -08003688 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Karac30dabf2012-06-12 16:20:30 +02003689 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003691 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 putname(name);
Jeff Laytonc6ee9202012-12-20 16:28:33 -05003693 if (retry_estale(error, lookup_flags)) {
3694 lookup_flags |= LOOKUP_REVAL;
3695 goto retry;
3696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 return error;
3698}
3699
Heiko Carstens3cdad422009-01-14 14:14:22 +01003700SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003701{
3702 return do_rmdir(AT_FDCWD, pathname);
3703}
3704
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003705/**
3706 * vfs_unlink - unlink a filesystem object
3707 * @dir: parent directory
3708 * @dentry: victim
3709 * @delegated_inode: returns victim inode, if the inode is delegated.
3710 *
3711 * The caller must hold dir->i_mutex.
3712 *
3713 * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
3714 * return a reference to the inode in delegated_inode. The caller
3715 * should then break the delegation on that inode and retry. Because
3716 * breaking a delegation may take a long time, the caller should drop
3717 * dir->i_mutex before doing so.
3718 *
3719 * Alternatively, a caller may pass NULL for delegated_inode. This may
3720 * be appropriate for callers that expect the underlying filesystem not
3721 * to be NFS exported.
3722 */
3723int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724{
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003725 struct inode *target = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 int error = may_delete(dir, dentry, 0);
3727
3728 if (error)
3729 return error;
3730
Al Viroacfa4382008-12-04 10:06:33 -05003731 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 return -EPERM;
3733
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003734 mutex_lock(&target->i_mutex);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07003735 if (is_local_mountpoint(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736 error = -EBUSY;
3737 else {
3738 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05003739 if (!error) {
J. Bruce Fields5a146962012-08-28 07:50:40 -07003740 error = try_break_deleg(target, delegated_inode);
3741 if (error)
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003742 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 error = dir->i_op->unlink(dir, dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07003744 if (!error) {
Al Virod83c49f2010-04-30 17:17:09 -04003745 dont_mount(dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07003746 detach_mounts(dentry);
3747 }
Al Virobec10522010-03-03 14:12:08 -05003748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 }
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003750out:
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003751 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752
3753 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
3754 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003755 fsnotify_link_count(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 }
Robert Love0eeca282005-07-12 17:06:03 -04003758
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 return error;
3760}
Al Viro4d359502014-03-14 12:20:17 -04003761EXPORT_SYMBOL(vfs_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762
3763/*
3764 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003765 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 * writeout happening, and we don't want to prevent access to the directory
3767 * while waiting on the I/O.
3768 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003769static long do_unlinkat(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770{
Al Viro2ad94ae2008-07-21 09:32:51 -04003771 int error;
Jeff Layton91a27b22012-10-10 15:25:28 -04003772 struct filename *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 struct dentry *dentry;
3774 struct nameidata nd;
3775 struct inode *inode = NULL;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003776 struct inode *delegated_inode = NULL;
Jeff Layton5d18f812012-12-20 16:38:04 -05003777 unsigned int lookup_flags = 0;
3778retry:
3779 name = user_path_parent(dfd, pathname, &nd, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04003780 if (IS_ERR(name))
3781 return PTR_ERR(name);
Al Viro2ad94ae2008-07-21 09:32:51 -04003782
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 error = -EISDIR;
3784 if (nd.last_type != LAST_NORM)
3785 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003786
3787 nd.flags &= ~LOOKUP_PARENT;
Jan Karac30dabf2012-06-12 16:20:30 +02003788 error = mnt_want_write(nd.path.mnt);
3789 if (error)
3790 goto exit1;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003791retry_deleg:
Jan Blunck4ac91372008-02-14 19:34:32 -08003792 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08003793 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 error = PTR_ERR(dentry);
3795 if (!IS_ERR(dentry)) {
3796 /* Why not before? Because we want correct error value */
Török Edwin50338b82011-06-16 00:06:14 +03003797 if (nd.last.name[nd.last.len])
3798 goto slashes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 inode = dentry->d_inode;
David Howellsb18825a2013-09-12 19:22:53 +01003800 if (d_is_negative(dentry))
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04003801 goto slashes;
3802 ihold(inode);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003803 error = security_path_unlink(&nd.path, dentry);
3804 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02003805 goto exit2;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003806 error = vfs_unlink(nd.path.dentry->d_inode, dentry, &delegated_inode);
Jan Karac30dabf2012-06-12 16:20:30 +02003807exit2:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 dput(dentry);
3809 }
Jan Blunck4ac91372008-02-14 19:34:32 -08003810 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 if (inode)
3812 iput(inode); /* truncate the inode here */
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003813 inode = NULL;
3814 if (delegated_inode) {
J. Bruce Fields5a146962012-08-28 07:50:40 -07003815 error = break_deleg_wait(&delegated_inode);
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003816 if (!error)
3817 goto retry_deleg;
3818 }
Jan Karac30dabf2012-06-12 16:20:30 +02003819 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003821 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 putname(name);
Jeff Layton5d18f812012-12-20 16:38:04 -05003823 if (retry_estale(error, lookup_flags)) {
3824 lookup_flags |= LOOKUP_REVAL;
3825 inode = NULL;
3826 goto retry;
3827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 return error;
3829
3830slashes:
David Howellsb18825a2013-09-12 19:22:53 +01003831 if (d_is_negative(dentry))
3832 error = -ENOENT;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02003833 else if (d_is_dir(dentry))
David Howellsb18825a2013-09-12 19:22:53 +01003834 error = -EISDIR;
3835 else
3836 error = -ENOTDIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 goto exit2;
3838}
3839
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003840SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003841{
3842 if ((flag & ~AT_REMOVEDIR) != 0)
3843 return -EINVAL;
3844
3845 if (flag & AT_REMOVEDIR)
3846 return do_rmdir(dfd, pathname);
3847
3848 return do_unlinkat(dfd, pathname);
3849}
3850
Heiko Carstens3480b252009-01-14 14:14:16 +01003851SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003852{
3853 return do_unlinkat(AT_FDCWD, pathname);
3854}
3855
Miklos Szeredidb2e7472008-06-24 16:50:16 +02003856int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003858 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859
3860 if (error)
3861 return error;
3862
Al Viroacfa4382008-12-04 10:06:33 -05003863 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 return -EPERM;
3865
3866 error = security_inode_symlink(dir, dentry, oldname);
3867 if (error)
3868 return error;
3869
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003871 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003872 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 return error;
3874}
Al Viro4d359502014-03-14 12:20:17 -04003875EXPORT_SYMBOL(vfs_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003877SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
3878 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879{
Al Viro2ad94ae2008-07-21 09:32:51 -04003880 int error;
Jeff Layton91a27b22012-10-10 15:25:28 -04003881 struct filename *from;
Dave Hansen6902d922006-09-30 23:29:01 -07003882 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003883 struct path path;
Jeff Laytonf46d3562012-12-11 12:10:08 -05003884 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885
3886 from = getname(oldname);
Al Viro2ad94ae2008-07-21 09:32:51 -04003887 if (IS_ERR(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 return PTR_ERR(from);
Jeff Laytonf46d3562012-12-11 12:10:08 -05003889retry:
3890 dentry = user_path_create(newdfd, newname, &path, lookup_flags);
Dave Hansen6902d922006-09-30 23:29:01 -07003891 error = PTR_ERR(dentry);
3892 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003893 goto out_putname;
Dave Hansen6902d922006-09-30 23:29:01 -07003894
Jeff Layton91a27b22012-10-10 15:25:28 -04003895 error = security_path_symlink(&path, dentry, from->name);
Al Viroa8104a92012-07-20 02:25:00 +04003896 if (!error)
Jeff Layton91a27b22012-10-10 15:25:28 -04003897 error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
Al Viro921a1652012-07-20 01:15:31 +04003898 done_path_create(&path, dentry);
Jeff Laytonf46d3562012-12-11 12:10:08 -05003899 if (retry_estale(error, lookup_flags)) {
3900 lookup_flags |= LOOKUP_REVAL;
3901 goto retry;
3902 }
Dave Hansen6902d922006-09-30 23:29:01 -07003903out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 putname(from);
3905 return error;
3906}
3907
Heiko Carstens3480b252009-01-14 14:14:16 +01003908SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003909{
3910 return sys_symlinkat(oldname, AT_FDCWD, newname);
3911}
3912
J. Bruce Fields146a8592011-09-20 17:14:31 -04003913/**
3914 * vfs_link - create a new link
3915 * @old_dentry: object to be linked
3916 * @dir: new parent
3917 * @new_dentry: where to create the new link
3918 * @delegated_inode: returns inode needing a delegation break
3919 *
3920 * The caller must hold dir->i_mutex
3921 *
3922 * If vfs_link discovers a delegation on the to-be-linked file in need
3923 * of breaking, it will return -EWOULDBLOCK and return a reference to the
3924 * inode in delegated_inode. The caller should then break the delegation
3925 * and retry. Because breaking a delegation may take a long time, the
3926 * caller should drop the i_mutex before doing so.
3927 *
3928 * Alternatively, a caller may pass NULL for delegated_inode. This may
3929 * be appropriate for callers that expect the underlying filesystem not
3930 * to be NFS exported.
3931 */
3932int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933{
3934 struct inode *inode = old_dentry->d_inode;
Al Viro8de52772012-02-06 12:45:27 -05003935 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 int error;
3937
3938 if (!inode)
3939 return -ENOENT;
3940
Miklos Szeredia95164d2008-07-30 15:08:48 +02003941 error = may_create(dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 if (error)
3943 return error;
3944
3945 if (dir->i_sb != inode->i_sb)
3946 return -EXDEV;
3947
3948 /*
3949 * A link to an append-only or immutable file cannot be created.
3950 */
3951 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3952 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05003953 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003955 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 return -EPERM;
3957
3958 error = security_inode_link(old_dentry, dir, new_dentry);
3959 if (error)
3960 return error;
3961
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003962 mutex_lock(&inode->i_mutex);
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05303963 /* Make sure we don't allow creating hardlink to an unlinked file */
Al Virof4e0c302013-06-11 08:34:36 +04003964 if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05303965 error = -ENOENT;
Al Viro8de52772012-02-06 12:45:27 -05003966 else if (max_links && inode->i_nlink >= max_links)
3967 error = -EMLINK;
J. Bruce Fields146a8592011-09-20 17:14:31 -04003968 else {
3969 error = try_break_deleg(inode, delegated_inode);
3970 if (!error)
3971 error = dir->i_op->link(old_dentry, dir, new_dentry);
3972 }
Al Virof4e0c302013-06-11 08:34:36 +04003973
3974 if (!error && (inode->i_state & I_LINKABLE)) {
3975 spin_lock(&inode->i_lock);
3976 inode->i_state &= ~I_LINKABLE;
3977 spin_unlock(&inode->i_lock);
3978 }
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003979 mutex_unlock(&inode->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07003980 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003981 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 return error;
3983}
Al Viro4d359502014-03-14 12:20:17 -04003984EXPORT_SYMBOL(vfs_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985
3986/*
3987 * Hardlinks are often used in delicate situations. We avoid
3988 * security-related surprises by not following symlinks on the
3989 * newname. --KAB
3990 *
3991 * We don't follow them on the oldname either to be compatible
3992 * with linux 2.0, and to avoid hard-linking to directories
3993 * and other special files. --ADM
3994 */
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003995SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
3996 int, newdfd, const char __user *, newname, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997{
3998 struct dentry *new_dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003999 struct path old_path, new_path;
J. Bruce Fields146a8592011-09-20 17:14:31 -04004000 struct inode *delegated_inode = NULL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304001 int how = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304004 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08004005 return -EINVAL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304006 /*
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07004007 * To use null names we require CAP_DAC_READ_SEARCH
4008 * This ensures that not everyone will be able to create
4009 * handlink using the passed filedescriptor.
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304010 */
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07004011 if (flags & AT_EMPTY_PATH) {
4012 if (!capable(CAP_DAC_READ_SEARCH))
4013 return -ENOENT;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304014 how = LOOKUP_EMPTY;
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07004015 }
Ulrich Drepperc04030e2006-02-24 13:04:21 -08004016
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304017 if (flags & AT_SYMLINK_FOLLOW)
4018 how |= LOOKUP_FOLLOW;
Jeff Layton442e31c2012-12-20 16:15:38 -05004019retry:
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304020 error = user_path_at(olddfd, oldname, how, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04004022 return error;
4023
Jeff Layton442e31c2012-12-20 16:15:38 -05004024 new_dentry = user_path_create(newdfd, newname, &new_path,
4025 (how & LOOKUP_REVAL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07004027 if (IS_ERR(new_dentry))
Al Virodae6ad82011-06-26 11:50:15 -04004028 goto out;
4029
4030 error = -EXDEV;
4031 if (old_path.mnt != new_path.mnt)
4032 goto out_dput;
Kees Cook800179c2012-07-25 17:29:07 -07004033 error = may_linkat(&old_path);
4034 if (unlikely(error))
4035 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04004036 error = security_path_link(old_path.dentry, &new_path, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004037 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04004038 goto out_dput;
J. Bruce Fields146a8592011-09-20 17:14:31 -04004039 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode);
Dave Hansen75c3f292008-02-15 14:37:45 -08004040out_dput:
Al Viro921a1652012-07-20 01:15:31 +04004041 done_path_create(&new_path, new_dentry);
J. Bruce Fields146a8592011-09-20 17:14:31 -04004042 if (delegated_inode) {
4043 error = break_deleg_wait(&delegated_inode);
Oleg Drokind22e6332014-01-31 15:41:58 -05004044 if (!error) {
4045 path_put(&old_path);
J. Bruce Fields146a8592011-09-20 17:14:31 -04004046 goto retry;
Oleg Drokind22e6332014-01-31 15:41:58 -05004047 }
J. Bruce Fields146a8592011-09-20 17:14:31 -04004048 }
Jeff Layton442e31c2012-12-20 16:15:38 -05004049 if (retry_estale(error, how)) {
Oleg Drokind22e6332014-01-31 15:41:58 -05004050 path_put(&old_path);
Jeff Layton442e31c2012-12-20 16:15:38 -05004051 how |= LOOKUP_REVAL;
4052 goto retry;
4053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054out:
Al Viro2d8f3032008-07-22 09:59:21 -04004055 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056
4057 return error;
4058}
4059
Heiko Carstens3480b252009-01-14 14:14:16 +01004060SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004061{
Ulrich Drepperc04030e2006-02-24 13:04:21 -08004062 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004063}
4064
Miklos Szeredibc270272014-04-01 17:08:42 +02004065/**
4066 * vfs_rename - rename a filesystem object
4067 * @old_dir: parent of source
4068 * @old_dentry: source
4069 * @new_dir: parent of destination
4070 * @new_dentry: destination
4071 * @delegated_inode: returns an inode needing a delegation break
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004072 * @flags: rename flags
Miklos Szeredibc270272014-04-01 17:08:42 +02004073 *
4074 * The caller must hold multiple mutexes--see lock_rename()).
4075 *
4076 * If vfs_rename discovers a delegation in need of breaking at either
4077 * the source or destination, it will return -EWOULDBLOCK and return a
4078 * reference to the inode in delegated_inode. The caller should then
4079 * break the delegation and retry. Because breaking a delegation may
4080 * take a long time, the caller should drop all locks before doing
4081 * so.
4082 *
4083 * Alternatively, a caller may pass NULL for delegated_inode. This may
4084 * be appropriate for callers that expect the underlying filesystem not
4085 * to be NFS exported.
4086 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 * The worst of all namespace operations - renaming directory. "Perverted"
4088 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
4089 * Problems:
J. Bruce Fieldsd03b29a2014-02-17 16:52:33 -05004090 * a) we can get into loop creation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 * b) race potential - two innocent renames can create a loop together.
4092 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004093 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 * story.
J. Bruce Fields6cedba82012-03-05 11:40:41 -05004095 * c) we have to lock _four_ objects - parents and victim (if it exists),
4096 * and source (if it is not a directory).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004097 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 * whether the target exists). Solution: try to be smart with locking
4099 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004100 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 * move will be locked. Thus we can rank directories by the tree
4102 * (ancestors first) and rank all non-directories after them.
4103 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004104 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 * HOWEVER, it relies on the assumption that any object with ->lookup()
4106 * has no more than 1 dentry. If "hybrid" objects will ever appear,
4107 * we'd better make sure that there's no link(2) for them.
Sage Weile4eaac02011-05-24 13:06:07 -07004108 * d) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004109 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05004111 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 * locking].
4113 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004115 struct inode *new_dir, struct dentry *new_dentry,
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004116 struct inode **delegated_inode, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117{
4118 int error;
Miklos Szeredibc270272014-04-01 17:08:42 +02004119 bool is_dir = d_is_dir(old_dentry);
Eric Paris59b0df22010-02-08 12:53:52 -05004120 const unsigned char *old_name;
Miklos Szeredibc270272014-04-01 17:08:42 +02004121 struct inode *source = old_dentry->d_inode;
4122 struct inode *target = new_dentry->d_inode;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004123 bool new_is_dir = false;
4124 unsigned max_links = new_dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125
Miklos Szeredibc270272014-04-01 17:08:42 +02004126 if (source == target)
4127 return 0;
4128
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 error = may_delete(old_dir, old_dentry, is_dir);
4130 if (error)
4131 return error;
4132
Miklos Szeredida1ce062014-04-01 17:08:43 +02004133 if (!target) {
Miklos Szeredia95164d2008-07-30 15:08:48 +02004134 error = may_create(new_dir, new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004135 } else {
4136 new_is_dir = d_is_dir(new_dentry);
4137
4138 if (!(flags & RENAME_EXCHANGE))
4139 error = may_delete(new_dir, new_dentry, is_dir);
4140 else
4141 error = may_delete(new_dir, new_dentry, new_is_dir);
4142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 if (error)
4144 return error;
4145
Miklos Szeredi7177a9c2014-07-23 15:15:30 +02004146 if (!old_dir->i_op->rename && !old_dir->i_op->rename2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 return -EPERM;
4148
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004149 if (flags && !old_dir->i_op->rename2)
4150 return -EINVAL;
4151
Miklos Szeredibc270272014-04-01 17:08:42 +02004152 /*
4153 * If we are going to change the parent - check write permissions,
4154 * we'll need to flip '..'.
4155 */
Miklos Szeredida1ce062014-04-01 17:08:43 +02004156 if (new_dir != old_dir) {
4157 if (is_dir) {
4158 error = inode_permission(source, MAY_WRITE);
4159 if (error)
4160 return error;
4161 }
4162 if ((flags & RENAME_EXCHANGE) && new_is_dir) {
4163 error = inode_permission(target, MAY_WRITE);
4164 if (error)
4165 return error;
4166 }
Miklos Szeredibc270272014-04-01 17:08:42 +02004167 }
Robert Love0eeca282005-07-12 17:06:03 -04004168
Miklos Szeredi0b3974e2014-04-01 17:08:43 +02004169 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry,
4170 flags);
Miklos Szeredibc270272014-04-01 17:08:42 +02004171 if (error)
4172 return error;
4173
4174 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
4175 dget(new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004176 if (!is_dir || (flags & RENAME_EXCHANGE))
Miklos Szeredibc270272014-04-01 17:08:42 +02004177 lock_two_nondirectories(source, target);
4178 else if (target)
4179 mutex_lock(&target->i_mutex);
4180
4181 error = -EBUSY;
Eric W. Biederman7af13642013-10-04 19:15:13 -07004182 if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry))
Miklos Szeredibc270272014-04-01 17:08:42 +02004183 goto out;
4184
Miklos Szeredida1ce062014-04-01 17:08:43 +02004185 if (max_links && new_dir != old_dir) {
Miklos Szeredibc270272014-04-01 17:08:42 +02004186 error = -EMLINK;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004187 if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links)
Miklos Szeredibc270272014-04-01 17:08:42 +02004188 goto out;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004189 if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir &&
4190 old_dir->i_nlink >= max_links)
4191 goto out;
4192 }
4193 if (is_dir && !(flags & RENAME_EXCHANGE) && target)
4194 shrink_dcache_parent(new_dentry);
4195 if (!is_dir) {
Miklos Szeredibc270272014-04-01 17:08:42 +02004196 error = try_break_deleg(source, delegated_inode);
4197 if (error)
4198 goto out;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004199 }
4200 if (target && !new_is_dir) {
4201 error = try_break_deleg(target, delegated_inode);
4202 if (error)
4203 goto out;
Miklos Szeredibc270272014-04-01 17:08:42 +02004204 }
Miklos Szeredi7177a9c2014-07-23 15:15:30 +02004205 if (!old_dir->i_op->rename2) {
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004206 error = old_dir->i_op->rename(old_dir, old_dentry,
4207 new_dir, new_dentry);
4208 } else {
Miklos Szeredi7177a9c2014-07-23 15:15:30 +02004209 WARN_ON(old_dir->i_op->rename != NULL);
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004210 error = old_dir->i_op->rename2(old_dir, old_dentry,
4211 new_dir, new_dentry, flags);
4212 }
Miklos Szeredibc270272014-04-01 17:08:42 +02004213 if (error)
4214 goto out;
4215
Miklos Szeredida1ce062014-04-01 17:08:43 +02004216 if (!(flags & RENAME_EXCHANGE) && target) {
Miklos Szeredibc270272014-04-01 17:08:42 +02004217 if (is_dir)
4218 target->i_flags |= S_DEAD;
4219 dont_mount(new_dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07004220 detach_mounts(new_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004221 }
Miklos Szeredida1ce062014-04-01 17:08:43 +02004222 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) {
4223 if (!(flags & RENAME_EXCHANGE))
4224 d_move(old_dentry, new_dentry);
4225 else
4226 d_exchange(old_dentry, new_dentry);
4227 }
Miklos Szeredibc270272014-04-01 17:08:42 +02004228out:
Miklos Szeredida1ce062014-04-01 17:08:43 +02004229 if (!is_dir || (flags & RENAME_EXCHANGE))
Miklos Szeredibc270272014-04-01 17:08:42 +02004230 unlock_two_nondirectories(source, target);
4231 else if (target)
4232 mutex_unlock(&target->i_mutex);
4233 dput(new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004234 if (!error) {
Al Viro123df292009-12-25 04:57:57 -05004235 fsnotify_move(old_dir, new_dir, old_name, is_dir,
Miklos Szeredida1ce062014-04-01 17:08:43 +02004236 !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
4237 if (flags & RENAME_EXCHANGE) {
4238 fsnotify_move(new_dir, old_dir, old_dentry->d_name.name,
4239 new_is_dir, NULL, new_dentry);
4240 }
4241 }
Robert Love0eeca282005-07-12 17:06:03 -04004242 fsnotify_oldname_free(old_name);
4243
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 return error;
4245}
Al Viro4d359502014-03-14 12:20:17 -04004246EXPORT_SYMBOL(vfs_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004248SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
4249 int, newdfd, const char __user *, newname, unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250{
Al Viro2ad94ae2008-07-21 09:32:51 -04004251 struct dentry *old_dir, *new_dir;
4252 struct dentry *old_dentry, *new_dentry;
4253 struct dentry *trap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 struct nameidata oldnd, newnd;
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004255 struct inode *delegated_inode = NULL;
Jeff Layton91a27b22012-10-10 15:25:28 -04004256 struct filename *from;
4257 struct filename *to;
Jeff Laytonc6a94282012-12-11 12:10:10 -05004258 unsigned int lookup_flags = 0;
4259 bool should_retry = false;
Al Viro2ad94ae2008-07-21 09:32:51 -04004260 int error;
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004261
Miklos Szeredi0d7a8552014-10-24 00:14:37 +02004262 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredida1ce062014-04-01 17:08:43 +02004263 return -EINVAL;
4264
Miklos Szeredi0d7a8552014-10-24 00:14:37 +02004265 if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) &&
4266 (flags & RENAME_EXCHANGE))
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004267 return -EINVAL;
4268
Miklos Szeredi0d7a8552014-10-24 00:14:37 +02004269 if ((flags & RENAME_WHITEOUT) && !capable(CAP_MKNOD))
4270 return -EPERM;
4271
Jeff Laytonc6a94282012-12-11 12:10:10 -05004272retry:
4273 from = user_path_parent(olddfd, oldname, &oldnd, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04004274 if (IS_ERR(from)) {
4275 error = PTR_ERR(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 goto exit;
Jeff Layton91a27b22012-10-10 15:25:28 -04004277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278
Jeff Laytonc6a94282012-12-11 12:10:10 -05004279 to = user_path_parent(newdfd, newname, &newnd, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04004280 if (IS_ERR(to)) {
4281 error = PTR_ERR(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 goto exit1;
Jeff Layton91a27b22012-10-10 15:25:28 -04004283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284
4285 error = -EXDEV;
Jan Blunck4ac91372008-02-14 19:34:32 -08004286 if (oldnd.path.mnt != newnd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 goto exit2;
4288
Jan Blunck4ac91372008-02-14 19:34:32 -08004289 old_dir = oldnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 error = -EBUSY;
4291 if (oldnd.last_type != LAST_NORM)
4292 goto exit2;
4293
Jan Blunck4ac91372008-02-14 19:34:32 -08004294 new_dir = newnd.path.dentry;
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004295 if (flags & RENAME_NOREPLACE)
4296 error = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 if (newnd.last_type != LAST_NORM)
4298 goto exit2;
4299
Jan Karac30dabf2012-06-12 16:20:30 +02004300 error = mnt_want_write(oldnd.path.mnt);
4301 if (error)
4302 goto exit2;
4303
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004304 oldnd.flags &= ~LOOKUP_PARENT;
4305 newnd.flags &= ~LOOKUP_PARENT;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004306 if (!(flags & RENAME_EXCHANGE))
4307 newnd.flags |= LOOKUP_RENAME_TARGET;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004308
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004309retry_deleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 trap = lock_rename(new_dir, old_dir);
4311
Christoph Hellwig49705b72005-11-08 21:35:06 -08004312 old_dentry = lookup_hash(&oldnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 error = PTR_ERR(old_dentry);
4314 if (IS_ERR(old_dentry))
4315 goto exit3;
4316 /* source must exist */
4317 error = -ENOENT;
David Howellsb18825a2013-09-12 19:22:53 +01004318 if (d_is_negative(old_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 goto exit4;
Christoph Hellwig49705b72005-11-08 21:35:06 -08004320 new_dentry = lookup_hash(&newnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 error = PTR_ERR(new_dentry);
4322 if (IS_ERR(new_dentry))
4323 goto exit4;
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004324 error = -EEXIST;
4325 if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry))
4326 goto exit5;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004327 if (flags & RENAME_EXCHANGE) {
4328 error = -ENOENT;
4329 if (d_is_negative(new_dentry))
4330 goto exit5;
4331
4332 if (!d_is_dir(new_dentry)) {
4333 error = -ENOTDIR;
4334 if (newnd.last.name[newnd.last.len])
4335 goto exit5;
4336 }
4337 }
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004338 /* unless the source is a directory trailing slashes give -ENOTDIR */
4339 if (!d_is_dir(old_dentry)) {
4340 error = -ENOTDIR;
4341 if (oldnd.last.name[oldnd.last.len])
4342 goto exit5;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004343 if (!(flags & RENAME_EXCHANGE) && newnd.last.name[newnd.last.len])
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004344 goto exit5;
4345 }
4346 /* source should not be ancestor of target */
4347 error = -EINVAL;
4348 if (old_dentry == trap)
4349 goto exit5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 /* target should not be an ancestor of source */
Miklos Szeredida1ce062014-04-01 17:08:43 +02004351 if (!(flags & RENAME_EXCHANGE))
4352 error = -ENOTEMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 if (new_dentry == trap)
4354 goto exit5;
4355
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004356 error = security_path_rename(&oldnd.path, old_dentry,
Miklos Szeredi0b3974e2014-04-01 17:08:43 +02004357 &newnd.path, new_dentry, flags);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004358 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02004359 goto exit5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 error = vfs_rename(old_dir->d_inode, old_dentry,
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004361 new_dir->d_inode, new_dentry,
4362 &delegated_inode, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363exit5:
4364 dput(new_dentry);
4365exit4:
4366 dput(old_dentry);
4367exit3:
4368 unlock_rename(new_dir, old_dir);
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004369 if (delegated_inode) {
4370 error = break_deleg_wait(&delegated_inode);
4371 if (!error)
4372 goto retry_deleg;
4373 }
Jan Karac30dabf2012-06-12 16:20:30 +02004374 mnt_drop_write(oldnd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375exit2:
Jeff Laytonc6a94282012-12-11 12:10:10 -05004376 if (retry_estale(error, lookup_flags))
4377 should_retry = true;
Jan Blunck1d957f92008-02-14 19:34:35 -08004378 path_put(&newnd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04004379 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08004381 path_put(&oldnd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 putname(from);
Jeff Laytonc6a94282012-12-11 12:10:10 -05004383 if (should_retry) {
4384 should_retry = false;
4385 lookup_flags |= LOOKUP_REVAL;
4386 goto retry;
4387 }
Al Viro2ad94ae2008-07-21 09:32:51 -04004388exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 return error;
4390}
4391
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004392SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4393 int, newdfd, const char __user *, newname)
4394{
4395 return sys_renameat2(olddfd, oldname, newdfd, newname, 0);
4396}
4397
Heiko Carstensa26eab22009-01-14 14:14:17 +01004398SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004399{
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004400 return sys_renameat2(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004401}
4402
Miklos Szeredi787fb6b2014-10-24 00:14:36 +02004403int vfs_whiteout(struct inode *dir, struct dentry *dentry)
4404{
4405 int error = may_create(dir, dentry);
4406 if (error)
4407 return error;
4408
4409 if (!dir->i_op->mknod)
4410 return -EPERM;
4411
4412 return dir->i_op->mknod(dir, dentry,
4413 S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
4414}
4415EXPORT_SYMBOL(vfs_whiteout);
4416
Al Viro5d826c82014-03-14 13:42:45 -04004417int readlink_copy(char __user *buffer, int buflen, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418{
Al Viro5d826c82014-03-14 13:42:45 -04004419 int len = PTR_ERR(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 if (IS_ERR(link))
4421 goto out;
4422
4423 len = strlen(link);
4424 if (len > (unsigned) buflen)
4425 len = buflen;
4426 if (copy_to_user(buffer, link, len))
4427 len = -EFAULT;
4428out:
4429 return len;
4430}
Al Viro5d826c82014-03-14 13:42:45 -04004431EXPORT_SYMBOL(readlink_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432
4433/*
4434 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
4435 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
4436 * using) it for any given inode is up to filesystem.
4437 */
4438int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4439{
4440 struct nameidata nd;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004441 void *cookie;
Marcin Slusarz694a1762008-06-09 16:40:37 -07004442 int res;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004443
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 nd.depth = 0;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004445 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
Marcin Slusarz694a1762008-06-09 16:40:37 -07004446 if (IS_ERR(cookie))
4447 return PTR_ERR(cookie);
4448
Al Viro5d826c82014-03-14 13:42:45 -04004449 res = readlink_copy(buffer, buflen, nd_get_link(&nd));
Marcin Slusarz694a1762008-06-09 16:40:37 -07004450 if (dentry->d_inode->i_op->put_link)
4451 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
4452 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453}
Al Viro4d359502014-03-14 12:20:17 -04004454EXPORT_SYMBOL(generic_readlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456/* get the link contents into pagecache */
4457static char *page_getlink(struct dentry * dentry, struct page **ppage)
4458{
Duane Griffinebd09ab2008-12-19 20:47:12 +00004459 char *kaddr;
4460 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 struct address_space *mapping = dentry->d_inode->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -07004462 page = read_mapping_page(mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 if (IS_ERR(page))
Nick Piggin6fe69002007-05-06 14:49:04 -07004464 return (char*)page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 *ppage = page;
Duane Griffinebd09ab2008-12-19 20:47:12 +00004466 kaddr = kmap(page);
4467 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
4468 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469}
4470
4471int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4472{
4473 struct page *page = NULL;
Al Viro5d826c82014-03-14 13:42:45 -04004474 int res = readlink_copy(buffer, buflen, page_getlink(dentry, &page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 if (page) {
4476 kunmap(page);
4477 page_cache_release(page);
4478 }
4479 return res;
4480}
Al Viro4d359502014-03-14 12:20:17 -04004481EXPORT_SYMBOL(page_readlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004483void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004485 struct page *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 nd_set_link(nd, page_getlink(dentry, &page));
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004487 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488}
Al Viro4d359502014-03-14 12:20:17 -04004489EXPORT_SYMBOL(page_follow_link_light);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004491void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004493 struct page *page = cookie;
4494
4495 if (page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 kunmap(page);
4497 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 }
4499}
Al Viro4d359502014-03-14 12:20:17 -04004500EXPORT_SYMBOL(page_put_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501
Nick Piggin54566b22009-01-04 12:00:53 -08004502/*
4503 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
4504 */
4505int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506{
4507 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004508 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07004509 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08004510 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 char *kaddr;
Nick Piggin54566b22009-01-04 12:00:53 -08004512 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
4513 if (nofs)
4514 flags |= AOP_FLAG_NOFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515
NeilBrown7e53cac2006-03-25 03:07:57 -08004516retry:
Nick Pigginafddba42007-10-16 01:25:01 -07004517 err = pagecache_write_begin(NULL, mapping, 0, len-1,
Nick Piggin54566b22009-01-04 12:00:53 -08004518 flags, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07004520 goto fail;
4521
Cong Wange8e3c3d2011-11-25 23:14:27 +08004522 kaddr = kmap_atomic(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 memcpy(kaddr, symname, len-1);
Cong Wange8e3c3d2011-11-25 23:14:27 +08004524 kunmap_atomic(kaddr);
Nick Pigginafddba42007-10-16 01:25:01 -07004525
4526 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4527 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 if (err < 0)
4529 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07004530 if (err < len-1)
4531 goto retry;
4532
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 mark_inode_dirty(inode);
4534 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535fail:
4536 return err;
4537}
Al Viro4d359502014-03-14 12:20:17 -04004538EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004540int page_symlink(struct inode *inode, const char *symname, int len)
4541{
4542 return __page_symlink(inode, symname, len,
Nick Piggin54566b22009-01-04 12:00:53 -08004543 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004544}
Al Viro4d359502014-03-14 12:20:17 -04004545EXPORT_SYMBOL(page_symlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004546
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08004547const struct inode_operations page_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 .readlink = generic_readlink,
4549 .follow_link = page_follow_link_light,
4550 .put_link = page_put_link,
4551};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552EXPORT_SYMBOL(page_symlink_inode_operations);