blob: 409a441ba2ae1c3b52f2b6d87563a9b0e80125b4 [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
Arjan van de Venf1662352006-01-14 13:21:31 -0800663static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
Nick Piggin31e6b012011-01-07 17:49:52 +1100665 int ret;
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 if (IS_ERR(link))
668 goto fail;
669
670 if (*link == '/') {
Al Viro2a737872009-04-07 11:49:53 -0400671 set_root(nd);
Jan Blunck1d957f92008-02-14 19:34:35 -0800672 path_put(&nd->path);
Al Viro2a737872009-04-07 11:49:53 -0400673 nd->path = nd->root;
674 path_get(&nd->root);
Al Viro16c2cd72011-02-22 15:50:10 -0500675 nd->flags |= LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100677 nd->inode = nd->path.dentry->d_inode;
Christoph Hellwigb4091d52008-11-05 15:07:21 +0100678
Nick Piggin31e6b012011-01-07 17:49:52 +1100679 ret = link_path_walk(link, nd);
680 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681fail:
Jan Blunck1d957f92008-02-14 19:34:35 -0800682 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return PTR_ERR(link);
684}
685
Jan Blunck1d957f92008-02-14 19:34:35 -0800686static void path_put_conditional(struct path *path, struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700687{
688 dput(path->dentry);
Jan Blunck4ac91372008-02-14 19:34:32 -0800689 if (path->mnt != nd->path.mnt)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700690 mntput(path->mnt);
691}
692
Nick Piggin7b9337a2011-01-14 08:42:43 +0000693static inline void path_to_nameidata(const struct path *path,
694 struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700695{
Nick Piggin31e6b012011-01-07 17:49:52 +1100696 if (!(nd->flags & LOOKUP_RCU)) {
697 dput(nd->path.dentry);
698 if (nd->path.mnt != path->mnt)
699 mntput(nd->path.mnt);
Huang Shijie9a229682010-04-02 17:37:13 +0800700 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100701 nd->path.mnt = path->mnt;
Jan Blunck4ac91372008-02-14 19:34:32 -0800702 nd->path.dentry = path->dentry;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700703}
704
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400705/*
706 * Helper to directly jump to a known parsed path from ->follow_link,
707 * caller must have taken a reference to path beforehand.
708 */
709void nd_jump_link(struct nameidata *nd, struct path *path)
710{
711 path_put(&nd->path);
712
713 nd->path = *path;
714 nd->inode = nd->path.dentry->d_inode;
715 nd->flags |= LOOKUP_JUMPED;
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400716}
717
Al Viro574197e2011-03-14 22:20:34 -0400718static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
719{
720 struct inode *inode = link->dentry->d_inode;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400721 if (inode->i_op->put_link)
Al Viro574197e2011-03-14 22:20:34 -0400722 inode->i_op->put_link(link->dentry, nd, cookie);
723 path_put(link);
724}
725
Linus Torvalds561ec642012-10-26 10:05:07 -0700726int sysctl_protected_symlinks __read_mostly = 0;
727int sysctl_protected_hardlinks __read_mostly = 0;
Kees Cook800179c2012-07-25 17:29:07 -0700728
729/**
730 * may_follow_link - Check symlink following for unsafe situations
731 * @link: The path of the symlink
Randy Dunlap55852632012-08-18 17:39:25 -0700732 * @nd: nameidata pathwalk data
Kees Cook800179c2012-07-25 17:29:07 -0700733 *
734 * In the case of the sysctl_protected_symlinks sysctl being enabled,
735 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
736 * in a sticky world-writable directory. This is to protect privileged
737 * processes from failing races against path names that may change out
738 * from under them by way of other users creating malicious symlinks.
739 * It will permit symlinks to be followed only when outside a sticky
740 * world-writable directory, or when the uid of the symlink and follower
741 * match, or when the directory owner matches the symlink's owner.
742 *
743 * Returns 0 if following the symlink is allowed, -ve on error.
744 */
745static inline int may_follow_link(struct path *link, struct nameidata *nd)
746{
747 const struct inode *inode;
748 const struct inode *parent;
749
750 if (!sysctl_protected_symlinks)
751 return 0;
752
753 /* Allowed if owner and follower match. */
754 inode = link->dentry->d_inode;
Eric W. Biederman81abe272012-08-03 09:38:08 -0700755 if (uid_eq(current_cred()->fsuid, inode->i_uid))
Kees Cook800179c2012-07-25 17:29:07 -0700756 return 0;
757
758 /* Allowed if parent directory not sticky and world-writable. */
759 parent = nd->path.dentry->d_inode;
760 if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
761 return 0;
762
763 /* Allowed if parent directory and link owner match. */
Eric W. Biederman81abe272012-08-03 09:38:08 -0700764 if (uid_eq(parent->i_uid, inode->i_uid))
Kees Cook800179c2012-07-25 17:29:07 -0700765 return 0;
766
Sasha Levinffd8d102012-10-04 19:56:40 -0400767 audit_log_link_denied("follow_link", link);
Kees Cook800179c2012-07-25 17:29:07 -0700768 path_put_conditional(link, nd);
769 path_put(&nd->path);
770 return -EACCES;
771}
772
773/**
774 * safe_hardlink_source - Check for safe hardlink conditions
775 * @inode: the source inode to hardlink from
776 *
777 * Return false if at least one of the following conditions:
778 * - inode is not a regular file
779 * - inode is setuid
780 * - inode is setgid and group-exec
781 * - access failure for read and write
782 *
783 * Otherwise returns true.
784 */
785static bool safe_hardlink_source(struct inode *inode)
786{
787 umode_t mode = inode->i_mode;
788
789 /* Special files should not get pinned to the filesystem. */
790 if (!S_ISREG(mode))
791 return false;
792
793 /* Setuid files should not get pinned to the filesystem. */
794 if (mode & S_ISUID)
795 return false;
796
797 /* Executable setgid files should not get pinned to the filesystem. */
798 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
799 return false;
800
801 /* Hardlinking to unreadable or unwritable sources is dangerous. */
802 if (inode_permission(inode, MAY_READ | MAY_WRITE))
803 return false;
804
805 return true;
806}
807
808/**
809 * may_linkat - Check permissions for creating a hardlink
810 * @link: the source to hardlink from
811 *
812 * Block hardlink when all of:
813 * - sysctl_protected_hardlinks enabled
814 * - fsuid does not match inode
815 * - hardlink source is unsafe (see safe_hardlink_source() above)
816 * - not CAP_FOWNER
817 *
818 * Returns 0 if successful, -ve on error.
819 */
820static int may_linkat(struct path *link)
821{
822 const struct cred *cred;
823 struct inode *inode;
824
825 if (!sysctl_protected_hardlinks)
826 return 0;
827
828 cred = current_cred();
829 inode = link->dentry->d_inode;
830
831 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
832 * otherwise, it must be a safe source.
833 */
Eric W. Biederman81abe272012-08-03 09:38:08 -0700834 if (uid_eq(cred->fsuid, inode->i_uid) || safe_hardlink_source(inode) ||
Kees Cook800179c2012-07-25 17:29:07 -0700835 capable(CAP_FOWNER))
836 return 0;
837
Kees Cooka51d9ea2012-07-25 17:29:08 -0700838 audit_log_link_denied("linkat", link);
Kees Cook800179c2012-07-25 17:29:07 -0700839 return -EPERM;
840}
841
Al Virodef4af32009-12-26 08:37:05 -0500842static __always_inline int
Al Viro574197e2011-03-14 22:20:34 -0400843follow_link(struct path *link, struct nameidata *nd, void **p)
Ian Kent051d3812006-03-27 01:14:53 -0800844{
Nick Piggin7b9337a2011-01-14 08:42:43 +0000845 struct dentry *dentry = link->dentry;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400846 int error;
847 char *s;
Ian Kent051d3812006-03-27 01:14:53 -0800848
Al Viro844a3912011-02-15 00:38:26 -0500849 BUG_ON(nd->flags & LOOKUP_RCU);
850
Al Viro0e794582011-03-16 02:45:02 -0400851 if (link->mnt == nd->path.mnt)
852 mntget(link->mnt);
853
Al Viro6d7b5aa2012-06-10 04:15:17 -0400854 error = -ELOOP;
855 if (unlikely(current->total_link_count >= 40))
856 goto out_put_nd_path;
857
Al Viro574197e2011-03-14 22:20:34 -0400858 cond_resched();
859 current->total_link_count++;
860
Al Viro68ac1232012-03-15 08:21:57 -0400861 touch_atime(link);
Ian Kent051d3812006-03-27 01:14:53 -0800862 nd_set_link(nd, NULL);
863
Al Viro36f3b4f2011-02-22 21:24:38 -0500864 error = security_inode_follow_link(link->dentry, nd);
Al Viro6d7b5aa2012-06-10 04:15:17 -0400865 if (error)
866 goto out_put_nd_path;
Al Viro36f3b4f2011-02-22 21:24:38 -0500867
Al Viro86acdca12009-12-22 23:45:11 -0500868 nd->last_type = LAST_BIND;
Al Virodef4af32009-12-26 08:37:05 -0500869 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
870 error = PTR_ERR(*p);
Al Viro6d7b5aa2012-06-10 04:15:17 -0400871 if (IS_ERR(*p))
Christoph Hellwig408ef012012-06-18 10:47:03 -0400872 goto out_put_nd_path;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400873
874 error = 0;
875 s = nd_get_link(nd);
876 if (s) {
877 error = __vfs_follow_link(nd, s);
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400878 if (unlikely(error))
879 put_link(nd, link, *p);
Ian Kent051d3812006-03-27 01:14:53 -0800880 }
Al Viro6d7b5aa2012-06-10 04:15:17 -0400881
882 return error;
883
884out_put_nd_path:
Arnd Bergmann98f6ef62012-10-11 13:20:00 +0000885 *p = NULL;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400886 path_put(&nd->path);
Al Viro6d7b5aa2012-06-10 04:15:17 -0400887 path_put(link);
Ian Kent051d3812006-03-27 01:14:53 -0800888 return error;
889}
890
Nick Piggin31e6b012011-01-07 17:49:52 +1100891static int follow_up_rcu(struct path *path)
892{
Al Viro0714a532011-11-24 22:19:58 -0500893 struct mount *mnt = real_mount(path->mnt);
894 struct mount *parent;
Nick Piggin31e6b012011-01-07 17:49:52 +1100895 struct dentry *mountpoint;
896
Al Viro0714a532011-11-24 22:19:58 -0500897 parent = mnt->mnt_parent;
898 if (&parent->mnt == path->mnt)
Nick Piggin31e6b012011-01-07 17:49:52 +1100899 return 0;
Al Viroa73324d2011-11-24 22:25:07 -0500900 mountpoint = mnt->mnt_mountpoint;
Nick Piggin31e6b012011-01-07 17:49:52 +1100901 path->dentry = mountpoint;
Al Viro0714a532011-11-24 22:19:58 -0500902 path->mnt = &parent->mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +1100903 return 1;
904}
905
David Howellsf015f1262012-06-25 12:55:28 +0100906/*
907 * follow_up - Find the mountpoint of path's vfsmount
908 *
909 * Given a path, find the mountpoint of its source file system.
910 * Replace @path with the path of the mountpoint in the parent mount.
911 * Up is towards /.
912 *
913 * Return 1 if we went up a level and 0 if we were already at the
914 * root.
915 */
Al Virobab77eb2009-04-18 03:26:48 -0400916int follow_up(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
Al Viro0714a532011-11-24 22:19:58 -0500918 struct mount *mnt = real_mount(path->mnt);
919 struct mount *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 struct dentry *mountpoint;
Nick Piggin99b7db72010-08-18 04:37:39 +1000921
Andi Kleen962830d2012-05-08 13:32:02 +0930922 br_read_lock(&vfsmount_lock);
Al Viro0714a532011-11-24 22:19:58 -0500923 parent = mnt->mnt_parent;
Al Viro3c0a6162012-07-18 17:32:50 +0400924 if (parent == mnt) {
Andi Kleen962830d2012-05-08 13:32:02 +0930925 br_read_unlock(&vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 return 0;
927 }
Al Viro0714a532011-11-24 22:19:58 -0500928 mntget(&parent->mnt);
Al Viroa73324d2011-11-24 22:25:07 -0500929 mountpoint = dget(mnt->mnt_mountpoint);
Andi Kleen962830d2012-05-08 13:32:02 +0930930 br_read_unlock(&vfsmount_lock);
Al Virobab77eb2009-04-18 03:26:48 -0400931 dput(path->dentry);
932 path->dentry = mountpoint;
933 mntput(path->mnt);
Al Viro0714a532011-11-24 22:19:58 -0500934 path->mnt = &parent->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 return 1;
936}
937
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100938/*
David Howells9875cf82011-01-14 18:45:21 +0000939 * Perform an automount
940 * - return -EISDIR to tell follow_managed() to stop and return the path we
941 * were called with.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 */
David Howells9875cf82011-01-14 18:45:21 +0000943static int follow_automount(struct path *path, unsigned flags,
944 bool *need_mntput)
Nick Piggin31e6b012011-01-07 17:49:52 +1100945{
David Howells9875cf82011-01-14 18:45:21 +0000946 struct vfsmount *mnt;
David Howellsea5b7782011-01-14 19:10:03 +0000947 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +1100948
David Howells9875cf82011-01-14 18:45:21 +0000949 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
950 return -EREMOTE;
Al Viro463ffb22005-06-06 13:36:05 -0700951
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200952 /* We don't want to mount if someone's just doing a stat -
953 * unless they're stat'ing a directory and appended a '/' to
954 * the name.
955 *
956 * We do, however, want to mount if someone wants to open or
957 * create a file of any type under the mountpoint, wants to
958 * traverse through the mountpoint or wants to open the
959 * mounted directory. Also, autofs may mark negative dentries
960 * as being automount points. These will need the attentions
961 * of the daemon to instantiate them before they can be used.
David Howells9875cf82011-01-14 18:45:21 +0000962 */
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200963 if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
Linus Torvaldsd94c1772011-09-26 17:44:55 -0700964 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200965 path->dentry->d_inode)
966 return -EISDIR;
967
David Howells9875cf82011-01-14 18:45:21 +0000968 current->total_link_count++;
969 if (current->total_link_count >= 40)
970 return -ELOOP;
971
972 mnt = path->dentry->d_op->d_automount(path);
973 if (IS_ERR(mnt)) {
974 /*
975 * The filesystem is allowed to return -EISDIR here to indicate
976 * it doesn't want to automount. For instance, autofs would do
977 * this so that its userspace daemon can mount on this dentry.
978 *
979 * However, we can only permit this if it's a terminal point in
980 * the path being looked up; if it wasn't then the remainder of
981 * the path is inaccessible and we should say so.
982 */
Al Viro49084c32011-06-25 21:59:52 -0400983 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
David Howells9875cf82011-01-14 18:45:21 +0000984 return -EREMOTE;
985 return PTR_ERR(mnt);
986 }
David Howellsea5b7782011-01-14 19:10:03 +0000987
David Howells9875cf82011-01-14 18:45:21 +0000988 if (!mnt) /* mount collision */
989 return 0;
990
Al Viro8aef1882011-06-16 15:10:06 +0100991 if (!*need_mntput) {
992 /* lock_mount() may release path->mnt on error */
993 mntget(path->mnt);
994 *need_mntput = true;
995 }
Al Viro19a167a2011-01-17 01:35:23 -0500996 err = finish_automount(mnt, path);
David Howellsea5b7782011-01-14 19:10:03 +0000997
David Howellsea5b7782011-01-14 19:10:03 +0000998 switch (err) {
999 case -EBUSY:
1000 /* Someone else made a mount here whilst we were busy */
Al Viro19a167a2011-01-17 01:35:23 -05001001 return 0;
David Howellsea5b7782011-01-14 19:10:03 +00001002 case 0:
Al Viro8aef1882011-06-16 15:10:06 +01001003 path_put(path);
David Howellsea5b7782011-01-14 19:10:03 +00001004 path->mnt = mnt;
1005 path->dentry = dget(mnt->mnt_root);
David Howellsea5b7782011-01-14 19:10:03 +00001006 return 0;
Al Viro19a167a2011-01-17 01:35:23 -05001007 default:
1008 return err;
David Howellsea5b7782011-01-14 19:10:03 +00001009 }
Al Viro19a167a2011-01-17 01:35:23 -05001010
David Howells9875cf82011-01-14 18:45:21 +00001011}
1012
1013/*
1014 * Handle a dentry that is managed in some way.
David Howellscc53ce52011-01-14 18:45:26 +00001015 * - Flagged for transit management (autofs)
David Howells9875cf82011-01-14 18:45:21 +00001016 * - Flagged as mountpoint
1017 * - Flagged as automount point
1018 *
1019 * This may only be called in refwalk mode.
1020 *
1021 * Serialization is taken care of in namespace.c
1022 */
1023static int follow_managed(struct path *path, unsigned flags)
1024{
Al Viro8aef1882011-06-16 15:10:06 +01001025 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
David Howells9875cf82011-01-14 18:45:21 +00001026 unsigned managed;
1027 bool need_mntput = false;
Al Viro8aef1882011-06-16 15:10:06 +01001028 int ret = 0;
David Howells9875cf82011-01-14 18:45:21 +00001029
1030 /* Given that we're not holding a lock here, we retain the value in a
1031 * local variable for each dentry as we look at it so that we don't see
1032 * the components of that value change under us */
1033 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1034 managed &= DCACHE_MANAGED_DENTRY,
1035 unlikely(managed != 0)) {
David Howellscc53ce52011-01-14 18:45:26 +00001036 /* Allow the filesystem to manage the transit without i_mutex
1037 * being held. */
1038 if (managed & DCACHE_MANAGE_TRANSIT) {
1039 BUG_ON(!path->dentry->d_op);
1040 BUG_ON(!path->dentry->d_op->d_manage);
Al Viro1aed3e42011-03-18 09:09:02 -04001041 ret = path->dentry->d_op->d_manage(path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +00001042 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001043 break;
David Howellscc53ce52011-01-14 18:45:26 +00001044 }
1045
David Howells9875cf82011-01-14 18:45:21 +00001046 /* Transit to a mounted filesystem. */
1047 if (managed & DCACHE_MOUNTED) {
1048 struct vfsmount *mounted = lookup_mnt(path);
1049 if (mounted) {
1050 dput(path->dentry);
1051 if (need_mntput)
1052 mntput(path->mnt);
1053 path->mnt = mounted;
1054 path->dentry = dget(mounted->mnt_root);
1055 need_mntput = true;
1056 continue;
1057 }
1058
1059 /* Something is mounted on this dentry in another
1060 * namespace and/or whatever was mounted there in this
1061 * namespace got unmounted before we managed to get the
1062 * vfsmount_lock */
1063 }
1064
1065 /* Handle an automount point */
1066 if (managed & DCACHE_NEED_AUTOMOUNT) {
1067 ret = follow_automount(path, flags, &need_mntput);
1068 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001069 break;
David Howells9875cf82011-01-14 18:45:21 +00001070 continue;
1071 }
1072
1073 /* We didn't change the current path point */
1074 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 }
Al Viro8aef1882011-06-16 15:10:06 +01001076
1077 if (need_mntput && path->mnt == mnt)
1078 mntput(path->mnt);
1079 if (ret == -EISDIR)
1080 ret = 0;
Al Viroa3fbbde2011-11-07 21:21:26 +00001081 return ret < 0 ? ret : need_mntput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082}
1083
David Howellscc53ce52011-01-14 18:45:26 +00001084int follow_down_one(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085{
1086 struct vfsmount *mounted;
1087
Al Viro1c755af2009-04-18 14:06:57 -04001088 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -04001090 dput(path->dentry);
1091 mntput(path->mnt);
1092 path->mnt = mounted;
1093 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 return 1;
1095 }
1096 return 0;
1097}
1098
Ian Kent62a73752011-03-25 01:51:02 +08001099static inline bool managed_dentry_might_block(struct dentry *dentry)
1100{
1101 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
1102 dentry->d_op->d_manage(dentry, true) < 0);
1103}
1104
David Howells9875cf82011-01-14 18:45:21 +00001105/*
Al Viro287548e2011-05-27 06:50:06 -04001106 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1107 * we meet a managed dentry that would need blocking.
David Howells9875cf82011-01-14 18:45:21 +00001108 */
1109static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
Al Viro287548e2011-05-27 06:50:06 -04001110 struct inode **inode)
David Howells9875cf82011-01-14 18:45:21 +00001111{
Ian Kent62a73752011-03-25 01:51:02 +08001112 for (;;) {
Al Viroc7105362011-11-24 18:22:03 -05001113 struct mount *mounted;
Ian Kent62a73752011-03-25 01:51:02 +08001114 /*
1115 * Don't forget we might have a non-mountpoint managed dentry
1116 * that wants to block transit.
1117 */
Al Viro287548e2011-05-27 06:50:06 -04001118 if (unlikely(managed_dentry_might_block(path->dentry)))
David Howellsab909112011-01-14 18:46:51 +00001119 return false;
Ian Kent62a73752011-03-25 01:51:02 +08001120
1121 if (!d_mountpoint(path->dentry))
1122 break;
1123
David Howells9875cf82011-01-14 18:45:21 +00001124 mounted = __lookup_mnt(path->mnt, path->dentry, 1);
1125 if (!mounted)
1126 break;
Al Viroc7105362011-11-24 18:22:03 -05001127 path->mnt = &mounted->mnt;
1128 path->dentry = mounted->mnt.mnt_root;
Al Viroa3fbbde2011-11-07 21:21:26 +00001129 nd->flags |= LOOKUP_JUMPED;
David Howells9875cf82011-01-14 18:45:21 +00001130 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
Linus Torvalds59430262011-07-18 15:43:29 -07001131 /*
1132 * Update the inode too. We don't need to re-check the
1133 * dentry sequence number here after this d_inode read,
1134 * because a mount-point is always pinned.
1135 */
1136 *inode = path->dentry->d_inode;
David Howells9875cf82011-01-14 18:45:21 +00001137 }
David Howells9875cf82011-01-14 18:45:21 +00001138 return true;
1139}
1140
Al Virodea39372011-05-27 06:53:39 -04001141static void follow_mount_rcu(struct nameidata *nd)
Al Viro287548e2011-05-27 06:50:06 -04001142{
Al Virodea39372011-05-27 06:53:39 -04001143 while (d_mountpoint(nd->path.dentry)) {
Al Viroc7105362011-11-24 18:22:03 -05001144 struct mount *mounted;
Al Virodea39372011-05-27 06:53:39 -04001145 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
Al Viro287548e2011-05-27 06:50:06 -04001146 if (!mounted)
1147 break;
Al Viroc7105362011-11-24 18:22:03 -05001148 nd->path.mnt = &mounted->mnt;
1149 nd->path.dentry = mounted->mnt.mnt_root;
Al Virodea39372011-05-27 06:53:39 -04001150 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro287548e2011-05-27 06:50:06 -04001151 }
1152}
1153
Nick Piggin31e6b012011-01-07 17:49:52 +11001154static int follow_dotdot_rcu(struct nameidata *nd)
1155{
Nick Piggin31e6b012011-01-07 17:49:52 +11001156 set_root_rcu(nd);
1157
David Howells9875cf82011-01-14 18:45:21 +00001158 while (1) {
Nick Piggin31e6b012011-01-07 17:49:52 +11001159 if (nd->path.dentry == nd->root.dentry &&
1160 nd->path.mnt == nd->root.mnt) {
1161 break;
1162 }
1163 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1164 struct dentry *old = nd->path.dentry;
1165 struct dentry *parent = old->d_parent;
1166 unsigned seq;
1167
1168 seq = read_seqcount_begin(&parent->d_seq);
1169 if (read_seqcount_retry(&old->d_seq, nd->seq))
Al Viroef7562d2011-03-04 14:35:59 -05001170 goto failed;
Nick Piggin31e6b012011-01-07 17:49:52 +11001171 nd->path.dentry = parent;
1172 nd->seq = seq;
1173 break;
1174 }
1175 if (!follow_up_rcu(&nd->path))
1176 break;
1177 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Nick Piggin31e6b012011-01-07 17:49:52 +11001178 }
Al Virodea39372011-05-27 06:53:39 -04001179 follow_mount_rcu(nd);
1180 nd->inode = nd->path.dentry->d_inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001181 return 0;
Al Viroef7562d2011-03-04 14:35:59 -05001182
1183failed:
1184 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001185 if (!(nd->flags & LOOKUP_ROOT))
1186 nd->root.mnt = NULL;
Al Viro32a79912012-07-18 20:43:19 +04001187 unlock_rcu_walk();
Al Viroef7562d2011-03-04 14:35:59 -05001188 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001189}
1190
David Howells9875cf82011-01-14 18:45:21 +00001191/*
David Howellscc53ce52011-01-14 18:45:26 +00001192 * Follow down to the covering mount currently visible to userspace. At each
1193 * point, the filesystem owning that dentry may be queried as to whether the
1194 * caller is permitted to proceed or not.
David Howellscc53ce52011-01-14 18:45:26 +00001195 */
Al Viro7cc90cc2011-03-18 09:04:20 -04001196int follow_down(struct path *path)
David Howellscc53ce52011-01-14 18:45:26 +00001197{
1198 unsigned managed;
1199 int ret;
1200
1201 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1202 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1203 /* Allow the filesystem to manage the transit without i_mutex
1204 * being held.
1205 *
1206 * We indicate to the filesystem if someone is trying to mount
1207 * something here. This gives autofs the chance to deny anyone
1208 * other than its daemon the right to mount on its
1209 * superstructure.
1210 *
1211 * The filesystem may sleep at this point.
1212 */
1213 if (managed & DCACHE_MANAGE_TRANSIT) {
1214 BUG_ON(!path->dentry->d_op);
1215 BUG_ON(!path->dentry->d_op->d_manage);
David Howellsab909112011-01-14 18:46:51 +00001216 ret = path->dentry->d_op->d_manage(
Al Viro1aed3e42011-03-18 09:09:02 -04001217 path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +00001218 if (ret < 0)
1219 return ret == -EISDIR ? 0 : ret;
1220 }
1221
1222 /* Transit to a mounted filesystem. */
1223 if (managed & DCACHE_MOUNTED) {
1224 struct vfsmount *mounted = lookup_mnt(path);
1225 if (!mounted)
1226 break;
1227 dput(path->dentry);
1228 mntput(path->mnt);
1229 path->mnt = mounted;
1230 path->dentry = dget(mounted->mnt_root);
1231 continue;
1232 }
1233
1234 /* Don't handle automount points here */
1235 break;
1236 }
1237 return 0;
1238}
1239
1240/*
David Howells9875cf82011-01-14 18:45:21 +00001241 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1242 */
1243static void follow_mount(struct path *path)
1244{
1245 while (d_mountpoint(path->dentry)) {
1246 struct vfsmount *mounted = lookup_mnt(path);
1247 if (!mounted)
1248 break;
1249 dput(path->dentry);
1250 mntput(path->mnt);
1251 path->mnt = mounted;
1252 path->dentry = dget(mounted->mnt_root);
1253 }
1254}
1255
Nick Piggin31e6b012011-01-07 17:49:52 +11001256static void follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
Al Viro2a737872009-04-07 11:49:53 -04001258 set_root(nd);
Andreas Mohre518ddb2006-09-29 02:01:22 -07001259
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 while(1) {
Jan Blunck4ac91372008-02-14 19:34:32 -08001261 struct dentry *old = nd->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Al Viro2a737872009-04-07 11:49:53 -04001263 if (nd->path.dentry == nd->root.dentry &&
1264 nd->path.mnt == nd->root.mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 break;
1266 }
Jan Blunck4ac91372008-02-14 19:34:32 -08001267 if (nd->path.dentry != nd->path.mnt->mnt_root) {
Al Viro3088dd72010-01-30 15:47:29 -05001268 /* rare case of legitimate dget_parent()... */
1269 nd->path.dentry = dget_parent(nd->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 dput(old);
1271 break;
1272 }
Al Viro3088dd72010-01-30 15:47:29 -05001273 if (!follow_up(&nd->path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 }
Al Viro79ed0222009-04-18 13:59:41 -04001276 follow_mount(&nd->path);
Nick Piggin31e6b012011-01-07 17:49:52 +11001277 nd->inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278}
1279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280/*
Miklos Szeredibad61182012-03-26 12:54:24 +02001281 * This looks up the name in dcache, possibly revalidates the old dentry and
1282 * allocates a new one if not found or not valid. In the need_lookup argument
1283 * returns whether i_op->lookup is necessary.
1284 *
1285 * dir->d_inode->i_mutex must be held
Nick Pigginbaa03892010-08-18 04:37:31 +10001286 */
Miklos Szeredibad61182012-03-26 12:54:24 +02001287static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
Al Viro201f9562012-06-22 12:42:10 +04001288 unsigned int flags, bool *need_lookup)
Nick Pigginbaa03892010-08-18 04:37:31 +10001289{
Nick Pigginbaa03892010-08-18 04:37:31 +10001290 struct dentry *dentry;
Miklos Szeredibad61182012-03-26 12:54:24 +02001291 int error;
Nick Pigginbaa03892010-08-18 04:37:31 +10001292
Miklos Szeredibad61182012-03-26 12:54:24 +02001293 *need_lookup = false;
1294 dentry = d_lookup(dir, name);
1295 if (dentry) {
Jeff Layton39e3c952012-11-28 11:30:53 -05001296 if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
Al Viro201f9562012-06-22 12:42:10 +04001297 error = d_revalidate(dentry, flags);
Miklos Szeredibad61182012-03-26 12:54:24 +02001298 if (unlikely(error <= 0)) {
1299 if (error < 0) {
1300 dput(dentry);
1301 return ERR_PTR(error);
1302 } else if (!d_invalidate(dentry)) {
1303 dput(dentry);
1304 dentry = NULL;
1305 }
1306 }
1307 }
1308 }
Nick Pigginbaa03892010-08-18 04:37:31 +10001309
Miklos Szeredibad61182012-03-26 12:54:24 +02001310 if (!dentry) {
1311 dentry = d_alloc(dir, name);
1312 if (unlikely(!dentry))
1313 return ERR_PTR(-ENOMEM);
Nick Pigginbaa03892010-08-18 04:37:31 +10001314
Miklos Szeredibad61182012-03-26 12:54:24 +02001315 *need_lookup = true;
Nick Pigginbaa03892010-08-18 04:37:31 +10001316 }
1317 return dentry;
1318}
1319
1320/*
Miklos Szeredibad61182012-03-26 12:54:24 +02001321 * Call i_op->lookup on the dentry. The dentry must be negative but may be
1322 * hashed if it was pouplated with DCACHE_NEED_LOOKUP.
1323 *
1324 * dir->d_inode->i_mutex must be held
Josef Bacik44396f42011-05-31 11:58:49 -04001325 */
Miklos Szeredibad61182012-03-26 12:54:24 +02001326static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
Al Viro72bd8662012-06-10 17:17:17 -04001327 unsigned int flags)
Josef Bacik44396f42011-05-31 11:58:49 -04001328{
Josef Bacik44396f42011-05-31 11:58:49 -04001329 struct dentry *old;
1330
1331 /* Don't create child dentry for a dead directory. */
Miklos Szeredibad61182012-03-26 12:54:24 +02001332 if (unlikely(IS_DEADDIR(dir))) {
Miklos Szeredie188dc02012-02-03 14:25:18 +01001333 dput(dentry);
Josef Bacik44396f42011-05-31 11:58:49 -04001334 return ERR_PTR(-ENOENT);
Miklos Szeredie188dc02012-02-03 14:25:18 +01001335 }
Josef Bacik44396f42011-05-31 11:58:49 -04001336
Al Viro72bd8662012-06-10 17:17:17 -04001337 old = dir->i_op->lookup(dir, dentry, flags);
Josef Bacik44396f42011-05-31 11:58:49 -04001338 if (unlikely(old)) {
1339 dput(dentry);
1340 dentry = old;
1341 }
1342 return dentry;
1343}
1344
Al Viroa3255542012-03-30 14:41:51 -04001345static struct dentry *__lookup_hash(struct qstr *name,
Al Viro72bd8662012-06-10 17:17:17 -04001346 struct dentry *base, unsigned int flags)
Al Viroa3255542012-03-30 14:41:51 -04001347{
Miklos Szeredibad61182012-03-26 12:54:24 +02001348 bool need_lookup;
Al Viroa3255542012-03-30 14:41:51 -04001349 struct dentry *dentry;
1350
Al Viro72bd8662012-06-10 17:17:17 -04001351 dentry = lookup_dcache(name, base, flags, &need_lookup);
Miklos Szeredibad61182012-03-26 12:54:24 +02001352 if (!need_lookup)
1353 return dentry;
Al Viroa3255542012-03-30 14:41:51 -04001354
Al Viro72bd8662012-06-10 17:17:17 -04001355 return lookup_real(base->d_inode, dentry, flags);
Al Viroa3255542012-03-30 14:41:51 -04001356}
1357
Josef Bacik44396f42011-05-31 11:58:49 -04001358/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 * It's more convoluted than I'd like it to be, but... it's still fairly
1360 * small and for now I'd prefer to have fast path as straight as possible.
1361 * It _is_ time-critical.
1362 */
Al Viroe97cdc82013-01-24 18:16:00 -05001363static int lookup_fast(struct nameidata *nd,
Miklos Szeredi697f5142012-05-21 17:30:05 +02001364 struct path *path, struct inode **inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
Jan Blunck4ac91372008-02-14 19:34:32 -08001366 struct vfsmount *mnt = nd->path.mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +11001367 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro5a18fff2011-03-11 04:44:53 -05001368 int need_reval = 1;
1369 int status = 1;
David Howells9875cf82011-01-14 18:45:21 +00001370 int err;
1371
Al Viro3cac2602009-08-13 18:27:43 +04001372 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001373 * Rename seqlock is not required here because in the off chance
1374 * of a false negative due to a concurrent rename, we're going to
1375 * do the non-racy lookup, below.
1376 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001377 if (nd->flags & LOOKUP_RCU) {
1378 unsigned seq;
Linus Torvaldsda53be12013-05-21 15:22:44 -07001379 dentry = __d_lookup_rcu(parent, &nd->last, &seq);
Al Viro5a18fff2011-03-11 04:44:53 -05001380 if (!dentry)
1381 goto unlazy;
1382
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001383 /*
1384 * This sequence count validates that the inode matches
1385 * the dentry name information from lookup.
1386 */
1387 *inode = dentry->d_inode;
1388 if (read_seqcount_retry(&dentry->d_seq, seq))
1389 return -ECHILD;
1390
1391 /*
1392 * This sequence count validates that the parent had no
1393 * changes while we did the lookup of the dentry above.
1394 *
1395 * The memory barrier in read_seqcount_begin of child is
1396 * enough, we can use __read_seqcount_retry here.
1397 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001398 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1399 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001400 nd->seq = seq;
Al Viro5a18fff2011-03-11 04:44:53 -05001401
Al Viro24643082011-02-15 01:26:22 -05001402 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
Al Viro4ce16ef32012-06-10 16:10:59 -04001403 status = d_revalidate(dentry, nd->flags);
Al Viro5a18fff2011-03-11 04:44:53 -05001404 if (unlikely(status <= 0)) {
1405 if (status != -ECHILD)
1406 need_reval = 0;
1407 goto unlazy;
1408 }
Al Viro24643082011-02-15 01:26:22 -05001409 }
Nick Piggin31e6b012011-01-07 17:49:52 +11001410 path->mnt = mnt;
1411 path->dentry = dentry;
Al Virod6e9bd22011-05-27 07:03:15 -04001412 if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1413 goto unlazy;
1414 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1415 goto unlazy;
1416 return 0;
Al Viro5a18fff2011-03-11 04:44:53 -05001417unlazy:
Al Viro19660af2011-03-25 10:32:48 -04001418 if (unlazy_walk(nd, dentry))
1419 return -ECHILD;
Al Viro5a18fff2011-03-11 04:44:53 -05001420 } else {
Al Viroe97cdc82013-01-24 18:16:00 -05001421 dentry = __d_lookup(parent, &nd->last);
Nick Piggin31e6b012011-01-07 17:49:52 +11001422 }
Al Viro5a18fff2011-03-11 04:44:53 -05001423
Al Viro81e6f522012-03-30 14:48:04 -04001424 if (unlikely(!dentry))
1425 goto need_lookup;
Al Viro5a18fff2011-03-11 04:44:53 -05001426
Al Viro5a18fff2011-03-11 04:44:53 -05001427 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
Al Viro4ce16ef32012-06-10 16:10:59 -04001428 status = d_revalidate(dentry, nd->flags);
Al Viro5a18fff2011-03-11 04:44:53 -05001429 if (unlikely(status <= 0)) {
1430 if (status < 0) {
1431 dput(dentry);
1432 return status;
1433 }
1434 if (!d_invalidate(dentry)) {
1435 dput(dentry);
Al Viro81e6f522012-03-30 14:48:04 -04001436 goto need_lookup;
Al Viro5a18fff2011-03-11 04:44:53 -05001437 }
1438 }
Miklos Szeredi697f5142012-05-21 17:30:05 +02001439
David Howells9875cf82011-01-14 18:45:21 +00001440 path->mnt = mnt;
1441 path->dentry = dentry;
1442 err = follow_managed(path, nd->flags);
Ian Kent89312212011-01-18 12:06:10 +08001443 if (unlikely(err < 0)) {
1444 path_put_conditional(path, nd);
David Howells9875cf82011-01-14 18:45:21 +00001445 return err;
Ian Kent89312212011-01-18 12:06:10 +08001446 }
Al Viroa3fbbde2011-11-07 21:21:26 +00001447 if (err)
1448 nd->flags |= LOOKUP_JUMPED;
David Howells9875cf82011-01-14 18:45:21 +00001449 *inode = path->dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 return 0;
Al Viro81e6f522012-03-30 14:48:04 -04001451
1452need_lookup:
Miklos Szeredi697f5142012-05-21 17:30:05 +02001453 return 1;
1454}
1455
1456/* Fast lookup failed, do it the slow way */
Al Virocc2a5272013-01-24 18:19:49 -05001457static int lookup_slow(struct nameidata *nd, struct path *path)
Miklos Szeredi697f5142012-05-21 17:30:05 +02001458{
1459 struct dentry *dentry, *parent;
1460 int err;
1461
1462 parent = nd->path.dentry;
Al Viro81e6f522012-03-30 14:48:04 -04001463 BUG_ON(nd->inode != parent->d_inode);
1464
1465 mutex_lock(&parent->d_inode->i_mutex);
Al Virocc2a5272013-01-24 18:19:49 -05001466 dentry = __lookup_hash(&nd->last, parent, nd->flags);
Al Viro81e6f522012-03-30 14:48:04 -04001467 mutex_unlock(&parent->d_inode->i_mutex);
1468 if (IS_ERR(dentry))
1469 return PTR_ERR(dentry);
Miklos Szeredi697f5142012-05-21 17:30:05 +02001470 path->mnt = nd->path.mnt;
1471 path->dentry = dentry;
1472 err = follow_managed(path, nd->flags);
1473 if (unlikely(err < 0)) {
1474 path_put_conditional(path, nd);
1475 return err;
1476 }
1477 if (err)
1478 nd->flags |= LOOKUP_JUMPED;
1479 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480}
1481
Al Viro52094c82011-02-21 21:34:47 -05001482static inline int may_lookup(struct nameidata *nd)
1483{
1484 if (nd->flags & LOOKUP_RCU) {
Al Viro4ad5abb2011-06-20 19:57:03 -04001485 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
Al Viro52094c82011-02-21 21:34:47 -05001486 if (err != -ECHILD)
1487 return err;
Al Viro19660af2011-03-25 10:32:48 -04001488 if (unlazy_walk(nd, NULL))
Al Viro52094c82011-02-21 21:34:47 -05001489 return -ECHILD;
1490 }
Al Viro4ad5abb2011-06-20 19:57:03 -04001491 return inode_permission(nd->inode, MAY_EXEC);
Al Viro52094c82011-02-21 21:34:47 -05001492}
1493
Al Viro9856fa12011-03-04 14:22:06 -05001494static inline int handle_dots(struct nameidata *nd, int type)
1495{
1496 if (type == LAST_DOTDOT) {
1497 if (nd->flags & LOOKUP_RCU) {
1498 if (follow_dotdot_rcu(nd))
1499 return -ECHILD;
1500 } else
1501 follow_dotdot(nd);
1502 }
1503 return 0;
1504}
1505
Al Viro951361f2011-03-04 14:44:37 -05001506static void terminate_walk(struct nameidata *nd)
1507{
1508 if (!(nd->flags & LOOKUP_RCU)) {
1509 path_put(&nd->path);
1510 } else {
1511 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001512 if (!(nd->flags & LOOKUP_ROOT))
1513 nd->root.mnt = NULL;
Al Viro32a79912012-07-18 20:43:19 +04001514 unlock_rcu_walk();
Al Viro951361f2011-03-04 14:44:37 -05001515 }
1516}
1517
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001518/*
1519 * Do we need to follow links? We _really_ want to be able
1520 * to do this check without having to look at inode->i_op,
1521 * so we keep a cache of "no, this doesn't need follow_link"
1522 * for the common case.
1523 */
Linus Torvalds7813b942011-08-07 09:53:20 -07001524static inline int should_follow_link(struct inode *inode, int follow)
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001525{
1526 if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
1527 if (likely(inode->i_op->follow_link))
1528 return follow;
1529
1530 /* This gets set once for the inode lifetime */
1531 spin_lock(&inode->i_lock);
1532 inode->i_opflags |= IOP_NOFOLLOW;
1533 spin_unlock(&inode->i_lock);
1534 }
1535 return 0;
1536}
1537
Al Viroce57dfc2011-03-13 19:58:58 -04001538static inline int walk_component(struct nameidata *nd, struct path *path,
Al Viro21b9b072013-01-24 18:10:25 -05001539 int follow)
Al Viroce57dfc2011-03-13 19:58:58 -04001540{
1541 struct inode *inode;
1542 int err;
1543 /*
1544 * "." and ".." are special - ".." especially so because it has
1545 * to be able to know about the current root directory and
1546 * parent relationships.
1547 */
Al Viro21b9b072013-01-24 18:10:25 -05001548 if (unlikely(nd->last_type != LAST_NORM))
1549 return handle_dots(nd, nd->last_type);
Al Viroe97cdc82013-01-24 18:16:00 -05001550 err = lookup_fast(nd, path, &inode);
Al Viroce57dfc2011-03-13 19:58:58 -04001551 if (unlikely(err)) {
Miklos Szeredi697f5142012-05-21 17:30:05 +02001552 if (err < 0)
1553 goto out_err;
1554
Al Virocc2a5272013-01-24 18:19:49 -05001555 err = lookup_slow(nd, path);
Miklos Szeredi697f5142012-05-21 17:30:05 +02001556 if (err < 0)
1557 goto out_err;
1558
1559 inode = path->dentry->d_inode;
Al Viroce57dfc2011-03-13 19:58:58 -04001560 }
Miklos Szeredi697f5142012-05-21 17:30:05 +02001561 err = -ENOENT;
1562 if (!inode)
1563 goto out_path_put;
1564
Linus Torvalds7813b942011-08-07 09:53:20 -07001565 if (should_follow_link(inode, follow)) {
Al Viro19660af2011-03-25 10:32:48 -04001566 if (nd->flags & LOOKUP_RCU) {
1567 if (unlikely(unlazy_walk(nd, path->dentry))) {
Miklos Szeredi697f5142012-05-21 17:30:05 +02001568 err = -ECHILD;
1569 goto out_err;
Al Viro19660af2011-03-25 10:32:48 -04001570 }
1571 }
Al Viroce57dfc2011-03-13 19:58:58 -04001572 BUG_ON(inode != path->dentry->d_inode);
1573 return 1;
1574 }
1575 path_to_nameidata(path, nd);
1576 nd->inode = inode;
1577 return 0;
Miklos Szeredi697f5142012-05-21 17:30:05 +02001578
1579out_path_put:
1580 path_to_nameidata(path, nd);
1581out_err:
1582 terminate_walk(nd);
1583 return err;
Al Viroce57dfc2011-03-13 19:58:58 -04001584}
1585
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586/*
Al Virob3563792011-03-14 21:54:55 -04001587 * This limits recursive symlink follows to 8, while
1588 * limiting consecutive symlinks to 40.
1589 *
1590 * Without that kind of total limit, nasty chains of consecutive
1591 * symlinks can cause almost arbitrarily long lookups.
1592 */
1593static inline int nested_symlink(struct path *path, struct nameidata *nd)
1594{
1595 int res;
1596
Al Virob3563792011-03-14 21:54:55 -04001597 if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1598 path_put_conditional(path, nd);
1599 path_put(&nd->path);
1600 return -ELOOP;
1601 }
Erez Zadok1a4022f2011-05-21 01:19:59 -04001602 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
Al Virob3563792011-03-14 21:54:55 -04001603
1604 nd->depth++;
1605 current->link_count++;
1606
1607 do {
1608 struct path link = *path;
1609 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04001610
1611 res = follow_link(&link, nd, &cookie);
Al Viro6d7b5aa2012-06-10 04:15:17 -04001612 if (res)
1613 break;
Al Viro21b9b072013-01-24 18:10:25 -05001614 res = walk_component(nd, path, LOOKUP_FOLLOW);
Al Viro574197e2011-03-14 22:20:34 -04001615 put_link(nd, &link, cookie);
Al Virob3563792011-03-14 21:54:55 -04001616 } while (res > 0);
1617
1618 current->link_count--;
1619 nd->depth--;
1620 return res;
1621}
1622
1623/*
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001624 * We really don't want to look at inode->i_op->lookup
1625 * when we don't have to. So we keep a cache bit in
1626 * the inode ->i_opflags field that says "yes, we can
1627 * do lookup on this inode".
1628 */
1629static inline int can_lookup(struct inode *inode)
1630{
1631 if (likely(inode->i_opflags & IOP_LOOKUP))
1632 return 1;
1633 if (likely(!inode->i_op->lookup))
1634 return 0;
1635
1636 /* We do this once for the lifetime of the inode */
1637 spin_lock(&inode->i_lock);
1638 inode->i_opflags |= IOP_LOOKUP;
1639 spin_unlock(&inode->i_lock);
1640 return 1;
1641}
1642
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001643/*
1644 * We can do the critical dentry name comparison and hashing
1645 * operations one word at a time, but we are limited to:
1646 *
1647 * - Architectures with fast unaligned word accesses. We could
1648 * do a "get_unaligned()" if this helps and is sufficiently
1649 * fast.
1650 *
1651 * - Little-endian machines (so that we can generate the mask
1652 * of low bytes efficiently). Again, we *could* do a byte
1653 * swapping load on big-endian architectures if that is not
1654 * expensive enough to make the optimization worthless.
1655 *
1656 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1657 * do not trap on the (extremely unlikely) case of a page
1658 * crossing operation.
1659 *
1660 * - Furthermore, we need an efficient 64-bit compile for the
1661 * 64-bit case in order to generate the "number of bytes in
1662 * the final mask". Again, that could be replaced with a
1663 * efficient population count instruction or similar.
1664 */
1665#ifdef CONFIG_DCACHE_WORD_ACCESS
1666
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001667#include <asm/word-at-a-time.h>
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001668
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001669#ifdef CONFIG_64BIT
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001670
1671static inline unsigned int fold_hash(unsigned long hash)
1672{
1673 hash += hash >> (8*sizeof(int));
1674 return hash;
1675}
1676
1677#else /* 32-bit case */
1678
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001679#define fold_hash(x) (x)
1680
1681#endif
1682
1683unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1684{
1685 unsigned long a, mask;
1686 unsigned long hash = 0;
1687
1688 for (;;) {
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001689 a = load_unaligned_zeropad(name);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001690 if (len < sizeof(unsigned long))
1691 break;
1692 hash += a;
Al Virof132c5b2012-03-22 21:59:52 +00001693 hash *= 9;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001694 name += sizeof(unsigned long);
1695 len -= sizeof(unsigned long);
1696 if (!len)
1697 goto done;
1698 }
1699 mask = ~(~0ul << len*8);
1700 hash += mask & a;
1701done:
1702 return fold_hash(hash);
1703}
1704EXPORT_SYMBOL(full_name_hash);
1705
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001706/*
1707 * Calculate the length and hash of the path component, and
1708 * return the length of the component;
1709 */
1710static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1711{
Linus Torvalds36126f82012-05-26 10:43:17 -07001712 unsigned long a, b, adata, bdata, mask, hash, len;
1713 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001714
1715 hash = a = 0;
1716 len = -sizeof(unsigned long);
1717 do {
1718 hash = (hash + a) * 9;
1719 len += sizeof(unsigned long);
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001720 a = load_unaligned_zeropad(name+len);
Linus Torvalds36126f82012-05-26 10:43:17 -07001721 b = a ^ REPEAT_BYTE('/');
1722 } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001723
Linus Torvalds36126f82012-05-26 10:43:17 -07001724 adata = prep_zero_mask(a, adata, &constants);
1725 bdata = prep_zero_mask(b, bdata, &constants);
1726
1727 mask = create_zero_mask(adata | bdata);
1728
1729 hash += a & zero_bytemask(mask);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001730 *hashp = fold_hash(hash);
1731
Linus Torvalds36126f82012-05-26 10:43:17 -07001732 return len + find_zero(mask);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001733}
1734
1735#else
1736
Linus Torvalds0145acc2012-03-02 14:32:59 -08001737unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1738{
1739 unsigned long hash = init_name_hash();
1740 while (len--)
1741 hash = partial_name_hash(*name++, hash);
1742 return end_name_hash(hash);
1743}
Linus Torvaldsae942ae2012-03-02 19:40:57 -08001744EXPORT_SYMBOL(full_name_hash);
Linus Torvalds0145acc2012-03-02 14:32:59 -08001745
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001746/*
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001747 * We know there's a real path component here of at least
1748 * one character.
1749 */
1750static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1751{
1752 unsigned long hash = init_name_hash();
1753 unsigned long len = 0, c;
1754
1755 c = (unsigned char)*name;
1756 do {
1757 len++;
1758 hash = partial_name_hash(c, hash);
1759 c = (unsigned char)name[len];
1760 } while (c && c != '/');
1761 *hashp = end_name_hash(hash);
1762 return len;
1763}
1764
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001765#endif
1766
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001767/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01001769 * This is the basic name resolution function, turning a pathname into
1770 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01001772 * Returns 0 and nd will have valid dentry and mnt on success.
1773 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 */
Al Viro6de88d72009-08-09 01:41:57 +04001775static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
1777 struct path next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
1780 while (*name=='/')
1781 name++;
1782 if (!*name)
Al Viro086e1832011-02-22 20:56:27 -05001783 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 /* At this point we know we have a real path component. */
1786 for(;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 struct qstr this;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001788 long len;
Al Virofe479a52011-02-22 15:10:03 -05001789 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Al Viro52094c82011-02-21 21:34:47 -05001791 err = may_lookup(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 if (err)
1793 break;
1794
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001795 len = hash_name(name, &this.hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 this.name = name;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001797 this.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
Al Virofe479a52011-02-22 15:10:03 -05001799 type = LAST_NORM;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001800 if (name[0] == '.') switch (len) {
Al Virofe479a52011-02-22 15:10:03 -05001801 case 2:
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001802 if (name[1] == '.') {
Al Virofe479a52011-02-22 15:10:03 -05001803 type = LAST_DOTDOT;
Al Viro16c2cd72011-02-22 15:50:10 -05001804 nd->flags |= LOOKUP_JUMPED;
1805 }
Al Virofe479a52011-02-22 15:10:03 -05001806 break;
1807 case 1:
1808 type = LAST_DOT;
1809 }
Al Viro5a202bc2011-03-08 14:17:44 -05001810 if (likely(type == LAST_NORM)) {
1811 struct dentry *parent = nd->path.dentry;
Al Viro16c2cd72011-02-22 15:50:10 -05001812 nd->flags &= ~LOOKUP_JUMPED;
Al Viro5a202bc2011-03-08 14:17:44 -05001813 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
Linus Torvaldsda53be12013-05-21 15:22:44 -07001814 err = parent->d_op->d_hash(parent, &this);
Al Viro5a202bc2011-03-08 14:17:44 -05001815 if (err < 0)
1816 break;
1817 }
1818 }
Al Virofe479a52011-02-22 15:10:03 -05001819
Al Viro5f4a6a62013-01-24 18:04:22 -05001820 nd->last = this;
1821 nd->last_type = type;
1822
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001823 if (!name[len])
Al Viro5f4a6a62013-01-24 18:04:22 -05001824 return 0;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001825 /*
1826 * If it wasn't NUL, we know it was '/'. Skip that
1827 * slash, and continue until no more slashes.
1828 */
1829 do {
1830 len++;
1831 } while (unlikely(name[len] == '/'));
1832 if (!name[len])
Al Viro5f4a6a62013-01-24 18:04:22 -05001833 return 0;
1834
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001835 name += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
Al Viro21b9b072013-01-24 18:10:25 -05001837 err = walk_component(nd, &next, LOOKUP_FOLLOW);
Al Viroce57dfc2011-03-13 19:58:58 -04001838 if (err < 0)
1839 return err;
Al Virofe479a52011-02-22 15:10:03 -05001840
Al Viroce57dfc2011-03-13 19:58:58 -04001841 if (err) {
Al Virob3563792011-03-14 21:54:55 -04001842 err = nested_symlink(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 if (err)
Al Viroa7472ba2011-03-04 14:39:30 -05001844 return err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001845 }
Al Viro5f4a6a62013-01-24 18:04:22 -05001846 if (!can_lookup(nd->inode)) {
1847 err = -ENOTDIR;
1848 break;
1849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 }
Al Viro951361f2011-03-04 14:44:37 -05001851 terminate_walk(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 return err;
1853}
1854
Al Viro70e9b352011-03-05 21:12:22 -05001855static int path_init(int dfd, const char *name, unsigned int flags,
1856 struct nameidata *nd, struct file **fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857{
Prasanna Medaea3834d2005-04-29 16:00:17 +01001858 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
1860 nd->last_type = LAST_ROOT; /* if there are only slashes... */
Al Viro16c2cd72011-02-22 15:50:10 -05001861 nd->flags = flags | LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 nd->depth = 0;
Al Viro5b6ca022011-03-09 23:04:47 -05001863 if (flags & LOOKUP_ROOT) {
1864 struct inode *inode = nd->root.dentry->d_inode;
Al Viro73d049a2011-03-11 12:08:24 -05001865 if (*name) {
Al Viro741b7c32012-12-20 13:41:28 -05001866 if (!can_lookup(inode))
Al Viro73d049a2011-03-11 12:08:24 -05001867 return -ENOTDIR;
1868 retval = inode_permission(inode, MAY_EXEC);
1869 if (retval)
1870 return retval;
1871 }
Al Viro5b6ca022011-03-09 23:04:47 -05001872 nd->path = nd->root;
1873 nd->inode = inode;
1874 if (flags & LOOKUP_RCU) {
Al Viro32a79912012-07-18 20:43:19 +04001875 lock_rcu_walk();
Al Viro5b6ca022011-03-09 23:04:47 -05001876 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1877 } else {
1878 path_get(&nd->path);
1879 }
1880 return 0;
1881 }
1882
Al Viro2a737872009-04-07 11:49:53 -04001883 nd->root.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 if (*name=='/') {
Al Viroe41f7d42011-02-22 14:02:58 -05001886 if (flags & LOOKUP_RCU) {
Al Viro32a79912012-07-18 20:43:19 +04001887 lock_rcu_walk();
Al Viroe41f7d42011-02-22 14:02:58 -05001888 set_root_rcu(nd);
1889 } else {
1890 set_root(nd);
1891 path_get(&nd->root);
1892 }
Al Viro2a737872009-04-07 11:49:53 -04001893 nd->path = nd->root;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001894 } else if (dfd == AT_FDCWD) {
Al Viroe41f7d42011-02-22 14:02:58 -05001895 if (flags & LOOKUP_RCU) {
1896 struct fs_struct *fs = current->fs;
1897 unsigned seq;
1898
Al Viro32a79912012-07-18 20:43:19 +04001899 lock_rcu_walk();
Al Viroe41f7d42011-02-22 14:02:58 -05001900
1901 do {
1902 seq = read_seqcount_begin(&fs->seq);
1903 nd->path = fs->pwd;
1904 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1905 } while (read_seqcount_retry(&fs->seq, seq));
1906 } else {
1907 get_fs_pwd(current->fs, &nd->path);
1908 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001909 } else {
Jeff Layton582aa642012-12-11 08:56:16 -05001910 /* Caller must check execute permissions on the starting path component */
Al Viro2903ff02012-08-28 12:52:22 -04001911 struct fd f = fdget_raw(dfd);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001912 struct dentry *dentry;
1913
Al Viro2903ff02012-08-28 12:52:22 -04001914 if (!f.file)
1915 return -EBADF;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001916
Al Viro2903ff02012-08-28 12:52:22 -04001917 dentry = f.file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001918
Al Virof52e0c12011-03-14 18:56:51 -04001919 if (*name) {
Al Viro741b7c32012-12-20 13:41:28 -05001920 if (!can_lookup(dentry->d_inode)) {
Al Viro2903ff02012-08-28 12:52:22 -04001921 fdput(f);
1922 return -ENOTDIR;
1923 }
Al Virof52e0c12011-03-14 18:56:51 -04001924 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001925
Al Viro2903ff02012-08-28 12:52:22 -04001926 nd->path = f.file->f_path;
Al Viroe41f7d42011-02-22 14:02:58 -05001927 if (flags & LOOKUP_RCU) {
Al Viro2903ff02012-08-28 12:52:22 -04001928 if (f.need_put)
1929 *fp = f.file;
Al Viroe41f7d42011-02-22 14:02:58 -05001930 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro32a79912012-07-18 20:43:19 +04001931 lock_rcu_walk();
Al Viroe41f7d42011-02-22 14:02:58 -05001932 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001933 path_get(&nd->path);
1934 fdput(f);
Al Viroe41f7d42011-02-22 14:02:58 -05001935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 }
Al Viroe41f7d42011-02-22 14:02:58 -05001937
Nick Piggin31e6b012011-01-07 17:49:52 +11001938 nd->inode = nd->path.dentry->d_inode;
Al Viro9b4a9b12009-04-07 11:44:16 -04001939 return 0;
Al Viro9b4a9b12009-04-07 11:44:16 -04001940}
1941
Al Virobd92d7f2011-03-14 19:54:59 -04001942static inline int lookup_last(struct nameidata *nd, struct path *path)
1943{
1944 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1945 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1946
1947 nd->flags &= ~LOOKUP_PARENT;
Al Viro21b9b072013-01-24 18:10:25 -05001948 return walk_component(nd, path, nd->flags & LOOKUP_FOLLOW);
Al Virobd92d7f2011-03-14 19:54:59 -04001949}
1950
Al Viro9b4a9b12009-04-07 11:44:16 -04001951/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroee0827c2011-02-21 23:38:09 -05001952static int path_lookupat(int dfd, const char *name,
Al Viro9b4a9b12009-04-07 11:44:16 -04001953 unsigned int flags, struct nameidata *nd)
1954{
Al Viro70e9b352011-03-05 21:12:22 -05001955 struct file *base = NULL;
Al Virobd92d7f2011-03-14 19:54:59 -04001956 struct path path;
1957 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001958
1959 /*
1960 * Path walking is largely split up into 2 different synchronisation
1961 * schemes, rcu-walk and ref-walk (explained in
1962 * Documentation/filesystems/path-lookup.txt). These share much of the
1963 * path walk code, but some things particularly setup, cleanup, and
1964 * following mounts are sufficiently divergent that functions are
1965 * duplicated. Typically there is a function foo(), and its RCU
1966 * analogue, foo_rcu().
1967 *
1968 * -ECHILD is the error number of choice (just to avoid clashes) that
1969 * is returned if some aspect of an rcu-walk fails. Such an error must
1970 * be handled by restarting a traditional ref-walk (which will always
1971 * be able to complete).
1972 */
Al Virobd92d7f2011-03-14 19:54:59 -04001973 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
Al Viroee0827c2011-02-21 23:38:09 -05001974
Al Virobd92d7f2011-03-14 19:54:59 -04001975 if (unlikely(err))
1976 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001977
1978 current->total_link_count = 0;
Al Virobd92d7f2011-03-14 19:54:59 -04001979 err = link_path_walk(name, nd);
1980
1981 if (!err && !(flags & LOOKUP_PARENT)) {
Al Virobd92d7f2011-03-14 19:54:59 -04001982 err = lookup_last(nd, &path);
1983 while (err > 0) {
1984 void *cookie;
1985 struct path link = path;
Kees Cook800179c2012-07-25 17:29:07 -07001986 err = may_follow_link(&link, nd);
1987 if (unlikely(err))
1988 break;
Al Virobd92d7f2011-03-14 19:54:59 -04001989 nd->flags |= LOOKUP_PARENT;
Al Viro574197e2011-03-14 22:20:34 -04001990 err = follow_link(&link, nd, &cookie);
Al Viro6d7b5aa2012-06-10 04:15:17 -04001991 if (err)
1992 break;
1993 err = lookup_last(nd, &path);
Al Viro574197e2011-03-14 22:20:34 -04001994 put_link(nd, &link, cookie);
Al Virobd92d7f2011-03-14 19:54:59 -04001995 }
1996 }
Al Viroee0827c2011-02-21 23:38:09 -05001997
Al Viro9f1fafe2011-03-25 11:00:12 -04001998 if (!err)
1999 err = complete_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04002000
2001 if (!err && nd->flags & LOOKUP_DIRECTORY) {
Al Viro05252902013-06-06 19:33:47 -04002002 if (!can_lookup(nd->inode)) {
Al Virobd92d7f2011-03-14 19:54:59 -04002003 path_put(&nd->path);
Al Virobd23a532011-03-23 09:56:30 -04002004 err = -ENOTDIR;
Al Virobd92d7f2011-03-14 19:54:59 -04002005 }
2006 }
Al Viro16c2cd72011-02-22 15:50:10 -05002007
Al Viro70e9b352011-03-05 21:12:22 -05002008 if (base)
2009 fput(base);
Al Viroee0827c2011-02-21 23:38:09 -05002010
Al Viro5b6ca022011-03-09 23:04:47 -05002011 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
Al Viro2a737872009-04-07 11:49:53 -04002012 path_put(&nd->root);
2013 nd->root.mnt = NULL;
2014 }
Al Virobd92d7f2011-03-14 19:54:59 -04002015 return err;
Al Viroee0827c2011-02-21 23:38:09 -05002016}
Nick Piggin31e6b012011-01-07 17:49:52 +11002017
Jeff Layton873f1ee2012-10-10 15:25:29 -04002018static int filename_lookup(int dfd, struct filename *name,
2019 unsigned int flags, struct nameidata *nd)
2020{
2021 int retval = path_lookupat(dfd, name->name, flags | LOOKUP_RCU, nd);
2022 if (unlikely(retval == -ECHILD))
2023 retval = path_lookupat(dfd, name->name, flags, nd);
2024 if (unlikely(retval == -ESTALE))
2025 retval = path_lookupat(dfd, name->name,
2026 flags | LOOKUP_REVAL, nd);
2027
2028 if (likely(!retval))
Jeff Laytonadb5c242012-10-10 16:43:13 -04002029 audit_inode(name, nd->path.dentry, flags & LOOKUP_PARENT);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002030 return retval;
2031}
2032
Al Viroee0827c2011-02-21 23:38:09 -05002033static int do_path_lookup(int dfd, const char *name,
2034 unsigned int flags, struct nameidata *nd)
2035{
Jeff Layton873f1ee2012-10-10 15:25:29 -04002036 struct filename filename = { .name = name };
Nick Piggin31e6b012011-01-07 17:49:52 +11002037
Jeff Layton873f1ee2012-10-10 15:25:29 -04002038 return filename_lookup(dfd, &filename, flags, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039}
2040
Al Viro79714f72012-06-15 03:01:42 +04002041/* does lookup, returns the object with parent locked */
2042struct dentry *kern_path_locked(const char *name, struct path *path)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002043{
Al Viro79714f72012-06-15 03:01:42 +04002044 struct nameidata nd;
2045 struct dentry *d;
2046 int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd);
2047 if (err)
2048 return ERR_PTR(err);
2049 if (nd.last_type != LAST_NORM) {
2050 path_put(&nd.path);
2051 return ERR_PTR(-EINVAL);
2052 }
2053 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Al Viro1e0ea002012-07-22 23:46:21 +04002054 d = __lookup_hash(&nd.last, nd.path.dentry, 0);
Al Viro79714f72012-06-15 03:01:42 +04002055 if (IS_ERR(d)) {
2056 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2057 path_put(&nd.path);
2058 return d;
2059 }
2060 *path = nd.path;
2061 return d;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002062}
2063
Al Virod1811462008-08-02 00:49:18 -04002064int kern_path(const char *name, unsigned int flags, struct path *path)
2065{
2066 struct nameidata nd;
2067 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
2068 if (!res)
2069 *path = nd.path;
2070 return res;
2071}
2072
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002073/**
2074 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2075 * @dentry: pointer to dentry of the base directory
2076 * @mnt: pointer to vfs mount of the base directory
2077 * @name: pointer to file name
2078 * @flags: lookup flags
Al Viroe0a01242011-06-27 17:00:37 -04002079 * @path: pointer to struct path to fill
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002080 */
2081int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2082 const char *name, unsigned int flags,
Al Viroe0a01242011-06-27 17:00:37 -04002083 struct path *path)
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002084{
Al Viroe0a01242011-06-27 17:00:37 -04002085 struct nameidata nd;
2086 int err;
2087 nd.root.dentry = dentry;
2088 nd.root.mnt = mnt;
2089 BUG_ON(flags & LOOKUP_PARENT);
Al Viro5b6ca022011-03-09 23:04:47 -05002090 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
Al Viroe0a01242011-06-27 17:00:37 -04002091 err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
2092 if (!err)
2093 *path = nd.path;
2094 return err;
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002095}
2096
James Morris057f6c02007-04-26 00:12:05 -07002097/*
2098 * Restricted form of lookup. Doesn't follow links, single-component only,
2099 * needs parent already locked. Doesn't follow mounts.
2100 * SMP-safe.
2101 */
Adrian Bunka244e162006-03-31 02:32:11 -08002102static struct dentry *lookup_hash(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103{
Al Viro72bd8662012-06-10 17:17:17 -04002104 return __lookup_hash(&nd->last, nd->path.dentry, nd->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105}
2106
Christoph Hellwigeead1912007-10-16 23:25:38 -07002107/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07002108 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07002109 * @name: pathname component to lookup
2110 * @base: base directory to lookup from
2111 * @len: maximum length @len should be interpreted to
2112 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07002113 * Note that this routine is purely a helper for filesystem usage and should
2114 * not be called by generic code. Also note that by using this function the
Christoph Hellwigeead1912007-10-16 23:25:38 -07002115 * nameidata argument is passed to the filesystem methods and a filesystem
2116 * using this helper needs to be prepared for that.
2117 */
James Morris057f6c02007-04-26 00:12:05 -07002118struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2119{
James Morris057f6c02007-04-26 00:12:05 -07002120 struct qstr this;
Al Viro6a96ba52011-03-07 23:49:20 -05002121 unsigned int c;
Miklos Szeredicda309d2012-03-26 12:54:21 +02002122 int err;
James Morris057f6c02007-04-26 00:12:05 -07002123
David Woodhouse2f9092e2009-04-20 23:18:37 +01002124 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
2125
Al Viro6a96ba52011-03-07 23:49:20 -05002126 this.name = name;
2127 this.len = len;
Linus Torvalds0145acc2012-03-02 14:32:59 -08002128 this.hash = full_name_hash(name, len);
Al Viro6a96ba52011-03-07 23:49:20 -05002129 if (!len)
2130 return ERR_PTR(-EACCES);
2131
Al Viro21d8a152012-11-29 22:17:21 -05002132 if (unlikely(name[0] == '.')) {
2133 if (len < 2 || (len == 2 && name[1] == '.'))
2134 return ERR_PTR(-EACCES);
2135 }
2136
Al Viro6a96ba52011-03-07 23:49:20 -05002137 while (len--) {
2138 c = *(const unsigned char *)name++;
2139 if (c == '/' || c == '\0')
2140 return ERR_PTR(-EACCES);
Al Viro6a96ba52011-03-07 23:49:20 -05002141 }
Al Viro5a202bc2011-03-08 14:17:44 -05002142 /*
2143 * See if the low-level filesystem might want
2144 * to use its own hash..
2145 */
2146 if (base->d_flags & DCACHE_OP_HASH) {
Linus Torvaldsda53be12013-05-21 15:22:44 -07002147 int err = base->d_op->d_hash(base, &this);
Al Viro5a202bc2011-03-08 14:17:44 -05002148 if (err < 0)
2149 return ERR_PTR(err);
2150 }
Christoph Hellwigeead1912007-10-16 23:25:38 -07002151
Miklos Szeredicda309d2012-03-26 12:54:21 +02002152 err = inode_permission(base->d_inode, MAY_EXEC);
2153 if (err)
2154 return ERR_PTR(err);
2155
Al Viro72bd8662012-06-10 17:17:17 -04002156 return __lookup_hash(&this, base, 0);
James Morris057f6c02007-04-26 00:12:05 -07002157}
2158
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002159int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2160 struct path *path, int *empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161{
Al Viro2d8f3032008-07-22 09:59:21 -04002162 struct nameidata nd;
Jeff Layton91a27b22012-10-10 15:25:28 -04002163 struct filename *tmp = getname_flags(name, flags, empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 int err = PTR_ERR(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 if (!IS_ERR(tmp)) {
Al Viro2d8f3032008-07-22 09:59:21 -04002166
2167 BUG_ON(flags & LOOKUP_PARENT);
2168
Jeff Layton873f1ee2012-10-10 15:25:29 -04002169 err = filename_lookup(dfd, tmp, flags, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 putname(tmp);
Al Viro2d8f3032008-07-22 09:59:21 -04002171 if (!err)
2172 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 }
2174 return err;
2175}
2176
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002177int user_path_at(int dfd, const char __user *name, unsigned flags,
2178 struct path *path)
2179{
Linus Torvaldsf7493e52012-03-22 16:10:40 -07002180 return user_path_at_empty(dfd, name, flags, path, NULL);
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002181}
2182
Jeff Layton873f1ee2012-10-10 15:25:29 -04002183/*
2184 * NB: most callers don't do anything directly with the reference to the
2185 * to struct filename, but the nd->last pointer points into the name string
2186 * allocated by getname. So we must hold the reference to it until all
2187 * path-walking is complete.
2188 */
Jeff Layton91a27b22012-10-10 15:25:28 -04002189static struct filename *
Jeff Layton9e790bd2012-12-11 12:10:09 -05002190user_path_parent(int dfd, const char __user *path, struct nameidata *nd,
2191 unsigned int flags)
Al Viro2ad94ae2008-07-21 09:32:51 -04002192{
Jeff Layton91a27b22012-10-10 15:25:28 -04002193 struct filename *s = getname(path);
Al Viro2ad94ae2008-07-21 09:32:51 -04002194 int error;
2195
Jeff Layton9e790bd2012-12-11 12:10:09 -05002196 /* only LOOKUP_REVAL is allowed in extra flags */
2197 flags &= LOOKUP_REVAL;
2198
Al Viro2ad94ae2008-07-21 09:32:51 -04002199 if (IS_ERR(s))
Jeff Layton91a27b22012-10-10 15:25:28 -04002200 return s;
Al Viro2ad94ae2008-07-21 09:32:51 -04002201
Jeff Layton9e790bd2012-12-11 12:10:09 -05002202 error = filename_lookup(dfd, s, flags | LOOKUP_PARENT, nd);
Jeff Layton91a27b22012-10-10 15:25:28 -04002203 if (error) {
Al Viro2ad94ae2008-07-21 09:32:51 -04002204 putname(s);
Jeff Layton91a27b22012-10-10 15:25:28 -04002205 return ERR_PTR(error);
2206 }
Al Viro2ad94ae2008-07-21 09:32:51 -04002207
Jeff Layton91a27b22012-10-10 15:25:28 -04002208 return s;
Al Viro2ad94ae2008-07-21 09:32:51 -04002209}
2210
Jeff Layton80334262013-07-26 06:23:25 -04002211/**
Al Viro197df042013-09-08 14:03:27 -04002212 * mountpoint_last - look up last component for umount
Jeff Layton80334262013-07-26 06:23:25 -04002213 * @nd: pathwalk nameidata - currently pointing at parent directory of "last"
2214 * @path: pointer to container for result
2215 *
2216 * This is a special lookup_last function just for umount. In this case, we
2217 * need to resolve the path without doing any revalidation.
2218 *
2219 * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
2220 * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
2221 * in almost all cases, this lookup will be served out of the dcache. The only
2222 * cases where it won't are if nd->last refers to a symlink or the path is
2223 * bogus and it doesn't exist.
2224 *
2225 * Returns:
2226 * -error: if there was an error during lookup. This includes -ENOENT if the
2227 * lookup found a negative dentry. The nd->path reference will also be
2228 * put in this case.
2229 *
2230 * 0: if we successfully resolved nd->path and found it to not to be a
2231 * symlink that needs to be followed. "path" will also be populated.
2232 * The nd->path reference will also be put.
2233 *
2234 * 1: if we successfully resolved nd->last and found it to be a symlink
2235 * that needs to be followed. "path" will be populated with the path
2236 * to the link, and nd->path will *not* be put.
2237 */
2238static int
Al Viro197df042013-09-08 14:03:27 -04002239mountpoint_last(struct nameidata *nd, struct path *path)
Jeff Layton80334262013-07-26 06:23:25 -04002240{
2241 int error = 0;
2242 struct dentry *dentry;
2243 struct dentry *dir = nd->path.dentry;
2244
Al Viro35759522013-09-08 13:41:33 -04002245 /* If we're in rcuwalk, drop out of it to handle last component */
2246 if (nd->flags & LOOKUP_RCU) {
2247 if (unlazy_walk(nd, NULL)) {
2248 error = -ECHILD;
2249 goto out;
2250 }
Jeff Layton80334262013-07-26 06:23:25 -04002251 }
2252
2253 nd->flags &= ~LOOKUP_PARENT;
2254
2255 if (unlikely(nd->last_type != LAST_NORM)) {
2256 error = handle_dots(nd, nd->last_type);
Al Viro35759522013-09-08 13:41:33 -04002257 if (error)
2258 goto out;
2259 dentry = dget(nd->path.dentry);
2260 goto done;
Jeff Layton80334262013-07-26 06:23:25 -04002261 }
2262
2263 mutex_lock(&dir->d_inode->i_mutex);
2264 dentry = d_lookup(dir, &nd->last);
2265 if (!dentry) {
2266 /*
2267 * No cached dentry. Mounted dentries are pinned in the cache,
2268 * so that means that this dentry is probably a symlink or the
2269 * path doesn't actually point to a mounted dentry.
2270 */
2271 dentry = d_alloc(dir, &nd->last);
2272 if (!dentry) {
2273 error = -ENOMEM;
Al Viro35759522013-09-08 13:41:33 -04002274 goto out;
Jeff Layton80334262013-07-26 06:23:25 -04002275 }
Al Viro35759522013-09-08 13:41:33 -04002276 dentry = lookup_real(dir->d_inode, dentry, nd->flags);
2277 error = PTR_ERR(dentry);
2278 if (IS_ERR(dentry))
2279 goto out;
Jeff Layton80334262013-07-26 06:23:25 -04002280 }
2281 mutex_unlock(&dir->d_inode->i_mutex);
2282
Al Viro35759522013-09-08 13:41:33 -04002283done:
2284 if (!dentry->d_inode) {
2285 error = -ENOENT;
2286 dput(dentry);
2287 goto out;
Jeff Layton80334262013-07-26 06:23:25 -04002288 }
Al Viro35759522013-09-08 13:41:33 -04002289 path->dentry = dentry;
2290 path->mnt = mntget(nd->path.mnt);
2291 if (should_follow_link(dentry->d_inode, nd->flags & LOOKUP_FOLLOW))
2292 return 1;
2293 follow_mount(path);
2294 error = 0;
2295out:
Jeff Layton80334262013-07-26 06:23:25 -04002296 terminate_walk(nd);
2297 return error;
2298}
2299
2300/**
Al Viro197df042013-09-08 14:03:27 -04002301 * path_mountpoint - look up a path to be umounted
Jeff Layton80334262013-07-26 06:23:25 -04002302 * @dfd: directory file descriptor to start walk from
2303 * @name: full pathname to walk
2304 * @flags: lookup flags
Jeff Layton80334262013-07-26 06:23:25 -04002305 *
2306 * Look up the given name, but don't attempt to revalidate the last component.
2307 * Returns 0 and "path" will be valid on success; Retuns error otherwise.
2308 */
2309static int
Al Viro197df042013-09-08 14:03:27 -04002310path_mountpoint(int dfd, const char *name, struct path *path, unsigned int flags)
Jeff Layton80334262013-07-26 06:23:25 -04002311{
2312 struct file *base = NULL;
2313 struct nameidata nd;
2314 int err;
2315
2316 err = path_init(dfd, name, flags | LOOKUP_PARENT, &nd, &base);
2317 if (unlikely(err))
2318 return err;
2319
2320 current->total_link_count = 0;
2321 err = link_path_walk(name, &nd);
2322 if (err)
2323 goto out;
2324
Al Viro197df042013-09-08 14:03:27 -04002325 err = mountpoint_last(&nd, path);
Jeff Layton80334262013-07-26 06:23:25 -04002326 while (err > 0) {
2327 void *cookie;
2328 struct path link = *path;
2329 err = may_follow_link(&link, &nd);
2330 if (unlikely(err))
2331 break;
2332 nd.flags |= LOOKUP_PARENT;
2333 err = follow_link(&link, &nd, &cookie);
2334 if (err)
2335 break;
Al Viro197df042013-09-08 14:03:27 -04002336 err = mountpoint_last(&nd, path);
Jeff Layton80334262013-07-26 06:23:25 -04002337 put_link(&nd, &link, cookie);
2338 }
2339out:
2340 if (base)
2341 fput(base);
2342
2343 if (nd.root.mnt && !(nd.flags & LOOKUP_ROOT))
2344 path_put(&nd.root);
2345
2346 return err;
2347}
2348
Al Viro2d864652013-09-08 20:18:44 -04002349static int
2350filename_mountpoint(int dfd, struct filename *s, struct path *path,
2351 unsigned int flags)
2352{
2353 int error = path_mountpoint(dfd, s->name, path, flags | LOOKUP_RCU);
2354 if (unlikely(error == -ECHILD))
2355 error = path_mountpoint(dfd, s->name, path, flags);
2356 if (unlikely(error == -ESTALE))
2357 error = path_mountpoint(dfd, s->name, path, flags | LOOKUP_REVAL);
2358 if (likely(!error))
2359 audit_inode(s, path->dentry, 0);
2360 return error;
2361}
2362
Jeff Layton80334262013-07-26 06:23:25 -04002363/**
Al Viro197df042013-09-08 14:03:27 -04002364 * user_path_mountpoint_at - lookup a path from userland in order to umount it
Jeff Layton80334262013-07-26 06:23:25 -04002365 * @dfd: directory file descriptor
2366 * @name: pathname from userland
2367 * @flags: lookup flags
2368 * @path: pointer to container to hold result
2369 *
2370 * A umount is a special case for path walking. We're not actually interested
2371 * in the inode in this situation, and ESTALE errors can be a problem. We
2372 * simply want track down the dentry and vfsmount attached at the mountpoint
2373 * and avoid revalidating the last component.
2374 *
2375 * Returns 0 and populates "path" on success.
2376 */
2377int
Al Viro197df042013-09-08 14:03:27 -04002378user_path_mountpoint_at(int dfd, const char __user *name, unsigned int flags,
Jeff Layton80334262013-07-26 06:23:25 -04002379 struct path *path)
2380{
2381 struct filename *s = getname(name);
2382 int error;
Jeff Layton80334262013-07-26 06:23:25 -04002383 if (IS_ERR(s))
2384 return PTR_ERR(s);
Al Viro2d864652013-09-08 20:18:44 -04002385 error = filename_mountpoint(dfd, s, path, flags);
Jeff Layton80334262013-07-26 06:23:25 -04002386 putname(s);
2387 return error;
2388}
2389
Al Viro2d864652013-09-08 20:18:44 -04002390int
2391kern_path_mountpoint(int dfd, const char *name, struct path *path,
2392 unsigned int flags)
2393{
2394 struct filename s = {.name = name};
2395 return filename_mountpoint(dfd, &s, path, flags);
2396}
2397EXPORT_SYMBOL(kern_path_mountpoint);
2398
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399/*
2400 * It's inline, so penalty for filesystems that don't use sticky bit is
2401 * minimal.
2402 */
2403static inline int check_sticky(struct inode *dir, struct inode *inode)
2404{
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002405 kuid_t fsuid = current_fsuid();
David Howellsda9592e2008-11-14 10:39:05 +11002406
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 if (!(dir->i_mode & S_ISVTX))
2408 return 0;
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002409 if (uid_eq(inode->i_uid, fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 return 0;
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002411 if (uid_eq(dir->i_uid, fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 return 0;
Eric W. Biederman1a48e2a2011-11-14 16:24:06 -08002413 return !inode_capable(inode, CAP_FOWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414}
2415
2416/*
2417 * Check whether we can remove a link victim from directory dir, check
2418 * whether the type of victim is right.
2419 * 1. We can't do it if dir is read-only (done in permission())
2420 * 2. We should have write and exec permissions on dir
2421 * 3. We can't remove anything from append-only dir
2422 * 4. We can't do anything with immutable dir (done in permission())
2423 * 5. If the sticky bit on dir is set we should either
2424 * a. be owner of dir, or
2425 * b. be owner of victim, or
2426 * c. have CAP_FOWNER capability
2427 * 6. If the victim is append-only or immutable we can't do antyhing with
2428 * links pointing to it.
2429 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2430 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2431 * 9. We can't remove a root or mountpoint.
2432 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
2433 * nfs_async_unlink().
2434 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08002435static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436{
2437 int error;
2438
2439 if (!victim->d_inode)
2440 return -ENOENT;
2441
2442 BUG_ON(victim->d_parent->d_inode != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04002443 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
Al Virof419a2e2008-07-22 00:07:17 -04002445 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 if (error)
2447 return error;
2448 if (IS_APPEND(dir))
2449 return -EPERM;
2450 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
Hugh Dickinsf9454542008-11-19 15:36:38 -08002451 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 return -EPERM;
2453 if (isdir) {
2454 if (!S_ISDIR(victim->d_inode->i_mode))
2455 return -ENOTDIR;
2456 if (IS_ROOT(victim))
2457 return -EBUSY;
2458 } else if (S_ISDIR(victim->d_inode->i_mode))
2459 return -EISDIR;
2460 if (IS_DEADDIR(dir))
2461 return -ENOENT;
2462 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2463 return -EBUSY;
2464 return 0;
2465}
2466
2467/* Check whether we can create an object with dentry child in directory
2468 * dir.
2469 * 1. We can't do it if child already exists (open has special treatment for
2470 * this case, but since we are inlined it's OK)
2471 * 2. We can't do it if dir is read-only (done in permission())
2472 * 3. We should have write and exec permissions on dir
2473 * 4. We can't do it if dir is immutable (done in permission())
2474 */
Miklos Szeredia95164d2008-07-30 15:08:48 +02002475static inline int may_create(struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476{
2477 if (child->d_inode)
2478 return -EEXIST;
2479 if (IS_DEADDIR(dir))
2480 return -ENOENT;
Al Virof419a2e2008-07-22 00:07:17 -04002481 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482}
2483
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484/*
2485 * p1 and p2 should be directories on the same fs.
2486 */
2487struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2488{
2489 struct dentry *p;
2490
2491 if (p1 == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07002492 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 return NULL;
2494 }
2495
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002496 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002498 p = d_ancestor(p2, p1);
2499 if (p) {
2500 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2501 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
2502 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 }
2504
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002505 p = d_ancestor(p1, p2);
2506 if (p) {
2507 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2508 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2509 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 }
2511
Ingo Molnarf2eace22006-07-03 00:25:05 -07002512 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2513 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 return NULL;
2515}
2516
2517void unlock_rename(struct dentry *p1, struct dentry *p2)
2518{
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002519 mutex_unlock(&p1->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 if (p1 != p2) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002521 mutex_unlock(&p2->d_inode->i_mutex);
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002522 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 }
2524}
2525
Al Viro4acdaf22011-07-26 01:42:34 -04002526int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viro312b63f2012-06-10 18:09:36 -04002527 bool want_excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002529 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 if (error)
2531 return error;
2532
Al Viroacfa4382008-12-04 10:06:33 -05002533 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 return -EACCES; /* shouldn't it be ENOSYS? */
2535 mode &= S_IALLUGO;
2536 mode |= S_IFREG;
2537 error = security_inode_create(dir, dentry, mode);
2538 if (error)
2539 return error;
Al Viro312b63f2012-06-10 18:09:36 -04002540 error = dir->i_op->create(dir, dentry, mode, want_excl);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002541 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002542 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 return error;
2544}
2545
Al Viro73d049a2011-03-11 12:08:24 -05002546static int may_open(struct path *path, int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002548 struct dentry *dentry = path->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 struct inode *inode = dentry->d_inode;
2550 int error;
2551
Al Virobcda7652011-03-13 16:42:14 -04002552 /* O_PATH? */
2553 if (!acc_mode)
2554 return 0;
2555
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 if (!inode)
2557 return -ENOENT;
2558
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002559 switch (inode->i_mode & S_IFMT) {
2560 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002562 case S_IFDIR:
2563 if (acc_mode & MAY_WRITE)
2564 return -EISDIR;
2565 break;
2566 case S_IFBLK:
2567 case S_IFCHR:
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002568 if (path->mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002570 /*FALLTHRU*/
2571 case S_IFIFO:
2572 case S_IFSOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002574 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08002575 }
Dave Hansenb41572e2007-10-16 23:31:14 -07002576
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002577 error = inode_permission(inode, acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07002578 if (error)
2579 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05002580
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 /*
2582 * An append-only file must be opened in append mode for writing.
2583 */
2584 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05002585 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05002586 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05002588 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 }
2590
2591 /* O_NOATIME can only be set by the owner or superuser */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07002592 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
Al Viro7715b522009-12-16 03:54:00 -05002593 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
J. Bruce Fieldsf3c7691e2011-09-21 10:58:13 -04002595 return 0;
Al Viro7715b522009-12-16 03:54:00 -05002596}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
Jeff Laytone1181ee2010-12-07 16:19:50 -05002598static int handle_truncate(struct file *filp)
Al Viro7715b522009-12-16 03:54:00 -05002599{
Jeff Laytone1181ee2010-12-07 16:19:50 -05002600 struct path *path = &filp->f_path;
Al Viro7715b522009-12-16 03:54:00 -05002601 struct inode *inode = path->dentry->d_inode;
2602 int error = get_write_access(inode);
2603 if (error)
2604 return error;
2605 /*
2606 * Refuse to truncate files with mandatory locks held on them.
2607 */
2608 error = locks_verify_locked(inode);
2609 if (!error)
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +09002610 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05002611 if (!error) {
2612 error = do_truncate(path->dentry, 0,
2613 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
Jeff Laytone1181ee2010-12-07 16:19:50 -05002614 filp);
Al Viro7715b522009-12-16 03:54:00 -05002615 }
2616 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04002617 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618}
2619
Dave Hansend57999e2008-02-15 14:37:27 -08002620static inline int open_to_namei_flags(int flag)
2621{
Al Viro8a5e9292011-06-25 19:15:54 -04002622 if ((flag & O_ACCMODE) == 3)
2623 flag--;
Dave Hansend57999e2008-02-15 14:37:27 -08002624 return flag;
2625}
2626
Miklos Szeredid18e9002012-06-05 15:10:17 +02002627static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode)
2628{
2629 int error = security_path_mknod(dir, dentry, mode, 0);
2630 if (error)
2631 return error;
2632
2633 error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
2634 if (error)
2635 return error;
2636
2637 return security_inode_create(dir->dentry->d_inode, dentry, mode);
2638}
2639
David Howells1acf0af2012-06-14 16:13:46 +01002640/*
2641 * Attempt to atomically look up, create and open a file from a negative
2642 * dentry.
2643 *
2644 * Returns 0 if successful. The file will have been created and attached to
2645 * @file by the filesystem calling finish_open().
2646 *
2647 * Returns 1 if the file was looked up only or didn't need creating. The
2648 * caller will need to perform the open themselves. @path will have been
2649 * updated to point to the new dentry. This may be negative.
2650 *
2651 * Returns an error code otherwise.
2652 */
Al Viro2675a4e2012-06-22 12:41:10 +04002653static int atomic_open(struct nameidata *nd, struct dentry *dentry,
2654 struct path *path, struct file *file,
2655 const struct open_flags *op,
Al Viro64894cf2012-07-31 00:53:35 +04002656 bool got_write, bool need_lookup,
Al Viro2675a4e2012-06-22 12:41:10 +04002657 int *opened)
Miklos Szeredid18e9002012-06-05 15:10:17 +02002658{
2659 struct inode *dir = nd->path.dentry->d_inode;
2660 unsigned open_flag = open_to_namei_flags(op->open_flag);
2661 umode_t mode;
2662 int error;
2663 int acc_mode;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002664 int create_error = 0;
2665 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
2666
2667 BUG_ON(dentry->d_inode);
2668
2669 /* Don't create child dentry for a dead directory. */
2670 if (unlikely(IS_DEADDIR(dir))) {
Al Viro2675a4e2012-06-22 12:41:10 +04002671 error = -ENOENT;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002672 goto out;
2673 }
2674
Miklos Szeredi62b259d2012-08-15 13:01:24 +02002675 mode = op->mode;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002676 if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
2677 mode &= ~current_umask();
2678
Al Virof8310c52012-07-30 11:50:30 +04002679 if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT)) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002680 open_flag &= ~O_TRUNC;
Al Viro47237682012-06-10 05:01:45 -04002681 *opened |= FILE_CREATED;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002682 }
2683
2684 /*
2685 * Checking write permission is tricky, bacuse we don't know if we are
2686 * going to actually need it: O_CREAT opens should work as long as the
2687 * file exists. But checking existence breaks atomicity. The trick is
2688 * to check access and if not granted clear O_CREAT from the flags.
2689 *
2690 * Another problem is returing the "right" error value (e.g. for an
2691 * O_EXCL open we want to return EEXIST not EROFS).
2692 */
Al Viro64894cf2012-07-31 00:53:35 +04002693 if (((open_flag & (O_CREAT | O_TRUNC)) ||
2694 (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) {
2695 if (!(open_flag & O_CREAT)) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002696 /*
2697 * No O_CREATE -> atomicity not a requirement -> fall
2698 * back to lookup + open
2699 */
2700 goto no_open;
2701 } else if (open_flag & (O_EXCL | O_TRUNC)) {
2702 /* Fall back and fail with the right error */
Al Viro64894cf2012-07-31 00:53:35 +04002703 create_error = -EROFS;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002704 goto no_open;
2705 } else {
2706 /* No side effects, safe to clear O_CREAT */
Al Viro64894cf2012-07-31 00:53:35 +04002707 create_error = -EROFS;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002708 open_flag &= ~O_CREAT;
2709 }
2710 }
2711
2712 if (open_flag & O_CREAT) {
Miklos Szeredi38227f72012-08-15 13:01:24 +02002713 error = may_o_create(&nd->path, dentry, mode);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002714 if (error) {
2715 create_error = error;
2716 if (open_flag & O_EXCL)
2717 goto no_open;
2718 open_flag &= ~O_CREAT;
2719 }
2720 }
2721
2722 if (nd->flags & LOOKUP_DIRECTORY)
2723 open_flag |= O_DIRECTORY;
2724
Al Viro30d90492012-06-22 12:40:19 +04002725 file->f_path.dentry = DENTRY_NOT_SET;
2726 file->f_path.mnt = nd->path.mnt;
2727 error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode,
Al Viro47237682012-06-10 05:01:45 -04002728 opened);
Al Virod9585272012-06-22 12:39:14 +04002729 if (error < 0) {
Al Virod9585272012-06-22 12:39:14 +04002730 if (create_error && error == -ENOENT)
2731 error = create_error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002732 goto out;
2733 }
2734
2735 acc_mode = op->acc_mode;
Al Viro47237682012-06-10 05:01:45 -04002736 if (*opened & FILE_CREATED) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002737 fsnotify_create(dir, dentry);
2738 acc_mode = MAY_OPEN;
2739 }
2740
Al Virod9585272012-06-22 12:39:14 +04002741 if (error) { /* returned 1, that is */
Al Viro30d90492012-06-22 12:40:19 +04002742 if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
Al Viro2675a4e2012-06-22 12:41:10 +04002743 error = -EIO;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002744 goto out;
2745 }
Al Viro30d90492012-06-22 12:40:19 +04002746 if (file->f_path.dentry) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002747 dput(dentry);
Al Viro30d90492012-06-22 12:40:19 +04002748 dentry = file->f_path.dentry;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002749 }
Sage Weil62b2ce92012-08-15 13:30:12 -07002750 if (create_error && dentry->d_inode == NULL) {
2751 error = create_error;
2752 goto out;
2753 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02002754 goto looked_up;
2755 }
2756
2757 /*
2758 * We didn't have the inode before the open, so check open permission
2759 * here.
2760 */
Al Viro2675a4e2012-06-22 12:41:10 +04002761 error = may_open(&file->f_path, acc_mode, open_flag);
2762 if (error)
2763 fput(file);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002764
2765out:
2766 dput(dentry);
Al Viro2675a4e2012-06-22 12:41:10 +04002767 return error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002768
Miklos Szeredid18e9002012-06-05 15:10:17 +02002769no_open:
2770 if (need_lookup) {
Al Viro72bd8662012-06-10 17:17:17 -04002771 dentry = lookup_real(dir, dentry, nd->flags);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002772 if (IS_ERR(dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04002773 return PTR_ERR(dentry);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002774
2775 if (create_error) {
2776 int open_flag = op->open_flag;
2777
Al Viro2675a4e2012-06-22 12:41:10 +04002778 error = create_error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002779 if ((open_flag & O_EXCL)) {
2780 if (!dentry->d_inode)
2781 goto out;
2782 } else if (!dentry->d_inode) {
2783 goto out;
2784 } else if ((open_flag & O_TRUNC) &&
2785 S_ISREG(dentry->d_inode->i_mode)) {
2786 goto out;
2787 }
2788 /* will fail later, go on to get the right error */
2789 }
2790 }
2791looked_up:
2792 path->dentry = dentry;
2793 path->mnt = nd->path.mnt;
Al Viro2675a4e2012-06-22 12:41:10 +04002794 return 1;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002795}
2796
Nick Piggin31e6b012011-01-07 17:49:52 +11002797/*
David Howells1acf0af2012-06-14 16:13:46 +01002798 * Look up and maybe create and open the last component.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002799 *
2800 * Must be called with i_mutex held on parent.
2801 *
David Howells1acf0af2012-06-14 16:13:46 +01002802 * Returns 0 if the file was successfully atomically created (if necessary) and
2803 * opened. In this case the file will be returned attached to @file.
2804 *
2805 * Returns 1 if the file was not completely opened at this time, though lookups
2806 * and creations will have been performed and the dentry returned in @path will
2807 * be positive upon return if O_CREAT was specified. If O_CREAT wasn't
2808 * specified then a negative dentry may be returned.
2809 *
2810 * An error code is returned otherwise.
2811 *
2812 * FILE_CREATE will be set in @*opened if the dentry was created and will be
2813 * cleared otherwise prior to returning.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002814 */
Al Viro2675a4e2012-06-22 12:41:10 +04002815static int lookup_open(struct nameidata *nd, struct path *path,
2816 struct file *file,
2817 const struct open_flags *op,
Al Viro64894cf2012-07-31 00:53:35 +04002818 bool got_write, int *opened)
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002819{
2820 struct dentry *dir = nd->path.dentry;
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002821 struct inode *dir_inode = dir->d_inode;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002822 struct dentry *dentry;
2823 int error;
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002824 bool need_lookup;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002825
Al Viro47237682012-06-10 05:01:45 -04002826 *opened &= ~FILE_CREATED;
Al Viro201f9562012-06-22 12:42:10 +04002827 dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002828 if (IS_ERR(dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04002829 return PTR_ERR(dentry);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002830
Miklos Szeredid18e9002012-06-05 15:10:17 +02002831 /* Cached positive dentry: will open in f_op->open */
2832 if (!need_lookup && dentry->d_inode)
2833 goto out_no_open;
2834
2835 if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) {
Al Viro64894cf2012-07-31 00:53:35 +04002836 return atomic_open(nd, dentry, path, file, op, got_write,
Al Viro47237682012-06-10 05:01:45 -04002837 need_lookup, opened);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002838 }
2839
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002840 if (need_lookup) {
2841 BUG_ON(dentry->d_inode);
2842
Al Viro72bd8662012-06-10 17:17:17 -04002843 dentry = lookup_real(dir_inode, dentry, nd->flags);
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002844 if (IS_ERR(dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04002845 return PTR_ERR(dentry);
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002846 }
2847
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002848 /* Negative dentry, just create the file */
2849 if (!dentry->d_inode && (op->open_flag & O_CREAT)) {
2850 umode_t mode = op->mode;
2851 if (!IS_POSIXACL(dir->d_inode))
2852 mode &= ~current_umask();
2853 /*
2854 * This write is needed to ensure that a
2855 * rw->ro transition does not occur between
2856 * the time when the file is created and when
2857 * a permanent write count is taken through
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02002858 * the 'struct file' in finish_open().
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002859 */
Al Viro64894cf2012-07-31 00:53:35 +04002860 if (!got_write) {
2861 error = -EROFS;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002862 goto out_dput;
Al Viro64894cf2012-07-31 00:53:35 +04002863 }
Al Viro47237682012-06-10 05:01:45 -04002864 *opened |= FILE_CREATED;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002865 error = security_path_mknod(&nd->path, dentry, mode, 0);
2866 if (error)
2867 goto out_dput;
Al Viro312b63f2012-06-10 18:09:36 -04002868 error = vfs_create(dir->d_inode, dentry, mode,
2869 nd->flags & LOOKUP_EXCL);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002870 if (error)
2871 goto out_dput;
2872 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02002873out_no_open:
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002874 path->dentry = dentry;
2875 path->mnt = nd->path.mnt;
Al Viro2675a4e2012-06-22 12:41:10 +04002876 return 1;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002877
2878out_dput:
2879 dput(dentry);
Al Viro2675a4e2012-06-22 12:41:10 +04002880 return error;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002881}
2882
2883/*
Al Virofe2d35f2011-03-05 22:58:25 -05002884 * Handle the last step of open()
Nick Piggin31e6b012011-01-07 17:49:52 +11002885 */
Al Viro2675a4e2012-06-22 12:41:10 +04002886static int do_last(struct nameidata *nd, struct path *path,
2887 struct file *file, const struct open_flags *op,
Jeff Layton669abf42012-10-10 16:43:10 -04002888 int *opened, struct filename *name)
Al Virofb1cc552009-12-24 01:58:28 -05002889{
Al Viroa1e28032009-12-24 02:12:06 -05002890 struct dentry *dir = nd->path.dentry;
Al Viroca344a892011-03-09 00:36:45 -05002891 int open_flag = op->open_flag;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002892 bool will_truncate = (open_flag & O_TRUNC) != 0;
Al Viro64894cf2012-07-31 00:53:35 +04002893 bool got_write = false;
Al Virobcda7652011-03-13 16:42:14 -04002894 int acc_mode = op->acc_mode;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02002895 struct inode *inode;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002896 bool symlink_ok = false;
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02002897 struct path save_parent = { .dentry = NULL, .mnt = NULL };
2898 bool retried = false;
Al Viro16c2cd72011-02-22 15:50:10 -05002899 int error;
Al Virofb1cc552009-12-24 01:58:28 -05002900
Al Viroc3e380b2011-02-23 13:39:45 -05002901 nd->flags &= ~LOOKUP_PARENT;
2902 nd->flags |= op->intent;
2903
Al Virobc77daa2013-06-06 09:12:33 -04002904 if (nd->last_type != LAST_NORM) {
Al Virofe2d35f2011-03-05 22:58:25 -05002905 error = handle_dots(nd, nd->last_type);
2906 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04002907 return error;
Miklos Szeredie83db162012-06-05 15:10:29 +02002908 goto finish_open;
Al Viro1f36f772009-12-26 10:56:19 -05002909 }
Al Viro67ee3ad2009-12-26 07:01:01 -05002910
Al Viroca344a892011-03-09 00:36:45 -05002911 if (!(open_flag & O_CREAT)) {
Al Virofe2d35f2011-03-05 22:58:25 -05002912 if (nd->last.name[nd->last.len])
2913 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
Al Virobcda7652011-03-13 16:42:14 -04002914 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002915 symlink_ok = true;
Al Virofe2d35f2011-03-05 22:58:25 -05002916 /* we _can_ be in RCU mode here */
Al Viroe97cdc82013-01-24 18:16:00 -05002917 error = lookup_fast(nd, path, &inode);
Miklos Szeredi71574862012-06-05 15:10:14 +02002918 if (likely(!error))
2919 goto finish_lookup;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02002920
Miklos Szeredi71574862012-06-05 15:10:14 +02002921 if (error < 0)
Al Viro2675a4e2012-06-22 12:41:10 +04002922 goto out;
Miklos Szeredi37d7fff2012-06-05 15:10:12 +02002923
Miklos Szeredi71574862012-06-05 15:10:14 +02002924 BUG_ON(nd->inode != dir->d_inode);
Miklos Szeredib6183df2012-06-05 15:10:13 +02002925 } else {
2926 /* create side of things */
2927 /*
2928 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
2929 * has been cleared when we got to the last component we are
2930 * about to look up
2931 */
2932 error = complete_walk(nd);
2933 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04002934 return error;
Miklos Szeredib6183df2012-06-05 15:10:13 +02002935
Jeff Layton33e22082013-04-12 15:16:32 -04002936 audit_inode(name, dir, LOOKUP_PARENT);
Miklos Szeredib6183df2012-06-05 15:10:13 +02002937 error = -EISDIR;
2938 /* trailing slashes? */
2939 if (nd->last.name[nd->last.len])
Al Viro2675a4e2012-06-22 12:41:10 +04002940 goto out;
Al Virofe2d35f2011-03-05 22:58:25 -05002941 }
2942
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02002943retry_lookup:
Al Viro64894cf2012-07-31 00:53:35 +04002944 if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
2945 error = mnt_want_write(nd->path.mnt);
2946 if (!error)
2947 got_write = true;
2948 /*
2949 * do _not_ fail yet - we might not need that or fail with
2950 * a different error; let lookup_open() decide; we'll be
2951 * dropping this one anyway.
2952 */
2953 }
Al Viroa1e28032009-12-24 02:12:06 -05002954 mutex_lock(&dir->d_inode->i_mutex);
Al Viro64894cf2012-07-31 00:53:35 +04002955 error = lookup_open(nd, path, file, op, got_write, opened);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002956 mutex_unlock(&dir->d_inode->i_mutex);
Al Viroa1e28032009-12-24 02:12:06 -05002957
Al Viro2675a4e2012-06-22 12:41:10 +04002958 if (error <= 0) {
2959 if (error)
Miklos Szeredid18e9002012-06-05 15:10:17 +02002960 goto out;
2961
Al Viro47237682012-06-10 05:01:45 -04002962 if ((*opened & FILE_CREATED) ||
Al Viro496ad9a2013-01-23 17:07:38 -05002963 !S_ISREG(file_inode(file)->i_mode))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002964 will_truncate = false;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002965
Jeff Laytonadb5c242012-10-10 16:43:13 -04002966 audit_inode(name, file->f_path.dentry, 0);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002967 goto opened;
2968 }
Al Virofb1cc552009-12-24 01:58:28 -05002969
Al Viro47237682012-06-10 05:01:45 -04002970 if (*opened & FILE_CREATED) {
Al Viro9b44f1b2011-03-09 00:17:27 -05002971 /* Don't check for write permission, don't truncate */
Al Viroca344a892011-03-09 00:36:45 -05002972 open_flag &= ~O_TRUNC;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002973 will_truncate = false;
Al Virobcda7652011-03-13 16:42:14 -04002974 acc_mode = MAY_OPEN;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002975 path_to_nameidata(path, nd);
Miklos Szeredie83db162012-06-05 15:10:29 +02002976 goto finish_open_created;
Al Virofb1cc552009-12-24 01:58:28 -05002977 }
2978
2979 /*
Jeff Layton3134f372012-07-25 10:19:47 -04002980 * create/update audit record if it already exists.
Al Virofb1cc552009-12-24 01:58:28 -05002981 */
Jeff Layton3134f372012-07-25 10:19:47 -04002982 if (path->dentry->d_inode)
Jeff Laytonadb5c242012-10-10 16:43:13 -04002983 audit_inode(name, path->dentry, 0);
Al Virofb1cc552009-12-24 01:58:28 -05002984
Miklos Szeredid18e9002012-06-05 15:10:17 +02002985 /*
2986 * If atomic_open() acquired write access it is dropped now due to
2987 * possible mount and symlink following (this might be optimized away if
2988 * necessary...)
2989 */
Al Viro64894cf2012-07-31 00:53:35 +04002990 if (got_write) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002991 mnt_drop_write(nd->path.mnt);
Al Viro64894cf2012-07-31 00:53:35 +04002992 got_write = false;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002993 }
2994
Al Virofb1cc552009-12-24 01:58:28 -05002995 error = -EEXIST;
Al Virof8310c52012-07-30 11:50:30 +04002996 if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))
Al Virofb1cc552009-12-24 01:58:28 -05002997 goto exit_dput;
2998
David Howells9875cf82011-01-14 18:45:21 +00002999 error = follow_managed(path, nd->flags);
3000 if (error < 0)
3001 goto exit_dput;
Al Virofb1cc552009-12-24 01:58:28 -05003002
Al Viroa3fbbde2011-11-07 21:21:26 +00003003 if (error)
3004 nd->flags |= LOOKUP_JUMPED;
3005
Miklos Szeredidecf3402012-05-21 17:30:08 +02003006 BUG_ON(nd->flags & LOOKUP_RCU);
3007 inode = path->dentry->d_inode;
Miklos Szeredi5f5daac2012-05-21 17:30:14 +02003008finish_lookup:
3009 /* we _can_ be in RCU mode here */
Al Virofb1cc552009-12-24 01:58:28 -05003010 error = -ENOENT;
Miklos Szeredi54c33e72012-05-21 17:30:10 +02003011 if (!inode) {
3012 path_to_nameidata(path, nd);
Al Viro2675a4e2012-06-22 12:41:10 +04003013 goto out;
Miklos Szeredi54c33e72012-05-21 17:30:10 +02003014 }
Al Viro9e67f362009-12-26 07:04:50 -05003015
Miklos Szeredid45ea862012-05-21 17:30:09 +02003016 if (should_follow_link(inode, !symlink_ok)) {
3017 if (nd->flags & LOOKUP_RCU) {
3018 if (unlikely(unlazy_walk(nd, path->dentry))) {
3019 error = -ECHILD;
Al Viro2675a4e2012-06-22 12:41:10 +04003020 goto out;
Miklos Szeredid45ea862012-05-21 17:30:09 +02003021 }
3022 }
3023 BUG_ON(inode != path->dentry->d_inode);
Al Viro2675a4e2012-06-22 12:41:10 +04003024 return 1;
Miklos Szeredid45ea862012-05-21 17:30:09 +02003025 }
Al Virofb1cc552009-12-24 01:58:28 -05003026
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003027 if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) {
3028 path_to_nameidata(path, nd);
3029 } else {
3030 save_parent.dentry = nd->path.dentry;
3031 save_parent.mnt = mntget(path->mnt);
3032 nd->path.dentry = path->dentry;
3033
3034 }
Miklos Szeredidecf3402012-05-21 17:30:08 +02003035 nd->inode = inode;
Al Viroa3fbbde2011-11-07 21:21:26 +00003036 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
Al Virobc77daa2013-06-06 09:12:33 -04003037finish_open:
Al Viroa3fbbde2011-11-07 21:21:26 +00003038 error = complete_walk(nd);
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003039 if (error) {
3040 path_put(&save_parent);
Al Viro2675a4e2012-06-22 12:41:10 +04003041 return error;
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003042 }
Al Virobc77daa2013-06-06 09:12:33 -04003043 audit_inode(name, nd->path.dentry, 0);
Al Virofb1cc552009-12-24 01:58:28 -05003044 error = -EISDIR;
Miklos Szeredi050ac842012-05-21 17:30:12 +02003045 if ((open_flag & O_CREAT) && S_ISDIR(nd->inode->i_mode))
Al Viro2675a4e2012-06-22 12:41:10 +04003046 goto out;
Miklos Szerediaf2f5542012-05-21 17:30:11 +02003047 error = -ENOTDIR;
Al Viro05252902013-06-06 19:33:47 -04003048 if ((nd->flags & LOOKUP_DIRECTORY) && !can_lookup(nd->inode))
Al Viro2675a4e2012-06-22 12:41:10 +04003049 goto out;
Al Viro6c0d46c2011-03-09 00:59:59 -05003050 if (!S_ISREG(nd->inode->i_mode))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003051 will_truncate = false;
Al Viro6c0d46c2011-03-09 00:59:59 -05003052
Al Viro0f9d1a12011-03-09 00:13:14 -05003053 if (will_truncate) {
3054 error = mnt_want_write(nd->path.mnt);
3055 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003056 goto out;
Al Viro64894cf2012-07-31 00:53:35 +04003057 got_write = true;
Al Viro0f9d1a12011-03-09 00:13:14 -05003058 }
Miklos Szeredie83db162012-06-05 15:10:29 +02003059finish_open_created:
Al Virobcda7652011-03-13 16:42:14 -04003060 error = may_open(&nd->path, acc_mode, open_flag);
Al Viroca344a892011-03-09 00:36:45 -05003061 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003062 goto out;
Al Viro30d90492012-06-22 12:40:19 +04003063 file->f_path.mnt = nd->path.mnt;
3064 error = finish_open(file, nd->path.dentry, NULL, opened);
3065 if (error) {
Al Viro30d90492012-06-22 12:40:19 +04003066 if (error == -EOPENSTALE)
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003067 goto stale_open;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003068 goto out;
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003069 }
Miklos Szeredia8277b92012-06-05 15:10:32 +02003070opened:
Al Viro2675a4e2012-06-22 12:41:10 +04003071 error = open_check_o_direct(file);
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003072 if (error)
3073 goto exit_fput;
Al Viro2675a4e2012-06-22 12:41:10 +04003074 error = ima_file_check(file, op->acc_mode);
Miklos Szerediaa4caad2012-06-05 15:10:28 +02003075 if (error)
3076 goto exit_fput;
3077
3078 if (will_truncate) {
Al Viro2675a4e2012-06-22 12:41:10 +04003079 error = handle_truncate(file);
Miklos Szerediaa4caad2012-06-05 15:10:28 +02003080 if (error)
3081 goto exit_fput;
Al Viro0f9d1a12011-03-09 00:13:14 -05003082 }
Al Viroca344a892011-03-09 00:36:45 -05003083out:
Al Viro64894cf2012-07-31 00:53:35 +04003084 if (got_write)
Al Viro0f9d1a12011-03-09 00:13:14 -05003085 mnt_drop_write(nd->path.mnt);
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003086 path_put(&save_parent);
Miklos Szeredie276ae62012-05-21 17:30:06 +02003087 terminate_walk(nd);
Al Viro2675a4e2012-06-22 12:41:10 +04003088 return error;
Al Virofb1cc552009-12-24 01:58:28 -05003089
Al Virofb1cc552009-12-24 01:58:28 -05003090exit_dput:
3091 path_put_conditional(path, nd);
Al Viroca344a892011-03-09 00:36:45 -05003092 goto out;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003093exit_fput:
Al Viro2675a4e2012-06-22 12:41:10 +04003094 fput(file);
3095 goto out;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003096
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003097stale_open:
3098 /* If no saved parent or already retried then can't retry */
3099 if (!save_parent.dentry || retried)
3100 goto out;
3101
3102 BUG_ON(save_parent.dentry != dir);
3103 path_put(&nd->path);
3104 nd->path = save_parent;
3105 nd->inode = dir->d_inode;
3106 save_parent.mnt = NULL;
3107 save_parent.dentry = NULL;
Al Viro64894cf2012-07-31 00:53:35 +04003108 if (got_write) {
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003109 mnt_drop_write(nd->path.mnt);
Al Viro64894cf2012-07-31 00:53:35 +04003110 got_write = false;
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003111 }
3112 retried = true;
3113 goto retry_lookup;
Al Virofb1cc552009-12-24 01:58:28 -05003114}
3115
Al Viro60545d02013-06-07 01:20:27 -04003116static int do_tmpfile(int dfd, struct filename *pathname,
3117 struct nameidata *nd, int flags,
3118 const struct open_flags *op,
3119 struct file *file, int *opened)
3120{
3121 static const struct qstr name = QSTR_INIT("/", 1);
3122 struct dentry *dentry, *child;
3123 struct inode *dir;
3124 int error = path_lookupat(dfd, pathname->name,
3125 flags | LOOKUP_DIRECTORY, nd);
3126 if (unlikely(error))
3127 return error;
3128 error = mnt_want_write(nd->path.mnt);
3129 if (unlikely(error))
3130 goto out;
3131 /* we want directory to be writable */
3132 error = inode_permission(nd->inode, MAY_WRITE | MAY_EXEC);
3133 if (error)
3134 goto out2;
3135 dentry = nd->path.dentry;
3136 dir = dentry->d_inode;
3137 if (!dir->i_op->tmpfile) {
3138 error = -EOPNOTSUPP;
3139 goto out2;
3140 }
3141 child = d_alloc(dentry, &name);
3142 if (unlikely(!child)) {
3143 error = -ENOMEM;
3144 goto out2;
3145 }
3146 nd->flags &= ~LOOKUP_DIRECTORY;
3147 nd->flags |= op->intent;
3148 dput(nd->path.dentry);
3149 nd->path.dentry = child;
3150 error = dir->i_op->tmpfile(dir, nd->path.dentry, op->mode);
3151 if (error)
3152 goto out2;
3153 audit_inode(pathname, nd->path.dentry, 0);
3154 error = may_open(&nd->path, op->acc_mode, op->open_flag);
3155 if (error)
3156 goto out2;
3157 file->f_path.mnt = nd->path.mnt;
3158 error = finish_open(file, nd->path.dentry, NULL, opened);
3159 if (error)
3160 goto out2;
3161 error = open_check_o_direct(file);
Al Virof4e0c302013-06-11 08:34:36 +04003162 if (error) {
Al Viro60545d02013-06-07 01:20:27 -04003163 fput(file);
Al Virof4e0c302013-06-11 08:34:36 +04003164 } else if (!(op->open_flag & O_EXCL)) {
3165 struct inode *inode = file_inode(file);
3166 spin_lock(&inode->i_lock);
3167 inode->i_state |= I_LINKABLE;
3168 spin_unlock(&inode->i_lock);
3169 }
Al Viro60545d02013-06-07 01:20:27 -04003170out2:
3171 mnt_drop_write(nd->path.mnt);
3172out:
3173 path_put(&nd->path);
3174 return error;
3175}
3176
Jeff Layton669abf42012-10-10 16:43:10 -04003177static struct file *path_openat(int dfd, struct filename *pathname,
Al Viro73d049a2011-03-11 12:08:24 -05003178 struct nameidata *nd, const struct open_flags *op, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179{
Al Virofe2d35f2011-03-05 22:58:25 -05003180 struct file *base = NULL;
Al Viro30d90492012-06-22 12:40:19 +04003181 struct file *file;
Al Viro9850c052010-01-13 15:01:15 -05003182 struct path path;
Al Viro47237682012-06-10 05:01:45 -04003183 int opened = 0;
Al Viro13aab422011-02-23 17:54:08 -05003184 int error;
Nick Piggin31e6b012011-01-07 17:49:52 +11003185
Al Viro30d90492012-06-22 12:40:19 +04003186 file = get_empty_filp();
Al Viro1afc99b2013-02-14 20:41:04 -05003187 if (IS_ERR(file))
3188 return file;
Nick Piggin31e6b012011-01-07 17:49:52 +11003189
Al Viro30d90492012-06-22 12:40:19 +04003190 file->f_flags = op->open_flag;
Nick Piggin31e6b012011-01-07 17:49:52 +11003191
Al Virobb458c62013-07-13 13:26:37 +04003192 if (unlikely(file->f_flags & __O_TMPFILE)) {
Al Viro60545d02013-06-07 01:20:27 -04003193 error = do_tmpfile(dfd, pathname, nd, flags, op, file, &opened);
3194 goto out;
3195 }
3196
Jeff Layton669abf42012-10-10 16:43:10 -04003197 error = path_init(dfd, pathname->name, flags | LOOKUP_PARENT, nd, &base);
Nick Piggin31e6b012011-01-07 17:49:52 +11003198 if (unlikely(error))
Al Viro2675a4e2012-06-22 12:41:10 +04003199 goto out;
Nick Piggin31e6b012011-01-07 17:49:52 +11003200
Al Virofe2d35f2011-03-05 22:58:25 -05003201 current->total_link_count = 0;
Jeff Layton669abf42012-10-10 16:43:10 -04003202 error = link_path_walk(pathname->name, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11003203 if (unlikely(error))
Al Viro2675a4e2012-06-22 12:41:10 +04003204 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205
Al Viro2675a4e2012-06-22 12:41:10 +04003206 error = do_last(nd, &path, file, op, &opened, pathname);
3207 while (unlikely(error > 0)) { /* trailing symlink */
Nick Piggin7b9337a2011-01-14 08:42:43 +00003208 struct path link = path;
Al Virodef4af32009-12-26 08:37:05 -05003209 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04003210 if (!(nd->flags & LOOKUP_FOLLOW)) {
Al Viro73d049a2011-03-11 12:08:24 -05003211 path_put_conditional(&path, nd);
3212 path_put(&nd->path);
Al Viro2675a4e2012-06-22 12:41:10 +04003213 error = -ELOOP;
Al Viro40b39132011-03-09 16:22:18 -05003214 break;
3215 }
Kees Cook800179c2012-07-25 17:29:07 -07003216 error = may_follow_link(&link, nd);
3217 if (unlikely(error))
3218 break;
Al Viro73d049a2011-03-11 12:08:24 -05003219 nd->flags |= LOOKUP_PARENT;
3220 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
Al Viro574197e2011-03-14 22:20:34 -04003221 error = follow_link(&link, nd, &cookie);
Al Viroc3e380b2011-02-23 13:39:45 -05003222 if (unlikely(error))
Al Viro2675a4e2012-06-22 12:41:10 +04003223 break;
3224 error = do_last(nd, &path, file, op, &opened, pathname);
Al Viro574197e2011-03-14 22:20:34 -04003225 put_link(nd, &link, cookie);
Al Viro806b6812009-12-26 07:16:40 -05003226 }
Al Viro10fa8e62009-12-26 07:09:49 -05003227out:
Al Viro73d049a2011-03-11 12:08:24 -05003228 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
3229 path_put(&nd->root);
Al Virofe2d35f2011-03-05 22:58:25 -05003230 if (base)
3231 fput(base);
Al Viro2675a4e2012-06-22 12:41:10 +04003232 if (!(opened & FILE_OPENED)) {
3233 BUG_ON(!error);
Al Viro30d90492012-06-22 12:40:19 +04003234 put_filp(file);
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003235 }
Al Viro2675a4e2012-06-22 12:41:10 +04003236 if (unlikely(error)) {
3237 if (error == -EOPENSTALE) {
3238 if (flags & LOOKUP_RCU)
3239 error = -ECHILD;
3240 else
3241 error = -ESTALE;
3242 }
3243 file = ERR_PTR(error);
3244 }
3245 return file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246}
3247
Jeff Layton669abf42012-10-10 16:43:10 -04003248struct file *do_filp_open(int dfd, struct filename *pathname,
Al Virof9652e12013-06-11 08:23:01 +04003249 const struct open_flags *op)
Al Viro13aab422011-02-23 17:54:08 -05003250{
Al Viro73d049a2011-03-11 12:08:24 -05003251 struct nameidata nd;
Al Virof9652e12013-06-11 08:23:01 +04003252 int flags = op->lookup_flags;
Al Viro13aab422011-02-23 17:54:08 -05003253 struct file *filp;
3254
Al Viro73d049a2011-03-11 12:08:24 -05003255 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
Al Viro13aab422011-02-23 17:54:08 -05003256 if (unlikely(filp == ERR_PTR(-ECHILD)))
Al Viro73d049a2011-03-11 12:08:24 -05003257 filp = path_openat(dfd, pathname, &nd, op, flags);
Al Viro13aab422011-02-23 17:54:08 -05003258 if (unlikely(filp == ERR_PTR(-ESTALE)))
Al Viro73d049a2011-03-11 12:08:24 -05003259 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
Al Viro13aab422011-02-23 17:54:08 -05003260 return filp;
3261}
3262
Al Viro73d049a2011-03-11 12:08:24 -05003263struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
Al Virof9652e12013-06-11 08:23:01 +04003264 const char *name, const struct open_flags *op)
Al Viro73d049a2011-03-11 12:08:24 -05003265{
3266 struct nameidata nd;
3267 struct file *file;
Jeff Layton669abf42012-10-10 16:43:10 -04003268 struct filename filename = { .name = name };
Al Virof9652e12013-06-11 08:23:01 +04003269 int flags = op->lookup_flags | LOOKUP_ROOT;
Al Viro73d049a2011-03-11 12:08:24 -05003270
3271 nd.root.mnt = mnt;
3272 nd.root.dentry = dentry;
3273
Al Virobcda7652011-03-13 16:42:14 -04003274 if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
Al Viro73d049a2011-03-11 12:08:24 -05003275 return ERR_PTR(-ELOOP);
3276
Jeff Layton669abf42012-10-10 16:43:10 -04003277 file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_RCU);
Al Viro73d049a2011-03-11 12:08:24 -05003278 if (unlikely(file == ERR_PTR(-ECHILD)))
Jeff Layton669abf42012-10-10 16:43:10 -04003279 file = path_openat(-1, &filename, &nd, op, flags);
Al Viro73d049a2011-03-11 12:08:24 -05003280 if (unlikely(file == ERR_PTR(-ESTALE)))
Jeff Layton669abf42012-10-10 16:43:10 -04003281 file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_REVAL);
Al Viro73d049a2011-03-11 12:08:24 -05003282 return file;
3283}
3284
Jeff Layton1ac12b42012-12-11 12:10:06 -05003285struct dentry *kern_path_create(int dfd, const char *pathname,
3286 struct path *path, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003288 struct dentry *dentry = ERR_PTR(-EEXIST);
Al Viroed75e952011-06-27 16:53:43 -04003289 struct nameidata nd;
Jan Karac30dabf2012-06-12 16:20:30 +02003290 int err2;
Jeff Layton1ac12b42012-12-11 12:10:06 -05003291 int error;
3292 bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
3293
3294 /*
3295 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
3296 * other flags passed in are ignored!
3297 */
3298 lookup_flags &= LOOKUP_REVAL;
3299
3300 error = do_path_lookup(dfd, pathname, LOOKUP_PARENT|lookup_flags, &nd);
Al Viroed75e952011-06-27 16:53:43 -04003301 if (error)
3302 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003304 /*
3305 * Yucky last component or no last component at all?
3306 * (foo/., foo/.., /////)
3307 */
Al Viroed75e952011-06-27 16:53:43 -04003308 if (nd.last_type != LAST_NORM)
3309 goto out;
3310 nd.flags &= ~LOOKUP_PARENT;
3311 nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003312
Jan Karac30dabf2012-06-12 16:20:30 +02003313 /* don't fail immediately if it's r/o, at least try to report other errors */
3314 err2 = mnt_want_write(nd.path.mnt);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003315 /*
3316 * Do the final lookup.
3317 */
Al Viroed75e952011-06-27 16:53:43 -04003318 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3319 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 if (IS_ERR(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003321 goto unlock;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003322
Al Viroa8104a92012-07-20 02:25:00 +04003323 error = -EEXIST;
Al Viroe9baf6e2008-05-15 04:49:12 -04003324 if (dentry->d_inode)
Al Viroa8104a92012-07-20 02:25:00 +04003325 goto fail;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003326 /*
3327 * Special case - lookup gave negative, but... we had foo/bar/
3328 * From the vfs_mknod() POV we just have a negative dentry -
3329 * all is fine. Let's be bastards - you had / on the end, you've
3330 * been asking for (non-existent) directory. -ENOENT for you.
3331 */
Al Viroed75e952011-06-27 16:53:43 -04003332 if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
Al Viroa8104a92012-07-20 02:25:00 +04003333 error = -ENOENT;
Al Viroed75e952011-06-27 16:53:43 -04003334 goto fail;
Al Viroe9baf6e2008-05-15 04:49:12 -04003335 }
Jan Karac30dabf2012-06-12 16:20:30 +02003336 if (unlikely(err2)) {
3337 error = err2;
Al Viroa8104a92012-07-20 02:25:00 +04003338 goto fail;
Jan Karac30dabf2012-06-12 16:20:30 +02003339 }
Al Viroed75e952011-06-27 16:53:43 -04003340 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342fail:
Al Viroa8104a92012-07-20 02:25:00 +04003343 dput(dentry);
3344 dentry = ERR_PTR(error);
3345unlock:
Al Viroed75e952011-06-27 16:53:43 -04003346 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Karac30dabf2012-06-12 16:20:30 +02003347 if (!err2)
3348 mnt_drop_write(nd.path.mnt);
Al Viroed75e952011-06-27 16:53:43 -04003349out:
3350 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 return dentry;
3352}
Al Virodae6ad82011-06-26 11:50:15 -04003353EXPORT_SYMBOL(kern_path_create);
3354
Al Viro921a1652012-07-20 01:15:31 +04003355void done_path_create(struct path *path, struct dentry *dentry)
3356{
3357 dput(dentry);
3358 mutex_unlock(&path->dentry->d_inode->i_mutex);
Al Viroa8104a92012-07-20 02:25:00 +04003359 mnt_drop_write(path->mnt);
Al Viro921a1652012-07-20 01:15:31 +04003360 path_put(path);
3361}
3362EXPORT_SYMBOL(done_path_create);
3363
Jeff Layton1ac12b42012-12-11 12:10:06 -05003364struct dentry *user_path_create(int dfd, const char __user *pathname,
3365 struct path *path, unsigned int lookup_flags)
Al Virodae6ad82011-06-26 11:50:15 -04003366{
Jeff Layton91a27b22012-10-10 15:25:28 -04003367 struct filename *tmp = getname(pathname);
Al Virodae6ad82011-06-26 11:50:15 -04003368 struct dentry *res;
3369 if (IS_ERR(tmp))
3370 return ERR_CAST(tmp);
Jeff Layton1ac12b42012-12-11 12:10:06 -05003371 res = kern_path_create(dfd, tmp->name, path, lookup_flags);
Al Virodae6ad82011-06-26 11:50:15 -04003372 putname(tmp);
3373 return res;
3374}
3375EXPORT_SYMBOL(user_path_create);
3376
Al Viro1a67aaf2011-07-26 01:52:52 -04003377int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003379 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380
3381 if (error)
3382 return error;
3383
Eric W. Biederman975d6b32011-11-13 12:16:43 -08003384 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 return -EPERM;
3386
Al Viroacfa4382008-12-04 10:06:33 -05003387 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 return -EPERM;
3389
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07003390 error = devcgroup_inode_mknod(mode, dev);
3391 if (error)
3392 return error;
3393
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 error = security_inode_mknod(dir, dentry, mode, dev);
3395 if (error)
3396 return error;
3397
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003399 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003400 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 return error;
3402}
3403
Al Virof69aac02011-07-26 04:31:40 -04003404static int may_mknod(umode_t mode)
Dave Hansen463c3192008-02-15 14:37:57 -08003405{
3406 switch (mode & S_IFMT) {
3407 case S_IFREG:
3408 case S_IFCHR:
3409 case S_IFBLK:
3410 case S_IFIFO:
3411 case S_IFSOCK:
3412 case 0: /* zero mode translates to S_IFREG */
3413 return 0;
3414 case S_IFDIR:
3415 return -EPERM;
3416 default:
3417 return -EINVAL;
3418 }
3419}
3420
Al Viro8208a222011-07-25 17:32:17 -04003421SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003422 unsigned, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423{
Al Viro2ad94ae2008-07-21 09:32:51 -04003424 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003425 struct path path;
3426 int error;
Jeff Layton972567f2012-12-20 16:00:10 -05003427 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428
Al Viro8e4bfca2012-07-20 01:17:26 +04003429 error = may_mknod(mode);
3430 if (error)
3431 return error;
Jeff Layton972567f2012-12-20 16:00:10 -05003432retry:
3433 dentry = user_path_create(dfd, filename, &path, lookup_flags);
Al Virodae6ad82011-06-26 11:50:15 -04003434 if (IS_ERR(dentry))
3435 return PTR_ERR(dentry);
Al Viro2ad94ae2008-07-21 09:32:51 -04003436
Al Virodae6ad82011-06-26 11:50:15 -04003437 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003438 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003439 error = security_path_mknod(&path, dentry, mode, dev);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003440 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04003441 goto out;
Dave Hansen463c3192008-02-15 14:37:57 -08003442 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 case 0: case S_IFREG:
Al Viro312b63f2012-06-10 18:09:36 -04003444 error = vfs_create(path.dentry->d_inode,dentry,mode,true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 break;
3446 case S_IFCHR: case S_IFBLK:
Al Virodae6ad82011-06-26 11:50:15 -04003447 error = vfs_mknod(path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 new_decode_dev(dev));
3449 break;
3450 case S_IFIFO: case S_IFSOCK:
Al Virodae6ad82011-06-26 11:50:15 -04003451 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 }
Al Viroa8104a92012-07-20 02:25:00 +04003454out:
Al Viro921a1652012-07-20 01:15:31 +04003455 done_path_create(&path, dentry);
Jeff Layton972567f2012-12-20 16:00:10 -05003456 if (retry_estale(error, lookup_flags)) {
3457 lookup_flags |= LOOKUP_REVAL;
3458 goto retry;
3459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 return error;
3461}
3462
Al Viro8208a222011-07-25 17:32:17 -04003463SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003464{
3465 return sys_mknodat(AT_FDCWD, filename, mode, dev);
3466}
3467
Al Viro18bb1db2011-07-26 01:41:39 -04003468int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003470 int error = may_create(dir, dentry);
Al Viro8de52772012-02-06 12:45:27 -05003471 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472
3473 if (error)
3474 return error;
3475
Al Viroacfa4382008-12-04 10:06:33 -05003476 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 return -EPERM;
3478
3479 mode &= (S_IRWXUGO|S_ISVTX);
3480 error = security_inode_mkdir(dir, dentry, mode);
3481 if (error)
3482 return error;
3483
Al Viro8de52772012-02-06 12:45:27 -05003484 if (max_links && dir->i_nlink >= max_links)
3485 return -EMLINK;
3486
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003488 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003489 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 return error;
3491}
3492
Al Viroa218d0f2011-11-21 14:59:34 -05003493SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494{
Dave Hansen6902d922006-09-30 23:29:01 -07003495 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003496 struct path path;
3497 int error;
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003498 unsigned int lookup_flags = LOOKUP_DIRECTORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003500retry:
3501 dentry = user_path_create(dfd, pathname, &path, lookup_flags);
Dave Hansen6902d922006-09-30 23:29:01 -07003502 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003503 return PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003504
Al Virodae6ad82011-06-26 11:50:15 -04003505 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003506 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003507 error = security_path_mkdir(&path, dentry, mode);
Al Viroa8104a92012-07-20 02:25:00 +04003508 if (!error)
3509 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
Al Viro921a1652012-07-20 01:15:31 +04003510 done_path_create(&path, dentry);
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003511 if (retry_estale(error, lookup_flags)) {
3512 lookup_flags |= LOOKUP_REVAL;
3513 goto retry;
3514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 return error;
3516}
3517
Al Viroa218d0f2011-11-21 14:59:34 -05003518SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003519{
3520 return sys_mkdirat(AT_FDCWD, pathname, mode);
3521}
3522
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523/*
Sage Weila71905f2011-05-24 13:06:08 -07003524 * The dentry_unhash() helper will try to drop the dentry early: we
J. Bruce Fieldsc0d02592012-02-15 11:48:40 -05003525 * should have a usage count of 1 if we're the only user of this
Sage Weila71905f2011-05-24 13:06:08 -07003526 * dentry, and if that is true (possibly after pruning the dcache),
3527 * then we drop the dentry now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 *
3529 * A low-level filesystem can, if it choses, legally
3530 * do a
3531 *
3532 * if (!d_unhashed(dentry))
3533 * return -EBUSY;
3534 *
3535 * if it cannot handle the case of removing a directory
3536 * that is still in use by something else..
3537 */
3538void dentry_unhash(struct dentry *dentry)
3539{
Vasily Averindc168422006-12-06 20:37:07 -08003540 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 spin_lock(&dentry->d_lock);
Waiman Long98474232013-08-28 18:24:59 -07003542 if (dentry->d_lockref.count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 __d_drop(dentry);
3544 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545}
3546
3547int vfs_rmdir(struct inode *dir, struct dentry *dentry)
3548{
3549 int error = may_delete(dir, dentry, 1);
3550
3551 if (error)
3552 return error;
3553
Al Viroacfa4382008-12-04 10:06:33 -05003554 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 return -EPERM;
3556
Al Viro1d2ef592011-09-14 18:55:41 +01003557 dget(dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003558 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559
Sage Weil912dbc12011-05-24 13:06:11 -07003560 error = -EBUSY;
3561 if (d_mountpoint(dentry))
3562 goto out;
3563
3564 error = security_inode_rmdir(dir, dentry);
3565 if (error)
3566 goto out;
3567
Sage Weil3cebde22011-05-29 21:20:59 -07003568 shrink_dcache_parent(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003569 error = dir->i_op->rmdir(dir, dentry);
3570 if (error)
3571 goto out;
3572
3573 dentry->d_inode->i_flags |= S_DEAD;
3574 dont_mount(dentry);
3575
3576out:
3577 mutex_unlock(&dentry->d_inode->i_mutex);
Al Viro1d2ef592011-09-14 18:55:41 +01003578 dput(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003579 if (!error)
3580 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 return error;
3582}
3583
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003584static long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585{
3586 int error = 0;
Jeff Layton91a27b22012-10-10 15:25:28 -04003587 struct filename *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 struct dentry *dentry;
3589 struct nameidata nd;
Jeff Laytonc6ee9202012-12-20 16:28:33 -05003590 unsigned int lookup_flags = 0;
3591retry:
3592 name = user_path_parent(dfd, pathname, &nd, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04003593 if (IS_ERR(name))
3594 return PTR_ERR(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595
3596 switch(nd.last_type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003597 case LAST_DOTDOT:
3598 error = -ENOTEMPTY;
3599 goto exit1;
3600 case LAST_DOT:
3601 error = -EINVAL;
3602 goto exit1;
3603 case LAST_ROOT:
3604 error = -EBUSY;
3605 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003607
3608 nd.flags &= ~LOOKUP_PARENT;
Jan Karac30dabf2012-06-12 16:20:30 +02003609 error = mnt_want_write(nd.path.mnt);
3610 if (error)
3611 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003612
Jan Blunck4ac91372008-02-14 19:34:32 -08003613 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08003614 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003616 if (IS_ERR(dentry))
3617 goto exit2;
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04003618 if (!dentry->d_inode) {
3619 error = -ENOENT;
3620 goto exit3;
3621 }
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003622 error = security_path_rmdir(&nd.path, dentry);
3623 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02003624 goto exit3;
Jan Blunck4ac91372008-02-14 19:34:32 -08003625 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
Dave Hansen06227532008-02-15 14:37:34 -08003626exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07003627 dput(dentry);
3628exit2:
Jan Blunck4ac91372008-02-14 19:34:32 -08003629 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Karac30dabf2012-06-12 16:20:30 +02003630 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003632 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 putname(name);
Jeff Laytonc6ee9202012-12-20 16:28:33 -05003634 if (retry_estale(error, lookup_flags)) {
3635 lookup_flags |= LOOKUP_REVAL;
3636 goto retry;
3637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 return error;
3639}
3640
Heiko Carstens3cdad422009-01-14 14:14:22 +01003641SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003642{
3643 return do_rmdir(AT_FDCWD, pathname);
3644}
3645
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646int vfs_unlink(struct inode *dir, struct dentry *dentry)
3647{
3648 int error = may_delete(dir, dentry, 0);
3649
3650 if (error)
3651 return error;
3652
Al Viroacfa4382008-12-04 10:06:33 -05003653 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 return -EPERM;
3655
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003656 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 if (d_mountpoint(dentry))
3658 error = -EBUSY;
3659 else {
3660 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05003661 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 error = dir->i_op->unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05003663 if (!error)
Al Virod83c49f2010-04-30 17:17:09 -04003664 dont_mount(dentry);
Al Virobec10522010-03-03 14:12:08 -05003665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003667 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668
3669 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
3670 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
Jan Karaece95912008-02-06 01:37:13 -08003671 fsnotify_link_count(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 }
Robert Love0eeca282005-07-12 17:06:03 -04003674
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 return error;
3676}
3677
3678/*
3679 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003680 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 * writeout happening, and we don't want to prevent access to the directory
3682 * while waiting on the I/O.
3683 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003684static long do_unlinkat(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685{
Al Viro2ad94ae2008-07-21 09:32:51 -04003686 int error;
Jeff Layton91a27b22012-10-10 15:25:28 -04003687 struct filename *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 struct dentry *dentry;
3689 struct nameidata nd;
3690 struct inode *inode = NULL;
Jeff Layton5d18f812012-12-20 16:38:04 -05003691 unsigned int lookup_flags = 0;
3692retry:
3693 name = user_path_parent(dfd, pathname, &nd, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04003694 if (IS_ERR(name))
3695 return PTR_ERR(name);
Al Viro2ad94ae2008-07-21 09:32:51 -04003696
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 error = -EISDIR;
3698 if (nd.last_type != LAST_NORM)
3699 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003700
3701 nd.flags &= ~LOOKUP_PARENT;
Jan Karac30dabf2012-06-12 16:20:30 +02003702 error = mnt_want_write(nd.path.mnt);
3703 if (error)
3704 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003705
Jan Blunck4ac91372008-02-14 19:34:32 -08003706 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08003707 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 error = PTR_ERR(dentry);
3709 if (!IS_ERR(dentry)) {
3710 /* Why not before? Because we want correct error value */
Török Edwin50338b82011-06-16 00:06:14 +03003711 if (nd.last.name[nd.last.len])
3712 goto slashes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 inode = dentry->d_inode;
Török Edwin50338b82011-06-16 00:06:14 +03003714 if (!inode)
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04003715 goto slashes;
3716 ihold(inode);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003717 error = security_path_unlink(&nd.path, dentry);
3718 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02003719 goto exit2;
Jan Blunck4ac91372008-02-14 19:34:32 -08003720 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
Jan Karac30dabf2012-06-12 16:20:30 +02003721exit2:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 dput(dentry);
3723 }
Jan Blunck4ac91372008-02-14 19:34:32 -08003724 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 if (inode)
3726 iput(inode); /* truncate the inode here */
Jan Karac30dabf2012-06-12 16:20:30 +02003727 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003729 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 putname(name);
Jeff Layton5d18f812012-12-20 16:38:04 -05003731 if (retry_estale(error, lookup_flags)) {
3732 lookup_flags |= LOOKUP_REVAL;
3733 inode = NULL;
3734 goto retry;
3735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736 return error;
3737
3738slashes:
3739 error = !dentry->d_inode ? -ENOENT :
3740 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
3741 goto exit2;
3742}
3743
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003744SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003745{
3746 if ((flag & ~AT_REMOVEDIR) != 0)
3747 return -EINVAL;
3748
3749 if (flag & AT_REMOVEDIR)
3750 return do_rmdir(dfd, pathname);
3751
3752 return do_unlinkat(dfd, pathname);
3753}
3754
Heiko Carstens3480b252009-01-14 14:14:16 +01003755SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003756{
3757 return do_unlinkat(AT_FDCWD, pathname);
3758}
3759
Miklos Szeredidb2e7472008-06-24 16:50:16 +02003760int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003762 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763
3764 if (error)
3765 return error;
3766
Al Viroacfa4382008-12-04 10:06:33 -05003767 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 return -EPERM;
3769
3770 error = security_inode_symlink(dir, dentry, oldname);
3771 if (error)
3772 return error;
3773
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003775 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003776 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 return error;
3778}
3779
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003780SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
3781 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782{
Al Viro2ad94ae2008-07-21 09:32:51 -04003783 int error;
Jeff Layton91a27b22012-10-10 15:25:28 -04003784 struct filename *from;
Dave Hansen6902d922006-09-30 23:29:01 -07003785 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003786 struct path path;
Jeff Laytonf46d3562012-12-11 12:10:08 -05003787 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788
3789 from = getname(oldname);
Al Viro2ad94ae2008-07-21 09:32:51 -04003790 if (IS_ERR(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 return PTR_ERR(from);
Jeff Laytonf46d3562012-12-11 12:10:08 -05003792retry:
3793 dentry = user_path_create(newdfd, newname, &path, lookup_flags);
Dave Hansen6902d922006-09-30 23:29:01 -07003794 error = PTR_ERR(dentry);
3795 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003796 goto out_putname;
Dave Hansen6902d922006-09-30 23:29:01 -07003797
Jeff Layton91a27b22012-10-10 15:25:28 -04003798 error = security_path_symlink(&path, dentry, from->name);
Al Viroa8104a92012-07-20 02:25:00 +04003799 if (!error)
Jeff Layton91a27b22012-10-10 15:25:28 -04003800 error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
Al Viro921a1652012-07-20 01:15:31 +04003801 done_path_create(&path, dentry);
Jeff Laytonf46d3562012-12-11 12:10:08 -05003802 if (retry_estale(error, lookup_flags)) {
3803 lookup_flags |= LOOKUP_REVAL;
3804 goto retry;
3805 }
Dave Hansen6902d922006-09-30 23:29:01 -07003806out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 putname(from);
3808 return error;
3809}
3810
Heiko Carstens3480b252009-01-14 14:14:16 +01003811SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003812{
3813 return sys_symlinkat(oldname, AT_FDCWD, newname);
3814}
3815
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
3817{
3818 struct inode *inode = old_dentry->d_inode;
Al Viro8de52772012-02-06 12:45:27 -05003819 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 int error;
3821
3822 if (!inode)
3823 return -ENOENT;
3824
Miklos Szeredia95164d2008-07-30 15:08:48 +02003825 error = may_create(dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 if (error)
3827 return error;
3828
3829 if (dir->i_sb != inode->i_sb)
3830 return -EXDEV;
3831
3832 /*
3833 * A link to an append-only or immutable file cannot be created.
3834 */
3835 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3836 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05003837 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003839 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 return -EPERM;
3841
3842 error = security_inode_link(old_dentry, dir, new_dentry);
3843 if (error)
3844 return error;
3845
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003846 mutex_lock(&inode->i_mutex);
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05303847 /* Make sure we don't allow creating hardlink to an unlinked file */
Al Virof4e0c302013-06-11 08:34:36 +04003848 if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05303849 error = -ENOENT;
Al Viro8de52772012-02-06 12:45:27 -05003850 else if (max_links && inode->i_nlink >= max_links)
3851 error = -EMLINK;
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05303852 else
3853 error = dir->i_op->link(old_dentry, dir, new_dentry);
Al Virof4e0c302013-06-11 08:34:36 +04003854
3855 if (!error && (inode->i_state & I_LINKABLE)) {
3856 spin_lock(&inode->i_lock);
3857 inode->i_state &= ~I_LINKABLE;
3858 spin_unlock(&inode->i_lock);
3859 }
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003860 mutex_unlock(&inode->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07003861 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003862 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 return error;
3864}
3865
3866/*
3867 * Hardlinks are often used in delicate situations. We avoid
3868 * security-related surprises by not following symlinks on the
3869 * newname. --KAB
3870 *
3871 * We don't follow them on the oldname either to be compatible
3872 * with linux 2.0, and to avoid hard-linking to directories
3873 * and other special files. --ADM
3874 */
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003875SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
3876 int, newdfd, const char __user *, newname, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877{
3878 struct dentry *new_dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003879 struct path old_path, new_path;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303880 int how = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303883 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003884 return -EINVAL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303885 /*
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07003886 * To use null names we require CAP_DAC_READ_SEARCH
3887 * This ensures that not everyone will be able to create
3888 * handlink using the passed filedescriptor.
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303889 */
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07003890 if (flags & AT_EMPTY_PATH) {
3891 if (!capable(CAP_DAC_READ_SEARCH))
3892 return -ENOENT;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303893 how = LOOKUP_EMPTY;
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07003894 }
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003895
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303896 if (flags & AT_SYMLINK_FOLLOW)
3897 how |= LOOKUP_FOLLOW;
Jeff Layton442e31c2012-12-20 16:15:38 -05003898retry:
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303899 error = user_path_at(olddfd, oldname, how, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04003901 return error;
3902
Jeff Layton442e31c2012-12-20 16:15:38 -05003903 new_dentry = user_path_create(newdfd, newname, &new_path,
3904 (how & LOOKUP_REVAL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003906 if (IS_ERR(new_dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003907 goto out;
3908
3909 error = -EXDEV;
3910 if (old_path.mnt != new_path.mnt)
3911 goto out_dput;
Kees Cook800179c2012-07-25 17:29:07 -07003912 error = may_linkat(&old_path);
3913 if (unlikely(error))
3914 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04003915 error = security_path_link(old_path.dentry, &new_path, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003916 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04003917 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04003918 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
Dave Hansen75c3f292008-02-15 14:37:45 -08003919out_dput:
Al Viro921a1652012-07-20 01:15:31 +04003920 done_path_create(&new_path, new_dentry);
Jeff Layton442e31c2012-12-20 16:15:38 -05003921 if (retry_estale(error, how)) {
3922 how |= LOOKUP_REVAL;
3923 goto retry;
3924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925out:
Al Viro2d8f3032008-07-22 09:59:21 -04003926 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927
3928 return error;
3929}
3930
Heiko Carstens3480b252009-01-14 14:14:16 +01003931SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003932{
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003933 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003934}
3935
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936/*
3937 * The worst of all namespace operations - renaming directory. "Perverted"
3938 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
3939 * Problems:
3940 * a) we can get into loop creation. Check is done in is_subdir().
3941 * b) race potential - two innocent renames can create a loop together.
3942 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08003943 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 * story.
3945 * c) we have to lock _three_ objects - parents and victim (if it exists).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003946 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 * whether the target exists). Solution: try to be smart with locking
3948 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08003949 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 * move will be locked. Thus we can rank directories by the tree
3951 * (ancestors first) and rank all non-directories after them.
3952 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08003953 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 * HOWEVER, it relies on the assumption that any object with ->lookup()
3955 * has no more than 1 dentry. If "hybrid" objects will ever appear,
3956 * we'd better make sure that there's no link(2) for them.
Sage Weile4eaac02011-05-24 13:06:07 -07003957 * d) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003958 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05003960 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 * locking].
3962 */
Adrian Bunk75c96f82005-05-05 16:16:09 -07003963static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
3964 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965{
3966 int error = 0;
Sage Weil9055cba2011-05-24 13:06:12 -07003967 struct inode *target = new_dentry->d_inode;
Al Viro8de52772012-02-06 12:45:27 -05003968 unsigned max_links = new_dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969
3970 /*
3971 * If we are going to change the parent - check write permissions,
3972 * we'll need to flip '..'.
3973 */
3974 if (new_dir != old_dir) {
Al Virof419a2e2008-07-22 00:07:17 -04003975 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 if (error)
3977 return error;
3978 }
3979
3980 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3981 if (error)
3982 return error;
3983
Al Viro1d2ef592011-09-14 18:55:41 +01003984 dget(new_dentry);
Al Virod83c49f2010-04-30 17:17:09 -04003985 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003986 mutex_lock(&target->i_mutex);
Sage Weil9055cba2011-05-24 13:06:12 -07003987
3988 error = -EBUSY;
3989 if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
3990 goto out;
3991
Al Viro8de52772012-02-06 12:45:27 -05003992 error = -EMLINK;
3993 if (max_links && !target && new_dir != old_dir &&
3994 new_dir->i_nlink >= max_links)
3995 goto out;
3996
Sage Weil3cebde22011-05-29 21:20:59 -07003997 if (target)
3998 shrink_dcache_parent(new_dentry);
Sage Weil9055cba2011-05-24 13:06:12 -07003999 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
4000 if (error)
4001 goto out;
4002
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 if (target) {
Sage Weil9055cba2011-05-24 13:06:12 -07004004 target->i_flags |= S_DEAD;
4005 dont_mount(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 }
Sage Weil9055cba2011-05-24 13:06:12 -07004007out:
4008 if (target)
4009 mutex_unlock(&target->i_mutex);
Al Viro1d2ef592011-09-14 18:55:41 +01004010 dput(new_dentry);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07004011 if (!error)
Mark Fasheh349457c2006-09-08 14:22:21 -07004012 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
4013 d_move(old_dentry,new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 return error;
4015}
4016
Adrian Bunk75c96f82005-05-05 16:16:09 -07004017static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
4018 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019{
Sage Weil51892bb2011-05-24 13:06:13 -07004020 struct inode *target = new_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 int error;
4022
4023 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
4024 if (error)
4025 return error;
4026
4027 dget(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004029 mutex_lock(&target->i_mutex);
Sage Weil51892bb2011-05-24 13:06:13 -07004030
4031 error = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
Sage Weil51892bb2011-05-24 13:06:13 -07004033 goto out;
4034
4035 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
4036 if (error)
4037 goto out;
4038
4039 if (target)
4040 dont_mount(new_dentry);
4041 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
4042 d_move(old_dentry, new_dentry);
4043out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004045 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 dput(new_dentry);
4047 return error;
4048}
4049
4050int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
4051 struct inode *new_dir, struct dentry *new_dentry)
4052{
4053 int error;
4054 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
Eric Paris59b0df22010-02-08 12:53:52 -05004055 const unsigned char *old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056
4057 if (old_dentry->d_inode == new_dentry->d_inode)
4058 return 0;
4059
4060 error = may_delete(old_dir, old_dentry, is_dir);
4061 if (error)
4062 return error;
4063
4064 if (!new_dentry->d_inode)
Miklos Szeredia95164d2008-07-30 15:08:48 +02004065 error = may_create(new_dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 else
4067 error = may_delete(new_dir, new_dentry, is_dir);
4068 if (error)
4069 return error;
4070
Al Viroacfa4382008-12-04 10:06:33 -05004071 if (!old_dir->i_op->rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 return -EPERM;
4073
Robert Love0eeca282005-07-12 17:06:03 -04004074 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
4075
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 if (is_dir)
4077 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
4078 else
4079 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
Al Viro123df292009-12-25 04:57:57 -05004080 if (!error)
4081 fsnotify_move(old_dir, new_dir, old_name, is_dir,
Al Viro5a190ae2007-06-07 12:19:32 -04004082 new_dentry->d_inode, old_dentry);
Robert Love0eeca282005-07-12 17:06:03 -04004083 fsnotify_oldname_free(old_name);
4084
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 return error;
4086}
4087
Heiko Carstens2e4d0922009-01-14 14:14:31 +01004088SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4089 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090{
Al Viro2ad94ae2008-07-21 09:32:51 -04004091 struct dentry *old_dir, *new_dir;
4092 struct dentry *old_dentry, *new_dentry;
4093 struct dentry *trap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 struct nameidata oldnd, newnd;
Jeff Layton91a27b22012-10-10 15:25:28 -04004095 struct filename *from;
4096 struct filename *to;
Jeff Laytonc6a94282012-12-11 12:10:10 -05004097 unsigned int lookup_flags = 0;
4098 bool should_retry = false;
Al Viro2ad94ae2008-07-21 09:32:51 -04004099 int error;
Jeff Laytonc6a94282012-12-11 12:10:10 -05004100retry:
4101 from = user_path_parent(olddfd, oldname, &oldnd, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04004102 if (IS_ERR(from)) {
4103 error = PTR_ERR(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 goto exit;
Jeff Layton91a27b22012-10-10 15:25:28 -04004105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106
Jeff Laytonc6a94282012-12-11 12:10:10 -05004107 to = user_path_parent(newdfd, newname, &newnd, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04004108 if (IS_ERR(to)) {
4109 error = PTR_ERR(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110 goto exit1;
Jeff Layton91a27b22012-10-10 15:25:28 -04004111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112
4113 error = -EXDEV;
Jan Blunck4ac91372008-02-14 19:34:32 -08004114 if (oldnd.path.mnt != newnd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 goto exit2;
4116
Jan Blunck4ac91372008-02-14 19:34:32 -08004117 old_dir = oldnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 error = -EBUSY;
4119 if (oldnd.last_type != LAST_NORM)
4120 goto exit2;
4121
Jan Blunck4ac91372008-02-14 19:34:32 -08004122 new_dir = newnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 if (newnd.last_type != LAST_NORM)
4124 goto exit2;
4125
Jan Karac30dabf2012-06-12 16:20:30 +02004126 error = mnt_want_write(oldnd.path.mnt);
4127 if (error)
4128 goto exit2;
4129
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004130 oldnd.flags &= ~LOOKUP_PARENT;
4131 newnd.flags &= ~LOOKUP_PARENT;
OGAWA Hirofumi4e9ed2f2008-10-16 07:50:29 +09004132 newnd.flags |= LOOKUP_RENAME_TARGET;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004133
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 trap = lock_rename(new_dir, old_dir);
4135
Christoph Hellwig49705b72005-11-08 21:35:06 -08004136 old_dentry = lookup_hash(&oldnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 error = PTR_ERR(old_dentry);
4138 if (IS_ERR(old_dentry))
4139 goto exit3;
4140 /* source must exist */
4141 error = -ENOENT;
4142 if (!old_dentry->d_inode)
4143 goto exit4;
4144 /* unless the source is a directory trailing slashes give -ENOTDIR */
4145 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
4146 error = -ENOTDIR;
4147 if (oldnd.last.name[oldnd.last.len])
4148 goto exit4;
4149 if (newnd.last.name[newnd.last.len])
4150 goto exit4;
4151 }
4152 /* source should not be ancestor of target */
4153 error = -EINVAL;
4154 if (old_dentry == trap)
4155 goto exit4;
Christoph Hellwig49705b72005-11-08 21:35:06 -08004156 new_dentry = lookup_hash(&newnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 error = PTR_ERR(new_dentry);
4158 if (IS_ERR(new_dentry))
4159 goto exit4;
4160 /* target should not be an ancestor of source */
4161 error = -ENOTEMPTY;
4162 if (new_dentry == trap)
4163 goto exit5;
4164
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004165 error = security_path_rename(&oldnd.path, old_dentry,
4166 &newnd.path, new_dentry);
4167 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02004168 goto exit5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 error = vfs_rename(old_dir->d_inode, old_dentry,
4170 new_dir->d_inode, new_dentry);
4171exit5:
4172 dput(new_dentry);
4173exit4:
4174 dput(old_dentry);
4175exit3:
4176 unlock_rename(new_dir, old_dir);
Jan Karac30dabf2012-06-12 16:20:30 +02004177 mnt_drop_write(oldnd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178exit2:
Jeff Laytonc6a94282012-12-11 12:10:10 -05004179 if (retry_estale(error, lookup_flags))
4180 should_retry = true;
Jan Blunck1d957f92008-02-14 19:34:35 -08004181 path_put(&newnd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04004182 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08004184 path_put(&oldnd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 putname(from);
Jeff Laytonc6a94282012-12-11 12:10:10 -05004186 if (should_retry) {
4187 should_retry = false;
4188 lookup_flags |= LOOKUP_REVAL;
4189 goto retry;
4190 }
Al Viro2ad94ae2008-07-21 09:32:51 -04004191exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 return error;
4193}
4194
Heiko Carstensa26eab22009-01-14 14:14:17 +01004195SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004196{
4197 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
4198}
4199
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
4201{
4202 int len;
4203
4204 len = PTR_ERR(link);
4205 if (IS_ERR(link))
4206 goto out;
4207
4208 len = strlen(link);
4209 if (len > (unsigned) buflen)
4210 len = buflen;
4211 if (copy_to_user(buffer, link, len))
4212 len = -EFAULT;
4213out:
4214 return len;
4215}
4216
4217/*
4218 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
4219 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
4220 * using) it for any given inode is up to filesystem.
4221 */
4222int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4223{
4224 struct nameidata nd;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004225 void *cookie;
Marcin Slusarz694a1762008-06-09 16:40:37 -07004226 int res;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004227
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 nd.depth = 0;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004229 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
Marcin Slusarz694a1762008-06-09 16:40:37 -07004230 if (IS_ERR(cookie))
4231 return PTR_ERR(cookie);
4232
4233 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
4234 if (dentry->d_inode->i_op->put_link)
4235 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
4236 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237}
4238
4239int vfs_follow_link(struct nameidata *nd, const char *link)
4240{
4241 return __vfs_follow_link(nd, link);
4242}
4243
4244/* get the link contents into pagecache */
4245static char *page_getlink(struct dentry * dentry, struct page **ppage)
4246{
Duane Griffinebd09ab2008-12-19 20:47:12 +00004247 char *kaddr;
4248 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 struct address_space *mapping = dentry->d_inode->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -07004250 page = read_mapping_page(mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 if (IS_ERR(page))
Nick Piggin6fe69002007-05-06 14:49:04 -07004252 return (char*)page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 *ppage = page;
Duane Griffinebd09ab2008-12-19 20:47:12 +00004254 kaddr = kmap(page);
4255 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
4256 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257}
4258
4259int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4260{
4261 struct page *page = NULL;
4262 char *s = page_getlink(dentry, &page);
4263 int res = vfs_readlink(dentry,buffer,buflen,s);
4264 if (page) {
4265 kunmap(page);
4266 page_cache_release(page);
4267 }
4268 return res;
4269}
4270
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004271void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004273 struct page *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 nd_set_link(nd, page_getlink(dentry, &page));
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004275 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276}
4277
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004278void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004280 struct page *page = cookie;
4281
4282 if (page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 kunmap(page);
4284 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 }
4286}
4287
Nick Piggin54566b22009-01-04 12:00:53 -08004288/*
4289 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
4290 */
4291int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292{
4293 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004294 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07004295 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08004296 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 char *kaddr;
Nick Piggin54566b22009-01-04 12:00:53 -08004298 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
4299 if (nofs)
4300 flags |= AOP_FLAG_NOFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301
NeilBrown7e53cac2006-03-25 03:07:57 -08004302retry:
Nick Pigginafddba42007-10-16 01:25:01 -07004303 err = pagecache_write_begin(NULL, mapping, 0, len-1,
Nick Piggin54566b22009-01-04 12:00:53 -08004304 flags, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07004306 goto fail;
4307
Cong Wange8e3c3d2011-11-25 23:14:27 +08004308 kaddr = kmap_atomic(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 memcpy(kaddr, symname, len-1);
Cong Wange8e3c3d2011-11-25 23:14:27 +08004310 kunmap_atomic(kaddr);
Nick Pigginafddba42007-10-16 01:25:01 -07004311
4312 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4313 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 if (err < 0)
4315 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07004316 if (err < len-1)
4317 goto retry;
4318
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 mark_inode_dirty(inode);
4320 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321fail:
4322 return err;
4323}
4324
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004325int page_symlink(struct inode *inode, const char *symname, int len)
4326{
4327 return __page_symlink(inode, symname, len,
Nick Piggin54566b22009-01-04 12:00:53 -08004328 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004329}
4330
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08004331const struct inode_operations page_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 .readlink = generic_readlink,
4333 .follow_link = page_follow_link_light,
4334 .put_link = page_put_link,
4335};
4336
Al Viro2d8f3032008-07-22 09:59:21 -04004337EXPORT_SYMBOL(user_path_at);
David Howellscc53ce52011-01-14 18:45:26 +00004338EXPORT_SYMBOL(follow_down_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339EXPORT_SYMBOL(follow_down);
4340EXPORT_SYMBOL(follow_up);
Al Virof6d2ac52012-08-26 12:55:54 -04004341EXPORT_SYMBOL(get_write_access); /* nfsd */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343EXPORT_SYMBOL(lookup_one_len);
4344EXPORT_SYMBOL(page_follow_link_light);
4345EXPORT_SYMBOL(page_put_link);
4346EXPORT_SYMBOL(page_readlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004347EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348EXPORT_SYMBOL(page_symlink);
4349EXPORT_SYMBOL(page_symlink_inode_operations);
Al Virod1811462008-08-02 00:49:18 -04004350EXPORT_SYMBOL(kern_path);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07004351EXPORT_SYMBOL(vfs_path_lookup);
Al Virof419a2e2008-07-22 00:07:17 -04004352EXPORT_SYMBOL(inode_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353EXPORT_SYMBOL(unlock_rename);
4354EXPORT_SYMBOL(vfs_create);
4355EXPORT_SYMBOL(vfs_follow_link);
4356EXPORT_SYMBOL(vfs_link);
4357EXPORT_SYMBOL(vfs_mkdir);
4358EXPORT_SYMBOL(vfs_mknod);
4359EXPORT_SYMBOL(generic_permission);
4360EXPORT_SYMBOL(vfs_readlink);
4361EXPORT_SYMBOL(vfs_rename);
4362EXPORT_SYMBOL(vfs_rmdir);
4363EXPORT_SYMBOL(vfs_symlink);
4364EXPORT_SYMBOL(vfs_unlink);
4365EXPORT_SYMBOL(dentry_unhash);
4366EXPORT_SYMBOL(generic_readlink);