blob: 2be5120b81b3c06512c032fd4f20ed4a0c57cc36 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/namei.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7/*
8 * Some corrections by tytso.
9 */
10
11/* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
12 * lookup logic.
13 */
14/* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
15 */
16
17#include <linux/init.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050018#include <linux/export.h>
David S. Miller44696902012-05-23 20:12:50 -070019#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/slab.h>
21#include <linux/fs.h>
22#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/pagemap.h>
Robert Love0eeca282005-07-12 17:06:03 -040024#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/personality.h>
26#include <linux/security.h>
Mimi Zohar6146f0d2009-02-04 09:06:57 -050027#include <linux/ima.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/syscalls.h>
29#include <linux/mount.h>
30#include <linux/audit.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080031#include <linux/capability.h>
Trond Myklebust834f2a42005-10-18 14:20:16 -070032#include <linux/file.h>
Ulrich Drepper5590ff02006-01-18 17:43:53 -080033#include <linux/fcntl.h>
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070034#include <linux/device_cgroup.h>
Al Viro5ad4e532009-03-29 19:50:06 -040035#include <linux/fs_struct.h>
Linus Torvaldse77819e2011-07-22 19:30:19 -070036#include <linux/posix_acl.h>
Linus Torvalds99d263d2014-09-13 11:30:10 -070037#include <linux/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/uaccess.h>
39
Eric Parise81e3f42009-12-04 15:47:36 -050040#include "internal.h"
Al Viroc7105362011-11-24 18:22:03 -050041#include "mount.h"
Eric Parise81e3f42009-12-04 15:47:36 -050042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/* [Feb-1997 T. Schoebel-Theuer]
44 * Fundamental changes in the pathname lookup mechanisms (namei)
45 * were necessary because of omirr. The reason is that omirr needs
46 * to know the _real_ pathname, not the user-supplied one, in case
47 * of symlinks (and also when transname replacements occur).
48 *
49 * The new code replaces the old recursive symlink resolution with
50 * an iterative one (in case of non-nested symlink chains). It does
51 * this with calls to <fs>_follow_link().
52 * As a side effect, dir_namei(), _namei() and follow_link() are now
53 * replaced with a single function lookup_dentry() that can handle all
54 * the special cases of the former code.
55 *
56 * With the new dcache, the pathname is stored at each inode, at least as
57 * long as the refcount of the inode is positive. As a side effect, the
58 * size of the dcache depends on the inode cache and thus is dynamic.
59 *
60 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
61 * resolution to correspond with current state of the code.
62 *
63 * Note that the symlink resolution is not *completely* iterative.
64 * There is still a significant amount of tail- and mid- recursion in
65 * the algorithm. Also, note that <fs>_readlink() is not used in
66 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
67 * may return different results than <fs>_follow_link(). Many virtual
68 * filesystems (including /proc) exhibit this behavior.
69 */
70
71/* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
72 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
73 * and the name already exists in form of a symlink, try to create the new
74 * name indicated by the symlink. The old code always complained that the
75 * name already exists, due to not following the symlink even if its target
76 * is nonexistent. The new semantics affects also mknod() and link() when
Lucas De Marchi25985ed2011-03-30 22:57:33 -030077 * the name is a symlink pointing to a non-existent name.
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 *
79 * I don't know which semantics is the right one, since I have no access
80 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
81 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
82 * "old" one. Personally, I think the new semantics is much more logical.
83 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
84 * file does succeed in both HP-UX and SunOs, but not in Solaris
85 * and in the old Linux semantics.
86 */
87
88/* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
89 * semantics. See the comments in "open_namei" and "do_link" below.
90 *
91 * [10-Sep-98 Alan Modra] Another symlink change.
92 */
93
94/* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
95 * inside the path - always follow.
96 * in the last component in creation/removal/renaming - never follow.
97 * if LOOKUP_FOLLOW passed - follow.
98 * if the pathname has trailing slashes - follow.
99 * otherwise - don't follow.
100 * (applied in that order).
101 *
102 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
103 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
104 * During the 2.4 we need to fix the userland stuff depending on it -
105 * hopefully we will be able to get rid of that wart in 2.5. So far only
106 * XEmacs seems to be relying on it...
107 */
108/*
109 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
Arjan van de Vena11f3a02006-03-23 03:00:33 -0800110 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 * any extra contention...
112 */
113
114/* In order to reduce some races, while at the same time doing additional
115 * checking and hopefully speeding things up, we copy filenames to the
116 * kernel data space before using them..
117 *
118 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
119 * PATH_MAX includes the nul terminator --RR.
120 */
Jeff Layton91a27b22012-10-10 15:25:28 -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 Lutomirski23adbe12014-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 Lutomirski23adbe12014-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 Lutomirski23adbe12014-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 Lutomirski23adbe12014-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}
Al Viro4d359502014-03-14 12:20:17 -0400363EXPORT_SYMBOL(generic_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700365/*
366 * We _really_ want to just do "generic_permission()" without
367 * even looking at the inode->i_op values. So we keep a cache
368 * flag in inode->i_opflags, that says "this has not special
369 * permission function, use the fast case".
370 */
371static inline int do_inode_permission(struct inode *inode, int mask)
372{
373 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
374 if (likely(inode->i_op->permission))
375 return inode->i_op->permission(inode, mask);
376
377 /* This gets set once for the inode lifetime */
378 spin_lock(&inode->i_lock);
379 inode->i_opflags |= IOP_FASTPERM;
380 spin_unlock(&inode->i_lock);
381 }
382 return generic_permission(inode, mask);
383}
384
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200385/**
David Howells0bdaea92012-06-25 12:55:46 +0100386 * __inode_permission - Check for access rights to a given inode
387 * @inode: Inode to check permission on
388 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200389 *
David Howells0bdaea92012-06-25 12:55:46 +0100390 * Check for read/write/execute permissions on an inode.
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530391 *
392 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
David Howells0bdaea92012-06-25 12:55:46 +0100393 *
394 * This does not check for a read-only file system. You probably want
395 * inode_permission().
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200396 */
David Howells0bdaea92012-06-25 12:55:46 +0100397int __inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Al Viroe6305c42008-07-15 21:03:57 -0400399 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700401 if (unlikely(mask & MAY_WRITE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 /*
403 * Nobody gets write access to an immutable file.
404 */
405 if (IS_IMMUTABLE(inode))
406 return -EACCES;
407 }
408
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700409 retval = do_inode_permission(inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 if (retval)
411 return retval;
412
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700413 retval = devcgroup_inode_permission(inode, mask);
414 if (retval)
415 return retval;
416
Eric Parisd09ca732010-07-23 11:43:57 -0400417 return security_inode_permission(inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
Al Virof4d6ff82011-06-19 13:14:21 -0400420/**
David Howells0bdaea92012-06-25 12:55:46 +0100421 * sb_permission - Check superblock-level permissions
422 * @sb: Superblock of inode to check permission on
Randy Dunlap55852632012-08-18 17:39:25 -0700423 * @inode: Inode to check permission on
David Howells0bdaea92012-06-25 12:55:46 +0100424 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
425 *
426 * Separate out file-system wide checks from inode-specific permission checks.
427 */
428static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
429{
430 if (unlikely(mask & MAY_WRITE)) {
431 umode_t mode = inode->i_mode;
432
433 /* Nobody gets write access to a read-only fs. */
434 if ((sb->s_flags & MS_RDONLY) &&
435 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
436 return -EROFS;
437 }
438 return 0;
439}
440
441/**
442 * inode_permission - Check for access rights to a given inode
443 * @inode: Inode to check permission on
444 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
445 *
446 * Check for read/write/execute permissions on an inode. We use fs[ug]id for
447 * this, letting us set arbitrary permissions for filesystem access without
448 * changing the "normal" UIDs which are used for other things.
449 *
450 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
451 */
452int inode_permission(struct inode *inode, int mask)
453{
454 int retval;
455
456 retval = sb_permission(inode->i_sb, inode, mask);
457 if (retval)
458 return retval;
459 return __inode_permission(inode, mask);
460}
Al Viro4d359502014-03-14 12:20:17 -0400461EXPORT_SYMBOL(inode_permission);
David Howells0bdaea92012-06-25 12:55:46 +0100462
463/**
Jan Blunck5dd784d02008-02-14 19:34:38 -0800464 * path_get - get a reference to a path
465 * @path: path to get the reference to
466 *
467 * Given a path increment the reference count to the dentry and the vfsmount.
468 */
Al Virodcf787f2013-03-01 23:51:07 -0500469void path_get(const struct path *path)
Jan Blunck5dd784d02008-02-14 19:34:38 -0800470{
471 mntget(path->mnt);
472 dget(path->dentry);
473}
474EXPORT_SYMBOL(path_get);
475
476/**
Jan Blunck1d957f92008-02-14 19:34:35 -0800477 * path_put - put a reference to a path
478 * @path: path to put the reference to
479 *
480 * Given a path decrement the reference count to the dentry and the vfsmount.
481 */
Al Virodcf787f2013-03-01 23:51:07 -0500482void path_put(const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
Jan Blunck1d957f92008-02-14 19:34:35 -0800484 dput(path->dentry);
485 mntput(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
Jan Blunck1d957f92008-02-14 19:34:35 -0800487EXPORT_SYMBOL(path_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Al Viro19660af2011-03-25 10:32:48 -0400489/*
Nick Piggin31e6b012011-01-07 17:49:52 +1100490 * Path walking has 2 modes, rcu-walk and ref-walk (see
Al Viro19660af2011-03-25 10:32:48 -0400491 * Documentation/filesystems/path-lookup.txt). In situations when we can't
492 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
493 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
494 * mode. Refcounts are grabbed at the last known good point before rcu-walk
495 * got stuck, so ref-walk may continue from there. If this is not successful
496 * (eg. a seqcount has changed), then failure is returned and it's up to caller
497 * to restart the path walk from the beginning in ref-walk mode.
Nick Piggin31e6b012011-01-07 17:49:52 +1100498 */
Nick Piggin31e6b012011-01-07 17:49:52 +1100499
500/**
Al Viro19660af2011-03-25 10:32:48 -0400501 * unlazy_walk - try to switch to ref-walk mode.
502 * @nd: nameidata pathwalk data
503 * @dentry: child of nd->path.dentry or NULL
Randy Dunlap39191622011-01-08 19:36:21 -0800504 * Returns: 0 on success, -ECHILD on failure
Nick Piggin31e6b012011-01-07 17:49:52 +1100505 *
Al Viro19660af2011-03-25 10:32:48 -0400506 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
507 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
508 * @nd or NULL. Must be called from rcu-walk context.
Nick Piggin31e6b012011-01-07 17:49:52 +1100509 */
Al Viro19660af2011-03-25 10:32:48 -0400510static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
Nick Piggin31e6b012011-01-07 17:49:52 +1100511{
512 struct fs_struct *fs = current->fs;
513 struct dentry *parent = nd->path.dentry;
514
515 BUG_ON(!(nd->flags & LOOKUP_RCU));
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700516
517 /*
Al Viro48a066e2013-09-29 22:06:07 -0400518 * After legitimizing the bastards, terminate_walk()
519 * will do the right thing for non-RCU mode, and all our
520 * subsequent exit cases should rcu_read_unlock()
521 * before returning. Do vfsmount first; if dentry
522 * can't be legitimized, just set nd->path.dentry to NULL
523 * and rely on dput(NULL) being a no-op.
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700524 */
Al Viro48a066e2013-09-29 22:06:07 -0400525 if (!legitimize_mnt(nd->path.mnt, nd->m_seq))
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700526 return -ECHILD;
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700527 nd->flags &= ~LOOKUP_RCU;
Linus Torvalds15570082013-09-02 11:38:06 -0700528
Al Viro48a066e2013-09-29 22:06:07 -0400529 if (!lockref_get_not_dead(&parent->d_lockref)) {
530 nd->path.dentry = NULL;
Al Virod870b4a2013-11-29 01:48:32 -0500531 goto out;
Al Viro48a066e2013-09-29 22:06:07 -0400532 }
533
Linus Torvalds15570082013-09-02 11:38:06 -0700534 /*
535 * For a negative lookup, the lookup sequence point is the parents
536 * sequence point, and it only needs to revalidate the parent dentry.
537 *
538 * For a positive lookup, we need to move both the parent and the
539 * dentry from the RCU domain to be properly refcounted. And the
540 * sequence number in the dentry validates *both* dentry counters,
541 * since we checked the sequence number of the parent after we got
542 * the child sequence number. So we know the parent must still
543 * be valid if the child sequence number is still valid.
544 */
Al Viro19660af2011-03-25 10:32:48 -0400545 if (!dentry) {
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700546 if (read_seqcount_retry(&parent->d_seq, nd->seq))
547 goto out;
Al Viro19660af2011-03-25 10:32:48 -0400548 BUG_ON(nd->inode != parent->d_inode);
549 } else {
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700550 if (!lockref_get_not_dead(&dentry->d_lockref))
551 goto out;
552 if (read_seqcount_retry(&dentry->d_seq, nd->seq))
553 goto drop_dentry;
Al Viro19660af2011-03-25 10:32:48 -0400554 }
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700555
556 /*
557 * Sequence counts matched. Now make sure that the root is
558 * still valid and get it if required.
559 */
560 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
561 spin_lock(&fs->lock);
562 if (nd->root.mnt != fs->root.mnt || nd->root.dentry != fs->root.dentry)
563 goto unlock_and_drop_dentry;
Nick Piggin31e6b012011-01-07 17:49:52 +1100564 path_get(&nd->root);
565 spin_unlock(&fs->lock);
566 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100567
Al Viro8b61e742013-11-08 12:45:01 -0500568 rcu_read_unlock();
Nick Piggin31e6b012011-01-07 17:49:52 +1100569 return 0;
Al Viro19660af2011-03-25 10:32:48 -0400570
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700571unlock_and_drop_dentry:
572 spin_unlock(&fs->lock);
573drop_dentry:
Al Viro8b61e742013-11-08 12:45:01 -0500574 rcu_read_unlock();
Linus Torvalds15570082013-09-02 11:38:06 -0700575 dput(dentry);
Linus Torvaldsd0d27272013-09-10 12:17:49 -0700576 goto drop_root_mnt;
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700577out:
Al Viro8b61e742013-11-08 12:45:01 -0500578 rcu_read_unlock();
Linus Torvaldsd0d27272013-09-10 12:17:49 -0700579drop_root_mnt:
580 if (!(nd->flags & LOOKUP_ROOT))
581 nd->root.mnt = NULL;
Nick Piggin31e6b012011-01-07 17:49:52 +1100582 return -ECHILD;
583}
584
Al Viro4ce16ef32012-06-10 16:10:59 -0400585static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
Nick Piggin34286d62011-01-07 17:49:57 +1100586{
Al Viro4ce16ef32012-06-10 16:10:59 -0400587 return dentry->d_op->d_revalidate(dentry, flags);
Nick Piggin34286d62011-01-07 17:49:57 +1100588}
589
Al Viro9f1fafe2011-03-25 11:00:12 -0400590/**
591 * complete_walk - successful completion of path walk
592 * @nd: pointer nameidata
Jeff Layton39159de2009-12-07 12:01:50 -0500593 *
Al Viro9f1fafe2011-03-25 11:00:12 -0400594 * If we had been in RCU mode, drop out of it and legitimize nd->path.
595 * Revalidate the final result, unless we'd already done that during
596 * the path walk or the filesystem doesn't ask for it. Return 0 on
597 * success, -error on failure. In case of failure caller does not
598 * need to drop nd->path.
Jeff Layton39159de2009-12-07 12:01:50 -0500599 */
Al Viro9f1fafe2011-03-25 11:00:12 -0400600static int complete_walk(struct nameidata *nd)
Jeff Layton39159de2009-12-07 12:01:50 -0500601{
Al Viro16c2cd72011-02-22 15:50:10 -0500602 struct dentry *dentry = nd->path.dentry;
Jeff Layton39159de2009-12-07 12:01:50 -0500603 int status;
Jeff Layton39159de2009-12-07 12:01:50 -0500604
Al Viro9f1fafe2011-03-25 11:00:12 -0400605 if (nd->flags & LOOKUP_RCU) {
606 nd->flags &= ~LOOKUP_RCU;
607 if (!(nd->flags & LOOKUP_ROOT))
608 nd->root.mnt = NULL;
Linus Torvalds15570082013-09-02 11:38:06 -0700609
Al Viro48a066e2013-09-29 22:06:07 -0400610 if (!legitimize_mnt(nd->path.mnt, nd->m_seq)) {
Al Viro8b61e742013-11-08 12:45:01 -0500611 rcu_read_unlock();
Al Viro48a066e2013-09-29 22:06:07 -0400612 return -ECHILD;
613 }
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700614 if (unlikely(!lockref_get_not_dead(&dentry->d_lockref))) {
Al Viro8b61e742013-11-08 12:45:01 -0500615 rcu_read_unlock();
Al Viro48a066e2013-09-29 22:06:07 -0400616 mntput(nd->path.mnt);
Al Viro9f1fafe2011-03-25 11:00:12 -0400617 return -ECHILD;
618 }
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700619 if (read_seqcount_retry(&dentry->d_seq, nd->seq)) {
Al Viro8b61e742013-11-08 12:45:01 -0500620 rcu_read_unlock();
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700621 dput(dentry);
Al Viro48a066e2013-09-29 22:06:07 -0400622 mntput(nd->path.mnt);
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700623 return -ECHILD;
624 }
Al Viro8b61e742013-11-08 12:45:01 -0500625 rcu_read_unlock();
Al Viro9f1fafe2011-03-25 11:00:12 -0400626 }
627
Al Viro16c2cd72011-02-22 15:50:10 -0500628 if (likely(!(nd->flags & LOOKUP_JUMPED)))
Jeff Layton39159de2009-12-07 12:01:50 -0500629 return 0;
630
Jeff Laytonecf3d1f2013-02-20 11:19:05 -0500631 if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
Al Viro16c2cd72011-02-22 15:50:10 -0500632 return 0;
633
Jeff Laytonecf3d1f2013-02-20 11:19:05 -0500634 status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
Jeff Layton39159de2009-12-07 12:01:50 -0500635 if (status > 0)
636 return 0;
637
Al Viro16c2cd72011-02-22 15:50:10 -0500638 if (!status)
Jeff Layton39159de2009-12-07 12:01:50 -0500639 status = -ESTALE;
Al Viro16c2cd72011-02-22 15:50:10 -0500640
Al Viro9f1fafe2011-03-25 11:00:12 -0400641 path_put(&nd->path);
Jeff Layton39159de2009-12-07 12:01:50 -0500642 return status;
643}
644
Al Viro2a737872009-04-07 11:49:53 -0400645static __always_inline void set_root(struct nameidata *nd)
646{
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200647 if (!nd->root.mnt)
648 get_fs_root(current->fs, &nd->root);
Al Viro2a737872009-04-07 11:49:53 -0400649}
650
Al Viro6de88d72009-08-09 01:41:57 +0400651static int link_path_walk(const char *, struct nameidata *);
652
Nick Piggin31e6b012011-01-07 17:49:52 +1100653static __always_inline void set_root_rcu(struct nameidata *nd)
654{
655 if (!nd->root.mnt) {
656 struct fs_struct *fs = current->fs;
Nick Pigginc28cc362011-01-07 17:49:53 +1100657 unsigned seq;
658
659 do {
660 seq = read_seqcount_begin(&fs->seq);
661 nd->root = fs->root;
Tim Chenc1530012011-04-15 11:39:29 -0700662 nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
Nick Pigginc28cc362011-01-07 17:49:53 +1100663 } while (read_seqcount_retry(&fs->seq, seq));
Nick Piggin31e6b012011-01-07 17:49:52 +1100664 }
665}
666
Jan Blunck1d957f92008-02-14 19:34:35 -0800667static void path_put_conditional(struct path *path, struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700668{
669 dput(path->dentry);
Jan Blunck4ac91372008-02-14 19:34:32 -0800670 if (path->mnt != nd->path.mnt)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700671 mntput(path->mnt);
672}
673
Nick Piggin7b9337a2011-01-14 08:42:43 +0000674static inline void path_to_nameidata(const struct path *path,
675 struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700676{
Nick Piggin31e6b012011-01-07 17:49:52 +1100677 if (!(nd->flags & LOOKUP_RCU)) {
678 dput(nd->path.dentry);
679 if (nd->path.mnt != path->mnt)
680 mntput(nd->path.mnt);
Huang Shijie9a229682010-04-02 17:37:13 +0800681 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100682 nd->path.mnt = path->mnt;
Jan Blunck4ac91372008-02-14 19:34:32 -0800683 nd->path.dentry = path->dentry;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700684}
685
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400686/*
687 * Helper to directly jump to a known parsed path from ->follow_link,
688 * caller must have taken a reference to path beforehand.
689 */
690void nd_jump_link(struct nameidata *nd, struct path *path)
691{
692 path_put(&nd->path);
693
694 nd->path = *path;
695 nd->inode = nd->path.dentry->d_inode;
696 nd->flags |= LOOKUP_JUMPED;
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400697}
698
Al Viro574197e2011-03-14 22:20:34 -0400699static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
700{
701 struct inode *inode = link->dentry->d_inode;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400702 if (inode->i_op->put_link)
Al Viro574197e2011-03-14 22:20:34 -0400703 inode->i_op->put_link(link->dentry, nd, cookie);
704 path_put(link);
705}
706
Linus Torvalds561ec642012-10-26 10:05:07 -0700707int sysctl_protected_symlinks __read_mostly = 0;
708int sysctl_protected_hardlinks __read_mostly = 0;
Kees Cook800179c2012-07-25 17:29:07 -0700709
710/**
711 * may_follow_link - Check symlink following for unsafe situations
712 * @link: The path of the symlink
Randy Dunlap55852632012-08-18 17:39:25 -0700713 * @nd: nameidata pathwalk data
Kees Cook800179c2012-07-25 17:29:07 -0700714 *
715 * In the case of the sysctl_protected_symlinks sysctl being enabled,
716 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
717 * in a sticky world-writable directory. This is to protect privileged
718 * processes from failing races against path names that may change out
719 * from under them by way of other users creating malicious symlinks.
720 * It will permit symlinks to be followed only when outside a sticky
721 * world-writable directory, or when the uid of the symlink and follower
722 * match, or when the directory owner matches the symlink's owner.
723 *
724 * Returns 0 if following the symlink is allowed, -ve on error.
725 */
726static inline int may_follow_link(struct path *link, struct nameidata *nd)
727{
728 const struct inode *inode;
729 const struct inode *parent;
730
731 if (!sysctl_protected_symlinks)
732 return 0;
733
734 /* Allowed if owner and follower match. */
735 inode = link->dentry->d_inode;
Eric W. Biederman81abe272012-08-03 09:38:08 -0700736 if (uid_eq(current_cred()->fsuid, inode->i_uid))
Kees Cook800179c2012-07-25 17:29:07 -0700737 return 0;
738
739 /* Allowed if parent directory not sticky and world-writable. */
740 parent = nd->path.dentry->d_inode;
741 if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
742 return 0;
743
744 /* Allowed if parent directory and link owner match. */
Eric W. Biederman81abe272012-08-03 09:38:08 -0700745 if (uid_eq(parent->i_uid, inode->i_uid))
Kees Cook800179c2012-07-25 17:29:07 -0700746 return 0;
747
Sasha Levinffd8d102012-10-04 19:56:40 -0400748 audit_log_link_denied("follow_link", link);
Kees Cook800179c2012-07-25 17:29:07 -0700749 path_put_conditional(link, nd);
750 path_put(&nd->path);
751 return -EACCES;
752}
753
754/**
755 * safe_hardlink_source - Check for safe hardlink conditions
756 * @inode: the source inode to hardlink from
757 *
758 * Return false if at least one of the following conditions:
759 * - inode is not a regular file
760 * - inode is setuid
761 * - inode is setgid and group-exec
762 * - access failure for read and write
763 *
764 * Otherwise returns true.
765 */
766static bool safe_hardlink_source(struct inode *inode)
767{
768 umode_t mode = inode->i_mode;
769
770 /* Special files should not get pinned to the filesystem. */
771 if (!S_ISREG(mode))
772 return false;
773
774 /* Setuid files should not get pinned to the filesystem. */
775 if (mode & S_ISUID)
776 return false;
777
778 /* Executable setgid files should not get pinned to the filesystem. */
779 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
780 return false;
781
782 /* Hardlinking to unreadable or unwritable sources is dangerous. */
783 if (inode_permission(inode, MAY_READ | MAY_WRITE))
784 return false;
785
786 return true;
787}
788
789/**
790 * may_linkat - Check permissions for creating a hardlink
791 * @link: the source to hardlink from
792 *
793 * Block hardlink when all of:
794 * - sysctl_protected_hardlinks enabled
795 * - fsuid does not match inode
796 * - hardlink source is unsafe (see safe_hardlink_source() above)
797 * - not CAP_FOWNER
798 *
799 * Returns 0 if successful, -ve on error.
800 */
801static int may_linkat(struct path *link)
802{
803 const struct cred *cred;
804 struct inode *inode;
805
806 if (!sysctl_protected_hardlinks)
807 return 0;
808
809 cred = current_cred();
810 inode = link->dentry->d_inode;
811
812 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
813 * otherwise, it must be a safe source.
814 */
Eric W. Biederman81abe272012-08-03 09:38:08 -0700815 if (uid_eq(cred->fsuid, inode->i_uid) || safe_hardlink_source(inode) ||
Kees Cook800179c2012-07-25 17:29:07 -0700816 capable(CAP_FOWNER))
817 return 0;
818
Kees Cooka51d9ea2012-07-25 17:29:08 -0700819 audit_log_link_denied("linkat", link);
Kees Cook800179c2012-07-25 17:29:07 -0700820 return -EPERM;
821}
822
Al Virodef4af32009-12-26 08:37:05 -0500823static __always_inline int
Al Viro574197e2011-03-14 22:20:34 -0400824follow_link(struct path *link, struct nameidata *nd, void **p)
Ian Kent051d3812006-03-27 01:14:53 -0800825{
Nick Piggin7b9337a2011-01-14 08:42:43 +0000826 struct dentry *dentry = link->dentry;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400827 int error;
828 char *s;
Ian Kent051d3812006-03-27 01:14:53 -0800829
Al Viro844a3912011-02-15 00:38:26 -0500830 BUG_ON(nd->flags & LOOKUP_RCU);
831
Al Viro0e794582011-03-16 02:45:02 -0400832 if (link->mnt == nd->path.mnt)
833 mntget(link->mnt);
834
Al Viro6d7b5aa2012-06-10 04:15:17 -0400835 error = -ELOOP;
836 if (unlikely(current->total_link_count >= 40))
837 goto out_put_nd_path;
838
Al Viro574197e2011-03-14 22:20:34 -0400839 cond_resched();
840 current->total_link_count++;
841
Al Viro68ac1232012-03-15 08:21:57 -0400842 touch_atime(link);
Ian Kent051d3812006-03-27 01:14:53 -0800843 nd_set_link(nd, NULL);
844
Al Viro36f3b4f2011-02-22 21:24:38 -0500845 error = security_inode_follow_link(link->dentry, nd);
Al Viro6d7b5aa2012-06-10 04:15:17 -0400846 if (error)
847 goto out_put_nd_path;
Al Viro36f3b4f2011-02-22 21:24:38 -0500848
Al Viro86acdca12009-12-22 23:45:11 -0500849 nd->last_type = LAST_BIND;
Al Virodef4af32009-12-26 08:37:05 -0500850 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
851 error = PTR_ERR(*p);
Al Viro6d7b5aa2012-06-10 04:15:17 -0400852 if (IS_ERR(*p))
Christoph Hellwig408ef012012-06-18 10:47:03 -0400853 goto out_put_nd_path;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400854
855 error = 0;
856 s = nd_get_link(nd);
857 if (s) {
Al Viro443ed252013-09-10 12:00:43 -0400858 if (unlikely(IS_ERR(s))) {
859 path_put(&nd->path);
860 put_link(nd, link, *p);
861 return PTR_ERR(s);
862 }
863 if (*s == '/') {
864 set_root(nd);
865 path_put(&nd->path);
866 nd->path = nd->root;
867 path_get(&nd->root);
868 nd->flags |= LOOKUP_JUMPED;
869 }
870 nd->inode = nd->path.dentry->d_inode;
871 error = link_path_walk(s, nd);
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400872 if (unlikely(error))
873 put_link(nd, link, *p);
Ian Kent051d3812006-03-27 01:14:53 -0800874 }
Al Viro6d7b5aa2012-06-10 04:15:17 -0400875
876 return error;
877
878out_put_nd_path:
Arnd Bergmann98f6ef62012-10-11 13:20:00 +0000879 *p = NULL;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400880 path_put(&nd->path);
Al Viro6d7b5aa2012-06-10 04:15:17 -0400881 path_put(link);
Ian Kent051d3812006-03-27 01:14:53 -0800882 return error;
883}
884
Nick Piggin31e6b012011-01-07 17:49:52 +1100885static int follow_up_rcu(struct path *path)
886{
Al Viro0714a532011-11-24 22:19:58 -0500887 struct mount *mnt = real_mount(path->mnt);
888 struct mount *parent;
Nick Piggin31e6b012011-01-07 17:49:52 +1100889 struct dentry *mountpoint;
890
Al Viro0714a532011-11-24 22:19:58 -0500891 parent = mnt->mnt_parent;
892 if (&parent->mnt == path->mnt)
Nick Piggin31e6b012011-01-07 17:49:52 +1100893 return 0;
Al Viroa73324d2011-11-24 22:25:07 -0500894 mountpoint = mnt->mnt_mountpoint;
Nick Piggin31e6b012011-01-07 17:49:52 +1100895 path->dentry = mountpoint;
Al Viro0714a532011-11-24 22:19:58 -0500896 path->mnt = &parent->mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +1100897 return 1;
898}
899
David Howellsf015f1262012-06-25 12:55:28 +0100900/*
901 * follow_up - Find the mountpoint of path's vfsmount
902 *
903 * Given a path, find the mountpoint of its source file system.
904 * Replace @path with the path of the mountpoint in the parent mount.
905 * Up is towards /.
906 *
907 * Return 1 if we went up a level and 0 if we were already at the
908 * root.
909 */
Al Virobab77eb2009-04-18 03:26:48 -0400910int follow_up(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
Al Viro0714a532011-11-24 22:19:58 -0500912 struct mount *mnt = real_mount(path->mnt);
913 struct mount *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 struct dentry *mountpoint;
Nick Piggin99b7db72010-08-18 04:37:39 +1000915
Al Viro48a066e2013-09-29 22:06:07 -0400916 read_seqlock_excl(&mount_lock);
Al Viro0714a532011-11-24 22:19:58 -0500917 parent = mnt->mnt_parent;
Al Viro3c0a6162012-07-18 17:32:50 +0400918 if (parent == mnt) {
Al Viro48a066e2013-09-29 22:06:07 -0400919 read_sequnlock_excl(&mount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 return 0;
921 }
Al Viro0714a532011-11-24 22:19:58 -0500922 mntget(&parent->mnt);
Al Viroa73324d2011-11-24 22:25:07 -0500923 mountpoint = dget(mnt->mnt_mountpoint);
Al Viro48a066e2013-09-29 22:06:07 -0400924 read_sequnlock_excl(&mount_lock);
Al Virobab77eb2009-04-18 03:26:48 -0400925 dput(path->dentry);
926 path->dentry = mountpoint;
927 mntput(path->mnt);
Al Viro0714a532011-11-24 22:19:58 -0500928 path->mnt = &parent->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 return 1;
930}
Al Viro4d359502014-03-14 12:20:17 -0400931EXPORT_SYMBOL(follow_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100933/*
David Howells9875cf82011-01-14 18:45:21 +0000934 * Perform an automount
935 * - return -EISDIR to tell follow_managed() to stop and return the path we
936 * were called with.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 */
David Howells9875cf82011-01-14 18:45:21 +0000938static int follow_automount(struct path *path, unsigned flags,
939 bool *need_mntput)
Nick Piggin31e6b012011-01-07 17:49:52 +1100940{
David Howells9875cf82011-01-14 18:45:21 +0000941 struct vfsmount *mnt;
David Howellsea5b7782011-01-14 19:10:03 +0000942 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +1100943
David Howells9875cf82011-01-14 18:45:21 +0000944 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
945 return -EREMOTE;
Al Viro463ffb22005-06-06 13:36:05 -0700946
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200947 /* We don't want to mount if someone's just doing a stat -
948 * unless they're stat'ing a directory and appended a '/' to
949 * the name.
950 *
951 * We do, however, want to mount if someone wants to open or
952 * create a file of any type under the mountpoint, wants to
953 * traverse through the mountpoint or wants to open the
954 * mounted directory. Also, autofs may mark negative dentries
955 * as being automount points. These will need the attentions
956 * of the daemon to instantiate them before they can be used.
David Howells9875cf82011-01-14 18:45:21 +0000957 */
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200958 if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
Linus Torvaldsd94c1772011-09-26 17:44:55 -0700959 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200960 path->dentry->d_inode)
961 return -EISDIR;
962
David Howells9875cf82011-01-14 18:45:21 +0000963 current->total_link_count++;
964 if (current->total_link_count >= 40)
965 return -ELOOP;
966
967 mnt = path->dentry->d_op->d_automount(path);
968 if (IS_ERR(mnt)) {
969 /*
970 * The filesystem is allowed to return -EISDIR here to indicate
971 * it doesn't want to automount. For instance, autofs would do
972 * this so that its userspace daemon can mount on this dentry.
973 *
974 * However, we can only permit this if it's a terminal point in
975 * the path being looked up; if it wasn't then the remainder of
976 * the path is inaccessible and we should say so.
977 */
Al Viro49084c32011-06-25 21:59:52 -0400978 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
David Howells9875cf82011-01-14 18:45:21 +0000979 return -EREMOTE;
980 return PTR_ERR(mnt);
981 }
David Howellsea5b7782011-01-14 19:10:03 +0000982
David Howells9875cf82011-01-14 18:45:21 +0000983 if (!mnt) /* mount collision */
984 return 0;
985
Al Viro8aef1882011-06-16 15:10:06 +0100986 if (!*need_mntput) {
987 /* lock_mount() may release path->mnt on error */
988 mntget(path->mnt);
989 *need_mntput = true;
990 }
Al Viro19a167a2011-01-17 01:35:23 -0500991 err = finish_automount(mnt, path);
David Howellsea5b7782011-01-14 19:10:03 +0000992
David Howellsea5b7782011-01-14 19:10:03 +0000993 switch (err) {
994 case -EBUSY:
995 /* Someone else made a mount here whilst we were busy */
Al Viro19a167a2011-01-17 01:35:23 -0500996 return 0;
David Howellsea5b7782011-01-14 19:10:03 +0000997 case 0:
Al Viro8aef1882011-06-16 15:10:06 +0100998 path_put(path);
David Howellsea5b7782011-01-14 19:10:03 +0000999 path->mnt = mnt;
1000 path->dentry = dget(mnt->mnt_root);
David Howellsea5b7782011-01-14 19:10:03 +00001001 return 0;
Al Viro19a167a2011-01-17 01:35:23 -05001002 default:
1003 return err;
David Howellsea5b7782011-01-14 19:10:03 +00001004 }
Al Viro19a167a2011-01-17 01:35:23 -05001005
David Howells9875cf82011-01-14 18:45:21 +00001006}
1007
1008/*
1009 * Handle a dentry that is managed in some way.
David Howellscc53ce52011-01-14 18:45:26 +00001010 * - Flagged for transit management (autofs)
David Howells9875cf82011-01-14 18:45:21 +00001011 * - Flagged as mountpoint
1012 * - Flagged as automount point
1013 *
1014 * This may only be called in refwalk mode.
1015 *
1016 * Serialization is taken care of in namespace.c
1017 */
1018static int follow_managed(struct path *path, unsigned flags)
1019{
Al Viro8aef1882011-06-16 15:10:06 +01001020 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
David Howells9875cf82011-01-14 18:45:21 +00001021 unsigned managed;
1022 bool need_mntput = false;
Al Viro8aef1882011-06-16 15:10:06 +01001023 int ret = 0;
David Howells9875cf82011-01-14 18:45:21 +00001024
1025 /* Given that we're not holding a lock here, we retain the value in a
1026 * local variable for each dentry as we look at it so that we don't see
1027 * the components of that value change under us */
1028 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1029 managed &= DCACHE_MANAGED_DENTRY,
1030 unlikely(managed != 0)) {
David Howellscc53ce52011-01-14 18:45:26 +00001031 /* Allow the filesystem to manage the transit without i_mutex
1032 * being held. */
1033 if (managed & DCACHE_MANAGE_TRANSIT) {
1034 BUG_ON(!path->dentry->d_op);
1035 BUG_ON(!path->dentry->d_op->d_manage);
Al Viro1aed3e42011-03-18 09:09:02 -04001036 ret = path->dentry->d_op->d_manage(path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +00001037 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001038 break;
David Howellscc53ce52011-01-14 18:45:26 +00001039 }
1040
David Howells9875cf82011-01-14 18:45:21 +00001041 /* Transit to a mounted filesystem. */
1042 if (managed & DCACHE_MOUNTED) {
1043 struct vfsmount *mounted = lookup_mnt(path);
1044 if (mounted) {
1045 dput(path->dentry);
1046 if (need_mntput)
1047 mntput(path->mnt);
1048 path->mnt = mounted;
1049 path->dentry = dget(mounted->mnt_root);
1050 need_mntput = true;
1051 continue;
1052 }
1053
1054 /* Something is mounted on this dentry in another
1055 * namespace and/or whatever was mounted there in this
Al Viro48a066e2013-09-29 22:06:07 -04001056 * namespace got unmounted before lookup_mnt() could
1057 * get it */
David Howells9875cf82011-01-14 18:45:21 +00001058 }
1059
1060 /* Handle an automount point */
1061 if (managed & DCACHE_NEED_AUTOMOUNT) {
1062 ret = follow_automount(path, flags, &need_mntput);
1063 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001064 break;
David Howells9875cf82011-01-14 18:45:21 +00001065 continue;
1066 }
1067
1068 /* We didn't change the current path point */
1069 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 }
Al Viro8aef1882011-06-16 15:10:06 +01001071
1072 if (need_mntput && path->mnt == mnt)
1073 mntput(path->mnt);
1074 if (ret == -EISDIR)
1075 ret = 0;
Al Viroa3fbbde2011-11-07 21:21:26 +00001076 return ret < 0 ? ret : need_mntput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077}
1078
David Howellscc53ce52011-01-14 18:45:26 +00001079int follow_down_one(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
1081 struct vfsmount *mounted;
1082
Al Viro1c755af2009-04-18 14:06:57 -04001083 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -04001085 dput(path->dentry);
1086 mntput(path->mnt);
1087 path->mnt = mounted;
1088 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 return 1;
1090 }
1091 return 0;
1092}
Al Viro4d359502014-03-14 12:20:17 -04001093EXPORT_SYMBOL(follow_down_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
NeilBrownb8faf032014-08-04 17:06:29 +10001095static inline int managed_dentry_rcu(struct dentry *dentry)
Ian Kent62a73752011-03-25 01:51:02 +08001096{
NeilBrownb8faf032014-08-04 17:06:29 +10001097 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT) ?
1098 dentry->d_op->d_manage(dentry, true) : 0;
Ian Kent62a73752011-03-25 01:51:02 +08001099}
1100
David Howells9875cf82011-01-14 18:45:21 +00001101/*
Al Viro287548e2011-05-27 06:50:06 -04001102 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1103 * we meet a managed dentry that would need blocking.
David Howells9875cf82011-01-14 18:45:21 +00001104 */
1105static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
Al Viro287548e2011-05-27 06:50:06 -04001106 struct inode **inode)
David Howells9875cf82011-01-14 18:45:21 +00001107{
Ian Kent62a73752011-03-25 01:51:02 +08001108 for (;;) {
Al Viroc7105362011-11-24 18:22:03 -05001109 struct mount *mounted;
Ian Kent62a73752011-03-25 01:51:02 +08001110 /*
1111 * Don't forget we might have a non-mountpoint managed dentry
1112 * that wants to block transit.
1113 */
NeilBrownb8faf032014-08-04 17:06:29 +10001114 switch (managed_dentry_rcu(path->dentry)) {
1115 case -ECHILD:
1116 default:
David Howellsab909112011-01-14 18:46:51 +00001117 return false;
NeilBrownb8faf032014-08-04 17:06:29 +10001118 case -EISDIR:
1119 return true;
1120 case 0:
1121 break;
1122 }
Ian Kent62a73752011-03-25 01:51:02 +08001123
1124 if (!d_mountpoint(path->dentry))
NeilBrownb8faf032014-08-04 17:06:29 +10001125 return !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
Ian Kent62a73752011-03-25 01:51:02 +08001126
Al Viro474279d2013-10-01 16:11:26 -04001127 mounted = __lookup_mnt(path->mnt, path->dentry);
David Howells9875cf82011-01-14 18:45:21 +00001128 if (!mounted)
1129 break;
Al Viroc7105362011-11-24 18:22:03 -05001130 path->mnt = &mounted->mnt;
1131 path->dentry = mounted->mnt.mnt_root;
Al Viroa3fbbde2011-11-07 21:21:26 +00001132 nd->flags |= LOOKUP_JUMPED;
David Howells9875cf82011-01-14 18:45:21 +00001133 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
Linus Torvalds59430262011-07-18 15:43:29 -07001134 /*
1135 * Update the inode too. We don't need to re-check the
1136 * dentry sequence number here after this d_inode read,
1137 * because a mount-point is always pinned.
1138 */
1139 *inode = path->dentry->d_inode;
David Howells9875cf82011-01-14 18:45:21 +00001140 }
NeilBrownb8faf032014-08-04 17:06:29 +10001141 return read_seqretry(&mount_lock, nd->m_seq) &&
1142 !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
Al Viro287548e2011-05-27 06:50:06 -04001143}
1144
Nick Piggin31e6b012011-01-07 17:49:52 +11001145static int follow_dotdot_rcu(struct nameidata *nd)
1146{
Nick Piggin31e6b012011-01-07 17:49:52 +11001147 set_root_rcu(nd);
1148
David Howells9875cf82011-01-14 18:45:21 +00001149 while (1) {
Nick Piggin31e6b012011-01-07 17:49:52 +11001150 if (nd->path.dentry == nd->root.dentry &&
1151 nd->path.mnt == nd->root.mnt) {
1152 break;
1153 }
1154 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1155 struct dentry *old = nd->path.dentry;
1156 struct dentry *parent = old->d_parent;
1157 unsigned seq;
1158
1159 seq = read_seqcount_begin(&parent->d_seq);
1160 if (read_seqcount_retry(&old->d_seq, nd->seq))
Al Viroef7562d2011-03-04 14:35:59 -05001161 goto failed;
Nick Piggin31e6b012011-01-07 17:49:52 +11001162 nd->path.dentry = parent;
1163 nd->seq = seq;
1164 break;
1165 }
1166 if (!follow_up_rcu(&nd->path))
1167 break;
1168 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Nick Piggin31e6b012011-01-07 17:49:52 +11001169 }
Al Virob37199e2014-03-20 15:18:22 -04001170 while (d_mountpoint(nd->path.dentry)) {
1171 struct mount *mounted;
1172 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry);
1173 if (!mounted)
1174 break;
1175 nd->path.mnt = &mounted->mnt;
1176 nd->path.dentry = mounted->mnt.mnt_root;
1177 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
1178 if (!read_seqretry(&mount_lock, nd->m_seq))
1179 goto failed;
1180 }
Al Virodea39372011-05-27 06:53:39 -04001181 nd->inode = nd->path.dentry->d_inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001182 return 0;
Al Viroef7562d2011-03-04 14:35:59 -05001183
1184failed:
1185 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001186 if (!(nd->flags & LOOKUP_ROOT))
1187 nd->root.mnt = NULL;
Al Viro8b61e742013-11-08 12:45:01 -05001188 rcu_read_unlock();
Al Viroef7562d2011-03-04 14:35:59 -05001189 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001190}
1191
David Howells9875cf82011-01-14 18:45:21 +00001192/*
David Howellscc53ce52011-01-14 18:45:26 +00001193 * Follow down to the covering mount currently visible to userspace. At each
1194 * point, the filesystem owning that dentry may be queried as to whether the
1195 * caller is permitted to proceed or not.
David Howellscc53ce52011-01-14 18:45:26 +00001196 */
Al Viro7cc90cc2011-03-18 09:04:20 -04001197int follow_down(struct path *path)
David Howellscc53ce52011-01-14 18:45:26 +00001198{
1199 unsigned managed;
1200 int ret;
1201
1202 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1203 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1204 /* Allow the filesystem to manage the transit without i_mutex
1205 * being held.
1206 *
1207 * We indicate to the filesystem if someone is trying to mount
1208 * something here. This gives autofs the chance to deny anyone
1209 * other than its daemon the right to mount on its
1210 * superstructure.
1211 *
1212 * The filesystem may sleep at this point.
1213 */
1214 if (managed & DCACHE_MANAGE_TRANSIT) {
1215 BUG_ON(!path->dentry->d_op);
1216 BUG_ON(!path->dentry->d_op->d_manage);
David Howellsab909112011-01-14 18:46:51 +00001217 ret = path->dentry->d_op->d_manage(
Al Viro1aed3e42011-03-18 09:09:02 -04001218 path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +00001219 if (ret < 0)
1220 return ret == -EISDIR ? 0 : ret;
1221 }
1222
1223 /* Transit to a mounted filesystem. */
1224 if (managed & DCACHE_MOUNTED) {
1225 struct vfsmount *mounted = lookup_mnt(path);
1226 if (!mounted)
1227 break;
1228 dput(path->dentry);
1229 mntput(path->mnt);
1230 path->mnt = mounted;
1231 path->dentry = dget(mounted->mnt_root);
1232 continue;
1233 }
1234
1235 /* Don't handle automount points here */
1236 break;
1237 }
1238 return 0;
1239}
Al Viro4d359502014-03-14 12:20:17 -04001240EXPORT_SYMBOL(follow_down);
David Howellscc53ce52011-01-14 18:45:26 +00001241
1242/*
David Howells9875cf82011-01-14 18:45:21 +00001243 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1244 */
1245static void follow_mount(struct path *path)
1246{
1247 while (d_mountpoint(path->dentry)) {
1248 struct vfsmount *mounted = lookup_mnt(path);
1249 if (!mounted)
1250 break;
1251 dput(path->dentry);
1252 mntput(path->mnt);
1253 path->mnt = mounted;
1254 path->dentry = dget(mounted->mnt_root);
1255 }
1256}
1257
Nick Piggin31e6b012011-01-07 17:49:52 +11001258static void follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259{
Al Viro2a737872009-04-07 11:49:53 -04001260 set_root(nd);
Andreas Mohre518ddb2006-09-29 02:01:22 -07001261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 while(1) {
Jan Blunck4ac91372008-02-14 19:34:32 -08001263 struct dentry *old = nd->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Al Viro2a737872009-04-07 11:49:53 -04001265 if (nd->path.dentry == nd->root.dentry &&
1266 nd->path.mnt == nd->root.mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 break;
1268 }
Jan Blunck4ac91372008-02-14 19:34:32 -08001269 if (nd->path.dentry != nd->path.mnt->mnt_root) {
Al Viro3088dd72010-01-30 15:47:29 -05001270 /* rare case of legitimate dget_parent()... */
1271 nd->path.dentry = dget_parent(nd->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 dput(old);
1273 break;
1274 }
Al Viro3088dd72010-01-30 15:47:29 -05001275 if (!follow_up(&nd->path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 }
Al Viro79ed0222009-04-18 13:59:41 -04001278 follow_mount(&nd->path);
Nick Piggin31e6b012011-01-07 17:49:52 +11001279 nd->inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280}
1281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282/*
Miklos Szeredibad61182012-03-26 12:54:24 +02001283 * This looks up the name in dcache, possibly revalidates the old dentry and
1284 * allocates a new one if not found or not valid. In the need_lookup argument
1285 * returns whether i_op->lookup is necessary.
1286 *
1287 * dir->d_inode->i_mutex must be held
Nick Pigginbaa03892010-08-18 04:37:31 +10001288 */
Miklos Szeredibad61182012-03-26 12:54:24 +02001289static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
Al Viro201f9562012-06-22 12:42:10 +04001290 unsigned int flags, bool *need_lookup)
Nick Pigginbaa03892010-08-18 04:37:31 +10001291{
Nick Pigginbaa03892010-08-18 04:37:31 +10001292 struct dentry *dentry;
Miklos Szeredibad61182012-03-26 12:54:24 +02001293 int error;
Nick Pigginbaa03892010-08-18 04:37:31 +10001294
Miklos Szeredibad61182012-03-26 12:54:24 +02001295 *need_lookup = false;
1296 dentry = d_lookup(dir, name);
1297 if (dentry) {
Jeff Layton39e3c952012-11-28 11:30:53 -05001298 if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
Al Viro201f9562012-06-22 12:42:10 +04001299 error = d_revalidate(dentry, flags);
Miklos Szeredibad61182012-03-26 12:54:24 +02001300 if (unlikely(error <= 0)) {
1301 if (error < 0) {
1302 dput(dentry);
1303 return ERR_PTR(error);
1304 } else if (!d_invalidate(dentry)) {
1305 dput(dentry);
1306 dentry = NULL;
1307 }
1308 }
1309 }
1310 }
Nick Pigginbaa03892010-08-18 04:37:31 +10001311
Miklos Szeredibad61182012-03-26 12:54:24 +02001312 if (!dentry) {
1313 dentry = d_alloc(dir, name);
1314 if (unlikely(!dentry))
1315 return ERR_PTR(-ENOMEM);
Nick Pigginbaa03892010-08-18 04:37:31 +10001316
Miklos Szeredibad61182012-03-26 12:54:24 +02001317 *need_lookup = true;
Nick Pigginbaa03892010-08-18 04:37:31 +10001318 }
1319 return dentry;
1320}
1321
1322/*
J. Bruce Fields13a2c3b2013-10-23 16:09:16 -04001323 * Call i_op->lookup on the dentry. The dentry must be negative and
1324 * unhashed.
Miklos Szeredibad61182012-03-26 12:54:24 +02001325 *
1326 * dir->d_inode->i_mutex must be held
Josef Bacik44396f42011-05-31 11:58:49 -04001327 */
Miklos Szeredibad61182012-03-26 12:54:24 +02001328static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
Al Viro72bd8662012-06-10 17:17:17 -04001329 unsigned int flags)
Josef Bacik44396f42011-05-31 11:58:49 -04001330{
Josef Bacik44396f42011-05-31 11:58:49 -04001331 struct dentry *old;
1332
1333 /* Don't create child dentry for a dead directory. */
Miklos Szeredibad61182012-03-26 12:54:24 +02001334 if (unlikely(IS_DEADDIR(dir))) {
Miklos Szeredie188dc02012-02-03 14:25:18 +01001335 dput(dentry);
Josef Bacik44396f42011-05-31 11:58:49 -04001336 return ERR_PTR(-ENOENT);
Miklos Szeredie188dc02012-02-03 14:25:18 +01001337 }
Josef Bacik44396f42011-05-31 11:58:49 -04001338
Al Viro72bd8662012-06-10 17:17:17 -04001339 old = dir->i_op->lookup(dir, dentry, flags);
Josef Bacik44396f42011-05-31 11:58:49 -04001340 if (unlikely(old)) {
1341 dput(dentry);
1342 dentry = old;
1343 }
1344 return dentry;
1345}
1346
Al Viroa3255542012-03-30 14:41:51 -04001347static struct dentry *__lookup_hash(struct qstr *name,
Al Viro72bd8662012-06-10 17:17:17 -04001348 struct dentry *base, unsigned int flags)
Al Viroa3255542012-03-30 14:41:51 -04001349{
Miklos Szeredibad61182012-03-26 12:54:24 +02001350 bool need_lookup;
Al Viroa3255542012-03-30 14:41:51 -04001351 struct dentry *dentry;
1352
Al Viro72bd8662012-06-10 17:17:17 -04001353 dentry = lookup_dcache(name, base, flags, &need_lookup);
Miklos Szeredibad61182012-03-26 12:54:24 +02001354 if (!need_lookup)
1355 return dentry;
Al Viroa3255542012-03-30 14:41:51 -04001356
Al Viro72bd8662012-06-10 17:17:17 -04001357 return lookup_real(base->d_inode, dentry, flags);
Al Viroa3255542012-03-30 14:41:51 -04001358}
1359
Josef Bacik44396f42011-05-31 11:58:49 -04001360/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 * It's more convoluted than I'd like it to be, but... it's still fairly
1362 * small and for now I'd prefer to have fast path as straight as possible.
1363 * It _is_ time-critical.
1364 */
Al Viroe97cdc82013-01-24 18:16:00 -05001365static int lookup_fast(struct nameidata *nd,
Miklos Szeredi697f5142012-05-21 17:30:05 +02001366 struct path *path, struct inode **inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367{
Jan Blunck4ac91372008-02-14 19:34:32 -08001368 struct vfsmount *mnt = nd->path.mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +11001369 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro5a18fff2011-03-11 04:44:53 -05001370 int need_reval = 1;
1371 int status = 1;
David Howells9875cf82011-01-14 18:45:21 +00001372 int err;
1373
Al Viro3cac2602009-08-13 18:27:43 +04001374 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001375 * Rename seqlock is not required here because in the off chance
1376 * of a false negative due to a concurrent rename, we're going to
1377 * do the non-racy lookup, below.
1378 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001379 if (nd->flags & LOOKUP_RCU) {
1380 unsigned seq;
Linus Torvaldsda53be12013-05-21 15:22:44 -07001381 dentry = __d_lookup_rcu(parent, &nd->last, &seq);
Al Viro5a18fff2011-03-11 04:44:53 -05001382 if (!dentry)
1383 goto unlazy;
1384
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001385 /*
1386 * This sequence count validates that the inode matches
1387 * the dentry name information from lookup.
1388 */
1389 *inode = dentry->d_inode;
1390 if (read_seqcount_retry(&dentry->d_seq, seq))
1391 return -ECHILD;
1392
1393 /*
1394 * This sequence count validates that the parent had no
1395 * changes while we did the lookup of the dentry above.
1396 *
1397 * The memory barrier in read_seqcount_begin of child is
1398 * enough, we can use __read_seqcount_retry here.
1399 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001400 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1401 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001402 nd->seq = seq;
Al Viro5a18fff2011-03-11 04:44:53 -05001403
Al Viro24643082011-02-15 01:26:22 -05001404 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
Al Viro4ce16ef32012-06-10 16:10:59 -04001405 status = d_revalidate(dentry, nd->flags);
Al Viro5a18fff2011-03-11 04:44:53 -05001406 if (unlikely(status <= 0)) {
1407 if (status != -ECHILD)
1408 need_reval = 0;
1409 goto unlazy;
1410 }
Al Viro24643082011-02-15 01:26:22 -05001411 }
Nick Piggin31e6b012011-01-07 17:49:52 +11001412 path->mnt = mnt;
1413 path->dentry = dentry;
NeilBrownb8faf032014-08-04 17:06:29 +10001414 if (likely(__follow_mount_rcu(nd, path, inode)))
1415 return 0;
Al Viro5a18fff2011-03-11 04:44:53 -05001416unlazy:
Al Viro19660af2011-03-25 10:32:48 -04001417 if (unlazy_walk(nd, dentry))
1418 return -ECHILD;
Al Viro5a18fff2011-03-11 04:44:53 -05001419 } else {
Al Viroe97cdc82013-01-24 18:16:00 -05001420 dentry = __d_lookup(parent, &nd->last);
Nick Piggin31e6b012011-01-07 17:49:52 +11001421 }
Al Viro5a18fff2011-03-11 04:44:53 -05001422
Al Viro81e6f522012-03-30 14:48:04 -04001423 if (unlikely(!dentry))
1424 goto need_lookup;
Al Viro5a18fff2011-03-11 04:44:53 -05001425
Al Viro5a18fff2011-03-11 04:44:53 -05001426 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
Al Viro4ce16ef32012-06-10 16:10:59 -04001427 status = d_revalidate(dentry, nd->flags);
Al Viro5a18fff2011-03-11 04:44:53 -05001428 if (unlikely(status <= 0)) {
1429 if (status < 0) {
1430 dput(dentry);
1431 return status;
1432 }
1433 if (!d_invalidate(dentry)) {
1434 dput(dentry);
Al Viro81e6f522012-03-30 14:48:04 -04001435 goto need_lookup;
Al Viro5a18fff2011-03-11 04:44:53 -05001436 }
1437 }
Miklos Szeredi697f5142012-05-21 17:30:05 +02001438
David Howells9875cf82011-01-14 18:45:21 +00001439 path->mnt = mnt;
1440 path->dentry = dentry;
1441 err = follow_managed(path, nd->flags);
Ian Kent89312212011-01-18 12:06:10 +08001442 if (unlikely(err < 0)) {
1443 path_put_conditional(path, nd);
David Howells9875cf82011-01-14 18:45:21 +00001444 return err;
Ian Kent89312212011-01-18 12:06:10 +08001445 }
Al Viroa3fbbde2011-11-07 21:21:26 +00001446 if (err)
1447 nd->flags |= LOOKUP_JUMPED;
David Howells9875cf82011-01-14 18:45:21 +00001448 *inode = path->dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 return 0;
Al Viro81e6f522012-03-30 14:48:04 -04001450
1451need_lookup:
Miklos Szeredi697f5142012-05-21 17:30:05 +02001452 return 1;
1453}
1454
1455/* Fast lookup failed, do it the slow way */
Al Virocc2a5272013-01-24 18:19:49 -05001456static int lookup_slow(struct nameidata *nd, struct path *path)
Miklos Szeredi697f5142012-05-21 17:30:05 +02001457{
1458 struct dentry *dentry, *parent;
1459 int err;
1460
1461 parent = nd->path.dentry;
Al Viro81e6f522012-03-30 14:48:04 -04001462 BUG_ON(nd->inode != parent->d_inode);
1463
1464 mutex_lock(&parent->d_inode->i_mutex);
Al Virocc2a5272013-01-24 18:19:49 -05001465 dentry = __lookup_hash(&nd->last, parent, nd->flags);
Al Viro81e6f522012-03-30 14:48:04 -04001466 mutex_unlock(&parent->d_inode->i_mutex);
1467 if (IS_ERR(dentry))
1468 return PTR_ERR(dentry);
Miklos Szeredi697f5142012-05-21 17:30:05 +02001469 path->mnt = nd->path.mnt;
1470 path->dentry = dentry;
1471 err = follow_managed(path, nd->flags);
1472 if (unlikely(err < 0)) {
1473 path_put_conditional(path, nd);
1474 return err;
1475 }
1476 if (err)
1477 nd->flags |= LOOKUP_JUMPED;
1478 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479}
1480
Al Viro52094c82011-02-21 21:34:47 -05001481static inline int may_lookup(struct nameidata *nd)
1482{
1483 if (nd->flags & LOOKUP_RCU) {
Al Viro4ad5abb2011-06-20 19:57:03 -04001484 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
Al Viro52094c82011-02-21 21:34:47 -05001485 if (err != -ECHILD)
1486 return err;
Al Viro19660af2011-03-25 10:32:48 -04001487 if (unlazy_walk(nd, NULL))
Al Viro52094c82011-02-21 21:34:47 -05001488 return -ECHILD;
1489 }
Al Viro4ad5abb2011-06-20 19:57:03 -04001490 return inode_permission(nd->inode, MAY_EXEC);
Al Viro52094c82011-02-21 21:34:47 -05001491}
1492
Al Viro9856fa12011-03-04 14:22:06 -05001493static inline int handle_dots(struct nameidata *nd, int type)
1494{
1495 if (type == LAST_DOTDOT) {
1496 if (nd->flags & LOOKUP_RCU) {
1497 if (follow_dotdot_rcu(nd))
1498 return -ECHILD;
1499 } else
1500 follow_dotdot(nd);
1501 }
1502 return 0;
1503}
1504
Al Viro951361f2011-03-04 14:44:37 -05001505static void terminate_walk(struct nameidata *nd)
1506{
1507 if (!(nd->flags & LOOKUP_RCU)) {
1508 path_put(&nd->path);
1509 } else {
1510 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001511 if (!(nd->flags & LOOKUP_ROOT))
1512 nd->root.mnt = NULL;
Al Viro8b61e742013-11-08 12:45:01 -05001513 rcu_read_unlock();
Al Viro951361f2011-03-04 14:44:37 -05001514 }
1515}
1516
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001517/*
1518 * Do we need to follow links? We _really_ want to be able
1519 * to do this check without having to look at inode->i_op,
1520 * so we keep a cache of "no, this doesn't need follow_link"
1521 * for the common case.
1522 */
David Howellsb18825a2013-09-12 19:22:53 +01001523static inline int should_follow_link(struct dentry *dentry, int follow)
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001524{
David Howellsb18825a2013-09-12 19:22:53 +01001525 return unlikely(d_is_symlink(dentry)) ? follow : 0;
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001526}
1527
Al Viroce57dfc2011-03-13 19:58:58 -04001528static inline int walk_component(struct nameidata *nd, struct path *path,
Al Viro21b9b072013-01-24 18:10:25 -05001529 int follow)
Al Viroce57dfc2011-03-13 19:58:58 -04001530{
1531 struct inode *inode;
1532 int err;
1533 /*
1534 * "." and ".." are special - ".." especially so because it has
1535 * to be able to know about the current root directory and
1536 * parent relationships.
1537 */
Al Viro21b9b072013-01-24 18:10:25 -05001538 if (unlikely(nd->last_type != LAST_NORM))
1539 return handle_dots(nd, nd->last_type);
Al Viroe97cdc82013-01-24 18:16:00 -05001540 err = lookup_fast(nd, path, &inode);
Al Viroce57dfc2011-03-13 19:58:58 -04001541 if (unlikely(err)) {
Miklos Szeredi697f5142012-05-21 17:30:05 +02001542 if (err < 0)
1543 goto out_err;
1544
Al Virocc2a5272013-01-24 18:19:49 -05001545 err = lookup_slow(nd, path);
Miklos Szeredi697f5142012-05-21 17:30:05 +02001546 if (err < 0)
1547 goto out_err;
1548
1549 inode = path->dentry->d_inode;
Al Viroce57dfc2011-03-13 19:58:58 -04001550 }
Miklos Szeredi697f5142012-05-21 17:30:05 +02001551 err = -ENOENT;
Al Viro22213312014-04-19 12:30:58 -04001552 if (!inode || d_is_negative(path->dentry))
Miklos Szeredi697f5142012-05-21 17:30:05 +02001553 goto out_path_put;
1554
David Howellsb18825a2013-09-12 19:22:53 +01001555 if (should_follow_link(path->dentry, follow)) {
Al Viro19660af2011-03-25 10:32:48 -04001556 if (nd->flags & LOOKUP_RCU) {
1557 if (unlikely(unlazy_walk(nd, path->dentry))) {
Miklos Szeredi697f5142012-05-21 17:30:05 +02001558 err = -ECHILD;
1559 goto out_err;
Al Viro19660af2011-03-25 10:32:48 -04001560 }
1561 }
Al Viroce57dfc2011-03-13 19:58:58 -04001562 BUG_ON(inode != path->dentry->d_inode);
1563 return 1;
1564 }
1565 path_to_nameidata(path, nd);
1566 nd->inode = inode;
1567 return 0;
Miklos Szeredi697f5142012-05-21 17:30:05 +02001568
1569out_path_put:
1570 path_to_nameidata(path, nd);
1571out_err:
1572 terminate_walk(nd);
1573 return err;
Al Viroce57dfc2011-03-13 19:58:58 -04001574}
1575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576/*
Al Virob3563792011-03-14 21:54:55 -04001577 * This limits recursive symlink follows to 8, while
1578 * limiting consecutive symlinks to 40.
1579 *
1580 * Without that kind of total limit, nasty chains of consecutive
1581 * symlinks can cause almost arbitrarily long lookups.
1582 */
1583static inline int nested_symlink(struct path *path, struct nameidata *nd)
1584{
1585 int res;
1586
Al Virob3563792011-03-14 21:54:55 -04001587 if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1588 path_put_conditional(path, nd);
1589 path_put(&nd->path);
1590 return -ELOOP;
1591 }
Erez Zadok1a4022f2011-05-21 01:19:59 -04001592 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
Al Virob3563792011-03-14 21:54:55 -04001593
1594 nd->depth++;
1595 current->link_count++;
1596
1597 do {
1598 struct path link = *path;
1599 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04001600
1601 res = follow_link(&link, nd, &cookie);
Al Viro6d7b5aa2012-06-10 04:15:17 -04001602 if (res)
1603 break;
Al Viro21b9b072013-01-24 18:10:25 -05001604 res = walk_component(nd, path, LOOKUP_FOLLOW);
Al Viro574197e2011-03-14 22:20:34 -04001605 put_link(nd, &link, cookie);
Al Virob3563792011-03-14 21:54:55 -04001606 } while (res > 0);
1607
1608 current->link_count--;
1609 nd->depth--;
1610 return res;
1611}
1612
1613/*
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001614 * We can do the critical dentry name comparison and hashing
1615 * operations one word at a time, but we are limited to:
1616 *
1617 * - Architectures with fast unaligned word accesses. We could
1618 * do a "get_unaligned()" if this helps and is sufficiently
1619 * fast.
1620 *
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001621 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1622 * do not trap on the (extremely unlikely) case of a page
1623 * crossing operation.
1624 *
1625 * - Furthermore, we need an efficient 64-bit compile for the
1626 * 64-bit case in order to generate the "number of bytes in
1627 * the final mask". Again, that could be replaced with a
1628 * efficient population count instruction or similar.
1629 */
1630#ifdef CONFIG_DCACHE_WORD_ACCESS
1631
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001632#include <asm/word-at-a-time.h>
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001633
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001634#ifdef CONFIG_64BIT
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001635
1636static inline unsigned int fold_hash(unsigned long hash)
1637{
Linus Torvalds99d263d2014-09-13 11:30:10 -07001638 return hash_64(hash, 32);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001639}
1640
1641#else /* 32-bit case */
1642
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001643#define fold_hash(x) (x)
1644
1645#endif
1646
1647unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1648{
1649 unsigned long a, mask;
1650 unsigned long hash = 0;
1651
1652 for (;;) {
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001653 a = load_unaligned_zeropad(name);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001654 if (len < sizeof(unsigned long))
1655 break;
1656 hash += a;
Al Virof132c5b2012-03-22 21:59:52 +00001657 hash *= 9;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001658 name += sizeof(unsigned long);
1659 len -= sizeof(unsigned long);
1660 if (!len)
1661 goto done;
1662 }
Will Deacona5c21dc2013-12-12 17:40:21 +00001663 mask = bytemask_from_count(len);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001664 hash += mask & a;
1665done:
1666 return fold_hash(hash);
1667}
1668EXPORT_SYMBOL(full_name_hash);
1669
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001670/*
1671 * Calculate the length and hash of the path component, and
Linus Torvalds9226b5b2014-09-14 17:28:32 -07001672 * fill in the qstr. return the "len" as the result.
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001673 */
Linus Torvalds9226b5b2014-09-14 17:28:32 -07001674static inline unsigned long hash_name(const char *name, struct qstr *res)
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001675{
Linus Torvalds36126f82012-05-26 10:43:17 -07001676 unsigned long a, b, adata, bdata, mask, hash, len;
1677 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001678
Linus Torvalds9226b5b2014-09-14 17:28:32 -07001679 res->name = name;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001680 hash = a = 0;
1681 len = -sizeof(unsigned long);
1682 do {
1683 hash = (hash + a) * 9;
1684 len += sizeof(unsigned long);
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001685 a = load_unaligned_zeropad(name+len);
Linus Torvalds36126f82012-05-26 10:43:17 -07001686 b = a ^ REPEAT_BYTE('/');
1687 } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001688
Linus Torvalds36126f82012-05-26 10:43:17 -07001689 adata = prep_zero_mask(a, adata, &constants);
1690 bdata = prep_zero_mask(b, bdata, &constants);
1691
1692 mask = create_zero_mask(adata | bdata);
1693
1694 hash += a & zero_bytemask(mask);
Linus Torvalds9226b5b2014-09-14 17:28:32 -07001695 len += find_zero(mask);
1696 res->hash_len = hashlen_create(fold_hash(hash), len);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001697
Linus Torvalds9226b5b2014-09-14 17:28:32 -07001698 return len;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001699}
1700
1701#else
1702
Linus Torvalds0145acc2012-03-02 14:32:59 -08001703unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1704{
1705 unsigned long hash = init_name_hash();
1706 while (len--)
1707 hash = partial_name_hash(*name++, hash);
1708 return end_name_hash(hash);
1709}
Linus Torvaldsae942ae2012-03-02 19:40:57 -08001710EXPORT_SYMBOL(full_name_hash);
Linus Torvalds0145acc2012-03-02 14:32:59 -08001711
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001712/*
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001713 * We know there's a real path component here of at least
1714 * one character.
1715 */
Linus Torvalds9226b5b2014-09-14 17:28:32 -07001716static inline long hash_name(const char *name, struct qstr *res)
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001717{
1718 unsigned long hash = init_name_hash();
1719 unsigned long len = 0, c;
1720
Linus Torvalds9226b5b2014-09-14 17:28:32 -07001721 res->name = name;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001722 c = (unsigned char)*name;
1723 do {
1724 len++;
1725 hash = partial_name_hash(c, hash);
1726 c = (unsigned char)name[len];
1727 } while (c && c != '/');
Linus Torvalds9226b5b2014-09-14 17:28:32 -07001728 res->hash_len = hashlen_create(end_name_hash(hash), len);
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001729 return len;
1730}
1731
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001732#endif
1733
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001734/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01001736 * This is the basic name resolution function, turning a pathname into
1737 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01001739 * Returns 0 and nd will have valid dentry and mnt on success.
1740 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 */
Al Viro6de88d72009-08-09 01:41:57 +04001742static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743{
1744 struct path next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
1747 while (*name=='/')
1748 name++;
1749 if (!*name)
Al Viro086e1832011-02-22 20:56:27 -05001750 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 /* At this point we know we have a real path component. */
1753 for(;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 struct qstr this;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001755 long len;
Al Virofe479a52011-02-22 15:10:03 -05001756 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
Al Viro52094c82011-02-21 21:34:47 -05001758 err = may_lookup(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 if (err)
1760 break;
1761
Linus Torvalds9226b5b2014-09-14 17:28:32 -07001762 len = hash_name(name, &this);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
Al Virofe479a52011-02-22 15:10:03 -05001764 type = LAST_NORM;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001765 if (name[0] == '.') switch (len) {
Al Virofe479a52011-02-22 15:10:03 -05001766 case 2:
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001767 if (name[1] == '.') {
Al Virofe479a52011-02-22 15:10:03 -05001768 type = LAST_DOTDOT;
Al Viro16c2cd72011-02-22 15:50:10 -05001769 nd->flags |= LOOKUP_JUMPED;
1770 }
Al Virofe479a52011-02-22 15:10:03 -05001771 break;
1772 case 1:
1773 type = LAST_DOT;
1774 }
Al Viro5a202bc2011-03-08 14:17:44 -05001775 if (likely(type == LAST_NORM)) {
1776 struct dentry *parent = nd->path.dentry;
Al Viro16c2cd72011-02-22 15:50:10 -05001777 nd->flags &= ~LOOKUP_JUMPED;
Al Viro5a202bc2011-03-08 14:17:44 -05001778 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
Linus Torvaldsda53be12013-05-21 15:22:44 -07001779 err = parent->d_op->d_hash(parent, &this);
Al Viro5a202bc2011-03-08 14:17:44 -05001780 if (err < 0)
1781 break;
1782 }
1783 }
Al Virofe479a52011-02-22 15:10:03 -05001784
Al Viro5f4a6a62013-01-24 18:04:22 -05001785 nd->last = this;
1786 nd->last_type = type;
1787
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001788 if (!name[len])
Al Viro5f4a6a62013-01-24 18:04:22 -05001789 return 0;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001790 /*
1791 * If it wasn't NUL, we know it was '/'. Skip that
1792 * slash, and continue until no more slashes.
1793 */
1794 do {
1795 len++;
1796 } while (unlikely(name[len] == '/'));
1797 if (!name[len])
Al Viro5f4a6a62013-01-24 18:04:22 -05001798 return 0;
1799
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001800 name += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
Al Viro21b9b072013-01-24 18:10:25 -05001802 err = walk_component(nd, &next, LOOKUP_FOLLOW);
Al Viroce57dfc2011-03-13 19:58:58 -04001803 if (err < 0)
1804 return err;
Al Virofe479a52011-02-22 15:10:03 -05001805
Al Viroce57dfc2011-03-13 19:58:58 -04001806 if (err) {
Al Virob3563792011-03-14 21:54:55 -04001807 err = nested_symlink(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 if (err)
Al Viroa7472ba2011-03-04 14:39:30 -05001809 return err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001810 }
Miklos Szeredi44b1d532014-04-01 17:08:41 +02001811 if (!d_can_lookup(nd->path.dentry)) {
Al Viro5f4a6a62013-01-24 18:04:22 -05001812 err = -ENOTDIR;
1813 break;
1814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 }
Al Viro951361f2011-03-04 14:44:37 -05001816 terminate_walk(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 return err;
1818}
1819
Al Viro70e9b352011-03-05 21:12:22 -05001820static int path_init(int dfd, const char *name, unsigned int flags,
1821 struct nameidata *nd, struct file **fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822{
Prasanna Medaea3834d2005-04-29 16:00:17 +01001823 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
1825 nd->last_type = LAST_ROOT; /* if there are only slashes... */
Al Viro16c2cd72011-02-22 15:50:10 -05001826 nd->flags = flags | LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 nd->depth = 0;
Al Viro5b6ca022011-03-09 23:04:47 -05001828 if (flags & LOOKUP_ROOT) {
David Howellsb18825a2013-09-12 19:22:53 +01001829 struct dentry *root = nd->root.dentry;
1830 struct inode *inode = root->d_inode;
Al Viro73d049a2011-03-11 12:08:24 -05001831 if (*name) {
Miklos Szeredi44b1d532014-04-01 17:08:41 +02001832 if (!d_can_lookup(root))
Al Viro73d049a2011-03-11 12:08:24 -05001833 return -ENOTDIR;
1834 retval = inode_permission(inode, MAY_EXEC);
1835 if (retval)
1836 return retval;
1837 }
Al Viro5b6ca022011-03-09 23:04:47 -05001838 nd->path = nd->root;
1839 nd->inode = inode;
1840 if (flags & LOOKUP_RCU) {
Al Viro8b61e742013-11-08 12:45:01 -05001841 rcu_read_lock();
Al Viro5b6ca022011-03-09 23:04:47 -05001842 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro48a066e2013-09-29 22:06:07 -04001843 nd->m_seq = read_seqbegin(&mount_lock);
Al Viro5b6ca022011-03-09 23:04:47 -05001844 } else {
1845 path_get(&nd->path);
1846 }
1847 return 0;
1848 }
1849
Al Viro2a737872009-04-07 11:49:53 -04001850 nd->root.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Al Viro48a066e2013-09-29 22:06:07 -04001852 nd->m_seq = read_seqbegin(&mount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 if (*name=='/') {
Al Viroe41f7d42011-02-22 14:02:58 -05001854 if (flags & LOOKUP_RCU) {
Al Viro8b61e742013-11-08 12:45:01 -05001855 rcu_read_lock();
Al Viroe41f7d42011-02-22 14:02:58 -05001856 set_root_rcu(nd);
1857 } else {
1858 set_root(nd);
1859 path_get(&nd->root);
1860 }
Al Viro2a737872009-04-07 11:49:53 -04001861 nd->path = nd->root;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001862 } else if (dfd == AT_FDCWD) {
Al Viroe41f7d42011-02-22 14:02:58 -05001863 if (flags & LOOKUP_RCU) {
1864 struct fs_struct *fs = current->fs;
1865 unsigned seq;
1866
Al Viro8b61e742013-11-08 12:45:01 -05001867 rcu_read_lock();
Al Viroe41f7d42011-02-22 14:02:58 -05001868
1869 do {
1870 seq = read_seqcount_begin(&fs->seq);
1871 nd->path = fs->pwd;
1872 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1873 } while (read_seqcount_retry(&fs->seq, seq));
1874 } else {
1875 get_fs_pwd(current->fs, &nd->path);
1876 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001877 } else {
Jeff Layton582aa642012-12-11 08:56:16 -05001878 /* Caller must check execute permissions on the starting path component */
Al Viro2903ff02012-08-28 12:52:22 -04001879 struct fd f = fdget_raw(dfd);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001880 struct dentry *dentry;
1881
Al Viro2903ff02012-08-28 12:52:22 -04001882 if (!f.file)
1883 return -EBADF;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001884
Al Viro2903ff02012-08-28 12:52:22 -04001885 dentry = f.file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001886
Al Virof52e0c12011-03-14 18:56:51 -04001887 if (*name) {
Miklos Szeredi44b1d532014-04-01 17:08:41 +02001888 if (!d_can_lookup(dentry)) {
Al Viro2903ff02012-08-28 12:52:22 -04001889 fdput(f);
1890 return -ENOTDIR;
1891 }
Al Virof52e0c12011-03-14 18:56:51 -04001892 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001893
Al Viro2903ff02012-08-28 12:52:22 -04001894 nd->path = f.file->f_path;
Al Viroe41f7d42011-02-22 14:02:58 -05001895 if (flags & LOOKUP_RCU) {
Linus Torvalds9c225f22014-03-03 09:36:58 -08001896 if (f.flags & FDPUT_FPUT)
Al Viro2903ff02012-08-28 12:52:22 -04001897 *fp = f.file;
Al Viroe41f7d42011-02-22 14:02:58 -05001898 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro8b61e742013-11-08 12:45:01 -05001899 rcu_read_lock();
Al Viroe41f7d42011-02-22 14:02:58 -05001900 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001901 path_get(&nd->path);
1902 fdput(f);
Al Viroe41f7d42011-02-22 14:02:58 -05001903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 }
Al Viroe41f7d42011-02-22 14:02:58 -05001905
Nick Piggin31e6b012011-01-07 17:49:52 +11001906 nd->inode = nd->path.dentry->d_inode;
Al Viro9b4a9b12009-04-07 11:44:16 -04001907 return 0;
Al Viro9b4a9b12009-04-07 11:44:16 -04001908}
1909
Al Virobd92d7f2011-03-14 19:54:59 -04001910static inline int lookup_last(struct nameidata *nd, struct path *path)
1911{
1912 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1913 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1914
1915 nd->flags &= ~LOOKUP_PARENT;
Al Viro21b9b072013-01-24 18:10:25 -05001916 return walk_component(nd, path, nd->flags & LOOKUP_FOLLOW);
Al Virobd92d7f2011-03-14 19:54:59 -04001917}
1918
Al Viro9b4a9b12009-04-07 11:44:16 -04001919/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroee0827c2011-02-21 23:38:09 -05001920static int path_lookupat(int dfd, const char *name,
Al Viro9b4a9b12009-04-07 11:44:16 -04001921 unsigned int flags, struct nameidata *nd)
1922{
Al Viro70e9b352011-03-05 21:12:22 -05001923 struct file *base = NULL;
Al Virobd92d7f2011-03-14 19:54:59 -04001924 struct path path;
1925 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001926
1927 /*
1928 * Path walking is largely split up into 2 different synchronisation
1929 * schemes, rcu-walk and ref-walk (explained in
1930 * Documentation/filesystems/path-lookup.txt). These share much of the
1931 * path walk code, but some things particularly setup, cleanup, and
1932 * following mounts are sufficiently divergent that functions are
1933 * duplicated. Typically there is a function foo(), and its RCU
1934 * analogue, foo_rcu().
1935 *
1936 * -ECHILD is the error number of choice (just to avoid clashes) that
1937 * is returned if some aspect of an rcu-walk fails. Such an error must
1938 * be handled by restarting a traditional ref-walk (which will always
1939 * be able to complete).
1940 */
Al Virobd92d7f2011-03-14 19:54:59 -04001941 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
Al Viroee0827c2011-02-21 23:38:09 -05001942
Al Virobd92d7f2011-03-14 19:54:59 -04001943 if (unlikely(err))
1944 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001945
1946 current->total_link_count = 0;
Al Virobd92d7f2011-03-14 19:54:59 -04001947 err = link_path_walk(name, nd);
1948
1949 if (!err && !(flags & LOOKUP_PARENT)) {
Al Virobd92d7f2011-03-14 19:54:59 -04001950 err = lookup_last(nd, &path);
1951 while (err > 0) {
1952 void *cookie;
1953 struct path link = path;
Kees Cook800179c2012-07-25 17:29:07 -07001954 err = may_follow_link(&link, nd);
1955 if (unlikely(err))
1956 break;
Al Virobd92d7f2011-03-14 19:54:59 -04001957 nd->flags |= LOOKUP_PARENT;
Al Viro574197e2011-03-14 22:20:34 -04001958 err = follow_link(&link, nd, &cookie);
Al Viro6d7b5aa2012-06-10 04:15:17 -04001959 if (err)
1960 break;
1961 err = lookup_last(nd, &path);
Al Viro574197e2011-03-14 22:20:34 -04001962 put_link(nd, &link, cookie);
Al Virobd92d7f2011-03-14 19:54:59 -04001963 }
1964 }
Al Viroee0827c2011-02-21 23:38:09 -05001965
Al Viro9f1fafe2011-03-25 11:00:12 -04001966 if (!err)
1967 err = complete_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04001968
1969 if (!err && nd->flags & LOOKUP_DIRECTORY) {
Miklos Szeredi44b1d532014-04-01 17:08:41 +02001970 if (!d_can_lookup(nd->path.dentry)) {
Al Virobd92d7f2011-03-14 19:54:59 -04001971 path_put(&nd->path);
Al Virobd23a532011-03-23 09:56:30 -04001972 err = -ENOTDIR;
Al Virobd92d7f2011-03-14 19:54:59 -04001973 }
1974 }
Al Viro16c2cd72011-02-22 15:50:10 -05001975
Al Viro70e9b352011-03-05 21:12:22 -05001976 if (base)
1977 fput(base);
Al Viroee0827c2011-02-21 23:38:09 -05001978
Al Viro5b6ca022011-03-09 23:04:47 -05001979 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
Al Viro2a737872009-04-07 11:49:53 -04001980 path_put(&nd->root);
1981 nd->root.mnt = NULL;
1982 }
Al Virobd92d7f2011-03-14 19:54:59 -04001983 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001984}
Nick Piggin31e6b012011-01-07 17:49:52 +11001985
Jeff Layton873f1ee2012-10-10 15:25:29 -04001986static int filename_lookup(int dfd, struct filename *name,
1987 unsigned int flags, struct nameidata *nd)
1988{
1989 int retval = path_lookupat(dfd, name->name, flags | LOOKUP_RCU, nd);
1990 if (unlikely(retval == -ECHILD))
1991 retval = path_lookupat(dfd, name->name, flags, nd);
1992 if (unlikely(retval == -ESTALE))
1993 retval = path_lookupat(dfd, name->name,
1994 flags | LOOKUP_REVAL, nd);
1995
1996 if (likely(!retval))
Jeff Laytonadb5c242012-10-10 16:43:13 -04001997 audit_inode(name, nd->path.dentry, flags & LOOKUP_PARENT);
Jeff Layton873f1ee2012-10-10 15:25:29 -04001998 return retval;
1999}
2000
Al Viroee0827c2011-02-21 23:38:09 -05002001static int do_path_lookup(int dfd, const char *name,
2002 unsigned int flags, struct nameidata *nd)
2003{
Jeff Layton873f1ee2012-10-10 15:25:29 -04002004 struct filename filename = { .name = name };
Nick Piggin31e6b012011-01-07 17:49:52 +11002005
Jeff Layton873f1ee2012-10-10 15:25:29 -04002006 return filename_lookup(dfd, &filename, flags, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007}
2008
Al Viro79714f72012-06-15 03:01:42 +04002009/* does lookup, returns the object with parent locked */
2010struct dentry *kern_path_locked(const char *name, struct path *path)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002011{
Al Viro79714f72012-06-15 03:01:42 +04002012 struct nameidata nd;
2013 struct dentry *d;
2014 int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd);
2015 if (err)
2016 return ERR_PTR(err);
2017 if (nd.last_type != LAST_NORM) {
2018 path_put(&nd.path);
2019 return ERR_PTR(-EINVAL);
2020 }
2021 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Al Viro1e0ea002012-07-22 23:46:21 +04002022 d = __lookup_hash(&nd.last, nd.path.dentry, 0);
Al Viro79714f72012-06-15 03:01:42 +04002023 if (IS_ERR(d)) {
2024 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2025 path_put(&nd.path);
2026 return d;
2027 }
2028 *path = nd.path;
2029 return d;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002030}
2031
Al Virod1811462008-08-02 00:49:18 -04002032int kern_path(const char *name, unsigned int flags, struct path *path)
2033{
2034 struct nameidata nd;
2035 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
2036 if (!res)
2037 *path = nd.path;
2038 return res;
2039}
Al Viro4d359502014-03-14 12:20:17 -04002040EXPORT_SYMBOL(kern_path);
Al Virod1811462008-08-02 00:49:18 -04002041
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002042/**
2043 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2044 * @dentry: pointer to dentry of the base directory
2045 * @mnt: pointer to vfs mount of the base directory
2046 * @name: pointer to file name
2047 * @flags: lookup flags
Al Viroe0a01242011-06-27 17:00:37 -04002048 * @path: pointer to struct path to fill
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002049 */
2050int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2051 const char *name, unsigned int flags,
Al Viroe0a01242011-06-27 17:00:37 -04002052 struct path *path)
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002053{
Al Viroe0a01242011-06-27 17:00:37 -04002054 struct nameidata nd;
2055 int err;
2056 nd.root.dentry = dentry;
2057 nd.root.mnt = mnt;
2058 BUG_ON(flags & LOOKUP_PARENT);
Al Viro5b6ca022011-03-09 23:04:47 -05002059 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
Al Viroe0a01242011-06-27 17:00:37 -04002060 err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
2061 if (!err)
2062 *path = nd.path;
2063 return err;
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002064}
Al Viro4d359502014-03-14 12:20:17 -04002065EXPORT_SYMBOL(vfs_path_lookup);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002066
James Morris057f6c02007-04-26 00:12:05 -07002067/*
2068 * Restricted form of lookup. Doesn't follow links, single-component only,
2069 * needs parent already locked. Doesn't follow mounts.
2070 * SMP-safe.
2071 */
Adrian Bunka244e162006-03-31 02:32:11 -08002072static struct dentry *lookup_hash(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073{
Al Viro72bd8662012-06-10 17:17:17 -04002074 return __lookup_hash(&nd->last, nd->path.dentry, nd->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075}
2076
Christoph Hellwigeead1912007-10-16 23:25:38 -07002077/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07002078 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07002079 * @name: pathname component to lookup
2080 * @base: base directory to lookup from
2081 * @len: maximum length @len should be interpreted to
2082 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07002083 * Note that this routine is purely a helper for filesystem usage and should
2084 * not be called by generic code. Also note that by using this function the
Christoph Hellwigeead1912007-10-16 23:25:38 -07002085 * nameidata argument is passed to the filesystem methods and a filesystem
2086 * using this helper needs to be prepared for that.
2087 */
James Morris057f6c02007-04-26 00:12:05 -07002088struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2089{
James Morris057f6c02007-04-26 00:12:05 -07002090 struct qstr this;
Al Viro6a96ba52011-03-07 23:49:20 -05002091 unsigned int c;
Miklos Szeredicda309d2012-03-26 12:54:21 +02002092 int err;
James Morris057f6c02007-04-26 00:12:05 -07002093
David Woodhouse2f9092e2009-04-20 23:18:37 +01002094 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
2095
Al Viro6a96ba52011-03-07 23:49:20 -05002096 this.name = name;
2097 this.len = len;
Linus Torvalds0145acc2012-03-02 14:32:59 -08002098 this.hash = full_name_hash(name, len);
Al Viro6a96ba52011-03-07 23:49:20 -05002099 if (!len)
2100 return ERR_PTR(-EACCES);
2101
Al Viro21d8a152012-11-29 22:17:21 -05002102 if (unlikely(name[0] == '.')) {
2103 if (len < 2 || (len == 2 && name[1] == '.'))
2104 return ERR_PTR(-EACCES);
2105 }
2106
Al Viro6a96ba52011-03-07 23:49:20 -05002107 while (len--) {
2108 c = *(const unsigned char *)name++;
2109 if (c == '/' || c == '\0')
2110 return ERR_PTR(-EACCES);
Al Viro6a96ba52011-03-07 23:49:20 -05002111 }
Al Viro5a202bc2011-03-08 14:17:44 -05002112 /*
2113 * See if the low-level filesystem might want
2114 * to use its own hash..
2115 */
2116 if (base->d_flags & DCACHE_OP_HASH) {
Linus Torvaldsda53be12013-05-21 15:22:44 -07002117 int err = base->d_op->d_hash(base, &this);
Al Viro5a202bc2011-03-08 14:17:44 -05002118 if (err < 0)
2119 return ERR_PTR(err);
2120 }
Christoph Hellwigeead1912007-10-16 23:25:38 -07002121
Miklos Szeredicda309d2012-03-26 12:54:21 +02002122 err = inode_permission(base->d_inode, MAY_EXEC);
2123 if (err)
2124 return ERR_PTR(err);
2125
Al Viro72bd8662012-06-10 17:17:17 -04002126 return __lookup_hash(&this, base, 0);
James Morris057f6c02007-04-26 00:12:05 -07002127}
Al Viro4d359502014-03-14 12:20:17 -04002128EXPORT_SYMBOL(lookup_one_len);
James Morris057f6c02007-04-26 00:12:05 -07002129
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002130int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2131 struct path *path, int *empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132{
Al Viro2d8f3032008-07-22 09:59:21 -04002133 struct nameidata nd;
Jeff Layton91a27b22012-10-10 15:25:28 -04002134 struct filename *tmp = getname_flags(name, flags, empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 int err = PTR_ERR(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 if (!IS_ERR(tmp)) {
Al Viro2d8f3032008-07-22 09:59:21 -04002137
2138 BUG_ON(flags & LOOKUP_PARENT);
2139
Jeff Layton873f1ee2012-10-10 15:25:29 -04002140 err = filename_lookup(dfd, tmp, flags, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 putname(tmp);
Al Viro2d8f3032008-07-22 09:59:21 -04002142 if (!err)
2143 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 }
2145 return err;
2146}
2147
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002148int user_path_at(int dfd, const char __user *name, unsigned flags,
2149 struct path *path)
2150{
Linus Torvaldsf7493e52012-03-22 16:10:40 -07002151 return user_path_at_empty(dfd, name, flags, path, NULL);
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002152}
Al Viro4d359502014-03-14 12:20:17 -04002153EXPORT_SYMBOL(user_path_at);
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002154
Jeff Layton873f1ee2012-10-10 15:25:29 -04002155/*
2156 * NB: most callers don't do anything directly with the reference to the
2157 * to struct filename, but the nd->last pointer points into the name string
2158 * allocated by getname. So we must hold the reference to it until all
2159 * path-walking is complete.
2160 */
Jeff Layton91a27b22012-10-10 15:25:28 -04002161static struct filename *
Jeff Layton9e790bd2012-12-11 12:10:09 -05002162user_path_parent(int dfd, const char __user *path, struct nameidata *nd,
2163 unsigned int flags)
Al Viro2ad94ae2008-07-21 09:32:51 -04002164{
Jeff Layton91a27b22012-10-10 15:25:28 -04002165 struct filename *s = getname(path);
Al Viro2ad94ae2008-07-21 09:32:51 -04002166 int error;
2167
Jeff Layton9e790bd2012-12-11 12:10:09 -05002168 /* only LOOKUP_REVAL is allowed in extra flags */
2169 flags &= LOOKUP_REVAL;
2170
Al Viro2ad94ae2008-07-21 09:32:51 -04002171 if (IS_ERR(s))
Jeff Layton91a27b22012-10-10 15:25:28 -04002172 return s;
Al Viro2ad94ae2008-07-21 09:32:51 -04002173
Jeff Layton9e790bd2012-12-11 12:10:09 -05002174 error = filename_lookup(dfd, s, flags | LOOKUP_PARENT, nd);
Jeff Layton91a27b22012-10-10 15:25:28 -04002175 if (error) {
Al Viro2ad94ae2008-07-21 09:32:51 -04002176 putname(s);
Jeff Layton91a27b22012-10-10 15:25:28 -04002177 return ERR_PTR(error);
2178 }
Al Viro2ad94ae2008-07-21 09:32:51 -04002179
Jeff Layton91a27b22012-10-10 15:25:28 -04002180 return s;
Al Viro2ad94ae2008-07-21 09:32:51 -04002181}
2182
Jeff Layton80334262013-07-26 06:23:25 -04002183/**
Al Viro197df042013-09-08 14:03:27 -04002184 * mountpoint_last - look up last component for umount
Jeff Layton80334262013-07-26 06:23:25 -04002185 * @nd: pathwalk nameidata - currently pointing at parent directory of "last"
2186 * @path: pointer to container for result
2187 *
2188 * This is a special lookup_last function just for umount. In this case, we
2189 * need to resolve the path without doing any revalidation.
2190 *
2191 * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
2192 * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
2193 * in almost all cases, this lookup will be served out of the dcache. The only
2194 * cases where it won't are if nd->last refers to a symlink or the path is
2195 * bogus and it doesn't exist.
2196 *
2197 * Returns:
2198 * -error: if there was an error during lookup. This includes -ENOENT if the
2199 * lookup found a negative dentry. The nd->path reference will also be
2200 * put in this case.
2201 *
2202 * 0: if we successfully resolved nd->path and found it to not to be a
2203 * symlink that needs to be followed. "path" will also be populated.
2204 * The nd->path reference will also be put.
2205 *
2206 * 1: if we successfully resolved nd->last and found it to be a symlink
2207 * that needs to be followed. "path" will be populated with the path
2208 * to the link, and nd->path will *not* be put.
2209 */
2210static int
Al Viro197df042013-09-08 14:03:27 -04002211mountpoint_last(struct nameidata *nd, struct path *path)
Jeff Layton80334262013-07-26 06:23:25 -04002212{
2213 int error = 0;
2214 struct dentry *dentry;
2215 struct dentry *dir = nd->path.dentry;
2216
Al Viro35759522013-09-08 13:41:33 -04002217 /* If we're in rcuwalk, drop out of it to handle last component */
2218 if (nd->flags & LOOKUP_RCU) {
2219 if (unlazy_walk(nd, NULL)) {
2220 error = -ECHILD;
2221 goto out;
2222 }
Jeff Layton80334262013-07-26 06:23:25 -04002223 }
2224
2225 nd->flags &= ~LOOKUP_PARENT;
2226
2227 if (unlikely(nd->last_type != LAST_NORM)) {
2228 error = handle_dots(nd, nd->last_type);
Al Viro35759522013-09-08 13:41:33 -04002229 if (error)
2230 goto out;
2231 dentry = dget(nd->path.dentry);
2232 goto done;
Jeff Layton80334262013-07-26 06:23:25 -04002233 }
2234
2235 mutex_lock(&dir->d_inode->i_mutex);
2236 dentry = d_lookup(dir, &nd->last);
2237 if (!dentry) {
2238 /*
2239 * No cached dentry. Mounted dentries are pinned in the cache,
2240 * so that means that this dentry is probably a symlink or the
2241 * path doesn't actually point to a mounted dentry.
2242 */
2243 dentry = d_alloc(dir, &nd->last);
2244 if (!dentry) {
2245 error = -ENOMEM;
Dave Jonesbcceeeb2013-09-10 17:04:25 -04002246 mutex_unlock(&dir->d_inode->i_mutex);
Al Viro35759522013-09-08 13:41:33 -04002247 goto out;
Jeff Layton80334262013-07-26 06:23:25 -04002248 }
Al Viro35759522013-09-08 13:41:33 -04002249 dentry = lookup_real(dir->d_inode, dentry, nd->flags);
2250 error = PTR_ERR(dentry);
Dave Jonesbcceeeb2013-09-10 17:04:25 -04002251 if (IS_ERR(dentry)) {
2252 mutex_unlock(&dir->d_inode->i_mutex);
Al Viro35759522013-09-08 13:41:33 -04002253 goto out;
Dave Jonesbcceeeb2013-09-10 17:04:25 -04002254 }
Jeff Layton80334262013-07-26 06:23:25 -04002255 }
2256 mutex_unlock(&dir->d_inode->i_mutex);
2257
Al Viro35759522013-09-08 13:41:33 -04002258done:
Al Viro22213312014-04-19 12:30:58 -04002259 if (!dentry->d_inode || d_is_negative(dentry)) {
Al Viro35759522013-09-08 13:41:33 -04002260 error = -ENOENT;
2261 dput(dentry);
2262 goto out;
Jeff Layton80334262013-07-26 06:23:25 -04002263 }
Al Viro35759522013-09-08 13:41:33 -04002264 path->dentry = dentry;
Vasily Averin295dc392014-07-21 12:30:23 +04002265 path->mnt = nd->path.mnt;
David Howellsb18825a2013-09-12 19:22:53 +01002266 if (should_follow_link(dentry, nd->flags & LOOKUP_FOLLOW))
Al Viro35759522013-09-08 13:41:33 -04002267 return 1;
Vasily Averin295dc392014-07-21 12:30:23 +04002268 mntget(path->mnt);
Al Viro35759522013-09-08 13:41:33 -04002269 follow_mount(path);
2270 error = 0;
2271out:
Jeff Layton80334262013-07-26 06:23:25 -04002272 terminate_walk(nd);
2273 return error;
2274}
2275
2276/**
Al Viro197df042013-09-08 14:03:27 -04002277 * path_mountpoint - look up a path to be umounted
Jeff Layton80334262013-07-26 06:23:25 -04002278 * @dfd: directory file descriptor to start walk from
2279 * @name: full pathname to walk
Randy Dunlap606d6fe2013-10-19 14:56:55 -07002280 * @path: pointer to container for result
Jeff Layton80334262013-07-26 06:23:25 -04002281 * @flags: lookup flags
Jeff Layton80334262013-07-26 06:23:25 -04002282 *
2283 * Look up the given name, but don't attempt to revalidate the last component.
Randy Dunlap606d6fe2013-10-19 14:56:55 -07002284 * Returns 0 and "path" will be valid on success; Returns error otherwise.
Jeff Layton80334262013-07-26 06:23:25 -04002285 */
2286static int
Al Viro197df042013-09-08 14:03:27 -04002287path_mountpoint(int dfd, const char *name, struct path *path, unsigned int flags)
Jeff Layton80334262013-07-26 06:23:25 -04002288{
2289 struct file *base = NULL;
2290 struct nameidata nd;
2291 int err;
2292
2293 err = path_init(dfd, name, flags | LOOKUP_PARENT, &nd, &base);
2294 if (unlikely(err))
2295 return err;
2296
2297 current->total_link_count = 0;
2298 err = link_path_walk(name, &nd);
2299 if (err)
2300 goto out;
2301
Al Viro197df042013-09-08 14:03:27 -04002302 err = mountpoint_last(&nd, path);
Jeff Layton80334262013-07-26 06:23:25 -04002303 while (err > 0) {
2304 void *cookie;
2305 struct path link = *path;
2306 err = may_follow_link(&link, &nd);
2307 if (unlikely(err))
2308 break;
2309 nd.flags |= LOOKUP_PARENT;
2310 err = follow_link(&link, &nd, &cookie);
2311 if (err)
2312 break;
Al Viro197df042013-09-08 14:03:27 -04002313 err = mountpoint_last(&nd, path);
Jeff Layton80334262013-07-26 06:23:25 -04002314 put_link(&nd, &link, cookie);
2315 }
2316out:
2317 if (base)
2318 fput(base);
2319
2320 if (nd.root.mnt && !(nd.flags & LOOKUP_ROOT))
2321 path_put(&nd.root);
2322
2323 return err;
2324}
2325
Al Viro2d864652013-09-08 20:18:44 -04002326static int
2327filename_mountpoint(int dfd, struct filename *s, struct path *path,
2328 unsigned int flags)
2329{
2330 int error = path_mountpoint(dfd, s->name, path, flags | LOOKUP_RCU);
2331 if (unlikely(error == -ECHILD))
2332 error = path_mountpoint(dfd, s->name, path, flags);
2333 if (unlikely(error == -ESTALE))
2334 error = path_mountpoint(dfd, s->name, path, flags | LOOKUP_REVAL);
2335 if (likely(!error))
2336 audit_inode(s, path->dentry, 0);
2337 return error;
2338}
2339
Jeff Layton80334262013-07-26 06:23:25 -04002340/**
Al Viro197df042013-09-08 14:03:27 -04002341 * user_path_mountpoint_at - lookup a path from userland in order to umount it
Jeff Layton80334262013-07-26 06:23:25 -04002342 * @dfd: directory file descriptor
2343 * @name: pathname from userland
2344 * @flags: lookup flags
2345 * @path: pointer to container to hold result
2346 *
2347 * A umount is a special case for path walking. We're not actually interested
2348 * in the inode in this situation, and ESTALE errors can be a problem. We
2349 * simply want track down the dentry and vfsmount attached at the mountpoint
2350 * and avoid revalidating the last component.
2351 *
2352 * Returns 0 and populates "path" on success.
2353 */
2354int
Al Viro197df042013-09-08 14:03:27 -04002355user_path_mountpoint_at(int dfd, const char __user *name, unsigned int flags,
Jeff Layton80334262013-07-26 06:23:25 -04002356 struct path *path)
2357{
2358 struct filename *s = getname(name);
2359 int error;
Jeff Layton80334262013-07-26 06:23:25 -04002360 if (IS_ERR(s))
2361 return PTR_ERR(s);
Al Viro2d864652013-09-08 20:18:44 -04002362 error = filename_mountpoint(dfd, s, path, flags);
Jeff Layton80334262013-07-26 06:23:25 -04002363 putname(s);
2364 return error;
2365}
2366
Al Viro2d864652013-09-08 20:18:44 -04002367int
2368kern_path_mountpoint(int dfd, const char *name, struct path *path,
2369 unsigned int flags)
2370{
2371 struct filename s = {.name = name};
2372 return filename_mountpoint(dfd, &s, path, flags);
2373}
2374EXPORT_SYMBOL(kern_path_mountpoint);
2375
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376/*
2377 * It's inline, so penalty for filesystems that don't use sticky bit is
2378 * minimal.
2379 */
2380static inline int check_sticky(struct inode *dir, struct inode *inode)
2381{
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002382 kuid_t fsuid = current_fsuid();
David Howellsda9592e2008-11-14 10:39:05 +11002383
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 if (!(dir->i_mode & S_ISVTX))
2385 return 0;
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002386 if (uid_eq(inode->i_uid, fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 return 0;
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002388 if (uid_eq(dir->i_uid, fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 return 0;
Andy Lutomirski23adbe12014-06-10 12:45:42 -07002390 return !capable_wrt_inode_uidgid(inode, CAP_FOWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391}
2392
2393/*
2394 * Check whether we can remove a link victim from directory dir, check
2395 * whether the type of victim is right.
2396 * 1. We can't do it if dir is read-only (done in permission())
2397 * 2. We should have write and exec permissions on dir
2398 * 3. We can't remove anything from append-only dir
2399 * 4. We can't do anything with immutable dir (done in permission())
2400 * 5. If the sticky bit on dir is set we should either
2401 * a. be owner of dir, or
2402 * b. be owner of victim, or
2403 * c. have CAP_FOWNER capability
2404 * 6. If the victim is append-only or immutable we can't do antyhing with
2405 * links pointing to it.
2406 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2407 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2408 * 9. We can't remove a root or mountpoint.
2409 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
2410 * nfs_async_unlink().
2411 */
David Howellsb18825a2013-09-12 19:22:53 +01002412static int may_delete(struct inode *dir, struct dentry *victim, bool isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413{
David Howellsb18825a2013-09-12 19:22:53 +01002414 struct inode *inode = victim->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 int error;
2416
David Howellsb18825a2013-09-12 19:22:53 +01002417 if (d_is_negative(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 return -ENOENT;
David Howellsb18825a2013-09-12 19:22:53 +01002419 BUG_ON(!inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
2421 BUG_ON(victim->d_parent->d_inode != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04002422 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423
Al Virof419a2e2008-07-22 00:07:17 -04002424 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 if (error)
2426 return error;
2427 if (IS_APPEND(dir))
2428 return -EPERM;
David Howellsb18825a2013-09-12 19:22:53 +01002429
2430 if (check_sticky(dir, inode) || IS_APPEND(inode) ||
2431 IS_IMMUTABLE(inode) || IS_SWAPFILE(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 return -EPERM;
2433 if (isdir) {
Miklos Szeredi44b1d532014-04-01 17:08:41 +02002434 if (!d_is_dir(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 return -ENOTDIR;
2436 if (IS_ROOT(victim))
2437 return -EBUSY;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02002438 } else if (d_is_dir(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 return -EISDIR;
2440 if (IS_DEADDIR(dir))
2441 return -ENOENT;
2442 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2443 return -EBUSY;
2444 return 0;
2445}
2446
2447/* Check whether we can create an object with dentry child in directory
2448 * dir.
2449 * 1. We can't do it if child already exists (open has special treatment for
2450 * this case, but since we are inlined it's OK)
2451 * 2. We can't do it if dir is read-only (done in permission())
2452 * 3. We should have write and exec permissions on dir
2453 * 4. We can't do it if dir is immutable (done in permission())
2454 */
Miklos Szeredia95164d2008-07-30 15:08:48 +02002455static inline int may_create(struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456{
Jeff Layton14e972b2013-05-08 10:25:58 -04002457 audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 if (child->d_inode)
2459 return -EEXIST;
2460 if (IS_DEADDIR(dir))
2461 return -ENOENT;
Al Virof419a2e2008-07-22 00:07:17 -04002462 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463}
2464
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465/*
2466 * p1 and p2 should be directories on the same fs.
2467 */
2468struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2469{
2470 struct dentry *p;
2471
2472 if (p1 == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07002473 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 return NULL;
2475 }
2476
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002477 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002479 p = d_ancestor(p2, p1);
2480 if (p) {
2481 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2482 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
2483 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 }
2485
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002486 p = d_ancestor(p1, p2);
2487 if (p) {
2488 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2489 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2490 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 }
2492
Ingo Molnarf2eace22006-07-03 00:25:05 -07002493 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2494 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 return NULL;
2496}
Al Viro4d359502014-03-14 12:20:17 -04002497EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498
2499void unlock_rename(struct dentry *p1, struct dentry *p2)
2500{
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002501 mutex_unlock(&p1->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 if (p1 != p2) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002503 mutex_unlock(&p2->d_inode->i_mutex);
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002504 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 }
2506}
Al Viro4d359502014-03-14 12:20:17 -04002507EXPORT_SYMBOL(unlock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
Al Viro4acdaf22011-07-26 01:42:34 -04002509int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viro312b63f2012-06-10 18:09:36 -04002510 bool want_excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002512 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 if (error)
2514 return error;
2515
Al Viroacfa4382008-12-04 10:06:33 -05002516 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 return -EACCES; /* shouldn't it be ENOSYS? */
2518 mode &= S_IALLUGO;
2519 mode |= S_IFREG;
2520 error = security_inode_create(dir, dentry, mode);
2521 if (error)
2522 return error;
Al Viro312b63f2012-06-10 18:09:36 -04002523 error = dir->i_op->create(dir, dentry, mode, want_excl);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002524 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002525 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 return error;
2527}
Al Viro4d359502014-03-14 12:20:17 -04002528EXPORT_SYMBOL(vfs_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529
Al Viro73d049a2011-03-11 12:08:24 -05002530static int may_open(struct path *path, int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002532 struct dentry *dentry = path->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 struct inode *inode = dentry->d_inode;
2534 int error;
2535
Al Virobcda7652011-03-13 16:42:14 -04002536 /* O_PATH? */
2537 if (!acc_mode)
2538 return 0;
2539
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 if (!inode)
2541 return -ENOENT;
2542
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002543 switch (inode->i_mode & S_IFMT) {
2544 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002546 case S_IFDIR:
2547 if (acc_mode & MAY_WRITE)
2548 return -EISDIR;
2549 break;
2550 case S_IFBLK:
2551 case S_IFCHR:
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002552 if (path->mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002554 /*FALLTHRU*/
2555 case S_IFIFO:
2556 case S_IFSOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002558 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08002559 }
Dave Hansenb41572e2007-10-16 23:31:14 -07002560
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002561 error = inode_permission(inode, acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07002562 if (error)
2563 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05002564
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 /*
2566 * An append-only file must be opened in append mode for writing.
2567 */
2568 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05002569 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05002570 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05002572 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 }
2574
2575 /* O_NOATIME can only be set by the owner or superuser */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07002576 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
Al Viro7715b522009-12-16 03:54:00 -05002577 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
J. Bruce Fieldsf3c7691e2011-09-21 10:58:13 -04002579 return 0;
Al Viro7715b522009-12-16 03:54:00 -05002580}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
Jeff Laytone1181ee2010-12-07 16:19:50 -05002582static int handle_truncate(struct file *filp)
Al Viro7715b522009-12-16 03:54:00 -05002583{
Jeff Laytone1181ee2010-12-07 16:19:50 -05002584 struct path *path = &filp->f_path;
Al Viro7715b522009-12-16 03:54:00 -05002585 struct inode *inode = path->dentry->d_inode;
2586 int error = get_write_access(inode);
2587 if (error)
2588 return error;
2589 /*
2590 * Refuse to truncate files with mandatory locks held on them.
2591 */
Jeff Laytond7a06982014-03-10 09:54:15 -04002592 error = locks_verify_locked(filp);
Al Viro7715b522009-12-16 03:54:00 -05002593 if (!error)
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +09002594 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05002595 if (!error) {
2596 error = do_truncate(path->dentry, 0,
2597 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
Jeff Laytone1181ee2010-12-07 16:19:50 -05002598 filp);
Al Viro7715b522009-12-16 03:54:00 -05002599 }
2600 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04002601 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602}
2603
Dave Hansend57999e2008-02-15 14:37:27 -08002604static inline int open_to_namei_flags(int flag)
2605{
Al Viro8a5e9292011-06-25 19:15:54 -04002606 if ((flag & O_ACCMODE) == 3)
2607 flag--;
Dave Hansend57999e2008-02-15 14:37:27 -08002608 return flag;
2609}
2610
Miklos Szeredid18e9002012-06-05 15:10:17 +02002611static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode)
2612{
2613 int error = security_path_mknod(dir, dentry, mode, 0);
2614 if (error)
2615 return error;
2616
2617 error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
2618 if (error)
2619 return error;
2620
2621 return security_inode_create(dir->dentry->d_inode, dentry, mode);
2622}
2623
David Howells1acf0af2012-06-14 16:13:46 +01002624/*
2625 * Attempt to atomically look up, create and open a file from a negative
2626 * dentry.
2627 *
2628 * Returns 0 if successful. The file will have been created and attached to
2629 * @file by the filesystem calling finish_open().
2630 *
2631 * Returns 1 if the file was looked up only or didn't need creating. The
2632 * caller will need to perform the open themselves. @path will have been
2633 * updated to point to the new dentry. This may be negative.
2634 *
2635 * Returns an error code otherwise.
2636 */
Al Viro2675a4e2012-06-22 12:41:10 +04002637static int atomic_open(struct nameidata *nd, struct dentry *dentry,
2638 struct path *path, struct file *file,
2639 const struct open_flags *op,
Al Viro64894cf2012-07-31 00:53:35 +04002640 bool got_write, bool need_lookup,
Al Viro2675a4e2012-06-22 12:41:10 +04002641 int *opened)
Miklos Szeredid18e9002012-06-05 15:10:17 +02002642{
2643 struct inode *dir = nd->path.dentry->d_inode;
2644 unsigned open_flag = open_to_namei_flags(op->open_flag);
2645 umode_t mode;
2646 int error;
2647 int acc_mode;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002648 int create_error = 0;
2649 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
Miklos Szeredi116cc022013-09-16 14:52:05 +02002650 bool excl;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002651
2652 BUG_ON(dentry->d_inode);
2653
2654 /* Don't create child dentry for a dead directory. */
2655 if (unlikely(IS_DEADDIR(dir))) {
Al Viro2675a4e2012-06-22 12:41:10 +04002656 error = -ENOENT;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002657 goto out;
2658 }
2659
Miklos Szeredi62b259d2012-08-15 13:01:24 +02002660 mode = op->mode;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002661 if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
2662 mode &= ~current_umask();
2663
Miklos Szeredi116cc022013-09-16 14:52:05 +02002664 excl = (open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT);
2665 if (excl)
Miklos Szeredid18e9002012-06-05 15:10:17 +02002666 open_flag &= ~O_TRUNC;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002667
2668 /*
2669 * Checking write permission is tricky, bacuse we don't know if we are
2670 * going to actually need it: O_CREAT opens should work as long as the
2671 * file exists. But checking existence breaks atomicity. The trick is
2672 * to check access and if not granted clear O_CREAT from the flags.
2673 *
2674 * Another problem is returing the "right" error value (e.g. for an
2675 * O_EXCL open we want to return EEXIST not EROFS).
2676 */
Al Viro64894cf2012-07-31 00:53:35 +04002677 if (((open_flag & (O_CREAT | O_TRUNC)) ||
2678 (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) {
2679 if (!(open_flag & O_CREAT)) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002680 /*
2681 * No O_CREATE -> atomicity not a requirement -> fall
2682 * back to lookup + open
2683 */
2684 goto no_open;
2685 } else if (open_flag & (O_EXCL | O_TRUNC)) {
2686 /* Fall back and fail with the right error */
Al Viro64894cf2012-07-31 00:53:35 +04002687 create_error = -EROFS;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002688 goto no_open;
2689 } else {
2690 /* No side effects, safe to clear O_CREAT */
Al Viro64894cf2012-07-31 00:53:35 +04002691 create_error = -EROFS;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002692 open_flag &= ~O_CREAT;
2693 }
2694 }
2695
2696 if (open_flag & O_CREAT) {
Miklos Szeredi38227f72012-08-15 13:01:24 +02002697 error = may_o_create(&nd->path, dentry, mode);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002698 if (error) {
2699 create_error = error;
2700 if (open_flag & O_EXCL)
2701 goto no_open;
2702 open_flag &= ~O_CREAT;
2703 }
2704 }
2705
2706 if (nd->flags & LOOKUP_DIRECTORY)
2707 open_flag |= O_DIRECTORY;
2708
Al Viro30d90492012-06-22 12:40:19 +04002709 file->f_path.dentry = DENTRY_NOT_SET;
2710 file->f_path.mnt = nd->path.mnt;
2711 error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode,
Al Viro47237682012-06-10 05:01:45 -04002712 opened);
Al Virod9585272012-06-22 12:39:14 +04002713 if (error < 0) {
Al Virod9585272012-06-22 12:39:14 +04002714 if (create_error && error == -ENOENT)
2715 error = create_error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002716 goto out;
2717 }
2718
Al Virod9585272012-06-22 12:39:14 +04002719 if (error) { /* returned 1, that is */
Al Viro30d90492012-06-22 12:40:19 +04002720 if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
Al Viro2675a4e2012-06-22 12:41:10 +04002721 error = -EIO;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002722 goto out;
2723 }
Al Viro30d90492012-06-22 12:40:19 +04002724 if (file->f_path.dentry) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002725 dput(dentry);
Al Viro30d90492012-06-22 12:40:19 +04002726 dentry = file->f_path.dentry;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002727 }
Al Viro03da6332013-09-16 19:22:33 -04002728 if (*opened & FILE_CREATED)
2729 fsnotify_create(dir, dentry);
2730 if (!dentry->d_inode) {
2731 WARN_ON(*opened & FILE_CREATED);
2732 if (create_error) {
2733 error = create_error;
2734 goto out;
2735 }
2736 } else {
2737 if (excl && !(*opened & FILE_CREATED)) {
2738 error = -EEXIST;
2739 goto out;
2740 }
Sage Weil62b2ce92012-08-15 13:30:12 -07002741 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02002742 goto looked_up;
2743 }
2744
2745 /*
2746 * We didn't have the inode before the open, so check open permission
2747 * here.
2748 */
Al Viro03da6332013-09-16 19:22:33 -04002749 acc_mode = op->acc_mode;
2750 if (*opened & FILE_CREATED) {
2751 WARN_ON(!(open_flag & O_CREAT));
2752 fsnotify_create(dir, dentry);
2753 acc_mode = MAY_OPEN;
2754 }
Al Viro2675a4e2012-06-22 12:41:10 +04002755 error = may_open(&file->f_path, acc_mode, open_flag);
2756 if (error)
2757 fput(file);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002758
2759out:
2760 dput(dentry);
Al Viro2675a4e2012-06-22 12:41:10 +04002761 return error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002762
Miklos Szeredid18e9002012-06-05 15:10:17 +02002763no_open:
2764 if (need_lookup) {
Al Viro72bd8662012-06-10 17:17:17 -04002765 dentry = lookup_real(dir, dentry, nd->flags);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002766 if (IS_ERR(dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04002767 return PTR_ERR(dentry);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002768
2769 if (create_error) {
2770 int open_flag = op->open_flag;
2771
Al Viro2675a4e2012-06-22 12:41:10 +04002772 error = create_error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002773 if ((open_flag & O_EXCL)) {
2774 if (!dentry->d_inode)
2775 goto out;
2776 } else if (!dentry->d_inode) {
2777 goto out;
2778 } else if ((open_flag & O_TRUNC) &&
2779 S_ISREG(dentry->d_inode->i_mode)) {
2780 goto out;
2781 }
2782 /* will fail later, go on to get the right error */
2783 }
2784 }
2785looked_up:
2786 path->dentry = dentry;
2787 path->mnt = nd->path.mnt;
Al Viro2675a4e2012-06-22 12:41:10 +04002788 return 1;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002789}
2790
Nick Piggin31e6b012011-01-07 17:49:52 +11002791/*
David Howells1acf0af2012-06-14 16:13:46 +01002792 * Look up and maybe create and open the last component.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002793 *
2794 * Must be called with i_mutex held on parent.
2795 *
David Howells1acf0af2012-06-14 16:13:46 +01002796 * Returns 0 if the file was successfully atomically created (if necessary) and
2797 * opened. In this case the file will be returned attached to @file.
2798 *
2799 * Returns 1 if the file was not completely opened at this time, though lookups
2800 * and creations will have been performed and the dentry returned in @path will
2801 * be positive upon return if O_CREAT was specified. If O_CREAT wasn't
2802 * specified then a negative dentry may be returned.
2803 *
2804 * An error code is returned otherwise.
2805 *
2806 * FILE_CREATE will be set in @*opened if the dentry was created and will be
2807 * cleared otherwise prior to returning.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002808 */
Al Viro2675a4e2012-06-22 12:41:10 +04002809static int lookup_open(struct nameidata *nd, struct path *path,
2810 struct file *file,
2811 const struct open_flags *op,
Al Viro64894cf2012-07-31 00:53:35 +04002812 bool got_write, int *opened)
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002813{
2814 struct dentry *dir = nd->path.dentry;
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002815 struct inode *dir_inode = dir->d_inode;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002816 struct dentry *dentry;
2817 int error;
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002818 bool need_lookup;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002819
Al Viro47237682012-06-10 05:01:45 -04002820 *opened &= ~FILE_CREATED;
Al Viro201f9562012-06-22 12:42:10 +04002821 dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002822 if (IS_ERR(dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04002823 return PTR_ERR(dentry);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002824
Miklos Szeredid18e9002012-06-05 15:10:17 +02002825 /* Cached positive dentry: will open in f_op->open */
2826 if (!need_lookup && dentry->d_inode)
2827 goto out_no_open;
2828
2829 if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) {
Al Viro64894cf2012-07-31 00:53:35 +04002830 return atomic_open(nd, dentry, path, file, op, got_write,
Al Viro47237682012-06-10 05:01:45 -04002831 need_lookup, opened);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002832 }
2833
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002834 if (need_lookup) {
2835 BUG_ON(dentry->d_inode);
2836
Al Viro72bd8662012-06-10 17:17:17 -04002837 dentry = lookup_real(dir_inode, dentry, nd->flags);
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002838 if (IS_ERR(dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04002839 return PTR_ERR(dentry);
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002840 }
2841
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002842 /* Negative dentry, just create the file */
2843 if (!dentry->d_inode && (op->open_flag & O_CREAT)) {
2844 umode_t mode = op->mode;
2845 if (!IS_POSIXACL(dir->d_inode))
2846 mode &= ~current_umask();
2847 /*
2848 * This write is needed to ensure that a
2849 * rw->ro transition does not occur between
2850 * the time when the file is created and when
2851 * a permanent write count is taken through
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02002852 * the 'struct file' in finish_open().
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002853 */
Al Viro64894cf2012-07-31 00:53:35 +04002854 if (!got_write) {
2855 error = -EROFS;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002856 goto out_dput;
Al Viro64894cf2012-07-31 00:53:35 +04002857 }
Al Viro47237682012-06-10 05:01:45 -04002858 *opened |= FILE_CREATED;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002859 error = security_path_mknod(&nd->path, dentry, mode, 0);
2860 if (error)
2861 goto out_dput;
Al Viro312b63f2012-06-10 18:09:36 -04002862 error = vfs_create(dir->d_inode, dentry, mode,
2863 nd->flags & LOOKUP_EXCL);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002864 if (error)
2865 goto out_dput;
2866 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02002867out_no_open:
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002868 path->dentry = dentry;
2869 path->mnt = nd->path.mnt;
Al Viro2675a4e2012-06-22 12:41:10 +04002870 return 1;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002871
2872out_dput:
2873 dput(dentry);
Al Viro2675a4e2012-06-22 12:41:10 +04002874 return error;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002875}
2876
2877/*
Al Virofe2d35f2011-03-05 22:58:25 -05002878 * Handle the last step of open()
Nick Piggin31e6b012011-01-07 17:49:52 +11002879 */
Al Viro2675a4e2012-06-22 12:41:10 +04002880static int do_last(struct nameidata *nd, struct path *path,
2881 struct file *file, const struct open_flags *op,
Jeff Layton669abf42012-10-10 16:43:10 -04002882 int *opened, struct filename *name)
Al Virofb1cc552009-12-24 01:58:28 -05002883{
Al Viroa1e28032009-12-24 02:12:06 -05002884 struct dentry *dir = nd->path.dentry;
Al Viroca344a892011-03-09 00:36:45 -05002885 int open_flag = op->open_flag;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002886 bool will_truncate = (open_flag & O_TRUNC) != 0;
Al Viro64894cf2012-07-31 00:53:35 +04002887 bool got_write = false;
Al Virobcda7652011-03-13 16:42:14 -04002888 int acc_mode = op->acc_mode;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02002889 struct inode *inode;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002890 bool symlink_ok = false;
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02002891 struct path save_parent = { .dentry = NULL, .mnt = NULL };
2892 bool retried = false;
Al Viro16c2cd72011-02-22 15:50:10 -05002893 int error;
Al Virofb1cc552009-12-24 01:58:28 -05002894
Al Viroc3e380b2011-02-23 13:39:45 -05002895 nd->flags &= ~LOOKUP_PARENT;
2896 nd->flags |= op->intent;
2897
Al Virobc77daa2013-06-06 09:12:33 -04002898 if (nd->last_type != LAST_NORM) {
Al Virofe2d35f2011-03-05 22:58:25 -05002899 error = handle_dots(nd, nd->last_type);
2900 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04002901 return error;
Miklos Szeredie83db162012-06-05 15:10:29 +02002902 goto finish_open;
Al Viro1f36f772009-12-26 10:56:19 -05002903 }
Al Viro67ee3ad2009-12-26 07:01:01 -05002904
Al Viroca344a892011-03-09 00:36:45 -05002905 if (!(open_flag & O_CREAT)) {
Al Virofe2d35f2011-03-05 22:58:25 -05002906 if (nd->last.name[nd->last.len])
2907 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
Al Virobcda7652011-03-13 16:42:14 -04002908 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002909 symlink_ok = true;
Al Virofe2d35f2011-03-05 22:58:25 -05002910 /* we _can_ be in RCU mode here */
Al Viroe97cdc82013-01-24 18:16:00 -05002911 error = lookup_fast(nd, path, &inode);
Miklos Szeredi71574862012-06-05 15:10:14 +02002912 if (likely(!error))
2913 goto finish_lookup;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02002914
Miklos Szeredi71574862012-06-05 15:10:14 +02002915 if (error < 0)
Al Viro2675a4e2012-06-22 12:41:10 +04002916 goto out;
Miklos Szeredi37d7fff2012-06-05 15:10:12 +02002917
Miklos Szeredi71574862012-06-05 15:10:14 +02002918 BUG_ON(nd->inode != dir->d_inode);
Miklos Szeredib6183df2012-06-05 15:10:13 +02002919 } else {
2920 /* create side of things */
2921 /*
2922 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
2923 * has been cleared when we got to the last component we are
2924 * about to look up
2925 */
2926 error = complete_walk(nd);
2927 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04002928 return error;
Miklos Szeredib6183df2012-06-05 15:10:13 +02002929
Jeff Layton33e22082013-04-12 15:16:32 -04002930 audit_inode(name, dir, LOOKUP_PARENT);
Miklos Szeredib6183df2012-06-05 15:10:13 +02002931 error = -EISDIR;
2932 /* trailing slashes? */
2933 if (nd->last.name[nd->last.len])
Al Viro2675a4e2012-06-22 12:41:10 +04002934 goto out;
Al Virofe2d35f2011-03-05 22:58:25 -05002935 }
2936
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02002937retry_lookup:
Al Viro64894cf2012-07-31 00:53:35 +04002938 if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
2939 error = mnt_want_write(nd->path.mnt);
2940 if (!error)
2941 got_write = true;
2942 /*
2943 * do _not_ fail yet - we might not need that or fail with
2944 * a different error; let lookup_open() decide; we'll be
2945 * dropping this one anyway.
2946 */
2947 }
Al Viroa1e28032009-12-24 02:12:06 -05002948 mutex_lock(&dir->d_inode->i_mutex);
Al Viro64894cf2012-07-31 00:53:35 +04002949 error = lookup_open(nd, path, file, op, got_write, opened);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002950 mutex_unlock(&dir->d_inode->i_mutex);
Al Viroa1e28032009-12-24 02:12:06 -05002951
Al Viro2675a4e2012-06-22 12:41:10 +04002952 if (error <= 0) {
2953 if (error)
Miklos Szeredid18e9002012-06-05 15:10:17 +02002954 goto out;
2955
Al Viro47237682012-06-10 05:01:45 -04002956 if ((*opened & FILE_CREATED) ||
Al Viro496ad9a2013-01-23 17:07:38 -05002957 !S_ISREG(file_inode(file)->i_mode))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002958 will_truncate = false;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002959
Jeff Laytonadb5c242012-10-10 16:43:13 -04002960 audit_inode(name, file->f_path.dentry, 0);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002961 goto opened;
2962 }
Al Virofb1cc552009-12-24 01:58:28 -05002963
Al Viro47237682012-06-10 05:01:45 -04002964 if (*opened & FILE_CREATED) {
Al Viro9b44f1b2011-03-09 00:17:27 -05002965 /* Don't check for write permission, don't truncate */
Al Viroca344a892011-03-09 00:36:45 -05002966 open_flag &= ~O_TRUNC;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002967 will_truncate = false;
Al Virobcda7652011-03-13 16:42:14 -04002968 acc_mode = MAY_OPEN;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002969 path_to_nameidata(path, nd);
Miklos Szeredie83db162012-06-05 15:10:29 +02002970 goto finish_open_created;
Al Virofb1cc552009-12-24 01:58:28 -05002971 }
2972
2973 /*
Jeff Layton3134f372012-07-25 10:19:47 -04002974 * create/update audit record if it already exists.
Al Virofb1cc552009-12-24 01:58:28 -05002975 */
David Howellsb18825a2013-09-12 19:22:53 +01002976 if (d_is_positive(path->dentry))
Jeff Laytonadb5c242012-10-10 16:43:13 -04002977 audit_inode(name, path->dentry, 0);
Al Virofb1cc552009-12-24 01:58:28 -05002978
Miklos Szeredid18e9002012-06-05 15:10:17 +02002979 /*
2980 * If atomic_open() acquired write access it is dropped now due to
2981 * possible mount and symlink following (this might be optimized away if
2982 * necessary...)
2983 */
Al Viro64894cf2012-07-31 00:53:35 +04002984 if (got_write) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002985 mnt_drop_write(nd->path.mnt);
Al Viro64894cf2012-07-31 00:53:35 +04002986 got_write = false;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002987 }
2988
Al Virofb1cc552009-12-24 01:58:28 -05002989 error = -EEXIST;
Al Virof8310c52012-07-30 11:50:30 +04002990 if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))
Al Virofb1cc552009-12-24 01:58:28 -05002991 goto exit_dput;
2992
David Howells9875cf82011-01-14 18:45:21 +00002993 error = follow_managed(path, nd->flags);
2994 if (error < 0)
2995 goto exit_dput;
Al Virofb1cc552009-12-24 01:58:28 -05002996
Al Viroa3fbbde2011-11-07 21:21:26 +00002997 if (error)
2998 nd->flags |= LOOKUP_JUMPED;
2999
Miklos Szeredidecf3402012-05-21 17:30:08 +02003000 BUG_ON(nd->flags & LOOKUP_RCU);
3001 inode = path->dentry->d_inode;
Miklos Szeredi5f5daac2012-05-21 17:30:14 +02003002finish_lookup:
3003 /* we _can_ be in RCU mode here */
Al Virofb1cc552009-12-24 01:58:28 -05003004 error = -ENOENT;
Al Viro22213312014-04-19 12:30:58 -04003005 if (!inode || d_is_negative(path->dentry)) {
Miklos Szeredi54c33e72012-05-21 17:30:10 +02003006 path_to_nameidata(path, nd);
Al Viro2675a4e2012-06-22 12:41:10 +04003007 goto out;
Miklos Szeredi54c33e72012-05-21 17:30:10 +02003008 }
Al Viro9e67f362009-12-26 07:04:50 -05003009
David Howellsb18825a2013-09-12 19:22:53 +01003010 if (should_follow_link(path->dentry, !symlink_ok)) {
Miklos Szeredid45ea862012-05-21 17:30:09 +02003011 if (nd->flags & LOOKUP_RCU) {
3012 if (unlikely(unlazy_walk(nd, path->dentry))) {
3013 error = -ECHILD;
Al Viro2675a4e2012-06-22 12:41:10 +04003014 goto out;
Miklos Szeredid45ea862012-05-21 17:30:09 +02003015 }
3016 }
3017 BUG_ON(inode != path->dentry->d_inode);
Al Viro2675a4e2012-06-22 12:41:10 +04003018 return 1;
Miklos Szeredid45ea862012-05-21 17:30:09 +02003019 }
Al Virofb1cc552009-12-24 01:58:28 -05003020
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003021 if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) {
3022 path_to_nameidata(path, nd);
3023 } else {
3024 save_parent.dentry = nd->path.dentry;
3025 save_parent.mnt = mntget(path->mnt);
3026 nd->path.dentry = path->dentry;
3027
3028 }
Miklos Szeredidecf3402012-05-21 17:30:08 +02003029 nd->inode = inode;
Al Viroa3fbbde2011-11-07 21:21:26 +00003030 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
Al Virobc77daa2013-06-06 09:12:33 -04003031finish_open:
Al Viroa3fbbde2011-11-07 21:21:26 +00003032 error = complete_walk(nd);
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003033 if (error) {
3034 path_put(&save_parent);
Al Viro2675a4e2012-06-22 12:41:10 +04003035 return error;
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003036 }
Al Virobc77daa2013-06-06 09:12:33 -04003037 audit_inode(name, nd->path.dentry, 0);
Al Virofb1cc552009-12-24 01:58:28 -05003038 error = -EISDIR;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02003039 if ((open_flag & O_CREAT) && d_is_dir(nd->path.dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04003040 goto out;
Miklos Szerediaf2f5542012-05-21 17:30:11 +02003041 error = -ENOTDIR;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02003042 if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04003043 goto out;
Al Viro6c0d46c2011-03-09 00:59:59 -05003044 if (!S_ISREG(nd->inode->i_mode))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003045 will_truncate = false;
Al Viro6c0d46c2011-03-09 00:59:59 -05003046
Al Viro0f9d1a12011-03-09 00:13:14 -05003047 if (will_truncate) {
3048 error = mnt_want_write(nd->path.mnt);
3049 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003050 goto out;
Al Viro64894cf2012-07-31 00:53:35 +04003051 got_write = true;
Al Viro0f9d1a12011-03-09 00:13:14 -05003052 }
Miklos Szeredie83db162012-06-05 15:10:29 +02003053finish_open_created:
Al Virobcda7652011-03-13 16:42:14 -04003054 error = may_open(&nd->path, acc_mode, open_flag);
Al Viroca344a892011-03-09 00:36:45 -05003055 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003056 goto out;
Al Viro30d90492012-06-22 12:40:19 +04003057 file->f_path.mnt = nd->path.mnt;
3058 error = finish_open(file, nd->path.dentry, NULL, opened);
3059 if (error) {
Al Viro30d90492012-06-22 12:40:19 +04003060 if (error == -EOPENSTALE)
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003061 goto stale_open;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003062 goto out;
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003063 }
Miklos Szeredia8277b92012-06-05 15:10:32 +02003064opened:
Al Viro2675a4e2012-06-22 12:41:10 +04003065 error = open_check_o_direct(file);
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003066 if (error)
3067 goto exit_fput;
Al Viro2675a4e2012-06-22 12:41:10 +04003068 error = ima_file_check(file, op->acc_mode);
Miklos Szerediaa4caad2012-06-05 15:10:28 +02003069 if (error)
3070 goto exit_fput;
3071
3072 if (will_truncate) {
Al Viro2675a4e2012-06-22 12:41:10 +04003073 error = handle_truncate(file);
Miklos Szerediaa4caad2012-06-05 15:10:28 +02003074 if (error)
3075 goto exit_fput;
Al Viro0f9d1a12011-03-09 00:13:14 -05003076 }
Al Viroca344a892011-03-09 00:36:45 -05003077out:
Al Viro64894cf2012-07-31 00:53:35 +04003078 if (got_write)
Al Viro0f9d1a12011-03-09 00:13:14 -05003079 mnt_drop_write(nd->path.mnt);
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003080 path_put(&save_parent);
Miklos Szeredie276ae62012-05-21 17:30:06 +02003081 terminate_walk(nd);
Al Viro2675a4e2012-06-22 12:41:10 +04003082 return error;
Al Virofb1cc552009-12-24 01:58:28 -05003083
Al Virofb1cc552009-12-24 01:58:28 -05003084exit_dput:
3085 path_put_conditional(path, nd);
Al Viroca344a892011-03-09 00:36:45 -05003086 goto out;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003087exit_fput:
Al Viro2675a4e2012-06-22 12:41:10 +04003088 fput(file);
3089 goto out;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003090
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003091stale_open:
3092 /* If no saved parent or already retried then can't retry */
3093 if (!save_parent.dentry || retried)
3094 goto out;
3095
3096 BUG_ON(save_parent.dentry != dir);
3097 path_put(&nd->path);
3098 nd->path = save_parent;
3099 nd->inode = dir->d_inode;
3100 save_parent.mnt = NULL;
3101 save_parent.dentry = NULL;
Al Viro64894cf2012-07-31 00:53:35 +04003102 if (got_write) {
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003103 mnt_drop_write(nd->path.mnt);
Al Viro64894cf2012-07-31 00:53:35 +04003104 got_write = false;
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003105 }
3106 retried = true;
3107 goto retry_lookup;
Al Virofb1cc552009-12-24 01:58:28 -05003108}
3109
Al Viro60545d02013-06-07 01:20:27 -04003110static int do_tmpfile(int dfd, struct filename *pathname,
3111 struct nameidata *nd, int flags,
3112 const struct open_flags *op,
3113 struct file *file, int *opened)
3114{
3115 static const struct qstr name = QSTR_INIT("/", 1);
3116 struct dentry *dentry, *child;
3117 struct inode *dir;
3118 int error = path_lookupat(dfd, pathname->name,
3119 flags | LOOKUP_DIRECTORY, nd);
3120 if (unlikely(error))
3121 return error;
3122 error = mnt_want_write(nd->path.mnt);
3123 if (unlikely(error))
3124 goto out;
3125 /* we want directory to be writable */
3126 error = inode_permission(nd->inode, MAY_WRITE | MAY_EXEC);
3127 if (error)
3128 goto out2;
3129 dentry = nd->path.dentry;
3130 dir = dentry->d_inode;
3131 if (!dir->i_op->tmpfile) {
3132 error = -EOPNOTSUPP;
3133 goto out2;
3134 }
3135 child = d_alloc(dentry, &name);
3136 if (unlikely(!child)) {
3137 error = -ENOMEM;
3138 goto out2;
3139 }
3140 nd->flags &= ~LOOKUP_DIRECTORY;
3141 nd->flags |= op->intent;
3142 dput(nd->path.dentry);
3143 nd->path.dentry = child;
3144 error = dir->i_op->tmpfile(dir, nd->path.dentry, op->mode);
3145 if (error)
3146 goto out2;
3147 audit_inode(pathname, nd->path.dentry, 0);
3148 error = may_open(&nd->path, op->acc_mode, op->open_flag);
3149 if (error)
3150 goto out2;
3151 file->f_path.mnt = nd->path.mnt;
3152 error = finish_open(file, nd->path.dentry, NULL, opened);
3153 if (error)
3154 goto out2;
3155 error = open_check_o_direct(file);
Al Virof4e0c302013-06-11 08:34:36 +04003156 if (error) {
Al Viro60545d02013-06-07 01:20:27 -04003157 fput(file);
Al Virof4e0c302013-06-11 08:34:36 +04003158 } else if (!(op->open_flag & O_EXCL)) {
3159 struct inode *inode = file_inode(file);
3160 spin_lock(&inode->i_lock);
3161 inode->i_state |= I_LINKABLE;
3162 spin_unlock(&inode->i_lock);
3163 }
Al Viro60545d02013-06-07 01:20:27 -04003164out2:
3165 mnt_drop_write(nd->path.mnt);
3166out:
3167 path_put(&nd->path);
3168 return error;
3169}
3170
Jeff Layton669abf42012-10-10 16:43:10 -04003171static struct file *path_openat(int dfd, struct filename *pathname,
Al Viro73d049a2011-03-11 12:08:24 -05003172 struct nameidata *nd, const struct open_flags *op, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173{
Al Virofe2d35f2011-03-05 22:58:25 -05003174 struct file *base = NULL;
Al Viro30d90492012-06-22 12:40:19 +04003175 struct file *file;
Al Viro9850c052010-01-13 15:01:15 -05003176 struct path path;
Al Viro47237682012-06-10 05:01:45 -04003177 int opened = 0;
Al Viro13aab422011-02-23 17:54:08 -05003178 int error;
Nick Piggin31e6b012011-01-07 17:49:52 +11003179
Al Viro30d90492012-06-22 12:40:19 +04003180 file = get_empty_filp();
Al Viro1afc99b2013-02-14 20:41:04 -05003181 if (IS_ERR(file))
3182 return file;
Nick Piggin31e6b012011-01-07 17:49:52 +11003183
Al Viro30d90492012-06-22 12:40:19 +04003184 file->f_flags = op->open_flag;
Nick Piggin31e6b012011-01-07 17:49:52 +11003185
Al Virobb458c62013-07-13 13:26:37 +04003186 if (unlikely(file->f_flags & __O_TMPFILE)) {
Al Viro60545d02013-06-07 01:20:27 -04003187 error = do_tmpfile(dfd, pathname, nd, flags, op, file, &opened);
3188 goto out;
3189 }
3190
Jeff Layton669abf42012-10-10 16:43:10 -04003191 error = path_init(dfd, pathname->name, flags | LOOKUP_PARENT, nd, &base);
Nick Piggin31e6b012011-01-07 17:49:52 +11003192 if (unlikely(error))
Al Viro2675a4e2012-06-22 12:41:10 +04003193 goto out;
Nick Piggin31e6b012011-01-07 17:49:52 +11003194
Al Virofe2d35f2011-03-05 22:58:25 -05003195 current->total_link_count = 0;
Jeff Layton669abf42012-10-10 16:43:10 -04003196 error = link_path_walk(pathname->name, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11003197 if (unlikely(error))
Al Viro2675a4e2012-06-22 12:41:10 +04003198 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199
Al Viro2675a4e2012-06-22 12:41:10 +04003200 error = do_last(nd, &path, file, op, &opened, pathname);
3201 while (unlikely(error > 0)) { /* trailing symlink */
Nick Piggin7b9337a2011-01-14 08:42:43 +00003202 struct path link = path;
Al Virodef4af32009-12-26 08:37:05 -05003203 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04003204 if (!(nd->flags & LOOKUP_FOLLOW)) {
Al Viro73d049a2011-03-11 12:08:24 -05003205 path_put_conditional(&path, nd);
3206 path_put(&nd->path);
Al Viro2675a4e2012-06-22 12:41:10 +04003207 error = -ELOOP;
Al Viro40b39132011-03-09 16:22:18 -05003208 break;
3209 }
Kees Cook800179c2012-07-25 17:29:07 -07003210 error = may_follow_link(&link, nd);
3211 if (unlikely(error))
3212 break;
Al Viro73d049a2011-03-11 12:08:24 -05003213 nd->flags |= LOOKUP_PARENT;
3214 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
Al Viro574197e2011-03-14 22:20:34 -04003215 error = follow_link(&link, nd, &cookie);
Al Viroc3e380b2011-02-23 13:39:45 -05003216 if (unlikely(error))
Al Viro2675a4e2012-06-22 12:41:10 +04003217 break;
3218 error = do_last(nd, &path, file, op, &opened, pathname);
Al Viro574197e2011-03-14 22:20:34 -04003219 put_link(nd, &link, cookie);
Al Viro806b6812009-12-26 07:16:40 -05003220 }
Al Viro10fa8e62009-12-26 07:09:49 -05003221out:
Al Viro73d049a2011-03-11 12:08:24 -05003222 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
3223 path_put(&nd->root);
Al Virofe2d35f2011-03-05 22:58:25 -05003224 if (base)
3225 fput(base);
Al Viro2675a4e2012-06-22 12:41:10 +04003226 if (!(opened & FILE_OPENED)) {
3227 BUG_ON(!error);
Al Viro30d90492012-06-22 12:40:19 +04003228 put_filp(file);
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003229 }
Al Viro2675a4e2012-06-22 12:41:10 +04003230 if (unlikely(error)) {
3231 if (error == -EOPENSTALE) {
3232 if (flags & LOOKUP_RCU)
3233 error = -ECHILD;
3234 else
3235 error = -ESTALE;
3236 }
3237 file = ERR_PTR(error);
3238 }
3239 return file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240}
3241
Jeff Layton669abf42012-10-10 16:43:10 -04003242struct file *do_filp_open(int dfd, struct filename *pathname,
Al Virof9652e12013-06-11 08:23:01 +04003243 const struct open_flags *op)
Al Viro13aab422011-02-23 17:54:08 -05003244{
Al Viro73d049a2011-03-11 12:08:24 -05003245 struct nameidata nd;
Al Virof9652e12013-06-11 08:23:01 +04003246 int flags = op->lookup_flags;
Al Viro13aab422011-02-23 17:54:08 -05003247 struct file *filp;
3248
Al Viro73d049a2011-03-11 12:08:24 -05003249 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
Al Viro13aab422011-02-23 17:54:08 -05003250 if (unlikely(filp == ERR_PTR(-ECHILD)))
Al Viro73d049a2011-03-11 12:08:24 -05003251 filp = path_openat(dfd, pathname, &nd, op, flags);
Al Viro13aab422011-02-23 17:54:08 -05003252 if (unlikely(filp == ERR_PTR(-ESTALE)))
Al Viro73d049a2011-03-11 12:08:24 -05003253 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
Al Viro13aab422011-02-23 17:54:08 -05003254 return filp;
3255}
3256
Al Viro73d049a2011-03-11 12:08:24 -05003257struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
Al Virof9652e12013-06-11 08:23:01 +04003258 const char *name, const struct open_flags *op)
Al Viro73d049a2011-03-11 12:08:24 -05003259{
3260 struct nameidata nd;
3261 struct file *file;
Jeff Layton669abf42012-10-10 16:43:10 -04003262 struct filename filename = { .name = name };
Al Virof9652e12013-06-11 08:23:01 +04003263 int flags = op->lookup_flags | LOOKUP_ROOT;
Al Viro73d049a2011-03-11 12:08:24 -05003264
3265 nd.root.mnt = mnt;
3266 nd.root.dentry = dentry;
3267
David Howellsb18825a2013-09-12 19:22:53 +01003268 if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN)
Al Viro73d049a2011-03-11 12:08:24 -05003269 return ERR_PTR(-ELOOP);
3270
Jeff Layton669abf42012-10-10 16:43:10 -04003271 file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_RCU);
Al Viro73d049a2011-03-11 12:08:24 -05003272 if (unlikely(file == ERR_PTR(-ECHILD)))
Jeff Layton669abf42012-10-10 16:43:10 -04003273 file = path_openat(-1, &filename, &nd, op, flags);
Al Viro73d049a2011-03-11 12:08:24 -05003274 if (unlikely(file == ERR_PTR(-ESTALE)))
Jeff Layton669abf42012-10-10 16:43:10 -04003275 file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_REVAL);
Al Viro73d049a2011-03-11 12:08:24 -05003276 return file;
3277}
3278
Jeff Layton1ac12b42012-12-11 12:10:06 -05003279struct dentry *kern_path_create(int dfd, const char *pathname,
3280 struct path *path, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003282 struct dentry *dentry = ERR_PTR(-EEXIST);
Al Viroed75e952011-06-27 16:53:43 -04003283 struct nameidata nd;
Jan Karac30dabf2012-06-12 16:20:30 +02003284 int err2;
Jeff Layton1ac12b42012-12-11 12:10:06 -05003285 int error;
3286 bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
3287
3288 /*
3289 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
3290 * other flags passed in are ignored!
3291 */
3292 lookup_flags &= LOOKUP_REVAL;
3293
3294 error = do_path_lookup(dfd, pathname, LOOKUP_PARENT|lookup_flags, &nd);
Al Viroed75e952011-06-27 16:53:43 -04003295 if (error)
3296 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003298 /*
3299 * Yucky last component or no last component at all?
3300 * (foo/., foo/.., /////)
3301 */
Al Viroed75e952011-06-27 16:53:43 -04003302 if (nd.last_type != LAST_NORM)
3303 goto out;
3304 nd.flags &= ~LOOKUP_PARENT;
3305 nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003306
Jan Karac30dabf2012-06-12 16:20:30 +02003307 /* don't fail immediately if it's r/o, at least try to report other errors */
3308 err2 = mnt_want_write(nd.path.mnt);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003309 /*
3310 * Do the final lookup.
3311 */
Al Viroed75e952011-06-27 16:53:43 -04003312 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3313 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 if (IS_ERR(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003315 goto unlock;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003316
Al Viroa8104a92012-07-20 02:25:00 +04003317 error = -EEXIST;
David Howellsb18825a2013-09-12 19:22:53 +01003318 if (d_is_positive(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003319 goto fail;
David Howellsb18825a2013-09-12 19:22:53 +01003320
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003321 /*
3322 * Special case - lookup gave negative, but... we had foo/bar/
3323 * From the vfs_mknod() POV we just have a negative dentry -
3324 * all is fine. Let's be bastards - you had / on the end, you've
3325 * been asking for (non-existent) directory. -ENOENT for you.
3326 */
Al Viroed75e952011-06-27 16:53:43 -04003327 if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
Al Viroa8104a92012-07-20 02:25:00 +04003328 error = -ENOENT;
Al Viroed75e952011-06-27 16:53:43 -04003329 goto fail;
Al Viroe9baf6e2008-05-15 04:49:12 -04003330 }
Jan Karac30dabf2012-06-12 16:20:30 +02003331 if (unlikely(err2)) {
3332 error = err2;
Al Viroa8104a92012-07-20 02:25:00 +04003333 goto fail;
Jan Karac30dabf2012-06-12 16:20:30 +02003334 }
Al Viroed75e952011-06-27 16:53:43 -04003335 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337fail:
Al Viroa8104a92012-07-20 02:25:00 +04003338 dput(dentry);
3339 dentry = ERR_PTR(error);
3340unlock:
Al Viroed75e952011-06-27 16:53:43 -04003341 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Karac30dabf2012-06-12 16:20:30 +02003342 if (!err2)
3343 mnt_drop_write(nd.path.mnt);
Al Viroed75e952011-06-27 16:53:43 -04003344out:
3345 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 return dentry;
3347}
Al Virodae6ad82011-06-26 11:50:15 -04003348EXPORT_SYMBOL(kern_path_create);
3349
Al Viro921a1652012-07-20 01:15:31 +04003350void done_path_create(struct path *path, struct dentry *dentry)
3351{
3352 dput(dentry);
3353 mutex_unlock(&path->dentry->d_inode->i_mutex);
Al Viroa8104a92012-07-20 02:25:00 +04003354 mnt_drop_write(path->mnt);
Al Viro921a1652012-07-20 01:15:31 +04003355 path_put(path);
3356}
3357EXPORT_SYMBOL(done_path_create);
3358
Jeff Layton1ac12b42012-12-11 12:10:06 -05003359struct dentry *user_path_create(int dfd, const char __user *pathname,
3360 struct path *path, unsigned int lookup_flags)
Al Virodae6ad82011-06-26 11:50:15 -04003361{
Jeff Layton91a27b22012-10-10 15:25:28 -04003362 struct filename *tmp = getname(pathname);
Al Virodae6ad82011-06-26 11:50:15 -04003363 struct dentry *res;
3364 if (IS_ERR(tmp))
3365 return ERR_CAST(tmp);
Jeff Layton1ac12b42012-12-11 12:10:06 -05003366 res = kern_path_create(dfd, tmp->name, path, lookup_flags);
Al Virodae6ad82011-06-26 11:50:15 -04003367 putname(tmp);
3368 return res;
3369}
3370EXPORT_SYMBOL(user_path_create);
3371
Al Viro1a67aaf2011-07-26 01:52:52 -04003372int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003374 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375
3376 if (error)
3377 return error;
3378
Eric W. Biederman975d6b32011-11-13 12:16:43 -08003379 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 return -EPERM;
3381
Al Viroacfa4382008-12-04 10:06:33 -05003382 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 return -EPERM;
3384
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07003385 error = devcgroup_inode_mknod(mode, dev);
3386 if (error)
3387 return error;
3388
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 error = security_inode_mknod(dir, dentry, mode, dev);
3390 if (error)
3391 return error;
3392
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003394 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003395 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 return error;
3397}
Al Viro4d359502014-03-14 12:20:17 -04003398EXPORT_SYMBOL(vfs_mknod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399
Al Virof69aac02011-07-26 04:31:40 -04003400static int may_mknod(umode_t mode)
Dave Hansen463c3192008-02-15 14:37:57 -08003401{
3402 switch (mode & S_IFMT) {
3403 case S_IFREG:
3404 case S_IFCHR:
3405 case S_IFBLK:
3406 case S_IFIFO:
3407 case S_IFSOCK:
3408 case 0: /* zero mode translates to S_IFREG */
3409 return 0;
3410 case S_IFDIR:
3411 return -EPERM;
3412 default:
3413 return -EINVAL;
3414 }
3415}
3416
Al Viro8208a222011-07-25 17:32:17 -04003417SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003418 unsigned, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419{
Al Viro2ad94ae2008-07-21 09:32:51 -04003420 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003421 struct path path;
3422 int error;
Jeff Layton972567f2012-12-20 16:00:10 -05003423 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424
Al Viro8e4bfca2012-07-20 01:17:26 +04003425 error = may_mknod(mode);
3426 if (error)
3427 return error;
Jeff Layton972567f2012-12-20 16:00:10 -05003428retry:
3429 dentry = user_path_create(dfd, filename, &path, lookup_flags);
Al Virodae6ad82011-06-26 11:50:15 -04003430 if (IS_ERR(dentry))
3431 return PTR_ERR(dentry);
Al Viro2ad94ae2008-07-21 09:32:51 -04003432
Al Virodae6ad82011-06-26 11:50:15 -04003433 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003434 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003435 error = security_path_mknod(&path, dentry, mode, dev);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003436 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04003437 goto out;
Dave Hansen463c3192008-02-15 14:37:57 -08003438 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 case 0: case S_IFREG:
Al Viro312b63f2012-06-10 18:09:36 -04003440 error = vfs_create(path.dentry->d_inode,dentry,mode,true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 break;
3442 case S_IFCHR: case S_IFBLK:
Al Virodae6ad82011-06-26 11:50:15 -04003443 error = vfs_mknod(path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 new_decode_dev(dev));
3445 break;
3446 case S_IFIFO: case S_IFSOCK:
Al Virodae6ad82011-06-26 11:50:15 -04003447 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 }
Al Viroa8104a92012-07-20 02:25:00 +04003450out:
Al Viro921a1652012-07-20 01:15:31 +04003451 done_path_create(&path, dentry);
Jeff Layton972567f2012-12-20 16:00:10 -05003452 if (retry_estale(error, lookup_flags)) {
3453 lookup_flags |= LOOKUP_REVAL;
3454 goto retry;
3455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 return error;
3457}
3458
Al Viro8208a222011-07-25 17:32:17 -04003459SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003460{
3461 return sys_mknodat(AT_FDCWD, filename, mode, dev);
3462}
3463
Al Viro18bb1db2011-07-26 01:41:39 -04003464int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003466 int error = may_create(dir, dentry);
Al Viro8de52772012-02-06 12:45:27 -05003467 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468
3469 if (error)
3470 return error;
3471
Al Viroacfa4382008-12-04 10:06:33 -05003472 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 return -EPERM;
3474
3475 mode &= (S_IRWXUGO|S_ISVTX);
3476 error = security_inode_mkdir(dir, dentry, mode);
3477 if (error)
3478 return error;
3479
Al Viro8de52772012-02-06 12:45:27 -05003480 if (max_links && dir->i_nlink >= max_links)
3481 return -EMLINK;
3482
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003484 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003485 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 return error;
3487}
Al Viro4d359502014-03-14 12:20:17 -04003488EXPORT_SYMBOL(vfs_mkdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489
Al Viroa218d0f2011-11-21 14:59:34 -05003490SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491{
Dave Hansen6902d922006-09-30 23:29:01 -07003492 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003493 struct path path;
3494 int error;
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003495 unsigned int lookup_flags = LOOKUP_DIRECTORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003497retry:
3498 dentry = user_path_create(dfd, pathname, &path, lookup_flags);
Dave Hansen6902d922006-09-30 23:29:01 -07003499 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003500 return PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003501
Al Virodae6ad82011-06-26 11:50:15 -04003502 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003503 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003504 error = security_path_mkdir(&path, dentry, mode);
Al Viroa8104a92012-07-20 02:25:00 +04003505 if (!error)
3506 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
Al Viro921a1652012-07-20 01:15:31 +04003507 done_path_create(&path, dentry);
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003508 if (retry_estale(error, lookup_flags)) {
3509 lookup_flags |= LOOKUP_REVAL;
3510 goto retry;
3511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 return error;
3513}
3514
Al Viroa218d0f2011-11-21 14:59:34 -05003515SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003516{
3517 return sys_mkdirat(AT_FDCWD, pathname, mode);
3518}
3519
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520/*
Sage Weila71905f2011-05-24 13:06:08 -07003521 * The dentry_unhash() helper will try to drop the dentry early: we
J. Bruce Fieldsc0d02592012-02-15 11:48:40 -05003522 * should have a usage count of 1 if we're the only user of this
Sage Weila71905f2011-05-24 13:06:08 -07003523 * dentry, and if that is true (possibly after pruning the dcache),
3524 * then we drop the dentry now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 *
3526 * A low-level filesystem can, if it choses, legally
3527 * do a
3528 *
3529 * if (!d_unhashed(dentry))
3530 * return -EBUSY;
3531 *
3532 * if it cannot handle the case of removing a directory
3533 * that is still in use by something else..
3534 */
3535void dentry_unhash(struct dentry *dentry)
3536{
Vasily Averindc168422006-12-06 20:37:07 -08003537 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 spin_lock(&dentry->d_lock);
Waiman Long98474232013-08-28 18:24:59 -07003539 if (dentry->d_lockref.count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 __d_drop(dentry);
3541 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542}
Al Viro4d359502014-03-14 12:20:17 -04003543EXPORT_SYMBOL(dentry_unhash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544
3545int vfs_rmdir(struct inode *dir, struct dentry *dentry)
3546{
3547 int error = may_delete(dir, dentry, 1);
3548
3549 if (error)
3550 return error;
3551
Al Viroacfa4382008-12-04 10:06:33 -05003552 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 return -EPERM;
3554
Al Viro1d2ef592011-09-14 18:55:41 +01003555 dget(dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003556 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557
Sage Weil912dbc12011-05-24 13:06:11 -07003558 error = -EBUSY;
3559 if (d_mountpoint(dentry))
3560 goto out;
3561
3562 error = security_inode_rmdir(dir, dentry);
3563 if (error)
3564 goto out;
3565
Sage Weil3cebde22011-05-29 21:20:59 -07003566 shrink_dcache_parent(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003567 error = dir->i_op->rmdir(dir, dentry);
3568 if (error)
3569 goto out;
3570
3571 dentry->d_inode->i_flags |= S_DEAD;
3572 dont_mount(dentry);
3573
3574out:
3575 mutex_unlock(&dentry->d_inode->i_mutex);
Al Viro1d2ef592011-09-14 18:55:41 +01003576 dput(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003577 if (!error)
3578 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 return error;
3580}
Al Viro4d359502014-03-14 12:20:17 -04003581EXPORT_SYMBOL(vfs_rmdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003583static long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584{
3585 int error = 0;
Jeff Layton91a27b22012-10-10 15:25:28 -04003586 struct filename *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 struct dentry *dentry;
3588 struct nameidata nd;
Jeff Laytonc6ee9202012-12-20 16:28:33 -05003589 unsigned int lookup_flags = 0;
3590retry:
3591 name = user_path_parent(dfd, pathname, &nd, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04003592 if (IS_ERR(name))
3593 return PTR_ERR(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594
3595 switch(nd.last_type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003596 case LAST_DOTDOT:
3597 error = -ENOTEMPTY;
3598 goto exit1;
3599 case LAST_DOT:
3600 error = -EINVAL;
3601 goto exit1;
3602 case LAST_ROOT:
3603 error = -EBUSY;
3604 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003606
3607 nd.flags &= ~LOOKUP_PARENT;
Jan Karac30dabf2012-06-12 16:20:30 +02003608 error = mnt_want_write(nd.path.mnt);
3609 if (error)
3610 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003611
Jan Blunck4ac91372008-02-14 19:34:32 -08003612 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08003613 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003615 if (IS_ERR(dentry))
3616 goto exit2;
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04003617 if (!dentry->d_inode) {
3618 error = -ENOENT;
3619 goto exit3;
3620 }
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003621 error = security_path_rmdir(&nd.path, dentry);
3622 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02003623 goto exit3;
Jan Blunck4ac91372008-02-14 19:34:32 -08003624 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
Dave Hansen06227532008-02-15 14:37:34 -08003625exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07003626 dput(dentry);
3627exit2:
Jan Blunck4ac91372008-02-14 19:34:32 -08003628 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Karac30dabf2012-06-12 16:20:30 +02003629 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003631 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 putname(name);
Jeff Laytonc6ee9202012-12-20 16:28:33 -05003633 if (retry_estale(error, lookup_flags)) {
3634 lookup_flags |= LOOKUP_REVAL;
3635 goto retry;
3636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 return error;
3638}
3639
Heiko Carstens3cdad422009-01-14 14:14:22 +01003640SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003641{
3642 return do_rmdir(AT_FDCWD, pathname);
3643}
3644
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003645/**
3646 * vfs_unlink - unlink a filesystem object
3647 * @dir: parent directory
3648 * @dentry: victim
3649 * @delegated_inode: returns victim inode, if the inode is delegated.
3650 *
3651 * The caller must hold dir->i_mutex.
3652 *
3653 * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
3654 * return a reference to the inode in delegated_inode. The caller
3655 * should then break the delegation on that inode and retry. Because
3656 * breaking a delegation may take a long time, the caller should drop
3657 * dir->i_mutex before doing so.
3658 *
3659 * Alternatively, a caller may pass NULL for delegated_inode. This may
3660 * be appropriate for callers that expect the underlying filesystem not
3661 * to be NFS exported.
3662 */
3663int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664{
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003665 struct inode *target = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 int error = may_delete(dir, dentry, 0);
3667
3668 if (error)
3669 return error;
3670
Al Viroacfa4382008-12-04 10:06:33 -05003671 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 return -EPERM;
3673
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003674 mutex_lock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 if (d_mountpoint(dentry))
3676 error = -EBUSY;
3677 else {
3678 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05003679 if (!error) {
J. Bruce Fields5a146962012-08-28 07:50:40 -07003680 error = try_break_deleg(target, delegated_inode);
3681 if (error)
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003682 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 error = dir->i_op->unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05003684 if (!error)
Al Virod83c49f2010-04-30 17:17:09 -04003685 dont_mount(dentry);
Al Virobec10522010-03-03 14:12:08 -05003686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 }
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003688out:
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003689 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690
3691 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
3692 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003693 fsnotify_link_count(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 }
Robert Love0eeca282005-07-12 17:06:03 -04003696
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 return error;
3698}
Al Viro4d359502014-03-14 12:20:17 -04003699EXPORT_SYMBOL(vfs_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700
3701/*
3702 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003703 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 * writeout happening, and we don't want to prevent access to the directory
3705 * while waiting on the I/O.
3706 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003707static long do_unlinkat(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708{
Al Viro2ad94ae2008-07-21 09:32:51 -04003709 int error;
Jeff Layton91a27b22012-10-10 15:25:28 -04003710 struct filename *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 struct dentry *dentry;
3712 struct nameidata nd;
3713 struct inode *inode = NULL;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003714 struct inode *delegated_inode = NULL;
Jeff Layton5d18f812012-12-20 16:38:04 -05003715 unsigned int lookup_flags = 0;
3716retry:
3717 name = user_path_parent(dfd, pathname, &nd, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04003718 if (IS_ERR(name))
3719 return PTR_ERR(name);
Al Viro2ad94ae2008-07-21 09:32:51 -04003720
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 error = -EISDIR;
3722 if (nd.last_type != LAST_NORM)
3723 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003724
3725 nd.flags &= ~LOOKUP_PARENT;
Jan Karac30dabf2012-06-12 16:20:30 +02003726 error = mnt_want_write(nd.path.mnt);
3727 if (error)
3728 goto exit1;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003729retry_deleg:
Jan Blunck4ac91372008-02-14 19:34:32 -08003730 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08003731 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 error = PTR_ERR(dentry);
3733 if (!IS_ERR(dentry)) {
3734 /* Why not before? Because we want correct error value */
Török Edwin50338b82011-06-16 00:06:14 +03003735 if (nd.last.name[nd.last.len])
3736 goto slashes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737 inode = dentry->d_inode;
David Howellsb18825a2013-09-12 19:22:53 +01003738 if (d_is_negative(dentry))
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04003739 goto slashes;
3740 ihold(inode);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003741 error = security_path_unlink(&nd.path, dentry);
3742 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02003743 goto exit2;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003744 error = vfs_unlink(nd.path.dentry->d_inode, dentry, &delegated_inode);
Jan Karac30dabf2012-06-12 16:20:30 +02003745exit2:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 dput(dentry);
3747 }
Jan Blunck4ac91372008-02-14 19:34:32 -08003748 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 if (inode)
3750 iput(inode); /* truncate the inode here */
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003751 inode = NULL;
3752 if (delegated_inode) {
J. Bruce Fields5a146962012-08-28 07:50:40 -07003753 error = break_deleg_wait(&delegated_inode);
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003754 if (!error)
3755 goto retry_deleg;
3756 }
Jan Karac30dabf2012-06-12 16:20:30 +02003757 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003759 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 putname(name);
Jeff Layton5d18f812012-12-20 16:38:04 -05003761 if (retry_estale(error, lookup_flags)) {
3762 lookup_flags |= LOOKUP_REVAL;
3763 inode = NULL;
3764 goto retry;
3765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 return error;
3767
3768slashes:
David Howellsb18825a2013-09-12 19:22:53 +01003769 if (d_is_negative(dentry))
3770 error = -ENOENT;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02003771 else if (d_is_dir(dentry))
David Howellsb18825a2013-09-12 19:22:53 +01003772 error = -EISDIR;
3773 else
3774 error = -ENOTDIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 goto exit2;
3776}
3777
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003778SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003779{
3780 if ((flag & ~AT_REMOVEDIR) != 0)
3781 return -EINVAL;
3782
3783 if (flag & AT_REMOVEDIR)
3784 return do_rmdir(dfd, pathname);
3785
3786 return do_unlinkat(dfd, pathname);
3787}
3788
Heiko Carstens3480b252009-01-14 14:14:16 +01003789SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003790{
3791 return do_unlinkat(AT_FDCWD, pathname);
3792}
3793
Miklos Szeredidb2e7472008-06-24 16:50:16 +02003794int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003796 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797
3798 if (error)
3799 return error;
3800
Al Viroacfa4382008-12-04 10:06:33 -05003801 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 return -EPERM;
3803
3804 error = security_inode_symlink(dir, dentry, oldname);
3805 if (error)
3806 return error;
3807
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003809 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003810 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 return error;
3812}
Al Viro4d359502014-03-14 12:20:17 -04003813EXPORT_SYMBOL(vfs_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003815SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
3816 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817{
Al Viro2ad94ae2008-07-21 09:32:51 -04003818 int error;
Jeff Layton91a27b22012-10-10 15:25:28 -04003819 struct filename *from;
Dave Hansen6902d922006-09-30 23:29:01 -07003820 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003821 struct path path;
Jeff Laytonf46d3562012-12-11 12:10:08 -05003822 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823
3824 from = getname(oldname);
Al Viro2ad94ae2008-07-21 09:32:51 -04003825 if (IS_ERR(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 return PTR_ERR(from);
Jeff Laytonf46d3562012-12-11 12:10:08 -05003827retry:
3828 dentry = user_path_create(newdfd, newname, &path, lookup_flags);
Dave Hansen6902d922006-09-30 23:29:01 -07003829 error = PTR_ERR(dentry);
3830 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003831 goto out_putname;
Dave Hansen6902d922006-09-30 23:29:01 -07003832
Jeff Layton91a27b22012-10-10 15:25:28 -04003833 error = security_path_symlink(&path, dentry, from->name);
Al Viroa8104a92012-07-20 02:25:00 +04003834 if (!error)
Jeff Layton91a27b22012-10-10 15:25:28 -04003835 error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
Al Viro921a1652012-07-20 01:15:31 +04003836 done_path_create(&path, dentry);
Jeff Laytonf46d3562012-12-11 12:10:08 -05003837 if (retry_estale(error, lookup_flags)) {
3838 lookup_flags |= LOOKUP_REVAL;
3839 goto retry;
3840 }
Dave Hansen6902d922006-09-30 23:29:01 -07003841out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 putname(from);
3843 return error;
3844}
3845
Heiko Carstens3480b252009-01-14 14:14:16 +01003846SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003847{
3848 return sys_symlinkat(oldname, AT_FDCWD, newname);
3849}
3850
J. Bruce Fields146a8592011-09-20 17:14:31 -04003851/**
3852 * vfs_link - create a new link
3853 * @old_dentry: object to be linked
3854 * @dir: new parent
3855 * @new_dentry: where to create the new link
3856 * @delegated_inode: returns inode needing a delegation break
3857 *
3858 * The caller must hold dir->i_mutex
3859 *
3860 * If vfs_link discovers a delegation on the to-be-linked file in need
3861 * of breaking, it will return -EWOULDBLOCK and return a reference to the
3862 * inode in delegated_inode. The caller should then break the delegation
3863 * and retry. Because breaking a delegation may take a long time, the
3864 * caller should drop the i_mutex before doing so.
3865 *
3866 * Alternatively, a caller may pass NULL for delegated_inode. This may
3867 * be appropriate for callers that expect the underlying filesystem not
3868 * to be NFS exported.
3869 */
3870int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871{
3872 struct inode *inode = old_dentry->d_inode;
Al Viro8de52772012-02-06 12:45:27 -05003873 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 int error;
3875
3876 if (!inode)
3877 return -ENOENT;
3878
Miklos Szeredia95164d2008-07-30 15:08:48 +02003879 error = may_create(dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 if (error)
3881 return error;
3882
3883 if (dir->i_sb != inode->i_sb)
3884 return -EXDEV;
3885
3886 /*
3887 * A link to an append-only or immutable file cannot be created.
3888 */
3889 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3890 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05003891 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003893 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894 return -EPERM;
3895
3896 error = security_inode_link(old_dentry, dir, new_dentry);
3897 if (error)
3898 return error;
3899
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003900 mutex_lock(&inode->i_mutex);
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05303901 /* Make sure we don't allow creating hardlink to an unlinked file */
Al Virof4e0c302013-06-11 08:34:36 +04003902 if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05303903 error = -ENOENT;
Al Viro8de52772012-02-06 12:45:27 -05003904 else if (max_links && inode->i_nlink >= max_links)
3905 error = -EMLINK;
J. Bruce Fields146a8592011-09-20 17:14:31 -04003906 else {
3907 error = try_break_deleg(inode, delegated_inode);
3908 if (!error)
3909 error = dir->i_op->link(old_dentry, dir, new_dentry);
3910 }
Al Virof4e0c302013-06-11 08:34:36 +04003911
3912 if (!error && (inode->i_state & I_LINKABLE)) {
3913 spin_lock(&inode->i_lock);
3914 inode->i_state &= ~I_LINKABLE;
3915 spin_unlock(&inode->i_lock);
3916 }
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003917 mutex_unlock(&inode->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07003918 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003919 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 return error;
3921}
Al Viro4d359502014-03-14 12:20:17 -04003922EXPORT_SYMBOL(vfs_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923
3924/*
3925 * Hardlinks are often used in delicate situations. We avoid
3926 * security-related surprises by not following symlinks on the
3927 * newname. --KAB
3928 *
3929 * We don't follow them on the oldname either to be compatible
3930 * with linux 2.0, and to avoid hard-linking to directories
3931 * and other special files. --ADM
3932 */
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003933SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
3934 int, newdfd, const char __user *, newname, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935{
3936 struct dentry *new_dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003937 struct path old_path, new_path;
J. Bruce Fields146a8592011-09-20 17:14:31 -04003938 struct inode *delegated_inode = NULL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303939 int how = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303942 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003943 return -EINVAL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303944 /*
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07003945 * To use null names we require CAP_DAC_READ_SEARCH
3946 * This ensures that not everyone will be able to create
3947 * handlink using the passed filedescriptor.
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303948 */
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07003949 if (flags & AT_EMPTY_PATH) {
3950 if (!capable(CAP_DAC_READ_SEARCH))
3951 return -ENOENT;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303952 how = LOOKUP_EMPTY;
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07003953 }
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003954
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303955 if (flags & AT_SYMLINK_FOLLOW)
3956 how |= LOOKUP_FOLLOW;
Jeff Layton442e31c2012-12-20 16:15:38 -05003957retry:
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303958 error = user_path_at(olddfd, oldname, how, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04003960 return error;
3961
Jeff Layton442e31c2012-12-20 16:15:38 -05003962 new_dentry = user_path_create(newdfd, newname, &new_path,
3963 (how & LOOKUP_REVAL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003965 if (IS_ERR(new_dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003966 goto out;
3967
3968 error = -EXDEV;
3969 if (old_path.mnt != new_path.mnt)
3970 goto out_dput;
Kees Cook800179c2012-07-25 17:29:07 -07003971 error = may_linkat(&old_path);
3972 if (unlikely(error))
3973 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04003974 error = security_path_link(old_path.dentry, &new_path, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003975 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04003976 goto out_dput;
J. Bruce Fields146a8592011-09-20 17:14:31 -04003977 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode);
Dave Hansen75c3f292008-02-15 14:37:45 -08003978out_dput:
Al Viro921a1652012-07-20 01:15:31 +04003979 done_path_create(&new_path, new_dentry);
J. Bruce Fields146a8592011-09-20 17:14:31 -04003980 if (delegated_inode) {
3981 error = break_deleg_wait(&delegated_inode);
Oleg Drokind22e6332014-01-31 15:41:58 -05003982 if (!error) {
3983 path_put(&old_path);
J. Bruce Fields146a8592011-09-20 17:14:31 -04003984 goto retry;
Oleg Drokind22e6332014-01-31 15:41:58 -05003985 }
J. Bruce Fields146a8592011-09-20 17:14:31 -04003986 }
Jeff Layton442e31c2012-12-20 16:15:38 -05003987 if (retry_estale(error, how)) {
Oleg Drokind22e6332014-01-31 15:41:58 -05003988 path_put(&old_path);
Jeff Layton442e31c2012-12-20 16:15:38 -05003989 how |= LOOKUP_REVAL;
3990 goto retry;
3991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992out:
Al Viro2d8f3032008-07-22 09:59:21 -04003993 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994
3995 return error;
3996}
3997
Heiko Carstens3480b252009-01-14 14:14:16 +01003998SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003999{
Ulrich Drepperc04030e2006-02-24 13:04:21 -08004000 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004001}
4002
Miklos Szeredibc270272014-04-01 17:08:42 +02004003/**
4004 * vfs_rename - rename a filesystem object
4005 * @old_dir: parent of source
4006 * @old_dentry: source
4007 * @new_dir: parent of destination
4008 * @new_dentry: destination
4009 * @delegated_inode: returns an inode needing a delegation break
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004010 * @flags: rename flags
Miklos Szeredibc270272014-04-01 17:08:42 +02004011 *
4012 * The caller must hold multiple mutexes--see lock_rename()).
4013 *
4014 * If vfs_rename discovers a delegation in need of breaking at either
4015 * the source or destination, it will return -EWOULDBLOCK and return a
4016 * reference to the inode in delegated_inode. The caller should then
4017 * break the delegation and retry. Because breaking a delegation may
4018 * take a long time, the caller should drop all locks before doing
4019 * so.
4020 *
4021 * Alternatively, a caller may pass NULL for delegated_inode. This may
4022 * be appropriate for callers that expect the underlying filesystem not
4023 * to be NFS exported.
4024 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 * The worst of all namespace operations - renaming directory. "Perverted"
4026 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
4027 * Problems:
J. Bruce Fieldsd03b29a2014-02-17 16:52:33 -05004028 * a) we can get into loop creation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 * b) race potential - two innocent renames can create a loop together.
4030 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004031 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 * story.
J. Bruce Fields6cedba82012-03-05 11:40:41 -05004033 * c) we have to lock _four_ objects - parents and victim (if it exists),
4034 * and source (if it is not a directory).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004035 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 * whether the target exists). Solution: try to be smart with locking
4037 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004038 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 * move will be locked. Thus we can rank directories by the tree
4040 * (ancestors first) and rank all non-directories after them.
4041 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004042 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 * HOWEVER, it relies on the assumption that any object with ->lookup()
4044 * has no more than 1 dentry. If "hybrid" objects will ever appear,
4045 * we'd better make sure that there's no link(2) for them.
Sage Weile4eaac02011-05-24 13:06:07 -07004046 * d) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004047 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05004049 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 * locking].
4051 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004053 struct inode *new_dir, struct dentry *new_dentry,
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004054 struct inode **delegated_inode, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055{
4056 int error;
Miklos Szeredibc270272014-04-01 17:08:42 +02004057 bool is_dir = d_is_dir(old_dentry);
Eric Paris59b0df22010-02-08 12:53:52 -05004058 const unsigned char *old_name;
Miklos Szeredibc270272014-04-01 17:08:42 +02004059 struct inode *source = old_dentry->d_inode;
4060 struct inode *target = new_dentry->d_inode;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004061 bool new_is_dir = false;
4062 unsigned max_links = new_dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063
Miklos Szeredibc270272014-04-01 17:08:42 +02004064 if (source == target)
4065 return 0;
4066
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 error = may_delete(old_dir, old_dentry, is_dir);
4068 if (error)
4069 return error;
4070
Miklos Szeredida1ce062014-04-01 17:08:43 +02004071 if (!target) {
Miklos Szeredia95164d2008-07-30 15:08:48 +02004072 error = may_create(new_dir, new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004073 } else {
4074 new_is_dir = d_is_dir(new_dentry);
4075
4076 if (!(flags & RENAME_EXCHANGE))
4077 error = may_delete(new_dir, new_dentry, is_dir);
4078 else
4079 error = may_delete(new_dir, new_dentry, new_is_dir);
4080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 if (error)
4082 return error;
4083
Miklos Szeredi7177a9c2014-07-23 15:15:30 +02004084 if (!old_dir->i_op->rename && !old_dir->i_op->rename2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 return -EPERM;
4086
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004087 if (flags && !old_dir->i_op->rename2)
4088 return -EINVAL;
4089
Miklos Szeredibc270272014-04-01 17:08:42 +02004090 /*
4091 * If we are going to change the parent - check write permissions,
4092 * we'll need to flip '..'.
4093 */
Miklos Szeredida1ce062014-04-01 17:08:43 +02004094 if (new_dir != old_dir) {
4095 if (is_dir) {
4096 error = inode_permission(source, MAY_WRITE);
4097 if (error)
4098 return error;
4099 }
4100 if ((flags & RENAME_EXCHANGE) && new_is_dir) {
4101 error = inode_permission(target, MAY_WRITE);
4102 if (error)
4103 return error;
4104 }
Miklos Szeredibc270272014-04-01 17:08:42 +02004105 }
Robert Love0eeca282005-07-12 17:06:03 -04004106
Miklos Szeredi0b3974e2014-04-01 17:08:43 +02004107 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry,
4108 flags);
Miklos Szeredibc270272014-04-01 17:08:42 +02004109 if (error)
4110 return error;
4111
4112 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
4113 dget(new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004114 if (!is_dir || (flags & RENAME_EXCHANGE))
Miklos Szeredibc270272014-04-01 17:08:42 +02004115 lock_two_nondirectories(source, target);
4116 else if (target)
4117 mutex_lock(&target->i_mutex);
4118
4119 error = -EBUSY;
4120 if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
4121 goto out;
4122
Miklos Szeredida1ce062014-04-01 17:08:43 +02004123 if (max_links && new_dir != old_dir) {
Miklos Szeredibc270272014-04-01 17:08:42 +02004124 error = -EMLINK;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004125 if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links)
Miklos Szeredibc270272014-04-01 17:08:42 +02004126 goto out;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004127 if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir &&
4128 old_dir->i_nlink >= max_links)
4129 goto out;
4130 }
4131 if (is_dir && !(flags & RENAME_EXCHANGE) && target)
4132 shrink_dcache_parent(new_dentry);
4133 if (!is_dir) {
Miklos Szeredibc270272014-04-01 17:08:42 +02004134 error = try_break_deleg(source, delegated_inode);
4135 if (error)
4136 goto out;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004137 }
4138 if (target && !new_is_dir) {
4139 error = try_break_deleg(target, delegated_inode);
4140 if (error)
4141 goto out;
Miklos Szeredibc270272014-04-01 17:08:42 +02004142 }
Miklos Szeredi7177a9c2014-07-23 15:15:30 +02004143 if (!old_dir->i_op->rename2) {
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004144 error = old_dir->i_op->rename(old_dir, old_dentry,
4145 new_dir, new_dentry);
4146 } else {
Miklos Szeredi7177a9c2014-07-23 15:15:30 +02004147 WARN_ON(old_dir->i_op->rename != NULL);
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004148 error = old_dir->i_op->rename2(old_dir, old_dentry,
4149 new_dir, new_dentry, flags);
4150 }
Miklos Szeredibc270272014-04-01 17:08:42 +02004151 if (error)
4152 goto out;
4153
Miklos Szeredida1ce062014-04-01 17:08:43 +02004154 if (!(flags & RENAME_EXCHANGE) && target) {
Miklos Szeredibc270272014-04-01 17:08:42 +02004155 if (is_dir)
4156 target->i_flags |= S_DEAD;
4157 dont_mount(new_dentry);
4158 }
Miklos Szeredida1ce062014-04-01 17:08:43 +02004159 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) {
4160 if (!(flags & RENAME_EXCHANGE))
4161 d_move(old_dentry, new_dentry);
4162 else
4163 d_exchange(old_dentry, new_dentry);
4164 }
Miklos Szeredibc270272014-04-01 17:08:42 +02004165out:
Miklos Szeredida1ce062014-04-01 17:08:43 +02004166 if (!is_dir || (flags & RENAME_EXCHANGE))
Miklos Szeredibc270272014-04-01 17:08:42 +02004167 unlock_two_nondirectories(source, target);
4168 else if (target)
4169 mutex_unlock(&target->i_mutex);
4170 dput(new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004171 if (!error) {
Al Viro123df292009-12-25 04:57:57 -05004172 fsnotify_move(old_dir, new_dir, old_name, is_dir,
Miklos Szeredida1ce062014-04-01 17:08:43 +02004173 !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
4174 if (flags & RENAME_EXCHANGE) {
4175 fsnotify_move(new_dir, old_dir, old_dentry->d_name.name,
4176 new_is_dir, NULL, new_dentry);
4177 }
4178 }
Robert Love0eeca282005-07-12 17:06:03 -04004179 fsnotify_oldname_free(old_name);
4180
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 return error;
4182}
Al Viro4d359502014-03-14 12:20:17 -04004183EXPORT_SYMBOL(vfs_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004185SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
4186 int, newdfd, const char __user *, newname, unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187{
Al Viro2ad94ae2008-07-21 09:32:51 -04004188 struct dentry *old_dir, *new_dir;
4189 struct dentry *old_dentry, *new_dentry;
4190 struct dentry *trap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 struct nameidata oldnd, newnd;
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004192 struct inode *delegated_inode = NULL;
Jeff Layton91a27b22012-10-10 15:25:28 -04004193 struct filename *from;
4194 struct filename *to;
Jeff Laytonc6a94282012-12-11 12:10:10 -05004195 unsigned int lookup_flags = 0;
4196 bool should_retry = false;
Al Viro2ad94ae2008-07-21 09:32:51 -04004197 int error;
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004198
Miklos Szeredida1ce062014-04-01 17:08:43 +02004199 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
4200 return -EINVAL;
4201
4202 if ((flags & RENAME_NOREPLACE) && (flags & RENAME_EXCHANGE))
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004203 return -EINVAL;
4204
Jeff Laytonc6a94282012-12-11 12:10:10 -05004205retry:
4206 from = user_path_parent(olddfd, oldname, &oldnd, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04004207 if (IS_ERR(from)) {
4208 error = PTR_ERR(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 goto exit;
Jeff Layton91a27b22012-10-10 15:25:28 -04004210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211
Jeff Laytonc6a94282012-12-11 12:10:10 -05004212 to = user_path_parent(newdfd, newname, &newnd, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04004213 if (IS_ERR(to)) {
4214 error = PTR_ERR(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 goto exit1;
Jeff Layton91a27b22012-10-10 15:25:28 -04004216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217
4218 error = -EXDEV;
Jan Blunck4ac91372008-02-14 19:34:32 -08004219 if (oldnd.path.mnt != newnd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 goto exit2;
4221
Jan Blunck4ac91372008-02-14 19:34:32 -08004222 old_dir = oldnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 error = -EBUSY;
4224 if (oldnd.last_type != LAST_NORM)
4225 goto exit2;
4226
Jan Blunck4ac91372008-02-14 19:34:32 -08004227 new_dir = newnd.path.dentry;
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004228 if (flags & RENAME_NOREPLACE)
4229 error = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 if (newnd.last_type != LAST_NORM)
4231 goto exit2;
4232
Jan Karac30dabf2012-06-12 16:20:30 +02004233 error = mnt_want_write(oldnd.path.mnt);
4234 if (error)
4235 goto exit2;
4236
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004237 oldnd.flags &= ~LOOKUP_PARENT;
4238 newnd.flags &= ~LOOKUP_PARENT;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004239 if (!(flags & RENAME_EXCHANGE))
4240 newnd.flags |= LOOKUP_RENAME_TARGET;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004241
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004242retry_deleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 trap = lock_rename(new_dir, old_dir);
4244
Christoph Hellwig49705b72005-11-08 21:35:06 -08004245 old_dentry = lookup_hash(&oldnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 error = PTR_ERR(old_dentry);
4247 if (IS_ERR(old_dentry))
4248 goto exit3;
4249 /* source must exist */
4250 error = -ENOENT;
David Howellsb18825a2013-09-12 19:22:53 +01004251 if (d_is_negative(old_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 goto exit4;
Christoph Hellwig49705b72005-11-08 21:35:06 -08004253 new_dentry = lookup_hash(&newnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 error = PTR_ERR(new_dentry);
4255 if (IS_ERR(new_dentry))
4256 goto exit4;
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004257 error = -EEXIST;
4258 if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry))
4259 goto exit5;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004260 if (flags & RENAME_EXCHANGE) {
4261 error = -ENOENT;
4262 if (d_is_negative(new_dentry))
4263 goto exit5;
4264
4265 if (!d_is_dir(new_dentry)) {
4266 error = -ENOTDIR;
4267 if (newnd.last.name[newnd.last.len])
4268 goto exit5;
4269 }
4270 }
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004271 /* unless the source is a directory trailing slashes give -ENOTDIR */
4272 if (!d_is_dir(old_dentry)) {
4273 error = -ENOTDIR;
4274 if (oldnd.last.name[oldnd.last.len])
4275 goto exit5;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004276 if (!(flags & RENAME_EXCHANGE) && newnd.last.name[newnd.last.len])
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004277 goto exit5;
4278 }
4279 /* source should not be ancestor of target */
4280 error = -EINVAL;
4281 if (old_dentry == trap)
4282 goto exit5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 /* target should not be an ancestor of source */
Miklos Szeredida1ce062014-04-01 17:08:43 +02004284 if (!(flags & RENAME_EXCHANGE))
4285 error = -ENOTEMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 if (new_dentry == trap)
4287 goto exit5;
4288
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004289 error = security_path_rename(&oldnd.path, old_dentry,
Miklos Szeredi0b3974e2014-04-01 17:08:43 +02004290 &newnd.path, new_dentry, flags);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004291 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02004292 goto exit5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 error = vfs_rename(old_dir->d_inode, old_dentry,
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004294 new_dir->d_inode, new_dentry,
4295 &delegated_inode, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296exit5:
4297 dput(new_dentry);
4298exit4:
4299 dput(old_dentry);
4300exit3:
4301 unlock_rename(new_dir, old_dir);
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004302 if (delegated_inode) {
4303 error = break_deleg_wait(&delegated_inode);
4304 if (!error)
4305 goto retry_deleg;
4306 }
Jan Karac30dabf2012-06-12 16:20:30 +02004307 mnt_drop_write(oldnd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308exit2:
Jeff Laytonc6a94282012-12-11 12:10:10 -05004309 if (retry_estale(error, lookup_flags))
4310 should_retry = true;
Jan Blunck1d957f92008-02-14 19:34:35 -08004311 path_put(&newnd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04004312 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08004314 path_put(&oldnd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 putname(from);
Jeff Laytonc6a94282012-12-11 12:10:10 -05004316 if (should_retry) {
4317 should_retry = false;
4318 lookup_flags |= LOOKUP_REVAL;
4319 goto retry;
4320 }
Al Viro2ad94ae2008-07-21 09:32:51 -04004321exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 return error;
4323}
4324
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004325SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4326 int, newdfd, const char __user *, newname)
4327{
4328 return sys_renameat2(olddfd, oldname, newdfd, newname, 0);
4329}
4330
Heiko Carstensa26eab22009-01-14 14:14:17 +01004331SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004332{
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004333 return sys_renameat2(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004334}
4335
Al Viro5d826c82014-03-14 13:42:45 -04004336int readlink_copy(char __user *buffer, int buflen, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337{
Al Viro5d826c82014-03-14 13:42:45 -04004338 int len = PTR_ERR(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 if (IS_ERR(link))
4340 goto out;
4341
4342 len = strlen(link);
4343 if (len > (unsigned) buflen)
4344 len = buflen;
4345 if (copy_to_user(buffer, link, len))
4346 len = -EFAULT;
4347out:
4348 return len;
4349}
Al Viro5d826c82014-03-14 13:42:45 -04004350EXPORT_SYMBOL(readlink_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351
4352/*
4353 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
4354 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
4355 * using) it for any given inode is up to filesystem.
4356 */
4357int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4358{
4359 struct nameidata nd;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004360 void *cookie;
Marcin Slusarz694a1762008-06-09 16:40:37 -07004361 int res;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004362
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 nd.depth = 0;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004364 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
Marcin Slusarz694a1762008-06-09 16:40:37 -07004365 if (IS_ERR(cookie))
4366 return PTR_ERR(cookie);
4367
Al Viro5d826c82014-03-14 13:42:45 -04004368 res = readlink_copy(buffer, buflen, nd_get_link(&nd));
Marcin Slusarz694a1762008-06-09 16:40:37 -07004369 if (dentry->d_inode->i_op->put_link)
4370 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
4371 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372}
Al Viro4d359502014-03-14 12:20:17 -04004373EXPORT_SYMBOL(generic_readlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375/* get the link contents into pagecache */
4376static char *page_getlink(struct dentry * dentry, struct page **ppage)
4377{
Duane Griffinebd09ab2008-12-19 20:47:12 +00004378 char *kaddr;
4379 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 struct address_space *mapping = dentry->d_inode->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -07004381 page = read_mapping_page(mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 if (IS_ERR(page))
Nick Piggin6fe69002007-05-06 14:49:04 -07004383 return (char*)page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 *ppage = page;
Duane Griffinebd09ab2008-12-19 20:47:12 +00004385 kaddr = kmap(page);
4386 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
4387 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388}
4389
4390int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4391{
4392 struct page *page = NULL;
Al Viro5d826c82014-03-14 13:42:45 -04004393 int res = readlink_copy(buffer, buflen, page_getlink(dentry, &page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 if (page) {
4395 kunmap(page);
4396 page_cache_release(page);
4397 }
4398 return res;
4399}
Al Viro4d359502014-03-14 12:20:17 -04004400EXPORT_SYMBOL(page_readlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004402void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004404 struct page *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 nd_set_link(nd, page_getlink(dentry, &page));
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004406 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407}
Al Viro4d359502014-03-14 12:20:17 -04004408EXPORT_SYMBOL(page_follow_link_light);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004410void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004412 struct page *page = cookie;
4413
4414 if (page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415 kunmap(page);
4416 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 }
4418}
Al Viro4d359502014-03-14 12:20:17 -04004419EXPORT_SYMBOL(page_put_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420
Nick Piggin54566b22009-01-04 12:00:53 -08004421/*
4422 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
4423 */
4424int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425{
4426 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004427 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07004428 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08004429 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 char *kaddr;
Nick Piggin54566b22009-01-04 12:00:53 -08004431 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
4432 if (nofs)
4433 flags |= AOP_FLAG_NOFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434
NeilBrown7e53cac2006-03-25 03:07:57 -08004435retry:
Nick Pigginafddba42007-10-16 01:25:01 -07004436 err = pagecache_write_begin(NULL, mapping, 0, len-1,
Nick Piggin54566b22009-01-04 12:00:53 -08004437 flags, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07004439 goto fail;
4440
Cong Wange8e3c3d2011-11-25 23:14:27 +08004441 kaddr = kmap_atomic(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 memcpy(kaddr, symname, len-1);
Cong Wange8e3c3d2011-11-25 23:14:27 +08004443 kunmap_atomic(kaddr);
Nick Pigginafddba42007-10-16 01:25:01 -07004444
4445 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4446 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 if (err < 0)
4448 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07004449 if (err < len-1)
4450 goto retry;
4451
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 mark_inode_dirty(inode);
4453 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454fail:
4455 return err;
4456}
Al Viro4d359502014-03-14 12:20:17 -04004457EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004459int page_symlink(struct inode *inode, const char *symname, int len)
4460{
4461 return __page_symlink(inode, symname, len,
Nick Piggin54566b22009-01-04 12:00:53 -08004462 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004463}
Al Viro4d359502014-03-14 12:20:17 -04004464EXPORT_SYMBOL(page_symlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004465
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08004466const struct inode_operations page_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 .readlink = generic_readlink,
4468 .follow_link = page_follow_link_light,
4469 .put_link = page_put_link,
4470};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471EXPORT_SYMBOL(page_symlink_inode_operations);