blob: 7ee6af0672b244fb8d2c1e132728182400d82304 [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) {
Al Viro7507c7e2015-04-24 15:47:07 -04001549 if (unlikely(nd->path.mnt != path->mnt ||
1550 unlazy_walk(nd, path->dentry))) {
Miklos Szeredi697f5142012-05-21 17:30:05 +02001551 err = -ECHILD;
1552 goto out_err;
Al Viro19660af2011-03-25 10:32:48 -04001553 }
1554 }
Al Viroce57dfc2011-03-13 19:58:58 -04001555 BUG_ON(inode != path->dentry->d_inode);
1556 return 1;
1557 }
1558 path_to_nameidata(path, nd);
1559 nd->inode = inode;
1560 return 0;
Miklos Szeredi697f5142012-05-21 17:30:05 +02001561
1562out_path_put:
1563 path_to_nameidata(path, nd);
1564out_err:
1565 terminate_walk(nd);
1566 return err;
Al Viroce57dfc2011-03-13 19:58:58 -04001567}
1568
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569/*
Al Virob3563792011-03-14 21:54:55 -04001570 * This limits recursive symlink follows to 8, while
1571 * limiting consecutive symlinks to 40.
1572 *
1573 * Without that kind of total limit, nasty chains of consecutive
1574 * symlinks can cause almost arbitrarily long lookups.
1575 */
1576static inline int nested_symlink(struct path *path, struct nameidata *nd)
1577{
1578 int res;
1579
Al Virob3563792011-03-14 21:54:55 -04001580 if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1581 path_put_conditional(path, nd);
1582 path_put(&nd->path);
1583 return -ELOOP;
1584 }
Erez Zadok1a4022f2011-05-21 01:19:59 -04001585 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
Al Virob3563792011-03-14 21:54:55 -04001586
1587 nd->depth++;
1588 current->link_count++;
1589
1590 do {
1591 struct path link = *path;
1592 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04001593
1594 res = follow_link(&link, nd, &cookie);
Al Viro6d7b5aa2012-06-10 04:15:17 -04001595 if (res)
1596 break;
Al Viro21b9b072013-01-24 18:10:25 -05001597 res = walk_component(nd, path, LOOKUP_FOLLOW);
Al Viro574197e2011-03-14 22:20:34 -04001598 put_link(nd, &link, cookie);
Al Virob3563792011-03-14 21:54:55 -04001599 } while (res > 0);
1600
1601 current->link_count--;
1602 nd->depth--;
1603 return res;
1604}
1605
1606/*
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001607 * We can do the critical dentry name comparison and hashing
1608 * operations one word at a time, but we are limited to:
1609 *
1610 * - Architectures with fast unaligned word accesses. We could
1611 * do a "get_unaligned()" if this helps and is sufficiently
1612 * fast.
1613 *
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001614 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1615 * do not trap on the (extremely unlikely) case of a page
1616 * crossing operation.
1617 *
1618 * - Furthermore, we need an efficient 64-bit compile for the
1619 * 64-bit case in order to generate the "number of bytes in
1620 * the final mask". Again, that could be replaced with a
1621 * efficient population count instruction or similar.
1622 */
1623#ifdef CONFIG_DCACHE_WORD_ACCESS
1624
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001625#include <asm/word-at-a-time.h>
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001626
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001627#ifdef CONFIG_64BIT
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001628
1629static inline unsigned int fold_hash(unsigned long hash)
1630{
Linus Torvaldsa8be8af2014-09-13 11:30:10 -07001631 return hash_64(hash, 32);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001632}
1633
1634#else /* 32-bit case */
1635
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001636#define fold_hash(x) (x)
1637
1638#endif
1639
1640unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1641{
1642 unsigned long a, mask;
1643 unsigned long hash = 0;
1644
1645 for (;;) {
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001646 a = load_unaligned_zeropad(name);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001647 if (len < sizeof(unsigned long))
1648 break;
1649 hash += a;
Al Virof132c5b2012-03-22 21:59:52 +00001650 hash *= 9;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001651 name += sizeof(unsigned long);
1652 len -= sizeof(unsigned long);
1653 if (!len)
1654 goto done;
1655 }
Will Deacona5c21dc2013-12-12 17:40:21 +00001656 mask = bytemask_from_count(len);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001657 hash += mask & a;
1658done:
1659 return fold_hash(hash);
1660}
1661EXPORT_SYMBOL(full_name_hash);
1662
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001663/*
1664 * Calculate the length and hash of the path component, and
1665 * return the length of the component;
1666 */
1667static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1668{
Linus Torvalds36126f82012-05-26 10:43:17 -07001669 unsigned long a, b, adata, bdata, mask, hash, len;
1670 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001671
1672 hash = a = 0;
1673 len = -sizeof(unsigned long);
1674 do {
1675 hash = (hash + a) * 9;
1676 len += sizeof(unsigned long);
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001677 a = load_unaligned_zeropad(name+len);
Linus Torvalds36126f82012-05-26 10:43:17 -07001678 b = a ^ REPEAT_BYTE('/');
1679 } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001680
Linus Torvalds36126f82012-05-26 10:43:17 -07001681 adata = prep_zero_mask(a, adata, &constants);
1682 bdata = prep_zero_mask(b, bdata, &constants);
1683
1684 mask = create_zero_mask(adata | bdata);
1685
1686 hash += a & zero_bytemask(mask);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001687 *hashp = fold_hash(hash);
1688
Linus Torvalds36126f82012-05-26 10:43:17 -07001689 return len + find_zero(mask);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001690}
1691
1692#else
1693
Linus Torvalds0145acc2012-03-02 14:32:59 -08001694unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1695{
1696 unsigned long hash = init_name_hash();
1697 while (len--)
1698 hash = partial_name_hash(*name++, hash);
1699 return end_name_hash(hash);
1700}
Linus Torvaldsae942ae2012-03-02 19:40:57 -08001701EXPORT_SYMBOL(full_name_hash);
Linus Torvalds0145acc2012-03-02 14:32:59 -08001702
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001703/*
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001704 * We know there's a real path component here of at least
1705 * one character.
1706 */
1707static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1708{
1709 unsigned long hash = init_name_hash();
1710 unsigned long len = 0, c;
1711
1712 c = (unsigned char)*name;
1713 do {
1714 len++;
1715 hash = partial_name_hash(c, hash);
1716 c = (unsigned char)name[len];
1717 } while (c && c != '/');
1718 *hashp = end_name_hash(hash);
1719 return len;
1720}
1721
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001722#endif
1723
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001724/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01001726 * This is the basic name resolution function, turning a pathname into
1727 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01001729 * Returns 0 and nd will have valid dentry and mnt on success.
1730 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 */
Al Viro6de88d72009-08-09 01:41:57 +04001732static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733{
1734 struct path next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
1737 while (*name=='/')
1738 name++;
1739 if (!*name)
Al Viro086e1832011-02-22 20:56:27 -05001740 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 /* At this point we know we have a real path component. */
1743 for(;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 struct qstr this;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001745 long len;
Al Virofe479a52011-02-22 15:10:03 -05001746 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
Al Viro52094c82011-02-21 21:34:47 -05001748 err = may_lookup(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 if (err)
1750 break;
1751
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001752 len = hash_name(name, &this.hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 this.name = name;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001754 this.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
Al Virofe479a52011-02-22 15:10:03 -05001756 type = LAST_NORM;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001757 if (name[0] == '.') switch (len) {
Al Virofe479a52011-02-22 15:10:03 -05001758 case 2:
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001759 if (name[1] == '.') {
Al Virofe479a52011-02-22 15:10:03 -05001760 type = LAST_DOTDOT;
Al Viro16c2cd72011-02-22 15:50:10 -05001761 nd->flags |= LOOKUP_JUMPED;
1762 }
Al Virofe479a52011-02-22 15:10:03 -05001763 break;
1764 case 1:
1765 type = LAST_DOT;
1766 }
Al Viro5a202bc2011-03-08 14:17:44 -05001767 if (likely(type == LAST_NORM)) {
1768 struct dentry *parent = nd->path.dentry;
Al Viro16c2cd72011-02-22 15:50:10 -05001769 nd->flags &= ~LOOKUP_JUMPED;
Al Viro5a202bc2011-03-08 14:17:44 -05001770 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
Linus Torvaldsda53be12013-05-21 15:22:44 -07001771 err = parent->d_op->d_hash(parent, &this);
Al Viro5a202bc2011-03-08 14:17:44 -05001772 if (err < 0)
1773 break;
1774 }
1775 }
Al Virofe479a52011-02-22 15:10:03 -05001776
Al Viro5f4a6a62013-01-24 18:04:22 -05001777 nd->last = this;
1778 nd->last_type = type;
1779
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001780 if (!name[len])
Al Viro5f4a6a62013-01-24 18:04:22 -05001781 return 0;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001782 /*
1783 * If it wasn't NUL, we know it was '/'. Skip that
1784 * slash, and continue until no more slashes.
1785 */
1786 do {
1787 len++;
1788 } while (unlikely(name[len] == '/'));
1789 if (!name[len])
Al Viro5f4a6a62013-01-24 18:04:22 -05001790 return 0;
1791
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001792 name += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Al Viro21b9b072013-01-24 18:10:25 -05001794 err = walk_component(nd, &next, LOOKUP_FOLLOW);
Al Viroce57dfc2011-03-13 19:58:58 -04001795 if (err < 0)
1796 return err;
Al Virofe479a52011-02-22 15:10:03 -05001797
Al Viroce57dfc2011-03-13 19:58:58 -04001798 if (err) {
Al Virob3563792011-03-14 21:54:55 -04001799 err = nested_symlink(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 if (err)
Al Viroa7472ba2011-03-04 14:39:30 -05001801 return err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001802 }
Miklos Szeredidb8bf542014-04-01 17:08:41 +02001803 if (!d_can_lookup(nd->path.dentry)) {
Al Viro5f4a6a62013-01-24 18:04:22 -05001804 err = -ENOTDIR;
1805 break;
1806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 }
Al Viro951361f2011-03-04 14:44:37 -05001808 terminate_walk(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 return err;
1810}
1811
Al Viro70e9b352011-03-05 21:12:22 -05001812static int path_init(int dfd, const char *name, unsigned int flags,
1813 struct nameidata *nd, struct file **fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814{
Prasanna Medaea3834d2005-04-29 16:00:17 +01001815 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
1817 nd->last_type = LAST_ROOT; /* if there are only slashes... */
Al Viro16c2cd72011-02-22 15:50:10 -05001818 nd->flags = flags | LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 nd->depth = 0;
Al Viro5b6ca022011-03-09 23:04:47 -05001820 if (flags & LOOKUP_ROOT) {
David Howellsb18825a2013-09-12 19:22:53 +01001821 struct dentry *root = nd->root.dentry;
1822 struct inode *inode = root->d_inode;
Al Viro73d049a2011-03-11 12:08:24 -05001823 if (*name) {
Miklos Szeredidb8bf542014-04-01 17:08:41 +02001824 if (!d_can_lookup(root))
Al Viro73d049a2011-03-11 12:08:24 -05001825 return -ENOTDIR;
1826 retval = inode_permission(inode, MAY_EXEC);
1827 if (retval)
1828 return retval;
1829 }
Al Viro5b6ca022011-03-09 23:04:47 -05001830 nd->path = nd->root;
1831 nd->inode = inode;
1832 if (flags & LOOKUP_RCU) {
Al Viro8b61e742013-11-08 12:45:01 -05001833 rcu_read_lock();
Al Viro5b6ca022011-03-09 23:04:47 -05001834 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro48a066e2013-09-29 22:06:07 -04001835 nd->m_seq = read_seqbegin(&mount_lock);
Al Viro5b6ca022011-03-09 23:04:47 -05001836 } else {
1837 path_get(&nd->path);
1838 }
1839 return 0;
1840 }
1841
Al Viro2a737872009-04-07 11:49:53 -04001842 nd->root.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
Al Viro48a066e2013-09-29 22:06:07 -04001844 nd->m_seq = read_seqbegin(&mount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 if (*name=='/') {
Al Viroe41f7d42011-02-22 14:02:58 -05001846 if (flags & LOOKUP_RCU) {
Al Viro8b61e742013-11-08 12:45:01 -05001847 rcu_read_lock();
Al Viroaed47132014-09-13 21:55:46 -04001848 nd->seq = set_root_rcu(nd);
Al Viroe41f7d42011-02-22 14:02:58 -05001849 } else {
1850 set_root(nd);
1851 path_get(&nd->root);
1852 }
Al Viro2a737872009-04-07 11:49:53 -04001853 nd->path = nd->root;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001854 } else if (dfd == AT_FDCWD) {
Al Viroe41f7d42011-02-22 14:02:58 -05001855 if (flags & LOOKUP_RCU) {
1856 struct fs_struct *fs = current->fs;
1857 unsigned seq;
1858
Al Viro8b61e742013-11-08 12:45:01 -05001859 rcu_read_lock();
Al Viroe41f7d42011-02-22 14:02:58 -05001860
1861 do {
1862 seq = read_seqcount_begin(&fs->seq);
1863 nd->path = fs->pwd;
1864 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1865 } while (read_seqcount_retry(&fs->seq, seq));
1866 } else {
1867 get_fs_pwd(current->fs, &nd->path);
1868 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001869 } else {
Jeff Layton582aa642012-12-11 08:56:16 -05001870 /* Caller must check execute permissions on the starting path component */
Al Viro2903ff02012-08-28 12:52:22 -04001871 struct fd f = fdget_raw(dfd);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001872 struct dentry *dentry;
1873
Al Viro2903ff02012-08-28 12:52:22 -04001874 if (!f.file)
1875 return -EBADF;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001876
Al Viro2903ff02012-08-28 12:52:22 -04001877 dentry = f.file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001878
Al Virof52e0c12011-03-14 18:56:51 -04001879 if (*name) {
Miklos Szeredidb8bf542014-04-01 17:08:41 +02001880 if (!d_can_lookup(dentry)) {
Al Viro2903ff02012-08-28 12:52:22 -04001881 fdput(f);
1882 return -ENOTDIR;
1883 }
Al Virof52e0c12011-03-14 18:56:51 -04001884 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001885
Al Viro2903ff02012-08-28 12:52:22 -04001886 nd->path = f.file->f_path;
Al Viroe41f7d42011-02-22 14:02:58 -05001887 if (flags & LOOKUP_RCU) {
Linus Torvalds9c225f22014-03-03 09:36:58 -08001888 if (f.flags & FDPUT_FPUT)
Al Viro2903ff02012-08-28 12:52:22 -04001889 *fp = f.file;
Al Viroe41f7d42011-02-22 14:02:58 -05001890 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro8b61e742013-11-08 12:45:01 -05001891 rcu_read_lock();
Al Viroe41f7d42011-02-22 14:02:58 -05001892 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001893 path_get(&nd->path);
1894 fdput(f);
Al Viroe41f7d42011-02-22 14:02:58 -05001895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 }
Al Viroe41f7d42011-02-22 14:02:58 -05001897
Nick Piggin31e6b012011-01-07 17:49:52 +11001898 nd->inode = nd->path.dentry->d_inode;
Al Viro9b4a9b12009-04-07 11:44:16 -04001899 return 0;
Al Viro9b4a9b12009-04-07 11:44:16 -04001900}
1901
Al Virobd92d7f2011-03-14 19:54:59 -04001902static inline int lookup_last(struct nameidata *nd, struct path *path)
1903{
1904 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1905 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1906
1907 nd->flags &= ~LOOKUP_PARENT;
Al Viro21b9b072013-01-24 18:10:25 -05001908 return walk_component(nd, path, nd->flags & LOOKUP_FOLLOW);
Al Virobd92d7f2011-03-14 19:54:59 -04001909}
1910
Al Viro9b4a9b12009-04-07 11:44:16 -04001911/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroee0827c2011-02-21 23:38:09 -05001912static int path_lookupat(int dfd, const char *name,
Al Viro9b4a9b12009-04-07 11:44:16 -04001913 unsigned int flags, struct nameidata *nd)
1914{
Al Viro70e9b352011-03-05 21:12:22 -05001915 struct file *base = NULL;
Al Virobd92d7f2011-03-14 19:54:59 -04001916 struct path path;
1917 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001918
1919 /*
1920 * Path walking is largely split up into 2 different synchronisation
1921 * schemes, rcu-walk and ref-walk (explained in
1922 * Documentation/filesystems/path-lookup.txt). These share much of the
1923 * path walk code, but some things particularly setup, cleanup, and
1924 * following mounts are sufficiently divergent that functions are
1925 * duplicated. Typically there is a function foo(), and its RCU
1926 * analogue, foo_rcu().
1927 *
1928 * -ECHILD is the error number of choice (just to avoid clashes) that
1929 * is returned if some aspect of an rcu-walk fails. Such an error must
1930 * be handled by restarting a traditional ref-walk (which will always
1931 * be able to complete).
1932 */
Al Virobd92d7f2011-03-14 19:54:59 -04001933 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
Al Viroee0827c2011-02-21 23:38:09 -05001934
Al Virobd92d7f2011-03-14 19:54:59 -04001935 if (unlikely(err))
1936 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001937
1938 current->total_link_count = 0;
Al Virobd92d7f2011-03-14 19:54:59 -04001939 err = link_path_walk(name, nd);
1940
1941 if (!err && !(flags & LOOKUP_PARENT)) {
Al Virobd92d7f2011-03-14 19:54:59 -04001942 err = lookup_last(nd, &path);
1943 while (err > 0) {
1944 void *cookie;
1945 struct path link = path;
Kees Cook800179c2012-07-25 17:29:07 -07001946 err = may_follow_link(&link, nd);
1947 if (unlikely(err))
1948 break;
Al Virobd92d7f2011-03-14 19:54:59 -04001949 nd->flags |= LOOKUP_PARENT;
Al Viro574197e2011-03-14 22:20:34 -04001950 err = follow_link(&link, nd, &cookie);
Al Viro6d7b5aa2012-06-10 04:15:17 -04001951 if (err)
1952 break;
1953 err = lookup_last(nd, &path);
Al Viro574197e2011-03-14 22:20:34 -04001954 put_link(nd, &link, cookie);
Al Virobd92d7f2011-03-14 19:54:59 -04001955 }
1956 }
Al Viroee0827c2011-02-21 23:38:09 -05001957
Al Viro9f1fafe2011-03-25 11:00:12 -04001958 if (!err)
1959 err = complete_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04001960
1961 if (!err && nd->flags & LOOKUP_DIRECTORY) {
Miklos Szeredidb8bf542014-04-01 17:08:41 +02001962 if (!d_can_lookup(nd->path.dentry)) {
Al Virobd92d7f2011-03-14 19:54:59 -04001963 path_put(&nd->path);
Al Virobd23a532011-03-23 09:56:30 -04001964 err = -ENOTDIR;
Al Virobd92d7f2011-03-14 19:54:59 -04001965 }
1966 }
Al Viro16c2cd72011-02-22 15:50:10 -05001967
Al Viro70e9b352011-03-05 21:12:22 -05001968 if (base)
1969 fput(base);
Al Viroee0827c2011-02-21 23:38:09 -05001970
Al Viro5b6ca022011-03-09 23:04:47 -05001971 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
Al Viro2a737872009-04-07 11:49:53 -04001972 path_put(&nd->root);
1973 nd->root.mnt = NULL;
1974 }
Al Virobd92d7f2011-03-14 19:54:59 -04001975 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001976}
Nick Piggin31e6b012011-01-07 17:49:52 +11001977
Jeff Layton873f1ee2012-10-10 15:25:29 -04001978static int filename_lookup(int dfd, struct filename *name,
1979 unsigned int flags, struct nameidata *nd)
1980{
1981 int retval = path_lookupat(dfd, name->name, flags | LOOKUP_RCU, nd);
1982 if (unlikely(retval == -ECHILD))
1983 retval = path_lookupat(dfd, name->name, flags, nd);
1984 if (unlikely(retval == -ESTALE))
1985 retval = path_lookupat(dfd, name->name,
1986 flags | LOOKUP_REVAL, nd);
1987
1988 if (likely(!retval))
Jeff Laytonadb5c242012-10-10 16:43:13 -04001989 audit_inode(name, nd->path.dentry, flags & LOOKUP_PARENT);
Jeff Layton873f1ee2012-10-10 15:25:29 -04001990 return retval;
1991}
1992
Al Viroee0827c2011-02-21 23:38:09 -05001993static int do_path_lookup(int dfd, const char *name,
1994 unsigned int flags, struct nameidata *nd)
1995{
Jeff Layton873f1ee2012-10-10 15:25:29 -04001996 struct filename filename = { .name = name };
Nick Piggin31e6b012011-01-07 17:49:52 +11001997
Jeff Layton873f1ee2012-10-10 15:25:29 -04001998 return filename_lookup(dfd, &filename, flags, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999}
2000
Al Viro79714f72012-06-15 03:01:42 +04002001/* does lookup, returns the object with parent locked */
2002struct dentry *kern_path_locked(const char *name, struct path *path)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002003{
Al Viro79714f72012-06-15 03:01:42 +04002004 struct nameidata nd;
2005 struct dentry *d;
2006 int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd);
2007 if (err)
2008 return ERR_PTR(err);
2009 if (nd.last_type != LAST_NORM) {
2010 path_put(&nd.path);
2011 return ERR_PTR(-EINVAL);
2012 }
2013 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Al Viro1e0ea002012-07-22 23:46:21 +04002014 d = __lookup_hash(&nd.last, nd.path.dentry, 0);
Al Viro79714f72012-06-15 03:01:42 +04002015 if (IS_ERR(d)) {
2016 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2017 path_put(&nd.path);
2018 return d;
2019 }
2020 *path = nd.path;
2021 return d;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002022}
2023
Al Virod1811462008-08-02 00:49:18 -04002024int kern_path(const char *name, unsigned int flags, struct path *path)
2025{
2026 struct nameidata nd;
2027 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
2028 if (!res)
2029 *path = nd.path;
2030 return res;
2031}
2032
Josef 'Jeff' Sipek16f182002007-07-19 01:48:18 -07002033/**
2034 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2035 * @dentry: pointer to dentry of the base directory
2036 * @mnt: pointer to vfs mount of the base directory
2037 * @name: pointer to file name
2038 * @flags: lookup flags
Al Viroe0a01242011-06-27 17:00:37 -04002039 * @path: pointer to struct path to fill
Josef 'Jeff' Sipek16f182002007-07-19 01:48:18 -07002040 */
2041int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2042 const char *name, unsigned int flags,
Al Viroe0a01242011-06-27 17:00:37 -04002043 struct path *path)
Josef 'Jeff' Sipek16f182002007-07-19 01:48:18 -07002044{
Al Viroe0a01242011-06-27 17:00:37 -04002045 struct nameidata nd;
2046 int err;
2047 nd.root.dentry = dentry;
2048 nd.root.mnt = mnt;
2049 BUG_ON(flags & LOOKUP_PARENT);
Al Viro5b6ca022011-03-09 23:04:47 -05002050 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
Al Viroe0a01242011-06-27 17:00:37 -04002051 err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
2052 if (!err)
2053 *path = nd.path;
2054 return err;
Josef 'Jeff' Sipek16f182002007-07-19 01:48:18 -07002055}
2056
James Morris057f6c02007-04-26 00:12:05 -07002057/*
2058 * Restricted form of lookup. Doesn't follow links, single-component only,
2059 * needs parent already locked. Doesn't follow mounts.
2060 * SMP-safe.
2061 */
Adrian Bunka244e162006-03-31 02:32:11 -08002062static struct dentry *lookup_hash(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063{
Al Viro72bd8662012-06-10 17:17:17 -04002064 return __lookup_hash(&nd->last, nd->path.dentry, nd->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065}
2066
Christoph Hellwigeead1912007-10-16 23:25:38 -07002067/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07002068 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07002069 * @name: pathname component to lookup
2070 * @base: base directory to lookup from
2071 * @len: maximum length @len should be interpreted to
2072 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07002073 * Note that this routine is purely a helper for filesystem usage and should
2074 * not be called by generic code. Also note that by using this function the
Christoph Hellwigeead1912007-10-16 23:25:38 -07002075 * nameidata argument is passed to the filesystem methods and a filesystem
2076 * using this helper needs to be prepared for that.
2077 */
James Morris057f6c02007-04-26 00:12:05 -07002078struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2079{
James Morris057f6c02007-04-26 00:12:05 -07002080 struct qstr this;
Al Viro6a96ba52011-03-07 23:49:20 -05002081 unsigned int c;
Miklos Szeredicda309d2012-03-26 12:54:21 +02002082 int err;
James Morris057f6c02007-04-26 00:12:05 -07002083
David Woodhouse2f9092e2009-04-20 23:18:37 +01002084 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
2085
Al Viro6a96ba52011-03-07 23:49:20 -05002086 this.name = name;
2087 this.len = len;
Linus Torvalds0145acc2012-03-02 14:32:59 -08002088 this.hash = full_name_hash(name, len);
Al Viro6a96ba52011-03-07 23:49:20 -05002089 if (!len)
2090 return ERR_PTR(-EACCES);
2091
Al Viro21d8a152012-11-29 22:17:21 -05002092 if (unlikely(name[0] == '.')) {
2093 if (len < 2 || (len == 2 && name[1] == '.'))
2094 return ERR_PTR(-EACCES);
2095 }
2096
Al Viro6a96ba52011-03-07 23:49:20 -05002097 while (len--) {
2098 c = *(const unsigned char *)name++;
2099 if (c == '/' || c == '\0')
2100 return ERR_PTR(-EACCES);
Al Viro6a96ba52011-03-07 23:49:20 -05002101 }
Al Viro5a202bc2011-03-08 14:17:44 -05002102 /*
2103 * See if the low-level filesystem might want
2104 * to use its own hash..
2105 */
2106 if (base->d_flags & DCACHE_OP_HASH) {
Linus Torvaldsda53be12013-05-21 15:22:44 -07002107 int err = base->d_op->d_hash(base, &this);
Al Viro5a202bc2011-03-08 14:17:44 -05002108 if (err < 0)
2109 return ERR_PTR(err);
2110 }
Christoph Hellwigeead1912007-10-16 23:25:38 -07002111
Miklos Szeredicda309d2012-03-26 12:54:21 +02002112 err = inode_permission(base->d_inode, MAY_EXEC);
2113 if (err)
2114 return ERR_PTR(err);
2115
Al Viro72bd8662012-06-10 17:17:17 -04002116 return __lookup_hash(&this, base, 0);
James Morris057f6c02007-04-26 00:12:05 -07002117}
2118
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002119int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2120 struct path *path, int *empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121{
Al Viro2d8f3032008-07-22 09:59:21 -04002122 struct nameidata nd;
Jeff Layton91a27b22012-10-10 15:25:28 -04002123 struct filename *tmp = getname_flags(name, flags, empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 int err = PTR_ERR(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 if (!IS_ERR(tmp)) {
Al Viro2d8f3032008-07-22 09:59:21 -04002126
2127 BUG_ON(flags & LOOKUP_PARENT);
2128
Jeff Layton873f1ee2012-10-10 15:25:29 -04002129 err = filename_lookup(dfd, tmp, flags, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 putname(tmp);
Al Viro2d8f3032008-07-22 09:59:21 -04002131 if (!err)
2132 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 }
2134 return err;
2135}
2136
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002137int user_path_at(int dfd, const char __user *name, unsigned flags,
2138 struct path *path)
2139{
Linus Torvaldsf7493e52012-03-22 16:10:40 -07002140 return user_path_at_empty(dfd, name, flags, path, NULL);
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002141}
2142
Jeff Layton873f1ee2012-10-10 15:25:29 -04002143/*
2144 * NB: most callers don't do anything directly with the reference to the
2145 * to struct filename, but the nd->last pointer points into the name string
2146 * allocated by getname. So we must hold the reference to it until all
2147 * path-walking is complete.
2148 */
Jeff Layton91a27b22012-10-10 15:25:28 -04002149static struct filename *
Jeff Layton9e790bd2012-12-11 12:10:09 -05002150user_path_parent(int dfd, const char __user *path, struct nameidata *nd,
2151 unsigned int flags)
Al Viro2ad94ae2008-07-21 09:32:51 -04002152{
Jeff Layton91a27b22012-10-10 15:25:28 -04002153 struct filename *s = getname(path);
Al Viro2ad94ae2008-07-21 09:32:51 -04002154 int error;
2155
Jeff Layton9e790bd2012-12-11 12:10:09 -05002156 /* only LOOKUP_REVAL is allowed in extra flags */
2157 flags &= LOOKUP_REVAL;
2158
Al Viro2ad94ae2008-07-21 09:32:51 -04002159 if (IS_ERR(s))
Jeff Layton91a27b22012-10-10 15:25:28 -04002160 return s;
Al Viro2ad94ae2008-07-21 09:32:51 -04002161
Jeff Layton9e790bd2012-12-11 12:10:09 -05002162 error = filename_lookup(dfd, s, flags | LOOKUP_PARENT, nd);
Jeff Layton91a27b22012-10-10 15:25:28 -04002163 if (error) {
Al Viro2ad94ae2008-07-21 09:32:51 -04002164 putname(s);
Jeff Layton91a27b22012-10-10 15:25:28 -04002165 return ERR_PTR(error);
2166 }
Al Viro2ad94ae2008-07-21 09:32:51 -04002167
Jeff Layton91a27b22012-10-10 15:25:28 -04002168 return s;
Al Viro2ad94ae2008-07-21 09:32:51 -04002169}
2170
Jeff Layton80334262013-07-26 06:23:25 -04002171/**
Al Viro197df042013-09-08 14:03:27 -04002172 * mountpoint_last - look up last component for umount
Jeff Layton80334262013-07-26 06:23:25 -04002173 * @nd: pathwalk nameidata - currently pointing at parent directory of "last"
2174 * @path: pointer to container for result
2175 *
2176 * This is a special lookup_last function just for umount. In this case, we
2177 * need to resolve the path without doing any revalidation.
2178 *
2179 * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
2180 * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
2181 * in almost all cases, this lookup will be served out of the dcache. The only
2182 * cases where it won't are if nd->last refers to a symlink or the path is
2183 * bogus and it doesn't exist.
2184 *
2185 * Returns:
2186 * -error: if there was an error during lookup. This includes -ENOENT if the
2187 * lookup found a negative dentry. The nd->path reference will also be
2188 * put in this case.
2189 *
2190 * 0: if we successfully resolved nd->path and found it to not to be a
2191 * symlink that needs to be followed. "path" will also be populated.
2192 * The nd->path reference will also be put.
2193 *
2194 * 1: if we successfully resolved nd->last and found it to be a symlink
2195 * that needs to be followed. "path" will be populated with the path
2196 * to the link, and nd->path will *not* be put.
2197 */
2198static int
Al Viro197df042013-09-08 14:03:27 -04002199mountpoint_last(struct nameidata *nd, struct path *path)
Jeff Layton80334262013-07-26 06:23:25 -04002200{
2201 int error = 0;
2202 struct dentry *dentry;
2203 struct dentry *dir = nd->path.dentry;
2204
Al Viro35759522013-09-08 13:41:33 -04002205 /* If we're in rcuwalk, drop out of it to handle last component */
2206 if (nd->flags & LOOKUP_RCU) {
2207 if (unlazy_walk(nd, NULL)) {
2208 error = -ECHILD;
2209 goto out;
2210 }
Jeff Layton80334262013-07-26 06:23:25 -04002211 }
2212
2213 nd->flags &= ~LOOKUP_PARENT;
2214
2215 if (unlikely(nd->last_type != LAST_NORM)) {
2216 error = handle_dots(nd, nd->last_type);
Al Viro35759522013-09-08 13:41:33 -04002217 if (error)
2218 goto out;
2219 dentry = dget(nd->path.dentry);
2220 goto done;
Jeff Layton80334262013-07-26 06:23:25 -04002221 }
2222
2223 mutex_lock(&dir->d_inode->i_mutex);
2224 dentry = d_lookup(dir, &nd->last);
2225 if (!dentry) {
2226 /*
2227 * No cached dentry. Mounted dentries are pinned in the cache,
2228 * so that means that this dentry is probably a symlink or the
2229 * path doesn't actually point to a mounted dentry.
2230 */
2231 dentry = d_alloc(dir, &nd->last);
2232 if (!dentry) {
2233 error = -ENOMEM;
Dave Jonesbcceeeb2013-09-10 17:04:25 -04002234 mutex_unlock(&dir->d_inode->i_mutex);
Al Viro35759522013-09-08 13:41:33 -04002235 goto out;
Jeff Layton80334262013-07-26 06:23:25 -04002236 }
Al Viro35759522013-09-08 13:41:33 -04002237 dentry = lookup_real(dir->d_inode, dentry, nd->flags);
2238 error = PTR_ERR(dentry);
Dave Jonesbcceeeb2013-09-10 17:04:25 -04002239 if (IS_ERR(dentry)) {
2240 mutex_unlock(&dir->d_inode->i_mutex);
Al Viro35759522013-09-08 13:41:33 -04002241 goto out;
Dave Jonesbcceeeb2013-09-10 17:04:25 -04002242 }
Jeff Layton80334262013-07-26 06:23:25 -04002243 }
2244 mutex_unlock(&dir->d_inode->i_mutex);
2245
Al Viro35759522013-09-08 13:41:33 -04002246done:
Al Virof0754e52014-04-19 12:30:58 -04002247 if (!dentry->d_inode || d_is_negative(dentry)) {
Al Viro35759522013-09-08 13:41:33 -04002248 error = -ENOENT;
2249 dput(dentry);
2250 goto out;
Jeff Layton80334262013-07-26 06:23:25 -04002251 }
Al Viro35759522013-09-08 13:41:33 -04002252 path->dentry = dentry;
Vasily Averin9b32e182014-07-21 12:30:23 +04002253 path->mnt = nd->path.mnt;
David Howellsb18825a2013-09-12 19:22:53 +01002254 if (should_follow_link(dentry, nd->flags & LOOKUP_FOLLOW))
Al Viro35759522013-09-08 13:41:33 -04002255 return 1;
Vasily Averin9b32e182014-07-21 12:30:23 +04002256 mntget(path->mnt);
Al Viro35759522013-09-08 13:41:33 -04002257 follow_mount(path);
2258 error = 0;
2259out:
Jeff Layton80334262013-07-26 06:23:25 -04002260 terminate_walk(nd);
2261 return error;
2262}
2263
2264/**
Al Viro197df042013-09-08 14:03:27 -04002265 * path_mountpoint - look up a path to be umounted
Jeff Layton80334262013-07-26 06:23:25 -04002266 * @dfd: directory file descriptor to start walk from
2267 * @name: full pathname to walk
Randy Dunlap606d6fe2013-10-19 14:56:55 -07002268 * @path: pointer to container for result
Jeff Layton80334262013-07-26 06:23:25 -04002269 * @flags: lookup flags
Jeff Layton80334262013-07-26 06:23:25 -04002270 *
2271 * Look up the given name, but don't attempt to revalidate the last component.
Randy Dunlap606d6fe2013-10-19 14:56:55 -07002272 * Returns 0 and "path" will be valid on success; Returns error otherwise.
Jeff Layton80334262013-07-26 06:23:25 -04002273 */
2274static int
Al Viro197df042013-09-08 14:03:27 -04002275path_mountpoint(int dfd, const char *name, struct path *path, unsigned int flags)
Jeff Layton80334262013-07-26 06:23:25 -04002276{
2277 struct file *base = NULL;
2278 struct nameidata nd;
2279 int err;
2280
2281 err = path_init(dfd, name, flags | LOOKUP_PARENT, &nd, &base);
2282 if (unlikely(err))
2283 return err;
2284
2285 current->total_link_count = 0;
2286 err = link_path_walk(name, &nd);
2287 if (err)
2288 goto out;
2289
Al Viro197df042013-09-08 14:03:27 -04002290 err = mountpoint_last(&nd, path);
Jeff Layton80334262013-07-26 06:23:25 -04002291 while (err > 0) {
2292 void *cookie;
2293 struct path link = *path;
2294 err = may_follow_link(&link, &nd);
2295 if (unlikely(err))
2296 break;
2297 nd.flags |= LOOKUP_PARENT;
2298 err = follow_link(&link, &nd, &cookie);
2299 if (err)
2300 break;
Al Viro197df042013-09-08 14:03:27 -04002301 err = mountpoint_last(&nd, path);
Jeff Layton80334262013-07-26 06:23:25 -04002302 put_link(&nd, &link, cookie);
2303 }
2304out:
2305 if (base)
2306 fput(base);
2307
2308 if (nd.root.mnt && !(nd.flags & LOOKUP_ROOT))
2309 path_put(&nd.root);
2310
2311 return err;
2312}
2313
Al Viro2d864652013-09-08 20:18:44 -04002314static int
2315filename_mountpoint(int dfd, struct filename *s, struct path *path,
2316 unsigned int flags)
2317{
2318 int error = path_mountpoint(dfd, s->name, path, flags | LOOKUP_RCU);
2319 if (unlikely(error == -ECHILD))
2320 error = path_mountpoint(dfd, s->name, path, flags);
2321 if (unlikely(error == -ESTALE))
2322 error = path_mountpoint(dfd, s->name, path, flags | LOOKUP_REVAL);
2323 if (likely(!error))
2324 audit_inode(s, path->dentry, 0);
2325 return error;
2326}
2327
Jeff Layton80334262013-07-26 06:23:25 -04002328/**
Al Viro197df042013-09-08 14:03:27 -04002329 * user_path_mountpoint_at - lookup a path from userland in order to umount it
Jeff Layton80334262013-07-26 06:23:25 -04002330 * @dfd: directory file descriptor
2331 * @name: pathname from userland
2332 * @flags: lookup flags
2333 * @path: pointer to container to hold result
2334 *
2335 * A umount is a special case for path walking. We're not actually interested
2336 * in the inode in this situation, and ESTALE errors can be a problem. We
2337 * simply want track down the dentry and vfsmount attached at the mountpoint
2338 * and avoid revalidating the last component.
2339 *
2340 * Returns 0 and populates "path" on success.
2341 */
2342int
Al Viro197df042013-09-08 14:03:27 -04002343user_path_mountpoint_at(int dfd, const char __user *name, unsigned int flags,
Jeff Layton80334262013-07-26 06:23:25 -04002344 struct path *path)
2345{
2346 struct filename *s = getname(name);
2347 int error;
Jeff Layton80334262013-07-26 06:23:25 -04002348 if (IS_ERR(s))
2349 return PTR_ERR(s);
Al Viro2d864652013-09-08 20:18:44 -04002350 error = filename_mountpoint(dfd, s, path, flags);
Jeff Layton80334262013-07-26 06:23:25 -04002351 putname(s);
2352 return error;
2353}
2354
Al Viro2d864652013-09-08 20:18:44 -04002355int
2356kern_path_mountpoint(int dfd, const char *name, struct path *path,
2357 unsigned int flags)
2358{
2359 struct filename s = {.name = name};
2360 return filename_mountpoint(dfd, &s, path, flags);
2361}
2362EXPORT_SYMBOL(kern_path_mountpoint);
2363
Miklos Szerediae3116532014-10-24 00:14:36 +02002364int __check_sticky(struct inode *dir, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365{
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002366 kuid_t fsuid = current_fsuid();
David Howellsda9592e2008-11-14 10:39:05 +11002367
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002368 if (uid_eq(inode->i_uid, fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 return 0;
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002370 if (uid_eq(dir->i_uid, fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 return 0;
Andy Lutomirski5bacea82014-06-10 12:45:42 -07002372 return !capable_wrt_inode_uidgid(inode, CAP_FOWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373}
Miklos Szerediae3116532014-10-24 00:14:36 +02002374EXPORT_SYMBOL(__check_sticky);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
2376/*
2377 * Check whether we can remove a link victim from directory dir, check
2378 * whether the type of victim is right.
2379 * 1. We can't do it if dir is read-only (done in permission())
2380 * 2. We should have write and exec permissions on dir
2381 * 3. We can't remove anything from append-only dir
2382 * 4. We can't do anything with immutable dir (done in permission())
2383 * 5. If the sticky bit on dir is set we should either
2384 * a. be owner of dir, or
2385 * b. be owner of victim, or
2386 * c. have CAP_FOWNER capability
2387 * 6. If the victim is append-only or immutable we can't do antyhing with
2388 * links pointing to it.
2389 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2390 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2391 * 9. We can't remove a root or mountpoint.
2392 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
2393 * nfs_async_unlink().
2394 */
David Howellsb18825a2013-09-12 19:22:53 +01002395static int may_delete(struct inode *dir, struct dentry *victim, bool isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396{
David Howellsb18825a2013-09-12 19:22:53 +01002397 struct inode *inode = victim->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 int error;
2399
David Howellsb18825a2013-09-12 19:22:53 +01002400 if (d_is_negative(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 return -ENOENT;
David Howellsb18825a2013-09-12 19:22:53 +01002402 BUG_ON(!inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
2404 BUG_ON(victim->d_parent->d_inode != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04002405 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
Al Virof419a2e2008-07-22 00:07:17 -04002407 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 if (error)
2409 return error;
2410 if (IS_APPEND(dir))
2411 return -EPERM;
David Howellsb18825a2013-09-12 19:22:53 +01002412
2413 if (check_sticky(dir, inode) || IS_APPEND(inode) ||
2414 IS_IMMUTABLE(inode) || IS_SWAPFILE(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 return -EPERM;
2416 if (isdir) {
Miklos Szeredidb8bf542014-04-01 17:08:41 +02002417 if (!d_is_dir(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 return -ENOTDIR;
2419 if (IS_ROOT(victim))
2420 return -EBUSY;
Miklos Szeredidb8bf542014-04-01 17:08:41 +02002421 } else if (d_is_dir(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 return -EISDIR;
2423 if (IS_DEADDIR(dir))
2424 return -ENOENT;
2425 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2426 return -EBUSY;
2427 return 0;
2428}
2429
2430/* Check whether we can create an object with dentry child in directory
2431 * dir.
2432 * 1. We can't do it if child already exists (open has special treatment for
2433 * this case, but since we are inlined it's OK)
2434 * 2. We can't do it if dir is read-only (done in permission())
2435 * 3. We should have write and exec permissions on dir
2436 * 4. We can't do it if dir is immutable (done in permission())
2437 */
Miklos Szeredia95164d2008-07-30 15:08:48 +02002438static inline int may_create(struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439{
Jeff Layton14e972b2013-05-08 10:25:58 -04002440 audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 if (child->d_inode)
2442 return -EEXIST;
2443 if (IS_DEADDIR(dir))
2444 return -ENOENT;
Al Virof419a2e2008-07-22 00:07:17 -04002445 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446}
2447
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448/*
2449 * p1 and p2 should be directories on the same fs.
2450 */
2451struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2452{
2453 struct dentry *p;
2454
2455 if (p1 == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07002456 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 return NULL;
2458 }
2459
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002460 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002462 p = d_ancestor(p2, p1);
2463 if (p) {
2464 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2465 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
2466 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 }
2468
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002469 p = d_ancestor(p1, p2);
2470 if (p) {
2471 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2472 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2473 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 }
2475
Ingo Molnarf2eace22006-07-03 00:25:05 -07002476 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Miklos Szeredi9ba06562014-10-27 15:42:01 +01002477 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 return NULL;
2479}
2480
2481void unlock_rename(struct dentry *p1, struct dentry *p2)
2482{
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002483 mutex_unlock(&p1->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 if (p1 != p2) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002485 mutex_unlock(&p2->d_inode->i_mutex);
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002486 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 }
2488}
2489
Al Viro4acdaf22011-07-26 01:42:34 -04002490int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viro312b63f2012-06-10 18:09:36 -04002491 bool want_excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002493 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 if (error)
2495 return error;
2496
Al Viroacfa4382008-12-04 10:06:33 -05002497 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 return -EACCES; /* shouldn't it be ENOSYS? */
2499 mode &= S_IALLUGO;
2500 mode |= S_IFREG;
2501 error = security_inode_create(dir, dentry, mode);
2502 if (error)
2503 return error;
Al Viro312b63f2012-06-10 18:09:36 -04002504 error = dir->i_op->create(dir, dentry, mode, want_excl);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002505 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002506 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 return error;
2508}
2509
Al Viro73d049a2011-03-11 12:08:24 -05002510static int may_open(struct path *path, int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002512 struct dentry *dentry = path->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 struct inode *inode = dentry->d_inode;
2514 int error;
2515
Al Virobcda7652011-03-13 16:42:14 -04002516 /* O_PATH? */
2517 if (!acc_mode)
2518 return 0;
2519
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 if (!inode)
2521 return -ENOENT;
2522
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002523 switch (inode->i_mode & S_IFMT) {
2524 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002526 case S_IFDIR:
2527 if (acc_mode & MAY_WRITE)
2528 return -EISDIR;
2529 break;
2530 case S_IFBLK:
2531 case S_IFCHR:
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002532 if (path->mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002534 /*FALLTHRU*/
2535 case S_IFIFO:
2536 case S_IFSOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002538 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08002539 }
Dave Hansenb41572e2007-10-16 23:31:14 -07002540
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002541 error = inode_permission(inode, acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07002542 if (error)
2543 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05002544
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 /*
2546 * An append-only file must be opened in append mode for writing.
2547 */
2548 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05002549 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05002550 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05002552 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 }
2554
2555 /* O_NOATIME can only be set by the owner or superuser */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07002556 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
Al Viro7715b522009-12-16 03:54:00 -05002557 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
J. Bruce Fieldsf3c7691e2011-09-21 10:58:13 -04002559 return 0;
Al Viro7715b522009-12-16 03:54:00 -05002560}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561
Jeff Laytone1181ee2010-12-07 16:19:50 -05002562static int handle_truncate(struct file *filp)
Al Viro7715b522009-12-16 03:54:00 -05002563{
Jeff Laytone1181ee2010-12-07 16:19:50 -05002564 struct path *path = &filp->f_path;
Al Viro7715b522009-12-16 03:54:00 -05002565 struct inode *inode = path->dentry->d_inode;
2566 int error = get_write_access(inode);
2567 if (error)
2568 return error;
2569 /*
2570 * Refuse to truncate files with mandatory locks held on them.
2571 */
2572 error = locks_verify_locked(inode);
2573 if (!error)
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +09002574 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05002575 if (!error) {
2576 error = do_truncate(path->dentry, 0,
2577 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
Jeff Laytone1181ee2010-12-07 16:19:50 -05002578 filp);
Al Viro7715b522009-12-16 03:54:00 -05002579 }
2580 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04002581 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582}
2583
Dave Hansend57999e2008-02-15 14:37:27 -08002584static inline int open_to_namei_flags(int flag)
2585{
Al Viro8a5e9292011-06-25 19:15:54 -04002586 if ((flag & O_ACCMODE) == 3)
2587 flag--;
Dave Hansend57999e2008-02-15 14:37:27 -08002588 return flag;
2589}
2590
Miklos Szeredid18e9002012-06-05 15:10:17 +02002591static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode)
2592{
2593 int error = security_path_mknod(dir, dentry, mode, 0);
2594 if (error)
2595 return error;
2596
2597 error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
2598 if (error)
2599 return error;
2600
2601 return security_inode_create(dir->dentry->d_inode, dentry, mode);
2602}
2603
David Howells1acf0af2012-06-14 16:13:46 +01002604/*
2605 * Attempt to atomically look up, create and open a file from a negative
2606 * dentry.
2607 *
2608 * Returns 0 if successful. The file will have been created and attached to
2609 * @file by the filesystem calling finish_open().
2610 *
2611 * Returns 1 if the file was looked up only or didn't need creating. The
2612 * caller will need to perform the open themselves. @path will have been
2613 * updated to point to the new dentry. This may be negative.
2614 *
2615 * Returns an error code otherwise.
2616 */
Al Viro2675a4e2012-06-22 12:41:10 +04002617static int atomic_open(struct nameidata *nd, struct dentry *dentry,
2618 struct path *path, struct file *file,
2619 const struct open_flags *op,
Al Viro64894cf2012-07-31 00:53:35 +04002620 bool got_write, bool need_lookup,
Al Viro2675a4e2012-06-22 12:41:10 +04002621 int *opened)
Miklos Szeredid18e9002012-06-05 15:10:17 +02002622{
2623 struct inode *dir = nd->path.dentry->d_inode;
2624 unsigned open_flag = open_to_namei_flags(op->open_flag);
2625 umode_t mode;
2626 int error;
2627 int acc_mode;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002628 int create_error = 0;
2629 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
Miklos Szeredi116cc022013-09-16 14:52:05 +02002630 bool excl;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002631
2632 BUG_ON(dentry->d_inode);
2633
2634 /* Don't create child dentry for a dead directory. */
2635 if (unlikely(IS_DEADDIR(dir))) {
Al Viro2675a4e2012-06-22 12:41:10 +04002636 error = -ENOENT;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002637 goto out;
2638 }
2639
Miklos Szeredi62b259d2012-08-15 13:01:24 +02002640 mode = op->mode;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002641 if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
2642 mode &= ~current_umask();
2643
Miklos Szeredi116cc022013-09-16 14:52:05 +02002644 excl = (open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT);
2645 if (excl)
Miklos Szeredid18e9002012-06-05 15:10:17 +02002646 open_flag &= ~O_TRUNC;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002647
2648 /*
2649 * Checking write permission is tricky, bacuse we don't know if we are
2650 * going to actually need it: O_CREAT opens should work as long as the
2651 * file exists. But checking existence breaks atomicity. The trick is
2652 * to check access and if not granted clear O_CREAT from the flags.
2653 *
2654 * Another problem is returing the "right" error value (e.g. for an
2655 * O_EXCL open we want to return EEXIST not EROFS).
2656 */
Al Viro64894cf2012-07-31 00:53:35 +04002657 if (((open_flag & (O_CREAT | O_TRUNC)) ||
2658 (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) {
2659 if (!(open_flag & O_CREAT)) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002660 /*
2661 * No O_CREATE -> atomicity not a requirement -> fall
2662 * back to lookup + open
2663 */
2664 goto no_open;
2665 } else if (open_flag & (O_EXCL | O_TRUNC)) {
2666 /* Fall back and fail with the right error */
Al Viro64894cf2012-07-31 00:53:35 +04002667 create_error = -EROFS;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002668 goto no_open;
2669 } else {
2670 /* No side effects, safe to clear O_CREAT */
Al Viro64894cf2012-07-31 00:53:35 +04002671 create_error = -EROFS;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002672 open_flag &= ~O_CREAT;
2673 }
2674 }
2675
2676 if (open_flag & O_CREAT) {
Miklos Szeredi38227f72012-08-15 13:01:24 +02002677 error = may_o_create(&nd->path, dentry, mode);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002678 if (error) {
2679 create_error = error;
2680 if (open_flag & O_EXCL)
2681 goto no_open;
2682 open_flag &= ~O_CREAT;
2683 }
2684 }
2685
2686 if (nd->flags & LOOKUP_DIRECTORY)
2687 open_flag |= O_DIRECTORY;
2688
Al Viro30d90492012-06-22 12:40:19 +04002689 file->f_path.dentry = DENTRY_NOT_SET;
2690 file->f_path.mnt = nd->path.mnt;
2691 error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode,
Al Viro47237682012-06-10 05:01:45 -04002692 opened);
Al Virod9585272012-06-22 12:39:14 +04002693 if (error < 0) {
Al Virod9585272012-06-22 12:39:14 +04002694 if (create_error && error == -ENOENT)
2695 error = create_error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002696 goto out;
2697 }
2698
Al Virod9585272012-06-22 12:39:14 +04002699 if (error) { /* returned 1, that is */
Al Viro30d90492012-06-22 12:40:19 +04002700 if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
Al Viro2675a4e2012-06-22 12:41:10 +04002701 error = -EIO;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002702 goto out;
2703 }
Al Viro30d90492012-06-22 12:40:19 +04002704 if (file->f_path.dentry) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002705 dput(dentry);
Al Viro30d90492012-06-22 12:40:19 +04002706 dentry = file->f_path.dentry;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002707 }
Al Viro03da6332013-09-16 19:22:33 -04002708 if (*opened & FILE_CREATED)
2709 fsnotify_create(dir, dentry);
2710 if (!dentry->d_inode) {
2711 WARN_ON(*opened & FILE_CREATED);
2712 if (create_error) {
2713 error = create_error;
2714 goto out;
2715 }
2716 } else {
2717 if (excl && !(*opened & FILE_CREATED)) {
2718 error = -EEXIST;
2719 goto out;
2720 }
Sage Weil62b2ce92012-08-15 13:30:12 -07002721 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02002722 goto looked_up;
2723 }
2724
2725 /*
2726 * We didn't have the inode before the open, so check open permission
2727 * here.
2728 */
Al Viro03da6332013-09-16 19:22:33 -04002729 acc_mode = op->acc_mode;
2730 if (*opened & FILE_CREATED) {
2731 WARN_ON(!(open_flag & O_CREAT));
2732 fsnotify_create(dir, dentry);
2733 acc_mode = MAY_OPEN;
2734 }
Al Viro2675a4e2012-06-22 12:41:10 +04002735 error = may_open(&file->f_path, acc_mode, open_flag);
2736 if (error)
2737 fput(file);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002738
2739out:
2740 dput(dentry);
Al Viro2675a4e2012-06-22 12:41:10 +04002741 return error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002742
Miklos Szeredid18e9002012-06-05 15:10:17 +02002743no_open:
2744 if (need_lookup) {
Al Viro72bd8662012-06-10 17:17:17 -04002745 dentry = lookup_real(dir, dentry, nd->flags);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002746 if (IS_ERR(dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04002747 return PTR_ERR(dentry);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002748
2749 if (create_error) {
2750 int open_flag = op->open_flag;
2751
Al Viro2675a4e2012-06-22 12:41:10 +04002752 error = create_error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002753 if ((open_flag & O_EXCL)) {
2754 if (!dentry->d_inode)
2755 goto out;
2756 } else if (!dentry->d_inode) {
2757 goto out;
2758 } else if ((open_flag & O_TRUNC) &&
2759 S_ISREG(dentry->d_inode->i_mode)) {
2760 goto out;
2761 }
2762 /* will fail later, go on to get the right error */
2763 }
2764 }
2765looked_up:
2766 path->dentry = dentry;
2767 path->mnt = nd->path.mnt;
Al Viro2675a4e2012-06-22 12:41:10 +04002768 return 1;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002769}
2770
Nick Piggin31e6b012011-01-07 17:49:52 +11002771/*
David Howells1acf0af2012-06-14 16:13:46 +01002772 * Look up and maybe create and open the last component.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002773 *
2774 * Must be called with i_mutex held on parent.
2775 *
David Howells1acf0af2012-06-14 16:13:46 +01002776 * Returns 0 if the file was successfully atomically created (if necessary) and
2777 * opened. In this case the file will be returned attached to @file.
2778 *
2779 * Returns 1 if the file was not completely opened at this time, though lookups
2780 * and creations will have been performed and the dentry returned in @path will
2781 * be positive upon return if O_CREAT was specified. If O_CREAT wasn't
2782 * specified then a negative dentry may be returned.
2783 *
2784 * An error code is returned otherwise.
2785 *
2786 * FILE_CREATE will be set in @*opened if the dentry was created and will be
2787 * cleared otherwise prior to returning.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002788 */
Al Viro2675a4e2012-06-22 12:41:10 +04002789static int lookup_open(struct nameidata *nd, struct path *path,
2790 struct file *file,
2791 const struct open_flags *op,
Al Viro64894cf2012-07-31 00:53:35 +04002792 bool got_write, int *opened)
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002793{
2794 struct dentry *dir = nd->path.dentry;
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002795 struct inode *dir_inode = dir->d_inode;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002796 struct dentry *dentry;
2797 int error;
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002798 bool need_lookup;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002799
Al Viro47237682012-06-10 05:01:45 -04002800 *opened &= ~FILE_CREATED;
Al Viro201f9562012-06-22 12:42:10 +04002801 dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002802 if (IS_ERR(dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04002803 return PTR_ERR(dentry);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002804
Miklos Szeredid18e9002012-06-05 15:10:17 +02002805 /* Cached positive dentry: will open in f_op->open */
2806 if (!need_lookup && dentry->d_inode)
2807 goto out_no_open;
2808
2809 if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) {
Al Viro64894cf2012-07-31 00:53:35 +04002810 return atomic_open(nd, dentry, path, file, op, got_write,
Al Viro47237682012-06-10 05:01:45 -04002811 need_lookup, opened);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002812 }
2813
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002814 if (need_lookup) {
2815 BUG_ON(dentry->d_inode);
2816
Al Viro72bd8662012-06-10 17:17:17 -04002817 dentry = lookup_real(dir_inode, dentry, nd->flags);
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002818 if (IS_ERR(dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04002819 return PTR_ERR(dentry);
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002820 }
2821
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002822 /* Negative dentry, just create the file */
2823 if (!dentry->d_inode && (op->open_flag & O_CREAT)) {
2824 umode_t mode = op->mode;
2825 if (!IS_POSIXACL(dir->d_inode))
2826 mode &= ~current_umask();
2827 /*
2828 * This write is needed to ensure that a
2829 * rw->ro transition does not occur between
2830 * the time when the file is created and when
2831 * a permanent write count is taken through
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02002832 * the 'struct file' in finish_open().
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002833 */
Al Viro64894cf2012-07-31 00:53:35 +04002834 if (!got_write) {
2835 error = -EROFS;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002836 goto out_dput;
Al Viro64894cf2012-07-31 00:53:35 +04002837 }
Al Viro47237682012-06-10 05:01:45 -04002838 *opened |= FILE_CREATED;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002839 error = security_path_mknod(&nd->path, dentry, mode, 0);
2840 if (error)
2841 goto out_dput;
Al Viro312b63f2012-06-10 18:09:36 -04002842 error = vfs_create(dir->d_inode, dentry, mode,
2843 nd->flags & LOOKUP_EXCL);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002844 if (error)
2845 goto out_dput;
2846 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02002847out_no_open:
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002848 path->dentry = dentry;
2849 path->mnt = nd->path.mnt;
Al Viro2675a4e2012-06-22 12:41:10 +04002850 return 1;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002851
2852out_dput:
2853 dput(dentry);
Al Viro2675a4e2012-06-22 12:41:10 +04002854 return error;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002855}
2856
2857/*
Al Virofe2d35f2011-03-05 22:58:25 -05002858 * Handle the last step of open()
Nick Piggin31e6b012011-01-07 17:49:52 +11002859 */
Al Viro2675a4e2012-06-22 12:41:10 +04002860static int do_last(struct nameidata *nd, struct path *path,
2861 struct file *file, const struct open_flags *op,
Jeff Layton669abf42012-10-10 16:43:10 -04002862 int *opened, struct filename *name)
Al Virofb1cc552009-12-24 01:58:28 -05002863{
Al Viroa1e28032009-12-24 02:12:06 -05002864 struct dentry *dir = nd->path.dentry;
Al Viroca344a892011-03-09 00:36:45 -05002865 int open_flag = op->open_flag;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002866 bool will_truncate = (open_flag & O_TRUNC) != 0;
Al Viro64894cf2012-07-31 00:53:35 +04002867 bool got_write = false;
Al Virobcda7652011-03-13 16:42:14 -04002868 int acc_mode = op->acc_mode;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02002869 struct inode *inode;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002870 bool symlink_ok = false;
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02002871 struct path save_parent = { .dentry = NULL, .mnt = NULL };
2872 bool retried = false;
Al Viro16c2cd72011-02-22 15:50:10 -05002873 int error;
Al Virofb1cc552009-12-24 01:58:28 -05002874
Al Viroc3e380b2011-02-23 13:39:45 -05002875 nd->flags &= ~LOOKUP_PARENT;
2876 nd->flags |= op->intent;
2877
Al Virobc77daa2013-06-06 09:12:33 -04002878 if (nd->last_type != LAST_NORM) {
Al Virofe2d35f2011-03-05 22:58:25 -05002879 error = handle_dots(nd, nd->last_type);
2880 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04002881 return error;
Miklos Szeredie83db162012-06-05 15:10:29 +02002882 goto finish_open;
Al Viro1f36f772009-12-26 10:56:19 -05002883 }
Al Viro67ee3ad2009-12-26 07:01:01 -05002884
Al Viroca344a892011-03-09 00:36:45 -05002885 if (!(open_flag & O_CREAT)) {
Al Virofe2d35f2011-03-05 22:58:25 -05002886 if (nd->last.name[nd->last.len])
2887 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
Al Virobcda7652011-03-13 16:42:14 -04002888 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002889 symlink_ok = true;
Al Virofe2d35f2011-03-05 22:58:25 -05002890 /* we _can_ be in RCU mode here */
Al Viroe97cdc82013-01-24 18:16:00 -05002891 error = lookup_fast(nd, path, &inode);
Miklos Szeredi71574862012-06-05 15:10:14 +02002892 if (likely(!error))
2893 goto finish_lookup;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02002894
Miklos Szeredi71574862012-06-05 15:10:14 +02002895 if (error < 0)
Al Viro2675a4e2012-06-22 12:41:10 +04002896 goto out;
Miklos Szeredi37d7fff2012-06-05 15:10:12 +02002897
Miklos Szeredi71574862012-06-05 15:10:14 +02002898 BUG_ON(nd->inode != dir->d_inode);
Miklos Szeredib6183df2012-06-05 15:10:13 +02002899 } else {
2900 /* create side of things */
2901 /*
2902 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
2903 * has been cleared when we got to the last component we are
2904 * about to look up
2905 */
2906 error = complete_walk(nd);
2907 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04002908 return error;
Miklos Szeredib6183df2012-06-05 15:10:13 +02002909
Jeff Layton33e22082013-04-12 15:16:32 -04002910 audit_inode(name, dir, LOOKUP_PARENT);
Miklos Szeredib6183df2012-06-05 15:10:13 +02002911 error = -EISDIR;
2912 /* trailing slashes? */
2913 if (nd->last.name[nd->last.len])
Al Viro2675a4e2012-06-22 12:41:10 +04002914 goto out;
Al Virofe2d35f2011-03-05 22:58:25 -05002915 }
2916
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02002917retry_lookup:
Al Viro64894cf2012-07-31 00:53:35 +04002918 if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
2919 error = mnt_want_write(nd->path.mnt);
2920 if (!error)
2921 got_write = true;
2922 /*
2923 * do _not_ fail yet - we might not need that or fail with
2924 * a different error; let lookup_open() decide; we'll be
2925 * dropping this one anyway.
2926 */
2927 }
Al Viroa1e28032009-12-24 02:12:06 -05002928 mutex_lock(&dir->d_inode->i_mutex);
Al Viro64894cf2012-07-31 00:53:35 +04002929 error = lookup_open(nd, path, file, op, got_write, opened);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002930 mutex_unlock(&dir->d_inode->i_mutex);
Al Viroa1e28032009-12-24 02:12:06 -05002931
Al Viro2675a4e2012-06-22 12:41:10 +04002932 if (error <= 0) {
2933 if (error)
Miklos Szeredid18e9002012-06-05 15:10:17 +02002934 goto out;
2935
Al Viro47237682012-06-10 05:01:45 -04002936 if ((*opened & FILE_CREATED) ||
Al Viro496ad9a2013-01-23 17:07:38 -05002937 !S_ISREG(file_inode(file)->i_mode))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002938 will_truncate = false;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002939
Jeff Laytonadb5c242012-10-10 16:43:13 -04002940 audit_inode(name, file->f_path.dentry, 0);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002941 goto opened;
2942 }
Al Virofb1cc552009-12-24 01:58:28 -05002943
Al Viro47237682012-06-10 05:01:45 -04002944 if (*opened & FILE_CREATED) {
Al Viro9b44f1b2011-03-09 00:17:27 -05002945 /* Don't check for write permission, don't truncate */
Al Viroca344a892011-03-09 00:36:45 -05002946 open_flag &= ~O_TRUNC;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002947 will_truncate = false;
Al Virobcda7652011-03-13 16:42:14 -04002948 acc_mode = MAY_OPEN;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002949 path_to_nameidata(path, nd);
Miklos Szeredie83db162012-06-05 15:10:29 +02002950 goto finish_open_created;
Al Virofb1cc552009-12-24 01:58:28 -05002951 }
2952
2953 /*
Jeff Layton3134f372012-07-25 10:19:47 -04002954 * create/update audit record if it already exists.
Al Virofb1cc552009-12-24 01:58:28 -05002955 */
David Howellsb18825a2013-09-12 19:22:53 +01002956 if (d_is_positive(path->dentry))
Jeff Laytonadb5c242012-10-10 16:43:13 -04002957 audit_inode(name, path->dentry, 0);
Al Virofb1cc552009-12-24 01:58:28 -05002958
Miklos Szeredid18e9002012-06-05 15:10:17 +02002959 /*
2960 * If atomic_open() acquired write access it is dropped now due to
2961 * possible mount and symlink following (this might be optimized away if
2962 * necessary...)
2963 */
Al Viro64894cf2012-07-31 00:53:35 +04002964 if (got_write) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002965 mnt_drop_write(nd->path.mnt);
Al Viro64894cf2012-07-31 00:53:35 +04002966 got_write = false;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002967 }
2968
Al Virofb1cc552009-12-24 01:58:28 -05002969 error = -EEXIST;
Al Virof8310c52012-07-30 11:50:30 +04002970 if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))
Al Virofb1cc552009-12-24 01:58:28 -05002971 goto exit_dput;
2972
David Howells9875cf82011-01-14 18:45:21 +00002973 error = follow_managed(path, nd->flags);
2974 if (error < 0)
2975 goto exit_dput;
Al Virofb1cc552009-12-24 01:58:28 -05002976
Al Viroa3fbbde2011-11-07 21:21:26 +00002977 if (error)
2978 nd->flags |= LOOKUP_JUMPED;
2979
Miklos Szeredidecf3402012-05-21 17:30:08 +02002980 BUG_ON(nd->flags & LOOKUP_RCU);
2981 inode = path->dentry->d_inode;
Miklos Szeredi5f5daac2012-05-21 17:30:14 +02002982finish_lookup:
2983 /* we _can_ be in RCU mode here */
Al Virofb1cc552009-12-24 01:58:28 -05002984 error = -ENOENT;
Al Virof0754e52014-04-19 12:30:58 -04002985 if (!inode || d_is_negative(path->dentry)) {
Miklos Szeredi54c33e72012-05-21 17:30:10 +02002986 path_to_nameidata(path, nd);
Al Viro2675a4e2012-06-22 12:41:10 +04002987 goto out;
Miklos Szeredi54c33e72012-05-21 17:30:10 +02002988 }
Al Viro9e67f362009-12-26 07:04:50 -05002989
David Howellsb18825a2013-09-12 19:22:53 +01002990 if (should_follow_link(path->dentry, !symlink_ok)) {
Miklos Szeredid45ea862012-05-21 17:30:09 +02002991 if (nd->flags & LOOKUP_RCU) {
Al Viro7507c7e2015-04-24 15:47:07 -04002992 if (unlikely(nd->path.mnt != path->mnt ||
2993 unlazy_walk(nd, path->dentry))) {
Miklos Szeredid45ea862012-05-21 17:30:09 +02002994 error = -ECHILD;
Al Viro2675a4e2012-06-22 12:41:10 +04002995 goto out;
Miklos Szeredid45ea862012-05-21 17:30:09 +02002996 }
2997 }
2998 BUG_ON(inode != path->dentry->d_inode);
Al Viro2675a4e2012-06-22 12:41:10 +04002999 return 1;
Miklos Szeredid45ea862012-05-21 17:30:09 +02003000 }
Al Virofb1cc552009-12-24 01:58:28 -05003001
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003002 if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) {
3003 path_to_nameidata(path, nd);
3004 } else {
3005 save_parent.dentry = nd->path.dentry;
3006 save_parent.mnt = mntget(path->mnt);
3007 nd->path.dentry = path->dentry;
3008
3009 }
Miklos Szeredidecf3402012-05-21 17:30:08 +02003010 nd->inode = inode;
Al Viroa3fbbde2011-11-07 21:21:26 +00003011 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
Al Virobc77daa2013-06-06 09:12:33 -04003012finish_open:
Al Viroa3fbbde2011-11-07 21:21:26 +00003013 error = complete_walk(nd);
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003014 if (error) {
3015 path_put(&save_parent);
Al Viro2675a4e2012-06-22 12:41:10 +04003016 return error;
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003017 }
Al Virobc77daa2013-06-06 09:12:33 -04003018 audit_inode(name, nd->path.dentry, 0);
Al Virofb1cc552009-12-24 01:58:28 -05003019 error = -EISDIR;
Miklos Szeredidb8bf542014-04-01 17:08:41 +02003020 if ((open_flag & O_CREAT) && d_is_dir(nd->path.dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04003021 goto out;
Miklos Szerediaf2f5542012-05-21 17:30:11 +02003022 error = -ENOTDIR;
Miklos Szeredidb8bf542014-04-01 17:08:41 +02003023 if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04003024 goto out;
Al Viro6c0d46c2011-03-09 00:59:59 -05003025 if (!S_ISREG(nd->inode->i_mode))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003026 will_truncate = false;
Al Viro6c0d46c2011-03-09 00:59:59 -05003027
Al Viro0f9d1a12011-03-09 00:13:14 -05003028 if (will_truncate) {
3029 error = mnt_want_write(nd->path.mnt);
3030 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003031 goto out;
Al Viro64894cf2012-07-31 00:53:35 +04003032 got_write = true;
Al Viro0f9d1a12011-03-09 00:13:14 -05003033 }
Miklos Szeredie83db162012-06-05 15:10:29 +02003034finish_open_created:
Al Virobcda7652011-03-13 16:42:14 -04003035 error = may_open(&nd->path, acc_mode, open_flag);
Al Viroca344a892011-03-09 00:36:45 -05003036 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003037 goto out;
Miklos Szeredi71732032014-10-24 00:14:35 +02003038
3039 BUG_ON(*opened & FILE_OPENED); /* once it's opened, it's opened */
3040 error = vfs_open(&nd->path, file, current_cred());
3041 if (!error) {
3042 *opened |= FILE_OPENED;
3043 } else {
Al Viro30d90492012-06-22 12:40:19 +04003044 if (error == -EOPENSTALE)
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003045 goto stale_open;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003046 goto out;
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003047 }
Miklos Szeredia8277b92012-06-05 15:10:32 +02003048opened:
Al Viro2675a4e2012-06-22 12:41:10 +04003049 error = open_check_o_direct(file);
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003050 if (error)
3051 goto exit_fput;
Al Viro2675a4e2012-06-22 12:41:10 +04003052 error = ima_file_check(file, op->acc_mode);
Miklos Szerediaa4caad2012-06-05 15:10:28 +02003053 if (error)
3054 goto exit_fput;
3055
3056 if (will_truncate) {
Al Viro2675a4e2012-06-22 12:41:10 +04003057 error = handle_truncate(file);
Miklos Szerediaa4caad2012-06-05 15:10:28 +02003058 if (error)
3059 goto exit_fput;
Al Viro0f9d1a12011-03-09 00:13:14 -05003060 }
Al Viroca344a892011-03-09 00:36:45 -05003061out:
Al Viro64894cf2012-07-31 00:53:35 +04003062 if (got_write)
Al Viro0f9d1a12011-03-09 00:13:14 -05003063 mnt_drop_write(nd->path.mnt);
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003064 path_put(&save_parent);
Miklos Szeredie276ae62012-05-21 17:30:06 +02003065 terminate_walk(nd);
Al Viro2675a4e2012-06-22 12:41:10 +04003066 return error;
Al Virofb1cc552009-12-24 01:58:28 -05003067
Al Virofb1cc552009-12-24 01:58:28 -05003068exit_dput:
3069 path_put_conditional(path, nd);
Al Viroca344a892011-03-09 00:36:45 -05003070 goto out;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003071exit_fput:
Al Viro2675a4e2012-06-22 12:41:10 +04003072 fput(file);
3073 goto out;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003074
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003075stale_open:
3076 /* If no saved parent or already retried then can't retry */
3077 if (!save_parent.dentry || retried)
3078 goto out;
3079
3080 BUG_ON(save_parent.dentry != dir);
3081 path_put(&nd->path);
3082 nd->path = save_parent;
3083 nd->inode = dir->d_inode;
3084 save_parent.mnt = NULL;
3085 save_parent.dentry = NULL;
Al Viro64894cf2012-07-31 00:53:35 +04003086 if (got_write) {
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003087 mnt_drop_write(nd->path.mnt);
Al Viro64894cf2012-07-31 00:53:35 +04003088 got_write = false;
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003089 }
3090 retried = true;
3091 goto retry_lookup;
Al Virofb1cc552009-12-24 01:58:28 -05003092}
3093
Al Viro60545d02013-06-07 01:20:27 -04003094static int do_tmpfile(int dfd, struct filename *pathname,
3095 struct nameidata *nd, int flags,
3096 const struct open_flags *op,
3097 struct file *file, int *opened)
3098{
3099 static const struct qstr name = QSTR_INIT("/", 1);
3100 struct dentry *dentry, *child;
3101 struct inode *dir;
3102 int error = path_lookupat(dfd, pathname->name,
3103 flags | LOOKUP_DIRECTORY, nd);
3104 if (unlikely(error))
3105 return error;
3106 error = mnt_want_write(nd->path.mnt);
3107 if (unlikely(error))
3108 goto out;
3109 /* we want directory to be writable */
3110 error = inode_permission(nd->inode, MAY_WRITE | MAY_EXEC);
3111 if (error)
3112 goto out2;
3113 dentry = nd->path.dentry;
3114 dir = dentry->d_inode;
3115 if (!dir->i_op->tmpfile) {
3116 error = -EOPNOTSUPP;
3117 goto out2;
3118 }
3119 child = d_alloc(dentry, &name);
3120 if (unlikely(!child)) {
3121 error = -ENOMEM;
3122 goto out2;
3123 }
3124 nd->flags &= ~LOOKUP_DIRECTORY;
3125 nd->flags |= op->intent;
3126 dput(nd->path.dentry);
3127 nd->path.dentry = child;
3128 error = dir->i_op->tmpfile(dir, nd->path.dentry, op->mode);
3129 if (error)
3130 goto out2;
3131 audit_inode(pathname, nd->path.dentry, 0);
Eric Rannaud74df6af2014-10-30 01:51:01 -07003132 /* Don't check for other permissions, the inode was just created */
3133 error = may_open(&nd->path, MAY_OPEN, op->open_flag);
Al Viro60545d02013-06-07 01:20:27 -04003134 if (error)
3135 goto out2;
3136 file->f_path.mnt = nd->path.mnt;
3137 error = finish_open(file, nd->path.dentry, NULL, opened);
3138 if (error)
3139 goto out2;
3140 error = open_check_o_direct(file);
Al Virof4e0c302013-06-11 08:34:36 +04003141 if (error) {
Al Viro60545d02013-06-07 01:20:27 -04003142 fput(file);
Al Virof4e0c302013-06-11 08:34:36 +04003143 } else if (!(op->open_flag & O_EXCL)) {
3144 struct inode *inode = file_inode(file);
3145 spin_lock(&inode->i_lock);
3146 inode->i_state |= I_LINKABLE;
3147 spin_unlock(&inode->i_lock);
3148 }
Al Viro60545d02013-06-07 01:20:27 -04003149out2:
3150 mnt_drop_write(nd->path.mnt);
3151out:
3152 path_put(&nd->path);
3153 return error;
3154}
3155
Jeff Layton669abf42012-10-10 16:43:10 -04003156static struct file *path_openat(int dfd, struct filename *pathname,
Al Viro73d049a2011-03-11 12:08:24 -05003157 struct nameidata *nd, const struct open_flags *op, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158{
Al Virofe2d35f2011-03-05 22:58:25 -05003159 struct file *base = NULL;
Al Viro30d90492012-06-22 12:40:19 +04003160 struct file *file;
Al Viro9850c052010-01-13 15:01:15 -05003161 struct path path;
Al Viro47237682012-06-10 05:01:45 -04003162 int opened = 0;
Al Viro13aab422011-02-23 17:54:08 -05003163 int error;
Nick Piggin31e6b012011-01-07 17:49:52 +11003164
Al Viro30d90492012-06-22 12:40:19 +04003165 file = get_empty_filp();
Al Viro1afc99b2013-02-14 20:41:04 -05003166 if (IS_ERR(file))
3167 return file;
Nick Piggin31e6b012011-01-07 17:49:52 +11003168
Al Viro30d90492012-06-22 12:40:19 +04003169 file->f_flags = op->open_flag;
Nick Piggin31e6b012011-01-07 17:49:52 +11003170
Al Virobb458c62013-07-13 13:26:37 +04003171 if (unlikely(file->f_flags & __O_TMPFILE)) {
Al Viro60545d02013-06-07 01:20:27 -04003172 error = do_tmpfile(dfd, pathname, nd, flags, op, file, &opened);
3173 goto out;
3174 }
3175
Jeff Layton669abf42012-10-10 16:43:10 -04003176 error = path_init(dfd, pathname->name, flags | LOOKUP_PARENT, nd, &base);
Nick Piggin31e6b012011-01-07 17:49:52 +11003177 if (unlikely(error))
Al Viro2675a4e2012-06-22 12:41:10 +04003178 goto out;
Nick Piggin31e6b012011-01-07 17:49:52 +11003179
Al Virofe2d35f2011-03-05 22:58:25 -05003180 current->total_link_count = 0;
Jeff Layton669abf42012-10-10 16:43:10 -04003181 error = link_path_walk(pathname->name, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11003182 if (unlikely(error))
Al Viro2675a4e2012-06-22 12:41:10 +04003183 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184
Al Viro2675a4e2012-06-22 12:41:10 +04003185 error = do_last(nd, &path, file, op, &opened, pathname);
3186 while (unlikely(error > 0)) { /* trailing symlink */
Nick Piggin7b9337a2011-01-14 08:42:43 +00003187 struct path link = path;
Al Virodef4af32009-12-26 08:37:05 -05003188 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04003189 if (!(nd->flags & LOOKUP_FOLLOW)) {
Al Viro73d049a2011-03-11 12:08:24 -05003190 path_put_conditional(&path, nd);
3191 path_put(&nd->path);
Al Viro2675a4e2012-06-22 12:41:10 +04003192 error = -ELOOP;
Al Viro40b39132011-03-09 16:22:18 -05003193 break;
3194 }
Kees Cook800179c2012-07-25 17:29:07 -07003195 error = may_follow_link(&link, nd);
3196 if (unlikely(error))
3197 break;
Al Viro73d049a2011-03-11 12:08:24 -05003198 nd->flags |= LOOKUP_PARENT;
3199 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
Al Viro574197e2011-03-14 22:20:34 -04003200 error = follow_link(&link, nd, &cookie);
Al Viroc3e380b2011-02-23 13:39:45 -05003201 if (unlikely(error))
Al Viro2675a4e2012-06-22 12:41:10 +04003202 break;
3203 error = do_last(nd, &path, file, op, &opened, pathname);
Al Viro574197e2011-03-14 22:20:34 -04003204 put_link(nd, &link, cookie);
Al Viro806b6812009-12-26 07:16:40 -05003205 }
Al Viro10fa8e62009-12-26 07:09:49 -05003206out:
Al Viro73d049a2011-03-11 12:08:24 -05003207 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
3208 path_put(&nd->root);
Al Virofe2d35f2011-03-05 22:58:25 -05003209 if (base)
3210 fput(base);
Al Viro2675a4e2012-06-22 12:41:10 +04003211 if (!(opened & FILE_OPENED)) {
3212 BUG_ON(!error);
Al Viro30d90492012-06-22 12:40:19 +04003213 put_filp(file);
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003214 }
Al Viro2675a4e2012-06-22 12:41:10 +04003215 if (unlikely(error)) {
3216 if (error == -EOPENSTALE) {
3217 if (flags & LOOKUP_RCU)
3218 error = -ECHILD;
3219 else
3220 error = -ESTALE;
3221 }
3222 file = ERR_PTR(error);
3223 }
3224 return file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225}
3226
Jeff Layton669abf42012-10-10 16:43:10 -04003227struct file *do_filp_open(int dfd, struct filename *pathname,
Al Virof9652e12013-06-11 08:23:01 +04003228 const struct open_flags *op)
Al Viro13aab422011-02-23 17:54:08 -05003229{
Al Viro73d049a2011-03-11 12:08:24 -05003230 struct nameidata nd;
Al Virof9652e12013-06-11 08:23:01 +04003231 int flags = op->lookup_flags;
Al Viro13aab422011-02-23 17:54:08 -05003232 struct file *filp;
3233
Al Viro73d049a2011-03-11 12:08:24 -05003234 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
Al Viro13aab422011-02-23 17:54:08 -05003235 if (unlikely(filp == ERR_PTR(-ECHILD)))
Al Viro73d049a2011-03-11 12:08:24 -05003236 filp = path_openat(dfd, pathname, &nd, op, flags);
Al Viro13aab422011-02-23 17:54:08 -05003237 if (unlikely(filp == ERR_PTR(-ESTALE)))
Al Viro73d049a2011-03-11 12:08:24 -05003238 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
Al Viro13aab422011-02-23 17:54:08 -05003239 return filp;
3240}
3241
Al Viro73d049a2011-03-11 12:08:24 -05003242struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
Al Virof9652e12013-06-11 08:23:01 +04003243 const char *name, const struct open_flags *op)
Al Viro73d049a2011-03-11 12:08:24 -05003244{
3245 struct nameidata nd;
3246 struct file *file;
Jeff Layton669abf42012-10-10 16:43:10 -04003247 struct filename filename = { .name = name };
Al Virof9652e12013-06-11 08:23:01 +04003248 int flags = op->lookup_flags | LOOKUP_ROOT;
Al Viro73d049a2011-03-11 12:08:24 -05003249
3250 nd.root.mnt = mnt;
3251 nd.root.dentry = dentry;
3252
David Howellsb18825a2013-09-12 19:22:53 +01003253 if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN)
Al Viro73d049a2011-03-11 12:08:24 -05003254 return ERR_PTR(-ELOOP);
3255
Jeff Layton669abf42012-10-10 16:43:10 -04003256 file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_RCU);
Al Viro73d049a2011-03-11 12:08:24 -05003257 if (unlikely(file == ERR_PTR(-ECHILD)))
Jeff Layton669abf42012-10-10 16:43:10 -04003258 file = path_openat(-1, &filename, &nd, op, flags);
Al Viro73d049a2011-03-11 12:08:24 -05003259 if (unlikely(file == ERR_PTR(-ESTALE)))
Jeff Layton669abf42012-10-10 16:43:10 -04003260 file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_REVAL);
Al Viro73d049a2011-03-11 12:08:24 -05003261 return file;
3262}
3263
Jeff Layton1ac12b42012-12-11 12:10:06 -05003264struct dentry *kern_path_create(int dfd, const char *pathname,
3265 struct path *path, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003267 struct dentry *dentry = ERR_PTR(-EEXIST);
Al Viroed75e952011-06-27 16:53:43 -04003268 struct nameidata nd;
Jan Karac30dabf2012-06-12 16:20:30 +02003269 int err2;
Jeff Layton1ac12b42012-12-11 12:10:06 -05003270 int error;
3271 bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
3272
3273 /*
3274 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
3275 * other flags passed in are ignored!
3276 */
3277 lookup_flags &= LOOKUP_REVAL;
3278
3279 error = do_path_lookup(dfd, pathname, LOOKUP_PARENT|lookup_flags, &nd);
Al Viroed75e952011-06-27 16:53:43 -04003280 if (error)
3281 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003283 /*
3284 * Yucky last component or no last component at all?
3285 * (foo/., foo/.., /////)
3286 */
Al Viroed75e952011-06-27 16:53:43 -04003287 if (nd.last_type != LAST_NORM)
3288 goto out;
3289 nd.flags &= ~LOOKUP_PARENT;
3290 nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003291
Jan Karac30dabf2012-06-12 16:20:30 +02003292 /* don't fail immediately if it's r/o, at least try to report other errors */
3293 err2 = mnt_want_write(nd.path.mnt);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003294 /*
3295 * Do the final lookup.
3296 */
Al Viroed75e952011-06-27 16:53:43 -04003297 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3298 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 if (IS_ERR(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003300 goto unlock;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003301
Al Viroa8104a92012-07-20 02:25:00 +04003302 error = -EEXIST;
David Howellsb18825a2013-09-12 19:22:53 +01003303 if (d_is_positive(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003304 goto fail;
David Howellsb18825a2013-09-12 19:22:53 +01003305
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003306 /*
3307 * Special case - lookup gave negative, but... we had foo/bar/
3308 * From the vfs_mknod() POV we just have a negative dentry -
3309 * all is fine. Let's be bastards - you had / on the end, you've
3310 * been asking for (non-existent) directory. -ENOENT for you.
3311 */
Al Viroed75e952011-06-27 16:53:43 -04003312 if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
Al Viroa8104a92012-07-20 02:25:00 +04003313 error = -ENOENT;
Al Viroed75e952011-06-27 16:53:43 -04003314 goto fail;
Al Viroe9baf6e2008-05-15 04:49:12 -04003315 }
Jan Karac30dabf2012-06-12 16:20:30 +02003316 if (unlikely(err2)) {
3317 error = err2;
Al Viroa8104a92012-07-20 02:25:00 +04003318 goto fail;
Jan Karac30dabf2012-06-12 16:20:30 +02003319 }
Al Viroed75e952011-06-27 16:53:43 -04003320 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322fail:
Al Viroa8104a92012-07-20 02:25:00 +04003323 dput(dentry);
3324 dentry = ERR_PTR(error);
3325unlock:
Al Viroed75e952011-06-27 16:53:43 -04003326 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Karac30dabf2012-06-12 16:20:30 +02003327 if (!err2)
3328 mnt_drop_write(nd.path.mnt);
Al Viroed75e952011-06-27 16:53:43 -04003329out:
3330 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 return dentry;
3332}
Al Virodae6ad82011-06-26 11:50:15 -04003333EXPORT_SYMBOL(kern_path_create);
3334
Al Viro921a1652012-07-20 01:15:31 +04003335void done_path_create(struct path *path, struct dentry *dentry)
3336{
3337 dput(dentry);
3338 mutex_unlock(&path->dentry->d_inode->i_mutex);
Al Viroa8104a92012-07-20 02:25:00 +04003339 mnt_drop_write(path->mnt);
Al Viro921a1652012-07-20 01:15:31 +04003340 path_put(path);
3341}
3342EXPORT_SYMBOL(done_path_create);
3343
Jeff Layton1ac12b42012-12-11 12:10:06 -05003344struct dentry *user_path_create(int dfd, const char __user *pathname,
3345 struct path *path, unsigned int lookup_flags)
Al Virodae6ad82011-06-26 11:50:15 -04003346{
Jeff Layton91a27b22012-10-10 15:25:28 -04003347 struct filename *tmp = getname(pathname);
Al Virodae6ad82011-06-26 11:50:15 -04003348 struct dentry *res;
3349 if (IS_ERR(tmp))
3350 return ERR_CAST(tmp);
Jeff Layton1ac12b42012-12-11 12:10:06 -05003351 res = kern_path_create(dfd, tmp->name, path, lookup_flags);
Al Virodae6ad82011-06-26 11:50:15 -04003352 putname(tmp);
3353 return res;
3354}
3355EXPORT_SYMBOL(user_path_create);
3356
Al Viro1a67aaf2011-07-26 01:52:52 -04003357int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003359 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360
3361 if (error)
3362 return error;
3363
Eric W. Biederman975d6b32011-11-13 12:16:43 -08003364 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 return -EPERM;
3366
Al Viroacfa4382008-12-04 10:06:33 -05003367 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 return -EPERM;
3369
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07003370 error = devcgroup_inode_mknod(mode, dev);
3371 if (error)
3372 return error;
3373
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 error = security_inode_mknod(dir, dentry, mode, dev);
3375 if (error)
3376 return error;
3377
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003379 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003380 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 return error;
3382}
3383
Al Virof69aac02011-07-26 04:31:40 -04003384static int may_mknod(umode_t mode)
Dave Hansen463c3192008-02-15 14:37:57 -08003385{
3386 switch (mode & S_IFMT) {
3387 case S_IFREG:
3388 case S_IFCHR:
3389 case S_IFBLK:
3390 case S_IFIFO:
3391 case S_IFSOCK:
3392 case 0: /* zero mode translates to S_IFREG */
3393 return 0;
3394 case S_IFDIR:
3395 return -EPERM;
3396 default:
3397 return -EINVAL;
3398 }
3399}
3400
Al Viro8208a222011-07-25 17:32:17 -04003401SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003402 unsigned, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403{
Al Viro2ad94ae2008-07-21 09:32:51 -04003404 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003405 struct path path;
3406 int error;
Jeff Layton972567f2012-12-20 16:00:10 -05003407 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408
Al Viro8e4bfca2012-07-20 01:17:26 +04003409 error = may_mknod(mode);
3410 if (error)
3411 return error;
Jeff Layton972567f2012-12-20 16:00:10 -05003412retry:
3413 dentry = user_path_create(dfd, filename, &path, lookup_flags);
Al Virodae6ad82011-06-26 11:50:15 -04003414 if (IS_ERR(dentry))
3415 return PTR_ERR(dentry);
Al Viro2ad94ae2008-07-21 09:32:51 -04003416
Al Virodae6ad82011-06-26 11:50:15 -04003417 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003418 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003419 error = security_path_mknod(&path, dentry, mode, dev);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003420 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04003421 goto out;
Dave Hansen463c3192008-02-15 14:37:57 -08003422 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 case 0: case S_IFREG:
Al Viro312b63f2012-06-10 18:09:36 -04003424 error = vfs_create(path.dentry->d_inode,dentry,mode,true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 break;
3426 case S_IFCHR: case S_IFBLK:
Al Virodae6ad82011-06-26 11:50:15 -04003427 error = vfs_mknod(path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 new_decode_dev(dev));
3429 break;
3430 case S_IFIFO: case S_IFSOCK:
Al Virodae6ad82011-06-26 11:50:15 -04003431 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 }
Al Viroa8104a92012-07-20 02:25:00 +04003434out:
Al Viro921a1652012-07-20 01:15:31 +04003435 done_path_create(&path, dentry);
Jeff Layton972567f2012-12-20 16:00:10 -05003436 if (retry_estale(error, lookup_flags)) {
3437 lookup_flags |= LOOKUP_REVAL;
3438 goto retry;
3439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 return error;
3441}
3442
Al Viro8208a222011-07-25 17:32:17 -04003443SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003444{
3445 return sys_mknodat(AT_FDCWD, filename, mode, dev);
3446}
3447
Al Viro18bb1db2011-07-26 01:41:39 -04003448int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003450 int error = may_create(dir, dentry);
Al Viro8de52772012-02-06 12:45:27 -05003451 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452
3453 if (error)
3454 return error;
3455
Al Viroacfa4382008-12-04 10:06:33 -05003456 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 return -EPERM;
3458
3459 mode &= (S_IRWXUGO|S_ISVTX);
3460 error = security_inode_mkdir(dir, dentry, mode);
3461 if (error)
3462 return error;
3463
Al Viro8de52772012-02-06 12:45:27 -05003464 if (max_links && dir->i_nlink >= max_links)
3465 return -EMLINK;
3466
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003468 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003469 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 return error;
3471}
3472
Al Viroa218d0f2011-11-21 14:59:34 -05003473SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474{
Dave Hansen6902d922006-09-30 23:29:01 -07003475 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003476 struct path path;
3477 int error;
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003478 unsigned int lookup_flags = LOOKUP_DIRECTORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003480retry:
3481 dentry = user_path_create(dfd, pathname, &path, lookup_flags);
Dave Hansen6902d922006-09-30 23:29:01 -07003482 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003483 return PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003484
Al Virodae6ad82011-06-26 11:50:15 -04003485 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003486 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003487 error = security_path_mkdir(&path, dentry, mode);
Al Viroa8104a92012-07-20 02:25:00 +04003488 if (!error)
3489 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
Al Viro921a1652012-07-20 01:15:31 +04003490 done_path_create(&path, dentry);
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003491 if (retry_estale(error, lookup_flags)) {
3492 lookup_flags |= LOOKUP_REVAL;
3493 goto retry;
3494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 return error;
3496}
3497
Al Viroa218d0f2011-11-21 14:59:34 -05003498SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003499{
3500 return sys_mkdirat(AT_FDCWD, pathname, mode);
3501}
3502
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503/*
Sage Weila71905f2011-05-24 13:06:08 -07003504 * The dentry_unhash() helper will try to drop the dentry early: we
J. Bruce Fieldsc0d02592012-02-15 11:48:40 -05003505 * should have a usage count of 1 if we're the only user of this
Sage Weila71905f2011-05-24 13:06:08 -07003506 * dentry, and if that is true (possibly after pruning the dcache),
3507 * then we drop the dentry now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 *
3509 * A low-level filesystem can, if it choses, legally
3510 * do a
3511 *
3512 * if (!d_unhashed(dentry))
3513 * return -EBUSY;
3514 *
3515 * if it cannot handle the case of removing a directory
3516 * that is still in use by something else..
3517 */
3518void dentry_unhash(struct dentry *dentry)
3519{
Vasily Averindc168422006-12-06 20:37:07 -08003520 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 spin_lock(&dentry->d_lock);
Waiman Long98474232013-08-28 18:24:59 -07003522 if (dentry->d_lockref.count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 __d_drop(dentry);
3524 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525}
3526
3527int vfs_rmdir(struct inode *dir, struct dentry *dentry)
3528{
3529 int error = may_delete(dir, dentry, 1);
3530
3531 if (error)
3532 return error;
3533
Al Viroacfa4382008-12-04 10:06:33 -05003534 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 return -EPERM;
3536
Al Viro1d2ef592011-09-14 18:55:41 +01003537 dget(dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003538 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539
Sage Weil912dbc12011-05-24 13:06:11 -07003540 error = -EBUSY;
3541 if (d_mountpoint(dentry))
3542 goto out;
3543
3544 error = security_inode_rmdir(dir, dentry);
3545 if (error)
3546 goto out;
3547
Sage Weil3cebde22011-05-29 21:20:59 -07003548 shrink_dcache_parent(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003549 error = dir->i_op->rmdir(dir, dentry);
3550 if (error)
3551 goto out;
3552
3553 dentry->d_inode->i_flags |= S_DEAD;
3554 dont_mount(dentry);
3555
3556out:
3557 mutex_unlock(&dentry->d_inode->i_mutex);
Al Viro1d2ef592011-09-14 18:55:41 +01003558 dput(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003559 if (!error)
3560 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 return error;
3562}
3563
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003564static long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565{
3566 int error = 0;
Jeff Layton91a27b22012-10-10 15:25:28 -04003567 struct filename *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 struct dentry *dentry;
3569 struct nameidata nd;
Jeff Laytonc6ee9202012-12-20 16:28:33 -05003570 unsigned int lookup_flags = 0;
3571retry:
3572 name = user_path_parent(dfd, pathname, &nd, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04003573 if (IS_ERR(name))
3574 return PTR_ERR(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575
3576 switch(nd.last_type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003577 case LAST_DOTDOT:
3578 error = -ENOTEMPTY;
3579 goto exit1;
3580 case LAST_DOT:
3581 error = -EINVAL;
3582 goto exit1;
3583 case LAST_ROOT:
3584 error = -EBUSY;
3585 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003587
3588 nd.flags &= ~LOOKUP_PARENT;
Jan Karac30dabf2012-06-12 16:20:30 +02003589 error = mnt_want_write(nd.path.mnt);
3590 if (error)
3591 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003592
Jan Blunck4ac91372008-02-14 19:34:32 -08003593 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08003594 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003596 if (IS_ERR(dentry))
3597 goto exit2;
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04003598 if (!dentry->d_inode) {
3599 error = -ENOENT;
3600 goto exit3;
3601 }
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003602 error = security_path_rmdir(&nd.path, dentry);
3603 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02003604 goto exit3;
Jan Blunck4ac91372008-02-14 19:34:32 -08003605 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
Dave Hansen06227532008-02-15 14:37:34 -08003606exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07003607 dput(dentry);
3608exit2:
Jan Blunck4ac91372008-02-14 19:34:32 -08003609 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Karac30dabf2012-06-12 16:20:30 +02003610 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003612 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 putname(name);
Jeff Laytonc6ee9202012-12-20 16:28:33 -05003614 if (retry_estale(error, lookup_flags)) {
3615 lookup_flags |= LOOKUP_REVAL;
3616 goto retry;
3617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 return error;
3619}
3620
Heiko Carstens3cdad422009-01-14 14:14:22 +01003621SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003622{
3623 return do_rmdir(AT_FDCWD, pathname);
3624}
3625
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003626/**
3627 * vfs_unlink - unlink a filesystem object
3628 * @dir: parent directory
3629 * @dentry: victim
3630 * @delegated_inode: returns victim inode, if the inode is delegated.
3631 *
3632 * The caller must hold dir->i_mutex.
3633 *
3634 * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
3635 * return a reference to the inode in delegated_inode. The caller
3636 * should then break the delegation on that inode and retry. Because
3637 * breaking a delegation may take a long time, the caller should drop
3638 * dir->i_mutex before doing so.
3639 *
3640 * Alternatively, a caller may pass NULL for delegated_inode. This may
3641 * be appropriate for callers that expect the underlying filesystem not
3642 * to be NFS exported.
3643 */
3644int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645{
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003646 struct inode *target = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 int error = may_delete(dir, dentry, 0);
3648
3649 if (error)
3650 return error;
3651
Al Viroacfa4382008-12-04 10:06:33 -05003652 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 return -EPERM;
3654
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003655 mutex_lock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 if (d_mountpoint(dentry))
3657 error = -EBUSY;
3658 else {
3659 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05003660 if (!error) {
J. Bruce Fields5a146962012-08-28 07:50:40 -07003661 error = try_break_deleg(target, delegated_inode);
3662 if (error)
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003663 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 error = dir->i_op->unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05003665 if (!error)
Al Virod83c49f2010-04-30 17:17:09 -04003666 dont_mount(dentry);
Al Virobec10522010-03-03 14:12:08 -05003667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 }
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003669out:
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003670 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671
3672 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
3673 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003674 fsnotify_link_count(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 }
Robert Love0eeca282005-07-12 17:06:03 -04003677
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 return error;
3679}
3680
3681/*
3682 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003683 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 * writeout happening, and we don't want to prevent access to the directory
3685 * while waiting on the I/O.
3686 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003687static long do_unlinkat(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688{
Al Viro2ad94ae2008-07-21 09:32:51 -04003689 int error;
Jeff Layton91a27b22012-10-10 15:25:28 -04003690 struct filename *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 struct dentry *dentry;
3692 struct nameidata nd;
3693 struct inode *inode = NULL;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003694 struct inode *delegated_inode = NULL;
Jeff Layton5d18f812012-12-20 16:38:04 -05003695 unsigned int lookup_flags = 0;
3696retry:
3697 name = user_path_parent(dfd, pathname, &nd, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04003698 if (IS_ERR(name))
3699 return PTR_ERR(name);
Al Viro2ad94ae2008-07-21 09:32:51 -04003700
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 error = -EISDIR;
3702 if (nd.last_type != LAST_NORM)
3703 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003704
3705 nd.flags &= ~LOOKUP_PARENT;
Jan Karac30dabf2012-06-12 16:20:30 +02003706 error = mnt_want_write(nd.path.mnt);
3707 if (error)
3708 goto exit1;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003709retry_deleg:
Jan Blunck4ac91372008-02-14 19:34:32 -08003710 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08003711 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 error = PTR_ERR(dentry);
3713 if (!IS_ERR(dentry)) {
3714 /* Why not before? Because we want correct error value */
Török Edwin50338b82011-06-16 00:06:14 +03003715 if (nd.last.name[nd.last.len])
3716 goto slashes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 inode = dentry->d_inode;
David Howellsb18825a2013-09-12 19:22:53 +01003718 if (d_is_negative(dentry))
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04003719 goto slashes;
3720 ihold(inode);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003721 error = security_path_unlink(&nd.path, dentry);
3722 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02003723 goto exit2;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003724 error = vfs_unlink(nd.path.dentry->d_inode, dentry, &delegated_inode);
Jan Karac30dabf2012-06-12 16:20:30 +02003725exit2:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 dput(dentry);
3727 }
Jan Blunck4ac91372008-02-14 19:34:32 -08003728 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 if (inode)
3730 iput(inode); /* truncate the inode here */
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003731 inode = NULL;
3732 if (delegated_inode) {
J. Bruce Fields5a146962012-08-28 07:50:40 -07003733 error = break_deleg_wait(&delegated_inode);
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003734 if (!error)
3735 goto retry_deleg;
3736 }
Jan Karac30dabf2012-06-12 16:20:30 +02003737 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003739 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 putname(name);
Jeff Layton5d18f812012-12-20 16:38:04 -05003741 if (retry_estale(error, lookup_flags)) {
3742 lookup_flags |= LOOKUP_REVAL;
3743 inode = NULL;
3744 goto retry;
3745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 return error;
3747
3748slashes:
David Howellsb18825a2013-09-12 19:22:53 +01003749 if (d_is_negative(dentry))
3750 error = -ENOENT;
Miklos Szeredidb8bf542014-04-01 17:08:41 +02003751 else if (d_is_dir(dentry))
David Howellsb18825a2013-09-12 19:22:53 +01003752 error = -EISDIR;
3753 else
3754 error = -ENOTDIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 goto exit2;
3756}
3757
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003758SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003759{
3760 if ((flag & ~AT_REMOVEDIR) != 0)
3761 return -EINVAL;
3762
3763 if (flag & AT_REMOVEDIR)
3764 return do_rmdir(dfd, pathname);
3765
3766 return do_unlinkat(dfd, pathname);
3767}
3768
Heiko Carstens3480b252009-01-14 14:14:16 +01003769SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003770{
3771 return do_unlinkat(AT_FDCWD, pathname);
3772}
3773
Miklos Szeredidb2e7472008-06-24 16:50:16 +02003774int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003776 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777
3778 if (error)
3779 return error;
3780
Al Viroacfa4382008-12-04 10:06:33 -05003781 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 return -EPERM;
3783
3784 error = security_inode_symlink(dir, dentry, oldname);
3785 if (error)
3786 return error;
3787
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003789 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003790 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 return error;
3792}
3793
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003794SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
3795 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796{
Al Viro2ad94ae2008-07-21 09:32:51 -04003797 int error;
Jeff Layton91a27b22012-10-10 15:25:28 -04003798 struct filename *from;
Dave Hansen6902d922006-09-30 23:29:01 -07003799 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003800 struct path path;
Jeff Laytonf46d3562012-12-11 12:10:08 -05003801 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802
3803 from = getname(oldname);
Al Viro2ad94ae2008-07-21 09:32:51 -04003804 if (IS_ERR(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 return PTR_ERR(from);
Jeff Laytonf46d3562012-12-11 12:10:08 -05003806retry:
3807 dentry = user_path_create(newdfd, newname, &path, lookup_flags);
Dave Hansen6902d922006-09-30 23:29:01 -07003808 error = PTR_ERR(dentry);
3809 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003810 goto out_putname;
Dave Hansen6902d922006-09-30 23:29:01 -07003811
Jeff Layton91a27b22012-10-10 15:25:28 -04003812 error = security_path_symlink(&path, dentry, from->name);
Al Viroa8104a92012-07-20 02:25:00 +04003813 if (!error)
Jeff Layton91a27b22012-10-10 15:25:28 -04003814 error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
Al Viro921a1652012-07-20 01:15:31 +04003815 done_path_create(&path, dentry);
Jeff Laytonf46d3562012-12-11 12:10:08 -05003816 if (retry_estale(error, lookup_flags)) {
3817 lookup_flags |= LOOKUP_REVAL;
3818 goto retry;
3819 }
Dave Hansen6902d922006-09-30 23:29:01 -07003820out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 putname(from);
3822 return error;
3823}
3824
Heiko Carstens3480b252009-01-14 14:14:16 +01003825SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003826{
3827 return sys_symlinkat(oldname, AT_FDCWD, newname);
3828}
3829
J. Bruce Fields146a8592011-09-20 17:14:31 -04003830/**
3831 * vfs_link - create a new link
3832 * @old_dentry: object to be linked
3833 * @dir: new parent
3834 * @new_dentry: where to create the new link
3835 * @delegated_inode: returns inode needing a delegation break
3836 *
3837 * The caller must hold dir->i_mutex
3838 *
3839 * If vfs_link discovers a delegation on the to-be-linked file in need
3840 * of breaking, it will return -EWOULDBLOCK and return a reference to the
3841 * inode in delegated_inode. The caller should then break the delegation
3842 * and retry. Because breaking a delegation may take a long time, the
3843 * caller should drop the i_mutex before doing so.
3844 *
3845 * Alternatively, a caller may pass NULL for delegated_inode. This may
3846 * be appropriate for callers that expect the underlying filesystem not
3847 * to be NFS exported.
3848 */
3849int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850{
3851 struct inode *inode = old_dentry->d_inode;
Al Viro8de52772012-02-06 12:45:27 -05003852 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 int error;
3854
3855 if (!inode)
3856 return -ENOENT;
3857
Miklos Szeredia95164d2008-07-30 15:08:48 +02003858 error = may_create(dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859 if (error)
3860 return error;
3861
3862 if (dir->i_sb != inode->i_sb)
3863 return -EXDEV;
3864
3865 /*
3866 * A link to an append-only or immutable file cannot be created.
3867 */
3868 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3869 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05003870 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003872 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 return -EPERM;
3874
3875 error = security_inode_link(old_dentry, dir, new_dentry);
3876 if (error)
3877 return error;
3878
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003879 mutex_lock(&inode->i_mutex);
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05303880 /* Make sure we don't allow creating hardlink to an unlinked file */
Al Virof4e0c302013-06-11 08:34:36 +04003881 if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05303882 error = -ENOENT;
Al Viro8de52772012-02-06 12:45:27 -05003883 else if (max_links && inode->i_nlink >= max_links)
3884 error = -EMLINK;
J. Bruce Fields146a8592011-09-20 17:14:31 -04003885 else {
3886 error = try_break_deleg(inode, delegated_inode);
3887 if (!error)
3888 error = dir->i_op->link(old_dentry, dir, new_dentry);
3889 }
Al Virof4e0c302013-06-11 08:34:36 +04003890
3891 if (!error && (inode->i_state & I_LINKABLE)) {
3892 spin_lock(&inode->i_lock);
3893 inode->i_state &= ~I_LINKABLE;
3894 spin_unlock(&inode->i_lock);
3895 }
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003896 mutex_unlock(&inode->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07003897 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003898 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 return error;
3900}
3901
3902/*
3903 * Hardlinks are often used in delicate situations. We avoid
3904 * security-related surprises by not following symlinks on the
3905 * newname. --KAB
3906 *
3907 * We don't follow them on the oldname either to be compatible
3908 * with linux 2.0, and to avoid hard-linking to directories
3909 * and other special files. --ADM
3910 */
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003911SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
3912 int, newdfd, const char __user *, newname, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913{
3914 struct dentry *new_dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003915 struct path old_path, new_path;
J. Bruce Fields146a8592011-09-20 17:14:31 -04003916 struct inode *delegated_inode = NULL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303917 int how = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303920 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003921 return -EINVAL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303922 /*
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07003923 * To use null names we require CAP_DAC_READ_SEARCH
3924 * This ensures that not everyone will be able to create
3925 * handlink using the passed filedescriptor.
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303926 */
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07003927 if (flags & AT_EMPTY_PATH) {
3928 if (!capable(CAP_DAC_READ_SEARCH))
3929 return -ENOENT;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303930 how = LOOKUP_EMPTY;
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07003931 }
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003932
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303933 if (flags & AT_SYMLINK_FOLLOW)
3934 how |= LOOKUP_FOLLOW;
Jeff Layton442e31c2012-12-20 16:15:38 -05003935retry:
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303936 error = user_path_at(olddfd, oldname, how, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04003938 return error;
3939
Jeff Layton442e31c2012-12-20 16:15:38 -05003940 new_dentry = user_path_create(newdfd, newname, &new_path,
3941 (how & LOOKUP_REVAL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003943 if (IS_ERR(new_dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003944 goto out;
3945
3946 error = -EXDEV;
3947 if (old_path.mnt != new_path.mnt)
3948 goto out_dput;
Kees Cook800179c2012-07-25 17:29:07 -07003949 error = may_linkat(&old_path);
3950 if (unlikely(error))
3951 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04003952 error = security_path_link(old_path.dentry, &new_path, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003953 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04003954 goto out_dput;
J. Bruce Fields146a8592011-09-20 17:14:31 -04003955 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode);
Dave Hansen75c3f292008-02-15 14:37:45 -08003956out_dput:
Al Viro921a1652012-07-20 01:15:31 +04003957 done_path_create(&new_path, new_dentry);
J. Bruce Fields146a8592011-09-20 17:14:31 -04003958 if (delegated_inode) {
3959 error = break_deleg_wait(&delegated_inode);
Oleg Drokind22e6332014-01-31 15:41:58 -05003960 if (!error) {
3961 path_put(&old_path);
J. Bruce Fields146a8592011-09-20 17:14:31 -04003962 goto retry;
Oleg Drokind22e6332014-01-31 15:41:58 -05003963 }
J. Bruce Fields146a8592011-09-20 17:14:31 -04003964 }
Jeff Layton442e31c2012-12-20 16:15:38 -05003965 if (retry_estale(error, how)) {
Oleg Drokind22e6332014-01-31 15:41:58 -05003966 path_put(&old_path);
Jeff Layton442e31c2012-12-20 16:15:38 -05003967 how |= LOOKUP_REVAL;
3968 goto retry;
3969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970out:
Al Viro2d8f3032008-07-22 09:59:21 -04003971 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972
3973 return error;
3974}
3975
Heiko Carstens3480b252009-01-14 14:14:16 +01003976SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003977{
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003978 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003979}
3980
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02003981/**
3982 * vfs_rename - rename a filesystem object
3983 * @old_dir: parent of source
3984 * @old_dentry: source
3985 * @new_dir: parent of destination
3986 * @new_dentry: destination
3987 * @delegated_inode: returns an inode needing a delegation break
Miklos Szeredi148622f2014-04-01 17:08:42 +02003988 * @flags: rename flags
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02003989 *
3990 * The caller must hold multiple mutexes--see lock_rename()).
3991 *
3992 * If vfs_rename discovers a delegation in need of breaking at either
3993 * the source or destination, it will return -EWOULDBLOCK and return a
3994 * reference to the inode in delegated_inode. The caller should then
3995 * break the delegation and retry. Because breaking a delegation may
3996 * take a long time, the caller should drop all locks before doing
3997 * so.
3998 *
3999 * Alternatively, a caller may pass NULL for delegated_inode. This may
4000 * be appropriate for callers that expect the underlying filesystem not
4001 * to be NFS exported.
4002 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 * The worst of all namespace operations - renaming directory. "Perverted"
4004 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
4005 * Problems:
4006 * a) we can get into loop creation. Check is done in is_subdir().
4007 * b) race potential - two innocent renames can create a loop together.
4008 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004009 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 * story.
J. Bruce Fields6cedba82012-03-05 11:40:41 -05004011 * c) we have to lock _four_ objects - parents and victim (if it exists),
4012 * and source (if it is not a directory).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004013 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 * whether the target exists). Solution: try to be smart with locking
4015 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004016 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 * move will be locked. Thus we can rank directories by the tree
4018 * (ancestors first) and rank all non-directories after them.
4019 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004020 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 * HOWEVER, it relies on the assumption that any object with ->lookup()
4022 * has no more than 1 dentry. If "hybrid" objects will ever appear,
4023 * we'd better make sure that there's no link(2) for them.
Sage Weile4eaac02011-05-24 13:06:07 -07004024 * d) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004025 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05004027 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 * locking].
4029 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004031 struct inode *new_dir, struct dentry *new_dentry,
Miklos Szeredi148622f2014-04-01 17:08:42 +02004032 struct inode **delegated_inode, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033{
4034 int error;
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02004035 bool is_dir = d_is_dir(old_dentry);
Eric Paris59b0df22010-02-08 12:53:52 -05004036 const unsigned char *old_name;
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02004037 struct inode *source = old_dentry->d_inode;
4038 struct inode *target = new_dentry->d_inode;
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004039 bool new_is_dir = false;
4040 unsigned max_links = new_dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02004042 if (source == target)
4043 return 0;
4044
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 error = may_delete(old_dir, old_dentry, is_dir);
4046 if (error)
4047 return error;
4048
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004049 if (!target) {
Miklos Szeredia95164d2008-07-30 15:08:48 +02004050 error = may_create(new_dir, new_dentry);
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004051 } else {
4052 new_is_dir = d_is_dir(new_dentry);
4053
4054 if (!(flags & RENAME_EXCHANGE))
4055 error = may_delete(new_dir, new_dentry, is_dir);
4056 else
4057 error = may_delete(new_dir, new_dentry, new_is_dir);
4058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 if (error)
4060 return error;
4061
Miklos Szeredia756aea2014-07-23 15:15:30 +02004062 if (!old_dir->i_op->rename && !old_dir->i_op->rename2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 return -EPERM;
4064
Miklos Szeredi148622f2014-04-01 17:08:42 +02004065 if (flags && !old_dir->i_op->rename2)
4066 return -EINVAL;
4067
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02004068 /*
4069 * If we are going to change the parent - check write permissions,
4070 * we'll need to flip '..'.
4071 */
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004072 if (new_dir != old_dir) {
4073 if (is_dir) {
4074 error = inode_permission(source, MAY_WRITE);
4075 if (error)
4076 return error;
4077 }
4078 if ((flags & RENAME_EXCHANGE) && new_is_dir) {
4079 error = inode_permission(target, MAY_WRITE);
4080 if (error)
4081 return error;
4082 }
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02004083 }
Robert Love0eeca282005-07-12 17:06:03 -04004084
Miklos Szeredi5371fcd2014-04-01 17:08:43 +02004085 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry,
4086 flags);
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02004087 if (error)
4088 return error;
4089
4090 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
4091 dget(new_dentry);
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004092 if (!is_dir || (flags & RENAME_EXCHANGE))
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02004093 lock_two_nondirectories(source, target);
4094 else if (target)
4095 mutex_lock(&target->i_mutex);
4096
4097 error = -EBUSY;
4098 if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
4099 goto out;
4100
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004101 if (max_links && new_dir != old_dir) {
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02004102 error = -EMLINK;
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004103 if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links)
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02004104 goto out;
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004105 if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir &&
4106 old_dir->i_nlink >= max_links)
4107 goto out;
4108 }
4109 if (is_dir && !(flags & RENAME_EXCHANGE) && target)
4110 shrink_dcache_parent(new_dentry);
4111 if (!is_dir) {
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02004112 error = try_break_deleg(source, delegated_inode);
4113 if (error)
4114 goto out;
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004115 }
4116 if (target && !new_is_dir) {
4117 error = try_break_deleg(target, delegated_inode);
4118 if (error)
4119 goto out;
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02004120 }
Miklos Szeredia756aea2014-07-23 15:15:30 +02004121 if (!old_dir->i_op->rename2) {
Miklos Szeredi148622f2014-04-01 17:08:42 +02004122 error = old_dir->i_op->rename(old_dir, old_dentry,
4123 new_dir, new_dentry);
4124 } else {
Miklos Szeredia756aea2014-07-23 15:15:30 +02004125 WARN_ON(old_dir->i_op->rename != NULL);
Miklos Szeredi148622f2014-04-01 17:08:42 +02004126 error = old_dir->i_op->rename2(old_dir, old_dentry,
4127 new_dir, new_dentry, flags);
4128 }
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02004129 if (error)
4130 goto out;
4131
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004132 if (!(flags & RENAME_EXCHANGE) && target) {
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02004133 if (is_dir)
4134 target->i_flags |= S_DEAD;
4135 dont_mount(new_dentry);
4136 }
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004137 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) {
4138 if (!(flags & RENAME_EXCHANGE))
4139 d_move(old_dentry, new_dentry);
4140 else
4141 d_exchange(old_dentry, new_dentry);
4142 }
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02004143out:
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004144 if (!is_dir || (flags & RENAME_EXCHANGE))
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02004145 unlock_two_nondirectories(source, target);
4146 else if (target)
4147 mutex_unlock(&target->i_mutex);
4148 dput(new_dentry);
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004149 if (!error) {
Al Viro123df292009-12-25 04:57:57 -05004150 fsnotify_move(old_dir, new_dir, old_name, is_dir,
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004151 !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
4152 if (flags & RENAME_EXCHANGE) {
4153 fsnotify_move(new_dir, old_dir, old_dentry->d_name.name,
4154 new_is_dir, NULL, new_dentry);
4155 }
4156 }
Robert Love0eeca282005-07-12 17:06:03 -04004157 fsnotify_oldname_free(old_name);
4158
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 return error;
4160}
4161
Miklos Szeredi148622f2014-04-01 17:08:42 +02004162SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
4163 int, newdfd, const char __user *, newname, unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164{
Al Viro2ad94ae2008-07-21 09:32:51 -04004165 struct dentry *old_dir, *new_dir;
4166 struct dentry *old_dentry, *new_dentry;
4167 struct dentry *trap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 struct nameidata oldnd, newnd;
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004169 struct inode *delegated_inode = NULL;
Jeff Layton91a27b22012-10-10 15:25:28 -04004170 struct filename *from;
4171 struct filename *to;
Jeff Laytonc6a94282012-12-11 12:10:10 -05004172 unsigned int lookup_flags = 0;
4173 bool should_retry = false;
Al Viro2ad94ae2008-07-21 09:32:51 -04004174 int error;
Miklos Szeredi148622f2014-04-01 17:08:42 +02004175
Miklos Szeredi0e98d782014-10-24 00:14:37 +02004176 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004177 return -EINVAL;
4178
Miklos Szeredi0e98d782014-10-24 00:14:37 +02004179 if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) &&
4180 (flags & RENAME_EXCHANGE))
Miklos Szeredi148622f2014-04-01 17:08:42 +02004181 return -EINVAL;
4182
Miklos Szeredi0e98d782014-10-24 00:14:37 +02004183 if ((flags & RENAME_WHITEOUT) && !capable(CAP_MKNOD))
4184 return -EPERM;
4185
Jeff Laytonc6a94282012-12-11 12:10:10 -05004186retry:
4187 from = user_path_parent(olddfd, oldname, &oldnd, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04004188 if (IS_ERR(from)) {
4189 error = PTR_ERR(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 goto exit;
Jeff Layton91a27b22012-10-10 15:25:28 -04004191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192
Jeff Laytonc6a94282012-12-11 12:10:10 -05004193 to = user_path_parent(newdfd, newname, &newnd, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04004194 if (IS_ERR(to)) {
4195 error = PTR_ERR(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 goto exit1;
Jeff Layton91a27b22012-10-10 15:25:28 -04004197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198
4199 error = -EXDEV;
Jan Blunck4ac91372008-02-14 19:34:32 -08004200 if (oldnd.path.mnt != newnd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 goto exit2;
4202
Jan Blunck4ac91372008-02-14 19:34:32 -08004203 old_dir = oldnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 error = -EBUSY;
4205 if (oldnd.last_type != LAST_NORM)
4206 goto exit2;
4207
Jan Blunck4ac91372008-02-14 19:34:32 -08004208 new_dir = newnd.path.dentry;
Miklos Szeredif1bdf912014-04-01 17:08:43 +02004209 if (flags & RENAME_NOREPLACE)
4210 error = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 if (newnd.last_type != LAST_NORM)
4212 goto exit2;
4213
Jan Karac30dabf2012-06-12 16:20:30 +02004214 error = mnt_want_write(oldnd.path.mnt);
4215 if (error)
4216 goto exit2;
4217
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004218 oldnd.flags &= ~LOOKUP_PARENT;
4219 newnd.flags &= ~LOOKUP_PARENT;
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004220 if (!(flags & RENAME_EXCHANGE))
4221 newnd.flags |= LOOKUP_RENAME_TARGET;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004222
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004223retry_deleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 trap = lock_rename(new_dir, old_dir);
4225
Christoph Hellwig49705b72005-11-08 21:35:06 -08004226 old_dentry = lookup_hash(&oldnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 error = PTR_ERR(old_dentry);
4228 if (IS_ERR(old_dentry))
4229 goto exit3;
4230 /* source must exist */
4231 error = -ENOENT;
David Howellsb18825a2013-09-12 19:22:53 +01004232 if (d_is_negative(old_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 goto exit4;
Christoph Hellwig49705b72005-11-08 21:35:06 -08004234 new_dentry = lookup_hash(&newnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 error = PTR_ERR(new_dentry);
4236 if (IS_ERR(new_dentry))
4237 goto exit4;
Miklos Szeredif1bdf912014-04-01 17:08:43 +02004238 error = -EEXIST;
4239 if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry))
4240 goto exit5;
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004241 if (flags & RENAME_EXCHANGE) {
4242 error = -ENOENT;
4243 if (d_is_negative(new_dentry))
4244 goto exit5;
4245
4246 if (!d_is_dir(new_dentry)) {
4247 error = -ENOTDIR;
4248 if (newnd.last.name[newnd.last.len])
4249 goto exit5;
4250 }
4251 }
Miklos Szeredif1bdf912014-04-01 17:08:43 +02004252 /* unless the source is a directory trailing slashes give -ENOTDIR */
4253 if (!d_is_dir(old_dentry)) {
4254 error = -ENOTDIR;
4255 if (oldnd.last.name[oldnd.last.len])
4256 goto exit5;
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004257 if (!(flags & RENAME_EXCHANGE) && newnd.last.name[newnd.last.len])
Miklos Szeredif1bdf912014-04-01 17:08:43 +02004258 goto exit5;
4259 }
4260 /* source should not be ancestor of target */
4261 error = -EINVAL;
4262 if (old_dentry == trap)
4263 goto exit5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 /* target should not be an ancestor of source */
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004265 if (!(flags & RENAME_EXCHANGE))
4266 error = -ENOTEMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267 if (new_dentry == trap)
4268 goto exit5;
4269
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004270 error = security_path_rename(&oldnd.path, old_dentry,
Miklos Szeredi5371fcd2014-04-01 17:08:43 +02004271 &newnd.path, new_dentry, flags);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004272 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02004273 goto exit5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 error = vfs_rename(old_dir->d_inode, old_dentry,
Miklos Szeredi148622f2014-04-01 17:08:42 +02004275 new_dir->d_inode, new_dentry,
4276 &delegated_inode, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277exit5:
4278 dput(new_dentry);
4279exit4:
4280 dput(old_dentry);
4281exit3:
4282 unlock_rename(new_dir, old_dir);
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004283 if (delegated_inode) {
4284 error = break_deleg_wait(&delegated_inode);
4285 if (!error)
4286 goto retry_deleg;
4287 }
Jan Karac30dabf2012-06-12 16:20:30 +02004288 mnt_drop_write(oldnd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289exit2:
Jeff Laytonc6a94282012-12-11 12:10:10 -05004290 if (retry_estale(error, lookup_flags))
4291 should_retry = true;
Jan Blunck1d957f92008-02-14 19:34:35 -08004292 path_put(&newnd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04004293 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08004295 path_put(&oldnd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 putname(from);
Jeff Laytonc6a94282012-12-11 12:10:10 -05004297 if (should_retry) {
4298 should_retry = false;
4299 lookup_flags |= LOOKUP_REVAL;
4300 goto retry;
4301 }
Al Viro2ad94ae2008-07-21 09:32:51 -04004302exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 return error;
4304}
4305
Miklos Szeredi148622f2014-04-01 17:08:42 +02004306SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4307 int, newdfd, const char __user *, newname)
4308{
4309 return sys_renameat2(olddfd, oldname, newdfd, newname, 0);
4310}
4311
Heiko Carstensa26eab22009-01-14 14:14:17 +01004312SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004313{
Miklos Szeredi148622f2014-04-01 17:08:42 +02004314 return sys_renameat2(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004315}
4316
Miklos Szeredi9354e062014-10-24 00:14:36 +02004317int vfs_whiteout(struct inode *dir, struct dentry *dentry)
4318{
4319 int error = may_create(dir, dentry);
4320 if (error)
4321 return error;
4322
4323 if (!dir->i_op->mknod)
4324 return -EPERM;
4325
4326 return dir->i_op->mknod(dir, dentry,
4327 S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
4328}
4329EXPORT_SYMBOL(vfs_whiteout);
4330
Al Viro0750d562014-03-14 13:42:45 -04004331int readlink_copy(char __user *buffer, int buflen, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332{
Al Viro0750d562014-03-14 13:42:45 -04004333 int len = PTR_ERR(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 if (IS_ERR(link))
4335 goto out;
4336
4337 len = strlen(link);
4338 if (len > (unsigned) buflen)
4339 len = buflen;
4340 if (copy_to_user(buffer, link, len))
4341 len = -EFAULT;
4342out:
4343 return len;
4344}
4345
4346/*
4347 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
4348 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
4349 * using) it for any given inode is up to filesystem.
4350 */
4351int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4352{
4353 struct nameidata nd;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004354 void *cookie;
Marcin Slusarz694a1762008-06-09 16:40:37 -07004355 int res;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004356
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 nd.depth = 0;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004358 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
Marcin Slusarz694a1762008-06-09 16:40:37 -07004359 if (IS_ERR(cookie))
4360 return PTR_ERR(cookie);
4361
Al Viro0750d562014-03-14 13:42:45 -04004362 res = readlink_copy(buffer, buflen, nd_get_link(&nd));
Marcin Slusarz694a1762008-06-09 16:40:37 -07004363 if (dentry->d_inode->i_op->put_link)
4364 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
4365 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366}
4367
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368/* get the link contents into pagecache */
4369static char *page_getlink(struct dentry * dentry, struct page **ppage)
4370{
Duane Griffinebd09ab2008-12-19 20:47:12 +00004371 char *kaddr;
4372 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 struct address_space *mapping = dentry->d_inode->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -07004374 page = read_mapping_page(mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 if (IS_ERR(page))
Nick Piggin6fe69002007-05-06 14:49:04 -07004376 return (char*)page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 *ppage = page;
Duane Griffinebd09ab2008-12-19 20:47:12 +00004378 kaddr = kmap(page);
4379 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
4380 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381}
4382
4383int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4384{
4385 struct page *page = NULL;
Al Viro0750d562014-03-14 13:42:45 -04004386 int res = readlink_copy(buffer, buflen, page_getlink(dentry, &page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 if (page) {
4388 kunmap(page);
4389 page_cache_release(page);
4390 }
4391 return res;
4392}
4393
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004394void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004396 struct page *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 nd_set_link(nd, page_getlink(dentry, &page));
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004398 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399}
4400
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004401void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004403 struct page *page = cookie;
4404
4405 if (page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 kunmap(page);
4407 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 }
4409}
4410
Nick Piggin54566b22009-01-04 12:00:53 -08004411/*
4412 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
4413 */
4414int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415{
4416 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004417 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07004418 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08004419 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 char *kaddr;
Nick Piggin54566b22009-01-04 12:00:53 -08004421 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
4422 if (nofs)
4423 flags |= AOP_FLAG_NOFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424
NeilBrown7e53cac2006-03-25 03:07:57 -08004425retry:
Nick Pigginafddba42007-10-16 01:25:01 -07004426 err = pagecache_write_begin(NULL, mapping, 0, len-1,
Nick Piggin54566b22009-01-04 12:00:53 -08004427 flags, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07004429 goto fail;
4430
Cong Wange8e3c3d2011-11-25 23:14:27 +08004431 kaddr = kmap_atomic(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 memcpy(kaddr, symname, len-1);
Cong Wange8e3c3d2011-11-25 23:14:27 +08004433 kunmap_atomic(kaddr);
Nick Pigginafddba42007-10-16 01:25:01 -07004434
4435 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4436 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 if (err < 0)
4438 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07004439 if (err < len-1)
4440 goto retry;
4441
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 mark_inode_dirty(inode);
4443 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444fail:
4445 return err;
4446}
4447
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004448int page_symlink(struct inode *inode, const char *symname, int len)
4449{
4450 return __page_symlink(inode, symname, len,
Nick Piggin54566b22009-01-04 12:00:53 -08004451 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004452}
4453
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08004454const struct inode_operations page_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 .readlink = generic_readlink,
4456 .follow_link = page_follow_link_light,
4457 .put_link = page_put_link,
4458};
4459
Al Viro2d8f3032008-07-22 09:59:21 -04004460EXPORT_SYMBOL(user_path_at);
David Howellscc53ce52011-01-14 18:45:26 +00004461EXPORT_SYMBOL(follow_down_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462EXPORT_SYMBOL(follow_down);
4463EXPORT_SYMBOL(follow_up);
Al Virof6d2ac52012-08-26 12:55:54 -04004464EXPORT_SYMBOL(get_write_access); /* nfsd */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466EXPORT_SYMBOL(lookup_one_len);
4467EXPORT_SYMBOL(page_follow_link_light);
4468EXPORT_SYMBOL(page_put_link);
4469EXPORT_SYMBOL(page_readlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004470EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471EXPORT_SYMBOL(page_symlink);
4472EXPORT_SYMBOL(page_symlink_inode_operations);
Al Virod1811462008-08-02 00:49:18 -04004473EXPORT_SYMBOL(kern_path);
Josef 'Jeff' Sipek16f182002007-07-19 01:48:18 -07004474EXPORT_SYMBOL(vfs_path_lookup);
Al Virof419a2e2008-07-22 00:07:17 -04004475EXPORT_SYMBOL(inode_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476EXPORT_SYMBOL(unlock_rename);
4477EXPORT_SYMBOL(vfs_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478EXPORT_SYMBOL(vfs_link);
4479EXPORT_SYMBOL(vfs_mkdir);
4480EXPORT_SYMBOL(vfs_mknod);
4481EXPORT_SYMBOL(generic_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482EXPORT_SYMBOL(vfs_rename);
4483EXPORT_SYMBOL(vfs_rmdir);
4484EXPORT_SYMBOL(vfs_symlink);
4485EXPORT_SYMBOL(vfs_unlink);
4486EXPORT_SYMBOL(dentry_unhash);
4487EXPORT_SYMBOL(generic_readlink);