blob: 6bbd8fdfb1f55319a5d97bd023f19d3b7b3d7cf4 [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 Layton91a27b22012-10-10 15:25:28 -0400122 __putname(name->name);
123 kfree(name);
124}
125
126static struct filename *
127getname_flags(const char __user *filename, int flags, int *empty)
128{
129 struct filename *result, *err;
130 char *kname;
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700131 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Jeff Layton7ac86262012-10-10 15:25:28 -0400133 result = audit_reusename(filename);
134 if (result)
135 return result;
136
Jeff Layton91a27b22012-10-10 15:25:28 -0400137 /* FIXME: create dedicated slabcache? */
138 result = kzalloc(sizeof(*result), GFP_KERNEL);
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700139 if (unlikely(!result))
Eric Paris4043cde2012-01-03 14:23:08 -0500140 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Jeff Layton91a27b22012-10-10 15:25:28 -0400142 kname = __getname();
143 if (unlikely(!kname)) {
144 err = ERR_PTR(-ENOMEM);
145 goto error_free_name;
146 }
147
148 result->name = kname;
149 result->uptr = filename;
150 len = strncpy_from_user(kname, filename, PATH_MAX);
151 if (unlikely(len < 0)) {
152 err = ERR_PTR(len);
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700153 goto error;
Jeff Layton91a27b22012-10-10 15:25:28 -0400154 }
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700155
156 /* The empty path is special. */
157 if (unlikely(!len)) {
158 if (empty)
Eric Paris4043cde2012-01-03 14:23:08 -0500159 *empty = 1;
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700160 err = ERR_PTR(-ENOENT);
161 if (!(flags & LOOKUP_EMPTY))
162 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 }
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700164
165 err = ERR_PTR(-ENAMETOOLONG);
166 if (likely(len < PATH_MAX)) {
167 audit_getname(result);
168 return result;
169 }
170
171error:
Jeff Layton91a27b22012-10-10 15:25:28 -0400172 __putname(kname);
173error_free_name:
174 kfree(result);
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700175 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
Jeff Layton91a27b22012-10-10 15:25:28 -0400178struct filename *
179getname(const char __user * filename)
Al Virof52e0c12011-03-14 18:56:51 -0400180{
Linus Torvaldsf7493e52012-03-22 16:10:40 -0700181 return getname_flags(filename, 0, NULL);
Al Virof52e0c12011-03-14 18:56:51 -0400182}
Jeff Layton91a27b22012-10-10 15:25:28 -0400183EXPORT_SYMBOL(getname);
Al Virof52e0c12011-03-14 18:56:51 -0400184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185#ifdef CONFIG_AUDITSYSCALL
Jeff Layton91a27b22012-10-10 15:25:28 -0400186void putname(struct filename *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Al Viro5ac3a9c2006-07-16 06:38:45 -0400188 if (unlikely(!audit_dummy_context()))
Jeff Layton91a27b22012-10-10 15:25:28 -0400189 return audit_putname(name);
190 final_putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192#endif
193
Linus Torvaldse77819e2011-07-22 19:30:19 -0700194static int check_acl(struct inode *inode, int mask)
195{
Linus Torvalds84635d62011-07-25 22:47:03 -0700196#ifdef CONFIG_FS_POSIX_ACL
Linus Torvaldse77819e2011-07-22 19:30:19 -0700197 struct posix_acl *acl;
198
Linus Torvaldse77819e2011-07-22 19:30:19 -0700199 if (mask & MAY_NOT_BLOCK) {
Al Viro35678662011-08-02 21:32:13 -0400200 acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
201 if (!acl)
Linus Torvaldse77819e2011-07-22 19:30:19 -0700202 return -EAGAIN;
Al Viro35678662011-08-02 21:32:13 -0400203 /* no ->get_acl() calls in RCU mode... */
204 if (acl == ACL_NOT_CACHED)
205 return -ECHILD;
Ari Savolainen206b1d02011-08-06 19:43:07 +0300206 return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
Linus Torvaldse77819e2011-07-22 19:30:19 -0700207 }
208
209 acl = get_cached_acl(inode, ACL_TYPE_ACCESS);
210
211 /*
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200212 * A filesystem can force a ACL callback by just never filling the
213 * ACL cache. But normally you'd fill the cache either at inode
214 * instantiation time, or on the first ->get_acl call.
Linus Torvaldse77819e2011-07-22 19:30:19 -0700215 *
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200216 * If the filesystem doesn't have a get_acl() function at all, we'll
217 * just create the negative cache entry.
Linus Torvaldse77819e2011-07-22 19:30:19 -0700218 */
219 if (acl == ACL_NOT_CACHED) {
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200220 if (inode->i_op->get_acl) {
221 acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS);
222 if (IS_ERR(acl))
223 return PTR_ERR(acl);
224 } else {
225 set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
226 return -EAGAIN;
227 }
Linus Torvaldse77819e2011-07-22 19:30:19 -0700228 }
229
230 if (acl) {
231 int error = posix_acl_permission(inode, acl, mask);
232 posix_acl_release(acl);
233 return error;
234 }
Linus Torvalds84635d62011-07-25 22:47:03 -0700235#endif
Linus Torvaldse77819e2011-07-22 19:30:19 -0700236
237 return -EAGAIN;
238}
239
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700240/*
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530241 * This does the basic permission checking
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700242 */
Al Viro7e401452011-06-20 19:12:17 -0400243static int acl_permission_check(struct inode *inode, int mask)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700244{
Linus Torvalds26cf46b2011-05-13 11:51:01 -0700245 unsigned int mode = inode->i_mode;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700246
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -0800247 if (likely(uid_eq(current_fsuid(), inode->i_uid)))
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700248 mode >>= 6;
249 else {
Linus Torvaldse77819e2011-07-22 19:30:19 -0700250 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
Al Viro7e401452011-06-20 19:12:17 -0400251 int error = check_acl(inode, mask);
Nick Pigginb74c79e2011-01-07 17:49:58 +1100252 if (error != -EAGAIN)
253 return error;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700254 }
255
256 if (in_group_p(inode->i_gid))
257 mode >>= 3;
258 }
259
260 /*
261 * If the DACs are ok we don't need any capability check.
262 */
Al Viro9c2c7032011-06-20 19:06:22 -0400263 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700264 return 0;
265 return -EACCES;
266}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268/**
Nick Pigginb74c79e2011-01-07 17:49:58 +1100269 * generic_permission - check for access rights on a Posix-like filesystem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 * @inode: inode to check access rights for
Andreas Gruenbacher8fd90c82011-10-23 23:13:30 +0530271 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 *
273 * Used to check for read/write/execute permissions on a file.
274 * We use "fsuid" for this, letting us set arbitrary permissions
275 * for filesystem access without changing the "normal" uids which
Nick Pigginb74c79e2011-01-07 17:49:58 +1100276 * are used for other things.
277 *
278 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
279 * request cannot be satisfied (eg. requires blocking or too much complexity).
280 * It would then be called again in ref-walk mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 */
Al Viro2830ba72011-06-20 19:16:29 -0400282int generic_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700284 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 /*
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530287 * Do the basic permission checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 */
Al Viro7e401452011-06-20 19:12:17 -0400289 ret = acl_permission_check(inode, mask);
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700290 if (ret != -EACCES)
291 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Al Virod594e7e2011-06-20 19:55:42 -0400293 if (S_ISDIR(inode->i_mode)) {
294 /* DACs are overridable for directories */
Eric W. Biederman1a48e2a2011-11-14 16:24:06 -0800295 if (inode_capable(inode, CAP_DAC_OVERRIDE))
Al Virod594e7e2011-06-20 19:55:42 -0400296 return 0;
297 if (!(mask & MAY_WRITE))
Eric W. Biederman1a48e2a2011-11-14 16:24:06 -0800298 if (inode_capable(inode, CAP_DAC_READ_SEARCH))
Al Virod594e7e2011-06-20 19:55:42 -0400299 return 0;
300 return -EACCES;
301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 /*
303 * Read/write DACs are always overridable.
Al Virod594e7e2011-06-20 19:55:42 -0400304 * Executable DACs are overridable when there is
305 * at least one exec bit set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 */
Al Virod594e7e2011-06-20 19:55:42 -0400307 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
Eric W. Biederman1a48e2a2011-11-14 16:24:06 -0800308 if (inode_capable(inode, CAP_DAC_OVERRIDE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 return 0;
310
311 /*
312 * Searching includes executable on directories, else just read.
313 */
Serge E. Hallyn7ea66002009-12-29 14:50:19 -0600314 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
Al Virod594e7e2011-06-20 19:55:42 -0400315 if (mask == MAY_READ)
Eric W. Biederman1a48e2a2011-11-14 16:24:06 -0800316 if (inode_capable(inode, CAP_DAC_READ_SEARCH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return 0;
318
319 return -EACCES;
320}
321
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700322/*
323 * We _really_ want to just do "generic_permission()" without
324 * even looking at the inode->i_op values. So we keep a cache
325 * flag in inode->i_opflags, that says "this has not special
326 * permission function, use the fast case".
327 */
328static inline int do_inode_permission(struct inode *inode, int mask)
329{
330 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
331 if (likely(inode->i_op->permission))
332 return inode->i_op->permission(inode, mask);
333
334 /* This gets set once for the inode lifetime */
335 spin_lock(&inode->i_lock);
336 inode->i_opflags |= IOP_FASTPERM;
337 spin_unlock(&inode->i_lock);
338 }
339 return generic_permission(inode, mask);
340}
341
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200342/**
David Howells0bdaea92012-06-25 12:55:46 +0100343 * __inode_permission - Check for access rights to a given inode
344 * @inode: Inode to check permission on
345 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200346 *
David Howells0bdaea92012-06-25 12:55:46 +0100347 * Check for read/write/execute permissions on an inode.
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530348 *
349 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
David Howells0bdaea92012-06-25 12:55:46 +0100350 *
351 * This does not check for a read-only file system. You probably want
352 * inode_permission().
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200353 */
David Howells0bdaea92012-06-25 12:55:46 +0100354int __inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Al Viroe6305c42008-07-15 21:03:57 -0400356 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700358 if (unlikely(mask & MAY_WRITE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 /*
360 * Nobody gets write access to an immutable file.
361 */
362 if (IS_IMMUTABLE(inode))
363 return -EACCES;
364 }
365
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700366 retval = do_inode_permission(inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 if (retval)
368 return retval;
369
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700370 retval = devcgroup_inode_permission(inode, mask);
371 if (retval)
372 return retval;
373
Eric Parisd09ca732010-07-23 11:43:57 -0400374 return security_inode_permission(inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
Al Virof4d6ff82011-06-19 13:14:21 -0400377/**
David Howells0bdaea92012-06-25 12:55:46 +0100378 * sb_permission - Check superblock-level permissions
379 * @sb: Superblock of inode to check permission on
Randy Dunlap55852632012-08-18 17:39:25 -0700380 * @inode: Inode to check permission on
David Howells0bdaea92012-06-25 12:55:46 +0100381 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
382 *
383 * Separate out file-system wide checks from inode-specific permission checks.
384 */
385static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
386{
387 if (unlikely(mask & MAY_WRITE)) {
388 umode_t mode = inode->i_mode;
389
390 /* Nobody gets write access to a read-only fs. */
391 if ((sb->s_flags & MS_RDONLY) &&
392 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
393 return -EROFS;
394 }
395 return 0;
396}
397
398/**
399 * inode_permission - Check for access rights to a given inode
400 * @inode: Inode to check permission on
401 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
402 *
403 * Check for read/write/execute permissions on an inode. We use fs[ug]id for
404 * this, letting us set arbitrary permissions for filesystem access without
405 * changing the "normal" UIDs which are used for other things.
406 *
407 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
408 */
409int inode_permission(struct inode *inode, int mask)
410{
411 int retval;
412
413 retval = sb_permission(inode->i_sb, inode, mask);
414 if (retval)
415 return retval;
416 return __inode_permission(inode, mask);
417}
418
419/**
Jan Blunck5dd784d02008-02-14 19:34:38 -0800420 * path_get - get a reference to a path
421 * @path: path to get the reference to
422 *
423 * Given a path increment the reference count to the dentry and the vfsmount.
424 */
425void path_get(struct path *path)
426{
427 mntget(path->mnt);
428 dget(path->dentry);
429}
430EXPORT_SYMBOL(path_get);
431
432/**
Jan Blunck1d957f92008-02-14 19:34:35 -0800433 * path_put - put a reference to a path
434 * @path: path to put the reference to
435 *
436 * Given a path decrement the reference count to the dentry and the vfsmount.
437 */
438void path_put(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
Jan Blunck1d957f92008-02-14 19:34:35 -0800440 dput(path->dentry);
441 mntput(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
Jan Blunck1d957f92008-02-14 19:34:35 -0800443EXPORT_SYMBOL(path_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Al Viro19660af2011-03-25 10:32:48 -0400445/*
Nick Piggin31e6b012011-01-07 17:49:52 +1100446 * Path walking has 2 modes, rcu-walk and ref-walk (see
Al Viro19660af2011-03-25 10:32:48 -0400447 * Documentation/filesystems/path-lookup.txt). In situations when we can't
448 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
449 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
450 * mode. Refcounts are grabbed at the last known good point before rcu-walk
451 * got stuck, so ref-walk may continue from there. If this is not successful
452 * (eg. a seqcount has changed), then failure is returned and it's up to caller
453 * to restart the path walk from the beginning in ref-walk mode.
Nick Piggin31e6b012011-01-07 17:49:52 +1100454 */
Nick Piggin31e6b012011-01-07 17:49:52 +1100455
Al Viro32a79912012-07-18 20:43:19 +0400456static inline void lock_rcu_walk(void)
457{
458 br_read_lock(&vfsmount_lock);
459 rcu_read_lock();
460}
461
462static inline void unlock_rcu_walk(void)
463{
464 rcu_read_unlock();
465 br_read_unlock(&vfsmount_lock);
466}
467
Nick Piggin31e6b012011-01-07 17:49:52 +1100468/**
Al Viro19660af2011-03-25 10:32:48 -0400469 * unlazy_walk - try to switch to ref-walk mode.
470 * @nd: nameidata pathwalk data
471 * @dentry: child of nd->path.dentry or NULL
Randy Dunlap39191622011-01-08 19:36:21 -0800472 * Returns: 0 on success, -ECHILD on failure
Nick Piggin31e6b012011-01-07 17:49:52 +1100473 *
Al Viro19660af2011-03-25 10:32:48 -0400474 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
475 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
476 * @nd or NULL. Must be called from rcu-walk context.
Nick Piggin31e6b012011-01-07 17:49:52 +1100477 */
Al Viro19660af2011-03-25 10:32:48 -0400478static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
Nick Piggin31e6b012011-01-07 17:49:52 +1100479{
480 struct fs_struct *fs = current->fs;
481 struct dentry *parent = nd->path.dentry;
Al Viro5b6ca022011-03-09 23:04:47 -0500482 int want_root = 0;
Nick Piggin31e6b012011-01-07 17:49:52 +1100483
484 BUG_ON(!(nd->flags & LOOKUP_RCU));
Al Viro5b6ca022011-03-09 23:04:47 -0500485 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
486 want_root = 1;
Nick Piggin31e6b012011-01-07 17:49:52 +1100487 spin_lock(&fs->lock);
488 if (nd->root.mnt != fs->root.mnt ||
489 nd->root.dentry != fs->root.dentry)
490 goto err_root;
491 }
492 spin_lock(&parent->d_lock);
Al Viro19660af2011-03-25 10:32:48 -0400493 if (!dentry) {
494 if (!__d_rcu_to_refcount(parent, nd->seq))
495 goto err_parent;
496 BUG_ON(nd->inode != parent->d_inode);
497 } else {
Al Viro94c0d4e2011-07-12 21:40:23 -0400498 if (dentry->d_parent != parent)
499 goto err_parent;
Al Viro19660af2011-03-25 10:32:48 -0400500 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
501 if (!__d_rcu_to_refcount(dentry, nd->seq))
502 goto err_child;
503 /*
504 * If the sequence check on the child dentry passed, then
505 * the child has not been removed from its parent. This
506 * means the parent dentry must be valid and able to take
507 * a reference at this point.
508 */
509 BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
510 BUG_ON(!parent->d_count);
511 parent->d_count++;
512 spin_unlock(&dentry->d_lock);
513 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100514 spin_unlock(&parent->d_lock);
Al Viro5b6ca022011-03-09 23:04:47 -0500515 if (want_root) {
Nick Piggin31e6b012011-01-07 17:49:52 +1100516 path_get(&nd->root);
517 spin_unlock(&fs->lock);
518 }
519 mntget(nd->path.mnt);
520
Al Viro32a79912012-07-18 20:43:19 +0400521 unlock_rcu_walk();
Nick Piggin31e6b012011-01-07 17:49:52 +1100522 nd->flags &= ~LOOKUP_RCU;
523 return 0;
Al Viro19660af2011-03-25 10:32:48 -0400524
525err_child:
Nick Piggin31e6b012011-01-07 17:49:52 +1100526 spin_unlock(&dentry->d_lock);
Al Viro19660af2011-03-25 10:32:48 -0400527err_parent:
Nick Piggin31e6b012011-01-07 17:49:52 +1100528 spin_unlock(&parent->d_lock);
529err_root:
Al Viro5b6ca022011-03-09 23:04:47 -0500530 if (want_root)
Nick Piggin31e6b012011-01-07 17:49:52 +1100531 spin_unlock(&fs->lock);
532 return -ECHILD;
533}
534
Al Viro4ce16ef32012-06-10 16:10:59 -0400535static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
Nick Piggin34286d62011-01-07 17:49:57 +1100536{
Al Viro4ce16ef32012-06-10 16:10:59 -0400537 return dentry->d_op->d_revalidate(dentry, flags);
Nick Piggin34286d62011-01-07 17:49:57 +1100538}
539
Al Viro9f1fafe2011-03-25 11:00:12 -0400540/**
541 * complete_walk - successful completion of path walk
542 * @nd: pointer nameidata
Jeff Layton39159de2009-12-07 12:01:50 -0500543 *
Al Viro9f1fafe2011-03-25 11:00:12 -0400544 * If we had been in RCU mode, drop out of it and legitimize nd->path.
545 * Revalidate the final result, unless we'd already done that during
546 * the path walk or the filesystem doesn't ask for it. Return 0 on
547 * success, -error on failure. In case of failure caller does not
548 * need to drop nd->path.
Jeff Layton39159de2009-12-07 12:01:50 -0500549 */
Al Viro9f1fafe2011-03-25 11:00:12 -0400550static int complete_walk(struct nameidata *nd)
Jeff Layton39159de2009-12-07 12:01:50 -0500551{
Al Viro16c2cd72011-02-22 15:50:10 -0500552 struct dentry *dentry = nd->path.dentry;
Jeff Layton39159de2009-12-07 12:01:50 -0500553 int status;
Jeff Layton39159de2009-12-07 12:01:50 -0500554
Al Viro9f1fafe2011-03-25 11:00:12 -0400555 if (nd->flags & LOOKUP_RCU) {
556 nd->flags &= ~LOOKUP_RCU;
557 if (!(nd->flags & LOOKUP_ROOT))
558 nd->root.mnt = NULL;
559 spin_lock(&dentry->d_lock);
560 if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
561 spin_unlock(&dentry->d_lock);
Al Viro32a79912012-07-18 20:43:19 +0400562 unlock_rcu_walk();
Al Viro9f1fafe2011-03-25 11:00:12 -0400563 return -ECHILD;
564 }
565 BUG_ON(nd->inode != dentry->d_inode);
566 spin_unlock(&dentry->d_lock);
567 mntget(nd->path.mnt);
Al Viro32a79912012-07-18 20:43:19 +0400568 unlock_rcu_walk();
Al Viro9f1fafe2011-03-25 11:00:12 -0400569 }
570
Al Viro16c2cd72011-02-22 15:50:10 -0500571 if (likely(!(nd->flags & LOOKUP_JUMPED)))
Jeff Layton39159de2009-12-07 12:01:50 -0500572 return 0;
573
Al Viro16c2cd72011-02-22 15:50:10 -0500574 if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
575 return 0;
576
577 if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
578 return 0;
579
580 /* Note: we do not d_invalidate() */
Al Viro4ce16ef32012-06-10 16:10:59 -0400581 status = d_revalidate(dentry, nd->flags);
Jeff Layton39159de2009-12-07 12:01:50 -0500582 if (status > 0)
583 return 0;
584
Al Viro16c2cd72011-02-22 15:50:10 -0500585 if (!status)
Jeff Layton39159de2009-12-07 12:01:50 -0500586 status = -ESTALE;
Al Viro16c2cd72011-02-22 15:50:10 -0500587
Al Viro9f1fafe2011-03-25 11:00:12 -0400588 path_put(&nd->path);
Jeff Layton39159de2009-12-07 12:01:50 -0500589 return status;
590}
591
Al Viro2a737872009-04-07 11:49:53 -0400592static __always_inline void set_root(struct nameidata *nd)
593{
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200594 if (!nd->root.mnt)
595 get_fs_root(current->fs, &nd->root);
Al Viro2a737872009-04-07 11:49:53 -0400596}
597
Al Viro6de88d72009-08-09 01:41:57 +0400598static int link_path_walk(const char *, struct nameidata *);
599
Nick Piggin31e6b012011-01-07 17:49:52 +1100600static __always_inline void set_root_rcu(struct nameidata *nd)
601{
602 if (!nd->root.mnt) {
603 struct fs_struct *fs = current->fs;
Nick Pigginc28cc362011-01-07 17:49:53 +1100604 unsigned seq;
605
606 do {
607 seq = read_seqcount_begin(&fs->seq);
608 nd->root = fs->root;
Tim Chenc1530012011-04-15 11:39:29 -0700609 nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
Nick Pigginc28cc362011-01-07 17:49:53 +1100610 } while (read_seqcount_retry(&fs->seq, seq));
Nick Piggin31e6b012011-01-07 17:49:52 +1100611 }
612}
613
Arjan van de Venf1662352006-01-14 13:21:31 -0800614static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Nick Piggin31e6b012011-01-07 17:49:52 +1100616 int ret;
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 if (IS_ERR(link))
619 goto fail;
620
621 if (*link == '/') {
Al Viro2a737872009-04-07 11:49:53 -0400622 set_root(nd);
Jan Blunck1d957f92008-02-14 19:34:35 -0800623 path_put(&nd->path);
Al Viro2a737872009-04-07 11:49:53 -0400624 nd->path = nd->root;
625 path_get(&nd->root);
Al Viro16c2cd72011-02-22 15:50:10 -0500626 nd->flags |= LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100628 nd->inode = nd->path.dentry->d_inode;
Christoph Hellwigb4091d52008-11-05 15:07:21 +0100629
Nick Piggin31e6b012011-01-07 17:49:52 +1100630 ret = link_path_walk(link, nd);
631 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632fail:
Jan Blunck1d957f92008-02-14 19:34:35 -0800633 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return PTR_ERR(link);
635}
636
Jan Blunck1d957f92008-02-14 19:34:35 -0800637static void path_put_conditional(struct path *path, struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700638{
639 dput(path->dentry);
Jan Blunck4ac91372008-02-14 19:34:32 -0800640 if (path->mnt != nd->path.mnt)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700641 mntput(path->mnt);
642}
643
Nick Piggin7b9337a2011-01-14 08:42:43 +0000644static inline void path_to_nameidata(const struct path *path,
645 struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700646{
Nick Piggin31e6b012011-01-07 17:49:52 +1100647 if (!(nd->flags & LOOKUP_RCU)) {
648 dput(nd->path.dentry);
649 if (nd->path.mnt != path->mnt)
650 mntput(nd->path.mnt);
Huang Shijie9a229682010-04-02 17:37:13 +0800651 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100652 nd->path.mnt = path->mnt;
Jan Blunck4ac91372008-02-14 19:34:32 -0800653 nd->path.dentry = path->dentry;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700654}
655
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400656/*
657 * Helper to directly jump to a known parsed path from ->follow_link,
658 * caller must have taken a reference to path beforehand.
659 */
660void nd_jump_link(struct nameidata *nd, struct path *path)
661{
662 path_put(&nd->path);
663
664 nd->path = *path;
665 nd->inode = nd->path.dentry->d_inode;
666 nd->flags |= LOOKUP_JUMPED;
667
668 BUG_ON(nd->inode->i_op->follow_link);
669}
670
Al Viro574197e2011-03-14 22:20:34 -0400671static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
672{
673 struct inode *inode = link->dentry->d_inode;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400674 if (inode->i_op->put_link)
Al Viro574197e2011-03-14 22:20:34 -0400675 inode->i_op->put_link(link->dentry, nd, cookie);
676 path_put(link);
677}
678
Kees Cook800179c2012-07-25 17:29:07 -0700679int sysctl_protected_symlinks __read_mostly = 1;
680int sysctl_protected_hardlinks __read_mostly = 1;
681
682/**
683 * may_follow_link - Check symlink following for unsafe situations
684 * @link: The path of the symlink
Randy Dunlap55852632012-08-18 17:39:25 -0700685 * @nd: nameidata pathwalk data
Kees Cook800179c2012-07-25 17:29:07 -0700686 *
687 * In the case of the sysctl_protected_symlinks sysctl being enabled,
688 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
689 * in a sticky world-writable directory. This is to protect privileged
690 * processes from failing races against path names that may change out
691 * from under them by way of other users creating malicious symlinks.
692 * It will permit symlinks to be followed only when outside a sticky
693 * world-writable directory, or when the uid of the symlink and follower
694 * match, or when the directory owner matches the symlink's owner.
695 *
696 * Returns 0 if following the symlink is allowed, -ve on error.
697 */
698static inline int may_follow_link(struct path *link, struct nameidata *nd)
699{
700 const struct inode *inode;
701 const struct inode *parent;
702
703 if (!sysctl_protected_symlinks)
704 return 0;
705
706 /* Allowed if owner and follower match. */
707 inode = link->dentry->d_inode;
Eric W. Biederman81abe272012-08-03 09:38:08 -0700708 if (uid_eq(current_cred()->fsuid, inode->i_uid))
Kees Cook800179c2012-07-25 17:29:07 -0700709 return 0;
710
711 /* Allowed if parent directory not sticky and world-writable. */
712 parent = nd->path.dentry->d_inode;
713 if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
714 return 0;
715
716 /* Allowed if parent directory and link owner match. */
Eric W. Biederman81abe272012-08-03 09:38:08 -0700717 if (uid_eq(parent->i_uid, inode->i_uid))
Kees Cook800179c2012-07-25 17:29:07 -0700718 return 0;
719
Sasha Levinffd8d102012-10-04 19:56:40 -0400720 audit_log_link_denied("follow_link", link);
Kees Cook800179c2012-07-25 17:29:07 -0700721 path_put_conditional(link, nd);
722 path_put(&nd->path);
723 return -EACCES;
724}
725
726/**
727 * safe_hardlink_source - Check for safe hardlink conditions
728 * @inode: the source inode to hardlink from
729 *
730 * Return false if at least one of the following conditions:
731 * - inode is not a regular file
732 * - inode is setuid
733 * - inode is setgid and group-exec
734 * - access failure for read and write
735 *
736 * Otherwise returns true.
737 */
738static bool safe_hardlink_source(struct inode *inode)
739{
740 umode_t mode = inode->i_mode;
741
742 /* Special files should not get pinned to the filesystem. */
743 if (!S_ISREG(mode))
744 return false;
745
746 /* Setuid files should not get pinned to the filesystem. */
747 if (mode & S_ISUID)
748 return false;
749
750 /* Executable setgid files should not get pinned to the filesystem. */
751 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
752 return false;
753
754 /* Hardlinking to unreadable or unwritable sources is dangerous. */
755 if (inode_permission(inode, MAY_READ | MAY_WRITE))
756 return false;
757
758 return true;
759}
760
761/**
762 * may_linkat - Check permissions for creating a hardlink
763 * @link: the source to hardlink from
764 *
765 * Block hardlink when all of:
766 * - sysctl_protected_hardlinks enabled
767 * - fsuid does not match inode
768 * - hardlink source is unsafe (see safe_hardlink_source() above)
769 * - not CAP_FOWNER
770 *
771 * Returns 0 if successful, -ve on error.
772 */
773static int may_linkat(struct path *link)
774{
775 const struct cred *cred;
776 struct inode *inode;
777
778 if (!sysctl_protected_hardlinks)
779 return 0;
780
781 cred = current_cred();
782 inode = link->dentry->d_inode;
783
784 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
785 * otherwise, it must be a safe source.
786 */
Eric W. Biederman81abe272012-08-03 09:38:08 -0700787 if (uid_eq(cred->fsuid, inode->i_uid) || safe_hardlink_source(inode) ||
Kees Cook800179c2012-07-25 17:29:07 -0700788 capable(CAP_FOWNER))
789 return 0;
790
Kees Cooka51d9ea2012-07-25 17:29:08 -0700791 audit_log_link_denied("linkat", link);
Kees Cook800179c2012-07-25 17:29:07 -0700792 return -EPERM;
793}
794
Al Virodef4af32009-12-26 08:37:05 -0500795static __always_inline int
Al Viro574197e2011-03-14 22:20:34 -0400796follow_link(struct path *link, struct nameidata *nd, void **p)
Ian Kent051d3812006-03-27 01:14:53 -0800797{
Nick Piggin7b9337a2011-01-14 08:42:43 +0000798 struct dentry *dentry = link->dentry;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400799 int error;
800 char *s;
Ian Kent051d3812006-03-27 01:14:53 -0800801
Al Viro844a3912011-02-15 00:38:26 -0500802 BUG_ON(nd->flags & LOOKUP_RCU);
803
Al Viro0e794582011-03-16 02:45:02 -0400804 if (link->mnt == nd->path.mnt)
805 mntget(link->mnt);
806
Al Viro6d7b5aa2012-06-10 04:15:17 -0400807 error = -ELOOP;
808 if (unlikely(current->total_link_count >= 40))
809 goto out_put_nd_path;
810
Al Viro574197e2011-03-14 22:20:34 -0400811 cond_resched();
812 current->total_link_count++;
813
Al Viro68ac1232012-03-15 08:21:57 -0400814 touch_atime(link);
Ian Kent051d3812006-03-27 01:14:53 -0800815 nd_set_link(nd, NULL);
816
Al Viro36f3b4f2011-02-22 21:24:38 -0500817 error = security_inode_follow_link(link->dentry, nd);
Al Viro6d7b5aa2012-06-10 04:15:17 -0400818 if (error)
819 goto out_put_nd_path;
Al Viro36f3b4f2011-02-22 21:24:38 -0500820
Al Viro86acdca12009-12-22 23:45:11 -0500821 nd->last_type = LAST_BIND;
Al Virodef4af32009-12-26 08:37:05 -0500822 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
823 error = PTR_ERR(*p);
Al Viro6d7b5aa2012-06-10 04:15:17 -0400824 if (IS_ERR(*p))
Christoph Hellwig408ef012012-06-18 10:47:03 -0400825 goto out_put_nd_path;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400826
827 error = 0;
828 s = nd_get_link(nd);
829 if (s) {
830 error = __vfs_follow_link(nd, s);
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400831 if (unlikely(error))
832 put_link(nd, link, *p);
Ian Kent051d3812006-03-27 01:14:53 -0800833 }
Al Viro6d7b5aa2012-06-10 04:15:17 -0400834
835 return error;
836
837out_put_nd_path:
Arnd Bergmann98f6ef62012-10-11 13:20:00 +0000838 *p = NULL;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400839 path_put(&nd->path);
Al Viro6d7b5aa2012-06-10 04:15:17 -0400840 path_put(link);
Ian Kent051d3812006-03-27 01:14:53 -0800841 return error;
842}
843
Nick Piggin31e6b012011-01-07 17:49:52 +1100844static int follow_up_rcu(struct path *path)
845{
Al Viro0714a532011-11-24 22:19:58 -0500846 struct mount *mnt = real_mount(path->mnt);
847 struct mount *parent;
Nick Piggin31e6b012011-01-07 17:49:52 +1100848 struct dentry *mountpoint;
849
Al Viro0714a532011-11-24 22:19:58 -0500850 parent = mnt->mnt_parent;
851 if (&parent->mnt == path->mnt)
Nick Piggin31e6b012011-01-07 17:49:52 +1100852 return 0;
Al Viroa73324d2011-11-24 22:25:07 -0500853 mountpoint = mnt->mnt_mountpoint;
Nick Piggin31e6b012011-01-07 17:49:52 +1100854 path->dentry = mountpoint;
Al Viro0714a532011-11-24 22:19:58 -0500855 path->mnt = &parent->mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +1100856 return 1;
857}
858
David Howellsf015f1262012-06-25 12:55:28 +0100859/*
860 * follow_up - Find the mountpoint of path's vfsmount
861 *
862 * Given a path, find the mountpoint of its source file system.
863 * Replace @path with the path of the mountpoint in the parent mount.
864 * Up is towards /.
865 *
866 * Return 1 if we went up a level and 0 if we were already at the
867 * root.
868 */
Al Virobab77eb2009-04-18 03:26:48 -0400869int follow_up(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
Al Viro0714a532011-11-24 22:19:58 -0500871 struct mount *mnt = real_mount(path->mnt);
872 struct mount *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 struct dentry *mountpoint;
Nick Piggin99b7db72010-08-18 04:37:39 +1000874
Andi Kleen962830d2012-05-08 13:32:02 +0930875 br_read_lock(&vfsmount_lock);
Al Viro0714a532011-11-24 22:19:58 -0500876 parent = mnt->mnt_parent;
Al Viro3c0a6162012-07-18 17:32:50 +0400877 if (parent == mnt) {
Andi Kleen962830d2012-05-08 13:32:02 +0930878 br_read_unlock(&vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 return 0;
880 }
Al Viro0714a532011-11-24 22:19:58 -0500881 mntget(&parent->mnt);
Al Viroa73324d2011-11-24 22:25:07 -0500882 mountpoint = dget(mnt->mnt_mountpoint);
Andi Kleen962830d2012-05-08 13:32:02 +0930883 br_read_unlock(&vfsmount_lock);
Al Virobab77eb2009-04-18 03:26:48 -0400884 dput(path->dentry);
885 path->dentry = mountpoint;
886 mntput(path->mnt);
Al Viro0714a532011-11-24 22:19:58 -0500887 path->mnt = &parent->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 return 1;
889}
890
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100891/*
David Howells9875cf82011-01-14 18:45:21 +0000892 * Perform an automount
893 * - return -EISDIR to tell follow_managed() to stop and return the path we
894 * were called with.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 */
David Howells9875cf82011-01-14 18:45:21 +0000896static int follow_automount(struct path *path, unsigned flags,
897 bool *need_mntput)
Nick Piggin31e6b012011-01-07 17:49:52 +1100898{
David Howells9875cf82011-01-14 18:45:21 +0000899 struct vfsmount *mnt;
David Howellsea5b7782011-01-14 19:10:03 +0000900 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +1100901
David Howells9875cf82011-01-14 18:45:21 +0000902 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
903 return -EREMOTE;
Al Viro463ffb22005-06-06 13:36:05 -0700904
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200905 /* We don't want to mount if someone's just doing a stat -
906 * unless they're stat'ing a directory and appended a '/' to
907 * the name.
908 *
909 * We do, however, want to mount if someone wants to open or
910 * create a file of any type under the mountpoint, wants to
911 * traverse through the mountpoint or wants to open the
912 * mounted directory. Also, autofs may mark negative dentries
913 * as being automount points. These will need the attentions
914 * of the daemon to instantiate them before they can be used.
David Howells9875cf82011-01-14 18:45:21 +0000915 */
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200916 if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
Linus Torvaldsd94c1772011-09-26 17:44:55 -0700917 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200918 path->dentry->d_inode)
919 return -EISDIR;
920
David Howells9875cf82011-01-14 18:45:21 +0000921 current->total_link_count++;
922 if (current->total_link_count >= 40)
923 return -ELOOP;
924
925 mnt = path->dentry->d_op->d_automount(path);
926 if (IS_ERR(mnt)) {
927 /*
928 * The filesystem is allowed to return -EISDIR here to indicate
929 * it doesn't want to automount. For instance, autofs would do
930 * this so that its userspace daemon can mount on this dentry.
931 *
932 * However, we can only permit this if it's a terminal point in
933 * the path being looked up; if it wasn't then the remainder of
934 * the path is inaccessible and we should say so.
935 */
Al Viro49084c32011-06-25 21:59:52 -0400936 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
David Howells9875cf82011-01-14 18:45:21 +0000937 return -EREMOTE;
938 return PTR_ERR(mnt);
939 }
David Howellsea5b7782011-01-14 19:10:03 +0000940
David Howells9875cf82011-01-14 18:45:21 +0000941 if (!mnt) /* mount collision */
942 return 0;
943
Al Viro8aef1882011-06-16 15:10:06 +0100944 if (!*need_mntput) {
945 /* lock_mount() may release path->mnt on error */
946 mntget(path->mnt);
947 *need_mntput = true;
948 }
Al Viro19a167a2011-01-17 01:35:23 -0500949 err = finish_automount(mnt, path);
David Howellsea5b7782011-01-14 19:10:03 +0000950
David Howellsea5b7782011-01-14 19:10:03 +0000951 switch (err) {
952 case -EBUSY:
953 /* Someone else made a mount here whilst we were busy */
Al Viro19a167a2011-01-17 01:35:23 -0500954 return 0;
David Howellsea5b7782011-01-14 19:10:03 +0000955 case 0:
Al Viro8aef1882011-06-16 15:10:06 +0100956 path_put(path);
David Howellsea5b7782011-01-14 19:10:03 +0000957 path->mnt = mnt;
958 path->dentry = dget(mnt->mnt_root);
David Howellsea5b7782011-01-14 19:10:03 +0000959 return 0;
Al Viro19a167a2011-01-17 01:35:23 -0500960 default:
961 return err;
David Howellsea5b7782011-01-14 19:10:03 +0000962 }
Al Viro19a167a2011-01-17 01:35:23 -0500963
David Howells9875cf82011-01-14 18:45:21 +0000964}
965
966/*
967 * Handle a dentry that is managed in some way.
David Howellscc53ce52011-01-14 18:45:26 +0000968 * - Flagged for transit management (autofs)
David Howells9875cf82011-01-14 18:45:21 +0000969 * - Flagged as mountpoint
970 * - Flagged as automount point
971 *
972 * This may only be called in refwalk mode.
973 *
974 * Serialization is taken care of in namespace.c
975 */
976static int follow_managed(struct path *path, unsigned flags)
977{
Al Viro8aef1882011-06-16 15:10:06 +0100978 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
David Howells9875cf82011-01-14 18:45:21 +0000979 unsigned managed;
980 bool need_mntput = false;
Al Viro8aef1882011-06-16 15:10:06 +0100981 int ret = 0;
David Howells9875cf82011-01-14 18:45:21 +0000982
983 /* Given that we're not holding a lock here, we retain the value in a
984 * local variable for each dentry as we look at it so that we don't see
985 * the components of that value change under us */
986 while (managed = ACCESS_ONCE(path->dentry->d_flags),
987 managed &= DCACHE_MANAGED_DENTRY,
988 unlikely(managed != 0)) {
David Howellscc53ce52011-01-14 18:45:26 +0000989 /* Allow the filesystem to manage the transit without i_mutex
990 * being held. */
991 if (managed & DCACHE_MANAGE_TRANSIT) {
992 BUG_ON(!path->dentry->d_op);
993 BUG_ON(!path->dentry->d_op->d_manage);
Al Viro1aed3e42011-03-18 09:09:02 -0400994 ret = path->dentry->d_op->d_manage(path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +0000995 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +0100996 break;
David Howellscc53ce52011-01-14 18:45:26 +0000997 }
998
David Howells9875cf82011-01-14 18:45:21 +0000999 /* Transit to a mounted filesystem. */
1000 if (managed & DCACHE_MOUNTED) {
1001 struct vfsmount *mounted = lookup_mnt(path);
1002 if (mounted) {
1003 dput(path->dentry);
1004 if (need_mntput)
1005 mntput(path->mnt);
1006 path->mnt = mounted;
1007 path->dentry = dget(mounted->mnt_root);
1008 need_mntput = true;
1009 continue;
1010 }
1011
1012 /* Something is mounted on this dentry in another
1013 * namespace and/or whatever was mounted there in this
1014 * namespace got unmounted before we managed to get the
1015 * vfsmount_lock */
1016 }
1017
1018 /* Handle an automount point */
1019 if (managed & DCACHE_NEED_AUTOMOUNT) {
1020 ret = follow_automount(path, flags, &need_mntput);
1021 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001022 break;
David Howells9875cf82011-01-14 18:45:21 +00001023 continue;
1024 }
1025
1026 /* We didn't change the current path point */
1027 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 }
Al Viro8aef1882011-06-16 15:10:06 +01001029
1030 if (need_mntput && path->mnt == mnt)
1031 mntput(path->mnt);
1032 if (ret == -EISDIR)
1033 ret = 0;
Al Viroa3fbbde2011-11-07 21:21:26 +00001034 return ret < 0 ? ret : need_mntput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035}
1036
David Howellscc53ce52011-01-14 18:45:26 +00001037int follow_down_one(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
1039 struct vfsmount *mounted;
1040
Al Viro1c755af2009-04-18 14:06:57 -04001041 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -04001043 dput(path->dentry);
1044 mntput(path->mnt);
1045 path->mnt = mounted;
1046 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 return 1;
1048 }
1049 return 0;
1050}
1051
Ian Kent62a73752011-03-25 01:51:02 +08001052static inline bool managed_dentry_might_block(struct dentry *dentry)
1053{
1054 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
1055 dentry->d_op->d_manage(dentry, true) < 0);
1056}
1057
David Howells9875cf82011-01-14 18:45:21 +00001058/*
Al Viro287548e2011-05-27 06:50:06 -04001059 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1060 * we meet a managed dentry that would need blocking.
David Howells9875cf82011-01-14 18:45:21 +00001061 */
1062static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
Al Viro287548e2011-05-27 06:50:06 -04001063 struct inode **inode)
David Howells9875cf82011-01-14 18:45:21 +00001064{
Ian Kent62a73752011-03-25 01:51:02 +08001065 for (;;) {
Al Viroc7105362011-11-24 18:22:03 -05001066 struct mount *mounted;
Ian Kent62a73752011-03-25 01:51:02 +08001067 /*
1068 * Don't forget we might have a non-mountpoint managed dentry
1069 * that wants to block transit.
1070 */
Al Viro287548e2011-05-27 06:50:06 -04001071 if (unlikely(managed_dentry_might_block(path->dentry)))
David Howellsab909112011-01-14 18:46:51 +00001072 return false;
Ian Kent62a73752011-03-25 01:51:02 +08001073
1074 if (!d_mountpoint(path->dentry))
1075 break;
1076
David Howells9875cf82011-01-14 18:45:21 +00001077 mounted = __lookup_mnt(path->mnt, path->dentry, 1);
1078 if (!mounted)
1079 break;
Al Viroc7105362011-11-24 18:22:03 -05001080 path->mnt = &mounted->mnt;
1081 path->dentry = mounted->mnt.mnt_root;
Al Viroa3fbbde2011-11-07 21:21:26 +00001082 nd->flags |= LOOKUP_JUMPED;
David Howells9875cf82011-01-14 18:45:21 +00001083 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
Linus Torvalds59430262011-07-18 15:43:29 -07001084 /*
1085 * Update the inode too. We don't need to re-check the
1086 * dentry sequence number here after this d_inode read,
1087 * because a mount-point is always pinned.
1088 */
1089 *inode = path->dentry->d_inode;
David Howells9875cf82011-01-14 18:45:21 +00001090 }
David Howells9875cf82011-01-14 18:45:21 +00001091 return true;
1092}
1093
Al Virodea39372011-05-27 06:53:39 -04001094static void follow_mount_rcu(struct nameidata *nd)
Al Viro287548e2011-05-27 06:50:06 -04001095{
Al Virodea39372011-05-27 06:53:39 -04001096 while (d_mountpoint(nd->path.dentry)) {
Al Viroc7105362011-11-24 18:22:03 -05001097 struct mount *mounted;
Al Virodea39372011-05-27 06:53:39 -04001098 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
Al Viro287548e2011-05-27 06:50:06 -04001099 if (!mounted)
1100 break;
Al Viroc7105362011-11-24 18:22:03 -05001101 nd->path.mnt = &mounted->mnt;
1102 nd->path.dentry = mounted->mnt.mnt_root;
Al Virodea39372011-05-27 06:53:39 -04001103 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro287548e2011-05-27 06:50:06 -04001104 }
1105}
1106
Nick Piggin31e6b012011-01-07 17:49:52 +11001107static int follow_dotdot_rcu(struct nameidata *nd)
1108{
Nick Piggin31e6b012011-01-07 17:49:52 +11001109 set_root_rcu(nd);
1110
David Howells9875cf82011-01-14 18:45:21 +00001111 while (1) {
Nick Piggin31e6b012011-01-07 17:49:52 +11001112 if (nd->path.dentry == nd->root.dentry &&
1113 nd->path.mnt == nd->root.mnt) {
1114 break;
1115 }
1116 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1117 struct dentry *old = nd->path.dentry;
1118 struct dentry *parent = old->d_parent;
1119 unsigned seq;
1120
1121 seq = read_seqcount_begin(&parent->d_seq);
1122 if (read_seqcount_retry(&old->d_seq, nd->seq))
Al Viroef7562d2011-03-04 14:35:59 -05001123 goto failed;
Nick Piggin31e6b012011-01-07 17:49:52 +11001124 nd->path.dentry = parent;
1125 nd->seq = seq;
1126 break;
1127 }
1128 if (!follow_up_rcu(&nd->path))
1129 break;
1130 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Nick Piggin31e6b012011-01-07 17:49:52 +11001131 }
Al Virodea39372011-05-27 06:53:39 -04001132 follow_mount_rcu(nd);
1133 nd->inode = nd->path.dentry->d_inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001134 return 0;
Al Viroef7562d2011-03-04 14:35:59 -05001135
1136failed:
1137 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001138 if (!(nd->flags & LOOKUP_ROOT))
1139 nd->root.mnt = NULL;
Al Viro32a79912012-07-18 20:43:19 +04001140 unlock_rcu_walk();
Al Viroef7562d2011-03-04 14:35:59 -05001141 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001142}
1143
David Howells9875cf82011-01-14 18:45:21 +00001144/*
David Howellscc53ce52011-01-14 18:45:26 +00001145 * Follow down to the covering mount currently visible to userspace. At each
1146 * point, the filesystem owning that dentry may be queried as to whether the
1147 * caller is permitted to proceed or not.
David Howellscc53ce52011-01-14 18:45:26 +00001148 */
Al Viro7cc90cc2011-03-18 09:04:20 -04001149int follow_down(struct path *path)
David Howellscc53ce52011-01-14 18:45:26 +00001150{
1151 unsigned managed;
1152 int ret;
1153
1154 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1155 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1156 /* Allow the filesystem to manage the transit without i_mutex
1157 * being held.
1158 *
1159 * We indicate to the filesystem if someone is trying to mount
1160 * something here. This gives autofs the chance to deny anyone
1161 * other than its daemon the right to mount on its
1162 * superstructure.
1163 *
1164 * The filesystem may sleep at this point.
1165 */
1166 if (managed & DCACHE_MANAGE_TRANSIT) {
1167 BUG_ON(!path->dentry->d_op);
1168 BUG_ON(!path->dentry->d_op->d_manage);
David Howellsab909112011-01-14 18:46:51 +00001169 ret = path->dentry->d_op->d_manage(
Al Viro1aed3e42011-03-18 09:09:02 -04001170 path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +00001171 if (ret < 0)
1172 return ret == -EISDIR ? 0 : ret;
1173 }
1174
1175 /* Transit to a mounted filesystem. */
1176 if (managed & DCACHE_MOUNTED) {
1177 struct vfsmount *mounted = lookup_mnt(path);
1178 if (!mounted)
1179 break;
1180 dput(path->dentry);
1181 mntput(path->mnt);
1182 path->mnt = mounted;
1183 path->dentry = dget(mounted->mnt_root);
1184 continue;
1185 }
1186
1187 /* Don't handle automount points here */
1188 break;
1189 }
1190 return 0;
1191}
1192
1193/*
David Howells9875cf82011-01-14 18:45:21 +00001194 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1195 */
1196static void follow_mount(struct path *path)
1197{
1198 while (d_mountpoint(path->dentry)) {
1199 struct vfsmount *mounted = lookup_mnt(path);
1200 if (!mounted)
1201 break;
1202 dput(path->dentry);
1203 mntput(path->mnt);
1204 path->mnt = mounted;
1205 path->dentry = dget(mounted->mnt_root);
1206 }
1207}
1208
Nick Piggin31e6b012011-01-07 17:49:52 +11001209static void follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210{
Al Viro2a737872009-04-07 11:49:53 -04001211 set_root(nd);
Andreas Mohre518ddb2006-09-29 02:01:22 -07001212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 while(1) {
Jan Blunck4ac91372008-02-14 19:34:32 -08001214 struct dentry *old = nd->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Al Viro2a737872009-04-07 11:49:53 -04001216 if (nd->path.dentry == nd->root.dentry &&
1217 nd->path.mnt == nd->root.mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 break;
1219 }
Jan Blunck4ac91372008-02-14 19:34:32 -08001220 if (nd->path.dentry != nd->path.mnt->mnt_root) {
Al Viro3088dd72010-01-30 15:47:29 -05001221 /* rare case of legitimate dget_parent()... */
1222 nd->path.dentry = dget_parent(nd->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 dput(old);
1224 break;
1225 }
Al Viro3088dd72010-01-30 15:47:29 -05001226 if (!follow_up(&nd->path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 }
Al Viro79ed0222009-04-18 13:59:41 -04001229 follow_mount(&nd->path);
Nick Piggin31e6b012011-01-07 17:49:52 +11001230 nd->inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231}
1232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233/*
Miklos Szeredibad61182012-03-26 12:54:24 +02001234 * This looks up the name in dcache, possibly revalidates the old dentry and
1235 * allocates a new one if not found or not valid. In the need_lookup argument
1236 * returns whether i_op->lookup is necessary.
1237 *
1238 * dir->d_inode->i_mutex must be held
Nick Pigginbaa03892010-08-18 04:37:31 +10001239 */
Miklos Szeredibad61182012-03-26 12:54:24 +02001240static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
Al Viro201f9562012-06-22 12:42:10 +04001241 unsigned int flags, bool *need_lookup)
Nick Pigginbaa03892010-08-18 04:37:31 +10001242{
Nick Pigginbaa03892010-08-18 04:37:31 +10001243 struct dentry *dentry;
Miklos Szeredibad61182012-03-26 12:54:24 +02001244 int error;
Nick Pigginbaa03892010-08-18 04:37:31 +10001245
Miklos Szeredibad61182012-03-26 12:54:24 +02001246 *need_lookup = false;
1247 dentry = d_lookup(dir, name);
1248 if (dentry) {
1249 if (d_need_lookup(dentry)) {
1250 *need_lookup = true;
1251 } else if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
Al Viro201f9562012-06-22 12:42:10 +04001252 error = d_revalidate(dentry, flags);
Miklos Szeredibad61182012-03-26 12:54:24 +02001253 if (unlikely(error <= 0)) {
1254 if (error < 0) {
1255 dput(dentry);
1256 return ERR_PTR(error);
1257 } else if (!d_invalidate(dentry)) {
1258 dput(dentry);
1259 dentry = NULL;
1260 }
1261 }
1262 }
1263 }
Nick Pigginbaa03892010-08-18 04:37:31 +10001264
Miklos Szeredibad61182012-03-26 12:54:24 +02001265 if (!dentry) {
1266 dentry = d_alloc(dir, name);
1267 if (unlikely(!dentry))
1268 return ERR_PTR(-ENOMEM);
Nick Pigginbaa03892010-08-18 04:37:31 +10001269
Miklos Szeredibad61182012-03-26 12:54:24 +02001270 *need_lookup = true;
Nick Pigginbaa03892010-08-18 04:37:31 +10001271 }
1272 return dentry;
1273}
1274
1275/*
Miklos Szeredibad61182012-03-26 12:54:24 +02001276 * Call i_op->lookup on the dentry. The dentry must be negative but may be
1277 * hashed if it was pouplated with DCACHE_NEED_LOOKUP.
1278 *
1279 * dir->d_inode->i_mutex must be held
Josef Bacik44396f42011-05-31 11:58:49 -04001280 */
Miklos Szeredibad61182012-03-26 12:54:24 +02001281static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
Al Viro72bd8662012-06-10 17:17:17 -04001282 unsigned int flags)
Josef Bacik44396f42011-05-31 11:58:49 -04001283{
Josef Bacik44396f42011-05-31 11:58:49 -04001284 struct dentry *old;
1285
1286 /* Don't create child dentry for a dead directory. */
Miklos Szeredibad61182012-03-26 12:54:24 +02001287 if (unlikely(IS_DEADDIR(dir))) {
Miklos Szeredie188dc02012-02-03 14:25:18 +01001288 dput(dentry);
Josef Bacik44396f42011-05-31 11:58:49 -04001289 return ERR_PTR(-ENOENT);
Miklos Szeredie188dc02012-02-03 14:25:18 +01001290 }
Josef Bacik44396f42011-05-31 11:58:49 -04001291
Al Viro72bd8662012-06-10 17:17:17 -04001292 old = dir->i_op->lookup(dir, dentry, flags);
Josef Bacik44396f42011-05-31 11:58:49 -04001293 if (unlikely(old)) {
1294 dput(dentry);
1295 dentry = old;
1296 }
1297 return dentry;
1298}
1299
Al Viroa3255542012-03-30 14:41:51 -04001300static struct dentry *__lookup_hash(struct qstr *name,
Al Viro72bd8662012-06-10 17:17:17 -04001301 struct dentry *base, unsigned int flags)
Al Viroa3255542012-03-30 14:41:51 -04001302{
Miklos Szeredibad61182012-03-26 12:54:24 +02001303 bool need_lookup;
Al Viroa3255542012-03-30 14:41:51 -04001304 struct dentry *dentry;
1305
Al Viro72bd8662012-06-10 17:17:17 -04001306 dentry = lookup_dcache(name, base, flags, &need_lookup);
Miklos Szeredibad61182012-03-26 12:54:24 +02001307 if (!need_lookup)
1308 return dentry;
Al Viroa3255542012-03-30 14:41:51 -04001309
Al Viro72bd8662012-06-10 17:17:17 -04001310 return lookup_real(base->d_inode, dentry, flags);
Al Viroa3255542012-03-30 14:41:51 -04001311}
1312
Josef Bacik44396f42011-05-31 11:58:49 -04001313/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 * It's more convoluted than I'd like it to be, but... it's still fairly
1315 * small and for now I'd prefer to have fast path as straight as possible.
1316 * It _is_ time-critical.
1317 */
Miklos Szeredi697f5142012-05-21 17:30:05 +02001318static int lookup_fast(struct nameidata *nd, struct qstr *name,
1319 struct path *path, struct inode **inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
Jan Blunck4ac91372008-02-14 19:34:32 -08001321 struct vfsmount *mnt = nd->path.mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +11001322 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro5a18fff2011-03-11 04:44:53 -05001323 int need_reval = 1;
1324 int status = 1;
David Howells9875cf82011-01-14 18:45:21 +00001325 int err;
1326
Al Viro3cac2602009-08-13 18:27:43 +04001327 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001328 * Rename seqlock is not required here because in the off chance
1329 * of a false negative due to a concurrent rename, we're going to
1330 * do the non-racy lookup, below.
1331 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001332 if (nd->flags & LOOKUP_RCU) {
1333 unsigned seq;
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001334 dentry = __d_lookup_rcu(parent, name, &seq, nd->inode);
Al Viro5a18fff2011-03-11 04:44:53 -05001335 if (!dentry)
1336 goto unlazy;
1337
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001338 /*
1339 * This sequence count validates that the inode matches
1340 * the dentry name information from lookup.
1341 */
1342 *inode = dentry->d_inode;
1343 if (read_seqcount_retry(&dentry->d_seq, seq))
1344 return -ECHILD;
1345
1346 /*
1347 * This sequence count validates that the parent had no
1348 * changes while we did the lookup of the dentry above.
1349 *
1350 * The memory barrier in read_seqcount_begin of child is
1351 * enough, we can use __read_seqcount_retry here.
1352 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001353 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1354 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001355 nd->seq = seq;
Al Viro5a18fff2011-03-11 04:44:53 -05001356
Miklos Szeredifa4ee152012-03-26 12:54:19 +02001357 if (unlikely(d_need_lookup(dentry)))
1358 goto unlazy;
Al Viro24643082011-02-15 01:26:22 -05001359 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
Al Viro4ce16ef32012-06-10 16:10:59 -04001360 status = d_revalidate(dentry, nd->flags);
Al Viro5a18fff2011-03-11 04:44:53 -05001361 if (unlikely(status <= 0)) {
1362 if (status != -ECHILD)
1363 need_reval = 0;
1364 goto unlazy;
1365 }
Al Viro24643082011-02-15 01:26:22 -05001366 }
Nick Piggin31e6b012011-01-07 17:49:52 +11001367 path->mnt = mnt;
1368 path->dentry = dentry;
Al Virod6e9bd22011-05-27 07:03:15 -04001369 if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1370 goto unlazy;
1371 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1372 goto unlazy;
1373 return 0;
Al Viro5a18fff2011-03-11 04:44:53 -05001374unlazy:
Al Viro19660af2011-03-25 10:32:48 -04001375 if (unlazy_walk(nd, dentry))
1376 return -ECHILD;
Al Viro5a18fff2011-03-11 04:44:53 -05001377 } else {
1378 dentry = __d_lookup(parent, name);
Nick Piggin31e6b012011-01-07 17:49:52 +11001379 }
Al Viro5a18fff2011-03-11 04:44:53 -05001380
Al Viro81e6f522012-03-30 14:48:04 -04001381 if (unlikely(!dentry))
1382 goto need_lookup;
Al Viro5a18fff2011-03-11 04:44:53 -05001383
Al Viro81e6f522012-03-30 14:48:04 -04001384 if (unlikely(d_need_lookup(dentry))) {
1385 dput(dentry);
1386 goto need_lookup;
Al Viro24643082011-02-15 01:26:22 -05001387 }
Al Viro81e6f522012-03-30 14:48:04 -04001388
Al Viro5a18fff2011-03-11 04:44:53 -05001389 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
Al Viro4ce16ef32012-06-10 16:10:59 -04001390 status = d_revalidate(dentry, nd->flags);
Al Viro5a18fff2011-03-11 04:44:53 -05001391 if (unlikely(status <= 0)) {
1392 if (status < 0) {
1393 dput(dentry);
1394 return status;
1395 }
1396 if (!d_invalidate(dentry)) {
1397 dput(dentry);
Al Viro81e6f522012-03-30 14:48:04 -04001398 goto need_lookup;
Al Viro5a18fff2011-03-11 04:44:53 -05001399 }
1400 }
Miklos Szeredi697f5142012-05-21 17:30:05 +02001401
David Howells9875cf82011-01-14 18:45:21 +00001402 path->mnt = mnt;
1403 path->dentry = dentry;
1404 err = follow_managed(path, nd->flags);
Ian Kent89312212011-01-18 12:06:10 +08001405 if (unlikely(err < 0)) {
1406 path_put_conditional(path, nd);
David Howells9875cf82011-01-14 18:45:21 +00001407 return err;
Ian Kent89312212011-01-18 12:06:10 +08001408 }
Al Viroa3fbbde2011-11-07 21:21:26 +00001409 if (err)
1410 nd->flags |= LOOKUP_JUMPED;
David Howells9875cf82011-01-14 18:45:21 +00001411 *inode = path->dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 return 0;
Al Viro81e6f522012-03-30 14:48:04 -04001413
1414need_lookup:
Miklos Szeredi697f5142012-05-21 17:30:05 +02001415 return 1;
1416}
1417
1418/* Fast lookup failed, do it the slow way */
1419static int lookup_slow(struct nameidata *nd, struct qstr *name,
1420 struct path *path)
1421{
1422 struct dentry *dentry, *parent;
1423 int err;
1424
1425 parent = nd->path.dentry;
Al Viro81e6f522012-03-30 14:48:04 -04001426 BUG_ON(nd->inode != parent->d_inode);
1427
1428 mutex_lock(&parent->d_inode->i_mutex);
Al Viro72bd8662012-06-10 17:17:17 -04001429 dentry = __lookup_hash(name, parent, nd->flags);
Al Viro81e6f522012-03-30 14:48:04 -04001430 mutex_unlock(&parent->d_inode->i_mutex);
1431 if (IS_ERR(dentry))
1432 return PTR_ERR(dentry);
Miklos Szeredi697f5142012-05-21 17:30:05 +02001433 path->mnt = nd->path.mnt;
1434 path->dentry = dentry;
1435 err = follow_managed(path, nd->flags);
1436 if (unlikely(err < 0)) {
1437 path_put_conditional(path, nd);
1438 return err;
1439 }
1440 if (err)
1441 nd->flags |= LOOKUP_JUMPED;
1442 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443}
1444
Al Viro52094c82011-02-21 21:34:47 -05001445static inline int may_lookup(struct nameidata *nd)
1446{
1447 if (nd->flags & LOOKUP_RCU) {
Al Viro4ad5abb2011-06-20 19:57:03 -04001448 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
Al Viro52094c82011-02-21 21:34:47 -05001449 if (err != -ECHILD)
1450 return err;
Al Viro19660af2011-03-25 10:32:48 -04001451 if (unlazy_walk(nd, NULL))
Al Viro52094c82011-02-21 21:34:47 -05001452 return -ECHILD;
1453 }
Al Viro4ad5abb2011-06-20 19:57:03 -04001454 return inode_permission(nd->inode, MAY_EXEC);
Al Viro52094c82011-02-21 21:34:47 -05001455}
1456
Al Viro9856fa12011-03-04 14:22:06 -05001457static inline int handle_dots(struct nameidata *nd, int type)
1458{
1459 if (type == LAST_DOTDOT) {
1460 if (nd->flags & LOOKUP_RCU) {
1461 if (follow_dotdot_rcu(nd))
1462 return -ECHILD;
1463 } else
1464 follow_dotdot(nd);
1465 }
1466 return 0;
1467}
1468
Al Viro951361f2011-03-04 14:44:37 -05001469static void terminate_walk(struct nameidata *nd)
1470{
1471 if (!(nd->flags & LOOKUP_RCU)) {
1472 path_put(&nd->path);
1473 } else {
1474 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001475 if (!(nd->flags & LOOKUP_ROOT))
1476 nd->root.mnt = NULL;
Al Viro32a79912012-07-18 20:43:19 +04001477 unlock_rcu_walk();
Al Viro951361f2011-03-04 14:44:37 -05001478 }
1479}
1480
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001481/*
1482 * Do we need to follow links? We _really_ want to be able
1483 * to do this check without having to look at inode->i_op,
1484 * so we keep a cache of "no, this doesn't need follow_link"
1485 * for the common case.
1486 */
Linus Torvalds7813b942011-08-07 09:53:20 -07001487static inline int should_follow_link(struct inode *inode, int follow)
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001488{
1489 if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
1490 if (likely(inode->i_op->follow_link))
1491 return follow;
1492
1493 /* This gets set once for the inode lifetime */
1494 spin_lock(&inode->i_lock);
1495 inode->i_opflags |= IOP_NOFOLLOW;
1496 spin_unlock(&inode->i_lock);
1497 }
1498 return 0;
1499}
1500
Al Viroce57dfc2011-03-13 19:58:58 -04001501static inline int walk_component(struct nameidata *nd, struct path *path,
1502 struct qstr *name, int type, int follow)
1503{
1504 struct inode *inode;
1505 int err;
1506 /*
1507 * "." and ".." are special - ".." especially so because it has
1508 * to be able to know about the current root directory and
1509 * parent relationships.
1510 */
1511 if (unlikely(type != LAST_NORM))
1512 return handle_dots(nd, type);
Miklos Szeredi697f5142012-05-21 17:30:05 +02001513 err = lookup_fast(nd, name, path, &inode);
Al Viroce57dfc2011-03-13 19:58:58 -04001514 if (unlikely(err)) {
Miklos Szeredi697f5142012-05-21 17:30:05 +02001515 if (err < 0)
1516 goto out_err;
1517
1518 err = lookup_slow(nd, name, path);
1519 if (err < 0)
1520 goto out_err;
1521
1522 inode = path->dentry->d_inode;
Al Viroce57dfc2011-03-13 19:58:58 -04001523 }
Miklos Szeredi697f5142012-05-21 17:30:05 +02001524 err = -ENOENT;
1525 if (!inode)
1526 goto out_path_put;
1527
Linus Torvalds7813b942011-08-07 09:53:20 -07001528 if (should_follow_link(inode, follow)) {
Al Viro19660af2011-03-25 10:32:48 -04001529 if (nd->flags & LOOKUP_RCU) {
1530 if (unlikely(unlazy_walk(nd, path->dentry))) {
Miklos Szeredi697f5142012-05-21 17:30:05 +02001531 err = -ECHILD;
1532 goto out_err;
Al Viro19660af2011-03-25 10:32:48 -04001533 }
1534 }
Al Viroce57dfc2011-03-13 19:58:58 -04001535 BUG_ON(inode != path->dentry->d_inode);
1536 return 1;
1537 }
1538 path_to_nameidata(path, nd);
1539 nd->inode = inode;
1540 return 0;
Miklos Szeredi697f5142012-05-21 17:30:05 +02001541
1542out_path_put:
1543 path_to_nameidata(path, nd);
1544out_err:
1545 terminate_walk(nd);
1546 return err;
Al Viroce57dfc2011-03-13 19:58:58 -04001547}
1548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549/*
Al Virob3563792011-03-14 21:54:55 -04001550 * This limits recursive symlink follows to 8, while
1551 * limiting consecutive symlinks to 40.
1552 *
1553 * Without that kind of total limit, nasty chains of consecutive
1554 * symlinks can cause almost arbitrarily long lookups.
1555 */
1556static inline int nested_symlink(struct path *path, struct nameidata *nd)
1557{
1558 int res;
1559
Al Virob3563792011-03-14 21:54:55 -04001560 if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1561 path_put_conditional(path, nd);
1562 path_put(&nd->path);
1563 return -ELOOP;
1564 }
Erez Zadok1a4022f2011-05-21 01:19:59 -04001565 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
Al Virob3563792011-03-14 21:54:55 -04001566
1567 nd->depth++;
1568 current->link_count++;
1569
1570 do {
1571 struct path link = *path;
1572 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04001573
1574 res = follow_link(&link, nd, &cookie);
Al Viro6d7b5aa2012-06-10 04:15:17 -04001575 if (res)
1576 break;
1577 res = walk_component(nd, path, &nd->last,
1578 nd->last_type, LOOKUP_FOLLOW);
Al Viro574197e2011-03-14 22:20:34 -04001579 put_link(nd, &link, cookie);
Al Virob3563792011-03-14 21:54:55 -04001580 } while (res > 0);
1581
1582 current->link_count--;
1583 nd->depth--;
1584 return res;
1585}
1586
1587/*
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001588 * We really don't want to look at inode->i_op->lookup
1589 * when we don't have to. So we keep a cache bit in
1590 * the inode ->i_opflags field that says "yes, we can
1591 * do lookup on this inode".
1592 */
1593static inline int can_lookup(struct inode *inode)
1594{
1595 if (likely(inode->i_opflags & IOP_LOOKUP))
1596 return 1;
1597 if (likely(!inode->i_op->lookup))
1598 return 0;
1599
1600 /* We do this once for the lifetime of the inode */
1601 spin_lock(&inode->i_lock);
1602 inode->i_opflags |= IOP_LOOKUP;
1603 spin_unlock(&inode->i_lock);
1604 return 1;
1605}
1606
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001607/*
1608 * We can do the critical dentry name comparison and hashing
1609 * operations one word at a time, but we are limited to:
1610 *
1611 * - Architectures with fast unaligned word accesses. We could
1612 * do a "get_unaligned()" if this helps and is sufficiently
1613 * fast.
1614 *
1615 * - Little-endian machines (so that we can generate the mask
1616 * of low bytes efficiently). Again, we *could* do a byte
1617 * swapping load on big-endian architectures if that is not
1618 * expensive enough to make the optimization worthless.
1619 *
1620 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1621 * do not trap on the (extremely unlikely) case of a page
1622 * crossing operation.
1623 *
1624 * - Furthermore, we need an efficient 64-bit compile for the
1625 * 64-bit case in order to generate the "number of bytes in
1626 * the final mask". Again, that could be replaced with a
1627 * efficient population count instruction or similar.
1628 */
1629#ifdef CONFIG_DCACHE_WORD_ACCESS
1630
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001631#include <asm/word-at-a-time.h>
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001632
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001633#ifdef CONFIG_64BIT
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001634
1635static inline unsigned int fold_hash(unsigned long hash)
1636{
1637 hash += hash >> (8*sizeof(int));
1638 return hash;
1639}
1640
1641#else /* 32-bit case */
1642
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001643#define fold_hash(x) (x)
1644
1645#endif
1646
1647unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1648{
1649 unsigned long a, mask;
1650 unsigned long hash = 0;
1651
1652 for (;;) {
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001653 a = load_unaligned_zeropad(name);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001654 if (len < sizeof(unsigned long))
1655 break;
1656 hash += a;
Al Virof132c5b2012-03-22 21:59:52 +00001657 hash *= 9;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001658 name += sizeof(unsigned long);
1659 len -= sizeof(unsigned long);
1660 if (!len)
1661 goto done;
1662 }
1663 mask = ~(~0ul << len*8);
1664 hash += mask & a;
1665done:
1666 return fold_hash(hash);
1667}
1668EXPORT_SYMBOL(full_name_hash);
1669
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001670/*
1671 * Calculate the length and hash of the path component, and
1672 * return the length of the component;
1673 */
1674static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1675{
Linus Torvalds36126f82012-05-26 10:43:17 -07001676 unsigned long a, b, adata, bdata, mask, hash, len;
1677 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001678
1679 hash = a = 0;
1680 len = -sizeof(unsigned long);
1681 do {
1682 hash = (hash + a) * 9;
1683 len += sizeof(unsigned long);
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001684 a = load_unaligned_zeropad(name+len);
Linus Torvalds36126f82012-05-26 10:43:17 -07001685 b = a ^ REPEAT_BYTE('/');
1686 } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001687
Linus Torvalds36126f82012-05-26 10:43:17 -07001688 adata = prep_zero_mask(a, adata, &constants);
1689 bdata = prep_zero_mask(b, bdata, &constants);
1690
1691 mask = create_zero_mask(adata | bdata);
1692
1693 hash += a & zero_bytemask(mask);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001694 *hashp = fold_hash(hash);
1695
Linus Torvalds36126f82012-05-26 10:43:17 -07001696 return len + find_zero(mask);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001697}
1698
1699#else
1700
Linus Torvalds0145acc2012-03-02 14:32:59 -08001701unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1702{
1703 unsigned long hash = init_name_hash();
1704 while (len--)
1705 hash = partial_name_hash(*name++, hash);
1706 return end_name_hash(hash);
1707}
Linus Torvaldsae942ae2012-03-02 19:40:57 -08001708EXPORT_SYMBOL(full_name_hash);
Linus Torvalds0145acc2012-03-02 14:32:59 -08001709
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001710/*
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001711 * We know there's a real path component here of at least
1712 * one character.
1713 */
1714static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1715{
1716 unsigned long hash = init_name_hash();
1717 unsigned long len = 0, c;
1718
1719 c = (unsigned char)*name;
1720 do {
1721 len++;
1722 hash = partial_name_hash(c, hash);
1723 c = (unsigned char)name[len];
1724 } while (c && c != '/');
1725 *hashp = end_name_hash(hash);
1726 return len;
1727}
1728
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001729#endif
1730
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001731/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01001733 * This is the basic name resolution function, turning a pathname into
1734 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01001736 * Returns 0 and nd will have valid dentry and mnt on success.
1737 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 */
Al Viro6de88d72009-08-09 01:41:57 +04001739static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740{
1741 struct path next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
1744 while (*name=='/')
1745 name++;
1746 if (!*name)
Al Viro086e1832011-02-22 20:56:27 -05001747 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 /* At this point we know we have a real path component. */
1750 for(;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 struct qstr this;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001752 long len;
Al Virofe479a52011-02-22 15:10:03 -05001753 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Al Viro52094c82011-02-21 21:34:47 -05001755 err = may_lookup(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 if (err)
1757 break;
1758
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001759 len = hash_name(name, &this.hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 this.name = name;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001761 this.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
Al Virofe479a52011-02-22 15:10:03 -05001763 type = LAST_NORM;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001764 if (name[0] == '.') switch (len) {
Al Virofe479a52011-02-22 15:10:03 -05001765 case 2:
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001766 if (name[1] == '.') {
Al Virofe479a52011-02-22 15:10:03 -05001767 type = LAST_DOTDOT;
Al Viro16c2cd72011-02-22 15:50:10 -05001768 nd->flags |= LOOKUP_JUMPED;
1769 }
Al Virofe479a52011-02-22 15:10:03 -05001770 break;
1771 case 1:
1772 type = LAST_DOT;
1773 }
Al Viro5a202bc2011-03-08 14:17:44 -05001774 if (likely(type == LAST_NORM)) {
1775 struct dentry *parent = nd->path.dentry;
Al Viro16c2cd72011-02-22 15:50:10 -05001776 nd->flags &= ~LOOKUP_JUMPED;
Al Viro5a202bc2011-03-08 14:17:44 -05001777 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1778 err = parent->d_op->d_hash(parent, nd->inode,
1779 &this);
1780 if (err < 0)
1781 break;
1782 }
1783 }
Al Virofe479a52011-02-22 15:10:03 -05001784
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001785 if (!name[len])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 goto last_component;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001787 /*
1788 * If it wasn't NUL, we know it was '/'. Skip that
1789 * slash, and continue until no more slashes.
1790 */
1791 do {
1792 len++;
1793 } while (unlikely(name[len] == '/'));
1794 if (!name[len])
Al Virob3563792011-03-14 21:54:55 -04001795 goto last_component;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001796 name += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
Al Viroce57dfc2011-03-13 19:58:58 -04001798 err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1799 if (err < 0)
1800 return err;
Al Virofe479a52011-02-22 15:10:03 -05001801
Al Viroce57dfc2011-03-13 19:58:58 -04001802 if (err) {
Al Virob3563792011-03-14 21:54:55 -04001803 err = nested_symlink(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 if (err)
Al Viroa7472ba2011-03-04 14:39:30 -05001805 return err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001806 }
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001807 if (can_lookup(nd->inode))
1808 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 err = -ENOTDIR;
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001810 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 /* here ends the main loop */
1812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813last_component:
Al Virob3563792011-03-14 21:54:55 -04001814 nd->last = this;
1815 nd->last_type = type;
Al Viro086e1832011-02-22 20:56:27 -05001816 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 }
Al Viro951361f2011-03-04 14:44:37 -05001818 terminate_walk(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 return err;
1820}
1821
Al Viro70e9b352011-03-05 21:12:22 -05001822static int path_init(int dfd, const char *name, unsigned int flags,
1823 struct nameidata *nd, struct file **fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824{
Prasanna Medaea3834d2005-04-29 16:00:17 +01001825 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
1827 nd->last_type = LAST_ROOT; /* if there are only slashes... */
Al Viro16c2cd72011-02-22 15:50:10 -05001828 nd->flags = flags | LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 nd->depth = 0;
Al Viro5b6ca022011-03-09 23:04:47 -05001830 if (flags & LOOKUP_ROOT) {
1831 struct inode *inode = nd->root.dentry->d_inode;
Al Viro73d049a2011-03-11 12:08:24 -05001832 if (*name) {
1833 if (!inode->i_op->lookup)
1834 return -ENOTDIR;
1835 retval = inode_permission(inode, MAY_EXEC);
1836 if (retval)
1837 return retval;
1838 }
Al Viro5b6ca022011-03-09 23:04:47 -05001839 nd->path = nd->root;
1840 nd->inode = inode;
1841 if (flags & LOOKUP_RCU) {
Al Viro32a79912012-07-18 20:43:19 +04001842 lock_rcu_walk();
Al Viro5b6ca022011-03-09 23:04:47 -05001843 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1844 } else {
1845 path_get(&nd->path);
1846 }
1847 return 0;
1848 }
1849
Al Viro2a737872009-04-07 11:49:53 -04001850 nd->root.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 if (*name=='/') {
Al Viroe41f7d42011-02-22 14:02:58 -05001853 if (flags & LOOKUP_RCU) {
Al Viro32a79912012-07-18 20:43:19 +04001854 lock_rcu_walk();
Al Viroe41f7d42011-02-22 14:02:58 -05001855 set_root_rcu(nd);
1856 } else {
1857 set_root(nd);
1858 path_get(&nd->root);
1859 }
Al Viro2a737872009-04-07 11:49:53 -04001860 nd->path = nd->root;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001861 } else if (dfd == AT_FDCWD) {
Al Viroe41f7d42011-02-22 14:02:58 -05001862 if (flags & LOOKUP_RCU) {
1863 struct fs_struct *fs = current->fs;
1864 unsigned seq;
1865
Al Viro32a79912012-07-18 20:43:19 +04001866 lock_rcu_walk();
Al Viroe41f7d42011-02-22 14:02:58 -05001867
1868 do {
1869 seq = read_seqcount_begin(&fs->seq);
1870 nd->path = fs->pwd;
1871 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1872 } while (read_seqcount_retry(&fs->seq, seq));
1873 } else {
1874 get_fs_pwd(current->fs, &nd->path);
1875 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001876 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001877 struct fd f = fdget_raw(dfd);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001878 struct dentry *dentry;
1879
Al Viro2903ff02012-08-28 12:52:22 -04001880 if (!f.file)
1881 return -EBADF;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001882
Al Viro2903ff02012-08-28 12:52:22 -04001883 dentry = f.file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001884
Al Virof52e0c12011-03-14 18:56:51 -04001885 if (*name) {
Al Viro2903ff02012-08-28 12:52:22 -04001886 if (!S_ISDIR(dentry->d_inode->i_mode)) {
1887 fdput(f);
1888 return -ENOTDIR;
1889 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001890
Al Viro4ad5abb2011-06-20 19:57:03 -04001891 retval = inode_permission(dentry->d_inode, MAY_EXEC);
Al Viro2903ff02012-08-28 12:52:22 -04001892 if (retval) {
1893 fdput(f);
1894 return retval;
1895 }
Al Virof52e0c12011-03-14 18:56:51 -04001896 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001897
Al Viro2903ff02012-08-28 12:52:22 -04001898 nd->path = f.file->f_path;
Al Viroe41f7d42011-02-22 14:02:58 -05001899 if (flags & LOOKUP_RCU) {
Al Viro2903ff02012-08-28 12:52:22 -04001900 if (f.need_put)
1901 *fp = f.file;
Al Viroe41f7d42011-02-22 14:02:58 -05001902 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro32a79912012-07-18 20:43:19 +04001903 lock_rcu_walk();
Al Viroe41f7d42011-02-22 14:02:58 -05001904 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001905 path_get(&nd->path);
1906 fdput(f);
Al Viroe41f7d42011-02-22 14:02:58 -05001907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 }
Al Viroe41f7d42011-02-22 14:02:58 -05001909
Nick Piggin31e6b012011-01-07 17:49:52 +11001910 nd->inode = nd->path.dentry->d_inode;
Al Viro9b4a9b12009-04-07 11:44:16 -04001911 return 0;
Al Viro9b4a9b12009-04-07 11:44:16 -04001912}
1913
Al Virobd92d7f2011-03-14 19:54:59 -04001914static inline int lookup_last(struct nameidata *nd, struct path *path)
1915{
1916 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1917 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1918
1919 nd->flags &= ~LOOKUP_PARENT;
1920 return walk_component(nd, path, &nd->last, nd->last_type,
1921 nd->flags & LOOKUP_FOLLOW);
1922}
1923
Al Viro9b4a9b12009-04-07 11:44:16 -04001924/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroee0827c2011-02-21 23:38:09 -05001925static int path_lookupat(int dfd, const char *name,
Al Viro9b4a9b12009-04-07 11:44:16 -04001926 unsigned int flags, struct nameidata *nd)
1927{
Al Viro70e9b352011-03-05 21:12:22 -05001928 struct file *base = NULL;
Al Virobd92d7f2011-03-14 19:54:59 -04001929 struct path path;
1930 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001931
1932 /*
1933 * Path walking is largely split up into 2 different synchronisation
1934 * schemes, rcu-walk and ref-walk (explained in
1935 * Documentation/filesystems/path-lookup.txt). These share much of the
1936 * path walk code, but some things particularly setup, cleanup, and
1937 * following mounts are sufficiently divergent that functions are
1938 * duplicated. Typically there is a function foo(), and its RCU
1939 * analogue, foo_rcu().
1940 *
1941 * -ECHILD is the error number of choice (just to avoid clashes) that
1942 * is returned if some aspect of an rcu-walk fails. Such an error must
1943 * be handled by restarting a traditional ref-walk (which will always
1944 * be able to complete).
1945 */
Al Virobd92d7f2011-03-14 19:54:59 -04001946 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
Al Viroee0827c2011-02-21 23:38:09 -05001947
Al Virobd92d7f2011-03-14 19:54:59 -04001948 if (unlikely(err))
1949 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001950
1951 current->total_link_count = 0;
Al Virobd92d7f2011-03-14 19:54:59 -04001952 err = link_path_walk(name, nd);
1953
1954 if (!err && !(flags & LOOKUP_PARENT)) {
Al Virobd92d7f2011-03-14 19:54:59 -04001955 err = lookup_last(nd, &path);
1956 while (err > 0) {
1957 void *cookie;
1958 struct path link = path;
Kees Cook800179c2012-07-25 17:29:07 -07001959 err = may_follow_link(&link, nd);
1960 if (unlikely(err))
1961 break;
Al Virobd92d7f2011-03-14 19:54:59 -04001962 nd->flags |= LOOKUP_PARENT;
Al Viro574197e2011-03-14 22:20:34 -04001963 err = follow_link(&link, nd, &cookie);
Al Viro6d7b5aa2012-06-10 04:15:17 -04001964 if (err)
1965 break;
1966 err = lookup_last(nd, &path);
Al Viro574197e2011-03-14 22:20:34 -04001967 put_link(nd, &link, cookie);
Al Virobd92d7f2011-03-14 19:54:59 -04001968 }
1969 }
Al Viroee0827c2011-02-21 23:38:09 -05001970
Al Viro9f1fafe2011-03-25 11:00:12 -04001971 if (!err)
1972 err = complete_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04001973
1974 if (!err && nd->flags & LOOKUP_DIRECTORY) {
1975 if (!nd->inode->i_op->lookup) {
1976 path_put(&nd->path);
Al Virobd23a532011-03-23 09:56:30 -04001977 err = -ENOTDIR;
Al Virobd92d7f2011-03-14 19:54:59 -04001978 }
1979 }
Al Viro16c2cd72011-02-22 15:50:10 -05001980
Al Viro70e9b352011-03-05 21:12:22 -05001981 if (base)
1982 fput(base);
Al Viroee0827c2011-02-21 23:38:09 -05001983
Al Viro5b6ca022011-03-09 23:04:47 -05001984 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
Al Viro2a737872009-04-07 11:49:53 -04001985 path_put(&nd->root);
1986 nd->root.mnt = NULL;
1987 }
Al Virobd92d7f2011-03-14 19:54:59 -04001988 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001989}
Nick Piggin31e6b012011-01-07 17:49:52 +11001990
Jeff Layton873f1ee2012-10-10 15:25:29 -04001991static int filename_lookup(int dfd, struct filename *name,
1992 unsigned int flags, struct nameidata *nd)
1993{
1994 int retval = path_lookupat(dfd, name->name, flags | LOOKUP_RCU, nd);
1995 if (unlikely(retval == -ECHILD))
1996 retval = path_lookupat(dfd, name->name, flags, nd);
1997 if (unlikely(retval == -ESTALE))
1998 retval = path_lookupat(dfd, name->name,
1999 flags | LOOKUP_REVAL, nd);
2000
2001 if (likely(!retval))
2002 audit_inode(name->name, nd->path.dentry,
2003 flags & LOOKUP_PARENT);
2004 return retval;
2005}
2006
Al Viroee0827c2011-02-21 23:38:09 -05002007static int do_path_lookup(int dfd, const char *name,
2008 unsigned int flags, struct nameidata *nd)
2009{
Jeff Layton873f1ee2012-10-10 15:25:29 -04002010 struct filename filename = { .name = name };
Nick Piggin31e6b012011-01-07 17:49:52 +11002011
Jeff Layton873f1ee2012-10-10 15:25:29 -04002012 return filename_lookup(dfd, &filename, flags, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013}
2014
Al Viro79714f72012-06-15 03:01:42 +04002015/* does lookup, returns the object with parent locked */
2016struct dentry *kern_path_locked(const char *name, struct path *path)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002017{
Al Viro79714f72012-06-15 03:01:42 +04002018 struct nameidata nd;
2019 struct dentry *d;
2020 int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd);
2021 if (err)
2022 return ERR_PTR(err);
2023 if (nd.last_type != LAST_NORM) {
2024 path_put(&nd.path);
2025 return ERR_PTR(-EINVAL);
2026 }
2027 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Al Viro1e0ea002012-07-22 23:46:21 +04002028 d = __lookup_hash(&nd.last, nd.path.dentry, 0);
Al Viro79714f72012-06-15 03:01:42 +04002029 if (IS_ERR(d)) {
2030 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2031 path_put(&nd.path);
2032 return d;
2033 }
2034 *path = nd.path;
2035 return d;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002036}
2037
Al Virod1811462008-08-02 00:49:18 -04002038int kern_path(const char *name, unsigned int flags, struct path *path)
2039{
2040 struct nameidata nd;
2041 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
2042 if (!res)
2043 *path = nd.path;
2044 return res;
2045}
2046
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002047/**
2048 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2049 * @dentry: pointer to dentry of the base directory
2050 * @mnt: pointer to vfs mount of the base directory
2051 * @name: pointer to file name
2052 * @flags: lookup flags
Al Viroe0a01242011-06-27 17:00:37 -04002053 * @path: pointer to struct path to fill
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002054 */
2055int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2056 const char *name, unsigned int flags,
Al Viroe0a01242011-06-27 17:00:37 -04002057 struct path *path)
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002058{
Al Viroe0a01242011-06-27 17:00:37 -04002059 struct nameidata nd;
2060 int err;
2061 nd.root.dentry = dentry;
2062 nd.root.mnt = mnt;
2063 BUG_ON(flags & LOOKUP_PARENT);
Al Viro5b6ca022011-03-09 23:04:47 -05002064 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
Al Viroe0a01242011-06-27 17:00:37 -04002065 err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
2066 if (!err)
2067 *path = nd.path;
2068 return err;
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002069}
2070
James Morris057f6c02007-04-26 00:12:05 -07002071/*
2072 * Restricted form of lookup. Doesn't follow links, single-component only,
2073 * needs parent already locked. Doesn't follow mounts.
2074 * SMP-safe.
2075 */
Adrian Bunka244e162006-03-31 02:32:11 -08002076static struct dentry *lookup_hash(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077{
Al Viro72bd8662012-06-10 17:17:17 -04002078 return __lookup_hash(&nd->last, nd->path.dentry, nd->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079}
2080
Christoph Hellwigeead1912007-10-16 23:25:38 -07002081/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07002082 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07002083 * @name: pathname component to lookup
2084 * @base: base directory to lookup from
2085 * @len: maximum length @len should be interpreted to
2086 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07002087 * Note that this routine is purely a helper for filesystem usage and should
2088 * not be called by generic code. Also note that by using this function the
Christoph Hellwigeead1912007-10-16 23:25:38 -07002089 * nameidata argument is passed to the filesystem methods and a filesystem
2090 * using this helper needs to be prepared for that.
2091 */
James Morris057f6c02007-04-26 00:12:05 -07002092struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2093{
James Morris057f6c02007-04-26 00:12:05 -07002094 struct qstr this;
Al Viro6a96ba52011-03-07 23:49:20 -05002095 unsigned int c;
Miklos Szeredicda309d2012-03-26 12:54:21 +02002096 int err;
James Morris057f6c02007-04-26 00:12:05 -07002097
David Woodhouse2f9092e2009-04-20 23:18:37 +01002098 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
2099
Al Viro6a96ba52011-03-07 23:49:20 -05002100 this.name = name;
2101 this.len = len;
Linus Torvalds0145acc2012-03-02 14:32:59 -08002102 this.hash = full_name_hash(name, len);
Al Viro6a96ba52011-03-07 23:49:20 -05002103 if (!len)
2104 return ERR_PTR(-EACCES);
2105
Al Viro6a96ba52011-03-07 23:49:20 -05002106 while (len--) {
2107 c = *(const unsigned char *)name++;
2108 if (c == '/' || c == '\0')
2109 return ERR_PTR(-EACCES);
Al Viro6a96ba52011-03-07 23:49:20 -05002110 }
Al Viro5a202bc2011-03-08 14:17:44 -05002111 /*
2112 * See if the low-level filesystem might want
2113 * to use its own hash..
2114 */
2115 if (base->d_flags & DCACHE_OP_HASH) {
2116 int err = base->d_op->d_hash(base, base->d_inode, &this);
2117 if (err < 0)
2118 return ERR_PTR(err);
2119 }
Christoph Hellwigeead1912007-10-16 23:25:38 -07002120
Miklos Szeredicda309d2012-03-26 12:54:21 +02002121 err = inode_permission(base->d_inode, MAY_EXEC);
2122 if (err)
2123 return ERR_PTR(err);
2124
Al Viro72bd8662012-06-10 17:17:17 -04002125 return __lookup_hash(&this, base, 0);
James Morris057f6c02007-04-26 00:12:05 -07002126}
2127
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002128int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2129 struct path *path, int *empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130{
Al Viro2d8f3032008-07-22 09:59:21 -04002131 struct nameidata nd;
Jeff Layton91a27b22012-10-10 15:25:28 -04002132 struct filename *tmp = getname_flags(name, flags, empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 int err = PTR_ERR(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 if (!IS_ERR(tmp)) {
Al Viro2d8f3032008-07-22 09:59:21 -04002135
2136 BUG_ON(flags & LOOKUP_PARENT);
2137
Jeff Layton873f1ee2012-10-10 15:25:29 -04002138 err = filename_lookup(dfd, tmp, flags, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 putname(tmp);
Al Viro2d8f3032008-07-22 09:59:21 -04002140 if (!err)
2141 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 }
2143 return err;
2144}
2145
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002146int user_path_at(int dfd, const char __user *name, unsigned flags,
2147 struct path *path)
2148{
Linus Torvaldsf7493e52012-03-22 16:10:40 -07002149 return user_path_at_empty(dfd, name, flags, path, NULL);
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002150}
2151
Jeff Layton873f1ee2012-10-10 15:25:29 -04002152/*
2153 * NB: most callers don't do anything directly with the reference to the
2154 * to struct filename, but the nd->last pointer points into the name string
2155 * allocated by getname. So we must hold the reference to it until all
2156 * path-walking is complete.
2157 */
Jeff Layton91a27b22012-10-10 15:25:28 -04002158static struct filename *
2159user_path_parent(int dfd, const char __user *path, struct nameidata *nd)
Al Viro2ad94ae2008-07-21 09:32:51 -04002160{
Jeff Layton91a27b22012-10-10 15:25:28 -04002161 struct filename *s = getname(path);
Al Viro2ad94ae2008-07-21 09:32:51 -04002162 int error;
2163
2164 if (IS_ERR(s))
Jeff Layton91a27b22012-10-10 15:25:28 -04002165 return s;
Al Viro2ad94ae2008-07-21 09:32:51 -04002166
Jeff Layton873f1ee2012-10-10 15:25:29 -04002167 error = filename_lookup(dfd, s, LOOKUP_PARENT, nd);
Jeff Layton91a27b22012-10-10 15:25:28 -04002168 if (error) {
Al Viro2ad94ae2008-07-21 09:32:51 -04002169 putname(s);
Jeff Layton91a27b22012-10-10 15:25:28 -04002170 return ERR_PTR(error);
2171 }
Al Viro2ad94ae2008-07-21 09:32:51 -04002172
Jeff Layton91a27b22012-10-10 15:25:28 -04002173 return s;
Al Viro2ad94ae2008-07-21 09:32:51 -04002174}
2175
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176/*
2177 * It's inline, so penalty for filesystems that don't use sticky bit is
2178 * minimal.
2179 */
2180static inline int check_sticky(struct inode *dir, struct inode *inode)
2181{
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002182 kuid_t fsuid = current_fsuid();
David Howellsda9592e2008-11-14 10:39:05 +11002183
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 if (!(dir->i_mode & S_ISVTX))
2185 return 0;
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002186 if (uid_eq(inode->i_uid, fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 return 0;
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002188 if (uid_eq(dir->i_uid, fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 return 0;
Eric W. Biederman1a48e2a2011-11-14 16:24:06 -08002190 return !inode_capable(inode, CAP_FOWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191}
2192
2193/*
2194 * Check whether we can remove a link victim from directory dir, check
2195 * whether the type of victim is right.
2196 * 1. We can't do it if dir is read-only (done in permission())
2197 * 2. We should have write and exec permissions on dir
2198 * 3. We can't remove anything from append-only dir
2199 * 4. We can't do anything with immutable dir (done in permission())
2200 * 5. If the sticky bit on dir is set we should either
2201 * a. be owner of dir, or
2202 * b. be owner of victim, or
2203 * c. have CAP_FOWNER capability
2204 * 6. If the victim is append-only or immutable we can't do antyhing with
2205 * links pointing to it.
2206 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2207 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2208 * 9. We can't remove a root or mountpoint.
2209 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
2210 * nfs_async_unlink().
2211 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08002212static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213{
2214 int error;
2215
2216 if (!victim->d_inode)
2217 return -ENOENT;
2218
2219 BUG_ON(victim->d_parent->d_inode != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04002220 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
Al Virof419a2e2008-07-22 00:07:17 -04002222 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 if (error)
2224 return error;
2225 if (IS_APPEND(dir))
2226 return -EPERM;
2227 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
Hugh Dickinsf9454542008-11-19 15:36:38 -08002228 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 return -EPERM;
2230 if (isdir) {
2231 if (!S_ISDIR(victim->d_inode->i_mode))
2232 return -ENOTDIR;
2233 if (IS_ROOT(victim))
2234 return -EBUSY;
2235 } else if (S_ISDIR(victim->d_inode->i_mode))
2236 return -EISDIR;
2237 if (IS_DEADDIR(dir))
2238 return -ENOENT;
2239 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2240 return -EBUSY;
2241 return 0;
2242}
2243
2244/* Check whether we can create an object with dentry child in directory
2245 * dir.
2246 * 1. We can't do it if child already exists (open has special treatment for
2247 * this case, but since we are inlined it's OK)
2248 * 2. We can't do it if dir is read-only (done in permission())
2249 * 3. We should have write and exec permissions on dir
2250 * 4. We can't do it if dir is immutable (done in permission())
2251 */
Miklos Szeredia95164d2008-07-30 15:08:48 +02002252static inline int may_create(struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253{
2254 if (child->d_inode)
2255 return -EEXIST;
2256 if (IS_DEADDIR(dir))
2257 return -ENOENT;
Al Virof419a2e2008-07-22 00:07:17 -04002258 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259}
2260
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261/*
2262 * p1 and p2 should be directories on the same fs.
2263 */
2264struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2265{
2266 struct dentry *p;
2267
2268 if (p1 == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07002269 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 return NULL;
2271 }
2272
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002273 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002275 p = d_ancestor(p2, p1);
2276 if (p) {
2277 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2278 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
2279 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 }
2281
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002282 p = d_ancestor(p1, p2);
2283 if (p) {
2284 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2285 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2286 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 }
2288
Ingo Molnarf2eace22006-07-03 00:25:05 -07002289 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2290 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 return NULL;
2292}
2293
2294void unlock_rename(struct dentry *p1, struct dentry *p2)
2295{
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002296 mutex_unlock(&p1->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 if (p1 != p2) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002298 mutex_unlock(&p2->d_inode->i_mutex);
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002299 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 }
2301}
2302
Al Viro4acdaf22011-07-26 01:42:34 -04002303int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viro312b63f2012-06-10 18:09:36 -04002304 bool want_excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002306 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 if (error)
2308 return error;
2309
Al Viroacfa4382008-12-04 10:06:33 -05002310 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 return -EACCES; /* shouldn't it be ENOSYS? */
2312 mode &= S_IALLUGO;
2313 mode |= S_IFREG;
2314 error = security_inode_create(dir, dentry, mode);
2315 if (error)
2316 return error;
Al Viro312b63f2012-06-10 18:09:36 -04002317 error = dir->i_op->create(dir, dentry, mode, want_excl);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002318 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002319 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 return error;
2321}
2322
Al Viro73d049a2011-03-11 12:08:24 -05002323static int may_open(struct path *path, int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002325 struct dentry *dentry = path->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 struct inode *inode = dentry->d_inode;
2327 int error;
2328
Al Virobcda7652011-03-13 16:42:14 -04002329 /* O_PATH? */
2330 if (!acc_mode)
2331 return 0;
2332
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 if (!inode)
2334 return -ENOENT;
2335
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002336 switch (inode->i_mode & S_IFMT) {
2337 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002339 case S_IFDIR:
2340 if (acc_mode & MAY_WRITE)
2341 return -EISDIR;
2342 break;
2343 case S_IFBLK:
2344 case S_IFCHR:
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002345 if (path->mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002347 /*FALLTHRU*/
2348 case S_IFIFO:
2349 case S_IFSOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002351 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08002352 }
Dave Hansenb41572e2007-10-16 23:31:14 -07002353
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002354 error = inode_permission(inode, acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07002355 if (error)
2356 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05002357
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 /*
2359 * An append-only file must be opened in append mode for writing.
2360 */
2361 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05002362 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05002363 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05002365 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 }
2367
2368 /* O_NOATIME can only be set by the owner or superuser */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07002369 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
Al Viro7715b522009-12-16 03:54:00 -05002370 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
J. Bruce Fieldsf3c7691e2011-09-21 10:58:13 -04002372 return 0;
Al Viro7715b522009-12-16 03:54:00 -05002373}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
Jeff Laytone1181ee2010-12-07 16:19:50 -05002375static int handle_truncate(struct file *filp)
Al Viro7715b522009-12-16 03:54:00 -05002376{
Jeff Laytone1181ee2010-12-07 16:19:50 -05002377 struct path *path = &filp->f_path;
Al Viro7715b522009-12-16 03:54:00 -05002378 struct inode *inode = path->dentry->d_inode;
2379 int error = get_write_access(inode);
2380 if (error)
2381 return error;
2382 /*
2383 * Refuse to truncate files with mandatory locks held on them.
2384 */
2385 error = locks_verify_locked(inode);
2386 if (!error)
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +09002387 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05002388 if (!error) {
2389 error = do_truncate(path->dentry, 0,
2390 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
Jeff Laytone1181ee2010-12-07 16:19:50 -05002391 filp);
Al Viro7715b522009-12-16 03:54:00 -05002392 }
2393 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04002394 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395}
2396
Dave Hansend57999e2008-02-15 14:37:27 -08002397static inline int open_to_namei_flags(int flag)
2398{
Al Viro8a5e9292011-06-25 19:15:54 -04002399 if ((flag & O_ACCMODE) == 3)
2400 flag--;
Dave Hansend57999e2008-02-15 14:37:27 -08002401 return flag;
2402}
2403
Miklos Szeredid18e9002012-06-05 15:10:17 +02002404static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode)
2405{
2406 int error = security_path_mknod(dir, dentry, mode, 0);
2407 if (error)
2408 return error;
2409
2410 error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
2411 if (error)
2412 return error;
2413
2414 return security_inode_create(dir->dentry->d_inode, dentry, mode);
2415}
2416
David Howells1acf0af2012-06-14 16:13:46 +01002417/*
2418 * Attempt to atomically look up, create and open a file from a negative
2419 * dentry.
2420 *
2421 * Returns 0 if successful. The file will have been created and attached to
2422 * @file by the filesystem calling finish_open().
2423 *
2424 * Returns 1 if the file was looked up only or didn't need creating. The
2425 * caller will need to perform the open themselves. @path will have been
2426 * updated to point to the new dentry. This may be negative.
2427 *
2428 * Returns an error code otherwise.
2429 */
Al Viro2675a4e2012-06-22 12:41:10 +04002430static int atomic_open(struct nameidata *nd, struct dentry *dentry,
2431 struct path *path, struct file *file,
2432 const struct open_flags *op,
Al Viro64894cf2012-07-31 00:53:35 +04002433 bool got_write, bool need_lookup,
Al Viro2675a4e2012-06-22 12:41:10 +04002434 int *opened)
Miklos Szeredid18e9002012-06-05 15:10:17 +02002435{
2436 struct inode *dir = nd->path.dentry->d_inode;
2437 unsigned open_flag = open_to_namei_flags(op->open_flag);
2438 umode_t mode;
2439 int error;
2440 int acc_mode;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002441 int create_error = 0;
2442 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
2443
2444 BUG_ON(dentry->d_inode);
2445
2446 /* Don't create child dentry for a dead directory. */
2447 if (unlikely(IS_DEADDIR(dir))) {
Al Viro2675a4e2012-06-22 12:41:10 +04002448 error = -ENOENT;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002449 goto out;
2450 }
2451
Miklos Szeredi62b259d2012-08-15 13:01:24 +02002452 mode = op->mode;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002453 if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
2454 mode &= ~current_umask();
2455
Al Virof8310c52012-07-30 11:50:30 +04002456 if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT)) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002457 open_flag &= ~O_TRUNC;
Al Viro47237682012-06-10 05:01:45 -04002458 *opened |= FILE_CREATED;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002459 }
2460
2461 /*
2462 * Checking write permission is tricky, bacuse we don't know if we are
2463 * going to actually need it: O_CREAT opens should work as long as the
2464 * file exists. But checking existence breaks atomicity. The trick is
2465 * to check access and if not granted clear O_CREAT from the flags.
2466 *
2467 * Another problem is returing the "right" error value (e.g. for an
2468 * O_EXCL open we want to return EEXIST not EROFS).
2469 */
Al Viro64894cf2012-07-31 00:53:35 +04002470 if (((open_flag & (O_CREAT | O_TRUNC)) ||
2471 (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) {
2472 if (!(open_flag & O_CREAT)) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002473 /*
2474 * No O_CREATE -> atomicity not a requirement -> fall
2475 * back to lookup + open
2476 */
2477 goto no_open;
2478 } else if (open_flag & (O_EXCL | O_TRUNC)) {
2479 /* Fall back and fail with the right error */
Al Viro64894cf2012-07-31 00:53:35 +04002480 create_error = -EROFS;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002481 goto no_open;
2482 } else {
2483 /* No side effects, safe to clear O_CREAT */
Al Viro64894cf2012-07-31 00:53:35 +04002484 create_error = -EROFS;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002485 open_flag &= ~O_CREAT;
2486 }
2487 }
2488
2489 if (open_flag & O_CREAT) {
Miklos Szeredi38227f72012-08-15 13:01:24 +02002490 error = may_o_create(&nd->path, dentry, mode);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002491 if (error) {
2492 create_error = error;
2493 if (open_flag & O_EXCL)
2494 goto no_open;
2495 open_flag &= ~O_CREAT;
2496 }
2497 }
2498
2499 if (nd->flags & LOOKUP_DIRECTORY)
2500 open_flag |= O_DIRECTORY;
2501
Al Viro30d90492012-06-22 12:40:19 +04002502 file->f_path.dentry = DENTRY_NOT_SET;
2503 file->f_path.mnt = nd->path.mnt;
2504 error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode,
Al Viro47237682012-06-10 05:01:45 -04002505 opened);
Al Virod9585272012-06-22 12:39:14 +04002506 if (error < 0) {
Al Virod9585272012-06-22 12:39:14 +04002507 if (create_error && error == -ENOENT)
2508 error = create_error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002509 goto out;
2510 }
2511
2512 acc_mode = op->acc_mode;
Al Viro47237682012-06-10 05:01:45 -04002513 if (*opened & FILE_CREATED) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002514 fsnotify_create(dir, dentry);
2515 acc_mode = MAY_OPEN;
2516 }
2517
Al Virod9585272012-06-22 12:39:14 +04002518 if (error) { /* returned 1, that is */
Al Viro30d90492012-06-22 12:40:19 +04002519 if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
Al Viro2675a4e2012-06-22 12:41:10 +04002520 error = -EIO;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002521 goto out;
2522 }
Al Viro30d90492012-06-22 12:40:19 +04002523 if (file->f_path.dentry) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002524 dput(dentry);
Al Viro30d90492012-06-22 12:40:19 +04002525 dentry = file->f_path.dentry;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002526 }
Sage Weil62b2ce92012-08-15 13:30:12 -07002527 if (create_error && dentry->d_inode == NULL) {
2528 error = create_error;
2529 goto out;
2530 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02002531 goto looked_up;
2532 }
2533
2534 /*
2535 * We didn't have the inode before the open, so check open permission
2536 * here.
2537 */
Al Viro2675a4e2012-06-22 12:41:10 +04002538 error = may_open(&file->f_path, acc_mode, open_flag);
2539 if (error)
2540 fput(file);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002541
2542out:
2543 dput(dentry);
Al Viro2675a4e2012-06-22 12:41:10 +04002544 return error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002545
Miklos Szeredid18e9002012-06-05 15:10:17 +02002546no_open:
2547 if (need_lookup) {
Al Viro72bd8662012-06-10 17:17:17 -04002548 dentry = lookup_real(dir, dentry, nd->flags);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002549 if (IS_ERR(dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04002550 return PTR_ERR(dentry);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002551
2552 if (create_error) {
2553 int open_flag = op->open_flag;
2554
Al Viro2675a4e2012-06-22 12:41:10 +04002555 error = create_error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002556 if ((open_flag & O_EXCL)) {
2557 if (!dentry->d_inode)
2558 goto out;
2559 } else if (!dentry->d_inode) {
2560 goto out;
2561 } else if ((open_flag & O_TRUNC) &&
2562 S_ISREG(dentry->d_inode->i_mode)) {
2563 goto out;
2564 }
2565 /* will fail later, go on to get the right error */
2566 }
2567 }
2568looked_up:
2569 path->dentry = dentry;
2570 path->mnt = nd->path.mnt;
Al Viro2675a4e2012-06-22 12:41:10 +04002571 return 1;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002572}
2573
Nick Piggin31e6b012011-01-07 17:49:52 +11002574/*
David Howells1acf0af2012-06-14 16:13:46 +01002575 * Look up and maybe create and open the last component.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002576 *
2577 * Must be called with i_mutex held on parent.
2578 *
David Howells1acf0af2012-06-14 16:13:46 +01002579 * Returns 0 if the file was successfully atomically created (if necessary) and
2580 * opened. In this case the file will be returned attached to @file.
2581 *
2582 * Returns 1 if the file was not completely opened at this time, though lookups
2583 * and creations will have been performed and the dentry returned in @path will
2584 * be positive upon return if O_CREAT was specified. If O_CREAT wasn't
2585 * specified then a negative dentry may be returned.
2586 *
2587 * An error code is returned otherwise.
2588 *
2589 * FILE_CREATE will be set in @*opened if the dentry was created and will be
2590 * cleared otherwise prior to returning.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002591 */
Al Viro2675a4e2012-06-22 12:41:10 +04002592static int lookup_open(struct nameidata *nd, struct path *path,
2593 struct file *file,
2594 const struct open_flags *op,
Al Viro64894cf2012-07-31 00:53:35 +04002595 bool got_write, int *opened)
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002596{
2597 struct dentry *dir = nd->path.dentry;
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002598 struct inode *dir_inode = dir->d_inode;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002599 struct dentry *dentry;
2600 int error;
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002601 bool need_lookup;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002602
Al Viro47237682012-06-10 05:01:45 -04002603 *opened &= ~FILE_CREATED;
Al Viro201f9562012-06-22 12:42:10 +04002604 dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002605 if (IS_ERR(dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04002606 return PTR_ERR(dentry);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002607
Miklos Szeredid18e9002012-06-05 15:10:17 +02002608 /* Cached positive dentry: will open in f_op->open */
2609 if (!need_lookup && dentry->d_inode)
2610 goto out_no_open;
2611
2612 if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) {
Al Viro64894cf2012-07-31 00:53:35 +04002613 return atomic_open(nd, dentry, path, file, op, got_write,
Al Viro47237682012-06-10 05:01:45 -04002614 need_lookup, opened);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002615 }
2616
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002617 if (need_lookup) {
2618 BUG_ON(dentry->d_inode);
2619
Al Viro72bd8662012-06-10 17:17:17 -04002620 dentry = lookup_real(dir_inode, dentry, nd->flags);
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002621 if (IS_ERR(dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04002622 return PTR_ERR(dentry);
Miklos Szeredi54ef4872012-06-05 15:10:16 +02002623 }
2624
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002625 /* Negative dentry, just create the file */
2626 if (!dentry->d_inode && (op->open_flag & O_CREAT)) {
2627 umode_t mode = op->mode;
2628 if (!IS_POSIXACL(dir->d_inode))
2629 mode &= ~current_umask();
2630 /*
2631 * This write is needed to ensure that a
2632 * rw->ro transition does not occur between
2633 * the time when the file is created and when
2634 * a permanent write count is taken through
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02002635 * the 'struct file' in finish_open().
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002636 */
Al Viro64894cf2012-07-31 00:53:35 +04002637 if (!got_write) {
2638 error = -EROFS;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002639 goto out_dput;
Al Viro64894cf2012-07-31 00:53:35 +04002640 }
Al Viro47237682012-06-10 05:01:45 -04002641 *opened |= FILE_CREATED;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002642 error = security_path_mknod(&nd->path, dentry, mode, 0);
2643 if (error)
2644 goto out_dput;
Al Viro312b63f2012-06-10 18:09:36 -04002645 error = vfs_create(dir->d_inode, dentry, mode,
2646 nd->flags & LOOKUP_EXCL);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002647 if (error)
2648 goto out_dput;
2649 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02002650out_no_open:
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002651 path->dentry = dentry;
2652 path->mnt = nd->path.mnt;
Al Viro2675a4e2012-06-22 12:41:10 +04002653 return 1;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002654
2655out_dput:
2656 dput(dentry);
Al Viro2675a4e2012-06-22 12:41:10 +04002657 return error;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002658}
2659
2660/*
Al Virofe2d35f2011-03-05 22:58:25 -05002661 * Handle the last step of open()
Nick Piggin31e6b012011-01-07 17:49:52 +11002662 */
Al Viro2675a4e2012-06-22 12:41:10 +04002663static int do_last(struct nameidata *nd, struct path *path,
2664 struct file *file, const struct open_flags *op,
Jeff Layton669abf42012-10-10 16:43:10 -04002665 int *opened, struct filename *name)
Al Virofb1cc552009-12-24 01:58:28 -05002666{
Al Viroa1e28032009-12-24 02:12:06 -05002667 struct dentry *dir = nd->path.dentry;
Al Viroca344a892011-03-09 00:36:45 -05002668 int open_flag = op->open_flag;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002669 bool will_truncate = (open_flag & O_TRUNC) != 0;
Al Viro64894cf2012-07-31 00:53:35 +04002670 bool got_write = false;
Al Virobcda7652011-03-13 16:42:14 -04002671 int acc_mode = op->acc_mode;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02002672 struct inode *inode;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002673 bool symlink_ok = false;
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02002674 struct path save_parent = { .dentry = NULL, .mnt = NULL };
2675 bool retried = false;
Al Viro16c2cd72011-02-22 15:50:10 -05002676 int error;
Jeff Layton669abf42012-10-10 16:43:10 -04002677 const char *pathname = name->name;
Al Virofb1cc552009-12-24 01:58:28 -05002678
Al Viroc3e380b2011-02-23 13:39:45 -05002679 nd->flags &= ~LOOKUP_PARENT;
2680 nd->flags |= op->intent;
2681
Al Viro1f36f772009-12-26 10:56:19 -05002682 switch (nd->last_type) {
2683 case LAST_DOTDOT:
Neil Brown176306f2010-05-24 16:57:56 +10002684 case LAST_DOT:
Al Virofe2d35f2011-03-05 22:58:25 -05002685 error = handle_dots(nd, nd->last_type);
2686 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04002687 return error;
Al Viro1f36f772009-12-26 10:56:19 -05002688 /* fallthrough */
Al Viro1f36f772009-12-26 10:56:19 -05002689 case LAST_ROOT:
Al Viro9f1fafe2011-03-25 11:00:12 -04002690 error = complete_walk(nd);
Al Viro16c2cd72011-02-22 15:50:10 -05002691 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04002692 return error;
Jeff Laytonbfcec702012-10-10 15:25:23 -04002693 audit_inode(pathname, nd->path.dentry, 0);
Al Viroca344a892011-03-09 00:36:45 -05002694 if (open_flag & O_CREAT) {
Al Virofe2d35f2011-03-05 22:58:25 -05002695 error = -EISDIR;
Al Viro2675a4e2012-06-22 12:41:10 +04002696 goto out;
Al Virofe2d35f2011-03-05 22:58:25 -05002697 }
Miklos Szeredie83db162012-06-05 15:10:29 +02002698 goto finish_open;
Al Viro1f36f772009-12-26 10:56:19 -05002699 case LAST_BIND:
Al Viro9f1fafe2011-03-25 11:00:12 -04002700 error = complete_walk(nd);
Al Viro16c2cd72011-02-22 15:50:10 -05002701 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04002702 return error;
Jeff Laytonbfcec702012-10-10 15:25:23 -04002703 audit_inode(pathname, dir, 0);
Miklos Szeredie83db162012-06-05 15:10:29 +02002704 goto finish_open;
Al Viro1f36f772009-12-26 10:56:19 -05002705 }
Al Viro67ee3ad2009-12-26 07:01:01 -05002706
Al Viroca344a892011-03-09 00:36:45 -05002707 if (!(open_flag & O_CREAT)) {
Al Virofe2d35f2011-03-05 22:58:25 -05002708 if (nd->last.name[nd->last.len])
2709 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
Al Virobcda7652011-03-13 16:42:14 -04002710 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002711 symlink_ok = true;
Al Virofe2d35f2011-03-05 22:58:25 -05002712 /* we _can_ be in RCU mode here */
Miklos Szeredia1eb3312012-05-21 17:30:07 +02002713 error = lookup_fast(nd, &nd->last, path, &inode);
Miklos Szeredi71574862012-06-05 15:10:14 +02002714 if (likely(!error))
2715 goto finish_lookup;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02002716
Miklos Szeredi71574862012-06-05 15:10:14 +02002717 if (error < 0)
Al Viro2675a4e2012-06-22 12:41:10 +04002718 goto out;
Miklos Szeredi37d7fff2012-06-05 15:10:12 +02002719
Miklos Szeredi71574862012-06-05 15:10:14 +02002720 BUG_ON(nd->inode != dir->d_inode);
Miklos Szeredib6183df2012-06-05 15:10:13 +02002721 } else {
2722 /* create side of things */
2723 /*
2724 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
2725 * has been cleared when we got to the last component we are
2726 * about to look up
2727 */
2728 error = complete_walk(nd);
2729 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04002730 return error;
Miklos Szeredib6183df2012-06-05 15:10:13 +02002731
Jeff Laytonbfcec702012-10-10 15:25:23 -04002732 audit_inode(pathname, dir, 0);
Miklos Szeredib6183df2012-06-05 15:10:13 +02002733 error = -EISDIR;
2734 /* trailing slashes? */
2735 if (nd->last.name[nd->last.len])
Al Viro2675a4e2012-06-22 12:41:10 +04002736 goto out;
Al Virofe2d35f2011-03-05 22:58:25 -05002737 }
2738
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02002739retry_lookup:
Al Viro64894cf2012-07-31 00:53:35 +04002740 if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
2741 error = mnt_want_write(nd->path.mnt);
2742 if (!error)
2743 got_write = true;
2744 /*
2745 * do _not_ fail yet - we might not need that or fail with
2746 * a different error; let lookup_open() decide; we'll be
2747 * dropping this one anyway.
2748 */
2749 }
Al Viroa1e28032009-12-24 02:12:06 -05002750 mutex_lock(&dir->d_inode->i_mutex);
Al Viro64894cf2012-07-31 00:53:35 +04002751 error = lookup_open(nd, path, file, op, got_write, opened);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002752 mutex_unlock(&dir->d_inode->i_mutex);
Al Viroa1e28032009-12-24 02:12:06 -05002753
Al Viro2675a4e2012-06-22 12:41:10 +04002754 if (error <= 0) {
2755 if (error)
Miklos Szeredid18e9002012-06-05 15:10:17 +02002756 goto out;
2757
Al Viro47237682012-06-10 05:01:45 -04002758 if ((*opened & FILE_CREATED) ||
Al Viro2675a4e2012-06-22 12:41:10 +04002759 !S_ISREG(file->f_path.dentry->d_inode->i_mode))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002760 will_truncate = false;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002761
Jeff Laytonbfcec702012-10-10 15:25:23 -04002762 audit_inode(pathname, file->f_path.dentry, 0);
Miklos Szeredid18e9002012-06-05 15:10:17 +02002763 goto opened;
2764 }
Al Virofb1cc552009-12-24 01:58:28 -05002765
Al Viro47237682012-06-10 05:01:45 -04002766 if (*opened & FILE_CREATED) {
Al Viro9b44f1b2011-03-09 00:17:27 -05002767 /* Don't check for write permission, don't truncate */
Al Viroca344a892011-03-09 00:36:45 -05002768 open_flag &= ~O_TRUNC;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002769 will_truncate = false;
Al Virobcda7652011-03-13 16:42:14 -04002770 acc_mode = MAY_OPEN;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02002771 path_to_nameidata(path, nd);
Miklos Szeredie83db162012-06-05 15:10:29 +02002772 goto finish_open_created;
Al Virofb1cc552009-12-24 01:58:28 -05002773 }
2774
2775 /*
Jeff Layton3134f372012-07-25 10:19:47 -04002776 * create/update audit record if it already exists.
Al Virofb1cc552009-12-24 01:58:28 -05002777 */
Jeff Layton3134f372012-07-25 10:19:47 -04002778 if (path->dentry->d_inode)
Jeff Laytonbfcec702012-10-10 15:25:23 -04002779 audit_inode(pathname, path->dentry, 0);
Al Virofb1cc552009-12-24 01:58:28 -05002780
Miklos Szeredid18e9002012-06-05 15:10:17 +02002781 /*
2782 * If atomic_open() acquired write access it is dropped now due to
2783 * possible mount and symlink following (this might be optimized away if
2784 * necessary...)
2785 */
Al Viro64894cf2012-07-31 00:53:35 +04002786 if (got_write) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02002787 mnt_drop_write(nd->path.mnt);
Al Viro64894cf2012-07-31 00:53:35 +04002788 got_write = false;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002789 }
2790
Al Virofb1cc552009-12-24 01:58:28 -05002791 error = -EEXIST;
Al Virof8310c52012-07-30 11:50:30 +04002792 if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))
Al Virofb1cc552009-12-24 01:58:28 -05002793 goto exit_dput;
2794
David Howells9875cf82011-01-14 18:45:21 +00002795 error = follow_managed(path, nd->flags);
2796 if (error < 0)
2797 goto exit_dput;
Al Virofb1cc552009-12-24 01:58:28 -05002798
Al Viroa3fbbde2011-11-07 21:21:26 +00002799 if (error)
2800 nd->flags |= LOOKUP_JUMPED;
2801
Miklos Szeredidecf3402012-05-21 17:30:08 +02002802 BUG_ON(nd->flags & LOOKUP_RCU);
2803 inode = path->dentry->d_inode;
Miklos Szeredi5f5daac2012-05-21 17:30:14 +02002804finish_lookup:
2805 /* we _can_ be in RCU mode here */
Al Virofb1cc552009-12-24 01:58:28 -05002806 error = -ENOENT;
Miklos Szeredi54c33e72012-05-21 17:30:10 +02002807 if (!inode) {
2808 path_to_nameidata(path, nd);
Al Viro2675a4e2012-06-22 12:41:10 +04002809 goto out;
Miklos Szeredi54c33e72012-05-21 17:30:10 +02002810 }
Al Viro9e67f362009-12-26 07:04:50 -05002811
Miklos Szeredid45ea862012-05-21 17:30:09 +02002812 if (should_follow_link(inode, !symlink_ok)) {
2813 if (nd->flags & LOOKUP_RCU) {
2814 if (unlikely(unlazy_walk(nd, path->dentry))) {
2815 error = -ECHILD;
Al Viro2675a4e2012-06-22 12:41:10 +04002816 goto out;
Miklos Szeredid45ea862012-05-21 17:30:09 +02002817 }
2818 }
2819 BUG_ON(inode != path->dentry->d_inode);
Al Viro2675a4e2012-06-22 12:41:10 +04002820 return 1;
Miklos Szeredid45ea862012-05-21 17:30:09 +02002821 }
Al Virofb1cc552009-12-24 01:58:28 -05002822
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02002823 if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) {
2824 path_to_nameidata(path, nd);
2825 } else {
2826 save_parent.dentry = nd->path.dentry;
2827 save_parent.mnt = mntget(path->mnt);
2828 nd->path.dentry = path->dentry;
2829
2830 }
Miklos Szeredidecf3402012-05-21 17:30:08 +02002831 nd->inode = inode;
Al Viroa3fbbde2011-11-07 21:21:26 +00002832 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
2833 error = complete_walk(nd);
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02002834 if (error) {
2835 path_put(&save_parent);
Al Viro2675a4e2012-06-22 12:41:10 +04002836 return error;
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02002837 }
Al Virofb1cc552009-12-24 01:58:28 -05002838 error = -EISDIR;
Miklos Szeredi050ac842012-05-21 17:30:12 +02002839 if ((open_flag & O_CREAT) && S_ISDIR(nd->inode->i_mode))
Al Viro2675a4e2012-06-22 12:41:10 +04002840 goto out;
Miklos Szerediaf2f5542012-05-21 17:30:11 +02002841 error = -ENOTDIR;
2842 if ((nd->flags & LOOKUP_DIRECTORY) && !nd->inode->i_op->lookup)
Al Viro2675a4e2012-06-22 12:41:10 +04002843 goto out;
Jeff Laytonbfcec702012-10-10 15:25:23 -04002844 audit_inode(pathname, nd->path.dentry, 0);
Miklos Szeredie83db162012-06-05 15:10:29 +02002845finish_open:
Al Viro6c0d46c2011-03-09 00:59:59 -05002846 if (!S_ISREG(nd->inode->i_mode))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02002847 will_truncate = false;
Al Viro6c0d46c2011-03-09 00:59:59 -05002848
Al Viro0f9d1a12011-03-09 00:13:14 -05002849 if (will_truncate) {
2850 error = mnt_want_write(nd->path.mnt);
2851 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04002852 goto out;
Al Viro64894cf2012-07-31 00:53:35 +04002853 got_write = true;
Al Viro0f9d1a12011-03-09 00:13:14 -05002854 }
Miklos Szeredie83db162012-06-05 15:10:29 +02002855finish_open_created:
Al Virobcda7652011-03-13 16:42:14 -04002856 error = may_open(&nd->path, acc_mode, open_flag);
Al Viroca344a892011-03-09 00:36:45 -05002857 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04002858 goto out;
Al Viro30d90492012-06-22 12:40:19 +04002859 file->f_path.mnt = nd->path.mnt;
2860 error = finish_open(file, nd->path.dentry, NULL, opened);
2861 if (error) {
Al Viro30d90492012-06-22 12:40:19 +04002862 if (error == -EOPENSTALE)
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02002863 goto stale_open;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02002864 goto out;
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02002865 }
Miklos Szeredia8277b92012-06-05 15:10:32 +02002866opened:
Al Viro2675a4e2012-06-22 12:41:10 +04002867 error = open_check_o_direct(file);
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02002868 if (error)
2869 goto exit_fput;
Al Viro2675a4e2012-06-22 12:41:10 +04002870 error = ima_file_check(file, op->acc_mode);
Miklos Szerediaa4caad2012-06-05 15:10:28 +02002871 if (error)
2872 goto exit_fput;
2873
2874 if (will_truncate) {
Al Viro2675a4e2012-06-22 12:41:10 +04002875 error = handle_truncate(file);
Miklos Szerediaa4caad2012-06-05 15:10:28 +02002876 if (error)
2877 goto exit_fput;
Al Viro0f9d1a12011-03-09 00:13:14 -05002878 }
Al Viroca344a892011-03-09 00:36:45 -05002879out:
Al Viro64894cf2012-07-31 00:53:35 +04002880 if (got_write)
Al Viro0f9d1a12011-03-09 00:13:14 -05002881 mnt_drop_write(nd->path.mnt);
Miklos Szeredi16b1c1c2012-05-21 17:30:19 +02002882 path_put(&save_parent);
Miklos Szeredie276ae62012-05-21 17:30:06 +02002883 terminate_walk(nd);
Al Viro2675a4e2012-06-22 12:41:10 +04002884 return error;
Al Virofb1cc552009-12-24 01:58:28 -05002885
Al Virofb1cc552009-12-24 01:58:28 -05002886exit_dput:
2887 path_put_conditional(path, nd);
Al Viroca344a892011-03-09 00:36:45 -05002888 goto out;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02002889exit_fput:
Al Viro2675a4e2012-06-22 12:41:10 +04002890 fput(file);
2891 goto out;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02002892
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02002893stale_open:
2894 /* If no saved parent or already retried then can't retry */
2895 if (!save_parent.dentry || retried)
2896 goto out;
2897
2898 BUG_ON(save_parent.dentry != dir);
2899 path_put(&nd->path);
2900 nd->path = save_parent;
2901 nd->inode = dir->d_inode;
2902 save_parent.mnt = NULL;
2903 save_parent.dentry = NULL;
Al Viro64894cf2012-07-31 00:53:35 +04002904 if (got_write) {
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02002905 mnt_drop_write(nd->path.mnt);
Al Viro64894cf2012-07-31 00:53:35 +04002906 got_write = false;
Miklos Szeredif60dc3d2012-06-05 15:10:31 +02002907 }
2908 retried = true;
2909 goto retry_lookup;
Al Virofb1cc552009-12-24 01:58:28 -05002910}
2911
Jeff Layton669abf42012-10-10 16:43:10 -04002912static struct file *path_openat(int dfd, struct filename *pathname,
Al Viro73d049a2011-03-11 12:08:24 -05002913 struct nameidata *nd, const struct open_flags *op, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914{
Al Virofe2d35f2011-03-05 22:58:25 -05002915 struct file *base = NULL;
Al Viro30d90492012-06-22 12:40:19 +04002916 struct file *file;
Al Viro9850c052010-01-13 15:01:15 -05002917 struct path path;
Al Viro47237682012-06-10 05:01:45 -04002918 int opened = 0;
Al Viro13aab422011-02-23 17:54:08 -05002919 int error;
Nick Piggin31e6b012011-01-07 17:49:52 +11002920
Al Viro30d90492012-06-22 12:40:19 +04002921 file = get_empty_filp();
2922 if (!file)
Nick Piggin31e6b012011-01-07 17:49:52 +11002923 return ERR_PTR(-ENFILE);
2924
Al Viro30d90492012-06-22 12:40:19 +04002925 file->f_flags = op->open_flag;
Nick Piggin31e6b012011-01-07 17:49:52 +11002926
Jeff Layton669abf42012-10-10 16:43:10 -04002927 error = path_init(dfd, pathname->name, flags | LOOKUP_PARENT, nd, &base);
Nick Piggin31e6b012011-01-07 17:49:52 +11002928 if (unlikely(error))
Al Viro2675a4e2012-06-22 12:41:10 +04002929 goto out;
Nick Piggin31e6b012011-01-07 17:49:52 +11002930
Al Virofe2d35f2011-03-05 22:58:25 -05002931 current->total_link_count = 0;
Jeff Layton669abf42012-10-10 16:43:10 -04002932 error = link_path_walk(pathname->name, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11002933 if (unlikely(error))
Al Viro2675a4e2012-06-22 12:41:10 +04002934 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935
Al Viro2675a4e2012-06-22 12:41:10 +04002936 error = do_last(nd, &path, file, op, &opened, pathname);
2937 while (unlikely(error > 0)) { /* trailing symlink */
Nick Piggin7b9337a2011-01-14 08:42:43 +00002938 struct path link = path;
Al Virodef4af32009-12-26 08:37:05 -05002939 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04002940 if (!(nd->flags & LOOKUP_FOLLOW)) {
Al Viro73d049a2011-03-11 12:08:24 -05002941 path_put_conditional(&path, nd);
2942 path_put(&nd->path);
Al Viro2675a4e2012-06-22 12:41:10 +04002943 error = -ELOOP;
Al Viro40b39132011-03-09 16:22:18 -05002944 break;
2945 }
Kees Cook800179c2012-07-25 17:29:07 -07002946 error = may_follow_link(&link, nd);
2947 if (unlikely(error))
2948 break;
Al Viro73d049a2011-03-11 12:08:24 -05002949 nd->flags |= LOOKUP_PARENT;
2950 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
Al Viro574197e2011-03-14 22:20:34 -04002951 error = follow_link(&link, nd, &cookie);
Al Viroc3e380b2011-02-23 13:39:45 -05002952 if (unlikely(error))
Al Viro2675a4e2012-06-22 12:41:10 +04002953 break;
2954 error = do_last(nd, &path, file, op, &opened, pathname);
Al Viro574197e2011-03-14 22:20:34 -04002955 put_link(nd, &link, cookie);
Al Viro806b6812009-12-26 07:16:40 -05002956 }
Al Viro10fa8e62009-12-26 07:09:49 -05002957out:
Al Viro73d049a2011-03-11 12:08:24 -05002958 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
2959 path_put(&nd->root);
Al Virofe2d35f2011-03-05 22:58:25 -05002960 if (base)
2961 fput(base);
Al Viro2675a4e2012-06-22 12:41:10 +04002962 if (!(opened & FILE_OPENED)) {
2963 BUG_ON(!error);
Al Viro30d90492012-06-22 12:40:19 +04002964 put_filp(file);
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02002965 }
Al Viro2675a4e2012-06-22 12:41:10 +04002966 if (unlikely(error)) {
2967 if (error == -EOPENSTALE) {
2968 if (flags & LOOKUP_RCU)
2969 error = -ECHILD;
2970 else
2971 error = -ESTALE;
2972 }
2973 file = ERR_PTR(error);
2974 }
2975 return file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976}
2977
Jeff Layton669abf42012-10-10 16:43:10 -04002978struct file *do_filp_open(int dfd, struct filename *pathname,
Al Viro13aab422011-02-23 17:54:08 -05002979 const struct open_flags *op, int flags)
2980{
Al Viro73d049a2011-03-11 12:08:24 -05002981 struct nameidata nd;
Al Viro13aab422011-02-23 17:54:08 -05002982 struct file *filp;
2983
Al Viro73d049a2011-03-11 12:08:24 -05002984 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
Al Viro13aab422011-02-23 17:54:08 -05002985 if (unlikely(filp == ERR_PTR(-ECHILD)))
Al Viro73d049a2011-03-11 12:08:24 -05002986 filp = path_openat(dfd, pathname, &nd, op, flags);
Al Viro13aab422011-02-23 17:54:08 -05002987 if (unlikely(filp == ERR_PTR(-ESTALE)))
Al Viro73d049a2011-03-11 12:08:24 -05002988 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
Al Viro13aab422011-02-23 17:54:08 -05002989 return filp;
2990}
2991
Al Viro73d049a2011-03-11 12:08:24 -05002992struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
2993 const char *name, const struct open_flags *op, int flags)
2994{
2995 struct nameidata nd;
2996 struct file *file;
Jeff Layton669abf42012-10-10 16:43:10 -04002997 struct filename filename = { .name = name };
Al Viro73d049a2011-03-11 12:08:24 -05002998
2999 nd.root.mnt = mnt;
3000 nd.root.dentry = dentry;
3001
3002 flags |= LOOKUP_ROOT;
3003
Al Virobcda7652011-03-13 16:42:14 -04003004 if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
Al Viro73d049a2011-03-11 12:08:24 -05003005 return ERR_PTR(-ELOOP);
3006
Jeff Layton669abf42012-10-10 16:43:10 -04003007 file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_RCU);
Al Viro73d049a2011-03-11 12:08:24 -05003008 if (unlikely(file == ERR_PTR(-ECHILD)))
Jeff Layton669abf42012-10-10 16:43:10 -04003009 file = path_openat(-1, &filename, &nd, op, flags);
Al Viro73d049a2011-03-11 12:08:24 -05003010 if (unlikely(file == ERR_PTR(-ESTALE)))
Jeff Layton669abf42012-10-10 16:43:10 -04003011 file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_REVAL);
Al Viro73d049a2011-03-11 12:08:24 -05003012 return file;
3013}
3014
Al Viroed75e952011-06-27 16:53:43 -04003015struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003017 struct dentry *dentry = ERR_PTR(-EEXIST);
Al Viroed75e952011-06-27 16:53:43 -04003018 struct nameidata nd;
Jan Karac30dabf2012-06-12 16:20:30 +02003019 int err2;
Al Viroed75e952011-06-27 16:53:43 -04003020 int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
3021 if (error)
3022 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003024 /*
3025 * Yucky last component or no last component at all?
3026 * (foo/., foo/.., /////)
3027 */
Al Viroed75e952011-06-27 16:53:43 -04003028 if (nd.last_type != LAST_NORM)
3029 goto out;
3030 nd.flags &= ~LOOKUP_PARENT;
3031 nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003032
Jan Karac30dabf2012-06-12 16:20:30 +02003033 /* don't fail immediately if it's r/o, at least try to report other errors */
3034 err2 = mnt_want_write(nd.path.mnt);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003035 /*
3036 * Do the final lookup.
3037 */
Al Viroed75e952011-06-27 16:53:43 -04003038 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3039 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 if (IS_ERR(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003041 goto unlock;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003042
Al Viroa8104a92012-07-20 02:25:00 +04003043 error = -EEXIST;
Al Viroe9baf6e2008-05-15 04:49:12 -04003044 if (dentry->d_inode)
Al Viroa8104a92012-07-20 02:25:00 +04003045 goto fail;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003046 /*
3047 * Special case - lookup gave negative, but... we had foo/bar/
3048 * From the vfs_mknod() POV we just have a negative dentry -
3049 * all is fine. Let's be bastards - you had / on the end, you've
3050 * been asking for (non-existent) directory. -ENOENT for you.
3051 */
Al Viroed75e952011-06-27 16:53:43 -04003052 if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
Al Viroa8104a92012-07-20 02:25:00 +04003053 error = -ENOENT;
Al Viroed75e952011-06-27 16:53:43 -04003054 goto fail;
Al Viroe9baf6e2008-05-15 04:49:12 -04003055 }
Jan Karac30dabf2012-06-12 16:20:30 +02003056 if (unlikely(err2)) {
3057 error = err2;
Al Viroa8104a92012-07-20 02:25:00 +04003058 goto fail;
Jan Karac30dabf2012-06-12 16:20:30 +02003059 }
Al Viroed75e952011-06-27 16:53:43 -04003060 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062fail:
Al Viroa8104a92012-07-20 02:25:00 +04003063 dput(dentry);
3064 dentry = ERR_PTR(error);
3065unlock:
Al Viroed75e952011-06-27 16:53:43 -04003066 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Karac30dabf2012-06-12 16:20:30 +02003067 if (!err2)
3068 mnt_drop_write(nd.path.mnt);
Al Viroed75e952011-06-27 16:53:43 -04003069out:
3070 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 return dentry;
3072}
Al Virodae6ad82011-06-26 11:50:15 -04003073EXPORT_SYMBOL(kern_path_create);
3074
Al Viro921a1652012-07-20 01:15:31 +04003075void done_path_create(struct path *path, struct dentry *dentry)
3076{
3077 dput(dentry);
3078 mutex_unlock(&path->dentry->d_inode->i_mutex);
Al Viroa8104a92012-07-20 02:25:00 +04003079 mnt_drop_write(path->mnt);
Al Viro921a1652012-07-20 01:15:31 +04003080 path_put(path);
3081}
3082EXPORT_SYMBOL(done_path_create);
3083
Al Virodae6ad82011-06-26 11:50:15 -04003084struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir)
3085{
Jeff Layton91a27b22012-10-10 15:25:28 -04003086 struct filename *tmp = getname(pathname);
Al Virodae6ad82011-06-26 11:50:15 -04003087 struct dentry *res;
3088 if (IS_ERR(tmp))
3089 return ERR_CAST(tmp);
Jeff Layton91a27b22012-10-10 15:25:28 -04003090 res = kern_path_create(dfd, tmp->name, path, is_dir);
Al Virodae6ad82011-06-26 11:50:15 -04003091 putname(tmp);
3092 return res;
3093}
3094EXPORT_SYMBOL(user_path_create);
3095
Al Viro1a67aaf2011-07-26 01:52:52 -04003096int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003098 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099
3100 if (error)
3101 return error;
3102
Eric W. Biederman975d6b32011-11-13 12:16:43 -08003103 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 return -EPERM;
3105
Al Viroacfa4382008-12-04 10:06:33 -05003106 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 return -EPERM;
3108
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07003109 error = devcgroup_inode_mknod(mode, dev);
3110 if (error)
3111 return error;
3112
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 error = security_inode_mknod(dir, dentry, mode, dev);
3114 if (error)
3115 return error;
3116
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003118 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003119 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 return error;
3121}
3122
Al Virof69aac02011-07-26 04:31:40 -04003123static int may_mknod(umode_t mode)
Dave Hansen463c3192008-02-15 14:37:57 -08003124{
3125 switch (mode & S_IFMT) {
3126 case S_IFREG:
3127 case S_IFCHR:
3128 case S_IFBLK:
3129 case S_IFIFO:
3130 case S_IFSOCK:
3131 case 0: /* zero mode translates to S_IFREG */
3132 return 0;
3133 case S_IFDIR:
3134 return -EPERM;
3135 default:
3136 return -EINVAL;
3137 }
3138}
3139
Al Viro8208a222011-07-25 17:32:17 -04003140SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003141 unsigned, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142{
Al Viro2ad94ae2008-07-21 09:32:51 -04003143 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003144 struct path path;
3145 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146
Al Viro8e4bfca2012-07-20 01:17:26 +04003147 error = may_mknod(mode);
3148 if (error)
3149 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150
Al Virodae6ad82011-06-26 11:50:15 -04003151 dentry = user_path_create(dfd, filename, &path, 0);
3152 if (IS_ERR(dentry))
3153 return PTR_ERR(dentry);
Al Viro2ad94ae2008-07-21 09:32:51 -04003154
Al Virodae6ad82011-06-26 11:50:15 -04003155 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003156 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003157 error = security_path_mknod(&path, dentry, mode, dev);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003158 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04003159 goto out;
Dave Hansen463c3192008-02-15 14:37:57 -08003160 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 case 0: case S_IFREG:
Al Viro312b63f2012-06-10 18:09:36 -04003162 error = vfs_create(path.dentry->d_inode,dentry,mode,true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 break;
3164 case S_IFCHR: case S_IFBLK:
Al Virodae6ad82011-06-26 11:50:15 -04003165 error = vfs_mknod(path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 new_decode_dev(dev));
3167 break;
3168 case S_IFIFO: case S_IFSOCK:
Al Virodae6ad82011-06-26 11:50:15 -04003169 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 }
Al Viroa8104a92012-07-20 02:25:00 +04003172out:
Al Viro921a1652012-07-20 01:15:31 +04003173 done_path_create(&path, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 return error;
3175}
3176
Al Viro8208a222011-07-25 17:32:17 -04003177SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003178{
3179 return sys_mknodat(AT_FDCWD, filename, mode, dev);
3180}
3181
Al Viro18bb1db2011-07-26 01:41:39 -04003182int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003184 int error = may_create(dir, dentry);
Al Viro8de52772012-02-06 12:45:27 -05003185 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186
3187 if (error)
3188 return error;
3189
Al Viroacfa4382008-12-04 10:06:33 -05003190 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 return -EPERM;
3192
3193 mode &= (S_IRWXUGO|S_ISVTX);
3194 error = security_inode_mkdir(dir, dentry, mode);
3195 if (error)
3196 return error;
3197
Al Viro8de52772012-02-06 12:45:27 -05003198 if (max_links && dir->i_nlink >= max_links)
3199 return -EMLINK;
3200
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003202 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003203 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 return error;
3205}
3206
Al Viroa218d0f2011-11-21 14:59:34 -05003207SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208{
Dave Hansen6902d922006-09-30 23:29:01 -07003209 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003210 struct path path;
3211 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212
Al Virodae6ad82011-06-26 11:50:15 -04003213 dentry = user_path_create(dfd, pathname, &path, 1);
Dave Hansen6902d922006-09-30 23:29:01 -07003214 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003215 return PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003216
Al Virodae6ad82011-06-26 11:50:15 -04003217 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003218 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003219 error = security_path_mkdir(&path, dentry, mode);
Al Viroa8104a92012-07-20 02:25:00 +04003220 if (!error)
3221 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
Al Viro921a1652012-07-20 01:15:31 +04003222 done_path_create(&path, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 return error;
3224}
3225
Al Viroa218d0f2011-11-21 14:59:34 -05003226SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003227{
3228 return sys_mkdirat(AT_FDCWD, pathname, mode);
3229}
3230
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231/*
Sage Weila71905f2011-05-24 13:06:08 -07003232 * The dentry_unhash() helper will try to drop the dentry early: we
J. Bruce Fieldsc0d02592012-02-15 11:48:40 -05003233 * should have a usage count of 1 if we're the only user of this
Sage Weila71905f2011-05-24 13:06:08 -07003234 * dentry, and if that is true (possibly after pruning the dcache),
3235 * then we drop the dentry now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 *
3237 * A low-level filesystem can, if it choses, legally
3238 * do a
3239 *
3240 * if (!d_unhashed(dentry))
3241 * return -EBUSY;
3242 *
3243 * if it cannot handle the case of removing a directory
3244 * that is still in use by something else..
3245 */
3246void dentry_unhash(struct dentry *dentry)
3247{
Vasily Averindc168422006-12-06 20:37:07 -08003248 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 spin_lock(&dentry->d_lock);
Sage Weil64252c72011-05-24 13:06:05 -07003250 if (dentry->d_count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 __d_drop(dentry);
3252 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253}
3254
3255int vfs_rmdir(struct inode *dir, struct dentry *dentry)
3256{
3257 int error = may_delete(dir, dentry, 1);
3258
3259 if (error)
3260 return error;
3261
Al Viroacfa4382008-12-04 10:06:33 -05003262 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 return -EPERM;
3264
Al Viro1d2ef592011-09-14 18:55:41 +01003265 dget(dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003266 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267
Sage Weil912dbc12011-05-24 13:06:11 -07003268 error = -EBUSY;
3269 if (d_mountpoint(dentry))
3270 goto out;
3271
3272 error = security_inode_rmdir(dir, dentry);
3273 if (error)
3274 goto out;
3275
Sage Weil3cebde22011-05-29 21:20:59 -07003276 shrink_dcache_parent(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003277 error = dir->i_op->rmdir(dir, dentry);
3278 if (error)
3279 goto out;
3280
3281 dentry->d_inode->i_flags |= S_DEAD;
3282 dont_mount(dentry);
3283
3284out:
3285 mutex_unlock(&dentry->d_inode->i_mutex);
Al Viro1d2ef592011-09-14 18:55:41 +01003286 dput(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003287 if (!error)
3288 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 return error;
3290}
3291
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003292static long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293{
3294 int error = 0;
Jeff Layton91a27b22012-10-10 15:25:28 -04003295 struct filename *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 struct dentry *dentry;
3297 struct nameidata nd;
3298
Jeff Layton91a27b22012-10-10 15:25:28 -04003299 name = user_path_parent(dfd, pathname, &nd);
3300 if (IS_ERR(name))
3301 return PTR_ERR(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302
3303 switch(nd.last_type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003304 case LAST_DOTDOT:
3305 error = -ENOTEMPTY;
3306 goto exit1;
3307 case LAST_DOT:
3308 error = -EINVAL;
3309 goto exit1;
3310 case LAST_ROOT:
3311 error = -EBUSY;
3312 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003314
3315 nd.flags &= ~LOOKUP_PARENT;
Jan Karac30dabf2012-06-12 16:20:30 +02003316 error = mnt_want_write(nd.path.mnt);
3317 if (error)
3318 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003319
Jan Blunck4ac91372008-02-14 19:34:32 -08003320 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08003321 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003323 if (IS_ERR(dentry))
3324 goto exit2;
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04003325 if (!dentry->d_inode) {
3326 error = -ENOENT;
3327 goto exit3;
3328 }
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003329 error = security_path_rmdir(&nd.path, dentry);
3330 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02003331 goto exit3;
Jan Blunck4ac91372008-02-14 19:34:32 -08003332 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
Dave Hansen06227532008-02-15 14:37:34 -08003333exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07003334 dput(dentry);
3335exit2:
Jan Blunck4ac91372008-02-14 19:34:32 -08003336 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Karac30dabf2012-06-12 16:20:30 +02003337 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003339 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 putname(name);
3341 return error;
3342}
3343
Heiko Carstens3cdad422009-01-14 14:14:22 +01003344SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003345{
3346 return do_rmdir(AT_FDCWD, pathname);
3347}
3348
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349int vfs_unlink(struct inode *dir, struct dentry *dentry)
3350{
3351 int error = may_delete(dir, dentry, 0);
3352
3353 if (error)
3354 return error;
3355
Al Viroacfa4382008-12-04 10:06:33 -05003356 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 return -EPERM;
3358
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003359 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 if (d_mountpoint(dentry))
3361 error = -EBUSY;
3362 else {
3363 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05003364 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 error = dir->i_op->unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05003366 if (!error)
Al Virod83c49f2010-04-30 17:17:09 -04003367 dont_mount(dentry);
Al Virobec10522010-03-03 14:12:08 -05003368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003370 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371
3372 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
3373 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
Jan Karaece95912008-02-06 01:37:13 -08003374 fsnotify_link_count(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 }
Robert Love0eeca282005-07-12 17:06:03 -04003377
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 return error;
3379}
3380
3381/*
3382 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003383 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 * writeout happening, and we don't want to prevent access to the directory
3385 * while waiting on the I/O.
3386 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003387static long do_unlinkat(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388{
Al Viro2ad94ae2008-07-21 09:32:51 -04003389 int error;
Jeff Layton91a27b22012-10-10 15:25:28 -04003390 struct filename *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 struct dentry *dentry;
3392 struct nameidata nd;
3393 struct inode *inode = NULL;
3394
Jeff Layton91a27b22012-10-10 15:25:28 -04003395 name = user_path_parent(dfd, pathname, &nd);
3396 if (IS_ERR(name))
3397 return PTR_ERR(name);
Al Viro2ad94ae2008-07-21 09:32:51 -04003398
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 error = -EISDIR;
3400 if (nd.last_type != LAST_NORM)
3401 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003402
3403 nd.flags &= ~LOOKUP_PARENT;
Jan Karac30dabf2012-06-12 16:20:30 +02003404 error = mnt_want_write(nd.path.mnt);
3405 if (error)
3406 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003407
Jan Blunck4ac91372008-02-14 19:34:32 -08003408 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08003409 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 error = PTR_ERR(dentry);
3411 if (!IS_ERR(dentry)) {
3412 /* Why not before? Because we want correct error value */
Török Edwin50338b82011-06-16 00:06:14 +03003413 if (nd.last.name[nd.last.len])
3414 goto slashes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 inode = dentry->d_inode;
Török Edwin50338b82011-06-16 00:06:14 +03003416 if (!inode)
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04003417 goto slashes;
3418 ihold(inode);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003419 error = security_path_unlink(&nd.path, dentry);
3420 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02003421 goto exit2;
Jan Blunck4ac91372008-02-14 19:34:32 -08003422 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
Jan Karac30dabf2012-06-12 16:20:30 +02003423exit2:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 dput(dentry);
3425 }
Jan Blunck4ac91372008-02-14 19:34:32 -08003426 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 if (inode)
3428 iput(inode); /* truncate the inode here */
Jan Karac30dabf2012-06-12 16:20:30 +02003429 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003431 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 putname(name);
3433 return error;
3434
3435slashes:
3436 error = !dentry->d_inode ? -ENOENT :
3437 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
3438 goto exit2;
3439}
3440
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003441SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003442{
3443 if ((flag & ~AT_REMOVEDIR) != 0)
3444 return -EINVAL;
3445
3446 if (flag & AT_REMOVEDIR)
3447 return do_rmdir(dfd, pathname);
3448
3449 return do_unlinkat(dfd, pathname);
3450}
3451
Heiko Carstens3480b252009-01-14 14:14:16 +01003452SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003453{
3454 return do_unlinkat(AT_FDCWD, pathname);
3455}
3456
Miklos Szeredidb2e7472008-06-24 16:50:16 +02003457int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003459 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460
3461 if (error)
3462 return error;
3463
Al Viroacfa4382008-12-04 10:06:33 -05003464 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 return -EPERM;
3466
3467 error = security_inode_symlink(dir, dentry, oldname);
3468 if (error)
3469 return error;
3470
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003472 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003473 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 return error;
3475}
3476
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003477SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
3478 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479{
Al Viro2ad94ae2008-07-21 09:32:51 -04003480 int error;
Jeff Layton91a27b22012-10-10 15:25:28 -04003481 struct filename *from;
Dave Hansen6902d922006-09-30 23:29:01 -07003482 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003483 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484
3485 from = getname(oldname);
Al Viro2ad94ae2008-07-21 09:32:51 -04003486 if (IS_ERR(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 return PTR_ERR(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04003488
Al Virodae6ad82011-06-26 11:50:15 -04003489 dentry = user_path_create(newdfd, newname, &path, 0);
Dave Hansen6902d922006-09-30 23:29:01 -07003490 error = PTR_ERR(dentry);
3491 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003492 goto out_putname;
Dave Hansen6902d922006-09-30 23:29:01 -07003493
Jeff Layton91a27b22012-10-10 15:25:28 -04003494 error = security_path_symlink(&path, dentry, from->name);
Al Viroa8104a92012-07-20 02:25:00 +04003495 if (!error)
Jeff Layton91a27b22012-10-10 15:25:28 -04003496 error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
Al Viro921a1652012-07-20 01:15:31 +04003497 done_path_create(&path, dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003498out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 putname(from);
3500 return error;
3501}
3502
Heiko Carstens3480b252009-01-14 14:14:16 +01003503SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003504{
3505 return sys_symlinkat(oldname, AT_FDCWD, newname);
3506}
3507
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
3509{
3510 struct inode *inode = old_dentry->d_inode;
Al Viro8de52772012-02-06 12:45:27 -05003511 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 int error;
3513
3514 if (!inode)
3515 return -ENOENT;
3516
Miklos Szeredia95164d2008-07-30 15:08:48 +02003517 error = may_create(dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 if (error)
3519 return error;
3520
3521 if (dir->i_sb != inode->i_sb)
3522 return -EXDEV;
3523
3524 /*
3525 * A link to an append-only or immutable file cannot be created.
3526 */
3527 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3528 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05003529 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003531 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 return -EPERM;
3533
3534 error = security_inode_link(old_dentry, dir, new_dentry);
3535 if (error)
3536 return error;
3537
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003538 mutex_lock(&inode->i_mutex);
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05303539 /* Make sure we don't allow creating hardlink to an unlinked file */
3540 if (inode->i_nlink == 0)
3541 error = -ENOENT;
Al Viro8de52772012-02-06 12:45:27 -05003542 else if (max_links && inode->i_nlink >= max_links)
3543 error = -EMLINK;
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05303544 else
3545 error = dir->i_op->link(old_dentry, dir, new_dentry);
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003546 mutex_unlock(&inode->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07003547 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003548 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 return error;
3550}
3551
3552/*
3553 * Hardlinks are often used in delicate situations. We avoid
3554 * security-related surprises by not following symlinks on the
3555 * newname. --KAB
3556 *
3557 * We don't follow them on the oldname either to be compatible
3558 * with linux 2.0, and to avoid hard-linking to directories
3559 * and other special files. --ADM
3560 */
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003561SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
3562 int, newdfd, const char __user *, newname, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563{
3564 struct dentry *new_dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003565 struct path old_path, new_path;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303566 int how = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303569 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003570 return -EINVAL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303571 /*
3572 * To use null names we require CAP_DAC_READ_SEARCH
3573 * This ensures that not everyone will be able to create
3574 * handlink using the passed filedescriptor.
3575 */
3576 if (flags & AT_EMPTY_PATH) {
3577 if (!capable(CAP_DAC_READ_SEARCH))
3578 return -ENOENT;
3579 how = LOOKUP_EMPTY;
3580 }
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003581
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303582 if (flags & AT_SYMLINK_FOLLOW)
3583 how |= LOOKUP_FOLLOW;
3584
3585 error = user_path_at(olddfd, oldname, how, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04003587 return error;
3588
Al Virodae6ad82011-06-26 11:50:15 -04003589 new_dentry = user_path_create(newdfd, newname, &new_path, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003591 if (IS_ERR(new_dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003592 goto out;
3593
3594 error = -EXDEV;
3595 if (old_path.mnt != new_path.mnt)
3596 goto out_dput;
Kees Cook800179c2012-07-25 17:29:07 -07003597 error = may_linkat(&old_path);
3598 if (unlikely(error))
3599 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04003600 error = security_path_link(old_path.dentry, &new_path, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003601 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04003602 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04003603 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
Dave Hansen75c3f292008-02-15 14:37:45 -08003604out_dput:
Al Viro921a1652012-07-20 01:15:31 +04003605 done_path_create(&new_path, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606out:
Al Viro2d8f3032008-07-22 09:59:21 -04003607 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608
3609 return error;
3610}
3611
Heiko Carstens3480b252009-01-14 14:14:16 +01003612SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003613{
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003614 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003615}
3616
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617/*
3618 * The worst of all namespace operations - renaming directory. "Perverted"
3619 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
3620 * Problems:
3621 * a) we can get into loop creation. Check is done in is_subdir().
3622 * b) race potential - two innocent renames can create a loop together.
3623 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08003624 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 * story.
3626 * c) we have to lock _three_ objects - parents and victim (if it exists).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003627 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 * whether the target exists). Solution: try to be smart with locking
3629 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08003630 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 * move will be locked. Thus we can rank directories by the tree
3632 * (ancestors first) and rank all non-directories after them.
3633 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08003634 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 * HOWEVER, it relies on the assumption that any object with ->lookup()
3636 * has no more than 1 dentry. If "hybrid" objects will ever appear,
3637 * we'd better make sure that there's no link(2) for them.
Sage Weile4eaac02011-05-24 13:06:07 -07003638 * d) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003639 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05003641 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 * locking].
3643 */
Adrian Bunk75c96f82005-05-05 16:16:09 -07003644static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
3645 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646{
3647 int error = 0;
Sage Weil9055cba2011-05-24 13:06:12 -07003648 struct inode *target = new_dentry->d_inode;
Al Viro8de52772012-02-06 12:45:27 -05003649 unsigned max_links = new_dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650
3651 /*
3652 * If we are going to change the parent - check write permissions,
3653 * we'll need to flip '..'.
3654 */
3655 if (new_dir != old_dir) {
Al Virof419a2e2008-07-22 00:07:17 -04003656 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 if (error)
3658 return error;
3659 }
3660
3661 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3662 if (error)
3663 return error;
3664
Al Viro1d2ef592011-09-14 18:55:41 +01003665 dget(new_dentry);
Al Virod83c49f2010-04-30 17:17:09 -04003666 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003667 mutex_lock(&target->i_mutex);
Sage Weil9055cba2011-05-24 13:06:12 -07003668
3669 error = -EBUSY;
3670 if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
3671 goto out;
3672
Al Viro8de52772012-02-06 12:45:27 -05003673 error = -EMLINK;
3674 if (max_links && !target && new_dir != old_dir &&
3675 new_dir->i_nlink >= max_links)
3676 goto out;
3677
Sage Weil3cebde22011-05-29 21:20:59 -07003678 if (target)
3679 shrink_dcache_parent(new_dentry);
Sage Weil9055cba2011-05-24 13:06:12 -07003680 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3681 if (error)
3682 goto out;
3683
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 if (target) {
Sage Weil9055cba2011-05-24 13:06:12 -07003685 target->i_flags |= S_DEAD;
3686 dont_mount(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 }
Sage Weil9055cba2011-05-24 13:06:12 -07003688out:
3689 if (target)
3690 mutex_unlock(&target->i_mutex);
Al Viro1d2ef592011-09-14 18:55:41 +01003691 dput(new_dentry);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07003692 if (!error)
Mark Fasheh349457c2006-09-08 14:22:21 -07003693 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3694 d_move(old_dentry,new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 return error;
3696}
3697
Adrian Bunk75c96f82005-05-05 16:16:09 -07003698static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
3699 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700{
Sage Weil51892bb2011-05-24 13:06:13 -07003701 struct inode *target = new_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 int error;
3703
3704 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3705 if (error)
3706 return error;
3707
3708 dget(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003710 mutex_lock(&target->i_mutex);
Sage Weil51892bb2011-05-24 13:06:13 -07003711
3712 error = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
Sage Weil51892bb2011-05-24 13:06:13 -07003714 goto out;
3715
3716 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3717 if (error)
3718 goto out;
3719
3720 if (target)
3721 dont_mount(new_dentry);
3722 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3723 d_move(old_dentry, new_dentry);
3724out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003726 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 dput(new_dentry);
3728 return error;
3729}
3730
3731int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
3732 struct inode *new_dir, struct dentry *new_dentry)
3733{
3734 int error;
3735 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
Eric Paris59b0df22010-02-08 12:53:52 -05003736 const unsigned char *old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737
3738 if (old_dentry->d_inode == new_dentry->d_inode)
3739 return 0;
3740
3741 error = may_delete(old_dir, old_dentry, is_dir);
3742 if (error)
3743 return error;
3744
3745 if (!new_dentry->d_inode)
Miklos Szeredia95164d2008-07-30 15:08:48 +02003746 error = may_create(new_dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 else
3748 error = may_delete(new_dir, new_dentry, is_dir);
3749 if (error)
3750 return error;
3751
Al Viroacfa4382008-12-04 10:06:33 -05003752 if (!old_dir->i_op->rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 return -EPERM;
3754
Robert Love0eeca282005-07-12 17:06:03 -04003755 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
3756
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 if (is_dir)
3758 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
3759 else
3760 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
Al Viro123df292009-12-25 04:57:57 -05003761 if (!error)
3762 fsnotify_move(old_dir, new_dir, old_name, is_dir,
Al Viro5a190ae2007-06-07 12:19:32 -04003763 new_dentry->d_inode, old_dentry);
Robert Love0eeca282005-07-12 17:06:03 -04003764 fsnotify_oldname_free(old_name);
3765
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 return error;
3767}
3768
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003769SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
3770 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771{
Al Viro2ad94ae2008-07-21 09:32:51 -04003772 struct dentry *old_dir, *new_dir;
3773 struct dentry *old_dentry, *new_dentry;
3774 struct dentry *trap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 struct nameidata oldnd, newnd;
Jeff Layton91a27b22012-10-10 15:25:28 -04003776 struct filename *from;
3777 struct filename *to;
Al Viro2ad94ae2008-07-21 09:32:51 -04003778 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779
Jeff Layton91a27b22012-10-10 15:25:28 -04003780 from = user_path_parent(olddfd, oldname, &oldnd);
3781 if (IS_ERR(from)) {
3782 error = PTR_ERR(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 goto exit;
Jeff Layton91a27b22012-10-10 15:25:28 -04003784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785
Jeff Layton91a27b22012-10-10 15:25:28 -04003786 to = user_path_parent(newdfd, newname, &newnd);
3787 if (IS_ERR(to)) {
3788 error = PTR_ERR(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 goto exit1;
Jeff Layton91a27b22012-10-10 15:25:28 -04003790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791
3792 error = -EXDEV;
Jan Blunck4ac91372008-02-14 19:34:32 -08003793 if (oldnd.path.mnt != newnd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 goto exit2;
3795
Jan Blunck4ac91372008-02-14 19:34:32 -08003796 old_dir = oldnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 error = -EBUSY;
3798 if (oldnd.last_type != LAST_NORM)
3799 goto exit2;
3800
Jan Blunck4ac91372008-02-14 19:34:32 -08003801 new_dir = newnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 if (newnd.last_type != LAST_NORM)
3803 goto exit2;
3804
Jan Karac30dabf2012-06-12 16:20:30 +02003805 error = mnt_want_write(oldnd.path.mnt);
3806 if (error)
3807 goto exit2;
3808
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003809 oldnd.flags &= ~LOOKUP_PARENT;
3810 newnd.flags &= ~LOOKUP_PARENT;
OGAWA Hirofumi4e9ed2f2008-10-16 07:50:29 +09003811 newnd.flags |= LOOKUP_RENAME_TARGET;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003812
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 trap = lock_rename(new_dir, old_dir);
3814
Christoph Hellwig49705b72005-11-08 21:35:06 -08003815 old_dentry = lookup_hash(&oldnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 error = PTR_ERR(old_dentry);
3817 if (IS_ERR(old_dentry))
3818 goto exit3;
3819 /* source must exist */
3820 error = -ENOENT;
3821 if (!old_dentry->d_inode)
3822 goto exit4;
3823 /* unless the source is a directory trailing slashes give -ENOTDIR */
3824 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
3825 error = -ENOTDIR;
3826 if (oldnd.last.name[oldnd.last.len])
3827 goto exit4;
3828 if (newnd.last.name[newnd.last.len])
3829 goto exit4;
3830 }
3831 /* source should not be ancestor of target */
3832 error = -EINVAL;
3833 if (old_dentry == trap)
3834 goto exit4;
Christoph Hellwig49705b72005-11-08 21:35:06 -08003835 new_dentry = lookup_hash(&newnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 error = PTR_ERR(new_dentry);
3837 if (IS_ERR(new_dentry))
3838 goto exit4;
3839 /* target should not be an ancestor of source */
3840 error = -ENOTEMPTY;
3841 if (new_dentry == trap)
3842 goto exit5;
3843
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003844 error = security_path_rename(&oldnd.path, old_dentry,
3845 &newnd.path, new_dentry);
3846 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02003847 goto exit5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 error = vfs_rename(old_dir->d_inode, old_dentry,
3849 new_dir->d_inode, new_dentry);
3850exit5:
3851 dput(new_dentry);
3852exit4:
3853 dput(old_dentry);
3854exit3:
3855 unlock_rename(new_dir, old_dir);
Jan Karac30dabf2012-06-12 16:20:30 +02003856 mnt_drop_write(oldnd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857exit2:
Jan Blunck1d957f92008-02-14 19:34:35 -08003858 path_put(&newnd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04003859 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003861 path_put(&oldnd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 putname(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04003863exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 return error;
3865}
3866
Heiko Carstensa26eab22009-01-14 14:14:17 +01003867SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003868{
3869 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
3870}
3871
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
3873{
3874 int len;
3875
3876 len = PTR_ERR(link);
3877 if (IS_ERR(link))
3878 goto out;
3879
3880 len = strlen(link);
3881 if (len > (unsigned) buflen)
3882 len = buflen;
3883 if (copy_to_user(buffer, link, len))
3884 len = -EFAULT;
3885out:
3886 return len;
3887}
3888
3889/*
3890 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
3891 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
3892 * using) it for any given inode is up to filesystem.
3893 */
3894int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3895{
3896 struct nameidata nd;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003897 void *cookie;
Marcin Slusarz694a1762008-06-09 16:40:37 -07003898 int res;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003899
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 nd.depth = 0;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003901 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
Marcin Slusarz694a1762008-06-09 16:40:37 -07003902 if (IS_ERR(cookie))
3903 return PTR_ERR(cookie);
3904
3905 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
3906 if (dentry->d_inode->i_op->put_link)
3907 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3908 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909}
3910
3911int vfs_follow_link(struct nameidata *nd, const char *link)
3912{
3913 return __vfs_follow_link(nd, link);
3914}
3915
3916/* get the link contents into pagecache */
3917static char *page_getlink(struct dentry * dentry, struct page **ppage)
3918{
Duane Griffinebd09ab2008-12-19 20:47:12 +00003919 char *kaddr;
3920 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 struct address_space *mapping = dentry->d_inode->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -07003922 page = read_mapping_page(mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 if (IS_ERR(page))
Nick Piggin6fe69002007-05-06 14:49:04 -07003924 return (char*)page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 *ppage = page;
Duane Griffinebd09ab2008-12-19 20:47:12 +00003926 kaddr = kmap(page);
3927 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3928 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929}
3930
3931int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3932{
3933 struct page *page = NULL;
3934 char *s = page_getlink(dentry, &page);
3935 int res = vfs_readlink(dentry,buffer,buflen,s);
3936 if (page) {
3937 kunmap(page);
3938 page_cache_release(page);
3939 }
3940 return res;
3941}
3942
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003943void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003945 struct page *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 nd_set_link(nd, page_getlink(dentry, &page));
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003947 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948}
3949
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003950void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003952 struct page *page = cookie;
3953
3954 if (page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 kunmap(page);
3956 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 }
3958}
3959
Nick Piggin54566b22009-01-04 12:00:53 -08003960/*
3961 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
3962 */
3963int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964{
3965 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003966 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07003967 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08003968 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 char *kaddr;
Nick Piggin54566b22009-01-04 12:00:53 -08003970 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
3971 if (nofs)
3972 flags |= AOP_FLAG_NOFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973
NeilBrown7e53cac2006-03-25 03:07:57 -08003974retry:
Nick Pigginafddba42007-10-16 01:25:01 -07003975 err = pagecache_write_begin(NULL, mapping, 0, len-1,
Nick Piggin54566b22009-01-04 12:00:53 -08003976 flags, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07003978 goto fail;
3979
Cong Wange8e3c3d2011-11-25 23:14:27 +08003980 kaddr = kmap_atomic(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 memcpy(kaddr, symname, len-1);
Cong Wange8e3c3d2011-11-25 23:14:27 +08003982 kunmap_atomic(kaddr);
Nick Pigginafddba42007-10-16 01:25:01 -07003983
3984 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3985 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 if (err < 0)
3987 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07003988 if (err < len-1)
3989 goto retry;
3990
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 mark_inode_dirty(inode);
3992 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993fail:
3994 return err;
3995}
3996
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003997int page_symlink(struct inode *inode, const char *symname, int len)
3998{
3999 return __page_symlink(inode, symname, len,
Nick Piggin54566b22009-01-04 12:00:53 -08004000 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004001}
4002
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08004003const struct inode_operations page_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 .readlink = generic_readlink,
4005 .follow_link = page_follow_link_light,
4006 .put_link = page_put_link,
4007};
4008
Al Viro2d8f3032008-07-22 09:59:21 -04004009EXPORT_SYMBOL(user_path_at);
David Howellscc53ce52011-01-14 18:45:26 +00004010EXPORT_SYMBOL(follow_down_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011EXPORT_SYMBOL(follow_down);
4012EXPORT_SYMBOL(follow_up);
Al Virof6d2ac52012-08-26 12:55:54 -04004013EXPORT_SYMBOL(get_write_access); /* nfsd */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015EXPORT_SYMBOL(lookup_one_len);
4016EXPORT_SYMBOL(page_follow_link_light);
4017EXPORT_SYMBOL(page_put_link);
4018EXPORT_SYMBOL(page_readlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004019EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020EXPORT_SYMBOL(page_symlink);
4021EXPORT_SYMBOL(page_symlink_inode_operations);
Al Virod1811462008-08-02 00:49:18 -04004022EXPORT_SYMBOL(kern_path);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07004023EXPORT_SYMBOL(vfs_path_lookup);
Al Virof419a2e2008-07-22 00:07:17 -04004024EXPORT_SYMBOL(inode_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025EXPORT_SYMBOL(unlock_rename);
4026EXPORT_SYMBOL(vfs_create);
4027EXPORT_SYMBOL(vfs_follow_link);
4028EXPORT_SYMBOL(vfs_link);
4029EXPORT_SYMBOL(vfs_mkdir);
4030EXPORT_SYMBOL(vfs_mknod);
4031EXPORT_SYMBOL(generic_permission);
4032EXPORT_SYMBOL(vfs_readlink);
4033EXPORT_SYMBOL(vfs_rename);
4034EXPORT_SYMBOL(vfs_rmdir);
4035EXPORT_SYMBOL(vfs_symlink);
4036EXPORT_SYMBOL(vfs_unlink);
4037EXPORT_SYMBOL(dentry_unhash);
4038EXPORT_SYMBOL(generic_readlink);