blob: 909c01d31e953de582cc9f2e898bb50c890b3189 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/slab.h>
20#include <linux/fs.h>
21#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/pagemap.h>
Robert Love0eeca282005-07-12 17:06:03 -040023#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/personality.h>
25#include <linux/security.h>
Mimi Zohar6146f0d2009-02-04 09:06:57 -050026#include <linux/ima.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/syscalls.h>
28#include <linux/mount.h>
29#include <linux/audit.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080030#include <linux/capability.h>
Trond Myklebust834f2a42005-10-18 14:20:16 -070031#include <linux/file.h>
Ulrich Drepper5590ff02006-01-18 17:43:53 -080032#include <linux/fcntl.h>
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070033#include <linux/device_cgroup.h>
Al Viro5ad4e532009-03-29 19:50:06 -040034#include <linux/fs_struct.h>
Linus Torvaldse77819e2011-07-22 19:30:19 -070035#include <linux/posix_acl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/uaccess.h>
37
Eric Parise81e3f42009-12-04 15:47:36 -050038#include "internal.h"
Al Viroc7105362011-11-24 18:22:03 -050039#include "mount.h"
Eric Parise81e3f42009-12-04 15:47:36 -050040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041/* [Feb-1997 T. Schoebel-Theuer]
42 * Fundamental changes in the pathname lookup mechanisms (namei)
43 * were necessary because of omirr. The reason is that omirr needs
44 * to know the _real_ pathname, not the user-supplied one, in case
45 * of symlinks (and also when transname replacements occur).
46 *
47 * The new code replaces the old recursive symlink resolution with
48 * an iterative one (in case of non-nested symlink chains). It does
49 * this with calls to <fs>_follow_link().
50 * As a side effect, dir_namei(), _namei() and follow_link() are now
51 * replaced with a single function lookup_dentry() that can handle all
52 * the special cases of the former code.
53 *
54 * With the new dcache, the pathname is stored at each inode, at least as
55 * long as the refcount of the inode is positive. As a side effect, the
56 * size of the dcache depends on the inode cache and thus is dynamic.
57 *
58 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
59 * resolution to correspond with current state of the code.
60 *
61 * Note that the symlink resolution is not *completely* iterative.
62 * There is still a significant amount of tail- and mid- recursion in
63 * the algorithm. Also, note that <fs>_readlink() is not used in
64 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
65 * may return different results than <fs>_follow_link(). Many virtual
66 * filesystems (including /proc) exhibit this behavior.
67 */
68
69/* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
70 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
71 * and the name already exists in form of a symlink, try to create the new
72 * name indicated by the symlink. The old code always complained that the
73 * name already exists, due to not following the symlink even if its target
74 * is nonexistent. The new semantics affects also mknod() and link() when
Lucas De Marchi25985ed2011-03-30 22:57:33 -030075 * the name is a symlink pointing to a non-existent name.
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 *
77 * I don't know which semantics is the right one, since I have no access
78 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
79 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
80 * "old" one. Personally, I think the new semantics is much more logical.
81 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
82 * file does succeed in both HP-UX and SunOs, but not in Solaris
83 * and in the old Linux semantics.
84 */
85
86/* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
87 * semantics. See the comments in "open_namei" and "do_link" below.
88 *
89 * [10-Sep-98 Alan Modra] Another symlink change.
90 */
91
92/* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
93 * inside the path - always follow.
94 * in the last component in creation/removal/renaming - never follow.
95 * if LOOKUP_FOLLOW passed - follow.
96 * if the pathname has trailing slashes - follow.
97 * otherwise - don't follow.
98 * (applied in that order).
99 *
100 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
101 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
102 * During the 2.4 we need to fix the userland stuff depending on it -
103 * hopefully we will be able to get rid of that wart in 2.5. So far only
104 * XEmacs seems to be relying on it...
105 */
106/*
107 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
Arjan van de Vena11f3a02006-03-23 03:00:33 -0800108 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 * any extra contention...
110 */
111
112/* In order to reduce some races, while at the same time doing additional
113 * checking and hopefully speeding things up, we copy filenames to the
114 * kernel data space before using them..
115 *
116 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
117 * PATH_MAX includes the nul terminator --RR.
118 */
Jeff Layton9cee5ca2012-10-10 15:25:28 -0400119void final_putname(struct filename *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
Jeff Layton869724e2012-10-10 16:43:13 -0400121 if (name->separate) {
122 __putname(name->name);
123 kfree(name);
124 } else {
125 __putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
Jeff Layton869724e2012-10-10 16:43:13 -0400129#define EMBEDDED_NAME_MAX (PATH_MAX - sizeof(struct filename))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Jeff Layton9cee5ca2012-10-10 15:25:28 -0400131static struct filename *
132getname_flags(const char __user *filename, int flags, int *empty)
133{
134 struct filename *result, *err;
Linus Torvalds256a73d2012-04-28 14:38:32 -0700135 int len;
Jeff Layton869724e2012-10-10 16:43:13 -0400136 long max;
137 char *kname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Jeff Layton869724e2012-10-10 16:43:13 -0400139 result = __getname();
Linus Torvalds256a73d2012-04-28 14:38:32 -0700140 if (unlikely(!result))
Eric Paris4043cde2012-01-03 14:23:08 -0500141 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Jeff Layton869724e2012-10-10 16:43:13 -0400143 /*
144 * First, try to embed the struct filename inside the names_cache
145 * allocation
146 */
147 kname = (char *)result + sizeof(*result);
Jeff Layton9cee5ca2012-10-10 15:25:28 -0400148 result->name = kname;
Jeff Layton869724e2012-10-10 16:43:13 -0400149 result->separate = false;
150 max = EMBEDDED_NAME_MAX;
151
152recopy:
153 len = strncpy_from_user(kname, filename, max);
Jeff Layton9cee5ca2012-10-10 15:25:28 -0400154 if (unlikely(len < 0)) {
155 err = ERR_PTR(len);
Linus Torvalds256a73d2012-04-28 14:38:32 -0700156 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 }
Linus Torvalds256a73d2012-04-28 14:38:32 -0700158
Jeff Layton869724e2012-10-10 16:43:13 -0400159 /*
160 * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
161 * separate struct filename so we can dedicate the entire
162 * names_cache allocation for the pathname, and re-do the copy from
163 * userland.
164 */
165 if (len == EMBEDDED_NAME_MAX && max == EMBEDDED_NAME_MAX) {
166 kname = (char *)result;
167
168 result = kzalloc(sizeof(*result), GFP_KERNEL);
169 if (!result) {
170 err = ERR_PTR(-ENOMEM);
171 result = (struct filename *)kname;
172 goto error;
173 }
174 result->name = kname;
175 result->separate = true;
176 max = PATH_MAX;
177 goto recopy;
178 }
179
Linus Torvalds256a73d2012-04-28 14:38:32 -0700180 /* The empty path is special. */
181 if (unlikely(!len)) {
182 if (empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 *empty = 1;
Linus Torvalds256a73d2012-04-28 14:38:32 -0700184 err = ERR_PTR(-ENOENT);
185 if (!(flags & LOOKUP_EMPTY))
186 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
Linus Torvalds256a73d2012-04-28 14:38:32 -0700188
189 err = ERR_PTR(-ENAMETOOLONG);
Jeff Layton869724e2012-10-10 16:43:13 -0400190 if (unlikely(len >= PATH_MAX))
191 goto error;
192
193 result->uptr = filename;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 audit_getname(result);
195 return result;
Linus Torvalds256a73d2012-04-28 14:38:32 -0700196
197error:
Jeff Layton869724e2012-10-10 16:43:13 -0400198 final_putname(result);
Linus Torvalds256a73d2012-04-28 14:38:32 -0700199 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
Jeff Layton9cee5ca2012-10-10 15:25:28 -0400202struct filename *
203getname(const char __user * filename)
Al Virof52e0c12011-03-14 18:56:51 -0400204{
Linus Torvaldsf7493e52012-03-22 16:10:40 -0700205 return getname_flags(filename, 0, NULL);
Al Virof52e0c12011-03-14 18:56:51 -0400206}
Jeff Layton9cee5ca2012-10-10 15:25:28 -0400207EXPORT_SYMBOL(getname);
Al Virof52e0c12011-03-14 18:56:51 -0400208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209#ifdef CONFIG_AUDITSYSCALL
Jeff Layton9cee5ca2012-10-10 15:25:28 -0400210void putname(struct filename *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
Al Viro5ac3a9c2006-07-16 06:38:45 -0400212 if (unlikely(!audit_dummy_context()))
Jeff Layton9cee5ca2012-10-10 15:25:28 -0400213 return audit_putname(name);
214 final_putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216#endif
217
Linus Torvaldse77819e2011-07-22 19:30:19 -0700218static int check_acl(struct inode *inode, int mask)
219{
Linus Torvalds84635d62011-07-25 22:47:03 -0700220#ifdef CONFIG_FS_POSIX_ACL
Linus Torvaldse77819e2011-07-22 19:30:19 -0700221 struct posix_acl *acl;
222
Linus Torvaldse77819e2011-07-22 19:30:19 -0700223 if (mask & MAY_NOT_BLOCK) {
Al Viro35678662011-08-02 21:32:13 -0400224 acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
225 if (!acl)
Linus Torvaldse77819e2011-07-22 19:30:19 -0700226 return -EAGAIN;
Al Viro35678662011-08-02 21:32:13 -0400227 /* no ->get_acl() calls in RCU mode... */
228 if (acl == ACL_NOT_CACHED)
229 return -ECHILD;
Ari Savolainen206b1d02011-08-06 19:43:07 +0300230 return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
Linus Torvaldse77819e2011-07-22 19:30:19 -0700231 }
232
233 acl = get_cached_acl(inode, ACL_TYPE_ACCESS);
234
235 /*
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200236 * A filesystem can force a ACL callback by just never filling the
237 * ACL cache. But normally you'd fill the cache either at inode
238 * instantiation time, or on the first ->get_acl call.
Linus Torvaldse77819e2011-07-22 19:30:19 -0700239 *
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200240 * If the filesystem doesn't have a get_acl() function at all, we'll
241 * just create the negative cache entry.
Linus Torvaldse77819e2011-07-22 19:30:19 -0700242 */
243 if (acl == ACL_NOT_CACHED) {
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200244 if (inode->i_op->get_acl) {
245 acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS);
246 if (IS_ERR(acl))
247 return PTR_ERR(acl);
248 } else {
249 set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
250 return -EAGAIN;
251 }
Linus Torvaldse77819e2011-07-22 19:30:19 -0700252 }
253
254 if (acl) {
255 int error = posix_acl_permission(inode, acl, mask);
256 posix_acl_release(acl);
257 return error;
258 }
Linus Torvalds84635d62011-07-25 22:47:03 -0700259#endif
Linus Torvaldse77819e2011-07-22 19:30:19 -0700260
261 return -EAGAIN;
262}
263
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700264/*
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530265 * This does the basic permission checking
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700266 */
Al Viro7e401452011-06-20 19:12:17 -0400267static int acl_permission_check(struct inode *inode, int mask)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700268{
Linus Torvalds26cf46b2011-05-13 11:51:01 -0700269 unsigned int mode = inode->i_mode;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700270
Serge E. Hallyne795b712011-03-23 16:43:25 -0700271 if (current_user_ns() != inode_userns(inode))
272 goto other_perms;
273
Linus Torvalds14067ff2011-07-25 19:55:52 -0700274 if (likely(current_fsuid() == inode->i_uid))
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700275 mode >>= 6;
276 else {
Linus Torvaldse77819e2011-07-22 19:30:19 -0700277 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
Al Viro7e401452011-06-20 19:12:17 -0400278 int error = check_acl(inode, mask);
Nick Pigginb74c79e2011-01-07 17:49:58 +1100279 if (error != -EAGAIN)
280 return error;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700281 }
282
283 if (in_group_p(inode->i_gid))
284 mode >>= 3;
285 }
286
Serge E. Hallyne795b712011-03-23 16:43:25 -0700287other_perms:
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700288 /*
289 * If the DACs are ok we don't need any capability check.
290 */
Al Viro9c2c7032011-06-20 19:06:22 -0400291 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700292 return 0;
293 return -EACCES;
294}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296/**
Nick Pigginb74c79e2011-01-07 17:49:58 +1100297 * generic_permission - check for access rights on a Posix-like filesystem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 * @inode: inode to check access rights for
Andreas Gruenbacher8fd90c82011-10-23 23:13:30 +0530299 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 *
301 * Used to check for read/write/execute permissions on a file.
302 * We use "fsuid" for this, letting us set arbitrary permissions
303 * for filesystem access without changing the "normal" uids which
Nick Pigginb74c79e2011-01-07 17:49:58 +1100304 * are used for other things.
305 *
306 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
307 * request cannot be satisfied (eg. requires blocking or too much complexity).
308 * It would then be called again in ref-walk mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 */
Al Viro2830ba72011-06-20 19:16:29 -0400310int generic_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700312 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 /*
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530315 * Do the basic permission checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 */
Al Viro7e401452011-06-20 19:12:17 -0400317 ret = acl_permission_check(inode, mask);
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700318 if (ret != -EACCES)
319 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Al Virod594e7e2011-06-20 19:55:42 -0400321 if (S_ISDIR(inode->i_mode)) {
322 /* DACs are overridable for directories */
323 if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
324 return 0;
325 if (!(mask & MAY_WRITE))
326 if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
327 return 0;
328 return -EACCES;
329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 /*
331 * Read/write DACs are always overridable.
Al Virod594e7e2011-06-20 19:55:42 -0400332 * Executable DACs are overridable when there is
333 * at least one exec bit set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 */
Al Virod594e7e2011-06-20 19:55:42 -0400335 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
Serge E. Hallyne795b712011-03-23 16:43:25 -0700336 if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return 0;
338
339 /*
340 * Searching includes executable on directories, else just read.
341 */
Serge E. Hallyn7ea66002009-12-29 14:50:19 -0600342 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
Al Virod594e7e2011-06-20 19:55:42 -0400343 if (mask == MAY_READ)
Serge E. Hallyne795b712011-03-23 16:43:25 -0700344 if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return 0;
346
347 return -EACCES;
348}
349
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700350/*
351 * We _really_ want to just do "generic_permission()" without
352 * even looking at the inode->i_op values. So we keep a cache
353 * flag in inode->i_opflags, that says "this has not special
354 * permission function, use the fast case".
355 */
Daniel Rosenberg17285112016-10-26 16:27:45 -0700356static inline int do_inode_permission(struct vfsmount *mnt, struct inode *inode, int mask)
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700357{
358 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
Daniel Rosenberg17285112016-10-26 16:27:45 -0700359 if (likely(mnt && inode->i_op->permission2))
360 return inode->i_op->permission2(mnt, inode, mask);
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700361 if (likely(inode->i_op->permission))
362 return inode->i_op->permission(inode, mask);
363
364 /* This gets set once for the inode lifetime */
365 spin_lock(&inode->i_lock);
366 inode->i_opflags |= IOP_FASTPERM;
367 spin_unlock(&inode->i_lock);
368 }
369 return generic_permission(inode, mask);
370}
371
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200372/**
373 * inode_permission - check for access rights to a given inode
374 * @inode: inode to check permission on
Andreas Gruenbacher8fd90c82011-10-23 23:13:30 +0530375 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200376 *
377 * Used to check for read/write/execute permissions on an inode.
378 * We use "fsuid" for this, letting us set arbitrary permissions
379 * for filesystem access without changing the "normal" uids which
380 * are used for other things.
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530381 *
382 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200383 */
Daniel Rosenberg17285112016-10-26 16:27:45 -0700384int inode_permission2(struct vfsmount *mnt, struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Al Viroe6305c42008-07-15 21:03:57 -0400386 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700388 if (unlikely(mask & MAY_WRITE)) {
Miklos Szeredi22590e42007-10-16 23:27:08 -0700389 umode_t mode = inode->i_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 /*
392 * Nobody gets write access to a read-only fs.
393 */
394 if (IS_RDONLY(inode) &&
395 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
396 return -EROFS;
397
398 /*
399 * Nobody gets write access to an immutable file.
400 */
401 if (IS_IMMUTABLE(inode))
402 return -EACCES;
403 }
404
Daniel Rosenberg17285112016-10-26 16:27:45 -0700405 retval = do_inode_permission(mnt, inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 if (retval)
407 return retval;
408
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700409 retval = devcgroup_inode_permission(inode, mask);
410 if (retval)
411 return retval;
412
Eric Parisd09ca732010-07-23 11:43:57 -0400413 return security_inode_permission(inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
Daniel Rosenberg17285112016-10-26 16:27:45 -0700415EXPORT_SYMBOL(inode_permission2);
416
417int inode_permission(struct inode *inode, int mask)
418{
419 return inode_permission2(NULL, inode, mask);
420}
421EXPORT_SYMBOL(inode_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Al Virof4d6ff82011-06-19 13:14:21 -0400423/**
Jan Blunck5dd784d2008-02-14 19:34:38 -0800424 * path_get - get a reference to a path
425 * @path: path to get the reference to
426 *
427 * Given a path increment the reference count to the dentry and the vfsmount.
428 */
429void path_get(struct path *path)
430{
431 mntget(path->mnt);
432 dget(path->dentry);
433}
434EXPORT_SYMBOL(path_get);
435
436/**
Jan Blunck1d957f92008-02-14 19:34:35 -0800437 * path_put - put a reference to a path
438 * @path: path to put the reference to
439 *
440 * Given a path decrement the reference count to the dentry and the vfsmount.
441 */
442void path_put(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Jan Blunck1d957f92008-02-14 19:34:35 -0800444 dput(path->dentry);
445 mntput(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
Jan Blunck1d957f92008-02-14 19:34:35 -0800447EXPORT_SYMBOL(path_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Eric W. Biedermanbf2e9fa2015-08-15 20:27:13 -0500449/**
450 * path_connected - Verify that a path->dentry is below path->mnt.mnt_root
451 * @path: nameidate to verify
452 *
453 * Rename can sometimes move a file or directory outside of a bind
454 * mount, path_connected allows those cases to be detected.
455 */
456static bool path_connected(const struct path *path)
457{
458 struct vfsmount *mnt = path->mnt;
459
460 /* Only bind mounts can have disconnected paths */
461 if (mnt->mnt_root == mnt->mnt_sb->s_root)
462 return true;
463
464 return is_subdir(path->dentry, mnt->mnt_root);
465}
466
Al Viro19660af2011-03-25 10:32:48 -0400467/*
Nick Piggin31e6b012011-01-07 17:49:52 +1100468 * Path walking has 2 modes, rcu-walk and ref-walk (see
Al Viro19660af2011-03-25 10:32:48 -0400469 * Documentation/filesystems/path-lookup.txt). In situations when we can't
470 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
471 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
472 * mode. Refcounts are grabbed at the last known good point before rcu-walk
473 * got stuck, so ref-walk may continue from there. If this is not successful
474 * (eg. a seqcount has changed), then failure is returned and it's up to caller
475 * to restart the path walk from the beginning in ref-walk mode.
Nick Piggin31e6b012011-01-07 17:49:52 +1100476 */
Nick Piggin31e6b012011-01-07 17:49:52 +1100477
Al Viro9caac002012-07-18 20:43:19 +0400478static inline void lock_rcu_walk(void)
479{
480 br_read_lock(&vfsmount_lock);
481 rcu_read_lock();
482}
483
484static inline void unlock_rcu_walk(void)
485{
486 rcu_read_unlock();
487 br_read_unlock(&vfsmount_lock);
488}
489
Nick Piggin31e6b012011-01-07 17:49:52 +1100490/**
Al Viro19660af2011-03-25 10:32:48 -0400491 * unlazy_walk - try to switch to ref-walk mode.
492 * @nd: nameidata pathwalk data
493 * @dentry: child of nd->path.dentry or NULL
Randy Dunlap39191622011-01-08 19:36:21 -0800494 * Returns: 0 on success, -ECHILD on failure
Nick Piggin31e6b012011-01-07 17:49:52 +1100495 *
Al Viro19660af2011-03-25 10:32:48 -0400496 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
497 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
498 * @nd or NULL. Must be called from rcu-walk context.
Nick Piggin31e6b012011-01-07 17:49:52 +1100499 */
Al Viro19660af2011-03-25 10:32:48 -0400500static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
Nick Piggin31e6b012011-01-07 17:49:52 +1100501{
502 struct fs_struct *fs = current->fs;
503 struct dentry *parent = nd->path.dentry;
Al Viro5b6ca022011-03-09 23:04:47 -0500504 int want_root = 0;
Nick Piggin31e6b012011-01-07 17:49:52 +1100505
506 BUG_ON(!(nd->flags & LOOKUP_RCU));
Al Viro5b6ca022011-03-09 23:04:47 -0500507 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
508 want_root = 1;
Nick Piggin31e6b012011-01-07 17:49:52 +1100509 spin_lock(&fs->lock);
510 if (nd->root.mnt != fs->root.mnt ||
511 nd->root.dentry != fs->root.dentry)
512 goto err_root;
513 }
514 spin_lock(&parent->d_lock);
Al Viro19660af2011-03-25 10:32:48 -0400515 if (!dentry) {
516 if (!__d_rcu_to_refcount(parent, nd->seq))
517 goto err_parent;
518 BUG_ON(nd->inode != parent->d_inode);
519 } else {
Al Viro94c0d4e2011-07-12 21:40:23 -0400520 if (dentry->d_parent != parent)
521 goto err_parent;
Al Viro19660af2011-03-25 10:32:48 -0400522 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
523 if (!__d_rcu_to_refcount(dentry, nd->seq))
524 goto err_child;
525 /*
526 * If the sequence check on the child dentry passed, then
527 * the child has not been removed from its parent. This
528 * means the parent dentry must be valid and able to take
529 * a reference at this point.
530 */
531 BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
532 BUG_ON(!parent->d_count);
533 parent->d_count++;
534 spin_unlock(&dentry->d_lock);
535 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100536 spin_unlock(&parent->d_lock);
Al Viro5b6ca022011-03-09 23:04:47 -0500537 if (want_root) {
Nick Piggin31e6b012011-01-07 17:49:52 +1100538 path_get(&nd->root);
539 spin_unlock(&fs->lock);
540 }
541 mntget(nd->path.mnt);
542
Al Viro9caac002012-07-18 20:43:19 +0400543 unlock_rcu_walk();
Nick Piggin31e6b012011-01-07 17:49:52 +1100544 nd->flags &= ~LOOKUP_RCU;
545 return 0;
Al Viro19660af2011-03-25 10:32:48 -0400546
547err_child:
Nick Piggin31e6b012011-01-07 17:49:52 +1100548 spin_unlock(&dentry->d_lock);
Al Viro19660af2011-03-25 10:32:48 -0400549err_parent:
Nick Piggin31e6b012011-01-07 17:49:52 +1100550 spin_unlock(&parent->d_lock);
551err_root:
Al Viro5b6ca022011-03-09 23:04:47 -0500552 if (want_root)
Nick Piggin31e6b012011-01-07 17:49:52 +1100553 spin_unlock(&fs->lock);
554 return -ECHILD;
555}
556
Al Viro28ca3262012-06-10 16:10:59 -0400557static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
Trond Myklebust834f2a42005-10-18 14:20:16 -0700558{
Al Viro28ca3262012-06-10 16:10:59 -0400559 return dentry->d_op->d_revalidate(dentry, flags);
Nick Piggin34286d62011-01-07 17:49:57 +1100560}
561
Al Viro9f1fafe2011-03-25 11:00:12 -0400562/**
563 * complete_walk - successful completion of path walk
564 * @nd: pointer nameidata
Jeff Layton39159de2009-12-07 12:01:50 -0500565 *
Al Viro9f1fafe2011-03-25 11:00:12 -0400566 * If we had been in RCU mode, drop out of it and legitimize nd->path.
567 * Revalidate the final result, unless we'd already done that during
568 * the path walk or the filesystem doesn't ask for it. Return 0 on
569 * success, -error on failure. In case of failure caller does not
570 * need to drop nd->path.
Jeff Layton39159de2009-12-07 12:01:50 -0500571 */
Al Viro9f1fafe2011-03-25 11:00:12 -0400572static int complete_walk(struct nameidata *nd)
Jeff Layton39159de2009-12-07 12:01:50 -0500573{
Al Viro16c2cd72011-02-22 15:50:10 -0500574 struct dentry *dentry = nd->path.dentry;
Jeff Layton39159de2009-12-07 12:01:50 -0500575 int status;
Jeff Layton39159de2009-12-07 12:01:50 -0500576
Al Viro9f1fafe2011-03-25 11:00:12 -0400577 if (nd->flags & LOOKUP_RCU) {
578 nd->flags &= ~LOOKUP_RCU;
579 if (!(nd->flags & LOOKUP_ROOT))
580 nd->root.mnt = NULL;
581 spin_lock(&dentry->d_lock);
582 if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
583 spin_unlock(&dentry->d_lock);
Al Viro9caac002012-07-18 20:43:19 +0400584 unlock_rcu_walk();
Al Viro9f1fafe2011-03-25 11:00:12 -0400585 return -ECHILD;
586 }
587 BUG_ON(nd->inode != dentry->d_inode);
588 spin_unlock(&dentry->d_lock);
589 mntget(nd->path.mnt);
Al Viro9caac002012-07-18 20:43:19 +0400590 unlock_rcu_walk();
Al Viro9f1fafe2011-03-25 11:00:12 -0400591 }
592
Al Viro16c2cd72011-02-22 15:50:10 -0500593 if (likely(!(nd->flags & LOOKUP_JUMPED)))
Jeff Layton39159de2009-12-07 12:01:50 -0500594 return 0;
595
Al Viro16c2cd72011-02-22 15:50:10 -0500596 if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
597 return 0;
598
599 if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
600 return 0;
601
602 /* Note: we do not d_invalidate() */
Al Viro28ca3262012-06-10 16:10:59 -0400603 status = d_revalidate(dentry, nd->flags);
Jeff Layton39159de2009-12-07 12:01:50 -0500604 if (status > 0)
605 return 0;
606
Al Viro16c2cd72011-02-22 15:50:10 -0500607 if (!status)
Jeff Layton39159de2009-12-07 12:01:50 -0500608 status = -ESTALE;
Al Viro16c2cd72011-02-22 15:50:10 -0500609
Al Viro9f1fafe2011-03-25 11:00:12 -0400610 path_put(&nd->path);
Jeff Layton39159de2009-12-07 12:01:50 -0500611 return status;
612}
613
Al Viro2a737872009-04-07 11:49:53 -0400614static __always_inline void set_root(struct nameidata *nd)
615{
Al Viro589a49f2014-09-13 21:55:46 -0400616 get_fs_root(current->fs, &nd->root);
Al Viro2a737872009-04-07 11:49:53 -0400617}
618
Al Viro6de88d72009-08-09 01:41:57 +0400619static int link_path_walk(const char *, struct nameidata *);
620
Al Viro589a49f2014-09-13 21:55:46 -0400621static __always_inline unsigned set_root_rcu(struct nameidata *nd)
Nick Piggin31e6b012011-01-07 17:49:52 +1100622{
Al Viro589a49f2014-09-13 21:55:46 -0400623 struct fs_struct *fs = current->fs;
624 unsigned seq, res;
Nick Pigginc28cc362011-01-07 17:49:53 +1100625
Al Viro589a49f2014-09-13 21:55:46 -0400626 do {
627 seq = read_seqcount_begin(&fs->seq);
628 nd->root = fs->root;
629 res = __read_seqcount_begin(&nd->root.dentry->d_seq);
630 } while (read_seqcount_retry(&fs->seq, seq));
631 return res;
Nick Piggin31e6b012011-01-07 17:49:52 +1100632}
633
Arjan van de Venf1662352006-01-14 13:21:31 -0800634static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Nick Piggin31e6b012011-01-07 17:49:52 +1100636 int ret;
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 if (IS_ERR(link))
639 goto fail;
640
641 if (*link == '/') {
Al Viro92214262014-09-13 21:55:46 -0400642 if (!nd->root.mnt)
643 set_root(nd);
Jan Blunck1d957f92008-02-14 19:34:35 -0800644 path_put(&nd->path);
Al Viro2a737872009-04-07 11:49:53 -0400645 nd->path = nd->root;
646 path_get(&nd->root);
Al Viro16c2cd72011-02-22 15:50:10 -0500647 nd->flags |= LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100649 nd->inode = nd->path.dentry->d_inode;
Christoph Hellwigb4091d52008-11-05 15:07:21 +0100650
Nick Piggin31e6b012011-01-07 17:49:52 +1100651 ret = link_path_walk(link, nd);
652 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653fail:
Jan Blunck1d957f92008-02-14 19:34:35 -0800654 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return PTR_ERR(link);
656}
657
Jan Blunck1d957f92008-02-14 19:34:35 -0800658static void path_put_conditional(struct path *path, struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700659{
660 dput(path->dentry);
Jan Blunck4ac91372008-02-14 19:34:32 -0800661 if (path->mnt != nd->path.mnt)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700662 mntput(path->mnt);
663}
664
Nick Piggin7b9337a2011-01-14 08:42:43 +0000665static inline void path_to_nameidata(const struct path *path,
666 struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700667{
Nick Piggin31e6b012011-01-07 17:49:52 +1100668 if (!(nd->flags & LOOKUP_RCU)) {
669 dput(nd->path.dentry);
670 if (nd->path.mnt != path->mnt)
671 mntput(nd->path.mnt);
Huang Shijie9a229682010-04-02 17:37:13 +0800672 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100673 nd->path.mnt = path->mnt;
Jan Blunck4ac91372008-02-14 19:34:32 -0800674 nd->path.dentry = path->dentry;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700675}
676
Christoph Hellwigfac26022012-06-18 10:47:04 -0400677/*
678 * Helper to directly jump to a known parsed path from ->follow_link,
679 * caller must have taken a reference to path beforehand.
680 */
681void nd_jump_link(struct nameidata *nd, struct path *path)
682{
683 path_put(&nd->path);
684
685 nd->path = *path;
686 nd->inode = nd->path.dentry->d_inode;
687 nd->flags |= LOOKUP_JUMPED;
Christoph Hellwigfac26022012-06-18 10:47:04 -0400688}
689
Al Viro574197e2011-03-14 22:20:34 -0400690static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
691{
692 struct inode *inode = link->dentry->d_inode;
Al Viro2c4d7392012-06-10 04:15:17 -0400693 if (inode->i_op->put_link)
Al Viro574197e2011-03-14 22:20:34 -0400694 inode->i_op->put_link(link->dentry, nd, cookie);
695 path_put(link);
696}
697
Linus Torvaldsfcf25fb2012-10-26 10:05:07 -0700698int sysctl_protected_symlinks __read_mostly = 0;
699int sysctl_protected_hardlinks __read_mostly = 0;
Kees Cook5092e9c2012-07-25 17:29:07 -0700700
701/**
702 * may_follow_link - Check symlink following for unsafe situations
703 * @link: The path of the symlink
704 *
705 * In the case of the sysctl_protected_symlinks sysctl being enabled,
706 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
707 * in a sticky world-writable directory. This is to protect privileged
708 * processes from failing races against path names that may change out
709 * from under them by way of other users creating malicious symlinks.
710 * It will permit symlinks to be followed only when outside a sticky
711 * world-writable directory, or when the uid of the symlink and follower
712 * match, or when the directory owner matches the symlink's owner.
713 *
714 * Returns 0 if following the symlink is allowed, -ve on error.
715 */
716static inline int may_follow_link(struct path *link, struct nameidata *nd)
717{
718 const struct inode *inode;
719 const struct inode *parent;
720
721 if (!sysctl_protected_symlinks)
722 return 0;
723
724 /* Allowed if owner and follower match. */
725 inode = link->dentry->d_inode;
726 if (current_cred()->fsuid == inode->i_uid)
727 return 0;
728
729 /* Allowed if parent directory not sticky and world-writable. */
730 parent = nd->path.dentry->d_inode;
731 if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
732 return 0;
733
734 /* Allowed if parent directory and link owner match. */
735 if (parent->i_uid == inode->i_uid)
736 return 0;
737
Sasha Levind4def9b2012-10-04 19:56:40 -0400738 audit_log_link_denied("follow_link", link);
Kees Cook5092e9c2012-07-25 17:29:07 -0700739 path_put_conditional(link, nd);
740 path_put(&nd->path);
741 return -EACCES;
742}
743
744/**
745 * safe_hardlink_source - Check for safe hardlink conditions
746 * @inode: the source inode to hardlink from
747 *
748 * Return false if at least one of the following conditions:
749 * - inode is not a regular file
750 * - inode is setuid
751 * - inode is setgid and group-exec
752 * - access failure for read and write
753 *
754 * Otherwise returns true.
755 */
756static bool safe_hardlink_source(struct inode *inode)
757{
758 umode_t mode = inode->i_mode;
759
760 /* Special files should not get pinned to the filesystem. */
761 if (!S_ISREG(mode))
762 return false;
763
764 /* Setuid files should not get pinned to the filesystem. */
765 if (mode & S_ISUID)
766 return false;
767
768 /* Executable setgid files should not get pinned to the filesystem. */
769 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
770 return false;
771
772 /* Hardlinking to unreadable or unwritable sources is dangerous. */
773 if (inode_permission(inode, MAY_READ | MAY_WRITE))
774 return false;
775
776 return true;
777}
778
779/**
780 * may_linkat - Check permissions for creating a hardlink
781 * @link: the source to hardlink from
782 *
783 * Block hardlink when all of:
784 * - sysctl_protected_hardlinks enabled
785 * - fsuid does not match inode
786 * - hardlink source is unsafe (see safe_hardlink_source() above)
787 * - not CAP_FOWNER
788 *
789 * Returns 0 if successful, -ve on error.
790 */
791static int may_linkat(struct path *link)
792{
793 const struct cred *cred;
794 struct inode *inode;
795
796 if (!sysctl_protected_hardlinks)
797 return 0;
798
799 cred = current_cred();
800 inode = link->dentry->d_inode;
801
802 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
803 * otherwise, it must be a safe source.
804 */
805 if (cred->fsuid == inode->i_uid || safe_hardlink_source(inode) ||
806 capable(CAP_FOWNER))
807 return 0;
808
Kees Cookccdd0902012-07-25 17:29:08 -0700809 audit_log_link_denied("linkat", link);
Kees Cook5092e9c2012-07-25 17:29:07 -0700810 return -EPERM;
811}
812
Al Virodef4af32009-12-26 08:37:05 -0500813static __always_inline int
Al Viro574197e2011-03-14 22:20:34 -0400814follow_link(struct path *link, struct nameidata *nd, void **p)
Ian Kent051d3812006-03-27 01:14:53 -0800815{
Nick Piggin7b9337a2011-01-14 08:42:43 +0000816 struct dentry *dentry = link->dentry;
Al Viro2c4d7392012-06-10 04:15:17 -0400817 int error;
818 char *s;
Ian Kent051d3812006-03-27 01:14:53 -0800819
Al Viro844a3912011-02-15 00:38:26 -0500820 BUG_ON(nd->flags & LOOKUP_RCU);
821
Al Viro0e794582011-03-16 02:45:02 -0400822 if (link->mnt == nd->path.mnt)
823 mntget(link->mnt);
824
Al Viro2c4d7392012-06-10 04:15:17 -0400825 error = -ELOOP;
826 if (unlikely(current->total_link_count >= 40))
827 goto out_put_nd_path;
828
Al Viro574197e2011-03-14 22:20:34 -0400829 cond_resched();
830 current->total_link_count++;
831
Al Viro68ac1232012-03-15 08:21:57 -0400832 touch_atime(link);
Ian Kent051d3812006-03-27 01:14:53 -0800833 nd_set_link(nd, NULL);
834
Al Viro36f3b4f2011-02-22 21:24:38 -0500835 error = security_inode_follow_link(link->dentry, nd);
Al Viro2c4d7392012-06-10 04:15:17 -0400836 if (error)
837 goto out_put_nd_path;
Al Viro36f3b4f2011-02-22 21:24:38 -0500838
Al Viro86acdca12009-12-22 23:45:11 -0500839 nd->last_type = LAST_BIND;
Al Virodef4af32009-12-26 08:37:05 -0500840 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
841 error = PTR_ERR(*p);
Al Viro2c4d7392012-06-10 04:15:17 -0400842 if (IS_ERR(*p))
Christoph Hellwigda51d392012-06-18 10:47:03 -0400843 goto out_put_nd_path;
Al Viro2c4d7392012-06-10 04:15:17 -0400844
845 error = 0;
846 s = nd_get_link(nd);
847 if (s) {
848 error = __vfs_follow_link(nd, s);
Christoph Hellwigfac26022012-06-18 10:47:04 -0400849 if (unlikely(error))
850 put_link(nd, link, *p);
Ian Kent051d3812006-03-27 01:14:53 -0800851 }
Al Viro2c4d7392012-06-10 04:15:17 -0400852
853 return error;
854
855out_put_nd_path:
Arnd Bergmanne1031ba2012-10-11 13:20:00 +0000856 *p = NULL;
Al Viro2c4d7392012-06-10 04:15:17 -0400857 path_put(&nd->path);
Al Viro2c4d7392012-06-10 04:15:17 -0400858 path_put(link);
Ian Kent051d3812006-03-27 01:14:53 -0800859 return error;
860}
861
Nick Piggin31e6b012011-01-07 17:49:52 +1100862static int follow_up_rcu(struct path *path)
863{
Al Viro0714a532011-11-24 22:19:58 -0500864 struct mount *mnt = real_mount(path->mnt);
865 struct mount *parent;
Nick Piggin31e6b012011-01-07 17:49:52 +1100866 struct dentry *mountpoint;
867
Al Viro0714a532011-11-24 22:19:58 -0500868 parent = mnt->mnt_parent;
869 if (&parent->mnt == path->mnt)
Nick Piggin31e6b012011-01-07 17:49:52 +1100870 return 0;
Al Viroa73324d2011-11-24 22:25:07 -0500871 mountpoint = mnt->mnt_mountpoint;
Nick Piggin31e6b012011-01-07 17:49:52 +1100872 path->dentry = mountpoint;
Al Viro0714a532011-11-24 22:19:58 -0500873 path->mnt = &parent->mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +1100874 return 1;
875}
876
David Howells105a0cd2012-06-25 12:55:28 +0100877/*
878 * follow_up - Find the mountpoint of path's vfsmount
879 *
880 * Given a path, find the mountpoint of its source file system.
881 * Replace @path with the path of the mountpoint in the parent mount.
882 * Up is towards /.
883 *
884 * Return 1 if we went up a level and 0 if we were already at the
885 * root.
886 */
Al Virobab77eb2009-04-18 03:26:48 -0400887int follow_up(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
Al Viro0714a532011-11-24 22:19:58 -0500889 struct mount *mnt = real_mount(path->mnt);
890 struct mount *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 struct dentry *mountpoint;
Nick Piggin99b7db72010-08-18 04:37:39 +1000892
Andi Kleenf0769502012-05-08 13:32:02 +0930893 br_read_lock(&vfsmount_lock);
Al Viro0714a532011-11-24 22:19:58 -0500894 parent = mnt->mnt_parent;
Al Virodab53a72012-07-18 17:32:50 +0400895 if (parent == mnt) {
Andi Kleenf0769502012-05-08 13:32:02 +0930896 br_read_unlock(&vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 return 0;
898 }
Al Viro0714a532011-11-24 22:19:58 -0500899 mntget(&parent->mnt);
Al Viroa73324d2011-11-24 22:25:07 -0500900 mountpoint = dget(mnt->mnt_mountpoint);
Andi Kleenf0769502012-05-08 13:32:02 +0930901 br_read_unlock(&vfsmount_lock);
Al Virobab77eb2009-04-18 03:26:48 -0400902 dput(path->dentry);
903 path->dentry = mountpoint;
904 mntput(path->mnt);
Al Viro0714a532011-11-24 22:19:58 -0500905 path->mnt = &parent->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return 1;
907}
908
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100909/*
David Howells9875cf82011-01-14 18:45:21 +0000910 * Perform an automount
911 * - return -EISDIR to tell follow_managed() to stop and return the path we
912 * were called with.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 */
David Howells9875cf82011-01-14 18:45:21 +0000914static int follow_automount(struct path *path, unsigned flags,
915 bool *need_mntput)
Nick Piggin31e6b012011-01-07 17:49:52 +1100916{
David Howells9875cf82011-01-14 18:45:21 +0000917 struct vfsmount *mnt;
David Howellsea5b7782011-01-14 19:10:03 +0000918 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +1100919
David Howells9875cf82011-01-14 18:45:21 +0000920 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
921 return -EREMOTE;
Al Viro463ffb22005-06-06 13:36:05 -0700922
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200923 /* We don't want to mount if someone's just doing a stat -
924 * unless they're stat'ing a directory and appended a '/' to
925 * the name.
926 *
927 * We do, however, want to mount if someone wants to open or
928 * create a file of any type under the mountpoint, wants to
929 * traverse through the mountpoint or wants to open the
930 * mounted directory. Also, autofs may mark negative dentries
931 * as being automount points. These will need the attentions
932 * of the daemon to instantiate them before they can be used.
David Howells9875cf82011-01-14 18:45:21 +0000933 */
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200934 if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
Linus Torvaldsd94c1772011-09-26 17:44:55 -0700935 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200936 path->dentry->d_inode)
937 return -EISDIR;
938
David Howells9875cf82011-01-14 18:45:21 +0000939 current->total_link_count++;
940 if (current->total_link_count >= 40)
941 return -ELOOP;
942
943 mnt = path->dentry->d_op->d_automount(path);
944 if (IS_ERR(mnt)) {
945 /*
946 * The filesystem is allowed to return -EISDIR here to indicate
947 * it doesn't want to automount. For instance, autofs would do
948 * this so that its userspace daemon can mount on this dentry.
949 *
950 * However, we can only permit this if it's a terminal point in
951 * the path being looked up; if it wasn't then the remainder of
952 * the path is inaccessible and we should say so.
953 */
Al Viro49084c32011-06-25 21:59:52 -0400954 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
David Howells9875cf82011-01-14 18:45:21 +0000955 return -EREMOTE;
956 return PTR_ERR(mnt);
957 }
David Howellsea5b7782011-01-14 19:10:03 +0000958
David Howells9875cf82011-01-14 18:45:21 +0000959 if (!mnt) /* mount collision */
960 return 0;
961
Al Viro8aef1882011-06-16 15:10:06 +0100962 if (!*need_mntput) {
963 /* lock_mount() may release path->mnt on error */
964 mntget(path->mnt);
965 *need_mntput = true;
966 }
Al Viro19a167a2011-01-17 01:35:23 -0500967 err = finish_automount(mnt, path);
David Howellsea5b7782011-01-14 19:10:03 +0000968
David Howellsea5b7782011-01-14 19:10:03 +0000969 switch (err) {
970 case -EBUSY:
971 /* Someone else made a mount here whilst we were busy */
Al Viro19a167a2011-01-17 01:35:23 -0500972 return 0;
David Howellsea5b7782011-01-14 19:10:03 +0000973 case 0:
Al Viro8aef1882011-06-16 15:10:06 +0100974 path_put(path);
David Howellsea5b7782011-01-14 19:10:03 +0000975 path->mnt = mnt;
976 path->dentry = dget(mnt->mnt_root);
David Howellsea5b7782011-01-14 19:10:03 +0000977 return 0;
Al Viro19a167a2011-01-17 01:35:23 -0500978 default:
979 return err;
David Howellsea5b7782011-01-14 19:10:03 +0000980 }
Al Viro19a167a2011-01-17 01:35:23 -0500981
David Howells9875cf82011-01-14 18:45:21 +0000982}
983
984/*
985 * Handle a dentry that is managed in some way.
David Howellscc53ce52011-01-14 18:45:26 +0000986 * - Flagged for transit management (autofs)
David Howells9875cf82011-01-14 18:45:21 +0000987 * - Flagged as mountpoint
988 * - Flagged as automount point
989 *
990 * This may only be called in refwalk mode.
991 *
992 * Serialization is taken care of in namespace.c
993 */
994static int follow_managed(struct path *path, unsigned flags)
995{
Al Viro8aef1882011-06-16 15:10:06 +0100996 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
David Howells9875cf82011-01-14 18:45:21 +0000997 unsigned managed;
998 bool need_mntput = false;
Al Viro8aef1882011-06-16 15:10:06 +0100999 int ret = 0;
David Howells9875cf82011-01-14 18:45:21 +00001000
1001 /* Given that we're not holding a lock here, we retain the value in a
1002 * local variable for each dentry as we look at it so that we don't see
1003 * the components of that value change under us */
1004 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1005 managed &= DCACHE_MANAGED_DENTRY,
1006 unlikely(managed != 0)) {
David Howellscc53ce52011-01-14 18:45:26 +00001007 /* Allow the filesystem to manage the transit without i_mutex
1008 * being held. */
1009 if (managed & DCACHE_MANAGE_TRANSIT) {
1010 BUG_ON(!path->dentry->d_op);
1011 BUG_ON(!path->dentry->d_op->d_manage);
Al Viro1aed3e42011-03-18 09:09:02 -04001012 ret = path->dentry->d_op->d_manage(path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +00001013 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001014 break;
David Howellscc53ce52011-01-14 18:45:26 +00001015 }
1016
David Howells9875cf82011-01-14 18:45:21 +00001017 /* Transit to a mounted filesystem. */
1018 if (managed & DCACHE_MOUNTED) {
1019 struct vfsmount *mounted = lookup_mnt(path);
1020 if (mounted) {
1021 dput(path->dentry);
1022 if (need_mntput)
1023 mntput(path->mnt);
1024 path->mnt = mounted;
1025 path->dentry = dget(mounted->mnt_root);
1026 need_mntput = true;
1027 continue;
1028 }
1029
1030 /* Something is mounted on this dentry in another
1031 * namespace and/or whatever was mounted there in this
1032 * namespace got unmounted before we managed to get the
1033 * vfsmount_lock */
1034 }
1035
1036 /* Handle an automount point */
1037 if (managed & DCACHE_NEED_AUTOMOUNT) {
1038 ret = follow_automount(path, flags, &need_mntput);
1039 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001040 break;
David Howells9875cf82011-01-14 18:45:21 +00001041 continue;
1042 }
1043
1044 /* We didn't change the current path point */
1045 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 }
Al Viro8aef1882011-06-16 15:10:06 +01001047
1048 if (need_mntput && path->mnt == mnt)
1049 mntput(path->mnt);
1050 if (ret == -EISDIR)
1051 ret = 0;
Al Viroa3fbbde2011-11-07 21:21:26 +00001052 return ret < 0 ? ret : need_mntput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053}
1054
David Howellscc53ce52011-01-14 18:45:26 +00001055int follow_down_one(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
1057 struct vfsmount *mounted;
1058
Al Viro1c755af2009-04-18 14:06:57 -04001059 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -04001061 dput(path->dentry);
1062 mntput(path->mnt);
1063 path->mnt = mounted;
1064 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 return 1;
1066 }
1067 return 0;
1068}
1069
Ian Kent62a73752011-03-25 01:51:02 +08001070static inline bool managed_dentry_might_block(struct dentry *dentry)
1071{
1072 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
1073 dentry->d_op->d_manage(dentry, true) < 0);
1074}
1075
David Howells9875cf82011-01-14 18:45:21 +00001076/*
Al Viro287548e2011-05-27 06:50:06 -04001077 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1078 * we meet a managed dentry that would need blocking.
David Howells9875cf82011-01-14 18:45:21 +00001079 */
1080static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
Al Viro287548e2011-05-27 06:50:06 -04001081 struct inode **inode)
David Howells9875cf82011-01-14 18:45:21 +00001082{
Ian Kent62a73752011-03-25 01:51:02 +08001083 for (;;) {
Al Viroc7105362011-11-24 18:22:03 -05001084 struct mount *mounted;
Ian Kent62a73752011-03-25 01:51:02 +08001085 /*
1086 * Don't forget we might have a non-mountpoint managed dentry
1087 * that wants to block transit.
1088 */
Al Viro287548e2011-05-27 06:50:06 -04001089 if (unlikely(managed_dentry_might_block(path->dentry)))
David Howellsab909112011-01-14 18:46:51 +00001090 return false;
Ian Kent62a73752011-03-25 01:51:02 +08001091
1092 if (!d_mountpoint(path->dentry))
1093 break;
1094
David Howells9875cf82011-01-14 18:45:21 +00001095 mounted = __lookup_mnt(path->mnt, path->dentry, 1);
1096 if (!mounted)
1097 break;
Al Viroc7105362011-11-24 18:22:03 -05001098 path->mnt = &mounted->mnt;
1099 path->dentry = mounted->mnt.mnt_root;
Al Viroa3fbbde2011-11-07 21:21:26 +00001100 nd->flags |= LOOKUP_JUMPED;
David Howells9875cf82011-01-14 18:45:21 +00001101 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
Linus Torvalds59430262011-07-18 15:43:29 -07001102 /*
1103 * Update the inode too. We don't need to re-check the
1104 * dentry sequence number here after this d_inode read,
1105 * because a mount-point is always pinned.
1106 */
1107 *inode = path->dentry->d_inode;
David Howells9875cf82011-01-14 18:45:21 +00001108 }
David Howells9875cf82011-01-14 18:45:21 +00001109 return true;
1110}
1111
Al Virodea39372011-05-27 06:53:39 -04001112static void follow_mount_rcu(struct nameidata *nd)
Al Viro287548e2011-05-27 06:50:06 -04001113{
Al Virodea39372011-05-27 06:53:39 -04001114 while (d_mountpoint(nd->path.dentry)) {
Al Viroc7105362011-11-24 18:22:03 -05001115 struct mount *mounted;
Al Virodea39372011-05-27 06:53:39 -04001116 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
Al Viro287548e2011-05-27 06:50:06 -04001117 if (!mounted)
1118 break;
Al Viroc7105362011-11-24 18:22:03 -05001119 nd->path.mnt = &mounted->mnt;
1120 nd->path.dentry = mounted->mnt.mnt_root;
Al Virodea39372011-05-27 06:53:39 -04001121 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro287548e2011-05-27 06:50:06 -04001122 }
1123}
1124
Nick Piggin31e6b012011-01-07 17:49:52 +11001125static int follow_dotdot_rcu(struct nameidata *nd)
1126{
Al Viro589a49f2014-09-13 21:55:46 -04001127 if (!nd->root.mnt)
1128 set_root_rcu(nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11001129
David Howells9875cf82011-01-14 18:45:21 +00001130 while (1) {
Nick Piggin31e6b012011-01-07 17:49:52 +11001131 if (nd->path.dentry == nd->root.dentry &&
1132 nd->path.mnt == nd->root.mnt) {
1133 break;
1134 }
1135 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1136 struct dentry *old = nd->path.dentry;
1137 struct dentry *parent = old->d_parent;
1138 unsigned seq;
1139
1140 seq = read_seqcount_begin(&parent->d_seq);
1141 if (read_seqcount_retry(&old->d_seq, nd->seq))
Al Viroef7562d2011-03-04 14:35:59 -05001142 goto failed;
Nick Piggin31e6b012011-01-07 17:49:52 +11001143 nd->path.dentry = parent;
1144 nd->seq = seq;
Eric W. Biedermanbf2e9fa2015-08-15 20:27:13 -05001145 if (unlikely(!path_connected(&nd->path)))
1146 goto failed;
Nick Piggin31e6b012011-01-07 17:49:52 +11001147 break;
1148 }
1149 if (!follow_up_rcu(&nd->path))
1150 break;
1151 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Nick Piggin31e6b012011-01-07 17:49:52 +11001152 }
Al Virodea39372011-05-27 06:53:39 -04001153 follow_mount_rcu(nd);
1154 nd->inode = nd->path.dentry->d_inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001155 return 0;
Al Viroef7562d2011-03-04 14:35:59 -05001156
1157failed:
1158 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001159 if (!(nd->flags & LOOKUP_ROOT))
1160 nd->root.mnt = NULL;
Al Viro9caac002012-07-18 20:43:19 +04001161 unlock_rcu_walk();
Al Viroef7562d2011-03-04 14:35:59 -05001162 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001163}
1164
David Howells9875cf82011-01-14 18:45:21 +00001165/*
David Howellscc53ce52011-01-14 18:45:26 +00001166 * Follow down to the covering mount currently visible to userspace. At each
1167 * point, the filesystem owning that dentry may be queried as to whether the
1168 * caller is permitted to proceed or not.
David Howellscc53ce52011-01-14 18:45:26 +00001169 */
Al Viro7cc90cc2011-03-18 09:04:20 -04001170int follow_down(struct path *path)
David Howellscc53ce52011-01-14 18:45:26 +00001171{
1172 unsigned managed;
1173 int ret;
1174
1175 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1176 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1177 /* Allow the filesystem to manage the transit without i_mutex
1178 * being held.
1179 *
1180 * We indicate to the filesystem if someone is trying to mount
1181 * something here. This gives autofs the chance to deny anyone
1182 * other than its daemon the right to mount on its
1183 * superstructure.
1184 *
1185 * The filesystem may sleep at this point.
1186 */
1187 if (managed & DCACHE_MANAGE_TRANSIT) {
1188 BUG_ON(!path->dentry->d_op);
1189 BUG_ON(!path->dentry->d_op->d_manage);
David Howellsab909112011-01-14 18:46:51 +00001190 ret = path->dentry->d_op->d_manage(
Al Viro1aed3e42011-03-18 09:09:02 -04001191 path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +00001192 if (ret < 0)
1193 return ret == -EISDIR ? 0 : ret;
1194 }
1195
1196 /* Transit to a mounted filesystem. */
1197 if (managed & DCACHE_MOUNTED) {
1198 struct vfsmount *mounted = lookup_mnt(path);
1199 if (!mounted)
1200 break;
1201 dput(path->dentry);
1202 mntput(path->mnt);
1203 path->mnt = mounted;
1204 path->dentry = dget(mounted->mnt_root);
1205 continue;
1206 }
1207
1208 /* Don't handle automount points here */
1209 break;
1210 }
1211 return 0;
1212}
1213
1214/*
David Howells9875cf82011-01-14 18:45:21 +00001215 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1216 */
1217static void follow_mount(struct path *path)
1218{
1219 while (d_mountpoint(path->dentry)) {
1220 struct vfsmount *mounted = lookup_mnt(path);
1221 if (!mounted)
1222 break;
1223 dput(path->dentry);
1224 mntput(path->mnt);
1225 path->mnt = mounted;
1226 path->dentry = dget(mounted->mnt_root);
1227 }
1228}
1229
Eric W. Biedermanbf2e9fa2015-08-15 20:27:13 -05001230static int follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
Al Viro589a49f2014-09-13 21:55:46 -04001232 if (!nd->root.mnt)
1233 set_root(nd);
Andreas Mohre518ddb2006-09-29 02:01:22 -07001234
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 while(1) {
Jan Blunck4ac91372008-02-14 19:34:32 -08001236 struct dentry *old = nd->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Al Viro2a737872009-04-07 11:49:53 -04001238 if (nd->path.dentry == nd->root.dentry &&
1239 nd->path.mnt == nd->root.mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 break;
1241 }
Jan Blunck4ac91372008-02-14 19:34:32 -08001242 if (nd->path.dentry != nd->path.mnt->mnt_root) {
Al Viro3088dd72010-01-30 15:47:29 -05001243 /* rare case of legitimate dget_parent()... */
1244 nd->path.dentry = dget_parent(nd->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 dput(old);
Eric W. Biedermanbf2e9fa2015-08-15 20:27:13 -05001246 if (unlikely(!path_connected(&nd->path))) {
1247 path_put(&nd->path);
1248 return -ENOENT;
1249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 break;
1251 }
Al Viro3088dd72010-01-30 15:47:29 -05001252 if (!follow_up(&nd->path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 }
Al Viro79ed0222009-04-18 13:59:41 -04001255 follow_mount(&nd->path);
Nick Piggin31e6b012011-01-07 17:49:52 +11001256 nd->inode = nd->path.dentry->d_inode;
Eric W. Biedermanbf2e9fa2015-08-15 20:27:13 -05001257 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258}
1259
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260/*
Miklos Szeredibad61182012-03-26 12:54:24 +02001261 * This looks up the name in dcache, possibly revalidates the old dentry and
1262 * allocates a new one if not found or not valid. In the need_lookup argument
1263 * returns whether i_op->lookup is necessary.
1264 *
1265 * dir->d_inode->i_mutex must be held
Nick Pigginbaa03892010-08-18 04:37:31 +10001266 */
Miklos Szeredibad61182012-03-26 12:54:24 +02001267static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
Al Viro61b61da2012-06-22 12:42:10 +04001268 unsigned int flags, bool *need_lookup)
Nick Pigginbaa03892010-08-18 04:37:31 +10001269{
Nick Pigginbaa03892010-08-18 04:37:31 +10001270 struct dentry *dentry;
Miklos Szeredibad61182012-03-26 12:54:24 +02001271 int error;
Nick Pigginbaa03892010-08-18 04:37:31 +10001272
Miklos Szeredibad61182012-03-26 12:54:24 +02001273 *need_lookup = false;
1274 dentry = d_lookup(dir, name);
1275 if (dentry) {
1276 if (d_need_lookup(dentry)) {
1277 *need_lookup = true;
1278 } else if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
Al Viro61b61da2012-06-22 12:42:10 +04001279 error = d_revalidate(dentry, flags);
Miklos Szeredibad61182012-03-26 12:54:24 +02001280 if (unlikely(error <= 0)) {
1281 if (error < 0) {
1282 dput(dentry);
1283 return ERR_PTR(error);
1284 } else if (!d_invalidate(dentry)) {
1285 dput(dentry);
1286 dentry = NULL;
1287 }
1288 }
1289 }
1290 }
Nick Pigginbaa03892010-08-18 04:37:31 +10001291
Miklos Szeredibad61182012-03-26 12:54:24 +02001292 if (!dentry) {
1293 dentry = d_alloc(dir, name);
1294 if (unlikely(!dentry))
1295 return ERR_PTR(-ENOMEM);
Nick Pigginbaa03892010-08-18 04:37:31 +10001296
Miklos Szeredibad61182012-03-26 12:54:24 +02001297 *need_lookup = true;
Nick Pigginbaa03892010-08-18 04:37:31 +10001298 }
1299 return dentry;
1300}
1301
1302/*
Miklos Szeredibad61182012-03-26 12:54:24 +02001303 * Call i_op->lookup on the dentry. The dentry must be negative but may be
1304 * hashed if it was pouplated with DCACHE_NEED_LOOKUP.
1305 *
1306 * dir->d_inode->i_mutex must be held
Josef Bacik44396f42011-05-31 11:58:49 -04001307 */
Miklos Szeredibad61182012-03-26 12:54:24 +02001308static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
Al Viro292d3af2012-06-10 17:17:17 -04001309 unsigned int flags)
Josef Bacik44396f42011-05-31 11:58:49 -04001310{
Josef Bacik44396f42011-05-31 11:58:49 -04001311 struct dentry *old;
1312
1313 /* Don't create child dentry for a dead directory. */
Miklos Szeredibad61182012-03-26 12:54:24 +02001314 if (unlikely(IS_DEADDIR(dir))) {
Miklos Szeredie188dc02012-02-03 14:25:18 +01001315 dput(dentry);
Josef Bacik44396f42011-05-31 11:58:49 -04001316 return ERR_PTR(-ENOENT);
Miklos Szeredie188dc02012-02-03 14:25:18 +01001317 }
Josef Bacik44396f42011-05-31 11:58:49 -04001318
Al Viro292d3af2012-06-10 17:17:17 -04001319 old = dir->i_op->lookup(dir, dentry, flags);
Josef Bacik44396f42011-05-31 11:58:49 -04001320 if (unlikely(old)) {
1321 dput(dentry);
1322 dentry = old;
1323 }
1324 return dentry;
1325}
1326
Al Viroa3255542012-03-30 14:41:51 -04001327static struct dentry *__lookup_hash(struct qstr *name,
Al Viro292d3af2012-06-10 17:17:17 -04001328 struct dentry *base, unsigned int flags)
Al Viroa3255542012-03-30 14:41:51 -04001329{
Miklos Szeredibad61182012-03-26 12:54:24 +02001330 bool need_lookup;
Al Viroa3255542012-03-30 14:41:51 -04001331 struct dentry *dentry;
1332
Al Viro292d3af2012-06-10 17:17:17 -04001333 dentry = lookup_dcache(name, base, flags, &need_lookup);
Miklos Szeredibad61182012-03-26 12:54:24 +02001334 if (!need_lookup)
1335 return dentry;
Al Viroa3255542012-03-30 14:41:51 -04001336
Al Viro292d3af2012-06-10 17:17:17 -04001337 return lookup_real(base->d_inode, dentry, flags);
Al Viroa3255542012-03-30 14:41:51 -04001338}
1339
Josef Bacik44396f42011-05-31 11:58:49 -04001340/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 * It's more convoluted than I'd like it to be, but... it's still fairly
1342 * small and for now I'd prefer to have fast path as straight as possible.
1343 * It _is_ time-critical.
1344 */
Miklos Szeredidb77d762012-05-21 17:30:05 +02001345static int lookup_fast(struct nameidata *nd, struct qstr *name,
1346 struct path *path, struct inode **inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{
Jan Blunck4ac91372008-02-14 19:34:32 -08001348 struct vfsmount *mnt = nd->path.mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +11001349 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro5a18fff2011-03-11 04:44:53 -05001350 int need_reval = 1;
1351 int status = 1;
David Howells9875cf82011-01-14 18:45:21 +00001352 int err;
1353
Al Viro3cac2602009-08-13 18:27:43 +04001354 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001355 * Rename seqlock is not required here because in the off chance
1356 * of a false negative due to a concurrent rename, we're going to
1357 * do the non-racy lookup, below.
1358 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001359 if (nd->flags & LOOKUP_RCU) {
1360 unsigned seq;
Linus Torvaldsb7d3740d2012-05-04 14:59:14 -07001361 dentry = __d_lookup_rcu(parent, name, &seq, nd->inode);
Al Viro5a18fff2011-03-11 04:44:53 -05001362 if (!dentry)
1363 goto unlazy;
1364
Linus Torvaldsb7d3740d2012-05-04 14:59:14 -07001365 /*
1366 * This sequence count validates that the inode matches
1367 * the dentry name information from lookup.
1368 */
1369 *inode = dentry->d_inode;
1370 if (read_seqcount_retry(&dentry->d_seq, seq))
1371 return -ECHILD;
1372
1373 /*
1374 * This sequence count validates that the parent had no
1375 * changes while we did the lookup of the dentry above.
1376 *
1377 * The memory barrier in read_seqcount_begin of child is
1378 * enough, we can use __read_seqcount_retry here.
1379 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001380 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1381 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001382 nd->seq = seq;
Al Viro5a18fff2011-03-11 04:44:53 -05001383
Miklos Szeredifa4ee152012-03-26 12:54:19 +02001384 if (unlikely(d_need_lookup(dentry)))
1385 goto unlazy;
Al Viro24643082011-02-15 01:26:22 -05001386 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
Al Viro28ca3262012-06-10 16:10:59 -04001387 status = d_revalidate(dentry, nd->flags);
Al Viro5a18fff2011-03-11 04:44:53 -05001388 if (unlikely(status <= 0)) {
1389 if (status != -ECHILD)
1390 need_reval = 0;
1391 goto unlazy;
1392 }
Al Viro24643082011-02-15 01:26:22 -05001393 }
Nick Piggin31e6b012011-01-07 17:49:52 +11001394 path->mnt = mnt;
1395 path->dentry = dentry;
Al Virod6e9bd22011-05-27 07:03:15 -04001396 if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1397 goto unlazy;
1398 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1399 goto unlazy;
1400 return 0;
Al Viro5a18fff2011-03-11 04:44:53 -05001401unlazy:
Al Viro19660af2011-03-25 10:32:48 -04001402 if (unlazy_walk(nd, dentry))
1403 return -ECHILD;
Al Viro5a18fff2011-03-11 04:44:53 -05001404 } else {
1405 dentry = __d_lookup(parent, name);
Nick Piggin31e6b012011-01-07 17:49:52 +11001406 }
Al Viro5a18fff2011-03-11 04:44:53 -05001407
Al Viro81e6f522012-03-30 14:48:04 -04001408 if (unlikely(!dentry))
1409 goto need_lookup;
Al Viro5a18fff2011-03-11 04:44:53 -05001410
Al Viro81e6f522012-03-30 14:48:04 -04001411 if (unlikely(d_need_lookup(dentry))) {
1412 dput(dentry);
1413 goto need_lookup;
Al Viro24643082011-02-15 01:26:22 -05001414 }
Al Viro81e6f522012-03-30 14:48:04 -04001415
Al Viro5a18fff2011-03-11 04:44:53 -05001416 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
Al Viro28ca3262012-06-10 16:10:59 -04001417 status = d_revalidate(dentry, nd->flags);
Al Viro5a18fff2011-03-11 04:44:53 -05001418 if (unlikely(status <= 0)) {
1419 if (status < 0) {
1420 dput(dentry);
1421 return status;
1422 }
1423 if (!d_invalidate(dentry)) {
1424 dput(dentry);
Al Viro81e6f522012-03-30 14:48:04 -04001425 goto need_lookup;
Al Viro5a18fff2011-03-11 04:44:53 -05001426 }
1427 }
Miklos Szeredidb77d762012-05-21 17:30:05 +02001428
David Howells9875cf82011-01-14 18:45:21 +00001429 path->mnt = mnt;
1430 path->dentry = dentry;
1431 err = follow_managed(path, nd->flags);
Ian Kent89312212011-01-18 12:06:10 +08001432 if (unlikely(err < 0)) {
1433 path_put_conditional(path, nd);
David Howells9875cf82011-01-14 18:45:21 +00001434 return err;
Ian Kent89312212011-01-18 12:06:10 +08001435 }
Al Viroa3fbbde2011-11-07 21:21:26 +00001436 if (err)
1437 nd->flags |= LOOKUP_JUMPED;
David Howells9875cf82011-01-14 18:45:21 +00001438 *inode = path->dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 return 0;
Al Viro81e6f522012-03-30 14:48:04 -04001440
1441need_lookup:
Miklos Szeredidb77d762012-05-21 17:30:05 +02001442 return 1;
1443}
1444
1445/* Fast lookup failed, do it the slow way */
1446static int lookup_slow(struct nameidata *nd, struct qstr *name,
1447 struct path *path)
1448{
1449 struct dentry *dentry, *parent;
1450 int err;
1451
1452 parent = nd->path.dentry;
Al Viro81e6f522012-03-30 14:48:04 -04001453 BUG_ON(nd->inode != parent->d_inode);
1454
1455 mutex_lock(&parent->d_inode->i_mutex);
Al Viro292d3af2012-06-10 17:17:17 -04001456 dentry = __lookup_hash(name, parent, nd->flags);
Al Viro81e6f522012-03-30 14:48:04 -04001457 mutex_unlock(&parent->d_inode->i_mutex);
1458 if (IS_ERR(dentry))
1459 return PTR_ERR(dentry);
Miklos Szeredidb77d762012-05-21 17:30:05 +02001460 path->mnt = nd->path.mnt;
1461 path->dentry = dentry;
1462 err = follow_managed(path, nd->flags);
1463 if (unlikely(err < 0)) {
1464 path_put_conditional(path, nd);
1465 return err;
1466 }
1467 if (err)
1468 nd->flags |= LOOKUP_JUMPED;
1469 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470}
1471
Al Viro52094c82011-02-21 21:34:47 -05001472static inline int may_lookup(struct nameidata *nd)
1473{
1474 if (nd->flags & LOOKUP_RCU) {
Daniel Rosenberg17285112016-10-26 16:27:45 -07001475 int err = inode_permission2(nd->path.mnt, nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
Al Viro52094c82011-02-21 21:34:47 -05001476 if (err != -ECHILD)
1477 return err;
Al Viro19660af2011-03-25 10:32:48 -04001478 if (unlazy_walk(nd, NULL))
Al Viro52094c82011-02-21 21:34:47 -05001479 return -ECHILD;
1480 }
Daniel Rosenberg17285112016-10-26 16:27:45 -07001481 return inode_permission2(nd->path.mnt, nd->inode, MAY_EXEC);
Al Viro52094c82011-02-21 21:34:47 -05001482}
1483
Al Viro9856fa12011-03-04 14:22:06 -05001484static inline int handle_dots(struct nameidata *nd, int type)
1485{
1486 if (type == LAST_DOTDOT) {
1487 if (nd->flags & LOOKUP_RCU) {
1488 if (follow_dotdot_rcu(nd))
1489 return -ECHILD;
1490 } else
Eric W. Biedermanbf2e9fa2015-08-15 20:27:13 -05001491 return follow_dotdot(nd);
Al Viro9856fa12011-03-04 14:22:06 -05001492 }
1493 return 0;
1494}
1495
Al Viro951361f2011-03-04 14:44:37 -05001496static void terminate_walk(struct nameidata *nd)
1497{
1498 if (!(nd->flags & LOOKUP_RCU)) {
1499 path_put(&nd->path);
1500 } else {
1501 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001502 if (!(nd->flags & LOOKUP_ROOT))
1503 nd->root.mnt = NULL;
Al Viro9caac002012-07-18 20:43:19 +04001504 unlock_rcu_walk();
Al Viro951361f2011-03-04 14:44:37 -05001505 }
1506}
1507
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001508/*
1509 * Do we need to follow links? We _really_ want to be able
1510 * to do this check without having to look at inode->i_op,
1511 * so we keep a cache of "no, this doesn't need follow_link"
1512 * for the common case.
1513 */
Linus Torvalds7813b942011-08-07 09:53:20 -07001514static inline int should_follow_link(struct inode *inode, int follow)
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001515{
1516 if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
1517 if (likely(inode->i_op->follow_link))
1518 return follow;
1519
1520 /* This gets set once for the inode lifetime */
1521 spin_lock(&inode->i_lock);
1522 inode->i_opflags |= IOP_NOFOLLOW;
1523 spin_unlock(&inode->i_lock);
1524 }
1525 return 0;
1526}
1527
Al Viroce57dfc2011-03-13 19:58:58 -04001528static inline int walk_component(struct nameidata *nd, struct path *path,
1529 struct qstr *name, int type, int follow)
1530{
1531 struct inode *inode;
1532 int err;
1533 /*
1534 * "." and ".." are special - ".." especially so because it has
1535 * to be able to know about the current root directory and
1536 * parent relationships.
1537 */
1538 if (unlikely(type != LAST_NORM))
1539 return handle_dots(nd, type);
Miklos Szeredidb77d762012-05-21 17:30:05 +02001540 err = lookup_fast(nd, name, path, &inode);
Al Viroce57dfc2011-03-13 19:58:58 -04001541 if (unlikely(err)) {
Miklos Szeredidb77d762012-05-21 17:30:05 +02001542 if (err < 0)
1543 goto out_err;
1544
1545 err = lookup_slow(nd, name, path);
1546 if (err < 0)
1547 goto out_err;
1548
1549 inode = path->dentry->d_inode;
Al Viroce57dfc2011-03-13 19:58:58 -04001550 }
Miklos Szeredidb77d762012-05-21 17:30:05 +02001551 err = -ENOENT;
1552 if (!inode)
1553 goto out_path_put;
1554
Linus Torvalds7813b942011-08-07 09:53:20 -07001555 if (should_follow_link(inode, follow)) {
Al Viro19660af2011-03-25 10:32:48 -04001556 if (nd->flags & LOOKUP_RCU) {
Al Viroa377bbd2015-04-24 15:47:07 -04001557 if (unlikely(nd->path.mnt != path->mnt ||
1558 unlazy_walk(nd, path->dentry))) {
Miklos Szeredidb77d762012-05-21 17:30:05 +02001559 err = -ECHILD;
1560 goto out_err;
Al Viro19660af2011-03-25 10:32:48 -04001561 }
1562 }
Al Viroce57dfc2011-03-13 19:58:58 -04001563 BUG_ON(inode != path->dentry->d_inode);
1564 return 1;
1565 }
1566 path_to_nameidata(path, nd);
1567 nd->inode = inode;
1568 return 0;
Miklos Szeredidb77d762012-05-21 17:30:05 +02001569
1570out_path_put:
1571 path_to_nameidata(path, nd);
1572out_err:
1573 terminate_walk(nd);
1574 return err;
Al Viroce57dfc2011-03-13 19:58:58 -04001575}
1576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577/*
Al Virob3563792011-03-14 21:54:55 -04001578 * This limits recursive symlink follows to 8, while
1579 * limiting consecutive symlinks to 40.
1580 *
1581 * Without that kind of total limit, nasty chains of consecutive
1582 * symlinks can cause almost arbitrarily long lookups.
1583 */
1584static inline int nested_symlink(struct path *path, struct nameidata *nd)
1585{
1586 int res;
1587
Al Virob3563792011-03-14 21:54:55 -04001588 if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1589 path_put_conditional(path, nd);
1590 path_put(&nd->path);
1591 return -ELOOP;
1592 }
Erez Zadok1a4022f2011-05-21 01:19:59 -04001593 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
Al Virob3563792011-03-14 21:54:55 -04001594
1595 nd->depth++;
1596 current->link_count++;
1597
1598 do {
1599 struct path link = *path;
1600 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04001601
1602 res = follow_link(&link, nd, &cookie);
Al Viro2c4d7392012-06-10 04:15:17 -04001603 if (res)
1604 break;
1605 res = walk_component(nd, path, &nd->last,
1606 nd->last_type, LOOKUP_FOLLOW);
Al Viro574197e2011-03-14 22:20:34 -04001607 put_link(nd, &link, cookie);
Al Virob3563792011-03-14 21:54:55 -04001608 } while (res > 0);
1609
1610 current->link_count--;
1611 nd->depth--;
1612 return res;
1613}
1614
1615/*
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001616 * We really don't want to look at inode->i_op->lookup
1617 * when we don't have to. So we keep a cache bit in
1618 * the inode ->i_opflags field that says "yes, we can
1619 * do lookup on this inode".
1620 */
1621static inline int can_lookup(struct inode *inode)
1622{
1623 if (likely(inode->i_opflags & IOP_LOOKUP))
1624 return 1;
1625 if (likely(!inode->i_op->lookup))
1626 return 0;
1627
1628 /* We do this once for the lifetime of the inode */
1629 spin_lock(&inode->i_lock);
1630 inode->i_opflags |= IOP_LOOKUP;
1631 spin_unlock(&inode->i_lock);
1632 return 1;
1633}
1634
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001635/*
1636 * We can do the critical dentry name comparison and hashing
1637 * operations one word at a time, but we are limited to:
1638 *
1639 * - Architectures with fast unaligned word accesses. We could
1640 * do a "get_unaligned()" if this helps and is sufficiently
1641 * fast.
1642 *
1643 * - Little-endian machines (so that we can generate the mask
1644 * of low bytes efficiently). Again, we *could* do a byte
1645 * swapping load on big-endian architectures if that is not
1646 * expensive enough to make the optimization worthless.
1647 *
1648 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1649 * do not trap on the (extremely unlikely) case of a page
1650 * crossing operation.
1651 *
1652 * - Furthermore, we need an efficient 64-bit compile for the
1653 * 64-bit case in order to generate the "number of bytes in
1654 * the final mask". Again, that could be replaced with a
1655 * efficient population count instruction or similar.
1656 */
1657#ifdef CONFIG_DCACHE_WORD_ACCESS
1658
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001659#include <asm/word-at-a-time.h>
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001660
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001661#ifdef CONFIG_64BIT
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001662
1663static inline unsigned int fold_hash(unsigned long hash)
1664{
1665 hash += hash >> (8*sizeof(int));
1666 return hash;
1667}
1668
1669#else /* 32-bit case */
1670
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001671#define fold_hash(x) (x)
1672
1673#endif
1674
1675unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1676{
1677 unsigned long a, mask;
1678 unsigned long hash = 0;
1679
1680 for (;;) {
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001681 a = load_unaligned_zeropad(name);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001682 if (len < sizeof(unsigned long))
1683 break;
1684 hash += a;
Al Virof132c5b2012-03-22 21:59:52 +00001685 hash *= 9;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001686 name += sizeof(unsigned long);
1687 len -= sizeof(unsigned long);
1688 if (!len)
1689 goto done;
1690 }
1691 mask = ~(~0ul << len*8);
1692 hash += mask & a;
1693done:
1694 return fold_hash(hash);
1695}
1696EXPORT_SYMBOL(full_name_hash);
1697
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001698/*
1699 * Calculate the length and hash of the path component, and
1700 * return the length of the component;
1701 */
1702static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1703{
1704 unsigned long a, mask, hash, len;
1705
1706 hash = a = 0;
1707 len = -sizeof(unsigned long);
1708 do {
1709 hash = (hash + a) * 9;
1710 len += sizeof(unsigned long);
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001711 a = load_unaligned_zeropad(name+len);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001712 /* Do we have any NUL or '/' bytes in this word? */
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001713 mask = has_zero(a) | has_zero(a ^ REPEAT_BYTE('/'));
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001714 } while (!mask);
1715
1716 /* The mask *below* the first high bit set */
1717 mask = (mask - 1) & ~mask;
1718 mask >>= 7;
1719 hash += a & mask;
1720 *hashp = fold_hash(hash);
1721
1722 return len + count_masked_bytes(mask);
1723}
1724
1725#else
1726
Linus Torvalds0145acc2012-03-02 14:32:59 -08001727unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1728{
1729 unsigned long hash = init_name_hash();
1730 while (len--)
1731 hash = partial_name_hash(*name++, hash);
1732 return end_name_hash(hash);
1733}
Linus Torvaldsae942ae2012-03-02 19:40:57 -08001734EXPORT_SYMBOL(full_name_hash);
Linus Torvalds0145acc2012-03-02 14:32:59 -08001735
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001736/*
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001737 * We know there's a real path component here of at least
1738 * one character.
1739 */
1740static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1741{
1742 unsigned long hash = init_name_hash();
1743 unsigned long len = 0, c;
1744
1745 c = (unsigned char)*name;
1746 do {
1747 len++;
1748 hash = partial_name_hash(c, hash);
1749 c = (unsigned char)name[len];
1750 } while (c && c != '/');
1751 *hashp = end_name_hash(hash);
1752 return len;
1753}
1754
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001755#endif
1756
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001757/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01001759 * This is the basic name resolution function, turning a pathname into
1760 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01001762 * Returns 0 and nd will have valid dentry and mnt on success.
1763 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 */
Al Viro6de88d72009-08-09 01:41:57 +04001765static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766{
1767 struct path next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
1770 while (*name=='/')
1771 name++;
1772 if (!*name)
Al Viro086e1832011-02-22 20:56:27 -05001773 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 /* At this point we know we have a real path component. */
1776 for(;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 struct qstr this;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001778 long len;
Al Virofe479a52011-02-22 15:10:03 -05001779 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
Al Viro52094c82011-02-21 21:34:47 -05001781 err = may_lookup(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 if (err)
1783 break;
1784
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001785 len = hash_name(name, &this.hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 this.name = name;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001787 this.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Al Virofe479a52011-02-22 15:10:03 -05001789 type = LAST_NORM;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001790 if (name[0] == '.') switch (len) {
Al Virofe479a52011-02-22 15:10:03 -05001791 case 2:
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001792 if (name[1] == '.') {
Al Virofe479a52011-02-22 15:10:03 -05001793 type = LAST_DOTDOT;
Al Viro16c2cd72011-02-22 15:50:10 -05001794 nd->flags |= LOOKUP_JUMPED;
1795 }
Al Virofe479a52011-02-22 15:10:03 -05001796 break;
1797 case 1:
1798 type = LAST_DOT;
1799 }
Al Viro5a202bc2011-03-08 14:17:44 -05001800 if (likely(type == LAST_NORM)) {
1801 struct dentry *parent = nd->path.dentry;
Al Viro16c2cd72011-02-22 15:50:10 -05001802 nd->flags &= ~LOOKUP_JUMPED;
Al Viro5a202bc2011-03-08 14:17:44 -05001803 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1804 err = parent->d_op->d_hash(parent, nd->inode,
1805 &this);
1806 if (err < 0)
1807 break;
1808 }
1809 }
Al Virofe479a52011-02-22 15:10:03 -05001810
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001811 if (!name[len])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 goto last_component;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001813 /*
1814 * If it wasn't NUL, we know it was '/'. Skip that
1815 * slash, and continue until no more slashes.
1816 */
1817 do {
1818 len++;
1819 } while (unlikely(name[len] == '/'));
1820 if (!name[len])
Al Virob3563792011-03-14 21:54:55 -04001821 goto last_component;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001822 name += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
Al Viroce57dfc2011-03-13 19:58:58 -04001824 err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1825 if (err < 0)
1826 return err;
Al Virofe479a52011-02-22 15:10:03 -05001827
Al Viroce57dfc2011-03-13 19:58:58 -04001828 if (err) {
Al Virob3563792011-03-14 21:54:55 -04001829 err = nested_symlink(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 if (err)
Al Viroa7472ba2011-03-04 14:39:30 -05001831 return err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001832 }
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001833 if (can_lookup(nd->inode))
1834 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 err = -ENOTDIR;
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001836 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 /* here ends the main loop */
1838
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839last_component:
Al Virob3563792011-03-14 21:54:55 -04001840 nd->last = this;
1841 nd->last_type = type;
Al Viro086e1832011-02-22 20:56:27 -05001842 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 }
Al Viro951361f2011-03-04 14:44:37 -05001844 terminate_walk(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 return err;
1846}
1847
Al Viro70e9b352011-03-05 21:12:22 -05001848static int path_init(int dfd, const char *name, unsigned int flags,
1849 struct nameidata *nd, struct file **fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850{
Prasanna Medaea3834d2005-04-29 16:00:17 +01001851 int retval = 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001852 int fput_needed;
1853 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
1855 nd->last_type = LAST_ROOT; /* if there are only slashes... */
Al Viro16c2cd72011-02-22 15:50:10 -05001856 nd->flags = flags | LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 nd->depth = 0;
Al Viro5b6ca022011-03-09 23:04:47 -05001858 if (flags & LOOKUP_ROOT) {
1859 struct inode *inode = nd->root.dentry->d_inode;
Daniel Rosenberg17285112016-10-26 16:27:45 -07001860 struct vfsmount *mnt = nd->root.mnt;
Al Viro73d049a2011-03-11 12:08:24 -05001861 if (*name) {
1862 if (!inode->i_op->lookup)
1863 return -ENOTDIR;
Daniel Rosenberg17285112016-10-26 16:27:45 -07001864 retval = inode_permission2(mnt, inode, MAY_EXEC);
Al Viro73d049a2011-03-11 12:08:24 -05001865 if (retval)
1866 return retval;
1867 }
Al Viro5b6ca022011-03-09 23:04:47 -05001868 nd->path = nd->root;
1869 nd->inode = inode;
1870 if (flags & LOOKUP_RCU) {
Al Viro9caac002012-07-18 20:43:19 +04001871 lock_rcu_walk();
Al Viro5b6ca022011-03-09 23:04:47 -05001872 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1873 } else {
1874 path_get(&nd->path);
1875 }
1876 return 0;
1877 }
1878
Al Viro2a737872009-04-07 11:49:53 -04001879 nd->root.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 if (*name=='/') {
Al Viroe41f7d42011-02-22 14:02:58 -05001882 if (flags & LOOKUP_RCU) {
Al Viro9caac002012-07-18 20:43:19 +04001883 lock_rcu_walk();
Al Viro589a49f2014-09-13 21:55:46 -04001884 nd->seq = set_root_rcu(nd);
Al Viroe41f7d42011-02-22 14:02:58 -05001885 } else {
1886 set_root(nd);
1887 path_get(&nd->root);
1888 }
Al Viro2a737872009-04-07 11:49:53 -04001889 nd->path = nd->root;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001890 } else if (dfd == AT_FDCWD) {
Al Viroe41f7d42011-02-22 14:02:58 -05001891 if (flags & LOOKUP_RCU) {
1892 struct fs_struct *fs = current->fs;
1893 unsigned seq;
1894
Al Viro9caac002012-07-18 20:43:19 +04001895 lock_rcu_walk();
Al Viroe41f7d42011-02-22 14:02:58 -05001896
1897 do {
1898 seq = read_seqcount_begin(&fs->seq);
1899 nd->path = fs->pwd;
1900 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1901 } while (read_seqcount_retry(&fs->seq, seq));
1902 } else {
1903 get_fs_pwd(current->fs, &nd->path);
1904 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001905 } else {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001906 struct dentry *dentry;
Daniel Rosenberg17285112016-10-26 16:27:45 -07001907 struct vfsmount *mnt;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001908
Al Viro1abf0c72011-03-13 03:51:11 -04001909 file = fget_raw_light(dfd, &fput_needed);
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001910 retval = -EBADF;
1911 if (!file)
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001912 goto out_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001913
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001914 dentry = file->f_path.dentry;
Daniel Rosenberg17285112016-10-26 16:27:45 -07001915 mnt = file->f_path.mnt;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001916
Al Virof52e0c12011-03-14 18:56:51 -04001917 if (*name) {
1918 retval = -ENOTDIR;
1919 if (!S_ISDIR(dentry->d_inode->i_mode))
1920 goto fput_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001921
Daniel Rosenberg17285112016-10-26 16:27:45 -07001922 retval = inode_permission2(mnt, dentry->d_inode, MAY_EXEC);
Al Virof52e0c12011-03-14 18:56:51 -04001923 if (retval)
1924 goto fput_fail;
1925 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001926
Jan Blunck5dd784d2008-02-14 19:34:38 -08001927 nd->path = file->f_path;
Al Viroe41f7d42011-02-22 14:02:58 -05001928 if (flags & LOOKUP_RCU) {
1929 if (fput_needed)
Al Viro70e9b352011-03-05 21:12:22 -05001930 *fp = file;
Al Viroe41f7d42011-02-22 14:02:58 -05001931 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro9caac002012-07-18 20:43:19 +04001932 lock_rcu_walk();
Al Viroe41f7d42011-02-22 14:02:58 -05001933 } else {
1934 path_get(&file->f_path);
1935 fput_light(file, fput_needed);
1936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 }
Al Viroe41f7d42011-02-22 14:02:58 -05001938
Nick Piggin31e6b012011-01-07 17:49:52 +11001939 nd->inode = nd->path.dentry->d_inode;
Al Viro9b4a9b12009-04-07 11:44:16 -04001940 return 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001941
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001942fput_fail:
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001943 fput_light(file, fput_needed);
Al Viro9b4a9b12009-04-07 11:44:16 -04001944out_fail:
1945 return retval;
1946}
1947
Al Virobd92d7f2011-03-14 19:54:59 -04001948static inline int lookup_last(struct nameidata *nd, struct path *path)
1949{
1950 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1951 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1952
1953 nd->flags &= ~LOOKUP_PARENT;
1954 return walk_component(nd, path, &nd->last, nd->last_type,
1955 nd->flags & LOOKUP_FOLLOW);
1956}
1957
Al Viro9b4a9b12009-04-07 11:44:16 -04001958/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroee0827c2011-02-21 23:38:09 -05001959static int path_lookupat(int dfd, const char *name,
Al Viro9b4a9b12009-04-07 11:44:16 -04001960 unsigned int flags, struct nameidata *nd)
1961{
Al Viro70e9b352011-03-05 21:12:22 -05001962 struct file *base = NULL;
Al Virobd92d7f2011-03-14 19:54:59 -04001963 struct path path;
1964 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001965
1966 /*
1967 * Path walking is largely split up into 2 different synchronisation
1968 * schemes, rcu-walk and ref-walk (explained in
1969 * Documentation/filesystems/path-lookup.txt). These share much of the
1970 * path walk code, but some things particularly setup, cleanup, and
1971 * following mounts are sufficiently divergent that functions are
1972 * duplicated. Typically there is a function foo(), and its RCU
1973 * analogue, foo_rcu().
1974 *
1975 * -ECHILD is the error number of choice (just to avoid clashes) that
1976 * is returned if some aspect of an rcu-walk fails. Such an error must
1977 * be handled by restarting a traditional ref-walk (which will always
1978 * be able to complete).
1979 */
Al Virobd92d7f2011-03-14 19:54:59 -04001980 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
Al Viroee0827c2011-02-21 23:38:09 -05001981
Al Virobd92d7f2011-03-14 19:54:59 -04001982 if (unlikely(err))
1983 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001984
1985 current->total_link_count = 0;
Al Virobd92d7f2011-03-14 19:54:59 -04001986 err = link_path_walk(name, nd);
1987
1988 if (!err && !(flags & LOOKUP_PARENT)) {
Al Virobd92d7f2011-03-14 19:54:59 -04001989 err = lookup_last(nd, &path);
1990 while (err > 0) {
1991 void *cookie;
1992 struct path link = path;
Kees Cook5092e9c2012-07-25 17:29:07 -07001993 err = may_follow_link(&link, nd);
1994 if (unlikely(err))
1995 break;
Al Virobd92d7f2011-03-14 19:54:59 -04001996 nd->flags |= LOOKUP_PARENT;
Al Viro574197e2011-03-14 22:20:34 -04001997 err = follow_link(&link, nd, &cookie);
Al Viro2c4d7392012-06-10 04:15:17 -04001998 if (err)
1999 break;
2000 err = lookup_last(nd, &path);
Al Viro574197e2011-03-14 22:20:34 -04002001 put_link(nd, &link, cookie);
Al Virobd92d7f2011-03-14 19:54:59 -04002002 }
2003 }
Al Viroee0827c2011-02-21 23:38:09 -05002004
Al Viro9f1fafe2011-03-25 11:00:12 -04002005 if (!err)
2006 err = complete_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04002007
2008 if (!err && nd->flags & LOOKUP_DIRECTORY) {
Al Viroa1e12632013-06-06 19:33:47 -04002009 if (!can_lookup(nd->inode)) {
Al Virobd92d7f2011-03-14 19:54:59 -04002010 path_put(&nd->path);
Al Virobd23a532011-03-23 09:56:30 -04002011 err = -ENOTDIR;
Al Virobd92d7f2011-03-14 19:54:59 -04002012 }
2013 }
Al Viro16c2cd72011-02-22 15:50:10 -05002014
Tom Marshall384972f2017-04-28 22:46:37 +00002015 if (!err) {
2016 struct super_block *sb = nd->inode->i_sb;
2017 if (sb->s_flags & MS_RDONLY) {
Tom Marshall1a4cd212017-05-19 18:24:49 +00002018 if (d_is_su(nd->path.dentry) && !su_visible()) {
2019 path_put(&nd->path);
Tom Marshall384972f2017-04-28 22:46:37 +00002020 err = -ENOENT;
Tom Marshall1a4cd212017-05-19 18:24:49 +00002021 }
Tom Marshall384972f2017-04-28 22:46:37 +00002022 }
2023 }
2024
Al Viro70e9b352011-03-05 21:12:22 -05002025 if (base)
2026 fput(base);
Al Viroee0827c2011-02-21 23:38:09 -05002027
Al Viro5b6ca022011-03-09 23:04:47 -05002028 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
Al Viro2a737872009-04-07 11:49:53 -04002029 path_put(&nd->root);
2030 nd->root.mnt = NULL;
2031 }
Al Virobd92d7f2011-03-14 19:54:59 -04002032 return err;
Al Viroee0827c2011-02-21 23:38:09 -05002033}
Nick Piggin31e6b012011-01-07 17:49:52 +11002034
Jeff Layton3a9b94c2012-10-10 15:25:29 -04002035static int filename_lookup(int dfd, struct filename *name,
Al Viroee0827c2011-02-21 23:38:09 -05002036 unsigned int flags, struct nameidata *nd)
2037{
Jeff Layton3a9b94c2012-10-10 15:25:29 -04002038 int retval = path_lookupat(dfd, name->name, flags | LOOKUP_RCU, nd);
Al Viroee0827c2011-02-21 23:38:09 -05002039 if (unlikely(retval == -ECHILD))
Jeff Layton3a9b94c2012-10-10 15:25:29 -04002040 retval = path_lookupat(dfd, name->name, flags, nd);
Al Viroee0827c2011-02-21 23:38:09 -05002041 if (unlikely(retval == -ESTALE))
Jeff Layton3a9b94c2012-10-10 15:25:29 -04002042 retval = path_lookupat(dfd, name->name,
2043 flags | LOOKUP_REVAL, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11002044
2045 if (likely(!retval)) {
2046 if (unlikely(!audit_dummy_context())) {
2047 if (nd->path.dentry && nd->inode)
Jeff Layton3a9b94c2012-10-10 15:25:29 -04002048 audit_inode(name->name, nd->path.dentry);
Nick Piggin31e6b012011-01-07 17:49:52 +11002049 }
2050 }
Al Viro9b4a9b12009-04-07 11:44:16 -04002051 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052}
2053
Jeff Layton3a9b94c2012-10-10 15:25:29 -04002054static int do_path_lookup(int dfd, const char *name,
2055 unsigned int flags, struct nameidata *nd)
2056{
2057 struct filename filename = { .name = name };
2058
2059 return filename_lookup(dfd, &filename, flags, nd);
2060}
2061
Al Viro0613fb82012-06-15 03:01:42 +04002062/* does lookup, returns the object with parent locked */
2063struct dentry *kern_path_locked(const char *name, struct path *path)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002064{
Al Viro0613fb82012-06-15 03:01:42 +04002065 struct nameidata nd;
2066 struct dentry *d;
2067 int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd);
2068 if (err)
2069 return ERR_PTR(err);
2070 if (nd.last_type != LAST_NORM) {
2071 path_put(&nd.path);
2072 return ERR_PTR(-EINVAL);
2073 }
2074 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Al Viro59efa862012-07-22 23:46:21 +04002075 d = __lookup_hash(&nd.last, nd.path.dentry, 0);
Al Viro0613fb82012-06-15 03:01:42 +04002076 if (IS_ERR(d)) {
2077 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2078 path_put(&nd.path);
2079 return d;
2080 }
2081 *path = nd.path;
2082 return d;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002083}
2084
Al Virod1811462008-08-02 00:49:18 -04002085int kern_path(const char *name, unsigned int flags, struct path *path)
2086{
2087 struct nameidata nd;
2088 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
2089 if (!res)
2090 *path = nd.path;
2091 return res;
2092}
2093
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002094/**
2095 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2096 * @dentry: pointer to dentry of the base directory
2097 * @mnt: pointer to vfs mount of the base directory
2098 * @name: pointer to file name
2099 * @flags: lookup flags
Al Viroe0a01242011-06-27 17:00:37 -04002100 * @path: pointer to struct path to fill
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002101 */
2102int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2103 const char *name, unsigned int flags,
Al Viroe0a01242011-06-27 17:00:37 -04002104 struct path *path)
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002105{
Al Viroe0a01242011-06-27 17:00:37 -04002106 struct nameidata nd;
2107 int err;
2108 nd.root.dentry = dentry;
2109 nd.root.mnt = mnt;
2110 BUG_ON(flags & LOOKUP_PARENT);
Al Viro5b6ca022011-03-09 23:04:47 -05002111 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
Al Viroe0a01242011-06-27 17:00:37 -04002112 err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
2113 if (!err)
2114 *path = nd.path;
2115 return err;
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002116}
2117
James Morris057f6c02007-04-26 00:12:05 -07002118/*
2119 * Restricted form of lookup. Doesn't follow links, single-component only,
2120 * needs parent already locked. Doesn't follow mounts.
2121 * SMP-safe.
2122 */
Adrian Bunka244e162006-03-31 02:32:11 -08002123static struct dentry *lookup_hash(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124{
Al Viro292d3af2012-06-10 17:17:17 -04002125 return __lookup_hash(&nd->last, nd->path.dentry, nd->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126}
2127
Christoph Hellwigeead1912007-10-16 23:25:38 -07002128/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07002129 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07002130 * @name: pathname component to lookup
Daniel Rosenberg17285112016-10-26 16:27:45 -07002131 * @mnt: mount we are looking up on
Christoph Hellwigeead1912007-10-16 23:25:38 -07002132 * @base: base directory to lookup from
2133 * @len: maximum length @len should be interpreted to
2134 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07002135 * Note that this routine is purely a helper for filesystem usage and should
2136 * not be called by generic code. Also note that by using this function the
Christoph Hellwigeead1912007-10-16 23:25:38 -07002137 * nameidata argument is passed to the filesystem methods and a filesystem
2138 * using this helper needs to be prepared for that.
2139 */
Daniel Rosenberg17285112016-10-26 16:27:45 -07002140struct dentry *lookup_one_len2(const char *name, struct vfsmount *mnt, struct dentry *base, int len)
James Morris057f6c02007-04-26 00:12:05 -07002141{
James Morris057f6c02007-04-26 00:12:05 -07002142 struct qstr this;
Al Viro6a96ba52011-03-07 23:49:20 -05002143 unsigned int c;
Miklos Szeredicda309d2012-03-26 12:54:21 +02002144 int err;
James Morris057f6c02007-04-26 00:12:05 -07002145
David Woodhouse2f9092e2009-04-20 23:18:37 +01002146 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
2147
Al Viro6a96ba52011-03-07 23:49:20 -05002148 this.name = name;
2149 this.len = len;
Linus Torvalds0145acc2012-03-02 14:32:59 -08002150 this.hash = full_name_hash(name, len);
Al Viro6a96ba52011-03-07 23:49:20 -05002151 if (!len)
2152 return ERR_PTR(-EACCES);
2153
Al Viro04618fb2012-11-29 22:17:21 -05002154 if (unlikely(name[0] == '.')) {
2155 if (len < 2 || (len == 2 && name[1] == '.'))
2156 return ERR_PTR(-EACCES);
2157 }
2158
Al Viro6a96ba52011-03-07 23:49:20 -05002159 while (len--) {
2160 c = *(const unsigned char *)name++;
2161 if (c == '/' || c == '\0')
2162 return ERR_PTR(-EACCES);
Al Viro6a96ba52011-03-07 23:49:20 -05002163 }
Al Viro5a202bc2011-03-08 14:17:44 -05002164 /*
2165 * See if the low-level filesystem might want
2166 * to use its own hash..
2167 */
2168 if (base->d_flags & DCACHE_OP_HASH) {
2169 int err = base->d_op->d_hash(base, base->d_inode, &this);
2170 if (err < 0)
2171 return ERR_PTR(err);
2172 }
Christoph Hellwigeead1912007-10-16 23:25:38 -07002173
Daniel Rosenberg17285112016-10-26 16:27:45 -07002174 err = inode_permission2(mnt, base->d_inode, MAY_EXEC);
Miklos Szeredicda309d2012-03-26 12:54:21 +02002175 if (err)
2176 return ERR_PTR(err);
2177
Al Viro292d3af2012-06-10 17:17:17 -04002178 return __lookup_hash(&this, base, 0);
James Morris057f6c02007-04-26 00:12:05 -07002179}
Daniel Rosenberg17285112016-10-26 16:27:45 -07002180EXPORT_SYMBOL(lookup_one_len2);
2181
2182struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2183{
2184 return lookup_one_len2(name, NULL, base, len);
2185}
2186EXPORT_SYMBOL(lookup_one_len);
James Morris057f6c02007-04-26 00:12:05 -07002187
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002188int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2189 struct path *path, int *empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190{
Al Viro2d8f3032008-07-22 09:59:21 -04002191 struct nameidata nd;
Jeff Layton9cee5ca2012-10-10 15:25:28 -04002192 struct filename *tmp = getname_flags(name, flags, empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 int err = PTR_ERR(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 if (!IS_ERR(tmp)) {
Al Viro2d8f3032008-07-22 09:59:21 -04002195
2196 BUG_ON(flags & LOOKUP_PARENT);
2197
Jeff Layton3a9b94c2012-10-10 15:25:29 -04002198 err = filename_lookup(dfd, tmp, flags, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 putname(tmp);
Al Viro2d8f3032008-07-22 09:59:21 -04002200 if (!err)
2201 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 }
2203 return err;
2204}
2205
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002206int user_path_at(int dfd, const char __user *name, unsigned flags,
2207 struct path *path)
2208{
Linus Torvaldsf7493e52012-03-22 16:10:40 -07002209 return user_path_at_empty(dfd, name, flags, path, NULL);
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002210}
2211
Jeff Layton3a9b94c2012-10-10 15:25:29 -04002212/*
2213 * NB: most callers don't do anything directly with the reference to the
2214 * to struct filename, but the nd->last pointer points into the name string
2215 * allocated by getname. So we must hold the reference to it until all
2216 * path-walking is complete.
2217 */
Jeff Layton9cee5ca2012-10-10 15:25:28 -04002218static struct filename *
2219user_path_parent(int dfd, const char __user *path, struct nameidata *nd)
Al Viro2ad94ae2008-07-21 09:32:51 -04002220{
Jeff Layton9cee5ca2012-10-10 15:25:28 -04002221 struct filename *s = getname(path);
Al Viro2ad94ae2008-07-21 09:32:51 -04002222 int error;
2223
2224 if (IS_ERR(s))
Jeff Layton9cee5ca2012-10-10 15:25:28 -04002225 return s;
Al Viro2ad94ae2008-07-21 09:32:51 -04002226
Jeff Layton3a9b94c2012-10-10 15:25:29 -04002227 error = filename_lookup(dfd, s, LOOKUP_PARENT, nd);
Jeff Layton9cee5ca2012-10-10 15:25:28 -04002228 if (error) {
Al Viro2ad94ae2008-07-21 09:32:51 -04002229 putname(s);
Jeff Layton9cee5ca2012-10-10 15:25:28 -04002230 return ERR_PTR(error);
2231 }
Al Viro2ad94ae2008-07-21 09:32:51 -04002232
Jeff Layton9cee5ca2012-10-10 15:25:28 -04002233 return s;
Al Viro2ad94ae2008-07-21 09:32:51 -04002234}
2235
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236/*
2237 * It's inline, so penalty for filesystems that don't use sticky bit is
2238 * minimal.
2239 */
2240static inline int check_sticky(struct inode *dir, struct inode *inode)
2241{
David Howellsda9592e2008-11-14 10:39:05 +11002242 uid_t fsuid = current_fsuid();
2243
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 if (!(dir->i_mode & S_ISVTX))
2245 return 0;
Serge E. Hallyne795b712011-03-23 16:43:25 -07002246 if (current_user_ns() != inode_userns(inode))
2247 goto other_userns;
David Howellsda9592e2008-11-14 10:39:05 +11002248 if (inode->i_uid == fsuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 return 0;
David Howellsda9592e2008-11-14 10:39:05 +11002250 if (dir->i_uid == fsuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 return 0;
Serge E. Hallyne795b712011-03-23 16:43:25 -07002252
2253other_userns:
2254 return !ns_capable(inode_userns(inode), CAP_FOWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255}
2256
2257/*
2258 * Check whether we can remove a link victim from directory dir, check
2259 * whether the type of victim is right.
2260 * 1. We can't do it if dir is read-only (done in permission())
2261 * 2. We should have write and exec permissions on dir
2262 * 3. We can't remove anything from append-only dir
2263 * 4. We can't do anything with immutable dir (done in permission())
2264 * 5. If the sticky bit on dir is set we should either
2265 * a. be owner of dir, or
2266 * b. be owner of victim, or
2267 * c. have CAP_FOWNER capability
2268 * 6. If the victim is append-only or immutable we can't do antyhing with
2269 * links pointing to it.
2270 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2271 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2272 * 9. We can't remove a root or mountpoint.
2273 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
2274 * nfs_async_unlink().
2275 */
Daniel Rosenberg17285112016-10-26 16:27:45 -07002276static int may_delete(struct vfsmount *mnt, struct inode *dir,struct dentry *victim,int isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277{
2278 int error;
2279
2280 if (!victim->d_inode)
2281 return -ENOENT;
2282
2283 BUG_ON(victim->d_parent->d_inode != dir);
Al Virocccc6bb2009-12-25 05:07:33 -05002284 audit_inode_child(victim, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
Daniel Rosenberg17285112016-10-26 16:27:45 -07002286 error = inode_permission2(mnt, dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 if (error)
2288 return error;
2289 if (IS_APPEND(dir))
2290 return -EPERM;
2291 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
Hugh Dickinsf9454542008-11-19 15:36:38 -08002292 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 return -EPERM;
2294 if (isdir) {
2295 if (!S_ISDIR(victim->d_inode->i_mode))
2296 return -ENOTDIR;
2297 if (IS_ROOT(victim))
2298 return -EBUSY;
2299 } else if (S_ISDIR(victim->d_inode->i_mode))
2300 return -EISDIR;
2301 if (IS_DEADDIR(dir))
2302 return -ENOENT;
2303 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2304 return -EBUSY;
2305 return 0;
2306}
2307
2308/* Check whether we can create an object with dentry child in directory
2309 * dir.
2310 * 1. We can't do it if child already exists (open has special treatment for
2311 * this case, but since we are inlined it's OK)
2312 * 2. We can't do it if dir is read-only (done in permission())
2313 * 3. We should have write and exec permissions on dir
2314 * 4. We can't do it if dir is immutable (done in permission())
2315 */
Daniel Rosenberg17285112016-10-26 16:27:45 -07002316static inline int may_create(struct vfsmount *mnt, struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317{
2318 if (child->d_inode)
2319 return -EEXIST;
2320 if (IS_DEADDIR(dir))
2321 return -ENOENT;
Daniel Rosenberg17285112016-10-26 16:27:45 -07002322 return inode_permission2(mnt, dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323}
2324
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325/*
2326 * p1 and p2 should be directories on the same fs.
2327 */
2328struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2329{
2330 struct dentry *p;
2331
2332 if (p1 == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07002333 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 return NULL;
2335 }
2336
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002337 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002339 p = d_ancestor(p2, p1);
2340 if (p) {
2341 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2342 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
2343 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 }
2345
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002346 p = d_ancestor(p1, p2);
2347 if (p) {
2348 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2349 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2350 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 }
2352
Ingo Molnarf2eace22006-07-03 00:25:05 -07002353 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2354 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 return NULL;
2356}
2357
2358void unlock_rename(struct dentry *p1, struct dentry *p2)
2359{
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002360 mutex_unlock(&p1->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 if (p1 != p2) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002362 mutex_unlock(&p2->d_inode->i_mutex);
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002363 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 }
2365}
2366
Daniel Rosenberg17285112016-10-26 16:27:45 -07002367int vfs_create2(struct vfsmount *mnt, struct inode *dir, struct dentry *dentry,
Al Viro8da0eaf2012-06-10 18:09:36 -04002368 umode_t mode, bool want_excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369{
Daniel Rosenberg17285112016-10-26 16:27:45 -07002370 int error = may_create(mnt, dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 if (error)
2372 return error;
2373
Al Viroacfa4382008-12-04 10:06:33 -05002374 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 return -EACCES; /* shouldn't it be ENOSYS? */
2376 mode &= S_IALLUGO;
2377 mode |= S_IFREG;
2378 error = security_inode_create(dir, dentry, mode);
2379 if (error)
2380 return error;
Al Viro8da0eaf2012-06-10 18:09:36 -04002381 error = dir->i_op->create(dir, dentry, mode, want_excl);
Amir Samuelov6a22e462014-05-26 11:44:06 +03002382 if (error)
2383 return error;
2384
2385 error = security_inode_post_create(dir, dentry, mode);
2386 if (error)
2387 return error;
2388
Stephen Smalleya74574a2005-09-09 13:01:44 -07002389 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002390 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 return error;
2392}
Daniel Rosenberg17285112016-10-26 16:27:45 -07002393EXPORT_SYMBOL(vfs_create2);
2394
2395int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viro8da0eaf2012-06-10 18:09:36 -04002396 bool want_excl)
Daniel Rosenberg17285112016-10-26 16:27:45 -07002397{
Al Viro8da0eaf2012-06-10 18:09:36 -04002398 return vfs_create2(NULL, dir, dentry, mode, want_excl);
Daniel Rosenberg17285112016-10-26 16:27:45 -07002399}
2400EXPORT_SYMBOL(vfs_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
Al Viro73d049a2011-03-11 12:08:24 -05002402static int may_open(struct path *path, int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002404 struct dentry *dentry = path->dentry;
Daniel Rosenberg17285112016-10-26 16:27:45 -07002405 struct vfsmount *mnt = path->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 struct inode *inode = dentry->d_inode;
2407 int error;
2408
Al Virobcda7652011-03-13 16:42:14 -04002409 /* O_PATH? */
2410 if (!acc_mode)
2411 return 0;
2412
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 if (!inode)
2414 return -ENOENT;
2415
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002416 switch (inode->i_mode & S_IFMT) {
2417 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002419 case S_IFDIR:
2420 if (acc_mode & MAY_WRITE)
2421 return -EISDIR;
2422 break;
2423 case S_IFBLK:
2424 case S_IFCHR:
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002425 if (path->mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002427 /*FALLTHRU*/
2428 case S_IFIFO:
2429 case S_IFSOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002431 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08002432 }
Dave Hansenb41572e2007-10-16 23:31:14 -07002433
Daniel Rosenberg17285112016-10-26 16:27:45 -07002434 error = inode_permission2(mnt, inode, acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07002435 if (error)
2436 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05002437
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 /*
2439 * An append-only file must be opened in append mode for writing.
2440 */
2441 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05002442 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05002443 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05002445 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 }
2447
2448 /* O_NOATIME can only be set by the owner or superuser */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07002449 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
Al Viro7715b522009-12-16 03:54:00 -05002450 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451
J. Bruce Fieldsf3c7691e2011-09-21 10:58:13 -04002452 return 0;
Al Viro7715b522009-12-16 03:54:00 -05002453}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
Jeff Laytone1181ee2010-12-07 16:19:50 -05002455static int handle_truncate(struct file *filp)
Al Viro7715b522009-12-16 03:54:00 -05002456{
Jeff Laytone1181ee2010-12-07 16:19:50 -05002457 struct path *path = &filp->f_path;
Al Viro7715b522009-12-16 03:54:00 -05002458 struct inode *inode = path->dentry->d_inode;
2459 int error = get_write_access(inode);
2460 if (error)
2461 return error;
2462 /*
2463 * Refuse to truncate files with mandatory locks held on them.
2464 */
2465 error = locks_verify_locked(inode);
2466 if (!error)
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +09002467 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05002468 if (!error) {
Daniel Rosenberga88a18f2016-12-27 16:36:40 -08002469 error = do_truncate2(path->mnt, path->dentry, 0,
Al Viro7715b522009-12-16 03:54:00 -05002470 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
Jeff Laytone1181ee2010-12-07 16:19:50 -05002471 filp);
Al Viro7715b522009-12-16 03:54:00 -05002472 }
2473 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04002474 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475}
2476
Dave Hansend57999e2008-02-15 14:37:27 -08002477static inline int open_to_namei_flags(int flag)
2478{
Al Viro8a5e9292011-06-25 19:15:54 -04002479 if ((flag & O_ACCMODE) == 3)
2480 flag--;
Dave Hansend57999e2008-02-15 14:37:27 -08002481 return flag;
2482}
2483
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002484static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode)
2485{
2486 int error = security_path_mknod(dir, dentry, mode, 0);
2487 if (error)
2488 return error;
2489
2490 error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
2491 if (error)
2492 return error;
2493
2494 return security_inode_create(dir->dentry->d_inode, dentry, mode);
2495}
2496
David Howells9898cc82012-06-14 16:13:46 +01002497/*
2498 * Attempt to atomically look up, create and open a file from a negative
2499 * dentry.
2500 *
2501 * Returns 0 if successful. The file will have been created and attached to
2502 * @file by the filesystem calling finish_open().
2503 *
2504 * Returns 1 if the file was looked up only or didn't need creating. The
2505 * caller will need to perform the open themselves. @path will have been
2506 * updated to point to the new dentry. This may be negative.
2507 *
2508 * Returns an error code otherwise.
2509 */
Al Viro70806c32012-06-22 12:41:10 +04002510static int atomic_open(struct nameidata *nd, struct dentry *dentry,
2511 struct path *path, struct file *file,
2512 const struct open_flags *op,
Al Viro8c162b92012-07-31 00:53:35 +04002513 bool got_write, bool need_lookup,
Al Viro70806c32012-06-22 12:41:10 +04002514 int *opened)
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002515{
2516 struct inode *dir = nd->path.dentry->d_inode;
2517 unsigned open_flag = open_to_namei_flags(op->open_flag);
2518 umode_t mode;
2519 int error;
2520 int acc_mode;
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002521 int create_error = 0;
2522 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
2523
2524 BUG_ON(dentry->d_inode);
2525
2526 /* Don't create child dentry for a dead directory. */
2527 if (unlikely(IS_DEADDIR(dir))) {
Al Viro70806c32012-06-22 12:41:10 +04002528 error = -ENOENT;
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002529 goto out;
2530 }
2531
Miklos Szeredi99fdd3a2012-08-15 13:01:24 +02002532 mode = op->mode;
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002533 if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
2534 mode &= ~current_umask();
2535
Al Viro5b2d9ff2012-07-30 11:50:30 +04002536 if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT)) {
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002537 open_flag &= ~O_TRUNC;
Al Viro8915ef32012-06-10 05:01:45 -04002538 *opened |= FILE_CREATED;
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002539 }
2540
2541 /*
2542 * Checking write permission is tricky, bacuse we don't know if we are
2543 * going to actually need it: O_CREAT opens should work as long as the
2544 * file exists. But checking existence breaks atomicity. The trick is
2545 * to check access and if not granted clear O_CREAT from the flags.
2546 *
2547 * Another problem is returing the "right" error value (e.g. for an
2548 * O_EXCL open we want to return EEXIST not EROFS).
2549 */
Al Viro8c162b92012-07-31 00:53:35 +04002550 if (((open_flag & (O_CREAT | O_TRUNC)) ||
2551 (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) {
2552 if (!(open_flag & O_CREAT)) {
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002553 /*
2554 * No O_CREATE -> atomicity not a requirement -> fall
2555 * back to lookup + open
2556 */
2557 goto no_open;
2558 } else if (open_flag & (O_EXCL | O_TRUNC)) {
2559 /* Fall back and fail with the right error */
Al Viro8c162b92012-07-31 00:53:35 +04002560 create_error = -EROFS;
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002561 goto no_open;
2562 } else {
2563 /* No side effects, safe to clear O_CREAT */
Al Viro8c162b92012-07-31 00:53:35 +04002564 create_error = -EROFS;
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002565 open_flag &= ~O_CREAT;
2566 }
2567 }
2568
2569 if (open_flag & O_CREAT) {
Miklos Szeredi63bab5b2012-08-15 13:01:24 +02002570 error = may_o_create(&nd->path, dentry, mode);
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002571 if (error) {
2572 create_error = error;
2573 if (open_flag & O_EXCL)
2574 goto no_open;
2575 open_flag &= ~O_CREAT;
2576 }
2577 }
2578
2579 if (nd->flags & LOOKUP_DIRECTORY)
2580 open_flag |= O_DIRECTORY;
2581
Al Virob2ebdc52012-06-22 12:40:19 +04002582 file->f_path.dentry = DENTRY_NOT_SET;
2583 file->f_path.mnt = nd->path.mnt;
2584 error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode,
Al Viro8915ef32012-06-10 05:01:45 -04002585 opened);
Al Viro50ed02d2012-06-22 12:39:14 +04002586 if (error < 0) {
Al Viro50ed02d2012-06-22 12:39:14 +04002587 if (create_error && error == -ENOENT)
2588 error = create_error;
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002589 goto out;
2590 }
2591
2592 acc_mode = op->acc_mode;
Al Viro8915ef32012-06-10 05:01:45 -04002593 if (*opened & FILE_CREATED) {
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002594 fsnotify_create(dir, dentry);
2595 acc_mode = MAY_OPEN;
2596 }
2597
Al Viro50ed02d2012-06-22 12:39:14 +04002598 if (error) { /* returned 1, that is */
Al Virob2ebdc52012-06-22 12:40:19 +04002599 if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
Al Viro70806c32012-06-22 12:41:10 +04002600 error = -EIO;
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002601 goto out;
2602 }
Al Virob2ebdc52012-06-22 12:40:19 +04002603 if (file->f_path.dentry) {
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002604 dput(dentry);
Al Virob2ebdc52012-06-22 12:40:19 +04002605 dentry = file->f_path.dentry;
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002606 }
Sage Weilb5e96de2012-08-15 13:30:12 -07002607 if (create_error && dentry->d_inode == NULL) {
2608 error = create_error;
2609 goto out;
2610 }
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002611 goto looked_up;
2612 }
2613
2614 /*
2615 * We didn't have the inode before the open, so check open permission
2616 * here.
2617 */
Al Viro70806c32012-06-22 12:41:10 +04002618 error = may_open(&file->f_path, acc_mode, open_flag);
2619 if (error)
2620 fput(file);
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002621
2622out:
2623 dput(dentry);
Al Viro70806c32012-06-22 12:41:10 +04002624 return error;
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002625
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002626no_open:
2627 if (need_lookup) {
Al Viro292d3af2012-06-10 17:17:17 -04002628 dentry = lookup_real(dir, dentry, nd->flags);
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002629 if (IS_ERR(dentry))
Al Viro70806c32012-06-22 12:41:10 +04002630 return PTR_ERR(dentry);
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002631
2632 if (create_error) {
2633 int open_flag = op->open_flag;
2634
Al Viro70806c32012-06-22 12:41:10 +04002635 error = create_error;
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002636 if ((open_flag & O_EXCL)) {
2637 if (!dentry->d_inode)
2638 goto out;
2639 } else if (!dentry->d_inode) {
2640 goto out;
2641 } else if ((open_flag & O_TRUNC) &&
2642 S_ISREG(dentry->d_inode->i_mode)) {
2643 goto out;
2644 }
2645 /* will fail later, go on to get the right error */
2646 }
2647 }
2648looked_up:
2649 path->dentry = dentry;
2650 path->mnt = nd->path.mnt;
Al Viro70806c32012-06-22 12:41:10 +04002651 return 1;
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002652}
2653
Nick Piggin31e6b012011-01-07 17:49:52 +11002654/*
David Howells9898cc82012-06-14 16:13:46 +01002655 * Look up and maybe create and open the last component.
Miklos Szeredi8632d4d2012-06-05 15:10:15 +02002656 *
2657 * Must be called with i_mutex held on parent.
2658 *
David Howells9898cc82012-06-14 16:13:46 +01002659 * Returns 0 if the file was successfully atomically created (if necessary) and
2660 * opened. In this case the file will be returned attached to @file.
2661 *
2662 * Returns 1 if the file was not completely opened at this time, though lookups
2663 * and creations will have been performed and the dentry returned in @path will
2664 * be positive upon return if O_CREAT was specified. If O_CREAT wasn't
2665 * specified then a negative dentry may be returned.
2666 *
2667 * An error code is returned otherwise.
2668 *
2669 * FILE_CREATE will be set in @*opened if the dentry was created and will be
2670 * cleared otherwise prior to returning.
Miklos Szeredi8632d4d2012-06-05 15:10:15 +02002671 */
Al Viro70806c32012-06-22 12:41:10 +04002672static int lookup_open(struct nameidata *nd, struct path *path,
2673 struct file *file,
2674 const struct open_flags *op,
Al Viro8c162b92012-07-31 00:53:35 +04002675 bool got_write, int *opened)
Miklos Szeredi8632d4d2012-06-05 15:10:15 +02002676{
2677 struct dentry *dir = nd->path.dentry;
Miklos Szeredi10606642012-06-05 15:10:16 +02002678 struct inode *dir_inode = dir->d_inode;
Miklos Szeredi8632d4d2012-06-05 15:10:15 +02002679 struct vfsmount *mnt = nd->path.mnt;
2680 struct dentry *dentry;
2681 int error;
Miklos Szeredi10606642012-06-05 15:10:16 +02002682 bool need_lookup;
Miklos Szeredi8632d4d2012-06-05 15:10:15 +02002683
Al Viro8915ef32012-06-10 05:01:45 -04002684 *opened &= ~FILE_CREATED;
Al Viro61b61da2012-06-22 12:42:10 +04002685 dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup);
Miklos Szeredi8632d4d2012-06-05 15:10:15 +02002686 if (IS_ERR(dentry))
Al Viro70806c32012-06-22 12:41:10 +04002687 return PTR_ERR(dentry);
Miklos Szeredi8632d4d2012-06-05 15:10:15 +02002688
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002689 /* Cached positive dentry: will open in f_op->open */
2690 if (!need_lookup && dentry->d_inode)
2691 goto out_no_open;
2692
2693 if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) {
Al Viro8c162b92012-07-31 00:53:35 +04002694 return atomic_open(nd, dentry, path, file, op, got_write,
Al Viro8915ef32012-06-10 05:01:45 -04002695 need_lookup, opened);
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002696 }
2697
Miklos Szeredi10606642012-06-05 15:10:16 +02002698 if (need_lookup) {
2699 BUG_ON(dentry->d_inode);
2700
Al Viro292d3af2012-06-10 17:17:17 -04002701 dentry = lookup_real(dir_inode, dentry, nd->flags);
Miklos Szeredi10606642012-06-05 15:10:16 +02002702 if (IS_ERR(dentry))
Al Viro70806c32012-06-22 12:41:10 +04002703 return PTR_ERR(dentry);
Miklos Szeredi10606642012-06-05 15:10:16 +02002704 }
2705
Miklos Szeredi8632d4d2012-06-05 15:10:15 +02002706 /* Negative dentry, just create the file */
2707 if (!dentry->d_inode && (op->open_flag & O_CREAT)) {
2708 umode_t mode = op->mode;
2709 if (!IS_POSIXACL(dir->d_inode))
2710 mode &= ~current_umask();
2711 /*
2712 * This write is needed to ensure that a
2713 * rw->ro transition does not occur between
2714 * the time when the file is created and when
2715 * a permanent write count is taken through
Miklos Szeredi2f80ad32012-06-05 15:10:27 +02002716 * the 'struct file' in finish_open().
Miklos Szeredi8632d4d2012-06-05 15:10:15 +02002717 */
Al Viro8c162b92012-07-31 00:53:35 +04002718 if (!got_write) {
2719 error = -EROFS;
Miklos Szeredi8632d4d2012-06-05 15:10:15 +02002720 goto out_dput;
Al Viro8c162b92012-07-31 00:53:35 +04002721 }
Al Viro8915ef32012-06-10 05:01:45 -04002722 *opened |= FILE_CREATED;
Miklos Szeredi8632d4d2012-06-05 15:10:15 +02002723 error = security_path_mknod(&nd->path, dentry, mode, 0);
2724 if (error)
2725 goto out_dput;
Al Viro8da0eaf2012-06-10 18:09:36 -04002726 error = vfs_create2(mnt, dir->d_inode, dentry, mode,
2727 nd->flags & LOOKUP_EXCL);
Miklos Szeredi8632d4d2012-06-05 15:10:15 +02002728 if (error)
2729 goto out_dput;
2730 }
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002731out_no_open:
Miklos Szeredi8632d4d2012-06-05 15:10:15 +02002732 path->dentry = dentry;
2733 path->mnt = nd->path.mnt;
Al Viro70806c32012-06-22 12:41:10 +04002734 return 1;
Miklos Szeredi8632d4d2012-06-05 15:10:15 +02002735
2736out_dput:
2737 dput(dentry);
Al Viro70806c32012-06-22 12:41:10 +04002738 return error;
Miklos Szeredi8632d4d2012-06-05 15:10:15 +02002739}
2740
Nick Piggin31e6b012011-01-07 17:49:52 +11002741/*
Al Virofe2d35f2011-03-05 22:58:25 -05002742 * Handle the last step of open()
Nick Piggin31e6b012011-01-07 17:49:52 +11002743 */
Al Viro70806c32012-06-22 12:41:10 +04002744static int do_last(struct nameidata *nd, struct path *path,
2745 struct file *file, const struct open_flags *op,
Jeff Layton631423f2012-10-10 16:43:10 -04002746 int *opened, struct filename *name)
Al Virofb1cc552009-12-24 01:58:28 -05002747{
Al Viroa1e28032009-12-24 02:12:06 -05002748 struct dentry *dir = nd->path.dentry;
Al Viroca344a892011-03-09 00:36:45 -05002749 int open_flag = op->open_flag;
Miklos Szeredid6a4ce32012-06-05 15:10:30 +02002750 bool will_truncate = (open_flag & O_TRUNC) != 0;
Al Viro8c162b92012-07-31 00:53:35 +04002751 bool got_write = false;
Al Virobcda7652011-03-13 16:42:14 -04002752 int acc_mode = op->acc_mode;
Miklos Szeredia5f84da2012-05-21 17:30:07 +02002753 struct inode *inode;
Miklos Szeredid6a4ce32012-06-05 15:10:30 +02002754 bool symlink_ok = false;
Miklos Szerediefbfa632012-05-21 17:30:19 +02002755 struct path save_parent = { .dentry = NULL, .mnt = NULL };
2756 bool retried = false;
Al Viro16c2cd72011-02-22 15:50:10 -05002757 int error;
Jeff Layton631423f2012-10-10 16:43:10 -04002758 const char *pathname = name->name;
Al Virofb1cc552009-12-24 01:58:28 -05002759
Al Viroc3e380b2011-02-23 13:39:45 -05002760 nd->flags &= ~LOOKUP_PARENT;
2761 nd->flags |= op->intent;
2762
Al Viro1f36f772009-12-26 10:56:19 -05002763 switch (nd->last_type) {
2764 case LAST_DOTDOT:
Neil Brown176306f2010-05-24 16:57:56 +10002765 case LAST_DOT:
Al Virofe2d35f2011-03-05 22:58:25 -05002766 error = handle_dots(nd, nd->last_type);
2767 if (error)
Al Viro70806c32012-06-22 12:41:10 +04002768 return error;
Al Viro1f36f772009-12-26 10:56:19 -05002769 /* fallthrough */
Al Viro1f36f772009-12-26 10:56:19 -05002770 case LAST_ROOT:
Al Viro9f1fafe2011-03-25 11:00:12 -04002771 error = complete_walk(nd);
Al Viro16c2cd72011-02-22 15:50:10 -05002772 if (error)
Al Viro70806c32012-06-22 12:41:10 +04002773 return error;
Al Virofe2d35f2011-03-05 22:58:25 -05002774 audit_inode(pathname, nd->path.dentry);
Al Viroca344a892011-03-09 00:36:45 -05002775 if (open_flag & O_CREAT) {
Al Virofe2d35f2011-03-05 22:58:25 -05002776 error = -EISDIR;
Al Viro70806c32012-06-22 12:41:10 +04002777 goto out;
Al Virofe2d35f2011-03-05 22:58:25 -05002778 }
Miklos Szeredi8d49e6e2012-06-05 15:10:29 +02002779 goto finish_open;
Al Viro1f36f772009-12-26 10:56:19 -05002780 case LAST_BIND:
Al Viro9f1fafe2011-03-25 11:00:12 -04002781 error = complete_walk(nd);
Al Viro16c2cd72011-02-22 15:50:10 -05002782 if (error)
Al Viro70806c32012-06-22 12:41:10 +04002783 return error;
Al Viro1f36f772009-12-26 10:56:19 -05002784 audit_inode(pathname, dir);
Miklos Szeredi8d49e6e2012-06-05 15:10:29 +02002785 goto finish_open;
Al Viro1f36f772009-12-26 10:56:19 -05002786 }
Al Viro67ee3ad2009-12-26 07:01:01 -05002787
Al Viroca344a892011-03-09 00:36:45 -05002788 if (!(open_flag & O_CREAT)) {
Al Virofe2d35f2011-03-05 22:58:25 -05002789 if (nd->last.name[nd->last.len])
2790 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
Al Virobcda7652011-03-13 16:42:14 -04002791 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
Miklos Szeredid6a4ce32012-06-05 15:10:30 +02002792 symlink_ok = true;
Al Virofe2d35f2011-03-05 22:58:25 -05002793 /* we _can_ be in RCU mode here */
Miklos Szeredia5f84da2012-05-21 17:30:07 +02002794 error = lookup_fast(nd, &nd->last, path, &inode);
Miklos Szeredia2ab3642012-06-05 15:10:14 +02002795 if (likely(!error))
2796 goto finish_lookup;
Miklos Szeredia5f84da2012-05-21 17:30:07 +02002797
Al Viroce57dfc2011-03-13 19:58:58 -04002798 if (error < 0)
Al Viro70806c32012-06-22 12:41:10 +04002799 goto out;
Miklos Szeredi513072f2012-06-05 15:10:12 +02002800
Miklos Szeredia2ab3642012-06-05 15:10:14 +02002801 BUG_ON(nd->inode != dir->d_inode);
Miklos Szeredi441a4582012-06-05 15:10:13 +02002802 } else {
2803 /* create side of things */
2804 /*
2805 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
2806 * has been cleared when we got to the last component we are
2807 * about to look up
2808 */
Al Viro9f1fafe2011-03-25 11:00:12 -04002809 error = complete_walk(nd);
2810 if (error)
Al Viro70806c32012-06-22 12:41:10 +04002811 return error;
Al Virofe2d35f2011-03-05 22:58:25 -05002812
Miklos Szeredi441a4582012-06-05 15:10:13 +02002813 audit_inode(pathname, dir);
2814 error = -EISDIR;
2815 /* trailing slashes? */
2816 if (nd->last.name[nd->last.len])
Al Viro70806c32012-06-22 12:41:10 +04002817 goto out;
Al Virofe2d35f2011-03-05 22:58:25 -05002818 }
2819
Miklos Szerediefbfa632012-05-21 17:30:19 +02002820retry_lookup:
Al Viro8c162b92012-07-31 00:53:35 +04002821 if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
Al Virofb1cc552009-12-24 01:58:28 -05002822 error = mnt_want_write(nd->path.mnt);
Al Viro8c162b92012-07-31 00:53:35 +04002823 if (!error)
2824 got_write = true;
2825 /*
2826 * do _not_ fail yet - we might not need that or fail with
2827 * a different error; let lookup_open() decide; we'll be
2828 * dropping this one anyway.
2829 */
2830 }
Al Virofb1cc552009-12-24 01:58:28 -05002831 mutex_lock(&dir->d_inode->i_mutex);
Al Viro8c162b92012-07-31 00:53:35 +04002832 error = lookup_open(nd, path, file, op, got_write, opened);
Miklos Szeredi8632d4d2012-06-05 15:10:15 +02002833 mutex_unlock(&dir->d_inode->i_mutex);
Al Virofb1cc552009-12-24 01:58:28 -05002834
Al Viro70806c32012-06-22 12:41:10 +04002835 if (error <= 0) {
Al Virofb1cc552009-12-24 01:58:28 -05002836 if (error)
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002837 goto out;
2838
Al Viro8915ef32012-06-10 05:01:45 -04002839 if ((*opened & FILE_CREATED) ||
Al Viro70806c32012-06-22 12:41:10 +04002840 !S_ISREG(file->f_path.dentry->d_inode->i_mode))
Miklos Szeredid6a4ce32012-06-05 15:10:30 +02002841 will_truncate = false;
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002842
Al Viro70806c32012-06-22 12:41:10 +04002843 audit_inode(pathname, file->f_path.dentry);
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002844 goto opened;
2845 }
Al Virofb1cc552009-12-24 01:58:28 -05002846
Al Viro8915ef32012-06-10 05:01:45 -04002847 if (*opened & FILE_CREATED) {
Al Viro9b44f1b2011-03-09 00:17:27 -05002848 /* Don't check for write permission, don't truncate */
Al Viroca344a892011-03-09 00:36:45 -05002849 open_flag &= ~O_TRUNC;
Miklos Szeredid6a4ce32012-06-05 15:10:30 +02002850 will_truncate = false;
Al Virobcda7652011-03-13 16:42:14 -04002851 acc_mode = MAY_OPEN;
Miklos Szeredi8632d4d2012-06-05 15:10:15 +02002852 path_to_nameidata(path, nd);
Miklos Szeredi8d49e6e2012-06-05 15:10:29 +02002853 goto finish_open_created;
Al Virofb1cc552009-12-24 01:58:28 -05002854 }
2855
2856 /*
Jeff Layton968c5ab2012-07-25 10:19:47 -04002857 * create/update audit record if it already exists.
Al Virofb1cc552009-12-24 01:58:28 -05002858 */
Jeff Layton968c5ab2012-07-25 10:19:47 -04002859 if (path->dentry->d_inode)
2860 audit_inode(pathname, path->dentry);
Al Virofb1cc552009-12-24 01:58:28 -05002861
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002862 /*
2863 * If atomic_open() acquired write access it is dropped now due to
2864 * possible mount and symlink following (this might be optimized away if
2865 * necessary...)
2866 */
Al Viro8c162b92012-07-31 00:53:35 +04002867 if (got_write) {
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002868 mnt_drop_write(nd->path.mnt);
Al Viro8c162b92012-07-31 00:53:35 +04002869 got_write = false;
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002870 }
Al Virofb1cc552009-12-24 01:58:28 -05002871
2872 error = -EEXIST;
Al Viro5b2d9ff2012-07-30 11:50:30 +04002873 if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))
Al Virofb1cc552009-12-24 01:58:28 -05002874 goto exit_dput;
2875
David Howells9875cf82011-01-14 18:45:21 +00002876 error = follow_managed(path, nd->flags);
2877 if (error < 0)
2878 goto exit_dput;
Al Virofb1cc552009-12-24 01:58:28 -05002879
Al Viroa3fbbde2011-11-07 21:21:26 +00002880 if (error)
2881 nd->flags |= LOOKUP_JUMPED;
2882
Miklos Szeredi5c7f4512012-05-21 17:30:08 +02002883 BUG_ON(nd->flags & LOOKUP_RCU);
2884 inode = path->dentry->d_inode;
Miklos Szeredi99ec9802012-05-21 17:30:14 +02002885finish_lookup:
2886 /* we _can_ be in RCU mode here */
Al Virofb1cc552009-12-24 01:58:28 -05002887 error = -ENOENT;
Miklos Szeredi16785062012-05-21 17:30:10 +02002888 if (!inode) {
2889 path_to_nameidata(path, nd);
Al Viro70806c32012-06-22 12:41:10 +04002890 goto out;
Miklos Szeredi16785062012-05-21 17:30:10 +02002891 }
Al Viro9e67f362009-12-26 07:04:50 -05002892
Miklos Szeredi458abf22012-05-21 17:30:09 +02002893 if (should_follow_link(inode, !symlink_ok)) {
2894 if (nd->flags & LOOKUP_RCU) {
2895 if (unlikely(unlazy_walk(nd, path->dentry))) {
2896 error = -ECHILD;
Al Viro70806c32012-06-22 12:41:10 +04002897 goto out;
Miklos Szeredi458abf22012-05-21 17:30:09 +02002898 }
2899 }
2900 BUG_ON(inode != path->dentry->d_inode);
Al Viro70806c32012-06-22 12:41:10 +04002901 return 1;
Miklos Szeredi458abf22012-05-21 17:30:09 +02002902 }
Al Virofb1cc552009-12-24 01:58:28 -05002903
Miklos Szerediefbfa632012-05-21 17:30:19 +02002904 if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) {
2905 path_to_nameidata(path, nd);
2906 } else {
2907 save_parent.dentry = nd->path.dentry;
2908 save_parent.mnt = mntget(path->mnt);
2909 nd->path.dentry = path->dentry;
2910
2911 }
Miklos Szeredi5c7f4512012-05-21 17:30:08 +02002912 nd->inode = inode;
Al Viroa3fbbde2011-11-07 21:21:26 +00002913 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
2914 error = complete_walk(nd);
Miklos Szerediefbfa632012-05-21 17:30:19 +02002915 if (error) {
2916 path_put(&save_parent);
Al Viro70806c32012-06-22 12:41:10 +04002917 return error;
Miklos Szerediefbfa632012-05-21 17:30:19 +02002918 }
Al Virofb1cc552009-12-24 01:58:28 -05002919 error = -EISDIR;
Miklos Szeredic6317212012-05-21 17:30:12 +02002920 if ((open_flag & O_CREAT) && S_ISDIR(nd->inode->i_mode))
Al Viro70806c32012-06-22 12:41:10 +04002921 goto out;
Miklos Szerediaba9dce2012-05-21 17:30:11 +02002922 error = -ENOTDIR;
Al Viroa1e12632013-06-06 19:33:47 -04002923 if ((nd->flags & LOOKUP_DIRECTORY) && !can_lookup(nd->inode))
Al Viro70806c32012-06-22 12:41:10 +04002924 goto out;
Miklos Szeredi60fc85b2012-05-21 17:30:13 +02002925 audit_inode(pathname, nd->path.dentry);
Miklos Szeredi8d49e6e2012-06-05 15:10:29 +02002926finish_open:
Al Viro6c0d46c2011-03-09 00:59:59 -05002927 if (!S_ISREG(nd->inode->i_mode))
Miklos Szeredid6a4ce32012-06-05 15:10:30 +02002928 will_truncate = false;
Al Viro6c0d46c2011-03-09 00:59:59 -05002929
Al Viro0f9d1a12011-03-09 00:13:14 -05002930 if (will_truncate) {
2931 error = mnt_want_write(nd->path.mnt);
2932 if (error)
Al Viro70806c32012-06-22 12:41:10 +04002933 goto out;
Al Viro8c162b92012-07-31 00:53:35 +04002934 got_write = true;
Al Viro0f9d1a12011-03-09 00:13:14 -05002935 }
Miklos Szeredi8d49e6e2012-06-05 15:10:29 +02002936finish_open_created:
Al Virobcda7652011-03-13 16:42:14 -04002937 error = may_open(&nd->path, acc_mode, open_flag);
Al Viroca344a892011-03-09 00:36:45 -05002938 if (error)
Al Viro70806c32012-06-22 12:41:10 +04002939 goto out;
Al Virob2ebdc52012-06-22 12:40:19 +04002940 file->f_path.mnt = nd->path.mnt;
2941 error = finish_open(file, nd->path.dentry, NULL, opened);
2942 if (error) {
Al Virob2ebdc52012-06-22 12:40:19 +04002943 if (error == -EOPENSTALE)
Miklos Szeredi36c27522012-06-05 15:10:31 +02002944 goto stale_open;
Miklos Szeredi2f80ad32012-06-05 15:10:27 +02002945 goto out;
Al Viro0f9d1a12011-03-09 00:13:14 -05002946 }
Miklos Szeredi2de99c02012-06-05 15:10:32 +02002947opened:
Al Viro70806c32012-06-22 12:41:10 +04002948 error = open_check_o_direct(file);
Miklos Szeredi2f80ad32012-06-05 15:10:27 +02002949 if (error)
2950 goto exit_fput;
Al Viro70806c32012-06-22 12:41:10 +04002951 error = ima_file_check(file, op->acc_mode);
Miklos Szeredi4233c212012-06-05 15:10:28 +02002952 if (error)
2953 goto exit_fput;
2954
2955 if (will_truncate) {
Al Viro70806c32012-06-22 12:41:10 +04002956 error = handle_truncate(file);
Miklos Szeredi4233c212012-06-05 15:10:28 +02002957 if (error)
2958 goto exit_fput;
Al Viro0f9d1a12011-03-09 00:13:14 -05002959 }
Al Viroca344a892011-03-09 00:36:45 -05002960out:
Al Viro8c162b92012-07-31 00:53:35 +04002961 if (got_write)
Al Viro0f9d1a12011-03-09 00:13:14 -05002962 mnt_drop_write(nd->path.mnt);
Miklos Szerediefbfa632012-05-21 17:30:19 +02002963 path_put(&save_parent);
Miklos Szeredi2aabf6d2012-05-21 17:30:06 +02002964 terminate_walk(nd);
Al Viro70806c32012-06-22 12:41:10 +04002965 return error;
Al Virofb1cc552009-12-24 01:58:28 -05002966
Al Virofb1cc552009-12-24 01:58:28 -05002967exit_dput:
2968 path_put_conditional(path, nd);
Al Viroca344a892011-03-09 00:36:45 -05002969 goto out;
Miklos Szeredi2f80ad32012-06-05 15:10:27 +02002970exit_fput:
Al Viro70806c32012-06-22 12:41:10 +04002971 fput(file);
2972 goto out;
Miklos Szeredi2f80ad32012-06-05 15:10:27 +02002973
Miklos Szeredi36c27522012-06-05 15:10:31 +02002974stale_open:
2975 /* If no saved parent or already retried then can't retry */
2976 if (!save_parent.dentry || retried)
2977 goto out;
2978
2979 BUG_ON(save_parent.dentry != dir);
2980 path_put(&nd->path);
2981 nd->path = save_parent;
2982 nd->inode = dir->d_inode;
2983 save_parent.mnt = NULL;
2984 save_parent.dentry = NULL;
Al Viro8c162b92012-07-31 00:53:35 +04002985 if (got_write) {
Miklos Szeredi36c27522012-06-05 15:10:31 +02002986 mnt_drop_write(nd->path.mnt);
Al Viro8c162b92012-07-31 00:53:35 +04002987 got_write = false;
Miklos Szeredi36c27522012-06-05 15:10:31 +02002988 }
2989 retried = true;
2990 goto retry_lookup;
Al Virofb1cc552009-12-24 01:58:28 -05002991}
2992
Al Virob0d54302013-06-07 01:20:27 -04002993static int do_tmpfile(int dfd, struct filename *pathname,
2994 struct nameidata *nd, int flags,
2995 const struct open_flags *op,
2996 struct file *file, int *opened)
2997{
2998 static const struct qstr name = QSTR_INIT("/", 1);
2999 struct dentry *dentry, *child;
3000 struct inode *dir;
3001 int error = path_lookupat(dfd, pathname->name,
3002 flags | LOOKUP_DIRECTORY, nd);
3003 if (unlikely(error))
3004 return error;
3005 error = mnt_want_write(nd->path.mnt);
3006 if (unlikely(error))
3007 goto out;
3008 /* we want directory to be writable */
3009 error = inode_permission(nd->inode, MAY_WRITE | MAY_EXEC);
3010 if (error)
3011 goto out2;
3012 dentry = nd->path.dentry;
3013 dir = dentry->d_inode;
3014 if (!dir->i_op->tmpfile) {
3015 error = -EOPNOTSUPP;
3016 goto out2;
3017 }
3018 child = d_alloc(dentry, &name);
3019 if (unlikely(!child)) {
3020 error = -ENOMEM;
3021 goto out2;
3022 }
3023 nd->flags &= ~LOOKUP_DIRECTORY;
3024 nd->flags |= op->intent;
3025 dput(nd->path.dentry);
3026 nd->path.dentry = child;
3027 error = dir->i_op->tmpfile(dir, nd->path.dentry, op->mode);
3028 if (error)
3029 goto out2;
3030 audit_inode(pathname->name, nd->path.dentry);
Eric Rannaud78cb8e02014-10-30 01:51:01 -07003031 /* Don't check for other permissions, the inode was just created */
3032 error = may_open(&nd->path, MAY_OPEN, op->open_flag);
Al Virob0d54302013-06-07 01:20:27 -04003033 if (error)
3034 goto out2;
3035 file->f_path.mnt = nd->path.mnt;
3036 error = finish_open(file, nd->path.dentry, NULL, opened);
3037 if (error)
3038 goto out2;
3039 error = open_check_o_direct(file);
Al Virof6f34262013-06-11 08:34:36 +04003040 if (error) {
Al Virob0d54302013-06-07 01:20:27 -04003041 fput(file);
Al Virof6f34262013-06-11 08:34:36 +04003042 } else if (!(op->open_flag & O_EXCL)) {
3043 struct inode *inode = file->f_path.dentry->d_inode;
3044 spin_lock(&inode->i_lock);
3045 inode->i_state |= I_LINKABLE;
3046 spin_unlock(&inode->i_lock);
3047 }
Al Virob0d54302013-06-07 01:20:27 -04003048out2:
3049 mnt_drop_write(nd->path.mnt);
3050out:
3051 path_put(&nd->path);
3052 return error;
3053}
3054
Jeff Layton631423f2012-10-10 16:43:10 -04003055static struct file *path_openat(int dfd, struct filename *pathname,
Al Viro73d049a2011-03-11 12:08:24 -05003056 struct nameidata *nd, const struct open_flags *op, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057{
Al Virofe2d35f2011-03-05 22:58:25 -05003058 struct file *base = NULL;
Al Virob2ebdc52012-06-22 12:40:19 +04003059 struct file *file;
Al Viro9850c052010-01-13 15:01:15 -05003060 struct path path;
Al Viro8915ef32012-06-10 05:01:45 -04003061 int opened = 0;
Al Viro13aab422011-02-23 17:54:08 -05003062 int error;
Nick Piggin31e6b012011-01-07 17:49:52 +11003063
Al Virob2ebdc52012-06-22 12:40:19 +04003064 file = get_empty_filp();
3065 if (!file)
Nick Piggin31e6b012011-01-07 17:49:52 +11003066 return ERR_PTR(-ENFILE);
3067
Al Virob2ebdc52012-06-22 12:40:19 +04003068 file->f_flags = op->open_flag;
Nick Piggin31e6b012011-01-07 17:49:52 +11003069
Al Virod50bcfe2013-07-13 13:26:37 +04003070 if (unlikely(file->f_flags & __O_TMPFILE)) {
Al Virob0d54302013-06-07 01:20:27 -04003071 error = do_tmpfile(dfd, pathname, nd, flags, op, file, &opened);
Al Viro8b411372015-05-08 22:53:15 -04003072 goto out2;
Al Virob0d54302013-06-07 01:20:27 -04003073 }
3074
Jeff Layton631423f2012-10-10 16:43:10 -04003075 error = path_init(dfd, pathname->name, flags | LOOKUP_PARENT, nd, &base);
Nick Piggin31e6b012011-01-07 17:49:52 +11003076 if (unlikely(error))
Al Viro70806c32012-06-22 12:41:10 +04003077 goto out;
Nick Piggin31e6b012011-01-07 17:49:52 +11003078
Al Virofe2d35f2011-03-05 22:58:25 -05003079 current->total_link_count = 0;
Jeff Layton631423f2012-10-10 16:43:10 -04003080 error = link_path_walk(pathname->name, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11003081 if (unlikely(error))
Al Viro70806c32012-06-22 12:41:10 +04003082 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083
Al Viro70806c32012-06-22 12:41:10 +04003084 error = do_last(nd, &path, file, op, &opened, pathname);
3085 while (unlikely(error > 0)) { /* trailing symlink */
Nick Piggin7b9337a2011-01-14 08:42:43 +00003086 struct path link = path;
Al Virodef4af32009-12-26 08:37:05 -05003087 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04003088 if (!(nd->flags & LOOKUP_FOLLOW)) {
Al Viro73d049a2011-03-11 12:08:24 -05003089 path_put_conditional(&path, nd);
3090 path_put(&nd->path);
Al Viro70806c32012-06-22 12:41:10 +04003091 error = -ELOOP;
Al Viro40b39132011-03-09 16:22:18 -05003092 break;
3093 }
Kees Cook5092e9c2012-07-25 17:29:07 -07003094 error = may_follow_link(&link, nd);
3095 if (unlikely(error))
3096 break;
Al Viro73d049a2011-03-11 12:08:24 -05003097 nd->flags |= LOOKUP_PARENT;
3098 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
Al Viro574197e2011-03-14 22:20:34 -04003099 error = follow_link(&link, nd, &cookie);
Al Viroc3e380b2011-02-23 13:39:45 -05003100 if (unlikely(error))
Al Viro70806c32012-06-22 12:41:10 +04003101 break;
3102 error = do_last(nd, &path, file, op, &opened, pathname);
Al Viro574197e2011-03-14 22:20:34 -04003103 put_link(nd, &link, cookie);
Al Viro806b6812009-12-26 07:16:40 -05003104 }
Al Viro10fa8e62009-12-26 07:09:49 -05003105out:
Al Viro73d049a2011-03-11 12:08:24 -05003106 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
3107 path_put(&nd->root);
Al Virofe2d35f2011-03-05 22:58:25 -05003108 if (base)
3109 fput(base);
Al Viro8b411372015-05-08 22:53:15 -04003110out2:
Al Viro70806c32012-06-22 12:41:10 +04003111 if (!(opened & FILE_OPENED)) {
3112 BUG_ON(!error);
Al Virob2ebdc52012-06-22 12:40:19 +04003113 put_filp(file);
Miklos Szeredi2f80ad32012-06-05 15:10:27 +02003114 }
Al Viro70806c32012-06-22 12:41:10 +04003115 if (unlikely(error)) {
3116 if (error == -EOPENSTALE) {
3117 if (flags & LOOKUP_RCU)
3118 error = -ECHILD;
3119 else
3120 error = -ESTALE;
3121 }
3122 file = ERR_PTR(error);
3123 }
3124 return file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125}
3126
Jeff Layton631423f2012-10-10 16:43:10 -04003127struct file *do_filp_open(int dfd, struct filename *pathname,
Al Viro13aab422011-02-23 17:54:08 -05003128 const struct open_flags *op, int flags)
3129{
Al Viro73d049a2011-03-11 12:08:24 -05003130 struct nameidata nd;
Al Viro13aab422011-02-23 17:54:08 -05003131 struct file *filp;
3132
Al Viro73d049a2011-03-11 12:08:24 -05003133 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
Al Viro13aab422011-02-23 17:54:08 -05003134 if (unlikely(filp == ERR_PTR(-ECHILD)))
Al Viro73d049a2011-03-11 12:08:24 -05003135 filp = path_openat(dfd, pathname, &nd, op, flags);
Al Viro13aab422011-02-23 17:54:08 -05003136 if (unlikely(filp == ERR_PTR(-ESTALE)))
Al Viro73d049a2011-03-11 12:08:24 -05003137 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
Al Viro13aab422011-02-23 17:54:08 -05003138 return filp;
3139}
3140
Al Viro73d049a2011-03-11 12:08:24 -05003141struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
3142 const char *name, const struct open_flags *op, int flags)
3143{
3144 struct nameidata nd;
3145 struct file *file;
Jeff Layton631423f2012-10-10 16:43:10 -04003146 struct filename filename = { .name = name };
Al Viro73d049a2011-03-11 12:08:24 -05003147
3148 nd.root.mnt = mnt;
3149 nd.root.dentry = dentry;
3150
3151 flags |= LOOKUP_ROOT;
3152
Al Virobcda7652011-03-13 16:42:14 -04003153 if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
Al Viro73d049a2011-03-11 12:08:24 -05003154 return ERR_PTR(-ELOOP);
3155
Jeff Layton631423f2012-10-10 16:43:10 -04003156 file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_RCU);
Al Viro73d049a2011-03-11 12:08:24 -05003157 if (unlikely(file == ERR_PTR(-ECHILD)))
Jeff Layton631423f2012-10-10 16:43:10 -04003158 file = path_openat(-1, &filename, &nd, op, flags);
Al Viro73d049a2011-03-11 12:08:24 -05003159 if (unlikely(file == ERR_PTR(-ESTALE)))
Jeff Layton631423f2012-10-10 16:43:10 -04003160 file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_REVAL);
Al Viro73d049a2011-03-11 12:08:24 -05003161 return file;
3162}
3163
Al Viroed75e952011-06-27 16:53:43 -04003164struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003166 struct dentry *dentry = ERR_PTR(-EEXIST);
Al Viroed75e952011-06-27 16:53:43 -04003167 struct nameidata nd;
Jan Kara7d140392012-06-12 16:20:30 +02003168 int err2;
Al Viroed75e952011-06-27 16:53:43 -04003169 int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
3170 if (error)
3171 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003173 /*
3174 * Yucky last component or no last component at all?
3175 * (foo/., foo/.., /////)
3176 */
Al Viroed75e952011-06-27 16:53:43 -04003177 if (nd.last_type != LAST_NORM)
3178 goto out;
3179 nd.flags &= ~LOOKUP_PARENT;
3180 nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003181
Jan Kara7d140392012-06-12 16:20:30 +02003182 /* don't fail immediately if it's r/o, at least try to report other errors */
3183 err2 = mnt_want_write(nd.path.mnt);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003184 /*
3185 * Do the final lookup.
3186 */
Al Viroed75e952011-06-27 16:53:43 -04003187 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3188 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 if (IS_ERR(dentry))
Al Viroeb8b9ae2012-07-20 02:25:00 +04003190 goto unlock;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003191
Al Viroeb8b9ae2012-07-20 02:25:00 +04003192 error = -EEXIST;
Al Viroe9baf6e2008-05-15 04:49:12 -04003193 if (dentry->d_inode)
Al Viroeb8b9ae2012-07-20 02:25:00 +04003194 goto fail;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003195 /*
3196 * Special case - lookup gave negative, but... we had foo/bar/
3197 * From the vfs_mknod() POV we just have a negative dentry -
3198 * all is fine. Let's be bastards - you had / on the end, you've
3199 * been asking for (non-existent) directory. -ENOENT for you.
3200 */
Al Viroed75e952011-06-27 16:53:43 -04003201 if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
Al Viroeb8b9ae2012-07-20 02:25:00 +04003202 error = -ENOENT;
Al Viroed75e952011-06-27 16:53:43 -04003203 goto fail;
Al Viroe9baf6e2008-05-15 04:49:12 -04003204 }
Jan Kara7d140392012-06-12 16:20:30 +02003205 if (unlikely(err2)) {
3206 error = err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 goto fail;
3208 }
Al Viroed75e952011-06-27 16:53:43 -04003209 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211fail:
Al Viroeb8b9ae2012-07-20 02:25:00 +04003212 dput(dentry);
3213 dentry = ERR_PTR(error);
3214unlock:
Al Viroed75e952011-06-27 16:53:43 -04003215 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Kara7d140392012-06-12 16:20:30 +02003216 if (!err2)
3217 mnt_drop_write(nd.path.mnt);
Al Viroed75e952011-06-27 16:53:43 -04003218out:
3219 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 return dentry;
3221}
Al Virodae6ad82011-06-26 11:50:15 -04003222EXPORT_SYMBOL(kern_path_create);
3223
Al Viro58cc3382012-07-20 01:15:31 +04003224void done_path_create(struct path *path, struct dentry *dentry)
3225{
3226 dput(dentry);
3227 mutex_unlock(&path->dentry->d_inode->i_mutex);
Al Viroeb8b9ae2012-07-20 02:25:00 +04003228 mnt_drop_write(path->mnt);
Al Viro58cc3382012-07-20 01:15:31 +04003229 path_put(path);
3230}
3231EXPORT_SYMBOL(done_path_create);
3232
Al Virodae6ad82011-06-26 11:50:15 -04003233struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir)
3234{
Jeff Layton9cee5ca2012-10-10 15:25:28 -04003235 struct filename *tmp = getname(pathname);
Al Virodae6ad82011-06-26 11:50:15 -04003236 struct dentry *res;
3237 if (IS_ERR(tmp))
3238 return ERR_CAST(tmp);
Jeff Layton9cee5ca2012-10-10 15:25:28 -04003239 res = kern_path_create(dfd, tmp->name, path, is_dir);
Al Virodae6ad82011-06-26 11:50:15 -04003240 putname(tmp);
3241 return res;
3242}
3243EXPORT_SYMBOL(user_path_create);
3244
Daniel Rosenberg17285112016-10-26 16:27:45 -07003245int vfs_mknod2(struct vfsmount *mnt, struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246{
Daniel Rosenberg17285112016-10-26 16:27:45 -07003247 int error = may_create(mnt, dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248
3249 if (error)
3250 return error;
3251
Eric W. Biedermanb95e2122011-11-13 12:16:43 -08003252 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 return -EPERM;
3254
Al Viroacfa4382008-12-04 10:06:33 -05003255 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 return -EPERM;
3257
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07003258 error = devcgroup_inode_mknod(mode, dev);
3259 if (error)
3260 return error;
3261
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 error = security_inode_mknod(dir, dentry, mode, dev);
3263 if (error)
3264 return error;
3265
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 error = dir->i_op->mknod(dir, dentry, mode, dev);
Amir Samuelov6a22e462014-05-26 11:44:06 +03003267 if (error)
3268 return error;
3269
3270 error = security_inode_post_create(dir, dentry, mode);
3271 if (error)
3272 return error;
3273
Stephen Smalleya74574a2005-09-09 13:01:44 -07003274 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003275 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 return error;
3277}
Daniel Rosenberg17285112016-10-26 16:27:45 -07003278EXPORT_SYMBOL(vfs_mknod2);
3279
3280int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
3281{
3282 return vfs_mknod2(NULL, dir, dentry, mode, dev);
3283}
3284EXPORT_SYMBOL(vfs_mknod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285
Al Virof69aac02011-07-26 04:31:40 -04003286static int may_mknod(umode_t mode)
Dave Hansen463c3192008-02-15 14:37:57 -08003287{
3288 switch (mode & S_IFMT) {
3289 case S_IFREG:
3290 case S_IFCHR:
3291 case S_IFBLK:
3292 case S_IFIFO:
3293 case S_IFSOCK:
3294 case 0: /* zero mode translates to S_IFREG */
3295 return 0;
3296 case S_IFDIR:
3297 return -EPERM;
3298 default:
3299 return -EINVAL;
3300 }
3301}
3302
Al Viro8208a222011-07-25 17:32:17 -04003303SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003304 unsigned, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305{
Al Viro2ad94ae2008-07-21 09:32:51 -04003306 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003307 struct path path;
3308 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309
Al Viro1db2b392012-07-20 01:17:26 +04003310 error = may_mknod(mode);
3311 if (error)
3312 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313
Al Virodae6ad82011-06-26 11:50:15 -04003314 dentry = user_path_create(dfd, filename, &path, 0);
3315 if (IS_ERR(dentry))
3316 return PTR_ERR(dentry);
Al Viro2ad94ae2008-07-21 09:32:51 -04003317
Al Virodae6ad82011-06-26 11:50:15 -04003318 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003319 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003320 error = security_path_mknod(&path, dentry, mode, dev);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003321 if (error)
Al Viroeb8b9ae2012-07-20 02:25:00 +04003322 goto out;
Dave Hansen463c3192008-02-15 14:37:57 -08003323 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 case 0: case S_IFREG:
Al Viro8da0eaf2012-06-10 18:09:36 -04003325 error = vfs_create2(path.mnt, path.dentry->d_inode,dentry,mode,true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 break;
3327 case S_IFCHR: case S_IFBLK:
Daniel Rosenberg17285112016-10-26 16:27:45 -07003328 error = vfs_mknod2(path.mnt, path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 new_decode_dev(dev));
3330 break;
3331 case S_IFIFO: case S_IFSOCK:
Al Virodae6ad82011-06-26 11:50:15 -04003332 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 }
Al Viroeb8b9ae2012-07-20 02:25:00 +04003335out:
Al Viro58cc3382012-07-20 01:15:31 +04003336 done_path_create(&path, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337
3338 return error;
3339}
3340
Al Viro8208a222011-07-25 17:32:17 -04003341SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003342{
3343 return sys_mknodat(AT_FDCWD, filename, mode, dev);
3344}
3345
Daniel Rosenberg17285112016-10-26 16:27:45 -07003346int vfs_mkdir2(struct vfsmount *mnt, struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347{
Daniel Rosenberg17285112016-10-26 16:27:45 -07003348 int error = may_create(mnt, dir, dentry);
Al Viro8de52772012-02-06 12:45:27 -05003349 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350
3351 if (error)
3352 return error;
3353
Al Viroacfa4382008-12-04 10:06:33 -05003354 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 return -EPERM;
3356
3357 mode &= (S_IRWXUGO|S_ISVTX);
3358 error = security_inode_mkdir(dir, dentry, mode);
3359 if (error)
3360 return error;
3361
Al Viro8de52772012-02-06 12:45:27 -05003362 if (max_links && dir->i_nlink >= max_links)
3363 return -EMLINK;
3364
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003366 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003367 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 return error;
3369}
Daniel Rosenberg17285112016-10-26 16:27:45 -07003370EXPORT_SYMBOL(vfs_mkdir2);
3371
3372int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
3373{
3374 return vfs_mkdir2(NULL, dir, dentry, mode);
3375}
3376EXPORT_SYMBOL(vfs_mkdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377
Al Viroa218d0f2011-11-21 14:59:34 -05003378SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379{
Dave Hansen6902d922006-09-30 23:29:01 -07003380 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003381 struct path path;
3382 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383
Al Virodae6ad82011-06-26 11:50:15 -04003384 dentry = user_path_create(dfd, pathname, &path, 1);
Dave Hansen6902d922006-09-30 23:29:01 -07003385 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003386 return PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003387
Al Virodae6ad82011-06-26 11:50:15 -04003388 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003389 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003390 error = security_path_mkdir(&path, dentry, mode);
Al Viroeb8b9ae2012-07-20 02:25:00 +04003391 if (!error)
3392 error = vfs_mkdir2(path.mnt, path.dentry->d_inode, dentry, mode);
Al Viro58cc3382012-07-20 01:15:31 +04003393 done_path_create(&path, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 return error;
3395}
3396
Al Viroa218d0f2011-11-21 14:59:34 -05003397SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003398{
3399 return sys_mkdirat(AT_FDCWD, pathname, mode);
3400}
3401
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402/*
Sage Weila71905f2011-05-24 13:06:08 -07003403 * The dentry_unhash() helper will try to drop the dentry early: we
J. Bruce Fieldsc0d02592012-02-15 11:48:40 -05003404 * should have a usage count of 1 if we're the only user of this
Sage Weila71905f2011-05-24 13:06:08 -07003405 * dentry, and if that is true (possibly after pruning the dcache),
3406 * then we drop the dentry now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 *
3408 * A low-level filesystem can, if it choses, legally
3409 * do a
3410 *
3411 * if (!d_unhashed(dentry))
3412 * return -EBUSY;
3413 *
3414 * if it cannot handle the case of removing a directory
3415 * that is still in use by something else..
3416 */
3417void dentry_unhash(struct dentry *dentry)
3418{
Vasily Averindc168422006-12-06 20:37:07 -08003419 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 spin_lock(&dentry->d_lock);
Sage Weil64252c72011-05-24 13:06:05 -07003421 if (dentry->d_count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 __d_drop(dentry);
3423 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424}
3425
Daniel Rosenberg17285112016-10-26 16:27:45 -07003426int vfs_rmdir2(struct vfsmount *mnt, struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427{
Daniel Rosenberg17285112016-10-26 16:27:45 -07003428 int error = may_delete(mnt, dir, dentry, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429
3430 if (error)
3431 return error;
3432
Al Viroacfa4382008-12-04 10:06:33 -05003433 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 return -EPERM;
3435
Al Viro1d2ef592011-09-14 18:55:41 +01003436 dget(dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003437 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438
Sage Weil912dbc12011-05-24 13:06:11 -07003439 error = -EBUSY;
3440 if (d_mountpoint(dentry))
3441 goto out;
3442
3443 error = security_inode_rmdir(dir, dentry);
3444 if (error)
3445 goto out;
3446
Sage Weil3cebde22011-05-29 21:20:59 -07003447 shrink_dcache_parent(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003448 error = dir->i_op->rmdir(dir, dentry);
3449 if (error)
3450 goto out;
3451
3452 dentry->d_inode->i_flags |= S_DEAD;
3453 dont_mount(dentry);
3454
3455out:
3456 mutex_unlock(&dentry->d_inode->i_mutex);
Al Viro1d2ef592011-09-14 18:55:41 +01003457 dput(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003458 if (!error)
3459 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 return error;
3461}
Daniel Rosenberg17285112016-10-26 16:27:45 -07003462EXPORT_SYMBOL(vfs_rmdir2);
3463
3464int vfs_rmdir(struct inode *dir, struct dentry *dentry)
3465{
3466 return vfs_rmdir2(NULL, dir, dentry);
3467}
3468EXPORT_SYMBOL(vfs_rmdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003470static long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471{
3472 int error = 0;
Jeff Layton9cee5ca2012-10-10 15:25:28 -04003473 struct filename *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 struct dentry *dentry;
3475 struct nameidata nd;
3476
Jeff Layton9cee5ca2012-10-10 15:25:28 -04003477 name = user_path_parent(dfd, pathname, &nd);
3478 if (IS_ERR(name))
3479 return PTR_ERR(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480
3481 switch(nd.last_type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003482 case LAST_DOTDOT:
3483 error = -ENOTEMPTY;
3484 goto exit1;
3485 case LAST_DOT:
3486 error = -EINVAL;
3487 goto exit1;
3488 case LAST_ROOT:
3489 error = -EBUSY;
3490 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003492
3493 nd.flags &= ~LOOKUP_PARENT;
Jan Kara7d140392012-06-12 16:20:30 +02003494 error = mnt_want_write(nd.path.mnt);
3495 if (error)
3496 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003497
Jan Blunck4ac91372008-02-14 19:34:32 -08003498 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08003499 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003501 if (IS_ERR(dentry))
3502 goto exit2;
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04003503 if (!dentry->d_inode) {
3504 error = -ENOENT;
3505 goto exit3;
3506 }
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003507 error = security_path_rmdir(&nd.path, dentry);
3508 if (error)
Jan Kara7d140392012-06-12 16:20:30 +02003509 goto exit3;
Daniel Rosenberg17285112016-10-26 16:27:45 -07003510 error = vfs_rmdir2(nd.path.mnt, nd.path.dentry->d_inode, dentry);
Dave Hansen06227532008-02-15 14:37:34 -08003511exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07003512 dput(dentry);
3513exit2:
Jan Blunck4ac91372008-02-14 19:34:32 -08003514 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Kara7d140392012-06-12 16:20:30 +02003515 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003517 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 putname(name);
3519 return error;
3520}
3521
Heiko Carstens3cdad422009-01-14 14:14:22 +01003522SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003523{
3524 return do_rmdir(AT_FDCWD, pathname);
3525}
3526
Daniel Rosenberg17285112016-10-26 16:27:45 -07003527int vfs_unlink2(struct vfsmount *mnt, struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528{
Daniel Rosenberg17285112016-10-26 16:27:45 -07003529 int error = may_delete(mnt, dir, dentry, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530
3531 if (error)
3532 return error;
3533
Al Viroacfa4382008-12-04 10:06:33 -05003534 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 return -EPERM;
3536
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003537 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 if (d_mountpoint(dentry))
3539 error = -EBUSY;
3540 else {
3541 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05003542 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 error = dir->i_op->unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05003544 if (!error)
Al Virod83c49f2010-04-30 17:17:09 -04003545 dont_mount(dentry);
Al Virobec10522010-03-03 14:12:08 -05003546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003548 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549
3550 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
3551 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
Jan Karaece95912008-02-06 01:37:13 -08003552 fsnotify_link_count(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 }
Robert Love0eeca282005-07-12 17:06:03 -04003555
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 return error;
3557}
Daniel Rosenberg17285112016-10-26 16:27:45 -07003558EXPORT_SYMBOL(vfs_unlink2);
3559
3560int vfs_unlink(struct inode *dir, struct dentry *dentry)
3561{
3562 return vfs_unlink2(NULL, dir, dentry);
3563}
3564EXPORT_SYMBOL(vfs_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565
3566/*
3567 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003568 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 * writeout happening, and we don't want to prevent access to the directory
3570 * while waiting on the I/O.
3571 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003572static long do_unlinkat(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573{
Al Viro2ad94ae2008-07-21 09:32:51 -04003574 int error;
Jeff Layton9cee5ca2012-10-10 15:25:28 -04003575 struct filename *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 struct dentry *dentry;
3577 struct nameidata nd;
3578 struct inode *inode = NULL;
3579
Jeff Layton9cee5ca2012-10-10 15:25:28 -04003580 name = user_path_parent(dfd, pathname, &nd);
3581 if (IS_ERR(name))
3582 return PTR_ERR(name);
Al Viro2ad94ae2008-07-21 09:32:51 -04003583
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 error = -EISDIR;
3585 if (nd.last_type != LAST_NORM)
3586 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003587
3588 nd.flags &= ~LOOKUP_PARENT;
Jan Kara7d140392012-06-12 16:20:30 +02003589 error = mnt_want_write(nd.path.mnt);
3590 if (error)
3591 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003592
Jan Blunck4ac91372008-02-14 19:34:32 -08003593 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08003594 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 error = PTR_ERR(dentry);
3596 if (!IS_ERR(dentry)) {
3597 /* Why not before? Because we want correct error value */
Török Edwin50338b82011-06-16 00:06:14 +03003598 if (nd.last.name[nd.last.len])
3599 goto slashes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 inode = dentry->d_inode;
Török Edwin50338b82011-06-16 00:06:14 +03003601 if (!inode)
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04003602 goto slashes;
3603 ihold(inode);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003604 error = security_path_unlink(&nd.path, dentry);
3605 if (error)
Jan Kara7d140392012-06-12 16:20:30 +02003606 goto exit2;
Daniel Rosenberg17285112016-10-26 16:27:45 -07003607 error = vfs_unlink2(nd.path.mnt, nd.path.dentry->d_inode, dentry);
Jan Kara7d140392012-06-12 16:20:30 +02003608exit2:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 dput(dentry);
3610 }
Jan Blunck4ac91372008-02-14 19:34:32 -08003611 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 if (inode)
3613 iput(inode); /* truncate the inode here */
Jan Kara7d140392012-06-12 16:20:30 +02003614 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003616 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 putname(name);
3618 return error;
3619
3620slashes:
3621 error = !dentry->d_inode ? -ENOENT :
3622 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
3623 goto exit2;
3624}
3625
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003626SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003627{
3628 if ((flag & ~AT_REMOVEDIR) != 0)
3629 return -EINVAL;
3630
3631 if (flag & AT_REMOVEDIR)
3632 return do_rmdir(dfd, pathname);
3633
3634 return do_unlinkat(dfd, pathname);
3635}
3636
Heiko Carstens3480b252009-01-14 14:14:16 +01003637SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003638{
3639 return do_unlinkat(AT_FDCWD, pathname);
3640}
3641
Daniel Rosenberg17285112016-10-26 16:27:45 -07003642int vfs_symlink2(struct vfsmount *mnt, struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643{
Daniel Rosenberg17285112016-10-26 16:27:45 -07003644 int error = may_create(mnt, dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645
3646 if (error)
3647 return error;
3648
Al Viroacfa4382008-12-04 10:06:33 -05003649 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 return -EPERM;
3651
3652 error = security_inode_symlink(dir, dentry, oldname);
3653 if (error)
3654 return error;
3655
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003657 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003658 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 return error;
3660}
Daniel Rosenberg17285112016-10-26 16:27:45 -07003661EXPORT_SYMBOL(vfs_symlink2);
3662
3663int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
3664{
3665 return vfs_symlink2(NULL, dir, dentry, oldname);
3666}
3667EXPORT_SYMBOL(vfs_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003669SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
3670 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671{
Al Viro2ad94ae2008-07-21 09:32:51 -04003672 int error;
Jeff Layton9cee5ca2012-10-10 15:25:28 -04003673 struct filename *from;
Dave Hansen6902d922006-09-30 23:29:01 -07003674 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003675 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676
3677 from = getname(oldname);
Al Viro2ad94ae2008-07-21 09:32:51 -04003678 if (IS_ERR(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 return PTR_ERR(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04003680
Al Virodae6ad82011-06-26 11:50:15 -04003681 dentry = user_path_create(newdfd, newname, &path, 0);
Dave Hansen6902d922006-09-30 23:29:01 -07003682 error = PTR_ERR(dentry);
3683 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003684 goto out_putname;
Dave Hansen6902d922006-09-30 23:29:01 -07003685
Jeff Layton9cee5ca2012-10-10 15:25:28 -04003686 error = security_path_symlink(&path, dentry, from->name);
Al Viroeb8b9ae2012-07-20 02:25:00 +04003687 if (!error)
Jeff Layton9cee5ca2012-10-10 15:25:28 -04003688 error = vfs_symlink2(path.mnt, path.dentry->d_inode, dentry, from->name);
Al Viro58cc3382012-07-20 01:15:31 +04003689 done_path_create(&path, dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003690out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 putname(from);
3692 return error;
3693}
3694
Heiko Carstens3480b252009-01-14 14:14:16 +01003695SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003696{
3697 return sys_symlinkat(oldname, AT_FDCWD, newname);
3698}
3699
Daniel Rosenberg17285112016-10-26 16:27:45 -07003700int vfs_link2(struct vfsmount *mnt, struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701{
3702 struct inode *inode = old_dentry->d_inode;
Al Viro8de52772012-02-06 12:45:27 -05003703 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 int error;
3705
3706 if (!inode)
3707 return -ENOENT;
3708
Daniel Rosenberg17285112016-10-26 16:27:45 -07003709 error = may_create(mnt, dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 if (error)
3711 return error;
3712
3713 if (dir->i_sb != inode->i_sb)
3714 return -EXDEV;
3715
3716 /*
3717 * A link to an append-only or immutable file cannot be created.
3718 */
3719 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3720 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05003721 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003723 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 return -EPERM;
3725
3726 error = security_inode_link(old_dentry, dir, new_dentry);
3727 if (error)
3728 return error;
3729
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003730 mutex_lock(&inode->i_mutex);
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05303731 /* Make sure we don't allow creating hardlink to an unlinked file */
Al Virof6f34262013-06-11 08:34:36 +04003732 if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05303733 error = -ENOENT;
Al Viro8de52772012-02-06 12:45:27 -05003734 else if (max_links && inode->i_nlink >= max_links)
3735 error = -EMLINK;
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05303736 else
3737 error = dir->i_op->link(old_dentry, dir, new_dentry);
Al Virof6f34262013-06-11 08:34:36 +04003738
3739 if (!error && (inode->i_state & I_LINKABLE)) {
3740 spin_lock(&inode->i_lock);
3741 inode->i_state &= ~I_LINKABLE;
3742 spin_unlock(&inode->i_lock);
3743 }
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003744 mutex_unlock(&inode->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07003745 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003746 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 return error;
3748}
Daniel Rosenberg17285112016-10-26 16:27:45 -07003749EXPORT_SYMBOL(vfs_link2);
3750
3751int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
3752{
3753 return vfs_link2(NULL, old_dentry, dir, new_dentry);
3754}
3755EXPORT_SYMBOL(vfs_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756
3757/*
3758 * Hardlinks are often used in delicate situations. We avoid
3759 * security-related surprises by not following symlinks on the
3760 * newname. --KAB
3761 *
3762 * We don't follow them on the oldname either to be compatible
3763 * with linux 2.0, and to avoid hard-linking to directories
3764 * and other special files. --ADM
3765 */
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003766SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
3767 int, newdfd, const char __user *, newname, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768{
3769 struct dentry *new_dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003770 struct path old_path, new_path;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303771 int how = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303774 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003775 return -EINVAL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303776 /*
3777 * To use null names we require CAP_DAC_READ_SEARCH
3778 * This ensures that not everyone will be able to create
3779 * handlink using the passed filedescriptor.
3780 */
3781 if (flags & AT_EMPTY_PATH) {
3782 if (!capable(CAP_DAC_READ_SEARCH))
3783 return -ENOENT;
3784 how = LOOKUP_EMPTY;
3785 }
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003786
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303787 if (flags & AT_SYMLINK_FOLLOW)
3788 how |= LOOKUP_FOLLOW;
3789
3790 error = user_path_at(olddfd, oldname, how, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04003792 return error;
3793
Al Virodae6ad82011-06-26 11:50:15 -04003794 new_dentry = user_path_create(newdfd, newname, &new_path, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003796 if (IS_ERR(new_dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003797 goto out;
3798
3799 error = -EXDEV;
3800 if (old_path.mnt != new_path.mnt)
3801 goto out_dput;
Kees Cook5092e9c2012-07-25 17:29:07 -07003802 error = may_linkat(&old_path);
3803 if (unlikely(error))
Dave Hansen75c3f292008-02-15 14:37:45 -08003804 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04003805 error = security_path_link(old_path.dentry, &new_path, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003806 if (error)
Al Viroeb8b9ae2012-07-20 02:25:00 +04003807 goto out_dput;
Daniel Rosenberg17285112016-10-26 16:27:45 -07003808 error = vfs_link2(old_path.mnt, old_path.dentry, new_path.dentry->d_inode, new_dentry);
Dave Hansen75c3f292008-02-15 14:37:45 -08003809out_dput:
Al Viro58cc3382012-07-20 01:15:31 +04003810 done_path_create(&new_path, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811out:
Al Viro2d8f3032008-07-22 09:59:21 -04003812 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813
3814 return error;
3815}
3816
Heiko Carstens3480b252009-01-14 14:14:16 +01003817SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003818{
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003819 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003820}
3821
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822/*
3823 * The worst of all namespace operations - renaming directory. "Perverted"
3824 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
3825 * Problems:
3826 * a) we can get into loop creation. Check is done in is_subdir().
3827 * b) race potential - two innocent renames can create a loop together.
3828 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08003829 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 * story.
3831 * c) we have to lock _three_ objects - parents and victim (if it exists).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003832 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 * whether the target exists). Solution: try to be smart with locking
3834 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08003835 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 * move will be locked. Thus we can rank directories by the tree
3837 * (ancestors first) and rank all non-directories after them.
3838 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08003839 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 * HOWEVER, it relies on the assumption that any object with ->lookup()
3841 * has no more than 1 dentry. If "hybrid" objects will ever appear,
3842 * we'd better make sure that there's no link(2) for them.
Sage Weile4eaac02011-05-24 13:06:07 -07003843 * d) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003844 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05003846 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 * locking].
3848 */
Daniel Rosenberg17285112016-10-26 16:27:45 -07003849static int vfs_rename_dir(struct vfsmount *mnt,
3850 struct inode *old_dir, struct dentry *old_dentry,
3851 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852{
3853 int error = 0;
Sage Weil9055cba2011-05-24 13:06:12 -07003854 struct inode *target = new_dentry->d_inode;
Al Viro8de52772012-02-06 12:45:27 -05003855 unsigned max_links = new_dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856
3857 /*
3858 * If we are going to change the parent - check write permissions,
3859 * we'll need to flip '..'.
3860 */
3861 if (new_dir != old_dir) {
Daniel Rosenberg17285112016-10-26 16:27:45 -07003862 error = inode_permission2(mnt, old_dentry->d_inode, MAY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 if (error)
3864 return error;
3865 }
3866
3867 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3868 if (error)
3869 return error;
3870
Al Viro1d2ef592011-09-14 18:55:41 +01003871 dget(new_dentry);
Al Virod83c49f2010-04-30 17:17:09 -04003872 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003873 mutex_lock(&target->i_mutex);
Sage Weil9055cba2011-05-24 13:06:12 -07003874
3875 error = -EBUSY;
3876 if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
3877 goto out;
3878
Al Viro8de52772012-02-06 12:45:27 -05003879 error = -EMLINK;
3880 if (max_links && !target && new_dir != old_dir &&
3881 new_dir->i_nlink >= max_links)
3882 goto out;
3883
Sage Weil3cebde22011-05-29 21:20:59 -07003884 if (target)
3885 shrink_dcache_parent(new_dentry);
Sage Weil9055cba2011-05-24 13:06:12 -07003886 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3887 if (error)
3888 goto out;
3889
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 if (target) {
Sage Weil9055cba2011-05-24 13:06:12 -07003891 target->i_flags |= S_DEAD;
3892 dont_mount(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 }
Sage Weil9055cba2011-05-24 13:06:12 -07003894out:
3895 if (target)
3896 mutex_unlock(&target->i_mutex);
Al Viro1d2ef592011-09-14 18:55:41 +01003897 dput(new_dentry);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07003898 if (!error)
Mark Fasheh349457c2006-09-08 14:22:21 -07003899 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3900 d_move(old_dentry,new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 return error;
3902}
3903
Adrian Bunk75c96f82005-05-05 16:16:09 -07003904static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
3905 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906{
Sage Weil51892bb2011-05-24 13:06:13 -07003907 struct inode *target = new_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 int error;
3909
3910 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3911 if (error)
3912 return error;
3913
3914 dget(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003916 mutex_lock(&target->i_mutex);
Sage Weil51892bb2011-05-24 13:06:13 -07003917
3918 error = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
Sage Weil51892bb2011-05-24 13:06:13 -07003920 goto out;
3921
3922 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3923 if (error)
3924 goto out;
3925
3926 if (target)
3927 dont_mount(new_dentry);
3928 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3929 d_move(old_dentry, new_dentry);
3930out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003932 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 dput(new_dentry);
3934 return error;
3935}
3936
Daniel Rosenberg17285112016-10-26 16:27:45 -07003937int vfs_rename2(struct vfsmount *mnt,
3938 struct inode *old_dir, struct dentry *old_dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 struct inode *new_dir, struct dentry *new_dentry)
3940{
3941 int error;
3942 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
Al Viro6e71d852017-07-07 14:51:19 -04003943 struct name_snapshot old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944
3945 if (old_dentry->d_inode == new_dentry->d_inode)
3946 return 0;
3947
Daniel Rosenberg17285112016-10-26 16:27:45 -07003948 error = may_delete(mnt, old_dir, old_dentry, is_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 if (error)
3950 return error;
3951
3952 if (!new_dentry->d_inode)
Daniel Rosenberg17285112016-10-26 16:27:45 -07003953 error = may_create(mnt, new_dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 else
Daniel Rosenberg17285112016-10-26 16:27:45 -07003955 error = may_delete(mnt, new_dir, new_dentry, is_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 if (error)
3957 return error;
3958
Al Viroacfa4382008-12-04 10:06:33 -05003959 if (!old_dir->i_op->rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 return -EPERM;
3961
Al Viro6e71d852017-07-07 14:51:19 -04003962 take_dentry_name_snapshot(&old_name, old_dentry);
Robert Love0eeca282005-07-12 17:06:03 -04003963
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 if (is_dir)
Daniel Rosenberg17285112016-10-26 16:27:45 -07003965 error = vfs_rename_dir(mnt, old_dir,old_dentry,new_dir,new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 else
3967 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
Al Viro123df292009-12-25 04:57:57 -05003968 if (!error)
Al Viro6e71d852017-07-07 14:51:19 -04003969 fsnotify_move(old_dir, new_dir, old_name.name, is_dir,
Al Viro5a190ae2007-06-07 12:19:32 -04003970 new_dentry->d_inode, old_dentry);
Al Viro6e71d852017-07-07 14:51:19 -04003971 release_dentry_name_snapshot(&old_name);
Robert Love0eeca282005-07-12 17:06:03 -04003972
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 return error;
3974}
Daniel Rosenberg17285112016-10-26 16:27:45 -07003975EXPORT_SYMBOL(vfs_rename2);
3976
3977int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
3978 struct inode *new_dir, struct dentry *new_dentry)
3979{
3980 return vfs_rename2(NULL, old_dir, old_dentry, new_dir, new_dentry);
3981}
3982EXPORT_SYMBOL(vfs_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003984SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
3985 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986{
Al Viro2ad94ae2008-07-21 09:32:51 -04003987 struct dentry *old_dir, *new_dir;
3988 struct dentry *old_dentry, *new_dentry;
3989 struct dentry *trap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 struct nameidata oldnd, newnd;
Jeff Layton9cee5ca2012-10-10 15:25:28 -04003991 struct filename *from;
3992 struct filename *to;
Al Viro2ad94ae2008-07-21 09:32:51 -04003993 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994
Jeff Layton9cee5ca2012-10-10 15:25:28 -04003995 from = user_path_parent(olddfd, oldname, &oldnd);
3996 if (IS_ERR(from)) {
3997 error = PTR_ERR(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 goto exit;
Jeff Layton9cee5ca2012-10-10 15:25:28 -04003999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000
Jeff Layton9cee5ca2012-10-10 15:25:28 -04004001 to = user_path_parent(newdfd, newname, &newnd);
4002 if (IS_ERR(to)) {
4003 error = PTR_ERR(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 goto exit1;
Jeff Layton9cee5ca2012-10-10 15:25:28 -04004005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006
4007 error = -EXDEV;
Jan Blunck4ac91372008-02-14 19:34:32 -08004008 if (oldnd.path.mnt != newnd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 goto exit2;
4010
Jan Blunck4ac91372008-02-14 19:34:32 -08004011 old_dir = oldnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 error = -EBUSY;
4013 if (oldnd.last_type != LAST_NORM)
4014 goto exit2;
4015
Jan Blunck4ac91372008-02-14 19:34:32 -08004016 new_dir = newnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 if (newnd.last_type != LAST_NORM)
4018 goto exit2;
4019
Jan Kara7d140392012-06-12 16:20:30 +02004020 error = mnt_want_write(oldnd.path.mnt);
4021 if (error)
4022 goto exit2;
4023
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004024 oldnd.flags &= ~LOOKUP_PARENT;
4025 newnd.flags &= ~LOOKUP_PARENT;
OGAWA Hirofumi4e9ed2f2008-10-16 07:50:29 +09004026 newnd.flags |= LOOKUP_RENAME_TARGET;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004027
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 trap = lock_rename(new_dir, old_dir);
4029
Christoph Hellwig49705b72005-11-08 21:35:06 -08004030 old_dentry = lookup_hash(&oldnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 error = PTR_ERR(old_dentry);
4032 if (IS_ERR(old_dentry))
4033 goto exit3;
4034 /* source must exist */
4035 error = -ENOENT;
4036 if (!old_dentry->d_inode)
4037 goto exit4;
4038 /* unless the source is a directory trailing slashes give -ENOTDIR */
4039 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
4040 error = -ENOTDIR;
4041 if (oldnd.last.name[oldnd.last.len])
4042 goto exit4;
4043 if (newnd.last.name[newnd.last.len])
4044 goto exit4;
4045 }
4046 /* source should not be ancestor of target */
4047 error = -EINVAL;
4048 if (old_dentry == trap)
4049 goto exit4;
Christoph Hellwig49705b72005-11-08 21:35:06 -08004050 new_dentry = lookup_hash(&newnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 error = PTR_ERR(new_dentry);
4052 if (IS_ERR(new_dentry))
4053 goto exit4;
4054 /* target should not be an ancestor of source */
4055 error = -ENOTEMPTY;
4056 if (new_dentry == trap)
4057 goto exit5;
4058
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004059 error = security_path_rename(&oldnd.path, old_dentry,
4060 &newnd.path, new_dentry);
4061 if (error)
Jan Kara7d140392012-06-12 16:20:30 +02004062 goto exit5;
Daniel Rosenberg17285112016-10-26 16:27:45 -07004063 error = vfs_rename2(oldnd.path.mnt, old_dir->d_inode, old_dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 new_dir->d_inode, new_dentry);
4065exit5:
4066 dput(new_dentry);
4067exit4:
4068 dput(old_dentry);
4069exit3:
4070 unlock_rename(new_dir, old_dir);
Jan Kara7d140392012-06-12 16:20:30 +02004071 mnt_drop_write(oldnd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072exit2:
Jan Blunck1d957f92008-02-14 19:34:35 -08004073 path_put(&newnd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04004074 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08004076 path_put(&oldnd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 putname(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04004078exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 return error;
4080}
4081
Heiko Carstensa26eab22009-01-14 14:14:17 +01004082SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004083{
4084 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
4085}
4086
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
4088{
4089 int len;
4090
4091 len = PTR_ERR(link);
4092 if (IS_ERR(link))
4093 goto out;
4094
4095 len = strlen(link);
4096 if (len > (unsigned) buflen)
4097 len = buflen;
4098 if (copy_to_user(buffer, link, len))
4099 len = -EFAULT;
4100out:
4101 return len;
4102}
4103
4104/*
4105 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
4106 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
4107 * using) it for any given inode is up to filesystem.
4108 */
4109int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4110{
4111 struct nameidata nd;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004112 void *cookie;
Marcin Slusarz694a1762008-06-09 16:40:37 -07004113 int res;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004114
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 nd.depth = 0;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004116 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
Marcin Slusarz694a1762008-06-09 16:40:37 -07004117 if (IS_ERR(cookie))
4118 return PTR_ERR(cookie);
4119
4120 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
4121 if (dentry->d_inode->i_op->put_link)
4122 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
4123 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124}
4125
4126int vfs_follow_link(struct nameidata *nd, const char *link)
4127{
4128 return __vfs_follow_link(nd, link);
4129}
4130
4131/* get the link contents into pagecache */
4132static char *page_getlink(struct dentry * dentry, struct page **ppage)
4133{
Duane Griffinebd09ab2008-12-19 20:47:12 +00004134 char *kaddr;
4135 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 struct address_space *mapping = dentry->d_inode->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -07004137 page = read_mapping_page(mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 if (IS_ERR(page))
Nick Piggin6fe69002007-05-06 14:49:04 -07004139 return (char*)page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 *ppage = page;
Duane Griffinebd09ab2008-12-19 20:47:12 +00004141 kaddr = kmap(page);
4142 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
4143 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144}
4145
4146int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4147{
4148 struct page *page = NULL;
4149 char *s = page_getlink(dentry, &page);
4150 int res = vfs_readlink(dentry,buffer,buflen,s);
4151 if (page) {
4152 kunmap(page);
4153 page_cache_release(page);
4154 }
4155 return res;
4156}
4157
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004158void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004160 struct page *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 nd_set_link(nd, page_getlink(dentry, &page));
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004162 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163}
4164
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004165void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004167 struct page *page = cookie;
4168
4169 if (page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 kunmap(page);
4171 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 }
4173}
4174
Nick Piggin54566b22009-01-04 12:00:53 -08004175/*
4176 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
4177 */
4178int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179{
4180 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004181 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07004182 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08004183 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 char *kaddr;
Nick Piggin54566b22009-01-04 12:00:53 -08004185 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
4186 if (nofs)
4187 flags |= AOP_FLAG_NOFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188
NeilBrown7e53cac2006-03-25 03:07:57 -08004189retry:
Nick Pigginafddba42007-10-16 01:25:01 -07004190 err = pagecache_write_begin(NULL, mapping, 0, len-1,
Nick Piggin54566b22009-01-04 12:00:53 -08004191 flags, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07004193 goto fail;
4194
Cong Wange8e3c3d2011-11-25 23:14:27 +08004195 kaddr = kmap_atomic(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 memcpy(kaddr, symname, len-1);
Cong Wange8e3c3d2011-11-25 23:14:27 +08004197 kunmap_atomic(kaddr);
Nick Pigginafddba42007-10-16 01:25:01 -07004198
4199 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4200 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 if (err < 0)
4202 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07004203 if (err < len-1)
4204 goto retry;
4205
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 mark_inode_dirty(inode);
4207 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208fail:
4209 return err;
4210}
4211
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004212int page_symlink(struct inode *inode, const char *symname, int len)
4213{
4214 return __page_symlink(inode, symname, len,
Nick Piggin54566b22009-01-04 12:00:53 -08004215 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004216}
4217
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08004218const struct inode_operations page_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 .readlink = generic_readlink,
4220 .follow_link = page_follow_link_light,
4221 .put_link = page_put_link,
4222};
4223
Al Viro2d8f3032008-07-22 09:59:21 -04004224EXPORT_SYMBOL(user_path_at);
David Howellscc53ce52011-01-14 18:45:26 +00004225EXPORT_SYMBOL(follow_down_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226EXPORT_SYMBOL(follow_down);
4227EXPORT_SYMBOL(follow_up);
Al Viro66cc1a22012-08-26 12:55:54 -04004228EXPORT_SYMBOL(get_write_access); /* nfsd */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230EXPORT_SYMBOL(page_follow_link_light);
4231EXPORT_SYMBOL(page_put_link);
4232EXPORT_SYMBOL(page_readlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004233EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234EXPORT_SYMBOL(page_symlink);
4235EXPORT_SYMBOL(page_symlink_inode_operations);
Al Virod1811462008-08-02 00:49:18 -04004236EXPORT_SYMBOL(kern_path);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07004237EXPORT_SYMBOL(vfs_path_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238EXPORT_SYMBOL(unlock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239EXPORT_SYMBOL(vfs_follow_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240EXPORT_SYMBOL(generic_permission);
4241EXPORT_SYMBOL(vfs_readlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242EXPORT_SYMBOL(dentry_unhash);
4243EXPORT_SYMBOL(generic_readlink);