blob: e3b23a0a5a5e106dd013587e3b43721295f9eac0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/namei.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7/*
8 * Some corrections by tytso.
9 */
10
11/* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
12 * lookup logic.
13 */
14/* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
15 */
16
17#include <linux/init.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050018#include <linux/export.h>
David S. Miller44696902012-05-23 20:12:50 -070019#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/slab.h>
21#include <linux/fs.h>
22#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/pagemap.h>
Robert Love0eeca282005-07-12 17:06:03 -040024#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/personality.h>
26#include <linux/security.h>
Mimi Zohar6146f0d2009-02-04 09:06:57 -050027#include <linux/ima.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/syscalls.h>
29#include <linux/mount.h>
30#include <linux/audit.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080031#include <linux/capability.h>
Trond Myklebust834f2a42005-10-18 14:20:16 -070032#include <linux/file.h>
Ulrich Drepper5590ff02006-01-18 17:43:53 -080033#include <linux/fcntl.h>
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070034#include <linux/device_cgroup.h>
Al Viro5ad4e532009-03-29 19:50:06 -040035#include <linux/fs_struct.h>
Linus Torvaldse77819e2011-07-22 19:30:19 -070036#include <linux/posix_acl.h>
Linus Torvaldsa8be8af2014-09-13 11:30:10 -070037#include <linux/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/uaccess.h>
39
Eric Parise81e3f42009-12-04 15:47:36 -050040#include "internal.h"
Al Viroc7105362011-11-24 18:22:03 -050041#include "mount.h"
Eric Parise81e3f42009-12-04 15:47:36 -050042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/* [Feb-1997 T. Schoebel-Theuer]
44 * Fundamental changes in the pathname lookup mechanisms (namei)
45 * were necessary because of omirr. The reason is that omirr needs
46 * to know the _real_ pathname, not the user-supplied one, in case
47 * of symlinks (and also when transname replacements occur).
48 *
49 * The new code replaces the old recursive symlink resolution with
50 * an iterative one (in case of non-nested symlink chains). It does
51 * this with calls to <fs>_follow_link().
52 * As a side effect, dir_namei(), _namei() and follow_link() are now
53 * replaced with a single function lookup_dentry() that can handle all
54 * the special cases of the former code.
55 *
56 * With the new dcache, the pathname is stored at each inode, at least as
57 * long as the refcount of the inode is positive. As a side effect, the
58 * size of the dcache depends on the inode cache and thus is dynamic.
59 *
60 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
61 * resolution to correspond with current state of the code.
62 *
63 * Note that the symlink resolution is not *completely* iterative.
64 * There is still a significant amount of tail- and mid- recursion in
65 * the algorithm. Also, note that <fs>_readlink() is not used in
66 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
67 * may return different results than <fs>_follow_link(). Many virtual
68 * filesystems (including /proc) exhibit this behavior.
69 */
70
71/* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
72 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
73 * and the name already exists in form of a symlink, try to create the new
74 * name indicated by the symlink. The old code always complained that the
75 * name already exists, due to not following the symlink even if its target
76 * is nonexistent. The new semantics affects also mknod() and link() when
Lucas De Marchi25985ed2011-03-30 22:57:33 -030077 * the name is a symlink pointing to a non-existent name.
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 *
79 * I don't know which semantics is the right one, since I have no access
80 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
81 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
82 * "old" one. Personally, I think the new semantics is much more logical.
83 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
84 * file does succeed in both HP-UX and SunOs, but not in Solaris
85 * and in the old Linux semantics.
86 */
87
88/* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
89 * semantics. See the comments in "open_namei" and "do_link" below.
90 *
91 * [10-Sep-98 Alan Modra] Another symlink change.
92 */
93
94/* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
95 * inside the path - always follow.
96 * in the last component in creation/removal/renaming - never follow.
97 * if LOOKUP_FOLLOW passed - follow.
98 * if the pathname has trailing slashes - follow.
99 * otherwise - don't follow.
100 * (applied in that order).
101 *
102 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
103 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
104 * During the 2.4 we need to fix the userland stuff depending on it -
105 * hopefully we will be able to get rid of that wart in 2.5. So far only
106 * XEmacs seems to be relying on it...
107 */
108/*
109 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
Arjan van de Vena11f3a02006-03-23 03:00:33 -0800110 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 * any extra contention...
112 */
113
114/* In order to reduce some races, while at the same time doing additional
115 * checking and hopefully speeding things up, we copy filenames to the
116 * kernel data space before using them..
117 *
118 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
119 * PATH_MAX includes the nul terminator --RR.
120 */
Jeff Layton91a27b22012-10-10 15:25:28 -0400121void final_putname(struct filename *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Jeff Layton7950e382012-10-10 16:43:13 -0400123 if (name->separate) {
124 __putname(name->name);
125 kfree(name);
126 } else {
127 __putname(name);
128 }
Jeff Layton91a27b22012-10-10 15:25:28 -0400129}
130
Jeff Layton7950e382012-10-10 16:43:13 -0400131#define EMBEDDED_NAME_MAX (PATH_MAX - sizeof(struct filename))
132
Jeff Layton91a27b22012-10-10 15:25:28 -0400133static struct filename *
134getname_flags(const char __user *filename, int flags, int *empty)
135{
136 struct filename *result, *err;
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700137 int len;
Jeff Layton7950e382012-10-10 16:43:13 -0400138 long max;
139 char *kname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Jeff Layton7ac86262012-10-10 15:25:28 -0400141 result = audit_reusename(filename);
142 if (result)
143 return result;
144
Jeff Layton7950e382012-10-10 16:43:13 -0400145 result = __getname();
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700146 if (unlikely(!result))
Eric Paris4043cde2012-01-03 14:23:08 -0500147 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Jeff Layton7950e382012-10-10 16:43:13 -0400149 /*
150 * First, try to embed the struct filename inside the names_cache
151 * allocation
152 */
153 kname = (char *)result + sizeof(*result);
Jeff Layton91a27b22012-10-10 15:25:28 -0400154 result->name = kname;
Jeff Layton7950e382012-10-10 16:43:13 -0400155 result->separate = false;
156 max = EMBEDDED_NAME_MAX;
157
158recopy:
159 len = strncpy_from_user(kname, filename, max);
Jeff Layton91a27b22012-10-10 15:25:28 -0400160 if (unlikely(len < 0)) {
161 err = ERR_PTR(len);
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700162 goto error;
Jeff Layton91a27b22012-10-10 15:25:28 -0400163 }
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700164
Jeff Layton7950e382012-10-10 16:43:13 -0400165 /*
166 * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
167 * separate struct filename so we can dedicate the entire
168 * names_cache allocation for the pathname, and re-do the copy from
169 * userland.
170 */
171 if (len == EMBEDDED_NAME_MAX && max == EMBEDDED_NAME_MAX) {
172 kname = (char *)result;
173
174 result = kzalloc(sizeof(*result), GFP_KERNEL);
175 if (!result) {
176 err = ERR_PTR(-ENOMEM);
177 result = (struct filename *)kname;
178 goto error;
179 }
180 result->name = kname;
181 result->separate = true;
182 max = PATH_MAX;
183 goto recopy;
184 }
185
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700186 /* The empty path is special. */
187 if (unlikely(!len)) {
188 if (empty)
Eric Paris4043cde2012-01-03 14:23:08 -0500189 *empty = 1;
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700190 err = ERR_PTR(-ENOENT);
191 if (!(flags & LOOKUP_EMPTY))
192 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 }
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700194
195 err = ERR_PTR(-ENAMETOOLONG);
Jeff Layton7950e382012-10-10 16:43:13 -0400196 if (unlikely(len >= PATH_MAX))
197 goto error;
198
199 result->uptr = filename;
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800200 result->aname = NULL;
Jeff Layton7950e382012-10-10 16:43:13 -0400201 audit_getname(result);
202 return result;
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700203
204error:
Jeff Layton7950e382012-10-10 16:43:13 -0400205 final_putname(result);
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700206 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
Jeff Layton91a27b22012-10-10 15:25:28 -0400209struct filename *
210getname(const char __user * filename)
Al Virof52e0c12011-03-14 18:56:51 -0400211{
Linus Torvaldsf7493e52012-03-22 16:10:40 -0700212 return getname_flags(filename, 0, NULL);
Al Virof52e0c12011-03-14 18:56:51 -0400213}
214
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800215/*
216 * The "getname_kernel()" interface doesn't do pathnames longer
217 * than EMBEDDED_NAME_MAX. Deal with it - you're a kernel user.
218 */
219struct filename *
220getname_kernel(const char * filename)
221{
222 struct filename *result;
223 char *kname;
224 int len;
225
226 len = strlen(filename);
227 if (len >= EMBEDDED_NAME_MAX)
228 return ERR_PTR(-ENAMETOOLONG);
229
230 result = __getname();
231 if (unlikely(!result))
232 return ERR_PTR(-ENOMEM);
233
234 kname = (char *)result + sizeof(*result);
235 result->name = kname;
236 result->uptr = NULL;
237 result->aname = NULL;
238 result->separate = false;
239
240 strlcpy(kname, filename, EMBEDDED_NAME_MAX);
241 return result;
242}
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244#ifdef CONFIG_AUDITSYSCALL
Jeff Layton91a27b22012-10-10 15:25:28 -0400245void putname(struct filename *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
Al Viro5ac3a9c2006-07-16 06:38:45 -0400247 if (unlikely(!audit_dummy_context()))
Jeff Layton91a27b22012-10-10 15:25:28 -0400248 return audit_putname(name);
249 final_putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251#endif
252
Linus Torvaldse77819e2011-07-22 19:30:19 -0700253static int check_acl(struct inode *inode, int mask)
254{
Linus Torvalds84635d62011-07-25 22:47:03 -0700255#ifdef CONFIG_FS_POSIX_ACL
Linus Torvaldse77819e2011-07-22 19:30:19 -0700256 struct posix_acl *acl;
257
Linus Torvaldse77819e2011-07-22 19:30:19 -0700258 if (mask & MAY_NOT_BLOCK) {
Al Viro35678662011-08-02 21:32:13 -0400259 acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
260 if (!acl)
Linus Torvaldse77819e2011-07-22 19:30:19 -0700261 return -EAGAIN;
Al Viro35678662011-08-02 21:32:13 -0400262 /* no ->get_acl() calls in RCU mode... */
263 if (acl == ACL_NOT_CACHED)
264 return -ECHILD;
Ari Savolainen206b1d02011-08-06 19:43:07 +0300265 return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
Linus Torvaldse77819e2011-07-22 19:30:19 -0700266 }
267
Christoph Hellwig2982baa2013-12-20 05:16:38 -0800268 acl = get_acl(inode, ACL_TYPE_ACCESS);
269 if (IS_ERR(acl))
270 return PTR_ERR(acl);
Linus Torvaldse77819e2011-07-22 19:30:19 -0700271 if (acl) {
272 int error = posix_acl_permission(inode, acl, mask);
273 posix_acl_release(acl);
274 return error;
275 }
Linus Torvalds84635d62011-07-25 22:47:03 -0700276#endif
Linus Torvaldse77819e2011-07-22 19:30:19 -0700277
278 return -EAGAIN;
279}
280
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700281/*
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530282 * This does the basic permission checking
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700283 */
Al Viro7e401452011-06-20 19:12:17 -0400284static int acl_permission_check(struct inode *inode, int mask)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700285{
Linus Torvalds26cf46b2011-05-13 11:51:01 -0700286 unsigned int mode = inode->i_mode;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700287
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -0800288 if (likely(uid_eq(current_fsuid(), inode->i_uid)))
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700289 mode >>= 6;
290 else {
Linus Torvaldse77819e2011-07-22 19:30:19 -0700291 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
Al Viro7e401452011-06-20 19:12:17 -0400292 int error = check_acl(inode, mask);
Nick Pigginb74c79e2011-01-07 17:49:58 +1100293 if (error != -EAGAIN)
294 return error;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700295 }
296
297 if (in_group_p(inode->i_gid))
298 mode >>= 3;
299 }
300
301 /*
302 * If the DACs are ok we don't need any capability check.
303 */
Al Viro9c2c7032011-06-20 19:06:22 -0400304 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700305 return 0;
306 return -EACCES;
307}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309/**
Nick Pigginb74c79e2011-01-07 17:49:58 +1100310 * generic_permission - check for access rights on a Posix-like filesystem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 * @inode: inode to check access rights for
Andreas Gruenbacher8fd90c82011-10-23 23:13:30 +0530312 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 *
314 * Used to check for read/write/execute permissions on a file.
315 * We use "fsuid" for this, letting us set arbitrary permissions
316 * for filesystem access without changing the "normal" uids which
Nick Pigginb74c79e2011-01-07 17:49:58 +1100317 * are used for other things.
318 *
319 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
320 * request cannot be satisfied (eg. requires blocking or too much complexity).
321 * It would then be called again in ref-walk mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 */
Al Viro2830ba72011-06-20 19:16:29 -0400323int generic_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700325 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 /*
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530328 * Do the basic permission checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 */
Al Viro7e401452011-06-20 19:12:17 -0400330 ret = acl_permission_check(inode, mask);
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700331 if (ret != -EACCES)
332 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Al Virod594e7e2011-06-20 19:55:42 -0400334 if (S_ISDIR(inode->i_mode)) {
335 /* DACs are overridable for directories */
Andy Lutomirski5bacea82014-06-10 12:45:42 -0700336 if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
Al Virod594e7e2011-06-20 19:55:42 -0400337 return 0;
338 if (!(mask & MAY_WRITE))
Andy Lutomirski5bacea82014-06-10 12:45:42 -0700339 if (capable_wrt_inode_uidgid(inode,
340 CAP_DAC_READ_SEARCH))
Al Virod594e7e2011-06-20 19:55:42 -0400341 return 0;
342 return -EACCES;
343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 /*
345 * Read/write DACs are always overridable.
Al Virod594e7e2011-06-20 19:55:42 -0400346 * Executable DACs are overridable when there is
347 * at least one exec bit set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 */
Al Virod594e7e2011-06-20 19:55:42 -0400349 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
Andy Lutomirski5bacea82014-06-10 12:45:42 -0700350 if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return 0;
352
353 /*
354 * Searching includes executable on directories, else just read.
355 */
Serge E. Hallyn7ea66002009-12-29 14:50:19 -0600356 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
Al Virod594e7e2011-06-20 19:55:42 -0400357 if (mask == MAY_READ)
Andy Lutomirski5bacea82014-06-10 12:45:42 -0700358 if (capable_wrt_inode_uidgid(inode, CAP_DAC_READ_SEARCH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return 0;
360
361 return -EACCES;
362}
363
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700364/*
365 * We _really_ want to just do "generic_permission()" without
366 * even looking at the inode->i_op values. So we keep a cache
367 * flag in inode->i_opflags, that says "this has not special
368 * permission function, use the fast case".
369 */
370static inline int do_inode_permission(struct inode *inode, int mask)
371{
372 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
373 if (likely(inode->i_op->permission))
374 return inode->i_op->permission(inode, mask);
375
376 /* This gets set once for the inode lifetime */
377 spin_lock(&inode->i_lock);
378 inode->i_opflags |= IOP_FASTPERM;
379 spin_unlock(&inode->i_lock);
380 }
381 return generic_permission(inode, mask);
382}
383
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200384/**
David Howells0bdaea92012-06-25 12:55:46 +0100385 * __inode_permission - Check for access rights to a given inode
386 * @inode: Inode to check permission on
387 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200388 *
David Howells0bdaea92012-06-25 12:55:46 +0100389 * Check for read/write/execute permissions on an inode.
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530390 *
391 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
David Howells0bdaea92012-06-25 12:55:46 +0100392 *
393 * This does not check for a read-only file system. You probably want
394 * inode_permission().
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200395 */
David Howells0bdaea92012-06-25 12:55:46 +0100396int __inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
Al Viroe6305c42008-07-15 21:03:57 -0400398 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700400 if (unlikely(mask & MAY_WRITE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 /*
402 * Nobody gets write access to an immutable file.
403 */
404 if (IS_IMMUTABLE(inode))
405 return -EACCES;
406 }
407
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700408 retval = do_inode_permission(inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 if (retval)
410 return retval;
411
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700412 retval = devcgroup_inode_permission(inode, mask);
413 if (retval)
414 return retval;
415
Eric Parisd09ca732010-07-23 11:43:57 -0400416 return security_inode_permission(inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
Miklos Szeredi96bd0c52014-10-24 00:14:35 +0200418EXPORT_SYMBOL(__inode_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Al Virof4d6ff82011-06-19 13:14:21 -0400420/**
David Howells0bdaea92012-06-25 12:55:46 +0100421 * sb_permission - Check superblock-level permissions
422 * @sb: Superblock of inode to check permission on
Randy Dunlap55852632012-08-18 17:39:25 -0700423 * @inode: Inode to check permission on
David Howells0bdaea92012-06-25 12:55:46 +0100424 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
425 *
426 * Separate out file-system wide checks from inode-specific permission checks.
427 */
428static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
429{
430 if (unlikely(mask & MAY_WRITE)) {
431 umode_t mode = inode->i_mode;
432
433 /* Nobody gets write access to a read-only fs. */
434 if ((sb->s_flags & MS_RDONLY) &&
435 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
436 return -EROFS;
437 }
438 return 0;
439}
440
441/**
442 * inode_permission - Check for access rights to a given inode
443 * @inode: Inode to check permission on
444 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
445 *
446 * Check for read/write/execute permissions on an inode. We use fs[ug]id for
447 * this, letting us set arbitrary permissions for filesystem access without
448 * changing the "normal" UIDs which are used for other things.
449 *
450 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
451 */
452int inode_permission(struct inode *inode, int mask)
453{
454 int retval;
455
456 retval = sb_permission(inode->i_sb, inode, mask);
457 if (retval)
458 return retval;
459 return __inode_permission(inode, mask);
460}
461
462/**
Jan Blunck5dd784d02008-02-14 19:34:38 -0800463 * path_get - get a reference to a path
464 * @path: path to get the reference to
465 *
466 * Given a path increment the reference count to the dentry and the vfsmount.
467 */
Al Virodcf787f2013-03-01 23:51:07 -0500468void path_get(const struct path *path)
Jan Blunck5dd784d02008-02-14 19:34:38 -0800469{
470 mntget(path->mnt);
471 dget(path->dentry);
472}
473EXPORT_SYMBOL(path_get);
474
475/**
Jan Blunck1d957f92008-02-14 19:34:35 -0800476 * path_put - put a reference to a path
477 * @path: path to put the reference to
478 *
479 * Given a path decrement the reference count to the dentry and the vfsmount.
480 */
Al Virodcf787f2013-03-01 23:51:07 -0500481void path_put(const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Jan Blunck1d957f92008-02-14 19:34:35 -0800483 dput(path->dentry);
484 mntput(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
Jan Blunck1d957f92008-02-14 19:34:35 -0800486EXPORT_SYMBOL(path_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Al Viro19660af2011-03-25 10:32:48 -0400488/*
Nick Piggin31e6b012011-01-07 17:49:52 +1100489 * Path walking has 2 modes, rcu-walk and ref-walk (see
Al Viro19660af2011-03-25 10:32:48 -0400490 * Documentation/filesystems/path-lookup.txt). In situations when we can't
491 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
492 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
493 * mode. Refcounts are grabbed at the last known good point before rcu-walk
494 * got stuck, so ref-walk may continue from there. If this is not successful
495 * (eg. a seqcount has changed), then failure is returned and it's up to caller
496 * to restart the path walk from the beginning in ref-walk mode.
Nick Piggin31e6b012011-01-07 17:49:52 +1100497 */
Nick Piggin31e6b012011-01-07 17:49:52 +1100498
499/**
Al Viro19660af2011-03-25 10:32:48 -0400500 * unlazy_walk - try to switch to ref-walk mode.
501 * @nd: nameidata pathwalk data
502 * @dentry: child of nd->path.dentry or NULL
Randy Dunlap39191622011-01-08 19:36:21 -0800503 * Returns: 0 on success, -ECHILD on failure
Nick Piggin31e6b012011-01-07 17:49:52 +1100504 *
Al Viro19660af2011-03-25 10:32:48 -0400505 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
506 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
507 * @nd or NULL. Must be called from rcu-walk context.
Nick Piggin31e6b012011-01-07 17:49:52 +1100508 */
Al Viro19660af2011-03-25 10:32:48 -0400509static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
Nick Piggin31e6b012011-01-07 17:49:52 +1100510{
511 struct fs_struct *fs = current->fs;
512 struct dentry *parent = nd->path.dentry;
513
514 BUG_ON(!(nd->flags & LOOKUP_RCU));
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700515
516 /*
Al Viro48a066e2013-09-29 22:06:07 -0400517 * After legitimizing the bastards, terminate_walk()
518 * will do the right thing for non-RCU mode, and all our
519 * subsequent exit cases should rcu_read_unlock()
520 * before returning. Do vfsmount first; if dentry
521 * can't be legitimized, just set nd->path.dentry to NULL
522 * and rely on dput(NULL) being a no-op.
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700523 */
Al Viro48a066e2013-09-29 22:06:07 -0400524 if (!legitimize_mnt(nd->path.mnt, nd->m_seq))
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700525 return -ECHILD;
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700526 nd->flags &= ~LOOKUP_RCU;
Linus Torvalds15570082013-09-02 11:38:06 -0700527
Al Viro48a066e2013-09-29 22:06:07 -0400528 if (!lockref_get_not_dead(&parent->d_lockref)) {
529 nd->path.dentry = NULL;
Al Virod870b4a2013-11-29 01:48:32 -0500530 goto out;
Al Viro48a066e2013-09-29 22:06:07 -0400531 }
532
Linus Torvalds15570082013-09-02 11:38:06 -0700533 /*
534 * For a negative lookup, the lookup sequence point is the parents
535 * sequence point, and it only needs to revalidate the parent dentry.
536 *
537 * For a positive lookup, we need to move both the parent and the
538 * dentry from the RCU domain to be properly refcounted. And the
539 * sequence number in the dentry validates *both* dentry counters,
540 * since we checked the sequence number of the parent after we got
541 * the child sequence number. So we know the parent must still
542 * be valid if the child sequence number is still valid.
543 */
Al Viro19660af2011-03-25 10:32:48 -0400544 if (!dentry) {
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700545 if (read_seqcount_retry(&parent->d_seq, nd->seq))
546 goto out;
Al Viro19660af2011-03-25 10:32:48 -0400547 BUG_ON(nd->inode != parent->d_inode);
548 } else {
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700549 if (!lockref_get_not_dead(&dentry->d_lockref))
550 goto out;
551 if (read_seqcount_retry(&dentry->d_seq, nd->seq))
552 goto drop_dentry;
Al Viro19660af2011-03-25 10:32:48 -0400553 }
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700554
555 /*
556 * Sequence counts matched. Now make sure that the root is
557 * still valid and get it if required.
558 */
559 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
560 spin_lock(&fs->lock);
561 if (nd->root.mnt != fs->root.mnt || nd->root.dentry != fs->root.dentry)
562 goto unlock_and_drop_dentry;
Nick Piggin31e6b012011-01-07 17:49:52 +1100563 path_get(&nd->root);
564 spin_unlock(&fs->lock);
565 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100566
Al Viro8b61e742013-11-08 12:45:01 -0500567 rcu_read_unlock();
Nick Piggin31e6b012011-01-07 17:49:52 +1100568 return 0;
Al Viro19660af2011-03-25 10:32:48 -0400569
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700570unlock_and_drop_dentry:
571 spin_unlock(&fs->lock);
572drop_dentry:
Al Viro8b61e742013-11-08 12:45:01 -0500573 rcu_read_unlock();
Linus Torvalds15570082013-09-02 11:38:06 -0700574 dput(dentry);
Linus Torvaldsd0d27272013-09-10 12:17:49 -0700575 goto drop_root_mnt;
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700576out:
Al Viro8b61e742013-11-08 12:45:01 -0500577 rcu_read_unlock();
Linus Torvaldsd0d27272013-09-10 12:17:49 -0700578drop_root_mnt:
579 if (!(nd->flags & LOOKUP_ROOT))
580 nd->root.mnt = NULL;
Nick Piggin31e6b012011-01-07 17:49:52 +1100581 return -ECHILD;
582}
583
Al Viro4ce16ef32012-06-10 16:10:59 -0400584static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
Nick Piggin34286d62011-01-07 17:49:57 +1100585{
Al Viro4ce16ef32012-06-10 16:10:59 -0400586 return dentry->d_op->d_revalidate(dentry, flags);
Nick Piggin34286d62011-01-07 17:49:57 +1100587}
588
Al Viro9f1fafe2011-03-25 11:00:12 -0400589/**
590 * complete_walk - successful completion of path walk
591 * @nd: pointer nameidata
Jeff Layton39159de2009-12-07 12:01:50 -0500592 *
Al Viro9f1fafe2011-03-25 11:00:12 -0400593 * If we had been in RCU mode, drop out of it and legitimize nd->path.
594 * Revalidate the final result, unless we'd already done that during
595 * the path walk or the filesystem doesn't ask for it. Return 0 on
596 * success, -error on failure. In case of failure caller does not
597 * need to drop nd->path.
Jeff Layton39159de2009-12-07 12:01:50 -0500598 */
Al Viro9f1fafe2011-03-25 11:00:12 -0400599static int complete_walk(struct nameidata *nd)
Jeff Layton39159de2009-12-07 12:01:50 -0500600{
Al Viro16c2cd72011-02-22 15:50:10 -0500601 struct dentry *dentry = nd->path.dentry;
Jeff Layton39159de2009-12-07 12:01:50 -0500602 int status;
Jeff Layton39159de2009-12-07 12:01:50 -0500603
Al Viro9f1fafe2011-03-25 11:00:12 -0400604 if (nd->flags & LOOKUP_RCU) {
605 nd->flags &= ~LOOKUP_RCU;
606 if (!(nd->flags & LOOKUP_ROOT))
607 nd->root.mnt = NULL;
Linus Torvalds15570082013-09-02 11:38:06 -0700608
Al Viro48a066e2013-09-29 22:06:07 -0400609 if (!legitimize_mnt(nd->path.mnt, nd->m_seq)) {
Al Viro8b61e742013-11-08 12:45:01 -0500610 rcu_read_unlock();
Al Viro48a066e2013-09-29 22:06:07 -0400611 return -ECHILD;
612 }
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700613 if (unlikely(!lockref_get_not_dead(&dentry->d_lockref))) {
Al Viro8b61e742013-11-08 12:45:01 -0500614 rcu_read_unlock();
Al Viro48a066e2013-09-29 22:06:07 -0400615 mntput(nd->path.mnt);
Al Viro9f1fafe2011-03-25 11:00:12 -0400616 return -ECHILD;
617 }
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700618 if (read_seqcount_retry(&dentry->d_seq, nd->seq)) {
Al Viro8b61e742013-11-08 12:45:01 -0500619 rcu_read_unlock();
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700620 dput(dentry);
Al Viro48a066e2013-09-29 22:06:07 -0400621 mntput(nd->path.mnt);
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700622 return -ECHILD;
623 }
Al Viro8b61e742013-11-08 12:45:01 -0500624 rcu_read_unlock();
Al Viro9f1fafe2011-03-25 11:00:12 -0400625 }
626
Al Viro16c2cd72011-02-22 15:50:10 -0500627 if (likely(!(nd->flags & LOOKUP_JUMPED)))
Jeff Layton39159de2009-12-07 12:01:50 -0500628 return 0;
629
Jeff Laytonecf3d1f2013-02-20 11:19:05 -0500630 if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
Al Viro16c2cd72011-02-22 15:50:10 -0500631 return 0;
632
Jeff Laytonecf3d1f2013-02-20 11:19:05 -0500633 status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
Jeff Layton39159de2009-12-07 12:01:50 -0500634 if (status > 0)
635 return 0;
636
Al Viro16c2cd72011-02-22 15:50:10 -0500637 if (!status)
Jeff Layton39159de2009-12-07 12:01:50 -0500638 status = -ESTALE;
Al Viro16c2cd72011-02-22 15:50:10 -0500639
Al Viro9f1fafe2011-03-25 11:00:12 -0400640 path_put(&nd->path);
Jeff Layton39159de2009-12-07 12:01:50 -0500641 return status;
642}
643
Al Viro2a737872009-04-07 11:49:53 -0400644static __always_inline void set_root(struct nameidata *nd)
645{
Al Viroaed47132014-09-13 21:55:46 -0400646 get_fs_root(current->fs, &nd->root);
Al Viro2a737872009-04-07 11:49:53 -0400647}
648
Al Viro6de88d72009-08-09 01:41:57 +0400649static int link_path_walk(const char *, struct nameidata *);
650
Al Viroaed47132014-09-13 21:55:46 -0400651static __always_inline unsigned set_root_rcu(struct nameidata *nd)
Nick Piggin31e6b012011-01-07 17:49:52 +1100652{
Al Viroaed47132014-09-13 21:55:46 -0400653 struct fs_struct *fs = current->fs;
654 unsigned seq, res;
Nick Pigginc28cc362011-01-07 17:49:53 +1100655
Al Viroaed47132014-09-13 21:55:46 -0400656 do {
657 seq = read_seqcount_begin(&fs->seq);
658 nd->root = fs->root;
659 res = __read_seqcount_begin(&nd->root.dentry->d_seq);
660 } while (read_seqcount_retry(&fs->seq, seq));
661 return res;
Nick Piggin31e6b012011-01-07 17:49:52 +1100662}
663
Jan Blunck1d957f92008-02-14 19:34:35 -0800664static void path_put_conditional(struct path *path, struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700665{
666 dput(path->dentry);
Jan Blunck4ac91372008-02-14 19:34:32 -0800667 if (path->mnt != nd->path.mnt)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700668 mntput(path->mnt);
669}
670
Nick Piggin7b9337a2011-01-14 08:42:43 +0000671static inline void path_to_nameidata(const struct path *path,
672 struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700673{
Nick Piggin31e6b012011-01-07 17:49:52 +1100674 if (!(nd->flags & LOOKUP_RCU)) {
675 dput(nd->path.dentry);
676 if (nd->path.mnt != path->mnt)
677 mntput(nd->path.mnt);
Huang Shijie9a229682010-04-02 17:37:13 +0800678 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100679 nd->path.mnt = path->mnt;
Jan Blunck4ac91372008-02-14 19:34:32 -0800680 nd->path.dentry = path->dentry;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700681}
682
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400683/*
684 * Helper to directly jump to a known parsed path from ->follow_link,
685 * caller must have taken a reference to path beforehand.
686 */
687void nd_jump_link(struct nameidata *nd, struct path *path)
688{
689 path_put(&nd->path);
690
691 nd->path = *path;
692 nd->inode = nd->path.dentry->d_inode;
693 nd->flags |= LOOKUP_JUMPED;
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400694}
695
Al Viro574197e2011-03-14 22:20:34 -0400696static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
697{
698 struct inode *inode = link->dentry->d_inode;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400699 if (inode->i_op->put_link)
Al Viro574197e2011-03-14 22:20:34 -0400700 inode->i_op->put_link(link->dentry, nd, cookie);
701 path_put(link);
702}
703
Linus Torvalds561ec642012-10-26 10:05:07 -0700704int sysctl_protected_symlinks __read_mostly = 0;
705int sysctl_protected_hardlinks __read_mostly = 0;
Kees Cook800179c2012-07-25 17:29:07 -0700706
707/**
708 * may_follow_link - Check symlink following for unsafe situations
709 * @link: The path of the symlink
Randy Dunlap55852632012-08-18 17:39:25 -0700710 * @nd: nameidata pathwalk data
Kees Cook800179c2012-07-25 17:29:07 -0700711 *
712 * In the case of the sysctl_protected_symlinks sysctl being enabled,
713 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
714 * in a sticky world-writable directory. This is to protect privileged
715 * processes from failing races against path names that may change out
716 * from under them by way of other users creating malicious symlinks.
717 * It will permit symlinks to be followed only when outside a sticky
718 * world-writable directory, or when the uid of the symlink and follower
719 * match, or when the directory owner matches the symlink's owner.
720 *
721 * Returns 0 if following the symlink is allowed, -ve on error.
722 */
723static inline int may_follow_link(struct path *link, struct nameidata *nd)
724{
725 const struct inode *inode;
726 const struct inode *parent;
727
728 if (!sysctl_protected_symlinks)
729 return 0;
730
731 /* Allowed if owner and follower match. */
732 inode = link->dentry->d_inode;
Eric W. Biederman81abe272012-08-03 09:38:08 -0700733 if (uid_eq(current_cred()->fsuid, inode->i_uid))
Kees Cook800179c2012-07-25 17:29:07 -0700734 return 0;
735
736 /* Allowed if parent directory not sticky and world-writable. */
737 parent = nd->path.dentry->d_inode;
738 if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
739 return 0;
740
741 /* Allowed if parent directory and link owner match. */
Eric W. Biederman81abe272012-08-03 09:38:08 -0700742 if (uid_eq(parent->i_uid, inode->i_uid))
Kees Cook800179c2012-07-25 17:29:07 -0700743 return 0;
744
Sasha Levinffd8d102012-10-04 19:56:40 -0400745 audit_log_link_denied("follow_link", link);
Kees Cook800179c2012-07-25 17:29:07 -0700746 path_put_conditional(link, nd);
747 path_put(&nd->path);
748 return -EACCES;
749}
750
751/**
752 * safe_hardlink_source - Check for safe hardlink conditions
753 * @inode: the source inode to hardlink from
754 *
755 * Return false if at least one of the following conditions:
756 * - inode is not a regular file
757 * - inode is setuid
758 * - inode is setgid and group-exec
759 * - access failure for read and write
760 *
761 * Otherwise returns true.
762 */
763static bool safe_hardlink_source(struct inode *inode)
764{
765 umode_t mode = inode->i_mode;
766
767 /* Special files should not get pinned to the filesystem. */
768 if (!S_ISREG(mode))
769 return false;
770
771 /* Setuid files should not get pinned to the filesystem. */
772 if (mode & S_ISUID)
773 return false;
774
775 /* Executable setgid files should not get pinned to the filesystem. */
776 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
777 return false;
778
779 /* Hardlinking to unreadable or unwritable sources is dangerous. */
780 if (inode_permission(inode, MAY_READ | MAY_WRITE))
781 return false;
782
783 return true;
784}
785
786/**
787 * may_linkat - Check permissions for creating a hardlink
788 * @link: the source to hardlink from
789 *
790 * Block hardlink when all of:
791 * - sysctl_protected_hardlinks enabled
792 * - fsuid does not match inode
793 * - hardlink source is unsafe (see safe_hardlink_source() above)
794 * - not CAP_FOWNER
795 *
796 * Returns 0 if successful, -ve on error.
797 */
798static int may_linkat(struct path *link)
799{
800 const struct cred *cred;
801 struct inode *inode;
802
803 if (!sysctl_protected_hardlinks)
804 return 0;
805
806 cred = current_cred();
807 inode = link->dentry->d_inode;
808
809 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
810 * otherwise, it must be a safe source.
811 */
Eric W. Biederman81abe272012-08-03 09:38:08 -0700812 if (uid_eq(cred->fsuid, inode->i_uid) || safe_hardlink_source(inode) ||
Kees Cook800179c2012-07-25 17:29:07 -0700813 capable(CAP_FOWNER))
814 return 0;
815
Kees Cooka51d9ea2012-07-25 17:29:08 -0700816 audit_log_link_denied("linkat", link);
Kees Cook800179c2012-07-25 17:29:07 -0700817 return -EPERM;
818}
819
Al Virodef4af32009-12-26 08:37:05 -0500820static __always_inline int
Al Viro574197e2011-03-14 22:20:34 -0400821follow_link(struct path *link, struct nameidata *nd, void **p)
Ian Kent051d3812006-03-27 01:14:53 -0800822{
Nick Piggin7b9337a2011-01-14 08:42:43 +0000823 struct dentry *dentry = link->dentry;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400824 int error;
825 char *s;
Ian Kent051d3812006-03-27 01:14:53 -0800826
Al Viro844a3912011-02-15 00:38:26 -0500827 BUG_ON(nd->flags & LOOKUP_RCU);
828
Al Viro0e794582011-03-16 02:45:02 -0400829 if (link->mnt == nd->path.mnt)
830 mntget(link->mnt);
831
Al Viro6d7b5aa2012-06-10 04:15:17 -0400832 error = -ELOOP;
833 if (unlikely(current->total_link_count >= 40))
834 goto out_put_nd_path;
835
Al Viro574197e2011-03-14 22:20:34 -0400836 cond_resched();
837 current->total_link_count++;
838
Al Viro68ac1232012-03-15 08:21:57 -0400839 touch_atime(link);
Ian Kent051d3812006-03-27 01:14:53 -0800840 nd_set_link(nd, NULL);
841
Al Viro36f3b4f2011-02-22 21:24:38 -0500842 error = security_inode_follow_link(link->dentry, nd);
Al Viro6d7b5aa2012-06-10 04:15:17 -0400843 if (error)
844 goto out_put_nd_path;
Al Viro36f3b4f2011-02-22 21:24:38 -0500845
Al Viro86acdca12009-12-22 23:45:11 -0500846 nd->last_type = LAST_BIND;
Al Virodef4af32009-12-26 08:37:05 -0500847 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
848 error = PTR_ERR(*p);
Al Viro6d7b5aa2012-06-10 04:15:17 -0400849 if (IS_ERR(*p))
Christoph Hellwig408ef012012-06-18 10:47:03 -0400850 goto out_put_nd_path;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400851
852 error = 0;
853 s = nd_get_link(nd);
854 if (s) {
Al Viro443ed252013-09-10 12:00:43 -0400855 if (unlikely(IS_ERR(s))) {
856 path_put(&nd->path);
857 put_link(nd, link, *p);
858 return PTR_ERR(s);
859 }
860 if (*s == '/') {
Al Viroaed47132014-09-13 21:55:46 -0400861 if (!nd->root.mnt)
862 set_root(nd);
Al Viro443ed252013-09-10 12:00:43 -0400863 path_put(&nd->path);
864 nd->path = nd->root;
865 path_get(&nd->root);
866 nd->flags |= LOOKUP_JUMPED;
867 }
868 nd->inode = nd->path.dentry->d_inode;
869 error = link_path_walk(s, nd);
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400870 if (unlikely(error))
871 put_link(nd, link, *p);
Ian Kent051d3812006-03-27 01:14:53 -0800872 }
Al Viro6d7b5aa2012-06-10 04:15:17 -0400873
874 return error;
875
876out_put_nd_path:
Arnd Bergmann98f6ef62012-10-11 13:20:00 +0000877 *p = NULL;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400878 path_put(&nd->path);
Al Viro6d7b5aa2012-06-10 04:15:17 -0400879 path_put(link);
Ian Kent051d3812006-03-27 01:14:53 -0800880 return error;
881}
882
Nick Piggin31e6b012011-01-07 17:49:52 +1100883static int follow_up_rcu(struct path *path)
884{
Al Viro0714a532011-11-24 22:19:58 -0500885 struct mount *mnt = real_mount(path->mnt);
886 struct mount *parent;
Nick Piggin31e6b012011-01-07 17:49:52 +1100887 struct dentry *mountpoint;
888
Al Viro0714a532011-11-24 22:19:58 -0500889 parent = mnt->mnt_parent;
890 if (&parent->mnt == path->mnt)
Nick Piggin31e6b012011-01-07 17:49:52 +1100891 return 0;
Al Viroa73324d2011-11-24 22:25:07 -0500892 mountpoint = mnt->mnt_mountpoint;
Nick Piggin31e6b012011-01-07 17:49:52 +1100893 path->dentry = mountpoint;
Al Viro0714a532011-11-24 22:19:58 -0500894 path->mnt = &parent->mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +1100895 return 1;
896}
897
David Howellsf015f1262012-06-25 12:55:28 +0100898/*
899 * follow_up - Find the mountpoint of path's vfsmount
900 *
901 * Given a path, find the mountpoint of its source file system.
902 * Replace @path with the path of the mountpoint in the parent mount.
903 * Up is towards /.
904 *
905 * Return 1 if we went up a level and 0 if we were already at the
906 * root.
907 */
Al Virobab77eb2009-04-18 03:26:48 -0400908int follow_up(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
Al Viro0714a532011-11-24 22:19:58 -0500910 struct mount *mnt = real_mount(path->mnt);
911 struct mount *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 struct dentry *mountpoint;
Nick Piggin99b7db72010-08-18 04:37:39 +1000913
Al Viro48a066e2013-09-29 22:06:07 -0400914 read_seqlock_excl(&mount_lock);
Al Viro0714a532011-11-24 22:19:58 -0500915 parent = mnt->mnt_parent;
Al Viro3c0a6162012-07-18 17:32:50 +0400916 if (parent == mnt) {
Al Viro48a066e2013-09-29 22:06:07 -0400917 read_sequnlock_excl(&mount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 return 0;
919 }
Al Viro0714a532011-11-24 22:19:58 -0500920 mntget(&parent->mnt);
Al Viroa73324d2011-11-24 22:25:07 -0500921 mountpoint = dget(mnt->mnt_mountpoint);
Al Viro48a066e2013-09-29 22:06:07 -0400922 read_sequnlock_excl(&mount_lock);
Al Virobab77eb2009-04-18 03:26:48 -0400923 dput(path->dentry);
924 path->dentry = mountpoint;
925 mntput(path->mnt);
Al Viro0714a532011-11-24 22:19:58 -0500926 path->mnt = &parent->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 return 1;
928}
929
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100930/*
David Howells9875cf82011-01-14 18:45:21 +0000931 * Perform an automount
932 * - return -EISDIR to tell follow_managed() to stop and return the path we
933 * were called with.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 */
David Howells9875cf82011-01-14 18:45:21 +0000935static int follow_automount(struct path *path, unsigned flags,
936 bool *need_mntput)
Nick Piggin31e6b012011-01-07 17:49:52 +1100937{
David Howells9875cf82011-01-14 18:45:21 +0000938 struct vfsmount *mnt;
David Howellsea5b7782011-01-14 19:10:03 +0000939 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +1100940
David Howells9875cf82011-01-14 18:45:21 +0000941 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
942 return -EREMOTE;
Al Viro463ffb22005-06-06 13:36:05 -0700943
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200944 /* We don't want to mount if someone's just doing a stat -
945 * unless they're stat'ing a directory and appended a '/' to
946 * the name.
947 *
948 * We do, however, want to mount if someone wants to open or
949 * create a file of any type under the mountpoint, wants to
950 * traverse through the mountpoint or wants to open the
951 * mounted directory. Also, autofs may mark negative dentries
952 * as being automount points. These will need the attentions
953 * of the daemon to instantiate them before they can be used.
David Howells9875cf82011-01-14 18:45:21 +0000954 */
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200955 if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
Linus Torvaldsd94c1772011-09-26 17:44:55 -0700956 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200957 path->dentry->d_inode)
958 return -EISDIR;
959
David Howells9875cf82011-01-14 18:45:21 +0000960 current->total_link_count++;
961 if (current->total_link_count >= 40)
962 return -ELOOP;
963
964 mnt = path->dentry->d_op->d_automount(path);
965 if (IS_ERR(mnt)) {
966 /*
967 * The filesystem is allowed to return -EISDIR here to indicate
968 * it doesn't want to automount. For instance, autofs would do
969 * this so that its userspace daemon can mount on this dentry.
970 *
971 * However, we can only permit this if it's a terminal point in
972 * the path being looked up; if it wasn't then the remainder of
973 * the path is inaccessible and we should say so.
974 */
Al Viro49084c32011-06-25 21:59:52 -0400975 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
David Howells9875cf82011-01-14 18:45:21 +0000976 return -EREMOTE;
977 return PTR_ERR(mnt);
978 }
David Howellsea5b7782011-01-14 19:10:03 +0000979
David Howells9875cf82011-01-14 18:45:21 +0000980 if (!mnt) /* mount collision */
981 return 0;
982
Al Viro8aef1882011-06-16 15:10:06 +0100983 if (!*need_mntput) {
984 /* lock_mount() may release path->mnt on error */
985 mntget(path->mnt);
986 *need_mntput = true;
987 }
Al Viro19a167a2011-01-17 01:35:23 -0500988 err = finish_automount(mnt, path);
David Howellsea5b7782011-01-14 19:10:03 +0000989
David Howellsea5b7782011-01-14 19:10:03 +0000990 switch (err) {
991 case -EBUSY:
992 /* Someone else made a mount here whilst we were busy */
Al Viro19a167a2011-01-17 01:35:23 -0500993 return 0;
David Howellsea5b7782011-01-14 19:10:03 +0000994 case 0:
Al Viro8aef1882011-06-16 15:10:06 +0100995 path_put(path);
David Howellsea5b7782011-01-14 19:10:03 +0000996 path->mnt = mnt;
997 path->dentry = dget(mnt->mnt_root);
David Howellsea5b7782011-01-14 19:10:03 +0000998 return 0;
Al Viro19a167a2011-01-17 01:35:23 -0500999 default:
1000 return err;
David Howellsea5b7782011-01-14 19:10:03 +00001001 }
Al Viro19a167a2011-01-17 01:35:23 -05001002
David Howells9875cf82011-01-14 18:45:21 +00001003}
1004
1005/*
1006 * Handle a dentry that is managed in some way.
David Howellscc53ce52011-01-14 18:45:26 +00001007 * - Flagged for transit management (autofs)
David Howells9875cf82011-01-14 18:45:21 +00001008 * - Flagged as mountpoint
1009 * - Flagged as automount point
1010 *
1011 * This may only be called in refwalk mode.
1012 *
1013 * Serialization is taken care of in namespace.c
1014 */
1015static int follow_managed(struct path *path, unsigned flags)
1016{
Al Viro8aef1882011-06-16 15:10:06 +01001017 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
David Howells9875cf82011-01-14 18:45:21 +00001018 unsigned managed;
1019 bool need_mntput = false;
Al Viro8aef1882011-06-16 15:10:06 +01001020 int ret = 0;
David Howells9875cf82011-01-14 18:45:21 +00001021
1022 /* Given that we're not holding a lock here, we retain the value in a
1023 * local variable for each dentry as we look at it so that we don't see
1024 * the components of that value change under us */
1025 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1026 managed &= DCACHE_MANAGED_DENTRY,
1027 unlikely(managed != 0)) {
David Howellscc53ce52011-01-14 18:45:26 +00001028 /* Allow the filesystem to manage the transit without i_mutex
1029 * being held. */
1030 if (managed & DCACHE_MANAGE_TRANSIT) {
1031 BUG_ON(!path->dentry->d_op);
1032 BUG_ON(!path->dentry->d_op->d_manage);
Al Viro1aed3e42011-03-18 09:09:02 -04001033 ret = path->dentry->d_op->d_manage(path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +00001034 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001035 break;
David Howellscc53ce52011-01-14 18:45:26 +00001036 }
1037
David Howells9875cf82011-01-14 18:45:21 +00001038 /* Transit to a mounted filesystem. */
1039 if (managed & DCACHE_MOUNTED) {
1040 struct vfsmount *mounted = lookup_mnt(path);
1041 if (mounted) {
1042 dput(path->dentry);
1043 if (need_mntput)
1044 mntput(path->mnt);
1045 path->mnt = mounted;
1046 path->dentry = dget(mounted->mnt_root);
1047 need_mntput = true;
1048 continue;
1049 }
1050
1051 /* Something is mounted on this dentry in another
1052 * namespace and/or whatever was mounted there in this
Al Viro48a066e2013-09-29 22:06:07 -04001053 * namespace got unmounted before lookup_mnt() could
1054 * get it */
David Howells9875cf82011-01-14 18:45:21 +00001055 }
1056
1057 /* Handle an automount point */
1058 if (managed & DCACHE_NEED_AUTOMOUNT) {
1059 ret = follow_automount(path, flags, &need_mntput);
1060 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001061 break;
David Howells9875cf82011-01-14 18:45:21 +00001062 continue;
1063 }
1064
1065 /* We didn't change the current path point */
1066 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 }
Al Viro8aef1882011-06-16 15:10:06 +01001068
1069 if (need_mntput && path->mnt == mnt)
1070 mntput(path->mnt);
1071 if (ret == -EISDIR)
1072 ret = 0;
Al Viroa3fbbde2011-11-07 21:21:26 +00001073 return ret < 0 ? ret : need_mntput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074}
1075
David Howellscc53ce52011-01-14 18:45:26 +00001076int follow_down_one(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
1078 struct vfsmount *mounted;
1079
Al Viro1c755af2009-04-18 14:06:57 -04001080 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -04001082 dput(path->dentry);
1083 mntput(path->mnt);
1084 path->mnt = mounted;
1085 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 return 1;
1087 }
1088 return 0;
1089}
1090
Ian Kent62a73752011-03-25 01:51:02 +08001091static inline bool managed_dentry_might_block(struct dentry *dentry)
1092{
1093 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
1094 dentry->d_op->d_manage(dentry, true) < 0);
1095}
1096
David Howells9875cf82011-01-14 18:45:21 +00001097/*
Al Viro287548e2011-05-27 06:50:06 -04001098 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1099 * we meet a managed dentry that would need blocking.
David Howells9875cf82011-01-14 18:45:21 +00001100 */
1101static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
Al Viro287548e2011-05-27 06:50:06 -04001102 struct inode **inode)
David Howells9875cf82011-01-14 18:45:21 +00001103{
Ian Kent62a73752011-03-25 01:51:02 +08001104 for (;;) {
Al Viroc7105362011-11-24 18:22:03 -05001105 struct mount *mounted;
Ian Kent62a73752011-03-25 01:51:02 +08001106 /*
1107 * Don't forget we might have a non-mountpoint managed dentry
1108 * that wants to block transit.
1109 */
Al Viro287548e2011-05-27 06:50:06 -04001110 if (unlikely(managed_dentry_might_block(path->dentry)))
David Howellsab909112011-01-14 18:46:51 +00001111 return false;
Ian Kent62a73752011-03-25 01:51:02 +08001112
1113 if (!d_mountpoint(path->dentry))
Al Virob37199e2014-03-20 15:18:22 -04001114 return true;
Ian Kent62a73752011-03-25 01:51:02 +08001115
Al Viro474279d2013-10-01 16:11:26 -04001116 mounted = __lookup_mnt(path->mnt, path->dentry);
David Howells9875cf82011-01-14 18:45:21 +00001117 if (!mounted)
1118 break;
Al Viroc7105362011-11-24 18:22:03 -05001119 path->mnt = &mounted->mnt;
1120 path->dentry = mounted->mnt.mnt_root;
Al Viroa3fbbde2011-11-07 21:21:26 +00001121 nd->flags |= LOOKUP_JUMPED;
David Howells9875cf82011-01-14 18:45:21 +00001122 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
Linus Torvalds59430262011-07-18 15:43:29 -07001123 /*
1124 * Update the inode too. We don't need to re-check the
1125 * dentry sequence number here after this d_inode read,
1126 * because a mount-point is always pinned.
1127 */
1128 *inode = path->dentry->d_inode;
David Howells9875cf82011-01-14 18:45:21 +00001129 }
Al Virob37199e2014-03-20 15:18:22 -04001130 return read_seqretry(&mount_lock, nd->m_seq);
Al Viro287548e2011-05-27 06:50:06 -04001131}
1132
Nick Piggin31e6b012011-01-07 17:49:52 +11001133static int follow_dotdot_rcu(struct nameidata *nd)
1134{
Al Viroaed47132014-09-13 21:55:46 -04001135 if (!nd->root.mnt)
1136 set_root_rcu(nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11001137
David Howells9875cf82011-01-14 18:45:21 +00001138 while (1) {
Nick Piggin31e6b012011-01-07 17:49:52 +11001139 if (nd->path.dentry == nd->root.dentry &&
1140 nd->path.mnt == nd->root.mnt) {
1141 break;
1142 }
1143 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1144 struct dentry *old = nd->path.dentry;
1145 struct dentry *parent = old->d_parent;
1146 unsigned seq;
1147
1148 seq = read_seqcount_begin(&parent->d_seq);
1149 if (read_seqcount_retry(&old->d_seq, nd->seq))
Al Viroef7562d2011-03-04 14:35:59 -05001150 goto failed;
Nick Piggin31e6b012011-01-07 17:49:52 +11001151 nd->path.dentry = parent;
1152 nd->seq = seq;
1153 break;
1154 }
1155 if (!follow_up_rcu(&nd->path))
1156 break;
1157 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Nick Piggin31e6b012011-01-07 17:49:52 +11001158 }
Al Virob37199e2014-03-20 15:18:22 -04001159 while (d_mountpoint(nd->path.dentry)) {
1160 struct mount *mounted;
1161 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry);
1162 if (!mounted)
1163 break;
1164 nd->path.mnt = &mounted->mnt;
1165 nd->path.dentry = mounted->mnt.mnt_root;
1166 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
1167 if (!read_seqretry(&mount_lock, nd->m_seq))
1168 goto failed;
1169 }
Al Virodea39372011-05-27 06:53:39 -04001170 nd->inode = nd->path.dentry->d_inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001171 return 0;
Al Viroef7562d2011-03-04 14:35:59 -05001172
1173failed:
1174 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001175 if (!(nd->flags & LOOKUP_ROOT))
1176 nd->root.mnt = NULL;
Al Viro8b61e742013-11-08 12:45:01 -05001177 rcu_read_unlock();
Al Viroef7562d2011-03-04 14:35:59 -05001178 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001179}
1180
David Howells9875cf82011-01-14 18:45:21 +00001181/*
David Howellscc53ce52011-01-14 18:45:26 +00001182 * Follow down to the covering mount currently visible to userspace. At each
1183 * point, the filesystem owning that dentry may be queried as to whether the
1184 * caller is permitted to proceed or not.
David Howellscc53ce52011-01-14 18:45:26 +00001185 */
Al Viro7cc90cc2011-03-18 09:04:20 -04001186int follow_down(struct path *path)
David Howellscc53ce52011-01-14 18:45:26 +00001187{
1188 unsigned managed;
1189 int ret;
1190
1191 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1192 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1193 /* Allow the filesystem to manage the transit without i_mutex
1194 * being held.
1195 *
1196 * We indicate to the filesystem if someone is trying to mount
1197 * something here. This gives autofs the chance to deny anyone
1198 * other than its daemon the right to mount on its
1199 * superstructure.
1200 *
1201 * The filesystem may sleep at this point.
1202 */
1203 if (managed & DCACHE_MANAGE_TRANSIT) {
1204 BUG_ON(!path->dentry->d_op);
1205 BUG_ON(!path->dentry->d_op->d_manage);
David Howellsab909112011-01-14 18:46:51 +00001206 ret = path->dentry->d_op->d_manage(
Al Viro1aed3e42011-03-18 09:09:02 -04001207 path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +00001208 if (ret < 0)
1209 return ret == -EISDIR ? 0 : ret;
1210 }
1211
1212 /* Transit to a mounted filesystem. */
1213 if (managed & DCACHE_MOUNTED) {
1214 struct vfsmount *mounted = lookup_mnt(path);
1215 if (!mounted)
1216 break;
1217 dput(path->dentry);
1218 mntput(path->mnt);
1219 path->mnt = mounted;
1220 path->dentry = dget(mounted->mnt_root);
1221 continue;
1222 }
1223
1224 /* Don't handle automount points here */
1225 break;
1226 }
1227 return 0;
1228}
1229
1230/*
David Howells9875cf82011-01-14 18:45:21 +00001231 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1232 */
1233static void follow_mount(struct path *path)
1234{
1235 while (d_mountpoint(path->dentry)) {
1236 struct vfsmount *mounted = lookup_mnt(path);
1237 if (!mounted)
1238 break;
1239 dput(path->dentry);
1240 mntput(path->mnt);
1241 path->mnt = mounted;
1242 path->dentry = dget(mounted->mnt_root);
1243 }
1244}
1245
Nick Piggin31e6b012011-01-07 17:49:52 +11001246static void follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
Al Viroaed47132014-09-13 21:55:46 -04001248 if (!nd->root.mnt)
1249 set_root(nd);
Andreas Mohre518ddb2006-09-29 02:01:22 -07001250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 while(1) {
Jan Blunck4ac91372008-02-14 19:34:32 -08001252 struct dentry *old = nd->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Al Viro2a737872009-04-07 11:49:53 -04001254 if (nd->path.dentry == nd->root.dentry &&
1255 nd->path.mnt == nd->root.mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 break;
1257 }
Jan Blunck4ac91372008-02-14 19:34:32 -08001258 if (nd->path.dentry != nd->path.mnt->mnt_root) {
Al Viro3088dd72010-01-30 15:47:29 -05001259 /* rare case of legitimate dget_parent()... */
1260 nd->path.dentry = dget_parent(nd->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 dput(old);
1262 break;
1263 }
Al Viro3088dd72010-01-30 15:47:29 -05001264 if (!follow_up(&nd->path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 }
Al Viro79ed0222009-04-18 13:59:41 -04001267 follow_mount(&nd->path);
Nick Piggin31e6b012011-01-07 17:49:52 +11001268 nd->inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269}
1270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271/*
Miklos Szeredibad61182012-03-26 12:54:24 +02001272 * This looks up the name in dcache, possibly revalidates the old dentry and
1273 * allocates a new one if not found or not valid. In the need_lookup argument
1274 * returns whether i_op->lookup is necessary.
1275 *
1276 * dir->d_inode->i_mutex must be held
Nick Pigginbaa03892010-08-18 04:37:31 +10001277 */
Miklos Szeredibad61182012-03-26 12:54:24 +02001278static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
Al Viro201f9562012-06-22 12:42:10 +04001279 unsigned int flags, bool *need_lookup)
Nick Pigginbaa03892010-08-18 04:37:31 +10001280{
Nick Pigginbaa03892010-08-18 04:37:31 +10001281 struct dentry *dentry;
Miklos Szeredibad61182012-03-26 12:54:24 +02001282 int error;
Nick Pigginbaa03892010-08-18 04:37:31 +10001283
Miklos Szeredibad61182012-03-26 12:54:24 +02001284 *need_lookup = false;
1285 dentry = d_lookup(dir, name);
1286 if (dentry) {
Jeff Layton39e3c952012-11-28 11:30:53 -05001287 if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
Al Viro201f9562012-06-22 12:42:10 +04001288 error = d_revalidate(dentry, flags);
Miklos Szeredibad61182012-03-26 12:54:24 +02001289 if (unlikely(error <= 0)) {
1290 if (error < 0) {
1291 dput(dentry);
1292 return ERR_PTR(error);
1293 } else if (!d_invalidate(dentry)) {
1294 dput(dentry);
1295 dentry = NULL;
1296 }
1297 }
1298 }
1299 }
Nick Pigginbaa03892010-08-18 04:37:31 +10001300
Miklos Szeredibad61182012-03-26 12:54:24 +02001301 if (!dentry) {
1302 dentry = d_alloc(dir, name);
1303 if (unlikely(!dentry))
1304 return ERR_PTR(-ENOMEM);
Nick Pigginbaa03892010-08-18 04:37:31 +10001305
Miklos Szeredibad61182012-03-26 12:54:24 +02001306 *need_lookup = true;
Nick Pigginbaa03892010-08-18 04:37:31 +10001307 }
1308 return dentry;
1309}
1310
1311/*
J. Bruce Fields13a2c3b2013-10-23 16:09:16 -04001312 * Call i_op->lookup on the dentry. The dentry must be negative and
1313 * unhashed.
Miklos Szeredibad61182012-03-26 12:54:24 +02001314 *
1315 * dir->d_inode->i_mutex must be held
Josef Bacik44396f42011-05-31 11:58:49 -04001316 */
Miklos Szeredibad61182012-03-26 12:54:24 +02001317static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
Al Viro72bd8662012-06-10 17:17:17 -04001318 unsigned int flags)
Josef Bacik44396f42011-05-31 11:58:49 -04001319{
Josef Bacik44396f42011-05-31 11:58:49 -04001320 struct dentry *old;
1321
1322 /* Don't create child dentry for a dead directory. */
Miklos Szeredibad61182012-03-26 12:54:24 +02001323 if (unlikely(IS_DEADDIR(dir))) {
Miklos Szeredie188dc02012-02-03 14:25:18 +01001324 dput(dentry);
Josef Bacik44396f42011-05-31 11:58:49 -04001325 return ERR_PTR(-ENOENT);
Miklos Szeredie188dc02012-02-03 14:25:18 +01001326 }
Josef Bacik44396f42011-05-31 11:58:49 -04001327
Al Viro72bd8662012-06-10 17:17:17 -04001328 old = dir->i_op->lookup(dir, dentry, flags);
Josef Bacik44396f42011-05-31 11:58:49 -04001329 if (unlikely(old)) {
1330 dput(dentry);
1331 dentry = old;
1332 }
1333 return dentry;
1334}
1335
Al Viroa3255542012-03-30 14:41:51 -04001336static struct dentry *__lookup_hash(struct qstr *name,
Al Viro72bd8662012-06-10 17:17:17 -04001337 struct dentry *base, unsigned int flags)
Al Viroa3255542012-03-30 14:41:51 -04001338{
Miklos Szeredibad61182012-03-26 12:54:24 +02001339 bool need_lookup;
Al Viroa3255542012-03-30 14:41:51 -04001340 struct dentry *dentry;
1341
Al Viro72bd8662012-06-10 17:17:17 -04001342 dentry = lookup_dcache(name, base, flags, &need_lookup);
Miklos Szeredibad61182012-03-26 12:54:24 +02001343 if (!need_lookup)
1344 return dentry;
Al Viroa3255542012-03-30 14:41:51 -04001345
Al Viro72bd8662012-06-10 17:17:17 -04001346 return lookup_real(base->d_inode, dentry, flags);
Al Viroa3255542012-03-30 14:41:51 -04001347}
1348
Josef Bacik44396f42011-05-31 11:58:49 -04001349/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 * It's more convoluted than I'd like it to be, but... it's still fairly
1351 * small and for now I'd prefer to have fast path as straight as possible.
1352 * It _is_ time-critical.
1353 */
Al Viroe97cdc82013-01-24 18:16:00 -05001354static int lookup_fast(struct nameidata *nd,
Miklos Szeredi697f5142012-05-21 17:30:05 +02001355 struct path *path, struct inode **inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356{
Jan Blunck4ac91372008-02-14 19:34:32 -08001357 struct vfsmount *mnt = nd->path.mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +11001358 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro5a18fff2011-03-11 04:44:53 -05001359 int need_reval = 1;
1360 int status = 1;
David Howells9875cf82011-01-14 18:45:21 +00001361 int err;
1362
Al Viro3cac2602009-08-13 18:27:43 +04001363 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001364 * Rename seqlock is not required here because in the off chance
1365 * of a false negative due to a concurrent rename, we're going to
1366 * do the non-racy lookup, below.
1367 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001368 if (nd->flags & LOOKUP_RCU) {
1369 unsigned seq;
Linus Torvaldsda53be12013-05-21 15:22:44 -07001370 dentry = __d_lookup_rcu(parent, &nd->last, &seq);
Al Viro5a18fff2011-03-11 04:44:53 -05001371 if (!dentry)
1372 goto unlazy;
1373
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001374 /*
1375 * This sequence count validates that the inode matches
1376 * the dentry name information from lookup.
1377 */
1378 *inode = dentry->d_inode;
1379 if (read_seqcount_retry(&dentry->d_seq, seq))
1380 return -ECHILD;
1381
1382 /*
1383 * This sequence count validates that the parent had no
1384 * changes while we did the lookup of the dentry above.
1385 *
1386 * The memory barrier in read_seqcount_begin of child is
1387 * enough, we can use __read_seqcount_retry here.
1388 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001389 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1390 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001391 nd->seq = seq;
Al Viro5a18fff2011-03-11 04:44:53 -05001392
Al Viro24643082011-02-15 01:26:22 -05001393 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
Al Viro4ce16ef32012-06-10 16:10:59 -04001394 status = d_revalidate(dentry, nd->flags);
Al Viro5a18fff2011-03-11 04:44:53 -05001395 if (unlikely(status <= 0)) {
1396 if (status != -ECHILD)
1397 need_reval = 0;
1398 goto unlazy;
1399 }
Al Viro24643082011-02-15 01:26:22 -05001400 }
Nick Piggin31e6b012011-01-07 17:49:52 +11001401 path->mnt = mnt;
1402 path->dentry = dentry;
Al Virod6e9bd22011-05-27 07:03:15 -04001403 if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1404 goto unlazy;
1405 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1406 goto unlazy;
1407 return 0;
Al Viro5a18fff2011-03-11 04:44:53 -05001408unlazy:
Al Viro19660af2011-03-25 10:32:48 -04001409 if (unlazy_walk(nd, dentry))
1410 return -ECHILD;
Al Viro5a18fff2011-03-11 04:44:53 -05001411 } else {
Al Viroe97cdc82013-01-24 18:16:00 -05001412 dentry = __d_lookup(parent, &nd->last);
Nick Piggin31e6b012011-01-07 17:49:52 +11001413 }
Al Viro5a18fff2011-03-11 04:44:53 -05001414
Al Viro81e6f522012-03-30 14:48:04 -04001415 if (unlikely(!dentry))
1416 goto need_lookup;
Al Viro5a18fff2011-03-11 04:44:53 -05001417
Al Viro5a18fff2011-03-11 04:44:53 -05001418 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
Al Viro4ce16ef32012-06-10 16:10:59 -04001419 status = d_revalidate(dentry, nd->flags);
Al Viro5a18fff2011-03-11 04:44:53 -05001420 if (unlikely(status <= 0)) {
1421 if (status < 0) {
1422 dput(dentry);
1423 return status;
1424 }
1425 if (!d_invalidate(dentry)) {
1426 dput(dentry);
Al Viro81e6f522012-03-30 14:48:04 -04001427 goto need_lookup;
Al Viro5a18fff2011-03-11 04:44:53 -05001428 }
1429 }
Miklos Szeredi697f5142012-05-21 17:30:05 +02001430
David Howells9875cf82011-01-14 18:45:21 +00001431 path->mnt = mnt;
1432 path->dentry = dentry;
1433 err = follow_managed(path, nd->flags);
Ian Kent89312212011-01-18 12:06:10 +08001434 if (unlikely(err < 0)) {
1435 path_put_conditional(path, nd);
David Howells9875cf82011-01-14 18:45:21 +00001436 return err;
Ian Kent89312212011-01-18 12:06:10 +08001437 }
Al Viroa3fbbde2011-11-07 21:21:26 +00001438 if (err)
1439 nd->flags |= LOOKUP_JUMPED;
David Howells9875cf82011-01-14 18:45:21 +00001440 *inode = path->dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 return 0;
Al Viro81e6f522012-03-30 14:48:04 -04001442
1443need_lookup:
Miklos Szeredi697f5142012-05-21 17:30:05 +02001444 return 1;
1445}
1446
1447/* Fast lookup failed, do it the slow way */
Al Virocc2a5272013-01-24 18:19:49 -05001448static int lookup_slow(struct nameidata *nd, struct path *path)
Miklos Szeredi697f5142012-05-21 17:30:05 +02001449{
1450 struct dentry *dentry, *parent;
1451 int err;
1452
1453 parent = nd->path.dentry;
Al Viro81e6f522012-03-30 14:48:04 -04001454 BUG_ON(nd->inode != parent->d_inode);
1455
1456 mutex_lock(&parent->d_inode->i_mutex);
Al Virocc2a5272013-01-24 18:19:49 -05001457 dentry = __lookup_hash(&nd->last, parent, nd->flags);
Al Viro81e6f522012-03-30 14:48:04 -04001458 mutex_unlock(&parent->d_inode->i_mutex);
1459 if (IS_ERR(dentry))
1460 return PTR_ERR(dentry);
Miklos Szeredi697f5142012-05-21 17:30:05 +02001461 path->mnt = nd->path.mnt;
1462 path->dentry = dentry;
1463 err = follow_managed(path, nd->flags);
1464 if (unlikely(err < 0)) {
1465 path_put_conditional(path, nd);
1466 return err;
1467 }
1468 if (err)
1469 nd->flags |= LOOKUP_JUMPED;
1470 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471}
1472
Al Viro52094c82011-02-21 21:34:47 -05001473static inline int may_lookup(struct nameidata *nd)
1474{
1475 if (nd->flags & LOOKUP_RCU) {
Al Viro4ad5abb2011-06-20 19:57:03 -04001476 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
Al Viro52094c82011-02-21 21:34:47 -05001477 if (err != -ECHILD)
1478 return err;
Al Viro19660af2011-03-25 10:32:48 -04001479 if (unlazy_walk(nd, NULL))
Al Viro52094c82011-02-21 21:34:47 -05001480 return -ECHILD;
1481 }
Al Viro4ad5abb2011-06-20 19:57:03 -04001482 return inode_permission(nd->inode, MAY_EXEC);
Al Viro52094c82011-02-21 21:34:47 -05001483}
1484
Al Viro9856fa12011-03-04 14:22:06 -05001485static inline int handle_dots(struct nameidata *nd, int type)
1486{
1487 if (type == LAST_DOTDOT) {
1488 if (nd->flags & LOOKUP_RCU) {
1489 if (follow_dotdot_rcu(nd))
1490 return -ECHILD;
1491 } else
1492 follow_dotdot(nd);
1493 }
1494 return 0;
1495}
1496
Al Viro951361f2011-03-04 14:44:37 -05001497static void terminate_walk(struct nameidata *nd)
1498{
1499 if (!(nd->flags & LOOKUP_RCU)) {
1500 path_put(&nd->path);
1501 } else {
1502 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001503 if (!(nd->flags & LOOKUP_ROOT))
1504 nd->root.mnt = NULL;
Al Viro8b61e742013-11-08 12:45:01 -05001505 rcu_read_unlock();
Al Viro951361f2011-03-04 14:44:37 -05001506 }
1507}
1508
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001509/*
1510 * Do we need to follow links? We _really_ want to be able
1511 * to do this check without having to look at inode->i_op,
1512 * so we keep a cache of "no, this doesn't need follow_link"
1513 * for the common case.
1514 */
David Howellsb18825a2013-09-12 19:22:53 +01001515static inline int should_follow_link(struct dentry *dentry, int follow)
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001516{
David Howellsb18825a2013-09-12 19:22:53 +01001517 return unlikely(d_is_symlink(dentry)) ? follow : 0;
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001518}
1519
Al Viroce57dfc2011-03-13 19:58:58 -04001520static inline int walk_component(struct nameidata *nd, struct path *path,
Al Viro21b9b072013-01-24 18:10:25 -05001521 int follow)
Al Viroce57dfc2011-03-13 19:58:58 -04001522{
1523 struct inode *inode;
1524 int err;
1525 /*
1526 * "." and ".." are special - ".." especially so because it has
1527 * to be able to know about the current root directory and
1528 * parent relationships.
1529 */
Al Viro21b9b072013-01-24 18:10:25 -05001530 if (unlikely(nd->last_type != LAST_NORM))
1531 return handle_dots(nd, nd->last_type);
Al Viroe97cdc82013-01-24 18:16:00 -05001532 err = lookup_fast(nd, path, &inode);
Al Viroce57dfc2011-03-13 19:58:58 -04001533 if (unlikely(err)) {
Miklos Szeredi697f5142012-05-21 17:30:05 +02001534 if (err < 0)
1535 goto out_err;
1536
Al Virocc2a5272013-01-24 18:19:49 -05001537 err = lookup_slow(nd, path);
Miklos Szeredi697f5142012-05-21 17:30:05 +02001538 if (err < 0)
1539 goto out_err;
1540
1541 inode = path->dentry->d_inode;
Al Viroce57dfc2011-03-13 19:58:58 -04001542 }
Miklos Szeredi697f5142012-05-21 17:30:05 +02001543 err = -ENOENT;
Al Virof0754e52014-04-19 12:30:58 -04001544 if (!inode || d_is_negative(path->dentry))
Miklos Szeredi697f5142012-05-21 17:30:05 +02001545 goto out_path_put;
1546
David Howellsb18825a2013-09-12 19:22:53 +01001547 if (should_follow_link(path->dentry, follow)) {
Al Viro19660af2011-03-25 10:32:48 -04001548 if (nd->flags & LOOKUP_RCU) {
1549 if (unlikely(unlazy_walk(nd, path->dentry))) {
Miklos Szeredi697f5142012-05-21 17:30:05 +02001550 err = -ECHILD;
1551 goto out_err;
Al Viro19660af2011-03-25 10:32:48 -04001552 }
1553 }
Al Viroce57dfc2011-03-13 19:58:58 -04001554 BUG_ON(inode != path->dentry->d_inode);
1555 return 1;
1556 }
1557 path_to_nameidata(path, nd);
1558 nd->inode = inode;
1559 return 0;
Miklos Szeredi697f5142012-05-21 17:30:05 +02001560
1561out_path_put:
1562 path_to_nameidata(path, nd);
1563out_err:
1564 terminate_walk(nd);
1565 return err;
Al Viroce57dfc2011-03-13 19:58:58 -04001566}
1567
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568/*
Al Virob3563792011-03-14 21:54:55 -04001569 * This limits recursive symlink follows to 8, while
1570 * limiting consecutive symlinks to 40.
1571 *
1572 * Without that kind of total limit, nasty chains of consecutive
1573 * symlinks can cause almost arbitrarily long lookups.
1574 */
1575static inline int nested_symlink(struct path *path, struct nameidata *nd)
1576{
1577 int res;
1578
Al Virob3563792011-03-14 21:54:55 -04001579 if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1580 path_put_conditional(path, nd);
1581 path_put(&nd->path);
1582 return -ELOOP;
1583 }
Erez Zadok1a4022f2011-05-21 01:19:59 -04001584 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
Al Virob3563792011-03-14 21:54:55 -04001585
1586 nd->depth++;
1587 current->link_count++;
1588
1589 do {
1590 struct path link = *path;
1591 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04001592
1593 res = follow_link(&link, nd, &cookie);
Al Viro6d7b5aa2012-06-10 04:15:17 -04001594 if (res)
1595 break;
Al Viro21b9b072013-01-24 18:10:25 -05001596 res = walk_component(nd, path, LOOKUP_FOLLOW);
Al Viro574197e2011-03-14 22:20:34 -04001597 put_link(nd, &link, cookie);
Al Virob3563792011-03-14 21:54:55 -04001598 } while (res > 0);
1599
1600 current->link_count--;
1601 nd->depth--;
1602 return res;
1603}
1604
1605/*
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001606 * We can do the critical dentry name comparison and hashing
1607 * operations one word at a time, but we are limited to:
1608 *
1609 * - Architectures with fast unaligned word accesses. We could
1610 * do a "get_unaligned()" if this helps and is sufficiently
1611 * fast.
1612 *
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001613 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1614 * do not trap on the (extremely unlikely) case of a page
1615 * crossing operation.
1616 *
1617 * - Furthermore, we need an efficient 64-bit compile for the
1618 * 64-bit case in order to generate the "number of bytes in
1619 * the final mask". Again, that could be replaced with a
1620 * efficient population count instruction or similar.
1621 */
1622#ifdef CONFIG_DCACHE_WORD_ACCESS
1623
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001624#include <asm/word-at-a-time.h>
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001625
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001626#ifdef CONFIG_64BIT
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001627
1628static inline unsigned int fold_hash(unsigned long hash)
1629{
Linus Torvaldsa8be8af2014-09-13 11:30:10 -07001630 return hash_64(hash, 32);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001631}
1632
1633#else /* 32-bit case */
1634
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001635#define fold_hash(x) (x)
1636
1637#endif
1638
1639unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1640{
1641 unsigned long a, mask;
1642 unsigned long hash = 0;
1643
1644 for (;;) {
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001645 a = load_unaligned_zeropad(name);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001646 if (len < sizeof(unsigned long))
1647 break;
1648 hash += a;
Al Virof132c5b2012-03-22 21:59:52 +00001649 hash *= 9;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001650 name += sizeof(unsigned long);
1651 len -= sizeof(unsigned long);
1652 if (!len)
1653 goto done;
1654 }
Will Deacona5c21dc2013-12-12 17:40:21 +00001655 mask = bytemask_from_count(len);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001656 hash += mask & a;
1657done:
1658 return fold_hash(hash);
1659}
1660EXPORT_SYMBOL(full_name_hash);
1661
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001662/*
1663 * Calculate the length and hash of the path component, and
1664 * return the length of the component;
1665 */
1666static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1667{
Linus Torvalds36126f82012-05-26 10:43:17 -07001668 unsigned long a, b, adata, bdata, mask, hash, len;
1669 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001670
1671 hash = a = 0;
1672 len = -sizeof(unsigned long);
1673 do {
1674 hash = (hash + a) * 9;
1675 len += sizeof(unsigned long);
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001676 a = load_unaligned_zeropad(name+len);
Linus Torvalds36126f82012-05-26 10:43:17 -07001677 b = a ^ REPEAT_BYTE('/');
1678 } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001679
Linus Torvalds36126f82012-05-26 10:43:17 -07001680 adata = prep_zero_mask(a, adata, &constants);
1681 bdata = prep_zero_mask(b, bdata, &constants);
1682
1683 mask = create_zero_mask(adata | bdata);
1684
1685 hash += a & zero_bytemask(mask);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001686 *hashp = fold_hash(hash);
1687
Linus Torvalds36126f82012-05-26 10:43:17 -07001688 return len + find_zero(mask);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001689}
1690
1691#else
1692
Linus Torvalds0145acc2012-03-02 14:32:59 -08001693unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1694{
1695 unsigned long hash = init_name_hash();
1696 while (len--)
1697 hash = partial_name_hash(*name++, hash);
1698 return end_name_hash(hash);
1699}
Linus Torvaldsae942ae2012-03-02 19:40:57 -08001700EXPORT_SYMBOL(full_name_hash);
Linus Torvalds0145acc2012-03-02 14:32:59 -08001701
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001702/*
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001703 * We know there's a real path component here of at least
1704 * one character.
1705 */
1706static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1707{
1708 unsigned long hash = init_name_hash();
1709 unsigned long len = 0, c;
1710
1711 c = (unsigned char)*name;
1712 do {
1713 len++;
1714 hash = partial_name_hash(c, hash);
1715 c = (unsigned char)name[len];
1716 } while (c && c != '/');
1717 *hashp = end_name_hash(hash);
1718 return len;
1719}
1720
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001721#endif
1722
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001723/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01001725 * This is the basic name resolution function, turning a pathname into
1726 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01001728 * Returns 0 and nd will have valid dentry and mnt on success.
1729 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 */
Al Viro6de88d72009-08-09 01:41:57 +04001731static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732{
1733 struct path next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
1736 while (*name=='/')
1737 name++;
1738 if (!*name)
Al Viro086e1832011-02-22 20:56:27 -05001739 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 /* At this point we know we have a real path component. */
1742 for(;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 struct qstr this;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001744 long len;
Al Virofe479a52011-02-22 15:10:03 -05001745 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Al Viro52094c82011-02-21 21:34:47 -05001747 err = may_lookup(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 if (err)
1749 break;
1750
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001751 len = hash_name(name, &this.hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 this.name = name;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001753 this.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Al Virofe479a52011-02-22 15:10:03 -05001755 type = LAST_NORM;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001756 if (name[0] == '.') switch (len) {
Al Virofe479a52011-02-22 15:10:03 -05001757 case 2:
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001758 if (name[1] == '.') {
Al Virofe479a52011-02-22 15:10:03 -05001759 type = LAST_DOTDOT;
Al Viro16c2cd72011-02-22 15:50:10 -05001760 nd->flags |= LOOKUP_JUMPED;
1761 }
Al Virofe479a52011-02-22 15:10:03 -05001762 break;
1763 case 1:
1764 type = LAST_DOT;
1765 }
Al Viro5a202bc2011-03-08 14:17:44 -05001766 if (likely(type == LAST_NORM)) {
1767 struct dentry *parent = nd->path.dentry;
Al Viro16c2cd72011-02-22 15:50:10 -05001768 nd->flags &= ~LOOKUP_JUMPED;
Al Viro5a202bc2011-03-08 14:17:44 -05001769 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
Linus Torvaldsda53be12013-05-21 15:22:44 -07001770 err = parent->d_op->d_hash(parent, &this);
Al Viro5a202bc2011-03-08 14:17:44 -05001771 if (err < 0)
1772 break;
1773 }
1774 }
Al Virofe479a52011-02-22 15:10:03 -05001775
Al Viro5f4a6a62013-01-24 18:04:22 -05001776 nd->last = this;
1777 nd->last_type = type;
1778
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001779 if (!name[len])
Al Viro5f4a6a62013-01-24 18:04:22 -05001780 return 0;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001781 /*
1782 * If it wasn't NUL, we know it was '/'. Skip that
1783 * slash, and continue until no more slashes.
1784 */
1785 do {
1786 len++;
1787 } while (unlikely(name[len] == '/'));
1788 if (!name[len])
Al Viro5f4a6a62013-01-24 18:04:22 -05001789 return 0;
1790
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001791 name += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Al Viro21b9b072013-01-24 18:10:25 -05001793 err = walk_component(nd, &next, LOOKUP_FOLLOW);
Al Viroce57dfc2011-03-13 19:58:58 -04001794 if (err < 0)
1795 return err;
Al Virofe479a52011-02-22 15:10:03 -05001796
Al Viroce57dfc2011-03-13 19:58:58 -04001797 if (err) {
Al Virob3563792011-03-14 21:54:55 -04001798 err = nested_symlink(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 if (err)
Al Viroa7472ba2011-03-04 14:39:30 -05001800 return err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001801 }
Miklos Szeredidb8bf542014-04-01 17:08:41 +02001802 if (!d_can_lookup(nd->path.dentry)) {
Al Viro5f4a6a62013-01-24 18:04:22 -05001803 err = -ENOTDIR;
1804 break;
1805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 }
Al Viro951361f2011-03-04 14:44:37 -05001807 terminate_walk(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 return err;
1809}
1810
Al Viro70e9b352011-03-05 21:12:22 -05001811static int path_init(int dfd, const char *name, unsigned int flags,
1812 struct nameidata *nd, struct file **fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813{
Prasanna Medaea3834d2005-04-29 16:00:17 +01001814 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
1816 nd->last_type = LAST_ROOT; /* if there are only slashes... */
Al Viro16c2cd72011-02-22 15:50:10 -05001817 nd->flags = flags | LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 nd->depth = 0;
Al Viro5b6ca022011-03-09 23:04:47 -05001819 if (flags & LOOKUP_ROOT) {
David Howellsb18825a2013-09-12 19:22:53 +01001820 struct dentry *root = nd->root.dentry;
1821 struct inode *inode = root->d_inode;
Al Viro73d049a2011-03-11 12:08:24 -05001822 if (*name) {
Miklos Szeredidb8bf542014-04-01 17:08:41 +02001823 if (!d_can_lookup(root))
Al Viro73d049a2011-03-11 12:08:24 -05001824 return -ENOTDIR;
1825 retval = inode_permission(inode, MAY_EXEC);
1826 if (retval)
1827 return retval;
1828 }
Al Viro5b6ca022011-03-09 23:04:47 -05001829 nd->path = nd->root;
1830 nd->inode = inode;
1831 if (flags & LOOKUP_RCU) {
Al Viro8b61e742013-11-08 12:45:01 -05001832 rcu_read_lock();
Al Viro5b6ca022011-03-09 23:04:47 -05001833 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro48a066e2013-09-29 22:06:07 -04001834 nd->m_seq = read_seqbegin(&mount_lock);
Al Viro5b6ca022011-03-09 23:04:47 -05001835 } else {
1836 path_get(&nd->path);
1837 }
1838 return 0;
1839 }
1840
Al Viro2a737872009-04-07 11:49:53 -04001841 nd->root.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
Al Viro48a066e2013-09-29 22:06:07 -04001843 nd->m_seq = read_seqbegin(&mount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 if (*name=='/') {
Al Viroe41f7d42011-02-22 14:02:58 -05001845 if (flags & LOOKUP_RCU) {
Al Viro8b61e742013-11-08 12:45:01 -05001846 rcu_read_lock();
Al Viroaed47132014-09-13 21:55:46 -04001847 nd->seq = set_root_rcu(nd);
Al Viroe41f7d42011-02-22 14:02:58 -05001848 } else {
1849 set_root(nd);
1850 path_get(&nd->root);
1851 }
Al Viro2a737872009-04-07 11:49:53 -04001852 nd->path = nd->root;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001853 } else if (dfd == AT_FDCWD) {
Al Viroe41f7d42011-02-22 14:02:58 -05001854 if (flags & LOOKUP_RCU) {
1855 struct fs_struct *fs = current->fs;
1856 unsigned seq;
1857
Al Viro8b61e742013-11-08 12:45:01 -05001858 rcu_read_lock();
Al Viroe41f7d42011-02-22 14:02:58 -05001859
1860 do {
1861 seq = read_seqcount_begin(&fs->seq);
1862 nd->path = fs->pwd;
1863 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1864 } while (read_seqcount_retry(&fs->seq, seq));
1865 } else {
1866 get_fs_pwd(current->fs, &nd->path);
1867 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001868 } else {
Jeff Layton582aa642012-12-11 08:56:16 -05001869 /* Caller must check execute permissions on the starting path component */
Al Viro2903ff02012-08-28 12:52:22 -04001870 struct fd f = fdget_raw(dfd);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001871 struct dentry *dentry;
1872
Al Viro2903ff02012-08-28 12:52:22 -04001873 if (!f.file)
1874 return -EBADF;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001875
Al Viro2903ff02012-08-28 12:52:22 -04001876 dentry = f.file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001877
Al Virof52e0c12011-03-14 18:56:51 -04001878 if (*name) {
Miklos Szeredidb8bf542014-04-01 17:08:41 +02001879 if (!d_can_lookup(dentry)) {
Al Viro2903ff02012-08-28 12:52:22 -04001880 fdput(f);
1881 return -ENOTDIR;
1882 }
Al Virof52e0c12011-03-14 18:56:51 -04001883 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001884
Al Viro2903ff02012-08-28 12:52:22 -04001885 nd->path = f.file->f_path;
Al Viroe41f7d42011-02-22 14:02:58 -05001886 if (flags & LOOKUP_RCU) {
Linus Torvalds9c225f22014-03-03 09:36:58 -08001887 if (f.flags & FDPUT_FPUT)
Al Viro2903ff02012-08-28 12:52:22 -04001888 *fp = f.file;
Al Viroe41f7d42011-02-22 14:02:58 -05001889 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro8b61e742013-11-08 12:45:01 -05001890 rcu_read_lock();
Al Viroe41f7d42011-02-22 14:02:58 -05001891 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001892 path_get(&nd->path);
1893 fdput(f);
Al Viroe41f7d42011-02-22 14:02:58 -05001894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 }
Al Viroe41f7d42011-02-22 14:02:58 -05001896
Nick Piggin31e6b012011-01-07 17:49:52 +11001897 nd->inode = nd->path.dentry->d_inode;
Al Viro9b4a9b12009-04-07 11:44:16 -04001898 return 0;
Al Viro9b4a9b12009-04-07 11:44:16 -04001899}
1900
Al Virobd92d7f2011-03-14 19:54:59 -04001901static inline int lookup_last(struct nameidata *nd, struct path *path)
1902{
1903 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1904 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1905
1906 nd->flags &= ~LOOKUP_PARENT;
Al Viro21b9b072013-01-24 18:10:25 -05001907 return walk_component(nd, path, nd->flags & LOOKUP_FOLLOW);
Al Virobd92d7f2011-03-14 19:54:59 -04001908}
1909
Al Viro9b4a9b12009-04-07 11:44:16 -04001910/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroee0827c2011-02-21 23:38:09 -05001911static int path_lookupat(int dfd, const char *name,
Al Viro9b4a9b12009-04-07 11:44:16 -04001912 unsigned int flags, struct nameidata *nd)
1913{
Al Viro70e9b352011-03-05 21:12:22 -05001914 struct file *base = NULL;
Al Virobd92d7f2011-03-14 19:54:59 -04001915 struct path path;
1916 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001917
1918 /*
1919 * Path walking is largely split up into 2 different synchronisation
1920 * schemes, rcu-walk and ref-walk (explained in
1921 * Documentation/filesystems/path-lookup.txt). These share much of the
1922 * path walk code, but some things particularly setup, cleanup, and
1923 * following mounts are sufficiently divergent that functions are
1924 * duplicated. Typically there is a function foo(), and its RCU
1925 * analogue, foo_rcu().
1926 *
1927 * -ECHILD is the error number of choice (just to avoid clashes) that
1928 * is returned if some aspect of an rcu-walk fails. Such an error must
1929 * be handled by restarting a traditional ref-walk (which will always
1930 * be able to complete).
1931 */
Al Virobd92d7f2011-03-14 19:54:59 -04001932 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
Al Viroee0827c2011-02-21 23:38:09 -05001933
Al Virobd92d7f2011-03-14 19:54:59 -04001934 if (unlikely(err))
1935 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001936
1937 current->total_link_count = 0;
Al Virobd92d7f2011-03-14 19:54:59 -04001938 err = link_path_walk(name, nd);
1939
1940 if (!err && !(flags & LOOKUP_PARENT)) {
Al Virobd92d7f2011-03-14 19:54:59 -04001941 err = lookup_last(nd, &path);
1942 while (err > 0) {
1943 void *cookie;
1944 struct path link = path;
Kees Cook800179c2012-07-25 17:29:07 -07001945 err = may_follow_link(&link, nd);
1946 if (unlikely(err))
1947 break;
Al Virobd92d7f2011-03-14 19:54:59 -04001948 nd->flags |= LOOKUP_PARENT;
Al Viro574197e2011-03-14 22:20:34 -04001949 err = follow_link(&link, nd, &cookie);
Al Viro6d7b5aa2012-06-10 04:15:17 -04001950 if (err)
1951 break;
1952 err = lookup_last(nd, &path);
Al Viro574197e2011-03-14 22:20:34 -04001953 put_link(nd, &link, cookie);
Al Virobd92d7f2011-03-14 19:54:59 -04001954 }
1955 }
Al Viroee0827c2011-02-21 23:38:09 -05001956
Al Viro9f1fafe2011-03-25 11:00:12 -04001957 if (!err)
1958 err = complete_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04001959
1960 if (!err && nd->flags & LOOKUP_DIRECTORY) {
Miklos Szeredidb8bf542014-04-01 17:08:41 +02001961 if (!d_can_lookup(nd->path.dentry)) {
Al Virobd92d7f2011-03-14 19:54:59 -04001962 path_put(&nd->path);
Al Virobd23a532011-03-23 09:56:30 -04001963 err = -ENOTDIR;
Al Virobd92d7f2011-03-14 19:54:59 -04001964 }
1965 }
Al Viro16c2cd72011-02-22 15:50:10 -05001966
Al Viro70e9b352011-03-05 21:12:22 -05001967 if (base)
1968 fput(base);
Al Viroee0827c2011-02-21 23:38:09 -05001969
Al Viro5b6ca022011-03-09 23:04:47 -05001970 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
Al Viro2a737872009-04-07 11:49:53 -04001971 path_put(&nd->root);
1972 nd->root.mnt = NULL;
1973 }
Al Virobd92d7f2011-03-14 19:54:59 -04001974 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001975}
Nick Piggin31e6b012011-01-07 17:49:52 +11001976
Jeff Layton873f1ee2012-10-10 15:25:29 -04001977static int filename_lookup(int dfd, struct filename *name,
1978 unsigned int flags, struct nameidata *nd)
1979{
1980 int retval = path_lookupat(dfd, name->name, flags | LOOKUP_RCU, nd);
1981 if (unlikely(retval == -ECHILD))
1982 retval = path_lookupat(dfd, name->name, flags, nd);
1983 if (unlikely(retval == -ESTALE))
1984 retval = path_lookupat(dfd, name->name,
1985 flags | LOOKUP_REVAL, nd);
1986
1987 if (likely(!retval))
Jeff Laytonadb5c242012-10-10 16:43:13 -04001988 audit_inode(name, nd->path.dentry, flags & LOOKUP_PARENT);
Jeff Layton873f1ee2012-10-10 15:25:29 -04001989 return retval;
1990}
1991
Al Viroee0827c2011-02-21 23:38:09 -05001992static int do_path_lookup(int dfd, const char *name,
1993 unsigned int flags, struct nameidata *nd)
1994{
Jeff Layton873f1ee2012-10-10 15:25:29 -04001995 struct filename filename = { .name = name };
Nick Piggin31e6b012011-01-07 17:49:52 +11001996
Jeff Layton873f1ee2012-10-10 15:25:29 -04001997 return filename_lookup(dfd, &filename, flags, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998}
1999
Al Viro79714f72012-06-15 03:01:42 +04002000/* does lookup, returns the object with parent locked */
2001struct dentry *kern_path_locked(const char *name, struct path *path)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002002{
Al Viro79714f72012-06-15 03:01:42 +04002003 struct nameidata nd;
2004 struct dentry *d;
2005 int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd);
2006 if (err)
2007 return ERR_PTR(err);
2008 if (nd.last_type != LAST_NORM) {
2009 path_put(&nd.path);
2010 return ERR_PTR(-EINVAL);
2011 }
2012 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Al Viro1e0ea002012-07-22 23:46:21 +04002013 d = __lookup_hash(&nd.last, nd.path.dentry, 0);
Al Viro79714f72012-06-15 03:01:42 +04002014 if (IS_ERR(d)) {
2015 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2016 path_put(&nd.path);
2017 return d;
2018 }
2019 *path = nd.path;
2020 return d;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002021}
2022
Al Virod1811462008-08-02 00:49:18 -04002023int kern_path(const char *name, unsigned int flags, struct path *path)
2024{
2025 struct nameidata nd;
2026 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
2027 if (!res)
2028 *path = nd.path;
2029 return res;
2030}
2031
Josef 'Jeff' Sipek16f182002007-07-19 01:48:18 -07002032/**
2033 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2034 * @dentry: pointer to dentry of the base directory
2035 * @mnt: pointer to vfs mount of the base directory
2036 * @name: pointer to file name
2037 * @flags: lookup flags
Al Viroe0a01242011-06-27 17:00:37 -04002038 * @path: pointer to struct path to fill
Josef 'Jeff' Sipek16f182002007-07-19 01:48:18 -07002039 */
2040int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2041 const char *name, unsigned int flags,
Al Viroe0a01242011-06-27 17:00:37 -04002042 struct path *path)
Josef 'Jeff' Sipek16f182002007-07-19 01:48:18 -07002043{
Al Viroe0a01242011-06-27 17:00:37 -04002044 struct nameidata nd;
2045 int err;
2046 nd.root.dentry = dentry;
2047 nd.root.mnt = mnt;
2048 BUG_ON(flags & LOOKUP_PARENT);
Al Viro5b6ca022011-03-09 23:04:47 -05002049 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
Al Viroe0a01242011-06-27 17:00:37 -04002050 err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
2051 if (!err)
2052 *path = nd.path;
2053 return err;
Josef 'Jeff' Sipek16f182002007-07-19 01:48:18 -07002054}
2055
James Morris057f6c02007-04-26 00:12:05 -07002056/*
2057 * Restricted form of lookup. Doesn't follow links, single-component only,
2058 * needs parent already locked. Doesn't follow mounts.
2059 * SMP-safe.
2060 */
Adrian Bunka244e162006-03-31 02:32:11 -08002061static struct dentry *lookup_hash(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062{
Al Viro72bd8662012-06-10 17:17:17 -04002063 return __lookup_hash(&nd->last, nd->path.dentry, nd->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064}
2065
Christoph Hellwigeead1912007-10-16 23:25:38 -07002066/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07002067 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07002068 * @name: pathname component to lookup
2069 * @base: base directory to lookup from
2070 * @len: maximum length @len should be interpreted to
2071 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07002072 * Note that this routine is purely a helper for filesystem usage and should
2073 * not be called by generic code. Also note that by using this function the
Christoph Hellwigeead1912007-10-16 23:25:38 -07002074 * nameidata argument is passed to the filesystem methods and a filesystem
2075 * using this helper needs to be prepared for that.
2076 */
James Morris057f6c02007-04-26 00:12:05 -07002077struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2078{
James Morris057f6c02007-04-26 00:12:05 -07002079 struct qstr this;
Al Viro6a96ba52011-03-07 23:49:20 -05002080 unsigned int c;
Miklos Szeredicda309d2012-03-26 12:54:21 +02002081 int err;
James Morris057f6c02007-04-26 00:12:05 -07002082
David Woodhouse2f9092e2009-04-20 23:18:37 +01002083 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
2084
Al Viro6a96ba52011-03-07 23:49:20 -05002085 this.name = name;
2086 this.len = len;
Linus Torvalds0145acc2012-03-02 14:32:59 -08002087 this.hash = full_name_hash(name, len);
Al Viro6a96ba52011-03-07 23:49:20 -05002088 if (!len)
2089 return ERR_PTR(-EACCES);
2090
Al Viro21d8a152012-11-29 22:17:21 -05002091 if (unlikely(name[0] == '.')) {
2092 if (len < 2 || (len == 2 && name[1] == '.'))
2093 return ERR_PTR(-EACCES);
2094 }
2095
Al Viro6a96ba52011-03-07 23:49:20 -05002096 while (len--) {
2097 c = *(const unsigned char *)name++;
2098 if (c == '/' || c == '\0')
2099 return ERR_PTR(-EACCES);
Al Viro6a96ba52011-03-07 23:49:20 -05002100 }
Al Viro5a202bc2011-03-08 14:17:44 -05002101 /*
2102 * See if the low-level filesystem might want
2103 * to use its own hash..
2104 */
2105 if (base->d_flags & DCACHE_OP_HASH) {
Linus Torvaldsda53be12013-05-21 15:22:44 -07002106 int err = base->d_op->d_hash(base, &this);
Al Viro5a202bc2011-03-08 14:17:44 -05002107 if (err < 0)
2108 return ERR_PTR(err);
2109 }
Christoph Hellwigeead1912007-10-16 23:25:38 -07002110
Miklos Szeredicda309d2012-03-26 12:54:21 +02002111 err = inode_permission(base->d_inode, MAY_EXEC);
2112 if (err)
2113 return ERR_PTR(err);
2114
Al Viro72bd8662012-06-10 17:17:17 -04002115 return __lookup_hash(&this, base, 0);
James Morris057f6c02007-04-26 00:12:05 -07002116}
2117
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002118int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2119 struct path *path, int *empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120{
Al Viro2d8f3032008-07-22 09:59:21 -04002121 struct nameidata nd;
Jeff Layton91a27b22012-10-10 15:25:28 -04002122 struct filename *tmp = getname_flags(name, flags, empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 int err = PTR_ERR(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 if (!IS_ERR(tmp)) {
Al Viro2d8f3032008-07-22 09:59:21 -04002125
2126 BUG_ON(flags & LOOKUP_PARENT);
2127
Jeff Layton873f1ee2012-10-10 15:25:29 -04002128 err = filename_lookup(dfd, tmp, flags, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 putname(tmp);
Al Viro2d8f3032008-07-22 09:59:21 -04002130 if (!err)
2131 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 }
2133 return err;
2134}
2135
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002136int user_path_at(int dfd, const char __user *name, unsigned flags,
2137 struct path *path)
2138{
Linus Torvaldsf7493e52012-03-22 16:10:40 -07002139 return user_path_at_empty(dfd, name, flags, path, NULL);
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002140}
2141
Jeff Layton873f1ee2012-10-10 15:25:29 -04002142/*
2143 * NB: most callers don't do anything directly with the reference to the
2144 * to struct filename, but the nd->last pointer points into the name string
2145 * allocated by getname. So we must hold the reference to it until all
2146 * path-walking is complete.
2147 */
Jeff Layton91a27b22012-10-10 15:25:28 -04002148static struct filename *
Jeff Layton9e790bd2012-12-11 12:10:09 -05002149user_path_parent(int dfd, const char __user *path, struct nameidata *nd,
2150 unsigned int flags)
Al Viro2ad94ae2008-07-21 09:32:51 -04002151{
Jeff Layton91a27b22012-10-10 15:25:28 -04002152 struct filename *s = getname(path);
Al Viro2ad94ae2008-07-21 09:32:51 -04002153 int error;
2154
Jeff Layton9e790bd2012-12-11 12:10:09 -05002155 /* only LOOKUP_REVAL is allowed in extra flags */
2156 flags &= LOOKUP_REVAL;
2157
Al Viro2ad94ae2008-07-21 09:32:51 -04002158 if (IS_ERR(s))
Jeff Layton91a27b22012-10-10 15:25:28 -04002159 return s;
Al Viro2ad94ae2008-07-21 09:32:51 -04002160
Jeff Layton9e790bd2012-12-11 12:10:09 -05002161 error = filename_lookup(dfd, s, flags | LOOKUP_PARENT, nd);
Jeff Layton91a27b22012-10-10 15:25:28 -04002162 if (error) {
Al Viro2ad94ae2008-07-21 09:32:51 -04002163 putname(s);
Jeff Layton91a27b22012-10-10 15:25:28 -04002164 return ERR_PTR(error);
2165 }
Al Viro2ad94ae2008-07-21 09:32:51 -04002166
Jeff Layton91a27b22012-10-10 15:25:28 -04002167 return s;
Al Viro2ad94ae2008-07-21 09:32:51 -04002168}
2169
Jeff Layton80334262013-07-26 06:23:25 -04002170/**
Al Viro197df042013-09-08 14:03:27 -04002171 * mountpoint_last - look up last component for umount
Jeff Layton80334262013-07-26 06:23:25 -04002172 * @nd: pathwalk nameidata - currently pointing at parent directory of "last"
2173 * @path: pointer to container for result
2174 *
2175 * This is a special lookup_last function just for umount. In this case, we
2176 * need to resolve the path without doing any revalidation.
2177 *
2178 * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
2179 * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
2180 * in almost all cases, this lookup will be served out of the dcache. The only
2181 * cases where it won't are if nd->last refers to a symlink or the path is
2182 * bogus and it doesn't exist.
2183 *
2184 * Returns:
2185 * -error: if there was an error during lookup. This includes -ENOENT if the
2186 * lookup found a negative dentry. The nd->path reference will also be
2187 * put in this case.
2188 *
2189 * 0: if we successfully resolved nd->path and found it to not to be a
2190 * symlink that needs to be followed. "path" will also be populated.
2191 * The nd->path reference will also be put.
2192 *
2193 * 1: if we successfully resolved nd->last and found it to be a symlink
2194 * that needs to be followed. "path" will be populated with the path
2195 * to the link, and nd->path will *not* be put.
2196 */
2197static int
Al Viro197df042013-09-08 14:03:27 -04002198mountpoint_last(struct nameidata *nd, struct path *path)
Jeff Layton80334262013-07-26 06:23:25 -04002199{
2200 int error = 0;
2201 struct dentry *dentry;
2202 struct dentry *dir = nd->path.dentry;
2203
Al Viro35759522013-09-08 13:41:33 -04002204 /* If we're in rcuwalk, drop out of it to handle last component */
2205 if (nd->flags & LOOKUP_RCU) {
2206 if (unlazy_walk(nd, NULL)) {
2207 error = -ECHILD;
2208 goto out;
2209 }
Jeff Layton80334262013-07-26 06:23:25 -04002210 }
2211
2212 nd->flags &= ~LOOKUP_PARENT;
2213
2214 if (unlikely(nd->last_type != LAST_NORM)) {
2215 error = handle_dots(nd, nd->last_type);
Al Viro35759522013-09-08 13:41:33 -04002216 if (error)
2217 goto out;
2218 dentry = dget(nd->path.dentry);
2219 goto done;
Jeff Layton80334262013-07-26 06:23:25 -04002220 }
2221
2222 mutex_lock(&dir->d_inode->i_mutex);
2223 dentry = d_lookup(dir, &nd->last);
2224 if (!dentry) {
2225 /*
2226 * No cached dentry. Mounted dentries are pinned in the cache,
2227 * so that means that this dentry is probably a symlink or the
2228 * path doesn't actually point to a mounted dentry.
2229 */
2230 dentry = d_alloc(dir, &nd->last);
2231 if (!dentry) {
2232 error = -ENOMEM;
Dave Jonesbcceeeb2013-09-10 17:04:25 -04002233 mutex_unlock(&dir->d_inode->i_mutex);
Al Viro35759522013-09-08 13:41:33 -04002234 goto out;
Jeff Layton80334262013-07-26 06:23:25 -04002235 }
Al Viro35759522013-09-08 13:41:33 -04002236 dentry = lookup_real(dir->d_inode, dentry, nd->flags);
2237 error = PTR_ERR(dentry);
Dave Jonesbcceeeb2013-09-10 17:04:25 -04002238 if (IS_ERR(dentry)) {
2239 mutex_unlock(&dir->d_inode->i_mutex);
Al Viro35759522013-09-08 13:41:33 -04002240 goto out;
Dave Jonesbcceeeb2013-09-10 17:04:25 -04002241 }
Jeff Layton80334262013-07-26 06:23:25 -04002242 }
2243 mutex_unlock(&dir->d_inode->i_mutex);
2244
Al Viro35759522013-09-08 13:41:33 -04002245done:
Al Virof0754e52014-04-19 12:30:58 -04002246 if (!dentry->d_inode || d_is_negative(dentry)) {
Al Viro35759522013-09-08 13:41:33 -04002247 error = -ENOENT;
2248 dput(dentry);
2249 goto out;
Jeff Layton80334262013-07-26 06:23:25 -04002250 }
Al Viro35759522013-09-08 13:41:33 -04002251 path->dentry = dentry;
Vasily Averin9b32e182014-07-21 12:30:23 +04002252 path->mnt = nd->path.mnt;
David Howellsb18825a2013-09-12 19:22:53 +01002253 if (should_follow_link(dentry, nd->flags & LOOKUP_FOLLOW))
Al Viro35759522013-09-08 13:41:33 -04002254 return 1;
Vasily Averin9b32e182014-07-21 12:30:23 +04002255 mntget(path->mnt);
Al Viro35759522013-09-08 13:41:33 -04002256 follow_mount(path);
2257 error = 0;
2258out:
Jeff Layton80334262013-07-26 06:23:25 -04002259 terminate_walk(nd);
2260 return error;
2261}
2262
2263/**
Al Viro197df042013-09-08 14:03:27 -04002264 * path_mountpoint - look up a path to be umounted
Jeff Layton80334262013-07-26 06:23:25 -04002265 * @dfd: directory file descriptor to start walk from
2266 * @name: full pathname to walk
Randy Dunlap606d6fe2013-10-19 14:56:55 -07002267 * @path: pointer to container for result
Jeff Layton80334262013-07-26 06:23:25 -04002268 * @flags: lookup flags
Jeff Layton80334262013-07-26 06:23:25 -04002269 *
2270 * Look up the given name, but don't attempt to revalidate the last component.
Randy Dunlap606d6fe2013-10-19 14:56:55 -07002271 * Returns 0 and "path" will be valid on success; Returns error otherwise.
Jeff Layton80334262013-07-26 06:23:25 -04002272 */
2273static int
Al Viro197df042013-09-08 14:03:27 -04002274path_mountpoint(int dfd, const char *name, struct path *path, unsigned int flags)
Jeff Layton80334262013-07-26 06:23:25 -04002275{
2276 struct file *base = NULL;
2277 struct nameidata nd;
2278 int err;
2279
2280 err = path_init(dfd, name, flags | LOOKUP_PARENT, &nd, &base);
2281 if (unlikely(err))
2282 return err;
2283
2284 current->total_link_count = 0;
2285 err = link_path_walk(name, &nd);
2286 if (err)
2287 goto out;
2288
Al Viro197df042013-09-08 14:03:27 -04002289 err = mountpoint_last(&nd, path);
Jeff Layton80334262013-07-26 06:23:25 -04002290 while (err > 0) {
2291 void *cookie;
2292 struct path link = *path;
2293 err = may_follow_link(&link, &nd);
2294 if (unlikely(err))
2295 break;
2296 nd.flags |= LOOKUP_PARENT;
2297 err = follow_link(&link, &nd, &cookie);
2298 if (err)
2299 break;
Al Viro197df042013-09-08 14:03:27 -04002300 err = mountpoint_last(&nd, path);
Jeff Layton80334262013-07-26 06:23:25 -04002301 put_link(&nd, &link, cookie);
2302 }
2303out:
2304 if (base)
2305 fput(base);
2306
2307 if (nd.root.mnt && !(nd.flags & LOOKUP_ROOT))
2308 path_put(&nd.root);
2309
2310 return err;
2311}
2312
Al Viro2d864652013-09-08 20:18:44 -04002313static int
2314filename_mountpoint(int dfd, struct filename *s, struct path *path,
2315 unsigned int flags)
2316{
2317 int error = path_mountpoint(dfd, s->name, path, flags | LOOKUP_RCU);
2318 if (unlikely(error == -ECHILD))
2319 error = path_mountpoint(dfd, s->name, path, flags);
2320 if (unlikely(error == -ESTALE))
2321 error = path_mountpoint(dfd, s->name, path, flags | LOOKUP_REVAL);
2322 if (likely(!error))
2323 audit_inode(s, path->dentry, 0);
2324 return error;
2325}
2326
Jeff Layton80334262013-07-26 06:23:25 -04002327/**
Al Viro197df042013-09-08 14:03:27 -04002328 * user_path_mountpoint_at - lookup a path from userland in order to umount it
Jeff Layton80334262013-07-26 06:23:25 -04002329 * @dfd: directory file descriptor
2330 * @name: pathname from userland
2331 * @flags: lookup flags
2332 * @path: pointer to container to hold result
2333 *
2334 * A umount is a special case for path walking. We're not actually interested
2335 * in the inode in this situation, and ESTALE errors can be a problem. We
2336 * simply want track down the dentry and vfsmount attached at the mountpoint
2337 * and avoid revalidating the last component.
2338 *
2339 * Returns 0 and populates "path" on success.
2340 */
2341int
Al Viro197df042013-09-08 14:03:27 -04002342user_path_mountpoint_at(int dfd, const char __user *name, unsigned int flags,
Jeff Layton80334262013-07-26 06:23:25 -04002343 struct path *path)
2344{
2345 struct filename *s = getname(name);
2346 int error;
Jeff Layton80334262013-07-26 06:23:25 -04002347 if (IS_ERR(s))
2348 return PTR_ERR(s);
Al Viro2d864652013-09-08 20:18:44 -04002349 error = filename_mountpoint(dfd, s, path, flags);
Jeff Layton80334262013-07-26 06:23:25 -04002350 putname(s);
2351 return error;
2352}
2353
Al Viro2d864652013-09-08 20:18:44 -04002354int
2355kern_path_mountpoint(int dfd, const char *name, struct path *path,
2356 unsigned int flags)
2357{
2358 struct filename s = {.name = name};
2359 return filename_mountpoint(dfd, &s, path, flags);
2360}
2361EXPORT_SYMBOL(kern_path_mountpoint);
2362
Miklos Szerediae3116532014-10-24 00:14:36 +02002363int __check_sticky(struct inode *dir, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364{
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002365 kuid_t fsuid = current_fsuid();
David Howellsda9592e2008-11-14 10:39:05 +11002366
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002367 if (uid_eq(inode->i_uid, fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 return 0;
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002369 if (uid_eq(dir->i_uid, fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 return 0;
Andy Lutomirski5bacea82014-06-10 12:45:42 -07002371 return !capable_wrt_inode_uidgid(inode, CAP_FOWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372}
Miklos Szerediae3116532014-10-24 00:14:36 +02002373EXPORT_SYMBOL(__check_sticky);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
2375/*
2376 * Check whether we can remove a link victim from directory dir, check
2377 * whether the type of victim is right.
2378 * 1. We can't do it if dir is read-only (done in permission())
2379 * 2. We should have write and exec permissions on dir
2380 * 3. We can't remove anything from append-only dir
2381 * 4. We can't do anything with immutable dir (done in permission())
2382 * 5. If the sticky bit on dir is set we should either
2383 * a. be owner of dir, or
2384 * b. be owner of victim, or
2385 * c. have CAP_FOWNER capability
2386 * 6. If the victim is append-only or immutable we can't do antyhing with
2387 * links pointing to it.
2388 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2389 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2390 * 9. We can't remove a root or mountpoint.
2391 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
2392 * nfs_async_unlink().
2393 */
David Howellsb18825a2013-09-12 19:22:53 +01002394static int may_delete(struct inode *dir, struct dentry *victim, bool isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395{
David Howellsb18825a2013-09-12 19:22:53 +01002396 struct inode *inode = victim->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 int error;
2398
David Howellsb18825a2013-09-12 19:22:53 +01002399 if (d_is_negative(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 return -ENOENT;
David Howellsb18825a2013-09-12 19:22:53 +01002401 BUG_ON(!inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
2403 BUG_ON(victim->d_parent->d_inode != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04002404 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
Al Virof419a2e2008-07-22 00:07:17 -04002406 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 if (error)
2408 return error;
2409 if (IS_APPEND(dir))
2410 return -EPERM;
David Howellsb18825a2013-09-12 19:22:53 +01002411
2412 if (check_sticky(dir, inode) || IS_APPEND(inode) ||
2413 IS_IMMUTABLE(inode) || IS_SWAPFILE(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 return -EPERM;
2415 if (isdir) {
Miklos Szeredidb8bf542014-04-01 17:08:41 +02002416 if (!d_is_dir(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 return -ENOTDIR;
2418 if (IS_ROOT(victim))
2419 return -EBUSY;
Miklos Szeredidb8bf542014-04-01 17:08:41 +02002420 } else if (d_is_dir(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 return -EISDIR;
2422 if (IS_DEADDIR(dir))
2423 return -ENOENT;
2424 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2425 return -EBUSY;
2426 return 0;
2427}
2428
2429/* Check whether we can create an object with dentry child in directory
2430 * dir.
2431 * 1. We can't do it if child already exists (open has special treatment for
2432 * this case, but since we are inlined it's OK)
2433 * 2. We can't do it if dir is read-only (done in permission())
2434 * 3. We should have write and exec permissions on dir
2435 * 4. We can't do it if dir is immutable (done in permission())
2436 */
Miklos Szeredia95164d2008-07-30 15:08:48 +02002437static inline int may_create(struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438{
Jeff Layton14e972b2013-05-08 10:25:58 -04002439 audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 if (child->d_inode)
2441 return -EEXIST;
2442 if (IS_DEADDIR(dir))
2443 return -ENOENT;
Al Virof419a2e2008-07-22 00:07:17 -04002444 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445}
2446
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447/*
2448 * p1 and p2 should be directories on the same fs.
2449 */
2450struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2451{
2452 struct dentry *p;
2453
2454 if (p1 == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07002455 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 return NULL;
2457 }
2458
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002459 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002461 p = d_ancestor(p2, p1);
2462 if (p) {
2463 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2464 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
2465 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 }
2467
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002468 p = d_ancestor(p1, p2);
2469 if (p) {
2470 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2471 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2472 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 }
2474
Ingo Molnarf2eace22006-07-03 00:25:05 -07002475 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Miklos Szeredi9ba06562014-10-27 15:42:01 +01002476 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 return NULL;
2478}
2479
2480void unlock_rename(struct dentry *p1, struct dentry *p2)
2481{
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002482 mutex_unlock(&p1->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 if (p1 != p2) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002484 mutex_unlock(&p2->d_inode->i_mutex);
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002485 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 }
2487}
2488
Al Viro4acdaf22011-07-26 01:42:34 -04002489int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viro312b63f2012-06-10 18:09:36 -04002490 bool want_excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002492 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 if (error)
2494 return error;
2495
Al Viroacfa4382008-12-04 10:06:33 -05002496 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 return -EACCES; /* shouldn't it be ENOSYS? */
2498 mode &= S_IALLUGO;
2499 mode |= S_IFREG;
2500 error = security_inode_create(dir, dentry, mode);
2501 if (error)
2502 return error;
Al Viro312b63f2012-06-10 18:09:36 -04002503 error = dir->i_op->create(dir, dentry, mode, want_excl);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002504 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002505 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 return error;
2507}
2508
Al Viro73d049a2011-03-11 12:08:24 -05002509static int may_open(struct path *path, int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002511 struct dentry *dentry = path->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 struct inode *inode = dentry->d_inode;
2513 int error;
2514
Al Virobcda7652011-03-13 16:42:14 -04002515 /* O_PATH? */
2516 if (!acc_mode)
2517 return 0;
2518
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 if (!inode)
2520 return -ENOENT;
2521
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002522 switch (inode->i_mode & S_IFMT) {
2523 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002525 case S_IFDIR:
2526 if (acc_mode & MAY_WRITE)
2527 return -EISDIR;
2528 break;
2529 case S_IFBLK:
2530 case S_IFCHR:
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002531 if (path->mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002533 /*FALLTHRU*/
2534 case S_IFIFO:
2535 case S_IFSOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002537 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08002538 }
Dave Hansenb41572e2007-10-16 23:31:14 -07002539
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002540 error = inode_permission(inode, acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07002541 if (error)
2542 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05002543
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 /*
2545 * An append-only file must be opened in append mode for writing.
2546 */
2547 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05002548 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05002549 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05002551 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 }
2553
2554 /* O_NOATIME can only be set by the owner or superuser */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07002555 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
Al Viro7715b522009-12-16 03:54:00 -05002556 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557
J. Bruce Fieldsf3c7691e2011-09-21 10:58:13 -04002558 return 0;
Al Viro7715b522009-12-16 03:54:00 -05002559}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560
Jeff Laytone1181ee2010-12-07 16:19:50 -05002561static int handle_truncate(struct file *filp)
Al Viro7715b522009-12-16 03:54:00 -05002562{
Jeff Laytone1181ee2010-12-07 16:19:50 -05002563 struct path *path = &filp->f_path;
Al Viro7715b522009-12-16 03:54:00 -05002564 struct inode *inode = path->dentry->d_inode;
2565 int error = get_write_access(inode);
2566 if (error)
2567 return error;
2568 /*
2569 * Refuse to truncate files with mandatory locks held on them.
2570 */
2571 error = locks_verify_locked(inode);
2572 if (!error)
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +09002573 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05002574 if (!error) {
2575 error = do_truncate(path->dentry, 0,
2576 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
Jeff Laytone1181ee2010-12-07 16:19:50 -05002577 filp);
Al Viro7715b522009-12-16 03:54:00 -05002578 }
2579 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04002580 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581}
2582
Dave Hansend57999e2008-02-15 14:37:27 -08002583static inline int open_to_namei_flags(int flag)
2584{
Al Viro8a5e9292011-06-25 19:15:54 -04002585 if ((flag & O_ACCMODE) == 3)
2586 flag--;
Dave Hansend57999e2008-02-15 14:37:27 -08002587 return flag;
2588}
2589
Miklos Szeredid18e9002012-06-05 15:10:17 +02002590static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode)
2591{
2592 int error = security_path_mknod(dir, dentry, mode, 0);
2593 if (error)
2594 return error;
2595
2596 error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
2597 if (error)
2598 return error;
2599
2600 return security_inode_create(dir->dentry->d_inode, dentry, mode);
2601}
2602
David Howells1acf0af2012-06-14 16:13:46 +01002603/*
2604 * Attempt to atomically look up, create and open a file from a negative
2605 * dentry.
2606 *
2607 * Returns 0 if successful. The file will have been created and attached to
2608 * @file by the filesystem calling finish_open().
2609 *
2610 * Returns 1 if the file was looked up only or didn't need creating. The
2611 * caller will need to perform the open themselves. @path will have been
2612 * updated to point to the new dentry. This may be negative.
2613 *
2614 * Returns an error code otherwise.
2615 */
Al Viro2675a4e2012-06-22 12:41:10 +04002616static int atomic_open(struct nameidata *nd, struct dentry *dentry,
2617 struct path *path, struct file *file,
2618 const struct open_flags *op,
Al Viro64894cf2012-07-31 00:53:35 +04002619 bool got_write, bool need_lookup,
Al Viro2675a4e2012-06-22 12:41:10 +04002620 int *opened)
Miklos Szeredid18e9002012-06-05 15:10:17 +02002621{
2622 struct inode *dir = nd->path.dentry->d_inode;
2623 unsigned open_flag = open_to_namei_flags(op->open_flag);
2624 umode_t mode;
2625 int error;
2626 int acc_mode;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002627 int create_error = 0;
2628 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
Miklos Szeredi116cc022013-09-16 14:52:05 +02002629 bool excl;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002630
2631 BUG_ON(dentry->d_inode);
2632
2633 /* Don't create child dentry for a dead directory. */
2634 if (unlikely(IS_DEADDIR(dir))) {
Al Viro2675a4e2012-06-22 12:41:10 +04002635 error = -ENOENT;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002636 goto out;
2637 }
2638
Miklos Szeredi62b259d2012-08-15 13:01:24 +02002639 mode = op->mode;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002640 if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
2641 mode &= ~current_umask();
2642
Miklos Szeredi116cc022013-09-16 14:52:05 +02002643 excl = (open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT);
2644 if (excl)
Miklos Szeredid18e9002012-06-05 15:10:17 +02002645 open_flag &= ~O_TRUNC;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002646
2647 /*
2648 * Checking write permission is tricky, bacuse we don't know if we are
2649 * going to actually need it: O_CREAT opens should work as long as the
2650 * file exists. But checking existence breaks atomicity. The trick is
2651 * to check access and if not granted clear O_CREAT from the flags.
2652 *
2653 * Another problem is returing the "right" error value (e.g. for an
2654 * O_EXCL open we want to return EEXIST not EROFS).
2655 */
Al Viro64894cf2012-07-31 00:53:35 +04002656 if (((open_flag & (O_CREAT | O_TRUNC)) ||
2657 (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) {
2658 if (!(open_flag & O_CREAT)) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002659 /*
2660 * No O_CREATE -> atomicity not a requirement -> fall
2661 * back to lookup + open
2662 */
2663 goto no_open;
2664 } else if (open_flag & (O_EXCL | O_TRUNC)) {
2665 /* Fall back and fail with the right error */
Al Viro64894cf2012-07-31 00:53:35 +04002666 create_error = -EROFS;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002667 goto no_open;
2668 } else {
2669 /* No side effects, safe to clear O_CREAT */
Al Viro64894cf2012-07-31 00:53:35 +04002670 create_error = -EROFS;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002671 open_flag &= ~O_CREAT;
2672 }
2673 }
2674
2675 if (open_flag & O_CREAT) {
Miklos Szeredi38227f72012-08-15 13:01:24 +02002676 error = may_o_create(&nd->path, dentry, mode);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002677 if (error) {
2678 create_error = error;
2679 if (open_flag & O_EXCL)
2680 goto no_open;
2681 open_flag &= ~O_CREAT;
2682 }
2683 }
2684
2685 if (nd->flags & LOOKUP_DIRECTORY)
2686 open_flag |= O_DIRECTORY;
2687
Al Viro30d90492012-06-22 12:40:19 +04002688 file->f_path.dentry = DENTRY_NOT_SET;
2689 file->f_path.mnt = nd->path.mnt;
2690 error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode,
Al Viro47237682012-06-10 05:01:45 -04002691 opened);
Al Virod9585272012-06-22 12:39:14 +04002692 if (error < 0) {
Al Virod9585272012-06-22 12:39:14 +04002693 if (create_error && error == -ENOENT)
2694 error = create_error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002695 goto out;
2696 }
2697
Al Virod9585272012-06-22 12:39:14 +04002698 if (error) { /* returned 1, that is */
Al Viro30d90492012-06-22 12:40:19 +04002699 if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
Al Viro2675a4e2012-06-22 12:41:10 +04002700 error = -EIO;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002701 goto out;
2702 }
Al Viro30d90492012-06-22 12:40:19 +04002703 if (file->f_path.dentry) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002704 dput(dentry);
Al Viro30d90492012-06-22 12:40:19 +04002705 dentry = file->f_path.dentry;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002706 }
Al Viro03da6332013-09-16 19:22:33 -04002707 if (*opened & FILE_CREATED)
2708 fsnotify_create(dir, dentry);
2709 if (!dentry->d_inode) {
2710 WARN_ON(*opened & FILE_CREATED);
2711 if (create_error) {
2712 error = create_error;
2713 goto out;
2714 }
2715 } else {
2716 if (excl && !(*opened & FILE_CREATED)) {
2717 error = -EEXIST;
2718 goto out;
2719 }
Sage Weil62b2ce92012-08-15 13:30:12 -07002720 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02002721 goto looked_up;
2722 }
2723
2724 /*
2725 * We didn't have the inode before the open, so check open permission
2726 * here.
2727 */
Al Viro03da6332013-09-16 19:22:33 -04002728 acc_mode = op->acc_mode;
2729 if (*opened & FILE_CREATED) {
2730 WARN_ON(!(open_flag & O_CREAT));
2731 fsnotify_create(dir, dentry);
2732 acc_mode = MAY_OPEN;
2733 }
Al Viro2675a4e2012-06-22 12:41:10 +04002734 error = may_open(&file->f_path, acc_mode, open_flag);
2735 if (error)
2736 fput(file);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002737
2738out:
2739 dput(dentry);
Al Viro2675a4e2012-06-22 12:41:10 +04002740 return error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002741
Miklos Szeredid18e9002012-06-05 15:10:17 +02002742no_open:
2743 if (need_lookup) {
Al Viro72bd8662012-06-10 17:17:17 -04002744 dentry = lookup_real(dir, dentry, nd->flags);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002745 if (IS_ERR(dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04002746 return PTR_ERR(dentry);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002747
2748 if (create_error) {
2749 int open_flag = op->open_flag;
2750
Al Viro2675a4e2012-06-22 12:41:10 +04002751 error = create_error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002752 if ((open_flag & O_EXCL)) {
2753 if (!dentry->d_inode)
2754 goto out;
2755 } else if (!dentry->d_inode) {
2756 goto out;
2757 } else if ((open_flag & O_TRUNC) &&
2758 S_ISREG(dentry->d_inode->i_mode)) {
2759 goto out;
2760 }
2761 /* will fail later, go on to get the right error */
2762 }
2763 }
2764looked_up:
2765 path->dentry = dentry;
2766 path->mnt = nd->path.mnt;
Al Viro2675a4e2012-06-22 12:41:10 +04002767 return 1;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002768}
2769
Nick Piggin31e6b012011-01-07 17:49:52 +11002770/*
David Howells1acf0af2012-06-14 16:13:46 +01002771 * Look up and maybe create and open the last component.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002772 *
2773 * Must be called with i_mutex held on parent.
2774 *
David Howells1acf0af2012-06-14 16:13:46 +01002775 * Returns 0 if the file was successfully atomically created (if necessary) and
2776 * opened. In this case the file will be returned attached to @file.
2777 *
2778 * Returns 1 if the file was not completely opened at this time, though lookups
2779 * and creations will have been performed and the dentry returned in @path will
2780 * be positive upon return if O_CREAT was specified. If O_CREAT wasn't
2781 * specified then a negative dentry may be returned.
2782 *
2783 * An error code is returned otherwise.
2784 *
2785 * FILE_CREATE will be set in @*opened if the dentry was created and will be
2786 * cleared otherwise prior to returning.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002787 */
Al Viro2675a4e2012-06-22 12:41:10 +04002788static int lookup_open(struct nameidata *nd, struct path *path,
2789 struct file *file,
2790 const struct open_flags *op,
Al Viro64894cf2012-07-31 00:53:35 +04002791 bool got_write, int *opened)
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002792{
2793 struct dentry *dir = nd->path.dentry;
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002794 struct inode *dir_inode = dir->d_inode;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002795 struct dentry *dentry;
2796 int error;
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002797 bool need_lookup;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002798
Al Viro47237682012-06-10 05:01:45 -04002799 *opened &= ~FILE_CREATED;
Al Viro201f9562012-06-22 12:42:10 +04002800 dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002801 if (IS_ERR(dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04002802 return PTR_ERR(dentry);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002803
Miklos Szeredid18e9002012-06-05 15:10:17 +02002804 /* Cached positive dentry: will open in f_op->open */
2805 if (!need_lookup && dentry->d_inode)
2806 goto out_no_open;
2807
2808 if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) {
Al Viro64894cf2012-07-31 00:53:35 +04002809 return atomic_open(nd, dentry, path, file, op, got_write,
Al Viro47237682012-06-10 05:01:45 -04002810 need_lookup, opened);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002811 }
2812
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002813 if (need_lookup) {
2814 BUG_ON(dentry->d_inode);
2815
Al Viro72bd8662012-06-10 17:17:17 -04002816 dentry = lookup_real(dir_inode, dentry, nd->flags);
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002817 if (IS_ERR(dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04002818 return PTR_ERR(dentry);
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002819 }
2820
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002821 /* Negative dentry, just create the file */
2822 if (!dentry->d_inode && (op->open_flag & O_CREAT)) {
2823 umode_t mode = op->mode;
2824 if (!IS_POSIXACL(dir->d_inode))
2825 mode &= ~current_umask();
2826 /*
2827 * This write is needed to ensure that a
2828 * rw->ro transition does not occur between
2829 * the time when the file is created and when
2830 * a permanent write count is taken through
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02002831 * the 'struct file' in finish_open().
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002832 */
Al Viro64894cf2012-07-31 00:53:35 +04002833 if (!got_write) {
2834 error = -EROFS;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002835 goto out_dput;
Al Viro64894cf2012-07-31 00:53:35 +04002836 }
Al Viro47237682012-06-10 05:01:45 -04002837 *opened |= FILE_CREATED;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002838 error = security_path_mknod(&nd->path, dentry, mode, 0);
2839 if (error)
2840 goto out_dput;
Al Viro312b63f2012-06-10 18:09:36 -04002841 error = vfs_create(dir->d_inode, dentry, mode,
2842 nd->flags & LOOKUP_EXCL);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002843 if (error)
2844 goto out_dput;
2845 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02002846out_no_open:
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002847 path->dentry = dentry;
2848 path->mnt = nd->path.mnt;
Al Viro2675a4e2012-06-22 12:41:10 +04002849 return 1;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002850
2851out_dput:
2852 dput(dentry);
Al Viro2675a4e2012-06-22 12:41:10 +04002853 return error;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002854}
2855
2856/*
Al Virofe2d35f2011-03-05 22:58:25 -05002857 * Handle the last step of open()
Nick Piggin31e6b012011-01-07 17:49:52 +11002858 */
Al Viro2675a4e2012-06-22 12:41:10 +04002859static int do_last(struct nameidata *nd, struct path *path,
2860 struct file *file, const struct open_flags *op,
Jeff Layton669abf42012-10-10 16:43:10 -04002861 int *opened, struct filename *name)
Al Virofb1cc552009-12-24 01:58:28 -05002862{
Al Viroa1e28032009-12-24 02:12:06 -05002863 struct dentry *dir = nd->path.dentry;
Al Viroca344a892011-03-09 00:36:45 -05002864 int open_flag = op->open_flag;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002865 bool will_truncate = (open_flag & O_TRUNC) != 0;
Al Viro64894cf2012-07-31 00:53:35 +04002866 bool got_write = false;
Al Virobcda7652011-03-13 16:42:14 -04002867 int acc_mode = op->acc_mode;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02002868 struct inode *inode;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002869 bool symlink_ok = false;
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02002870 struct path save_parent = { .dentry = NULL, .mnt = NULL };
2871 bool retried = false;
Al Viro16c2cd72011-02-22 15:50:10 -05002872 int error;
Al Virofb1cc552009-12-24 01:58:28 -05002873
Al Viroc3e380b2011-02-23 13:39:45 -05002874 nd->flags &= ~LOOKUP_PARENT;
2875 nd->flags |= op->intent;
2876
Al Virobc77daa2013-06-06 09:12:33 -04002877 if (nd->last_type != LAST_NORM) {
Al Virofe2d35f2011-03-05 22:58:25 -05002878 error = handle_dots(nd, nd->last_type);
2879 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04002880 return error;
Miklos Szeredie83db162012-06-05 15:10:29 +02002881 goto finish_open;
Al Viro1f36f772009-12-26 10:56:19 -05002882 }
Al Viro67ee3ad2009-12-26 07:01:01 -05002883
Al Viroca344a892011-03-09 00:36:45 -05002884 if (!(open_flag & O_CREAT)) {
Al Virofe2d35f2011-03-05 22:58:25 -05002885 if (nd->last.name[nd->last.len])
2886 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
Al Virobcda7652011-03-13 16:42:14 -04002887 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002888 symlink_ok = true;
Al Virofe2d35f2011-03-05 22:58:25 -05002889 /* we _can_ be in RCU mode here */
Al Viroe97cdc82013-01-24 18:16:00 -05002890 error = lookup_fast(nd, path, &inode);
Miklos Szeredi71574862012-06-05 15:10:14 +02002891 if (likely(!error))
2892 goto finish_lookup;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02002893
Miklos Szeredi71574862012-06-05 15:10:14 +02002894 if (error < 0)
Al Viro2675a4e2012-06-22 12:41:10 +04002895 goto out;
Miklos Szeredi37d7fff2012-06-05 15:10:12 +02002896
Miklos Szeredi71574862012-06-05 15:10:14 +02002897 BUG_ON(nd->inode != dir->d_inode);
Miklos Szeredib6183df2012-06-05 15:10:13 +02002898 } else {
2899 /* create side of things */
2900 /*
2901 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
2902 * has been cleared when we got to the last component we are
2903 * about to look up
2904 */
2905 error = complete_walk(nd);
2906 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04002907 return error;
Miklos Szeredib6183df2012-06-05 15:10:13 +02002908
Jeff Layton33e22082013-04-12 15:16:32 -04002909 audit_inode(name, dir, LOOKUP_PARENT);
Miklos Szeredib6183df2012-06-05 15:10:13 +02002910 error = -EISDIR;
2911 /* trailing slashes? */
2912 if (nd->last.name[nd->last.len])
Al Viro2675a4e2012-06-22 12:41:10 +04002913 goto out;
Al Virofe2d35f2011-03-05 22:58:25 -05002914 }
2915
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02002916retry_lookup:
Al Viro64894cf2012-07-31 00:53:35 +04002917 if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
2918 error = mnt_want_write(nd->path.mnt);
2919 if (!error)
2920 got_write = true;
2921 /*
2922 * do _not_ fail yet - we might not need that or fail with
2923 * a different error; let lookup_open() decide; we'll be
2924 * dropping this one anyway.
2925 */
2926 }
Al Viroa1e28032009-12-24 02:12:06 -05002927 mutex_lock(&dir->d_inode->i_mutex);
Al Viro64894cf2012-07-31 00:53:35 +04002928 error = lookup_open(nd, path, file, op, got_write, opened);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002929 mutex_unlock(&dir->d_inode->i_mutex);
Al Viroa1e28032009-12-24 02:12:06 -05002930
Al Viro2675a4e2012-06-22 12:41:10 +04002931 if (error <= 0) {
2932 if (error)
Miklos Szeredid18e9002012-06-05 15:10:17 +02002933 goto out;
2934
Al Viro47237682012-06-10 05:01:45 -04002935 if ((*opened & FILE_CREATED) ||
Al Viro496ad9a2013-01-23 17:07:38 -05002936 !S_ISREG(file_inode(file)->i_mode))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002937 will_truncate = false;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002938
Jeff Laytonadb5c242012-10-10 16:43:13 -04002939 audit_inode(name, file->f_path.dentry, 0);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002940 goto opened;
2941 }
Al Virofb1cc552009-12-24 01:58:28 -05002942
Al Viro47237682012-06-10 05:01:45 -04002943 if (*opened & FILE_CREATED) {
Al Viro9b44f1b2011-03-09 00:17:27 -05002944 /* Don't check for write permission, don't truncate */
Al Viroca344a892011-03-09 00:36:45 -05002945 open_flag &= ~O_TRUNC;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002946 will_truncate = false;
Al Virobcda7652011-03-13 16:42:14 -04002947 acc_mode = MAY_OPEN;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002948 path_to_nameidata(path, nd);
Miklos Szeredie83db162012-06-05 15:10:29 +02002949 goto finish_open_created;
Al Virofb1cc552009-12-24 01:58:28 -05002950 }
2951
2952 /*
Jeff Layton3134f372012-07-25 10:19:47 -04002953 * create/update audit record if it already exists.
Al Virofb1cc552009-12-24 01:58:28 -05002954 */
David Howellsb18825a2013-09-12 19:22:53 +01002955 if (d_is_positive(path->dentry))
Jeff Laytonadb5c242012-10-10 16:43:13 -04002956 audit_inode(name, path->dentry, 0);
Al Virofb1cc552009-12-24 01:58:28 -05002957
Miklos Szeredid18e9002012-06-05 15:10:17 +02002958 /*
2959 * If atomic_open() acquired write access it is dropped now due to
2960 * possible mount and symlink following (this might be optimized away if
2961 * necessary...)
2962 */
Al Viro64894cf2012-07-31 00:53:35 +04002963 if (got_write) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002964 mnt_drop_write(nd->path.mnt);
Al Viro64894cf2012-07-31 00:53:35 +04002965 got_write = false;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002966 }
2967
Al Virofb1cc552009-12-24 01:58:28 -05002968 error = -EEXIST;
Al Virof8310c52012-07-30 11:50:30 +04002969 if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))
Al Virofb1cc552009-12-24 01:58:28 -05002970 goto exit_dput;
2971
David Howells9875cf82011-01-14 18:45:21 +00002972 error = follow_managed(path, nd->flags);
2973 if (error < 0)
2974 goto exit_dput;
Al Virofb1cc552009-12-24 01:58:28 -05002975
Al Viroa3fbbde2011-11-07 21:21:26 +00002976 if (error)
2977 nd->flags |= LOOKUP_JUMPED;
2978
Miklos Szeredidecf3402012-05-21 17:30:08 +02002979 BUG_ON(nd->flags & LOOKUP_RCU);
2980 inode = path->dentry->d_inode;
Miklos Szeredi5f5daac2012-05-21 17:30:14 +02002981finish_lookup:
2982 /* we _can_ be in RCU mode here */
Al Virofb1cc552009-12-24 01:58:28 -05002983 error = -ENOENT;
Al Virof0754e52014-04-19 12:30:58 -04002984 if (!inode || d_is_negative(path->dentry)) {
Miklos Szeredi54c33e72012-05-21 17:30:10 +02002985 path_to_nameidata(path, nd);
Al Viro2675a4e2012-06-22 12:41:10 +04002986 goto out;
Miklos Szeredi54c33e72012-05-21 17:30:10 +02002987 }
Al Viro9e67f362009-12-26 07:04:50 -05002988
David Howellsb18825a2013-09-12 19:22:53 +01002989 if (should_follow_link(path->dentry, !symlink_ok)) {
Miklos Szeredid45ea862012-05-21 17:30:09 +02002990 if (nd->flags & LOOKUP_RCU) {
2991 if (unlikely(unlazy_walk(nd, path->dentry))) {
2992 error = -ECHILD;
Al Viro2675a4e2012-06-22 12:41:10 +04002993 goto out;
Miklos Szeredid45ea862012-05-21 17:30:09 +02002994 }
2995 }
2996 BUG_ON(inode != path->dentry->d_inode);
Al Viro2675a4e2012-06-22 12:41:10 +04002997 return 1;
Miklos Szeredid45ea862012-05-21 17:30:09 +02002998 }
Al Virofb1cc552009-12-24 01:58:28 -05002999
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003000 if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) {
3001 path_to_nameidata(path, nd);
3002 } else {
3003 save_parent.dentry = nd->path.dentry;
3004 save_parent.mnt = mntget(path->mnt);
3005 nd->path.dentry = path->dentry;
3006
3007 }
Miklos Szeredidecf3402012-05-21 17:30:08 +02003008 nd->inode = inode;
Al Viroa3fbbde2011-11-07 21:21:26 +00003009 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
Al Virobc77daa2013-06-06 09:12:33 -04003010finish_open:
Al Viroa3fbbde2011-11-07 21:21:26 +00003011 error = complete_walk(nd);
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003012 if (error) {
3013 path_put(&save_parent);
Al Viro2675a4e2012-06-22 12:41:10 +04003014 return error;
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003015 }
Al Virobc77daa2013-06-06 09:12:33 -04003016 audit_inode(name, nd->path.dentry, 0);
Al Virofb1cc552009-12-24 01:58:28 -05003017 error = -EISDIR;
Miklos Szeredidb8bf542014-04-01 17:08:41 +02003018 if ((open_flag & O_CREAT) && d_is_dir(nd->path.dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04003019 goto out;
Miklos Szerediaf2f5542012-05-21 17:30:11 +02003020 error = -ENOTDIR;
Miklos Szeredidb8bf542014-04-01 17:08:41 +02003021 if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04003022 goto out;
Al Viro6c0d46c2011-03-09 00:59:59 -05003023 if (!S_ISREG(nd->inode->i_mode))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003024 will_truncate = false;
Al Viro6c0d46c2011-03-09 00:59:59 -05003025
Al Viro0f9d1a12011-03-09 00:13:14 -05003026 if (will_truncate) {
3027 error = mnt_want_write(nd->path.mnt);
3028 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003029 goto out;
Al Viro64894cf2012-07-31 00:53:35 +04003030 got_write = true;
Al Viro0f9d1a12011-03-09 00:13:14 -05003031 }
Miklos Szeredie83db162012-06-05 15:10:29 +02003032finish_open_created:
Al Virobcda7652011-03-13 16:42:14 -04003033 error = may_open(&nd->path, acc_mode, open_flag);
Al Viroca344a892011-03-09 00:36:45 -05003034 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003035 goto out;
Miklos Szeredi71732032014-10-24 00:14:35 +02003036
3037 BUG_ON(*opened & FILE_OPENED); /* once it's opened, it's opened */
3038 error = vfs_open(&nd->path, file, current_cred());
3039 if (!error) {
3040 *opened |= FILE_OPENED;
3041 } else {
Al Viro30d90492012-06-22 12:40:19 +04003042 if (error == -EOPENSTALE)
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003043 goto stale_open;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003044 goto out;
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003045 }
Miklos Szeredia8277b92012-06-05 15:10:32 +02003046opened:
Al Viro2675a4e2012-06-22 12:41:10 +04003047 error = open_check_o_direct(file);
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003048 if (error)
3049 goto exit_fput;
Al Viro2675a4e2012-06-22 12:41:10 +04003050 error = ima_file_check(file, op->acc_mode);
Miklos Szerediaa4caad2012-06-05 15:10:28 +02003051 if (error)
3052 goto exit_fput;
3053
3054 if (will_truncate) {
Al Viro2675a4e2012-06-22 12:41:10 +04003055 error = handle_truncate(file);
Miklos Szerediaa4caad2012-06-05 15:10:28 +02003056 if (error)
3057 goto exit_fput;
Al Viro0f9d1a12011-03-09 00:13:14 -05003058 }
Al Viroca344a892011-03-09 00:36:45 -05003059out:
Al Viro64894cf2012-07-31 00:53:35 +04003060 if (got_write)
Al Viro0f9d1a12011-03-09 00:13:14 -05003061 mnt_drop_write(nd->path.mnt);
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003062 path_put(&save_parent);
Miklos Szeredie276ae62012-05-21 17:30:06 +02003063 terminate_walk(nd);
Al Viro2675a4e2012-06-22 12:41:10 +04003064 return error;
Al Virofb1cc552009-12-24 01:58:28 -05003065
Al Virofb1cc552009-12-24 01:58:28 -05003066exit_dput:
3067 path_put_conditional(path, nd);
Al Viroca344a892011-03-09 00:36:45 -05003068 goto out;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003069exit_fput:
Al Viro2675a4e2012-06-22 12:41:10 +04003070 fput(file);
3071 goto out;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003072
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003073stale_open:
3074 /* If no saved parent or already retried then can't retry */
3075 if (!save_parent.dentry || retried)
3076 goto out;
3077
3078 BUG_ON(save_parent.dentry != dir);
3079 path_put(&nd->path);
3080 nd->path = save_parent;
3081 nd->inode = dir->d_inode;
3082 save_parent.mnt = NULL;
3083 save_parent.dentry = NULL;
Al Viro64894cf2012-07-31 00:53:35 +04003084 if (got_write) {
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003085 mnt_drop_write(nd->path.mnt);
Al Viro64894cf2012-07-31 00:53:35 +04003086 got_write = false;
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003087 }
3088 retried = true;
3089 goto retry_lookup;
Al Virofb1cc552009-12-24 01:58:28 -05003090}
3091
Al Viro60545d02013-06-07 01:20:27 -04003092static int do_tmpfile(int dfd, struct filename *pathname,
3093 struct nameidata *nd, int flags,
3094 const struct open_flags *op,
3095 struct file *file, int *opened)
3096{
3097 static const struct qstr name = QSTR_INIT("/", 1);
3098 struct dentry *dentry, *child;
3099 struct inode *dir;
3100 int error = path_lookupat(dfd, pathname->name,
3101 flags | LOOKUP_DIRECTORY, nd);
3102 if (unlikely(error))
3103 return error;
3104 error = mnt_want_write(nd->path.mnt);
3105 if (unlikely(error))
3106 goto out;
3107 /* we want directory to be writable */
3108 error = inode_permission(nd->inode, MAY_WRITE | MAY_EXEC);
3109 if (error)
3110 goto out2;
3111 dentry = nd->path.dentry;
3112 dir = dentry->d_inode;
3113 if (!dir->i_op->tmpfile) {
3114 error = -EOPNOTSUPP;
3115 goto out2;
3116 }
3117 child = d_alloc(dentry, &name);
3118 if (unlikely(!child)) {
3119 error = -ENOMEM;
3120 goto out2;
3121 }
3122 nd->flags &= ~LOOKUP_DIRECTORY;
3123 nd->flags |= op->intent;
3124 dput(nd->path.dentry);
3125 nd->path.dentry = child;
3126 error = dir->i_op->tmpfile(dir, nd->path.dentry, op->mode);
3127 if (error)
3128 goto out2;
3129 audit_inode(pathname, nd->path.dentry, 0);
Eric Rannaud74df6af2014-10-30 01:51:01 -07003130 /* Don't check for other permissions, the inode was just created */
3131 error = may_open(&nd->path, MAY_OPEN, op->open_flag);
Al Viro60545d02013-06-07 01:20:27 -04003132 if (error)
3133 goto out2;
3134 file->f_path.mnt = nd->path.mnt;
3135 error = finish_open(file, nd->path.dentry, NULL, opened);
3136 if (error)
3137 goto out2;
3138 error = open_check_o_direct(file);
Al Virof4e0c302013-06-11 08:34:36 +04003139 if (error) {
Al Viro60545d02013-06-07 01:20:27 -04003140 fput(file);
Al Virof4e0c302013-06-11 08:34:36 +04003141 } else if (!(op->open_flag & O_EXCL)) {
3142 struct inode *inode = file_inode(file);
3143 spin_lock(&inode->i_lock);
3144 inode->i_state |= I_LINKABLE;
3145 spin_unlock(&inode->i_lock);
3146 }
Al Viro60545d02013-06-07 01:20:27 -04003147out2:
3148 mnt_drop_write(nd->path.mnt);
3149out:
3150 path_put(&nd->path);
3151 return error;
3152}
3153
Jeff Layton669abf42012-10-10 16:43:10 -04003154static struct file *path_openat(int dfd, struct filename *pathname,
Al Viro73d049a2011-03-11 12:08:24 -05003155 struct nameidata *nd, const struct open_flags *op, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156{
Al Virofe2d35f2011-03-05 22:58:25 -05003157 struct file *base = NULL;
Al Viro30d90492012-06-22 12:40:19 +04003158 struct file *file;
Al Viro9850c052010-01-13 15:01:15 -05003159 struct path path;
Al Viro47237682012-06-10 05:01:45 -04003160 int opened = 0;
Al Viro13aab422011-02-23 17:54:08 -05003161 int error;
Nick Piggin31e6b012011-01-07 17:49:52 +11003162
Al Viro30d90492012-06-22 12:40:19 +04003163 file = get_empty_filp();
Al Viro1afc99b2013-02-14 20:41:04 -05003164 if (IS_ERR(file))
3165 return file;
Nick Piggin31e6b012011-01-07 17:49:52 +11003166
Al Viro30d90492012-06-22 12:40:19 +04003167 file->f_flags = op->open_flag;
Nick Piggin31e6b012011-01-07 17:49:52 +11003168
Al Virobb458c62013-07-13 13:26:37 +04003169 if (unlikely(file->f_flags & __O_TMPFILE)) {
Al Viro60545d02013-06-07 01:20:27 -04003170 error = do_tmpfile(dfd, pathname, nd, flags, op, file, &opened);
3171 goto out;
3172 }
3173
Jeff Layton669abf42012-10-10 16:43:10 -04003174 error = path_init(dfd, pathname->name, flags | LOOKUP_PARENT, nd, &base);
Nick Piggin31e6b012011-01-07 17:49:52 +11003175 if (unlikely(error))
Al Viro2675a4e2012-06-22 12:41:10 +04003176 goto out;
Nick Piggin31e6b012011-01-07 17:49:52 +11003177
Al Virofe2d35f2011-03-05 22:58:25 -05003178 current->total_link_count = 0;
Jeff Layton669abf42012-10-10 16:43:10 -04003179 error = link_path_walk(pathname->name, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11003180 if (unlikely(error))
Al Viro2675a4e2012-06-22 12:41:10 +04003181 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182
Al Viro2675a4e2012-06-22 12:41:10 +04003183 error = do_last(nd, &path, file, op, &opened, pathname);
3184 while (unlikely(error > 0)) { /* trailing symlink */
Nick Piggin7b9337a2011-01-14 08:42:43 +00003185 struct path link = path;
Al Virodef4af32009-12-26 08:37:05 -05003186 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04003187 if (!(nd->flags & LOOKUP_FOLLOW)) {
Al Viro73d049a2011-03-11 12:08:24 -05003188 path_put_conditional(&path, nd);
3189 path_put(&nd->path);
Al Viro2675a4e2012-06-22 12:41:10 +04003190 error = -ELOOP;
Al Viro40b39132011-03-09 16:22:18 -05003191 break;
3192 }
Kees Cook800179c2012-07-25 17:29:07 -07003193 error = may_follow_link(&link, nd);
3194 if (unlikely(error))
3195 break;
Al Viro73d049a2011-03-11 12:08:24 -05003196 nd->flags |= LOOKUP_PARENT;
3197 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
Al Viro574197e2011-03-14 22:20:34 -04003198 error = follow_link(&link, nd, &cookie);
Al Viroc3e380b2011-02-23 13:39:45 -05003199 if (unlikely(error))
Al Viro2675a4e2012-06-22 12:41:10 +04003200 break;
3201 error = do_last(nd, &path, file, op, &opened, pathname);
Al Viro574197e2011-03-14 22:20:34 -04003202 put_link(nd, &link, cookie);
Al Viro806b6812009-12-26 07:16:40 -05003203 }
Al Viro10fa8e62009-12-26 07:09:49 -05003204out:
Al Viro73d049a2011-03-11 12:08:24 -05003205 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
3206 path_put(&nd->root);
Al Virofe2d35f2011-03-05 22:58:25 -05003207 if (base)
3208 fput(base);
Al Viro2675a4e2012-06-22 12:41:10 +04003209 if (!(opened & FILE_OPENED)) {
3210 BUG_ON(!error);
Al Viro30d90492012-06-22 12:40:19 +04003211 put_filp(file);
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003212 }
Al Viro2675a4e2012-06-22 12:41:10 +04003213 if (unlikely(error)) {
3214 if (error == -EOPENSTALE) {
3215 if (flags & LOOKUP_RCU)
3216 error = -ECHILD;
3217 else
3218 error = -ESTALE;
3219 }
3220 file = ERR_PTR(error);
3221 }
3222 return file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223}
3224
Jeff Layton669abf42012-10-10 16:43:10 -04003225struct file *do_filp_open(int dfd, struct filename *pathname,
Al Virof9652e12013-06-11 08:23:01 +04003226 const struct open_flags *op)
Al Viro13aab422011-02-23 17:54:08 -05003227{
Al Viro73d049a2011-03-11 12:08:24 -05003228 struct nameidata nd;
Al Virof9652e12013-06-11 08:23:01 +04003229 int flags = op->lookup_flags;
Al Viro13aab422011-02-23 17:54:08 -05003230 struct file *filp;
3231
Al Viro73d049a2011-03-11 12:08:24 -05003232 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
Al Viro13aab422011-02-23 17:54:08 -05003233 if (unlikely(filp == ERR_PTR(-ECHILD)))
Al Viro73d049a2011-03-11 12:08:24 -05003234 filp = path_openat(dfd, pathname, &nd, op, flags);
Al Viro13aab422011-02-23 17:54:08 -05003235 if (unlikely(filp == ERR_PTR(-ESTALE)))
Al Viro73d049a2011-03-11 12:08:24 -05003236 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
Al Viro13aab422011-02-23 17:54:08 -05003237 return filp;
3238}
3239
Al Viro73d049a2011-03-11 12:08:24 -05003240struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
Al Virof9652e12013-06-11 08:23:01 +04003241 const char *name, const struct open_flags *op)
Al Viro73d049a2011-03-11 12:08:24 -05003242{
3243 struct nameidata nd;
3244 struct file *file;
Jeff Layton669abf42012-10-10 16:43:10 -04003245 struct filename filename = { .name = name };
Al Virof9652e12013-06-11 08:23:01 +04003246 int flags = op->lookup_flags | LOOKUP_ROOT;
Al Viro73d049a2011-03-11 12:08:24 -05003247
3248 nd.root.mnt = mnt;
3249 nd.root.dentry = dentry;
3250
David Howellsb18825a2013-09-12 19:22:53 +01003251 if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN)
Al Viro73d049a2011-03-11 12:08:24 -05003252 return ERR_PTR(-ELOOP);
3253
Jeff Layton669abf42012-10-10 16:43:10 -04003254 file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_RCU);
Al Viro73d049a2011-03-11 12:08:24 -05003255 if (unlikely(file == ERR_PTR(-ECHILD)))
Jeff Layton669abf42012-10-10 16:43:10 -04003256 file = path_openat(-1, &filename, &nd, op, flags);
Al Viro73d049a2011-03-11 12:08:24 -05003257 if (unlikely(file == ERR_PTR(-ESTALE)))
Jeff Layton669abf42012-10-10 16:43:10 -04003258 file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_REVAL);
Al Viro73d049a2011-03-11 12:08:24 -05003259 return file;
3260}
3261
Jeff Layton1ac12b42012-12-11 12:10:06 -05003262struct dentry *kern_path_create(int dfd, const char *pathname,
3263 struct path *path, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003265 struct dentry *dentry = ERR_PTR(-EEXIST);
Al Viroed75e952011-06-27 16:53:43 -04003266 struct nameidata nd;
Jan Karac30dabf2012-06-12 16:20:30 +02003267 int err2;
Jeff Layton1ac12b42012-12-11 12:10:06 -05003268 int error;
3269 bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
3270
3271 /*
3272 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
3273 * other flags passed in are ignored!
3274 */
3275 lookup_flags &= LOOKUP_REVAL;
3276
3277 error = do_path_lookup(dfd, pathname, LOOKUP_PARENT|lookup_flags, &nd);
Al Viroed75e952011-06-27 16:53:43 -04003278 if (error)
3279 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003281 /*
3282 * Yucky last component or no last component at all?
3283 * (foo/., foo/.., /////)
3284 */
Al Viroed75e952011-06-27 16:53:43 -04003285 if (nd.last_type != LAST_NORM)
3286 goto out;
3287 nd.flags &= ~LOOKUP_PARENT;
3288 nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003289
Jan Karac30dabf2012-06-12 16:20:30 +02003290 /* don't fail immediately if it's r/o, at least try to report other errors */
3291 err2 = mnt_want_write(nd.path.mnt);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003292 /*
3293 * Do the final lookup.
3294 */
Al Viroed75e952011-06-27 16:53:43 -04003295 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3296 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 if (IS_ERR(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003298 goto unlock;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003299
Al Viroa8104a92012-07-20 02:25:00 +04003300 error = -EEXIST;
David Howellsb18825a2013-09-12 19:22:53 +01003301 if (d_is_positive(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003302 goto fail;
David Howellsb18825a2013-09-12 19:22:53 +01003303
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003304 /*
3305 * Special case - lookup gave negative, but... we had foo/bar/
3306 * From the vfs_mknod() POV we just have a negative dentry -
3307 * all is fine. Let's be bastards - you had / on the end, you've
3308 * been asking for (non-existent) directory. -ENOENT for you.
3309 */
Al Viroed75e952011-06-27 16:53:43 -04003310 if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
Al Viroa8104a92012-07-20 02:25:00 +04003311 error = -ENOENT;
Al Viroed75e952011-06-27 16:53:43 -04003312 goto fail;
Al Viroe9baf6e2008-05-15 04:49:12 -04003313 }
Jan Karac30dabf2012-06-12 16:20:30 +02003314 if (unlikely(err2)) {
3315 error = err2;
Al Viroa8104a92012-07-20 02:25:00 +04003316 goto fail;
Jan Karac30dabf2012-06-12 16:20:30 +02003317 }
Al Viroed75e952011-06-27 16:53:43 -04003318 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320fail:
Al Viroa8104a92012-07-20 02:25:00 +04003321 dput(dentry);
3322 dentry = ERR_PTR(error);
3323unlock:
Al Viroed75e952011-06-27 16:53:43 -04003324 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Karac30dabf2012-06-12 16:20:30 +02003325 if (!err2)
3326 mnt_drop_write(nd.path.mnt);
Al Viroed75e952011-06-27 16:53:43 -04003327out:
3328 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 return dentry;
3330}
Al Virodae6ad82011-06-26 11:50:15 -04003331EXPORT_SYMBOL(kern_path_create);
3332
Al Viro921a1652012-07-20 01:15:31 +04003333void done_path_create(struct path *path, struct dentry *dentry)
3334{
3335 dput(dentry);
3336 mutex_unlock(&path->dentry->d_inode->i_mutex);
Al Viroa8104a92012-07-20 02:25:00 +04003337 mnt_drop_write(path->mnt);
Al Viro921a1652012-07-20 01:15:31 +04003338 path_put(path);
3339}
3340EXPORT_SYMBOL(done_path_create);
3341
Jeff Layton1ac12b42012-12-11 12:10:06 -05003342struct dentry *user_path_create(int dfd, const char __user *pathname,
3343 struct path *path, unsigned int lookup_flags)
Al Virodae6ad82011-06-26 11:50:15 -04003344{
Jeff Layton91a27b22012-10-10 15:25:28 -04003345 struct filename *tmp = getname(pathname);
Al Virodae6ad82011-06-26 11:50:15 -04003346 struct dentry *res;
3347 if (IS_ERR(tmp))
3348 return ERR_CAST(tmp);
Jeff Layton1ac12b42012-12-11 12:10:06 -05003349 res = kern_path_create(dfd, tmp->name, path, lookup_flags);
Al Virodae6ad82011-06-26 11:50:15 -04003350 putname(tmp);
3351 return res;
3352}
3353EXPORT_SYMBOL(user_path_create);
3354
Al Viro1a67aaf2011-07-26 01:52:52 -04003355int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003357 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358
3359 if (error)
3360 return error;
3361
Eric W. Biederman975d6b32011-11-13 12:16:43 -08003362 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 return -EPERM;
3364
Al Viroacfa4382008-12-04 10:06:33 -05003365 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 return -EPERM;
3367
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07003368 error = devcgroup_inode_mknod(mode, dev);
3369 if (error)
3370 return error;
3371
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 error = security_inode_mknod(dir, dentry, mode, dev);
3373 if (error)
3374 return error;
3375
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003377 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003378 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 return error;
3380}
3381
Al Virof69aac02011-07-26 04:31:40 -04003382static int may_mknod(umode_t mode)
Dave Hansen463c3192008-02-15 14:37:57 -08003383{
3384 switch (mode & S_IFMT) {
3385 case S_IFREG:
3386 case S_IFCHR:
3387 case S_IFBLK:
3388 case S_IFIFO:
3389 case S_IFSOCK:
3390 case 0: /* zero mode translates to S_IFREG */
3391 return 0;
3392 case S_IFDIR:
3393 return -EPERM;
3394 default:
3395 return -EINVAL;
3396 }
3397}
3398
Al Viro8208a222011-07-25 17:32:17 -04003399SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003400 unsigned, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401{
Al Viro2ad94ae2008-07-21 09:32:51 -04003402 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003403 struct path path;
3404 int error;
Jeff Layton972567f2012-12-20 16:00:10 -05003405 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406
Al Viro8e4bfca2012-07-20 01:17:26 +04003407 error = may_mknod(mode);
3408 if (error)
3409 return error;
Jeff Layton972567f2012-12-20 16:00:10 -05003410retry:
3411 dentry = user_path_create(dfd, filename, &path, lookup_flags);
Al Virodae6ad82011-06-26 11:50:15 -04003412 if (IS_ERR(dentry))
3413 return PTR_ERR(dentry);
Al Viro2ad94ae2008-07-21 09:32:51 -04003414
Al Virodae6ad82011-06-26 11:50:15 -04003415 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003416 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003417 error = security_path_mknod(&path, dentry, mode, dev);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003418 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04003419 goto out;
Dave Hansen463c3192008-02-15 14:37:57 -08003420 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 case 0: case S_IFREG:
Al Viro312b63f2012-06-10 18:09:36 -04003422 error = vfs_create(path.dentry->d_inode,dentry,mode,true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 break;
3424 case S_IFCHR: case S_IFBLK:
Al Virodae6ad82011-06-26 11:50:15 -04003425 error = vfs_mknod(path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 new_decode_dev(dev));
3427 break;
3428 case S_IFIFO: case S_IFSOCK:
Al Virodae6ad82011-06-26 11:50:15 -04003429 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 }
Al Viroa8104a92012-07-20 02:25:00 +04003432out:
Al Viro921a1652012-07-20 01:15:31 +04003433 done_path_create(&path, dentry);
Jeff Layton972567f2012-12-20 16:00:10 -05003434 if (retry_estale(error, lookup_flags)) {
3435 lookup_flags |= LOOKUP_REVAL;
3436 goto retry;
3437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 return error;
3439}
3440
Al Viro8208a222011-07-25 17:32:17 -04003441SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003442{
3443 return sys_mknodat(AT_FDCWD, filename, mode, dev);
3444}
3445
Al Viro18bb1db2011-07-26 01:41:39 -04003446int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003448 int error = may_create(dir, dentry);
Al Viro8de52772012-02-06 12:45:27 -05003449 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450
3451 if (error)
3452 return error;
3453
Al Viroacfa4382008-12-04 10:06:33 -05003454 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 return -EPERM;
3456
3457 mode &= (S_IRWXUGO|S_ISVTX);
3458 error = security_inode_mkdir(dir, dentry, mode);
3459 if (error)
3460 return error;
3461
Al Viro8de52772012-02-06 12:45:27 -05003462 if (max_links && dir->i_nlink >= max_links)
3463 return -EMLINK;
3464
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003466 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003467 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 return error;
3469}
3470
Al Viroa218d0f2011-11-21 14:59:34 -05003471SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472{
Dave Hansen6902d922006-09-30 23:29:01 -07003473 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003474 struct path path;
3475 int error;
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003476 unsigned int lookup_flags = LOOKUP_DIRECTORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003478retry:
3479 dentry = user_path_create(dfd, pathname, &path, lookup_flags);
Dave Hansen6902d922006-09-30 23:29:01 -07003480 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003481 return PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003482
Al Virodae6ad82011-06-26 11:50:15 -04003483 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003484 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003485 error = security_path_mkdir(&path, dentry, mode);
Al Viroa8104a92012-07-20 02:25:00 +04003486 if (!error)
3487 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
Al Viro921a1652012-07-20 01:15:31 +04003488 done_path_create(&path, dentry);
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003489 if (retry_estale(error, lookup_flags)) {
3490 lookup_flags |= LOOKUP_REVAL;
3491 goto retry;
3492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 return error;
3494}
3495
Al Viroa218d0f2011-11-21 14:59:34 -05003496SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003497{
3498 return sys_mkdirat(AT_FDCWD, pathname, mode);
3499}
3500
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501/*
Sage Weila71905f2011-05-24 13:06:08 -07003502 * The dentry_unhash() helper will try to drop the dentry early: we
J. Bruce Fieldsc0d02592012-02-15 11:48:40 -05003503 * should have a usage count of 1 if we're the only user of this
Sage Weila71905f2011-05-24 13:06:08 -07003504 * dentry, and if that is true (possibly after pruning the dcache),
3505 * then we drop the dentry now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 *
3507 * A low-level filesystem can, if it choses, legally
3508 * do a
3509 *
3510 * if (!d_unhashed(dentry))
3511 * return -EBUSY;
3512 *
3513 * if it cannot handle the case of removing a directory
3514 * that is still in use by something else..
3515 */
3516void dentry_unhash(struct dentry *dentry)
3517{
Vasily Averindc168422006-12-06 20:37:07 -08003518 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 spin_lock(&dentry->d_lock);
Waiman Long98474232013-08-28 18:24:59 -07003520 if (dentry->d_lockref.count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 __d_drop(dentry);
3522 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523}
3524
3525int vfs_rmdir(struct inode *dir, struct dentry *dentry)
3526{
3527 int error = may_delete(dir, dentry, 1);
3528
3529 if (error)
3530 return error;
3531
Al Viroacfa4382008-12-04 10:06:33 -05003532 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 return -EPERM;
3534
Al Viro1d2ef592011-09-14 18:55:41 +01003535 dget(dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003536 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537
Sage Weil912dbc12011-05-24 13:06:11 -07003538 error = -EBUSY;
3539 if (d_mountpoint(dentry))
3540 goto out;
3541
3542 error = security_inode_rmdir(dir, dentry);
3543 if (error)
3544 goto out;
3545
Sage Weil3cebde22011-05-29 21:20:59 -07003546 shrink_dcache_parent(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003547 error = dir->i_op->rmdir(dir, dentry);
3548 if (error)
3549 goto out;
3550
3551 dentry->d_inode->i_flags |= S_DEAD;
3552 dont_mount(dentry);
3553
3554out:
3555 mutex_unlock(&dentry->d_inode->i_mutex);
Al Viro1d2ef592011-09-14 18:55:41 +01003556 dput(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003557 if (!error)
3558 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 return error;
3560}
3561
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003562static long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563{
3564 int error = 0;
Jeff Layton91a27b22012-10-10 15:25:28 -04003565 struct filename *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 struct dentry *dentry;
3567 struct nameidata nd;
Jeff Laytonc6ee9202012-12-20 16:28:33 -05003568 unsigned int lookup_flags = 0;
3569retry:
3570 name = user_path_parent(dfd, pathname, &nd, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04003571 if (IS_ERR(name))
3572 return PTR_ERR(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573
3574 switch(nd.last_type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003575 case LAST_DOTDOT:
3576 error = -ENOTEMPTY;
3577 goto exit1;
3578 case LAST_DOT:
3579 error = -EINVAL;
3580 goto exit1;
3581 case LAST_ROOT:
3582 error = -EBUSY;
3583 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003585
3586 nd.flags &= ~LOOKUP_PARENT;
Jan Karac30dabf2012-06-12 16:20:30 +02003587 error = mnt_want_write(nd.path.mnt);
3588 if (error)
3589 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003590
Jan Blunck4ac91372008-02-14 19:34:32 -08003591 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08003592 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003594 if (IS_ERR(dentry))
3595 goto exit2;
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04003596 if (!dentry->d_inode) {
3597 error = -ENOENT;
3598 goto exit3;
3599 }
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003600 error = security_path_rmdir(&nd.path, dentry);
3601 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02003602 goto exit3;
Jan Blunck4ac91372008-02-14 19:34:32 -08003603 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
Dave Hansen06227532008-02-15 14:37:34 -08003604exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07003605 dput(dentry);
3606exit2:
Jan Blunck4ac91372008-02-14 19:34:32 -08003607 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Karac30dabf2012-06-12 16:20:30 +02003608 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003610 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 putname(name);
Jeff Laytonc6ee9202012-12-20 16:28:33 -05003612 if (retry_estale(error, lookup_flags)) {
3613 lookup_flags |= LOOKUP_REVAL;
3614 goto retry;
3615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 return error;
3617}
3618
Heiko Carstens3cdad422009-01-14 14:14:22 +01003619SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003620{
3621 return do_rmdir(AT_FDCWD, pathname);
3622}
3623
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003624/**
3625 * vfs_unlink - unlink a filesystem object
3626 * @dir: parent directory
3627 * @dentry: victim
3628 * @delegated_inode: returns victim inode, if the inode is delegated.
3629 *
3630 * The caller must hold dir->i_mutex.
3631 *
3632 * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
3633 * return a reference to the inode in delegated_inode. The caller
3634 * should then break the delegation on that inode and retry. Because
3635 * breaking a delegation may take a long time, the caller should drop
3636 * dir->i_mutex before doing so.
3637 *
3638 * Alternatively, a caller may pass NULL for delegated_inode. This may
3639 * be appropriate for callers that expect the underlying filesystem not
3640 * to be NFS exported.
3641 */
3642int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643{
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003644 struct inode *target = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 int error = may_delete(dir, dentry, 0);
3646
3647 if (error)
3648 return error;
3649
Al Viroacfa4382008-12-04 10:06:33 -05003650 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 return -EPERM;
3652
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003653 mutex_lock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 if (d_mountpoint(dentry))
3655 error = -EBUSY;
3656 else {
3657 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05003658 if (!error) {
J. Bruce Fields5a146962012-08-28 07:50:40 -07003659 error = try_break_deleg(target, delegated_inode);
3660 if (error)
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003661 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 error = dir->i_op->unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05003663 if (!error)
Al Virod83c49f2010-04-30 17:17:09 -04003664 dont_mount(dentry);
Al Virobec10522010-03-03 14:12:08 -05003665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 }
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003667out:
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003668 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669
3670 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
3671 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003672 fsnotify_link_count(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 }
Robert Love0eeca282005-07-12 17:06:03 -04003675
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 return error;
3677}
3678
3679/*
3680 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003681 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 * writeout happening, and we don't want to prevent access to the directory
3683 * while waiting on the I/O.
3684 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003685static long do_unlinkat(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686{
Al Viro2ad94ae2008-07-21 09:32:51 -04003687 int error;
Jeff Layton91a27b22012-10-10 15:25:28 -04003688 struct filename *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 struct dentry *dentry;
3690 struct nameidata nd;
3691 struct inode *inode = NULL;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003692 struct inode *delegated_inode = NULL;
Jeff Layton5d18f812012-12-20 16:38:04 -05003693 unsigned int lookup_flags = 0;
3694retry:
3695 name = user_path_parent(dfd, pathname, &nd, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04003696 if (IS_ERR(name))
3697 return PTR_ERR(name);
Al Viro2ad94ae2008-07-21 09:32:51 -04003698
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 error = -EISDIR;
3700 if (nd.last_type != LAST_NORM)
3701 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003702
3703 nd.flags &= ~LOOKUP_PARENT;
Jan Karac30dabf2012-06-12 16:20:30 +02003704 error = mnt_want_write(nd.path.mnt);
3705 if (error)
3706 goto exit1;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003707retry_deleg:
Jan Blunck4ac91372008-02-14 19:34:32 -08003708 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08003709 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 error = PTR_ERR(dentry);
3711 if (!IS_ERR(dentry)) {
3712 /* Why not before? Because we want correct error value */
Török Edwin50338b82011-06-16 00:06:14 +03003713 if (nd.last.name[nd.last.len])
3714 goto slashes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 inode = dentry->d_inode;
David Howellsb18825a2013-09-12 19:22:53 +01003716 if (d_is_negative(dentry))
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04003717 goto slashes;
3718 ihold(inode);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003719 error = security_path_unlink(&nd.path, dentry);
3720 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02003721 goto exit2;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003722 error = vfs_unlink(nd.path.dentry->d_inode, dentry, &delegated_inode);
Jan Karac30dabf2012-06-12 16:20:30 +02003723exit2:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 dput(dentry);
3725 }
Jan Blunck4ac91372008-02-14 19:34:32 -08003726 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 if (inode)
3728 iput(inode); /* truncate the inode here */
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003729 inode = NULL;
3730 if (delegated_inode) {
J. Bruce Fields5a146962012-08-28 07:50:40 -07003731 error = break_deleg_wait(&delegated_inode);
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003732 if (!error)
3733 goto retry_deleg;
3734 }
Jan Karac30dabf2012-06-12 16:20:30 +02003735 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003737 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 putname(name);
Jeff Layton5d18f812012-12-20 16:38:04 -05003739 if (retry_estale(error, lookup_flags)) {
3740 lookup_flags |= LOOKUP_REVAL;
3741 inode = NULL;
3742 goto retry;
3743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 return error;
3745
3746slashes:
David Howellsb18825a2013-09-12 19:22:53 +01003747 if (d_is_negative(dentry))
3748 error = -ENOENT;
Miklos Szeredidb8bf542014-04-01 17:08:41 +02003749 else if (d_is_dir(dentry))
David Howellsb18825a2013-09-12 19:22:53 +01003750 error = -EISDIR;
3751 else
3752 error = -ENOTDIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 goto exit2;
3754}
3755
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003756SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003757{
3758 if ((flag & ~AT_REMOVEDIR) != 0)
3759 return -EINVAL;
3760
3761 if (flag & AT_REMOVEDIR)
3762 return do_rmdir(dfd, pathname);
3763
3764 return do_unlinkat(dfd, pathname);
3765}
3766
Heiko Carstens3480b252009-01-14 14:14:16 +01003767SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003768{
3769 return do_unlinkat(AT_FDCWD, pathname);
3770}
3771
Miklos Szeredidb2e7472008-06-24 16:50:16 +02003772int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003774 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775
3776 if (error)
3777 return error;
3778
Al Viroacfa4382008-12-04 10:06:33 -05003779 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 return -EPERM;
3781
3782 error = security_inode_symlink(dir, dentry, oldname);
3783 if (error)
3784 return error;
3785
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003787 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003788 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 return error;
3790}
3791
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003792SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
3793 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794{
Al Viro2ad94ae2008-07-21 09:32:51 -04003795 int error;
Jeff Layton91a27b22012-10-10 15:25:28 -04003796 struct filename *from;
Dave Hansen6902d922006-09-30 23:29:01 -07003797 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003798 struct path path;
Jeff Laytonf46d3562012-12-11 12:10:08 -05003799 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800
3801 from = getname(oldname);
Al Viro2ad94ae2008-07-21 09:32:51 -04003802 if (IS_ERR(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 return PTR_ERR(from);
Jeff Laytonf46d3562012-12-11 12:10:08 -05003804retry:
3805 dentry = user_path_create(newdfd, newname, &path, lookup_flags);
Dave Hansen6902d922006-09-30 23:29:01 -07003806 error = PTR_ERR(dentry);
3807 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003808 goto out_putname;
Dave Hansen6902d922006-09-30 23:29:01 -07003809
Jeff Layton91a27b22012-10-10 15:25:28 -04003810 error = security_path_symlink(&path, dentry, from->name);
Al Viroa8104a92012-07-20 02:25:00 +04003811 if (!error)
Jeff Layton91a27b22012-10-10 15:25:28 -04003812 error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
Al Viro921a1652012-07-20 01:15:31 +04003813 done_path_create(&path, dentry);
Jeff Laytonf46d3562012-12-11 12:10:08 -05003814 if (retry_estale(error, lookup_flags)) {
3815 lookup_flags |= LOOKUP_REVAL;
3816 goto retry;
3817 }
Dave Hansen6902d922006-09-30 23:29:01 -07003818out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 putname(from);
3820 return error;
3821}
3822
Heiko Carstens3480b252009-01-14 14:14:16 +01003823SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003824{
3825 return sys_symlinkat(oldname, AT_FDCWD, newname);
3826}
3827
J. Bruce Fields146a8592011-09-20 17:14:31 -04003828/**
3829 * vfs_link - create a new link
3830 * @old_dentry: object to be linked
3831 * @dir: new parent
3832 * @new_dentry: where to create the new link
3833 * @delegated_inode: returns inode needing a delegation break
3834 *
3835 * The caller must hold dir->i_mutex
3836 *
3837 * If vfs_link discovers a delegation on the to-be-linked file in need
3838 * of breaking, it will return -EWOULDBLOCK and return a reference to the
3839 * inode in delegated_inode. The caller should then break the delegation
3840 * and retry. Because breaking a delegation may take a long time, the
3841 * caller should drop the i_mutex before doing so.
3842 *
3843 * Alternatively, a caller may pass NULL for delegated_inode. This may
3844 * be appropriate for callers that expect the underlying filesystem not
3845 * to be NFS exported.
3846 */
3847int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848{
3849 struct inode *inode = old_dentry->d_inode;
Al Viro8de52772012-02-06 12:45:27 -05003850 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 int error;
3852
3853 if (!inode)
3854 return -ENOENT;
3855
Miklos Szeredia95164d2008-07-30 15:08:48 +02003856 error = may_create(dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 if (error)
3858 return error;
3859
3860 if (dir->i_sb != inode->i_sb)
3861 return -EXDEV;
3862
3863 /*
3864 * A link to an append-only or immutable file cannot be created.
3865 */
3866 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3867 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05003868 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003870 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 return -EPERM;
3872
3873 error = security_inode_link(old_dentry, dir, new_dentry);
3874 if (error)
3875 return error;
3876
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003877 mutex_lock(&inode->i_mutex);
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05303878 /* Make sure we don't allow creating hardlink to an unlinked file */
Al Virof4e0c302013-06-11 08:34:36 +04003879 if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05303880 error = -ENOENT;
Al Viro8de52772012-02-06 12:45:27 -05003881 else if (max_links && inode->i_nlink >= max_links)
3882 error = -EMLINK;
J. Bruce Fields146a8592011-09-20 17:14:31 -04003883 else {
3884 error = try_break_deleg(inode, delegated_inode);
3885 if (!error)
3886 error = dir->i_op->link(old_dentry, dir, new_dentry);
3887 }
Al Virof4e0c302013-06-11 08:34:36 +04003888
3889 if (!error && (inode->i_state & I_LINKABLE)) {
3890 spin_lock(&inode->i_lock);
3891 inode->i_state &= ~I_LINKABLE;
3892 spin_unlock(&inode->i_lock);
3893 }
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003894 mutex_unlock(&inode->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07003895 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003896 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897 return error;
3898}
3899
3900/*
3901 * Hardlinks are often used in delicate situations. We avoid
3902 * security-related surprises by not following symlinks on the
3903 * newname. --KAB
3904 *
3905 * We don't follow them on the oldname either to be compatible
3906 * with linux 2.0, and to avoid hard-linking to directories
3907 * and other special files. --ADM
3908 */
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003909SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
3910 int, newdfd, const char __user *, newname, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911{
3912 struct dentry *new_dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003913 struct path old_path, new_path;
J. Bruce Fields146a8592011-09-20 17:14:31 -04003914 struct inode *delegated_inode = NULL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303915 int how = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303918 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003919 return -EINVAL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303920 /*
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07003921 * To use null names we require CAP_DAC_READ_SEARCH
3922 * This ensures that not everyone will be able to create
3923 * handlink using the passed filedescriptor.
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303924 */
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07003925 if (flags & AT_EMPTY_PATH) {
3926 if (!capable(CAP_DAC_READ_SEARCH))
3927 return -ENOENT;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303928 how = LOOKUP_EMPTY;
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07003929 }
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003930
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303931 if (flags & AT_SYMLINK_FOLLOW)
3932 how |= LOOKUP_FOLLOW;
Jeff Layton442e31c2012-12-20 16:15:38 -05003933retry:
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303934 error = user_path_at(olddfd, oldname, how, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04003936 return error;
3937
Jeff Layton442e31c2012-12-20 16:15:38 -05003938 new_dentry = user_path_create(newdfd, newname, &new_path,
3939 (how & LOOKUP_REVAL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003941 if (IS_ERR(new_dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003942 goto out;
3943
3944 error = -EXDEV;
3945 if (old_path.mnt != new_path.mnt)
3946 goto out_dput;
Kees Cook800179c2012-07-25 17:29:07 -07003947 error = may_linkat(&old_path);
3948 if (unlikely(error))
3949 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04003950 error = security_path_link(old_path.dentry, &new_path, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003951 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04003952 goto out_dput;
J. Bruce Fields146a8592011-09-20 17:14:31 -04003953 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode);
Dave Hansen75c3f292008-02-15 14:37:45 -08003954out_dput:
Al Viro921a1652012-07-20 01:15:31 +04003955 done_path_create(&new_path, new_dentry);
J. Bruce Fields146a8592011-09-20 17:14:31 -04003956 if (delegated_inode) {
3957 error = break_deleg_wait(&delegated_inode);
Oleg Drokind22e6332014-01-31 15:41:58 -05003958 if (!error) {
3959 path_put(&old_path);
J. Bruce Fields146a8592011-09-20 17:14:31 -04003960 goto retry;
Oleg Drokind22e6332014-01-31 15:41:58 -05003961 }
J. Bruce Fields146a8592011-09-20 17:14:31 -04003962 }
Jeff Layton442e31c2012-12-20 16:15:38 -05003963 if (retry_estale(error, how)) {
Oleg Drokind22e6332014-01-31 15:41:58 -05003964 path_put(&old_path);
Jeff Layton442e31c2012-12-20 16:15:38 -05003965 how |= LOOKUP_REVAL;
3966 goto retry;
3967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968out:
Al Viro2d8f3032008-07-22 09:59:21 -04003969 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970
3971 return error;
3972}
3973
Heiko Carstens3480b252009-01-14 14:14:16 +01003974SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003975{
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003976 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003977}
3978
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02003979/**
3980 * vfs_rename - rename a filesystem object
3981 * @old_dir: parent of source
3982 * @old_dentry: source
3983 * @new_dir: parent of destination
3984 * @new_dentry: destination
3985 * @delegated_inode: returns an inode needing a delegation break
Miklos Szeredi148622f2014-04-01 17:08:42 +02003986 * @flags: rename flags
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02003987 *
3988 * The caller must hold multiple mutexes--see lock_rename()).
3989 *
3990 * If vfs_rename discovers a delegation in need of breaking at either
3991 * the source or destination, it will return -EWOULDBLOCK and return a
3992 * reference to the inode in delegated_inode. The caller should then
3993 * break the delegation and retry. Because breaking a delegation may
3994 * take a long time, the caller should drop all locks before doing
3995 * so.
3996 *
3997 * Alternatively, a caller may pass NULL for delegated_inode. This may
3998 * be appropriate for callers that expect the underlying filesystem not
3999 * to be NFS exported.
4000 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 * The worst of all namespace operations - renaming directory. "Perverted"
4002 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
4003 * Problems:
4004 * a) we can get into loop creation. Check is done in is_subdir().
4005 * b) race potential - two innocent renames can create a loop together.
4006 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004007 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 * story.
J. Bruce Fields6cedba82012-03-05 11:40:41 -05004009 * c) we have to lock _four_ objects - parents and victim (if it exists),
4010 * and source (if it is not a directory).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004011 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 * whether the target exists). Solution: try to be smart with locking
4013 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004014 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 * move will be locked. Thus we can rank directories by the tree
4016 * (ancestors first) and rank all non-directories after them.
4017 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004018 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 * HOWEVER, it relies on the assumption that any object with ->lookup()
4020 * has no more than 1 dentry. If "hybrid" objects will ever appear,
4021 * we'd better make sure that there's no link(2) for them.
Sage Weile4eaac02011-05-24 13:06:07 -07004022 * d) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004023 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05004025 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 * locking].
4027 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004029 struct inode *new_dir, struct dentry *new_dentry,
Miklos Szeredi148622f2014-04-01 17:08:42 +02004030 struct inode **delegated_inode, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031{
4032 int error;
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02004033 bool is_dir = d_is_dir(old_dentry);
Eric Paris59b0df22010-02-08 12:53:52 -05004034 const unsigned char *old_name;
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02004035 struct inode *source = old_dentry->d_inode;
4036 struct inode *target = new_dentry->d_inode;
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004037 bool new_is_dir = false;
4038 unsigned max_links = new_dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02004040 if (source == target)
4041 return 0;
4042
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 error = may_delete(old_dir, old_dentry, is_dir);
4044 if (error)
4045 return error;
4046
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004047 if (!target) {
Miklos Szeredia95164d2008-07-30 15:08:48 +02004048 error = may_create(new_dir, new_dentry);
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004049 } else {
4050 new_is_dir = d_is_dir(new_dentry);
4051
4052 if (!(flags & RENAME_EXCHANGE))
4053 error = may_delete(new_dir, new_dentry, is_dir);
4054 else
4055 error = may_delete(new_dir, new_dentry, new_is_dir);
4056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 if (error)
4058 return error;
4059
Miklos Szeredia756aea2014-07-23 15:15:30 +02004060 if (!old_dir->i_op->rename && !old_dir->i_op->rename2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 return -EPERM;
4062
Miklos Szeredi148622f2014-04-01 17:08:42 +02004063 if (flags && !old_dir->i_op->rename2)
4064 return -EINVAL;
4065
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02004066 /*
4067 * If we are going to change the parent - check write permissions,
4068 * we'll need to flip '..'.
4069 */
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004070 if (new_dir != old_dir) {
4071 if (is_dir) {
4072 error = inode_permission(source, MAY_WRITE);
4073 if (error)
4074 return error;
4075 }
4076 if ((flags & RENAME_EXCHANGE) && new_is_dir) {
4077 error = inode_permission(target, MAY_WRITE);
4078 if (error)
4079 return error;
4080 }
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02004081 }
Robert Love0eeca282005-07-12 17:06:03 -04004082
Miklos Szeredi5371fcd2014-04-01 17:08:43 +02004083 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry,
4084 flags);
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02004085 if (error)
4086 return error;
4087
4088 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
4089 dget(new_dentry);
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004090 if (!is_dir || (flags & RENAME_EXCHANGE))
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02004091 lock_two_nondirectories(source, target);
4092 else if (target)
4093 mutex_lock(&target->i_mutex);
4094
4095 error = -EBUSY;
4096 if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
4097 goto out;
4098
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004099 if (max_links && new_dir != old_dir) {
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02004100 error = -EMLINK;
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004101 if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links)
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02004102 goto out;
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004103 if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir &&
4104 old_dir->i_nlink >= max_links)
4105 goto out;
4106 }
4107 if (is_dir && !(flags & RENAME_EXCHANGE) && target)
4108 shrink_dcache_parent(new_dentry);
4109 if (!is_dir) {
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02004110 error = try_break_deleg(source, delegated_inode);
4111 if (error)
4112 goto out;
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004113 }
4114 if (target && !new_is_dir) {
4115 error = try_break_deleg(target, delegated_inode);
4116 if (error)
4117 goto out;
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02004118 }
Miklos Szeredia756aea2014-07-23 15:15:30 +02004119 if (!old_dir->i_op->rename2) {
Miklos Szeredi148622f2014-04-01 17:08:42 +02004120 error = old_dir->i_op->rename(old_dir, old_dentry,
4121 new_dir, new_dentry);
4122 } else {
Miklos Szeredia756aea2014-07-23 15:15:30 +02004123 WARN_ON(old_dir->i_op->rename != NULL);
Miklos Szeredi148622f2014-04-01 17:08:42 +02004124 error = old_dir->i_op->rename2(old_dir, old_dentry,
4125 new_dir, new_dentry, flags);
4126 }
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02004127 if (error)
4128 goto out;
4129
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004130 if (!(flags & RENAME_EXCHANGE) && target) {
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02004131 if (is_dir)
4132 target->i_flags |= S_DEAD;
4133 dont_mount(new_dentry);
4134 }
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004135 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) {
4136 if (!(flags & RENAME_EXCHANGE))
4137 d_move(old_dentry, new_dentry);
4138 else
4139 d_exchange(old_dentry, new_dentry);
4140 }
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02004141out:
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004142 if (!is_dir || (flags & RENAME_EXCHANGE))
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02004143 unlock_two_nondirectories(source, target);
4144 else if (target)
4145 mutex_unlock(&target->i_mutex);
4146 dput(new_dentry);
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004147 if (!error) {
Al Viro123df292009-12-25 04:57:57 -05004148 fsnotify_move(old_dir, new_dir, old_name, is_dir,
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004149 !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
4150 if (flags & RENAME_EXCHANGE) {
4151 fsnotify_move(new_dir, old_dir, old_dentry->d_name.name,
4152 new_is_dir, NULL, new_dentry);
4153 }
4154 }
Robert Love0eeca282005-07-12 17:06:03 -04004155 fsnotify_oldname_free(old_name);
4156
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 return error;
4158}
4159
Miklos Szeredi148622f2014-04-01 17:08:42 +02004160SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
4161 int, newdfd, const char __user *, newname, unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162{
Al Viro2ad94ae2008-07-21 09:32:51 -04004163 struct dentry *old_dir, *new_dir;
4164 struct dentry *old_dentry, *new_dentry;
4165 struct dentry *trap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 struct nameidata oldnd, newnd;
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004167 struct inode *delegated_inode = NULL;
Jeff Layton91a27b22012-10-10 15:25:28 -04004168 struct filename *from;
4169 struct filename *to;
Jeff Laytonc6a94282012-12-11 12:10:10 -05004170 unsigned int lookup_flags = 0;
4171 bool should_retry = false;
Al Viro2ad94ae2008-07-21 09:32:51 -04004172 int error;
Miklos Szeredi148622f2014-04-01 17:08:42 +02004173
Miklos Szeredi0e98d782014-10-24 00:14:37 +02004174 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004175 return -EINVAL;
4176
Miklos Szeredi0e98d782014-10-24 00:14:37 +02004177 if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) &&
4178 (flags & RENAME_EXCHANGE))
Miklos Szeredi148622f2014-04-01 17:08:42 +02004179 return -EINVAL;
4180
Miklos Szeredi0e98d782014-10-24 00:14:37 +02004181 if ((flags & RENAME_WHITEOUT) && !capable(CAP_MKNOD))
4182 return -EPERM;
4183
Jeff Laytonc6a94282012-12-11 12:10:10 -05004184retry:
4185 from = user_path_parent(olddfd, oldname, &oldnd, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04004186 if (IS_ERR(from)) {
4187 error = PTR_ERR(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 goto exit;
Jeff Layton91a27b22012-10-10 15:25:28 -04004189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190
Jeff Laytonc6a94282012-12-11 12:10:10 -05004191 to = user_path_parent(newdfd, newname, &newnd, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04004192 if (IS_ERR(to)) {
4193 error = PTR_ERR(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 goto exit1;
Jeff Layton91a27b22012-10-10 15:25:28 -04004195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196
4197 error = -EXDEV;
Jan Blunck4ac91372008-02-14 19:34:32 -08004198 if (oldnd.path.mnt != newnd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 goto exit2;
4200
Jan Blunck4ac91372008-02-14 19:34:32 -08004201 old_dir = oldnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 error = -EBUSY;
4203 if (oldnd.last_type != LAST_NORM)
4204 goto exit2;
4205
Jan Blunck4ac91372008-02-14 19:34:32 -08004206 new_dir = newnd.path.dentry;
Miklos Szeredif1bdf912014-04-01 17:08:43 +02004207 if (flags & RENAME_NOREPLACE)
4208 error = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 if (newnd.last_type != LAST_NORM)
4210 goto exit2;
4211
Jan Karac30dabf2012-06-12 16:20:30 +02004212 error = mnt_want_write(oldnd.path.mnt);
4213 if (error)
4214 goto exit2;
4215
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004216 oldnd.flags &= ~LOOKUP_PARENT;
4217 newnd.flags &= ~LOOKUP_PARENT;
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004218 if (!(flags & RENAME_EXCHANGE))
4219 newnd.flags |= LOOKUP_RENAME_TARGET;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004220
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004221retry_deleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 trap = lock_rename(new_dir, old_dir);
4223
Christoph Hellwig49705b72005-11-08 21:35:06 -08004224 old_dentry = lookup_hash(&oldnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 error = PTR_ERR(old_dentry);
4226 if (IS_ERR(old_dentry))
4227 goto exit3;
4228 /* source must exist */
4229 error = -ENOENT;
David Howellsb18825a2013-09-12 19:22:53 +01004230 if (d_is_negative(old_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 goto exit4;
Christoph Hellwig49705b72005-11-08 21:35:06 -08004232 new_dentry = lookup_hash(&newnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 error = PTR_ERR(new_dentry);
4234 if (IS_ERR(new_dentry))
4235 goto exit4;
Miklos Szeredif1bdf912014-04-01 17:08:43 +02004236 error = -EEXIST;
4237 if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry))
4238 goto exit5;
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004239 if (flags & RENAME_EXCHANGE) {
4240 error = -ENOENT;
4241 if (d_is_negative(new_dentry))
4242 goto exit5;
4243
4244 if (!d_is_dir(new_dentry)) {
4245 error = -ENOTDIR;
4246 if (newnd.last.name[newnd.last.len])
4247 goto exit5;
4248 }
4249 }
Miklos Szeredif1bdf912014-04-01 17:08:43 +02004250 /* unless the source is a directory trailing slashes give -ENOTDIR */
4251 if (!d_is_dir(old_dentry)) {
4252 error = -ENOTDIR;
4253 if (oldnd.last.name[oldnd.last.len])
4254 goto exit5;
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004255 if (!(flags & RENAME_EXCHANGE) && newnd.last.name[newnd.last.len])
Miklos Szeredif1bdf912014-04-01 17:08:43 +02004256 goto exit5;
4257 }
4258 /* source should not be ancestor of target */
4259 error = -EINVAL;
4260 if (old_dentry == trap)
4261 goto exit5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 /* target should not be an ancestor of source */
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004263 if (!(flags & RENAME_EXCHANGE))
4264 error = -ENOTEMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 if (new_dentry == trap)
4266 goto exit5;
4267
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004268 error = security_path_rename(&oldnd.path, old_dentry,
Miklos Szeredi5371fcd2014-04-01 17:08:43 +02004269 &newnd.path, new_dentry, flags);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004270 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02004271 goto exit5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 error = vfs_rename(old_dir->d_inode, old_dentry,
Miklos Szeredi148622f2014-04-01 17:08:42 +02004273 new_dir->d_inode, new_dentry,
4274 &delegated_inode, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275exit5:
4276 dput(new_dentry);
4277exit4:
4278 dput(old_dentry);
4279exit3:
4280 unlock_rename(new_dir, old_dir);
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004281 if (delegated_inode) {
4282 error = break_deleg_wait(&delegated_inode);
4283 if (!error)
4284 goto retry_deleg;
4285 }
Jan Karac30dabf2012-06-12 16:20:30 +02004286 mnt_drop_write(oldnd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287exit2:
Jeff Laytonc6a94282012-12-11 12:10:10 -05004288 if (retry_estale(error, lookup_flags))
4289 should_retry = true;
Jan Blunck1d957f92008-02-14 19:34:35 -08004290 path_put(&newnd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04004291 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08004293 path_put(&oldnd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 putname(from);
Jeff Laytonc6a94282012-12-11 12:10:10 -05004295 if (should_retry) {
4296 should_retry = false;
4297 lookup_flags |= LOOKUP_REVAL;
4298 goto retry;
4299 }
Al Viro2ad94ae2008-07-21 09:32:51 -04004300exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 return error;
4302}
4303
Miklos Szeredi148622f2014-04-01 17:08:42 +02004304SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4305 int, newdfd, const char __user *, newname)
4306{
4307 return sys_renameat2(olddfd, oldname, newdfd, newname, 0);
4308}
4309
Heiko Carstensa26eab22009-01-14 14:14:17 +01004310SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004311{
Miklos Szeredi148622f2014-04-01 17:08:42 +02004312 return sys_renameat2(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004313}
4314
Miklos Szeredi9354e062014-10-24 00:14:36 +02004315int vfs_whiteout(struct inode *dir, struct dentry *dentry)
4316{
4317 int error = may_create(dir, dentry);
4318 if (error)
4319 return error;
4320
4321 if (!dir->i_op->mknod)
4322 return -EPERM;
4323
4324 return dir->i_op->mknod(dir, dentry,
4325 S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
4326}
4327EXPORT_SYMBOL(vfs_whiteout);
4328
Al Viro0750d562014-03-14 13:42:45 -04004329int readlink_copy(char __user *buffer, int buflen, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330{
Al Viro0750d562014-03-14 13:42:45 -04004331 int len = PTR_ERR(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 if (IS_ERR(link))
4333 goto out;
4334
4335 len = strlen(link);
4336 if (len > (unsigned) buflen)
4337 len = buflen;
4338 if (copy_to_user(buffer, link, len))
4339 len = -EFAULT;
4340out:
4341 return len;
4342}
Alex Shiab0f88e2015-08-20 14:56:22 +08004343EXPORT_SYMBOL(readlink_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344
4345/*
4346 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
4347 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
4348 * using) it for any given inode is up to filesystem.
4349 */
4350int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4351{
4352 struct nameidata nd;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004353 void *cookie;
Marcin Slusarz694a1762008-06-09 16:40:37 -07004354 int res;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004355
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 nd.depth = 0;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004357 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
Marcin Slusarz694a1762008-06-09 16:40:37 -07004358 if (IS_ERR(cookie))
4359 return PTR_ERR(cookie);
4360
Al Viro0750d562014-03-14 13:42:45 -04004361 res = readlink_copy(buffer, buflen, nd_get_link(&nd));
Marcin Slusarz694a1762008-06-09 16:40:37 -07004362 if (dentry->d_inode->i_op->put_link)
4363 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
4364 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365}
4366
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367/* get the link contents into pagecache */
4368static char *page_getlink(struct dentry * dentry, struct page **ppage)
4369{
Duane Griffinebd09ab2008-12-19 20:47:12 +00004370 char *kaddr;
4371 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372 struct address_space *mapping = dentry->d_inode->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -07004373 page = read_mapping_page(mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 if (IS_ERR(page))
Nick Piggin6fe69002007-05-06 14:49:04 -07004375 return (char*)page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 *ppage = page;
Duane Griffinebd09ab2008-12-19 20:47:12 +00004377 kaddr = kmap(page);
4378 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
4379 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380}
4381
4382int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4383{
4384 struct page *page = NULL;
Al Viro0750d562014-03-14 13:42:45 -04004385 int res = readlink_copy(buffer, buflen, page_getlink(dentry, &page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 if (page) {
4387 kunmap(page);
4388 page_cache_release(page);
4389 }
4390 return res;
4391}
4392
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004393void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004395 struct page *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 nd_set_link(nd, page_getlink(dentry, &page));
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004397 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398}
4399
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004400void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004402 struct page *page = cookie;
4403
4404 if (page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 kunmap(page);
4406 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 }
4408}
4409
Nick Piggin54566b22009-01-04 12:00:53 -08004410/*
4411 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
4412 */
4413int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414{
4415 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004416 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07004417 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08004418 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 char *kaddr;
Nick Piggin54566b22009-01-04 12:00:53 -08004420 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
4421 if (nofs)
4422 flags |= AOP_FLAG_NOFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423
NeilBrown7e53cac2006-03-25 03:07:57 -08004424retry:
Nick Pigginafddba42007-10-16 01:25:01 -07004425 err = pagecache_write_begin(NULL, mapping, 0, len-1,
Nick Piggin54566b22009-01-04 12:00:53 -08004426 flags, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07004428 goto fail;
4429
Cong Wange8e3c3d2011-11-25 23:14:27 +08004430 kaddr = kmap_atomic(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 memcpy(kaddr, symname, len-1);
Cong Wange8e3c3d2011-11-25 23:14:27 +08004432 kunmap_atomic(kaddr);
Nick Pigginafddba42007-10-16 01:25:01 -07004433
4434 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4435 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 if (err < 0)
4437 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07004438 if (err < len-1)
4439 goto retry;
4440
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 mark_inode_dirty(inode);
4442 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443fail:
4444 return err;
4445}
4446
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004447int page_symlink(struct inode *inode, const char *symname, int len)
4448{
4449 return __page_symlink(inode, symname, len,
Nick Piggin54566b22009-01-04 12:00:53 -08004450 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004451}
4452
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08004453const struct inode_operations page_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 .readlink = generic_readlink,
4455 .follow_link = page_follow_link_light,
4456 .put_link = page_put_link,
4457};
4458
Al Viro2d8f3032008-07-22 09:59:21 -04004459EXPORT_SYMBOL(user_path_at);
David Howellscc53ce52011-01-14 18:45:26 +00004460EXPORT_SYMBOL(follow_down_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461EXPORT_SYMBOL(follow_down);
4462EXPORT_SYMBOL(follow_up);
Al Virof6d2ac52012-08-26 12:55:54 -04004463EXPORT_SYMBOL(get_write_access); /* nfsd */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465EXPORT_SYMBOL(lookup_one_len);
4466EXPORT_SYMBOL(page_follow_link_light);
4467EXPORT_SYMBOL(page_put_link);
4468EXPORT_SYMBOL(page_readlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004469EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470EXPORT_SYMBOL(page_symlink);
4471EXPORT_SYMBOL(page_symlink_inode_operations);
Al Virod1811462008-08-02 00:49:18 -04004472EXPORT_SYMBOL(kern_path);
Josef 'Jeff' Sipek16f182002007-07-19 01:48:18 -07004473EXPORT_SYMBOL(vfs_path_lookup);
Al Virof419a2e2008-07-22 00:07:17 -04004474EXPORT_SYMBOL(inode_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475EXPORT_SYMBOL(unlock_rename);
4476EXPORT_SYMBOL(vfs_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477EXPORT_SYMBOL(vfs_link);
4478EXPORT_SYMBOL(vfs_mkdir);
4479EXPORT_SYMBOL(vfs_mknod);
4480EXPORT_SYMBOL(generic_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481EXPORT_SYMBOL(vfs_rename);
4482EXPORT_SYMBOL(vfs_rmdir);
4483EXPORT_SYMBOL(vfs_symlink);
4484EXPORT_SYMBOL(vfs_unlink);
4485EXPORT_SYMBOL(dentry_unhash);
4486EXPORT_SYMBOL(generic_readlink);