blob: b987228e6afb45bed270a1844c02324abc47f5d1 [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}
418
Al Virof4d6ff82011-06-19 13:14:21 -0400419/**
David Howells0bdaea92012-06-25 12:55:46 +0100420 * sb_permission - Check superblock-level permissions
421 * @sb: Superblock of inode to check permission on
Randy Dunlap55852632012-08-18 17:39:25 -0700422 * @inode: Inode to check permission on
David Howells0bdaea92012-06-25 12:55:46 +0100423 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
424 *
425 * Separate out file-system wide checks from inode-specific permission checks.
426 */
427static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
428{
429 if (unlikely(mask & MAY_WRITE)) {
430 umode_t mode = inode->i_mode;
431
432 /* Nobody gets write access to a read-only fs. */
433 if ((sb->s_flags & MS_RDONLY) &&
434 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
435 return -EROFS;
436 }
437 return 0;
438}
439
440/**
441 * inode_permission - Check for access rights to a given inode
442 * @inode: Inode to check permission on
443 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
444 *
445 * Check for read/write/execute permissions on an inode. We use fs[ug]id for
446 * this, letting us set arbitrary permissions for filesystem access without
447 * changing the "normal" UIDs which are used for other things.
448 *
449 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
450 */
451int inode_permission(struct inode *inode, int mask)
452{
453 int retval;
454
455 retval = sb_permission(inode->i_sb, inode, mask);
456 if (retval)
457 return retval;
458 return __inode_permission(inode, mask);
459}
460
461/**
Jan Blunck5dd784d02008-02-14 19:34:38 -0800462 * path_get - get a reference to a path
463 * @path: path to get the reference to
464 *
465 * Given a path increment the reference count to the dentry and the vfsmount.
466 */
Al Virodcf787f2013-03-01 23:51:07 -0500467void path_get(const struct path *path)
Jan Blunck5dd784d02008-02-14 19:34:38 -0800468{
469 mntget(path->mnt);
470 dget(path->dentry);
471}
472EXPORT_SYMBOL(path_get);
473
474/**
Jan Blunck1d957f92008-02-14 19:34:35 -0800475 * path_put - put a reference to a path
476 * @path: path to put the reference to
477 *
478 * Given a path decrement the reference count to the dentry and the vfsmount.
479 */
Al Virodcf787f2013-03-01 23:51:07 -0500480void path_put(const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
Jan Blunck1d957f92008-02-14 19:34:35 -0800482 dput(path->dentry);
483 mntput(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
Jan Blunck1d957f92008-02-14 19:34:35 -0800485EXPORT_SYMBOL(path_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Al Viro19660af2011-03-25 10:32:48 -0400487/*
Nick Piggin31e6b012011-01-07 17:49:52 +1100488 * Path walking has 2 modes, rcu-walk and ref-walk (see
Al Viro19660af2011-03-25 10:32:48 -0400489 * Documentation/filesystems/path-lookup.txt). In situations when we can't
490 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
491 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
492 * mode. Refcounts are grabbed at the last known good point before rcu-walk
493 * got stuck, so ref-walk may continue from there. If this is not successful
494 * (eg. a seqcount has changed), then failure is returned and it's up to caller
495 * to restart the path walk from the beginning in ref-walk mode.
Nick Piggin31e6b012011-01-07 17:49:52 +1100496 */
Nick Piggin31e6b012011-01-07 17:49:52 +1100497
498/**
Al Viro19660af2011-03-25 10:32:48 -0400499 * unlazy_walk - try to switch to ref-walk mode.
500 * @nd: nameidata pathwalk data
501 * @dentry: child of nd->path.dentry or NULL
Randy Dunlap39191622011-01-08 19:36:21 -0800502 * Returns: 0 on success, -ECHILD on failure
Nick Piggin31e6b012011-01-07 17:49:52 +1100503 *
Al Viro19660af2011-03-25 10:32:48 -0400504 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
505 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
506 * @nd or NULL. Must be called from rcu-walk context.
Nick Piggin31e6b012011-01-07 17:49:52 +1100507 */
Al Viro19660af2011-03-25 10:32:48 -0400508static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
Nick Piggin31e6b012011-01-07 17:49:52 +1100509{
510 struct fs_struct *fs = current->fs;
511 struct dentry *parent = nd->path.dentry;
512
513 BUG_ON(!(nd->flags & LOOKUP_RCU));
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700514
515 /*
Al Viro48a066e2013-09-29 22:06:07 -0400516 * After legitimizing the bastards, terminate_walk()
517 * will do the right thing for non-RCU mode, and all our
518 * subsequent exit cases should rcu_read_unlock()
519 * before returning. Do vfsmount first; if dentry
520 * can't be legitimized, just set nd->path.dentry to NULL
521 * and rely on dput(NULL) being a no-op.
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700522 */
Al Viro48a066e2013-09-29 22:06:07 -0400523 if (!legitimize_mnt(nd->path.mnt, nd->m_seq))
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700524 return -ECHILD;
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700525 nd->flags &= ~LOOKUP_RCU;
Linus Torvalds15570082013-09-02 11:38:06 -0700526
Al Viro48a066e2013-09-29 22:06:07 -0400527 if (!lockref_get_not_dead(&parent->d_lockref)) {
528 nd->path.dentry = NULL;
Al Virod870b4a2013-11-29 01:48:32 -0500529 goto out;
Al Viro48a066e2013-09-29 22:06:07 -0400530 }
531
Linus Torvalds15570082013-09-02 11:38:06 -0700532 /*
533 * For a negative lookup, the lookup sequence point is the parents
534 * sequence point, and it only needs to revalidate the parent dentry.
535 *
536 * For a positive lookup, we need to move both the parent and the
537 * dentry from the RCU domain to be properly refcounted. And the
538 * sequence number in the dentry validates *both* dentry counters,
539 * since we checked the sequence number of the parent after we got
540 * the child sequence number. So we know the parent must still
541 * be valid if the child sequence number is still valid.
542 */
Al Viro19660af2011-03-25 10:32:48 -0400543 if (!dentry) {
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700544 if (read_seqcount_retry(&parent->d_seq, nd->seq))
545 goto out;
Al Viro19660af2011-03-25 10:32:48 -0400546 BUG_ON(nd->inode != parent->d_inode);
547 } else {
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700548 if (!lockref_get_not_dead(&dentry->d_lockref))
549 goto out;
550 if (read_seqcount_retry(&dentry->d_seq, nd->seq))
551 goto drop_dentry;
Al Viro19660af2011-03-25 10:32:48 -0400552 }
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700553
554 /*
555 * Sequence counts matched. Now make sure that the root is
556 * still valid and get it if required.
557 */
558 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
559 spin_lock(&fs->lock);
560 if (nd->root.mnt != fs->root.mnt || nd->root.dentry != fs->root.dentry)
561 goto unlock_and_drop_dentry;
Nick Piggin31e6b012011-01-07 17:49:52 +1100562 path_get(&nd->root);
563 spin_unlock(&fs->lock);
564 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100565
Al Viro8b61e742013-11-08 12:45:01 -0500566 rcu_read_unlock();
Nick Piggin31e6b012011-01-07 17:49:52 +1100567 return 0;
Al Viro19660af2011-03-25 10:32:48 -0400568
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700569unlock_and_drop_dentry:
570 spin_unlock(&fs->lock);
571drop_dentry:
Al Viro8b61e742013-11-08 12:45:01 -0500572 rcu_read_unlock();
Linus Torvalds15570082013-09-02 11:38:06 -0700573 dput(dentry);
Linus Torvaldsd0d27272013-09-10 12:17:49 -0700574 goto drop_root_mnt;
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700575out:
Al Viro8b61e742013-11-08 12:45:01 -0500576 rcu_read_unlock();
Linus Torvaldsd0d27272013-09-10 12:17:49 -0700577drop_root_mnt:
578 if (!(nd->flags & LOOKUP_ROOT))
579 nd->root.mnt = NULL;
Nick Piggin31e6b012011-01-07 17:49:52 +1100580 return -ECHILD;
581}
582
Al Viro4ce16ef32012-06-10 16:10:59 -0400583static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
Nick Piggin34286d62011-01-07 17:49:57 +1100584{
Al Viro4ce16ef32012-06-10 16:10:59 -0400585 return dentry->d_op->d_revalidate(dentry, flags);
Nick Piggin34286d62011-01-07 17:49:57 +1100586}
587
Al Viro9f1fafe2011-03-25 11:00:12 -0400588/**
589 * complete_walk - successful completion of path walk
590 * @nd: pointer nameidata
Jeff Layton39159de2009-12-07 12:01:50 -0500591 *
Al Viro9f1fafe2011-03-25 11:00:12 -0400592 * If we had been in RCU mode, drop out of it and legitimize nd->path.
593 * Revalidate the final result, unless we'd already done that during
594 * the path walk or the filesystem doesn't ask for it. Return 0 on
595 * success, -error on failure. In case of failure caller does not
596 * need to drop nd->path.
Jeff Layton39159de2009-12-07 12:01:50 -0500597 */
Al Viro9f1fafe2011-03-25 11:00:12 -0400598static int complete_walk(struct nameidata *nd)
Jeff Layton39159de2009-12-07 12:01:50 -0500599{
Al Viro16c2cd72011-02-22 15:50:10 -0500600 struct dentry *dentry = nd->path.dentry;
Jeff Layton39159de2009-12-07 12:01:50 -0500601 int status;
Jeff Layton39159de2009-12-07 12:01:50 -0500602
Al Viro9f1fafe2011-03-25 11:00:12 -0400603 if (nd->flags & LOOKUP_RCU) {
604 nd->flags &= ~LOOKUP_RCU;
605 if (!(nd->flags & LOOKUP_ROOT))
606 nd->root.mnt = NULL;
Linus Torvalds15570082013-09-02 11:38:06 -0700607
Al Viro48a066e2013-09-29 22:06:07 -0400608 if (!legitimize_mnt(nd->path.mnt, nd->m_seq)) {
Al Viro8b61e742013-11-08 12:45:01 -0500609 rcu_read_unlock();
Al Viro48a066e2013-09-29 22:06:07 -0400610 return -ECHILD;
611 }
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700612 if (unlikely(!lockref_get_not_dead(&dentry->d_lockref))) {
Al Viro8b61e742013-11-08 12:45:01 -0500613 rcu_read_unlock();
Al Viro48a066e2013-09-29 22:06:07 -0400614 mntput(nd->path.mnt);
Al Viro9f1fafe2011-03-25 11:00:12 -0400615 return -ECHILD;
616 }
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700617 if (read_seqcount_retry(&dentry->d_seq, nd->seq)) {
Al Viro8b61e742013-11-08 12:45:01 -0500618 rcu_read_unlock();
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700619 dput(dentry);
Al Viro48a066e2013-09-29 22:06:07 -0400620 mntput(nd->path.mnt);
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700621 return -ECHILD;
622 }
Al Viro8b61e742013-11-08 12:45:01 -0500623 rcu_read_unlock();
Al Viro9f1fafe2011-03-25 11:00:12 -0400624 }
625
Al Viro16c2cd72011-02-22 15:50:10 -0500626 if (likely(!(nd->flags & LOOKUP_JUMPED)))
Jeff Layton39159de2009-12-07 12:01:50 -0500627 return 0;
628
Jeff Laytonecf3d1f2013-02-20 11:19:05 -0500629 if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
Al Viro16c2cd72011-02-22 15:50:10 -0500630 return 0;
631
Jeff Laytonecf3d1f2013-02-20 11:19:05 -0500632 status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
Jeff Layton39159de2009-12-07 12:01:50 -0500633 if (status > 0)
634 return 0;
635
Al Viro16c2cd72011-02-22 15:50:10 -0500636 if (!status)
Jeff Layton39159de2009-12-07 12:01:50 -0500637 status = -ESTALE;
Al Viro16c2cd72011-02-22 15:50:10 -0500638
Al Viro9f1fafe2011-03-25 11:00:12 -0400639 path_put(&nd->path);
Jeff Layton39159de2009-12-07 12:01:50 -0500640 return status;
641}
642
Al Viro2a737872009-04-07 11:49:53 -0400643static __always_inline void set_root(struct nameidata *nd)
644{
Al Viroaed47132014-09-13 21:55:46 -0400645 get_fs_root(current->fs, &nd->root);
Al Viro2a737872009-04-07 11:49:53 -0400646}
647
Al Viro6de88d72009-08-09 01:41:57 +0400648static int link_path_walk(const char *, struct nameidata *);
649
Al Viroaed47132014-09-13 21:55:46 -0400650static __always_inline unsigned set_root_rcu(struct nameidata *nd)
Nick Piggin31e6b012011-01-07 17:49:52 +1100651{
Al Viroaed47132014-09-13 21:55:46 -0400652 struct fs_struct *fs = current->fs;
653 unsigned seq, res;
Nick Pigginc28cc362011-01-07 17:49:53 +1100654
Al Viroaed47132014-09-13 21:55:46 -0400655 do {
656 seq = read_seqcount_begin(&fs->seq);
657 nd->root = fs->root;
658 res = __read_seqcount_begin(&nd->root.dentry->d_seq);
659 } while (read_seqcount_retry(&fs->seq, seq));
660 return res;
Nick Piggin31e6b012011-01-07 17:49:52 +1100661}
662
Jan Blunck1d957f92008-02-14 19:34:35 -0800663static void path_put_conditional(struct path *path, struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700664{
665 dput(path->dentry);
Jan Blunck4ac91372008-02-14 19:34:32 -0800666 if (path->mnt != nd->path.mnt)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700667 mntput(path->mnt);
668}
669
Nick Piggin7b9337a2011-01-14 08:42:43 +0000670static inline void path_to_nameidata(const struct path *path,
671 struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700672{
Nick Piggin31e6b012011-01-07 17:49:52 +1100673 if (!(nd->flags & LOOKUP_RCU)) {
674 dput(nd->path.dentry);
675 if (nd->path.mnt != path->mnt)
676 mntput(nd->path.mnt);
Huang Shijie9a229682010-04-02 17:37:13 +0800677 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100678 nd->path.mnt = path->mnt;
Jan Blunck4ac91372008-02-14 19:34:32 -0800679 nd->path.dentry = path->dentry;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700680}
681
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400682/*
683 * Helper to directly jump to a known parsed path from ->follow_link,
684 * caller must have taken a reference to path beforehand.
685 */
686void nd_jump_link(struct nameidata *nd, struct path *path)
687{
688 path_put(&nd->path);
689
690 nd->path = *path;
691 nd->inode = nd->path.dentry->d_inode;
692 nd->flags |= LOOKUP_JUMPED;
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400693}
694
Al Viro574197e2011-03-14 22:20:34 -0400695static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
696{
697 struct inode *inode = link->dentry->d_inode;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400698 if (inode->i_op->put_link)
Al Viro574197e2011-03-14 22:20:34 -0400699 inode->i_op->put_link(link->dentry, nd, cookie);
700 path_put(link);
701}
702
Linus Torvalds561ec642012-10-26 10:05:07 -0700703int sysctl_protected_symlinks __read_mostly = 0;
704int sysctl_protected_hardlinks __read_mostly = 0;
Kees Cook800179c2012-07-25 17:29:07 -0700705
706/**
707 * may_follow_link - Check symlink following for unsafe situations
708 * @link: The path of the symlink
Randy Dunlap55852632012-08-18 17:39:25 -0700709 * @nd: nameidata pathwalk data
Kees Cook800179c2012-07-25 17:29:07 -0700710 *
711 * In the case of the sysctl_protected_symlinks sysctl being enabled,
712 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
713 * in a sticky world-writable directory. This is to protect privileged
714 * processes from failing races against path names that may change out
715 * from under them by way of other users creating malicious symlinks.
716 * It will permit symlinks to be followed only when outside a sticky
717 * world-writable directory, or when the uid of the symlink and follower
718 * match, or when the directory owner matches the symlink's owner.
719 *
720 * Returns 0 if following the symlink is allowed, -ve on error.
721 */
722static inline int may_follow_link(struct path *link, struct nameidata *nd)
723{
724 const struct inode *inode;
725 const struct inode *parent;
726
727 if (!sysctl_protected_symlinks)
728 return 0;
729
730 /* Allowed if owner and follower match. */
731 inode = link->dentry->d_inode;
Eric W. Biederman81abe272012-08-03 09:38:08 -0700732 if (uid_eq(current_cred()->fsuid, inode->i_uid))
Kees Cook800179c2012-07-25 17:29:07 -0700733 return 0;
734
735 /* Allowed if parent directory not sticky and world-writable. */
736 parent = nd->path.dentry->d_inode;
737 if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
738 return 0;
739
740 /* Allowed if parent directory and link owner match. */
Eric W. Biederman81abe272012-08-03 09:38:08 -0700741 if (uid_eq(parent->i_uid, inode->i_uid))
Kees Cook800179c2012-07-25 17:29:07 -0700742 return 0;
743
Sasha Levinffd8d102012-10-04 19:56:40 -0400744 audit_log_link_denied("follow_link", link);
Kees Cook800179c2012-07-25 17:29:07 -0700745 path_put_conditional(link, nd);
746 path_put(&nd->path);
747 return -EACCES;
748}
749
750/**
751 * safe_hardlink_source - Check for safe hardlink conditions
752 * @inode: the source inode to hardlink from
753 *
754 * Return false if at least one of the following conditions:
755 * - inode is not a regular file
756 * - inode is setuid
757 * - inode is setgid and group-exec
758 * - access failure for read and write
759 *
760 * Otherwise returns true.
761 */
762static bool safe_hardlink_source(struct inode *inode)
763{
764 umode_t mode = inode->i_mode;
765
766 /* Special files should not get pinned to the filesystem. */
767 if (!S_ISREG(mode))
768 return false;
769
770 /* Setuid files should not get pinned to the filesystem. */
771 if (mode & S_ISUID)
772 return false;
773
774 /* Executable setgid files should not get pinned to the filesystem. */
775 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
776 return false;
777
778 /* Hardlinking to unreadable or unwritable sources is dangerous. */
779 if (inode_permission(inode, MAY_READ | MAY_WRITE))
780 return false;
781
782 return true;
783}
784
785/**
786 * may_linkat - Check permissions for creating a hardlink
787 * @link: the source to hardlink from
788 *
789 * Block hardlink when all of:
790 * - sysctl_protected_hardlinks enabled
791 * - fsuid does not match inode
792 * - hardlink source is unsafe (see safe_hardlink_source() above)
793 * - not CAP_FOWNER
794 *
795 * Returns 0 if successful, -ve on error.
796 */
797static int may_linkat(struct path *link)
798{
799 const struct cred *cred;
800 struct inode *inode;
801
802 if (!sysctl_protected_hardlinks)
803 return 0;
804
805 cred = current_cred();
806 inode = link->dentry->d_inode;
807
808 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
809 * otherwise, it must be a safe source.
810 */
Eric W. Biederman81abe272012-08-03 09:38:08 -0700811 if (uid_eq(cred->fsuid, inode->i_uid) || safe_hardlink_source(inode) ||
Kees Cook800179c2012-07-25 17:29:07 -0700812 capable(CAP_FOWNER))
813 return 0;
814
Kees Cooka51d9ea2012-07-25 17:29:08 -0700815 audit_log_link_denied("linkat", link);
Kees Cook800179c2012-07-25 17:29:07 -0700816 return -EPERM;
817}
818
Al Virodef4af32009-12-26 08:37:05 -0500819static __always_inline int
Al Viro574197e2011-03-14 22:20:34 -0400820follow_link(struct path *link, struct nameidata *nd, void **p)
Ian Kent051d3812006-03-27 01:14:53 -0800821{
Nick Piggin7b9337a2011-01-14 08:42:43 +0000822 struct dentry *dentry = link->dentry;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400823 int error;
824 char *s;
Ian Kent051d3812006-03-27 01:14:53 -0800825
Al Viro844a3912011-02-15 00:38:26 -0500826 BUG_ON(nd->flags & LOOKUP_RCU);
827
Al Viro0e794582011-03-16 02:45:02 -0400828 if (link->mnt == nd->path.mnt)
829 mntget(link->mnt);
830
Al Viro6d7b5aa2012-06-10 04:15:17 -0400831 error = -ELOOP;
832 if (unlikely(current->total_link_count >= 40))
833 goto out_put_nd_path;
834
Al Viro574197e2011-03-14 22:20:34 -0400835 cond_resched();
836 current->total_link_count++;
837
Al Viro68ac1232012-03-15 08:21:57 -0400838 touch_atime(link);
Ian Kent051d3812006-03-27 01:14:53 -0800839 nd_set_link(nd, NULL);
840
Al Viro36f3b4f2011-02-22 21:24:38 -0500841 error = security_inode_follow_link(link->dentry, nd);
Al Viro6d7b5aa2012-06-10 04:15:17 -0400842 if (error)
843 goto out_put_nd_path;
Al Viro36f3b4f2011-02-22 21:24:38 -0500844
Al Viro86acdca12009-12-22 23:45:11 -0500845 nd->last_type = LAST_BIND;
Al Virodef4af32009-12-26 08:37:05 -0500846 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
847 error = PTR_ERR(*p);
Al Viro6d7b5aa2012-06-10 04:15:17 -0400848 if (IS_ERR(*p))
Christoph Hellwig408ef012012-06-18 10:47:03 -0400849 goto out_put_nd_path;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400850
851 error = 0;
852 s = nd_get_link(nd);
853 if (s) {
Al Viro443ed252013-09-10 12:00:43 -0400854 if (unlikely(IS_ERR(s))) {
855 path_put(&nd->path);
856 put_link(nd, link, *p);
857 return PTR_ERR(s);
858 }
859 if (*s == '/') {
Al Viroaed47132014-09-13 21:55:46 -0400860 if (!nd->root.mnt)
861 set_root(nd);
Al Viro443ed252013-09-10 12:00:43 -0400862 path_put(&nd->path);
863 nd->path = nd->root;
864 path_get(&nd->root);
865 nd->flags |= LOOKUP_JUMPED;
866 }
867 nd->inode = nd->path.dentry->d_inode;
868 error = link_path_walk(s, nd);
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400869 if (unlikely(error))
870 put_link(nd, link, *p);
Ian Kent051d3812006-03-27 01:14:53 -0800871 }
Al Viro6d7b5aa2012-06-10 04:15:17 -0400872
873 return error;
874
875out_put_nd_path:
Arnd Bergmann98f6ef62012-10-11 13:20:00 +0000876 *p = NULL;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400877 path_put(&nd->path);
Al Viro6d7b5aa2012-06-10 04:15:17 -0400878 path_put(link);
Ian Kent051d3812006-03-27 01:14:53 -0800879 return error;
880}
881
Nick Piggin31e6b012011-01-07 17:49:52 +1100882static int follow_up_rcu(struct path *path)
883{
Al Viro0714a532011-11-24 22:19:58 -0500884 struct mount *mnt = real_mount(path->mnt);
885 struct mount *parent;
Nick Piggin31e6b012011-01-07 17:49:52 +1100886 struct dentry *mountpoint;
887
Al Viro0714a532011-11-24 22:19:58 -0500888 parent = mnt->mnt_parent;
889 if (&parent->mnt == path->mnt)
Nick Piggin31e6b012011-01-07 17:49:52 +1100890 return 0;
Al Viroa73324d2011-11-24 22:25:07 -0500891 mountpoint = mnt->mnt_mountpoint;
Nick Piggin31e6b012011-01-07 17:49:52 +1100892 path->dentry = mountpoint;
Al Viro0714a532011-11-24 22:19:58 -0500893 path->mnt = &parent->mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +1100894 return 1;
895}
896
David Howellsf015f1262012-06-25 12:55:28 +0100897/*
898 * follow_up - Find the mountpoint of path's vfsmount
899 *
900 * Given a path, find the mountpoint of its source file system.
901 * Replace @path with the path of the mountpoint in the parent mount.
902 * Up is towards /.
903 *
904 * Return 1 if we went up a level and 0 if we were already at the
905 * root.
906 */
Al Virobab77eb2009-04-18 03:26:48 -0400907int follow_up(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
Al Viro0714a532011-11-24 22:19:58 -0500909 struct mount *mnt = real_mount(path->mnt);
910 struct mount *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 struct dentry *mountpoint;
Nick Piggin99b7db72010-08-18 04:37:39 +1000912
Al Viro48a066e2013-09-29 22:06:07 -0400913 read_seqlock_excl(&mount_lock);
Al Viro0714a532011-11-24 22:19:58 -0500914 parent = mnt->mnt_parent;
Al Viro3c0a6162012-07-18 17:32:50 +0400915 if (parent == mnt) {
Al Viro48a066e2013-09-29 22:06:07 -0400916 read_sequnlock_excl(&mount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 return 0;
918 }
Al Viro0714a532011-11-24 22:19:58 -0500919 mntget(&parent->mnt);
Al Viroa73324d2011-11-24 22:25:07 -0500920 mountpoint = dget(mnt->mnt_mountpoint);
Al Viro48a066e2013-09-29 22:06:07 -0400921 read_sequnlock_excl(&mount_lock);
Al Virobab77eb2009-04-18 03:26:48 -0400922 dput(path->dentry);
923 path->dentry = mountpoint;
924 mntput(path->mnt);
Al Viro0714a532011-11-24 22:19:58 -0500925 path->mnt = &parent->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 return 1;
927}
928
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100929/*
David Howells9875cf82011-01-14 18:45:21 +0000930 * Perform an automount
931 * - return -EISDIR to tell follow_managed() to stop and return the path we
932 * were called with.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 */
David Howells9875cf82011-01-14 18:45:21 +0000934static int follow_automount(struct path *path, unsigned flags,
935 bool *need_mntput)
Nick Piggin31e6b012011-01-07 17:49:52 +1100936{
David Howells9875cf82011-01-14 18:45:21 +0000937 struct vfsmount *mnt;
David Howellsea5b7782011-01-14 19:10:03 +0000938 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +1100939
David Howells9875cf82011-01-14 18:45:21 +0000940 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
941 return -EREMOTE;
Al Viro463ffb22005-06-06 13:36:05 -0700942
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200943 /* We don't want to mount if someone's just doing a stat -
944 * unless they're stat'ing a directory and appended a '/' to
945 * the name.
946 *
947 * We do, however, want to mount if someone wants to open or
948 * create a file of any type under the mountpoint, wants to
949 * traverse through the mountpoint or wants to open the
950 * mounted directory. Also, autofs may mark negative dentries
951 * as being automount points. These will need the attentions
952 * of the daemon to instantiate them before they can be used.
David Howells9875cf82011-01-14 18:45:21 +0000953 */
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200954 if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
Linus Torvaldsd94c1772011-09-26 17:44:55 -0700955 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200956 path->dentry->d_inode)
957 return -EISDIR;
958
David Howells9875cf82011-01-14 18:45:21 +0000959 current->total_link_count++;
960 if (current->total_link_count >= 40)
961 return -ELOOP;
962
963 mnt = path->dentry->d_op->d_automount(path);
964 if (IS_ERR(mnt)) {
965 /*
966 * The filesystem is allowed to return -EISDIR here to indicate
967 * it doesn't want to automount. For instance, autofs would do
968 * this so that its userspace daemon can mount on this dentry.
969 *
970 * However, we can only permit this if it's a terminal point in
971 * the path being looked up; if it wasn't then the remainder of
972 * the path is inaccessible and we should say so.
973 */
Al Viro49084c32011-06-25 21:59:52 -0400974 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
David Howells9875cf82011-01-14 18:45:21 +0000975 return -EREMOTE;
976 return PTR_ERR(mnt);
977 }
David Howellsea5b7782011-01-14 19:10:03 +0000978
David Howells9875cf82011-01-14 18:45:21 +0000979 if (!mnt) /* mount collision */
980 return 0;
981
Al Viro8aef1882011-06-16 15:10:06 +0100982 if (!*need_mntput) {
983 /* lock_mount() may release path->mnt on error */
984 mntget(path->mnt);
985 *need_mntput = true;
986 }
Al Viro19a167a2011-01-17 01:35:23 -0500987 err = finish_automount(mnt, path);
David Howellsea5b7782011-01-14 19:10:03 +0000988
David Howellsea5b7782011-01-14 19:10:03 +0000989 switch (err) {
990 case -EBUSY:
991 /* Someone else made a mount here whilst we were busy */
Al Viro19a167a2011-01-17 01:35:23 -0500992 return 0;
David Howellsea5b7782011-01-14 19:10:03 +0000993 case 0:
Al Viro8aef1882011-06-16 15:10:06 +0100994 path_put(path);
David Howellsea5b7782011-01-14 19:10:03 +0000995 path->mnt = mnt;
996 path->dentry = dget(mnt->mnt_root);
David Howellsea5b7782011-01-14 19:10:03 +0000997 return 0;
Al Viro19a167a2011-01-17 01:35:23 -0500998 default:
999 return err;
David Howellsea5b7782011-01-14 19:10:03 +00001000 }
Al Viro19a167a2011-01-17 01:35:23 -05001001
David Howells9875cf82011-01-14 18:45:21 +00001002}
1003
1004/*
1005 * Handle a dentry that is managed in some way.
David Howellscc53ce52011-01-14 18:45:26 +00001006 * - Flagged for transit management (autofs)
David Howells9875cf82011-01-14 18:45:21 +00001007 * - Flagged as mountpoint
1008 * - Flagged as automount point
1009 *
1010 * This may only be called in refwalk mode.
1011 *
1012 * Serialization is taken care of in namespace.c
1013 */
1014static int follow_managed(struct path *path, unsigned flags)
1015{
Al Viro8aef1882011-06-16 15:10:06 +01001016 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
David Howells9875cf82011-01-14 18:45:21 +00001017 unsigned managed;
1018 bool need_mntput = false;
Al Viro8aef1882011-06-16 15:10:06 +01001019 int ret = 0;
David Howells9875cf82011-01-14 18:45:21 +00001020
1021 /* Given that we're not holding a lock here, we retain the value in a
1022 * local variable for each dentry as we look at it so that we don't see
1023 * the components of that value change under us */
1024 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1025 managed &= DCACHE_MANAGED_DENTRY,
1026 unlikely(managed != 0)) {
David Howellscc53ce52011-01-14 18:45:26 +00001027 /* Allow the filesystem to manage the transit without i_mutex
1028 * being held. */
1029 if (managed & DCACHE_MANAGE_TRANSIT) {
1030 BUG_ON(!path->dentry->d_op);
1031 BUG_ON(!path->dentry->d_op->d_manage);
Al Viro1aed3e42011-03-18 09:09:02 -04001032 ret = path->dentry->d_op->d_manage(path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +00001033 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001034 break;
David Howellscc53ce52011-01-14 18:45:26 +00001035 }
1036
David Howells9875cf82011-01-14 18:45:21 +00001037 /* Transit to a mounted filesystem. */
1038 if (managed & DCACHE_MOUNTED) {
1039 struct vfsmount *mounted = lookup_mnt(path);
1040 if (mounted) {
1041 dput(path->dentry);
1042 if (need_mntput)
1043 mntput(path->mnt);
1044 path->mnt = mounted;
1045 path->dentry = dget(mounted->mnt_root);
1046 need_mntput = true;
1047 continue;
1048 }
1049
1050 /* Something is mounted on this dentry in another
1051 * namespace and/or whatever was mounted there in this
Al Viro48a066e2013-09-29 22:06:07 -04001052 * namespace got unmounted before lookup_mnt() could
1053 * get it */
David Howells9875cf82011-01-14 18:45:21 +00001054 }
1055
1056 /* Handle an automount point */
1057 if (managed & DCACHE_NEED_AUTOMOUNT) {
1058 ret = follow_automount(path, flags, &need_mntput);
1059 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001060 break;
David Howells9875cf82011-01-14 18:45:21 +00001061 continue;
1062 }
1063
1064 /* We didn't change the current path point */
1065 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 }
Al Viro8aef1882011-06-16 15:10:06 +01001067
1068 if (need_mntput && path->mnt == mnt)
1069 mntput(path->mnt);
1070 if (ret == -EISDIR)
1071 ret = 0;
Al Viroa3fbbde2011-11-07 21:21:26 +00001072 return ret < 0 ? ret : need_mntput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073}
1074
David Howellscc53ce52011-01-14 18:45:26 +00001075int follow_down_one(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
1077 struct vfsmount *mounted;
1078
Al Viro1c755af2009-04-18 14:06:57 -04001079 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -04001081 dput(path->dentry);
1082 mntput(path->mnt);
1083 path->mnt = mounted;
1084 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 return 1;
1086 }
1087 return 0;
1088}
1089
Ian Kent62a73752011-03-25 01:51:02 +08001090static inline bool managed_dentry_might_block(struct dentry *dentry)
1091{
1092 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
1093 dentry->d_op->d_manage(dentry, true) < 0);
1094}
1095
David Howells9875cf82011-01-14 18:45:21 +00001096/*
Al Viro287548e2011-05-27 06:50:06 -04001097 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1098 * we meet a managed dentry that would need blocking.
David Howells9875cf82011-01-14 18:45:21 +00001099 */
1100static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
Al Viro287548e2011-05-27 06:50:06 -04001101 struct inode **inode)
David Howells9875cf82011-01-14 18:45:21 +00001102{
Ian Kent62a73752011-03-25 01:51:02 +08001103 for (;;) {
Al Viroc7105362011-11-24 18:22:03 -05001104 struct mount *mounted;
Ian Kent62a73752011-03-25 01:51:02 +08001105 /*
1106 * Don't forget we might have a non-mountpoint managed dentry
1107 * that wants to block transit.
1108 */
Al Viro287548e2011-05-27 06:50:06 -04001109 if (unlikely(managed_dentry_might_block(path->dentry)))
David Howellsab909112011-01-14 18:46:51 +00001110 return false;
Ian Kent62a73752011-03-25 01:51:02 +08001111
1112 if (!d_mountpoint(path->dentry))
Al Virob37199e2014-03-20 15:18:22 -04001113 return true;
Ian Kent62a73752011-03-25 01:51:02 +08001114
Al Viro474279d2013-10-01 16:11:26 -04001115 mounted = __lookup_mnt(path->mnt, path->dentry);
David Howells9875cf82011-01-14 18:45:21 +00001116 if (!mounted)
1117 break;
Al Viroc7105362011-11-24 18:22:03 -05001118 path->mnt = &mounted->mnt;
1119 path->dentry = mounted->mnt.mnt_root;
Al Viroa3fbbde2011-11-07 21:21:26 +00001120 nd->flags |= LOOKUP_JUMPED;
David Howells9875cf82011-01-14 18:45:21 +00001121 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
Linus Torvalds59430262011-07-18 15:43:29 -07001122 /*
1123 * Update the inode too. We don't need to re-check the
1124 * dentry sequence number here after this d_inode read,
1125 * because a mount-point is always pinned.
1126 */
1127 *inode = path->dentry->d_inode;
David Howells9875cf82011-01-14 18:45:21 +00001128 }
Al Virob37199e2014-03-20 15:18:22 -04001129 return read_seqretry(&mount_lock, nd->m_seq);
Al Viro287548e2011-05-27 06:50:06 -04001130}
1131
Nick Piggin31e6b012011-01-07 17:49:52 +11001132static int follow_dotdot_rcu(struct nameidata *nd)
1133{
Al Viroaed47132014-09-13 21:55:46 -04001134 if (!nd->root.mnt)
1135 set_root_rcu(nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11001136
David Howells9875cf82011-01-14 18:45:21 +00001137 while (1) {
Nick Piggin31e6b012011-01-07 17:49:52 +11001138 if (nd->path.dentry == nd->root.dentry &&
1139 nd->path.mnt == nd->root.mnt) {
1140 break;
1141 }
1142 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1143 struct dentry *old = nd->path.dentry;
1144 struct dentry *parent = old->d_parent;
1145 unsigned seq;
1146
1147 seq = read_seqcount_begin(&parent->d_seq);
1148 if (read_seqcount_retry(&old->d_seq, nd->seq))
Al Viroef7562d2011-03-04 14:35:59 -05001149 goto failed;
Nick Piggin31e6b012011-01-07 17:49:52 +11001150 nd->path.dentry = parent;
1151 nd->seq = seq;
1152 break;
1153 }
1154 if (!follow_up_rcu(&nd->path))
1155 break;
1156 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Nick Piggin31e6b012011-01-07 17:49:52 +11001157 }
Al Virob37199e2014-03-20 15:18:22 -04001158 while (d_mountpoint(nd->path.dentry)) {
1159 struct mount *mounted;
1160 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry);
1161 if (!mounted)
1162 break;
1163 nd->path.mnt = &mounted->mnt;
1164 nd->path.dentry = mounted->mnt.mnt_root;
1165 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
1166 if (!read_seqretry(&mount_lock, nd->m_seq))
1167 goto failed;
1168 }
Al Virodea39372011-05-27 06:53:39 -04001169 nd->inode = nd->path.dentry->d_inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001170 return 0;
Al Viroef7562d2011-03-04 14:35:59 -05001171
1172failed:
1173 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001174 if (!(nd->flags & LOOKUP_ROOT))
1175 nd->root.mnt = NULL;
Al Viro8b61e742013-11-08 12:45:01 -05001176 rcu_read_unlock();
Al Viroef7562d2011-03-04 14:35:59 -05001177 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001178}
1179
David Howells9875cf82011-01-14 18:45:21 +00001180/*
David Howellscc53ce52011-01-14 18:45:26 +00001181 * Follow down to the covering mount currently visible to userspace. At each
1182 * point, the filesystem owning that dentry may be queried as to whether the
1183 * caller is permitted to proceed or not.
David Howellscc53ce52011-01-14 18:45:26 +00001184 */
Al Viro7cc90cc2011-03-18 09:04:20 -04001185int follow_down(struct path *path)
David Howellscc53ce52011-01-14 18:45:26 +00001186{
1187 unsigned managed;
1188 int ret;
1189
1190 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1191 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1192 /* Allow the filesystem to manage the transit without i_mutex
1193 * being held.
1194 *
1195 * We indicate to the filesystem if someone is trying to mount
1196 * something here. This gives autofs the chance to deny anyone
1197 * other than its daemon the right to mount on its
1198 * superstructure.
1199 *
1200 * The filesystem may sleep at this point.
1201 */
1202 if (managed & DCACHE_MANAGE_TRANSIT) {
1203 BUG_ON(!path->dentry->d_op);
1204 BUG_ON(!path->dentry->d_op->d_manage);
David Howellsab909112011-01-14 18:46:51 +00001205 ret = path->dentry->d_op->d_manage(
Al Viro1aed3e42011-03-18 09:09:02 -04001206 path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +00001207 if (ret < 0)
1208 return ret == -EISDIR ? 0 : ret;
1209 }
1210
1211 /* Transit to a mounted filesystem. */
1212 if (managed & DCACHE_MOUNTED) {
1213 struct vfsmount *mounted = lookup_mnt(path);
1214 if (!mounted)
1215 break;
1216 dput(path->dentry);
1217 mntput(path->mnt);
1218 path->mnt = mounted;
1219 path->dentry = dget(mounted->mnt_root);
1220 continue;
1221 }
1222
1223 /* Don't handle automount points here */
1224 break;
1225 }
1226 return 0;
1227}
1228
1229/*
David Howells9875cf82011-01-14 18:45:21 +00001230 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1231 */
1232static void follow_mount(struct path *path)
1233{
1234 while (d_mountpoint(path->dentry)) {
1235 struct vfsmount *mounted = lookup_mnt(path);
1236 if (!mounted)
1237 break;
1238 dput(path->dentry);
1239 mntput(path->mnt);
1240 path->mnt = mounted;
1241 path->dentry = dget(mounted->mnt_root);
1242 }
1243}
1244
Nick Piggin31e6b012011-01-07 17:49:52 +11001245static void follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
Al Viroaed47132014-09-13 21:55:46 -04001247 if (!nd->root.mnt)
1248 set_root(nd);
Andreas Mohre518ddb2006-09-29 02:01:22 -07001249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 while(1) {
Jan Blunck4ac91372008-02-14 19:34:32 -08001251 struct dentry *old = nd->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Al Viro2a737872009-04-07 11:49:53 -04001253 if (nd->path.dentry == nd->root.dentry &&
1254 nd->path.mnt == nd->root.mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 break;
1256 }
Jan Blunck4ac91372008-02-14 19:34:32 -08001257 if (nd->path.dentry != nd->path.mnt->mnt_root) {
Al Viro3088dd72010-01-30 15:47:29 -05001258 /* rare case of legitimate dget_parent()... */
1259 nd->path.dentry = dget_parent(nd->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 dput(old);
1261 break;
1262 }
Al Viro3088dd72010-01-30 15:47:29 -05001263 if (!follow_up(&nd->path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
Al Viro79ed0222009-04-18 13:59:41 -04001266 follow_mount(&nd->path);
Nick Piggin31e6b012011-01-07 17:49:52 +11001267 nd->inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268}
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270/*
Miklos Szeredibad61182012-03-26 12:54:24 +02001271 * This looks up the name in dcache, possibly revalidates the old dentry and
1272 * allocates a new one if not found or not valid. In the need_lookup argument
1273 * returns whether i_op->lookup is necessary.
1274 *
1275 * dir->d_inode->i_mutex must be held
Nick Pigginbaa03892010-08-18 04:37:31 +10001276 */
Miklos Szeredibad61182012-03-26 12:54:24 +02001277static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
Al Viro201f9562012-06-22 12:42:10 +04001278 unsigned int flags, bool *need_lookup)
Nick Pigginbaa03892010-08-18 04:37:31 +10001279{
Nick Pigginbaa03892010-08-18 04:37:31 +10001280 struct dentry *dentry;
Miklos Szeredibad61182012-03-26 12:54:24 +02001281 int error;
Nick Pigginbaa03892010-08-18 04:37:31 +10001282
Miklos Szeredibad61182012-03-26 12:54:24 +02001283 *need_lookup = false;
1284 dentry = d_lookup(dir, name);
1285 if (dentry) {
Jeff Layton39e3c952012-11-28 11:30:53 -05001286 if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
Al Viro201f9562012-06-22 12:42:10 +04001287 error = d_revalidate(dentry, flags);
Miklos Szeredibad61182012-03-26 12:54:24 +02001288 if (unlikely(error <= 0)) {
1289 if (error < 0) {
1290 dput(dentry);
1291 return ERR_PTR(error);
1292 } else if (!d_invalidate(dentry)) {
1293 dput(dentry);
1294 dentry = NULL;
1295 }
1296 }
1297 }
1298 }
Nick Pigginbaa03892010-08-18 04:37:31 +10001299
Miklos Szeredibad61182012-03-26 12:54:24 +02001300 if (!dentry) {
1301 dentry = d_alloc(dir, name);
1302 if (unlikely(!dentry))
1303 return ERR_PTR(-ENOMEM);
Nick Pigginbaa03892010-08-18 04:37:31 +10001304
Miklos Szeredibad61182012-03-26 12:54:24 +02001305 *need_lookup = true;
Nick Pigginbaa03892010-08-18 04:37:31 +10001306 }
1307 return dentry;
1308}
1309
1310/*
J. Bruce Fields13a2c3b2013-10-23 16:09:16 -04001311 * Call i_op->lookup on the dentry. The dentry must be negative and
1312 * unhashed.
Miklos Szeredibad61182012-03-26 12:54:24 +02001313 *
1314 * dir->d_inode->i_mutex must be held
Josef Bacik44396f42011-05-31 11:58:49 -04001315 */
Miklos Szeredibad61182012-03-26 12:54:24 +02001316static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
Al Viro72bd8662012-06-10 17:17:17 -04001317 unsigned int flags)
Josef Bacik44396f42011-05-31 11:58:49 -04001318{
Josef Bacik44396f42011-05-31 11:58:49 -04001319 struct dentry *old;
1320
1321 /* Don't create child dentry for a dead directory. */
Miklos Szeredibad61182012-03-26 12:54:24 +02001322 if (unlikely(IS_DEADDIR(dir))) {
Miklos Szeredie188dc02012-02-03 14:25:18 +01001323 dput(dentry);
Josef Bacik44396f42011-05-31 11:58:49 -04001324 return ERR_PTR(-ENOENT);
Miklos Szeredie188dc02012-02-03 14:25:18 +01001325 }
Josef Bacik44396f42011-05-31 11:58:49 -04001326
Al Viro72bd8662012-06-10 17:17:17 -04001327 old = dir->i_op->lookup(dir, dentry, flags);
Josef Bacik44396f42011-05-31 11:58:49 -04001328 if (unlikely(old)) {
1329 dput(dentry);
1330 dentry = old;
1331 }
1332 return dentry;
1333}
1334
Al Viroa3255542012-03-30 14:41:51 -04001335static struct dentry *__lookup_hash(struct qstr *name,
Al Viro72bd8662012-06-10 17:17:17 -04001336 struct dentry *base, unsigned int flags)
Al Viroa3255542012-03-30 14:41:51 -04001337{
Miklos Szeredibad61182012-03-26 12:54:24 +02001338 bool need_lookup;
Al Viroa3255542012-03-30 14:41:51 -04001339 struct dentry *dentry;
1340
Al Viro72bd8662012-06-10 17:17:17 -04001341 dentry = lookup_dcache(name, base, flags, &need_lookup);
Miklos Szeredibad61182012-03-26 12:54:24 +02001342 if (!need_lookup)
1343 return dentry;
Al Viroa3255542012-03-30 14:41:51 -04001344
Al Viro72bd8662012-06-10 17:17:17 -04001345 return lookup_real(base->d_inode, dentry, flags);
Al Viroa3255542012-03-30 14:41:51 -04001346}
1347
Josef Bacik44396f42011-05-31 11:58:49 -04001348/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 * It's more convoluted than I'd like it to be, but... it's still fairly
1350 * small and for now I'd prefer to have fast path as straight as possible.
1351 * It _is_ time-critical.
1352 */
Al Viroe97cdc82013-01-24 18:16:00 -05001353static int lookup_fast(struct nameidata *nd,
Miklos Szeredi697f5142012-05-21 17:30:05 +02001354 struct path *path, struct inode **inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355{
Jan Blunck4ac91372008-02-14 19:34:32 -08001356 struct vfsmount *mnt = nd->path.mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +11001357 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro5a18fff2011-03-11 04:44:53 -05001358 int need_reval = 1;
1359 int status = 1;
David Howells9875cf82011-01-14 18:45:21 +00001360 int err;
1361
Al Viro3cac2602009-08-13 18:27:43 +04001362 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001363 * Rename seqlock is not required here because in the off chance
1364 * of a false negative due to a concurrent rename, we're going to
1365 * do the non-racy lookup, below.
1366 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001367 if (nd->flags & LOOKUP_RCU) {
1368 unsigned seq;
Linus Torvaldsda53be12013-05-21 15:22:44 -07001369 dentry = __d_lookup_rcu(parent, &nd->last, &seq);
Al Viro5a18fff2011-03-11 04:44:53 -05001370 if (!dentry)
1371 goto unlazy;
1372
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001373 /*
1374 * This sequence count validates that the inode matches
1375 * the dentry name information from lookup.
1376 */
1377 *inode = dentry->d_inode;
1378 if (read_seqcount_retry(&dentry->d_seq, seq))
1379 return -ECHILD;
1380
1381 /*
1382 * This sequence count validates that the parent had no
1383 * changes while we did the lookup of the dentry above.
1384 *
1385 * The memory barrier in read_seqcount_begin of child is
1386 * enough, we can use __read_seqcount_retry here.
1387 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001388 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1389 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001390 nd->seq = seq;
Al Viro5a18fff2011-03-11 04:44:53 -05001391
Al Viro24643082011-02-15 01:26:22 -05001392 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
Al Viro4ce16ef32012-06-10 16:10:59 -04001393 status = d_revalidate(dentry, nd->flags);
Al Viro5a18fff2011-03-11 04:44:53 -05001394 if (unlikely(status <= 0)) {
1395 if (status != -ECHILD)
1396 need_reval = 0;
1397 goto unlazy;
1398 }
Al Viro24643082011-02-15 01:26:22 -05001399 }
Nick Piggin31e6b012011-01-07 17:49:52 +11001400 path->mnt = mnt;
1401 path->dentry = dentry;
Al Virod6e9bd22011-05-27 07:03:15 -04001402 if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1403 goto unlazy;
1404 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1405 goto unlazy;
1406 return 0;
Al Viro5a18fff2011-03-11 04:44:53 -05001407unlazy:
Al Viro19660af2011-03-25 10:32:48 -04001408 if (unlazy_walk(nd, dentry))
1409 return -ECHILD;
Al Viro5a18fff2011-03-11 04:44:53 -05001410 } else {
Al Viroe97cdc82013-01-24 18:16:00 -05001411 dentry = __d_lookup(parent, &nd->last);
Nick Piggin31e6b012011-01-07 17:49:52 +11001412 }
Al Viro5a18fff2011-03-11 04:44:53 -05001413
Al Viro81e6f522012-03-30 14:48:04 -04001414 if (unlikely(!dentry))
1415 goto need_lookup;
Al Viro5a18fff2011-03-11 04:44:53 -05001416
Al Viro5a18fff2011-03-11 04:44:53 -05001417 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
Al Viro4ce16ef32012-06-10 16:10:59 -04001418 status = d_revalidate(dentry, nd->flags);
Al Viro5a18fff2011-03-11 04:44:53 -05001419 if (unlikely(status <= 0)) {
1420 if (status < 0) {
1421 dput(dentry);
1422 return status;
1423 }
1424 if (!d_invalidate(dentry)) {
1425 dput(dentry);
Al Viro81e6f522012-03-30 14:48:04 -04001426 goto need_lookup;
Al Viro5a18fff2011-03-11 04:44:53 -05001427 }
1428 }
Miklos Szeredi697f5142012-05-21 17:30:05 +02001429
David Howells9875cf82011-01-14 18:45:21 +00001430 path->mnt = mnt;
1431 path->dentry = dentry;
1432 err = follow_managed(path, nd->flags);
Ian Kent89312212011-01-18 12:06:10 +08001433 if (unlikely(err < 0)) {
1434 path_put_conditional(path, nd);
David Howells9875cf82011-01-14 18:45:21 +00001435 return err;
Ian Kent89312212011-01-18 12:06:10 +08001436 }
Al Viroa3fbbde2011-11-07 21:21:26 +00001437 if (err)
1438 nd->flags |= LOOKUP_JUMPED;
David Howells9875cf82011-01-14 18:45:21 +00001439 *inode = path->dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 return 0;
Al Viro81e6f522012-03-30 14:48:04 -04001441
1442need_lookup:
Miklos Szeredi697f5142012-05-21 17:30:05 +02001443 return 1;
1444}
1445
1446/* Fast lookup failed, do it the slow way */
Al Virocc2a5272013-01-24 18:19:49 -05001447static int lookup_slow(struct nameidata *nd, struct path *path)
Miklos Szeredi697f5142012-05-21 17:30:05 +02001448{
1449 struct dentry *dentry, *parent;
1450 int err;
1451
1452 parent = nd->path.dentry;
Al Viro81e6f522012-03-30 14:48:04 -04001453 BUG_ON(nd->inode != parent->d_inode);
1454
1455 mutex_lock(&parent->d_inode->i_mutex);
Al Virocc2a5272013-01-24 18:19:49 -05001456 dentry = __lookup_hash(&nd->last, parent, nd->flags);
Al Viro81e6f522012-03-30 14:48:04 -04001457 mutex_unlock(&parent->d_inode->i_mutex);
1458 if (IS_ERR(dentry))
1459 return PTR_ERR(dentry);
Miklos Szeredi697f5142012-05-21 17:30:05 +02001460 path->mnt = nd->path.mnt;
1461 path->dentry = dentry;
1462 err = follow_managed(path, nd->flags);
1463 if (unlikely(err < 0)) {
1464 path_put_conditional(path, nd);
1465 return err;
1466 }
1467 if (err)
1468 nd->flags |= LOOKUP_JUMPED;
1469 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470}
1471
Al Viro52094c82011-02-21 21:34:47 -05001472static inline int may_lookup(struct nameidata *nd)
1473{
1474 if (nd->flags & LOOKUP_RCU) {
Al Viro4ad5abb2011-06-20 19:57:03 -04001475 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
Al Viro52094c82011-02-21 21:34:47 -05001476 if (err != -ECHILD)
1477 return err;
Al Viro19660af2011-03-25 10:32:48 -04001478 if (unlazy_walk(nd, NULL))
Al Viro52094c82011-02-21 21:34:47 -05001479 return -ECHILD;
1480 }
Al Viro4ad5abb2011-06-20 19:57:03 -04001481 return inode_permission(nd->inode, MAY_EXEC);
Al Viro52094c82011-02-21 21:34:47 -05001482}
1483
Al Viro9856fa12011-03-04 14:22:06 -05001484static inline int handle_dots(struct nameidata *nd, int type)
1485{
1486 if (type == LAST_DOTDOT) {
1487 if (nd->flags & LOOKUP_RCU) {
1488 if (follow_dotdot_rcu(nd))
1489 return -ECHILD;
1490 } else
1491 follow_dotdot(nd);
1492 }
1493 return 0;
1494}
1495
Al Viro951361f2011-03-04 14:44:37 -05001496static void terminate_walk(struct nameidata *nd)
1497{
1498 if (!(nd->flags & LOOKUP_RCU)) {
1499 path_put(&nd->path);
1500 } else {
1501 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001502 if (!(nd->flags & LOOKUP_ROOT))
1503 nd->root.mnt = NULL;
Al Viro8b61e742013-11-08 12:45:01 -05001504 rcu_read_unlock();
Al Viro951361f2011-03-04 14:44:37 -05001505 }
1506}
1507
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001508/*
1509 * Do we need to follow links? We _really_ want to be able
1510 * to do this check without having to look at inode->i_op,
1511 * so we keep a cache of "no, this doesn't need follow_link"
1512 * for the common case.
1513 */
David Howellsb18825a2013-09-12 19:22:53 +01001514static inline int should_follow_link(struct dentry *dentry, int follow)
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001515{
David Howellsb18825a2013-09-12 19:22:53 +01001516 return unlikely(d_is_symlink(dentry)) ? follow : 0;
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001517}
1518
Al Viroce57dfc2011-03-13 19:58:58 -04001519static inline int walk_component(struct nameidata *nd, struct path *path,
Al Viro21b9b072013-01-24 18:10:25 -05001520 int follow)
Al Viroce57dfc2011-03-13 19:58:58 -04001521{
1522 struct inode *inode;
1523 int err;
1524 /*
1525 * "." and ".." are special - ".." especially so because it has
1526 * to be able to know about the current root directory and
1527 * parent relationships.
1528 */
Al Viro21b9b072013-01-24 18:10:25 -05001529 if (unlikely(nd->last_type != LAST_NORM))
1530 return handle_dots(nd, nd->last_type);
Al Viroe97cdc82013-01-24 18:16:00 -05001531 err = lookup_fast(nd, path, &inode);
Al Viroce57dfc2011-03-13 19:58:58 -04001532 if (unlikely(err)) {
Miklos Szeredi697f5142012-05-21 17:30:05 +02001533 if (err < 0)
1534 goto out_err;
1535
Al Virocc2a5272013-01-24 18:19:49 -05001536 err = lookup_slow(nd, path);
Miklos Szeredi697f5142012-05-21 17:30:05 +02001537 if (err < 0)
1538 goto out_err;
1539
1540 inode = path->dentry->d_inode;
Al Viroce57dfc2011-03-13 19:58:58 -04001541 }
Miklos Szeredi697f5142012-05-21 17:30:05 +02001542 err = -ENOENT;
Al Virof0754e52014-04-19 12:30:58 -04001543 if (!inode || d_is_negative(path->dentry))
Miklos Szeredi697f5142012-05-21 17:30:05 +02001544 goto out_path_put;
1545
David Howellsb18825a2013-09-12 19:22:53 +01001546 if (should_follow_link(path->dentry, follow)) {
Al Viro19660af2011-03-25 10:32:48 -04001547 if (nd->flags & LOOKUP_RCU) {
1548 if (unlikely(unlazy_walk(nd, path->dentry))) {
Miklos Szeredi697f5142012-05-21 17:30:05 +02001549 err = -ECHILD;
1550 goto out_err;
Al Viro19660af2011-03-25 10:32:48 -04001551 }
1552 }
Al Viroce57dfc2011-03-13 19:58:58 -04001553 BUG_ON(inode != path->dentry->d_inode);
1554 return 1;
1555 }
1556 path_to_nameidata(path, nd);
1557 nd->inode = inode;
1558 return 0;
Miklos Szeredi697f5142012-05-21 17:30:05 +02001559
1560out_path_put:
1561 path_to_nameidata(path, nd);
1562out_err:
1563 terminate_walk(nd);
1564 return err;
Al Viroce57dfc2011-03-13 19:58:58 -04001565}
1566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567/*
Al Virob3563792011-03-14 21:54:55 -04001568 * This limits recursive symlink follows to 8, while
1569 * limiting consecutive symlinks to 40.
1570 *
1571 * Without that kind of total limit, nasty chains of consecutive
1572 * symlinks can cause almost arbitrarily long lookups.
1573 */
1574static inline int nested_symlink(struct path *path, struct nameidata *nd)
1575{
1576 int res;
1577
Al Virob3563792011-03-14 21:54:55 -04001578 if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1579 path_put_conditional(path, nd);
1580 path_put(&nd->path);
1581 return -ELOOP;
1582 }
Erez Zadok1a4022f2011-05-21 01:19:59 -04001583 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
Al Virob3563792011-03-14 21:54:55 -04001584
1585 nd->depth++;
1586 current->link_count++;
1587
1588 do {
1589 struct path link = *path;
1590 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04001591
1592 res = follow_link(&link, nd, &cookie);
Al Viro6d7b5aa2012-06-10 04:15:17 -04001593 if (res)
1594 break;
Al Viro21b9b072013-01-24 18:10:25 -05001595 res = walk_component(nd, path, LOOKUP_FOLLOW);
Al Viro574197e2011-03-14 22:20:34 -04001596 put_link(nd, &link, cookie);
Al Virob3563792011-03-14 21:54:55 -04001597 } while (res > 0);
1598
1599 current->link_count--;
1600 nd->depth--;
1601 return res;
1602}
1603
1604/*
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001605 * We can do the critical dentry name comparison and hashing
1606 * operations one word at a time, but we are limited to:
1607 *
1608 * - Architectures with fast unaligned word accesses. We could
1609 * do a "get_unaligned()" if this helps and is sufficiently
1610 * fast.
1611 *
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001612 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1613 * do not trap on the (extremely unlikely) case of a page
1614 * crossing operation.
1615 *
1616 * - Furthermore, we need an efficient 64-bit compile for the
1617 * 64-bit case in order to generate the "number of bytes in
1618 * the final mask". Again, that could be replaced with a
1619 * efficient population count instruction or similar.
1620 */
1621#ifdef CONFIG_DCACHE_WORD_ACCESS
1622
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001623#include <asm/word-at-a-time.h>
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001624
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001625#ifdef CONFIG_64BIT
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001626
1627static inline unsigned int fold_hash(unsigned long hash)
1628{
Linus Torvaldsa8be8af2014-09-13 11:30:10 -07001629 return hash_64(hash, 32);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001630}
1631
1632#else /* 32-bit case */
1633
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001634#define fold_hash(x) (x)
1635
1636#endif
1637
1638unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1639{
1640 unsigned long a, mask;
1641 unsigned long hash = 0;
1642
1643 for (;;) {
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001644 a = load_unaligned_zeropad(name);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001645 if (len < sizeof(unsigned long))
1646 break;
1647 hash += a;
Al Virof132c5b2012-03-22 21:59:52 +00001648 hash *= 9;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001649 name += sizeof(unsigned long);
1650 len -= sizeof(unsigned long);
1651 if (!len)
1652 goto done;
1653 }
Will Deacona5c21dc2013-12-12 17:40:21 +00001654 mask = bytemask_from_count(len);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001655 hash += mask & a;
1656done:
1657 return fold_hash(hash);
1658}
1659EXPORT_SYMBOL(full_name_hash);
1660
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001661/*
1662 * Calculate the length and hash of the path component, and
1663 * return the length of the component;
1664 */
1665static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1666{
Linus Torvalds36126f82012-05-26 10:43:17 -07001667 unsigned long a, b, adata, bdata, mask, hash, len;
1668 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001669
1670 hash = a = 0;
1671 len = -sizeof(unsigned long);
1672 do {
1673 hash = (hash + a) * 9;
1674 len += sizeof(unsigned long);
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001675 a = load_unaligned_zeropad(name+len);
Linus Torvalds36126f82012-05-26 10:43:17 -07001676 b = a ^ REPEAT_BYTE('/');
1677 } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001678
Linus Torvalds36126f82012-05-26 10:43:17 -07001679 adata = prep_zero_mask(a, adata, &constants);
1680 bdata = prep_zero_mask(b, bdata, &constants);
1681
1682 mask = create_zero_mask(adata | bdata);
1683
1684 hash += a & zero_bytemask(mask);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001685 *hashp = fold_hash(hash);
1686
Linus Torvalds36126f82012-05-26 10:43:17 -07001687 return len + find_zero(mask);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001688}
1689
1690#else
1691
Linus Torvalds0145acc2012-03-02 14:32:59 -08001692unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1693{
1694 unsigned long hash = init_name_hash();
1695 while (len--)
1696 hash = partial_name_hash(*name++, hash);
1697 return end_name_hash(hash);
1698}
Linus Torvaldsae942ae2012-03-02 19:40:57 -08001699EXPORT_SYMBOL(full_name_hash);
Linus Torvalds0145acc2012-03-02 14:32:59 -08001700
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001701/*
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001702 * We know there's a real path component here of at least
1703 * one character.
1704 */
1705static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1706{
1707 unsigned long hash = init_name_hash();
1708 unsigned long len = 0, c;
1709
1710 c = (unsigned char)*name;
1711 do {
1712 len++;
1713 hash = partial_name_hash(c, hash);
1714 c = (unsigned char)name[len];
1715 } while (c && c != '/');
1716 *hashp = end_name_hash(hash);
1717 return len;
1718}
1719
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001720#endif
1721
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001722/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01001724 * This is the basic name resolution function, turning a pathname into
1725 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01001727 * Returns 0 and nd will have valid dentry and mnt on success.
1728 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 */
Al Viro6de88d72009-08-09 01:41:57 +04001730static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731{
1732 struct path next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
1735 while (*name=='/')
1736 name++;
1737 if (!*name)
Al Viro086e1832011-02-22 20:56:27 -05001738 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 /* At this point we know we have a real path component. */
1741 for(;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 struct qstr this;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001743 long len;
Al Virofe479a52011-02-22 15:10:03 -05001744 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
Al Viro52094c82011-02-21 21:34:47 -05001746 err = may_lookup(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 if (err)
1748 break;
1749
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001750 len = hash_name(name, &this.hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 this.name = name;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001752 this.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Al Virofe479a52011-02-22 15:10:03 -05001754 type = LAST_NORM;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001755 if (name[0] == '.') switch (len) {
Al Virofe479a52011-02-22 15:10:03 -05001756 case 2:
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001757 if (name[1] == '.') {
Al Virofe479a52011-02-22 15:10:03 -05001758 type = LAST_DOTDOT;
Al Viro16c2cd72011-02-22 15:50:10 -05001759 nd->flags |= LOOKUP_JUMPED;
1760 }
Al Virofe479a52011-02-22 15:10:03 -05001761 break;
1762 case 1:
1763 type = LAST_DOT;
1764 }
Al Viro5a202bc2011-03-08 14:17:44 -05001765 if (likely(type == LAST_NORM)) {
1766 struct dentry *parent = nd->path.dentry;
Al Viro16c2cd72011-02-22 15:50:10 -05001767 nd->flags &= ~LOOKUP_JUMPED;
Al Viro5a202bc2011-03-08 14:17:44 -05001768 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
Linus Torvaldsda53be12013-05-21 15:22:44 -07001769 err = parent->d_op->d_hash(parent, &this);
Al Viro5a202bc2011-03-08 14:17:44 -05001770 if (err < 0)
1771 break;
1772 }
1773 }
Al Virofe479a52011-02-22 15:10:03 -05001774
Al Viro5f4a6a62013-01-24 18:04:22 -05001775 nd->last = this;
1776 nd->last_type = type;
1777
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001778 if (!name[len])
Al Viro5f4a6a62013-01-24 18:04:22 -05001779 return 0;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001780 /*
1781 * If it wasn't NUL, we know it was '/'. Skip that
1782 * slash, and continue until no more slashes.
1783 */
1784 do {
1785 len++;
1786 } while (unlikely(name[len] == '/'));
1787 if (!name[len])
Al Viro5f4a6a62013-01-24 18:04:22 -05001788 return 0;
1789
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001790 name += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Al Viro21b9b072013-01-24 18:10:25 -05001792 err = walk_component(nd, &next, LOOKUP_FOLLOW);
Al Viroce57dfc2011-03-13 19:58:58 -04001793 if (err < 0)
1794 return err;
Al Virofe479a52011-02-22 15:10:03 -05001795
Al Viroce57dfc2011-03-13 19:58:58 -04001796 if (err) {
Al Virob3563792011-03-14 21:54:55 -04001797 err = nested_symlink(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 if (err)
Al Viroa7472ba2011-03-04 14:39:30 -05001799 return err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001800 }
Miklos Szeredidb8bf542014-04-01 17:08:41 +02001801 if (!d_can_lookup(nd->path.dentry)) {
Al Viro5f4a6a62013-01-24 18:04:22 -05001802 err = -ENOTDIR;
1803 break;
1804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 }
Al Viro951361f2011-03-04 14:44:37 -05001806 terminate_walk(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 return err;
1808}
1809
Al Viro70e9b352011-03-05 21:12:22 -05001810static int path_init(int dfd, const char *name, unsigned int flags,
1811 struct nameidata *nd, struct file **fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812{
Prasanna Medaea3834d2005-04-29 16:00:17 +01001813 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
1815 nd->last_type = LAST_ROOT; /* if there are only slashes... */
Al Viro16c2cd72011-02-22 15:50:10 -05001816 nd->flags = flags | LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 nd->depth = 0;
Al Viro5b6ca022011-03-09 23:04:47 -05001818 if (flags & LOOKUP_ROOT) {
David Howellsb18825a2013-09-12 19:22:53 +01001819 struct dentry *root = nd->root.dentry;
1820 struct inode *inode = root->d_inode;
Al Viro73d049a2011-03-11 12:08:24 -05001821 if (*name) {
Miklos Szeredidb8bf542014-04-01 17:08:41 +02001822 if (!d_can_lookup(root))
Al Viro73d049a2011-03-11 12:08:24 -05001823 return -ENOTDIR;
1824 retval = inode_permission(inode, MAY_EXEC);
1825 if (retval)
1826 return retval;
1827 }
Al Viro5b6ca022011-03-09 23:04:47 -05001828 nd->path = nd->root;
1829 nd->inode = inode;
1830 if (flags & LOOKUP_RCU) {
Al Viro8b61e742013-11-08 12:45:01 -05001831 rcu_read_lock();
Al Viro5b6ca022011-03-09 23:04:47 -05001832 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro48a066e2013-09-29 22:06:07 -04001833 nd->m_seq = read_seqbegin(&mount_lock);
Al Viro5b6ca022011-03-09 23:04:47 -05001834 } else {
1835 path_get(&nd->path);
1836 }
1837 return 0;
1838 }
1839
Al Viro2a737872009-04-07 11:49:53 -04001840 nd->root.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
Al Viro48a066e2013-09-29 22:06:07 -04001842 nd->m_seq = read_seqbegin(&mount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 if (*name=='/') {
Al Viroe41f7d42011-02-22 14:02:58 -05001844 if (flags & LOOKUP_RCU) {
Al Viro8b61e742013-11-08 12:45:01 -05001845 rcu_read_lock();
Al Viroaed47132014-09-13 21:55:46 -04001846 nd->seq = set_root_rcu(nd);
Al Viroe41f7d42011-02-22 14:02:58 -05001847 } else {
1848 set_root(nd);
1849 path_get(&nd->root);
1850 }
Al Viro2a737872009-04-07 11:49:53 -04001851 nd->path = nd->root;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001852 } else if (dfd == AT_FDCWD) {
Al Viroe41f7d42011-02-22 14:02:58 -05001853 if (flags & LOOKUP_RCU) {
1854 struct fs_struct *fs = current->fs;
1855 unsigned seq;
1856
Al Viro8b61e742013-11-08 12:45:01 -05001857 rcu_read_lock();
Al Viroe41f7d42011-02-22 14:02:58 -05001858
1859 do {
1860 seq = read_seqcount_begin(&fs->seq);
1861 nd->path = fs->pwd;
1862 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1863 } while (read_seqcount_retry(&fs->seq, seq));
1864 } else {
1865 get_fs_pwd(current->fs, &nd->path);
1866 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001867 } else {
Jeff Layton582aa642012-12-11 08:56:16 -05001868 /* Caller must check execute permissions on the starting path component */
Al Viro2903ff02012-08-28 12:52:22 -04001869 struct fd f = fdget_raw(dfd);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001870 struct dentry *dentry;
1871
Al Viro2903ff02012-08-28 12:52:22 -04001872 if (!f.file)
1873 return -EBADF;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001874
Al Viro2903ff02012-08-28 12:52:22 -04001875 dentry = f.file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001876
Al Virof52e0c12011-03-14 18:56:51 -04001877 if (*name) {
Miklos Szeredidb8bf542014-04-01 17:08:41 +02001878 if (!d_can_lookup(dentry)) {
Al Viro2903ff02012-08-28 12:52:22 -04001879 fdput(f);
1880 return -ENOTDIR;
1881 }
Al Virof52e0c12011-03-14 18:56:51 -04001882 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001883
Al Viro2903ff02012-08-28 12:52:22 -04001884 nd->path = f.file->f_path;
Al Viroe41f7d42011-02-22 14:02:58 -05001885 if (flags & LOOKUP_RCU) {
Linus Torvalds9c225f22014-03-03 09:36:58 -08001886 if (f.flags & FDPUT_FPUT)
Al Viro2903ff02012-08-28 12:52:22 -04001887 *fp = f.file;
Al Viroe41f7d42011-02-22 14:02:58 -05001888 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro8b61e742013-11-08 12:45:01 -05001889 rcu_read_lock();
Al Viroe41f7d42011-02-22 14:02:58 -05001890 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001891 path_get(&nd->path);
1892 fdput(f);
Al Viroe41f7d42011-02-22 14:02:58 -05001893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 }
Al Viroe41f7d42011-02-22 14:02:58 -05001895
Nick Piggin31e6b012011-01-07 17:49:52 +11001896 nd->inode = nd->path.dentry->d_inode;
Al Viro9b4a9b12009-04-07 11:44:16 -04001897 return 0;
Al Viro9b4a9b12009-04-07 11:44:16 -04001898}
1899
Al Virobd92d7f2011-03-14 19:54:59 -04001900static inline int lookup_last(struct nameidata *nd, struct path *path)
1901{
1902 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1903 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1904
1905 nd->flags &= ~LOOKUP_PARENT;
Al Viro21b9b072013-01-24 18:10:25 -05001906 return walk_component(nd, path, nd->flags & LOOKUP_FOLLOW);
Al Virobd92d7f2011-03-14 19:54:59 -04001907}
1908
Al Viro9b4a9b12009-04-07 11:44:16 -04001909/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroee0827c2011-02-21 23:38:09 -05001910static int path_lookupat(int dfd, const char *name,
Al Viro9b4a9b12009-04-07 11:44:16 -04001911 unsigned int flags, struct nameidata *nd)
1912{
Al Viro70e9b352011-03-05 21:12:22 -05001913 struct file *base = NULL;
Al Virobd92d7f2011-03-14 19:54:59 -04001914 struct path path;
1915 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001916
1917 /*
1918 * Path walking is largely split up into 2 different synchronisation
1919 * schemes, rcu-walk and ref-walk (explained in
1920 * Documentation/filesystems/path-lookup.txt). These share much of the
1921 * path walk code, but some things particularly setup, cleanup, and
1922 * following mounts are sufficiently divergent that functions are
1923 * duplicated. Typically there is a function foo(), and its RCU
1924 * analogue, foo_rcu().
1925 *
1926 * -ECHILD is the error number of choice (just to avoid clashes) that
1927 * is returned if some aspect of an rcu-walk fails. Such an error must
1928 * be handled by restarting a traditional ref-walk (which will always
1929 * be able to complete).
1930 */
Al Virobd92d7f2011-03-14 19:54:59 -04001931 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
Al Viroee0827c2011-02-21 23:38:09 -05001932
Al Virobd92d7f2011-03-14 19:54:59 -04001933 if (unlikely(err))
1934 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001935
1936 current->total_link_count = 0;
Al Virobd92d7f2011-03-14 19:54:59 -04001937 err = link_path_walk(name, nd);
1938
1939 if (!err && !(flags & LOOKUP_PARENT)) {
Al Virobd92d7f2011-03-14 19:54:59 -04001940 err = lookup_last(nd, &path);
1941 while (err > 0) {
1942 void *cookie;
1943 struct path link = path;
Kees Cook800179c2012-07-25 17:29:07 -07001944 err = may_follow_link(&link, nd);
1945 if (unlikely(err))
1946 break;
Al Virobd92d7f2011-03-14 19:54:59 -04001947 nd->flags |= LOOKUP_PARENT;
Al Viro574197e2011-03-14 22:20:34 -04001948 err = follow_link(&link, nd, &cookie);
Al Viro6d7b5aa2012-06-10 04:15:17 -04001949 if (err)
1950 break;
1951 err = lookup_last(nd, &path);
Al Viro574197e2011-03-14 22:20:34 -04001952 put_link(nd, &link, cookie);
Al Virobd92d7f2011-03-14 19:54:59 -04001953 }
1954 }
Al Viroee0827c2011-02-21 23:38:09 -05001955
Al Viro9f1fafe2011-03-25 11:00:12 -04001956 if (!err)
1957 err = complete_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04001958
1959 if (!err && nd->flags & LOOKUP_DIRECTORY) {
Miklos Szeredidb8bf542014-04-01 17:08:41 +02001960 if (!d_can_lookup(nd->path.dentry)) {
Al Virobd92d7f2011-03-14 19:54:59 -04001961 path_put(&nd->path);
Al Virobd23a532011-03-23 09:56:30 -04001962 err = -ENOTDIR;
Al Virobd92d7f2011-03-14 19:54:59 -04001963 }
1964 }
Al Viro16c2cd72011-02-22 15:50:10 -05001965
Al Viro70e9b352011-03-05 21:12:22 -05001966 if (base)
1967 fput(base);
Al Viroee0827c2011-02-21 23:38:09 -05001968
Al Viro5b6ca022011-03-09 23:04:47 -05001969 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
Al Viro2a737872009-04-07 11:49:53 -04001970 path_put(&nd->root);
1971 nd->root.mnt = NULL;
1972 }
Al Virobd92d7f2011-03-14 19:54:59 -04001973 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001974}
Nick Piggin31e6b012011-01-07 17:49:52 +11001975
Jeff Layton873f1ee2012-10-10 15:25:29 -04001976static int filename_lookup(int dfd, struct filename *name,
1977 unsigned int flags, struct nameidata *nd)
1978{
1979 int retval = path_lookupat(dfd, name->name, flags | LOOKUP_RCU, nd);
1980 if (unlikely(retval == -ECHILD))
1981 retval = path_lookupat(dfd, name->name, flags, nd);
1982 if (unlikely(retval == -ESTALE))
1983 retval = path_lookupat(dfd, name->name,
1984 flags | LOOKUP_REVAL, nd);
1985
1986 if (likely(!retval))
Jeff Laytonadb5c242012-10-10 16:43:13 -04001987 audit_inode(name, nd->path.dentry, flags & LOOKUP_PARENT);
Jeff Layton873f1ee2012-10-10 15:25:29 -04001988 return retval;
1989}
1990
Al Viroee0827c2011-02-21 23:38:09 -05001991static int do_path_lookup(int dfd, const char *name,
1992 unsigned int flags, struct nameidata *nd)
1993{
Jeff Layton873f1ee2012-10-10 15:25:29 -04001994 struct filename filename = { .name = name };
Nick Piggin31e6b012011-01-07 17:49:52 +11001995
Jeff Layton873f1ee2012-10-10 15:25:29 -04001996 return filename_lookup(dfd, &filename, flags, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997}
1998
Al Viro79714f72012-06-15 03:01:42 +04001999/* does lookup, returns the object with parent locked */
2000struct dentry *kern_path_locked(const char *name, struct path *path)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002001{
Al Viro79714f72012-06-15 03:01:42 +04002002 struct nameidata nd;
2003 struct dentry *d;
2004 int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd);
2005 if (err)
2006 return ERR_PTR(err);
2007 if (nd.last_type != LAST_NORM) {
2008 path_put(&nd.path);
2009 return ERR_PTR(-EINVAL);
2010 }
2011 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Al Viro1e0ea002012-07-22 23:46:21 +04002012 d = __lookup_hash(&nd.last, nd.path.dentry, 0);
Al Viro79714f72012-06-15 03:01:42 +04002013 if (IS_ERR(d)) {
2014 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2015 path_put(&nd.path);
2016 return d;
2017 }
2018 *path = nd.path;
2019 return d;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002020}
2021
Al Virod1811462008-08-02 00:49:18 -04002022int kern_path(const char *name, unsigned int flags, struct path *path)
2023{
2024 struct nameidata nd;
2025 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
2026 if (!res)
2027 *path = nd.path;
2028 return res;
2029}
2030
Josef 'Jeff' Sipek16f182002007-07-19 01:48:18 -07002031/**
2032 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2033 * @dentry: pointer to dentry of the base directory
2034 * @mnt: pointer to vfs mount of the base directory
2035 * @name: pointer to file name
2036 * @flags: lookup flags
Al Viroe0a01242011-06-27 17:00:37 -04002037 * @path: pointer to struct path to fill
Josef 'Jeff' Sipek16f182002007-07-19 01:48:18 -07002038 */
2039int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2040 const char *name, unsigned int flags,
Al Viroe0a01242011-06-27 17:00:37 -04002041 struct path *path)
Josef 'Jeff' Sipek16f182002007-07-19 01:48:18 -07002042{
Al Viroe0a01242011-06-27 17:00:37 -04002043 struct nameidata nd;
2044 int err;
2045 nd.root.dentry = dentry;
2046 nd.root.mnt = mnt;
2047 BUG_ON(flags & LOOKUP_PARENT);
Al Viro5b6ca022011-03-09 23:04:47 -05002048 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
Al Viroe0a01242011-06-27 17:00:37 -04002049 err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
2050 if (!err)
2051 *path = nd.path;
2052 return err;
Josef 'Jeff' Sipek16f182002007-07-19 01:48:18 -07002053}
2054
James Morris057f6c02007-04-26 00:12:05 -07002055/*
2056 * Restricted form of lookup. Doesn't follow links, single-component only,
2057 * needs parent already locked. Doesn't follow mounts.
2058 * SMP-safe.
2059 */
Adrian Bunka244e162006-03-31 02:32:11 -08002060static struct dentry *lookup_hash(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061{
Al Viro72bd8662012-06-10 17:17:17 -04002062 return __lookup_hash(&nd->last, nd->path.dentry, nd->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063}
2064
Christoph Hellwigeead1912007-10-16 23:25:38 -07002065/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07002066 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07002067 * @name: pathname component to lookup
2068 * @base: base directory to lookup from
2069 * @len: maximum length @len should be interpreted to
2070 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07002071 * Note that this routine is purely a helper for filesystem usage and should
2072 * not be called by generic code. Also note that by using this function the
Christoph Hellwigeead1912007-10-16 23:25:38 -07002073 * nameidata argument is passed to the filesystem methods and a filesystem
2074 * using this helper needs to be prepared for that.
2075 */
James Morris057f6c02007-04-26 00:12:05 -07002076struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2077{
James Morris057f6c02007-04-26 00:12:05 -07002078 struct qstr this;
Al Viro6a96ba52011-03-07 23:49:20 -05002079 unsigned int c;
Miklos Szeredicda309d2012-03-26 12:54:21 +02002080 int err;
James Morris057f6c02007-04-26 00:12:05 -07002081
David Woodhouse2f9092e2009-04-20 23:18:37 +01002082 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
2083
Al Viro6a96ba52011-03-07 23:49:20 -05002084 this.name = name;
2085 this.len = len;
Linus Torvalds0145acc2012-03-02 14:32:59 -08002086 this.hash = full_name_hash(name, len);
Al Viro6a96ba52011-03-07 23:49:20 -05002087 if (!len)
2088 return ERR_PTR(-EACCES);
2089
Al Viro21d8a152012-11-29 22:17:21 -05002090 if (unlikely(name[0] == '.')) {
2091 if (len < 2 || (len == 2 && name[1] == '.'))
2092 return ERR_PTR(-EACCES);
2093 }
2094
Al Viro6a96ba52011-03-07 23:49:20 -05002095 while (len--) {
2096 c = *(const unsigned char *)name++;
2097 if (c == '/' || c == '\0')
2098 return ERR_PTR(-EACCES);
Al Viro6a96ba52011-03-07 23:49:20 -05002099 }
Al Viro5a202bc2011-03-08 14:17:44 -05002100 /*
2101 * See if the low-level filesystem might want
2102 * to use its own hash..
2103 */
2104 if (base->d_flags & DCACHE_OP_HASH) {
Linus Torvaldsda53be12013-05-21 15:22:44 -07002105 int err = base->d_op->d_hash(base, &this);
Al Viro5a202bc2011-03-08 14:17:44 -05002106 if (err < 0)
2107 return ERR_PTR(err);
2108 }
Christoph Hellwigeead1912007-10-16 23:25:38 -07002109
Miklos Szeredicda309d2012-03-26 12:54:21 +02002110 err = inode_permission(base->d_inode, MAY_EXEC);
2111 if (err)
2112 return ERR_PTR(err);
2113
Al Viro72bd8662012-06-10 17:17:17 -04002114 return __lookup_hash(&this, base, 0);
James Morris057f6c02007-04-26 00:12:05 -07002115}
2116
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002117int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2118 struct path *path, int *empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119{
Al Viro2d8f3032008-07-22 09:59:21 -04002120 struct nameidata nd;
Jeff Layton91a27b22012-10-10 15:25:28 -04002121 struct filename *tmp = getname_flags(name, flags, empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 int err = PTR_ERR(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 if (!IS_ERR(tmp)) {
Al Viro2d8f3032008-07-22 09:59:21 -04002124
2125 BUG_ON(flags & LOOKUP_PARENT);
2126
Jeff Layton873f1ee2012-10-10 15:25:29 -04002127 err = filename_lookup(dfd, tmp, flags, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 putname(tmp);
Al Viro2d8f3032008-07-22 09:59:21 -04002129 if (!err)
2130 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 }
2132 return err;
2133}
2134
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002135int user_path_at(int dfd, const char __user *name, unsigned flags,
2136 struct path *path)
2137{
Linus Torvaldsf7493e52012-03-22 16:10:40 -07002138 return user_path_at_empty(dfd, name, flags, path, NULL);
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002139}
2140
Jeff Layton873f1ee2012-10-10 15:25:29 -04002141/*
2142 * NB: most callers don't do anything directly with the reference to the
2143 * to struct filename, but the nd->last pointer points into the name string
2144 * allocated by getname. So we must hold the reference to it until all
2145 * path-walking is complete.
2146 */
Jeff Layton91a27b22012-10-10 15:25:28 -04002147static struct filename *
Jeff Layton9e790bd2012-12-11 12:10:09 -05002148user_path_parent(int dfd, const char __user *path, struct nameidata *nd,
2149 unsigned int flags)
Al Viro2ad94ae2008-07-21 09:32:51 -04002150{
Jeff Layton91a27b22012-10-10 15:25:28 -04002151 struct filename *s = getname(path);
Al Viro2ad94ae2008-07-21 09:32:51 -04002152 int error;
2153
Jeff Layton9e790bd2012-12-11 12:10:09 -05002154 /* only LOOKUP_REVAL is allowed in extra flags */
2155 flags &= LOOKUP_REVAL;
2156
Al Viro2ad94ae2008-07-21 09:32:51 -04002157 if (IS_ERR(s))
Jeff Layton91a27b22012-10-10 15:25:28 -04002158 return s;
Al Viro2ad94ae2008-07-21 09:32:51 -04002159
Jeff Layton9e790bd2012-12-11 12:10:09 -05002160 error = filename_lookup(dfd, s, flags | LOOKUP_PARENT, nd);
Jeff Layton91a27b22012-10-10 15:25:28 -04002161 if (error) {
Al Viro2ad94ae2008-07-21 09:32:51 -04002162 putname(s);
Jeff Layton91a27b22012-10-10 15:25:28 -04002163 return ERR_PTR(error);
2164 }
Al Viro2ad94ae2008-07-21 09:32:51 -04002165
Jeff Layton91a27b22012-10-10 15:25:28 -04002166 return s;
Al Viro2ad94ae2008-07-21 09:32:51 -04002167}
2168
Jeff Layton80334262013-07-26 06:23:25 -04002169/**
Al Viro197df042013-09-08 14:03:27 -04002170 * mountpoint_last - look up last component for umount
Jeff Layton80334262013-07-26 06:23:25 -04002171 * @nd: pathwalk nameidata - currently pointing at parent directory of "last"
2172 * @path: pointer to container for result
2173 *
2174 * This is a special lookup_last function just for umount. In this case, we
2175 * need to resolve the path without doing any revalidation.
2176 *
2177 * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
2178 * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
2179 * in almost all cases, this lookup will be served out of the dcache. The only
2180 * cases where it won't are if nd->last refers to a symlink or the path is
2181 * bogus and it doesn't exist.
2182 *
2183 * Returns:
2184 * -error: if there was an error during lookup. This includes -ENOENT if the
2185 * lookup found a negative dentry. The nd->path reference will also be
2186 * put in this case.
2187 *
2188 * 0: if we successfully resolved nd->path and found it to not to be a
2189 * symlink that needs to be followed. "path" will also be populated.
2190 * The nd->path reference will also be put.
2191 *
2192 * 1: if we successfully resolved nd->last and found it to be a symlink
2193 * that needs to be followed. "path" will be populated with the path
2194 * to the link, and nd->path will *not* be put.
2195 */
2196static int
Al Viro197df042013-09-08 14:03:27 -04002197mountpoint_last(struct nameidata *nd, struct path *path)
Jeff Layton80334262013-07-26 06:23:25 -04002198{
2199 int error = 0;
2200 struct dentry *dentry;
2201 struct dentry *dir = nd->path.dentry;
2202
Al Viro35759522013-09-08 13:41:33 -04002203 /* If we're in rcuwalk, drop out of it to handle last component */
2204 if (nd->flags & LOOKUP_RCU) {
2205 if (unlazy_walk(nd, NULL)) {
2206 error = -ECHILD;
2207 goto out;
2208 }
Jeff Layton80334262013-07-26 06:23:25 -04002209 }
2210
2211 nd->flags &= ~LOOKUP_PARENT;
2212
2213 if (unlikely(nd->last_type != LAST_NORM)) {
2214 error = handle_dots(nd, nd->last_type);
Al Viro35759522013-09-08 13:41:33 -04002215 if (error)
2216 goto out;
2217 dentry = dget(nd->path.dentry);
2218 goto done;
Jeff Layton80334262013-07-26 06:23:25 -04002219 }
2220
2221 mutex_lock(&dir->d_inode->i_mutex);
2222 dentry = d_lookup(dir, &nd->last);
2223 if (!dentry) {
2224 /*
2225 * No cached dentry. Mounted dentries are pinned in the cache,
2226 * so that means that this dentry is probably a symlink or the
2227 * path doesn't actually point to a mounted dentry.
2228 */
2229 dentry = d_alloc(dir, &nd->last);
2230 if (!dentry) {
2231 error = -ENOMEM;
Dave Jonesbcceeeb2013-09-10 17:04:25 -04002232 mutex_unlock(&dir->d_inode->i_mutex);
Al Viro35759522013-09-08 13:41:33 -04002233 goto out;
Jeff Layton80334262013-07-26 06:23:25 -04002234 }
Al Viro35759522013-09-08 13:41:33 -04002235 dentry = lookup_real(dir->d_inode, dentry, nd->flags);
2236 error = PTR_ERR(dentry);
Dave Jonesbcceeeb2013-09-10 17:04:25 -04002237 if (IS_ERR(dentry)) {
2238 mutex_unlock(&dir->d_inode->i_mutex);
Al Viro35759522013-09-08 13:41:33 -04002239 goto out;
Dave Jonesbcceeeb2013-09-10 17:04:25 -04002240 }
Jeff Layton80334262013-07-26 06:23:25 -04002241 }
2242 mutex_unlock(&dir->d_inode->i_mutex);
2243
Al Viro35759522013-09-08 13:41:33 -04002244done:
Al Virof0754e52014-04-19 12:30:58 -04002245 if (!dentry->d_inode || d_is_negative(dentry)) {
Al Viro35759522013-09-08 13:41:33 -04002246 error = -ENOENT;
2247 dput(dentry);
2248 goto out;
Jeff Layton80334262013-07-26 06:23:25 -04002249 }
Al Viro35759522013-09-08 13:41:33 -04002250 path->dentry = dentry;
Vasily Averin9b32e182014-07-21 12:30:23 +04002251 path->mnt = nd->path.mnt;
David Howellsb18825a2013-09-12 19:22:53 +01002252 if (should_follow_link(dentry, nd->flags & LOOKUP_FOLLOW))
Al Viro35759522013-09-08 13:41:33 -04002253 return 1;
Vasily Averin9b32e182014-07-21 12:30:23 +04002254 mntget(path->mnt);
Al Viro35759522013-09-08 13:41:33 -04002255 follow_mount(path);
2256 error = 0;
2257out:
Jeff Layton80334262013-07-26 06:23:25 -04002258 terminate_walk(nd);
2259 return error;
2260}
2261
2262/**
Al Viro197df042013-09-08 14:03:27 -04002263 * path_mountpoint - look up a path to be umounted
Jeff Layton80334262013-07-26 06:23:25 -04002264 * @dfd: directory file descriptor to start walk from
2265 * @name: full pathname to walk
Randy Dunlap606d6fe2013-10-19 14:56:55 -07002266 * @path: pointer to container for result
Jeff Layton80334262013-07-26 06:23:25 -04002267 * @flags: lookup flags
Jeff Layton80334262013-07-26 06:23:25 -04002268 *
2269 * Look up the given name, but don't attempt to revalidate the last component.
Randy Dunlap606d6fe2013-10-19 14:56:55 -07002270 * Returns 0 and "path" will be valid on success; Returns error otherwise.
Jeff Layton80334262013-07-26 06:23:25 -04002271 */
2272static int
Al Viro197df042013-09-08 14:03:27 -04002273path_mountpoint(int dfd, const char *name, struct path *path, unsigned int flags)
Jeff Layton80334262013-07-26 06:23:25 -04002274{
2275 struct file *base = NULL;
2276 struct nameidata nd;
2277 int err;
2278
2279 err = path_init(dfd, name, flags | LOOKUP_PARENT, &nd, &base);
2280 if (unlikely(err))
2281 return err;
2282
2283 current->total_link_count = 0;
2284 err = link_path_walk(name, &nd);
2285 if (err)
2286 goto out;
2287
Al Viro197df042013-09-08 14:03:27 -04002288 err = mountpoint_last(&nd, path);
Jeff Layton80334262013-07-26 06:23:25 -04002289 while (err > 0) {
2290 void *cookie;
2291 struct path link = *path;
2292 err = may_follow_link(&link, &nd);
2293 if (unlikely(err))
2294 break;
2295 nd.flags |= LOOKUP_PARENT;
2296 err = follow_link(&link, &nd, &cookie);
2297 if (err)
2298 break;
Al Viro197df042013-09-08 14:03:27 -04002299 err = mountpoint_last(&nd, path);
Jeff Layton80334262013-07-26 06:23:25 -04002300 put_link(&nd, &link, cookie);
2301 }
2302out:
2303 if (base)
2304 fput(base);
2305
2306 if (nd.root.mnt && !(nd.flags & LOOKUP_ROOT))
2307 path_put(&nd.root);
2308
2309 return err;
2310}
2311
Al Viro2d864652013-09-08 20:18:44 -04002312static int
2313filename_mountpoint(int dfd, struct filename *s, struct path *path,
2314 unsigned int flags)
2315{
2316 int error = path_mountpoint(dfd, s->name, path, flags | LOOKUP_RCU);
2317 if (unlikely(error == -ECHILD))
2318 error = path_mountpoint(dfd, s->name, path, flags);
2319 if (unlikely(error == -ESTALE))
2320 error = path_mountpoint(dfd, s->name, path, flags | LOOKUP_REVAL);
2321 if (likely(!error))
2322 audit_inode(s, path->dentry, 0);
2323 return error;
2324}
2325
Jeff Layton80334262013-07-26 06:23:25 -04002326/**
Al Viro197df042013-09-08 14:03:27 -04002327 * user_path_mountpoint_at - lookup a path from userland in order to umount it
Jeff Layton80334262013-07-26 06:23:25 -04002328 * @dfd: directory file descriptor
2329 * @name: pathname from userland
2330 * @flags: lookup flags
2331 * @path: pointer to container to hold result
2332 *
2333 * A umount is a special case for path walking. We're not actually interested
2334 * in the inode in this situation, and ESTALE errors can be a problem. We
2335 * simply want track down the dentry and vfsmount attached at the mountpoint
2336 * and avoid revalidating the last component.
2337 *
2338 * Returns 0 and populates "path" on success.
2339 */
2340int
Al Viro197df042013-09-08 14:03:27 -04002341user_path_mountpoint_at(int dfd, const char __user *name, unsigned int flags,
Jeff Layton80334262013-07-26 06:23:25 -04002342 struct path *path)
2343{
2344 struct filename *s = getname(name);
2345 int error;
Jeff Layton80334262013-07-26 06:23:25 -04002346 if (IS_ERR(s))
2347 return PTR_ERR(s);
Al Viro2d864652013-09-08 20:18:44 -04002348 error = filename_mountpoint(dfd, s, path, flags);
Jeff Layton80334262013-07-26 06:23:25 -04002349 putname(s);
2350 return error;
2351}
2352
Al Viro2d864652013-09-08 20:18:44 -04002353int
2354kern_path_mountpoint(int dfd, const char *name, struct path *path,
2355 unsigned int flags)
2356{
2357 struct filename s = {.name = name};
2358 return filename_mountpoint(dfd, &s, path, flags);
2359}
2360EXPORT_SYMBOL(kern_path_mountpoint);
2361
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362/*
2363 * It's inline, so penalty for filesystems that don't use sticky bit is
2364 * minimal.
2365 */
2366static inline int check_sticky(struct inode *dir, struct inode *inode)
2367{
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002368 kuid_t fsuid = current_fsuid();
David Howellsda9592e2008-11-14 10:39:05 +11002369
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 if (!(dir->i_mode & S_ISVTX))
2371 return 0;
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002372 if (uid_eq(inode->i_uid, fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 return 0;
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002374 if (uid_eq(dir->i_uid, fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 return 0;
Andy Lutomirski5bacea82014-06-10 12:45:42 -07002376 return !capable_wrt_inode_uidgid(inode, CAP_FOWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377}
2378
2379/*
2380 * Check whether we can remove a link victim from directory dir, check
2381 * whether the type of victim is right.
2382 * 1. We can't do it if dir is read-only (done in permission())
2383 * 2. We should have write and exec permissions on dir
2384 * 3. We can't remove anything from append-only dir
2385 * 4. We can't do anything with immutable dir (done in permission())
2386 * 5. If the sticky bit on dir is set we should either
2387 * a. be owner of dir, or
2388 * b. be owner of victim, or
2389 * c. have CAP_FOWNER capability
2390 * 6. If the victim is append-only or immutable we can't do antyhing with
2391 * links pointing to it.
2392 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2393 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2394 * 9. We can't remove a root or mountpoint.
2395 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
2396 * nfs_async_unlink().
2397 */
David Howellsb18825a2013-09-12 19:22:53 +01002398static int may_delete(struct inode *dir, struct dentry *victim, bool isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399{
David Howellsb18825a2013-09-12 19:22:53 +01002400 struct inode *inode = victim->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 int error;
2402
David Howellsb18825a2013-09-12 19:22:53 +01002403 if (d_is_negative(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 return -ENOENT;
David Howellsb18825a2013-09-12 19:22:53 +01002405 BUG_ON(!inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
2407 BUG_ON(victim->d_parent->d_inode != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04002408 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
Al Virof419a2e2008-07-22 00:07:17 -04002410 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 if (error)
2412 return error;
2413 if (IS_APPEND(dir))
2414 return -EPERM;
David Howellsb18825a2013-09-12 19:22:53 +01002415
2416 if (check_sticky(dir, inode) || IS_APPEND(inode) ||
2417 IS_IMMUTABLE(inode) || IS_SWAPFILE(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 return -EPERM;
2419 if (isdir) {
Miklos Szeredidb8bf542014-04-01 17:08:41 +02002420 if (!d_is_dir(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 return -ENOTDIR;
2422 if (IS_ROOT(victim))
2423 return -EBUSY;
Miklos Szeredidb8bf542014-04-01 17:08:41 +02002424 } else if (d_is_dir(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 return -EISDIR;
2426 if (IS_DEADDIR(dir))
2427 return -ENOENT;
2428 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2429 return -EBUSY;
2430 return 0;
2431}
2432
2433/* Check whether we can create an object with dentry child in directory
2434 * dir.
2435 * 1. We can't do it if child already exists (open has special treatment for
2436 * this case, but since we are inlined it's OK)
2437 * 2. We can't do it if dir is read-only (done in permission())
2438 * 3. We should have write and exec permissions on dir
2439 * 4. We can't do it if dir is immutable (done in permission())
2440 */
Miklos Szeredia95164d2008-07-30 15:08:48 +02002441static inline int may_create(struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442{
Jeff Layton14e972b2013-05-08 10:25:58 -04002443 audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 if (child->d_inode)
2445 return -EEXIST;
2446 if (IS_DEADDIR(dir))
2447 return -ENOENT;
Al Virof419a2e2008-07-22 00:07:17 -04002448 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449}
2450
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451/*
2452 * p1 and p2 should be directories on the same fs.
2453 */
2454struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2455{
2456 struct dentry *p;
2457
2458 if (p1 == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07002459 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 return NULL;
2461 }
2462
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002463 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002465 p = d_ancestor(p2, p1);
2466 if (p) {
2467 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2468 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
2469 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 }
2471
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002472 p = d_ancestor(p1, p2);
2473 if (p) {
2474 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2475 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2476 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 }
2478
Ingo Molnarf2eace22006-07-03 00:25:05 -07002479 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2480 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 return NULL;
2482}
2483
2484void unlock_rename(struct dentry *p1, struct dentry *p2)
2485{
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002486 mutex_unlock(&p1->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 if (p1 != p2) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002488 mutex_unlock(&p2->d_inode->i_mutex);
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002489 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 }
2491}
2492
Al Viro4acdaf22011-07-26 01:42:34 -04002493int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viro312b63f2012-06-10 18:09:36 -04002494 bool want_excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002496 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 if (error)
2498 return error;
2499
Al Viroacfa4382008-12-04 10:06:33 -05002500 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 return -EACCES; /* shouldn't it be ENOSYS? */
2502 mode &= S_IALLUGO;
2503 mode |= S_IFREG;
2504 error = security_inode_create(dir, dentry, mode);
2505 if (error)
2506 return error;
Al Viro312b63f2012-06-10 18:09:36 -04002507 error = dir->i_op->create(dir, dentry, mode, want_excl);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002508 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002509 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 return error;
2511}
2512
Al Viro73d049a2011-03-11 12:08:24 -05002513static int may_open(struct path *path, int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002515 struct dentry *dentry = path->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 struct inode *inode = dentry->d_inode;
2517 int error;
2518
Al Virobcda7652011-03-13 16:42:14 -04002519 /* O_PATH? */
2520 if (!acc_mode)
2521 return 0;
2522
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 if (!inode)
2524 return -ENOENT;
2525
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002526 switch (inode->i_mode & S_IFMT) {
2527 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002529 case S_IFDIR:
2530 if (acc_mode & MAY_WRITE)
2531 return -EISDIR;
2532 break;
2533 case S_IFBLK:
2534 case S_IFCHR:
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002535 if (path->mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002537 /*FALLTHRU*/
2538 case S_IFIFO:
2539 case S_IFSOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002541 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08002542 }
Dave Hansenb41572e2007-10-16 23:31:14 -07002543
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002544 error = inode_permission(inode, acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07002545 if (error)
2546 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05002547
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 /*
2549 * An append-only file must be opened in append mode for writing.
2550 */
2551 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05002552 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05002553 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05002555 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 }
2557
2558 /* O_NOATIME can only be set by the owner or superuser */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07002559 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
Al Viro7715b522009-12-16 03:54:00 -05002560 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561
J. Bruce Fieldsf3c7691e2011-09-21 10:58:13 -04002562 return 0;
Al Viro7715b522009-12-16 03:54:00 -05002563}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
Jeff Laytone1181ee2010-12-07 16:19:50 -05002565static int handle_truncate(struct file *filp)
Al Viro7715b522009-12-16 03:54:00 -05002566{
Jeff Laytone1181ee2010-12-07 16:19:50 -05002567 struct path *path = &filp->f_path;
Al Viro7715b522009-12-16 03:54:00 -05002568 struct inode *inode = path->dentry->d_inode;
2569 int error = get_write_access(inode);
2570 if (error)
2571 return error;
2572 /*
2573 * Refuse to truncate files with mandatory locks held on them.
2574 */
2575 error = locks_verify_locked(inode);
2576 if (!error)
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +09002577 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05002578 if (!error) {
2579 error = do_truncate(path->dentry, 0,
2580 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
Jeff Laytone1181ee2010-12-07 16:19:50 -05002581 filp);
Al Viro7715b522009-12-16 03:54:00 -05002582 }
2583 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04002584 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585}
2586
Dave Hansend57999e2008-02-15 14:37:27 -08002587static inline int open_to_namei_flags(int flag)
2588{
Al Viro8a5e9292011-06-25 19:15:54 -04002589 if ((flag & O_ACCMODE) == 3)
2590 flag--;
Dave Hansend57999e2008-02-15 14:37:27 -08002591 return flag;
2592}
2593
Miklos Szeredid18e9002012-06-05 15:10:17 +02002594static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode)
2595{
2596 int error = security_path_mknod(dir, dentry, mode, 0);
2597 if (error)
2598 return error;
2599
2600 error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
2601 if (error)
2602 return error;
2603
2604 return security_inode_create(dir->dentry->d_inode, dentry, mode);
2605}
2606
David Howells1acf0af2012-06-14 16:13:46 +01002607/*
2608 * Attempt to atomically look up, create and open a file from a negative
2609 * dentry.
2610 *
2611 * Returns 0 if successful. The file will have been created and attached to
2612 * @file by the filesystem calling finish_open().
2613 *
2614 * Returns 1 if the file was looked up only or didn't need creating. The
2615 * caller will need to perform the open themselves. @path will have been
2616 * updated to point to the new dentry. This may be negative.
2617 *
2618 * Returns an error code otherwise.
2619 */
Al Viro2675a4e2012-06-22 12:41:10 +04002620static int atomic_open(struct nameidata *nd, struct dentry *dentry,
2621 struct path *path, struct file *file,
2622 const struct open_flags *op,
Al Viro64894cf2012-07-31 00:53:35 +04002623 bool got_write, bool need_lookup,
Al Viro2675a4e2012-06-22 12:41:10 +04002624 int *opened)
Miklos Szeredid18e9002012-06-05 15:10:17 +02002625{
2626 struct inode *dir = nd->path.dentry->d_inode;
2627 unsigned open_flag = open_to_namei_flags(op->open_flag);
2628 umode_t mode;
2629 int error;
2630 int acc_mode;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002631 int create_error = 0;
2632 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
Miklos Szeredi116cc022013-09-16 14:52:05 +02002633 bool excl;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002634
2635 BUG_ON(dentry->d_inode);
2636
2637 /* Don't create child dentry for a dead directory. */
2638 if (unlikely(IS_DEADDIR(dir))) {
Al Viro2675a4e2012-06-22 12:41:10 +04002639 error = -ENOENT;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002640 goto out;
2641 }
2642
Miklos Szeredi62b259d2012-08-15 13:01:24 +02002643 mode = op->mode;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002644 if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
2645 mode &= ~current_umask();
2646
Miklos Szeredi116cc022013-09-16 14:52:05 +02002647 excl = (open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT);
2648 if (excl)
Miklos Szeredid18e9002012-06-05 15:10:17 +02002649 open_flag &= ~O_TRUNC;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002650
2651 /*
2652 * Checking write permission is tricky, bacuse we don't know if we are
2653 * going to actually need it: O_CREAT opens should work as long as the
2654 * file exists. But checking existence breaks atomicity. The trick is
2655 * to check access and if not granted clear O_CREAT from the flags.
2656 *
2657 * Another problem is returing the "right" error value (e.g. for an
2658 * O_EXCL open we want to return EEXIST not EROFS).
2659 */
Al Viro64894cf2012-07-31 00:53:35 +04002660 if (((open_flag & (O_CREAT | O_TRUNC)) ||
2661 (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) {
2662 if (!(open_flag & O_CREAT)) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002663 /*
2664 * No O_CREATE -> atomicity not a requirement -> fall
2665 * back to lookup + open
2666 */
2667 goto no_open;
2668 } else if (open_flag & (O_EXCL | O_TRUNC)) {
2669 /* Fall back and fail with the right error */
Al Viro64894cf2012-07-31 00:53:35 +04002670 create_error = -EROFS;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002671 goto no_open;
2672 } else {
2673 /* No side effects, safe to clear O_CREAT */
Al Viro64894cf2012-07-31 00:53:35 +04002674 create_error = -EROFS;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002675 open_flag &= ~O_CREAT;
2676 }
2677 }
2678
2679 if (open_flag & O_CREAT) {
Miklos Szeredi38227f72012-08-15 13:01:24 +02002680 error = may_o_create(&nd->path, dentry, mode);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002681 if (error) {
2682 create_error = error;
2683 if (open_flag & O_EXCL)
2684 goto no_open;
2685 open_flag &= ~O_CREAT;
2686 }
2687 }
2688
2689 if (nd->flags & LOOKUP_DIRECTORY)
2690 open_flag |= O_DIRECTORY;
2691
Al Viro30d90492012-06-22 12:40:19 +04002692 file->f_path.dentry = DENTRY_NOT_SET;
2693 file->f_path.mnt = nd->path.mnt;
2694 error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode,
Al Viro47237682012-06-10 05:01:45 -04002695 opened);
Al Virod9585272012-06-22 12:39:14 +04002696 if (error < 0) {
Al Virod9585272012-06-22 12:39:14 +04002697 if (create_error && error == -ENOENT)
2698 error = create_error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002699 goto out;
2700 }
2701
Al Virod9585272012-06-22 12:39:14 +04002702 if (error) { /* returned 1, that is */
Al Viro30d90492012-06-22 12:40:19 +04002703 if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
Al Viro2675a4e2012-06-22 12:41:10 +04002704 error = -EIO;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002705 goto out;
2706 }
Al Viro30d90492012-06-22 12:40:19 +04002707 if (file->f_path.dentry) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002708 dput(dentry);
Al Viro30d90492012-06-22 12:40:19 +04002709 dentry = file->f_path.dentry;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002710 }
Al Viro03da6332013-09-16 19:22:33 -04002711 if (*opened & FILE_CREATED)
2712 fsnotify_create(dir, dentry);
2713 if (!dentry->d_inode) {
2714 WARN_ON(*opened & FILE_CREATED);
2715 if (create_error) {
2716 error = create_error;
2717 goto out;
2718 }
2719 } else {
2720 if (excl && !(*opened & FILE_CREATED)) {
2721 error = -EEXIST;
2722 goto out;
2723 }
Sage Weil62b2ce92012-08-15 13:30:12 -07002724 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02002725 goto looked_up;
2726 }
2727
2728 /*
2729 * We didn't have the inode before the open, so check open permission
2730 * here.
2731 */
Al Viro03da6332013-09-16 19:22:33 -04002732 acc_mode = op->acc_mode;
2733 if (*opened & FILE_CREATED) {
2734 WARN_ON(!(open_flag & O_CREAT));
2735 fsnotify_create(dir, dentry);
2736 acc_mode = MAY_OPEN;
2737 }
Al Viro2675a4e2012-06-22 12:41:10 +04002738 error = may_open(&file->f_path, acc_mode, open_flag);
2739 if (error)
2740 fput(file);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002741
2742out:
2743 dput(dentry);
Al Viro2675a4e2012-06-22 12:41:10 +04002744 return error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002745
Miklos Szeredid18e9002012-06-05 15:10:17 +02002746no_open:
2747 if (need_lookup) {
Al Viro72bd8662012-06-10 17:17:17 -04002748 dentry = lookup_real(dir, dentry, nd->flags);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002749 if (IS_ERR(dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04002750 return PTR_ERR(dentry);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002751
2752 if (create_error) {
2753 int open_flag = op->open_flag;
2754
Al Viro2675a4e2012-06-22 12:41:10 +04002755 error = create_error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002756 if ((open_flag & O_EXCL)) {
2757 if (!dentry->d_inode)
2758 goto out;
2759 } else if (!dentry->d_inode) {
2760 goto out;
2761 } else if ((open_flag & O_TRUNC) &&
2762 S_ISREG(dentry->d_inode->i_mode)) {
2763 goto out;
2764 }
2765 /* will fail later, go on to get the right error */
2766 }
2767 }
2768looked_up:
2769 path->dentry = dentry;
2770 path->mnt = nd->path.mnt;
Al Viro2675a4e2012-06-22 12:41:10 +04002771 return 1;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002772}
2773
Nick Piggin31e6b012011-01-07 17:49:52 +11002774/*
David Howells1acf0af2012-06-14 16:13:46 +01002775 * Look up and maybe create and open the last component.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002776 *
2777 * Must be called with i_mutex held on parent.
2778 *
David Howells1acf0af2012-06-14 16:13:46 +01002779 * Returns 0 if the file was successfully atomically created (if necessary) and
2780 * opened. In this case the file will be returned attached to @file.
2781 *
2782 * Returns 1 if the file was not completely opened at this time, though lookups
2783 * and creations will have been performed and the dentry returned in @path will
2784 * be positive upon return if O_CREAT was specified. If O_CREAT wasn't
2785 * specified then a negative dentry may be returned.
2786 *
2787 * An error code is returned otherwise.
2788 *
2789 * FILE_CREATE will be set in @*opened if the dentry was created and will be
2790 * cleared otherwise prior to returning.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002791 */
Al Viro2675a4e2012-06-22 12:41:10 +04002792static int lookup_open(struct nameidata *nd, struct path *path,
2793 struct file *file,
2794 const struct open_flags *op,
Al Viro64894cf2012-07-31 00:53:35 +04002795 bool got_write, int *opened)
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002796{
2797 struct dentry *dir = nd->path.dentry;
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002798 struct inode *dir_inode = dir->d_inode;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002799 struct dentry *dentry;
2800 int error;
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002801 bool need_lookup;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002802
Al Viro47237682012-06-10 05:01:45 -04002803 *opened &= ~FILE_CREATED;
Al Viro201f9562012-06-22 12:42:10 +04002804 dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002805 if (IS_ERR(dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04002806 return PTR_ERR(dentry);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002807
Miklos Szeredid18e9002012-06-05 15:10:17 +02002808 /* Cached positive dentry: will open in f_op->open */
2809 if (!need_lookup && dentry->d_inode)
2810 goto out_no_open;
2811
2812 if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) {
Al Viro64894cf2012-07-31 00:53:35 +04002813 return atomic_open(nd, dentry, path, file, op, got_write,
Al Viro47237682012-06-10 05:01:45 -04002814 need_lookup, opened);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002815 }
2816
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002817 if (need_lookup) {
2818 BUG_ON(dentry->d_inode);
2819
Al Viro72bd8662012-06-10 17:17:17 -04002820 dentry = lookup_real(dir_inode, dentry, nd->flags);
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002821 if (IS_ERR(dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04002822 return PTR_ERR(dentry);
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002823 }
2824
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002825 /* Negative dentry, just create the file */
2826 if (!dentry->d_inode && (op->open_flag & O_CREAT)) {
2827 umode_t mode = op->mode;
2828 if (!IS_POSIXACL(dir->d_inode))
2829 mode &= ~current_umask();
2830 /*
2831 * This write is needed to ensure that a
2832 * rw->ro transition does not occur between
2833 * the time when the file is created and when
2834 * a permanent write count is taken through
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02002835 * the 'struct file' in finish_open().
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002836 */
Al Viro64894cf2012-07-31 00:53:35 +04002837 if (!got_write) {
2838 error = -EROFS;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002839 goto out_dput;
Al Viro64894cf2012-07-31 00:53:35 +04002840 }
Al Viro47237682012-06-10 05:01:45 -04002841 *opened |= FILE_CREATED;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002842 error = security_path_mknod(&nd->path, dentry, mode, 0);
2843 if (error)
2844 goto out_dput;
Al Viro312b63f2012-06-10 18:09:36 -04002845 error = vfs_create(dir->d_inode, dentry, mode,
2846 nd->flags & LOOKUP_EXCL);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002847 if (error)
2848 goto out_dput;
2849 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02002850out_no_open:
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002851 path->dentry = dentry;
2852 path->mnt = nd->path.mnt;
Al Viro2675a4e2012-06-22 12:41:10 +04002853 return 1;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002854
2855out_dput:
2856 dput(dentry);
Al Viro2675a4e2012-06-22 12:41:10 +04002857 return error;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002858}
2859
2860/*
Al Virofe2d35f2011-03-05 22:58:25 -05002861 * Handle the last step of open()
Nick Piggin31e6b012011-01-07 17:49:52 +11002862 */
Al Viro2675a4e2012-06-22 12:41:10 +04002863static int do_last(struct nameidata *nd, struct path *path,
2864 struct file *file, const struct open_flags *op,
Jeff Layton669abf42012-10-10 16:43:10 -04002865 int *opened, struct filename *name)
Al Virofb1cc552009-12-24 01:58:28 -05002866{
Al Viroa1e28032009-12-24 02:12:06 -05002867 struct dentry *dir = nd->path.dentry;
Al Viroca344a892011-03-09 00:36:45 -05002868 int open_flag = op->open_flag;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002869 bool will_truncate = (open_flag & O_TRUNC) != 0;
Al Viro64894cf2012-07-31 00:53:35 +04002870 bool got_write = false;
Al Virobcda7652011-03-13 16:42:14 -04002871 int acc_mode = op->acc_mode;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02002872 struct inode *inode;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002873 bool symlink_ok = false;
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02002874 struct path save_parent = { .dentry = NULL, .mnt = NULL };
2875 bool retried = false;
Al Viro16c2cd72011-02-22 15:50:10 -05002876 int error;
Al Virofb1cc552009-12-24 01:58:28 -05002877
Al Viroc3e380b2011-02-23 13:39:45 -05002878 nd->flags &= ~LOOKUP_PARENT;
2879 nd->flags |= op->intent;
2880
Al Virobc77daa2013-06-06 09:12:33 -04002881 if (nd->last_type != LAST_NORM) {
Al Virofe2d35f2011-03-05 22:58:25 -05002882 error = handle_dots(nd, nd->last_type);
2883 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04002884 return error;
Miklos Szeredie83db162012-06-05 15:10:29 +02002885 goto finish_open;
Al Viro1f36f772009-12-26 10:56:19 -05002886 }
Al Viro67ee3ad2009-12-26 07:01:01 -05002887
Al Viroca344a892011-03-09 00:36:45 -05002888 if (!(open_flag & O_CREAT)) {
Al Virofe2d35f2011-03-05 22:58:25 -05002889 if (nd->last.name[nd->last.len])
2890 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
Al Virobcda7652011-03-13 16:42:14 -04002891 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002892 symlink_ok = true;
Al Virofe2d35f2011-03-05 22:58:25 -05002893 /* we _can_ be in RCU mode here */
Al Viroe97cdc82013-01-24 18:16:00 -05002894 error = lookup_fast(nd, path, &inode);
Miklos Szeredi71574862012-06-05 15:10:14 +02002895 if (likely(!error))
2896 goto finish_lookup;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02002897
Miklos Szeredi71574862012-06-05 15:10:14 +02002898 if (error < 0)
Al Viro2675a4e2012-06-22 12:41:10 +04002899 goto out;
Miklos Szeredi37d7fff2012-06-05 15:10:12 +02002900
Miklos Szeredi71574862012-06-05 15:10:14 +02002901 BUG_ON(nd->inode != dir->d_inode);
Miklos Szeredib6183df2012-06-05 15:10:13 +02002902 } else {
2903 /* create side of things */
2904 /*
2905 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
2906 * has been cleared when we got to the last component we are
2907 * about to look up
2908 */
2909 error = complete_walk(nd);
2910 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04002911 return error;
Miklos Szeredib6183df2012-06-05 15:10:13 +02002912
Jeff Layton33e22082013-04-12 15:16:32 -04002913 audit_inode(name, dir, LOOKUP_PARENT);
Miklos Szeredib6183df2012-06-05 15:10:13 +02002914 error = -EISDIR;
2915 /* trailing slashes? */
2916 if (nd->last.name[nd->last.len])
Al Viro2675a4e2012-06-22 12:41:10 +04002917 goto out;
Al Virofe2d35f2011-03-05 22:58:25 -05002918 }
2919
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02002920retry_lookup:
Al Viro64894cf2012-07-31 00:53:35 +04002921 if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
2922 error = mnt_want_write(nd->path.mnt);
2923 if (!error)
2924 got_write = true;
2925 /*
2926 * do _not_ fail yet - we might not need that or fail with
2927 * a different error; let lookup_open() decide; we'll be
2928 * dropping this one anyway.
2929 */
2930 }
Al Viroa1e28032009-12-24 02:12:06 -05002931 mutex_lock(&dir->d_inode->i_mutex);
Al Viro64894cf2012-07-31 00:53:35 +04002932 error = lookup_open(nd, path, file, op, got_write, opened);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002933 mutex_unlock(&dir->d_inode->i_mutex);
Al Viroa1e28032009-12-24 02:12:06 -05002934
Al Viro2675a4e2012-06-22 12:41:10 +04002935 if (error <= 0) {
2936 if (error)
Miklos Szeredid18e9002012-06-05 15:10:17 +02002937 goto out;
2938
Al Viro47237682012-06-10 05:01:45 -04002939 if ((*opened & FILE_CREATED) ||
Al Viro496ad9a2013-01-23 17:07:38 -05002940 !S_ISREG(file_inode(file)->i_mode))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002941 will_truncate = false;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002942
Jeff Laytonadb5c242012-10-10 16:43:13 -04002943 audit_inode(name, file->f_path.dentry, 0);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002944 goto opened;
2945 }
Al Virofb1cc552009-12-24 01:58:28 -05002946
Al Viro47237682012-06-10 05:01:45 -04002947 if (*opened & FILE_CREATED) {
Al Viro9b44f1b2011-03-09 00:17:27 -05002948 /* Don't check for write permission, don't truncate */
Al Viroca344a892011-03-09 00:36:45 -05002949 open_flag &= ~O_TRUNC;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002950 will_truncate = false;
Al Virobcda7652011-03-13 16:42:14 -04002951 acc_mode = MAY_OPEN;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002952 path_to_nameidata(path, nd);
Miklos Szeredie83db162012-06-05 15:10:29 +02002953 goto finish_open_created;
Al Virofb1cc552009-12-24 01:58:28 -05002954 }
2955
2956 /*
Jeff Layton3134f372012-07-25 10:19:47 -04002957 * create/update audit record if it already exists.
Al Virofb1cc552009-12-24 01:58:28 -05002958 */
David Howellsb18825a2013-09-12 19:22:53 +01002959 if (d_is_positive(path->dentry))
Jeff Laytonadb5c242012-10-10 16:43:13 -04002960 audit_inode(name, path->dentry, 0);
Al Virofb1cc552009-12-24 01:58:28 -05002961
Miklos Szeredid18e9002012-06-05 15:10:17 +02002962 /*
2963 * If atomic_open() acquired write access it is dropped now due to
2964 * possible mount and symlink following (this might be optimized away if
2965 * necessary...)
2966 */
Al Viro64894cf2012-07-31 00:53:35 +04002967 if (got_write) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002968 mnt_drop_write(nd->path.mnt);
Al Viro64894cf2012-07-31 00:53:35 +04002969 got_write = false;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002970 }
2971
Al Virofb1cc552009-12-24 01:58:28 -05002972 error = -EEXIST;
Al Virof8310c52012-07-30 11:50:30 +04002973 if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))
Al Virofb1cc552009-12-24 01:58:28 -05002974 goto exit_dput;
2975
David Howells9875cf82011-01-14 18:45:21 +00002976 error = follow_managed(path, nd->flags);
2977 if (error < 0)
2978 goto exit_dput;
Al Virofb1cc552009-12-24 01:58:28 -05002979
Al Viroa3fbbde2011-11-07 21:21:26 +00002980 if (error)
2981 nd->flags |= LOOKUP_JUMPED;
2982
Miklos Szeredidecf3402012-05-21 17:30:08 +02002983 BUG_ON(nd->flags & LOOKUP_RCU);
2984 inode = path->dentry->d_inode;
Miklos Szeredi5f5daac2012-05-21 17:30:14 +02002985finish_lookup:
2986 /* we _can_ be in RCU mode here */
Al Virofb1cc552009-12-24 01:58:28 -05002987 error = -ENOENT;
Al Virof0754e52014-04-19 12:30:58 -04002988 if (!inode || d_is_negative(path->dentry)) {
Miklos Szeredi54c33e72012-05-21 17:30:10 +02002989 path_to_nameidata(path, nd);
Al Viro2675a4e2012-06-22 12:41:10 +04002990 goto out;
Miklos Szeredi54c33e72012-05-21 17:30:10 +02002991 }
Al Viro9e67f362009-12-26 07:04:50 -05002992
David Howellsb18825a2013-09-12 19:22:53 +01002993 if (should_follow_link(path->dentry, !symlink_ok)) {
Miklos Szeredid45ea862012-05-21 17:30:09 +02002994 if (nd->flags & LOOKUP_RCU) {
2995 if (unlikely(unlazy_walk(nd, path->dentry))) {
2996 error = -ECHILD;
Al Viro2675a4e2012-06-22 12:41:10 +04002997 goto out;
Miklos Szeredid45ea862012-05-21 17:30:09 +02002998 }
2999 }
3000 BUG_ON(inode != path->dentry->d_inode);
Al Viro2675a4e2012-06-22 12:41:10 +04003001 return 1;
Miklos Szeredid45ea862012-05-21 17:30:09 +02003002 }
Al Virofb1cc552009-12-24 01:58:28 -05003003
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003004 if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) {
3005 path_to_nameidata(path, nd);
3006 } else {
3007 save_parent.dentry = nd->path.dentry;
3008 save_parent.mnt = mntget(path->mnt);
3009 nd->path.dentry = path->dentry;
3010
3011 }
Miklos Szeredidecf3402012-05-21 17:30:08 +02003012 nd->inode = inode;
Al Viroa3fbbde2011-11-07 21:21:26 +00003013 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
Al Virobc77daa2013-06-06 09:12:33 -04003014finish_open:
Al Viroa3fbbde2011-11-07 21:21:26 +00003015 error = complete_walk(nd);
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003016 if (error) {
3017 path_put(&save_parent);
Al Viro2675a4e2012-06-22 12:41:10 +04003018 return error;
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003019 }
Al Virobc77daa2013-06-06 09:12:33 -04003020 audit_inode(name, nd->path.dentry, 0);
Al Virofb1cc552009-12-24 01:58:28 -05003021 error = -EISDIR;
Miklos Szeredidb8bf542014-04-01 17:08:41 +02003022 if ((open_flag & O_CREAT) && d_is_dir(nd->path.dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04003023 goto out;
Miklos Szerediaf2f5542012-05-21 17:30:11 +02003024 error = -ENOTDIR;
Miklos Szeredidb8bf542014-04-01 17:08:41 +02003025 if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04003026 goto out;
Al Viro6c0d46c2011-03-09 00:59:59 -05003027 if (!S_ISREG(nd->inode->i_mode))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003028 will_truncate = false;
Al Viro6c0d46c2011-03-09 00:59:59 -05003029
Al Viro0f9d1a12011-03-09 00:13:14 -05003030 if (will_truncate) {
3031 error = mnt_want_write(nd->path.mnt);
3032 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003033 goto out;
Al Viro64894cf2012-07-31 00:53:35 +04003034 got_write = true;
Al Viro0f9d1a12011-03-09 00:13:14 -05003035 }
Miklos Szeredie83db162012-06-05 15:10:29 +02003036finish_open_created:
Al Virobcda7652011-03-13 16:42:14 -04003037 error = may_open(&nd->path, acc_mode, open_flag);
Al Viroca344a892011-03-09 00:36:45 -05003038 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003039 goto out;
Miklos Szeredi71732032014-10-24 00:14:35 +02003040
3041 BUG_ON(*opened & FILE_OPENED); /* once it's opened, it's opened */
3042 error = vfs_open(&nd->path, file, current_cred());
3043 if (!error) {
3044 *opened |= FILE_OPENED;
3045 } else {
Al Viro30d90492012-06-22 12:40:19 +04003046 if (error == -EOPENSTALE)
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003047 goto stale_open;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003048 goto out;
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003049 }
Miklos Szeredia8277b92012-06-05 15:10:32 +02003050opened:
Al Viro2675a4e2012-06-22 12:41:10 +04003051 error = open_check_o_direct(file);
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003052 if (error)
3053 goto exit_fput;
Al Viro2675a4e2012-06-22 12:41:10 +04003054 error = ima_file_check(file, op->acc_mode);
Miklos Szerediaa4caad2012-06-05 15:10:28 +02003055 if (error)
3056 goto exit_fput;
3057
3058 if (will_truncate) {
Al Viro2675a4e2012-06-22 12:41:10 +04003059 error = handle_truncate(file);
Miklos Szerediaa4caad2012-06-05 15:10:28 +02003060 if (error)
3061 goto exit_fput;
Al Viro0f9d1a12011-03-09 00:13:14 -05003062 }
Al Viroca344a892011-03-09 00:36:45 -05003063out:
Al Viro64894cf2012-07-31 00:53:35 +04003064 if (got_write)
Al Viro0f9d1a12011-03-09 00:13:14 -05003065 mnt_drop_write(nd->path.mnt);
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003066 path_put(&save_parent);
Miklos Szeredie276ae62012-05-21 17:30:06 +02003067 terminate_walk(nd);
Al Viro2675a4e2012-06-22 12:41:10 +04003068 return error;
Al Virofb1cc552009-12-24 01:58:28 -05003069
Al Virofb1cc552009-12-24 01:58:28 -05003070exit_dput:
3071 path_put_conditional(path, nd);
Al Viroca344a892011-03-09 00:36:45 -05003072 goto out;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003073exit_fput:
Al Viro2675a4e2012-06-22 12:41:10 +04003074 fput(file);
3075 goto out;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003076
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003077stale_open:
3078 /* If no saved parent or already retried then can't retry */
3079 if (!save_parent.dentry || retried)
3080 goto out;
3081
3082 BUG_ON(save_parent.dentry != dir);
3083 path_put(&nd->path);
3084 nd->path = save_parent;
3085 nd->inode = dir->d_inode;
3086 save_parent.mnt = NULL;
3087 save_parent.dentry = NULL;
Al Viro64894cf2012-07-31 00:53:35 +04003088 if (got_write) {
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003089 mnt_drop_write(nd->path.mnt);
Al Viro64894cf2012-07-31 00:53:35 +04003090 got_write = false;
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003091 }
3092 retried = true;
3093 goto retry_lookup;
Al Virofb1cc552009-12-24 01:58:28 -05003094}
3095
Al Viro60545d02013-06-07 01:20:27 -04003096static int do_tmpfile(int dfd, struct filename *pathname,
3097 struct nameidata *nd, int flags,
3098 const struct open_flags *op,
3099 struct file *file, int *opened)
3100{
3101 static const struct qstr name = QSTR_INIT("/", 1);
3102 struct dentry *dentry, *child;
3103 struct inode *dir;
3104 int error = path_lookupat(dfd, pathname->name,
3105 flags | LOOKUP_DIRECTORY, nd);
3106 if (unlikely(error))
3107 return error;
3108 error = mnt_want_write(nd->path.mnt);
3109 if (unlikely(error))
3110 goto out;
3111 /* we want directory to be writable */
3112 error = inode_permission(nd->inode, MAY_WRITE | MAY_EXEC);
3113 if (error)
3114 goto out2;
3115 dentry = nd->path.dentry;
3116 dir = dentry->d_inode;
3117 if (!dir->i_op->tmpfile) {
3118 error = -EOPNOTSUPP;
3119 goto out2;
3120 }
3121 child = d_alloc(dentry, &name);
3122 if (unlikely(!child)) {
3123 error = -ENOMEM;
3124 goto out2;
3125 }
3126 nd->flags &= ~LOOKUP_DIRECTORY;
3127 nd->flags |= op->intent;
3128 dput(nd->path.dentry);
3129 nd->path.dentry = child;
3130 error = dir->i_op->tmpfile(dir, nd->path.dentry, op->mode);
3131 if (error)
3132 goto out2;
3133 audit_inode(pathname, nd->path.dentry, 0);
Eric Rannaud74df6af2014-10-30 01:51:01 -07003134 /* Don't check for other permissions, the inode was just created */
3135 error = may_open(&nd->path, MAY_OPEN, op->open_flag);
Al Viro60545d02013-06-07 01:20:27 -04003136 if (error)
3137 goto out2;
3138 file->f_path.mnt = nd->path.mnt;
3139 error = finish_open(file, nd->path.dentry, NULL, opened);
3140 if (error)
3141 goto out2;
3142 error = open_check_o_direct(file);
Al Virof4e0c302013-06-11 08:34:36 +04003143 if (error) {
Al Viro60545d02013-06-07 01:20:27 -04003144 fput(file);
Al Virof4e0c302013-06-11 08:34:36 +04003145 } else if (!(op->open_flag & O_EXCL)) {
3146 struct inode *inode = file_inode(file);
3147 spin_lock(&inode->i_lock);
3148 inode->i_state |= I_LINKABLE;
3149 spin_unlock(&inode->i_lock);
3150 }
Al Viro60545d02013-06-07 01:20:27 -04003151out2:
3152 mnt_drop_write(nd->path.mnt);
3153out:
3154 path_put(&nd->path);
3155 return error;
3156}
3157
Jeff Layton669abf42012-10-10 16:43:10 -04003158static struct file *path_openat(int dfd, struct filename *pathname,
Al Viro73d049a2011-03-11 12:08:24 -05003159 struct nameidata *nd, const struct open_flags *op, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160{
Al Virofe2d35f2011-03-05 22:58:25 -05003161 struct file *base = NULL;
Al Viro30d90492012-06-22 12:40:19 +04003162 struct file *file;
Al Viro9850c052010-01-13 15:01:15 -05003163 struct path path;
Al Viro47237682012-06-10 05:01:45 -04003164 int opened = 0;
Al Viro13aab422011-02-23 17:54:08 -05003165 int error;
Nick Piggin31e6b012011-01-07 17:49:52 +11003166
Al Viro30d90492012-06-22 12:40:19 +04003167 file = get_empty_filp();
Al Viro1afc99b2013-02-14 20:41:04 -05003168 if (IS_ERR(file))
3169 return file;
Nick Piggin31e6b012011-01-07 17:49:52 +11003170
Al Viro30d90492012-06-22 12:40:19 +04003171 file->f_flags = op->open_flag;
Nick Piggin31e6b012011-01-07 17:49:52 +11003172
Al Virobb458c62013-07-13 13:26:37 +04003173 if (unlikely(file->f_flags & __O_TMPFILE)) {
Al Viro60545d02013-06-07 01:20:27 -04003174 error = do_tmpfile(dfd, pathname, nd, flags, op, file, &opened);
3175 goto out;
3176 }
3177
Jeff Layton669abf42012-10-10 16:43:10 -04003178 error = path_init(dfd, pathname->name, flags | LOOKUP_PARENT, nd, &base);
Nick Piggin31e6b012011-01-07 17:49:52 +11003179 if (unlikely(error))
Al Viro2675a4e2012-06-22 12:41:10 +04003180 goto out;
Nick Piggin31e6b012011-01-07 17:49:52 +11003181
Al Virofe2d35f2011-03-05 22:58:25 -05003182 current->total_link_count = 0;
Jeff Layton669abf42012-10-10 16:43:10 -04003183 error = link_path_walk(pathname->name, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11003184 if (unlikely(error))
Al Viro2675a4e2012-06-22 12:41:10 +04003185 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186
Al Viro2675a4e2012-06-22 12:41:10 +04003187 error = do_last(nd, &path, file, op, &opened, pathname);
3188 while (unlikely(error > 0)) { /* trailing symlink */
Nick Piggin7b9337a2011-01-14 08:42:43 +00003189 struct path link = path;
Al Virodef4af32009-12-26 08:37:05 -05003190 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04003191 if (!(nd->flags & LOOKUP_FOLLOW)) {
Al Viro73d049a2011-03-11 12:08:24 -05003192 path_put_conditional(&path, nd);
3193 path_put(&nd->path);
Al Viro2675a4e2012-06-22 12:41:10 +04003194 error = -ELOOP;
Al Viro40b39132011-03-09 16:22:18 -05003195 break;
3196 }
Kees Cook800179c2012-07-25 17:29:07 -07003197 error = may_follow_link(&link, nd);
3198 if (unlikely(error))
3199 break;
Al Viro73d049a2011-03-11 12:08:24 -05003200 nd->flags |= LOOKUP_PARENT;
3201 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
Al Viro574197e2011-03-14 22:20:34 -04003202 error = follow_link(&link, nd, &cookie);
Al Viroc3e380b2011-02-23 13:39:45 -05003203 if (unlikely(error))
Al Viro2675a4e2012-06-22 12:41:10 +04003204 break;
3205 error = do_last(nd, &path, file, op, &opened, pathname);
Al Viro574197e2011-03-14 22:20:34 -04003206 put_link(nd, &link, cookie);
Al Viro806b6812009-12-26 07:16:40 -05003207 }
Al Viro10fa8e62009-12-26 07:09:49 -05003208out:
Al Viro73d049a2011-03-11 12:08:24 -05003209 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
3210 path_put(&nd->root);
Al Virofe2d35f2011-03-05 22:58:25 -05003211 if (base)
3212 fput(base);
Al Viro2675a4e2012-06-22 12:41:10 +04003213 if (!(opened & FILE_OPENED)) {
3214 BUG_ON(!error);
Al Viro30d90492012-06-22 12:40:19 +04003215 put_filp(file);
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003216 }
Al Viro2675a4e2012-06-22 12:41:10 +04003217 if (unlikely(error)) {
3218 if (error == -EOPENSTALE) {
3219 if (flags & LOOKUP_RCU)
3220 error = -ECHILD;
3221 else
3222 error = -ESTALE;
3223 }
3224 file = ERR_PTR(error);
3225 }
3226 return file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227}
3228
Jeff Layton669abf42012-10-10 16:43:10 -04003229struct file *do_filp_open(int dfd, struct filename *pathname,
Al Virof9652e12013-06-11 08:23:01 +04003230 const struct open_flags *op)
Al Viro13aab422011-02-23 17:54:08 -05003231{
Al Viro73d049a2011-03-11 12:08:24 -05003232 struct nameidata nd;
Al Virof9652e12013-06-11 08:23:01 +04003233 int flags = op->lookup_flags;
Al Viro13aab422011-02-23 17:54:08 -05003234 struct file *filp;
3235
Al Viro73d049a2011-03-11 12:08:24 -05003236 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
Al Viro13aab422011-02-23 17:54:08 -05003237 if (unlikely(filp == ERR_PTR(-ECHILD)))
Al Viro73d049a2011-03-11 12:08:24 -05003238 filp = path_openat(dfd, pathname, &nd, op, flags);
Al Viro13aab422011-02-23 17:54:08 -05003239 if (unlikely(filp == ERR_PTR(-ESTALE)))
Al Viro73d049a2011-03-11 12:08:24 -05003240 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
Al Viro13aab422011-02-23 17:54:08 -05003241 return filp;
3242}
3243
Al Viro73d049a2011-03-11 12:08:24 -05003244struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
Al Virof9652e12013-06-11 08:23:01 +04003245 const char *name, const struct open_flags *op)
Al Viro73d049a2011-03-11 12:08:24 -05003246{
3247 struct nameidata nd;
3248 struct file *file;
Jeff Layton669abf42012-10-10 16:43:10 -04003249 struct filename filename = { .name = name };
Al Virof9652e12013-06-11 08:23:01 +04003250 int flags = op->lookup_flags | LOOKUP_ROOT;
Al Viro73d049a2011-03-11 12:08:24 -05003251
3252 nd.root.mnt = mnt;
3253 nd.root.dentry = dentry;
3254
David Howellsb18825a2013-09-12 19:22:53 +01003255 if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN)
Al Viro73d049a2011-03-11 12:08:24 -05003256 return ERR_PTR(-ELOOP);
3257
Jeff Layton669abf42012-10-10 16:43:10 -04003258 file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_RCU);
Al Viro73d049a2011-03-11 12:08:24 -05003259 if (unlikely(file == ERR_PTR(-ECHILD)))
Jeff Layton669abf42012-10-10 16:43:10 -04003260 file = path_openat(-1, &filename, &nd, op, flags);
Al Viro73d049a2011-03-11 12:08:24 -05003261 if (unlikely(file == ERR_PTR(-ESTALE)))
Jeff Layton669abf42012-10-10 16:43:10 -04003262 file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_REVAL);
Al Viro73d049a2011-03-11 12:08:24 -05003263 return file;
3264}
3265
Jeff Layton1ac12b42012-12-11 12:10:06 -05003266struct dentry *kern_path_create(int dfd, const char *pathname,
3267 struct path *path, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003269 struct dentry *dentry = ERR_PTR(-EEXIST);
Al Viroed75e952011-06-27 16:53:43 -04003270 struct nameidata nd;
Jan Karac30dabf2012-06-12 16:20:30 +02003271 int err2;
Jeff Layton1ac12b42012-12-11 12:10:06 -05003272 int error;
3273 bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
3274
3275 /*
3276 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
3277 * other flags passed in are ignored!
3278 */
3279 lookup_flags &= LOOKUP_REVAL;
3280
3281 error = do_path_lookup(dfd, pathname, LOOKUP_PARENT|lookup_flags, &nd);
Al Viroed75e952011-06-27 16:53:43 -04003282 if (error)
3283 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003285 /*
3286 * Yucky last component or no last component at all?
3287 * (foo/., foo/.., /////)
3288 */
Al Viroed75e952011-06-27 16:53:43 -04003289 if (nd.last_type != LAST_NORM)
3290 goto out;
3291 nd.flags &= ~LOOKUP_PARENT;
3292 nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003293
Jan Karac30dabf2012-06-12 16:20:30 +02003294 /* don't fail immediately if it's r/o, at least try to report other errors */
3295 err2 = mnt_want_write(nd.path.mnt);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003296 /*
3297 * Do the final lookup.
3298 */
Al Viroed75e952011-06-27 16:53:43 -04003299 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3300 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 if (IS_ERR(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003302 goto unlock;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003303
Al Viroa8104a92012-07-20 02:25:00 +04003304 error = -EEXIST;
David Howellsb18825a2013-09-12 19:22:53 +01003305 if (d_is_positive(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003306 goto fail;
David Howellsb18825a2013-09-12 19:22:53 +01003307
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003308 /*
3309 * Special case - lookup gave negative, but... we had foo/bar/
3310 * From the vfs_mknod() POV we just have a negative dentry -
3311 * all is fine. Let's be bastards - you had / on the end, you've
3312 * been asking for (non-existent) directory. -ENOENT for you.
3313 */
Al Viroed75e952011-06-27 16:53:43 -04003314 if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
Al Viroa8104a92012-07-20 02:25:00 +04003315 error = -ENOENT;
Al Viroed75e952011-06-27 16:53:43 -04003316 goto fail;
Al Viroe9baf6e2008-05-15 04:49:12 -04003317 }
Jan Karac30dabf2012-06-12 16:20:30 +02003318 if (unlikely(err2)) {
3319 error = err2;
Al Viroa8104a92012-07-20 02:25:00 +04003320 goto fail;
Jan Karac30dabf2012-06-12 16:20:30 +02003321 }
Al Viroed75e952011-06-27 16:53:43 -04003322 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324fail:
Al Viroa8104a92012-07-20 02:25:00 +04003325 dput(dentry);
3326 dentry = ERR_PTR(error);
3327unlock:
Al Viroed75e952011-06-27 16:53:43 -04003328 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Karac30dabf2012-06-12 16:20:30 +02003329 if (!err2)
3330 mnt_drop_write(nd.path.mnt);
Al Viroed75e952011-06-27 16:53:43 -04003331out:
3332 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 return dentry;
3334}
Al Virodae6ad82011-06-26 11:50:15 -04003335EXPORT_SYMBOL(kern_path_create);
3336
Al Viro921a1652012-07-20 01:15:31 +04003337void done_path_create(struct path *path, struct dentry *dentry)
3338{
3339 dput(dentry);
3340 mutex_unlock(&path->dentry->d_inode->i_mutex);
Al Viroa8104a92012-07-20 02:25:00 +04003341 mnt_drop_write(path->mnt);
Al Viro921a1652012-07-20 01:15:31 +04003342 path_put(path);
3343}
3344EXPORT_SYMBOL(done_path_create);
3345
Jeff Layton1ac12b42012-12-11 12:10:06 -05003346struct dentry *user_path_create(int dfd, const char __user *pathname,
3347 struct path *path, unsigned int lookup_flags)
Al Virodae6ad82011-06-26 11:50:15 -04003348{
Jeff Layton91a27b22012-10-10 15:25:28 -04003349 struct filename *tmp = getname(pathname);
Al Virodae6ad82011-06-26 11:50:15 -04003350 struct dentry *res;
3351 if (IS_ERR(tmp))
3352 return ERR_CAST(tmp);
Jeff Layton1ac12b42012-12-11 12:10:06 -05003353 res = kern_path_create(dfd, tmp->name, path, lookup_flags);
Al Virodae6ad82011-06-26 11:50:15 -04003354 putname(tmp);
3355 return res;
3356}
3357EXPORT_SYMBOL(user_path_create);
3358
Al Viro1a67aaf2011-07-26 01:52:52 -04003359int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003361 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362
3363 if (error)
3364 return error;
3365
Eric W. Biederman975d6b32011-11-13 12:16:43 -08003366 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 return -EPERM;
3368
Al Viroacfa4382008-12-04 10:06:33 -05003369 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 return -EPERM;
3371
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07003372 error = devcgroup_inode_mknod(mode, dev);
3373 if (error)
3374 return error;
3375
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 error = security_inode_mknod(dir, dentry, mode, dev);
3377 if (error)
3378 return error;
3379
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003381 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003382 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 return error;
3384}
3385
Al Virof69aac02011-07-26 04:31:40 -04003386static int may_mknod(umode_t mode)
Dave Hansen463c3192008-02-15 14:37:57 -08003387{
3388 switch (mode & S_IFMT) {
3389 case S_IFREG:
3390 case S_IFCHR:
3391 case S_IFBLK:
3392 case S_IFIFO:
3393 case S_IFSOCK:
3394 case 0: /* zero mode translates to S_IFREG */
3395 return 0;
3396 case S_IFDIR:
3397 return -EPERM;
3398 default:
3399 return -EINVAL;
3400 }
3401}
3402
Al Viro8208a222011-07-25 17:32:17 -04003403SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003404 unsigned, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405{
Al Viro2ad94ae2008-07-21 09:32:51 -04003406 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003407 struct path path;
3408 int error;
Jeff Layton972567f2012-12-20 16:00:10 -05003409 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410
Al Viro8e4bfca2012-07-20 01:17:26 +04003411 error = may_mknod(mode);
3412 if (error)
3413 return error;
Jeff Layton972567f2012-12-20 16:00:10 -05003414retry:
3415 dentry = user_path_create(dfd, filename, &path, lookup_flags);
Al Virodae6ad82011-06-26 11:50:15 -04003416 if (IS_ERR(dentry))
3417 return PTR_ERR(dentry);
Al Viro2ad94ae2008-07-21 09:32:51 -04003418
Al Virodae6ad82011-06-26 11:50:15 -04003419 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003420 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003421 error = security_path_mknod(&path, dentry, mode, dev);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003422 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04003423 goto out;
Dave Hansen463c3192008-02-15 14:37:57 -08003424 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 case 0: case S_IFREG:
Al Viro312b63f2012-06-10 18:09:36 -04003426 error = vfs_create(path.dentry->d_inode,dentry,mode,true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 break;
3428 case S_IFCHR: case S_IFBLK:
Al Virodae6ad82011-06-26 11:50:15 -04003429 error = vfs_mknod(path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 new_decode_dev(dev));
3431 break;
3432 case S_IFIFO: case S_IFSOCK:
Al Virodae6ad82011-06-26 11:50:15 -04003433 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 }
Al Viroa8104a92012-07-20 02:25:00 +04003436out:
Al Viro921a1652012-07-20 01:15:31 +04003437 done_path_create(&path, dentry);
Jeff Layton972567f2012-12-20 16:00:10 -05003438 if (retry_estale(error, lookup_flags)) {
3439 lookup_flags |= LOOKUP_REVAL;
3440 goto retry;
3441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 return error;
3443}
3444
Al Viro8208a222011-07-25 17:32:17 -04003445SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003446{
3447 return sys_mknodat(AT_FDCWD, filename, mode, dev);
3448}
3449
Al Viro18bb1db2011-07-26 01:41:39 -04003450int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003452 int error = may_create(dir, dentry);
Al Viro8de52772012-02-06 12:45:27 -05003453 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454
3455 if (error)
3456 return error;
3457
Al Viroacfa4382008-12-04 10:06:33 -05003458 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 return -EPERM;
3460
3461 mode &= (S_IRWXUGO|S_ISVTX);
3462 error = security_inode_mkdir(dir, dentry, mode);
3463 if (error)
3464 return error;
3465
Al Viro8de52772012-02-06 12:45:27 -05003466 if (max_links && dir->i_nlink >= max_links)
3467 return -EMLINK;
3468
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003470 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003471 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 return error;
3473}
3474
Al Viroa218d0f2011-11-21 14:59:34 -05003475SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476{
Dave Hansen6902d922006-09-30 23:29:01 -07003477 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003478 struct path path;
3479 int error;
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003480 unsigned int lookup_flags = LOOKUP_DIRECTORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003482retry:
3483 dentry = user_path_create(dfd, pathname, &path, lookup_flags);
Dave Hansen6902d922006-09-30 23:29:01 -07003484 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003485 return PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003486
Al Virodae6ad82011-06-26 11:50:15 -04003487 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003488 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003489 error = security_path_mkdir(&path, dentry, mode);
Al Viroa8104a92012-07-20 02:25:00 +04003490 if (!error)
3491 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
Al Viro921a1652012-07-20 01:15:31 +04003492 done_path_create(&path, dentry);
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003493 if (retry_estale(error, lookup_flags)) {
3494 lookup_flags |= LOOKUP_REVAL;
3495 goto retry;
3496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 return error;
3498}
3499
Al Viroa218d0f2011-11-21 14:59:34 -05003500SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003501{
3502 return sys_mkdirat(AT_FDCWD, pathname, mode);
3503}
3504
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505/*
Sage Weila71905f2011-05-24 13:06:08 -07003506 * The dentry_unhash() helper will try to drop the dentry early: we
J. Bruce Fieldsc0d02592012-02-15 11:48:40 -05003507 * should have a usage count of 1 if we're the only user of this
Sage Weila71905f2011-05-24 13:06:08 -07003508 * dentry, and if that is true (possibly after pruning the dcache),
3509 * then we drop the dentry now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 *
3511 * A low-level filesystem can, if it choses, legally
3512 * do a
3513 *
3514 * if (!d_unhashed(dentry))
3515 * return -EBUSY;
3516 *
3517 * if it cannot handle the case of removing a directory
3518 * that is still in use by something else..
3519 */
3520void dentry_unhash(struct dentry *dentry)
3521{
Vasily Averindc168422006-12-06 20:37:07 -08003522 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 spin_lock(&dentry->d_lock);
Waiman Long98474232013-08-28 18:24:59 -07003524 if (dentry->d_lockref.count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 __d_drop(dentry);
3526 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527}
3528
3529int vfs_rmdir(struct inode *dir, struct dentry *dentry)
3530{
3531 int error = may_delete(dir, dentry, 1);
3532
3533 if (error)
3534 return error;
3535
Al Viroacfa4382008-12-04 10:06:33 -05003536 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 return -EPERM;
3538
Al Viro1d2ef592011-09-14 18:55:41 +01003539 dget(dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003540 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541
Sage Weil912dbc12011-05-24 13:06:11 -07003542 error = -EBUSY;
3543 if (d_mountpoint(dentry))
3544 goto out;
3545
3546 error = security_inode_rmdir(dir, dentry);
3547 if (error)
3548 goto out;
3549
Sage Weil3cebde22011-05-29 21:20:59 -07003550 shrink_dcache_parent(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003551 error = dir->i_op->rmdir(dir, dentry);
3552 if (error)
3553 goto out;
3554
3555 dentry->d_inode->i_flags |= S_DEAD;
3556 dont_mount(dentry);
3557
3558out:
3559 mutex_unlock(&dentry->d_inode->i_mutex);
Al Viro1d2ef592011-09-14 18:55:41 +01003560 dput(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003561 if (!error)
3562 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 return error;
3564}
3565
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003566static long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567{
3568 int error = 0;
Jeff Layton91a27b22012-10-10 15:25:28 -04003569 struct filename *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 struct dentry *dentry;
3571 struct nameidata nd;
Jeff Laytonc6ee9202012-12-20 16:28:33 -05003572 unsigned int lookup_flags = 0;
3573retry:
3574 name = user_path_parent(dfd, pathname, &nd, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04003575 if (IS_ERR(name))
3576 return PTR_ERR(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577
3578 switch(nd.last_type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003579 case LAST_DOTDOT:
3580 error = -ENOTEMPTY;
3581 goto exit1;
3582 case LAST_DOT:
3583 error = -EINVAL;
3584 goto exit1;
3585 case LAST_ROOT:
3586 error = -EBUSY;
3587 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003589
3590 nd.flags &= ~LOOKUP_PARENT;
Jan Karac30dabf2012-06-12 16:20:30 +02003591 error = mnt_want_write(nd.path.mnt);
3592 if (error)
3593 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003594
Jan Blunck4ac91372008-02-14 19:34:32 -08003595 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08003596 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003598 if (IS_ERR(dentry))
3599 goto exit2;
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04003600 if (!dentry->d_inode) {
3601 error = -ENOENT;
3602 goto exit3;
3603 }
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003604 error = security_path_rmdir(&nd.path, dentry);
3605 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02003606 goto exit3;
Jan Blunck4ac91372008-02-14 19:34:32 -08003607 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
Dave Hansen06227532008-02-15 14:37:34 -08003608exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07003609 dput(dentry);
3610exit2:
Jan Blunck4ac91372008-02-14 19:34:32 -08003611 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Karac30dabf2012-06-12 16:20:30 +02003612 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003614 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 putname(name);
Jeff Laytonc6ee9202012-12-20 16:28:33 -05003616 if (retry_estale(error, lookup_flags)) {
3617 lookup_flags |= LOOKUP_REVAL;
3618 goto retry;
3619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 return error;
3621}
3622
Heiko Carstens3cdad422009-01-14 14:14:22 +01003623SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003624{
3625 return do_rmdir(AT_FDCWD, pathname);
3626}
3627
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003628/**
3629 * vfs_unlink - unlink a filesystem object
3630 * @dir: parent directory
3631 * @dentry: victim
3632 * @delegated_inode: returns victim inode, if the inode is delegated.
3633 *
3634 * The caller must hold dir->i_mutex.
3635 *
3636 * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
3637 * return a reference to the inode in delegated_inode. The caller
3638 * should then break the delegation on that inode and retry. Because
3639 * breaking a delegation may take a long time, the caller should drop
3640 * dir->i_mutex before doing so.
3641 *
3642 * Alternatively, a caller may pass NULL for delegated_inode. This may
3643 * be appropriate for callers that expect the underlying filesystem not
3644 * to be NFS exported.
3645 */
3646int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647{
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003648 struct inode *target = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 int error = may_delete(dir, dentry, 0);
3650
3651 if (error)
3652 return error;
3653
Al Viroacfa4382008-12-04 10:06:33 -05003654 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 return -EPERM;
3656
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003657 mutex_lock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 if (d_mountpoint(dentry))
3659 error = -EBUSY;
3660 else {
3661 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05003662 if (!error) {
J. Bruce Fields5a146962012-08-28 07:50:40 -07003663 error = try_break_deleg(target, delegated_inode);
3664 if (error)
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003665 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 error = dir->i_op->unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05003667 if (!error)
Al Virod83c49f2010-04-30 17:17:09 -04003668 dont_mount(dentry);
Al Virobec10522010-03-03 14:12:08 -05003669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 }
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003671out:
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003672 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673
3674 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
3675 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003676 fsnotify_link_count(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 }
Robert Love0eeca282005-07-12 17:06:03 -04003679
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 return error;
3681}
3682
3683/*
3684 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003685 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 * writeout happening, and we don't want to prevent access to the directory
3687 * while waiting on the I/O.
3688 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003689static long do_unlinkat(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690{
Al Viro2ad94ae2008-07-21 09:32:51 -04003691 int error;
Jeff Layton91a27b22012-10-10 15:25:28 -04003692 struct filename *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 struct dentry *dentry;
3694 struct nameidata nd;
3695 struct inode *inode = NULL;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003696 struct inode *delegated_inode = NULL;
Jeff Layton5d18f812012-12-20 16:38:04 -05003697 unsigned int lookup_flags = 0;
3698retry:
3699 name = user_path_parent(dfd, pathname, &nd, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04003700 if (IS_ERR(name))
3701 return PTR_ERR(name);
Al Viro2ad94ae2008-07-21 09:32:51 -04003702
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 error = -EISDIR;
3704 if (nd.last_type != LAST_NORM)
3705 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003706
3707 nd.flags &= ~LOOKUP_PARENT;
Jan Karac30dabf2012-06-12 16:20:30 +02003708 error = mnt_want_write(nd.path.mnt);
3709 if (error)
3710 goto exit1;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003711retry_deleg:
Jan Blunck4ac91372008-02-14 19:34:32 -08003712 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08003713 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 error = PTR_ERR(dentry);
3715 if (!IS_ERR(dentry)) {
3716 /* Why not before? Because we want correct error value */
Török Edwin50338b82011-06-16 00:06:14 +03003717 if (nd.last.name[nd.last.len])
3718 goto slashes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 inode = dentry->d_inode;
David Howellsb18825a2013-09-12 19:22:53 +01003720 if (d_is_negative(dentry))
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04003721 goto slashes;
3722 ihold(inode);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003723 error = security_path_unlink(&nd.path, dentry);
3724 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02003725 goto exit2;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003726 error = vfs_unlink(nd.path.dentry->d_inode, dentry, &delegated_inode);
Jan Karac30dabf2012-06-12 16:20:30 +02003727exit2:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 dput(dentry);
3729 }
Jan Blunck4ac91372008-02-14 19:34:32 -08003730 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 if (inode)
3732 iput(inode); /* truncate the inode here */
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003733 inode = NULL;
3734 if (delegated_inode) {
J. Bruce Fields5a146962012-08-28 07:50:40 -07003735 error = break_deleg_wait(&delegated_inode);
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003736 if (!error)
3737 goto retry_deleg;
3738 }
Jan Karac30dabf2012-06-12 16:20:30 +02003739 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003741 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 putname(name);
Jeff Layton5d18f812012-12-20 16:38:04 -05003743 if (retry_estale(error, lookup_flags)) {
3744 lookup_flags |= LOOKUP_REVAL;
3745 inode = NULL;
3746 goto retry;
3747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 return error;
3749
3750slashes:
David Howellsb18825a2013-09-12 19:22:53 +01003751 if (d_is_negative(dentry))
3752 error = -ENOENT;
Miklos Szeredidb8bf542014-04-01 17:08:41 +02003753 else if (d_is_dir(dentry))
David Howellsb18825a2013-09-12 19:22:53 +01003754 error = -EISDIR;
3755 else
3756 error = -ENOTDIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 goto exit2;
3758}
3759
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003760SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003761{
3762 if ((flag & ~AT_REMOVEDIR) != 0)
3763 return -EINVAL;
3764
3765 if (flag & AT_REMOVEDIR)
3766 return do_rmdir(dfd, pathname);
3767
3768 return do_unlinkat(dfd, pathname);
3769}
3770
Heiko Carstens3480b252009-01-14 14:14:16 +01003771SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003772{
3773 return do_unlinkat(AT_FDCWD, pathname);
3774}
3775
Miklos Szeredidb2e7472008-06-24 16:50:16 +02003776int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003778 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779
3780 if (error)
3781 return error;
3782
Al Viroacfa4382008-12-04 10:06:33 -05003783 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 return -EPERM;
3785
3786 error = security_inode_symlink(dir, dentry, oldname);
3787 if (error)
3788 return error;
3789
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003791 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003792 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 return error;
3794}
3795
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003796SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
3797 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798{
Al Viro2ad94ae2008-07-21 09:32:51 -04003799 int error;
Jeff Layton91a27b22012-10-10 15:25:28 -04003800 struct filename *from;
Dave Hansen6902d922006-09-30 23:29:01 -07003801 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003802 struct path path;
Jeff Laytonf46d3562012-12-11 12:10:08 -05003803 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804
3805 from = getname(oldname);
Al Viro2ad94ae2008-07-21 09:32:51 -04003806 if (IS_ERR(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 return PTR_ERR(from);
Jeff Laytonf46d3562012-12-11 12:10:08 -05003808retry:
3809 dentry = user_path_create(newdfd, newname, &path, lookup_flags);
Dave Hansen6902d922006-09-30 23:29:01 -07003810 error = PTR_ERR(dentry);
3811 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003812 goto out_putname;
Dave Hansen6902d922006-09-30 23:29:01 -07003813
Jeff Layton91a27b22012-10-10 15:25:28 -04003814 error = security_path_symlink(&path, dentry, from->name);
Al Viroa8104a92012-07-20 02:25:00 +04003815 if (!error)
Jeff Layton91a27b22012-10-10 15:25:28 -04003816 error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
Al Viro921a1652012-07-20 01:15:31 +04003817 done_path_create(&path, dentry);
Jeff Laytonf46d3562012-12-11 12:10:08 -05003818 if (retry_estale(error, lookup_flags)) {
3819 lookup_flags |= LOOKUP_REVAL;
3820 goto retry;
3821 }
Dave Hansen6902d922006-09-30 23:29:01 -07003822out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 putname(from);
3824 return error;
3825}
3826
Heiko Carstens3480b252009-01-14 14:14:16 +01003827SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003828{
3829 return sys_symlinkat(oldname, AT_FDCWD, newname);
3830}
3831
J. Bruce Fields146a8592011-09-20 17:14:31 -04003832/**
3833 * vfs_link - create a new link
3834 * @old_dentry: object to be linked
3835 * @dir: new parent
3836 * @new_dentry: where to create the new link
3837 * @delegated_inode: returns inode needing a delegation break
3838 *
3839 * The caller must hold dir->i_mutex
3840 *
3841 * If vfs_link discovers a delegation on the to-be-linked file in need
3842 * of breaking, it will return -EWOULDBLOCK and return a reference to the
3843 * inode in delegated_inode. The caller should then break the delegation
3844 * and retry. Because breaking a delegation may take a long time, the
3845 * caller should drop the i_mutex before doing so.
3846 *
3847 * Alternatively, a caller may pass NULL for delegated_inode. This may
3848 * be appropriate for callers that expect the underlying filesystem not
3849 * to be NFS exported.
3850 */
3851int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852{
3853 struct inode *inode = old_dentry->d_inode;
Al Viro8de52772012-02-06 12:45:27 -05003854 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 int error;
3856
3857 if (!inode)
3858 return -ENOENT;
3859
Miklos Szeredia95164d2008-07-30 15:08:48 +02003860 error = may_create(dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 if (error)
3862 return error;
3863
3864 if (dir->i_sb != inode->i_sb)
3865 return -EXDEV;
3866
3867 /*
3868 * A link to an append-only or immutable file cannot be created.
3869 */
3870 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3871 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05003872 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003874 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 return -EPERM;
3876
3877 error = security_inode_link(old_dentry, dir, new_dentry);
3878 if (error)
3879 return error;
3880
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003881 mutex_lock(&inode->i_mutex);
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05303882 /* Make sure we don't allow creating hardlink to an unlinked file */
Al Virof4e0c302013-06-11 08:34:36 +04003883 if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05303884 error = -ENOENT;
Al Viro8de52772012-02-06 12:45:27 -05003885 else if (max_links && inode->i_nlink >= max_links)
3886 error = -EMLINK;
J. Bruce Fields146a8592011-09-20 17:14:31 -04003887 else {
3888 error = try_break_deleg(inode, delegated_inode);
3889 if (!error)
3890 error = dir->i_op->link(old_dentry, dir, new_dentry);
3891 }
Al Virof4e0c302013-06-11 08:34:36 +04003892
3893 if (!error && (inode->i_state & I_LINKABLE)) {
3894 spin_lock(&inode->i_lock);
3895 inode->i_state &= ~I_LINKABLE;
3896 spin_unlock(&inode->i_lock);
3897 }
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003898 mutex_unlock(&inode->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07003899 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003900 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 return error;
3902}
3903
3904/*
3905 * Hardlinks are often used in delicate situations. We avoid
3906 * security-related surprises by not following symlinks on the
3907 * newname. --KAB
3908 *
3909 * We don't follow them on the oldname either to be compatible
3910 * with linux 2.0, and to avoid hard-linking to directories
3911 * and other special files. --ADM
3912 */
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003913SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
3914 int, newdfd, const char __user *, newname, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915{
3916 struct dentry *new_dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003917 struct path old_path, new_path;
J. Bruce Fields146a8592011-09-20 17:14:31 -04003918 struct inode *delegated_inode = NULL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303919 int how = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303922 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003923 return -EINVAL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303924 /*
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07003925 * To use null names we require CAP_DAC_READ_SEARCH
3926 * This ensures that not everyone will be able to create
3927 * handlink using the passed filedescriptor.
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303928 */
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07003929 if (flags & AT_EMPTY_PATH) {
3930 if (!capable(CAP_DAC_READ_SEARCH))
3931 return -ENOENT;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303932 how = LOOKUP_EMPTY;
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07003933 }
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003934
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303935 if (flags & AT_SYMLINK_FOLLOW)
3936 how |= LOOKUP_FOLLOW;
Jeff Layton442e31c2012-12-20 16:15:38 -05003937retry:
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303938 error = user_path_at(olddfd, oldname, how, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04003940 return error;
3941
Jeff Layton442e31c2012-12-20 16:15:38 -05003942 new_dentry = user_path_create(newdfd, newname, &new_path,
3943 (how & LOOKUP_REVAL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003945 if (IS_ERR(new_dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003946 goto out;
3947
3948 error = -EXDEV;
3949 if (old_path.mnt != new_path.mnt)
3950 goto out_dput;
Kees Cook800179c2012-07-25 17:29:07 -07003951 error = may_linkat(&old_path);
3952 if (unlikely(error))
3953 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04003954 error = security_path_link(old_path.dentry, &new_path, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003955 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04003956 goto out_dput;
J. Bruce Fields146a8592011-09-20 17:14:31 -04003957 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode);
Dave Hansen75c3f292008-02-15 14:37:45 -08003958out_dput:
Al Viro921a1652012-07-20 01:15:31 +04003959 done_path_create(&new_path, new_dentry);
J. Bruce Fields146a8592011-09-20 17:14:31 -04003960 if (delegated_inode) {
3961 error = break_deleg_wait(&delegated_inode);
Oleg Drokind22e6332014-01-31 15:41:58 -05003962 if (!error) {
3963 path_put(&old_path);
J. Bruce Fields146a8592011-09-20 17:14:31 -04003964 goto retry;
Oleg Drokind22e6332014-01-31 15:41:58 -05003965 }
J. Bruce Fields146a8592011-09-20 17:14:31 -04003966 }
Jeff Layton442e31c2012-12-20 16:15:38 -05003967 if (retry_estale(error, how)) {
Oleg Drokind22e6332014-01-31 15:41:58 -05003968 path_put(&old_path);
Jeff Layton442e31c2012-12-20 16:15:38 -05003969 how |= LOOKUP_REVAL;
3970 goto retry;
3971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972out:
Al Viro2d8f3032008-07-22 09:59:21 -04003973 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974
3975 return error;
3976}
3977
Heiko Carstens3480b252009-01-14 14:14:16 +01003978SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003979{
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003980 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003981}
3982
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02003983/**
3984 * vfs_rename - rename a filesystem object
3985 * @old_dir: parent of source
3986 * @old_dentry: source
3987 * @new_dir: parent of destination
3988 * @new_dentry: destination
3989 * @delegated_inode: returns an inode needing a delegation break
Miklos Szeredi148622f2014-04-01 17:08:42 +02003990 * @flags: rename flags
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02003991 *
3992 * The caller must hold multiple mutexes--see lock_rename()).
3993 *
3994 * If vfs_rename discovers a delegation in need of breaking at either
3995 * the source or destination, it will return -EWOULDBLOCK and return a
3996 * reference to the inode in delegated_inode. The caller should then
3997 * break the delegation and retry. Because breaking a delegation may
3998 * take a long time, the caller should drop all locks before doing
3999 * so.
4000 *
4001 * Alternatively, a caller may pass NULL for delegated_inode. This may
4002 * be appropriate for callers that expect the underlying filesystem not
4003 * to be NFS exported.
4004 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005 * The worst of all namespace operations - renaming directory. "Perverted"
4006 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
4007 * Problems:
4008 * a) we can get into loop creation. Check is done in is_subdir().
4009 * b) race potential - two innocent renames can create a loop together.
4010 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004011 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 * story.
J. Bruce Fields6cedba82012-03-05 11:40:41 -05004013 * c) we have to lock _four_ objects - parents and victim (if it exists),
4014 * and source (if it is not a directory).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004015 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 * whether the target exists). Solution: try to be smart with locking
4017 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004018 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 * move will be locked. Thus we can rank directories by the tree
4020 * (ancestors first) and rank all non-directories after them.
4021 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004022 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 * HOWEVER, it relies on the assumption that any object with ->lookup()
4024 * has no more than 1 dentry. If "hybrid" objects will ever appear,
4025 * we'd better make sure that there's no link(2) for them.
Sage Weile4eaac02011-05-24 13:06:07 -07004026 * d) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004027 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05004029 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 * locking].
4031 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004033 struct inode *new_dir, struct dentry *new_dentry,
Miklos Szeredi148622f2014-04-01 17:08:42 +02004034 struct inode **delegated_inode, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035{
4036 int error;
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02004037 bool is_dir = d_is_dir(old_dentry);
Eric Paris59b0df22010-02-08 12:53:52 -05004038 const unsigned char *old_name;
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02004039 struct inode *source = old_dentry->d_inode;
4040 struct inode *target = new_dentry->d_inode;
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004041 bool new_is_dir = false;
4042 unsigned max_links = new_dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02004044 if (source == target)
4045 return 0;
4046
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 error = may_delete(old_dir, old_dentry, is_dir);
4048 if (error)
4049 return error;
4050
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004051 if (!target) {
Miklos Szeredia95164d2008-07-30 15:08:48 +02004052 error = may_create(new_dir, new_dentry);
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004053 } else {
4054 new_is_dir = d_is_dir(new_dentry);
4055
4056 if (!(flags & RENAME_EXCHANGE))
4057 error = may_delete(new_dir, new_dentry, is_dir);
4058 else
4059 error = may_delete(new_dir, new_dentry, new_is_dir);
4060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 if (error)
4062 return error;
4063
Miklos Szeredia756aea2014-07-23 15:15:30 +02004064 if (!old_dir->i_op->rename && !old_dir->i_op->rename2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 return -EPERM;
4066
Miklos Szeredi148622f2014-04-01 17:08:42 +02004067 if (flags && !old_dir->i_op->rename2)
4068 return -EINVAL;
4069
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02004070 /*
4071 * If we are going to change the parent - check write permissions,
4072 * we'll need to flip '..'.
4073 */
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004074 if (new_dir != old_dir) {
4075 if (is_dir) {
4076 error = inode_permission(source, MAY_WRITE);
4077 if (error)
4078 return error;
4079 }
4080 if ((flags & RENAME_EXCHANGE) && new_is_dir) {
4081 error = inode_permission(target, MAY_WRITE);
4082 if (error)
4083 return error;
4084 }
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02004085 }
Robert Love0eeca282005-07-12 17:06:03 -04004086
Miklos Szeredi5371fcd2014-04-01 17:08:43 +02004087 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry,
4088 flags);
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02004089 if (error)
4090 return error;
4091
4092 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
4093 dget(new_dentry);
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004094 if (!is_dir || (flags & RENAME_EXCHANGE))
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02004095 lock_two_nondirectories(source, target);
4096 else if (target)
4097 mutex_lock(&target->i_mutex);
4098
4099 error = -EBUSY;
4100 if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
4101 goto out;
4102
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004103 if (max_links && new_dir != old_dir) {
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02004104 error = -EMLINK;
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004105 if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links)
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02004106 goto out;
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004107 if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir &&
4108 old_dir->i_nlink >= max_links)
4109 goto out;
4110 }
4111 if (is_dir && !(flags & RENAME_EXCHANGE) && target)
4112 shrink_dcache_parent(new_dentry);
4113 if (!is_dir) {
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02004114 error = try_break_deleg(source, delegated_inode);
4115 if (error)
4116 goto out;
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004117 }
4118 if (target && !new_is_dir) {
4119 error = try_break_deleg(target, delegated_inode);
4120 if (error)
4121 goto out;
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02004122 }
Miklos Szeredia756aea2014-07-23 15:15:30 +02004123 if (!old_dir->i_op->rename2) {
Miklos Szeredi148622f2014-04-01 17:08:42 +02004124 error = old_dir->i_op->rename(old_dir, old_dentry,
4125 new_dir, new_dentry);
4126 } else {
Miklos Szeredia756aea2014-07-23 15:15:30 +02004127 WARN_ON(old_dir->i_op->rename != NULL);
Miklos Szeredi148622f2014-04-01 17:08:42 +02004128 error = old_dir->i_op->rename2(old_dir, old_dentry,
4129 new_dir, new_dentry, flags);
4130 }
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02004131 if (error)
4132 goto out;
4133
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004134 if (!(flags & RENAME_EXCHANGE) && target) {
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02004135 if (is_dir)
4136 target->i_flags |= S_DEAD;
4137 dont_mount(new_dentry);
4138 }
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004139 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) {
4140 if (!(flags & RENAME_EXCHANGE))
4141 d_move(old_dentry, new_dentry);
4142 else
4143 d_exchange(old_dentry, new_dentry);
4144 }
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02004145out:
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004146 if (!is_dir || (flags & RENAME_EXCHANGE))
Miklos Szeredi75a4b5d2014-04-01 17:08:42 +02004147 unlock_two_nondirectories(source, target);
4148 else if (target)
4149 mutex_unlock(&target->i_mutex);
4150 dput(new_dentry);
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004151 if (!error) {
Al Viro123df292009-12-25 04:57:57 -05004152 fsnotify_move(old_dir, new_dir, old_name, is_dir,
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004153 !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
4154 if (flags & RENAME_EXCHANGE) {
4155 fsnotify_move(new_dir, old_dir, old_dentry->d_name.name,
4156 new_is_dir, NULL, new_dentry);
4157 }
4158 }
Robert Love0eeca282005-07-12 17:06:03 -04004159 fsnotify_oldname_free(old_name);
4160
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 return error;
4162}
4163
Miklos Szeredi148622f2014-04-01 17:08:42 +02004164SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
4165 int, newdfd, const char __user *, newname, unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166{
Al Viro2ad94ae2008-07-21 09:32:51 -04004167 struct dentry *old_dir, *new_dir;
4168 struct dentry *old_dentry, *new_dentry;
4169 struct dentry *trap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 struct nameidata oldnd, newnd;
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004171 struct inode *delegated_inode = NULL;
Jeff Layton91a27b22012-10-10 15:25:28 -04004172 struct filename *from;
4173 struct filename *to;
Jeff Laytonc6a94282012-12-11 12:10:10 -05004174 unsigned int lookup_flags = 0;
4175 bool should_retry = false;
Al Viro2ad94ae2008-07-21 09:32:51 -04004176 int error;
Miklos Szeredi148622f2014-04-01 17:08:42 +02004177
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004178 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
4179 return -EINVAL;
4180
4181 if ((flags & RENAME_NOREPLACE) && (flags & RENAME_EXCHANGE))
Miklos Szeredi148622f2014-04-01 17:08:42 +02004182 return -EINVAL;
4183
Jeff Laytonc6a94282012-12-11 12:10:10 -05004184retry:
4185 from = user_path_parent(olddfd, oldname, &oldnd, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04004186 if (IS_ERR(from)) {
4187 error = PTR_ERR(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 goto exit;
Jeff Layton91a27b22012-10-10 15:25:28 -04004189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190
Jeff Laytonc6a94282012-12-11 12:10:10 -05004191 to = user_path_parent(newdfd, newname, &newnd, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04004192 if (IS_ERR(to)) {
4193 error = PTR_ERR(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 goto exit1;
Jeff Layton91a27b22012-10-10 15:25:28 -04004195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196
4197 error = -EXDEV;
Jan Blunck4ac91372008-02-14 19:34:32 -08004198 if (oldnd.path.mnt != newnd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 goto exit2;
4200
Jan Blunck4ac91372008-02-14 19:34:32 -08004201 old_dir = oldnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 error = -EBUSY;
4203 if (oldnd.last_type != LAST_NORM)
4204 goto exit2;
4205
Jan Blunck4ac91372008-02-14 19:34:32 -08004206 new_dir = newnd.path.dentry;
Miklos Szeredif1bdf912014-04-01 17:08:43 +02004207 if (flags & RENAME_NOREPLACE)
4208 error = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 if (newnd.last_type != LAST_NORM)
4210 goto exit2;
4211
Jan Karac30dabf2012-06-12 16:20:30 +02004212 error = mnt_want_write(oldnd.path.mnt);
4213 if (error)
4214 goto exit2;
4215
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004216 oldnd.flags &= ~LOOKUP_PARENT;
4217 newnd.flags &= ~LOOKUP_PARENT;
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004218 if (!(flags & RENAME_EXCHANGE))
4219 newnd.flags |= LOOKUP_RENAME_TARGET;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004220
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004221retry_deleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 trap = lock_rename(new_dir, old_dir);
4223
Christoph Hellwig49705b72005-11-08 21:35:06 -08004224 old_dentry = lookup_hash(&oldnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 error = PTR_ERR(old_dentry);
4226 if (IS_ERR(old_dentry))
4227 goto exit3;
4228 /* source must exist */
4229 error = -ENOENT;
David Howellsb18825a2013-09-12 19:22:53 +01004230 if (d_is_negative(old_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 goto exit4;
Christoph Hellwig49705b72005-11-08 21:35:06 -08004232 new_dentry = lookup_hash(&newnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 error = PTR_ERR(new_dentry);
4234 if (IS_ERR(new_dentry))
4235 goto exit4;
Miklos Szeredif1bdf912014-04-01 17:08:43 +02004236 error = -EEXIST;
4237 if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry))
4238 goto exit5;
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004239 if (flags & RENAME_EXCHANGE) {
4240 error = -ENOENT;
4241 if (d_is_negative(new_dentry))
4242 goto exit5;
4243
4244 if (!d_is_dir(new_dentry)) {
4245 error = -ENOTDIR;
4246 if (newnd.last.name[newnd.last.len])
4247 goto exit5;
4248 }
4249 }
Miklos Szeredif1bdf912014-04-01 17:08:43 +02004250 /* unless the source is a directory trailing slashes give -ENOTDIR */
4251 if (!d_is_dir(old_dentry)) {
4252 error = -ENOTDIR;
4253 if (oldnd.last.name[oldnd.last.len])
4254 goto exit5;
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004255 if (!(flags & RENAME_EXCHANGE) && newnd.last.name[newnd.last.len])
Miklos Szeredif1bdf912014-04-01 17:08:43 +02004256 goto exit5;
4257 }
4258 /* source should not be ancestor of target */
4259 error = -EINVAL;
4260 if (old_dentry == trap)
4261 goto exit5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 /* target should not be an ancestor of source */
Miklos Szeredic3ae3e82014-04-01 17:08:43 +02004263 if (!(flags & RENAME_EXCHANGE))
4264 error = -ENOTEMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 if (new_dentry == trap)
4266 goto exit5;
4267
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004268 error = security_path_rename(&oldnd.path, old_dentry,
Miklos Szeredi5371fcd2014-04-01 17:08:43 +02004269 &newnd.path, new_dentry, flags);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004270 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02004271 goto exit5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 error = vfs_rename(old_dir->d_inode, old_dentry,
Miklos Szeredi148622f2014-04-01 17:08:42 +02004273 new_dir->d_inode, new_dentry,
4274 &delegated_inode, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275exit5:
4276 dput(new_dentry);
4277exit4:
4278 dput(old_dentry);
4279exit3:
4280 unlock_rename(new_dir, old_dir);
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004281 if (delegated_inode) {
4282 error = break_deleg_wait(&delegated_inode);
4283 if (!error)
4284 goto retry_deleg;
4285 }
Jan Karac30dabf2012-06-12 16:20:30 +02004286 mnt_drop_write(oldnd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287exit2:
Jeff Laytonc6a94282012-12-11 12:10:10 -05004288 if (retry_estale(error, lookup_flags))
4289 should_retry = true;
Jan Blunck1d957f92008-02-14 19:34:35 -08004290 path_put(&newnd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04004291 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08004293 path_put(&oldnd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 putname(from);
Jeff Laytonc6a94282012-12-11 12:10:10 -05004295 if (should_retry) {
4296 should_retry = false;
4297 lookup_flags |= LOOKUP_REVAL;
4298 goto retry;
4299 }
Al Viro2ad94ae2008-07-21 09:32:51 -04004300exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 return error;
4302}
4303
Miklos Szeredi148622f2014-04-01 17:08:42 +02004304SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4305 int, newdfd, const char __user *, newname)
4306{
4307 return sys_renameat2(olddfd, oldname, newdfd, newname, 0);
4308}
4309
Heiko Carstensa26eab22009-01-14 14:14:17 +01004310SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004311{
Miklos Szeredi148622f2014-04-01 17:08:42 +02004312 return sys_renameat2(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004313}
4314
Al Viro0750d562014-03-14 13:42:45 -04004315int readlink_copy(char __user *buffer, int buflen, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316{
Al Viro0750d562014-03-14 13:42:45 -04004317 int len = PTR_ERR(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 if (IS_ERR(link))
4319 goto out;
4320
4321 len = strlen(link);
4322 if (len > (unsigned) buflen)
4323 len = buflen;
4324 if (copy_to_user(buffer, link, len))
4325 len = -EFAULT;
4326out:
4327 return len;
4328}
4329
4330/*
4331 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
4332 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
4333 * using) it for any given inode is up to filesystem.
4334 */
4335int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4336{
4337 struct nameidata nd;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004338 void *cookie;
Marcin Slusarz694a1762008-06-09 16:40:37 -07004339 int res;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004340
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 nd.depth = 0;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004342 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
Marcin Slusarz694a1762008-06-09 16:40:37 -07004343 if (IS_ERR(cookie))
4344 return PTR_ERR(cookie);
4345
Al Viro0750d562014-03-14 13:42:45 -04004346 res = readlink_copy(buffer, buflen, nd_get_link(&nd));
Marcin Slusarz694a1762008-06-09 16:40:37 -07004347 if (dentry->d_inode->i_op->put_link)
4348 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
4349 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350}
4351
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352/* get the link contents into pagecache */
4353static char *page_getlink(struct dentry * dentry, struct page **ppage)
4354{
Duane Griffinebd09ab2008-12-19 20:47:12 +00004355 char *kaddr;
4356 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 struct address_space *mapping = dentry->d_inode->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -07004358 page = read_mapping_page(mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 if (IS_ERR(page))
Nick Piggin6fe69002007-05-06 14:49:04 -07004360 return (char*)page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 *ppage = page;
Duane Griffinebd09ab2008-12-19 20:47:12 +00004362 kaddr = kmap(page);
4363 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
4364 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365}
4366
4367int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4368{
4369 struct page *page = NULL;
Al Viro0750d562014-03-14 13:42:45 -04004370 int res = readlink_copy(buffer, buflen, page_getlink(dentry, &page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 if (page) {
4372 kunmap(page);
4373 page_cache_release(page);
4374 }
4375 return res;
4376}
4377
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004378void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004380 struct page *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 nd_set_link(nd, page_getlink(dentry, &page));
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004382 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383}
4384
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004385void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004387 struct page *page = cookie;
4388
4389 if (page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 kunmap(page);
4391 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 }
4393}
4394
Nick Piggin54566b22009-01-04 12:00:53 -08004395/*
4396 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
4397 */
4398int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399{
4400 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004401 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07004402 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08004403 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 char *kaddr;
Nick Piggin54566b22009-01-04 12:00:53 -08004405 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
4406 if (nofs)
4407 flags |= AOP_FLAG_NOFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408
NeilBrown7e53cac2006-03-25 03:07:57 -08004409retry:
Nick Pigginafddba42007-10-16 01:25:01 -07004410 err = pagecache_write_begin(NULL, mapping, 0, len-1,
Nick Piggin54566b22009-01-04 12:00:53 -08004411 flags, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07004413 goto fail;
4414
Cong Wange8e3c3d2011-11-25 23:14:27 +08004415 kaddr = kmap_atomic(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 memcpy(kaddr, symname, len-1);
Cong Wange8e3c3d2011-11-25 23:14:27 +08004417 kunmap_atomic(kaddr);
Nick Pigginafddba42007-10-16 01:25:01 -07004418
4419 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4420 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 if (err < 0)
4422 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07004423 if (err < len-1)
4424 goto retry;
4425
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 mark_inode_dirty(inode);
4427 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428fail:
4429 return err;
4430}
4431
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004432int page_symlink(struct inode *inode, const char *symname, int len)
4433{
4434 return __page_symlink(inode, symname, len,
Nick Piggin54566b22009-01-04 12:00:53 -08004435 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004436}
4437
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08004438const struct inode_operations page_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 .readlink = generic_readlink,
4440 .follow_link = page_follow_link_light,
4441 .put_link = page_put_link,
4442};
4443
Al Viro2d8f3032008-07-22 09:59:21 -04004444EXPORT_SYMBOL(user_path_at);
David Howellscc53ce52011-01-14 18:45:26 +00004445EXPORT_SYMBOL(follow_down_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446EXPORT_SYMBOL(follow_down);
4447EXPORT_SYMBOL(follow_up);
Al Virof6d2ac52012-08-26 12:55:54 -04004448EXPORT_SYMBOL(get_write_access); /* nfsd */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450EXPORT_SYMBOL(lookup_one_len);
4451EXPORT_SYMBOL(page_follow_link_light);
4452EXPORT_SYMBOL(page_put_link);
4453EXPORT_SYMBOL(page_readlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004454EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455EXPORT_SYMBOL(page_symlink);
4456EXPORT_SYMBOL(page_symlink_inode_operations);
Al Virod1811462008-08-02 00:49:18 -04004457EXPORT_SYMBOL(kern_path);
Josef 'Jeff' Sipek16f182002007-07-19 01:48:18 -07004458EXPORT_SYMBOL(vfs_path_lookup);
Al Virof419a2e2008-07-22 00:07:17 -04004459EXPORT_SYMBOL(inode_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460EXPORT_SYMBOL(unlock_rename);
4461EXPORT_SYMBOL(vfs_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462EXPORT_SYMBOL(vfs_link);
4463EXPORT_SYMBOL(vfs_mkdir);
4464EXPORT_SYMBOL(vfs_mknod);
4465EXPORT_SYMBOL(generic_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466EXPORT_SYMBOL(vfs_rename);
4467EXPORT_SYMBOL(vfs_rmdir);
4468EXPORT_SYMBOL(vfs_symlink);
4469EXPORT_SYMBOL(vfs_unlink);
4470EXPORT_SYMBOL(dentry_unhash);
4471EXPORT_SYMBOL(generic_readlink);