blob: 645268f23eb64cb8c2931ce33391beb2d0080c36 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/namei.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7/*
8 * Some corrections by tytso.
9 */
10
11/* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
12 * lookup logic.
13 */
14/* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
15 */
16
17#include <linux/init.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050018#include <linux/export.h>
David S. Miller44696902012-05-23 20:12:50 -070019#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/slab.h>
21#include <linux/fs.h>
22#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/pagemap.h>
Robert Love0eeca282005-07-12 17:06:03 -040024#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/personality.h>
26#include <linux/security.h>
Mimi Zohar6146f0d2009-02-04 09:06:57 -050027#include <linux/ima.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/syscalls.h>
29#include <linux/mount.h>
30#include <linux/audit.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080031#include <linux/capability.h>
Trond Myklebust834f2a42005-10-18 14:20:16 -070032#include <linux/file.h>
Ulrich Drepper5590ff02006-01-18 17:43:53 -080033#include <linux/fcntl.h>
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070034#include <linux/device_cgroup.h>
Al Viro5ad4e532009-03-29 19:50:06 -040035#include <linux/fs_struct.h>
Linus Torvaldse77819e2011-07-22 19:30:19 -070036#include <linux/posix_acl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <asm/uaccess.h>
38
Eric Parise81e3f42009-12-04 15:47:36 -050039#include "internal.h"
Al Viroc7105362011-11-24 18:22:03 -050040#include "mount.h"
Eric Parise81e3f42009-12-04 15:47:36 -050041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042/* [Feb-1997 T. Schoebel-Theuer]
43 * Fundamental changes in the pathname lookup mechanisms (namei)
44 * were necessary because of omirr. The reason is that omirr needs
45 * to know the _real_ pathname, not the user-supplied one, in case
46 * of symlinks (and also when transname replacements occur).
47 *
48 * The new code replaces the old recursive symlink resolution with
49 * an iterative one (in case of non-nested symlink chains). It does
50 * this with calls to <fs>_follow_link().
51 * As a side effect, dir_namei(), _namei() and follow_link() are now
52 * replaced with a single function lookup_dentry() that can handle all
53 * the special cases of the former code.
54 *
55 * With the new dcache, the pathname is stored at each inode, at least as
56 * long as the refcount of the inode is positive. As a side effect, the
57 * size of the dcache depends on the inode cache and thus is dynamic.
58 *
59 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
60 * resolution to correspond with current state of the code.
61 *
62 * Note that the symlink resolution is not *completely* iterative.
63 * There is still a significant amount of tail- and mid- recursion in
64 * the algorithm. Also, note that <fs>_readlink() is not used in
65 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
66 * may return different results than <fs>_follow_link(). Many virtual
67 * filesystems (including /proc) exhibit this behavior.
68 */
69
70/* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
71 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
72 * and the name already exists in form of a symlink, try to create the new
73 * name indicated by the symlink. The old code always complained that the
74 * name already exists, due to not following the symlink even if its target
75 * is nonexistent. The new semantics affects also mknod() and link() when
Lucas De Marchi25985ed2011-03-30 22:57:33 -030076 * the name is a symlink pointing to a non-existent name.
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 *
78 * I don't know which semantics is the right one, since I have no access
79 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
80 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
81 * "old" one. Personally, I think the new semantics is much more logical.
82 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
83 * file does succeed in both HP-UX and SunOs, but not in Solaris
84 * and in the old Linux semantics.
85 */
86
87/* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
88 * semantics. See the comments in "open_namei" and "do_link" below.
89 *
90 * [10-Sep-98 Alan Modra] Another symlink change.
91 */
92
93/* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
94 * inside the path - always follow.
95 * in the last component in creation/removal/renaming - never follow.
96 * if LOOKUP_FOLLOW passed - follow.
97 * if the pathname has trailing slashes - follow.
98 * otherwise - don't follow.
99 * (applied in that order).
100 *
101 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
102 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
103 * During the 2.4 we need to fix the userland stuff depending on it -
104 * hopefully we will be able to get rid of that wart in 2.5. So far only
105 * XEmacs seems to be relying on it...
106 */
107/*
108 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
Arjan van de Vena11f3a02006-03-23 03:00:33 -0800109 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 * any extra contention...
111 */
112
113/* In order to reduce some races, while at the same time doing additional
114 * checking and hopefully speeding things up, we copy filenames to the
115 * kernel data space before using them..
116 *
117 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
118 * PATH_MAX includes the nul terminator --RR.
119 */
Jeff Layton91a27b22012-10-10 15:25:28 -0400120void final_putname(struct filename *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Jeff Layton7950e382012-10-10 16:43:13 -0400122 if (name->separate) {
123 __putname(name->name);
124 kfree(name);
125 } else {
126 __putname(name);
127 }
Jeff Layton91a27b22012-10-10 15:25:28 -0400128}
129
Jeff Layton7950e382012-10-10 16:43:13 -0400130#define EMBEDDED_NAME_MAX (PATH_MAX - sizeof(struct filename))
131
Jeff Layton91a27b22012-10-10 15:25:28 -0400132static struct filename *
133getname_flags(const char __user *filename, int flags, int *empty)
134{
135 struct filename *result, *err;
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700136 int len;
Jeff Layton7950e382012-10-10 16:43:13 -0400137 long max;
138 char *kname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Jeff Layton7ac86262012-10-10 15:25:28 -0400140 result = audit_reusename(filename);
141 if (result)
142 return result;
143
Jeff Layton7950e382012-10-10 16:43:13 -0400144 result = __getname();
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700145 if (unlikely(!result))
Eric Paris4043cde2012-01-03 14:23:08 -0500146 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Jeff Layton7950e382012-10-10 16:43:13 -0400148 /*
149 * First, try to embed the struct filename inside the names_cache
150 * allocation
151 */
152 kname = (char *)result + sizeof(*result);
Jeff Layton91a27b22012-10-10 15:25:28 -0400153 result->name = kname;
Jeff Layton7950e382012-10-10 16:43:13 -0400154 result->separate = false;
155 max = EMBEDDED_NAME_MAX;
156
157recopy:
158 len = strncpy_from_user(kname, filename, max);
Jeff Layton91a27b22012-10-10 15:25:28 -0400159 if (unlikely(len < 0)) {
160 err = ERR_PTR(len);
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700161 goto error;
Jeff Layton91a27b22012-10-10 15:25:28 -0400162 }
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700163
Jeff Layton7950e382012-10-10 16:43:13 -0400164 /*
165 * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
166 * separate struct filename so we can dedicate the entire
167 * names_cache allocation for the pathname, and re-do the copy from
168 * userland.
169 */
170 if (len == EMBEDDED_NAME_MAX && max == EMBEDDED_NAME_MAX) {
171 kname = (char *)result;
172
173 result = kzalloc(sizeof(*result), GFP_KERNEL);
174 if (!result) {
175 err = ERR_PTR(-ENOMEM);
176 result = (struct filename *)kname;
177 goto error;
178 }
179 result->name = kname;
180 result->separate = true;
181 max = PATH_MAX;
182 goto recopy;
183 }
184
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700185 /* The empty path is special. */
186 if (unlikely(!len)) {
187 if (empty)
Eric Paris4043cde2012-01-03 14:23:08 -0500188 *empty = 1;
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700189 err = ERR_PTR(-ENOENT);
190 if (!(flags & LOOKUP_EMPTY))
191 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 }
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700193
194 err = ERR_PTR(-ENAMETOOLONG);
Jeff Layton7950e382012-10-10 16:43:13 -0400195 if (unlikely(len >= PATH_MAX))
196 goto error;
197
198 result->uptr = filename;
199 audit_getname(result);
200 return result;
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700201
202error:
Jeff Layton7950e382012-10-10 16:43:13 -0400203 final_putname(result);
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700204 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206
Jeff Layton91a27b22012-10-10 15:25:28 -0400207struct filename *
208getname(const char __user * filename)
Al Virof52e0c12011-03-14 18:56:51 -0400209{
Linus Torvaldsf7493e52012-03-22 16:10:40 -0700210 return getname_flags(filename, 0, NULL);
Al Virof52e0c12011-03-14 18:56:51 -0400211}
Jeff Layton91a27b22012-10-10 15:25:28 -0400212EXPORT_SYMBOL(getname);
Al Virof52e0c12011-03-14 18:56:51 -0400213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214#ifdef CONFIG_AUDITSYSCALL
Jeff Layton91a27b22012-10-10 15:25:28 -0400215void putname(struct filename *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
Al Viro5ac3a9c2006-07-16 06:38:45 -0400217 if (unlikely(!audit_dummy_context()))
Jeff Layton91a27b22012-10-10 15:25:28 -0400218 return audit_putname(name);
219 final_putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221#endif
222
Linus Torvaldse77819e2011-07-22 19:30:19 -0700223static int check_acl(struct inode *inode, int mask)
224{
Linus Torvalds84635d62011-07-25 22:47:03 -0700225#ifdef CONFIG_FS_POSIX_ACL
Linus Torvaldse77819e2011-07-22 19:30:19 -0700226 struct posix_acl *acl;
227
Linus Torvaldse77819e2011-07-22 19:30:19 -0700228 if (mask & MAY_NOT_BLOCK) {
Al Viro35678662011-08-02 21:32:13 -0400229 acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
230 if (!acl)
Linus Torvaldse77819e2011-07-22 19:30:19 -0700231 return -EAGAIN;
Al Viro35678662011-08-02 21:32:13 -0400232 /* no ->get_acl() calls in RCU mode... */
233 if (acl == ACL_NOT_CACHED)
234 return -ECHILD;
Ari Savolainen206b1d02011-08-06 19:43:07 +0300235 return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
Linus Torvaldse77819e2011-07-22 19:30:19 -0700236 }
237
238 acl = get_cached_acl(inode, ACL_TYPE_ACCESS);
239
240 /*
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200241 * A filesystem can force a ACL callback by just never filling the
242 * ACL cache. But normally you'd fill the cache either at inode
243 * instantiation time, or on the first ->get_acl call.
Linus Torvaldse77819e2011-07-22 19:30:19 -0700244 *
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200245 * If the filesystem doesn't have a get_acl() function at all, we'll
246 * just create the negative cache entry.
Linus Torvaldse77819e2011-07-22 19:30:19 -0700247 */
248 if (acl == ACL_NOT_CACHED) {
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200249 if (inode->i_op->get_acl) {
250 acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS);
251 if (IS_ERR(acl))
252 return PTR_ERR(acl);
253 } else {
254 set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
255 return -EAGAIN;
256 }
Linus Torvaldse77819e2011-07-22 19:30:19 -0700257 }
258
259 if (acl) {
260 int error = posix_acl_permission(inode, acl, mask);
261 posix_acl_release(acl);
262 return error;
263 }
Linus Torvalds84635d62011-07-25 22:47:03 -0700264#endif
Linus Torvaldse77819e2011-07-22 19:30:19 -0700265
266 return -EAGAIN;
267}
268
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700269/*
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530270 * This does the basic permission checking
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700271 */
Al Viro7e401452011-06-20 19:12:17 -0400272static int acl_permission_check(struct inode *inode, int mask)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700273{
Linus Torvalds26cf46b2011-05-13 11:51:01 -0700274 unsigned int mode = inode->i_mode;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700275
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -0800276 if (likely(uid_eq(current_fsuid(), inode->i_uid)))
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700277 mode >>= 6;
278 else {
Linus Torvaldse77819e2011-07-22 19:30:19 -0700279 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
Al Viro7e401452011-06-20 19:12:17 -0400280 int error = check_acl(inode, mask);
Nick Pigginb74c79e2011-01-07 17:49:58 +1100281 if (error != -EAGAIN)
282 return error;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700283 }
284
285 if (in_group_p(inode->i_gid))
286 mode >>= 3;
287 }
288
289 /*
290 * If the DACs are ok we don't need any capability check.
291 */
Al Viro9c2c7032011-06-20 19:06:22 -0400292 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700293 return 0;
294 return -EACCES;
295}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297/**
Nick Pigginb74c79e2011-01-07 17:49:58 +1100298 * generic_permission - check for access rights on a Posix-like filesystem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 * @inode: inode to check access rights for
Andreas Gruenbacher8fd90c82011-10-23 23:13:30 +0530300 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 *
302 * Used to check for read/write/execute permissions on a file.
303 * We use "fsuid" for this, letting us set arbitrary permissions
304 * for filesystem access without changing the "normal" uids which
Nick Pigginb74c79e2011-01-07 17:49:58 +1100305 * are used for other things.
306 *
307 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
308 * request cannot be satisfied (eg. requires blocking or too much complexity).
309 * It would then be called again in ref-walk mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 */
Al Viro2830ba72011-06-20 19:16:29 -0400311int generic_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700313 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 /*
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530316 * Do the basic permission checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 */
Al Viro7e401452011-06-20 19:12:17 -0400318 ret = acl_permission_check(inode, mask);
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700319 if (ret != -EACCES)
320 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Al Virod594e7e2011-06-20 19:55:42 -0400322 if (S_ISDIR(inode->i_mode)) {
323 /* DACs are overridable for directories */
Eric W. Biederman1a48e2a2011-11-14 16:24:06 -0800324 if (inode_capable(inode, CAP_DAC_OVERRIDE))
Al Virod594e7e2011-06-20 19:55:42 -0400325 return 0;
326 if (!(mask & MAY_WRITE))
Eric W. Biederman1a48e2a2011-11-14 16:24:06 -0800327 if (inode_capable(inode, CAP_DAC_READ_SEARCH))
Al Virod594e7e2011-06-20 19:55:42 -0400328 return 0;
329 return -EACCES;
330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 /*
332 * Read/write DACs are always overridable.
Al Virod594e7e2011-06-20 19:55:42 -0400333 * Executable DACs are overridable when there is
334 * at least one exec bit set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 */
Al Virod594e7e2011-06-20 19:55:42 -0400336 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
Eric W. Biederman1a48e2a2011-11-14 16:24:06 -0800337 if (inode_capable(inode, CAP_DAC_OVERRIDE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 return 0;
339
340 /*
341 * Searching includes executable on directories, else just read.
342 */
Serge E. Hallyn7ea66002009-12-29 14:50:19 -0600343 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
Al Virod594e7e2011-06-20 19:55:42 -0400344 if (mask == MAY_READ)
Eric W. Biederman1a48e2a2011-11-14 16:24:06 -0800345 if (inode_capable(inode, CAP_DAC_READ_SEARCH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 return 0;
347
348 return -EACCES;
349}
350
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700351/*
352 * We _really_ want to just do "generic_permission()" without
353 * even looking at the inode->i_op values. So we keep a cache
354 * flag in inode->i_opflags, that says "this has not special
355 * permission function, use the fast case".
356 */
357static inline int do_inode_permission(struct inode *inode, int mask)
358{
359 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
360 if (likely(inode->i_op->permission))
361 return inode->i_op->permission(inode, mask);
362
363 /* This gets set once for the inode lifetime */
364 spin_lock(&inode->i_lock);
365 inode->i_opflags |= IOP_FASTPERM;
366 spin_unlock(&inode->i_lock);
367 }
368 return generic_permission(inode, mask);
369}
370
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200371/**
David Howells0bdaea92012-06-25 12:55:46 +0100372 * __inode_permission - Check for access rights to a given inode
373 * @inode: Inode to check permission on
374 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200375 *
David Howells0bdaea92012-06-25 12:55:46 +0100376 * Check for read/write/execute permissions on an inode.
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530377 *
378 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
David Howells0bdaea92012-06-25 12:55:46 +0100379 *
380 * This does not check for a read-only file system. You probably want
381 * inode_permission().
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200382 */
David Howells0bdaea92012-06-25 12:55:46 +0100383int __inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Al Viroe6305c42008-07-15 21:03:57 -0400385 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700387 if (unlikely(mask & MAY_WRITE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 /*
389 * Nobody gets write access to an immutable file.
390 */
391 if (IS_IMMUTABLE(inode))
392 return -EACCES;
393 }
394
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700395 retval = do_inode_permission(inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (retval)
397 return retval;
398
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700399 retval = devcgroup_inode_permission(inode, mask);
400 if (retval)
401 return retval;
402
Eric Parisd09ca732010-07-23 11:43:57 -0400403 return security_inode_permission(inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
Al Virof4d6ff82011-06-19 13:14:21 -0400406/**
David Howells0bdaea92012-06-25 12:55:46 +0100407 * sb_permission - Check superblock-level permissions
408 * @sb: Superblock of inode to check permission on
Randy Dunlap55852632012-08-18 17:39:25 -0700409 * @inode: Inode to check permission on
David Howells0bdaea92012-06-25 12:55:46 +0100410 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
411 *
412 * Separate out file-system wide checks from inode-specific permission checks.
413 */
414static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
415{
416 if (unlikely(mask & MAY_WRITE)) {
417 umode_t mode = inode->i_mode;
418
419 /* Nobody gets write access to a read-only fs. */
420 if ((sb->s_flags & MS_RDONLY) &&
421 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
422 return -EROFS;
423 }
424 return 0;
425}
426
427/**
428 * inode_permission - Check for access rights to a given inode
429 * @inode: Inode to check permission on
430 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
431 *
432 * Check for read/write/execute permissions on an inode. We use fs[ug]id for
433 * this, letting us set arbitrary permissions for filesystem access without
434 * changing the "normal" UIDs which are used for other things.
435 *
436 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
437 */
438int inode_permission(struct inode *inode, int mask)
439{
440 int retval;
441
442 retval = sb_permission(inode->i_sb, inode, mask);
443 if (retval)
444 return retval;
445 return __inode_permission(inode, mask);
446}
447
448/**
Jan Blunck5dd784d02008-02-14 19:34:38 -0800449 * path_get - get a reference to a path
450 * @path: path to get the reference to
451 *
452 * Given a path increment the reference count to the dentry and the vfsmount.
453 */
Al Virodcf787f2013-03-01 23:51:07 -0500454void path_get(const struct path *path)
Jan Blunck5dd784d02008-02-14 19:34:38 -0800455{
456 mntget(path->mnt);
457 dget(path->dentry);
458}
459EXPORT_SYMBOL(path_get);
460
461/**
Jan Blunck1d957f92008-02-14 19:34:35 -0800462 * path_put - put a reference to a path
463 * @path: path to put the reference to
464 *
465 * Given a path decrement the reference count to the dentry and the vfsmount.
466 */
Al Virodcf787f2013-03-01 23:51:07 -0500467void path_put(const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Jan Blunck1d957f92008-02-14 19:34:35 -0800469 dput(path->dentry);
470 mntput(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
Jan Blunck1d957f92008-02-14 19:34:35 -0800472EXPORT_SYMBOL(path_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Al Viro19660af2011-03-25 10:32:48 -0400474/*
Nick Piggin31e6b012011-01-07 17:49:52 +1100475 * Path walking has 2 modes, rcu-walk and ref-walk (see
Al Viro19660af2011-03-25 10:32:48 -0400476 * Documentation/filesystems/path-lookup.txt). In situations when we can't
477 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
478 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
479 * mode. Refcounts are grabbed at the last known good point before rcu-walk
480 * got stuck, so ref-walk may continue from there. If this is not successful
481 * (eg. a seqcount has changed), then failure is returned and it's up to caller
482 * to restart the path walk from the beginning in ref-walk mode.
Nick Piggin31e6b012011-01-07 17:49:52 +1100483 */
Nick Piggin31e6b012011-01-07 17:49:52 +1100484
Al Viro32a79912012-07-18 20:43:19 +0400485static inline void lock_rcu_walk(void)
486{
487 br_read_lock(&vfsmount_lock);
488 rcu_read_lock();
489}
490
491static inline void unlock_rcu_walk(void)
492{
493 rcu_read_unlock();
494 br_read_unlock(&vfsmount_lock);
495}
496
Nick Piggin31e6b012011-01-07 17:49:52 +1100497/**
Al Viro19660af2011-03-25 10:32:48 -0400498 * unlazy_walk - try to switch to ref-walk mode.
499 * @nd: nameidata pathwalk data
500 * @dentry: child of nd->path.dentry or NULL
Randy Dunlap39191622011-01-08 19:36:21 -0800501 * Returns: 0 on success, -ECHILD on failure
Nick Piggin31e6b012011-01-07 17:49:52 +1100502 *
Al Viro19660af2011-03-25 10:32:48 -0400503 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
504 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
505 * @nd or NULL. Must be called from rcu-walk context.
Nick Piggin31e6b012011-01-07 17:49:52 +1100506 */
Al Viro19660af2011-03-25 10:32:48 -0400507static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
Nick Piggin31e6b012011-01-07 17:49:52 +1100508{
509 struct fs_struct *fs = current->fs;
510 struct dentry *parent = nd->path.dentry;
511
512 BUG_ON(!(nd->flags & LOOKUP_RCU));
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700513
514 /*
515 * Get a reference to the parent first: we're
516 * going to make "path_put(nd->path)" valid in
517 * non-RCU context for "terminate_walk()".
518 *
519 * If this doesn't work, return immediately with
520 * RCU walking still active (and then we will do
521 * the RCU walk cleanup in terminate_walk()).
522 */
523 if (!lockref_get_not_dead(&parent->d_lockref))
524 return -ECHILD;
525
526 /*
527 * After the mntget(), we terminate_walk() will do
528 * the right thing for non-RCU mode, and all our
529 * subsequent exit cases should unlock_rcu_walk()
530 * before returning.
531 */
532 mntget(nd->path.mnt);
533 nd->flags &= ~LOOKUP_RCU;
Linus Torvalds15570082013-09-02 11:38:06 -0700534
535 /*
536 * For a negative lookup, the lookup sequence point is the parents
537 * sequence point, and it only needs to revalidate the parent dentry.
538 *
539 * For a positive lookup, we need to move both the parent and the
540 * dentry from the RCU domain to be properly refcounted. And the
541 * sequence number in the dentry validates *both* dentry counters,
542 * since we checked the sequence number of the parent after we got
543 * the child sequence number. So we know the parent must still
544 * be valid if the child sequence number is still valid.
545 */
Al Viro19660af2011-03-25 10:32:48 -0400546 if (!dentry) {
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700547 if (read_seqcount_retry(&parent->d_seq, nd->seq))
548 goto out;
Al Viro19660af2011-03-25 10:32:48 -0400549 BUG_ON(nd->inode != parent->d_inode);
550 } else {
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700551 if (!lockref_get_not_dead(&dentry->d_lockref))
552 goto out;
553 if (read_seqcount_retry(&dentry->d_seq, nd->seq))
554 goto drop_dentry;
Al Viro19660af2011-03-25 10:32:48 -0400555 }
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700556
557 /*
558 * Sequence counts matched. Now make sure that the root is
559 * still valid and get it if required.
560 */
561 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
562 spin_lock(&fs->lock);
563 if (nd->root.mnt != fs->root.mnt || nd->root.dentry != fs->root.dentry)
564 goto unlock_and_drop_dentry;
Nick Piggin31e6b012011-01-07 17:49:52 +1100565 path_get(&nd->root);
566 spin_unlock(&fs->lock);
567 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100568
Al Viro32a79912012-07-18 20:43:19 +0400569 unlock_rcu_walk();
Nick Piggin31e6b012011-01-07 17:49:52 +1100570 return 0;
Al Viro19660af2011-03-25 10:32:48 -0400571
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700572unlock_and_drop_dentry:
573 spin_unlock(&fs->lock);
574drop_dentry:
575 unlock_rcu_walk();
Linus Torvalds15570082013-09-02 11:38:06 -0700576 dput(dentry);
Linus Torvaldsd0d27272013-09-10 12:17:49 -0700577 goto drop_root_mnt;
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700578out:
579 unlock_rcu_walk();
Linus Torvaldsd0d27272013-09-10 12:17:49 -0700580drop_root_mnt:
581 if (!(nd->flags & LOOKUP_ROOT))
582 nd->root.mnt = NULL;
Nick Piggin31e6b012011-01-07 17:49:52 +1100583 return -ECHILD;
584}
585
Al Viro4ce16ef32012-06-10 16:10:59 -0400586static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
Nick Piggin34286d62011-01-07 17:49:57 +1100587{
Al Viro4ce16ef32012-06-10 16:10:59 -0400588 return dentry->d_op->d_revalidate(dentry, flags);
Nick Piggin34286d62011-01-07 17:49:57 +1100589}
590
Al Viro9f1fafe2011-03-25 11:00:12 -0400591/**
592 * complete_walk - successful completion of path walk
593 * @nd: pointer nameidata
Jeff Layton39159de2009-12-07 12:01:50 -0500594 *
Al Viro9f1fafe2011-03-25 11:00:12 -0400595 * If we had been in RCU mode, drop out of it and legitimize nd->path.
596 * Revalidate the final result, unless we'd already done that during
597 * the path walk or the filesystem doesn't ask for it. Return 0 on
598 * success, -error on failure. In case of failure caller does not
599 * need to drop nd->path.
Jeff Layton39159de2009-12-07 12:01:50 -0500600 */
Al Viro9f1fafe2011-03-25 11:00:12 -0400601static int complete_walk(struct nameidata *nd)
Jeff Layton39159de2009-12-07 12:01:50 -0500602{
Al Viro16c2cd72011-02-22 15:50:10 -0500603 struct dentry *dentry = nd->path.dentry;
Jeff Layton39159de2009-12-07 12:01:50 -0500604 int status;
Jeff Layton39159de2009-12-07 12:01:50 -0500605
Al Viro9f1fafe2011-03-25 11:00:12 -0400606 if (nd->flags & LOOKUP_RCU) {
607 nd->flags &= ~LOOKUP_RCU;
608 if (!(nd->flags & LOOKUP_ROOT))
609 nd->root.mnt = NULL;
Linus Torvalds15570082013-09-02 11:38:06 -0700610
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700611 if (unlikely(!lockref_get_not_dead(&dentry->d_lockref))) {
Al Viro32a79912012-07-18 20:43:19 +0400612 unlock_rcu_walk();
Al Viro9f1fafe2011-03-25 11:00:12 -0400613 return -ECHILD;
614 }
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700615 if (read_seqcount_retry(&dentry->d_seq, nd->seq)) {
616 unlock_rcu_walk();
617 dput(dentry);
618 return -ECHILD;
619 }
Al Viro9f1fafe2011-03-25 11:00:12 -0400620 mntget(nd->path.mnt);
Al Viro32a79912012-07-18 20:43:19 +0400621 unlock_rcu_walk();
Al Viro9f1fafe2011-03-25 11:00:12 -0400622 }
623
Al Viro16c2cd72011-02-22 15:50:10 -0500624 if (likely(!(nd->flags & LOOKUP_JUMPED)))
Jeff Layton39159de2009-12-07 12:01:50 -0500625 return 0;
626
Jeff Laytonecf3d1f2013-02-20 11:19:05 -0500627 if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
Al Viro16c2cd72011-02-22 15:50:10 -0500628 return 0;
629
Jeff Laytonecf3d1f2013-02-20 11:19:05 -0500630 status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
Jeff Layton39159de2009-12-07 12:01:50 -0500631 if (status > 0)
632 return 0;
633
Al Viro16c2cd72011-02-22 15:50:10 -0500634 if (!status)
Jeff Layton39159de2009-12-07 12:01:50 -0500635 status = -ESTALE;
Al Viro16c2cd72011-02-22 15:50:10 -0500636
Al Viro9f1fafe2011-03-25 11:00:12 -0400637 path_put(&nd->path);
Jeff Layton39159de2009-12-07 12:01:50 -0500638 return status;
639}
640
Al Viro2a737872009-04-07 11:49:53 -0400641static __always_inline void set_root(struct nameidata *nd)
642{
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200643 if (!nd->root.mnt)
644 get_fs_root(current->fs, &nd->root);
Al Viro2a737872009-04-07 11:49:53 -0400645}
646
Al Viro6de88d72009-08-09 01:41:57 +0400647static int link_path_walk(const char *, struct nameidata *);
648
Nick Piggin31e6b012011-01-07 17:49:52 +1100649static __always_inline void set_root_rcu(struct nameidata *nd)
650{
651 if (!nd->root.mnt) {
652 struct fs_struct *fs = current->fs;
Nick Pigginc28cc362011-01-07 17:49:53 +1100653 unsigned seq;
654
655 do {
656 seq = read_seqcount_begin(&fs->seq);
657 nd->root = fs->root;
Tim Chenc1530012011-04-15 11:39:29 -0700658 nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
Nick Pigginc28cc362011-01-07 17:49:53 +1100659 } while (read_seqcount_retry(&fs->seq, seq));
Nick Piggin31e6b012011-01-07 17:49:52 +1100660 }
661}
662
Jan Blunck1d957f92008-02-14 19:34:35 -0800663static void path_put_conditional(struct path *path, struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700664{
665 dput(path->dentry);
Jan Blunck4ac91372008-02-14 19:34:32 -0800666 if (path->mnt != nd->path.mnt)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700667 mntput(path->mnt);
668}
669
Nick Piggin7b9337a2011-01-14 08:42:43 +0000670static inline void path_to_nameidata(const struct path *path,
671 struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700672{
Nick Piggin31e6b012011-01-07 17:49:52 +1100673 if (!(nd->flags & LOOKUP_RCU)) {
674 dput(nd->path.dentry);
675 if (nd->path.mnt != path->mnt)
676 mntput(nd->path.mnt);
Huang Shijie9a229682010-04-02 17:37:13 +0800677 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100678 nd->path.mnt = path->mnt;
Jan Blunck4ac91372008-02-14 19:34:32 -0800679 nd->path.dentry = path->dentry;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700680}
681
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400682/*
683 * Helper to directly jump to a known parsed path from ->follow_link,
684 * caller must have taken a reference to path beforehand.
685 */
686void nd_jump_link(struct nameidata *nd, struct path *path)
687{
688 path_put(&nd->path);
689
690 nd->path = *path;
691 nd->inode = nd->path.dentry->d_inode;
692 nd->flags |= LOOKUP_JUMPED;
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400693}
694
Al Viro574197e2011-03-14 22:20:34 -0400695static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
696{
697 struct inode *inode = link->dentry->d_inode;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400698 if (inode->i_op->put_link)
Al Viro574197e2011-03-14 22:20:34 -0400699 inode->i_op->put_link(link->dentry, nd, cookie);
700 path_put(link);
701}
702
Linus Torvalds561ec642012-10-26 10:05:07 -0700703int sysctl_protected_symlinks __read_mostly = 0;
704int sysctl_protected_hardlinks __read_mostly = 0;
Kees Cook800179c2012-07-25 17:29:07 -0700705
706/**
707 * may_follow_link - Check symlink following for unsafe situations
708 * @link: The path of the symlink
Randy Dunlap55852632012-08-18 17:39:25 -0700709 * @nd: nameidata pathwalk data
Kees Cook800179c2012-07-25 17:29:07 -0700710 *
711 * In the case of the sysctl_protected_symlinks sysctl being enabled,
712 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
713 * in a sticky world-writable directory. This is to protect privileged
714 * processes from failing races against path names that may change out
715 * from under them by way of other users creating malicious symlinks.
716 * It will permit symlinks to be followed only when outside a sticky
717 * world-writable directory, or when the uid of the symlink and follower
718 * match, or when the directory owner matches the symlink's owner.
719 *
720 * Returns 0 if following the symlink is allowed, -ve on error.
721 */
722static inline int may_follow_link(struct path *link, struct nameidata *nd)
723{
724 const struct inode *inode;
725 const struct inode *parent;
726
727 if (!sysctl_protected_symlinks)
728 return 0;
729
730 /* Allowed if owner and follower match. */
731 inode = link->dentry->d_inode;
Eric W. Biederman81abe272012-08-03 09:38:08 -0700732 if (uid_eq(current_cred()->fsuid, inode->i_uid))
Kees Cook800179c2012-07-25 17:29:07 -0700733 return 0;
734
735 /* Allowed if parent directory not sticky and world-writable. */
736 parent = nd->path.dentry->d_inode;
737 if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
738 return 0;
739
740 /* Allowed if parent directory and link owner match. */
Eric W. Biederman81abe272012-08-03 09:38:08 -0700741 if (uid_eq(parent->i_uid, inode->i_uid))
Kees Cook800179c2012-07-25 17:29:07 -0700742 return 0;
743
Sasha Levinffd8d102012-10-04 19:56:40 -0400744 audit_log_link_denied("follow_link", link);
Kees Cook800179c2012-07-25 17:29:07 -0700745 path_put_conditional(link, nd);
746 path_put(&nd->path);
747 return -EACCES;
748}
749
750/**
751 * safe_hardlink_source - Check for safe hardlink conditions
752 * @inode: the source inode to hardlink from
753 *
754 * Return false if at least one of the following conditions:
755 * - inode is not a regular file
756 * - inode is setuid
757 * - inode is setgid and group-exec
758 * - access failure for read and write
759 *
760 * Otherwise returns true.
761 */
762static bool safe_hardlink_source(struct inode *inode)
763{
764 umode_t mode = inode->i_mode;
765
766 /* Special files should not get pinned to the filesystem. */
767 if (!S_ISREG(mode))
768 return false;
769
770 /* Setuid files should not get pinned to the filesystem. */
771 if (mode & S_ISUID)
772 return false;
773
774 /* Executable setgid files should not get pinned to the filesystem. */
775 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
776 return false;
777
778 /* Hardlinking to unreadable or unwritable sources is dangerous. */
779 if (inode_permission(inode, MAY_READ | MAY_WRITE))
780 return false;
781
782 return true;
783}
784
785/**
786 * may_linkat - Check permissions for creating a hardlink
787 * @link: the source to hardlink from
788 *
789 * Block hardlink when all of:
790 * - sysctl_protected_hardlinks enabled
791 * - fsuid does not match inode
792 * - hardlink source is unsafe (see safe_hardlink_source() above)
793 * - not CAP_FOWNER
794 *
795 * Returns 0 if successful, -ve on error.
796 */
797static int may_linkat(struct path *link)
798{
799 const struct cred *cred;
800 struct inode *inode;
801
802 if (!sysctl_protected_hardlinks)
803 return 0;
804
805 cred = current_cred();
806 inode = link->dentry->d_inode;
807
808 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
809 * otherwise, it must be a safe source.
810 */
Eric W. Biederman81abe272012-08-03 09:38:08 -0700811 if (uid_eq(cred->fsuid, inode->i_uid) || safe_hardlink_source(inode) ||
Kees Cook800179c2012-07-25 17:29:07 -0700812 capable(CAP_FOWNER))
813 return 0;
814
Kees Cooka51d9ea2012-07-25 17:29:08 -0700815 audit_log_link_denied("linkat", link);
Kees Cook800179c2012-07-25 17:29:07 -0700816 return -EPERM;
817}
818
Al Virodef4af32009-12-26 08:37:05 -0500819static __always_inline int
Al Viro574197e2011-03-14 22:20:34 -0400820follow_link(struct path *link, struct nameidata *nd, void **p)
Ian Kent051d3812006-03-27 01:14:53 -0800821{
Nick Piggin7b9337a2011-01-14 08:42:43 +0000822 struct dentry *dentry = link->dentry;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400823 int error;
824 char *s;
Ian Kent051d3812006-03-27 01:14:53 -0800825
Al Viro844a3912011-02-15 00:38:26 -0500826 BUG_ON(nd->flags & LOOKUP_RCU);
827
Al Viro0e794582011-03-16 02:45:02 -0400828 if (link->mnt == nd->path.mnt)
829 mntget(link->mnt);
830
Al Viro6d7b5aa2012-06-10 04:15:17 -0400831 error = -ELOOP;
832 if (unlikely(current->total_link_count >= 40))
833 goto out_put_nd_path;
834
Al Viro574197e2011-03-14 22:20:34 -0400835 cond_resched();
836 current->total_link_count++;
837
Al Viro68ac1232012-03-15 08:21:57 -0400838 touch_atime(link);
Ian Kent051d3812006-03-27 01:14:53 -0800839 nd_set_link(nd, NULL);
840
Al Viro36f3b4f2011-02-22 21:24:38 -0500841 error = security_inode_follow_link(link->dentry, nd);
Al Viro6d7b5aa2012-06-10 04:15:17 -0400842 if (error)
843 goto out_put_nd_path;
Al Viro36f3b4f2011-02-22 21:24:38 -0500844
Al Viro86acdca12009-12-22 23:45:11 -0500845 nd->last_type = LAST_BIND;
Al Virodef4af32009-12-26 08:37:05 -0500846 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
847 error = PTR_ERR(*p);
Al Viro6d7b5aa2012-06-10 04:15:17 -0400848 if (IS_ERR(*p))
Christoph Hellwig408ef012012-06-18 10:47:03 -0400849 goto out_put_nd_path;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400850
851 error = 0;
852 s = nd_get_link(nd);
853 if (s) {
Al Viro443ed252013-09-10 12:00:43 -0400854 if (unlikely(IS_ERR(s))) {
855 path_put(&nd->path);
856 put_link(nd, link, *p);
857 return PTR_ERR(s);
858 }
859 if (*s == '/') {
860 set_root(nd);
861 path_put(&nd->path);
862 nd->path = nd->root;
863 path_get(&nd->root);
864 nd->flags |= LOOKUP_JUMPED;
865 }
866 nd->inode = nd->path.dentry->d_inode;
867 error = link_path_walk(s, nd);
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400868 if (unlikely(error))
869 put_link(nd, link, *p);
Ian Kent051d3812006-03-27 01:14:53 -0800870 }
Al Viro6d7b5aa2012-06-10 04:15:17 -0400871
872 return error;
873
874out_put_nd_path:
Arnd Bergmann98f6ef62012-10-11 13:20:00 +0000875 *p = NULL;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400876 path_put(&nd->path);
Al Viro6d7b5aa2012-06-10 04:15:17 -0400877 path_put(link);
Ian Kent051d3812006-03-27 01:14:53 -0800878 return error;
879}
880
Nick Piggin31e6b012011-01-07 17:49:52 +1100881static int follow_up_rcu(struct path *path)
882{
Al Viro0714a532011-11-24 22:19:58 -0500883 struct mount *mnt = real_mount(path->mnt);
884 struct mount *parent;
Nick Piggin31e6b012011-01-07 17:49:52 +1100885 struct dentry *mountpoint;
886
Al Viro0714a532011-11-24 22:19:58 -0500887 parent = mnt->mnt_parent;
888 if (&parent->mnt == path->mnt)
Nick Piggin31e6b012011-01-07 17:49:52 +1100889 return 0;
Al Viroa73324d2011-11-24 22:25:07 -0500890 mountpoint = mnt->mnt_mountpoint;
Nick Piggin31e6b012011-01-07 17:49:52 +1100891 path->dentry = mountpoint;
Al Viro0714a532011-11-24 22:19:58 -0500892 path->mnt = &parent->mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +1100893 return 1;
894}
895
David Howellsf015f1262012-06-25 12:55:28 +0100896/*
897 * follow_up - Find the mountpoint of path's vfsmount
898 *
899 * Given a path, find the mountpoint of its source file system.
900 * Replace @path with the path of the mountpoint in the parent mount.
901 * Up is towards /.
902 *
903 * Return 1 if we went up a level and 0 if we were already at the
904 * root.
905 */
Al Virobab77eb2009-04-18 03:26:48 -0400906int follow_up(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Al Viro0714a532011-11-24 22:19:58 -0500908 struct mount *mnt = real_mount(path->mnt);
909 struct mount *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 struct dentry *mountpoint;
Nick Piggin99b7db72010-08-18 04:37:39 +1000911
Andi Kleen962830d2012-05-08 13:32:02 +0930912 br_read_lock(&vfsmount_lock);
Al Viro0714a532011-11-24 22:19:58 -0500913 parent = mnt->mnt_parent;
Al Viro3c0a6162012-07-18 17:32:50 +0400914 if (parent == mnt) {
Andi Kleen962830d2012-05-08 13:32:02 +0930915 br_read_unlock(&vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 return 0;
917 }
Al Viro0714a532011-11-24 22:19:58 -0500918 mntget(&parent->mnt);
Al Viroa73324d2011-11-24 22:25:07 -0500919 mountpoint = dget(mnt->mnt_mountpoint);
Andi Kleen962830d2012-05-08 13:32:02 +0930920 br_read_unlock(&vfsmount_lock);
Al Virobab77eb2009-04-18 03:26:48 -0400921 dput(path->dentry);
922 path->dentry = mountpoint;
923 mntput(path->mnt);
Al Viro0714a532011-11-24 22:19:58 -0500924 path->mnt = &parent->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 return 1;
926}
927
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100928/*
David Howells9875cf82011-01-14 18:45:21 +0000929 * Perform an automount
930 * - return -EISDIR to tell follow_managed() to stop and return the path we
931 * were called with.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 */
David Howells9875cf82011-01-14 18:45:21 +0000933static int follow_automount(struct path *path, unsigned flags,
934 bool *need_mntput)
Nick Piggin31e6b012011-01-07 17:49:52 +1100935{
David Howells9875cf82011-01-14 18:45:21 +0000936 struct vfsmount *mnt;
David Howellsea5b7782011-01-14 19:10:03 +0000937 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +1100938
David Howells9875cf82011-01-14 18:45:21 +0000939 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
940 return -EREMOTE;
Al Viro463ffb22005-06-06 13:36:05 -0700941
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200942 /* We don't want to mount if someone's just doing a stat -
943 * unless they're stat'ing a directory and appended a '/' to
944 * the name.
945 *
946 * We do, however, want to mount if someone wants to open or
947 * create a file of any type under the mountpoint, wants to
948 * traverse through the mountpoint or wants to open the
949 * mounted directory. Also, autofs may mark negative dentries
950 * as being automount points. These will need the attentions
951 * of the daemon to instantiate them before they can be used.
David Howells9875cf82011-01-14 18:45:21 +0000952 */
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200953 if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
Linus Torvaldsd94c1772011-09-26 17:44:55 -0700954 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200955 path->dentry->d_inode)
956 return -EISDIR;
957
David Howells9875cf82011-01-14 18:45:21 +0000958 current->total_link_count++;
959 if (current->total_link_count >= 40)
960 return -ELOOP;
961
962 mnt = path->dentry->d_op->d_automount(path);
963 if (IS_ERR(mnt)) {
964 /*
965 * The filesystem is allowed to return -EISDIR here to indicate
966 * it doesn't want to automount. For instance, autofs would do
967 * this so that its userspace daemon can mount on this dentry.
968 *
969 * However, we can only permit this if it's a terminal point in
970 * the path being looked up; if it wasn't then the remainder of
971 * the path is inaccessible and we should say so.
972 */
Al Viro49084c32011-06-25 21:59:52 -0400973 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
David Howells9875cf82011-01-14 18:45:21 +0000974 return -EREMOTE;
975 return PTR_ERR(mnt);
976 }
David Howellsea5b7782011-01-14 19:10:03 +0000977
David Howells9875cf82011-01-14 18:45:21 +0000978 if (!mnt) /* mount collision */
979 return 0;
980
Al Viro8aef1882011-06-16 15:10:06 +0100981 if (!*need_mntput) {
982 /* lock_mount() may release path->mnt on error */
983 mntget(path->mnt);
984 *need_mntput = true;
985 }
Al Viro19a167a2011-01-17 01:35:23 -0500986 err = finish_automount(mnt, path);
David Howellsea5b7782011-01-14 19:10:03 +0000987
David Howellsea5b7782011-01-14 19:10:03 +0000988 switch (err) {
989 case -EBUSY:
990 /* Someone else made a mount here whilst we were busy */
Al Viro19a167a2011-01-17 01:35:23 -0500991 return 0;
David Howellsea5b7782011-01-14 19:10:03 +0000992 case 0:
Al Viro8aef1882011-06-16 15:10:06 +0100993 path_put(path);
David Howellsea5b7782011-01-14 19:10:03 +0000994 path->mnt = mnt;
995 path->dentry = dget(mnt->mnt_root);
David Howellsea5b7782011-01-14 19:10:03 +0000996 return 0;
Al Viro19a167a2011-01-17 01:35:23 -0500997 default:
998 return err;
David Howellsea5b7782011-01-14 19:10:03 +0000999 }
Al Viro19a167a2011-01-17 01:35:23 -05001000
David Howells9875cf82011-01-14 18:45:21 +00001001}
1002
1003/*
1004 * Handle a dentry that is managed in some way.
David Howellscc53ce52011-01-14 18:45:26 +00001005 * - Flagged for transit management (autofs)
David Howells9875cf82011-01-14 18:45:21 +00001006 * - Flagged as mountpoint
1007 * - Flagged as automount point
1008 *
1009 * This may only be called in refwalk mode.
1010 *
1011 * Serialization is taken care of in namespace.c
1012 */
1013static int follow_managed(struct path *path, unsigned flags)
1014{
Al Viro8aef1882011-06-16 15:10:06 +01001015 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
David Howells9875cf82011-01-14 18:45:21 +00001016 unsigned managed;
1017 bool need_mntput = false;
Al Viro8aef1882011-06-16 15:10:06 +01001018 int ret = 0;
David Howells9875cf82011-01-14 18:45:21 +00001019
1020 /* Given that we're not holding a lock here, we retain the value in a
1021 * local variable for each dentry as we look at it so that we don't see
1022 * the components of that value change under us */
1023 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1024 managed &= DCACHE_MANAGED_DENTRY,
1025 unlikely(managed != 0)) {
David Howellscc53ce52011-01-14 18:45:26 +00001026 /* Allow the filesystem to manage the transit without i_mutex
1027 * being held. */
1028 if (managed & DCACHE_MANAGE_TRANSIT) {
1029 BUG_ON(!path->dentry->d_op);
1030 BUG_ON(!path->dentry->d_op->d_manage);
Al Viro1aed3e42011-03-18 09:09:02 -04001031 ret = path->dentry->d_op->d_manage(path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +00001032 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001033 break;
David Howellscc53ce52011-01-14 18:45:26 +00001034 }
1035
David Howells9875cf82011-01-14 18:45:21 +00001036 /* Transit to a mounted filesystem. */
1037 if (managed & DCACHE_MOUNTED) {
1038 struct vfsmount *mounted = lookup_mnt(path);
1039 if (mounted) {
1040 dput(path->dentry);
1041 if (need_mntput)
1042 mntput(path->mnt);
1043 path->mnt = mounted;
1044 path->dentry = dget(mounted->mnt_root);
1045 need_mntput = true;
1046 continue;
1047 }
1048
1049 /* Something is mounted on this dentry in another
1050 * namespace and/or whatever was mounted there in this
1051 * namespace got unmounted before we managed to get the
1052 * vfsmount_lock */
1053 }
1054
1055 /* Handle an automount point */
1056 if (managed & DCACHE_NEED_AUTOMOUNT) {
1057 ret = follow_automount(path, flags, &need_mntput);
1058 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001059 break;
David Howells9875cf82011-01-14 18:45:21 +00001060 continue;
1061 }
1062
1063 /* We didn't change the current path point */
1064 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 }
Al Viro8aef1882011-06-16 15:10:06 +01001066
1067 if (need_mntput && path->mnt == mnt)
1068 mntput(path->mnt);
1069 if (ret == -EISDIR)
1070 ret = 0;
Al Viroa3fbbde2011-11-07 21:21:26 +00001071 return ret < 0 ? ret : need_mntput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072}
1073
David Howellscc53ce52011-01-14 18:45:26 +00001074int follow_down_one(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
1076 struct vfsmount *mounted;
1077
Al Viro1c755af2009-04-18 14:06:57 -04001078 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -04001080 dput(path->dentry);
1081 mntput(path->mnt);
1082 path->mnt = mounted;
1083 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 return 1;
1085 }
1086 return 0;
1087}
1088
Ian Kent62a73752011-03-25 01:51:02 +08001089static inline bool managed_dentry_might_block(struct dentry *dentry)
1090{
1091 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
1092 dentry->d_op->d_manage(dentry, true) < 0);
1093}
1094
David Howells9875cf82011-01-14 18:45:21 +00001095/*
Al Viro287548e2011-05-27 06:50:06 -04001096 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1097 * we meet a managed dentry that would need blocking.
David Howells9875cf82011-01-14 18:45:21 +00001098 */
1099static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
Al Viro287548e2011-05-27 06:50:06 -04001100 struct inode **inode)
David Howells9875cf82011-01-14 18:45:21 +00001101{
Ian Kent62a73752011-03-25 01:51:02 +08001102 for (;;) {
Al Viroc7105362011-11-24 18:22:03 -05001103 struct mount *mounted;
Ian Kent62a73752011-03-25 01:51:02 +08001104 /*
1105 * Don't forget we might have a non-mountpoint managed dentry
1106 * that wants to block transit.
1107 */
Al Viro287548e2011-05-27 06:50:06 -04001108 if (unlikely(managed_dentry_might_block(path->dentry)))
David Howellsab909112011-01-14 18:46:51 +00001109 return false;
Ian Kent62a73752011-03-25 01:51:02 +08001110
1111 if (!d_mountpoint(path->dentry))
1112 break;
1113
David Howells9875cf82011-01-14 18:45:21 +00001114 mounted = __lookup_mnt(path->mnt, path->dentry, 1);
1115 if (!mounted)
1116 break;
Al Viroc7105362011-11-24 18:22:03 -05001117 path->mnt = &mounted->mnt;
1118 path->dentry = mounted->mnt.mnt_root;
Al Viroa3fbbde2011-11-07 21:21:26 +00001119 nd->flags |= LOOKUP_JUMPED;
David Howells9875cf82011-01-14 18:45:21 +00001120 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
Linus Torvalds59430262011-07-18 15:43:29 -07001121 /*
1122 * Update the inode too. We don't need to re-check the
1123 * dentry sequence number here after this d_inode read,
1124 * because a mount-point is always pinned.
1125 */
1126 *inode = path->dentry->d_inode;
David Howells9875cf82011-01-14 18:45:21 +00001127 }
David Howells9875cf82011-01-14 18:45:21 +00001128 return true;
1129}
1130
Al Virodea39372011-05-27 06:53:39 -04001131static void follow_mount_rcu(struct nameidata *nd)
Al Viro287548e2011-05-27 06:50:06 -04001132{
Al Virodea39372011-05-27 06:53:39 -04001133 while (d_mountpoint(nd->path.dentry)) {
Al Viroc7105362011-11-24 18:22:03 -05001134 struct mount *mounted;
Al Virodea39372011-05-27 06:53:39 -04001135 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
Al Viro287548e2011-05-27 06:50:06 -04001136 if (!mounted)
1137 break;
Al Viroc7105362011-11-24 18:22:03 -05001138 nd->path.mnt = &mounted->mnt;
1139 nd->path.dentry = mounted->mnt.mnt_root;
Al Virodea39372011-05-27 06:53:39 -04001140 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro287548e2011-05-27 06:50:06 -04001141 }
1142}
1143
Nick Piggin31e6b012011-01-07 17:49:52 +11001144static int follow_dotdot_rcu(struct nameidata *nd)
1145{
Nick Piggin31e6b012011-01-07 17:49:52 +11001146 set_root_rcu(nd);
1147
David Howells9875cf82011-01-14 18:45:21 +00001148 while (1) {
Nick Piggin31e6b012011-01-07 17:49:52 +11001149 if (nd->path.dentry == nd->root.dentry &&
1150 nd->path.mnt == nd->root.mnt) {
1151 break;
1152 }
1153 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1154 struct dentry *old = nd->path.dentry;
1155 struct dentry *parent = old->d_parent;
1156 unsigned seq;
1157
1158 seq = read_seqcount_begin(&parent->d_seq);
1159 if (read_seqcount_retry(&old->d_seq, nd->seq))
Al Viroef7562d2011-03-04 14:35:59 -05001160 goto failed;
Nick Piggin31e6b012011-01-07 17:49:52 +11001161 nd->path.dentry = parent;
1162 nd->seq = seq;
1163 break;
1164 }
1165 if (!follow_up_rcu(&nd->path))
1166 break;
1167 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Nick Piggin31e6b012011-01-07 17:49:52 +11001168 }
Al Virodea39372011-05-27 06:53:39 -04001169 follow_mount_rcu(nd);
1170 nd->inode = nd->path.dentry->d_inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001171 return 0;
Al Viroef7562d2011-03-04 14:35:59 -05001172
1173failed:
1174 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001175 if (!(nd->flags & LOOKUP_ROOT))
1176 nd->root.mnt = NULL;
Al Viro32a79912012-07-18 20:43:19 +04001177 unlock_rcu_walk();
Al Viroef7562d2011-03-04 14:35:59 -05001178 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001179}
1180
David Howells9875cf82011-01-14 18:45:21 +00001181/*
David Howellscc53ce52011-01-14 18:45:26 +00001182 * Follow down to the covering mount currently visible to userspace. At each
1183 * point, the filesystem owning that dentry may be queried as to whether the
1184 * caller is permitted to proceed or not.
David Howellscc53ce52011-01-14 18:45:26 +00001185 */
Al Viro7cc90cc2011-03-18 09:04:20 -04001186int follow_down(struct path *path)
David Howellscc53ce52011-01-14 18:45:26 +00001187{
1188 unsigned managed;
1189 int ret;
1190
1191 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1192 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1193 /* Allow the filesystem to manage the transit without i_mutex
1194 * being held.
1195 *
1196 * We indicate to the filesystem if someone is trying to mount
1197 * something here. This gives autofs the chance to deny anyone
1198 * other than its daemon the right to mount on its
1199 * superstructure.
1200 *
1201 * The filesystem may sleep at this point.
1202 */
1203 if (managed & DCACHE_MANAGE_TRANSIT) {
1204 BUG_ON(!path->dentry->d_op);
1205 BUG_ON(!path->dentry->d_op->d_manage);
David Howellsab909112011-01-14 18:46:51 +00001206 ret = path->dentry->d_op->d_manage(
Al Viro1aed3e42011-03-18 09:09:02 -04001207 path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +00001208 if (ret < 0)
1209 return ret == -EISDIR ? 0 : ret;
1210 }
1211
1212 /* Transit to a mounted filesystem. */
1213 if (managed & DCACHE_MOUNTED) {
1214 struct vfsmount *mounted = lookup_mnt(path);
1215 if (!mounted)
1216 break;
1217 dput(path->dentry);
1218 mntput(path->mnt);
1219 path->mnt = mounted;
1220 path->dentry = dget(mounted->mnt_root);
1221 continue;
1222 }
1223
1224 /* Don't handle automount points here */
1225 break;
1226 }
1227 return 0;
1228}
1229
1230/*
David Howells9875cf82011-01-14 18:45:21 +00001231 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1232 */
1233static void follow_mount(struct path *path)
1234{
1235 while (d_mountpoint(path->dentry)) {
1236 struct vfsmount *mounted = lookup_mnt(path);
1237 if (!mounted)
1238 break;
1239 dput(path->dentry);
1240 mntput(path->mnt);
1241 path->mnt = mounted;
1242 path->dentry = dget(mounted->mnt_root);
1243 }
1244}
1245
Nick Piggin31e6b012011-01-07 17:49:52 +11001246static void follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
Al Viro2a737872009-04-07 11:49:53 -04001248 set_root(nd);
Andreas Mohre518ddb2006-09-29 02:01:22 -07001249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 while(1) {
Jan Blunck4ac91372008-02-14 19:34:32 -08001251 struct dentry *old = nd->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Al Viro2a737872009-04-07 11:49:53 -04001253 if (nd->path.dentry == nd->root.dentry &&
1254 nd->path.mnt == nd->root.mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 break;
1256 }
Jan Blunck4ac91372008-02-14 19:34:32 -08001257 if (nd->path.dentry != nd->path.mnt->mnt_root) {
Al Viro3088dd72010-01-30 15:47:29 -05001258 /* rare case of legitimate dget_parent()... */
1259 nd->path.dentry = dget_parent(nd->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 dput(old);
1261 break;
1262 }
Al Viro3088dd72010-01-30 15:47:29 -05001263 if (!follow_up(&nd->path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
Al Viro79ed0222009-04-18 13:59:41 -04001266 follow_mount(&nd->path);
Nick Piggin31e6b012011-01-07 17:49:52 +11001267 nd->inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268}
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270/*
Miklos Szeredibad61182012-03-26 12:54:24 +02001271 * This looks up the name in dcache, possibly revalidates the old dentry and
1272 * allocates a new one if not found or not valid. In the need_lookup argument
1273 * returns whether i_op->lookup is necessary.
1274 *
1275 * dir->d_inode->i_mutex must be held
Nick Pigginbaa03892010-08-18 04:37:31 +10001276 */
Miklos Szeredibad61182012-03-26 12:54:24 +02001277static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
Al Viro201f9562012-06-22 12:42:10 +04001278 unsigned int flags, bool *need_lookup)
Nick Pigginbaa03892010-08-18 04:37:31 +10001279{
Nick Pigginbaa03892010-08-18 04:37:31 +10001280 struct dentry *dentry;
Miklos Szeredibad61182012-03-26 12:54:24 +02001281 int error;
Nick Pigginbaa03892010-08-18 04:37:31 +10001282
Miklos Szeredibad61182012-03-26 12:54:24 +02001283 *need_lookup = false;
1284 dentry = d_lookup(dir, name);
1285 if (dentry) {
Jeff Layton39e3c952012-11-28 11:30:53 -05001286 if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
Al Viro201f9562012-06-22 12:42:10 +04001287 error = d_revalidate(dentry, flags);
Miklos Szeredibad61182012-03-26 12:54:24 +02001288 if (unlikely(error <= 0)) {
1289 if (error < 0) {
1290 dput(dentry);
1291 return ERR_PTR(error);
1292 } else if (!d_invalidate(dentry)) {
1293 dput(dentry);
1294 dentry = NULL;
1295 }
1296 }
1297 }
1298 }
Nick Pigginbaa03892010-08-18 04:37:31 +10001299
Miklos Szeredibad61182012-03-26 12:54:24 +02001300 if (!dentry) {
1301 dentry = d_alloc(dir, name);
1302 if (unlikely(!dentry))
1303 return ERR_PTR(-ENOMEM);
Nick Pigginbaa03892010-08-18 04:37:31 +10001304
Miklos Szeredibad61182012-03-26 12:54:24 +02001305 *need_lookup = true;
Nick Pigginbaa03892010-08-18 04:37:31 +10001306 }
1307 return dentry;
1308}
1309
1310/*
Miklos Szeredibad61182012-03-26 12:54:24 +02001311 * Call i_op->lookup on the dentry. The dentry must be negative but may be
1312 * hashed if it was pouplated with DCACHE_NEED_LOOKUP.
1313 *
1314 * dir->d_inode->i_mutex must be held
Josef Bacik44396f42011-05-31 11:58:49 -04001315 */
Miklos Szeredibad61182012-03-26 12:54:24 +02001316static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
Al Viro72bd8662012-06-10 17:17:17 -04001317 unsigned int flags)
Josef Bacik44396f42011-05-31 11:58:49 -04001318{
Josef Bacik44396f42011-05-31 11:58:49 -04001319 struct dentry *old;
1320
1321 /* Don't create child dentry for a dead directory. */
Miklos Szeredibad61182012-03-26 12:54:24 +02001322 if (unlikely(IS_DEADDIR(dir))) {
Miklos Szeredie188dc02012-02-03 14:25:18 +01001323 dput(dentry);
Josef Bacik44396f42011-05-31 11:58:49 -04001324 return ERR_PTR(-ENOENT);
Miklos Szeredie188dc02012-02-03 14:25:18 +01001325 }
Josef Bacik44396f42011-05-31 11:58:49 -04001326
Al Viro72bd8662012-06-10 17:17:17 -04001327 old = dir->i_op->lookup(dir, dentry, flags);
Josef Bacik44396f42011-05-31 11:58:49 -04001328 if (unlikely(old)) {
1329 dput(dentry);
1330 dentry = old;
1331 }
1332 return dentry;
1333}
1334
Al Viroa3255542012-03-30 14:41:51 -04001335static struct dentry *__lookup_hash(struct qstr *name,
Al Viro72bd8662012-06-10 17:17:17 -04001336 struct dentry *base, unsigned int flags)
Al Viroa3255542012-03-30 14:41:51 -04001337{
Miklos Szeredibad61182012-03-26 12:54:24 +02001338 bool need_lookup;
Al Viroa3255542012-03-30 14:41:51 -04001339 struct dentry *dentry;
1340
Al Viro72bd8662012-06-10 17:17:17 -04001341 dentry = lookup_dcache(name, base, flags, &need_lookup);
Miklos Szeredibad61182012-03-26 12:54:24 +02001342 if (!need_lookup)
1343 return dentry;
Al Viroa3255542012-03-30 14:41:51 -04001344
Al Viro72bd8662012-06-10 17:17:17 -04001345 return lookup_real(base->d_inode, dentry, flags);
Al Viroa3255542012-03-30 14:41:51 -04001346}
1347
Josef Bacik44396f42011-05-31 11:58:49 -04001348/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 * It's more convoluted than I'd like it to be, but... it's still fairly
1350 * small and for now I'd prefer to have fast path as straight as possible.
1351 * It _is_ time-critical.
1352 */
Al Viroe97cdc82013-01-24 18:16:00 -05001353static int lookup_fast(struct nameidata *nd,
Miklos Szeredi697f5142012-05-21 17:30:05 +02001354 struct path *path, struct inode **inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355{
Jan Blunck4ac91372008-02-14 19:34:32 -08001356 struct vfsmount *mnt = nd->path.mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +11001357 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro5a18fff2011-03-11 04:44:53 -05001358 int need_reval = 1;
1359 int status = 1;
David Howells9875cf82011-01-14 18:45:21 +00001360 int err;
1361
Al Viro3cac2602009-08-13 18:27:43 +04001362 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001363 * Rename seqlock is not required here because in the off chance
1364 * of a false negative due to a concurrent rename, we're going to
1365 * do the non-racy lookup, below.
1366 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001367 if (nd->flags & LOOKUP_RCU) {
1368 unsigned seq;
Linus Torvaldsda53be12013-05-21 15:22:44 -07001369 dentry = __d_lookup_rcu(parent, &nd->last, &seq);
Al Viro5a18fff2011-03-11 04:44:53 -05001370 if (!dentry)
1371 goto unlazy;
1372
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001373 /*
1374 * This sequence count validates that the inode matches
1375 * the dentry name information from lookup.
1376 */
1377 *inode = dentry->d_inode;
1378 if (read_seqcount_retry(&dentry->d_seq, seq))
1379 return -ECHILD;
1380
1381 /*
1382 * This sequence count validates that the parent had no
1383 * changes while we did the lookup of the dentry above.
1384 *
1385 * The memory barrier in read_seqcount_begin of child is
1386 * enough, we can use __read_seqcount_retry here.
1387 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001388 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1389 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001390 nd->seq = seq;
Al Viro5a18fff2011-03-11 04:44:53 -05001391
Al Viro24643082011-02-15 01:26:22 -05001392 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
Al Viro4ce16ef32012-06-10 16:10:59 -04001393 status = d_revalidate(dentry, nd->flags);
Al Viro5a18fff2011-03-11 04:44:53 -05001394 if (unlikely(status <= 0)) {
1395 if (status != -ECHILD)
1396 need_reval = 0;
1397 goto unlazy;
1398 }
Al Viro24643082011-02-15 01:26:22 -05001399 }
Nick Piggin31e6b012011-01-07 17:49:52 +11001400 path->mnt = mnt;
1401 path->dentry = dentry;
Al Virod6e9bd22011-05-27 07:03:15 -04001402 if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1403 goto unlazy;
1404 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1405 goto unlazy;
1406 return 0;
Al Viro5a18fff2011-03-11 04:44:53 -05001407unlazy:
Al Viro19660af2011-03-25 10:32:48 -04001408 if (unlazy_walk(nd, dentry))
1409 return -ECHILD;
Al Viro5a18fff2011-03-11 04:44:53 -05001410 } else {
Al Viroe97cdc82013-01-24 18:16:00 -05001411 dentry = __d_lookup(parent, &nd->last);
Nick Piggin31e6b012011-01-07 17:49:52 +11001412 }
Al Viro5a18fff2011-03-11 04:44:53 -05001413
Al Viro81e6f522012-03-30 14:48:04 -04001414 if (unlikely(!dentry))
1415 goto need_lookup;
Al Viro5a18fff2011-03-11 04:44:53 -05001416
Al Viro5a18fff2011-03-11 04:44:53 -05001417 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
Al Viro4ce16ef32012-06-10 16:10:59 -04001418 status = d_revalidate(dentry, nd->flags);
Al Viro5a18fff2011-03-11 04:44:53 -05001419 if (unlikely(status <= 0)) {
1420 if (status < 0) {
1421 dput(dentry);
1422 return status;
1423 }
1424 if (!d_invalidate(dentry)) {
1425 dput(dentry);
Al Viro81e6f522012-03-30 14:48:04 -04001426 goto need_lookup;
Al Viro5a18fff2011-03-11 04:44:53 -05001427 }
1428 }
Miklos Szeredi697f5142012-05-21 17:30:05 +02001429
David Howells9875cf82011-01-14 18:45:21 +00001430 path->mnt = mnt;
1431 path->dentry = dentry;
1432 err = follow_managed(path, nd->flags);
Ian Kent89312212011-01-18 12:06:10 +08001433 if (unlikely(err < 0)) {
1434 path_put_conditional(path, nd);
David Howells9875cf82011-01-14 18:45:21 +00001435 return err;
Ian Kent89312212011-01-18 12:06:10 +08001436 }
Al Viroa3fbbde2011-11-07 21:21:26 +00001437 if (err)
1438 nd->flags |= LOOKUP_JUMPED;
David Howells9875cf82011-01-14 18:45:21 +00001439 *inode = path->dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 return 0;
Al Viro81e6f522012-03-30 14:48:04 -04001441
1442need_lookup:
Miklos Szeredi697f5142012-05-21 17:30:05 +02001443 return 1;
1444}
1445
1446/* Fast lookup failed, do it the slow way */
Al Virocc2a5272013-01-24 18:19:49 -05001447static int lookup_slow(struct nameidata *nd, struct path *path)
Miklos Szeredi697f5142012-05-21 17:30:05 +02001448{
1449 struct dentry *dentry, *parent;
1450 int err;
1451
1452 parent = nd->path.dentry;
Al Viro81e6f522012-03-30 14:48:04 -04001453 BUG_ON(nd->inode != parent->d_inode);
1454
1455 mutex_lock(&parent->d_inode->i_mutex);
Al Virocc2a5272013-01-24 18:19:49 -05001456 dentry = __lookup_hash(&nd->last, parent, nd->flags);
Al Viro81e6f522012-03-30 14:48:04 -04001457 mutex_unlock(&parent->d_inode->i_mutex);
1458 if (IS_ERR(dentry))
1459 return PTR_ERR(dentry);
Miklos Szeredi697f5142012-05-21 17:30:05 +02001460 path->mnt = nd->path.mnt;
1461 path->dentry = dentry;
1462 err = follow_managed(path, nd->flags);
1463 if (unlikely(err < 0)) {
1464 path_put_conditional(path, nd);
1465 return err;
1466 }
1467 if (err)
1468 nd->flags |= LOOKUP_JUMPED;
1469 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470}
1471
Al Viro52094c82011-02-21 21:34:47 -05001472static inline int may_lookup(struct nameidata *nd)
1473{
1474 if (nd->flags & LOOKUP_RCU) {
Al Viro4ad5abb2011-06-20 19:57:03 -04001475 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
Al Viro52094c82011-02-21 21:34:47 -05001476 if (err != -ECHILD)
1477 return err;
Al Viro19660af2011-03-25 10:32:48 -04001478 if (unlazy_walk(nd, NULL))
Al Viro52094c82011-02-21 21:34:47 -05001479 return -ECHILD;
1480 }
Al Viro4ad5abb2011-06-20 19:57:03 -04001481 return inode_permission(nd->inode, MAY_EXEC);
Al Viro52094c82011-02-21 21:34:47 -05001482}
1483
Al Viro9856fa12011-03-04 14:22:06 -05001484static inline int handle_dots(struct nameidata *nd, int type)
1485{
1486 if (type == LAST_DOTDOT) {
1487 if (nd->flags & LOOKUP_RCU) {
1488 if (follow_dotdot_rcu(nd))
1489 return -ECHILD;
1490 } else
1491 follow_dotdot(nd);
1492 }
1493 return 0;
1494}
1495
Al Viro951361f2011-03-04 14:44:37 -05001496static void terminate_walk(struct nameidata *nd)
1497{
1498 if (!(nd->flags & LOOKUP_RCU)) {
1499 path_put(&nd->path);
1500 } else {
1501 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001502 if (!(nd->flags & LOOKUP_ROOT))
1503 nd->root.mnt = NULL;
Al Viro32a79912012-07-18 20:43:19 +04001504 unlock_rcu_walk();
Al Viro951361f2011-03-04 14:44:37 -05001505 }
1506}
1507
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001508/*
1509 * Do we need to follow links? We _really_ want to be able
1510 * to do this check without having to look at inode->i_op,
1511 * so we keep a cache of "no, this doesn't need follow_link"
1512 * for the common case.
1513 */
Linus Torvalds7813b942011-08-07 09:53:20 -07001514static inline int should_follow_link(struct inode *inode, int follow)
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001515{
1516 if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
1517 if (likely(inode->i_op->follow_link))
1518 return follow;
1519
1520 /* This gets set once for the inode lifetime */
1521 spin_lock(&inode->i_lock);
1522 inode->i_opflags |= IOP_NOFOLLOW;
1523 spin_unlock(&inode->i_lock);
1524 }
1525 return 0;
1526}
1527
Al Viroce57dfc2011-03-13 19:58:58 -04001528static inline int walk_component(struct nameidata *nd, struct path *path,
Al Viro21b9b072013-01-24 18:10:25 -05001529 int follow)
Al Viroce57dfc2011-03-13 19:58:58 -04001530{
1531 struct inode *inode;
1532 int err;
1533 /*
1534 * "." and ".." are special - ".." especially so because it has
1535 * to be able to know about the current root directory and
1536 * parent relationships.
1537 */
Al Viro21b9b072013-01-24 18:10:25 -05001538 if (unlikely(nd->last_type != LAST_NORM))
1539 return handle_dots(nd, nd->last_type);
Al Viroe97cdc82013-01-24 18:16:00 -05001540 err = lookup_fast(nd, path, &inode);
Al Viroce57dfc2011-03-13 19:58:58 -04001541 if (unlikely(err)) {
Miklos Szeredi697f5142012-05-21 17:30:05 +02001542 if (err < 0)
1543 goto out_err;
1544
Al Virocc2a5272013-01-24 18:19:49 -05001545 err = lookup_slow(nd, path);
Miklos Szeredi697f5142012-05-21 17:30:05 +02001546 if (err < 0)
1547 goto out_err;
1548
1549 inode = path->dentry->d_inode;
Al Viroce57dfc2011-03-13 19:58:58 -04001550 }
Miklos Szeredi697f5142012-05-21 17:30:05 +02001551 err = -ENOENT;
1552 if (!inode)
1553 goto out_path_put;
1554
Linus Torvalds7813b942011-08-07 09:53:20 -07001555 if (should_follow_link(inode, follow)) {
Al Viro19660af2011-03-25 10:32:48 -04001556 if (nd->flags & LOOKUP_RCU) {
1557 if (unlikely(unlazy_walk(nd, path->dentry))) {
Miklos Szeredi697f5142012-05-21 17:30:05 +02001558 err = -ECHILD;
1559 goto out_err;
Al Viro19660af2011-03-25 10:32:48 -04001560 }
1561 }
Al Viroce57dfc2011-03-13 19:58:58 -04001562 BUG_ON(inode != path->dentry->d_inode);
1563 return 1;
1564 }
1565 path_to_nameidata(path, nd);
1566 nd->inode = inode;
1567 return 0;
Miklos Szeredi697f5142012-05-21 17:30:05 +02001568
1569out_path_put:
1570 path_to_nameidata(path, nd);
1571out_err:
1572 terminate_walk(nd);
1573 return err;
Al Viroce57dfc2011-03-13 19:58:58 -04001574}
1575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576/*
Al Virob3563792011-03-14 21:54:55 -04001577 * This limits recursive symlink follows to 8, while
1578 * limiting consecutive symlinks to 40.
1579 *
1580 * Without that kind of total limit, nasty chains of consecutive
1581 * symlinks can cause almost arbitrarily long lookups.
1582 */
1583static inline int nested_symlink(struct path *path, struct nameidata *nd)
1584{
1585 int res;
1586
Al Virob3563792011-03-14 21:54:55 -04001587 if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1588 path_put_conditional(path, nd);
1589 path_put(&nd->path);
1590 return -ELOOP;
1591 }
Erez Zadok1a4022f2011-05-21 01:19:59 -04001592 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
Al Virob3563792011-03-14 21:54:55 -04001593
1594 nd->depth++;
1595 current->link_count++;
1596
1597 do {
1598 struct path link = *path;
1599 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04001600
1601 res = follow_link(&link, nd, &cookie);
Al Viro6d7b5aa2012-06-10 04:15:17 -04001602 if (res)
1603 break;
Al Viro21b9b072013-01-24 18:10:25 -05001604 res = walk_component(nd, path, LOOKUP_FOLLOW);
Al Viro574197e2011-03-14 22:20:34 -04001605 put_link(nd, &link, cookie);
Al Virob3563792011-03-14 21:54:55 -04001606 } while (res > 0);
1607
1608 current->link_count--;
1609 nd->depth--;
1610 return res;
1611}
1612
1613/*
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001614 * We really don't want to look at inode->i_op->lookup
1615 * when we don't have to. So we keep a cache bit in
1616 * the inode ->i_opflags field that says "yes, we can
1617 * do lookup on this inode".
1618 */
1619static inline int can_lookup(struct inode *inode)
1620{
1621 if (likely(inode->i_opflags & IOP_LOOKUP))
1622 return 1;
1623 if (likely(!inode->i_op->lookup))
1624 return 0;
1625
1626 /* We do this once for the lifetime of the inode */
1627 spin_lock(&inode->i_lock);
1628 inode->i_opflags |= IOP_LOOKUP;
1629 spin_unlock(&inode->i_lock);
1630 return 1;
1631}
1632
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001633/*
1634 * We can do the critical dentry name comparison and hashing
1635 * operations one word at a time, but we are limited to:
1636 *
1637 * - Architectures with fast unaligned word accesses. We could
1638 * do a "get_unaligned()" if this helps and is sufficiently
1639 * fast.
1640 *
1641 * - Little-endian machines (so that we can generate the mask
1642 * of low bytes efficiently). Again, we *could* do a byte
1643 * swapping load on big-endian architectures if that is not
1644 * expensive enough to make the optimization worthless.
1645 *
1646 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1647 * do not trap on the (extremely unlikely) case of a page
1648 * crossing operation.
1649 *
1650 * - Furthermore, we need an efficient 64-bit compile for the
1651 * 64-bit case in order to generate the "number of bytes in
1652 * the final mask". Again, that could be replaced with a
1653 * efficient population count instruction or similar.
1654 */
1655#ifdef CONFIG_DCACHE_WORD_ACCESS
1656
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001657#include <asm/word-at-a-time.h>
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001658
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001659#ifdef CONFIG_64BIT
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001660
1661static inline unsigned int fold_hash(unsigned long hash)
1662{
1663 hash += hash >> (8*sizeof(int));
1664 return hash;
1665}
1666
1667#else /* 32-bit case */
1668
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001669#define fold_hash(x) (x)
1670
1671#endif
1672
1673unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1674{
1675 unsigned long a, mask;
1676 unsigned long hash = 0;
1677
1678 for (;;) {
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001679 a = load_unaligned_zeropad(name);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001680 if (len < sizeof(unsigned long))
1681 break;
1682 hash += a;
Al Virof132c5b2012-03-22 21:59:52 +00001683 hash *= 9;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001684 name += sizeof(unsigned long);
1685 len -= sizeof(unsigned long);
1686 if (!len)
1687 goto done;
1688 }
1689 mask = ~(~0ul << len*8);
1690 hash += mask & a;
1691done:
1692 return fold_hash(hash);
1693}
1694EXPORT_SYMBOL(full_name_hash);
1695
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001696/*
1697 * Calculate the length and hash of the path component, and
1698 * return the length of the component;
1699 */
1700static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1701{
Linus Torvalds36126f82012-05-26 10:43:17 -07001702 unsigned long a, b, adata, bdata, mask, hash, len;
1703 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001704
1705 hash = a = 0;
1706 len = -sizeof(unsigned long);
1707 do {
1708 hash = (hash + a) * 9;
1709 len += sizeof(unsigned long);
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001710 a = load_unaligned_zeropad(name+len);
Linus Torvalds36126f82012-05-26 10:43:17 -07001711 b = a ^ REPEAT_BYTE('/');
1712 } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001713
Linus Torvalds36126f82012-05-26 10:43:17 -07001714 adata = prep_zero_mask(a, adata, &constants);
1715 bdata = prep_zero_mask(b, bdata, &constants);
1716
1717 mask = create_zero_mask(adata | bdata);
1718
1719 hash += a & zero_bytemask(mask);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001720 *hashp = fold_hash(hash);
1721
Linus Torvalds36126f82012-05-26 10:43:17 -07001722 return len + find_zero(mask);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001723}
1724
1725#else
1726
Linus Torvalds0145acc2012-03-02 14:32:59 -08001727unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1728{
1729 unsigned long hash = init_name_hash();
1730 while (len--)
1731 hash = partial_name_hash(*name++, hash);
1732 return end_name_hash(hash);
1733}
Linus Torvaldsae942ae2012-03-02 19:40:57 -08001734EXPORT_SYMBOL(full_name_hash);
Linus Torvalds0145acc2012-03-02 14:32:59 -08001735
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001736/*
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001737 * We know there's a real path component here of at least
1738 * one character.
1739 */
1740static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1741{
1742 unsigned long hash = init_name_hash();
1743 unsigned long len = 0, c;
1744
1745 c = (unsigned char)*name;
1746 do {
1747 len++;
1748 hash = partial_name_hash(c, hash);
1749 c = (unsigned char)name[len];
1750 } while (c && c != '/');
1751 *hashp = end_name_hash(hash);
1752 return len;
1753}
1754
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001755#endif
1756
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001757/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01001759 * This is the basic name resolution function, turning a pathname into
1760 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01001762 * Returns 0 and nd will have valid dentry and mnt on success.
1763 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 */
Al Viro6de88d72009-08-09 01:41:57 +04001765static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766{
1767 struct path next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
1770 while (*name=='/')
1771 name++;
1772 if (!*name)
Al Viro086e1832011-02-22 20:56:27 -05001773 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 /* At this point we know we have a real path component. */
1776 for(;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 struct qstr this;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001778 long len;
Al Virofe479a52011-02-22 15:10:03 -05001779 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
Al Viro52094c82011-02-21 21:34:47 -05001781 err = may_lookup(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 if (err)
1783 break;
1784
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001785 len = hash_name(name, &this.hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 this.name = name;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001787 this.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Al Virofe479a52011-02-22 15:10:03 -05001789 type = LAST_NORM;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001790 if (name[0] == '.') switch (len) {
Al Virofe479a52011-02-22 15:10:03 -05001791 case 2:
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001792 if (name[1] == '.') {
Al Virofe479a52011-02-22 15:10:03 -05001793 type = LAST_DOTDOT;
Al Viro16c2cd72011-02-22 15:50:10 -05001794 nd->flags |= LOOKUP_JUMPED;
1795 }
Al Virofe479a52011-02-22 15:10:03 -05001796 break;
1797 case 1:
1798 type = LAST_DOT;
1799 }
Al Viro5a202bc2011-03-08 14:17:44 -05001800 if (likely(type == LAST_NORM)) {
1801 struct dentry *parent = nd->path.dentry;
Al Viro16c2cd72011-02-22 15:50:10 -05001802 nd->flags &= ~LOOKUP_JUMPED;
Al Viro5a202bc2011-03-08 14:17:44 -05001803 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
Linus Torvaldsda53be12013-05-21 15:22:44 -07001804 err = parent->d_op->d_hash(parent, &this);
Al Viro5a202bc2011-03-08 14:17:44 -05001805 if (err < 0)
1806 break;
1807 }
1808 }
Al Virofe479a52011-02-22 15:10:03 -05001809
Al Viro5f4a6a62013-01-24 18:04:22 -05001810 nd->last = this;
1811 nd->last_type = type;
1812
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001813 if (!name[len])
Al Viro5f4a6a62013-01-24 18:04:22 -05001814 return 0;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001815 /*
1816 * If it wasn't NUL, we know it was '/'. Skip that
1817 * slash, and continue until no more slashes.
1818 */
1819 do {
1820 len++;
1821 } while (unlikely(name[len] == '/'));
1822 if (!name[len])
Al Viro5f4a6a62013-01-24 18:04:22 -05001823 return 0;
1824
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001825 name += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Al Viro21b9b072013-01-24 18:10:25 -05001827 err = walk_component(nd, &next, LOOKUP_FOLLOW);
Al Viroce57dfc2011-03-13 19:58:58 -04001828 if (err < 0)
1829 return err;
Al Virofe479a52011-02-22 15:10:03 -05001830
Al Viroce57dfc2011-03-13 19:58:58 -04001831 if (err) {
Al Virob3563792011-03-14 21:54:55 -04001832 err = nested_symlink(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 if (err)
Al Viroa7472ba2011-03-04 14:39:30 -05001834 return err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001835 }
Al Viro5f4a6a62013-01-24 18:04:22 -05001836 if (!can_lookup(nd->inode)) {
1837 err = -ENOTDIR;
1838 break;
1839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 }
Al Viro951361f2011-03-04 14:44:37 -05001841 terminate_walk(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 return err;
1843}
1844
Al Viro70e9b352011-03-05 21:12:22 -05001845static int path_init(int dfd, const char *name, unsigned int flags,
1846 struct nameidata *nd, struct file **fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847{
Prasanna Medaea3834d2005-04-29 16:00:17 +01001848 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
1850 nd->last_type = LAST_ROOT; /* if there are only slashes... */
Al Viro16c2cd72011-02-22 15:50:10 -05001851 nd->flags = flags | LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 nd->depth = 0;
Al Viro5b6ca022011-03-09 23:04:47 -05001853 if (flags & LOOKUP_ROOT) {
1854 struct inode *inode = nd->root.dentry->d_inode;
Al Viro73d049a2011-03-11 12:08:24 -05001855 if (*name) {
Al Viro741b7c32012-12-20 13:41:28 -05001856 if (!can_lookup(inode))
Al Viro73d049a2011-03-11 12:08:24 -05001857 return -ENOTDIR;
1858 retval = inode_permission(inode, MAY_EXEC);
1859 if (retval)
1860 return retval;
1861 }
Al Viro5b6ca022011-03-09 23:04:47 -05001862 nd->path = nd->root;
1863 nd->inode = inode;
1864 if (flags & LOOKUP_RCU) {
Al Viro32a79912012-07-18 20:43:19 +04001865 lock_rcu_walk();
Al Viro5b6ca022011-03-09 23:04:47 -05001866 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1867 } else {
1868 path_get(&nd->path);
1869 }
1870 return 0;
1871 }
1872
Al Viro2a737872009-04-07 11:49:53 -04001873 nd->root.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 if (*name=='/') {
Al Viroe41f7d42011-02-22 14:02:58 -05001876 if (flags & LOOKUP_RCU) {
Al Viro32a79912012-07-18 20:43:19 +04001877 lock_rcu_walk();
Al Viroe41f7d42011-02-22 14:02:58 -05001878 set_root_rcu(nd);
1879 } else {
1880 set_root(nd);
1881 path_get(&nd->root);
1882 }
Al Viro2a737872009-04-07 11:49:53 -04001883 nd->path = nd->root;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001884 } else if (dfd == AT_FDCWD) {
Al Viroe41f7d42011-02-22 14:02:58 -05001885 if (flags & LOOKUP_RCU) {
1886 struct fs_struct *fs = current->fs;
1887 unsigned seq;
1888
Al Viro32a79912012-07-18 20:43:19 +04001889 lock_rcu_walk();
Al Viroe41f7d42011-02-22 14:02:58 -05001890
1891 do {
1892 seq = read_seqcount_begin(&fs->seq);
1893 nd->path = fs->pwd;
1894 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1895 } while (read_seqcount_retry(&fs->seq, seq));
1896 } else {
1897 get_fs_pwd(current->fs, &nd->path);
1898 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001899 } else {
Jeff Layton582aa642012-12-11 08:56:16 -05001900 /* Caller must check execute permissions on the starting path component */
Al Viro2903ff02012-08-28 12:52:22 -04001901 struct fd f = fdget_raw(dfd);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001902 struct dentry *dentry;
1903
Al Viro2903ff02012-08-28 12:52:22 -04001904 if (!f.file)
1905 return -EBADF;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001906
Al Viro2903ff02012-08-28 12:52:22 -04001907 dentry = f.file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001908
Al Virof52e0c12011-03-14 18:56:51 -04001909 if (*name) {
Al Viro741b7c32012-12-20 13:41:28 -05001910 if (!can_lookup(dentry->d_inode)) {
Al Viro2903ff02012-08-28 12:52:22 -04001911 fdput(f);
1912 return -ENOTDIR;
1913 }
Al Virof52e0c12011-03-14 18:56:51 -04001914 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001915
Al Viro2903ff02012-08-28 12:52:22 -04001916 nd->path = f.file->f_path;
Al Viroe41f7d42011-02-22 14:02:58 -05001917 if (flags & LOOKUP_RCU) {
Al Viro2903ff02012-08-28 12:52:22 -04001918 if (f.need_put)
1919 *fp = f.file;
Al Viroe41f7d42011-02-22 14:02:58 -05001920 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro32a79912012-07-18 20:43:19 +04001921 lock_rcu_walk();
Al Viroe41f7d42011-02-22 14:02:58 -05001922 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001923 path_get(&nd->path);
1924 fdput(f);
Al Viroe41f7d42011-02-22 14:02:58 -05001925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 }
Al Viroe41f7d42011-02-22 14:02:58 -05001927
Nick Piggin31e6b012011-01-07 17:49:52 +11001928 nd->inode = nd->path.dentry->d_inode;
Al Viro9b4a9b12009-04-07 11:44:16 -04001929 return 0;
Al Viro9b4a9b12009-04-07 11:44:16 -04001930}
1931
Al Virobd92d7f2011-03-14 19:54:59 -04001932static inline int lookup_last(struct nameidata *nd, struct path *path)
1933{
1934 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1935 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1936
1937 nd->flags &= ~LOOKUP_PARENT;
Al Viro21b9b072013-01-24 18:10:25 -05001938 return walk_component(nd, path, nd->flags & LOOKUP_FOLLOW);
Al Virobd92d7f2011-03-14 19:54:59 -04001939}
1940
Al Viro9b4a9b12009-04-07 11:44:16 -04001941/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroee0827c2011-02-21 23:38:09 -05001942static int path_lookupat(int dfd, const char *name,
Al Viro9b4a9b12009-04-07 11:44:16 -04001943 unsigned int flags, struct nameidata *nd)
1944{
Al Viro70e9b352011-03-05 21:12:22 -05001945 struct file *base = NULL;
Al Virobd92d7f2011-03-14 19:54:59 -04001946 struct path path;
1947 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001948
1949 /*
1950 * Path walking is largely split up into 2 different synchronisation
1951 * schemes, rcu-walk and ref-walk (explained in
1952 * Documentation/filesystems/path-lookup.txt). These share much of the
1953 * path walk code, but some things particularly setup, cleanup, and
1954 * following mounts are sufficiently divergent that functions are
1955 * duplicated. Typically there is a function foo(), and its RCU
1956 * analogue, foo_rcu().
1957 *
1958 * -ECHILD is the error number of choice (just to avoid clashes) that
1959 * is returned if some aspect of an rcu-walk fails. Such an error must
1960 * be handled by restarting a traditional ref-walk (which will always
1961 * be able to complete).
1962 */
Al Virobd92d7f2011-03-14 19:54:59 -04001963 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
Al Viroee0827c2011-02-21 23:38:09 -05001964
Al Virobd92d7f2011-03-14 19:54:59 -04001965 if (unlikely(err))
1966 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001967
1968 current->total_link_count = 0;
Al Virobd92d7f2011-03-14 19:54:59 -04001969 err = link_path_walk(name, nd);
1970
1971 if (!err && !(flags & LOOKUP_PARENT)) {
Al Virobd92d7f2011-03-14 19:54:59 -04001972 err = lookup_last(nd, &path);
1973 while (err > 0) {
1974 void *cookie;
1975 struct path link = path;
Kees Cook800179c2012-07-25 17:29:07 -07001976 err = may_follow_link(&link, nd);
1977 if (unlikely(err))
1978 break;
Al Virobd92d7f2011-03-14 19:54:59 -04001979 nd->flags |= LOOKUP_PARENT;
Al Viro574197e2011-03-14 22:20:34 -04001980 err = follow_link(&link, nd, &cookie);
Al Viro6d7b5aa2012-06-10 04:15:17 -04001981 if (err)
1982 break;
1983 err = lookup_last(nd, &path);
Al Viro574197e2011-03-14 22:20:34 -04001984 put_link(nd, &link, cookie);
Al Virobd92d7f2011-03-14 19:54:59 -04001985 }
1986 }
Al Viroee0827c2011-02-21 23:38:09 -05001987
Al Viro9f1fafe2011-03-25 11:00:12 -04001988 if (!err)
1989 err = complete_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04001990
1991 if (!err && nd->flags & LOOKUP_DIRECTORY) {
Al Viro05252902013-06-06 19:33:47 -04001992 if (!can_lookup(nd->inode)) {
Al Virobd92d7f2011-03-14 19:54:59 -04001993 path_put(&nd->path);
Al Virobd23a532011-03-23 09:56:30 -04001994 err = -ENOTDIR;
Al Virobd92d7f2011-03-14 19:54:59 -04001995 }
1996 }
Al Viro16c2cd72011-02-22 15:50:10 -05001997
Al Viro70e9b352011-03-05 21:12:22 -05001998 if (base)
1999 fput(base);
Al Viroee0827c2011-02-21 23:38:09 -05002000
Al Viro5b6ca022011-03-09 23:04:47 -05002001 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
Al Viro2a737872009-04-07 11:49:53 -04002002 path_put(&nd->root);
2003 nd->root.mnt = NULL;
2004 }
Al Virobd92d7f2011-03-14 19:54:59 -04002005 return err;
Al Viroee0827c2011-02-21 23:38:09 -05002006}
Nick Piggin31e6b012011-01-07 17:49:52 +11002007
Jeff Layton873f1ee2012-10-10 15:25:29 -04002008static int filename_lookup(int dfd, struct filename *name,
2009 unsigned int flags, struct nameidata *nd)
2010{
2011 int retval = path_lookupat(dfd, name->name, flags | LOOKUP_RCU, nd);
2012 if (unlikely(retval == -ECHILD))
2013 retval = path_lookupat(dfd, name->name, flags, nd);
2014 if (unlikely(retval == -ESTALE))
2015 retval = path_lookupat(dfd, name->name,
2016 flags | LOOKUP_REVAL, nd);
2017
2018 if (likely(!retval))
Jeff Laytonadb5c242012-10-10 16:43:13 -04002019 audit_inode(name, nd->path.dentry, flags & LOOKUP_PARENT);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002020 return retval;
2021}
2022
Al Viroee0827c2011-02-21 23:38:09 -05002023static int do_path_lookup(int dfd, const char *name,
2024 unsigned int flags, struct nameidata *nd)
2025{
Jeff Layton873f1ee2012-10-10 15:25:29 -04002026 struct filename filename = { .name = name };
Nick Piggin31e6b012011-01-07 17:49:52 +11002027
Jeff Layton873f1ee2012-10-10 15:25:29 -04002028 return filename_lookup(dfd, &filename, flags, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029}
2030
Al Viro79714f72012-06-15 03:01:42 +04002031/* does lookup, returns the object with parent locked */
2032struct dentry *kern_path_locked(const char *name, struct path *path)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002033{
Al Viro79714f72012-06-15 03:01:42 +04002034 struct nameidata nd;
2035 struct dentry *d;
2036 int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd);
2037 if (err)
2038 return ERR_PTR(err);
2039 if (nd.last_type != LAST_NORM) {
2040 path_put(&nd.path);
2041 return ERR_PTR(-EINVAL);
2042 }
2043 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Al Viro1e0ea002012-07-22 23:46:21 +04002044 d = __lookup_hash(&nd.last, nd.path.dentry, 0);
Al Viro79714f72012-06-15 03:01:42 +04002045 if (IS_ERR(d)) {
2046 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2047 path_put(&nd.path);
2048 return d;
2049 }
2050 *path = nd.path;
2051 return d;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002052}
2053
Al Virod1811462008-08-02 00:49:18 -04002054int kern_path(const char *name, unsigned int flags, struct path *path)
2055{
2056 struct nameidata nd;
2057 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
2058 if (!res)
2059 *path = nd.path;
2060 return res;
2061}
2062
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002063/**
2064 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2065 * @dentry: pointer to dentry of the base directory
2066 * @mnt: pointer to vfs mount of the base directory
2067 * @name: pointer to file name
2068 * @flags: lookup flags
Al Viroe0a01242011-06-27 17:00:37 -04002069 * @path: pointer to struct path to fill
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002070 */
2071int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2072 const char *name, unsigned int flags,
Al Viroe0a01242011-06-27 17:00:37 -04002073 struct path *path)
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002074{
Al Viroe0a01242011-06-27 17:00:37 -04002075 struct nameidata nd;
2076 int err;
2077 nd.root.dentry = dentry;
2078 nd.root.mnt = mnt;
2079 BUG_ON(flags & LOOKUP_PARENT);
Al Viro5b6ca022011-03-09 23:04:47 -05002080 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
Al Viroe0a01242011-06-27 17:00:37 -04002081 err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
2082 if (!err)
2083 *path = nd.path;
2084 return err;
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002085}
2086
James Morris057f6c02007-04-26 00:12:05 -07002087/*
2088 * Restricted form of lookup. Doesn't follow links, single-component only,
2089 * needs parent already locked. Doesn't follow mounts.
2090 * SMP-safe.
2091 */
Adrian Bunka244e162006-03-31 02:32:11 -08002092static struct dentry *lookup_hash(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093{
Al Viro72bd8662012-06-10 17:17:17 -04002094 return __lookup_hash(&nd->last, nd->path.dentry, nd->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095}
2096
Christoph Hellwigeead1912007-10-16 23:25:38 -07002097/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07002098 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07002099 * @name: pathname component to lookup
2100 * @base: base directory to lookup from
2101 * @len: maximum length @len should be interpreted to
2102 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07002103 * Note that this routine is purely a helper for filesystem usage and should
2104 * not be called by generic code. Also note that by using this function the
Christoph Hellwigeead1912007-10-16 23:25:38 -07002105 * nameidata argument is passed to the filesystem methods and a filesystem
2106 * using this helper needs to be prepared for that.
2107 */
James Morris057f6c02007-04-26 00:12:05 -07002108struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2109{
James Morris057f6c02007-04-26 00:12:05 -07002110 struct qstr this;
Al Viro6a96ba52011-03-07 23:49:20 -05002111 unsigned int c;
Miklos Szeredicda309d2012-03-26 12:54:21 +02002112 int err;
James Morris057f6c02007-04-26 00:12:05 -07002113
David Woodhouse2f9092e2009-04-20 23:18:37 +01002114 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
2115
Al Viro6a96ba52011-03-07 23:49:20 -05002116 this.name = name;
2117 this.len = len;
Linus Torvalds0145acc2012-03-02 14:32:59 -08002118 this.hash = full_name_hash(name, len);
Al Viro6a96ba52011-03-07 23:49:20 -05002119 if (!len)
2120 return ERR_PTR(-EACCES);
2121
Al Viro21d8a152012-11-29 22:17:21 -05002122 if (unlikely(name[0] == '.')) {
2123 if (len < 2 || (len == 2 && name[1] == '.'))
2124 return ERR_PTR(-EACCES);
2125 }
2126
Al Viro6a96ba52011-03-07 23:49:20 -05002127 while (len--) {
2128 c = *(const unsigned char *)name++;
2129 if (c == '/' || c == '\0')
2130 return ERR_PTR(-EACCES);
Al Viro6a96ba52011-03-07 23:49:20 -05002131 }
Al Viro5a202bc2011-03-08 14:17:44 -05002132 /*
2133 * See if the low-level filesystem might want
2134 * to use its own hash..
2135 */
2136 if (base->d_flags & DCACHE_OP_HASH) {
Linus Torvaldsda53be12013-05-21 15:22:44 -07002137 int err = base->d_op->d_hash(base, &this);
Al Viro5a202bc2011-03-08 14:17:44 -05002138 if (err < 0)
2139 return ERR_PTR(err);
2140 }
Christoph Hellwigeead1912007-10-16 23:25:38 -07002141
Miklos Szeredicda309d2012-03-26 12:54:21 +02002142 err = inode_permission(base->d_inode, MAY_EXEC);
2143 if (err)
2144 return ERR_PTR(err);
2145
Al Viro72bd8662012-06-10 17:17:17 -04002146 return __lookup_hash(&this, base, 0);
James Morris057f6c02007-04-26 00:12:05 -07002147}
2148
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002149int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2150 struct path *path, int *empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151{
Al Viro2d8f3032008-07-22 09:59:21 -04002152 struct nameidata nd;
Jeff Layton91a27b22012-10-10 15:25:28 -04002153 struct filename *tmp = getname_flags(name, flags, empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 int err = PTR_ERR(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 if (!IS_ERR(tmp)) {
Al Viro2d8f3032008-07-22 09:59:21 -04002156
2157 BUG_ON(flags & LOOKUP_PARENT);
2158
Jeff Layton873f1ee2012-10-10 15:25:29 -04002159 err = filename_lookup(dfd, tmp, flags, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 putname(tmp);
Al Viro2d8f3032008-07-22 09:59:21 -04002161 if (!err)
2162 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 }
2164 return err;
2165}
2166
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002167int user_path_at(int dfd, const char __user *name, unsigned flags,
2168 struct path *path)
2169{
Linus Torvaldsf7493e52012-03-22 16:10:40 -07002170 return user_path_at_empty(dfd, name, flags, path, NULL);
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002171}
2172
Jeff Layton873f1ee2012-10-10 15:25:29 -04002173/*
2174 * NB: most callers don't do anything directly with the reference to the
2175 * to struct filename, but the nd->last pointer points into the name string
2176 * allocated by getname. So we must hold the reference to it until all
2177 * path-walking is complete.
2178 */
Jeff Layton91a27b22012-10-10 15:25:28 -04002179static struct filename *
Jeff Layton9e790bd2012-12-11 12:10:09 -05002180user_path_parent(int dfd, const char __user *path, struct nameidata *nd,
2181 unsigned int flags)
Al Viro2ad94ae2008-07-21 09:32:51 -04002182{
Jeff Layton91a27b22012-10-10 15:25:28 -04002183 struct filename *s = getname(path);
Al Viro2ad94ae2008-07-21 09:32:51 -04002184 int error;
2185
Jeff Layton9e790bd2012-12-11 12:10:09 -05002186 /* only LOOKUP_REVAL is allowed in extra flags */
2187 flags &= LOOKUP_REVAL;
2188
Al Viro2ad94ae2008-07-21 09:32:51 -04002189 if (IS_ERR(s))
Jeff Layton91a27b22012-10-10 15:25:28 -04002190 return s;
Al Viro2ad94ae2008-07-21 09:32:51 -04002191
Jeff Layton9e790bd2012-12-11 12:10:09 -05002192 error = filename_lookup(dfd, s, flags | LOOKUP_PARENT, nd);
Jeff Layton91a27b22012-10-10 15:25:28 -04002193 if (error) {
Al Viro2ad94ae2008-07-21 09:32:51 -04002194 putname(s);
Jeff Layton91a27b22012-10-10 15:25:28 -04002195 return ERR_PTR(error);
2196 }
Al Viro2ad94ae2008-07-21 09:32:51 -04002197
Jeff Layton91a27b22012-10-10 15:25:28 -04002198 return s;
Al Viro2ad94ae2008-07-21 09:32:51 -04002199}
2200
Jeff Layton80334262013-07-26 06:23:25 -04002201/**
Al Viro197df042013-09-08 14:03:27 -04002202 * mountpoint_last - look up last component for umount
Jeff Layton80334262013-07-26 06:23:25 -04002203 * @nd: pathwalk nameidata - currently pointing at parent directory of "last"
2204 * @path: pointer to container for result
2205 *
2206 * This is a special lookup_last function just for umount. In this case, we
2207 * need to resolve the path without doing any revalidation.
2208 *
2209 * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
2210 * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
2211 * in almost all cases, this lookup will be served out of the dcache. The only
2212 * cases where it won't are if nd->last refers to a symlink or the path is
2213 * bogus and it doesn't exist.
2214 *
2215 * Returns:
2216 * -error: if there was an error during lookup. This includes -ENOENT if the
2217 * lookup found a negative dentry. The nd->path reference will also be
2218 * put in this case.
2219 *
2220 * 0: if we successfully resolved nd->path and found it to not to be a
2221 * symlink that needs to be followed. "path" will also be populated.
2222 * The nd->path reference will also be put.
2223 *
2224 * 1: if we successfully resolved nd->last and found it to be a symlink
2225 * that needs to be followed. "path" will be populated with the path
2226 * to the link, and nd->path will *not* be put.
2227 */
2228static int
Al Viro197df042013-09-08 14:03:27 -04002229mountpoint_last(struct nameidata *nd, struct path *path)
Jeff Layton80334262013-07-26 06:23:25 -04002230{
2231 int error = 0;
2232 struct dentry *dentry;
2233 struct dentry *dir = nd->path.dentry;
2234
Al Viro35759522013-09-08 13:41:33 -04002235 /* If we're in rcuwalk, drop out of it to handle last component */
2236 if (nd->flags & LOOKUP_RCU) {
2237 if (unlazy_walk(nd, NULL)) {
2238 error = -ECHILD;
2239 goto out;
2240 }
Jeff Layton80334262013-07-26 06:23:25 -04002241 }
2242
2243 nd->flags &= ~LOOKUP_PARENT;
2244
2245 if (unlikely(nd->last_type != LAST_NORM)) {
2246 error = handle_dots(nd, nd->last_type);
Al Viro35759522013-09-08 13:41:33 -04002247 if (error)
2248 goto out;
2249 dentry = dget(nd->path.dentry);
2250 goto done;
Jeff Layton80334262013-07-26 06:23:25 -04002251 }
2252
2253 mutex_lock(&dir->d_inode->i_mutex);
2254 dentry = d_lookup(dir, &nd->last);
2255 if (!dentry) {
2256 /*
2257 * No cached dentry. Mounted dentries are pinned in the cache,
2258 * so that means that this dentry is probably a symlink or the
2259 * path doesn't actually point to a mounted dentry.
2260 */
2261 dentry = d_alloc(dir, &nd->last);
2262 if (!dentry) {
2263 error = -ENOMEM;
Dave Jonesbcceeeb2013-09-10 17:04:25 -04002264 mutex_unlock(&dir->d_inode->i_mutex);
Al Viro35759522013-09-08 13:41:33 -04002265 goto out;
Jeff Layton80334262013-07-26 06:23:25 -04002266 }
Al Viro35759522013-09-08 13:41:33 -04002267 dentry = lookup_real(dir->d_inode, dentry, nd->flags);
2268 error = PTR_ERR(dentry);
Dave Jonesbcceeeb2013-09-10 17:04:25 -04002269 if (IS_ERR(dentry)) {
2270 mutex_unlock(&dir->d_inode->i_mutex);
Al Viro35759522013-09-08 13:41:33 -04002271 goto out;
Dave Jonesbcceeeb2013-09-10 17:04:25 -04002272 }
Jeff Layton80334262013-07-26 06:23:25 -04002273 }
2274 mutex_unlock(&dir->d_inode->i_mutex);
2275
Al Viro35759522013-09-08 13:41:33 -04002276done:
2277 if (!dentry->d_inode) {
2278 error = -ENOENT;
2279 dput(dentry);
2280 goto out;
Jeff Layton80334262013-07-26 06:23:25 -04002281 }
Al Viro35759522013-09-08 13:41:33 -04002282 path->dentry = dentry;
2283 path->mnt = mntget(nd->path.mnt);
2284 if (should_follow_link(dentry->d_inode, nd->flags & LOOKUP_FOLLOW))
2285 return 1;
2286 follow_mount(path);
2287 error = 0;
2288out:
Jeff Layton80334262013-07-26 06:23:25 -04002289 terminate_walk(nd);
2290 return error;
2291}
2292
2293/**
Al Viro197df042013-09-08 14:03:27 -04002294 * path_mountpoint - look up a path to be umounted
Jeff Layton80334262013-07-26 06:23:25 -04002295 * @dfd: directory file descriptor to start walk from
2296 * @name: full pathname to walk
2297 * @flags: lookup flags
Jeff Layton80334262013-07-26 06:23:25 -04002298 *
2299 * Look up the given name, but don't attempt to revalidate the last component.
2300 * Returns 0 and "path" will be valid on success; Retuns error otherwise.
2301 */
2302static int
Al Viro197df042013-09-08 14:03:27 -04002303path_mountpoint(int dfd, const char *name, struct path *path, unsigned int flags)
Jeff Layton80334262013-07-26 06:23:25 -04002304{
2305 struct file *base = NULL;
2306 struct nameidata nd;
2307 int err;
2308
2309 err = path_init(dfd, name, flags | LOOKUP_PARENT, &nd, &base);
2310 if (unlikely(err))
2311 return err;
2312
2313 current->total_link_count = 0;
2314 err = link_path_walk(name, &nd);
2315 if (err)
2316 goto out;
2317
Al Viro197df042013-09-08 14:03:27 -04002318 err = mountpoint_last(&nd, path);
Jeff Layton80334262013-07-26 06:23:25 -04002319 while (err > 0) {
2320 void *cookie;
2321 struct path link = *path;
2322 err = may_follow_link(&link, &nd);
2323 if (unlikely(err))
2324 break;
2325 nd.flags |= LOOKUP_PARENT;
2326 err = follow_link(&link, &nd, &cookie);
2327 if (err)
2328 break;
Al Viro197df042013-09-08 14:03:27 -04002329 err = mountpoint_last(&nd, path);
Jeff Layton80334262013-07-26 06:23:25 -04002330 put_link(&nd, &link, cookie);
2331 }
2332out:
2333 if (base)
2334 fput(base);
2335
2336 if (nd.root.mnt && !(nd.flags & LOOKUP_ROOT))
2337 path_put(&nd.root);
2338
2339 return err;
2340}
2341
Al Viro2d864652013-09-08 20:18:44 -04002342static int
2343filename_mountpoint(int dfd, struct filename *s, struct path *path,
2344 unsigned int flags)
2345{
2346 int error = path_mountpoint(dfd, s->name, path, flags | LOOKUP_RCU);
2347 if (unlikely(error == -ECHILD))
2348 error = path_mountpoint(dfd, s->name, path, flags);
2349 if (unlikely(error == -ESTALE))
2350 error = path_mountpoint(dfd, s->name, path, flags | LOOKUP_REVAL);
2351 if (likely(!error))
2352 audit_inode(s, path->dentry, 0);
2353 return error;
2354}
2355
Jeff Layton80334262013-07-26 06:23:25 -04002356/**
Al Viro197df042013-09-08 14:03:27 -04002357 * user_path_mountpoint_at - lookup a path from userland in order to umount it
Jeff Layton80334262013-07-26 06:23:25 -04002358 * @dfd: directory file descriptor
2359 * @name: pathname from userland
2360 * @flags: lookup flags
2361 * @path: pointer to container to hold result
2362 *
2363 * A umount is a special case for path walking. We're not actually interested
2364 * in the inode in this situation, and ESTALE errors can be a problem. We
2365 * simply want track down the dentry and vfsmount attached at the mountpoint
2366 * and avoid revalidating the last component.
2367 *
2368 * Returns 0 and populates "path" on success.
2369 */
2370int
Al Viro197df042013-09-08 14:03:27 -04002371user_path_mountpoint_at(int dfd, const char __user *name, unsigned int flags,
Jeff Layton80334262013-07-26 06:23:25 -04002372 struct path *path)
2373{
2374 struct filename *s = getname(name);
2375 int error;
Jeff Layton80334262013-07-26 06:23:25 -04002376 if (IS_ERR(s))
2377 return PTR_ERR(s);
Al Viro2d864652013-09-08 20:18:44 -04002378 error = filename_mountpoint(dfd, s, path, flags);
Jeff Layton80334262013-07-26 06:23:25 -04002379 putname(s);
2380 return error;
2381}
2382
Al Viro2d864652013-09-08 20:18:44 -04002383int
2384kern_path_mountpoint(int dfd, const char *name, struct path *path,
2385 unsigned int flags)
2386{
2387 struct filename s = {.name = name};
2388 return filename_mountpoint(dfd, &s, path, flags);
2389}
2390EXPORT_SYMBOL(kern_path_mountpoint);
2391
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392/*
2393 * It's inline, so penalty for filesystems that don't use sticky bit is
2394 * minimal.
2395 */
2396static inline int check_sticky(struct inode *dir, struct inode *inode)
2397{
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002398 kuid_t fsuid = current_fsuid();
David Howellsda9592e2008-11-14 10:39:05 +11002399
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 if (!(dir->i_mode & S_ISVTX))
2401 return 0;
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002402 if (uid_eq(inode->i_uid, fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 return 0;
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002404 if (uid_eq(dir->i_uid, fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 return 0;
Eric W. Biederman1a48e2a2011-11-14 16:24:06 -08002406 return !inode_capable(inode, CAP_FOWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407}
2408
2409/*
2410 * Check whether we can remove a link victim from directory dir, check
2411 * whether the type of victim is right.
2412 * 1. We can't do it if dir is read-only (done in permission())
2413 * 2. We should have write and exec permissions on dir
2414 * 3. We can't remove anything from append-only dir
2415 * 4. We can't do anything with immutable dir (done in permission())
2416 * 5. If the sticky bit on dir is set we should either
2417 * a. be owner of dir, or
2418 * b. be owner of victim, or
2419 * c. have CAP_FOWNER capability
2420 * 6. If the victim is append-only or immutable we can't do antyhing with
2421 * links pointing to it.
2422 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2423 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2424 * 9. We can't remove a root or mountpoint.
2425 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
2426 * nfs_async_unlink().
2427 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08002428static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429{
2430 int error;
2431
2432 if (!victim->d_inode)
2433 return -ENOENT;
2434
2435 BUG_ON(victim->d_parent->d_inode != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04002436 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
Al Virof419a2e2008-07-22 00:07:17 -04002438 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 if (error)
2440 return error;
2441 if (IS_APPEND(dir))
2442 return -EPERM;
2443 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
Hugh Dickinsf9454542008-11-19 15:36:38 -08002444 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 return -EPERM;
2446 if (isdir) {
2447 if (!S_ISDIR(victim->d_inode->i_mode))
2448 return -ENOTDIR;
2449 if (IS_ROOT(victim))
2450 return -EBUSY;
2451 } else if (S_ISDIR(victim->d_inode->i_mode))
2452 return -EISDIR;
2453 if (IS_DEADDIR(dir))
2454 return -ENOENT;
2455 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2456 return -EBUSY;
2457 return 0;
2458}
2459
2460/* Check whether we can create an object with dentry child in directory
2461 * dir.
2462 * 1. We can't do it if child already exists (open has special treatment for
2463 * this case, but since we are inlined it's OK)
2464 * 2. We can't do it if dir is read-only (done in permission())
2465 * 3. We should have write and exec permissions on dir
2466 * 4. We can't do it if dir is immutable (done in permission())
2467 */
Miklos Szeredia95164d2008-07-30 15:08:48 +02002468static inline int may_create(struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469{
2470 if (child->d_inode)
2471 return -EEXIST;
2472 if (IS_DEADDIR(dir))
2473 return -ENOENT;
Al Virof419a2e2008-07-22 00:07:17 -04002474 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475}
2476
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477/*
2478 * p1 and p2 should be directories on the same fs.
2479 */
2480struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2481{
2482 struct dentry *p;
2483
2484 if (p1 == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07002485 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 return NULL;
2487 }
2488
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002489 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002491 p = d_ancestor(p2, p1);
2492 if (p) {
2493 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2494 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
2495 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 }
2497
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002498 p = d_ancestor(p1, p2);
2499 if (p) {
2500 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2501 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2502 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 }
2504
Ingo Molnarf2eace22006-07-03 00:25:05 -07002505 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2506 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 return NULL;
2508}
2509
2510void unlock_rename(struct dentry *p1, struct dentry *p2)
2511{
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002512 mutex_unlock(&p1->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 if (p1 != p2) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002514 mutex_unlock(&p2->d_inode->i_mutex);
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002515 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 }
2517}
2518
Al Viro4acdaf22011-07-26 01:42:34 -04002519int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viro312b63f2012-06-10 18:09:36 -04002520 bool want_excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002522 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 if (error)
2524 return error;
2525
Al Viroacfa4382008-12-04 10:06:33 -05002526 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 return -EACCES; /* shouldn't it be ENOSYS? */
2528 mode &= S_IALLUGO;
2529 mode |= S_IFREG;
2530 error = security_inode_create(dir, dentry, mode);
2531 if (error)
2532 return error;
Al Viro312b63f2012-06-10 18:09:36 -04002533 error = dir->i_op->create(dir, dentry, mode, want_excl);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002534 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002535 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 return error;
2537}
2538
Al Viro73d049a2011-03-11 12:08:24 -05002539static int may_open(struct path *path, int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002541 struct dentry *dentry = path->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 struct inode *inode = dentry->d_inode;
2543 int error;
2544
Al Virobcda7652011-03-13 16:42:14 -04002545 /* O_PATH? */
2546 if (!acc_mode)
2547 return 0;
2548
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 if (!inode)
2550 return -ENOENT;
2551
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002552 switch (inode->i_mode & S_IFMT) {
2553 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002555 case S_IFDIR:
2556 if (acc_mode & MAY_WRITE)
2557 return -EISDIR;
2558 break;
2559 case S_IFBLK:
2560 case S_IFCHR:
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002561 if (path->mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002563 /*FALLTHRU*/
2564 case S_IFIFO:
2565 case S_IFSOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002567 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08002568 }
Dave Hansenb41572e2007-10-16 23:31:14 -07002569
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002570 error = inode_permission(inode, acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07002571 if (error)
2572 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05002573
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 /*
2575 * An append-only file must be opened in append mode for writing.
2576 */
2577 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05002578 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05002579 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05002581 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 }
2583
2584 /* O_NOATIME can only be set by the owner or superuser */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07002585 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
Al Viro7715b522009-12-16 03:54:00 -05002586 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
J. Bruce Fieldsf3c7691e2011-09-21 10:58:13 -04002588 return 0;
Al Viro7715b522009-12-16 03:54:00 -05002589}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
Jeff Laytone1181ee2010-12-07 16:19:50 -05002591static int handle_truncate(struct file *filp)
Al Viro7715b522009-12-16 03:54:00 -05002592{
Jeff Laytone1181ee2010-12-07 16:19:50 -05002593 struct path *path = &filp->f_path;
Al Viro7715b522009-12-16 03:54:00 -05002594 struct inode *inode = path->dentry->d_inode;
2595 int error = get_write_access(inode);
2596 if (error)
2597 return error;
2598 /*
2599 * Refuse to truncate files with mandatory locks held on them.
2600 */
2601 error = locks_verify_locked(inode);
2602 if (!error)
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +09002603 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05002604 if (!error) {
2605 error = do_truncate(path->dentry, 0,
2606 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
Jeff Laytone1181ee2010-12-07 16:19:50 -05002607 filp);
Al Viro7715b522009-12-16 03:54:00 -05002608 }
2609 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04002610 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611}
2612
Dave Hansend57999e2008-02-15 14:37:27 -08002613static inline int open_to_namei_flags(int flag)
2614{
Al Viro8a5e9292011-06-25 19:15:54 -04002615 if ((flag & O_ACCMODE) == 3)
2616 flag--;
Dave Hansend57999e2008-02-15 14:37:27 -08002617 return flag;
2618}
2619
Miklos Szeredid18e9002012-06-05 15:10:17 +02002620static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode)
2621{
2622 int error = security_path_mknod(dir, dentry, mode, 0);
2623 if (error)
2624 return error;
2625
2626 error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
2627 if (error)
2628 return error;
2629
2630 return security_inode_create(dir->dentry->d_inode, dentry, mode);
2631}
2632
David Howells1acf0af2012-06-14 16:13:46 +01002633/*
2634 * Attempt to atomically look up, create and open a file from a negative
2635 * dentry.
2636 *
2637 * Returns 0 if successful. The file will have been created and attached to
2638 * @file by the filesystem calling finish_open().
2639 *
2640 * Returns 1 if the file was looked up only or didn't need creating. The
2641 * caller will need to perform the open themselves. @path will have been
2642 * updated to point to the new dentry. This may be negative.
2643 *
2644 * Returns an error code otherwise.
2645 */
Al Viro2675a4e2012-06-22 12:41:10 +04002646static int atomic_open(struct nameidata *nd, struct dentry *dentry,
2647 struct path *path, struct file *file,
2648 const struct open_flags *op,
Al Viro64894cf2012-07-31 00:53:35 +04002649 bool got_write, bool need_lookup,
Al Viro2675a4e2012-06-22 12:41:10 +04002650 int *opened)
Miklos Szeredid18e9002012-06-05 15:10:17 +02002651{
2652 struct inode *dir = nd->path.dentry->d_inode;
2653 unsigned open_flag = open_to_namei_flags(op->open_flag);
2654 umode_t mode;
2655 int error;
2656 int acc_mode;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002657 int create_error = 0;
2658 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
Miklos Szeredi116cc022013-09-16 14:52:05 +02002659 bool excl;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002660
2661 BUG_ON(dentry->d_inode);
2662
2663 /* Don't create child dentry for a dead directory. */
2664 if (unlikely(IS_DEADDIR(dir))) {
Al Viro2675a4e2012-06-22 12:41:10 +04002665 error = -ENOENT;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002666 goto out;
2667 }
2668
Miklos Szeredi62b259d2012-08-15 13:01:24 +02002669 mode = op->mode;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002670 if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
2671 mode &= ~current_umask();
2672
Miklos Szeredi116cc022013-09-16 14:52:05 +02002673 excl = (open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT);
2674 if (excl)
Miklos Szeredid18e9002012-06-05 15:10:17 +02002675 open_flag &= ~O_TRUNC;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002676
2677 /*
2678 * Checking write permission is tricky, bacuse we don't know if we are
2679 * going to actually need it: O_CREAT opens should work as long as the
2680 * file exists. But checking existence breaks atomicity. The trick is
2681 * to check access and if not granted clear O_CREAT from the flags.
2682 *
2683 * Another problem is returing the "right" error value (e.g. for an
2684 * O_EXCL open we want to return EEXIST not EROFS).
2685 */
Al Viro64894cf2012-07-31 00:53:35 +04002686 if (((open_flag & (O_CREAT | O_TRUNC)) ||
2687 (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) {
2688 if (!(open_flag & O_CREAT)) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002689 /*
2690 * No O_CREATE -> atomicity not a requirement -> fall
2691 * back to lookup + open
2692 */
2693 goto no_open;
2694 } else if (open_flag & (O_EXCL | O_TRUNC)) {
2695 /* Fall back and fail with the right error */
Al Viro64894cf2012-07-31 00:53:35 +04002696 create_error = -EROFS;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002697 goto no_open;
2698 } else {
2699 /* No side effects, safe to clear O_CREAT */
Al Viro64894cf2012-07-31 00:53:35 +04002700 create_error = -EROFS;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002701 open_flag &= ~O_CREAT;
2702 }
2703 }
2704
2705 if (open_flag & O_CREAT) {
Miklos Szeredi38227f72012-08-15 13:01:24 +02002706 error = may_o_create(&nd->path, dentry, mode);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002707 if (error) {
2708 create_error = error;
2709 if (open_flag & O_EXCL)
2710 goto no_open;
2711 open_flag &= ~O_CREAT;
2712 }
2713 }
2714
2715 if (nd->flags & LOOKUP_DIRECTORY)
2716 open_flag |= O_DIRECTORY;
2717
Al Viro30d90492012-06-22 12:40:19 +04002718 file->f_path.dentry = DENTRY_NOT_SET;
2719 file->f_path.mnt = nd->path.mnt;
2720 error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode,
Al Viro47237682012-06-10 05:01:45 -04002721 opened);
Al Virod9585272012-06-22 12:39:14 +04002722 if (error < 0) {
Al Virod9585272012-06-22 12:39:14 +04002723 if (create_error && error == -ENOENT)
2724 error = create_error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002725 goto out;
2726 }
2727
Al Virod9585272012-06-22 12:39:14 +04002728 if (error) { /* returned 1, that is */
Al Viro30d90492012-06-22 12:40:19 +04002729 if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
Al Viro2675a4e2012-06-22 12:41:10 +04002730 error = -EIO;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002731 goto out;
2732 }
Al Viro30d90492012-06-22 12:40:19 +04002733 if (file->f_path.dentry) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002734 dput(dentry);
Al Viro30d90492012-06-22 12:40:19 +04002735 dentry = file->f_path.dentry;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002736 }
Al Viro03da6332013-09-16 19:22:33 -04002737 if (*opened & FILE_CREATED)
2738 fsnotify_create(dir, dentry);
2739 if (!dentry->d_inode) {
2740 WARN_ON(*opened & FILE_CREATED);
2741 if (create_error) {
2742 error = create_error;
2743 goto out;
2744 }
2745 } else {
2746 if (excl && !(*opened & FILE_CREATED)) {
2747 error = -EEXIST;
2748 goto out;
2749 }
Sage Weil62b2ce92012-08-15 13:30:12 -07002750 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02002751 goto looked_up;
2752 }
2753
2754 /*
2755 * We didn't have the inode before the open, so check open permission
2756 * here.
2757 */
Al Viro03da6332013-09-16 19:22:33 -04002758 acc_mode = op->acc_mode;
2759 if (*opened & FILE_CREATED) {
2760 WARN_ON(!(open_flag & O_CREAT));
2761 fsnotify_create(dir, dentry);
2762 acc_mode = MAY_OPEN;
2763 }
Al Viro2675a4e2012-06-22 12:41:10 +04002764 error = may_open(&file->f_path, acc_mode, open_flag);
2765 if (error)
2766 fput(file);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002767
2768out:
2769 dput(dentry);
Al Viro2675a4e2012-06-22 12:41:10 +04002770 return error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002771
Miklos Szeredid18e9002012-06-05 15:10:17 +02002772no_open:
2773 if (need_lookup) {
Al Viro72bd8662012-06-10 17:17:17 -04002774 dentry = lookup_real(dir, dentry, nd->flags);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002775 if (IS_ERR(dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04002776 return PTR_ERR(dentry);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002777
2778 if (create_error) {
2779 int open_flag = op->open_flag;
2780
Al Viro2675a4e2012-06-22 12:41:10 +04002781 error = create_error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002782 if ((open_flag & O_EXCL)) {
2783 if (!dentry->d_inode)
2784 goto out;
2785 } else if (!dentry->d_inode) {
2786 goto out;
2787 } else if ((open_flag & O_TRUNC) &&
2788 S_ISREG(dentry->d_inode->i_mode)) {
2789 goto out;
2790 }
2791 /* will fail later, go on to get the right error */
2792 }
2793 }
2794looked_up:
2795 path->dentry = dentry;
2796 path->mnt = nd->path.mnt;
Al Viro2675a4e2012-06-22 12:41:10 +04002797 return 1;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002798}
2799
Nick Piggin31e6b012011-01-07 17:49:52 +11002800/*
David Howells1acf0af2012-06-14 16:13:46 +01002801 * Look up and maybe create and open the last component.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002802 *
2803 * Must be called with i_mutex held on parent.
2804 *
David Howells1acf0af2012-06-14 16:13:46 +01002805 * Returns 0 if the file was successfully atomically created (if necessary) and
2806 * opened. In this case the file will be returned attached to @file.
2807 *
2808 * Returns 1 if the file was not completely opened at this time, though lookups
2809 * and creations will have been performed and the dentry returned in @path will
2810 * be positive upon return if O_CREAT was specified. If O_CREAT wasn't
2811 * specified then a negative dentry may be returned.
2812 *
2813 * An error code is returned otherwise.
2814 *
2815 * FILE_CREATE will be set in @*opened if the dentry was created and will be
2816 * cleared otherwise prior to returning.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002817 */
Al Viro2675a4e2012-06-22 12:41:10 +04002818static int lookup_open(struct nameidata *nd, struct path *path,
2819 struct file *file,
2820 const struct open_flags *op,
Al Viro64894cf2012-07-31 00:53:35 +04002821 bool got_write, int *opened)
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002822{
2823 struct dentry *dir = nd->path.dentry;
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002824 struct inode *dir_inode = dir->d_inode;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002825 struct dentry *dentry;
2826 int error;
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002827 bool need_lookup;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002828
Al Viro47237682012-06-10 05:01:45 -04002829 *opened &= ~FILE_CREATED;
Al Viro201f9562012-06-22 12:42:10 +04002830 dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002831 if (IS_ERR(dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04002832 return PTR_ERR(dentry);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002833
Miklos Szeredid18e9002012-06-05 15:10:17 +02002834 /* Cached positive dentry: will open in f_op->open */
2835 if (!need_lookup && dentry->d_inode)
2836 goto out_no_open;
2837
2838 if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) {
Al Viro64894cf2012-07-31 00:53:35 +04002839 return atomic_open(nd, dentry, path, file, op, got_write,
Al Viro47237682012-06-10 05:01:45 -04002840 need_lookup, opened);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002841 }
2842
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002843 if (need_lookup) {
2844 BUG_ON(dentry->d_inode);
2845
Al Viro72bd8662012-06-10 17:17:17 -04002846 dentry = lookup_real(dir_inode, dentry, nd->flags);
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002847 if (IS_ERR(dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04002848 return PTR_ERR(dentry);
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002849 }
2850
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002851 /* Negative dentry, just create the file */
2852 if (!dentry->d_inode && (op->open_flag & O_CREAT)) {
2853 umode_t mode = op->mode;
2854 if (!IS_POSIXACL(dir->d_inode))
2855 mode &= ~current_umask();
2856 /*
2857 * This write is needed to ensure that a
2858 * rw->ro transition does not occur between
2859 * the time when the file is created and when
2860 * a permanent write count is taken through
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02002861 * the 'struct file' in finish_open().
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002862 */
Al Viro64894cf2012-07-31 00:53:35 +04002863 if (!got_write) {
2864 error = -EROFS;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002865 goto out_dput;
Al Viro64894cf2012-07-31 00:53:35 +04002866 }
Al Viro47237682012-06-10 05:01:45 -04002867 *opened |= FILE_CREATED;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002868 error = security_path_mknod(&nd->path, dentry, mode, 0);
2869 if (error)
2870 goto out_dput;
Al Viro312b63f2012-06-10 18:09:36 -04002871 error = vfs_create(dir->d_inode, dentry, mode,
2872 nd->flags & LOOKUP_EXCL);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002873 if (error)
2874 goto out_dput;
2875 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02002876out_no_open:
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002877 path->dentry = dentry;
2878 path->mnt = nd->path.mnt;
Al Viro2675a4e2012-06-22 12:41:10 +04002879 return 1;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002880
2881out_dput:
2882 dput(dentry);
Al Viro2675a4e2012-06-22 12:41:10 +04002883 return error;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002884}
2885
2886/*
Al Virofe2d35f2011-03-05 22:58:25 -05002887 * Handle the last step of open()
Nick Piggin31e6b012011-01-07 17:49:52 +11002888 */
Al Viro2675a4e2012-06-22 12:41:10 +04002889static int do_last(struct nameidata *nd, struct path *path,
2890 struct file *file, const struct open_flags *op,
Jeff Layton669abf42012-10-10 16:43:10 -04002891 int *opened, struct filename *name)
Al Virofb1cc552009-12-24 01:58:28 -05002892{
Al Viroa1e28032009-12-24 02:12:06 -05002893 struct dentry *dir = nd->path.dentry;
Al Viroca344a892011-03-09 00:36:45 -05002894 int open_flag = op->open_flag;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002895 bool will_truncate = (open_flag & O_TRUNC) != 0;
Al Viro64894cf2012-07-31 00:53:35 +04002896 bool got_write = false;
Al Virobcda7652011-03-13 16:42:14 -04002897 int acc_mode = op->acc_mode;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02002898 struct inode *inode;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002899 bool symlink_ok = false;
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02002900 struct path save_parent = { .dentry = NULL, .mnt = NULL };
2901 bool retried = false;
Al Viro16c2cd72011-02-22 15:50:10 -05002902 int error;
Al Virofb1cc552009-12-24 01:58:28 -05002903
Al Viroc3e380b2011-02-23 13:39:45 -05002904 nd->flags &= ~LOOKUP_PARENT;
2905 nd->flags |= op->intent;
2906
Al Virobc77daa2013-06-06 09:12:33 -04002907 if (nd->last_type != LAST_NORM) {
Al Virofe2d35f2011-03-05 22:58:25 -05002908 error = handle_dots(nd, nd->last_type);
2909 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04002910 return error;
Miklos Szeredie83db162012-06-05 15:10:29 +02002911 goto finish_open;
Al Viro1f36f772009-12-26 10:56:19 -05002912 }
Al Viro67ee3ad2009-12-26 07:01:01 -05002913
Al Viroca344a892011-03-09 00:36:45 -05002914 if (!(open_flag & O_CREAT)) {
Al Virofe2d35f2011-03-05 22:58:25 -05002915 if (nd->last.name[nd->last.len])
2916 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
Al Virobcda7652011-03-13 16:42:14 -04002917 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002918 symlink_ok = true;
Al Virofe2d35f2011-03-05 22:58:25 -05002919 /* we _can_ be in RCU mode here */
Al Viroe97cdc82013-01-24 18:16:00 -05002920 error = lookup_fast(nd, path, &inode);
Miklos Szeredi71574862012-06-05 15:10:14 +02002921 if (likely(!error))
2922 goto finish_lookup;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02002923
Miklos Szeredi71574862012-06-05 15:10:14 +02002924 if (error < 0)
Al Viro2675a4e2012-06-22 12:41:10 +04002925 goto out;
Miklos Szeredi37d7fff2012-06-05 15:10:12 +02002926
Miklos Szeredi71574862012-06-05 15:10:14 +02002927 BUG_ON(nd->inode != dir->d_inode);
Miklos Szeredib6183df2012-06-05 15:10:13 +02002928 } else {
2929 /* create side of things */
2930 /*
2931 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
2932 * has been cleared when we got to the last component we are
2933 * about to look up
2934 */
2935 error = complete_walk(nd);
2936 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04002937 return error;
Miklos Szeredib6183df2012-06-05 15:10:13 +02002938
Jeff Layton33e22082013-04-12 15:16:32 -04002939 audit_inode(name, dir, LOOKUP_PARENT);
Miklos Szeredib6183df2012-06-05 15:10:13 +02002940 error = -EISDIR;
2941 /* trailing slashes? */
2942 if (nd->last.name[nd->last.len])
Al Viro2675a4e2012-06-22 12:41:10 +04002943 goto out;
Al Virofe2d35f2011-03-05 22:58:25 -05002944 }
2945
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02002946retry_lookup:
Al Viro64894cf2012-07-31 00:53:35 +04002947 if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
2948 error = mnt_want_write(nd->path.mnt);
2949 if (!error)
2950 got_write = true;
2951 /*
2952 * do _not_ fail yet - we might not need that or fail with
2953 * a different error; let lookup_open() decide; we'll be
2954 * dropping this one anyway.
2955 */
2956 }
Al Viroa1e28032009-12-24 02:12:06 -05002957 mutex_lock(&dir->d_inode->i_mutex);
Al Viro64894cf2012-07-31 00:53:35 +04002958 error = lookup_open(nd, path, file, op, got_write, opened);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002959 mutex_unlock(&dir->d_inode->i_mutex);
Al Viroa1e28032009-12-24 02:12:06 -05002960
Al Viro2675a4e2012-06-22 12:41:10 +04002961 if (error <= 0) {
2962 if (error)
Miklos Szeredid18e9002012-06-05 15:10:17 +02002963 goto out;
2964
Al Viro47237682012-06-10 05:01:45 -04002965 if ((*opened & FILE_CREATED) ||
Al Viro496ad9a2013-01-23 17:07:38 -05002966 !S_ISREG(file_inode(file)->i_mode))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002967 will_truncate = false;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002968
Jeff Laytonadb5c242012-10-10 16:43:13 -04002969 audit_inode(name, file->f_path.dentry, 0);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002970 goto opened;
2971 }
Al Virofb1cc552009-12-24 01:58:28 -05002972
Al Viro47237682012-06-10 05:01:45 -04002973 if (*opened & FILE_CREATED) {
Al Viro9b44f1b2011-03-09 00:17:27 -05002974 /* Don't check for write permission, don't truncate */
Al Viroca344a892011-03-09 00:36:45 -05002975 open_flag &= ~O_TRUNC;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002976 will_truncate = false;
Al Virobcda7652011-03-13 16:42:14 -04002977 acc_mode = MAY_OPEN;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002978 path_to_nameidata(path, nd);
Miklos Szeredie83db162012-06-05 15:10:29 +02002979 goto finish_open_created;
Al Virofb1cc552009-12-24 01:58:28 -05002980 }
2981
2982 /*
Jeff Layton3134f372012-07-25 10:19:47 -04002983 * create/update audit record if it already exists.
Al Virofb1cc552009-12-24 01:58:28 -05002984 */
Jeff Layton3134f372012-07-25 10:19:47 -04002985 if (path->dentry->d_inode)
Jeff Laytonadb5c242012-10-10 16:43:13 -04002986 audit_inode(name, path->dentry, 0);
Al Virofb1cc552009-12-24 01:58:28 -05002987
Miklos Szeredid18e9002012-06-05 15:10:17 +02002988 /*
2989 * If atomic_open() acquired write access it is dropped now due to
2990 * possible mount and symlink following (this might be optimized away if
2991 * necessary...)
2992 */
Al Viro64894cf2012-07-31 00:53:35 +04002993 if (got_write) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002994 mnt_drop_write(nd->path.mnt);
Al Viro64894cf2012-07-31 00:53:35 +04002995 got_write = false;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002996 }
2997
Al Virofb1cc552009-12-24 01:58:28 -05002998 error = -EEXIST;
Al Virof8310c52012-07-30 11:50:30 +04002999 if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))
Al Virofb1cc552009-12-24 01:58:28 -05003000 goto exit_dput;
3001
David Howells9875cf82011-01-14 18:45:21 +00003002 error = follow_managed(path, nd->flags);
3003 if (error < 0)
3004 goto exit_dput;
Al Virofb1cc552009-12-24 01:58:28 -05003005
Al Viroa3fbbde2011-11-07 21:21:26 +00003006 if (error)
3007 nd->flags |= LOOKUP_JUMPED;
3008
Miklos Szeredidecf3402012-05-21 17:30:08 +02003009 BUG_ON(nd->flags & LOOKUP_RCU);
3010 inode = path->dentry->d_inode;
Miklos Szeredi5f5daac2012-05-21 17:30:14 +02003011finish_lookup:
3012 /* we _can_ be in RCU mode here */
Al Virofb1cc552009-12-24 01:58:28 -05003013 error = -ENOENT;
Miklos Szeredi54c33e72012-05-21 17:30:10 +02003014 if (!inode) {
3015 path_to_nameidata(path, nd);
Al Viro2675a4e2012-06-22 12:41:10 +04003016 goto out;
Miklos Szeredi54c33e72012-05-21 17:30:10 +02003017 }
Al Viro9e67f362009-12-26 07:04:50 -05003018
Miklos Szeredid45ea862012-05-21 17:30:09 +02003019 if (should_follow_link(inode, !symlink_ok)) {
3020 if (nd->flags & LOOKUP_RCU) {
3021 if (unlikely(unlazy_walk(nd, path->dentry))) {
3022 error = -ECHILD;
Al Viro2675a4e2012-06-22 12:41:10 +04003023 goto out;
Miklos Szeredid45ea862012-05-21 17:30:09 +02003024 }
3025 }
3026 BUG_ON(inode != path->dentry->d_inode);
Al Viro2675a4e2012-06-22 12:41:10 +04003027 return 1;
Miklos Szeredid45ea862012-05-21 17:30:09 +02003028 }
Al Virofb1cc552009-12-24 01:58:28 -05003029
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003030 if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) {
3031 path_to_nameidata(path, nd);
3032 } else {
3033 save_parent.dentry = nd->path.dentry;
3034 save_parent.mnt = mntget(path->mnt);
3035 nd->path.dentry = path->dentry;
3036
3037 }
Miklos Szeredidecf3402012-05-21 17:30:08 +02003038 nd->inode = inode;
Al Viroa3fbbde2011-11-07 21:21:26 +00003039 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
Al Virobc77daa2013-06-06 09:12:33 -04003040finish_open:
Al Viroa3fbbde2011-11-07 21:21:26 +00003041 error = complete_walk(nd);
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003042 if (error) {
3043 path_put(&save_parent);
Al Viro2675a4e2012-06-22 12:41:10 +04003044 return error;
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003045 }
Al Virobc77daa2013-06-06 09:12:33 -04003046 audit_inode(name, nd->path.dentry, 0);
Al Virofb1cc552009-12-24 01:58:28 -05003047 error = -EISDIR;
Miklos Szeredi050ac842012-05-21 17:30:12 +02003048 if ((open_flag & O_CREAT) && S_ISDIR(nd->inode->i_mode))
Al Viro2675a4e2012-06-22 12:41:10 +04003049 goto out;
Miklos Szerediaf2f5542012-05-21 17:30:11 +02003050 error = -ENOTDIR;
Al Viro05252902013-06-06 19:33:47 -04003051 if ((nd->flags & LOOKUP_DIRECTORY) && !can_lookup(nd->inode))
Al Viro2675a4e2012-06-22 12:41:10 +04003052 goto out;
Al Viro6c0d46c2011-03-09 00:59:59 -05003053 if (!S_ISREG(nd->inode->i_mode))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003054 will_truncate = false;
Al Viro6c0d46c2011-03-09 00:59:59 -05003055
Al Viro0f9d1a12011-03-09 00:13:14 -05003056 if (will_truncate) {
3057 error = mnt_want_write(nd->path.mnt);
3058 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003059 goto out;
Al Viro64894cf2012-07-31 00:53:35 +04003060 got_write = true;
Al Viro0f9d1a12011-03-09 00:13:14 -05003061 }
Miklos Szeredie83db162012-06-05 15:10:29 +02003062finish_open_created:
Al Virobcda7652011-03-13 16:42:14 -04003063 error = may_open(&nd->path, acc_mode, open_flag);
Al Viroca344a892011-03-09 00:36:45 -05003064 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003065 goto out;
Al Viro30d90492012-06-22 12:40:19 +04003066 file->f_path.mnt = nd->path.mnt;
3067 error = finish_open(file, nd->path.dentry, NULL, opened);
3068 if (error) {
Al Viro30d90492012-06-22 12:40:19 +04003069 if (error == -EOPENSTALE)
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003070 goto stale_open;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003071 goto out;
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003072 }
Miklos Szeredia8277b92012-06-05 15:10:32 +02003073opened:
Al Viro2675a4e2012-06-22 12:41:10 +04003074 error = open_check_o_direct(file);
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003075 if (error)
3076 goto exit_fput;
Al Viro2675a4e2012-06-22 12:41:10 +04003077 error = ima_file_check(file, op->acc_mode);
Miklos Szerediaa4caad2012-06-05 15:10:28 +02003078 if (error)
3079 goto exit_fput;
3080
3081 if (will_truncate) {
Al Viro2675a4e2012-06-22 12:41:10 +04003082 error = handle_truncate(file);
Miklos Szerediaa4caad2012-06-05 15:10:28 +02003083 if (error)
3084 goto exit_fput;
Al Viro0f9d1a12011-03-09 00:13:14 -05003085 }
Al Viroca344a892011-03-09 00:36:45 -05003086out:
Al Viro64894cf2012-07-31 00:53:35 +04003087 if (got_write)
Al Viro0f9d1a12011-03-09 00:13:14 -05003088 mnt_drop_write(nd->path.mnt);
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02003089 path_put(&save_parent);
Miklos Szeredie276ae62012-05-21 17:30:06 +02003090 terminate_walk(nd);
Al Viro2675a4e2012-06-22 12:41:10 +04003091 return error;
Al Virofb1cc552009-12-24 01:58:28 -05003092
Al Virofb1cc552009-12-24 01:58:28 -05003093exit_dput:
3094 path_put_conditional(path, nd);
Al Viroca344a892011-03-09 00:36:45 -05003095 goto out;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003096exit_fput:
Al Viro2675a4e2012-06-22 12:41:10 +04003097 fput(file);
3098 goto out;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003099
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003100stale_open:
3101 /* If no saved parent or already retried then can't retry */
3102 if (!save_parent.dentry || retried)
3103 goto out;
3104
3105 BUG_ON(save_parent.dentry != dir);
3106 path_put(&nd->path);
3107 nd->path = save_parent;
3108 nd->inode = dir->d_inode;
3109 save_parent.mnt = NULL;
3110 save_parent.dentry = NULL;
Al Viro64894cf2012-07-31 00:53:35 +04003111 if (got_write) {
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003112 mnt_drop_write(nd->path.mnt);
Al Viro64894cf2012-07-31 00:53:35 +04003113 got_write = false;
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02003114 }
3115 retried = true;
3116 goto retry_lookup;
Al Virofb1cc552009-12-24 01:58:28 -05003117}
3118
Al Viro60545d02013-06-07 01:20:27 -04003119static int do_tmpfile(int dfd, struct filename *pathname,
3120 struct nameidata *nd, int flags,
3121 const struct open_flags *op,
3122 struct file *file, int *opened)
3123{
3124 static const struct qstr name = QSTR_INIT("/", 1);
3125 struct dentry *dentry, *child;
3126 struct inode *dir;
3127 int error = path_lookupat(dfd, pathname->name,
3128 flags | LOOKUP_DIRECTORY, nd);
3129 if (unlikely(error))
3130 return error;
3131 error = mnt_want_write(nd->path.mnt);
3132 if (unlikely(error))
3133 goto out;
3134 /* we want directory to be writable */
3135 error = inode_permission(nd->inode, MAY_WRITE | MAY_EXEC);
3136 if (error)
3137 goto out2;
3138 dentry = nd->path.dentry;
3139 dir = dentry->d_inode;
3140 if (!dir->i_op->tmpfile) {
3141 error = -EOPNOTSUPP;
3142 goto out2;
3143 }
3144 child = d_alloc(dentry, &name);
3145 if (unlikely(!child)) {
3146 error = -ENOMEM;
3147 goto out2;
3148 }
3149 nd->flags &= ~LOOKUP_DIRECTORY;
3150 nd->flags |= op->intent;
3151 dput(nd->path.dentry);
3152 nd->path.dentry = child;
3153 error = dir->i_op->tmpfile(dir, nd->path.dentry, op->mode);
3154 if (error)
3155 goto out2;
3156 audit_inode(pathname, nd->path.dentry, 0);
3157 error = may_open(&nd->path, op->acc_mode, op->open_flag);
3158 if (error)
3159 goto out2;
3160 file->f_path.mnt = nd->path.mnt;
3161 error = finish_open(file, nd->path.dentry, NULL, opened);
3162 if (error)
3163 goto out2;
3164 error = open_check_o_direct(file);
Al Virof4e0c302013-06-11 08:34:36 +04003165 if (error) {
Al Viro60545d02013-06-07 01:20:27 -04003166 fput(file);
Al Virof4e0c302013-06-11 08:34:36 +04003167 } else if (!(op->open_flag & O_EXCL)) {
3168 struct inode *inode = file_inode(file);
3169 spin_lock(&inode->i_lock);
3170 inode->i_state |= I_LINKABLE;
3171 spin_unlock(&inode->i_lock);
3172 }
Al Viro60545d02013-06-07 01:20:27 -04003173out2:
3174 mnt_drop_write(nd->path.mnt);
3175out:
3176 path_put(&nd->path);
3177 return error;
3178}
3179
Jeff Layton669abf42012-10-10 16:43:10 -04003180static struct file *path_openat(int dfd, struct filename *pathname,
Al Viro73d049a2011-03-11 12:08:24 -05003181 struct nameidata *nd, const struct open_flags *op, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182{
Al Virofe2d35f2011-03-05 22:58:25 -05003183 struct file *base = NULL;
Al Viro30d90492012-06-22 12:40:19 +04003184 struct file *file;
Al Viro9850c052010-01-13 15:01:15 -05003185 struct path path;
Al Viro47237682012-06-10 05:01:45 -04003186 int opened = 0;
Al Viro13aab422011-02-23 17:54:08 -05003187 int error;
Nick Piggin31e6b012011-01-07 17:49:52 +11003188
Al Viro30d90492012-06-22 12:40:19 +04003189 file = get_empty_filp();
Al Viro1afc99b2013-02-14 20:41:04 -05003190 if (IS_ERR(file))
3191 return file;
Nick Piggin31e6b012011-01-07 17:49:52 +11003192
Al Viro30d90492012-06-22 12:40:19 +04003193 file->f_flags = op->open_flag;
Nick Piggin31e6b012011-01-07 17:49:52 +11003194
Al Virobb458c62013-07-13 13:26:37 +04003195 if (unlikely(file->f_flags & __O_TMPFILE)) {
Al Viro60545d02013-06-07 01:20:27 -04003196 error = do_tmpfile(dfd, pathname, nd, flags, op, file, &opened);
3197 goto out;
3198 }
3199
Jeff Layton669abf42012-10-10 16:43:10 -04003200 error = path_init(dfd, pathname->name, flags | LOOKUP_PARENT, nd, &base);
Nick Piggin31e6b012011-01-07 17:49:52 +11003201 if (unlikely(error))
Al Viro2675a4e2012-06-22 12:41:10 +04003202 goto out;
Nick Piggin31e6b012011-01-07 17:49:52 +11003203
Al Virofe2d35f2011-03-05 22:58:25 -05003204 current->total_link_count = 0;
Jeff Layton669abf42012-10-10 16:43:10 -04003205 error = link_path_walk(pathname->name, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11003206 if (unlikely(error))
Al Viro2675a4e2012-06-22 12:41:10 +04003207 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208
Al Viro2675a4e2012-06-22 12:41:10 +04003209 error = do_last(nd, &path, file, op, &opened, pathname);
3210 while (unlikely(error > 0)) { /* trailing symlink */
Nick Piggin7b9337a2011-01-14 08:42:43 +00003211 struct path link = path;
Al Virodef4af32009-12-26 08:37:05 -05003212 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04003213 if (!(nd->flags & LOOKUP_FOLLOW)) {
Al Viro73d049a2011-03-11 12:08:24 -05003214 path_put_conditional(&path, nd);
3215 path_put(&nd->path);
Al Viro2675a4e2012-06-22 12:41:10 +04003216 error = -ELOOP;
Al Viro40b39132011-03-09 16:22:18 -05003217 break;
3218 }
Kees Cook800179c2012-07-25 17:29:07 -07003219 error = may_follow_link(&link, nd);
3220 if (unlikely(error))
3221 break;
Al Viro73d049a2011-03-11 12:08:24 -05003222 nd->flags |= LOOKUP_PARENT;
3223 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
Al Viro574197e2011-03-14 22:20:34 -04003224 error = follow_link(&link, nd, &cookie);
Al Viroc3e380b2011-02-23 13:39:45 -05003225 if (unlikely(error))
Al Viro2675a4e2012-06-22 12:41:10 +04003226 break;
3227 error = do_last(nd, &path, file, op, &opened, pathname);
Al Viro574197e2011-03-14 22:20:34 -04003228 put_link(nd, &link, cookie);
Al Viro806b6812009-12-26 07:16:40 -05003229 }
Al Viro10fa8e62009-12-26 07:09:49 -05003230out:
Al Viro73d049a2011-03-11 12:08:24 -05003231 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
3232 path_put(&nd->root);
Al Virofe2d35f2011-03-05 22:58:25 -05003233 if (base)
3234 fput(base);
Al Viro2675a4e2012-06-22 12:41:10 +04003235 if (!(opened & FILE_OPENED)) {
3236 BUG_ON(!error);
Al Viro30d90492012-06-22 12:40:19 +04003237 put_filp(file);
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003238 }
Al Viro2675a4e2012-06-22 12:41:10 +04003239 if (unlikely(error)) {
3240 if (error == -EOPENSTALE) {
3241 if (flags & LOOKUP_RCU)
3242 error = -ECHILD;
3243 else
3244 error = -ESTALE;
3245 }
3246 file = ERR_PTR(error);
3247 }
3248 return file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249}
3250
Jeff Layton669abf42012-10-10 16:43:10 -04003251struct file *do_filp_open(int dfd, struct filename *pathname,
Al Virof9652e12013-06-11 08:23:01 +04003252 const struct open_flags *op)
Al Viro13aab422011-02-23 17:54:08 -05003253{
Al Viro73d049a2011-03-11 12:08:24 -05003254 struct nameidata nd;
Al Virof9652e12013-06-11 08:23:01 +04003255 int flags = op->lookup_flags;
Al Viro13aab422011-02-23 17:54:08 -05003256 struct file *filp;
3257
Al Viro73d049a2011-03-11 12:08:24 -05003258 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
Al Viro13aab422011-02-23 17:54:08 -05003259 if (unlikely(filp == ERR_PTR(-ECHILD)))
Al Viro73d049a2011-03-11 12:08:24 -05003260 filp = path_openat(dfd, pathname, &nd, op, flags);
Al Viro13aab422011-02-23 17:54:08 -05003261 if (unlikely(filp == ERR_PTR(-ESTALE)))
Al Viro73d049a2011-03-11 12:08:24 -05003262 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
Al Viro13aab422011-02-23 17:54:08 -05003263 return filp;
3264}
3265
Al Viro73d049a2011-03-11 12:08:24 -05003266struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
Al Virof9652e12013-06-11 08:23:01 +04003267 const char *name, const struct open_flags *op)
Al Viro73d049a2011-03-11 12:08:24 -05003268{
3269 struct nameidata nd;
3270 struct file *file;
Jeff Layton669abf42012-10-10 16:43:10 -04003271 struct filename filename = { .name = name };
Al Virof9652e12013-06-11 08:23:01 +04003272 int flags = op->lookup_flags | LOOKUP_ROOT;
Al Viro73d049a2011-03-11 12:08:24 -05003273
3274 nd.root.mnt = mnt;
3275 nd.root.dentry = dentry;
3276
Al Virobcda7652011-03-13 16:42:14 -04003277 if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
Al Viro73d049a2011-03-11 12:08:24 -05003278 return ERR_PTR(-ELOOP);
3279
Jeff Layton669abf42012-10-10 16:43:10 -04003280 file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_RCU);
Al Viro73d049a2011-03-11 12:08:24 -05003281 if (unlikely(file == ERR_PTR(-ECHILD)))
Jeff Layton669abf42012-10-10 16:43:10 -04003282 file = path_openat(-1, &filename, &nd, op, flags);
Al Viro73d049a2011-03-11 12:08:24 -05003283 if (unlikely(file == ERR_PTR(-ESTALE)))
Jeff Layton669abf42012-10-10 16:43:10 -04003284 file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_REVAL);
Al Viro73d049a2011-03-11 12:08:24 -05003285 return file;
3286}
3287
Jeff Layton1ac12b42012-12-11 12:10:06 -05003288struct dentry *kern_path_create(int dfd, const char *pathname,
3289 struct path *path, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003291 struct dentry *dentry = ERR_PTR(-EEXIST);
Al Viroed75e952011-06-27 16:53:43 -04003292 struct nameidata nd;
Jan Karac30dabf2012-06-12 16:20:30 +02003293 int err2;
Jeff Layton1ac12b42012-12-11 12:10:06 -05003294 int error;
3295 bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
3296
3297 /*
3298 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
3299 * other flags passed in are ignored!
3300 */
3301 lookup_flags &= LOOKUP_REVAL;
3302
3303 error = do_path_lookup(dfd, pathname, LOOKUP_PARENT|lookup_flags, &nd);
Al Viroed75e952011-06-27 16:53:43 -04003304 if (error)
3305 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003307 /*
3308 * Yucky last component or no last component at all?
3309 * (foo/., foo/.., /////)
3310 */
Al Viroed75e952011-06-27 16:53:43 -04003311 if (nd.last_type != LAST_NORM)
3312 goto out;
3313 nd.flags &= ~LOOKUP_PARENT;
3314 nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003315
Jan Karac30dabf2012-06-12 16:20:30 +02003316 /* don't fail immediately if it's r/o, at least try to report other errors */
3317 err2 = mnt_want_write(nd.path.mnt);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003318 /*
3319 * Do the final lookup.
3320 */
Al Viroed75e952011-06-27 16:53:43 -04003321 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3322 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 if (IS_ERR(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003324 goto unlock;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003325
Al Viroa8104a92012-07-20 02:25:00 +04003326 error = -EEXIST;
Al Viroe9baf6e2008-05-15 04:49:12 -04003327 if (dentry->d_inode)
Al Viroa8104a92012-07-20 02:25:00 +04003328 goto fail;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003329 /*
3330 * Special case - lookup gave negative, but... we had foo/bar/
3331 * From the vfs_mknod() POV we just have a negative dentry -
3332 * all is fine. Let's be bastards - you had / on the end, you've
3333 * been asking for (non-existent) directory. -ENOENT for you.
3334 */
Al Viroed75e952011-06-27 16:53:43 -04003335 if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
Al Viroa8104a92012-07-20 02:25:00 +04003336 error = -ENOENT;
Al Viroed75e952011-06-27 16:53:43 -04003337 goto fail;
Al Viroe9baf6e2008-05-15 04:49:12 -04003338 }
Jan Karac30dabf2012-06-12 16:20:30 +02003339 if (unlikely(err2)) {
3340 error = err2;
Al Viroa8104a92012-07-20 02:25:00 +04003341 goto fail;
Jan Karac30dabf2012-06-12 16:20:30 +02003342 }
Al Viroed75e952011-06-27 16:53:43 -04003343 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345fail:
Al Viroa8104a92012-07-20 02:25:00 +04003346 dput(dentry);
3347 dentry = ERR_PTR(error);
3348unlock:
Al Viroed75e952011-06-27 16:53:43 -04003349 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Karac30dabf2012-06-12 16:20:30 +02003350 if (!err2)
3351 mnt_drop_write(nd.path.mnt);
Al Viroed75e952011-06-27 16:53:43 -04003352out:
3353 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 return dentry;
3355}
Al Virodae6ad82011-06-26 11:50:15 -04003356EXPORT_SYMBOL(kern_path_create);
3357
Al Viro921a1652012-07-20 01:15:31 +04003358void done_path_create(struct path *path, struct dentry *dentry)
3359{
3360 dput(dentry);
3361 mutex_unlock(&path->dentry->d_inode->i_mutex);
Al Viroa8104a92012-07-20 02:25:00 +04003362 mnt_drop_write(path->mnt);
Al Viro921a1652012-07-20 01:15:31 +04003363 path_put(path);
3364}
3365EXPORT_SYMBOL(done_path_create);
3366
Jeff Layton1ac12b42012-12-11 12:10:06 -05003367struct dentry *user_path_create(int dfd, const char __user *pathname,
3368 struct path *path, unsigned int lookup_flags)
Al Virodae6ad82011-06-26 11:50:15 -04003369{
Jeff Layton91a27b22012-10-10 15:25:28 -04003370 struct filename *tmp = getname(pathname);
Al Virodae6ad82011-06-26 11:50:15 -04003371 struct dentry *res;
3372 if (IS_ERR(tmp))
3373 return ERR_CAST(tmp);
Jeff Layton1ac12b42012-12-11 12:10:06 -05003374 res = kern_path_create(dfd, tmp->name, path, lookup_flags);
Al Virodae6ad82011-06-26 11:50:15 -04003375 putname(tmp);
3376 return res;
3377}
3378EXPORT_SYMBOL(user_path_create);
3379
Al Viro1a67aaf2011-07-26 01:52:52 -04003380int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003382 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383
3384 if (error)
3385 return error;
3386
Eric W. Biederman975d6b32011-11-13 12:16:43 -08003387 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 return -EPERM;
3389
Al Viroacfa4382008-12-04 10:06:33 -05003390 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 return -EPERM;
3392
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07003393 error = devcgroup_inode_mknod(mode, dev);
3394 if (error)
3395 return error;
3396
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 error = security_inode_mknod(dir, dentry, mode, dev);
3398 if (error)
3399 return error;
3400
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003402 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003403 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 return error;
3405}
3406
Al Virof69aac02011-07-26 04:31:40 -04003407static int may_mknod(umode_t mode)
Dave Hansen463c3192008-02-15 14:37:57 -08003408{
3409 switch (mode & S_IFMT) {
3410 case S_IFREG:
3411 case S_IFCHR:
3412 case S_IFBLK:
3413 case S_IFIFO:
3414 case S_IFSOCK:
3415 case 0: /* zero mode translates to S_IFREG */
3416 return 0;
3417 case S_IFDIR:
3418 return -EPERM;
3419 default:
3420 return -EINVAL;
3421 }
3422}
3423
Al Viro8208a222011-07-25 17:32:17 -04003424SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003425 unsigned, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426{
Al Viro2ad94ae2008-07-21 09:32:51 -04003427 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003428 struct path path;
3429 int error;
Jeff Layton972567f2012-12-20 16:00:10 -05003430 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431
Al Viro8e4bfca2012-07-20 01:17:26 +04003432 error = may_mknod(mode);
3433 if (error)
3434 return error;
Jeff Layton972567f2012-12-20 16:00:10 -05003435retry:
3436 dentry = user_path_create(dfd, filename, &path, lookup_flags);
Al Virodae6ad82011-06-26 11:50:15 -04003437 if (IS_ERR(dentry))
3438 return PTR_ERR(dentry);
Al Viro2ad94ae2008-07-21 09:32:51 -04003439
Al Virodae6ad82011-06-26 11:50:15 -04003440 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003441 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003442 error = security_path_mknod(&path, dentry, mode, dev);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003443 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04003444 goto out;
Dave Hansen463c3192008-02-15 14:37:57 -08003445 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 case 0: case S_IFREG:
Al Viro312b63f2012-06-10 18:09:36 -04003447 error = vfs_create(path.dentry->d_inode,dentry,mode,true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 break;
3449 case S_IFCHR: case S_IFBLK:
Al Virodae6ad82011-06-26 11:50:15 -04003450 error = vfs_mknod(path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 new_decode_dev(dev));
3452 break;
3453 case S_IFIFO: case S_IFSOCK:
Al Virodae6ad82011-06-26 11:50:15 -04003454 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 }
Al Viroa8104a92012-07-20 02:25:00 +04003457out:
Al Viro921a1652012-07-20 01:15:31 +04003458 done_path_create(&path, dentry);
Jeff Layton972567f2012-12-20 16:00:10 -05003459 if (retry_estale(error, lookup_flags)) {
3460 lookup_flags |= LOOKUP_REVAL;
3461 goto retry;
3462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 return error;
3464}
3465
Al Viro8208a222011-07-25 17:32:17 -04003466SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003467{
3468 return sys_mknodat(AT_FDCWD, filename, mode, dev);
3469}
3470
Al Viro18bb1db2011-07-26 01:41:39 -04003471int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003473 int error = may_create(dir, dentry);
Al Viro8de52772012-02-06 12:45:27 -05003474 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475
3476 if (error)
3477 return error;
3478
Al Viroacfa4382008-12-04 10:06:33 -05003479 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 return -EPERM;
3481
3482 mode &= (S_IRWXUGO|S_ISVTX);
3483 error = security_inode_mkdir(dir, dentry, mode);
3484 if (error)
3485 return error;
3486
Al Viro8de52772012-02-06 12:45:27 -05003487 if (max_links && dir->i_nlink >= max_links)
3488 return -EMLINK;
3489
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003491 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003492 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 return error;
3494}
3495
Al Viroa218d0f2011-11-21 14:59:34 -05003496SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497{
Dave Hansen6902d922006-09-30 23:29:01 -07003498 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003499 struct path path;
3500 int error;
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003501 unsigned int lookup_flags = LOOKUP_DIRECTORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003503retry:
3504 dentry = user_path_create(dfd, pathname, &path, lookup_flags);
Dave Hansen6902d922006-09-30 23:29:01 -07003505 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003506 return PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003507
Al Virodae6ad82011-06-26 11:50:15 -04003508 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003509 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003510 error = security_path_mkdir(&path, dentry, mode);
Al Viroa8104a92012-07-20 02:25:00 +04003511 if (!error)
3512 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
Al Viro921a1652012-07-20 01:15:31 +04003513 done_path_create(&path, dentry);
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003514 if (retry_estale(error, lookup_flags)) {
3515 lookup_flags |= LOOKUP_REVAL;
3516 goto retry;
3517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 return error;
3519}
3520
Al Viroa218d0f2011-11-21 14:59:34 -05003521SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003522{
3523 return sys_mkdirat(AT_FDCWD, pathname, mode);
3524}
3525
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526/*
Sage Weila71905f2011-05-24 13:06:08 -07003527 * The dentry_unhash() helper will try to drop the dentry early: we
J. Bruce Fieldsc0d02592012-02-15 11:48:40 -05003528 * should have a usage count of 1 if we're the only user of this
Sage Weila71905f2011-05-24 13:06:08 -07003529 * dentry, and if that is true (possibly after pruning the dcache),
3530 * then we drop the dentry now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 *
3532 * A low-level filesystem can, if it choses, legally
3533 * do a
3534 *
3535 * if (!d_unhashed(dentry))
3536 * return -EBUSY;
3537 *
3538 * if it cannot handle the case of removing a directory
3539 * that is still in use by something else..
3540 */
3541void dentry_unhash(struct dentry *dentry)
3542{
Vasily Averindc168422006-12-06 20:37:07 -08003543 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 spin_lock(&dentry->d_lock);
Waiman Long98474232013-08-28 18:24:59 -07003545 if (dentry->d_lockref.count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 __d_drop(dentry);
3547 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548}
3549
3550int vfs_rmdir(struct inode *dir, struct dentry *dentry)
3551{
3552 int error = may_delete(dir, dentry, 1);
3553
3554 if (error)
3555 return error;
3556
Al Viroacfa4382008-12-04 10:06:33 -05003557 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 return -EPERM;
3559
Al Viro1d2ef592011-09-14 18:55:41 +01003560 dget(dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003561 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562
Sage Weil912dbc12011-05-24 13:06:11 -07003563 error = -EBUSY;
3564 if (d_mountpoint(dentry))
3565 goto out;
3566
3567 error = security_inode_rmdir(dir, dentry);
3568 if (error)
3569 goto out;
3570
Sage Weil3cebde22011-05-29 21:20:59 -07003571 shrink_dcache_parent(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003572 error = dir->i_op->rmdir(dir, dentry);
3573 if (error)
3574 goto out;
3575
3576 dentry->d_inode->i_flags |= S_DEAD;
3577 dont_mount(dentry);
3578
3579out:
3580 mutex_unlock(&dentry->d_inode->i_mutex);
Al Viro1d2ef592011-09-14 18:55:41 +01003581 dput(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003582 if (!error)
3583 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 return error;
3585}
3586
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003587static long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588{
3589 int error = 0;
Jeff Layton91a27b22012-10-10 15:25:28 -04003590 struct filename *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 struct dentry *dentry;
3592 struct nameidata nd;
Jeff Laytonc6ee9202012-12-20 16:28:33 -05003593 unsigned int lookup_flags = 0;
3594retry:
3595 name = user_path_parent(dfd, pathname, &nd, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04003596 if (IS_ERR(name))
3597 return PTR_ERR(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598
3599 switch(nd.last_type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003600 case LAST_DOTDOT:
3601 error = -ENOTEMPTY;
3602 goto exit1;
3603 case LAST_DOT:
3604 error = -EINVAL;
3605 goto exit1;
3606 case LAST_ROOT:
3607 error = -EBUSY;
3608 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003610
3611 nd.flags &= ~LOOKUP_PARENT;
Jan Karac30dabf2012-06-12 16:20:30 +02003612 error = mnt_want_write(nd.path.mnt);
3613 if (error)
3614 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003615
Jan Blunck4ac91372008-02-14 19:34:32 -08003616 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08003617 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003619 if (IS_ERR(dentry))
3620 goto exit2;
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04003621 if (!dentry->d_inode) {
3622 error = -ENOENT;
3623 goto exit3;
3624 }
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003625 error = security_path_rmdir(&nd.path, dentry);
3626 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02003627 goto exit3;
Jan Blunck4ac91372008-02-14 19:34:32 -08003628 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
Dave Hansen06227532008-02-15 14:37:34 -08003629exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07003630 dput(dentry);
3631exit2:
Jan Blunck4ac91372008-02-14 19:34:32 -08003632 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Karac30dabf2012-06-12 16:20:30 +02003633 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003635 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 putname(name);
Jeff Laytonc6ee9202012-12-20 16:28:33 -05003637 if (retry_estale(error, lookup_flags)) {
3638 lookup_flags |= LOOKUP_REVAL;
3639 goto retry;
3640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 return error;
3642}
3643
Heiko Carstens3cdad422009-01-14 14:14:22 +01003644SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003645{
3646 return do_rmdir(AT_FDCWD, pathname);
3647}
3648
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649int vfs_unlink(struct inode *dir, struct dentry *dentry)
3650{
3651 int error = may_delete(dir, dentry, 0);
3652
3653 if (error)
3654 return error;
3655
Al Viroacfa4382008-12-04 10:06:33 -05003656 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 return -EPERM;
3658
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003659 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 if (d_mountpoint(dentry))
3661 error = -EBUSY;
3662 else {
3663 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05003664 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 error = dir->i_op->unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05003666 if (!error)
Al Virod83c49f2010-04-30 17:17:09 -04003667 dont_mount(dentry);
Al Virobec10522010-03-03 14:12:08 -05003668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003670 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671
3672 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
3673 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
Jan Karaece95912008-02-06 01:37:13 -08003674 fsnotify_link_count(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 }
Robert Love0eeca282005-07-12 17:06:03 -04003677
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 return error;
3679}
3680
3681/*
3682 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003683 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 * writeout happening, and we don't want to prevent access to the directory
3685 * while waiting on the I/O.
3686 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003687static long do_unlinkat(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688{
Al Viro2ad94ae2008-07-21 09:32:51 -04003689 int error;
Jeff Layton91a27b22012-10-10 15:25:28 -04003690 struct filename *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 struct dentry *dentry;
3692 struct nameidata nd;
3693 struct inode *inode = NULL;
Jeff Layton5d18f812012-12-20 16:38:04 -05003694 unsigned int lookup_flags = 0;
3695retry:
3696 name = user_path_parent(dfd, pathname, &nd, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04003697 if (IS_ERR(name))
3698 return PTR_ERR(name);
Al Viro2ad94ae2008-07-21 09:32:51 -04003699
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 error = -EISDIR;
3701 if (nd.last_type != LAST_NORM)
3702 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003703
3704 nd.flags &= ~LOOKUP_PARENT;
Jan Karac30dabf2012-06-12 16:20:30 +02003705 error = mnt_want_write(nd.path.mnt);
3706 if (error)
3707 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003708
Jan Blunck4ac91372008-02-14 19:34:32 -08003709 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08003710 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 error = PTR_ERR(dentry);
3712 if (!IS_ERR(dentry)) {
3713 /* Why not before? Because we want correct error value */
Török Edwin50338b82011-06-16 00:06:14 +03003714 if (nd.last.name[nd.last.len])
3715 goto slashes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 inode = dentry->d_inode;
Török Edwin50338b82011-06-16 00:06:14 +03003717 if (!inode)
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04003718 goto slashes;
3719 ihold(inode);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003720 error = security_path_unlink(&nd.path, dentry);
3721 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02003722 goto exit2;
Jan Blunck4ac91372008-02-14 19:34:32 -08003723 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
Jan Karac30dabf2012-06-12 16:20:30 +02003724exit2:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 dput(dentry);
3726 }
Jan Blunck4ac91372008-02-14 19:34:32 -08003727 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 if (inode)
3729 iput(inode); /* truncate the inode here */
Jan Karac30dabf2012-06-12 16:20:30 +02003730 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003732 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 putname(name);
Jeff Layton5d18f812012-12-20 16:38:04 -05003734 if (retry_estale(error, lookup_flags)) {
3735 lookup_flags |= LOOKUP_REVAL;
3736 inode = NULL;
3737 goto retry;
3738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 return error;
3740
3741slashes:
3742 error = !dentry->d_inode ? -ENOENT :
3743 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
3744 goto exit2;
3745}
3746
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003747SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003748{
3749 if ((flag & ~AT_REMOVEDIR) != 0)
3750 return -EINVAL;
3751
3752 if (flag & AT_REMOVEDIR)
3753 return do_rmdir(dfd, pathname);
3754
3755 return do_unlinkat(dfd, pathname);
3756}
3757
Heiko Carstens3480b252009-01-14 14:14:16 +01003758SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003759{
3760 return do_unlinkat(AT_FDCWD, pathname);
3761}
3762
Miklos Szeredidb2e7472008-06-24 16:50:16 +02003763int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003765 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766
3767 if (error)
3768 return error;
3769
Al Viroacfa4382008-12-04 10:06:33 -05003770 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 return -EPERM;
3772
3773 error = security_inode_symlink(dir, dentry, oldname);
3774 if (error)
3775 return error;
3776
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003778 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003779 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 return error;
3781}
3782
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003783SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
3784 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785{
Al Viro2ad94ae2008-07-21 09:32:51 -04003786 int error;
Jeff Layton91a27b22012-10-10 15:25:28 -04003787 struct filename *from;
Dave Hansen6902d922006-09-30 23:29:01 -07003788 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003789 struct path path;
Jeff Laytonf46d3562012-12-11 12:10:08 -05003790 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791
3792 from = getname(oldname);
Al Viro2ad94ae2008-07-21 09:32:51 -04003793 if (IS_ERR(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 return PTR_ERR(from);
Jeff Laytonf46d3562012-12-11 12:10:08 -05003795retry:
3796 dentry = user_path_create(newdfd, newname, &path, lookup_flags);
Dave Hansen6902d922006-09-30 23:29:01 -07003797 error = PTR_ERR(dentry);
3798 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003799 goto out_putname;
Dave Hansen6902d922006-09-30 23:29:01 -07003800
Jeff Layton91a27b22012-10-10 15:25:28 -04003801 error = security_path_symlink(&path, dentry, from->name);
Al Viroa8104a92012-07-20 02:25:00 +04003802 if (!error)
Jeff Layton91a27b22012-10-10 15:25:28 -04003803 error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
Al Viro921a1652012-07-20 01:15:31 +04003804 done_path_create(&path, dentry);
Jeff Laytonf46d3562012-12-11 12:10:08 -05003805 if (retry_estale(error, lookup_flags)) {
3806 lookup_flags |= LOOKUP_REVAL;
3807 goto retry;
3808 }
Dave Hansen6902d922006-09-30 23:29:01 -07003809out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 putname(from);
3811 return error;
3812}
3813
Heiko Carstens3480b252009-01-14 14:14:16 +01003814SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003815{
3816 return sys_symlinkat(oldname, AT_FDCWD, newname);
3817}
3818
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
3820{
3821 struct inode *inode = old_dentry->d_inode;
Al Viro8de52772012-02-06 12:45:27 -05003822 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 int error;
3824
3825 if (!inode)
3826 return -ENOENT;
3827
Miklos Szeredia95164d2008-07-30 15:08:48 +02003828 error = may_create(dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 if (error)
3830 return error;
3831
3832 if (dir->i_sb != inode->i_sb)
3833 return -EXDEV;
3834
3835 /*
3836 * A link to an append-only or immutable file cannot be created.
3837 */
3838 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3839 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05003840 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003842 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 return -EPERM;
3844
3845 error = security_inode_link(old_dentry, dir, new_dentry);
3846 if (error)
3847 return error;
3848
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003849 mutex_lock(&inode->i_mutex);
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05303850 /* Make sure we don't allow creating hardlink to an unlinked file */
Al Virof4e0c302013-06-11 08:34:36 +04003851 if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05303852 error = -ENOENT;
Al Viro8de52772012-02-06 12:45:27 -05003853 else if (max_links && inode->i_nlink >= max_links)
3854 error = -EMLINK;
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05303855 else
3856 error = dir->i_op->link(old_dentry, dir, new_dentry);
Al Virof4e0c302013-06-11 08:34:36 +04003857
3858 if (!error && (inode->i_state & I_LINKABLE)) {
3859 spin_lock(&inode->i_lock);
3860 inode->i_state &= ~I_LINKABLE;
3861 spin_unlock(&inode->i_lock);
3862 }
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003863 mutex_unlock(&inode->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07003864 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003865 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 return error;
3867}
3868
3869/*
3870 * Hardlinks are often used in delicate situations. We avoid
3871 * security-related surprises by not following symlinks on the
3872 * newname. --KAB
3873 *
3874 * We don't follow them on the oldname either to be compatible
3875 * with linux 2.0, and to avoid hard-linking to directories
3876 * and other special files. --ADM
3877 */
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003878SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
3879 int, newdfd, const char __user *, newname, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880{
3881 struct dentry *new_dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003882 struct path old_path, new_path;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303883 int how = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303886 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003887 return -EINVAL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303888 /*
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07003889 * To use null names we require CAP_DAC_READ_SEARCH
3890 * This ensures that not everyone will be able to create
3891 * handlink using the passed filedescriptor.
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303892 */
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07003893 if (flags & AT_EMPTY_PATH) {
3894 if (!capable(CAP_DAC_READ_SEARCH))
3895 return -ENOENT;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303896 how = LOOKUP_EMPTY;
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07003897 }
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003898
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303899 if (flags & AT_SYMLINK_FOLLOW)
3900 how |= LOOKUP_FOLLOW;
Jeff Layton442e31c2012-12-20 16:15:38 -05003901retry:
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303902 error = user_path_at(olddfd, oldname, how, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04003904 return error;
3905
Jeff Layton442e31c2012-12-20 16:15:38 -05003906 new_dentry = user_path_create(newdfd, newname, &new_path,
3907 (how & LOOKUP_REVAL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003909 if (IS_ERR(new_dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003910 goto out;
3911
3912 error = -EXDEV;
3913 if (old_path.mnt != new_path.mnt)
3914 goto out_dput;
Kees Cook800179c2012-07-25 17:29:07 -07003915 error = may_linkat(&old_path);
3916 if (unlikely(error))
3917 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04003918 error = security_path_link(old_path.dentry, &new_path, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003919 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04003920 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04003921 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
Dave Hansen75c3f292008-02-15 14:37:45 -08003922out_dput:
Al Viro921a1652012-07-20 01:15:31 +04003923 done_path_create(&new_path, new_dentry);
Jeff Layton442e31c2012-12-20 16:15:38 -05003924 if (retry_estale(error, how)) {
3925 how |= LOOKUP_REVAL;
3926 goto retry;
3927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928out:
Al Viro2d8f3032008-07-22 09:59:21 -04003929 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930
3931 return error;
3932}
3933
Heiko Carstens3480b252009-01-14 14:14:16 +01003934SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003935{
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003936 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003937}
3938
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939/*
3940 * The worst of all namespace operations - renaming directory. "Perverted"
3941 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
3942 * Problems:
3943 * a) we can get into loop creation. Check is done in is_subdir().
3944 * b) race potential - two innocent renames can create a loop together.
3945 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08003946 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 * story.
3948 * c) we have to lock _three_ objects - parents and victim (if it exists).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003949 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 * whether the target exists). Solution: try to be smart with locking
3951 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08003952 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 * move will be locked. Thus we can rank directories by the tree
3954 * (ancestors first) and rank all non-directories after them.
3955 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08003956 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 * HOWEVER, it relies on the assumption that any object with ->lookup()
3958 * has no more than 1 dentry. If "hybrid" objects will ever appear,
3959 * we'd better make sure that there's no link(2) for them.
Sage Weile4eaac02011-05-24 13:06:07 -07003960 * d) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003961 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05003963 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 * locking].
3965 */
Adrian Bunk75c96f82005-05-05 16:16:09 -07003966static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
3967 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968{
3969 int error = 0;
Sage Weil9055cba2011-05-24 13:06:12 -07003970 struct inode *target = new_dentry->d_inode;
Al Viro8de52772012-02-06 12:45:27 -05003971 unsigned max_links = new_dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972
3973 /*
3974 * If we are going to change the parent - check write permissions,
3975 * we'll need to flip '..'.
3976 */
3977 if (new_dir != old_dir) {
Al Virof419a2e2008-07-22 00:07:17 -04003978 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 if (error)
3980 return error;
3981 }
3982
3983 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3984 if (error)
3985 return error;
3986
Al Viro1d2ef592011-09-14 18:55:41 +01003987 dget(new_dentry);
Al Virod83c49f2010-04-30 17:17:09 -04003988 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003989 mutex_lock(&target->i_mutex);
Sage Weil9055cba2011-05-24 13:06:12 -07003990
3991 error = -EBUSY;
3992 if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
3993 goto out;
3994
Al Viro8de52772012-02-06 12:45:27 -05003995 error = -EMLINK;
3996 if (max_links && !target && new_dir != old_dir &&
3997 new_dir->i_nlink >= max_links)
3998 goto out;
3999
Sage Weil3cebde22011-05-29 21:20:59 -07004000 if (target)
4001 shrink_dcache_parent(new_dentry);
Sage Weil9055cba2011-05-24 13:06:12 -07004002 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
4003 if (error)
4004 goto out;
4005
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 if (target) {
Sage Weil9055cba2011-05-24 13:06:12 -07004007 target->i_flags |= S_DEAD;
4008 dont_mount(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 }
Sage Weil9055cba2011-05-24 13:06:12 -07004010out:
4011 if (target)
4012 mutex_unlock(&target->i_mutex);
Al Viro1d2ef592011-09-14 18:55:41 +01004013 dput(new_dentry);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07004014 if (!error)
Mark Fasheh349457c2006-09-08 14:22:21 -07004015 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
4016 d_move(old_dentry,new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 return error;
4018}
4019
Adrian Bunk75c96f82005-05-05 16:16:09 -07004020static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
4021 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022{
Sage Weil51892bb2011-05-24 13:06:13 -07004023 struct inode *target = new_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 int error;
4025
4026 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
4027 if (error)
4028 return error;
4029
4030 dget(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004032 mutex_lock(&target->i_mutex);
Sage Weil51892bb2011-05-24 13:06:13 -07004033
4034 error = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
Sage Weil51892bb2011-05-24 13:06:13 -07004036 goto out;
4037
4038 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
4039 if (error)
4040 goto out;
4041
4042 if (target)
4043 dont_mount(new_dentry);
4044 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
4045 d_move(old_dentry, new_dentry);
4046out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004048 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 dput(new_dentry);
4050 return error;
4051}
4052
4053int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
4054 struct inode *new_dir, struct dentry *new_dentry)
4055{
4056 int error;
4057 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
Eric Paris59b0df22010-02-08 12:53:52 -05004058 const unsigned char *old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059
4060 if (old_dentry->d_inode == new_dentry->d_inode)
4061 return 0;
4062
4063 error = may_delete(old_dir, old_dentry, is_dir);
4064 if (error)
4065 return error;
4066
4067 if (!new_dentry->d_inode)
Miklos Szeredia95164d2008-07-30 15:08:48 +02004068 error = may_create(new_dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 else
4070 error = may_delete(new_dir, new_dentry, is_dir);
4071 if (error)
4072 return error;
4073
Al Viroacfa4382008-12-04 10:06:33 -05004074 if (!old_dir->i_op->rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 return -EPERM;
4076
Robert Love0eeca282005-07-12 17:06:03 -04004077 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
4078
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 if (is_dir)
4080 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
4081 else
4082 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
Al Viro123df292009-12-25 04:57:57 -05004083 if (!error)
4084 fsnotify_move(old_dir, new_dir, old_name, is_dir,
Al Viro5a190ae2007-06-07 12:19:32 -04004085 new_dentry->d_inode, old_dentry);
Robert Love0eeca282005-07-12 17:06:03 -04004086 fsnotify_oldname_free(old_name);
4087
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 return error;
4089}
4090
Heiko Carstens2e4d0922009-01-14 14:14:31 +01004091SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4092 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093{
Al Viro2ad94ae2008-07-21 09:32:51 -04004094 struct dentry *old_dir, *new_dir;
4095 struct dentry *old_dentry, *new_dentry;
4096 struct dentry *trap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 struct nameidata oldnd, newnd;
Jeff Layton91a27b22012-10-10 15:25:28 -04004098 struct filename *from;
4099 struct filename *to;
Jeff Laytonc6a94282012-12-11 12:10:10 -05004100 unsigned int lookup_flags = 0;
4101 bool should_retry = false;
Al Viro2ad94ae2008-07-21 09:32:51 -04004102 int error;
Jeff Laytonc6a94282012-12-11 12:10:10 -05004103retry:
4104 from = user_path_parent(olddfd, oldname, &oldnd, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04004105 if (IS_ERR(from)) {
4106 error = PTR_ERR(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 goto exit;
Jeff Layton91a27b22012-10-10 15:25:28 -04004108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109
Jeff Laytonc6a94282012-12-11 12:10:10 -05004110 to = user_path_parent(newdfd, newname, &newnd, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04004111 if (IS_ERR(to)) {
4112 error = PTR_ERR(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 goto exit1;
Jeff Layton91a27b22012-10-10 15:25:28 -04004114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115
4116 error = -EXDEV;
Jan Blunck4ac91372008-02-14 19:34:32 -08004117 if (oldnd.path.mnt != newnd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 goto exit2;
4119
Jan Blunck4ac91372008-02-14 19:34:32 -08004120 old_dir = oldnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 error = -EBUSY;
4122 if (oldnd.last_type != LAST_NORM)
4123 goto exit2;
4124
Jan Blunck4ac91372008-02-14 19:34:32 -08004125 new_dir = newnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 if (newnd.last_type != LAST_NORM)
4127 goto exit2;
4128
Jan Karac30dabf2012-06-12 16:20:30 +02004129 error = mnt_want_write(oldnd.path.mnt);
4130 if (error)
4131 goto exit2;
4132
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004133 oldnd.flags &= ~LOOKUP_PARENT;
4134 newnd.flags &= ~LOOKUP_PARENT;
OGAWA Hirofumi4e9ed2f2008-10-16 07:50:29 +09004135 newnd.flags |= LOOKUP_RENAME_TARGET;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004136
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 trap = lock_rename(new_dir, old_dir);
4138
Christoph Hellwig49705b72005-11-08 21:35:06 -08004139 old_dentry = lookup_hash(&oldnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 error = PTR_ERR(old_dentry);
4141 if (IS_ERR(old_dentry))
4142 goto exit3;
4143 /* source must exist */
4144 error = -ENOENT;
4145 if (!old_dentry->d_inode)
4146 goto exit4;
4147 /* unless the source is a directory trailing slashes give -ENOTDIR */
4148 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
4149 error = -ENOTDIR;
4150 if (oldnd.last.name[oldnd.last.len])
4151 goto exit4;
4152 if (newnd.last.name[newnd.last.len])
4153 goto exit4;
4154 }
4155 /* source should not be ancestor of target */
4156 error = -EINVAL;
4157 if (old_dentry == trap)
4158 goto exit4;
Christoph Hellwig49705b72005-11-08 21:35:06 -08004159 new_dentry = lookup_hash(&newnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 error = PTR_ERR(new_dentry);
4161 if (IS_ERR(new_dentry))
4162 goto exit4;
4163 /* target should not be an ancestor of source */
4164 error = -ENOTEMPTY;
4165 if (new_dentry == trap)
4166 goto exit5;
4167
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004168 error = security_path_rename(&oldnd.path, old_dentry,
4169 &newnd.path, new_dentry);
4170 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02004171 goto exit5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 error = vfs_rename(old_dir->d_inode, old_dentry,
4173 new_dir->d_inode, new_dentry);
4174exit5:
4175 dput(new_dentry);
4176exit4:
4177 dput(old_dentry);
4178exit3:
4179 unlock_rename(new_dir, old_dir);
Jan Karac30dabf2012-06-12 16:20:30 +02004180 mnt_drop_write(oldnd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181exit2:
Jeff Laytonc6a94282012-12-11 12:10:10 -05004182 if (retry_estale(error, lookup_flags))
4183 should_retry = true;
Jan Blunck1d957f92008-02-14 19:34:35 -08004184 path_put(&newnd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04004185 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08004187 path_put(&oldnd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 putname(from);
Jeff Laytonc6a94282012-12-11 12:10:10 -05004189 if (should_retry) {
4190 should_retry = false;
4191 lookup_flags |= LOOKUP_REVAL;
4192 goto retry;
4193 }
Al Viro2ad94ae2008-07-21 09:32:51 -04004194exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 return error;
4196}
4197
Heiko Carstensa26eab22009-01-14 14:14:17 +01004198SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004199{
4200 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
4201}
4202
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
4204{
4205 int len;
4206
4207 len = PTR_ERR(link);
4208 if (IS_ERR(link))
4209 goto out;
4210
4211 len = strlen(link);
4212 if (len > (unsigned) buflen)
4213 len = buflen;
4214 if (copy_to_user(buffer, link, len))
4215 len = -EFAULT;
4216out:
4217 return len;
4218}
4219
4220/*
4221 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
4222 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
4223 * using) it for any given inode is up to filesystem.
4224 */
4225int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4226{
4227 struct nameidata nd;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004228 void *cookie;
Marcin Slusarz694a1762008-06-09 16:40:37 -07004229 int res;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004230
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 nd.depth = 0;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004232 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
Marcin Slusarz694a1762008-06-09 16:40:37 -07004233 if (IS_ERR(cookie))
4234 return PTR_ERR(cookie);
4235
4236 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
4237 if (dentry->d_inode->i_op->put_link)
4238 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
4239 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240}
4241
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242/* get the link contents into pagecache */
4243static char *page_getlink(struct dentry * dentry, struct page **ppage)
4244{
Duane Griffinebd09ab2008-12-19 20:47:12 +00004245 char *kaddr;
4246 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 struct address_space *mapping = dentry->d_inode->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -07004248 page = read_mapping_page(mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 if (IS_ERR(page))
Nick Piggin6fe69002007-05-06 14:49:04 -07004250 return (char*)page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 *ppage = page;
Duane Griffinebd09ab2008-12-19 20:47:12 +00004252 kaddr = kmap(page);
4253 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
4254 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255}
4256
4257int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4258{
4259 struct page *page = NULL;
4260 char *s = page_getlink(dentry, &page);
4261 int res = vfs_readlink(dentry,buffer,buflen,s);
4262 if (page) {
4263 kunmap(page);
4264 page_cache_release(page);
4265 }
4266 return res;
4267}
4268
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004269void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004271 struct page *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 nd_set_link(nd, page_getlink(dentry, &page));
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004273 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274}
4275
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004276void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004278 struct page *page = cookie;
4279
4280 if (page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 kunmap(page);
4282 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 }
4284}
4285
Nick Piggin54566b22009-01-04 12:00:53 -08004286/*
4287 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
4288 */
4289int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290{
4291 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004292 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07004293 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08004294 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 char *kaddr;
Nick Piggin54566b22009-01-04 12:00:53 -08004296 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
4297 if (nofs)
4298 flags |= AOP_FLAG_NOFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299
NeilBrown7e53cac2006-03-25 03:07:57 -08004300retry:
Nick Pigginafddba42007-10-16 01:25:01 -07004301 err = pagecache_write_begin(NULL, mapping, 0, len-1,
Nick Piggin54566b22009-01-04 12:00:53 -08004302 flags, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07004304 goto fail;
4305
Cong Wange8e3c3d2011-11-25 23:14:27 +08004306 kaddr = kmap_atomic(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 memcpy(kaddr, symname, len-1);
Cong Wange8e3c3d2011-11-25 23:14:27 +08004308 kunmap_atomic(kaddr);
Nick Pigginafddba42007-10-16 01:25:01 -07004309
4310 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4311 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 if (err < 0)
4313 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07004314 if (err < len-1)
4315 goto retry;
4316
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 mark_inode_dirty(inode);
4318 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319fail:
4320 return err;
4321}
4322
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004323int page_symlink(struct inode *inode, const char *symname, int len)
4324{
4325 return __page_symlink(inode, symname, len,
Nick Piggin54566b22009-01-04 12:00:53 -08004326 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004327}
4328
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08004329const struct inode_operations page_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 .readlink = generic_readlink,
4331 .follow_link = page_follow_link_light,
4332 .put_link = page_put_link,
4333};
4334
Al Viro2d8f3032008-07-22 09:59:21 -04004335EXPORT_SYMBOL(user_path_at);
David Howellscc53ce52011-01-14 18:45:26 +00004336EXPORT_SYMBOL(follow_down_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337EXPORT_SYMBOL(follow_down);
4338EXPORT_SYMBOL(follow_up);
Al Virof6d2ac52012-08-26 12:55:54 -04004339EXPORT_SYMBOL(get_write_access); /* nfsd */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341EXPORT_SYMBOL(lookup_one_len);
4342EXPORT_SYMBOL(page_follow_link_light);
4343EXPORT_SYMBOL(page_put_link);
4344EXPORT_SYMBOL(page_readlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004345EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346EXPORT_SYMBOL(page_symlink);
4347EXPORT_SYMBOL(page_symlink_inode_operations);
Al Virod1811462008-08-02 00:49:18 -04004348EXPORT_SYMBOL(kern_path);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07004349EXPORT_SYMBOL(vfs_path_lookup);
Al Virof419a2e2008-07-22 00:07:17 -04004350EXPORT_SYMBOL(inode_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351EXPORT_SYMBOL(unlock_rename);
4352EXPORT_SYMBOL(vfs_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353EXPORT_SYMBOL(vfs_link);
4354EXPORT_SYMBOL(vfs_mkdir);
4355EXPORT_SYMBOL(vfs_mknod);
4356EXPORT_SYMBOL(generic_permission);
4357EXPORT_SYMBOL(vfs_readlink);
4358EXPORT_SYMBOL(vfs_rename);
4359EXPORT_SYMBOL(vfs_rmdir);
4360EXPORT_SYMBOL(vfs_symlink);
4361EXPORT_SYMBOL(vfs_unlink);
4362EXPORT_SYMBOL(dentry_unhash);
4363EXPORT_SYMBOL(generic_readlink);