blob: caa28051e197e898e3c2bc52afce37bcbb284853 [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 Torvalds1da177e2005-04-16 15:20:36 -070037#include <asm/uaccess.h>
38
Eric Parise81e3f42009-12-04 15:47:36 -050039#include "internal.h"
Al Viroc7105362011-11-24 18:22:03 -050040#include "mount.h"
Eric Parise81e3f42009-12-04 15:47:36 -050041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042/* [Feb-1997 T. Schoebel-Theuer]
43 * Fundamental changes in the pathname lookup mechanisms (namei)
44 * were necessary because of omirr. The reason is that omirr needs
45 * to know the _real_ pathname, not the user-supplied one, in case
46 * of symlinks (and also when transname replacements occur).
47 *
48 * The new code replaces the old recursive symlink resolution with
49 * an iterative one (in case of non-nested symlink chains). It does
50 * this with calls to <fs>_follow_link().
51 * As a side effect, dir_namei(), _namei() and follow_link() are now
52 * replaced with a single function lookup_dentry() that can handle all
53 * the special cases of the former code.
54 *
55 * With the new dcache, the pathname is stored at each inode, at least as
56 * long as the refcount of the inode is positive. As a side effect, the
57 * size of the dcache depends on the inode cache and thus is dynamic.
58 *
59 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
60 * resolution to correspond with current state of the code.
61 *
62 * Note that the symlink resolution is not *completely* iterative.
63 * There is still a significant amount of tail- and mid- recursion in
64 * the algorithm. Also, note that <fs>_readlink() is not used in
65 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
66 * may return different results than <fs>_follow_link(). Many virtual
67 * filesystems (including /proc) exhibit this behavior.
68 */
69
70/* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
71 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
72 * and the name already exists in form of a symlink, try to create the new
73 * name indicated by the symlink. The old code always complained that the
74 * name already exists, due to not following the symlink even if its target
75 * is nonexistent. The new semantics affects also mknod() and link() when
Lucas De Marchi25985ed2011-03-30 22:57:33 -030076 * the name is a symlink pointing to a non-existent name.
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 *
78 * I don't know which semantics is the right one, since I have no access
79 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
80 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
81 * "old" one. Personally, I think the new semantics is much more logical.
82 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
83 * file does succeed in both HP-UX and SunOs, but not in Solaris
84 * and in the old Linux semantics.
85 */
86
87/* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
88 * semantics. See the comments in "open_namei" and "do_link" below.
89 *
90 * [10-Sep-98 Alan Modra] Another symlink change.
91 */
92
93/* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
94 * inside the path - always follow.
95 * in the last component in creation/removal/renaming - never follow.
96 * if LOOKUP_FOLLOW passed - follow.
97 * if the pathname has trailing slashes - follow.
98 * otherwise - don't follow.
99 * (applied in that order).
100 *
101 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
102 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
103 * During the 2.4 we need to fix the userland stuff depending on it -
104 * hopefully we will be able to get rid of that wart in 2.5. So far only
105 * XEmacs seems to be relying on it...
106 */
107/*
108 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
Arjan van de Vena11f3a02006-03-23 03:00:33 -0800109 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 * any extra contention...
111 */
112
113/* In order to reduce some races, while at the same time doing additional
114 * checking and hopefully speeding things up, we copy filenames to the
115 * kernel data space before using them..
116 *
117 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
118 * PATH_MAX includes the nul terminator --RR.
119 */
Jeff Layton91a27b22012-10-10 15:25:28 -0400120void final_putname(struct filename *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Jeff Layton7950e382012-10-10 16:43:13 -0400122 if (name->separate) {
123 __putname(name->name);
124 kfree(name);
125 } else {
126 __putname(name);
127 }
Jeff Layton91a27b22012-10-10 15:25:28 -0400128}
129
Jeff Layton7950e382012-10-10 16:43:13 -0400130#define EMBEDDED_NAME_MAX (PATH_MAX - sizeof(struct filename))
131
Jeff Layton91a27b22012-10-10 15:25:28 -0400132static struct filename *
133getname_flags(const char __user *filename, int flags, int *empty)
134{
135 struct filename *result, *err;
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700136 int len;
Jeff Layton7950e382012-10-10 16:43:13 -0400137 long max;
138 char *kname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Jeff Layton7ac86262012-10-10 15:25:28 -0400140 result = audit_reusename(filename);
141 if (result)
142 return result;
143
Jeff Layton7950e382012-10-10 16:43:13 -0400144 result = __getname();
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700145 if (unlikely(!result))
Eric Paris4043cde2012-01-03 14:23:08 -0500146 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Jeff Layton7950e382012-10-10 16:43:13 -0400148 /*
149 * First, try to embed the struct filename inside the names_cache
150 * allocation
151 */
152 kname = (char *)result + sizeof(*result);
Jeff Layton91a27b22012-10-10 15:25:28 -0400153 result->name = kname;
Jeff Layton7950e382012-10-10 16:43:13 -0400154 result->separate = false;
155 max = EMBEDDED_NAME_MAX;
156
157recopy:
158 len = strncpy_from_user(kname, filename, max);
Jeff Layton91a27b22012-10-10 15:25:28 -0400159 if (unlikely(len < 0)) {
160 err = ERR_PTR(len);
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700161 goto error;
Jeff Layton91a27b22012-10-10 15:25:28 -0400162 }
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700163
Jeff Layton7950e382012-10-10 16:43:13 -0400164 /*
165 * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
166 * separate struct filename so we can dedicate the entire
167 * names_cache allocation for the pathname, and re-do the copy from
168 * userland.
169 */
170 if (len == EMBEDDED_NAME_MAX && max == EMBEDDED_NAME_MAX) {
171 kname = (char *)result;
172
173 result = kzalloc(sizeof(*result), GFP_KERNEL);
174 if (!result) {
175 err = ERR_PTR(-ENOMEM);
176 result = (struct filename *)kname;
177 goto error;
178 }
179 result->name = kname;
180 result->separate = true;
181 max = PATH_MAX;
182 goto recopy;
183 }
184
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700185 /* The empty path is special. */
186 if (unlikely(!len)) {
187 if (empty)
Eric Paris4043cde2012-01-03 14:23:08 -0500188 *empty = 1;
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700189 err = ERR_PTR(-ENOENT);
190 if (!(flags & LOOKUP_EMPTY))
191 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 }
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700193
194 err = ERR_PTR(-ENAMETOOLONG);
Jeff Layton7950e382012-10-10 16:43:13 -0400195 if (unlikely(len >= PATH_MAX))
196 goto error;
197
198 result->uptr = filename;
199 audit_getname(result);
200 return result;
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700201
202error:
Jeff Layton7950e382012-10-10 16:43:13 -0400203 final_putname(result);
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700204 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206
Jeff Layton91a27b22012-10-10 15:25:28 -0400207struct filename *
208getname(const char __user * filename)
Al Virof52e0c12011-03-14 18:56:51 -0400209{
Linus Torvaldsf7493e52012-03-22 16:10:40 -0700210 return getname_flags(filename, 0, NULL);
Al Virof52e0c12011-03-14 18:56:51 -0400211}
Jeff Layton91a27b22012-10-10 15:25:28 -0400212EXPORT_SYMBOL(getname);
Al Virof52e0c12011-03-14 18:56:51 -0400213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214#ifdef CONFIG_AUDITSYSCALL
Jeff Layton91a27b22012-10-10 15:25:28 -0400215void putname(struct filename *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
Al Viro5ac3a9c2006-07-16 06:38:45 -0400217 if (unlikely(!audit_dummy_context()))
Jeff Layton91a27b22012-10-10 15:25:28 -0400218 return audit_putname(name);
219 final_putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221#endif
222
Linus Torvaldse77819e2011-07-22 19:30:19 -0700223static int check_acl(struct inode *inode, int mask)
224{
Linus Torvalds84635d62011-07-25 22:47:03 -0700225#ifdef CONFIG_FS_POSIX_ACL
Linus Torvaldse77819e2011-07-22 19:30:19 -0700226 struct posix_acl *acl;
227
Linus Torvaldse77819e2011-07-22 19:30:19 -0700228 if (mask & MAY_NOT_BLOCK) {
Al Viro35678662011-08-02 21:32:13 -0400229 acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
230 if (!acl)
Linus Torvaldse77819e2011-07-22 19:30:19 -0700231 return -EAGAIN;
Al Viro35678662011-08-02 21:32:13 -0400232 /* no ->get_acl() calls in RCU mode... */
233 if (acl == ACL_NOT_CACHED)
234 return -ECHILD;
Ari Savolainen206b1d02011-08-06 19:43:07 +0300235 return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
Linus Torvaldse77819e2011-07-22 19:30:19 -0700236 }
237
238 acl = get_cached_acl(inode, ACL_TYPE_ACCESS);
239
240 /*
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200241 * A filesystem can force a ACL callback by just never filling the
242 * ACL cache. But normally you'd fill the cache either at inode
243 * instantiation time, or on the first ->get_acl call.
Linus Torvaldse77819e2011-07-22 19:30:19 -0700244 *
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200245 * If the filesystem doesn't have a get_acl() function at all, we'll
246 * just create the negative cache entry.
Linus Torvaldse77819e2011-07-22 19:30:19 -0700247 */
248 if (acl == ACL_NOT_CACHED) {
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200249 if (inode->i_op->get_acl) {
250 acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS);
251 if (IS_ERR(acl))
252 return PTR_ERR(acl);
253 } else {
254 set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
255 return -EAGAIN;
256 }
Linus Torvaldse77819e2011-07-22 19:30:19 -0700257 }
258
259 if (acl) {
260 int error = posix_acl_permission(inode, acl, mask);
261 posix_acl_release(acl);
262 return error;
263 }
Linus Torvalds84635d62011-07-25 22:47:03 -0700264#endif
Linus Torvaldse77819e2011-07-22 19:30:19 -0700265
266 return -EAGAIN;
267}
268
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700269/*
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530270 * This does the basic permission checking
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700271 */
Al Viro7e401452011-06-20 19:12:17 -0400272static int acl_permission_check(struct inode *inode, int mask)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700273{
Linus Torvalds26cf46b2011-05-13 11:51:01 -0700274 unsigned int mode = inode->i_mode;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700275
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -0800276 if (likely(uid_eq(current_fsuid(), inode->i_uid)))
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700277 mode >>= 6;
278 else {
Linus Torvaldse77819e2011-07-22 19:30:19 -0700279 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
Al Viro7e401452011-06-20 19:12:17 -0400280 int error = check_acl(inode, mask);
Nick Pigginb74c79e2011-01-07 17:49:58 +1100281 if (error != -EAGAIN)
282 return error;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700283 }
284
285 if (in_group_p(inode->i_gid))
286 mode >>= 3;
287 }
288
289 /*
290 * If the DACs are ok we don't need any capability check.
291 */
Al Viro9c2c7032011-06-20 19:06:22 -0400292 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700293 return 0;
294 return -EACCES;
295}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297/**
Nick Pigginb74c79e2011-01-07 17:49:58 +1100298 * generic_permission - check for access rights on a Posix-like filesystem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 * @inode: inode to check access rights for
Andreas Gruenbacher8fd90c82011-10-23 23:13:30 +0530300 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 *
302 * Used to check for read/write/execute permissions on a file.
303 * We use "fsuid" for this, letting us set arbitrary permissions
304 * for filesystem access without changing the "normal" uids which
Nick Pigginb74c79e2011-01-07 17:49:58 +1100305 * are used for other things.
306 *
307 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
308 * request cannot be satisfied (eg. requires blocking or too much complexity).
309 * It would then be called again in ref-walk mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 */
Al Viro2830ba72011-06-20 19:16:29 -0400311int generic_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700313 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 /*
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530316 * Do the basic permission checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 */
Al Viro7e401452011-06-20 19:12:17 -0400318 ret = acl_permission_check(inode, mask);
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700319 if (ret != -EACCES)
320 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Al Virod594e7e2011-06-20 19:55:42 -0400322 if (S_ISDIR(inode->i_mode)) {
323 /* DACs are overridable for directories */
Eric W. Biederman1a48e2a2011-11-14 16:24:06 -0800324 if (inode_capable(inode, CAP_DAC_OVERRIDE))
Al Virod594e7e2011-06-20 19:55:42 -0400325 return 0;
326 if (!(mask & MAY_WRITE))
Eric W. Biederman1a48e2a2011-11-14 16:24:06 -0800327 if (inode_capable(inode, CAP_DAC_READ_SEARCH))
Al Virod594e7e2011-06-20 19:55:42 -0400328 return 0;
329 return -EACCES;
330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 /*
332 * Read/write DACs are always overridable.
Al Virod594e7e2011-06-20 19:55:42 -0400333 * Executable DACs are overridable when there is
334 * at least one exec bit set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 */
Al Virod594e7e2011-06-20 19:55:42 -0400336 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
Eric W. Biederman1a48e2a2011-11-14 16:24:06 -0800337 if (inode_capable(inode, CAP_DAC_OVERRIDE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 return 0;
339
340 /*
341 * Searching includes executable on directories, else just read.
342 */
Serge E. Hallyn7ea66002009-12-29 14:50:19 -0600343 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
Al Virod594e7e2011-06-20 19:55:42 -0400344 if (mask == MAY_READ)
Eric W. Biederman1a48e2a2011-11-14 16:24:06 -0800345 if (inode_capable(inode, CAP_DAC_READ_SEARCH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 return 0;
347
348 return -EACCES;
349}
350
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700351/*
352 * We _really_ want to just do "generic_permission()" without
353 * even looking at the inode->i_op values. So we keep a cache
354 * flag in inode->i_opflags, that says "this has not special
355 * permission function, use the fast case".
356 */
357static inline int do_inode_permission(struct inode *inode, int mask)
358{
359 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
360 if (likely(inode->i_op->permission))
361 return inode->i_op->permission(inode, mask);
362
363 /* This gets set once for the inode lifetime */
364 spin_lock(&inode->i_lock);
365 inode->i_opflags |= IOP_FASTPERM;
366 spin_unlock(&inode->i_lock);
367 }
368 return generic_permission(inode, mask);
369}
370
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200371/**
David Howells0bdaea92012-06-25 12:55:46 +0100372 * __inode_permission - Check for access rights to a given inode
373 * @inode: Inode to check permission on
374 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200375 *
David Howells0bdaea92012-06-25 12:55:46 +0100376 * Check for read/write/execute permissions on an inode.
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530377 *
378 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
David Howells0bdaea92012-06-25 12:55:46 +0100379 *
380 * This does not check for a read-only file system. You probably want
381 * inode_permission().
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200382 */
David Howells0bdaea92012-06-25 12:55:46 +0100383int __inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Al Viroe6305c42008-07-15 21:03:57 -0400385 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700387 if (unlikely(mask & MAY_WRITE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 /*
389 * Nobody gets write access to an immutable file.
390 */
391 if (IS_IMMUTABLE(inode))
392 return -EACCES;
393 }
394
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700395 retval = do_inode_permission(inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (retval)
397 return retval;
398
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700399 retval = devcgroup_inode_permission(inode, mask);
400 if (retval)
401 return retval;
402
Eric Parisd09ca732010-07-23 11:43:57 -0400403 return security_inode_permission(inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
Al Virof4d6ff82011-06-19 13:14:21 -0400406/**
David Howells0bdaea92012-06-25 12:55:46 +0100407 * sb_permission - Check superblock-level permissions
408 * @sb: Superblock of inode to check permission on
Randy Dunlap55852632012-08-18 17:39:25 -0700409 * @inode: Inode to check permission on
David Howells0bdaea92012-06-25 12:55:46 +0100410 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
411 *
412 * Separate out file-system wide checks from inode-specific permission checks.
413 */
414static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
415{
416 if (unlikely(mask & MAY_WRITE)) {
417 umode_t mode = inode->i_mode;
418
419 /* Nobody gets write access to a read-only fs. */
420 if ((sb->s_flags & MS_RDONLY) &&
421 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
422 return -EROFS;
423 }
424 return 0;
425}
426
427/**
428 * inode_permission - Check for access rights to a given inode
429 * @inode: Inode to check permission on
430 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
431 *
432 * Check for read/write/execute permissions on an inode. We use fs[ug]id for
433 * this, letting us set arbitrary permissions for filesystem access without
434 * changing the "normal" UIDs which are used for other things.
435 *
436 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
437 */
438int inode_permission(struct inode *inode, int mask)
439{
440 int retval;
441
442 retval = sb_permission(inode->i_sb, inode, mask);
443 if (retval)
444 return retval;
445 return __inode_permission(inode, mask);
446}
447
448/**
Jan Blunck5dd784d02008-02-14 19:34:38 -0800449 * path_get - get a reference to a path
450 * @path: path to get the reference to
451 *
452 * Given a path increment the reference count to the dentry and the vfsmount.
453 */
Al Virodcf787f2013-03-01 23:51:07 -0500454void path_get(const struct path *path)
Jan Blunck5dd784d02008-02-14 19:34:38 -0800455{
456 mntget(path->mnt);
457 dget(path->dentry);
458}
459EXPORT_SYMBOL(path_get);
460
461/**
Jan Blunck1d957f92008-02-14 19:34:35 -0800462 * path_put - put a reference to a path
463 * @path: path to put the reference to
464 *
465 * Given a path decrement the reference count to the dentry and the vfsmount.
466 */
Al Virodcf787f2013-03-01 23:51:07 -0500467void path_put(const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Jan Blunck1d957f92008-02-14 19:34:35 -0800469 dput(path->dentry);
470 mntput(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
Jan Blunck1d957f92008-02-14 19:34:35 -0800472EXPORT_SYMBOL(path_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Al Viro19660af2011-03-25 10:32:48 -0400474/*
Nick Piggin31e6b012011-01-07 17:49:52 +1100475 * Path walking has 2 modes, rcu-walk and ref-walk (see
Al Viro19660af2011-03-25 10:32:48 -0400476 * Documentation/filesystems/path-lookup.txt). In situations when we can't
477 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
478 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
479 * mode. Refcounts are grabbed at the last known good point before rcu-walk
480 * got stuck, so ref-walk may continue from there. If this is not successful
481 * (eg. a seqcount has changed), then failure is returned and it's up to caller
482 * to restart the path walk from the beginning in ref-walk mode.
Nick Piggin31e6b012011-01-07 17:49:52 +1100483 */
Nick Piggin31e6b012011-01-07 17:49:52 +1100484
Al Viro32a79912012-07-18 20:43:19 +0400485static inline void lock_rcu_walk(void)
486{
487 br_read_lock(&vfsmount_lock);
488 rcu_read_lock();
489}
490
491static inline void unlock_rcu_walk(void)
492{
493 rcu_read_unlock();
494 br_read_unlock(&vfsmount_lock);
495}
496
Nick Piggin31e6b012011-01-07 17:49:52 +1100497/**
Al Viro19660af2011-03-25 10:32:48 -0400498 * unlazy_walk - try to switch to ref-walk mode.
499 * @nd: nameidata pathwalk data
500 * @dentry: child of nd->path.dentry or NULL
Randy Dunlap39191622011-01-08 19:36:21 -0800501 * Returns: 0 on success, -ECHILD on failure
Nick Piggin31e6b012011-01-07 17:49:52 +1100502 *
Al Viro19660af2011-03-25 10:32:48 -0400503 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
504 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
505 * @nd or NULL. Must be called from rcu-walk context.
Nick Piggin31e6b012011-01-07 17:49:52 +1100506 */
Al Viro19660af2011-03-25 10:32:48 -0400507static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
Nick Piggin31e6b012011-01-07 17:49:52 +1100508{
509 struct fs_struct *fs = current->fs;
510 struct dentry *parent = nd->path.dentry;
511
512 BUG_ON(!(nd->flags & LOOKUP_RCU));
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700513
514 /*
515 * Get a reference to the parent first: we're
516 * going to make "path_put(nd->path)" valid in
517 * non-RCU context for "terminate_walk()".
518 *
519 * If this doesn't work, return immediately with
520 * RCU walking still active (and then we will do
521 * the RCU walk cleanup in terminate_walk()).
522 */
523 if (!lockref_get_not_dead(&parent->d_lockref))
524 return -ECHILD;
525
526 /*
527 * After the mntget(), we terminate_walk() will do
528 * the right thing for non-RCU mode, and all our
529 * subsequent exit cases should unlock_rcu_walk()
530 * before returning.
531 */
532 mntget(nd->path.mnt);
533 nd->flags &= ~LOOKUP_RCU;
Linus Torvalds15570082013-09-02 11:38:06 -0700534
535 /*
536 * For a negative lookup, the lookup sequence point is the parents
537 * sequence point, and it only needs to revalidate the parent dentry.
538 *
539 * For a positive lookup, we need to move both the parent and the
540 * dentry from the RCU domain to be properly refcounted. And the
541 * sequence number in the dentry validates *both* dentry counters,
542 * since we checked the sequence number of the parent after we got
543 * the child sequence number. So we know the parent must still
544 * be valid if the child sequence number is still valid.
545 */
Al Viro19660af2011-03-25 10:32:48 -0400546 if (!dentry) {
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700547 if (read_seqcount_retry(&parent->d_seq, nd->seq))
548 goto out;
Al Viro19660af2011-03-25 10:32:48 -0400549 BUG_ON(nd->inode != parent->d_inode);
550 } else {
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700551 if (!lockref_get_not_dead(&dentry->d_lockref))
552 goto out;
553 if (read_seqcount_retry(&dentry->d_seq, nd->seq))
554 goto drop_dentry;
Al Viro19660af2011-03-25 10:32:48 -0400555 }
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700556
557 /*
558 * Sequence counts matched. Now make sure that the root is
559 * still valid and get it if required.
560 */
561 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
562 spin_lock(&fs->lock);
563 if (nd->root.mnt != fs->root.mnt || nd->root.dentry != fs->root.dentry)
564 goto unlock_and_drop_dentry;
Nick Piggin31e6b012011-01-07 17:49:52 +1100565 path_get(&nd->root);
566 spin_unlock(&fs->lock);
567 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100568
Al Viro32a79912012-07-18 20:43:19 +0400569 unlock_rcu_walk();
Nick Piggin31e6b012011-01-07 17:49:52 +1100570 return 0;
Al Viro19660af2011-03-25 10:32:48 -0400571
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700572unlock_and_drop_dentry:
573 spin_unlock(&fs->lock);
574drop_dentry:
575 unlock_rcu_walk();
Linus Torvalds15570082013-09-02 11:38:06 -0700576 dput(dentry);
Linus Torvaldsd0d27272013-09-10 12:17:49 -0700577 goto drop_root_mnt;
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700578out:
579 unlock_rcu_walk();
Linus Torvaldsd0d27272013-09-10 12:17:49 -0700580drop_root_mnt:
581 if (!(nd->flags & LOOKUP_ROOT))
582 nd->root.mnt = NULL;
Nick Piggin31e6b012011-01-07 17:49:52 +1100583 return -ECHILD;
584}
585
Al Viro4ce16ef32012-06-10 16:10:59 -0400586static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
Nick Piggin34286d62011-01-07 17:49:57 +1100587{
Al Viro4ce16ef32012-06-10 16:10:59 -0400588 return dentry->d_op->d_revalidate(dentry, flags);
Nick Piggin34286d62011-01-07 17:49:57 +1100589}
590
Al Viro9f1fafe2011-03-25 11:00:12 -0400591/**
592 * complete_walk - successful completion of path walk
593 * @nd: pointer nameidata
Jeff Layton39159de2009-12-07 12:01:50 -0500594 *
Al Viro9f1fafe2011-03-25 11:00:12 -0400595 * If we had been in RCU mode, drop out of it and legitimize nd->path.
596 * Revalidate the final result, unless we'd already done that during
597 * the path walk or the filesystem doesn't ask for it. Return 0 on
598 * success, -error on failure. In case of failure caller does not
599 * need to drop nd->path.
Jeff Layton39159de2009-12-07 12:01:50 -0500600 */
Al Viro9f1fafe2011-03-25 11:00:12 -0400601static int complete_walk(struct nameidata *nd)
Jeff Layton39159de2009-12-07 12:01:50 -0500602{
Al Viro16c2cd72011-02-22 15:50:10 -0500603 struct dentry *dentry = nd->path.dentry;
Jeff Layton39159de2009-12-07 12:01:50 -0500604 int status;
Jeff Layton39159de2009-12-07 12:01:50 -0500605
Al Viro9f1fafe2011-03-25 11:00:12 -0400606 if (nd->flags & LOOKUP_RCU) {
607 nd->flags &= ~LOOKUP_RCU;
608 if (!(nd->flags & LOOKUP_ROOT))
609 nd->root.mnt = NULL;
Linus Torvalds15570082013-09-02 11:38:06 -0700610
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700611 if (unlikely(!lockref_get_not_dead(&dentry->d_lockref))) {
Al Viro32a79912012-07-18 20:43:19 +0400612 unlock_rcu_walk();
Al Viro9f1fafe2011-03-25 11:00:12 -0400613 return -ECHILD;
614 }
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700615 if (read_seqcount_retry(&dentry->d_seq, nd->seq)) {
616 unlock_rcu_walk();
617 dput(dentry);
618 return -ECHILD;
619 }
Al Viro9f1fafe2011-03-25 11:00:12 -0400620 mntget(nd->path.mnt);
Al Viro32a79912012-07-18 20:43:19 +0400621 unlock_rcu_walk();
Al Viro9f1fafe2011-03-25 11:00:12 -0400622 }
623
Al Viro16c2cd72011-02-22 15:50:10 -0500624 if (likely(!(nd->flags & LOOKUP_JUMPED)))
Jeff Layton39159de2009-12-07 12:01:50 -0500625 return 0;
626
Jeff Laytonecf3d1f2013-02-20 11:19:05 -0500627 if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
Al Viro16c2cd72011-02-22 15:50:10 -0500628 return 0;
629
Jeff Laytonecf3d1f2013-02-20 11:19:05 -0500630 status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
Jeff Layton39159de2009-12-07 12:01:50 -0500631 if (status > 0)
632 return 0;
633
Al Viro16c2cd72011-02-22 15:50:10 -0500634 if (!status)
Jeff Layton39159de2009-12-07 12:01:50 -0500635 status = -ESTALE;
Al Viro16c2cd72011-02-22 15:50:10 -0500636
Al Viro9f1fafe2011-03-25 11:00:12 -0400637 path_put(&nd->path);
Jeff Layton39159de2009-12-07 12:01:50 -0500638 return status;
639}
640
Al Viro2a737872009-04-07 11:49:53 -0400641static __always_inline void set_root(struct nameidata *nd)
642{
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200643 if (!nd->root.mnt)
644 get_fs_root(current->fs, &nd->root);
Al Viro2a737872009-04-07 11:49:53 -0400645}
646
Al Viro6de88d72009-08-09 01:41:57 +0400647static int link_path_walk(const char *, struct nameidata *);
648
Nick Piggin31e6b012011-01-07 17:49:52 +1100649static __always_inline void set_root_rcu(struct nameidata *nd)
650{
651 if (!nd->root.mnt) {
652 struct fs_struct *fs = current->fs;
Nick Pigginc28cc362011-01-07 17:49:53 +1100653 unsigned seq;
654
655 do {
656 seq = read_seqcount_begin(&fs->seq);
657 nd->root = fs->root;
Tim Chenc1530012011-04-15 11:39:29 -0700658 nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
Nick Pigginc28cc362011-01-07 17:49:53 +1100659 } while (read_seqcount_retry(&fs->seq, seq));
Nick Piggin31e6b012011-01-07 17:49:52 +1100660 }
661}
662
Jan Blunck1d957f92008-02-14 19:34:35 -0800663static void path_put_conditional(struct path *path, struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700664{
665 dput(path->dentry);
Jan Blunck4ac91372008-02-14 19:34:32 -0800666 if (path->mnt != nd->path.mnt)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700667 mntput(path->mnt);
668}
669
Nick Piggin7b9337a2011-01-14 08:42:43 +0000670static inline void path_to_nameidata(const struct path *path,
671 struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700672{
Nick Piggin31e6b012011-01-07 17:49:52 +1100673 if (!(nd->flags & LOOKUP_RCU)) {
674 dput(nd->path.dentry);
675 if (nd->path.mnt != path->mnt)
676 mntput(nd->path.mnt);
Huang Shijie9a229682010-04-02 17:37:13 +0800677 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100678 nd->path.mnt = path->mnt;
Jan Blunck4ac91372008-02-14 19:34:32 -0800679 nd->path.dentry = path->dentry;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700680}
681
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400682/*
683 * Helper to directly jump to a known parsed path from ->follow_link,
684 * caller must have taken a reference to path beforehand.
685 */
686void nd_jump_link(struct nameidata *nd, struct path *path)
687{
688 path_put(&nd->path);
689
690 nd->path = *path;
691 nd->inode = nd->path.dentry->d_inode;
692 nd->flags |= LOOKUP_JUMPED;
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400693}
694
Al Viro574197e2011-03-14 22:20:34 -0400695static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
696{
697 struct inode *inode = link->dentry->d_inode;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400698 if (inode->i_op->put_link)
Al Viro574197e2011-03-14 22:20:34 -0400699 inode->i_op->put_link(link->dentry, nd, cookie);
700 path_put(link);
701}
702
Linus Torvalds561ec642012-10-26 10:05:07 -0700703int sysctl_protected_symlinks __read_mostly = 0;
704int sysctl_protected_hardlinks __read_mostly = 0;
Kees Cook800179c2012-07-25 17:29:07 -0700705
706/**
707 * may_follow_link - Check symlink following for unsafe situations
708 * @link: The path of the symlink
Randy Dunlap55852632012-08-18 17:39:25 -0700709 * @nd: nameidata pathwalk data
Kees Cook800179c2012-07-25 17:29:07 -0700710 *
711 * In the case of the sysctl_protected_symlinks sysctl being enabled,
712 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
713 * in a sticky world-writable directory. This is to protect privileged
714 * processes from failing races against path names that may change out
715 * from under them by way of other users creating malicious symlinks.
716 * It will permit symlinks to be followed only when outside a sticky
717 * world-writable directory, or when the uid of the symlink and follower
718 * match, or when the directory owner matches the symlink's owner.
719 *
720 * Returns 0 if following the symlink is allowed, -ve on error.
721 */
722static inline int may_follow_link(struct path *link, struct nameidata *nd)
723{
724 const struct inode *inode;
725 const struct inode *parent;
726
727 if (!sysctl_protected_symlinks)
728 return 0;
729
730 /* Allowed if owner and follower match. */
731 inode = link->dentry->d_inode;
Eric W. Biederman81abe272012-08-03 09:38:08 -0700732 if (uid_eq(current_cred()->fsuid, inode->i_uid))
Kees Cook800179c2012-07-25 17:29:07 -0700733 return 0;
734
735 /* Allowed if parent directory not sticky and world-writable. */
736 parent = nd->path.dentry->d_inode;
737 if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
738 return 0;
739
740 /* Allowed if parent directory and link owner match. */
Eric W. Biederman81abe272012-08-03 09:38:08 -0700741 if (uid_eq(parent->i_uid, inode->i_uid))
Kees Cook800179c2012-07-25 17:29:07 -0700742 return 0;
743
Sasha Levinffd8d102012-10-04 19:56:40 -0400744 audit_log_link_denied("follow_link", link);
Kees Cook800179c2012-07-25 17:29:07 -0700745 path_put_conditional(link, nd);
746 path_put(&nd->path);
747 return -EACCES;
748}
749
750/**
751 * safe_hardlink_source - Check for safe hardlink conditions
752 * @inode: the source inode to hardlink from
753 *
754 * Return false if at least one of the following conditions:
755 * - inode is not a regular file
756 * - inode is setuid
757 * - inode is setgid and group-exec
758 * - access failure for read and write
759 *
760 * Otherwise returns true.
761 */
762static bool safe_hardlink_source(struct inode *inode)
763{
764 umode_t mode = inode->i_mode;
765
766 /* Special files should not get pinned to the filesystem. */
767 if (!S_ISREG(mode))
768 return false;
769
770 /* Setuid files should not get pinned to the filesystem. */
771 if (mode & S_ISUID)
772 return false;
773
774 /* Executable setgid files should not get pinned to the filesystem. */
775 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
776 return false;
777
778 /* Hardlinking to unreadable or unwritable sources is dangerous. */
779 if (inode_permission(inode, MAY_READ | MAY_WRITE))
780 return false;
781
782 return true;
783}
784
785/**
786 * may_linkat - Check permissions for creating a hardlink
787 * @link: the source to hardlink from
788 *
789 * Block hardlink when all of:
790 * - sysctl_protected_hardlinks enabled
791 * - fsuid does not match inode
792 * - hardlink source is unsafe (see safe_hardlink_source() above)
793 * - not CAP_FOWNER
794 *
795 * Returns 0 if successful, -ve on error.
796 */
797static int may_linkat(struct path *link)
798{
799 const struct cred *cred;
800 struct inode *inode;
801
802 if (!sysctl_protected_hardlinks)
803 return 0;
804
805 cred = current_cred();
806 inode = link->dentry->d_inode;
807
808 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
809 * otherwise, it must be a safe source.
810 */
Eric W. Biederman81abe272012-08-03 09:38:08 -0700811 if (uid_eq(cred->fsuid, inode->i_uid) || safe_hardlink_source(inode) ||
Kees Cook800179c2012-07-25 17:29:07 -0700812 capable(CAP_FOWNER))
813 return 0;
814
Kees Cooka51d9ea2012-07-25 17:29:08 -0700815 audit_log_link_denied("linkat", link);
Kees Cook800179c2012-07-25 17:29:07 -0700816 return -EPERM;
817}
818
Al Virodef4af32009-12-26 08:37:05 -0500819static __always_inline int
Al Viro574197e2011-03-14 22:20:34 -0400820follow_link(struct path *link, struct nameidata *nd, void **p)
Ian Kent051d3812006-03-27 01:14:53 -0800821{
Nick Piggin7b9337a2011-01-14 08:42:43 +0000822 struct dentry *dentry = link->dentry;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400823 int error;
824 char *s;
Ian Kent051d3812006-03-27 01:14:53 -0800825
Al Viro844a3912011-02-15 00:38:26 -0500826 BUG_ON(nd->flags & LOOKUP_RCU);
827
Al Viro0e794582011-03-16 02:45:02 -0400828 if (link->mnt == nd->path.mnt)
829 mntget(link->mnt);
830
Al Viro6d7b5aa2012-06-10 04:15:17 -0400831 error = -ELOOP;
832 if (unlikely(current->total_link_count >= 40))
833 goto out_put_nd_path;
834
Al Viro574197e2011-03-14 22:20:34 -0400835 cond_resched();
836 current->total_link_count++;
837
Al Viro68ac1232012-03-15 08:21:57 -0400838 touch_atime(link);
Ian Kent051d3812006-03-27 01:14:53 -0800839 nd_set_link(nd, NULL);
840
Al Viro36f3b4f2011-02-22 21:24:38 -0500841 error = security_inode_follow_link(link->dentry, nd);
Al Viro6d7b5aa2012-06-10 04:15:17 -0400842 if (error)
843 goto out_put_nd_path;
Al Viro36f3b4f2011-02-22 21:24:38 -0500844
Al Viro86acdca12009-12-22 23:45:11 -0500845 nd->last_type = LAST_BIND;
Al Virodef4af32009-12-26 08:37:05 -0500846 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
847 error = PTR_ERR(*p);
Al Viro6d7b5aa2012-06-10 04:15:17 -0400848 if (IS_ERR(*p))
Christoph Hellwig408ef012012-06-18 10:47:03 -0400849 goto out_put_nd_path;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400850
851 error = 0;
852 s = nd_get_link(nd);
853 if (s) {
Al Viro443ed252013-09-10 12:00:43 -0400854 if (unlikely(IS_ERR(s))) {
855 path_put(&nd->path);
856 put_link(nd, link, *p);
857 return PTR_ERR(s);
858 }
859 if (*s == '/') {
860 set_root(nd);
861 path_put(&nd->path);
862 nd->path = nd->root;
863 path_get(&nd->root);
864 nd->flags |= LOOKUP_JUMPED;
865 }
866 nd->inode = nd->path.dentry->d_inode;
867 error = link_path_walk(s, nd);
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400868 if (unlikely(error))
869 put_link(nd, link, *p);
Ian Kent051d3812006-03-27 01:14:53 -0800870 }
Al Viro6d7b5aa2012-06-10 04:15:17 -0400871
872 return error;
873
874out_put_nd_path:
Arnd Bergmann98f6ef62012-10-11 13:20:00 +0000875 *p = NULL;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400876 path_put(&nd->path);
Al Viro6d7b5aa2012-06-10 04:15:17 -0400877 path_put(link);
Ian Kent051d3812006-03-27 01:14:53 -0800878 return error;
879}
880
Nick Piggin31e6b012011-01-07 17:49:52 +1100881static int follow_up_rcu(struct path *path)
882{
Al Viro0714a532011-11-24 22:19:58 -0500883 struct mount *mnt = real_mount(path->mnt);
884 struct mount *parent;
Nick Piggin31e6b012011-01-07 17:49:52 +1100885 struct dentry *mountpoint;
886
Al Viro0714a532011-11-24 22:19:58 -0500887 parent = mnt->mnt_parent;
888 if (&parent->mnt == path->mnt)
Nick Piggin31e6b012011-01-07 17:49:52 +1100889 return 0;
Al Viroa73324d2011-11-24 22:25:07 -0500890 mountpoint = mnt->mnt_mountpoint;
Nick Piggin31e6b012011-01-07 17:49:52 +1100891 path->dentry = mountpoint;
Al Viro0714a532011-11-24 22:19:58 -0500892 path->mnt = &parent->mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +1100893 return 1;
894}
895
David Howellsf015f1262012-06-25 12:55:28 +0100896/*
897 * follow_up - Find the mountpoint of path's vfsmount
898 *
899 * Given a path, find the mountpoint of its source file system.
900 * Replace @path with the path of the mountpoint in the parent mount.
901 * Up is towards /.
902 *
903 * Return 1 if we went up a level and 0 if we were already at the
904 * root.
905 */
Al Virobab77eb2009-04-18 03:26:48 -0400906int follow_up(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Al Viro0714a532011-11-24 22:19:58 -0500908 struct mount *mnt = real_mount(path->mnt);
909 struct mount *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 struct dentry *mountpoint;
Nick Piggin99b7db72010-08-18 04:37:39 +1000911
Andi Kleen962830d2012-05-08 13:32:02 +0930912 br_read_lock(&vfsmount_lock);
Al Viro0714a532011-11-24 22:19:58 -0500913 parent = mnt->mnt_parent;
Al Viro3c0a6162012-07-18 17:32:50 +0400914 if (parent == mnt) {
Andi Kleen962830d2012-05-08 13:32:02 +0930915 br_read_unlock(&vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 return 0;
917 }
Al Viro0714a532011-11-24 22:19:58 -0500918 mntget(&parent->mnt);
Al Viroa73324d2011-11-24 22:25:07 -0500919 mountpoint = dget(mnt->mnt_mountpoint);
Andi Kleen962830d2012-05-08 13:32:02 +0930920 br_read_unlock(&vfsmount_lock);
Al Virobab77eb2009-04-18 03:26:48 -0400921 dput(path->dentry);
922 path->dentry = mountpoint;
923 mntput(path->mnt);
Al Viro0714a532011-11-24 22:19:58 -0500924 path->mnt = &parent->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 return 1;
926}
927
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100928/*
David Howells9875cf82011-01-14 18:45:21 +0000929 * Perform an automount
930 * - return -EISDIR to tell follow_managed() to stop and return the path we
931 * were called with.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 */
David Howells9875cf82011-01-14 18:45:21 +0000933static int follow_automount(struct path *path, unsigned flags,
934 bool *need_mntput)
Nick Piggin31e6b012011-01-07 17:49:52 +1100935{
David Howells9875cf82011-01-14 18:45:21 +0000936 struct vfsmount *mnt;
David Howellsea5b7782011-01-14 19:10:03 +0000937 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +1100938
David Howells9875cf82011-01-14 18:45:21 +0000939 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
940 return -EREMOTE;
Al Viro463ffb22005-06-06 13:36:05 -0700941
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200942 /* We don't want to mount if someone's just doing a stat -
943 * unless they're stat'ing a directory and appended a '/' to
944 * the name.
945 *
946 * We do, however, want to mount if someone wants to open or
947 * create a file of any type under the mountpoint, wants to
948 * traverse through the mountpoint or wants to open the
949 * mounted directory. Also, autofs may mark negative dentries
950 * as being automount points. These will need the attentions
951 * of the daemon to instantiate them before they can be used.
David Howells9875cf82011-01-14 18:45:21 +0000952 */
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200953 if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
Linus Torvaldsd94c1772011-09-26 17:44:55 -0700954 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200955 path->dentry->d_inode)
956 return -EISDIR;
957
David Howells9875cf82011-01-14 18:45:21 +0000958 current->total_link_count++;
959 if (current->total_link_count >= 40)
960 return -ELOOP;
961
962 mnt = path->dentry->d_op->d_automount(path);
963 if (IS_ERR(mnt)) {
964 /*
965 * The filesystem is allowed to return -EISDIR here to indicate
966 * it doesn't want to automount. For instance, autofs would do
967 * this so that its userspace daemon can mount on this dentry.
968 *
969 * However, we can only permit this if it's a terminal point in
970 * the path being looked up; if it wasn't then the remainder of
971 * the path is inaccessible and we should say so.
972 */
Al Viro49084c32011-06-25 21:59:52 -0400973 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
David Howells9875cf82011-01-14 18:45:21 +0000974 return -EREMOTE;
975 return PTR_ERR(mnt);
976 }
David Howellsea5b7782011-01-14 19:10:03 +0000977
David Howells9875cf82011-01-14 18:45:21 +0000978 if (!mnt) /* mount collision */
979 return 0;
980
Al Viro8aef1882011-06-16 15:10:06 +0100981 if (!*need_mntput) {
982 /* lock_mount() may release path->mnt on error */
983 mntget(path->mnt);
984 *need_mntput = true;
985 }
Al Viro19a167a2011-01-17 01:35:23 -0500986 err = finish_automount(mnt, path);
David Howellsea5b7782011-01-14 19:10:03 +0000987
David Howellsea5b7782011-01-14 19:10:03 +0000988 switch (err) {
989 case -EBUSY:
990 /* Someone else made a mount here whilst we were busy */
Al Viro19a167a2011-01-17 01:35:23 -0500991 return 0;
David Howellsea5b7782011-01-14 19:10:03 +0000992 case 0:
Al Viro8aef1882011-06-16 15:10:06 +0100993 path_put(path);
David Howellsea5b7782011-01-14 19:10:03 +0000994 path->mnt = mnt;
995 path->dentry = dget(mnt->mnt_root);
David Howellsea5b7782011-01-14 19:10:03 +0000996 return 0;
Al Viro19a167a2011-01-17 01:35:23 -0500997 default:
998 return err;
David Howellsea5b7782011-01-14 19:10:03 +0000999 }
Al Viro19a167a2011-01-17 01:35:23 -05001000
David Howells9875cf82011-01-14 18:45:21 +00001001}
1002
1003/*
1004 * Handle a dentry that is managed in some way.
David Howellscc53ce52011-01-14 18:45:26 +00001005 * - Flagged for transit management (autofs)
David Howells9875cf82011-01-14 18:45:21 +00001006 * - Flagged as mountpoint
1007 * - Flagged as automount point
1008 *
1009 * This may only be called in refwalk mode.
1010 *
1011 * Serialization is taken care of in namespace.c
1012 */
1013static int follow_managed(struct path *path, unsigned flags)
1014{
Al Viro8aef1882011-06-16 15:10:06 +01001015 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
David Howells9875cf82011-01-14 18:45:21 +00001016 unsigned managed;
1017 bool need_mntput = false;
Al Viro8aef1882011-06-16 15:10:06 +01001018 int ret = 0;
David Howells9875cf82011-01-14 18:45:21 +00001019
1020 /* Given that we're not holding a lock here, we retain the value in a
1021 * local variable for each dentry as we look at it so that we don't see
1022 * the components of that value change under us */
1023 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1024 managed &= DCACHE_MANAGED_DENTRY,
1025 unlikely(managed != 0)) {
David Howellscc53ce52011-01-14 18:45:26 +00001026 /* Allow the filesystem to manage the transit without i_mutex
1027 * being held. */
1028 if (managed & DCACHE_MANAGE_TRANSIT) {
1029 BUG_ON(!path->dentry->d_op);
1030 BUG_ON(!path->dentry->d_op->d_manage);
Al Viro1aed3e42011-03-18 09:09:02 -04001031 ret = path->dentry->d_op->d_manage(path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +00001032 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001033 break;
David Howellscc53ce52011-01-14 18:45:26 +00001034 }
1035
David Howells9875cf82011-01-14 18:45:21 +00001036 /* Transit to a mounted filesystem. */
1037 if (managed & DCACHE_MOUNTED) {
1038 struct vfsmount *mounted = lookup_mnt(path);
1039 if (mounted) {
1040 dput(path->dentry);
1041 if (need_mntput)
1042 mntput(path->mnt);
1043 path->mnt = mounted;
1044 path->dentry = dget(mounted->mnt_root);
1045 need_mntput = true;
1046 continue;
1047 }
1048
1049 /* Something is mounted on this dentry in another
1050 * namespace and/or whatever was mounted there in this
1051 * namespace got unmounted before we managed to get the
1052 * vfsmount_lock */
1053 }
1054
1055 /* Handle an automount point */
1056 if (managed & DCACHE_NEED_AUTOMOUNT) {
1057 ret = follow_automount(path, flags, &need_mntput);
1058 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001059 break;
David Howells9875cf82011-01-14 18:45:21 +00001060 continue;
1061 }
1062
1063 /* We didn't change the current path point */
1064 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 }
Al Viro8aef1882011-06-16 15:10:06 +01001066
1067 if (need_mntput && path->mnt == mnt)
1068 mntput(path->mnt);
1069 if (ret == -EISDIR)
1070 ret = 0;
Al Viroa3fbbde2011-11-07 21:21:26 +00001071 return ret < 0 ? ret : need_mntput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072}
1073
David Howellscc53ce52011-01-14 18:45:26 +00001074int follow_down_one(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
1076 struct vfsmount *mounted;
1077
Al Viro1c755af2009-04-18 14:06:57 -04001078 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -04001080 dput(path->dentry);
1081 mntput(path->mnt);
1082 path->mnt = mounted;
1083 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 return 1;
1085 }
1086 return 0;
1087}
1088
Ian Kent62a73752011-03-25 01:51:02 +08001089static inline bool managed_dentry_might_block(struct dentry *dentry)
1090{
1091 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
1092 dentry->d_op->d_manage(dentry, true) < 0);
1093}
1094
David Howells9875cf82011-01-14 18:45:21 +00001095/*
Al Viro287548e2011-05-27 06:50:06 -04001096 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1097 * we meet a managed dentry that would need blocking.
David Howells9875cf82011-01-14 18:45:21 +00001098 */
1099static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
Al Viro287548e2011-05-27 06:50:06 -04001100 struct inode **inode)
David Howells9875cf82011-01-14 18:45:21 +00001101{
Ian Kent62a73752011-03-25 01:51:02 +08001102 for (;;) {
Al Viroc7105362011-11-24 18:22:03 -05001103 struct mount *mounted;
Ian Kent62a73752011-03-25 01:51:02 +08001104 /*
1105 * Don't forget we might have a non-mountpoint managed dentry
1106 * that wants to block transit.
1107 */
Al Viro287548e2011-05-27 06:50:06 -04001108 if (unlikely(managed_dentry_might_block(path->dentry)))
David Howellsab909112011-01-14 18:46:51 +00001109 return false;
Ian Kent62a73752011-03-25 01:51:02 +08001110
1111 if (!d_mountpoint(path->dentry))
1112 break;
1113
David Howells9875cf82011-01-14 18:45:21 +00001114 mounted = __lookup_mnt(path->mnt, path->dentry, 1);
1115 if (!mounted)
1116 break;
Al Viroc7105362011-11-24 18:22:03 -05001117 path->mnt = &mounted->mnt;
1118 path->dentry = mounted->mnt.mnt_root;
Al Viroa3fbbde2011-11-07 21:21:26 +00001119 nd->flags |= LOOKUP_JUMPED;
David Howells9875cf82011-01-14 18:45:21 +00001120 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
Linus Torvalds59430262011-07-18 15:43:29 -07001121 /*
1122 * Update the inode too. We don't need to re-check the
1123 * dentry sequence number here after this d_inode read,
1124 * because a mount-point is always pinned.
1125 */
1126 *inode = path->dentry->d_inode;
David Howells9875cf82011-01-14 18:45:21 +00001127 }
David Howells9875cf82011-01-14 18:45:21 +00001128 return true;
1129}
1130
Al Virodea39372011-05-27 06:53:39 -04001131static void follow_mount_rcu(struct nameidata *nd)
Al Viro287548e2011-05-27 06:50:06 -04001132{
Al Virodea39372011-05-27 06:53:39 -04001133 while (d_mountpoint(nd->path.dentry)) {
Al Viroc7105362011-11-24 18:22:03 -05001134 struct mount *mounted;
Al Virodea39372011-05-27 06:53:39 -04001135 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
Al Viro287548e2011-05-27 06:50:06 -04001136 if (!mounted)
1137 break;
Al Viroc7105362011-11-24 18:22:03 -05001138 nd->path.mnt = &mounted->mnt;
1139 nd->path.dentry = mounted->mnt.mnt_root;
Al Virodea39372011-05-27 06:53:39 -04001140 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro287548e2011-05-27 06:50:06 -04001141 }
1142}
1143
Nick Piggin31e6b012011-01-07 17:49:52 +11001144static int follow_dotdot_rcu(struct nameidata *nd)
1145{
Nick Piggin31e6b012011-01-07 17:49:52 +11001146 set_root_rcu(nd);
1147
David Howells9875cf82011-01-14 18:45:21 +00001148 while (1) {
Nick Piggin31e6b012011-01-07 17:49:52 +11001149 if (nd->path.dentry == nd->root.dentry &&
1150 nd->path.mnt == nd->root.mnt) {
1151 break;
1152 }
1153 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1154 struct dentry *old = nd->path.dentry;
1155 struct dentry *parent = old->d_parent;
1156 unsigned seq;
1157
1158 seq = read_seqcount_begin(&parent->d_seq);
1159 if (read_seqcount_retry(&old->d_seq, nd->seq))
Al Viroef7562d2011-03-04 14:35:59 -05001160 goto failed;
Nick Piggin31e6b012011-01-07 17:49:52 +11001161 nd->path.dentry = parent;
1162 nd->seq = seq;
1163 break;
1164 }
1165 if (!follow_up_rcu(&nd->path))
1166 break;
1167 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Nick Piggin31e6b012011-01-07 17:49:52 +11001168 }
Al Virodea39372011-05-27 06:53:39 -04001169 follow_mount_rcu(nd);
1170 nd->inode = nd->path.dentry->d_inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001171 return 0;
Al Viroef7562d2011-03-04 14:35:59 -05001172
1173failed:
1174 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001175 if (!(nd->flags & LOOKUP_ROOT))
1176 nd->root.mnt = NULL;
Al Viro32a79912012-07-18 20:43:19 +04001177 unlock_rcu_walk();
Al Viroef7562d2011-03-04 14:35:59 -05001178 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001179}
1180
David Howells9875cf82011-01-14 18:45:21 +00001181/*
David Howellscc53ce52011-01-14 18:45:26 +00001182 * Follow down to the covering mount currently visible to userspace. At each
1183 * point, the filesystem owning that dentry may be queried as to whether the
1184 * caller is permitted to proceed or not.
David Howellscc53ce52011-01-14 18:45:26 +00001185 */
Al Viro7cc90cc2011-03-18 09:04:20 -04001186int follow_down(struct path *path)
David Howellscc53ce52011-01-14 18:45:26 +00001187{
1188 unsigned managed;
1189 int ret;
1190
1191 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1192 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1193 /* Allow the filesystem to manage the transit without i_mutex
1194 * being held.
1195 *
1196 * We indicate to the filesystem if someone is trying to mount
1197 * something here. This gives autofs the chance to deny anyone
1198 * other than its daemon the right to mount on its
1199 * superstructure.
1200 *
1201 * The filesystem may sleep at this point.
1202 */
1203 if (managed & DCACHE_MANAGE_TRANSIT) {
1204 BUG_ON(!path->dentry->d_op);
1205 BUG_ON(!path->dentry->d_op->d_manage);
David Howellsab909112011-01-14 18:46:51 +00001206 ret = path->dentry->d_op->d_manage(
Al Viro1aed3e42011-03-18 09:09:02 -04001207 path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +00001208 if (ret < 0)
1209 return ret == -EISDIR ? 0 : ret;
1210 }
1211
1212 /* Transit to a mounted filesystem. */
1213 if (managed & DCACHE_MOUNTED) {
1214 struct vfsmount *mounted = lookup_mnt(path);
1215 if (!mounted)
1216 break;
1217 dput(path->dentry);
1218 mntput(path->mnt);
1219 path->mnt = mounted;
1220 path->dentry = dget(mounted->mnt_root);
1221 continue;
1222 }
1223
1224 /* Don't handle automount points here */
1225 break;
1226 }
1227 return 0;
1228}
1229
1230/*
David Howells9875cf82011-01-14 18:45:21 +00001231 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1232 */
1233static void follow_mount(struct path *path)
1234{
1235 while (d_mountpoint(path->dentry)) {
1236 struct vfsmount *mounted = lookup_mnt(path);
1237 if (!mounted)
1238 break;
1239 dput(path->dentry);
1240 mntput(path->mnt);
1241 path->mnt = mounted;
1242 path->dentry = dget(mounted->mnt_root);
1243 }
1244}
1245
Nick Piggin31e6b012011-01-07 17:49:52 +11001246static void follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
Al Viro2a737872009-04-07 11:49:53 -04001248 set_root(nd);
Andreas Mohre518ddb2006-09-29 02:01:22 -07001249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 while(1) {
Jan Blunck4ac91372008-02-14 19:34:32 -08001251 struct dentry *old = nd->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Al Viro2a737872009-04-07 11:49:53 -04001253 if (nd->path.dentry == nd->root.dentry &&
1254 nd->path.mnt == nd->root.mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 break;
1256 }
Jan Blunck4ac91372008-02-14 19:34:32 -08001257 if (nd->path.dentry != nd->path.mnt->mnt_root) {
Al Viro3088dd72010-01-30 15:47:29 -05001258 /* rare case of legitimate dget_parent()... */
1259 nd->path.dentry = dget_parent(nd->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 dput(old);
1261 break;
1262 }
Al Viro3088dd72010-01-30 15:47:29 -05001263 if (!follow_up(&nd->path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
Al Viro79ed0222009-04-18 13:59:41 -04001266 follow_mount(&nd->path);
Nick Piggin31e6b012011-01-07 17:49:52 +11001267 nd->inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268}
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270/*
Miklos Szeredibad61182012-03-26 12:54:24 +02001271 * This looks up the name in dcache, possibly revalidates the old dentry and
1272 * allocates a new one if not found or not valid. In the need_lookup argument
1273 * returns whether i_op->lookup is necessary.
1274 *
1275 * dir->d_inode->i_mutex must be held
Nick Pigginbaa03892010-08-18 04:37:31 +10001276 */
Miklos Szeredibad61182012-03-26 12:54:24 +02001277static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
Al Viro201f9562012-06-22 12:42:10 +04001278 unsigned int flags, bool *need_lookup)
Nick Pigginbaa03892010-08-18 04:37:31 +10001279{
Nick Pigginbaa03892010-08-18 04:37:31 +10001280 struct dentry *dentry;
Miklos Szeredibad61182012-03-26 12:54:24 +02001281 int error;
Nick Pigginbaa03892010-08-18 04:37:31 +10001282
Miklos Szeredibad61182012-03-26 12:54:24 +02001283 *need_lookup = false;
1284 dentry = d_lookup(dir, name);
1285 if (dentry) {
Jeff Layton39e3c952012-11-28 11:30:53 -05001286 if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
Al Viro201f9562012-06-22 12:42:10 +04001287 error = d_revalidate(dentry, flags);
Miklos Szeredibad61182012-03-26 12:54:24 +02001288 if (unlikely(error <= 0)) {
1289 if (error < 0) {
1290 dput(dentry);
1291 return ERR_PTR(error);
1292 } else if (!d_invalidate(dentry)) {
1293 dput(dentry);
1294 dentry = NULL;
1295 }
1296 }
1297 }
1298 }
Nick Pigginbaa03892010-08-18 04:37:31 +10001299
Miklos Szeredibad61182012-03-26 12:54:24 +02001300 if (!dentry) {
1301 dentry = d_alloc(dir, name);
1302 if (unlikely(!dentry))
1303 return ERR_PTR(-ENOMEM);
Nick Pigginbaa03892010-08-18 04:37:31 +10001304
Miklos Szeredibad61182012-03-26 12:54:24 +02001305 *need_lookup = true;
Nick Pigginbaa03892010-08-18 04:37:31 +10001306 }
1307 return dentry;
1308}
1309
1310/*
Miklos Szeredibad61182012-03-26 12:54:24 +02001311 * Call i_op->lookup on the dentry. The dentry must be negative but may be
1312 * hashed if it was pouplated with DCACHE_NEED_LOOKUP.
1313 *
1314 * dir->d_inode->i_mutex must be held
Josef Bacik44396f42011-05-31 11:58:49 -04001315 */
Miklos Szeredibad61182012-03-26 12:54:24 +02001316static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
Al Viro72bd8662012-06-10 17:17:17 -04001317 unsigned int flags)
Josef Bacik44396f42011-05-31 11:58:49 -04001318{
Josef Bacik44396f42011-05-31 11:58:49 -04001319 struct dentry *old;
1320
1321 /* Don't create child dentry for a dead directory. */
Miklos Szeredibad61182012-03-26 12:54:24 +02001322 if (unlikely(IS_DEADDIR(dir))) {
Miklos Szeredie188dc02012-02-03 14:25:18 +01001323 dput(dentry);
Josef Bacik44396f42011-05-31 11:58:49 -04001324 return ERR_PTR(-ENOENT);
Miklos Szeredie188dc02012-02-03 14:25:18 +01001325 }
Josef Bacik44396f42011-05-31 11:58:49 -04001326
Al Viro72bd8662012-06-10 17:17:17 -04001327 old = dir->i_op->lookup(dir, dentry, flags);
Josef Bacik44396f42011-05-31 11:58:49 -04001328 if (unlikely(old)) {
1329 dput(dentry);
1330 dentry = old;
1331 }
1332 return dentry;
1333}
1334
Al Viroa3255542012-03-30 14:41:51 -04001335static struct dentry *__lookup_hash(struct qstr *name,
Al Viro72bd8662012-06-10 17:17:17 -04001336 struct dentry *base, unsigned int flags)
Al Viroa3255542012-03-30 14:41:51 -04001337{
Miklos Szeredibad61182012-03-26 12:54:24 +02001338 bool need_lookup;
Al Viroa3255542012-03-30 14:41:51 -04001339 struct dentry *dentry;
1340
Al Viro72bd8662012-06-10 17:17:17 -04001341 dentry = lookup_dcache(name, base, flags, &need_lookup);
Miklos Szeredibad61182012-03-26 12:54:24 +02001342 if (!need_lookup)
1343 return dentry;
Al Viroa3255542012-03-30 14:41:51 -04001344
Al Viro72bd8662012-06-10 17:17:17 -04001345 return lookup_real(base->d_inode, dentry, flags);
Al Viroa3255542012-03-30 14:41:51 -04001346}
1347
Josef Bacik44396f42011-05-31 11:58:49 -04001348/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 * It's more convoluted than I'd like it to be, but... it's still fairly
1350 * small and for now I'd prefer to have fast path as straight as possible.
1351 * It _is_ time-critical.
1352 */
Al Viroe97cdc82013-01-24 18:16:00 -05001353static int lookup_fast(struct nameidata *nd,
Miklos Szeredi697f5142012-05-21 17:30:05 +02001354 struct path *path, struct inode **inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355{
Jan Blunck4ac91372008-02-14 19:34:32 -08001356 struct vfsmount *mnt = nd->path.mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +11001357 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro5a18fff2011-03-11 04:44:53 -05001358 int need_reval = 1;
1359 int status = 1;
David Howells9875cf82011-01-14 18:45:21 +00001360 int err;
1361
Al Viro3cac2602009-08-13 18:27:43 +04001362 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001363 * Rename seqlock is not required here because in the off chance
1364 * of a false negative due to a concurrent rename, we're going to
1365 * do the non-racy lookup, below.
1366 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001367 if (nd->flags & LOOKUP_RCU) {
1368 unsigned seq;
Linus Torvaldsda53be12013-05-21 15:22:44 -07001369 dentry = __d_lookup_rcu(parent, &nd->last, &seq);
Al Viro5a18fff2011-03-11 04:44:53 -05001370 if (!dentry)
1371 goto unlazy;
1372
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001373 /*
1374 * This sequence count validates that the inode matches
1375 * the dentry name information from lookup.
1376 */
1377 *inode = dentry->d_inode;
1378 if (read_seqcount_retry(&dentry->d_seq, seq))
1379 return -ECHILD;
1380
1381 /*
1382 * This sequence count validates that the parent had no
1383 * changes while we did the lookup of the dentry above.
1384 *
1385 * The memory barrier in read_seqcount_begin of child is
1386 * enough, we can use __read_seqcount_retry here.
1387 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001388 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1389 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001390 nd->seq = seq;
Al Viro5a18fff2011-03-11 04:44:53 -05001391
Al Viro24643082011-02-15 01:26:22 -05001392 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
Al Viro4ce16ef32012-06-10 16:10:59 -04001393 status = d_revalidate(dentry, nd->flags);
Al Viro5a18fff2011-03-11 04:44:53 -05001394 if (unlikely(status <= 0)) {
1395 if (status != -ECHILD)
1396 need_reval = 0;
1397 goto unlazy;
1398 }
Al Viro24643082011-02-15 01:26:22 -05001399 }
Nick Piggin31e6b012011-01-07 17:49:52 +11001400 path->mnt = mnt;
1401 path->dentry = dentry;
Al Virod6e9bd22011-05-27 07:03:15 -04001402 if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1403 goto unlazy;
1404 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1405 goto unlazy;
1406 return 0;
Al Viro5a18fff2011-03-11 04:44:53 -05001407unlazy:
Al Viro19660af2011-03-25 10:32:48 -04001408 if (unlazy_walk(nd, dentry))
1409 return -ECHILD;
Al Viro5a18fff2011-03-11 04:44:53 -05001410 } else {
Al Viroe97cdc82013-01-24 18:16:00 -05001411 dentry = __d_lookup(parent, &nd->last);
Nick Piggin31e6b012011-01-07 17:49:52 +11001412 }
Al Viro5a18fff2011-03-11 04:44:53 -05001413
Al Viro81e6f522012-03-30 14:48:04 -04001414 if (unlikely(!dentry))
1415 goto need_lookup;
Al Viro5a18fff2011-03-11 04:44:53 -05001416
Al Viro5a18fff2011-03-11 04:44:53 -05001417 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
Al Viro4ce16ef32012-06-10 16:10:59 -04001418 status = d_revalidate(dentry, nd->flags);
Al Viro5a18fff2011-03-11 04:44:53 -05001419 if (unlikely(status <= 0)) {
1420 if (status < 0) {
1421 dput(dentry);
1422 return status;
1423 }
1424 if (!d_invalidate(dentry)) {
1425 dput(dentry);
Al Viro81e6f522012-03-30 14:48:04 -04001426 goto need_lookup;
Al Viro5a18fff2011-03-11 04:44:53 -05001427 }
1428 }
Miklos Szeredi697f5142012-05-21 17:30:05 +02001429
David Howells9875cf82011-01-14 18:45:21 +00001430 path->mnt = mnt;
1431 path->dentry = dentry;
1432 err = follow_managed(path, nd->flags);
Ian Kent89312212011-01-18 12:06:10 +08001433 if (unlikely(err < 0)) {
1434 path_put_conditional(path, nd);
David Howells9875cf82011-01-14 18:45:21 +00001435 return err;
Ian Kent89312212011-01-18 12:06:10 +08001436 }
Al Viroa3fbbde2011-11-07 21:21:26 +00001437 if (err)
1438 nd->flags |= LOOKUP_JUMPED;
David Howells9875cf82011-01-14 18:45:21 +00001439 *inode = path->dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 return 0;
Al Viro81e6f522012-03-30 14:48:04 -04001441
1442need_lookup:
Miklos Szeredi697f5142012-05-21 17:30:05 +02001443 return 1;
1444}
1445
1446/* Fast lookup failed, do it the slow way */
Al Virocc2a5272013-01-24 18:19:49 -05001447static int lookup_slow(struct nameidata *nd, struct path *path)
Miklos Szeredi697f5142012-05-21 17:30:05 +02001448{
1449 struct dentry *dentry, *parent;
1450 int err;
1451
1452 parent = nd->path.dentry;
Al Viro81e6f522012-03-30 14:48:04 -04001453 BUG_ON(nd->inode != parent->d_inode);
1454
1455 mutex_lock(&parent->d_inode->i_mutex);
Al Virocc2a5272013-01-24 18:19:49 -05001456 dentry = __lookup_hash(&nd->last, parent, nd->flags);
Al Viro81e6f522012-03-30 14:48:04 -04001457 mutex_unlock(&parent->d_inode->i_mutex);
1458 if (IS_ERR(dentry))
1459 return PTR_ERR(dentry);
Miklos Szeredi697f5142012-05-21 17:30:05 +02001460 path->mnt = nd->path.mnt;
1461 path->dentry = dentry;
1462 err = follow_managed(path, nd->flags);
1463 if (unlikely(err < 0)) {
1464 path_put_conditional(path, nd);
1465 return err;
1466 }
1467 if (err)
1468 nd->flags |= LOOKUP_JUMPED;
1469 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470}
1471
Al Viro52094c82011-02-21 21:34:47 -05001472static inline int may_lookup(struct nameidata *nd)
1473{
1474 if (nd->flags & LOOKUP_RCU) {
Al Viro4ad5abb2011-06-20 19:57:03 -04001475 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
Al Viro52094c82011-02-21 21:34:47 -05001476 if (err != -ECHILD)
1477 return err;
Al Viro19660af2011-03-25 10:32:48 -04001478 if (unlazy_walk(nd, NULL))
Al Viro52094c82011-02-21 21:34:47 -05001479 return -ECHILD;
1480 }
Al Viro4ad5abb2011-06-20 19:57:03 -04001481 return inode_permission(nd->inode, MAY_EXEC);
Al Viro52094c82011-02-21 21:34:47 -05001482}
1483
Al Viro9856fa12011-03-04 14:22:06 -05001484static inline int handle_dots(struct nameidata *nd, int type)
1485{
1486 if (type == LAST_DOTDOT) {
1487 if (nd->flags & LOOKUP_RCU) {
1488 if (follow_dotdot_rcu(nd))
1489 return -ECHILD;
1490 } else
1491 follow_dotdot(nd);
1492 }
1493 return 0;
1494}
1495
Al Viro951361f2011-03-04 14:44:37 -05001496static void terminate_walk(struct nameidata *nd)
1497{
1498 if (!(nd->flags & LOOKUP_RCU)) {
1499 path_put(&nd->path);
1500 } else {
1501 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001502 if (!(nd->flags & LOOKUP_ROOT))
1503 nd->root.mnt = NULL;
Al Viro32a79912012-07-18 20:43:19 +04001504 unlock_rcu_walk();
Al Viro951361f2011-03-04 14:44:37 -05001505 }
1506}
1507
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001508/*
1509 * Do we need to follow links? We _really_ want to be able
1510 * to do this check without having to look at inode->i_op,
1511 * so we keep a cache of "no, this doesn't need follow_link"
1512 * for the common case.
1513 */
Linus Torvalds7813b942011-08-07 09:53:20 -07001514static inline int should_follow_link(struct inode *inode, int follow)
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001515{
1516 if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
1517 if (likely(inode->i_op->follow_link))
1518 return follow;
1519
1520 /* This gets set once for the inode lifetime */
1521 spin_lock(&inode->i_lock);
1522 inode->i_opflags |= IOP_NOFOLLOW;
1523 spin_unlock(&inode->i_lock);
1524 }
1525 return 0;
1526}
1527
Al Viroce57dfc2011-03-13 19:58:58 -04001528static inline int walk_component(struct nameidata *nd, struct path *path,
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;
1552 if (!inode)
1553 goto out_path_put;
1554
Linus Torvalds7813b942011-08-07 09:53:20 -07001555 if (should_follow_link(inode, follow)) {
Al Viro19660af2011-03-25 10:32:48 -04001556 if (nd->flags & LOOKUP_RCU) {
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 Torvalds3ddcd052011-08-06 22:45:50 -07001614 * We really don't want to look at inode->i_op->lookup
1615 * when we don't have to. So we keep a cache bit in
1616 * the inode ->i_opflags field that says "yes, we can
1617 * do lookup on this inode".
1618 */
1619static inline int can_lookup(struct inode *inode)
1620{
1621 if (likely(inode->i_opflags & IOP_LOOKUP))
1622 return 1;
1623 if (likely(!inode->i_op->lookup))
1624 return 0;
1625
1626 /* We do this once for the lifetime of the inode */
1627 spin_lock(&inode->i_lock);
1628 inode->i_opflags |= IOP_LOOKUP;
1629 spin_unlock(&inode->i_lock);
1630 return 1;
1631}
1632
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001633/*
1634 * We can do the critical dentry name comparison and hashing
1635 * operations one word at a time, but we are limited to:
1636 *
1637 * - Architectures with fast unaligned word accesses. We could
1638 * do a "get_unaligned()" if this helps and is sufficiently
1639 * fast.
1640 *
1641 * - Little-endian machines (so that we can generate the mask
1642 * of low bytes efficiently). Again, we *could* do a byte
1643 * swapping load on big-endian architectures if that is not
1644 * expensive enough to make the optimization worthless.
1645 *
1646 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1647 * do not trap on the (extremely unlikely) case of a page
1648 * crossing operation.
1649 *
1650 * - Furthermore, we need an efficient 64-bit compile for the
1651 * 64-bit case in order to generate the "number of bytes in
1652 * the final mask". Again, that could be replaced with a
1653 * efficient population count instruction or similar.
1654 */
1655#ifdef CONFIG_DCACHE_WORD_ACCESS
1656
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001657#include <asm/word-at-a-time.h>
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001658
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001659#ifdef CONFIG_64BIT
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001660
1661static inline unsigned int fold_hash(unsigned long hash)
1662{
1663 hash += hash >> (8*sizeof(int));
1664 return hash;
1665}
1666
1667#else /* 32-bit case */
1668
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001669#define fold_hash(x) (x)
1670
1671#endif
1672
1673unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1674{
1675 unsigned long a, mask;
1676 unsigned long hash = 0;
1677
1678 for (;;) {
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001679 a = load_unaligned_zeropad(name);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001680 if (len < sizeof(unsigned long))
1681 break;
1682 hash += a;
Al Virof132c5b2012-03-22 21:59:52 +00001683 hash *= 9;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001684 name += sizeof(unsigned long);
1685 len -= sizeof(unsigned long);
1686 if (!len)
1687 goto done;
1688 }
1689 mask = ~(~0ul << len*8);
1690 hash += mask & a;
1691done:
1692 return fold_hash(hash);
1693}
1694EXPORT_SYMBOL(full_name_hash);
1695
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001696/*
1697 * Calculate the length and hash of the path component, and
1698 * return the length of the component;
1699 */
1700static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1701{
Linus Torvalds36126f82012-05-26 10:43:17 -07001702 unsigned long a, b, adata, bdata, mask, hash, len;
1703 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001704
1705 hash = a = 0;
1706 len = -sizeof(unsigned long);
1707 do {
1708 hash = (hash + a) * 9;
1709 len += sizeof(unsigned long);
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001710 a = load_unaligned_zeropad(name+len);
Linus Torvalds36126f82012-05-26 10:43:17 -07001711 b = a ^ REPEAT_BYTE('/');
1712 } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001713
Linus Torvalds36126f82012-05-26 10:43:17 -07001714 adata = prep_zero_mask(a, adata, &constants);
1715 bdata = prep_zero_mask(b, bdata, &constants);
1716
1717 mask = create_zero_mask(adata | bdata);
1718
1719 hash += a & zero_bytemask(mask);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001720 *hashp = fold_hash(hash);
1721
Linus Torvalds36126f82012-05-26 10:43:17 -07001722 return len + find_zero(mask);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001723}
1724
1725#else
1726
Linus Torvalds0145acc2012-03-02 14:32:59 -08001727unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1728{
1729 unsigned long hash = init_name_hash();
1730 while (len--)
1731 hash = partial_name_hash(*name++, hash);
1732 return end_name_hash(hash);
1733}
Linus Torvaldsae942ae2012-03-02 19:40:57 -08001734EXPORT_SYMBOL(full_name_hash);
Linus Torvalds0145acc2012-03-02 14:32:59 -08001735
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001736/*
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001737 * We know there's a real path component here of at least
1738 * one character.
1739 */
1740static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1741{
1742 unsigned long hash = init_name_hash();
1743 unsigned long len = 0, c;
1744
1745 c = (unsigned char)*name;
1746 do {
1747 len++;
1748 hash = partial_name_hash(c, hash);
1749 c = (unsigned char)name[len];
1750 } while (c && c != '/');
1751 *hashp = end_name_hash(hash);
1752 return len;
1753}
1754
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001755#endif
1756
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001757/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01001759 * This is the basic name resolution function, turning a pathname into
1760 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01001762 * Returns 0 and nd will have valid dentry and mnt on success.
1763 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 */
Al Viro6de88d72009-08-09 01:41:57 +04001765static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766{
1767 struct path next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
1770 while (*name=='/')
1771 name++;
1772 if (!*name)
Al Viro086e1832011-02-22 20:56:27 -05001773 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 /* At this point we know we have a real path component. */
1776 for(;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 struct qstr this;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001778 long len;
Al Virofe479a52011-02-22 15:10:03 -05001779 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
Al Viro52094c82011-02-21 21:34:47 -05001781 err = may_lookup(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 if (err)
1783 break;
1784
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001785 len = hash_name(name, &this.hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 this.name = name;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001787 this.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Al Virofe479a52011-02-22 15:10:03 -05001789 type = LAST_NORM;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001790 if (name[0] == '.') switch (len) {
Al Virofe479a52011-02-22 15:10:03 -05001791 case 2:
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001792 if (name[1] == '.') {
Al Virofe479a52011-02-22 15:10:03 -05001793 type = LAST_DOTDOT;
Al Viro16c2cd72011-02-22 15:50:10 -05001794 nd->flags |= LOOKUP_JUMPED;
1795 }
Al Virofe479a52011-02-22 15:10:03 -05001796 break;
1797 case 1:
1798 type = LAST_DOT;
1799 }
Al Viro5a202bc2011-03-08 14:17:44 -05001800 if (likely(type == LAST_NORM)) {
1801 struct dentry *parent = nd->path.dentry;
Al Viro16c2cd72011-02-22 15:50:10 -05001802 nd->flags &= ~LOOKUP_JUMPED;
Al Viro5a202bc2011-03-08 14:17:44 -05001803 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
Linus Torvaldsda53be12013-05-21 15:22:44 -07001804 err = parent->d_op->d_hash(parent, &this);
Al Viro5a202bc2011-03-08 14:17:44 -05001805 if (err < 0)
1806 break;
1807 }
1808 }
Al Virofe479a52011-02-22 15:10:03 -05001809
Al Viro5f4a6a62013-01-24 18:04:22 -05001810 nd->last = this;
1811 nd->last_type = type;
1812
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001813 if (!name[len])
Al Viro5f4a6a62013-01-24 18:04:22 -05001814 return 0;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001815 /*
1816 * If it wasn't NUL, we know it was '/'. Skip that
1817 * slash, and continue until no more slashes.
1818 */
1819 do {
1820 len++;
1821 } while (unlikely(name[len] == '/'));
1822 if (!name[len])
Al Viro5f4a6a62013-01-24 18:04:22 -05001823 return 0;
1824
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001825 name += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Al Viro21b9b072013-01-24 18:10:25 -05001827 err = walk_component(nd, &next, LOOKUP_FOLLOW);
Al Viroce57dfc2011-03-13 19:58:58 -04001828 if (err < 0)
1829 return err;
Al Virofe479a52011-02-22 15:10:03 -05001830
Al Viroce57dfc2011-03-13 19:58:58 -04001831 if (err) {
Al Virob3563792011-03-14 21:54:55 -04001832 err = nested_symlink(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 if (err)
Al Viroa7472ba2011-03-04 14:39:30 -05001834 return err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001835 }
Al Viro5f4a6a62013-01-24 18:04:22 -05001836 if (!can_lookup(nd->inode)) {
1837 err = -ENOTDIR;
1838 break;
1839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 }
Al Viro951361f2011-03-04 14:44:37 -05001841 terminate_walk(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 return err;
1843}
1844
Al Viro70e9b352011-03-05 21:12:22 -05001845static int path_init(int dfd, const char *name, unsigned int flags,
1846 struct nameidata *nd, struct file **fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847{
Prasanna Medaea3834d2005-04-29 16:00:17 +01001848 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
1850 nd->last_type = LAST_ROOT; /* if there are only slashes... */
Al Viro16c2cd72011-02-22 15:50:10 -05001851 nd->flags = flags | LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 nd->depth = 0;
Al Viro5b6ca022011-03-09 23:04:47 -05001853 if (flags & LOOKUP_ROOT) {
1854 struct inode *inode = nd->root.dentry->d_inode;
Al Viro73d049a2011-03-11 12:08:24 -05001855 if (*name) {
Al Viro741b7c32012-12-20 13:41:28 -05001856 if (!can_lookup(inode))
Al Viro73d049a2011-03-11 12:08:24 -05001857 return -ENOTDIR;
1858 retval = inode_permission(inode, MAY_EXEC);
1859 if (retval)
1860 return retval;
1861 }
Al Viro5b6ca022011-03-09 23:04:47 -05001862 nd->path = nd->root;
1863 nd->inode = inode;
1864 if (flags & LOOKUP_RCU) {
Al Viro32a79912012-07-18 20:43:19 +04001865 lock_rcu_walk();
Al Viro5b6ca022011-03-09 23:04:47 -05001866 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1867 } else {
1868 path_get(&nd->path);
1869 }
1870 return 0;
1871 }
1872
Al Viro2a737872009-04-07 11:49:53 -04001873 nd->root.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 if (*name=='/') {
Al Viroe41f7d42011-02-22 14:02:58 -05001876 if (flags & LOOKUP_RCU) {
Al Viro32a79912012-07-18 20:43:19 +04001877 lock_rcu_walk();
Al Viroe41f7d42011-02-22 14:02:58 -05001878 set_root_rcu(nd);
1879 } else {
1880 set_root(nd);
1881 path_get(&nd->root);
1882 }
Al Viro2a737872009-04-07 11:49:53 -04001883 nd->path = nd->root;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001884 } else if (dfd == AT_FDCWD) {
Al Viroe41f7d42011-02-22 14:02:58 -05001885 if (flags & LOOKUP_RCU) {
1886 struct fs_struct *fs = current->fs;
1887 unsigned seq;
1888
Al Viro32a79912012-07-18 20:43:19 +04001889 lock_rcu_walk();
Al Viroe41f7d42011-02-22 14:02:58 -05001890
1891 do {
1892 seq = read_seqcount_begin(&fs->seq);
1893 nd->path = fs->pwd;
1894 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1895 } while (read_seqcount_retry(&fs->seq, seq));
1896 } else {
1897 get_fs_pwd(current->fs, &nd->path);
1898 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001899 } else {
Jeff Layton582aa642012-12-11 08:56:16 -05001900 /* Caller must check execute permissions on the starting path component */
Al Viro2903ff02012-08-28 12:52:22 -04001901 struct fd f = fdget_raw(dfd);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001902 struct dentry *dentry;
1903
Al Viro2903ff02012-08-28 12:52:22 -04001904 if (!f.file)
1905 return -EBADF;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001906
Al Viro2903ff02012-08-28 12:52:22 -04001907 dentry = f.file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001908
Al Virof52e0c12011-03-14 18:56:51 -04001909 if (*name) {
Al Viro741b7c32012-12-20 13:41:28 -05001910 if (!can_lookup(dentry->d_inode)) {
Al Viro2903ff02012-08-28 12:52:22 -04001911 fdput(f);
1912 return -ENOTDIR;
1913 }
Al Virof52e0c12011-03-14 18:56:51 -04001914 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001915
Al Viro2903ff02012-08-28 12:52:22 -04001916 nd->path = f.file->f_path;
Al Viroe41f7d42011-02-22 14:02:58 -05001917 if (flags & LOOKUP_RCU) {
Al Viro2903ff02012-08-28 12:52:22 -04001918 if (f.need_put)
1919 *fp = f.file;
Al Viroe41f7d42011-02-22 14:02:58 -05001920 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro32a79912012-07-18 20:43:19 +04001921 lock_rcu_walk();
Al Viroe41f7d42011-02-22 14:02:58 -05001922 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001923 path_get(&nd->path);
1924 fdput(f);
Al Viroe41f7d42011-02-22 14:02:58 -05001925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 }
Al Viroe41f7d42011-02-22 14:02:58 -05001927
Nick Piggin31e6b012011-01-07 17:49:52 +11001928 nd->inode = nd->path.dentry->d_inode;
Al Viro9b4a9b12009-04-07 11:44:16 -04001929 return 0;
Al Viro9b4a9b12009-04-07 11:44:16 -04001930}
1931
Al Virobd92d7f2011-03-14 19:54:59 -04001932static inline int lookup_last(struct nameidata *nd, struct path *path)
1933{
1934 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1935 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1936
1937 nd->flags &= ~LOOKUP_PARENT;
Al Viro21b9b072013-01-24 18:10:25 -05001938 return walk_component(nd, path, nd->flags & LOOKUP_FOLLOW);
Al Virobd92d7f2011-03-14 19:54:59 -04001939}
1940
Al Viro9b4a9b12009-04-07 11:44:16 -04001941/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroee0827c2011-02-21 23:38:09 -05001942static int path_lookupat(int dfd, const char *name,
Al Viro9b4a9b12009-04-07 11:44:16 -04001943 unsigned int flags, struct nameidata *nd)
1944{
Al Viro70e9b352011-03-05 21:12:22 -05001945 struct file *base = NULL;
Al Virobd92d7f2011-03-14 19:54:59 -04001946 struct path path;
1947 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001948
1949 /*
1950 * Path walking is largely split up into 2 different synchronisation
1951 * schemes, rcu-walk and ref-walk (explained in
1952 * Documentation/filesystems/path-lookup.txt). These share much of the
1953 * path walk code, but some things particularly setup, cleanup, and
1954 * following mounts are sufficiently divergent that functions are
1955 * duplicated. Typically there is a function foo(), and its RCU
1956 * analogue, foo_rcu().
1957 *
1958 * -ECHILD is the error number of choice (just to avoid clashes) that
1959 * is returned if some aspect of an rcu-walk fails. Such an error must
1960 * be handled by restarting a traditional ref-walk (which will always
1961 * be able to complete).
1962 */
Al Virobd92d7f2011-03-14 19:54:59 -04001963 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
Al Viroee0827c2011-02-21 23:38:09 -05001964
Al Virobd92d7f2011-03-14 19:54:59 -04001965 if (unlikely(err))
1966 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001967
1968 current->total_link_count = 0;
Al Virobd92d7f2011-03-14 19:54:59 -04001969 err = link_path_walk(name, nd);
1970
1971 if (!err && !(flags & LOOKUP_PARENT)) {
Al Virobd92d7f2011-03-14 19:54:59 -04001972 err = lookup_last(nd, &path);
1973 while (err > 0) {
1974 void *cookie;
1975 struct path link = path;
Kees Cook800179c2012-07-25 17:29:07 -07001976 err = may_follow_link(&link, nd);
1977 if (unlikely(err))
1978 break;
Al Virobd92d7f2011-03-14 19:54:59 -04001979 nd->flags |= LOOKUP_PARENT;
Al Viro574197e2011-03-14 22:20:34 -04001980 err = follow_link(&link, nd, &cookie);
Al Viro6d7b5aa2012-06-10 04:15:17 -04001981 if (err)
1982 break;
1983 err = lookup_last(nd, &path);
Al Viro574197e2011-03-14 22:20:34 -04001984 put_link(nd, &link, cookie);
Al Virobd92d7f2011-03-14 19:54:59 -04001985 }
1986 }
Al Viroee0827c2011-02-21 23:38:09 -05001987
Al Viro9f1fafe2011-03-25 11:00:12 -04001988 if (!err)
1989 err = complete_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04001990
1991 if (!err && nd->flags & LOOKUP_DIRECTORY) {
Al Viro05252902013-06-06 19:33:47 -04001992 if (!can_lookup(nd->inode)) {
Al Virobd92d7f2011-03-14 19:54:59 -04001993 path_put(&nd->path);
Al Virobd23a532011-03-23 09:56:30 -04001994 err = -ENOTDIR;
Al Virobd92d7f2011-03-14 19:54:59 -04001995 }
1996 }
Al Viro16c2cd72011-02-22 15:50:10 -05001997
Al Viro70e9b352011-03-05 21:12:22 -05001998 if (base)
1999 fput(base);
Al Viroee0827c2011-02-21 23:38:09 -05002000
Al Viro5b6ca022011-03-09 23:04:47 -05002001 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
Al Viro2a737872009-04-07 11:49:53 -04002002 path_put(&nd->root);
2003 nd->root.mnt = NULL;
2004 }
Al Virobd92d7f2011-03-14 19:54:59 -04002005 return err;
Al Viroee0827c2011-02-21 23:38:09 -05002006}
Nick Piggin31e6b012011-01-07 17:49:52 +11002007
Jeff Layton873f1ee2012-10-10 15:25:29 -04002008static int filename_lookup(int dfd, struct filename *name,
2009 unsigned int flags, struct nameidata *nd)
2010{
2011 int retval = path_lookupat(dfd, name->name, flags | LOOKUP_RCU, nd);
2012 if (unlikely(retval == -ECHILD))
2013 retval = path_lookupat(dfd, name->name, flags, nd);
2014 if (unlikely(retval == -ESTALE))
2015 retval = path_lookupat(dfd, name->name,
2016 flags | LOOKUP_REVAL, nd);
2017
2018 if (likely(!retval))
Jeff Laytonadb5c242012-10-10 16:43:13 -04002019 audit_inode(name, nd->path.dentry, flags & LOOKUP_PARENT);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002020 return retval;
2021}
2022
Al Viroee0827c2011-02-21 23:38:09 -05002023static int do_path_lookup(int dfd, const char *name,
2024 unsigned int flags, struct nameidata *nd)
2025{
Jeff Layton873f1ee2012-10-10 15:25:29 -04002026 struct filename filename = { .name = name };
Nick Piggin31e6b012011-01-07 17:49:52 +11002027
Jeff Layton873f1ee2012-10-10 15:25:29 -04002028 return filename_lookup(dfd, &filename, flags, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029}
2030
Al Viro79714f72012-06-15 03:01:42 +04002031/* does lookup, returns the object with parent locked */
2032struct dentry *kern_path_locked(const char *name, struct path *path)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002033{
Al Viro79714f72012-06-15 03:01:42 +04002034 struct nameidata nd;
2035 struct dentry *d;
2036 int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd);
2037 if (err)
2038 return ERR_PTR(err);
2039 if (nd.last_type != LAST_NORM) {
2040 path_put(&nd.path);
2041 return ERR_PTR(-EINVAL);
2042 }
2043 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Al Viro1e0ea002012-07-22 23:46:21 +04002044 d = __lookup_hash(&nd.last, nd.path.dentry, 0);
Al Viro79714f72012-06-15 03:01:42 +04002045 if (IS_ERR(d)) {
2046 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2047 path_put(&nd.path);
2048 return d;
2049 }
2050 *path = nd.path;
2051 return d;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002052}
2053
Al Virod1811462008-08-02 00:49:18 -04002054int kern_path(const char *name, unsigned int flags, struct path *path)
2055{
2056 struct nameidata nd;
2057 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
2058 if (!res)
2059 *path = nd.path;
2060 return res;
2061}
2062
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002063/**
2064 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2065 * @dentry: pointer to dentry of the base directory
2066 * @mnt: pointer to vfs mount of the base directory
2067 * @name: pointer to file name
2068 * @flags: lookup flags
Al Viroe0a01242011-06-27 17:00:37 -04002069 * @path: pointer to struct path to fill
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002070 */
2071int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2072 const char *name, unsigned int flags,
Al Viroe0a01242011-06-27 17:00:37 -04002073 struct path *path)
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002074{
Al Viroe0a01242011-06-27 17:00:37 -04002075 struct nameidata nd;
2076 int err;
2077 nd.root.dentry = dentry;
2078 nd.root.mnt = mnt;
2079 BUG_ON(flags & LOOKUP_PARENT);
Al Viro5b6ca022011-03-09 23:04:47 -05002080 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
Al Viroe0a01242011-06-27 17:00:37 -04002081 err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
2082 if (!err)
2083 *path = nd.path;
2084 return err;
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002085}
2086
James Morris057f6c02007-04-26 00:12:05 -07002087/*
2088 * Restricted form of lookup. Doesn't follow links, single-component only,
2089 * needs parent already locked. Doesn't follow mounts.
2090 * SMP-safe.
2091 */
Adrian Bunka244e162006-03-31 02:32:11 -08002092static struct dentry *lookup_hash(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093{
Al Viro72bd8662012-06-10 17:17:17 -04002094 return __lookup_hash(&nd->last, nd->path.dentry, nd->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095}
2096
Christoph Hellwigeead1912007-10-16 23:25:38 -07002097/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07002098 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07002099 * @name: pathname component to lookup
2100 * @base: base directory to lookup from
2101 * @len: maximum length @len should be interpreted to
2102 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07002103 * Note that this routine is purely a helper for filesystem usage and should
2104 * not be called by generic code. Also note that by using this function the
Christoph Hellwigeead1912007-10-16 23:25:38 -07002105 * nameidata argument is passed to the filesystem methods and a filesystem
2106 * using this helper needs to be prepared for that.
2107 */
James Morris057f6c02007-04-26 00:12:05 -07002108struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2109{
James Morris057f6c02007-04-26 00:12:05 -07002110 struct qstr this;
Al Viro6a96ba52011-03-07 23:49:20 -05002111 unsigned int c;
Miklos Szeredicda309d2012-03-26 12:54:21 +02002112 int err;
James Morris057f6c02007-04-26 00:12:05 -07002113
David Woodhouse2f9092e2009-04-20 23:18:37 +01002114 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
2115
Al Viro6a96ba52011-03-07 23:49:20 -05002116 this.name = name;
2117 this.len = len;
Linus Torvalds0145acc2012-03-02 14:32:59 -08002118 this.hash = full_name_hash(name, len);
Al Viro6a96ba52011-03-07 23:49:20 -05002119 if (!len)
2120 return ERR_PTR(-EACCES);
2121
Al Viro21d8a152012-11-29 22:17:21 -05002122 if (unlikely(name[0] == '.')) {
2123 if (len < 2 || (len == 2 && name[1] == '.'))
2124 return ERR_PTR(-EACCES);
2125 }
2126
Al Viro6a96ba52011-03-07 23:49:20 -05002127 while (len--) {
2128 c = *(const unsigned char *)name++;
2129 if (c == '/' || c == '\0')
2130 return ERR_PTR(-EACCES);
Al Viro6a96ba52011-03-07 23:49:20 -05002131 }
Al Viro5a202bc2011-03-08 14:17:44 -05002132 /*
2133 * See if the low-level filesystem might want
2134 * to use its own hash..
2135 */
2136 if (base->d_flags & DCACHE_OP_HASH) {
Linus Torvaldsda53be12013-05-21 15:22:44 -07002137 int err = base->d_op->d_hash(base, &this);
Al Viro5a202bc2011-03-08 14:17:44 -05002138 if (err < 0)
2139 return ERR_PTR(err);
2140 }
Christoph Hellwigeead1912007-10-16 23:25:38 -07002141
Miklos Szeredicda309d2012-03-26 12:54:21 +02002142 err = inode_permission(base->d_inode, MAY_EXEC);
2143 if (err)
2144 return ERR_PTR(err);
2145
Al Viro72bd8662012-06-10 17:17:17 -04002146 return __lookup_hash(&this, base, 0);
James Morris057f6c02007-04-26 00:12:05 -07002147}
2148
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002149int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2150 struct path *path, int *empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151{
Al Viro2d8f3032008-07-22 09:59:21 -04002152 struct nameidata nd;
Jeff Layton91a27b22012-10-10 15:25:28 -04002153 struct filename *tmp = getname_flags(name, flags, empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 int err = PTR_ERR(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 if (!IS_ERR(tmp)) {
Al Viro2d8f3032008-07-22 09:59:21 -04002156
2157 BUG_ON(flags & LOOKUP_PARENT);
2158
Jeff Layton873f1ee2012-10-10 15:25:29 -04002159 err = filename_lookup(dfd, tmp, flags, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 putname(tmp);
Al Viro2d8f3032008-07-22 09:59:21 -04002161 if (!err)
2162 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 }
2164 return err;
2165}
2166
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002167int user_path_at(int dfd, const char __user *name, unsigned flags,
2168 struct path *path)
2169{
Linus Torvaldsf7493e52012-03-22 16:10:40 -07002170 return user_path_at_empty(dfd, name, flags, path, NULL);
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002171}
2172
Jeff Layton873f1ee2012-10-10 15:25:29 -04002173/*
2174 * NB: most callers don't do anything directly with the reference to the
2175 * to struct filename, but the nd->last pointer points into the name string
2176 * allocated by getname. So we must hold the reference to it until all
2177 * path-walking is complete.
2178 */
Jeff Layton91a27b22012-10-10 15:25:28 -04002179static struct filename *
Jeff Layton9e790bd2012-12-11 12:10:09 -05002180user_path_parent(int dfd, const char __user *path, struct nameidata *nd,
2181 unsigned int flags)
Al Viro2ad94ae2008-07-21 09:32:51 -04002182{
Jeff Layton91a27b22012-10-10 15:25:28 -04002183 struct filename *s = getname(path);
Al Viro2ad94ae2008-07-21 09:32:51 -04002184 int error;
2185
Jeff Layton9e790bd2012-12-11 12:10:09 -05002186 /* only LOOKUP_REVAL is allowed in extra flags */
2187 flags &= LOOKUP_REVAL;
2188
Al Viro2ad94ae2008-07-21 09:32:51 -04002189 if (IS_ERR(s))
Jeff Layton91a27b22012-10-10 15:25:28 -04002190 return s;
Al Viro2ad94ae2008-07-21 09:32:51 -04002191
Jeff Layton9e790bd2012-12-11 12:10:09 -05002192 error = filename_lookup(dfd, s, flags | LOOKUP_PARENT, nd);
Jeff Layton91a27b22012-10-10 15:25:28 -04002193 if (error) {
Al Viro2ad94ae2008-07-21 09:32:51 -04002194 putname(s);
Jeff Layton91a27b22012-10-10 15:25:28 -04002195 return ERR_PTR(error);
2196 }
Al Viro2ad94ae2008-07-21 09:32:51 -04002197
Jeff Layton91a27b22012-10-10 15:25:28 -04002198 return s;
Al Viro2ad94ae2008-07-21 09:32:51 -04002199}
2200
Jeff Layton80334262013-07-26 06:23:25 -04002201/**
Al Viro197df042013-09-08 14:03:27 -04002202 * mountpoint_last - look up last component for umount
Jeff Layton80334262013-07-26 06:23:25 -04002203 * @nd: pathwalk nameidata - currently pointing at parent directory of "last"
2204 * @path: pointer to container for result
2205 *
2206 * This is a special lookup_last function just for umount. In this case, we
2207 * need to resolve the path without doing any revalidation.
2208 *
2209 * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
2210 * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
2211 * in almost all cases, this lookup will be served out of the dcache. The only
2212 * cases where it won't are if nd->last refers to a symlink or the path is
2213 * bogus and it doesn't exist.
2214 *
2215 * Returns:
2216 * -error: if there was an error during lookup. This includes -ENOENT if the
2217 * lookup found a negative dentry. The nd->path reference will also be
2218 * put in this case.
2219 *
2220 * 0: if we successfully resolved nd->path and found it to not to be a
2221 * symlink that needs to be followed. "path" will also be populated.
2222 * The nd->path reference will also be put.
2223 *
2224 * 1: if we successfully resolved nd->last and found it to be a symlink
2225 * that needs to be followed. "path" will be populated with the path
2226 * to the link, and nd->path will *not* be put.
2227 */
2228static int
Al Viro197df042013-09-08 14:03:27 -04002229mountpoint_last(struct nameidata *nd, struct path *path)
Jeff Layton80334262013-07-26 06:23:25 -04002230{
2231 int error = 0;
2232 struct dentry *dentry;
2233 struct dentry *dir = nd->path.dentry;
2234
Al Viro35759522013-09-08 13:41:33 -04002235 /* If we're in rcuwalk, drop out of it to handle last component */
2236 if (nd->flags & LOOKUP_RCU) {
2237 if (unlazy_walk(nd, NULL)) {
2238 error = -ECHILD;
2239 goto out;
2240 }
Jeff Layton80334262013-07-26 06:23:25 -04002241 }
2242
2243 nd->flags &= ~LOOKUP_PARENT;
2244
2245 if (unlikely(nd->last_type != LAST_NORM)) {
2246 error = handle_dots(nd, nd->last_type);
Al Viro35759522013-09-08 13:41:33 -04002247 if (error)
2248 goto out;
2249 dentry = dget(nd->path.dentry);
2250 goto done;
Jeff Layton80334262013-07-26 06:23:25 -04002251 }
2252
2253 mutex_lock(&dir->d_inode->i_mutex);
2254 dentry = d_lookup(dir, &nd->last);
2255 if (!dentry) {
2256 /*
2257 * No cached dentry. Mounted dentries are pinned in the cache,
2258 * so that means that this dentry is probably a symlink or the
2259 * path doesn't actually point to a mounted dentry.
2260 */
2261 dentry = d_alloc(dir, &nd->last);
2262 if (!dentry) {
2263 error = -ENOMEM;
Dave Jonesbcceeeb2013-09-10 17:04:25 -04002264 mutex_unlock(&dir->d_inode->i_mutex);
Al Viro35759522013-09-08 13:41:33 -04002265 goto out;
Jeff Layton80334262013-07-26 06:23:25 -04002266 }
Al Viro35759522013-09-08 13:41:33 -04002267 dentry = lookup_real(dir->d_inode, dentry, nd->flags);
2268 error = PTR_ERR(dentry);
Dave Jonesbcceeeb2013-09-10 17:04:25 -04002269 if (IS_ERR(dentry)) {
2270 mutex_unlock(&dir->d_inode->i_mutex);
Al Viro35759522013-09-08 13:41:33 -04002271 goto out;
Dave Jonesbcceeeb2013-09-10 17:04:25 -04002272 }
Jeff Layton80334262013-07-26 06:23:25 -04002273 }
2274 mutex_unlock(&dir->d_inode->i_mutex);
2275
Al Viro35759522013-09-08 13:41:33 -04002276done:
2277 if (!dentry->d_inode) {
2278 error = -ENOENT;
2279 dput(dentry);
2280 goto out;
Jeff Layton80334262013-07-26 06:23:25 -04002281 }
Al Viro35759522013-09-08 13:41:33 -04002282 path->dentry = dentry;
2283 path->mnt = mntget(nd->path.mnt);
2284 if (should_follow_link(dentry->d_inode, nd->flags & LOOKUP_FOLLOW))
2285 return 1;
2286 follow_mount(path);
2287 error = 0;
2288out:
Jeff Layton80334262013-07-26 06:23:25 -04002289 terminate_walk(nd);
2290 return error;
2291}
2292
2293/**
Al Viro197df042013-09-08 14:03:27 -04002294 * path_mountpoint - look up a path to be umounted
Jeff Layton80334262013-07-26 06:23:25 -04002295 * @dfd: directory file descriptor to start walk from
2296 * @name: full pathname to walk
Randy Dunlap606d6fe2013-10-19 14:56:55 -07002297 * @path: pointer to container for result
Jeff Layton80334262013-07-26 06:23:25 -04002298 * @flags: lookup flags
Jeff Layton80334262013-07-26 06:23:25 -04002299 *
2300 * Look up the given name, but don't attempt to revalidate the last component.
Randy Dunlap606d6fe2013-10-19 14:56:55 -07002301 * Returns 0 and "path" will be valid on success; Returns error otherwise.
Jeff Layton80334262013-07-26 06:23:25 -04002302 */
2303static int
Al Viro197df042013-09-08 14:03:27 -04002304path_mountpoint(int dfd, const char *name, struct path *path, unsigned int flags)
Jeff Layton80334262013-07-26 06:23:25 -04002305{
2306 struct file *base = NULL;
2307 struct nameidata nd;
2308 int err;
2309
2310 err = path_init(dfd, name, flags | LOOKUP_PARENT, &nd, &base);
2311 if (unlikely(err))
2312 return err;
2313
2314 current->total_link_count = 0;
2315 err = link_path_walk(name, &nd);
2316 if (err)
2317 goto out;
2318
Al Viro197df042013-09-08 14:03:27 -04002319 err = mountpoint_last(&nd, path);
Jeff Layton80334262013-07-26 06:23:25 -04002320 while (err > 0) {
2321 void *cookie;
2322 struct path link = *path;
2323 err = may_follow_link(&link, &nd);
2324 if (unlikely(err))
2325 break;
2326 nd.flags |= LOOKUP_PARENT;
2327 err = follow_link(&link, &nd, &cookie);
2328 if (err)
2329 break;
Al Viro197df042013-09-08 14:03:27 -04002330 err = mountpoint_last(&nd, path);
Jeff Layton80334262013-07-26 06:23:25 -04002331 put_link(&nd, &link, cookie);
2332 }
2333out:
2334 if (base)
2335 fput(base);
2336
2337 if (nd.root.mnt && !(nd.flags & LOOKUP_ROOT))
2338 path_put(&nd.root);
2339
2340 return err;
2341}
2342
Al Viro2d864652013-09-08 20:18:44 -04002343static int
2344filename_mountpoint(int dfd, struct filename *s, struct path *path,
2345 unsigned int flags)
2346{
2347 int error = path_mountpoint(dfd, s->name, path, flags | LOOKUP_RCU);
2348 if (unlikely(error == -ECHILD))
2349 error = path_mountpoint(dfd, s->name, path, flags);
2350 if (unlikely(error == -ESTALE))
2351 error = path_mountpoint(dfd, s->name, path, flags | LOOKUP_REVAL);
2352 if (likely(!error))
2353 audit_inode(s, path->dentry, 0);
2354 return error;
2355}
2356
Jeff Layton80334262013-07-26 06:23:25 -04002357/**
Al Viro197df042013-09-08 14:03:27 -04002358 * user_path_mountpoint_at - lookup a path from userland in order to umount it
Jeff Layton80334262013-07-26 06:23:25 -04002359 * @dfd: directory file descriptor
2360 * @name: pathname from userland
2361 * @flags: lookup flags
2362 * @path: pointer to container to hold result
2363 *
2364 * A umount is a special case for path walking. We're not actually interested
2365 * in the inode in this situation, and ESTALE errors can be a problem. We
2366 * simply want track down the dentry and vfsmount attached at the mountpoint
2367 * and avoid revalidating the last component.
2368 *
2369 * Returns 0 and populates "path" on success.
2370 */
2371int
Al Viro197df042013-09-08 14:03:27 -04002372user_path_mountpoint_at(int dfd, const char __user *name, unsigned int flags,
Jeff Layton80334262013-07-26 06:23:25 -04002373 struct path *path)
2374{
2375 struct filename *s = getname(name);
2376 int error;
Jeff Layton80334262013-07-26 06:23:25 -04002377 if (IS_ERR(s))
2378 return PTR_ERR(s);
Al Viro2d864652013-09-08 20:18:44 -04002379 error = filename_mountpoint(dfd, s, path, flags);
Jeff Layton80334262013-07-26 06:23:25 -04002380 putname(s);
2381 return error;
2382}
2383
Al Viro2d864652013-09-08 20:18:44 -04002384int
2385kern_path_mountpoint(int dfd, const char *name, struct path *path,
2386 unsigned int flags)
2387{
2388 struct filename s = {.name = name};
2389 return filename_mountpoint(dfd, &s, path, flags);
2390}
2391EXPORT_SYMBOL(kern_path_mountpoint);
2392
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393/*
2394 * It's inline, so penalty for filesystems that don't use sticky bit is
2395 * minimal.
2396 */
2397static inline int check_sticky(struct inode *dir, struct inode *inode)
2398{
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002399 kuid_t fsuid = current_fsuid();
David Howellsda9592e2008-11-14 10:39:05 +11002400
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 if (!(dir->i_mode & S_ISVTX))
2402 return 0;
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002403 if (uid_eq(inode->i_uid, fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 return 0;
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002405 if (uid_eq(dir->i_uid, fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 return 0;
Eric W. Biederman1a48e2a2011-11-14 16:24:06 -08002407 return !inode_capable(inode, CAP_FOWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408}
2409
2410/*
2411 * Check whether we can remove a link victim from directory dir, check
2412 * whether the type of victim is right.
2413 * 1. We can't do it if dir is read-only (done in permission())
2414 * 2. We should have write and exec permissions on dir
2415 * 3. We can't remove anything from append-only dir
2416 * 4. We can't do anything with immutable dir (done in permission())
2417 * 5. If the sticky bit on dir is set we should either
2418 * a. be owner of dir, or
2419 * b. be owner of victim, or
2420 * c. have CAP_FOWNER capability
2421 * 6. If the victim is append-only or immutable we can't do antyhing with
2422 * links pointing to it.
2423 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2424 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2425 * 9. We can't remove a root or mountpoint.
2426 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
2427 * nfs_async_unlink().
2428 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08002429static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430{
2431 int error;
2432
2433 if (!victim->d_inode)
2434 return -ENOENT;
2435
2436 BUG_ON(victim->d_parent->d_inode != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04002437 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
Al Virof419a2e2008-07-22 00:07:17 -04002439 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 if (error)
2441 return error;
2442 if (IS_APPEND(dir))
2443 return -EPERM;
2444 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
Hugh Dickinsf9454542008-11-19 15:36:38 -08002445 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 return -EPERM;
2447 if (isdir) {
2448 if (!S_ISDIR(victim->d_inode->i_mode))
2449 return -ENOTDIR;
2450 if (IS_ROOT(victim))
2451 return -EBUSY;
2452 } else if (S_ISDIR(victim->d_inode->i_mode))
2453 return -EISDIR;
2454 if (IS_DEADDIR(dir))
2455 return -ENOENT;
2456 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2457 return -EBUSY;
2458 return 0;
2459}
2460
2461/* Check whether we can create an object with dentry child in directory
2462 * dir.
2463 * 1. We can't do it if child already exists (open has special treatment for
2464 * this case, but since we are inlined it's OK)
2465 * 2. We can't do it if dir is read-only (done in permission())
2466 * 3. We should have write and exec permissions on dir
2467 * 4. We can't do it if dir is immutable (done in permission())
2468 */
Miklos Szeredia95164d2008-07-30 15:08:48 +02002469static inline int may_create(struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470{
2471 if (child->d_inode)
2472 return -EEXIST;
2473 if (IS_DEADDIR(dir))
2474 return -ENOENT;
Al Virof419a2e2008-07-22 00:07:17 -04002475 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476}
2477
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478/*
2479 * p1 and p2 should be directories on the same fs.
2480 */
2481struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2482{
2483 struct dentry *p;
2484
2485 if (p1 == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07002486 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 return NULL;
2488 }
2489
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002490 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002492 p = d_ancestor(p2, p1);
2493 if (p) {
2494 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2495 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
2496 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 }
2498
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002499 p = d_ancestor(p1, p2);
2500 if (p) {
2501 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2502 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2503 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 }
2505
Ingo Molnarf2eace22006-07-03 00:25:05 -07002506 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2507 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 return NULL;
2509}
2510
2511void unlock_rename(struct dentry *p1, struct dentry *p2)
2512{
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002513 mutex_unlock(&p1->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 if (p1 != p2) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002515 mutex_unlock(&p2->d_inode->i_mutex);
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002516 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 }
2518}
2519
Al Viro4acdaf22011-07-26 01:42:34 -04002520int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viro312b63f2012-06-10 18:09:36 -04002521 bool want_excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002523 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 if (error)
2525 return error;
2526
Al Viroacfa4382008-12-04 10:06:33 -05002527 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 return -EACCES; /* shouldn't it be ENOSYS? */
2529 mode &= S_IALLUGO;
2530 mode |= S_IFREG;
2531 error = security_inode_create(dir, dentry, mode);
2532 if (error)
2533 return error;
Al Viro312b63f2012-06-10 18:09:36 -04002534 error = dir->i_op->create(dir, dentry, mode, want_excl);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002535 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002536 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 return error;
2538}
2539
Al Viro73d049a2011-03-11 12:08:24 -05002540static int may_open(struct path *path, int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002542 struct dentry *dentry = path->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 struct inode *inode = dentry->d_inode;
2544 int error;
2545
Al Virobcda7652011-03-13 16:42:14 -04002546 /* O_PATH? */
2547 if (!acc_mode)
2548 return 0;
2549
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 if (!inode)
2551 return -ENOENT;
2552
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002553 switch (inode->i_mode & S_IFMT) {
2554 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002556 case S_IFDIR:
2557 if (acc_mode & MAY_WRITE)
2558 return -EISDIR;
2559 break;
2560 case S_IFBLK:
2561 case S_IFCHR:
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002562 if (path->mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002564 /*FALLTHRU*/
2565 case S_IFIFO:
2566 case S_IFSOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002568 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08002569 }
Dave Hansenb41572e2007-10-16 23:31:14 -07002570
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002571 error = inode_permission(inode, acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07002572 if (error)
2573 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05002574
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 /*
2576 * An append-only file must be opened in append mode for writing.
2577 */
2578 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05002579 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05002580 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05002582 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 }
2584
2585 /* O_NOATIME can only be set by the owner or superuser */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07002586 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
Al Viro7715b522009-12-16 03:54:00 -05002587 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588
J. Bruce Fieldsf3c7691e2011-09-21 10:58:13 -04002589 return 0;
Al Viro7715b522009-12-16 03:54:00 -05002590}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
Jeff Laytone1181ee2010-12-07 16:19:50 -05002592static int handle_truncate(struct file *filp)
Al Viro7715b522009-12-16 03:54:00 -05002593{
Jeff Laytone1181ee2010-12-07 16:19:50 -05002594 struct path *path = &filp->f_path;
Al Viro7715b522009-12-16 03:54:00 -05002595 struct inode *inode = path->dentry->d_inode;
2596 int error = get_write_access(inode);
2597 if (error)
2598 return error;
2599 /*
2600 * Refuse to truncate files with mandatory locks held on them.
2601 */
2602 error = locks_verify_locked(inode);
2603 if (!error)
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +09002604 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05002605 if (!error) {
2606 error = do_truncate(path->dentry, 0,
2607 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
Jeff Laytone1181ee2010-12-07 16:19:50 -05002608 filp);
Al Viro7715b522009-12-16 03:54:00 -05002609 }
2610 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04002611 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612}
2613
Dave Hansend57999e2008-02-15 14:37:27 -08002614static inline int open_to_namei_flags(int flag)
2615{
Al Viro8a5e9292011-06-25 19:15:54 -04002616 if ((flag & O_ACCMODE) == 3)
2617 flag--;
Dave Hansend57999e2008-02-15 14:37:27 -08002618 return flag;
2619}
2620
Miklos Szeredid18e9002012-06-05 15:10:17 +02002621static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode)
2622{
2623 int error = security_path_mknod(dir, dentry, mode, 0);
2624 if (error)
2625 return error;
2626
2627 error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
2628 if (error)
2629 return error;
2630
2631 return security_inode_create(dir->dentry->d_inode, dentry, mode);
2632}
2633
David Howells1acf0af2012-06-14 16:13:46 +01002634/*
2635 * Attempt to atomically look up, create and open a file from a negative
2636 * dentry.
2637 *
2638 * Returns 0 if successful. The file will have been created and attached to
2639 * @file by the filesystem calling finish_open().
2640 *
2641 * Returns 1 if the file was looked up only or didn't need creating. The
2642 * caller will need to perform the open themselves. @path will have been
2643 * updated to point to the new dentry. This may be negative.
2644 *
2645 * Returns an error code otherwise.
2646 */
Al Viro2675a4e2012-06-22 12:41:10 +04002647static int atomic_open(struct nameidata *nd, struct dentry *dentry,
2648 struct path *path, struct file *file,
2649 const struct open_flags *op,
Al Viro64894cf2012-07-31 00:53:35 +04002650 bool got_write, bool need_lookup,
Al Viro2675a4e2012-06-22 12:41:10 +04002651 int *opened)
Miklos Szeredid18e9002012-06-05 15:10:17 +02002652{
2653 struct inode *dir = nd->path.dentry->d_inode;
2654 unsigned open_flag = open_to_namei_flags(op->open_flag);
2655 umode_t mode;
2656 int error;
2657 int acc_mode;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002658 int create_error = 0;
2659 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
Miklos Szeredi116cc022013-09-16 14:52:05 +02002660 bool excl;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002661
2662 BUG_ON(dentry->d_inode);
2663
2664 /* Don't create child dentry for a dead directory. */
2665 if (unlikely(IS_DEADDIR(dir))) {
Al Viro2675a4e2012-06-22 12:41:10 +04002666 error = -ENOENT;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002667 goto out;
2668 }
2669
Miklos Szeredi62b259d2012-08-15 13:01:24 +02002670 mode = op->mode;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002671 if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
2672 mode &= ~current_umask();
2673
Miklos Szeredi116cc022013-09-16 14:52:05 +02002674 excl = (open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT);
2675 if (excl)
Miklos Szeredid18e9002012-06-05 15:10:17 +02002676 open_flag &= ~O_TRUNC;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002677
2678 /*
2679 * Checking write permission is tricky, bacuse we don't know if we are
2680 * going to actually need it: O_CREAT opens should work as long as the
2681 * file exists. But checking existence breaks atomicity. The trick is
2682 * to check access and if not granted clear O_CREAT from the flags.
2683 *
2684 * Another problem is returing the "right" error value (e.g. for an
2685 * O_EXCL open we want to return EEXIST not EROFS).
2686 */
Al Viro64894cf2012-07-31 00:53:35 +04002687 if (((open_flag & (O_CREAT | O_TRUNC)) ||
2688 (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) {
2689 if (!(open_flag & O_CREAT)) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002690 /*
2691 * No O_CREATE -> atomicity not a requirement -> fall
2692 * back to lookup + open
2693 */
2694 goto no_open;
2695 } else if (open_flag & (O_EXCL | O_TRUNC)) {
2696 /* Fall back and fail with the right error */
Al Viro64894cf2012-07-31 00:53:35 +04002697 create_error = -EROFS;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002698 goto no_open;
2699 } else {
2700 /* No side effects, safe to clear O_CREAT */
Al Viro64894cf2012-07-31 00:53:35 +04002701 create_error = -EROFS;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002702 open_flag &= ~O_CREAT;
2703 }
2704 }
2705
2706 if (open_flag & O_CREAT) {
Miklos Szeredi38227f72012-08-15 13:01:24 +02002707 error = may_o_create(&nd->path, dentry, mode);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002708 if (error) {
2709 create_error = error;
2710 if (open_flag & O_EXCL)
2711 goto no_open;
2712 open_flag &= ~O_CREAT;
2713 }
2714 }
2715
2716 if (nd->flags & LOOKUP_DIRECTORY)
2717 open_flag |= O_DIRECTORY;
2718
Al Viro30d90492012-06-22 12:40:19 +04002719 file->f_path.dentry = DENTRY_NOT_SET;
2720 file->f_path.mnt = nd->path.mnt;
2721 error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode,
Al Viro47237682012-06-10 05:01:45 -04002722 opened);
Al Virod9585272012-06-22 12:39:14 +04002723 if (error < 0) {
Al Virod9585272012-06-22 12:39:14 +04002724 if (create_error && error == -ENOENT)
2725 error = create_error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002726 goto out;
2727 }
2728
Al Virod9585272012-06-22 12:39:14 +04002729 if (error) { /* returned 1, that is */
Al Viro30d90492012-06-22 12:40:19 +04002730 if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
Al Viro2675a4e2012-06-22 12:41:10 +04002731 error = -EIO;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002732 goto out;
2733 }
Al Viro30d90492012-06-22 12:40:19 +04002734 if (file->f_path.dentry) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002735 dput(dentry);
Al Viro30d90492012-06-22 12:40:19 +04002736 dentry = file->f_path.dentry;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002737 }
Al Viro03da6332013-09-16 19:22:33 -04002738 if (*opened & FILE_CREATED)
2739 fsnotify_create(dir, dentry);
2740 if (!dentry->d_inode) {
2741 WARN_ON(*opened & FILE_CREATED);
2742 if (create_error) {
2743 error = create_error;
2744 goto out;
2745 }
2746 } else {
2747 if (excl && !(*opened & FILE_CREATED)) {
2748 error = -EEXIST;
2749 goto out;
2750 }
Sage Weil62b2ce92012-08-15 13:30:12 -07002751 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02002752 goto looked_up;
2753 }
2754
2755 /*
2756 * We didn't have the inode before the open, so check open permission
2757 * here.
2758 */
Al Viro03da6332013-09-16 19:22:33 -04002759 acc_mode = op->acc_mode;
2760 if (*opened & FILE_CREATED) {
2761 WARN_ON(!(open_flag & O_CREAT));
2762 fsnotify_create(dir, dentry);
2763 acc_mode = MAY_OPEN;
2764 }
Al Viro2675a4e2012-06-22 12:41:10 +04002765 error = may_open(&file->f_path, acc_mode, open_flag);
2766 if (error)
2767 fput(file);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002768
2769out:
2770 dput(dentry);
Al Viro2675a4e2012-06-22 12:41:10 +04002771 return error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002772
Miklos Szeredid18e9002012-06-05 15:10:17 +02002773no_open:
2774 if (need_lookup) {
Al Viro72bd8662012-06-10 17:17:17 -04002775 dentry = lookup_real(dir, dentry, nd->flags);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002776 if (IS_ERR(dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04002777 return PTR_ERR(dentry);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002778
2779 if (create_error) {
2780 int open_flag = op->open_flag;
2781
Al Viro2675a4e2012-06-22 12:41:10 +04002782 error = create_error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002783 if ((open_flag & O_EXCL)) {
2784 if (!dentry->d_inode)
2785 goto out;
2786 } else if (!dentry->d_inode) {
2787 goto out;
2788 } else if ((open_flag & O_TRUNC) &&
2789 S_ISREG(dentry->d_inode->i_mode)) {
2790 goto out;
2791 }
2792 /* will fail later, go on to get the right error */
2793 }
2794 }
2795looked_up:
2796 path->dentry = dentry;
2797 path->mnt = nd->path.mnt;
Al Viro2675a4e2012-06-22 12:41:10 +04002798 return 1;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002799}
2800
Nick Piggin31e6b012011-01-07 17:49:52 +11002801/*
David Howells1acf0af2012-06-14 16:13:46 +01002802 * Look up and maybe create and open the last component.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002803 *
2804 * Must be called with i_mutex held on parent.
2805 *
David Howells1acf0af2012-06-14 16:13:46 +01002806 * Returns 0 if the file was successfully atomically created (if necessary) and
2807 * opened. In this case the file will be returned attached to @file.
2808 *
2809 * Returns 1 if the file was not completely opened at this time, though lookups
2810 * and creations will have been performed and the dentry returned in @path will
2811 * be positive upon return if O_CREAT was specified. If O_CREAT wasn't
2812 * specified then a negative dentry may be returned.
2813 *
2814 * An error code is returned otherwise.
2815 *
2816 * FILE_CREATE will be set in @*opened if the dentry was created and will be
2817 * cleared otherwise prior to returning.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002818 */
Al Viro2675a4e2012-06-22 12:41:10 +04002819static int lookup_open(struct nameidata *nd, struct path *path,
2820 struct file *file,
2821 const struct open_flags *op,
Al Viro64894cf2012-07-31 00:53:35 +04002822 bool got_write, int *opened)
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002823{
2824 struct dentry *dir = nd->path.dentry;
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002825 struct inode *dir_inode = dir->d_inode;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002826 struct dentry *dentry;
2827 int error;
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002828 bool need_lookup;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002829
Al Viro47237682012-06-10 05:01:45 -04002830 *opened &= ~FILE_CREATED;
Al Viro201f9562012-06-22 12:42:10 +04002831 dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002832 if (IS_ERR(dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04002833 return PTR_ERR(dentry);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002834
Miklos Szeredid18e9002012-06-05 15:10:17 +02002835 /* Cached positive dentry: will open in f_op->open */
2836 if (!need_lookup && dentry->d_inode)
2837 goto out_no_open;
2838
2839 if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) {
Al Viro64894cf2012-07-31 00:53:35 +04002840 return atomic_open(nd, dentry, path, file, op, got_write,
Al Viro47237682012-06-10 05:01:45 -04002841 need_lookup, opened);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002842 }
2843
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002844 if (need_lookup) {
2845 BUG_ON(dentry->d_inode);
2846
Al Viro72bd8662012-06-10 17:17:17 -04002847 dentry = lookup_real(dir_inode, dentry, nd->flags);
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002848 if (IS_ERR(dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04002849 return PTR_ERR(dentry);
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002850 }
2851
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002852 /* Negative dentry, just create the file */
2853 if (!dentry->d_inode && (op->open_flag & O_CREAT)) {
2854 umode_t mode = op->mode;
2855 if (!IS_POSIXACL(dir->d_inode))
2856 mode &= ~current_umask();
2857 /*
2858 * This write is needed to ensure that a
2859 * rw->ro transition does not occur between
2860 * the time when the file is created and when
2861 * a permanent write count is taken through
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02002862 * the 'struct file' in finish_open().
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002863 */
Al Viro64894cf2012-07-31 00:53:35 +04002864 if (!got_write) {
2865 error = -EROFS;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002866 goto out_dput;
Al Viro64894cf2012-07-31 00:53:35 +04002867 }
Al Viro47237682012-06-10 05:01:45 -04002868 *opened |= FILE_CREATED;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002869 error = security_path_mknod(&nd->path, dentry, mode, 0);
2870 if (error)
2871 goto out_dput;
Al Viro312b63f2012-06-10 18:09:36 -04002872 error = vfs_create(dir->d_inode, dentry, mode,
2873 nd->flags & LOOKUP_EXCL);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002874 if (error)
2875 goto out_dput;
2876 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02002877out_no_open:
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002878 path->dentry = dentry;
2879 path->mnt = nd->path.mnt;
Al Viro2675a4e2012-06-22 12:41:10 +04002880 return 1;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002881
2882out_dput:
2883 dput(dentry);
Al Viro2675a4e2012-06-22 12:41:10 +04002884 return error;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002885}
2886
2887/*
Al Virofe2d35f2011-03-05 22:58:25 -05002888 * Handle the last step of open()
Nick Piggin31e6b012011-01-07 17:49:52 +11002889 */
Al Viro2675a4e2012-06-22 12:41:10 +04002890static int do_last(struct nameidata *nd, struct path *path,
2891 struct file *file, const struct open_flags *op,
Jeff Layton669abf42012-10-10 16:43:10 -04002892 int *opened, struct filename *name)
Al Virofb1cc552009-12-24 01:58:28 -05002893{
Al Viroa1e28032009-12-24 02:12:06 -05002894 struct dentry *dir = nd->path.dentry;
Al Viroca344a892011-03-09 00:36:45 -05002895 int open_flag = op->open_flag;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002896 bool will_truncate = (open_flag & O_TRUNC) != 0;
Al Viro64894cf2012-07-31 00:53:35 +04002897 bool got_write = false;
Al Virobcda7652011-03-13 16:42:14 -04002898 int acc_mode = op->acc_mode;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02002899 struct inode *inode;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002900 bool symlink_ok = false;
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02002901 struct path save_parent = { .dentry = NULL, .mnt = NULL };
2902 bool retried = false;
Al Viro16c2cd72011-02-22 15:50:10 -05002903 int error;
Al Virofb1cc552009-12-24 01:58:28 -05002904
Al Viroc3e380b2011-02-23 13:39:45 -05002905 nd->flags &= ~LOOKUP_PARENT;
2906 nd->flags |= op->intent;
2907
Al Virobc77daa2013-06-06 09:12:33 -04002908 if (nd->last_type != LAST_NORM) {
Al Virofe2d35f2011-03-05 22:58:25 -05002909 error = handle_dots(nd, nd->last_type);
2910 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04002911 return error;
Miklos Szeredie83db162012-06-05 15:10:29 +02002912 goto finish_open;
Al Viro1f36f772009-12-26 10:56:19 -05002913 }
Al Viro67ee3ad2009-12-26 07:01:01 -05002914
Al Viroca344a892011-03-09 00:36:45 -05002915 if (!(open_flag & O_CREAT)) {
Al Virofe2d35f2011-03-05 22:58:25 -05002916 if (nd->last.name[nd->last.len])
2917 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
Al Virobcda7652011-03-13 16:42:14 -04002918 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002919 symlink_ok = true;
Al Virofe2d35f2011-03-05 22:58:25 -05002920 /* we _can_ be in RCU mode here */
Al Viroe97cdc82013-01-24 18:16:00 -05002921 error = lookup_fast(nd, path, &inode);
Miklos Szeredi71574862012-06-05 15:10:14 +02002922 if (likely(!error))
2923 goto finish_lookup;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02002924
Miklos Szeredi71574862012-06-05 15:10:14 +02002925 if (error < 0)
Al Viro2675a4e2012-06-22 12:41:10 +04002926 goto out;
Miklos Szeredi37d7fff2012-06-05 15:10:12 +02002927
Miklos Szeredi71574862012-06-05 15:10:14 +02002928 BUG_ON(nd->inode != dir->d_inode);
Miklos Szeredib6183df2012-06-05 15:10:13 +02002929 } else {
2930 /* create side of things */
2931 /*
2932 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
2933 * has been cleared when we got to the last component we are
2934 * about to look up
2935 */
2936 error = complete_walk(nd);
2937 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04002938 return error;
Miklos Szeredib6183df2012-06-05 15:10:13 +02002939
Jeff Layton33e22082013-04-12 15:16:32 -04002940 audit_inode(name, dir, LOOKUP_PARENT);
Miklos Szeredib6183df2012-06-05 15:10:13 +02002941 error = -EISDIR;
2942 /* trailing slashes? */
2943 if (nd->last.name[nd->last.len])
Al Viro2675a4e2012-06-22 12:41:10 +04002944 goto out;
Al Virofe2d35f2011-03-05 22:58:25 -05002945 }
2946
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02002947retry_lookup:
Al Viro64894cf2012-07-31 00:53:35 +04002948 if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
2949 error = mnt_want_write(nd->path.mnt);
2950 if (!error)
2951 got_write = true;
2952 /*
2953 * do _not_ fail yet - we might not need that or fail with
2954 * a different error; let lookup_open() decide; we'll be
2955 * dropping this one anyway.
2956 */
2957 }
Al Viroa1e28032009-12-24 02:12:06 -05002958 mutex_lock(&dir->d_inode->i_mutex);
Al Viro64894cf2012-07-31 00:53:35 +04002959 error = lookup_open(nd, path, file, op, got_write, opened);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002960 mutex_unlock(&dir->d_inode->i_mutex);
Al Viroa1e28032009-12-24 02:12:06 -05002961
Al Viro2675a4e2012-06-22 12:41:10 +04002962 if (error <= 0) {
2963 if (error)
Miklos Szeredid18e9002012-06-05 15:10:17 +02002964 goto out;
2965
Al Viro47237682012-06-10 05:01:45 -04002966 if ((*opened & FILE_CREATED) ||
Al Viro496ad9a2013-01-23 17:07:38 -05002967 !S_ISREG(file_inode(file)->i_mode))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002968 will_truncate = false;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002969
Jeff Laytonadb5c242012-10-10 16:43:13 -04002970 audit_inode(name, file->f_path.dentry, 0);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002971 goto opened;
2972 }
Al Virofb1cc552009-12-24 01:58:28 -05002973
Al Viro47237682012-06-10 05:01:45 -04002974 if (*opened & FILE_CREATED) {
Al Viro9b44f1b2011-03-09 00:17:27 -05002975 /* Don't check for write permission, don't truncate */
Al Viroca344a892011-03-09 00:36:45 -05002976 open_flag &= ~O_TRUNC;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002977 will_truncate = false;
Al Virobcda7652011-03-13 16:42:14 -04002978 acc_mode = MAY_OPEN;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002979 path_to_nameidata(path, nd);
Miklos Szeredie83db162012-06-05 15:10:29 +02002980 goto finish_open_created;
Al Virofb1cc552009-12-24 01:58:28 -05002981 }
2982
2983 /*
Jeff Layton3134f372012-07-25 10:19:47 -04002984 * create/update audit record if it already exists.
Al Virofb1cc552009-12-24 01:58:28 -05002985 */
Jeff Layton3134f372012-07-25 10:19:47 -04002986 if (path->dentry->d_inode)
Jeff Laytonadb5c242012-10-10 16:43:13 -04002987 audit_inode(name, path->dentry, 0);
Al Virofb1cc552009-12-24 01:58:28 -05002988
Miklos Szeredid18e9002012-06-05 15:10:17 +02002989 /*
2990 * If atomic_open() acquired write access it is dropped now due to
2991 * possible mount and symlink following (this might be optimized away if
2992 * necessary...)
2993 */
Al Viro64894cf2012-07-31 00:53:35 +04002994 if (got_write) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002995 mnt_drop_write(nd->path.mnt);
Al Viro64894cf2012-07-31 00:53:35 +04002996 got_write = false;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002997 }
2998
Al Virofb1cc552009-12-24 01:58:28 -05002999 error = -EEXIST;
Al Virof8310c52012-07-30 11:50:30 +04003000 if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))
Al Virofb1cc552009-12-24 01:58:28 -05003001 goto exit_dput;
3002
David Howells9875cf82011-01-14 18:45:21 +00003003 error = follow_managed(path, nd->flags);
3004 if (error < 0)
3005 goto exit_dput;
Al Virofb1cc552009-12-24 01:58:28 -05003006
Al Viroa3fbbde2011-11-07 21:21:26 +00003007 if (error)
3008 nd->flags |= LOOKUP_JUMPED;
3009
Miklos Szeredidecf3402012-05-21 17:30:08 +02003010 BUG_ON(nd->flags & LOOKUP_RCU);
3011 inode = path->dentry->d_inode;
Miklos Szeredi5f5daac2012-05-21 17:30:14 +02003012finish_lookup:
3013 /* we _can_ be in RCU mode here */
Al Virofb1cc552009-12-24 01:58:28 -05003014 error = -ENOENT;
Miklos Szeredi54c33e72012-05-21 17:30:10 +02003015 if (!inode) {
3016 path_to_nameidata(path, nd);
Al Viro2675a4e2012-06-22 12:41:10 +04003017 goto out;
Miklos Szeredi54c33e72012-05-21 17:30:10 +02003018 }
Al Viro9e67f362009-12-26 07:04:50 -05003019
Miklos Szeredid45ea862012-05-21 17:30:09 +02003020 if (should_follow_link(inode, !symlink_ok)) {
3021 if (nd->flags & LOOKUP_RCU) {
3022 if (unlikely(unlazy_walk(nd, path->dentry))) {
3023 error = -ECHILD;
Al Viro2675a4e2012-06-22 12:41:10 +04003024 goto out;
Miklos Szeredid45ea862012-05-21 17:30:09 +02003025 }
3026 }
3027 BUG_ON(inode != path->dentry->d_inode);
Al Viro2675a4e2012-06-22 12:41:10 +04003028 return 1;
Miklos Szeredid45ea862012-05-21 17:30:09 +02003029 }
Al Virofb1cc552009-12-24 01:58:28 -05003030
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003031 if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) {
3032 path_to_nameidata(path, nd);
3033 } else {
3034 save_parent.dentry = nd->path.dentry;
3035 save_parent.mnt = mntget(path->mnt);
3036 nd->path.dentry = path->dentry;
3037
3038 }
Miklos Szeredidecf3402012-05-21 17:30:08 +02003039 nd->inode = inode;
Al Viroa3fbbde2011-11-07 21:21:26 +00003040 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
Al Virobc77daa2013-06-06 09:12:33 -04003041finish_open:
Al Viroa3fbbde2011-11-07 21:21:26 +00003042 error = complete_walk(nd);
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003043 if (error) {
3044 path_put(&save_parent);
Al Viro2675a4e2012-06-22 12:41:10 +04003045 return error;
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003046 }
Al Virobc77daa2013-06-06 09:12:33 -04003047 audit_inode(name, nd->path.dentry, 0);
Al Virofb1cc552009-12-24 01:58:28 -05003048 error = -EISDIR;
Miklos Szeredi050ac842012-05-21 17:30:12 +02003049 if ((open_flag & O_CREAT) && S_ISDIR(nd->inode->i_mode))
Al Viro2675a4e2012-06-22 12:41:10 +04003050 goto out;
Miklos Szerediaf2f5542012-05-21 17:30:11 +02003051 error = -ENOTDIR;
Al Viro05252902013-06-06 19:33:47 -04003052 if ((nd->flags & LOOKUP_DIRECTORY) && !can_lookup(nd->inode))
Al Viro2675a4e2012-06-22 12:41:10 +04003053 goto out;
Al Viro6c0d46c2011-03-09 00:59:59 -05003054 if (!S_ISREG(nd->inode->i_mode))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003055 will_truncate = false;
Al Viro6c0d46c2011-03-09 00:59:59 -05003056
Al Viro0f9d1a12011-03-09 00:13:14 -05003057 if (will_truncate) {
3058 error = mnt_want_write(nd->path.mnt);
3059 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003060 goto out;
Al Viro64894cf2012-07-31 00:53:35 +04003061 got_write = true;
Al Viro0f9d1a12011-03-09 00:13:14 -05003062 }
Miklos Szeredie83db162012-06-05 15:10:29 +02003063finish_open_created:
Al Virobcda7652011-03-13 16:42:14 -04003064 error = may_open(&nd->path, acc_mode, open_flag);
Al Viroca344a892011-03-09 00:36:45 -05003065 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003066 goto out;
Al Viro30d90492012-06-22 12:40:19 +04003067 file->f_path.mnt = nd->path.mnt;
3068 error = finish_open(file, nd->path.dentry, NULL, opened);
3069 if (error) {
Al Viro30d90492012-06-22 12:40:19 +04003070 if (error == -EOPENSTALE)
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003071 goto stale_open;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003072 goto out;
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003073 }
Miklos Szeredia8277b92012-06-05 15:10:32 +02003074opened:
Al Viro2675a4e2012-06-22 12:41:10 +04003075 error = open_check_o_direct(file);
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003076 if (error)
3077 goto exit_fput;
Al Viro2675a4e2012-06-22 12:41:10 +04003078 error = ima_file_check(file, op->acc_mode);
Miklos Szerediaa4caad2012-06-05 15:10:28 +02003079 if (error)
3080 goto exit_fput;
3081
3082 if (will_truncate) {
Al Viro2675a4e2012-06-22 12:41:10 +04003083 error = handle_truncate(file);
Miklos Szerediaa4caad2012-06-05 15:10:28 +02003084 if (error)
3085 goto exit_fput;
Al Viro0f9d1a12011-03-09 00:13:14 -05003086 }
Al Viroca344a892011-03-09 00:36:45 -05003087out:
Al Viro64894cf2012-07-31 00:53:35 +04003088 if (got_write)
Al Viro0f9d1a12011-03-09 00:13:14 -05003089 mnt_drop_write(nd->path.mnt);
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003090 path_put(&save_parent);
Miklos Szeredie276ae62012-05-21 17:30:06 +02003091 terminate_walk(nd);
Al Viro2675a4e2012-06-22 12:41:10 +04003092 return error;
Al Virofb1cc552009-12-24 01:58:28 -05003093
Al Virofb1cc552009-12-24 01:58:28 -05003094exit_dput:
3095 path_put_conditional(path, nd);
Al Viroca344a892011-03-09 00:36:45 -05003096 goto out;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003097exit_fput:
Al Viro2675a4e2012-06-22 12:41:10 +04003098 fput(file);
3099 goto out;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003100
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003101stale_open:
3102 /* If no saved parent or already retried then can't retry */
3103 if (!save_parent.dentry || retried)
3104 goto out;
3105
3106 BUG_ON(save_parent.dentry != dir);
3107 path_put(&nd->path);
3108 nd->path = save_parent;
3109 nd->inode = dir->d_inode;
3110 save_parent.mnt = NULL;
3111 save_parent.dentry = NULL;
Al Viro64894cf2012-07-31 00:53:35 +04003112 if (got_write) {
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003113 mnt_drop_write(nd->path.mnt);
Al Viro64894cf2012-07-31 00:53:35 +04003114 got_write = false;
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003115 }
3116 retried = true;
3117 goto retry_lookup;
Al Virofb1cc552009-12-24 01:58:28 -05003118}
3119
Al Viro60545d02013-06-07 01:20:27 -04003120static int do_tmpfile(int dfd, struct filename *pathname,
3121 struct nameidata *nd, int flags,
3122 const struct open_flags *op,
3123 struct file *file, int *opened)
3124{
3125 static const struct qstr name = QSTR_INIT("/", 1);
3126 struct dentry *dentry, *child;
3127 struct inode *dir;
3128 int error = path_lookupat(dfd, pathname->name,
3129 flags | LOOKUP_DIRECTORY, nd);
3130 if (unlikely(error))
3131 return error;
3132 error = mnt_want_write(nd->path.mnt);
3133 if (unlikely(error))
3134 goto out;
3135 /* we want directory to be writable */
3136 error = inode_permission(nd->inode, MAY_WRITE | MAY_EXEC);
3137 if (error)
3138 goto out2;
3139 dentry = nd->path.dentry;
3140 dir = dentry->d_inode;
3141 if (!dir->i_op->tmpfile) {
3142 error = -EOPNOTSUPP;
3143 goto out2;
3144 }
3145 child = d_alloc(dentry, &name);
3146 if (unlikely(!child)) {
3147 error = -ENOMEM;
3148 goto out2;
3149 }
3150 nd->flags &= ~LOOKUP_DIRECTORY;
3151 nd->flags |= op->intent;
3152 dput(nd->path.dentry);
3153 nd->path.dentry = child;
3154 error = dir->i_op->tmpfile(dir, nd->path.dentry, op->mode);
3155 if (error)
3156 goto out2;
3157 audit_inode(pathname, nd->path.dentry, 0);
3158 error = may_open(&nd->path, op->acc_mode, op->open_flag);
3159 if (error)
3160 goto out2;
3161 file->f_path.mnt = nd->path.mnt;
3162 error = finish_open(file, nd->path.dentry, NULL, opened);
3163 if (error)
3164 goto out2;
3165 error = open_check_o_direct(file);
Al Virof4e0c302013-06-11 08:34:36 +04003166 if (error) {
Al Viro60545d02013-06-07 01:20:27 -04003167 fput(file);
Al Virof4e0c302013-06-11 08:34:36 +04003168 } else if (!(op->open_flag & O_EXCL)) {
3169 struct inode *inode = file_inode(file);
3170 spin_lock(&inode->i_lock);
3171 inode->i_state |= I_LINKABLE;
3172 spin_unlock(&inode->i_lock);
3173 }
Al Viro60545d02013-06-07 01:20:27 -04003174out2:
3175 mnt_drop_write(nd->path.mnt);
3176out:
3177 path_put(&nd->path);
3178 return error;
3179}
3180
Jeff Layton669abf42012-10-10 16:43:10 -04003181static struct file *path_openat(int dfd, struct filename *pathname,
Al Viro73d049a2011-03-11 12:08:24 -05003182 struct nameidata *nd, const struct open_flags *op, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183{
Al Virofe2d35f2011-03-05 22:58:25 -05003184 struct file *base = NULL;
Al Viro30d90492012-06-22 12:40:19 +04003185 struct file *file;
Al Viro9850c052010-01-13 15:01:15 -05003186 struct path path;
Al Viro47237682012-06-10 05:01:45 -04003187 int opened = 0;
Al Viro13aab422011-02-23 17:54:08 -05003188 int error;
Nick Piggin31e6b012011-01-07 17:49:52 +11003189
Al Viro30d90492012-06-22 12:40:19 +04003190 file = get_empty_filp();
Al Viro1afc99b2013-02-14 20:41:04 -05003191 if (IS_ERR(file))
3192 return file;
Nick Piggin31e6b012011-01-07 17:49:52 +11003193
Al Viro30d90492012-06-22 12:40:19 +04003194 file->f_flags = op->open_flag;
Nick Piggin31e6b012011-01-07 17:49:52 +11003195
Al Virobb458c62013-07-13 13:26:37 +04003196 if (unlikely(file->f_flags & __O_TMPFILE)) {
Al Viro60545d02013-06-07 01:20:27 -04003197 error = do_tmpfile(dfd, pathname, nd, flags, op, file, &opened);
3198 goto out;
3199 }
3200
Jeff Layton669abf42012-10-10 16:43:10 -04003201 error = path_init(dfd, pathname->name, flags | LOOKUP_PARENT, nd, &base);
Nick Piggin31e6b012011-01-07 17:49:52 +11003202 if (unlikely(error))
Al Viro2675a4e2012-06-22 12:41:10 +04003203 goto out;
Nick Piggin31e6b012011-01-07 17:49:52 +11003204
Al Virofe2d35f2011-03-05 22:58:25 -05003205 current->total_link_count = 0;
Jeff Layton669abf42012-10-10 16:43:10 -04003206 error = link_path_walk(pathname->name, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11003207 if (unlikely(error))
Al Viro2675a4e2012-06-22 12:41:10 +04003208 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209
Al Viro2675a4e2012-06-22 12:41:10 +04003210 error = do_last(nd, &path, file, op, &opened, pathname);
3211 while (unlikely(error > 0)) { /* trailing symlink */
Nick Piggin7b9337a2011-01-14 08:42:43 +00003212 struct path link = path;
Al Virodef4af32009-12-26 08:37:05 -05003213 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04003214 if (!(nd->flags & LOOKUP_FOLLOW)) {
Al Viro73d049a2011-03-11 12:08:24 -05003215 path_put_conditional(&path, nd);
3216 path_put(&nd->path);
Al Viro2675a4e2012-06-22 12:41:10 +04003217 error = -ELOOP;
Al Viro40b39132011-03-09 16:22:18 -05003218 break;
3219 }
Kees Cook800179c2012-07-25 17:29:07 -07003220 error = may_follow_link(&link, nd);
3221 if (unlikely(error))
3222 break;
Al Viro73d049a2011-03-11 12:08:24 -05003223 nd->flags |= LOOKUP_PARENT;
3224 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
Al Viro574197e2011-03-14 22:20:34 -04003225 error = follow_link(&link, nd, &cookie);
Al Viroc3e380b2011-02-23 13:39:45 -05003226 if (unlikely(error))
Al Viro2675a4e2012-06-22 12:41:10 +04003227 break;
3228 error = do_last(nd, &path, file, op, &opened, pathname);
Al Viro574197e2011-03-14 22:20:34 -04003229 put_link(nd, &link, cookie);
Al Viro806b6812009-12-26 07:16:40 -05003230 }
Al Viro10fa8e62009-12-26 07:09:49 -05003231out:
Al Viro73d049a2011-03-11 12:08:24 -05003232 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
3233 path_put(&nd->root);
Al Virofe2d35f2011-03-05 22:58:25 -05003234 if (base)
3235 fput(base);
Al Viro2675a4e2012-06-22 12:41:10 +04003236 if (!(opened & FILE_OPENED)) {
3237 BUG_ON(!error);
Al Viro30d90492012-06-22 12:40:19 +04003238 put_filp(file);
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003239 }
Al Viro2675a4e2012-06-22 12:41:10 +04003240 if (unlikely(error)) {
3241 if (error == -EOPENSTALE) {
3242 if (flags & LOOKUP_RCU)
3243 error = -ECHILD;
3244 else
3245 error = -ESTALE;
3246 }
3247 file = ERR_PTR(error);
3248 }
3249 return file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250}
3251
Jeff Layton669abf42012-10-10 16:43:10 -04003252struct file *do_filp_open(int dfd, struct filename *pathname,
Al Virof9652e12013-06-11 08:23:01 +04003253 const struct open_flags *op)
Al Viro13aab422011-02-23 17:54:08 -05003254{
Al Viro73d049a2011-03-11 12:08:24 -05003255 struct nameidata nd;
Al Virof9652e12013-06-11 08:23:01 +04003256 int flags = op->lookup_flags;
Al Viro13aab422011-02-23 17:54:08 -05003257 struct file *filp;
3258
Al Viro73d049a2011-03-11 12:08:24 -05003259 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
Al Viro13aab422011-02-23 17:54:08 -05003260 if (unlikely(filp == ERR_PTR(-ECHILD)))
Al Viro73d049a2011-03-11 12:08:24 -05003261 filp = path_openat(dfd, pathname, &nd, op, flags);
Al Viro13aab422011-02-23 17:54:08 -05003262 if (unlikely(filp == ERR_PTR(-ESTALE)))
Al Viro73d049a2011-03-11 12:08:24 -05003263 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
Al Viro13aab422011-02-23 17:54:08 -05003264 return filp;
3265}
3266
Al Viro73d049a2011-03-11 12:08:24 -05003267struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
Al Virof9652e12013-06-11 08:23:01 +04003268 const char *name, const struct open_flags *op)
Al Viro73d049a2011-03-11 12:08:24 -05003269{
3270 struct nameidata nd;
3271 struct file *file;
Jeff Layton669abf42012-10-10 16:43:10 -04003272 struct filename filename = { .name = name };
Al Virof9652e12013-06-11 08:23:01 +04003273 int flags = op->lookup_flags | LOOKUP_ROOT;
Al Viro73d049a2011-03-11 12:08:24 -05003274
3275 nd.root.mnt = mnt;
3276 nd.root.dentry = dentry;
3277
Al Virobcda7652011-03-13 16:42:14 -04003278 if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
Al Viro73d049a2011-03-11 12:08:24 -05003279 return ERR_PTR(-ELOOP);
3280
Jeff Layton669abf42012-10-10 16:43:10 -04003281 file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_RCU);
Al Viro73d049a2011-03-11 12:08:24 -05003282 if (unlikely(file == ERR_PTR(-ECHILD)))
Jeff Layton669abf42012-10-10 16:43:10 -04003283 file = path_openat(-1, &filename, &nd, op, flags);
Al Viro73d049a2011-03-11 12:08:24 -05003284 if (unlikely(file == ERR_PTR(-ESTALE)))
Jeff Layton669abf42012-10-10 16:43:10 -04003285 file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_REVAL);
Al Viro73d049a2011-03-11 12:08:24 -05003286 return file;
3287}
3288
Jeff Layton1ac12b42012-12-11 12:10:06 -05003289struct dentry *kern_path_create(int dfd, const char *pathname,
3290 struct path *path, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003292 struct dentry *dentry = ERR_PTR(-EEXIST);
Al Viroed75e952011-06-27 16:53:43 -04003293 struct nameidata nd;
Jan Karac30dabf2012-06-12 16:20:30 +02003294 int err2;
Jeff Layton1ac12b42012-12-11 12:10:06 -05003295 int error;
3296 bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
3297
3298 /*
3299 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
3300 * other flags passed in are ignored!
3301 */
3302 lookup_flags &= LOOKUP_REVAL;
3303
3304 error = do_path_lookup(dfd, pathname, LOOKUP_PARENT|lookup_flags, &nd);
Al Viroed75e952011-06-27 16:53:43 -04003305 if (error)
3306 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003308 /*
3309 * Yucky last component or no last component at all?
3310 * (foo/., foo/.., /////)
3311 */
Al Viroed75e952011-06-27 16:53:43 -04003312 if (nd.last_type != LAST_NORM)
3313 goto out;
3314 nd.flags &= ~LOOKUP_PARENT;
3315 nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003316
Jan Karac30dabf2012-06-12 16:20:30 +02003317 /* don't fail immediately if it's r/o, at least try to report other errors */
3318 err2 = mnt_want_write(nd.path.mnt);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003319 /*
3320 * Do the final lookup.
3321 */
Al Viroed75e952011-06-27 16:53:43 -04003322 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3323 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 if (IS_ERR(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003325 goto unlock;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003326
Al Viroa8104a92012-07-20 02:25:00 +04003327 error = -EEXIST;
Al Viroe9baf6e2008-05-15 04:49:12 -04003328 if (dentry->d_inode)
Al Viroa8104a92012-07-20 02:25:00 +04003329 goto fail;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003330 /*
3331 * Special case - lookup gave negative, but... we had foo/bar/
3332 * From the vfs_mknod() POV we just have a negative dentry -
3333 * all is fine. Let's be bastards - you had / on the end, you've
3334 * been asking for (non-existent) directory. -ENOENT for you.
3335 */
Al Viroed75e952011-06-27 16:53:43 -04003336 if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
Al Viroa8104a92012-07-20 02:25:00 +04003337 error = -ENOENT;
Al Viroed75e952011-06-27 16:53:43 -04003338 goto fail;
Al Viroe9baf6e2008-05-15 04:49:12 -04003339 }
Jan Karac30dabf2012-06-12 16:20:30 +02003340 if (unlikely(err2)) {
3341 error = err2;
Al Viroa8104a92012-07-20 02:25:00 +04003342 goto fail;
Jan Karac30dabf2012-06-12 16:20:30 +02003343 }
Al Viroed75e952011-06-27 16:53:43 -04003344 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346fail:
Al Viroa8104a92012-07-20 02:25:00 +04003347 dput(dentry);
3348 dentry = ERR_PTR(error);
3349unlock:
Al Viroed75e952011-06-27 16:53:43 -04003350 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Karac30dabf2012-06-12 16:20:30 +02003351 if (!err2)
3352 mnt_drop_write(nd.path.mnt);
Al Viroed75e952011-06-27 16:53:43 -04003353out:
3354 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 return dentry;
3356}
Al Virodae6ad82011-06-26 11:50:15 -04003357EXPORT_SYMBOL(kern_path_create);
3358
Al Viro921a1652012-07-20 01:15:31 +04003359void done_path_create(struct path *path, struct dentry *dentry)
3360{
3361 dput(dentry);
3362 mutex_unlock(&path->dentry->d_inode->i_mutex);
Al Viroa8104a92012-07-20 02:25:00 +04003363 mnt_drop_write(path->mnt);
Al Viro921a1652012-07-20 01:15:31 +04003364 path_put(path);
3365}
3366EXPORT_SYMBOL(done_path_create);
3367
Jeff Layton1ac12b42012-12-11 12:10:06 -05003368struct dentry *user_path_create(int dfd, const char __user *pathname,
3369 struct path *path, unsigned int lookup_flags)
Al Virodae6ad82011-06-26 11:50:15 -04003370{
Jeff Layton91a27b22012-10-10 15:25:28 -04003371 struct filename *tmp = getname(pathname);
Al Virodae6ad82011-06-26 11:50:15 -04003372 struct dentry *res;
3373 if (IS_ERR(tmp))
3374 return ERR_CAST(tmp);
Jeff Layton1ac12b42012-12-11 12:10:06 -05003375 res = kern_path_create(dfd, tmp->name, path, lookup_flags);
Al Virodae6ad82011-06-26 11:50:15 -04003376 putname(tmp);
3377 return res;
3378}
3379EXPORT_SYMBOL(user_path_create);
3380
Al Viro1a67aaf2011-07-26 01:52:52 -04003381int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003383 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384
3385 if (error)
3386 return error;
3387
Eric W. Biederman975d6b32011-11-13 12:16:43 -08003388 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 return -EPERM;
3390
Al Viroacfa4382008-12-04 10:06:33 -05003391 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 return -EPERM;
3393
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07003394 error = devcgroup_inode_mknod(mode, dev);
3395 if (error)
3396 return error;
3397
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 error = security_inode_mknod(dir, dentry, mode, dev);
3399 if (error)
3400 return error;
3401
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003403 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003404 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 return error;
3406}
3407
Al Virof69aac02011-07-26 04:31:40 -04003408static int may_mknod(umode_t mode)
Dave Hansen463c3192008-02-15 14:37:57 -08003409{
3410 switch (mode & S_IFMT) {
3411 case S_IFREG:
3412 case S_IFCHR:
3413 case S_IFBLK:
3414 case S_IFIFO:
3415 case S_IFSOCK:
3416 case 0: /* zero mode translates to S_IFREG */
3417 return 0;
3418 case S_IFDIR:
3419 return -EPERM;
3420 default:
3421 return -EINVAL;
3422 }
3423}
3424
Al Viro8208a222011-07-25 17:32:17 -04003425SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003426 unsigned, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427{
Al Viro2ad94ae2008-07-21 09:32:51 -04003428 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003429 struct path path;
3430 int error;
Jeff Layton972567f2012-12-20 16:00:10 -05003431 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432
Al Viro8e4bfca2012-07-20 01:17:26 +04003433 error = may_mknod(mode);
3434 if (error)
3435 return error;
Jeff Layton972567f2012-12-20 16:00:10 -05003436retry:
3437 dentry = user_path_create(dfd, filename, &path, lookup_flags);
Al Virodae6ad82011-06-26 11:50:15 -04003438 if (IS_ERR(dentry))
3439 return PTR_ERR(dentry);
Al Viro2ad94ae2008-07-21 09:32:51 -04003440
Al Virodae6ad82011-06-26 11:50:15 -04003441 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003442 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003443 error = security_path_mknod(&path, dentry, mode, dev);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003444 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04003445 goto out;
Dave Hansen463c3192008-02-15 14:37:57 -08003446 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 case 0: case S_IFREG:
Al Viro312b63f2012-06-10 18:09:36 -04003448 error = vfs_create(path.dentry->d_inode,dentry,mode,true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 break;
3450 case S_IFCHR: case S_IFBLK:
Al Virodae6ad82011-06-26 11:50:15 -04003451 error = vfs_mknod(path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 new_decode_dev(dev));
3453 break;
3454 case S_IFIFO: case S_IFSOCK:
Al Virodae6ad82011-06-26 11:50:15 -04003455 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 }
Al Viroa8104a92012-07-20 02:25:00 +04003458out:
Al Viro921a1652012-07-20 01:15:31 +04003459 done_path_create(&path, dentry);
Jeff Layton972567f2012-12-20 16:00:10 -05003460 if (retry_estale(error, lookup_flags)) {
3461 lookup_flags |= LOOKUP_REVAL;
3462 goto retry;
3463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 return error;
3465}
3466
Al Viro8208a222011-07-25 17:32:17 -04003467SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003468{
3469 return sys_mknodat(AT_FDCWD, filename, mode, dev);
3470}
3471
Al Viro18bb1db2011-07-26 01:41:39 -04003472int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003474 int error = may_create(dir, dentry);
Al Viro8de52772012-02-06 12:45:27 -05003475 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476
3477 if (error)
3478 return error;
3479
Al Viroacfa4382008-12-04 10:06:33 -05003480 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 return -EPERM;
3482
3483 mode &= (S_IRWXUGO|S_ISVTX);
3484 error = security_inode_mkdir(dir, dentry, mode);
3485 if (error)
3486 return error;
3487
Al Viro8de52772012-02-06 12:45:27 -05003488 if (max_links && dir->i_nlink >= max_links)
3489 return -EMLINK;
3490
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003492 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003493 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 return error;
3495}
3496
Al Viroa218d0f2011-11-21 14:59:34 -05003497SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498{
Dave Hansen6902d922006-09-30 23:29:01 -07003499 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003500 struct path path;
3501 int error;
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003502 unsigned int lookup_flags = LOOKUP_DIRECTORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003504retry:
3505 dentry = user_path_create(dfd, pathname, &path, lookup_flags);
Dave Hansen6902d922006-09-30 23:29:01 -07003506 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003507 return PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003508
Al Virodae6ad82011-06-26 11:50:15 -04003509 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003510 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003511 error = security_path_mkdir(&path, dentry, mode);
Al Viroa8104a92012-07-20 02:25:00 +04003512 if (!error)
3513 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
Al Viro921a1652012-07-20 01:15:31 +04003514 done_path_create(&path, dentry);
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003515 if (retry_estale(error, lookup_flags)) {
3516 lookup_flags |= LOOKUP_REVAL;
3517 goto retry;
3518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 return error;
3520}
3521
Al Viroa218d0f2011-11-21 14:59:34 -05003522SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003523{
3524 return sys_mkdirat(AT_FDCWD, pathname, mode);
3525}
3526
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527/*
Sage Weila71905f2011-05-24 13:06:08 -07003528 * The dentry_unhash() helper will try to drop the dentry early: we
J. Bruce Fieldsc0d02592012-02-15 11:48:40 -05003529 * should have a usage count of 1 if we're the only user of this
Sage Weila71905f2011-05-24 13:06:08 -07003530 * dentry, and if that is true (possibly after pruning the dcache),
3531 * then we drop the dentry now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 *
3533 * A low-level filesystem can, if it choses, legally
3534 * do a
3535 *
3536 * if (!d_unhashed(dentry))
3537 * return -EBUSY;
3538 *
3539 * if it cannot handle the case of removing a directory
3540 * that is still in use by something else..
3541 */
3542void dentry_unhash(struct dentry *dentry)
3543{
Vasily Averindc168422006-12-06 20:37:07 -08003544 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 spin_lock(&dentry->d_lock);
Waiman Long98474232013-08-28 18:24:59 -07003546 if (dentry->d_lockref.count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 __d_drop(dentry);
3548 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549}
3550
3551int vfs_rmdir(struct inode *dir, struct dentry *dentry)
3552{
3553 int error = may_delete(dir, dentry, 1);
3554
3555 if (error)
3556 return error;
3557
Al Viroacfa4382008-12-04 10:06:33 -05003558 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 return -EPERM;
3560
Al Viro1d2ef592011-09-14 18:55:41 +01003561 dget(dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003562 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563
Sage Weil912dbc12011-05-24 13:06:11 -07003564 error = -EBUSY;
3565 if (d_mountpoint(dentry))
3566 goto out;
3567
3568 error = security_inode_rmdir(dir, dentry);
3569 if (error)
3570 goto out;
3571
Sage Weil3cebde22011-05-29 21:20:59 -07003572 shrink_dcache_parent(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003573 error = dir->i_op->rmdir(dir, dentry);
3574 if (error)
3575 goto out;
3576
3577 dentry->d_inode->i_flags |= S_DEAD;
3578 dont_mount(dentry);
3579
3580out:
3581 mutex_unlock(&dentry->d_inode->i_mutex);
Al Viro1d2ef592011-09-14 18:55:41 +01003582 dput(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003583 if (!error)
3584 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 return error;
3586}
3587
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003588static long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589{
3590 int error = 0;
Jeff Layton91a27b22012-10-10 15:25:28 -04003591 struct filename *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 struct dentry *dentry;
3593 struct nameidata nd;
Jeff Laytonc6ee9202012-12-20 16:28:33 -05003594 unsigned int lookup_flags = 0;
3595retry:
3596 name = user_path_parent(dfd, pathname, &nd, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04003597 if (IS_ERR(name))
3598 return PTR_ERR(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599
3600 switch(nd.last_type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003601 case LAST_DOTDOT:
3602 error = -ENOTEMPTY;
3603 goto exit1;
3604 case LAST_DOT:
3605 error = -EINVAL;
3606 goto exit1;
3607 case LAST_ROOT:
3608 error = -EBUSY;
3609 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003611
3612 nd.flags &= ~LOOKUP_PARENT;
Jan Karac30dabf2012-06-12 16:20:30 +02003613 error = mnt_want_write(nd.path.mnt);
3614 if (error)
3615 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003616
Jan Blunck4ac91372008-02-14 19:34:32 -08003617 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08003618 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003620 if (IS_ERR(dentry))
3621 goto exit2;
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04003622 if (!dentry->d_inode) {
3623 error = -ENOENT;
3624 goto exit3;
3625 }
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003626 error = security_path_rmdir(&nd.path, dentry);
3627 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02003628 goto exit3;
Jan Blunck4ac91372008-02-14 19:34:32 -08003629 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
Dave Hansen06227532008-02-15 14:37:34 -08003630exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07003631 dput(dentry);
3632exit2:
Jan Blunck4ac91372008-02-14 19:34:32 -08003633 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Karac30dabf2012-06-12 16:20:30 +02003634 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003636 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 putname(name);
Jeff Laytonc6ee9202012-12-20 16:28:33 -05003638 if (retry_estale(error, lookup_flags)) {
3639 lookup_flags |= LOOKUP_REVAL;
3640 goto retry;
3641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 return error;
3643}
3644
Heiko Carstens3cdad422009-01-14 14:14:22 +01003645SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003646{
3647 return do_rmdir(AT_FDCWD, pathname);
3648}
3649
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650int vfs_unlink(struct inode *dir, struct dentry *dentry)
3651{
3652 int error = may_delete(dir, dentry, 0);
3653
3654 if (error)
3655 return error;
3656
Al Viroacfa4382008-12-04 10:06:33 -05003657 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 return -EPERM;
3659
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003660 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 if (d_mountpoint(dentry))
3662 error = -EBUSY;
3663 else {
3664 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05003665 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 error = dir->i_op->unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05003667 if (!error)
Al Virod83c49f2010-04-30 17:17:09 -04003668 dont_mount(dentry);
Al Virobec10522010-03-03 14:12:08 -05003669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003671 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672
3673 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
3674 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
Jan Karaece95912008-02-06 01:37:13 -08003675 fsnotify_link_count(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 }
Robert Love0eeca282005-07-12 17:06:03 -04003678
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 return error;
3680}
3681
3682/*
3683 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003684 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 * writeout happening, and we don't want to prevent access to the directory
3686 * while waiting on the I/O.
3687 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003688static long do_unlinkat(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689{
Al Viro2ad94ae2008-07-21 09:32:51 -04003690 int error;
Jeff Layton91a27b22012-10-10 15:25:28 -04003691 struct filename *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 struct dentry *dentry;
3693 struct nameidata nd;
3694 struct inode *inode = NULL;
Jeff Layton5d18f812012-12-20 16:38:04 -05003695 unsigned int lookup_flags = 0;
3696retry:
3697 name = user_path_parent(dfd, pathname, &nd, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04003698 if (IS_ERR(name))
3699 return PTR_ERR(name);
Al Viro2ad94ae2008-07-21 09:32:51 -04003700
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 error = -EISDIR;
3702 if (nd.last_type != LAST_NORM)
3703 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003704
3705 nd.flags &= ~LOOKUP_PARENT;
Jan Karac30dabf2012-06-12 16:20:30 +02003706 error = mnt_want_write(nd.path.mnt);
3707 if (error)
3708 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003709
Jan Blunck4ac91372008-02-14 19:34:32 -08003710 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08003711 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 error = PTR_ERR(dentry);
3713 if (!IS_ERR(dentry)) {
3714 /* Why not before? Because we want correct error value */
Török Edwin50338b82011-06-16 00:06:14 +03003715 if (nd.last.name[nd.last.len])
3716 goto slashes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 inode = dentry->d_inode;
Török Edwin50338b82011-06-16 00:06:14 +03003718 if (!inode)
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04003719 goto slashes;
3720 ihold(inode);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003721 error = security_path_unlink(&nd.path, dentry);
3722 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02003723 goto exit2;
Jan Blunck4ac91372008-02-14 19:34:32 -08003724 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
Jan Karac30dabf2012-06-12 16:20:30 +02003725exit2:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 dput(dentry);
3727 }
Jan Blunck4ac91372008-02-14 19:34:32 -08003728 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 if (inode)
3730 iput(inode); /* truncate the inode here */
Jan Karac30dabf2012-06-12 16:20:30 +02003731 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003733 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 putname(name);
Jeff Layton5d18f812012-12-20 16:38:04 -05003735 if (retry_estale(error, lookup_flags)) {
3736 lookup_flags |= LOOKUP_REVAL;
3737 inode = NULL;
3738 goto retry;
3739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 return error;
3741
3742slashes:
3743 error = !dentry->d_inode ? -ENOENT :
3744 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
3745 goto exit2;
3746}
3747
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003748SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003749{
3750 if ((flag & ~AT_REMOVEDIR) != 0)
3751 return -EINVAL;
3752
3753 if (flag & AT_REMOVEDIR)
3754 return do_rmdir(dfd, pathname);
3755
3756 return do_unlinkat(dfd, pathname);
3757}
3758
Heiko Carstens3480b252009-01-14 14:14:16 +01003759SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003760{
3761 return do_unlinkat(AT_FDCWD, pathname);
3762}
3763
Miklos Szeredidb2e7472008-06-24 16:50:16 +02003764int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003766 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767
3768 if (error)
3769 return error;
3770
Al Viroacfa4382008-12-04 10:06:33 -05003771 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 return -EPERM;
3773
3774 error = security_inode_symlink(dir, dentry, oldname);
3775 if (error)
3776 return error;
3777
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003779 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003780 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 return error;
3782}
3783
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003784SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
3785 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786{
Al Viro2ad94ae2008-07-21 09:32:51 -04003787 int error;
Jeff Layton91a27b22012-10-10 15:25:28 -04003788 struct filename *from;
Dave Hansen6902d922006-09-30 23:29:01 -07003789 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003790 struct path path;
Jeff Laytonf46d3562012-12-11 12:10:08 -05003791 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792
3793 from = getname(oldname);
Al Viro2ad94ae2008-07-21 09:32:51 -04003794 if (IS_ERR(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 return PTR_ERR(from);
Jeff Laytonf46d3562012-12-11 12:10:08 -05003796retry:
3797 dentry = user_path_create(newdfd, newname, &path, lookup_flags);
Dave Hansen6902d922006-09-30 23:29:01 -07003798 error = PTR_ERR(dentry);
3799 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003800 goto out_putname;
Dave Hansen6902d922006-09-30 23:29:01 -07003801
Jeff Layton91a27b22012-10-10 15:25:28 -04003802 error = security_path_symlink(&path, dentry, from->name);
Al Viroa8104a92012-07-20 02:25:00 +04003803 if (!error)
Jeff Layton91a27b22012-10-10 15:25:28 -04003804 error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
Al Viro921a1652012-07-20 01:15:31 +04003805 done_path_create(&path, dentry);
Jeff Laytonf46d3562012-12-11 12:10:08 -05003806 if (retry_estale(error, lookup_flags)) {
3807 lookup_flags |= LOOKUP_REVAL;
3808 goto retry;
3809 }
Dave Hansen6902d922006-09-30 23:29:01 -07003810out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 putname(from);
3812 return error;
3813}
3814
Heiko Carstens3480b252009-01-14 14:14:16 +01003815SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003816{
3817 return sys_symlinkat(oldname, AT_FDCWD, newname);
3818}
3819
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
3821{
3822 struct inode *inode = old_dentry->d_inode;
Al Viro8de52772012-02-06 12:45:27 -05003823 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 int error;
3825
3826 if (!inode)
3827 return -ENOENT;
3828
Miklos Szeredia95164d2008-07-30 15:08:48 +02003829 error = may_create(dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 if (error)
3831 return error;
3832
3833 if (dir->i_sb != inode->i_sb)
3834 return -EXDEV;
3835
3836 /*
3837 * A link to an append-only or immutable file cannot be created.
3838 */
3839 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3840 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05003841 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003843 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 return -EPERM;
3845
3846 error = security_inode_link(old_dentry, dir, new_dentry);
3847 if (error)
3848 return error;
3849
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003850 mutex_lock(&inode->i_mutex);
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05303851 /* Make sure we don't allow creating hardlink to an unlinked file */
Al Virof4e0c302013-06-11 08:34:36 +04003852 if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05303853 error = -ENOENT;
Al Viro8de52772012-02-06 12:45:27 -05003854 else if (max_links && inode->i_nlink >= max_links)
3855 error = -EMLINK;
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05303856 else
3857 error = dir->i_op->link(old_dentry, dir, new_dentry);
Al Virof4e0c302013-06-11 08:34:36 +04003858
3859 if (!error && (inode->i_state & I_LINKABLE)) {
3860 spin_lock(&inode->i_lock);
3861 inode->i_state &= ~I_LINKABLE;
3862 spin_unlock(&inode->i_lock);
3863 }
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003864 mutex_unlock(&inode->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07003865 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003866 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 return error;
3868}
3869
3870/*
3871 * Hardlinks are often used in delicate situations. We avoid
3872 * security-related surprises by not following symlinks on the
3873 * newname. --KAB
3874 *
3875 * We don't follow them on the oldname either to be compatible
3876 * with linux 2.0, and to avoid hard-linking to directories
3877 * and other special files. --ADM
3878 */
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003879SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
3880 int, newdfd, const char __user *, newname, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881{
3882 struct dentry *new_dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003883 struct path old_path, new_path;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303884 int how = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303887 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003888 return -EINVAL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303889 /*
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07003890 * To use null names we require CAP_DAC_READ_SEARCH
3891 * This ensures that not everyone will be able to create
3892 * handlink using the passed filedescriptor.
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303893 */
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07003894 if (flags & AT_EMPTY_PATH) {
3895 if (!capable(CAP_DAC_READ_SEARCH))
3896 return -ENOENT;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303897 how = LOOKUP_EMPTY;
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07003898 }
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003899
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303900 if (flags & AT_SYMLINK_FOLLOW)
3901 how |= LOOKUP_FOLLOW;
Jeff Layton442e31c2012-12-20 16:15:38 -05003902retry:
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303903 error = user_path_at(olddfd, oldname, how, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04003905 return error;
3906
Jeff Layton442e31c2012-12-20 16:15:38 -05003907 new_dentry = user_path_create(newdfd, newname, &new_path,
3908 (how & LOOKUP_REVAL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003910 if (IS_ERR(new_dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003911 goto out;
3912
3913 error = -EXDEV;
3914 if (old_path.mnt != new_path.mnt)
3915 goto out_dput;
Kees Cook800179c2012-07-25 17:29:07 -07003916 error = may_linkat(&old_path);
3917 if (unlikely(error))
3918 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04003919 error = security_path_link(old_path.dentry, &new_path, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003920 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04003921 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04003922 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
Dave Hansen75c3f292008-02-15 14:37:45 -08003923out_dput:
Al Viro921a1652012-07-20 01:15:31 +04003924 done_path_create(&new_path, new_dentry);
Jeff Layton442e31c2012-12-20 16:15:38 -05003925 if (retry_estale(error, how)) {
3926 how |= LOOKUP_REVAL;
3927 goto retry;
3928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929out:
Al Viro2d8f3032008-07-22 09:59:21 -04003930 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931
3932 return error;
3933}
3934
Heiko Carstens3480b252009-01-14 14:14:16 +01003935SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003936{
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003937 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003938}
3939
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940/*
3941 * The worst of all namespace operations - renaming directory. "Perverted"
3942 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
3943 * Problems:
3944 * a) we can get into loop creation. Check is done in is_subdir().
3945 * b) race potential - two innocent renames can create a loop together.
3946 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08003947 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 * story.
3949 * c) we have to lock _three_ objects - parents and victim (if it exists).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003950 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 * whether the target exists). Solution: try to be smart with locking
3952 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08003953 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 * move will be locked. Thus we can rank directories by the tree
3955 * (ancestors first) and rank all non-directories after them.
3956 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08003957 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 * HOWEVER, it relies on the assumption that any object with ->lookup()
3959 * has no more than 1 dentry. If "hybrid" objects will ever appear,
3960 * we'd better make sure that there's no link(2) for them.
Sage Weile4eaac02011-05-24 13:06:07 -07003961 * d) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003962 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05003964 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 * locking].
3966 */
Adrian Bunk75c96f82005-05-05 16:16:09 -07003967static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
3968 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969{
3970 int error = 0;
Sage Weil9055cba2011-05-24 13:06:12 -07003971 struct inode *target = new_dentry->d_inode;
Al Viro8de52772012-02-06 12:45:27 -05003972 unsigned max_links = new_dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973
3974 /*
3975 * If we are going to change the parent - check write permissions,
3976 * we'll need to flip '..'.
3977 */
3978 if (new_dir != old_dir) {
Al Virof419a2e2008-07-22 00:07:17 -04003979 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 if (error)
3981 return error;
3982 }
3983
3984 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3985 if (error)
3986 return error;
3987
Al Viro1d2ef592011-09-14 18:55:41 +01003988 dget(new_dentry);
Al Virod83c49f2010-04-30 17:17:09 -04003989 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003990 mutex_lock(&target->i_mutex);
Sage Weil9055cba2011-05-24 13:06:12 -07003991
3992 error = -EBUSY;
3993 if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
3994 goto out;
3995
Al Viro8de52772012-02-06 12:45:27 -05003996 error = -EMLINK;
3997 if (max_links && !target && new_dir != old_dir &&
3998 new_dir->i_nlink >= max_links)
3999 goto out;
4000
Sage Weil3cebde22011-05-29 21:20:59 -07004001 if (target)
4002 shrink_dcache_parent(new_dentry);
Sage Weil9055cba2011-05-24 13:06:12 -07004003 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
4004 if (error)
4005 goto out;
4006
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 if (target) {
Sage Weil9055cba2011-05-24 13:06:12 -07004008 target->i_flags |= S_DEAD;
4009 dont_mount(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 }
Sage Weil9055cba2011-05-24 13:06:12 -07004011out:
4012 if (target)
4013 mutex_unlock(&target->i_mutex);
Al Viro1d2ef592011-09-14 18:55:41 +01004014 dput(new_dentry);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07004015 if (!error)
Mark Fasheh349457c2006-09-08 14:22:21 -07004016 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
4017 d_move(old_dentry,new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 return error;
4019}
4020
Adrian Bunk75c96f82005-05-05 16:16:09 -07004021static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
4022 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023{
Sage Weil51892bb2011-05-24 13:06:13 -07004024 struct inode *target = new_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 int error;
4026
4027 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
4028 if (error)
4029 return error;
4030
4031 dget(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004033 mutex_lock(&target->i_mutex);
Sage Weil51892bb2011-05-24 13:06:13 -07004034
4035 error = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
Sage Weil51892bb2011-05-24 13:06:13 -07004037 goto out;
4038
4039 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
4040 if (error)
4041 goto out;
4042
4043 if (target)
4044 dont_mount(new_dentry);
4045 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
4046 d_move(old_dentry, new_dentry);
4047out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004049 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 dput(new_dentry);
4051 return error;
4052}
4053
4054int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
4055 struct inode *new_dir, struct dentry *new_dentry)
4056{
4057 int error;
4058 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
Eric Paris59b0df22010-02-08 12:53:52 -05004059 const unsigned char *old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060
4061 if (old_dentry->d_inode == new_dentry->d_inode)
4062 return 0;
4063
4064 error = may_delete(old_dir, old_dentry, is_dir);
4065 if (error)
4066 return error;
4067
4068 if (!new_dentry->d_inode)
Miklos Szeredia95164d2008-07-30 15:08:48 +02004069 error = may_create(new_dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 else
4071 error = may_delete(new_dir, new_dentry, is_dir);
4072 if (error)
4073 return error;
4074
Al Viroacfa4382008-12-04 10:06:33 -05004075 if (!old_dir->i_op->rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 return -EPERM;
4077
Robert Love0eeca282005-07-12 17:06:03 -04004078 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
4079
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 if (is_dir)
4081 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
4082 else
4083 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
Al Viro123df292009-12-25 04:57:57 -05004084 if (!error)
4085 fsnotify_move(old_dir, new_dir, old_name, is_dir,
Al Viro5a190ae2007-06-07 12:19:32 -04004086 new_dentry->d_inode, old_dentry);
Robert Love0eeca282005-07-12 17:06:03 -04004087 fsnotify_oldname_free(old_name);
4088
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 return error;
4090}
4091
Heiko Carstens2e4d0922009-01-14 14:14:31 +01004092SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4093 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094{
Al Viro2ad94ae2008-07-21 09:32:51 -04004095 struct dentry *old_dir, *new_dir;
4096 struct dentry *old_dentry, *new_dentry;
4097 struct dentry *trap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 struct nameidata oldnd, newnd;
Jeff Layton91a27b22012-10-10 15:25:28 -04004099 struct filename *from;
4100 struct filename *to;
Jeff Laytonc6a94282012-12-11 12:10:10 -05004101 unsigned int lookup_flags = 0;
4102 bool should_retry = false;
Al Viro2ad94ae2008-07-21 09:32:51 -04004103 int error;
Jeff Laytonc6a94282012-12-11 12:10:10 -05004104retry:
4105 from = user_path_parent(olddfd, oldname, &oldnd, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04004106 if (IS_ERR(from)) {
4107 error = PTR_ERR(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 goto exit;
Jeff Layton91a27b22012-10-10 15:25:28 -04004109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110
Jeff Laytonc6a94282012-12-11 12:10:10 -05004111 to = user_path_parent(newdfd, newname, &newnd, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04004112 if (IS_ERR(to)) {
4113 error = PTR_ERR(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 goto exit1;
Jeff Layton91a27b22012-10-10 15:25:28 -04004115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116
4117 error = -EXDEV;
Jan Blunck4ac91372008-02-14 19:34:32 -08004118 if (oldnd.path.mnt != newnd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 goto exit2;
4120
Jan Blunck4ac91372008-02-14 19:34:32 -08004121 old_dir = oldnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 error = -EBUSY;
4123 if (oldnd.last_type != LAST_NORM)
4124 goto exit2;
4125
Jan Blunck4ac91372008-02-14 19:34:32 -08004126 new_dir = newnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 if (newnd.last_type != LAST_NORM)
4128 goto exit2;
4129
Jan Karac30dabf2012-06-12 16:20:30 +02004130 error = mnt_want_write(oldnd.path.mnt);
4131 if (error)
4132 goto exit2;
4133
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004134 oldnd.flags &= ~LOOKUP_PARENT;
4135 newnd.flags &= ~LOOKUP_PARENT;
OGAWA Hirofumi4e9ed2f2008-10-16 07:50:29 +09004136 newnd.flags |= LOOKUP_RENAME_TARGET;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004137
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 trap = lock_rename(new_dir, old_dir);
4139
Christoph Hellwig49705b72005-11-08 21:35:06 -08004140 old_dentry = lookup_hash(&oldnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 error = PTR_ERR(old_dentry);
4142 if (IS_ERR(old_dentry))
4143 goto exit3;
4144 /* source must exist */
4145 error = -ENOENT;
4146 if (!old_dentry->d_inode)
4147 goto exit4;
4148 /* unless the source is a directory trailing slashes give -ENOTDIR */
4149 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
4150 error = -ENOTDIR;
4151 if (oldnd.last.name[oldnd.last.len])
4152 goto exit4;
4153 if (newnd.last.name[newnd.last.len])
4154 goto exit4;
4155 }
4156 /* source should not be ancestor of target */
4157 error = -EINVAL;
4158 if (old_dentry == trap)
4159 goto exit4;
Christoph Hellwig49705b72005-11-08 21:35:06 -08004160 new_dentry = lookup_hash(&newnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 error = PTR_ERR(new_dentry);
4162 if (IS_ERR(new_dentry))
4163 goto exit4;
4164 /* target should not be an ancestor of source */
4165 error = -ENOTEMPTY;
4166 if (new_dentry == trap)
4167 goto exit5;
4168
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004169 error = security_path_rename(&oldnd.path, old_dentry,
4170 &newnd.path, new_dentry);
4171 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02004172 goto exit5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 error = vfs_rename(old_dir->d_inode, old_dentry,
4174 new_dir->d_inode, new_dentry);
4175exit5:
4176 dput(new_dentry);
4177exit4:
4178 dput(old_dentry);
4179exit3:
4180 unlock_rename(new_dir, old_dir);
Jan Karac30dabf2012-06-12 16:20:30 +02004181 mnt_drop_write(oldnd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182exit2:
Jeff Laytonc6a94282012-12-11 12:10:10 -05004183 if (retry_estale(error, lookup_flags))
4184 should_retry = true;
Jan Blunck1d957f92008-02-14 19:34:35 -08004185 path_put(&newnd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04004186 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08004188 path_put(&oldnd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 putname(from);
Jeff Laytonc6a94282012-12-11 12:10:10 -05004190 if (should_retry) {
4191 should_retry = false;
4192 lookup_flags |= LOOKUP_REVAL;
4193 goto retry;
4194 }
Al Viro2ad94ae2008-07-21 09:32:51 -04004195exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 return error;
4197}
4198
Heiko Carstensa26eab22009-01-14 14:14:17 +01004199SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004200{
4201 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
4202}
4203
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
4205{
4206 int len;
4207
4208 len = PTR_ERR(link);
4209 if (IS_ERR(link))
4210 goto out;
4211
4212 len = strlen(link);
4213 if (len > (unsigned) buflen)
4214 len = buflen;
4215 if (copy_to_user(buffer, link, len))
4216 len = -EFAULT;
4217out:
4218 return len;
4219}
4220
4221/*
4222 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
4223 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
4224 * using) it for any given inode is up to filesystem.
4225 */
4226int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4227{
4228 struct nameidata nd;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004229 void *cookie;
Marcin Slusarz694a1762008-06-09 16:40:37 -07004230 int res;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004231
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 nd.depth = 0;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004233 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
Marcin Slusarz694a1762008-06-09 16:40:37 -07004234 if (IS_ERR(cookie))
4235 return PTR_ERR(cookie);
4236
4237 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
4238 if (dentry->d_inode->i_op->put_link)
4239 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
4240 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241}
4242
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243/* get the link contents into pagecache */
4244static char *page_getlink(struct dentry * dentry, struct page **ppage)
4245{
Duane Griffinebd09ab2008-12-19 20:47:12 +00004246 char *kaddr;
4247 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 struct address_space *mapping = dentry->d_inode->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -07004249 page = read_mapping_page(mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 if (IS_ERR(page))
Nick Piggin6fe69002007-05-06 14:49:04 -07004251 return (char*)page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 *ppage = page;
Duane Griffinebd09ab2008-12-19 20:47:12 +00004253 kaddr = kmap(page);
4254 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
4255 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256}
4257
4258int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4259{
4260 struct page *page = NULL;
4261 char *s = page_getlink(dentry, &page);
4262 int res = vfs_readlink(dentry,buffer,buflen,s);
4263 if (page) {
4264 kunmap(page);
4265 page_cache_release(page);
4266 }
4267 return res;
4268}
4269
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004270void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004272 struct page *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 nd_set_link(nd, page_getlink(dentry, &page));
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004274 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275}
4276
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004277void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004279 struct page *page = cookie;
4280
4281 if (page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 kunmap(page);
4283 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 }
4285}
4286
Nick Piggin54566b22009-01-04 12:00:53 -08004287/*
4288 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
4289 */
4290int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291{
4292 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004293 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07004294 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08004295 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 char *kaddr;
Nick Piggin54566b22009-01-04 12:00:53 -08004297 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
4298 if (nofs)
4299 flags |= AOP_FLAG_NOFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300
NeilBrown7e53cac2006-03-25 03:07:57 -08004301retry:
Nick Pigginafddba42007-10-16 01:25:01 -07004302 err = pagecache_write_begin(NULL, mapping, 0, len-1,
Nick Piggin54566b22009-01-04 12:00:53 -08004303 flags, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07004305 goto fail;
4306
Cong Wange8e3c3d2011-11-25 23:14:27 +08004307 kaddr = kmap_atomic(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 memcpy(kaddr, symname, len-1);
Cong Wange8e3c3d2011-11-25 23:14:27 +08004309 kunmap_atomic(kaddr);
Nick Pigginafddba42007-10-16 01:25:01 -07004310
4311 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4312 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 if (err < 0)
4314 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07004315 if (err < len-1)
4316 goto retry;
4317
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 mark_inode_dirty(inode);
4319 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320fail:
4321 return err;
4322}
4323
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004324int page_symlink(struct inode *inode, const char *symname, int len)
4325{
4326 return __page_symlink(inode, symname, len,
Nick Piggin54566b22009-01-04 12:00:53 -08004327 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004328}
4329
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08004330const struct inode_operations page_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 .readlink = generic_readlink,
4332 .follow_link = page_follow_link_light,
4333 .put_link = page_put_link,
4334};
4335
Al Viro2d8f3032008-07-22 09:59:21 -04004336EXPORT_SYMBOL(user_path_at);
David Howellscc53ce52011-01-14 18:45:26 +00004337EXPORT_SYMBOL(follow_down_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338EXPORT_SYMBOL(follow_down);
4339EXPORT_SYMBOL(follow_up);
Al Virof6d2ac52012-08-26 12:55:54 -04004340EXPORT_SYMBOL(get_write_access); /* nfsd */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342EXPORT_SYMBOL(lookup_one_len);
4343EXPORT_SYMBOL(page_follow_link_light);
4344EXPORT_SYMBOL(page_put_link);
4345EXPORT_SYMBOL(page_readlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004346EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347EXPORT_SYMBOL(page_symlink);
4348EXPORT_SYMBOL(page_symlink_inode_operations);
Al Virod1811462008-08-02 00:49:18 -04004349EXPORT_SYMBOL(kern_path);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07004350EXPORT_SYMBOL(vfs_path_lookup);
Al Virof419a2e2008-07-22 00:07:17 -04004351EXPORT_SYMBOL(inode_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352EXPORT_SYMBOL(unlock_rename);
4353EXPORT_SYMBOL(vfs_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354EXPORT_SYMBOL(vfs_link);
4355EXPORT_SYMBOL(vfs_mkdir);
4356EXPORT_SYMBOL(vfs_mknod);
4357EXPORT_SYMBOL(generic_permission);
4358EXPORT_SYMBOL(vfs_readlink);
4359EXPORT_SYMBOL(vfs_rename);
4360EXPORT_SYMBOL(vfs_rmdir);
4361EXPORT_SYMBOL(vfs_symlink);
4362EXPORT_SYMBOL(vfs_unlink);
4363EXPORT_SYMBOL(dentry_unhash);
4364EXPORT_SYMBOL(generic_readlink);