blob: 758bae7393051a0ba9230a7767290d44ef4540a2 [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>
18#include <linux/module.h>
19#include <linux/slab.h>
20#include <linux/fs.h>
21#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/pagemap.h>
Robert Love0eeca282005-07-12 17:06:03 -040023#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/personality.h>
25#include <linux/security.h>
Mimi Zohar6146f0d2009-02-04 09:06:57 -050026#include <linux/ima.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/syscalls.h>
28#include <linux/mount.h>
29#include <linux/audit.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080030#include <linux/capability.h>
Trond Myklebust834f2a42005-10-18 14:20:16 -070031#include <linux/file.h>
Ulrich Drepper5590ff02006-01-18 17:43:53 -080032#include <linux/fcntl.h>
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070033#include <linux/device_cgroup.h>
Al Viro5ad4e532009-03-29 19:50:06 -040034#include <linux/fs_struct.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/uaccess.h>
36
Eric Parise81e3f42009-12-04 15:47:36 -050037#include "internal.h"
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039/* [Feb-1997 T. Schoebel-Theuer]
40 * Fundamental changes in the pathname lookup mechanisms (namei)
41 * were necessary because of omirr. The reason is that omirr needs
42 * to know the _real_ pathname, not the user-supplied one, in case
43 * of symlinks (and also when transname replacements occur).
44 *
45 * The new code replaces the old recursive symlink resolution with
46 * an iterative one (in case of non-nested symlink chains). It does
47 * this with calls to <fs>_follow_link().
48 * As a side effect, dir_namei(), _namei() and follow_link() are now
49 * replaced with a single function lookup_dentry() that can handle all
50 * the special cases of the former code.
51 *
52 * With the new dcache, the pathname is stored at each inode, at least as
53 * long as the refcount of the inode is positive. As a side effect, the
54 * size of the dcache depends on the inode cache and thus is dynamic.
55 *
56 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
57 * resolution to correspond with current state of the code.
58 *
59 * Note that the symlink resolution is not *completely* iterative.
60 * There is still a significant amount of tail- and mid- recursion in
61 * the algorithm. Also, note that <fs>_readlink() is not used in
62 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
63 * may return different results than <fs>_follow_link(). Many virtual
64 * filesystems (including /proc) exhibit this behavior.
65 */
66
67/* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
68 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
69 * and the name already exists in form of a symlink, try to create the new
70 * name indicated by the symlink. The old code always complained that the
71 * name already exists, due to not following the symlink even if its target
72 * is nonexistent. The new semantics affects also mknod() and link() when
Lucas De Marchi25985ed2011-03-30 22:57:33 -030073 * the name is a symlink pointing to a non-existent name.
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 *
75 * I don't know which semantics is the right one, since I have no access
76 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
77 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
78 * "old" one. Personally, I think the new semantics is much more logical.
79 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
80 * file does succeed in both HP-UX and SunOs, but not in Solaris
81 * and in the old Linux semantics.
82 */
83
84/* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
85 * semantics. See the comments in "open_namei" and "do_link" below.
86 *
87 * [10-Sep-98 Alan Modra] Another symlink change.
88 */
89
90/* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
91 * inside the path - always follow.
92 * in the last component in creation/removal/renaming - never follow.
93 * if LOOKUP_FOLLOW passed - follow.
94 * if the pathname has trailing slashes - follow.
95 * otherwise - don't follow.
96 * (applied in that order).
97 *
98 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
99 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
100 * During the 2.4 we need to fix the userland stuff depending on it -
101 * hopefully we will be able to get rid of that wart in 2.5. So far only
102 * XEmacs seems to be relying on it...
103 */
104/*
105 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
Arjan van de Vena11f3a02006-03-23 03:00:33 -0800106 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 * any extra contention...
108 */
109
110/* In order to reduce some races, while at the same time doing additional
111 * checking and hopefully speeding things up, we copy filenames to the
112 * kernel data space before using them..
113 *
114 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
115 * PATH_MAX includes the nul terminator --RR.
116 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800117static int do_getname(const char __user *filename, char *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
119 int retval;
120 unsigned long len = PATH_MAX;
121
122 if (!segment_eq(get_fs(), KERNEL_DS)) {
123 if ((unsigned long) filename >= TASK_SIZE)
124 return -EFAULT;
125 if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
126 len = TASK_SIZE - (unsigned long) filename;
127 }
128
129 retval = strncpy_from_user(page, filename, len);
130 if (retval > 0) {
131 if (retval < len)
132 return 0;
133 return -ENAMETOOLONG;
134 } else if (!retval)
135 retval = -ENOENT;
136 return retval;
137}
138
Al Virof52e0c12011-03-14 18:56:51 -0400139static char *getname_flags(const char __user * filename, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
141 char *tmp, *result;
142
143 result = ERR_PTR(-ENOMEM);
144 tmp = __getname();
145 if (tmp) {
146 int retval = do_getname(filename, tmp);
147
148 result = tmp;
149 if (retval < 0) {
Al Virof52e0c12011-03-14 18:56:51 -0400150 if (retval != -ENOENT || !(flags & LOOKUP_EMPTY)) {
151 __putname(tmp);
152 result = ERR_PTR(retval);
153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 }
155 }
156 audit_getname(result);
157 return result;
158}
159
Al Virof52e0c12011-03-14 18:56:51 -0400160char *getname(const char __user * filename)
161{
162 return getname_flags(filename, 0);
163}
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165#ifdef CONFIG_AUDITSYSCALL
166void putname(const char *name)
167{
Al Viro5ac3a9c2006-07-16 06:38:45 -0400168 if (unlikely(!audit_dummy_context()))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 audit_putname(name);
170 else
171 __putname(name);
172}
173EXPORT_SYMBOL(putname);
174#endif
175
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700176/*
177 * This does basic POSIX ACL permission checking
178 */
Al Viro7e401452011-06-20 19:12:17 -0400179static int acl_permission_check(struct inode *inode, int mask)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700180{
Al Viro7e401452011-06-20 19:12:17 -0400181 int (*check_acl)(struct inode *inode, int mask);
Linus Torvalds26cf46b2011-05-13 11:51:01 -0700182 unsigned int mode = inode->i_mode;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700183
Al Viro9c2c7032011-06-20 19:06:22 -0400184 mask &= MAY_READ | MAY_WRITE | MAY_EXEC | MAY_NOT_BLOCK;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700185
Serge E. Hallyne795b712011-03-23 16:43:25 -0700186 if (current_user_ns() != inode_userns(inode))
187 goto other_perms;
188
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700189 if (current_fsuid() == inode->i_uid)
190 mode >>= 6;
191 else {
Al Viro178ea7352011-06-20 11:31:30 -0400192 check_acl = inode->i_op->check_acl;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700193 if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
Al Viro7e401452011-06-20 19:12:17 -0400194 int error = check_acl(inode, mask);
Nick Pigginb74c79e2011-01-07 17:49:58 +1100195 if (error != -EAGAIN)
196 return error;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700197 }
198
199 if (in_group_p(inode->i_gid))
200 mode >>= 3;
201 }
202
Serge E. Hallyne795b712011-03-23 16:43:25 -0700203other_perms:
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700204 /*
205 * If the DACs are ok we don't need any capability check.
206 */
Al Viro9c2c7032011-06-20 19:06:22 -0400207 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700208 return 0;
209 return -EACCES;
210}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212/**
Nick Pigginb74c79e2011-01-07 17:49:58 +1100213 * generic_permission - check for access rights on a Posix-like filesystem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 * @inode: inode to check access rights for
215 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
Randy Dunlap39191622011-01-08 19:36:21 -0800216 * @flags: IPERM_FLAG_ flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 *
218 * Used to check for read/write/execute permissions on a file.
219 * We use "fsuid" for this, letting us set arbitrary permissions
220 * for filesystem access without changing the "normal" uids which
Nick Pigginb74c79e2011-01-07 17:49:58 +1100221 * are used for other things.
222 *
223 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
224 * request cannot be satisfied (eg. requires blocking or too much complexity).
225 * It would then be called again in ref-walk mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 */
Al Viro2830ba72011-06-20 19:16:29 -0400227int generic_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700229 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 /*
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700232 * Do the basic POSIX ACL permission checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 */
Al Viro7e401452011-06-20 19:12:17 -0400234 ret = acl_permission_check(inode, mask);
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700235 if (ret != -EACCES)
236 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Al Virod594e7e2011-06-20 19:55:42 -0400238 if (S_ISDIR(inode->i_mode)) {
239 /* DACs are overridable for directories */
240 if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
241 return 0;
242 if (!(mask & MAY_WRITE))
243 if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
244 return 0;
245 return -EACCES;
246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 /*
248 * Read/write DACs are always overridable.
Al Virod594e7e2011-06-20 19:55:42 -0400249 * Executable DACs are overridable when there is
250 * at least one exec bit set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 */
Al Virod594e7e2011-06-20 19:55:42 -0400252 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
Serge E. Hallyne795b712011-03-23 16:43:25 -0700253 if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return 0;
255
256 /*
257 * Searching includes executable on directories, else just read.
258 */
Serge E. Hallyn7ea66002009-12-29 14:50:19 -0600259 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
Al Virod594e7e2011-06-20 19:55:42 -0400260 if (mask == MAY_READ)
Serge E. Hallyne795b712011-03-23 16:43:25 -0700261 if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return 0;
263
264 return -EACCES;
265}
266
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200267/**
268 * inode_permission - check for access rights to a given inode
269 * @inode: inode to check permission on
270 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
271 *
272 * Used to check for read/write/execute permissions on an inode.
273 * We use "fsuid" for this, letting us set arbitrary permissions
274 * for filesystem access without changing the "normal" uids which
275 * are used for other things.
276 */
Al Virof419a2e2008-07-22 00:07:17 -0400277int inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
Al Viroe6305c42008-07-15 21:03:57 -0400279 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 if (mask & MAY_WRITE) {
Miklos Szeredi22590e42007-10-16 23:27:08 -0700282 umode_t mode = inode->i_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 /*
285 * Nobody gets write access to a read-only fs.
286 */
287 if (IS_RDONLY(inode) &&
288 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
289 return -EROFS;
290
291 /*
292 * Nobody gets write access to an immutable file.
293 */
294 if (IS_IMMUTABLE(inode))
295 return -EACCES;
296 }
297
Al Viroacfa4382008-12-04 10:06:33 -0500298 if (inode->i_op->permission)
Al Viro10556cb2011-06-20 19:28:19 -0400299 retval = inode->i_op->permission(inode, mask);
Miklos Szeredif696a362008-07-31 13:41:58 +0200300 else
Al Viro2830ba72011-06-20 19:16:29 -0400301 retval = generic_permission(inode, mask);
Miklos Szeredif696a362008-07-31 13:41:58 +0200302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 if (retval)
304 return retval;
305
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700306 retval = devcgroup_inode_permission(inode, mask);
307 if (retval)
308 return retval;
309
Eric Parisd09ca732010-07-23 11:43:57 -0400310 return security_inode_permission(inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
Al Virof4d6ff82011-06-19 13:14:21 -0400313/**
314 * exec_permission - check for right to do lookups in a given directory
315 * @inode: inode to check permission on
Al Viroeecdd352011-06-20 19:48:41 -0400316 * @mask: MAY_EXEC and possibly MAY_NOT_BLOCK flags.
Al Virof4d6ff82011-06-19 13:14:21 -0400317 *
318 * Short-cut version of inode_permission(), for calling on directories
319 * during pathname resolution. Combines parts of inode_permission()
320 * and generic_permission(), and tests ONLY for MAY_EXEC permission.
321 *
322 * If appropriate, check DAC only. If not appropriate, or
323 * short-cut DAC fails, then call ->permission() to do more
324 * complete permission check.
325 */
Al Viroeecdd352011-06-20 19:48:41 -0400326static inline int exec_permission(struct inode *inode, int mask)
Al Virof4d6ff82011-06-19 13:14:21 -0400327{
328 int ret;
329 struct user_namespace *ns = inode_userns(inode);
330
331 if (inode->i_op->permission) {
Al Viro10556cb2011-06-20 19:28:19 -0400332 ret = inode->i_op->permission(inode, mask);
Al Virof4d6ff82011-06-19 13:14:21 -0400333 if (likely(!ret))
334 goto ok;
335 } else {
Al Viro7e401452011-06-20 19:12:17 -0400336 ret = acl_permission_check(inode, mask);
Al Virof4d6ff82011-06-19 13:14:21 -0400337 if (likely(!ret))
338 goto ok;
339 if (ret != -EACCES)
340 return ret;
341 if (ns_capable(ns, CAP_DAC_OVERRIDE) ||
342 ns_capable(ns, CAP_DAC_READ_SEARCH))
343 goto ok;
344 }
345 return ret;
346ok:
Al Viroeecdd352011-06-20 19:48:41 -0400347 return security_inode_permission(inode, mask);
Al Virof4d6ff82011-06-19 13:14:21 -0400348}
349
Jan Blunck1d957f92008-02-14 19:34:35 -0800350/**
Jan Blunck5dd784d2008-02-14 19:34:38 -0800351 * path_get - get a reference to a path
352 * @path: path to get the reference to
353 *
354 * Given a path increment the reference count to the dentry and the vfsmount.
355 */
356void path_get(struct path *path)
357{
358 mntget(path->mnt);
359 dget(path->dentry);
360}
361EXPORT_SYMBOL(path_get);
362
363/**
Jan Blunck1d957f92008-02-14 19:34:35 -0800364 * path_put - put a reference to a path
365 * @path: path to put the reference to
366 *
367 * Given a path decrement the reference count to the dentry and the vfsmount.
368 */
369void path_put(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
Jan Blunck1d957f92008-02-14 19:34:35 -0800371 dput(path->dentry);
372 mntput(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
Jan Blunck1d957f92008-02-14 19:34:35 -0800374EXPORT_SYMBOL(path_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Al Viro19660af2011-03-25 10:32:48 -0400376/*
Nick Piggin31e6b012011-01-07 17:49:52 +1100377 * Path walking has 2 modes, rcu-walk and ref-walk (see
Al Viro19660af2011-03-25 10:32:48 -0400378 * Documentation/filesystems/path-lookup.txt). In situations when we can't
379 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
380 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
381 * mode. Refcounts are grabbed at the last known good point before rcu-walk
382 * got stuck, so ref-walk may continue from there. If this is not successful
383 * (eg. a seqcount has changed), then failure is returned and it's up to caller
384 * to restart the path walk from the beginning in ref-walk mode.
Nick Piggin31e6b012011-01-07 17:49:52 +1100385 */
Nick Piggin31e6b012011-01-07 17:49:52 +1100386
387/**
Al Viro19660af2011-03-25 10:32:48 -0400388 * unlazy_walk - try to switch to ref-walk mode.
389 * @nd: nameidata pathwalk data
390 * @dentry: child of nd->path.dentry or NULL
Randy Dunlap39191622011-01-08 19:36:21 -0800391 * Returns: 0 on success, -ECHILD on failure
Nick Piggin31e6b012011-01-07 17:49:52 +1100392 *
Al Viro19660af2011-03-25 10:32:48 -0400393 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
394 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
395 * @nd or NULL. Must be called from rcu-walk context.
Nick Piggin31e6b012011-01-07 17:49:52 +1100396 */
Al Viro19660af2011-03-25 10:32:48 -0400397static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
Nick Piggin31e6b012011-01-07 17:49:52 +1100398{
399 struct fs_struct *fs = current->fs;
400 struct dentry *parent = nd->path.dentry;
Al Viro5b6ca022011-03-09 23:04:47 -0500401 int want_root = 0;
Nick Piggin31e6b012011-01-07 17:49:52 +1100402
403 BUG_ON(!(nd->flags & LOOKUP_RCU));
Al Viro5b6ca022011-03-09 23:04:47 -0500404 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
405 want_root = 1;
Nick Piggin31e6b012011-01-07 17:49:52 +1100406 spin_lock(&fs->lock);
407 if (nd->root.mnt != fs->root.mnt ||
408 nd->root.dentry != fs->root.dentry)
409 goto err_root;
410 }
411 spin_lock(&parent->d_lock);
Al Viro19660af2011-03-25 10:32:48 -0400412 if (!dentry) {
413 if (!__d_rcu_to_refcount(parent, nd->seq))
414 goto err_parent;
415 BUG_ON(nd->inode != parent->d_inode);
416 } else {
Al Viro94c0d4e2011-07-12 21:40:23 -0400417 if (dentry->d_parent != parent)
418 goto err_parent;
Al Viro19660af2011-03-25 10:32:48 -0400419 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
420 if (!__d_rcu_to_refcount(dentry, nd->seq))
421 goto err_child;
422 /*
423 * If the sequence check on the child dentry passed, then
424 * the child has not been removed from its parent. This
425 * means the parent dentry must be valid and able to take
426 * a reference at this point.
427 */
428 BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
429 BUG_ON(!parent->d_count);
430 parent->d_count++;
431 spin_unlock(&dentry->d_lock);
432 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100433 spin_unlock(&parent->d_lock);
Al Viro5b6ca022011-03-09 23:04:47 -0500434 if (want_root) {
Nick Piggin31e6b012011-01-07 17:49:52 +1100435 path_get(&nd->root);
436 spin_unlock(&fs->lock);
437 }
438 mntget(nd->path.mnt);
439
440 rcu_read_unlock();
441 br_read_unlock(vfsmount_lock);
442 nd->flags &= ~LOOKUP_RCU;
443 return 0;
Al Viro19660af2011-03-25 10:32:48 -0400444
445err_child:
Nick Piggin31e6b012011-01-07 17:49:52 +1100446 spin_unlock(&dentry->d_lock);
Al Viro19660af2011-03-25 10:32:48 -0400447err_parent:
Nick Piggin31e6b012011-01-07 17:49:52 +1100448 spin_unlock(&parent->d_lock);
449err_root:
Al Viro5b6ca022011-03-09 23:04:47 -0500450 if (want_root)
Nick Piggin31e6b012011-01-07 17:49:52 +1100451 spin_unlock(&fs->lock);
452 return -ECHILD;
453}
454
Nick Piggin31e6b012011-01-07 17:49:52 +1100455/**
Trond Myklebust834f2a42005-10-18 14:20:16 -0700456 * release_open_intent - free up open intent resources
457 * @nd: pointer to nameidata
458 */
459void release_open_intent(struct nameidata *nd)
460{
Linus Torvalds2dab5972011-02-11 15:53:38 -0800461 struct file *file = nd->intent.open.file;
462
463 if (file && !IS_ERR(file)) {
464 if (file->f_path.dentry == NULL)
465 put_filp(file);
466 else
467 fput(file);
468 }
Trond Myklebust834f2a42005-10-18 14:20:16 -0700469}
470
Al Virof60aef72011-02-15 01:35:28 -0500471static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
Nick Piggin34286d62011-01-07 17:49:57 +1100472{
Al Virof60aef72011-02-15 01:35:28 -0500473 return dentry->d_op->d_revalidate(dentry, nd);
Nick Piggin34286d62011-01-07 17:49:57 +1100474}
475
Al Virof5e1c1c2011-02-15 01:32:55 -0500476static struct dentry *
Ian Kentbcdc5e02006-09-27 01:50:44 -0700477do_revalidate(struct dentry *dentry, struct nameidata *nd)
478{
Al Virof5e1c1c2011-02-15 01:32:55 -0500479 int status = d_revalidate(dentry, nd);
Ian Kentbcdc5e02006-09-27 01:50:44 -0700480 if (unlikely(status <= 0)) {
481 /*
482 * The dentry failed validation.
483 * If d_revalidate returned 0 attempt to invalidate
484 * the dentry otherwise d_revalidate is asking us
485 * to return a fail status.
486 */
Nick Piggin34286d62011-01-07 17:49:57 +1100487 if (status < 0) {
Al Virof5e1c1c2011-02-15 01:32:55 -0500488 dput(dentry);
Nick Piggin34286d62011-01-07 17:49:57 +1100489 dentry = ERR_PTR(status);
Al Virof5e1c1c2011-02-15 01:32:55 -0500490 } else if (!d_invalidate(dentry)) {
491 dput(dentry);
492 dentry = NULL;
Ian Kentbcdc5e02006-09-27 01:50:44 -0700493 }
494 }
495 return dentry;
496}
497
Al Viro9f1fafe2011-03-25 11:00:12 -0400498/**
499 * complete_walk - successful completion of path walk
500 * @nd: pointer nameidata
Jeff Layton39159de2009-12-07 12:01:50 -0500501 *
Al Viro9f1fafe2011-03-25 11:00:12 -0400502 * If we had been in RCU mode, drop out of it and legitimize nd->path.
503 * Revalidate the final result, unless we'd already done that during
504 * the path walk or the filesystem doesn't ask for it. Return 0 on
505 * success, -error on failure. In case of failure caller does not
506 * need to drop nd->path.
Jeff Layton39159de2009-12-07 12:01:50 -0500507 */
Al Viro9f1fafe2011-03-25 11:00:12 -0400508static int complete_walk(struct nameidata *nd)
Jeff Layton39159de2009-12-07 12:01:50 -0500509{
Al Viro16c2cd72011-02-22 15:50:10 -0500510 struct dentry *dentry = nd->path.dentry;
Jeff Layton39159de2009-12-07 12:01:50 -0500511 int status;
Jeff Layton39159de2009-12-07 12:01:50 -0500512
Al Viro9f1fafe2011-03-25 11:00:12 -0400513 if (nd->flags & LOOKUP_RCU) {
514 nd->flags &= ~LOOKUP_RCU;
515 if (!(nd->flags & LOOKUP_ROOT))
516 nd->root.mnt = NULL;
517 spin_lock(&dentry->d_lock);
518 if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
519 spin_unlock(&dentry->d_lock);
520 rcu_read_unlock();
521 br_read_unlock(vfsmount_lock);
522 return -ECHILD;
523 }
524 BUG_ON(nd->inode != dentry->d_inode);
525 spin_unlock(&dentry->d_lock);
526 mntget(nd->path.mnt);
527 rcu_read_unlock();
528 br_read_unlock(vfsmount_lock);
529 }
530
Al Viro16c2cd72011-02-22 15:50:10 -0500531 if (likely(!(nd->flags & LOOKUP_JUMPED)))
Jeff Layton39159de2009-12-07 12:01:50 -0500532 return 0;
533
Al Viro16c2cd72011-02-22 15:50:10 -0500534 if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
535 return 0;
536
537 if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
538 return 0;
539
540 /* Note: we do not d_invalidate() */
Nick Piggin34286d62011-01-07 17:49:57 +1100541 status = d_revalidate(dentry, nd);
Jeff Layton39159de2009-12-07 12:01:50 -0500542 if (status > 0)
543 return 0;
544
Al Viro16c2cd72011-02-22 15:50:10 -0500545 if (!status)
Jeff Layton39159de2009-12-07 12:01:50 -0500546 status = -ESTALE;
Al Viro16c2cd72011-02-22 15:50:10 -0500547
Al Viro9f1fafe2011-03-25 11:00:12 -0400548 path_put(&nd->path);
Jeff Layton39159de2009-12-07 12:01:50 -0500549 return status;
550}
551
Al Viro2a737872009-04-07 11:49:53 -0400552static __always_inline void set_root(struct nameidata *nd)
553{
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200554 if (!nd->root.mnt)
555 get_fs_root(current->fs, &nd->root);
Al Viro2a737872009-04-07 11:49:53 -0400556}
557
Al Viro6de88d72009-08-09 01:41:57 +0400558static int link_path_walk(const char *, struct nameidata *);
559
Nick Piggin31e6b012011-01-07 17:49:52 +1100560static __always_inline void set_root_rcu(struct nameidata *nd)
561{
562 if (!nd->root.mnt) {
563 struct fs_struct *fs = current->fs;
Nick Pigginc28cc362011-01-07 17:49:53 +1100564 unsigned seq;
565
566 do {
567 seq = read_seqcount_begin(&fs->seq);
568 nd->root = fs->root;
Tim Chenc1530012011-04-15 11:39:29 -0700569 nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
Nick Pigginc28cc362011-01-07 17:49:53 +1100570 } while (read_seqcount_retry(&fs->seq, seq));
Nick Piggin31e6b012011-01-07 17:49:52 +1100571 }
572}
573
Arjan van de Venf1662352006-01-14 13:21:31 -0800574static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
Nick Piggin31e6b012011-01-07 17:49:52 +1100576 int ret;
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if (IS_ERR(link))
579 goto fail;
580
581 if (*link == '/') {
Al Viro2a737872009-04-07 11:49:53 -0400582 set_root(nd);
Jan Blunck1d957f92008-02-14 19:34:35 -0800583 path_put(&nd->path);
Al Viro2a737872009-04-07 11:49:53 -0400584 nd->path = nd->root;
585 path_get(&nd->root);
Al Viro16c2cd72011-02-22 15:50:10 -0500586 nd->flags |= LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100588 nd->inode = nd->path.dentry->d_inode;
Christoph Hellwigb4091d52008-11-05 15:07:21 +0100589
Nick Piggin31e6b012011-01-07 17:49:52 +1100590 ret = link_path_walk(link, nd);
591 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592fail:
Jan Blunck1d957f92008-02-14 19:34:35 -0800593 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return PTR_ERR(link);
595}
596
Jan Blunck1d957f92008-02-14 19:34:35 -0800597static void path_put_conditional(struct path *path, struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700598{
599 dput(path->dentry);
Jan Blunck4ac91372008-02-14 19:34:32 -0800600 if (path->mnt != nd->path.mnt)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700601 mntput(path->mnt);
602}
603
Nick Piggin7b9337a2011-01-14 08:42:43 +0000604static inline void path_to_nameidata(const struct path *path,
605 struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700606{
Nick Piggin31e6b012011-01-07 17:49:52 +1100607 if (!(nd->flags & LOOKUP_RCU)) {
608 dput(nd->path.dentry);
609 if (nd->path.mnt != path->mnt)
610 mntput(nd->path.mnt);
Huang Shijie9a229682010-04-02 17:37:13 +0800611 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100612 nd->path.mnt = path->mnt;
Jan Blunck4ac91372008-02-14 19:34:32 -0800613 nd->path.dentry = path->dentry;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700614}
615
Al Viro574197e2011-03-14 22:20:34 -0400616static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
617{
618 struct inode *inode = link->dentry->d_inode;
619 if (!IS_ERR(cookie) && inode->i_op->put_link)
620 inode->i_op->put_link(link->dentry, nd, cookie);
621 path_put(link);
622}
623
Al Virodef4af32009-12-26 08:37:05 -0500624static __always_inline int
Al Viro574197e2011-03-14 22:20:34 -0400625follow_link(struct path *link, struct nameidata *nd, void **p)
Ian Kent051d3812006-03-27 01:14:53 -0800626{
627 int error;
Nick Piggin7b9337a2011-01-14 08:42:43 +0000628 struct dentry *dentry = link->dentry;
Ian Kent051d3812006-03-27 01:14:53 -0800629
Al Viro844a3912011-02-15 00:38:26 -0500630 BUG_ON(nd->flags & LOOKUP_RCU);
631
Al Viro0e794582011-03-16 02:45:02 -0400632 if (link->mnt == nd->path.mnt)
633 mntget(link->mnt);
634
Al Viro574197e2011-03-14 22:20:34 -0400635 if (unlikely(current->total_link_count >= 40)) {
636 *p = ERR_PTR(-ELOOP); /* no ->put_link(), please */
Al Viro574197e2011-03-14 22:20:34 -0400637 path_put(&nd->path);
638 return -ELOOP;
639 }
640 cond_resched();
641 current->total_link_count++;
642
Nick Piggin7b9337a2011-01-14 08:42:43 +0000643 touch_atime(link->mnt, dentry);
Ian Kent051d3812006-03-27 01:14:53 -0800644 nd_set_link(nd, NULL);
645
Al Viro36f3b4f2011-02-22 21:24:38 -0500646 error = security_inode_follow_link(link->dentry, nd);
647 if (error) {
648 *p = ERR_PTR(error); /* no ->put_link(), please */
649 path_put(&nd->path);
650 return error;
651 }
652
Al Viro86acdca12009-12-22 23:45:11 -0500653 nd->last_type = LAST_BIND;
Al Virodef4af32009-12-26 08:37:05 -0500654 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
655 error = PTR_ERR(*p);
656 if (!IS_ERR(*p)) {
Ian Kent051d3812006-03-27 01:14:53 -0800657 char *s = nd_get_link(nd);
658 error = 0;
659 if (s)
660 error = __vfs_follow_link(nd, s);
Al Virobcda7652011-03-13 16:42:14 -0400661 else if (nd->last_type == LAST_BIND) {
Al Viro16c2cd72011-02-22 15:50:10 -0500662 nd->flags |= LOOKUP_JUMPED;
Al Virob21041d2011-03-14 20:01:51 -0400663 nd->inode = nd->path.dentry->d_inode;
664 if (nd->inode->i_op->follow_link) {
Al Virobcda7652011-03-13 16:42:14 -0400665 /* stepped on a _really_ weird one */
666 path_put(&nd->path);
667 error = -ELOOP;
668 }
669 }
Ian Kent051d3812006-03-27 01:14:53 -0800670 }
Ian Kent051d3812006-03-27 01:14:53 -0800671 return error;
672}
673
Nick Piggin31e6b012011-01-07 17:49:52 +1100674static int follow_up_rcu(struct path *path)
675{
676 struct vfsmount *parent;
677 struct dentry *mountpoint;
678
679 parent = path->mnt->mnt_parent;
680 if (parent == path->mnt)
681 return 0;
682 mountpoint = path->mnt->mnt_mountpoint;
683 path->dentry = mountpoint;
684 path->mnt = parent;
685 return 1;
686}
687
Al Virobab77eb2009-04-18 03:26:48 -0400688int follow_up(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
690 struct vfsmount *parent;
691 struct dentry *mountpoint;
Nick Piggin99b7db72010-08-18 04:37:39 +1000692
693 br_read_lock(vfsmount_lock);
Al Virobab77eb2009-04-18 03:26:48 -0400694 parent = path->mnt->mnt_parent;
695 if (parent == path->mnt) {
Nick Piggin99b7db72010-08-18 04:37:39 +1000696 br_read_unlock(vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return 0;
698 }
699 mntget(parent);
Al Virobab77eb2009-04-18 03:26:48 -0400700 mountpoint = dget(path->mnt->mnt_mountpoint);
Nick Piggin99b7db72010-08-18 04:37:39 +1000701 br_read_unlock(vfsmount_lock);
Al Virobab77eb2009-04-18 03:26:48 -0400702 dput(path->dentry);
703 path->dentry = mountpoint;
704 mntput(path->mnt);
705 path->mnt = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return 1;
707}
708
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100709/*
David Howells9875cf82011-01-14 18:45:21 +0000710 * Perform an automount
711 * - return -EISDIR to tell follow_managed() to stop and return the path we
712 * were called with.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 */
David Howells9875cf82011-01-14 18:45:21 +0000714static int follow_automount(struct path *path, unsigned flags,
715 bool *need_mntput)
Nick Piggin31e6b012011-01-07 17:49:52 +1100716{
David Howells9875cf82011-01-14 18:45:21 +0000717 struct vfsmount *mnt;
David Howellsea5b7782011-01-14 19:10:03 +0000718 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +1100719
David Howells9875cf82011-01-14 18:45:21 +0000720 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
721 return -EREMOTE;
Al Viro463ffb22005-06-06 13:36:05 -0700722
David Howells6f45b652011-01-14 18:45:31 +0000723 /* We don't want to mount if someone supplied AT_NO_AUTOMOUNT
724 * and this is the terminal part of the path.
725 */
726 if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_CONTINUE))
727 return -EISDIR; /* we actually want to stop here */
728
David Howells9875cf82011-01-14 18:45:21 +0000729 /* We want to mount if someone is trying to open/create a file of any
730 * type under the mountpoint, wants to traverse through the mountpoint
731 * or wants to open the mounted directory.
732 *
733 * We don't want to mount if someone's just doing a stat and they've
734 * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and
735 * appended a '/' to the name.
736 */
737 if (!(flags & LOOKUP_FOLLOW) &&
738 !(flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY |
739 LOOKUP_OPEN | LOOKUP_CREATE)))
740 return -EISDIR;
741
742 current->total_link_count++;
743 if (current->total_link_count >= 40)
744 return -ELOOP;
745
746 mnt = path->dentry->d_op->d_automount(path);
747 if (IS_ERR(mnt)) {
748 /*
749 * The filesystem is allowed to return -EISDIR here to indicate
750 * it doesn't want to automount. For instance, autofs would do
751 * this so that its userspace daemon can mount on this dentry.
752 *
753 * However, we can only permit this if it's a terminal point in
754 * the path being looked up; if it wasn't then the remainder of
755 * the path is inaccessible and we should say so.
756 */
757 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_CONTINUE))
758 return -EREMOTE;
759 return PTR_ERR(mnt);
760 }
David Howellsea5b7782011-01-14 19:10:03 +0000761
David Howells9875cf82011-01-14 18:45:21 +0000762 if (!mnt) /* mount collision */
763 return 0;
764
Al Viro8aef1882011-06-16 15:10:06 +0100765 if (!*need_mntput) {
766 /* lock_mount() may release path->mnt on error */
767 mntget(path->mnt);
768 *need_mntput = true;
769 }
Al Viro19a167a2011-01-17 01:35:23 -0500770 err = finish_automount(mnt, path);
David Howellsea5b7782011-01-14 19:10:03 +0000771
David Howellsea5b7782011-01-14 19:10:03 +0000772 switch (err) {
773 case -EBUSY:
774 /* Someone else made a mount here whilst we were busy */
Al Viro19a167a2011-01-17 01:35:23 -0500775 return 0;
David Howellsea5b7782011-01-14 19:10:03 +0000776 case 0:
Al Viro8aef1882011-06-16 15:10:06 +0100777 path_put(path);
David Howellsea5b7782011-01-14 19:10:03 +0000778 path->mnt = mnt;
779 path->dentry = dget(mnt->mnt_root);
David Howellsea5b7782011-01-14 19:10:03 +0000780 return 0;
Al Viro19a167a2011-01-17 01:35:23 -0500781 default:
782 return err;
David Howellsea5b7782011-01-14 19:10:03 +0000783 }
Al Viro19a167a2011-01-17 01:35:23 -0500784
David Howells9875cf82011-01-14 18:45:21 +0000785}
786
787/*
788 * Handle a dentry that is managed in some way.
David Howellscc53ce52011-01-14 18:45:26 +0000789 * - Flagged for transit management (autofs)
David Howells9875cf82011-01-14 18:45:21 +0000790 * - Flagged as mountpoint
791 * - Flagged as automount point
792 *
793 * This may only be called in refwalk mode.
794 *
795 * Serialization is taken care of in namespace.c
796 */
797static int follow_managed(struct path *path, unsigned flags)
798{
Al Viro8aef1882011-06-16 15:10:06 +0100799 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
David Howells9875cf82011-01-14 18:45:21 +0000800 unsigned managed;
801 bool need_mntput = false;
Al Viro8aef1882011-06-16 15:10:06 +0100802 int ret = 0;
David Howells9875cf82011-01-14 18:45:21 +0000803
804 /* Given that we're not holding a lock here, we retain the value in a
805 * local variable for each dentry as we look at it so that we don't see
806 * the components of that value change under us */
807 while (managed = ACCESS_ONCE(path->dentry->d_flags),
808 managed &= DCACHE_MANAGED_DENTRY,
809 unlikely(managed != 0)) {
David Howellscc53ce52011-01-14 18:45:26 +0000810 /* Allow the filesystem to manage the transit without i_mutex
811 * being held. */
812 if (managed & DCACHE_MANAGE_TRANSIT) {
813 BUG_ON(!path->dentry->d_op);
814 BUG_ON(!path->dentry->d_op->d_manage);
Al Viro1aed3e42011-03-18 09:09:02 -0400815 ret = path->dentry->d_op->d_manage(path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +0000816 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +0100817 break;
David Howellscc53ce52011-01-14 18:45:26 +0000818 }
819
David Howells9875cf82011-01-14 18:45:21 +0000820 /* Transit to a mounted filesystem. */
821 if (managed & DCACHE_MOUNTED) {
822 struct vfsmount *mounted = lookup_mnt(path);
823 if (mounted) {
824 dput(path->dentry);
825 if (need_mntput)
826 mntput(path->mnt);
827 path->mnt = mounted;
828 path->dentry = dget(mounted->mnt_root);
829 need_mntput = true;
830 continue;
831 }
832
833 /* Something is mounted on this dentry in another
834 * namespace and/or whatever was mounted there in this
835 * namespace got unmounted before we managed to get the
836 * vfsmount_lock */
837 }
838
839 /* Handle an automount point */
840 if (managed & DCACHE_NEED_AUTOMOUNT) {
841 ret = follow_automount(path, flags, &need_mntput);
842 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +0100843 break;
David Howells9875cf82011-01-14 18:45:21 +0000844 continue;
845 }
846
847 /* We didn't change the current path point */
848 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
Al Viro8aef1882011-06-16 15:10:06 +0100850
851 if (need_mntput && path->mnt == mnt)
852 mntput(path->mnt);
853 if (ret == -EISDIR)
854 ret = 0;
855 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856}
857
David Howellscc53ce52011-01-14 18:45:26 +0000858int follow_down_one(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
860 struct vfsmount *mounted;
861
Al Viro1c755af2009-04-18 14:06:57 -0400862 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -0400864 dput(path->dentry);
865 mntput(path->mnt);
866 path->mnt = mounted;
867 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 return 1;
869 }
870 return 0;
871}
872
Ian Kent62a73752011-03-25 01:51:02 +0800873static inline bool managed_dentry_might_block(struct dentry *dentry)
874{
875 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
876 dentry->d_op->d_manage(dentry, true) < 0);
877}
878
David Howells9875cf82011-01-14 18:45:21 +0000879/*
Al Viro287548e2011-05-27 06:50:06 -0400880 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
881 * we meet a managed dentry that would need blocking.
David Howells9875cf82011-01-14 18:45:21 +0000882 */
883static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
Al Viro287548e2011-05-27 06:50:06 -0400884 struct inode **inode)
David Howells9875cf82011-01-14 18:45:21 +0000885{
Ian Kent62a73752011-03-25 01:51:02 +0800886 for (;;) {
David Howells9875cf82011-01-14 18:45:21 +0000887 struct vfsmount *mounted;
Ian Kent62a73752011-03-25 01:51:02 +0800888 /*
889 * Don't forget we might have a non-mountpoint managed dentry
890 * that wants to block transit.
891 */
Al Viro287548e2011-05-27 06:50:06 -0400892 if (unlikely(managed_dentry_might_block(path->dentry)))
David Howellsab909112011-01-14 18:46:51 +0000893 return false;
Ian Kent62a73752011-03-25 01:51:02 +0800894
895 if (!d_mountpoint(path->dentry))
896 break;
897
David Howells9875cf82011-01-14 18:45:21 +0000898 mounted = __lookup_mnt(path->mnt, path->dentry, 1);
899 if (!mounted)
900 break;
901 path->mnt = mounted;
902 path->dentry = mounted->mnt_root;
903 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
Linus Torvalds59430262011-07-18 15:43:29 -0700904 /*
905 * Update the inode too. We don't need to re-check the
906 * dentry sequence number here after this d_inode read,
907 * because a mount-point is always pinned.
908 */
909 *inode = path->dentry->d_inode;
David Howells9875cf82011-01-14 18:45:21 +0000910 }
David Howells9875cf82011-01-14 18:45:21 +0000911 return true;
912}
913
Al Virodea39372011-05-27 06:53:39 -0400914static void follow_mount_rcu(struct nameidata *nd)
Al Viro287548e2011-05-27 06:50:06 -0400915{
Al Virodea39372011-05-27 06:53:39 -0400916 while (d_mountpoint(nd->path.dentry)) {
Al Viro287548e2011-05-27 06:50:06 -0400917 struct vfsmount *mounted;
Al Virodea39372011-05-27 06:53:39 -0400918 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
Al Viro287548e2011-05-27 06:50:06 -0400919 if (!mounted)
920 break;
Al Virodea39372011-05-27 06:53:39 -0400921 nd->path.mnt = mounted;
922 nd->path.dentry = mounted->mnt_root;
923 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro287548e2011-05-27 06:50:06 -0400924 }
925}
926
Nick Piggin31e6b012011-01-07 17:49:52 +1100927static int follow_dotdot_rcu(struct nameidata *nd)
928{
Nick Piggin31e6b012011-01-07 17:49:52 +1100929 set_root_rcu(nd);
930
David Howells9875cf82011-01-14 18:45:21 +0000931 while (1) {
Nick Piggin31e6b012011-01-07 17:49:52 +1100932 if (nd->path.dentry == nd->root.dentry &&
933 nd->path.mnt == nd->root.mnt) {
934 break;
935 }
936 if (nd->path.dentry != nd->path.mnt->mnt_root) {
937 struct dentry *old = nd->path.dentry;
938 struct dentry *parent = old->d_parent;
939 unsigned seq;
940
941 seq = read_seqcount_begin(&parent->d_seq);
942 if (read_seqcount_retry(&old->d_seq, nd->seq))
Al Viroef7562d2011-03-04 14:35:59 -0500943 goto failed;
Nick Piggin31e6b012011-01-07 17:49:52 +1100944 nd->path.dentry = parent;
945 nd->seq = seq;
946 break;
947 }
948 if (!follow_up_rcu(&nd->path))
949 break;
950 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Nick Piggin31e6b012011-01-07 17:49:52 +1100951 }
Al Virodea39372011-05-27 06:53:39 -0400952 follow_mount_rcu(nd);
953 nd->inode = nd->path.dentry->d_inode;
Nick Piggin31e6b012011-01-07 17:49:52 +1100954 return 0;
Al Viroef7562d2011-03-04 14:35:59 -0500955
956failed:
957 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -0500958 if (!(nd->flags & LOOKUP_ROOT))
959 nd->root.mnt = NULL;
Al Viroef7562d2011-03-04 14:35:59 -0500960 rcu_read_unlock();
961 br_read_unlock(vfsmount_lock);
962 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +1100963}
964
David Howells9875cf82011-01-14 18:45:21 +0000965/*
David Howellscc53ce52011-01-14 18:45:26 +0000966 * Follow down to the covering mount currently visible to userspace. At each
967 * point, the filesystem owning that dentry may be queried as to whether the
968 * caller is permitted to proceed or not.
David Howellscc53ce52011-01-14 18:45:26 +0000969 */
Al Viro7cc90cc2011-03-18 09:04:20 -0400970int follow_down(struct path *path)
David Howellscc53ce52011-01-14 18:45:26 +0000971{
972 unsigned managed;
973 int ret;
974
975 while (managed = ACCESS_ONCE(path->dentry->d_flags),
976 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
977 /* Allow the filesystem to manage the transit without i_mutex
978 * being held.
979 *
980 * We indicate to the filesystem if someone is trying to mount
981 * something here. This gives autofs the chance to deny anyone
982 * other than its daemon the right to mount on its
983 * superstructure.
984 *
985 * The filesystem may sleep at this point.
986 */
987 if (managed & DCACHE_MANAGE_TRANSIT) {
988 BUG_ON(!path->dentry->d_op);
989 BUG_ON(!path->dentry->d_op->d_manage);
David Howellsab909112011-01-14 18:46:51 +0000990 ret = path->dentry->d_op->d_manage(
Al Viro1aed3e42011-03-18 09:09:02 -0400991 path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +0000992 if (ret < 0)
993 return ret == -EISDIR ? 0 : ret;
994 }
995
996 /* Transit to a mounted filesystem. */
997 if (managed & DCACHE_MOUNTED) {
998 struct vfsmount *mounted = lookup_mnt(path);
999 if (!mounted)
1000 break;
1001 dput(path->dentry);
1002 mntput(path->mnt);
1003 path->mnt = mounted;
1004 path->dentry = dget(mounted->mnt_root);
1005 continue;
1006 }
1007
1008 /* Don't handle automount points here */
1009 break;
1010 }
1011 return 0;
1012}
1013
1014/*
David Howells9875cf82011-01-14 18:45:21 +00001015 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1016 */
1017static void follow_mount(struct path *path)
1018{
1019 while (d_mountpoint(path->dentry)) {
1020 struct vfsmount *mounted = lookup_mnt(path);
1021 if (!mounted)
1022 break;
1023 dput(path->dentry);
1024 mntput(path->mnt);
1025 path->mnt = mounted;
1026 path->dentry = dget(mounted->mnt_root);
1027 }
1028}
1029
Nick Piggin31e6b012011-01-07 17:49:52 +11001030static void follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
Al Viro2a737872009-04-07 11:49:53 -04001032 set_root(nd);
Andreas Mohre518ddb2006-09-29 02:01:22 -07001033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 while(1) {
Jan Blunck4ac91372008-02-14 19:34:32 -08001035 struct dentry *old = nd->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Al Viro2a737872009-04-07 11:49:53 -04001037 if (nd->path.dentry == nd->root.dentry &&
1038 nd->path.mnt == nd->root.mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 break;
1040 }
Jan Blunck4ac91372008-02-14 19:34:32 -08001041 if (nd->path.dentry != nd->path.mnt->mnt_root) {
Al Viro3088dd72010-01-30 15:47:29 -05001042 /* rare case of legitimate dget_parent()... */
1043 nd->path.dentry = dget_parent(nd->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 dput(old);
1045 break;
1046 }
Al Viro3088dd72010-01-30 15:47:29 -05001047 if (!follow_up(&nd->path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 }
Al Viro79ed0222009-04-18 13:59:41 -04001050 follow_mount(&nd->path);
Nick Piggin31e6b012011-01-07 17:49:52 +11001051 nd->inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052}
1053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054/*
Nick Pigginbaa03892010-08-18 04:37:31 +10001055 * Allocate a dentry with name and parent, and perform a parent
1056 * directory ->lookup on it. Returns the new dentry, or ERR_PTR
1057 * on error. parent->d_inode->i_mutex must be held. d_lookup must
1058 * have verified that no child exists while under i_mutex.
1059 */
1060static struct dentry *d_alloc_and_lookup(struct dentry *parent,
1061 struct qstr *name, struct nameidata *nd)
1062{
1063 struct inode *inode = parent->d_inode;
1064 struct dentry *dentry;
1065 struct dentry *old;
1066
1067 /* Don't create child dentry for a dead directory. */
1068 if (unlikely(IS_DEADDIR(inode)))
1069 return ERR_PTR(-ENOENT);
1070
1071 dentry = d_alloc(parent, name);
1072 if (unlikely(!dentry))
1073 return ERR_PTR(-ENOMEM);
1074
1075 old = inode->i_op->lookup(inode, dentry, nd);
1076 if (unlikely(old)) {
1077 dput(dentry);
1078 dentry = old;
1079 }
1080 return dentry;
1081}
1082
1083/*
Josef Bacik44396f42011-05-31 11:58:49 -04001084 * We already have a dentry, but require a lookup to be performed on the parent
1085 * directory to fill in d_inode. Returns the new dentry, or ERR_PTR on error.
1086 * parent->d_inode->i_mutex must be held. d_lookup must have verified that no
1087 * child exists while under i_mutex.
1088 */
1089static struct dentry *d_inode_lookup(struct dentry *parent, struct dentry *dentry,
1090 struct nameidata *nd)
1091{
1092 struct inode *inode = parent->d_inode;
1093 struct dentry *old;
1094
1095 /* Don't create child dentry for a dead directory. */
1096 if (unlikely(IS_DEADDIR(inode)))
1097 return ERR_PTR(-ENOENT);
1098
1099 old = inode->i_op->lookup(inode, dentry, nd);
1100 if (unlikely(old)) {
1101 dput(dentry);
1102 dentry = old;
1103 }
1104 return dentry;
1105}
1106
1107/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 * It's more convoluted than I'd like it to be, but... it's still fairly
1109 * small and for now I'd prefer to have fast path as straight as possible.
1110 * It _is_ time-critical.
1111 */
1112static int do_lookup(struct nameidata *nd, struct qstr *name,
Nick Piggin31e6b012011-01-07 17:49:52 +11001113 struct path *path, struct inode **inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
Jan Blunck4ac91372008-02-14 19:34:32 -08001115 struct vfsmount *mnt = nd->path.mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +11001116 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro5a18fff2011-03-11 04:44:53 -05001117 int need_reval = 1;
1118 int status = 1;
David Howells9875cf82011-01-14 18:45:21 +00001119 int err;
1120
Al Viro3cac2602009-08-13 18:27:43 +04001121 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001122 * Rename seqlock is not required here because in the off chance
1123 * of a false negative due to a concurrent rename, we're going to
1124 * do the non-racy lookup, below.
1125 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001126 if (nd->flags & LOOKUP_RCU) {
1127 unsigned seq;
Nick Piggin31e6b012011-01-07 17:49:52 +11001128 *inode = nd->inode;
1129 dentry = __d_lookup_rcu(parent, name, &seq, inode);
Al Viro5a18fff2011-03-11 04:44:53 -05001130 if (!dentry)
1131 goto unlazy;
1132
Nick Piggin31e6b012011-01-07 17:49:52 +11001133 /* Memory barrier in read_seqcount_begin of child is enough */
1134 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1135 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001136 nd->seq = seq;
Al Viro5a18fff2011-03-11 04:44:53 -05001137
Al Viro24643082011-02-15 01:26:22 -05001138 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
Al Viro5a18fff2011-03-11 04:44:53 -05001139 status = d_revalidate(dentry, nd);
1140 if (unlikely(status <= 0)) {
1141 if (status != -ECHILD)
1142 need_reval = 0;
1143 goto unlazy;
1144 }
Al Viro24643082011-02-15 01:26:22 -05001145 }
Josef Bacik44396f42011-05-31 11:58:49 -04001146 if (unlikely(d_need_lookup(dentry)))
1147 goto unlazy;
Nick Piggin31e6b012011-01-07 17:49:52 +11001148 path->mnt = mnt;
1149 path->dentry = dentry;
Al Virod6e9bd22011-05-27 07:03:15 -04001150 if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1151 goto unlazy;
1152 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1153 goto unlazy;
1154 return 0;
Al Viro5a18fff2011-03-11 04:44:53 -05001155unlazy:
Al Viro19660af2011-03-25 10:32:48 -04001156 if (unlazy_walk(nd, dentry))
1157 return -ECHILD;
Al Viro5a18fff2011-03-11 04:44:53 -05001158 } else {
1159 dentry = __d_lookup(parent, name);
Nick Piggin31e6b012011-01-07 17:49:52 +11001160 }
Al Viro5a18fff2011-03-11 04:44:53 -05001161
Josef Bacik44396f42011-05-31 11:58:49 -04001162 if (dentry && unlikely(d_need_lookup(dentry))) {
1163 dput(dentry);
1164 dentry = NULL;
1165 }
Al Viro5a18fff2011-03-11 04:44:53 -05001166retry:
1167 if (unlikely(!dentry)) {
1168 struct inode *dir = parent->d_inode;
1169 BUG_ON(nd->inode != dir);
1170
1171 mutex_lock(&dir->i_mutex);
1172 dentry = d_lookup(parent, name);
1173 if (likely(!dentry)) {
1174 dentry = d_alloc_and_lookup(parent, name, nd);
1175 if (IS_ERR(dentry)) {
1176 mutex_unlock(&dir->i_mutex);
1177 return PTR_ERR(dentry);
1178 }
1179 /* known good */
1180 need_reval = 0;
1181 status = 1;
Josef Bacik44396f42011-05-31 11:58:49 -04001182 } else if (unlikely(d_need_lookup(dentry))) {
1183 dentry = d_inode_lookup(parent, dentry, nd);
1184 if (IS_ERR(dentry)) {
1185 mutex_unlock(&dir->i_mutex);
1186 return PTR_ERR(dentry);
1187 }
1188 /* known good */
1189 need_reval = 0;
1190 status = 1;
Al Viro5a18fff2011-03-11 04:44:53 -05001191 }
1192 mutex_unlock(&dir->i_mutex);
Al Viro24643082011-02-15 01:26:22 -05001193 }
Al Viro5a18fff2011-03-11 04:44:53 -05001194 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
1195 status = d_revalidate(dentry, nd);
1196 if (unlikely(status <= 0)) {
1197 if (status < 0) {
1198 dput(dentry);
1199 return status;
1200 }
1201 if (!d_invalidate(dentry)) {
1202 dput(dentry);
1203 dentry = NULL;
1204 need_reval = 1;
1205 goto retry;
1206 }
1207 }
1208
David Howells9875cf82011-01-14 18:45:21 +00001209 path->mnt = mnt;
1210 path->dentry = dentry;
1211 err = follow_managed(path, nd->flags);
Ian Kent89312212011-01-18 12:06:10 +08001212 if (unlikely(err < 0)) {
1213 path_put_conditional(path, nd);
David Howells9875cf82011-01-14 18:45:21 +00001214 return err;
Ian Kent89312212011-01-18 12:06:10 +08001215 }
David Howells9875cf82011-01-14 18:45:21 +00001216 *inode = path->dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218}
1219
Al Viro52094c82011-02-21 21:34:47 -05001220static inline int may_lookup(struct nameidata *nd)
1221{
1222 if (nd->flags & LOOKUP_RCU) {
Al Viroeecdd352011-06-20 19:48:41 -04001223 int err = exec_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
Al Viro52094c82011-02-21 21:34:47 -05001224 if (err != -ECHILD)
1225 return err;
Al Viro19660af2011-03-25 10:32:48 -04001226 if (unlazy_walk(nd, NULL))
Al Viro52094c82011-02-21 21:34:47 -05001227 return -ECHILD;
1228 }
Al Viroeecdd352011-06-20 19:48:41 -04001229 return exec_permission(nd->inode, MAY_EXEC);
Al Viro52094c82011-02-21 21:34:47 -05001230}
1231
Al Viro9856fa12011-03-04 14:22:06 -05001232static inline int handle_dots(struct nameidata *nd, int type)
1233{
1234 if (type == LAST_DOTDOT) {
1235 if (nd->flags & LOOKUP_RCU) {
1236 if (follow_dotdot_rcu(nd))
1237 return -ECHILD;
1238 } else
1239 follow_dotdot(nd);
1240 }
1241 return 0;
1242}
1243
Al Viro951361f2011-03-04 14:44:37 -05001244static void terminate_walk(struct nameidata *nd)
1245{
1246 if (!(nd->flags & LOOKUP_RCU)) {
1247 path_put(&nd->path);
1248 } else {
1249 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001250 if (!(nd->flags & LOOKUP_ROOT))
1251 nd->root.mnt = NULL;
Al Viro951361f2011-03-04 14:44:37 -05001252 rcu_read_unlock();
1253 br_read_unlock(vfsmount_lock);
1254 }
1255}
1256
Al Viroce57dfc2011-03-13 19:58:58 -04001257static inline int walk_component(struct nameidata *nd, struct path *path,
1258 struct qstr *name, int type, int follow)
1259{
1260 struct inode *inode;
1261 int err;
1262 /*
1263 * "." and ".." are special - ".." especially so because it has
1264 * to be able to know about the current root directory and
1265 * parent relationships.
1266 */
1267 if (unlikely(type != LAST_NORM))
1268 return handle_dots(nd, type);
1269 err = do_lookup(nd, name, path, &inode);
1270 if (unlikely(err)) {
1271 terminate_walk(nd);
1272 return err;
1273 }
1274 if (!inode) {
1275 path_to_nameidata(path, nd);
1276 terminate_walk(nd);
1277 return -ENOENT;
1278 }
1279 if (unlikely(inode->i_op->follow_link) && follow) {
Al Viro19660af2011-03-25 10:32:48 -04001280 if (nd->flags & LOOKUP_RCU) {
1281 if (unlikely(unlazy_walk(nd, path->dentry))) {
1282 terminate_walk(nd);
1283 return -ECHILD;
1284 }
1285 }
Al Viroce57dfc2011-03-13 19:58:58 -04001286 BUG_ON(inode != path->dentry->d_inode);
1287 return 1;
1288 }
1289 path_to_nameidata(path, nd);
1290 nd->inode = inode;
1291 return 0;
1292}
1293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294/*
Al Virob3563792011-03-14 21:54:55 -04001295 * This limits recursive symlink follows to 8, while
1296 * limiting consecutive symlinks to 40.
1297 *
1298 * Without that kind of total limit, nasty chains of consecutive
1299 * symlinks can cause almost arbitrarily long lookups.
1300 */
1301static inline int nested_symlink(struct path *path, struct nameidata *nd)
1302{
1303 int res;
1304
Al Virob3563792011-03-14 21:54:55 -04001305 if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1306 path_put_conditional(path, nd);
1307 path_put(&nd->path);
1308 return -ELOOP;
1309 }
Erez Zadok1a4022f2011-05-21 01:19:59 -04001310 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
Al Virob3563792011-03-14 21:54:55 -04001311
1312 nd->depth++;
1313 current->link_count++;
1314
1315 do {
1316 struct path link = *path;
1317 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04001318
1319 res = follow_link(&link, nd, &cookie);
Al Virob3563792011-03-14 21:54:55 -04001320 if (!res)
1321 res = walk_component(nd, path, &nd->last,
1322 nd->last_type, LOOKUP_FOLLOW);
Al Viro574197e2011-03-14 22:20:34 -04001323 put_link(nd, &link, cookie);
Al Virob3563792011-03-14 21:54:55 -04001324 } while (res > 0);
1325
1326 current->link_count--;
1327 nd->depth--;
1328 return res;
1329}
1330
1331/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01001333 * This is the basic name resolution function, turning a pathname into
1334 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01001336 * Returns 0 and nd will have valid dentry and mnt on success.
1337 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 */
Al Viro6de88d72009-08-09 01:41:57 +04001339static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
1341 struct path next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 int err;
1343 unsigned int lookup_flags = nd->flags;
1344
1345 while (*name=='/')
1346 name++;
1347 if (!*name)
Al Viro086e1832011-02-22 20:56:27 -05001348 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 /* At this point we know we have a real path component. */
1351 for(;;) {
1352 unsigned long hash;
1353 struct qstr this;
1354 unsigned int c;
Al Virofe479a52011-02-22 15:10:03 -05001355 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Trond Myklebustcdce5d62005-10-18 14:20:18 -07001357 nd->flags |= LOOKUP_CONTINUE;
Al Viro52094c82011-02-21 21:34:47 -05001358
1359 err = may_lookup(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 if (err)
1361 break;
1362
1363 this.name = name;
1364 c = *(const unsigned char *)name;
1365
1366 hash = init_name_hash();
1367 do {
1368 name++;
1369 hash = partial_name_hash(c, hash);
1370 c = *(const unsigned char *)name;
1371 } while (c && (c != '/'));
1372 this.len = name - (const char *) this.name;
1373 this.hash = end_name_hash(hash);
1374
Al Virofe479a52011-02-22 15:10:03 -05001375 type = LAST_NORM;
1376 if (this.name[0] == '.') switch (this.len) {
1377 case 2:
Al Viro16c2cd72011-02-22 15:50:10 -05001378 if (this.name[1] == '.') {
Al Virofe479a52011-02-22 15:10:03 -05001379 type = LAST_DOTDOT;
Al Viro16c2cd72011-02-22 15:50:10 -05001380 nd->flags |= LOOKUP_JUMPED;
1381 }
Al Virofe479a52011-02-22 15:10:03 -05001382 break;
1383 case 1:
1384 type = LAST_DOT;
1385 }
Al Viro5a202bc2011-03-08 14:17:44 -05001386 if (likely(type == LAST_NORM)) {
1387 struct dentry *parent = nd->path.dentry;
Al Viro16c2cd72011-02-22 15:50:10 -05001388 nd->flags &= ~LOOKUP_JUMPED;
Al Viro5a202bc2011-03-08 14:17:44 -05001389 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1390 err = parent->d_op->d_hash(parent, nd->inode,
1391 &this);
1392 if (err < 0)
1393 break;
1394 }
1395 }
Al Virofe479a52011-02-22 15:10:03 -05001396
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 /* remove trailing slashes? */
1398 if (!c)
1399 goto last_component;
1400 while (*++name == '/');
1401 if (!*name)
Al Virob3563792011-03-14 21:54:55 -04001402 goto last_component;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Al Viroce57dfc2011-03-13 19:58:58 -04001404 err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1405 if (err < 0)
1406 return err;
Al Virofe479a52011-02-22 15:10:03 -05001407
Al Viroce57dfc2011-03-13 19:58:58 -04001408 if (err) {
Al Virob3563792011-03-14 21:54:55 -04001409 err = nested_symlink(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 if (err)
Al Viroa7472ba2011-03-04 14:39:30 -05001411 return err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 err = -ENOTDIR;
Nick Piggin31e6b012011-01-07 17:49:52 +11001414 if (!nd->inode->i_op->lookup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 break;
1416 continue;
1417 /* here ends the main loop */
1418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419last_component:
Trond Myklebustf55eab82006-02-04 23:28:01 -08001420 /* Clear LOOKUP_CONTINUE iff it was previously unset */
1421 nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
Al Virob3563792011-03-14 21:54:55 -04001422 nd->last = this;
1423 nd->last_type = type;
Al Viro086e1832011-02-22 20:56:27 -05001424 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 }
Al Viro951361f2011-03-04 14:44:37 -05001426 terminate_walk(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 return err;
1428}
1429
Al Viro70e9b352011-03-05 21:12:22 -05001430static int path_init(int dfd, const char *name, unsigned int flags,
1431 struct nameidata *nd, struct file **fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432{
Prasanna Medaea3834d2005-04-29 16:00:17 +01001433 int retval = 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001434 int fput_needed;
1435 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
1437 nd->last_type = LAST_ROOT; /* if there are only slashes... */
Al Viro16c2cd72011-02-22 15:50:10 -05001438 nd->flags = flags | LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 nd->depth = 0;
Al Viro5b6ca022011-03-09 23:04:47 -05001440 if (flags & LOOKUP_ROOT) {
1441 struct inode *inode = nd->root.dentry->d_inode;
Al Viro73d049a2011-03-11 12:08:24 -05001442 if (*name) {
1443 if (!inode->i_op->lookup)
1444 return -ENOTDIR;
1445 retval = inode_permission(inode, MAY_EXEC);
1446 if (retval)
1447 return retval;
1448 }
Al Viro5b6ca022011-03-09 23:04:47 -05001449 nd->path = nd->root;
1450 nd->inode = inode;
1451 if (flags & LOOKUP_RCU) {
1452 br_read_lock(vfsmount_lock);
1453 rcu_read_lock();
1454 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1455 } else {
1456 path_get(&nd->path);
1457 }
1458 return 0;
1459 }
1460
Al Viro2a737872009-04-07 11:49:53 -04001461 nd->root.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 if (*name=='/') {
Al Viroe41f7d42011-02-22 14:02:58 -05001464 if (flags & LOOKUP_RCU) {
1465 br_read_lock(vfsmount_lock);
1466 rcu_read_lock();
1467 set_root_rcu(nd);
1468 } else {
1469 set_root(nd);
1470 path_get(&nd->root);
1471 }
Al Viro2a737872009-04-07 11:49:53 -04001472 nd->path = nd->root;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001473 } else if (dfd == AT_FDCWD) {
Al Viroe41f7d42011-02-22 14:02:58 -05001474 if (flags & LOOKUP_RCU) {
1475 struct fs_struct *fs = current->fs;
1476 unsigned seq;
1477
1478 br_read_lock(vfsmount_lock);
1479 rcu_read_lock();
1480
1481 do {
1482 seq = read_seqcount_begin(&fs->seq);
1483 nd->path = fs->pwd;
1484 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1485 } while (read_seqcount_retry(&fs->seq, seq));
1486 } else {
1487 get_fs_pwd(current->fs, &nd->path);
1488 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001489 } else {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001490 struct dentry *dentry;
1491
Al Viro1abf0c72011-03-13 03:51:11 -04001492 file = fget_raw_light(dfd, &fput_needed);
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001493 retval = -EBADF;
1494 if (!file)
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001495 goto out_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001496
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001497 dentry = file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001498
Al Virof52e0c12011-03-14 18:56:51 -04001499 if (*name) {
1500 retval = -ENOTDIR;
1501 if (!S_ISDIR(dentry->d_inode->i_mode))
1502 goto fput_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001503
Al Viroeecdd352011-06-20 19:48:41 -04001504 retval = exec_permission(dentry->d_inode, MAY_EXEC);
Al Virof52e0c12011-03-14 18:56:51 -04001505 if (retval)
1506 goto fput_fail;
1507 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001508
Jan Blunck5dd784d2008-02-14 19:34:38 -08001509 nd->path = file->f_path;
Al Viroe41f7d42011-02-22 14:02:58 -05001510 if (flags & LOOKUP_RCU) {
1511 if (fput_needed)
Al Viro70e9b352011-03-05 21:12:22 -05001512 *fp = file;
Al Viroe41f7d42011-02-22 14:02:58 -05001513 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1514 br_read_lock(vfsmount_lock);
1515 rcu_read_lock();
1516 } else {
1517 path_get(&file->f_path);
1518 fput_light(file, fput_needed);
1519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 }
Al Viroe41f7d42011-02-22 14:02:58 -05001521
Nick Piggin31e6b012011-01-07 17:49:52 +11001522 nd->inode = nd->path.dentry->d_inode;
Al Viro9b4a9b12009-04-07 11:44:16 -04001523 return 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001524
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001525fput_fail:
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001526 fput_light(file, fput_needed);
Al Viro9b4a9b12009-04-07 11:44:16 -04001527out_fail:
1528 return retval;
1529}
1530
Al Virobd92d7f2011-03-14 19:54:59 -04001531static inline int lookup_last(struct nameidata *nd, struct path *path)
1532{
1533 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1534 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1535
1536 nd->flags &= ~LOOKUP_PARENT;
1537 return walk_component(nd, path, &nd->last, nd->last_type,
1538 nd->flags & LOOKUP_FOLLOW);
1539}
1540
Al Viro9b4a9b12009-04-07 11:44:16 -04001541/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroee0827c2011-02-21 23:38:09 -05001542static int path_lookupat(int dfd, const char *name,
Al Viro9b4a9b12009-04-07 11:44:16 -04001543 unsigned int flags, struct nameidata *nd)
1544{
Al Viro70e9b352011-03-05 21:12:22 -05001545 struct file *base = NULL;
Al Virobd92d7f2011-03-14 19:54:59 -04001546 struct path path;
1547 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001548
1549 /*
1550 * Path walking is largely split up into 2 different synchronisation
1551 * schemes, rcu-walk and ref-walk (explained in
1552 * Documentation/filesystems/path-lookup.txt). These share much of the
1553 * path walk code, but some things particularly setup, cleanup, and
1554 * following mounts are sufficiently divergent that functions are
1555 * duplicated. Typically there is a function foo(), and its RCU
1556 * analogue, foo_rcu().
1557 *
1558 * -ECHILD is the error number of choice (just to avoid clashes) that
1559 * is returned if some aspect of an rcu-walk fails. Such an error must
1560 * be handled by restarting a traditional ref-walk (which will always
1561 * be able to complete).
1562 */
Al Virobd92d7f2011-03-14 19:54:59 -04001563 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
Al Viroee0827c2011-02-21 23:38:09 -05001564
Al Virobd92d7f2011-03-14 19:54:59 -04001565 if (unlikely(err))
1566 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001567
1568 current->total_link_count = 0;
Al Virobd92d7f2011-03-14 19:54:59 -04001569 err = link_path_walk(name, nd);
1570
1571 if (!err && !(flags & LOOKUP_PARENT)) {
Al Virobd92d7f2011-03-14 19:54:59 -04001572 err = lookup_last(nd, &path);
1573 while (err > 0) {
1574 void *cookie;
1575 struct path link = path;
Al Virobd92d7f2011-03-14 19:54:59 -04001576 nd->flags |= LOOKUP_PARENT;
Al Viro574197e2011-03-14 22:20:34 -04001577 err = follow_link(&link, nd, &cookie);
Al Virobd92d7f2011-03-14 19:54:59 -04001578 if (!err)
1579 err = lookup_last(nd, &path);
Al Viro574197e2011-03-14 22:20:34 -04001580 put_link(nd, &link, cookie);
Al Virobd92d7f2011-03-14 19:54:59 -04001581 }
1582 }
Al Viroee0827c2011-02-21 23:38:09 -05001583
Al Viro9f1fafe2011-03-25 11:00:12 -04001584 if (!err)
1585 err = complete_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04001586
1587 if (!err && nd->flags & LOOKUP_DIRECTORY) {
1588 if (!nd->inode->i_op->lookup) {
1589 path_put(&nd->path);
Al Virobd23a532011-03-23 09:56:30 -04001590 err = -ENOTDIR;
Al Virobd92d7f2011-03-14 19:54:59 -04001591 }
1592 }
Al Viro16c2cd72011-02-22 15:50:10 -05001593
Al Viro70e9b352011-03-05 21:12:22 -05001594 if (base)
1595 fput(base);
Al Viroee0827c2011-02-21 23:38:09 -05001596
Al Viro5b6ca022011-03-09 23:04:47 -05001597 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
Al Viro2a737872009-04-07 11:49:53 -04001598 path_put(&nd->root);
1599 nd->root.mnt = NULL;
1600 }
Al Virobd92d7f2011-03-14 19:54:59 -04001601 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001602}
Nick Piggin31e6b012011-01-07 17:49:52 +11001603
Al Viroee0827c2011-02-21 23:38:09 -05001604static int do_path_lookup(int dfd, const char *name,
1605 unsigned int flags, struct nameidata *nd)
1606{
1607 int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1608 if (unlikely(retval == -ECHILD))
1609 retval = path_lookupat(dfd, name, flags, nd);
1610 if (unlikely(retval == -ESTALE))
1611 retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11001612
1613 if (likely(!retval)) {
1614 if (unlikely(!audit_dummy_context())) {
1615 if (nd->path.dentry && nd->inode)
1616 audit_inode(name, nd->path.dentry);
1617 }
1618 }
Al Viro9b4a9b12009-04-07 11:44:16 -04001619 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620}
1621
Al Viroc9c6cac2011-02-16 15:15:47 -05001622int kern_path_parent(const char *name, struct nameidata *nd)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001623{
Al Viroc9c6cac2011-02-16 15:15:47 -05001624 return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001625}
1626
Al Virod1811462008-08-02 00:49:18 -04001627int kern_path(const char *name, unsigned int flags, struct path *path)
1628{
1629 struct nameidata nd;
1630 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1631 if (!res)
1632 *path = nd.path;
1633 return res;
1634}
1635
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001636/**
1637 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1638 * @dentry: pointer to dentry of the base directory
1639 * @mnt: pointer to vfs mount of the base directory
1640 * @name: pointer to file name
1641 * @flags: lookup flags
1642 * @nd: pointer to nameidata
1643 */
1644int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
1645 const char *name, unsigned int flags,
1646 struct nameidata *nd)
1647{
Al Viro5b6ca022011-03-09 23:04:47 -05001648 nd->root.dentry = dentry;
1649 nd->root.mnt = mnt;
1650 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
1651 return do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, nd);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001652}
1653
Christoph Hellwigeead1912007-10-16 23:25:38 -07001654static struct dentry *__lookup_hash(struct qstr *name,
1655 struct dentry *base, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656{
Christoph Hellwig81fca442010-10-06 10:47:47 +02001657 struct inode *inode = base->d_inode;
James Morris057f6c02007-04-26 00:12:05 -07001658 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 int err;
1660
Al Viroeecdd352011-06-20 19:48:41 -04001661 err = exec_permission(inode, MAY_EXEC);
Christoph Hellwig81fca442010-10-06 10:47:47 +02001662 if (err)
1663 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
1665 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001666 * Don't bother with __d_lookup: callers are for creat as
1667 * well as unlink, so a lot of the time it would cost
1668 * a double lookup.
Al Viro6e6b1bd2009-08-13 23:38:37 +04001669 */
Nick Pigginb04f7842010-08-18 04:37:34 +10001670 dentry = d_lookup(base, name);
Al Viro6e6b1bd2009-08-13 23:38:37 +04001671
Josef Bacik44396f42011-05-31 11:58:49 -04001672 if (dentry && d_need_lookup(dentry)) {
1673 /*
1674 * __lookup_hash is called with the parent dir's i_mutex already
1675 * held, so we are good to go here.
1676 */
1677 dentry = d_inode_lookup(base, dentry, nd);
1678 if (IS_ERR(dentry))
1679 return dentry;
1680 }
1681
Nick Pigginfb045ad2011-01-07 17:49:55 +11001682 if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE))
Al Viro6e6b1bd2009-08-13 23:38:37 +04001683 dentry = do_revalidate(dentry, nd);
1684
Nick Pigginbaa03892010-08-18 04:37:31 +10001685 if (!dentry)
1686 dentry = d_alloc_and_lookup(base, name, nd);
Al Viro5a202bc2011-03-08 14:17:44 -05001687
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 return dentry;
1689}
1690
James Morris057f6c02007-04-26 00:12:05 -07001691/*
1692 * Restricted form of lookup. Doesn't follow links, single-component only,
1693 * needs parent already locked. Doesn't follow mounts.
1694 * SMP-safe.
1695 */
Adrian Bunka244e162006-03-31 02:32:11 -08001696static struct dentry *lookup_hash(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697{
Jan Blunck4ac91372008-02-14 19:34:32 -08001698 return __lookup_hash(&nd->last, nd->path.dentry, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699}
1700
Christoph Hellwigeead1912007-10-16 23:25:38 -07001701/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07001702 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07001703 * @name: pathname component to lookup
1704 * @base: base directory to lookup from
1705 * @len: maximum length @len should be interpreted to
1706 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07001707 * Note that this routine is purely a helper for filesystem usage and should
1708 * not be called by generic code. Also note that by using this function the
Christoph Hellwigeead1912007-10-16 23:25:38 -07001709 * nameidata argument is passed to the filesystem methods and a filesystem
1710 * using this helper needs to be prepared for that.
1711 */
James Morris057f6c02007-04-26 00:12:05 -07001712struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1713{
James Morris057f6c02007-04-26 00:12:05 -07001714 struct qstr this;
Al Viro6a96ba52011-03-07 23:49:20 -05001715 unsigned long hash;
1716 unsigned int c;
James Morris057f6c02007-04-26 00:12:05 -07001717
David Woodhouse2f9092e2009-04-20 23:18:37 +01001718 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
1719
Al Viro6a96ba52011-03-07 23:49:20 -05001720 this.name = name;
1721 this.len = len;
1722 if (!len)
1723 return ERR_PTR(-EACCES);
1724
1725 hash = init_name_hash();
1726 while (len--) {
1727 c = *(const unsigned char *)name++;
1728 if (c == '/' || c == '\0')
1729 return ERR_PTR(-EACCES);
1730 hash = partial_name_hash(c, hash);
1731 }
1732 this.hash = end_name_hash(hash);
Al Viro5a202bc2011-03-08 14:17:44 -05001733 /*
1734 * See if the low-level filesystem might want
1735 * to use its own hash..
1736 */
1737 if (base->d_flags & DCACHE_OP_HASH) {
1738 int err = base->d_op->d_hash(base, base->d_inode, &this);
1739 if (err < 0)
1740 return ERR_PTR(err);
1741 }
Christoph Hellwigeead1912007-10-16 23:25:38 -07001742
Christoph Hellwig49705b72005-11-08 21:35:06 -08001743 return __lookup_hash(&this, base, NULL);
James Morris057f6c02007-04-26 00:12:05 -07001744}
1745
Al Viro2d8f3032008-07-22 09:59:21 -04001746int user_path_at(int dfd, const char __user *name, unsigned flags,
1747 struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748{
Al Viro2d8f3032008-07-22 09:59:21 -04001749 struct nameidata nd;
Al Virof52e0c12011-03-14 18:56:51 -04001750 char *tmp = getname_flags(name, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 int err = PTR_ERR(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 if (!IS_ERR(tmp)) {
Al Viro2d8f3032008-07-22 09:59:21 -04001753
1754 BUG_ON(flags & LOOKUP_PARENT);
1755
1756 err = do_path_lookup(dfd, tmp, flags, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 putname(tmp);
Al Viro2d8f3032008-07-22 09:59:21 -04001758 if (!err)
1759 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 }
1761 return err;
1762}
1763
Al Viro2ad94ae2008-07-21 09:32:51 -04001764static int user_path_parent(int dfd, const char __user *path,
1765 struct nameidata *nd, char **name)
1766{
1767 char *s = getname(path);
1768 int error;
1769
1770 if (IS_ERR(s))
1771 return PTR_ERR(s);
1772
1773 error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
1774 if (error)
1775 putname(s);
1776 else
1777 *name = s;
1778
1779 return error;
1780}
1781
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782/*
1783 * It's inline, so penalty for filesystems that don't use sticky bit is
1784 * minimal.
1785 */
1786static inline int check_sticky(struct inode *dir, struct inode *inode)
1787{
David Howellsda9592e2008-11-14 10:39:05 +11001788 uid_t fsuid = current_fsuid();
1789
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 if (!(dir->i_mode & S_ISVTX))
1791 return 0;
Serge E. Hallyne795b712011-03-23 16:43:25 -07001792 if (current_user_ns() != inode_userns(inode))
1793 goto other_userns;
David Howellsda9592e2008-11-14 10:39:05 +11001794 if (inode->i_uid == fsuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 return 0;
David Howellsda9592e2008-11-14 10:39:05 +11001796 if (dir->i_uid == fsuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 return 0;
Serge E. Hallyne795b712011-03-23 16:43:25 -07001798
1799other_userns:
1800 return !ns_capable(inode_userns(inode), CAP_FOWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801}
1802
1803/*
1804 * Check whether we can remove a link victim from directory dir, check
1805 * whether the type of victim is right.
1806 * 1. We can't do it if dir is read-only (done in permission())
1807 * 2. We should have write and exec permissions on dir
1808 * 3. We can't remove anything from append-only dir
1809 * 4. We can't do anything with immutable dir (done in permission())
1810 * 5. If the sticky bit on dir is set we should either
1811 * a. be owner of dir, or
1812 * b. be owner of victim, or
1813 * c. have CAP_FOWNER capability
1814 * 6. If the victim is append-only or immutable we can't do antyhing with
1815 * links pointing to it.
1816 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1817 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1818 * 9. We can't remove a root or mountpoint.
1819 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1820 * nfs_async_unlink().
1821 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001822static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823{
1824 int error;
1825
1826 if (!victim->d_inode)
1827 return -ENOENT;
1828
1829 BUG_ON(victim->d_parent->d_inode != dir);
Al Virocccc6bb2009-12-25 05:07:33 -05001830 audit_inode_child(victim, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Al Virof419a2e2008-07-22 00:07:17 -04001832 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 if (error)
1834 return error;
1835 if (IS_APPEND(dir))
1836 return -EPERM;
1837 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
Hugh Dickinsf9454542008-11-19 15:36:38 -08001838 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 return -EPERM;
1840 if (isdir) {
1841 if (!S_ISDIR(victim->d_inode->i_mode))
1842 return -ENOTDIR;
1843 if (IS_ROOT(victim))
1844 return -EBUSY;
1845 } else if (S_ISDIR(victim->d_inode->i_mode))
1846 return -EISDIR;
1847 if (IS_DEADDIR(dir))
1848 return -ENOENT;
1849 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1850 return -EBUSY;
1851 return 0;
1852}
1853
1854/* Check whether we can create an object with dentry child in directory
1855 * dir.
1856 * 1. We can't do it if child already exists (open has special treatment for
1857 * this case, but since we are inlined it's OK)
1858 * 2. We can't do it if dir is read-only (done in permission())
1859 * 3. We should have write and exec permissions on dir
1860 * 4. We can't do it if dir is immutable (done in permission())
1861 */
Miklos Szeredia95164d2008-07-30 15:08:48 +02001862static inline int may_create(struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863{
1864 if (child->d_inode)
1865 return -EEXIST;
1866 if (IS_DEADDIR(dir))
1867 return -ENOENT;
Al Virof419a2e2008-07-22 00:07:17 -04001868 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869}
1870
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871/*
1872 * p1 and p2 should be directories on the same fs.
1873 */
1874struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1875{
1876 struct dentry *p;
1877
1878 if (p1 == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07001879 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 return NULL;
1881 }
1882
Arjan van de Vena11f3a02006-03-23 03:00:33 -08001883 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001885 p = d_ancestor(p2, p1);
1886 if (p) {
1887 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1888 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
1889 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 }
1891
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001892 p = d_ancestor(p1, p2);
1893 if (p) {
1894 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1895 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1896 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 }
1898
Ingo Molnarf2eace22006-07-03 00:25:05 -07001899 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1900 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 return NULL;
1902}
1903
1904void unlock_rename(struct dentry *p1, struct dentry *p2)
1905{
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001906 mutex_unlock(&p1->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 if (p1 != p2) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001908 mutex_unlock(&p2->d_inode->i_mutex);
Arjan van de Vena11f3a02006-03-23 03:00:33 -08001909 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 }
1911}
1912
1913int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1914 struct nameidata *nd)
1915{
Miklos Szeredia95164d2008-07-30 15:08:48 +02001916 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
1918 if (error)
1919 return error;
1920
Al Viroacfa4382008-12-04 10:06:33 -05001921 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 return -EACCES; /* shouldn't it be ENOSYS? */
1923 mode &= S_IALLUGO;
1924 mode |= S_IFREG;
1925 error = security_inode_create(dir, dentry, mode);
1926 if (error)
1927 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 error = dir->i_op->create(dir, dentry, mode, nd);
Stephen Smalleya74574a2005-09-09 13:01:44 -07001929 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00001930 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 return error;
1932}
1933
Al Viro73d049a2011-03-11 12:08:24 -05001934static int may_open(struct path *path, int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001936 struct dentry *dentry = path->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 struct inode *inode = dentry->d_inode;
1938 int error;
1939
Al Virobcda7652011-03-13 16:42:14 -04001940 /* O_PATH? */
1941 if (!acc_mode)
1942 return 0;
1943
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 if (!inode)
1945 return -ENOENT;
1946
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001947 switch (inode->i_mode & S_IFMT) {
1948 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001950 case S_IFDIR:
1951 if (acc_mode & MAY_WRITE)
1952 return -EISDIR;
1953 break;
1954 case S_IFBLK:
1955 case S_IFCHR:
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001956 if (path->mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001958 /*FALLTHRU*/
1959 case S_IFIFO:
1960 case S_IFSOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001962 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08001963 }
Dave Hansenb41572e2007-10-16 23:31:14 -07001964
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001965 error = inode_permission(inode, acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07001966 if (error)
1967 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05001968
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 /*
1970 * An append-only file must be opened in append mode for writing.
1971 */
1972 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05001973 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05001974 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05001976 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 }
1978
1979 /* O_NOATIME can only be set by the owner or superuser */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001980 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
Al Viro7715b522009-12-16 03:54:00 -05001981 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
1983 /*
1984 * Ensure there are no outstanding leases on the file.
1985 */
Al Virob65a9cf2009-12-16 06:27:40 -05001986 return break_lease(inode, flag);
Al Viro7715b522009-12-16 03:54:00 -05001987}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
Jeff Laytone1181ee2010-12-07 16:19:50 -05001989static int handle_truncate(struct file *filp)
Al Viro7715b522009-12-16 03:54:00 -05001990{
Jeff Laytone1181ee2010-12-07 16:19:50 -05001991 struct path *path = &filp->f_path;
Al Viro7715b522009-12-16 03:54:00 -05001992 struct inode *inode = path->dentry->d_inode;
1993 int error = get_write_access(inode);
1994 if (error)
1995 return error;
1996 /*
1997 * Refuse to truncate files with mandatory locks held on them.
1998 */
1999 error = locks_verify_locked(inode);
2000 if (!error)
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +09002001 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05002002 if (!error) {
2003 error = do_truncate(path->dentry, 0,
2004 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
Jeff Laytone1181ee2010-12-07 16:19:50 -05002005 filp);
Al Viro7715b522009-12-16 03:54:00 -05002006 }
2007 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04002008 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009}
2010
Dave Hansend57999e2008-02-15 14:37:27 -08002011/*
Dave Hansend57999e2008-02-15 14:37:27 -08002012 * Note that while the flag value (low two bits) for sys_open means:
2013 * 00 - read-only
2014 * 01 - write-only
2015 * 10 - read-write
2016 * 11 - special
2017 * it is changed into
2018 * 00 - no permissions needed
2019 * 01 - read-permission
2020 * 10 - write-permission
2021 * 11 - read-write
2022 * for the internal routines (ie open_namei()/follow_link() etc)
2023 * This is more logical, and also allows the 00 "no perm needed"
2024 * to be used for symlinks (where the permissions are checked
2025 * later).
2026 *
2027*/
2028static inline int open_to_namei_flags(int flag)
2029{
2030 if ((flag+1) & O_ACCMODE)
2031 flag++;
2032 return flag;
2033}
2034
Nick Piggin31e6b012011-01-07 17:49:52 +11002035/*
Al Virofe2d35f2011-03-05 22:58:25 -05002036 * Handle the last step of open()
Nick Piggin31e6b012011-01-07 17:49:52 +11002037 */
Al Virofb1cc552009-12-24 01:58:28 -05002038static struct file *do_last(struct nameidata *nd, struct path *path,
Al Viroc3e380b2011-02-23 13:39:45 -05002039 const struct open_flags *op, const char *pathname)
Al Virofb1cc552009-12-24 01:58:28 -05002040{
Al Viroa1e28032009-12-24 02:12:06 -05002041 struct dentry *dir = nd->path.dentry;
Al Viro6c0d46c2011-03-09 00:59:59 -05002042 struct dentry *dentry;
Al Viroca344a892011-03-09 00:36:45 -05002043 int open_flag = op->open_flag;
Al Viro6c0d46c2011-03-09 00:59:59 -05002044 int will_truncate = open_flag & O_TRUNC;
Al Viroca344a892011-03-09 00:36:45 -05002045 int want_write = 0;
Al Virobcda7652011-03-13 16:42:14 -04002046 int acc_mode = op->acc_mode;
Al Virofb1cc552009-12-24 01:58:28 -05002047 struct file *filp;
Al Viro16c2cd72011-02-22 15:50:10 -05002048 int error;
Al Virofb1cc552009-12-24 01:58:28 -05002049
Al Viroc3e380b2011-02-23 13:39:45 -05002050 nd->flags &= ~LOOKUP_PARENT;
2051 nd->flags |= op->intent;
2052
Al Viro1f36f772009-12-26 10:56:19 -05002053 switch (nd->last_type) {
2054 case LAST_DOTDOT:
Neil Brown176306f2010-05-24 16:57:56 +10002055 case LAST_DOT:
Al Virofe2d35f2011-03-05 22:58:25 -05002056 error = handle_dots(nd, nd->last_type);
2057 if (error)
2058 return ERR_PTR(error);
Al Viro1f36f772009-12-26 10:56:19 -05002059 /* fallthrough */
Al Viro1f36f772009-12-26 10:56:19 -05002060 case LAST_ROOT:
Al Viro9f1fafe2011-03-25 11:00:12 -04002061 error = complete_walk(nd);
Al Viro16c2cd72011-02-22 15:50:10 -05002062 if (error)
Al Viro9f1fafe2011-03-25 11:00:12 -04002063 return ERR_PTR(error);
Al Virofe2d35f2011-03-05 22:58:25 -05002064 audit_inode(pathname, nd->path.dentry);
Al Viroca344a892011-03-09 00:36:45 -05002065 if (open_flag & O_CREAT) {
Al Virofe2d35f2011-03-05 22:58:25 -05002066 error = -EISDIR;
2067 goto exit;
2068 }
2069 goto ok;
Al Viro1f36f772009-12-26 10:56:19 -05002070 case LAST_BIND:
Al Viro9f1fafe2011-03-25 11:00:12 -04002071 error = complete_walk(nd);
Al Viro16c2cd72011-02-22 15:50:10 -05002072 if (error)
Al Viro9f1fafe2011-03-25 11:00:12 -04002073 return ERR_PTR(error);
Al Viro1f36f772009-12-26 10:56:19 -05002074 audit_inode(pathname, dir);
Al Viro67ee3ad2009-12-26 07:01:01 -05002075 goto ok;
Al Viro1f36f772009-12-26 10:56:19 -05002076 }
Al Viro67ee3ad2009-12-26 07:01:01 -05002077
Al Viroca344a892011-03-09 00:36:45 -05002078 if (!(open_flag & O_CREAT)) {
Al Virobcda7652011-03-13 16:42:14 -04002079 int symlink_ok = 0;
Al Virofe2d35f2011-03-05 22:58:25 -05002080 if (nd->last.name[nd->last.len])
2081 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
Al Virobcda7652011-03-13 16:42:14 -04002082 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2083 symlink_ok = 1;
Al Virofe2d35f2011-03-05 22:58:25 -05002084 /* we _can_ be in RCU mode here */
Al Viroce57dfc2011-03-13 19:58:58 -04002085 error = walk_component(nd, path, &nd->last, LAST_NORM,
2086 !symlink_ok);
2087 if (error < 0)
Al Virofe2d35f2011-03-05 22:58:25 -05002088 return ERR_PTR(error);
Al Viroce57dfc2011-03-13 19:58:58 -04002089 if (error) /* symlink */
Al Virofe2d35f2011-03-05 22:58:25 -05002090 return NULL;
Al Virofe2d35f2011-03-05 22:58:25 -05002091 /* sayonara */
Al Viro9f1fafe2011-03-25 11:00:12 -04002092 error = complete_walk(nd);
2093 if (error)
2094 return ERR_PTR(-ECHILD);
Al Virofe2d35f2011-03-05 22:58:25 -05002095
2096 error = -ENOTDIR;
2097 if (nd->flags & LOOKUP_DIRECTORY) {
Al Viroce57dfc2011-03-13 19:58:58 -04002098 if (!nd->inode->i_op->lookup)
Al Virofe2d35f2011-03-05 22:58:25 -05002099 goto exit;
2100 }
2101 audit_inode(pathname, nd->path.dentry);
2102 goto ok;
2103 }
2104
2105 /* create side of things */
Al Viro9f1fafe2011-03-25 11:00:12 -04002106 error = complete_walk(nd);
2107 if (error)
2108 return ERR_PTR(error);
Al Virofe2d35f2011-03-05 22:58:25 -05002109
2110 audit_inode(pathname, dir);
Al Viro16c2cd72011-02-22 15:50:10 -05002111 error = -EISDIR;
Al Viro1f36f772009-12-26 10:56:19 -05002112 /* trailing slashes? */
Nick Piggin31e6b012011-01-07 17:49:52 +11002113 if (nd->last.name[nd->last.len])
2114 goto exit;
Al Viroa2c36b42009-12-24 03:39:50 -05002115
Al Viroa1e28032009-12-24 02:12:06 -05002116 mutex_lock(&dir->d_inode->i_mutex);
2117
Al Viro6c0d46c2011-03-09 00:59:59 -05002118 dentry = lookup_hash(nd);
2119 error = PTR_ERR(dentry);
2120 if (IS_ERR(dentry)) {
Al Virofb1cc552009-12-24 01:58:28 -05002121 mutex_unlock(&dir->d_inode->i_mutex);
2122 goto exit;
2123 }
2124
Al Viro6c0d46c2011-03-09 00:59:59 -05002125 path->dentry = dentry;
2126 path->mnt = nd->path.mnt;
2127
Al Virofb1cc552009-12-24 01:58:28 -05002128 /* Negative dentry, just create the file */
Al Viro6c0d46c2011-03-09 00:59:59 -05002129 if (!dentry->d_inode) {
2130 int mode = op->mode;
2131 if (!IS_POSIXACL(dir->d_inode))
2132 mode &= ~current_umask();
Al Virofb1cc552009-12-24 01:58:28 -05002133 /*
2134 * This write is needed to ensure that a
Al Viro6c0d46c2011-03-09 00:59:59 -05002135 * rw->ro transition does not occur between
Al Virofb1cc552009-12-24 01:58:28 -05002136 * the time when the file is created and when
2137 * a permanent write count is taken through
2138 * the 'struct file' in nameidata_to_filp().
2139 */
2140 error = mnt_want_write(nd->path.mnt);
2141 if (error)
2142 goto exit_mutex_unlock;
Al Viroca344a892011-03-09 00:36:45 -05002143 want_write = 1;
Al Viro9b44f1b2011-03-09 00:17:27 -05002144 /* Don't check for write permission, don't truncate */
Al Viroca344a892011-03-09 00:36:45 -05002145 open_flag &= ~O_TRUNC;
Al Viro6c0d46c2011-03-09 00:59:59 -05002146 will_truncate = 0;
Al Virobcda7652011-03-13 16:42:14 -04002147 acc_mode = MAY_OPEN;
Al Viro6c0d46c2011-03-09 00:59:59 -05002148 error = security_path_mknod(&nd->path, dentry, mode, 0);
2149 if (error)
2150 goto exit_mutex_unlock;
2151 error = vfs_create(dir->d_inode, dentry, mode, nd);
2152 if (error)
2153 goto exit_mutex_unlock;
2154 mutex_unlock(&dir->d_inode->i_mutex);
2155 dput(nd->path.dentry);
2156 nd->path.dentry = dentry;
Al Viroca344a892011-03-09 00:36:45 -05002157 goto common;
Al Virofb1cc552009-12-24 01:58:28 -05002158 }
2159
2160 /*
2161 * It already exists.
2162 */
2163 mutex_unlock(&dir->d_inode->i_mutex);
2164 audit_inode(pathname, path->dentry);
2165
2166 error = -EEXIST;
Al Viroca344a892011-03-09 00:36:45 -05002167 if (open_flag & O_EXCL)
Al Virofb1cc552009-12-24 01:58:28 -05002168 goto exit_dput;
2169
David Howells9875cf82011-01-14 18:45:21 +00002170 error = follow_managed(path, nd->flags);
2171 if (error < 0)
2172 goto exit_dput;
Al Virofb1cc552009-12-24 01:58:28 -05002173
2174 error = -ENOENT;
2175 if (!path->dentry->d_inode)
2176 goto exit_dput;
Al Viro9e67f362009-12-26 07:04:50 -05002177
2178 if (path->dentry->d_inode->i_op->follow_link)
Al Virofb1cc552009-12-24 01:58:28 -05002179 return NULL;
Al Virofb1cc552009-12-24 01:58:28 -05002180
2181 path_to_nameidata(path, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11002182 nd->inode = path->dentry->d_inode;
Al Virofb1cc552009-12-24 01:58:28 -05002183 error = -EISDIR;
Nick Piggin31e6b012011-01-07 17:49:52 +11002184 if (S_ISDIR(nd->inode->i_mode))
Al Virofb1cc552009-12-24 01:58:28 -05002185 goto exit;
Al Viro67ee3ad2009-12-26 07:01:01 -05002186ok:
Al Viro6c0d46c2011-03-09 00:59:59 -05002187 if (!S_ISREG(nd->inode->i_mode))
2188 will_truncate = 0;
2189
Al Viro0f9d1a12011-03-09 00:13:14 -05002190 if (will_truncate) {
2191 error = mnt_want_write(nd->path.mnt);
2192 if (error)
2193 goto exit;
Al Viroca344a892011-03-09 00:36:45 -05002194 want_write = 1;
Al Viro0f9d1a12011-03-09 00:13:14 -05002195 }
Al Viroca344a892011-03-09 00:36:45 -05002196common:
Al Virobcda7652011-03-13 16:42:14 -04002197 error = may_open(&nd->path, acc_mode, open_flag);
Al Viroca344a892011-03-09 00:36:45 -05002198 if (error)
Al Viro0f9d1a12011-03-09 00:13:14 -05002199 goto exit;
Al Viro0f9d1a12011-03-09 00:13:14 -05002200 filp = nameidata_to_filp(nd);
2201 if (!IS_ERR(filp)) {
2202 error = ima_file_check(filp, op->acc_mode);
2203 if (error) {
2204 fput(filp);
2205 filp = ERR_PTR(error);
2206 }
2207 }
2208 if (!IS_ERR(filp)) {
2209 if (will_truncate) {
2210 error = handle_truncate(filp);
2211 if (error) {
2212 fput(filp);
2213 filp = ERR_PTR(error);
2214 }
2215 }
2216 }
Al Viroca344a892011-03-09 00:36:45 -05002217out:
2218 if (want_write)
Al Viro0f9d1a12011-03-09 00:13:14 -05002219 mnt_drop_write(nd->path.mnt);
2220 path_put(&nd->path);
Al Virofb1cc552009-12-24 01:58:28 -05002221 return filp;
2222
2223exit_mutex_unlock:
2224 mutex_unlock(&dir->d_inode->i_mutex);
2225exit_dput:
2226 path_put_conditional(path, nd);
2227exit:
Al Viroca344a892011-03-09 00:36:45 -05002228 filp = ERR_PTR(error);
2229 goto out;
Al Virofb1cc552009-12-24 01:58:28 -05002230}
2231
Al Viro13aab422011-02-23 17:54:08 -05002232static struct file *path_openat(int dfd, const char *pathname,
Al Viro73d049a2011-03-11 12:08:24 -05002233 struct nameidata *nd, const struct open_flags *op, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234{
Al Virofe2d35f2011-03-05 22:58:25 -05002235 struct file *base = NULL;
Dave Hansen4a3fd212008-02-15 14:37:48 -08002236 struct file *filp;
Al Viro9850c052010-01-13 15:01:15 -05002237 struct path path;
Al Viro13aab422011-02-23 17:54:08 -05002238 int error;
Nick Piggin31e6b012011-01-07 17:49:52 +11002239
2240 filp = get_empty_filp();
2241 if (!filp)
2242 return ERR_PTR(-ENFILE);
2243
Al Viro47c805d2011-02-23 17:44:09 -05002244 filp->f_flags = op->open_flag;
Al Viro73d049a2011-03-11 12:08:24 -05002245 nd->intent.open.file = filp;
2246 nd->intent.open.flags = open_to_namei_flags(op->open_flag);
2247 nd->intent.open.create_mode = op->mode;
Nick Piggin31e6b012011-01-07 17:49:52 +11002248
Al Viro73d049a2011-03-11 12:08:24 -05002249 error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
Nick Piggin31e6b012011-01-07 17:49:52 +11002250 if (unlikely(error))
Al Viro13aab422011-02-23 17:54:08 -05002251 goto out_filp;
Nick Piggin31e6b012011-01-07 17:49:52 +11002252
Al Virofe2d35f2011-03-05 22:58:25 -05002253 current->total_link_count = 0;
Al Viro73d049a2011-03-11 12:08:24 -05002254 error = link_path_walk(pathname, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11002255 if (unlikely(error))
2256 goto out_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
Al Viro73d049a2011-03-11 12:08:24 -05002258 filp = do_last(nd, &path, op, pathname);
Al Viro806b6812009-12-26 07:16:40 -05002259 while (unlikely(!filp)) { /* trailing symlink */
Nick Piggin7b9337a2011-01-14 08:42:43 +00002260 struct path link = path;
Al Virodef4af32009-12-26 08:37:05 -05002261 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04002262 if (!(nd->flags & LOOKUP_FOLLOW)) {
Al Viro73d049a2011-03-11 12:08:24 -05002263 path_put_conditional(&path, nd);
2264 path_put(&nd->path);
Al Viro40b39132011-03-09 16:22:18 -05002265 filp = ERR_PTR(-ELOOP);
2266 break;
2267 }
Al Viro73d049a2011-03-11 12:08:24 -05002268 nd->flags |= LOOKUP_PARENT;
2269 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
Al Viro574197e2011-03-14 22:20:34 -04002270 error = follow_link(&link, nd, &cookie);
Al Viroc3e380b2011-02-23 13:39:45 -05002271 if (unlikely(error))
Al Virof1afe9e2011-02-22 22:27:28 -05002272 filp = ERR_PTR(error);
Al Viroc3e380b2011-02-23 13:39:45 -05002273 else
Al Viro73d049a2011-03-11 12:08:24 -05002274 filp = do_last(nd, &path, op, pathname);
Al Viro574197e2011-03-14 22:20:34 -04002275 put_link(nd, &link, cookie);
Al Viro806b6812009-12-26 07:16:40 -05002276 }
Al Viro10fa8e62009-12-26 07:09:49 -05002277out:
Al Viro73d049a2011-03-11 12:08:24 -05002278 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
2279 path_put(&nd->root);
Al Virofe2d35f2011-03-05 22:58:25 -05002280 if (base)
2281 fput(base);
Al Viro73d049a2011-03-11 12:08:24 -05002282 release_open_intent(nd);
Al Viro10fa8e62009-12-26 07:09:49 -05002283 return filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
Nick Piggin31e6b012011-01-07 17:49:52 +11002285out_filp:
Al Viro806b6812009-12-26 07:16:40 -05002286 filp = ERR_PTR(error);
Al Viro10fa8e62009-12-26 07:09:49 -05002287 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288}
2289
Al Viro13aab422011-02-23 17:54:08 -05002290struct file *do_filp_open(int dfd, const char *pathname,
2291 const struct open_flags *op, int flags)
2292{
Al Viro73d049a2011-03-11 12:08:24 -05002293 struct nameidata nd;
Al Viro13aab422011-02-23 17:54:08 -05002294 struct file *filp;
2295
Al Viro73d049a2011-03-11 12:08:24 -05002296 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
Al Viro13aab422011-02-23 17:54:08 -05002297 if (unlikely(filp == ERR_PTR(-ECHILD)))
Al Viro73d049a2011-03-11 12:08:24 -05002298 filp = path_openat(dfd, pathname, &nd, op, flags);
Al Viro13aab422011-02-23 17:54:08 -05002299 if (unlikely(filp == ERR_PTR(-ESTALE)))
Al Viro73d049a2011-03-11 12:08:24 -05002300 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
Al Viro13aab422011-02-23 17:54:08 -05002301 return filp;
2302}
2303
Al Viro73d049a2011-03-11 12:08:24 -05002304struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
2305 const char *name, const struct open_flags *op, int flags)
2306{
2307 struct nameidata nd;
2308 struct file *file;
2309
2310 nd.root.mnt = mnt;
2311 nd.root.dentry = dentry;
2312
2313 flags |= LOOKUP_ROOT;
2314
Al Virobcda7652011-03-13 16:42:14 -04002315 if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
Al Viro73d049a2011-03-11 12:08:24 -05002316 return ERR_PTR(-ELOOP);
2317
2318 file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
2319 if (unlikely(file == ERR_PTR(-ECHILD)))
2320 file = path_openat(-1, name, &nd, op, flags);
2321 if (unlikely(file == ERR_PTR(-ESTALE)))
2322 file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
2323 return file;
2324}
2325
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326/**
2327 * lookup_create - lookup a dentry, creating it if it doesn't exist
2328 * @nd: nameidata info
2329 * @is_dir: directory flag
2330 *
2331 * Simple function to lookup and return a dentry and create it
2332 * if it doesn't exist. Is SMP-safe.
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002333 *
Jan Blunck4ac91372008-02-14 19:34:32 -08002334 * Returns with nd->path.dentry->d_inode->i_mutex locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 */
2336struct dentry *lookup_create(struct nameidata *nd, int is_dir)
2337{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002338 struct dentry *dentry = ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
Jan Blunck4ac91372008-02-14 19:34:32 -08002340 mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002341 /*
2342 * Yucky last component or no last component at all?
2343 * (foo/., foo/.., /////)
2344 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 if (nd->last_type != LAST_NORM)
2346 goto fail;
2347 nd->flags &= ~LOOKUP_PARENT;
Al Viro35165862008-08-05 03:00:49 -04002348 nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
ASANO Masahiroa6349042006-08-22 20:06:02 -04002349 nd->intent.open.flags = O_EXCL;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002350
2351 /*
2352 * Do the final lookup.
2353 */
Christoph Hellwig49705b72005-11-08 21:35:06 -08002354 dentry = lookup_hash(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 if (IS_ERR(dentry))
2356 goto fail;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002357
Al Viroe9baf6e2008-05-15 04:49:12 -04002358 if (dentry->d_inode)
2359 goto eexist;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002360 /*
2361 * Special case - lookup gave negative, but... we had foo/bar/
2362 * From the vfs_mknod() POV we just have a negative dentry -
2363 * all is fine. Let's be bastards - you had / on the end, you've
2364 * been asking for (non-existent) directory. -ENOENT for you.
2365 */
Al Viroe9baf6e2008-05-15 04:49:12 -04002366 if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
2367 dput(dentry);
2368 dentry = ERR_PTR(-ENOENT);
2369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 return dentry;
Al Viroe9baf6e2008-05-15 04:49:12 -04002371eexist:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 dput(dentry);
Al Viroe9baf6e2008-05-15 04:49:12 -04002373 dentry = ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374fail:
2375 return dentry;
2376}
Christoph Hellwigf81a0bf2005-05-19 12:26:43 -07002377EXPORT_SYMBOL_GPL(lookup_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
2379int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
2380{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002381 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
2383 if (error)
2384 return error;
2385
Serge E. Hallyne795b712011-03-23 16:43:25 -07002386 if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
2387 !ns_capable(inode_userns(dir), CAP_MKNOD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 return -EPERM;
2389
Al Viroacfa4382008-12-04 10:06:33 -05002390 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 return -EPERM;
2392
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07002393 error = devcgroup_inode_mknod(mode, dev);
2394 if (error)
2395 return error;
2396
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 error = security_inode_mknod(dir, dentry, mode, dev);
2398 if (error)
2399 return error;
2400
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002402 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002403 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 return error;
2405}
2406
Dave Hansen463c3192008-02-15 14:37:57 -08002407static int may_mknod(mode_t mode)
2408{
2409 switch (mode & S_IFMT) {
2410 case S_IFREG:
2411 case S_IFCHR:
2412 case S_IFBLK:
2413 case S_IFIFO:
2414 case S_IFSOCK:
2415 case 0: /* zero mode translates to S_IFREG */
2416 return 0;
2417 case S_IFDIR:
2418 return -EPERM;
2419 default:
2420 return -EINVAL;
2421 }
2422}
2423
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002424SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
2425 unsigned, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426{
Al Viro2ad94ae2008-07-21 09:32:51 -04002427 int error;
2428 char *tmp;
2429 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 struct nameidata nd;
2431
2432 if (S_ISDIR(mode))
2433 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
Al Viro2ad94ae2008-07-21 09:32:51 -04002435 error = user_path_parent(dfd, filename, &nd, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002437 return error;
2438
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 dentry = lookup_create(&nd, 0);
Dave Hansen463c3192008-02-15 14:37:57 -08002440 if (IS_ERR(dentry)) {
2441 error = PTR_ERR(dentry);
2442 goto out_unlock;
2443 }
Jan Blunck4ac91372008-02-14 19:34:32 -08002444 if (!IS_POSIXACL(nd.path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04002445 mode &= ~current_umask();
Dave Hansen463c3192008-02-15 14:37:57 -08002446 error = may_mknod(mode);
2447 if (error)
2448 goto out_dput;
2449 error = mnt_want_write(nd.path.mnt);
2450 if (error)
2451 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002452 error = security_path_mknod(&nd.path, dentry, mode, dev);
2453 if (error)
2454 goto out_drop_write;
Dave Hansen463c3192008-02-15 14:37:57 -08002455 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 case 0: case S_IFREG:
Jan Blunck4ac91372008-02-14 19:34:32 -08002457 error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 break;
2459 case S_IFCHR: case S_IFBLK:
Jan Blunck4ac91372008-02-14 19:34:32 -08002460 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 new_decode_dev(dev));
2462 break;
2463 case S_IFIFO: case S_IFSOCK:
Jan Blunck4ac91372008-02-14 19:34:32 -08002464 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 }
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002467out_drop_write:
Dave Hansen463c3192008-02-15 14:37:57 -08002468 mnt_drop_write(nd.path.mnt);
2469out_dput:
2470 dput(dentry);
2471out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002472 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002473 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 putname(tmp);
2475
2476 return error;
2477}
2478
Heiko Carstens3480b252009-01-14 14:14:16 +01002479SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002480{
2481 return sys_mknodat(AT_FDCWD, filename, mode, dev);
2482}
2483
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2485{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002486 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
2488 if (error)
2489 return error;
2490
Al Viroacfa4382008-12-04 10:06:33 -05002491 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 return -EPERM;
2493
2494 mode &= (S_IRWXUGO|S_ISVTX);
2495 error = security_inode_mkdir(dir, dentry, mode);
2496 if (error)
2497 return error;
2498
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002500 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002501 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 return error;
2503}
2504
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002505SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506{
2507 int error = 0;
2508 char * tmp;
Dave Hansen6902d922006-09-30 23:29:01 -07002509 struct dentry *dentry;
2510 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
Al Viro2ad94ae2008-07-21 09:32:51 -04002512 error = user_path_parent(dfd, pathname, &nd, &tmp);
2513 if (error)
Dave Hansen6902d922006-09-30 23:29:01 -07002514 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515
Dave Hansen6902d922006-09-30 23:29:01 -07002516 dentry = lookup_create(&nd, 1);
2517 error = PTR_ERR(dentry);
2518 if (IS_ERR(dentry))
2519 goto out_unlock;
2520
Jan Blunck4ac91372008-02-14 19:34:32 -08002521 if (!IS_POSIXACL(nd.path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04002522 mode &= ~current_umask();
Dave Hansen463c3192008-02-15 14:37:57 -08002523 error = mnt_want_write(nd.path.mnt);
2524 if (error)
2525 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002526 error = security_path_mkdir(&nd.path, dentry, mode);
2527 if (error)
2528 goto out_drop_write;
Jan Blunck4ac91372008-02-14 19:34:32 -08002529 error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002530out_drop_write:
Dave Hansen463c3192008-02-15 14:37:57 -08002531 mnt_drop_write(nd.path.mnt);
2532out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002533 dput(dentry);
2534out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002535 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002536 path_put(&nd.path);
Dave Hansen6902d922006-09-30 23:29:01 -07002537 putname(tmp);
2538out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 return error;
2540}
2541
Heiko Carstens3cdad422009-01-14 14:14:22 +01002542SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002543{
2544 return sys_mkdirat(AT_FDCWD, pathname, mode);
2545}
2546
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547/*
Sage Weila71905f2011-05-24 13:06:08 -07002548 * The dentry_unhash() helper will try to drop the dentry early: we
2549 * should have a usage count of 2 if we're the only user of this
2550 * dentry, and if that is true (possibly after pruning the dcache),
2551 * then we drop the dentry now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 *
2553 * A low-level filesystem can, if it choses, legally
2554 * do a
2555 *
2556 * if (!d_unhashed(dentry))
2557 * return -EBUSY;
2558 *
2559 * if it cannot handle the case of removing a directory
2560 * that is still in use by something else..
2561 */
2562void dentry_unhash(struct dentry *dentry)
2563{
Vasily Averindc168422006-12-06 20:37:07 -08002564 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 spin_lock(&dentry->d_lock);
Sage Weil64252c72011-05-24 13:06:05 -07002566 if (dentry->d_count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 __d_drop(dentry);
2568 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569}
2570
2571int vfs_rmdir(struct inode *dir, struct dentry *dentry)
2572{
2573 int error = may_delete(dir, dentry, 1);
2574
2575 if (error)
2576 return error;
2577
Al Viroacfa4382008-12-04 10:06:33 -05002578 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 return -EPERM;
2580
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002581 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582
Sage Weil912dbc12011-05-24 13:06:11 -07002583 error = -EBUSY;
2584 if (d_mountpoint(dentry))
2585 goto out;
2586
2587 error = security_inode_rmdir(dir, dentry);
2588 if (error)
2589 goto out;
2590
Sage Weil3cebde22011-05-29 21:20:59 -07002591 shrink_dcache_parent(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07002592 error = dir->i_op->rmdir(dir, dentry);
2593 if (error)
2594 goto out;
2595
2596 dentry->d_inode->i_flags |= S_DEAD;
2597 dont_mount(dentry);
2598
2599out:
2600 mutex_unlock(&dentry->d_inode->i_mutex);
2601 if (!error)
2602 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 return error;
2604}
2605
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002606static long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607{
2608 int error = 0;
2609 char * name;
2610 struct dentry *dentry;
2611 struct nameidata nd;
2612
Al Viro2ad94ae2008-07-21 09:32:51 -04002613 error = user_path_parent(dfd, pathname, &nd, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002615 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
2617 switch(nd.last_type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002618 case LAST_DOTDOT:
2619 error = -ENOTEMPTY;
2620 goto exit1;
2621 case LAST_DOT:
2622 error = -EINVAL;
2623 goto exit1;
2624 case LAST_ROOT:
2625 error = -EBUSY;
2626 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002628
2629 nd.flags &= ~LOOKUP_PARENT;
2630
Jan Blunck4ac91372008-02-14 19:34:32 -08002631 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002632 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002634 if (IS_ERR(dentry))
2635 goto exit2;
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04002636 if (!dentry->d_inode) {
2637 error = -ENOENT;
2638 goto exit3;
2639 }
Dave Hansen06227532008-02-15 14:37:34 -08002640 error = mnt_want_write(nd.path.mnt);
2641 if (error)
2642 goto exit3;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002643 error = security_path_rmdir(&nd.path, dentry);
2644 if (error)
2645 goto exit4;
Jan Blunck4ac91372008-02-14 19:34:32 -08002646 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002647exit4:
Dave Hansen06227532008-02-15 14:37:34 -08002648 mnt_drop_write(nd.path.mnt);
2649exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07002650 dput(dentry);
2651exit2:
Jan Blunck4ac91372008-02-14 19:34:32 -08002652 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002654 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 putname(name);
2656 return error;
2657}
2658
Heiko Carstens3cdad422009-01-14 14:14:22 +01002659SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002660{
2661 return do_rmdir(AT_FDCWD, pathname);
2662}
2663
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664int vfs_unlink(struct inode *dir, struct dentry *dentry)
2665{
2666 int error = may_delete(dir, dentry, 0);
2667
2668 if (error)
2669 return error;
2670
Al Viroacfa4382008-12-04 10:06:33 -05002671 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 return -EPERM;
2673
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002674 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 if (d_mountpoint(dentry))
2676 error = -EBUSY;
2677 else {
2678 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05002679 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 error = dir->i_op->unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05002681 if (!error)
Al Virod83c49f2010-04-30 17:17:09 -04002682 dont_mount(dentry);
Al Virobec10522010-03-03 14:12:08 -05002683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002685 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686
2687 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2688 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
Jan Karaece95912008-02-06 01:37:13 -08002689 fsnotify_link_count(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 }
Robert Love0eeca282005-07-12 17:06:03 -04002692
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 return error;
2694}
2695
2696/*
2697 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002698 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 * writeout happening, and we don't want to prevent access to the directory
2700 * while waiting on the I/O.
2701 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002702static long do_unlinkat(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703{
Al Viro2ad94ae2008-07-21 09:32:51 -04002704 int error;
2705 char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 struct dentry *dentry;
2707 struct nameidata nd;
2708 struct inode *inode = NULL;
2709
Al Viro2ad94ae2008-07-21 09:32:51 -04002710 error = user_path_parent(dfd, pathname, &nd, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002712 return error;
2713
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 error = -EISDIR;
2715 if (nd.last_type != LAST_NORM)
2716 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002717
2718 nd.flags &= ~LOOKUP_PARENT;
2719
Jan Blunck4ac91372008-02-14 19:34:32 -08002720 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002721 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 error = PTR_ERR(dentry);
2723 if (!IS_ERR(dentry)) {
2724 /* Why not before? Because we want correct error value */
Török Edwin50338b82011-06-16 00:06:14 +03002725 if (nd.last.name[nd.last.len])
2726 goto slashes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 inode = dentry->d_inode;
Török Edwin50338b82011-06-16 00:06:14 +03002728 if (!inode)
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04002729 goto slashes;
2730 ihold(inode);
Dave Hansen06227532008-02-15 14:37:34 -08002731 error = mnt_want_write(nd.path.mnt);
2732 if (error)
2733 goto exit2;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002734 error = security_path_unlink(&nd.path, dentry);
2735 if (error)
2736 goto exit3;
Jan Blunck4ac91372008-02-14 19:34:32 -08002737 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002738exit3:
Dave Hansen06227532008-02-15 14:37:34 -08002739 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 exit2:
2741 dput(dentry);
2742 }
Jan Blunck4ac91372008-02-14 19:34:32 -08002743 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 if (inode)
2745 iput(inode); /* truncate the inode here */
2746exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002747 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 putname(name);
2749 return error;
2750
2751slashes:
2752 error = !dentry->d_inode ? -ENOENT :
2753 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2754 goto exit2;
2755}
2756
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002757SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002758{
2759 if ((flag & ~AT_REMOVEDIR) != 0)
2760 return -EINVAL;
2761
2762 if (flag & AT_REMOVEDIR)
2763 return do_rmdir(dfd, pathname);
2764
2765 return do_unlinkat(dfd, pathname);
2766}
2767
Heiko Carstens3480b252009-01-14 14:14:16 +01002768SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002769{
2770 return do_unlinkat(AT_FDCWD, pathname);
2771}
2772
Miklos Szeredidb2e7472008-06-24 16:50:16 +02002773int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002775 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776
2777 if (error)
2778 return error;
2779
Al Viroacfa4382008-12-04 10:06:33 -05002780 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 return -EPERM;
2782
2783 error = security_inode_symlink(dir, dentry, oldname);
2784 if (error)
2785 return error;
2786
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002788 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002789 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 return error;
2791}
2792
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002793SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
2794 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795{
Al Viro2ad94ae2008-07-21 09:32:51 -04002796 int error;
2797 char *from;
2798 char *to;
Dave Hansen6902d922006-09-30 23:29:01 -07002799 struct dentry *dentry;
2800 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801
2802 from = getname(oldname);
Al Viro2ad94ae2008-07-21 09:32:51 -04002803 if (IS_ERR(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 return PTR_ERR(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04002805
2806 error = user_path_parent(newdfd, newname, &nd, &to);
2807 if (error)
Dave Hansen6902d922006-09-30 23:29:01 -07002808 goto out_putname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809
Dave Hansen6902d922006-09-30 23:29:01 -07002810 dentry = lookup_create(&nd, 0);
2811 error = PTR_ERR(dentry);
2812 if (IS_ERR(dentry))
2813 goto out_unlock;
2814
Dave Hansen75c3f292008-02-15 14:37:45 -08002815 error = mnt_want_write(nd.path.mnt);
2816 if (error)
2817 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002818 error = security_path_symlink(&nd.path, dentry, from);
2819 if (error)
2820 goto out_drop_write;
Miklos Szeredidb2e7472008-06-24 16:50:16 +02002821 error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002822out_drop_write:
Dave Hansen75c3f292008-02-15 14:37:45 -08002823 mnt_drop_write(nd.path.mnt);
2824out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002825 dput(dentry);
2826out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002827 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002828 path_put(&nd.path);
Dave Hansen6902d922006-09-30 23:29:01 -07002829 putname(to);
2830out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 putname(from);
2832 return error;
2833}
2834
Heiko Carstens3480b252009-01-14 14:14:16 +01002835SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002836{
2837 return sys_symlinkat(oldname, AT_FDCWD, newname);
2838}
2839
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2841{
2842 struct inode *inode = old_dentry->d_inode;
2843 int error;
2844
2845 if (!inode)
2846 return -ENOENT;
2847
Miklos Szeredia95164d2008-07-30 15:08:48 +02002848 error = may_create(dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 if (error)
2850 return error;
2851
2852 if (dir->i_sb != inode->i_sb)
2853 return -EXDEV;
2854
2855 /*
2856 * A link to an append-only or immutable file cannot be created.
2857 */
2858 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2859 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05002860 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002862 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 return -EPERM;
2864
2865 error = security_inode_link(old_dentry, dir, new_dentry);
2866 if (error)
2867 return error;
2868
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002869 mutex_lock(&inode->i_mutex);
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05302870 /* Make sure we don't allow creating hardlink to an unlinked file */
2871 if (inode->i_nlink == 0)
2872 error = -ENOENT;
2873 else
2874 error = dir->i_op->link(old_dentry, dir, new_dentry);
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002875 mutex_unlock(&inode->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07002876 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002877 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 return error;
2879}
2880
2881/*
2882 * Hardlinks are often used in delicate situations. We avoid
2883 * security-related surprises by not following symlinks on the
2884 * newname. --KAB
2885 *
2886 * We don't follow them on the oldname either to be compatible
2887 * with linux 2.0, and to avoid hard-linking to directories
2888 * and other special files. --ADM
2889 */
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002890SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
2891 int, newdfd, const char __user *, newname, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892{
2893 struct dentry *new_dentry;
Al Viro2d8f3032008-07-22 09:59:21 -04002894 struct nameidata nd;
2895 struct path old_path;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05302896 int how = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 int error;
Al Viro2ad94ae2008-07-21 09:32:51 -04002898 char *to;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05302900 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002901 return -EINVAL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05302902 /*
2903 * To use null names we require CAP_DAC_READ_SEARCH
2904 * This ensures that not everyone will be able to create
2905 * handlink using the passed filedescriptor.
2906 */
2907 if (flags & AT_EMPTY_PATH) {
2908 if (!capable(CAP_DAC_READ_SEARCH))
2909 return -ENOENT;
2910 how = LOOKUP_EMPTY;
2911 }
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002912
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05302913 if (flags & AT_SYMLINK_FOLLOW)
2914 how |= LOOKUP_FOLLOW;
2915
2916 error = user_path_at(olddfd, oldname, how, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002918 return error;
2919
2920 error = user_path_parent(newdfd, newname, &nd, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 if (error)
2922 goto out;
2923 error = -EXDEV;
Al Viro2d8f3032008-07-22 09:59:21 -04002924 if (old_path.mnt != nd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 goto out_release;
2926 new_dentry = lookup_create(&nd, 0);
2927 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002928 if (IS_ERR(new_dentry))
2929 goto out_unlock;
Dave Hansen75c3f292008-02-15 14:37:45 -08002930 error = mnt_want_write(nd.path.mnt);
2931 if (error)
2932 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002933 error = security_path_link(old_path.dentry, &nd.path, new_dentry);
2934 if (error)
2935 goto out_drop_write;
Al Viro2d8f3032008-07-22 09:59:21 -04002936 error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002937out_drop_write:
Dave Hansen75c3f292008-02-15 14:37:45 -08002938 mnt_drop_write(nd.path.mnt);
2939out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002940 dput(new_dentry);
2941out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002942 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943out_release:
Jan Blunck1d957f92008-02-14 19:34:35 -08002944 path_put(&nd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04002945 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946out:
Al Viro2d8f3032008-07-22 09:59:21 -04002947 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
2949 return error;
2950}
2951
Heiko Carstens3480b252009-01-14 14:14:16 +01002952SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002953{
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002954 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002955}
2956
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957/*
2958 * The worst of all namespace operations - renaming directory. "Perverted"
2959 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2960 * Problems:
2961 * a) we can get into loop creation. Check is done in is_subdir().
2962 * b) race potential - two innocent renames can create a loop together.
2963 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002964 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 * story.
2966 * c) we have to lock _three_ objects - parents and victim (if it exists).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002967 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 * whether the target exists). Solution: try to be smart with locking
2969 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002970 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 * move will be locked. Thus we can rank directories by the tree
2972 * (ancestors first) and rank all non-directories after them.
2973 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002974 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 * HOWEVER, it relies on the assumption that any object with ->lookup()
2976 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2977 * we'd better make sure that there's no link(2) for them.
Sage Weile4eaac02011-05-24 13:06:07 -07002978 * d) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002979 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05002981 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 * locking].
2983 */
Adrian Bunk75c96f82005-05-05 16:16:09 -07002984static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
2985 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986{
2987 int error = 0;
Sage Weil9055cba2011-05-24 13:06:12 -07002988 struct inode *target = new_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989
2990 /*
2991 * If we are going to change the parent - check write permissions,
2992 * we'll need to flip '..'.
2993 */
2994 if (new_dir != old_dir) {
Al Virof419a2e2008-07-22 00:07:17 -04002995 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 if (error)
2997 return error;
2998 }
2999
3000 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3001 if (error)
3002 return error;
3003
Al Virod83c49f2010-04-30 17:17:09 -04003004 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003005 mutex_lock(&target->i_mutex);
Sage Weil9055cba2011-05-24 13:06:12 -07003006
3007 error = -EBUSY;
3008 if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
3009 goto out;
3010
Sage Weil3cebde22011-05-29 21:20:59 -07003011 if (target)
3012 shrink_dcache_parent(new_dentry);
Sage Weil9055cba2011-05-24 13:06:12 -07003013 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3014 if (error)
3015 goto out;
3016
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 if (target) {
Sage Weil9055cba2011-05-24 13:06:12 -07003018 target->i_flags |= S_DEAD;
3019 dont_mount(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 }
Sage Weil9055cba2011-05-24 13:06:12 -07003021out:
3022 if (target)
3023 mutex_unlock(&target->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07003024 if (!error)
Mark Fasheh349457c2006-09-08 14:22:21 -07003025 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3026 d_move(old_dentry,new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 return error;
3028}
3029
Adrian Bunk75c96f82005-05-05 16:16:09 -07003030static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
3031 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032{
Sage Weil51892bb2011-05-24 13:06:13 -07003033 struct inode *target = new_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 int error;
3035
3036 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3037 if (error)
3038 return error;
3039
3040 dget(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003042 mutex_lock(&target->i_mutex);
Sage Weil51892bb2011-05-24 13:06:13 -07003043
3044 error = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
Sage Weil51892bb2011-05-24 13:06:13 -07003046 goto out;
3047
3048 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3049 if (error)
3050 goto out;
3051
3052 if (target)
3053 dont_mount(new_dentry);
3054 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3055 d_move(old_dentry, new_dentry);
3056out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003058 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 dput(new_dentry);
3060 return error;
3061}
3062
3063int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
3064 struct inode *new_dir, struct dentry *new_dentry)
3065{
3066 int error;
3067 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
Eric Paris59b0df22010-02-08 12:53:52 -05003068 const unsigned char *old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069
3070 if (old_dentry->d_inode == new_dentry->d_inode)
3071 return 0;
3072
3073 error = may_delete(old_dir, old_dentry, is_dir);
3074 if (error)
3075 return error;
3076
3077 if (!new_dentry->d_inode)
Miklos Szeredia95164d2008-07-30 15:08:48 +02003078 error = may_create(new_dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 else
3080 error = may_delete(new_dir, new_dentry, is_dir);
3081 if (error)
3082 return error;
3083
Al Viroacfa4382008-12-04 10:06:33 -05003084 if (!old_dir->i_op->rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 return -EPERM;
3086
Robert Love0eeca282005-07-12 17:06:03 -04003087 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
3088
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 if (is_dir)
3090 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
3091 else
3092 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
Al Viro123df292009-12-25 04:57:57 -05003093 if (!error)
3094 fsnotify_move(old_dir, new_dir, old_name, is_dir,
Al Viro5a190ae2007-06-07 12:19:32 -04003095 new_dentry->d_inode, old_dentry);
Robert Love0eeca282005-07-12 17:06:03 -04003096 fsnotify_oldname_free(old_name);
3097
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 return error;
3099}
3100
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003101SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
3102 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103{
Al Viro2ad94ae2008-07-21 09:32:51 -04003104 struct dentry *old_dir, *new_dir;
3105 struct dentry *old_dentry, *new_dentry;
3106 struct dentry *trap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 struct nameidata oldnd, newnd;
Al Viro2ad94ae2008-07-21 09:32:51 -04003108 char *from;
3109 char *to;
3110 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111
Al Viro2ad94ae2008-07-21 09:32:51 -04003112 error = user_path_parent(olddfd, oldname, &oldnd, &from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 if (error)
3114 goto exit;
3115
Al Viro2ad94ae2008-07-21 09:32:51 -04003116 error = user_path_parent(newdfd, newname, &newnd, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 if (error)
3118 goto exit1;
3119
3120 error = -EXDEV;
Jan Blunck4ac91372008-02-14 19:34:32 -08003121 if (oldnd.path.mnt != newnd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 goto exit2;
3123
Jan Blunck4ac91372008-02-14 19:34:32 -08003124 old_dir = oldnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 error = -EBUSY;
3126 if (oldnd.last_type != LAST_NORM)
3127 goto exit2;
3128
Jan Blunck4ac91372008-02-14 19:34:32 -08003129 new_dir = newnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 if (newnd.last_type != LAST_NORM)
3131 goto exit2;
3132
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003133 oldnd.flags &= ~LOOKUP_PARENT;
3134 newnd.flags &= ~LOOKUP_PARENT;
OGAWA Hirofumi4e9ed2f2008-10-16 07:50:29 +09003135 newnd.flags |= LOOKUP_RENAME_TARGET;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003136
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 trap = lock_rename(new_dir, old_dir);
3138
Christoph Hellwig49705b72005-11-08 21:35:06 -08003139 old_dentry = lookup_hash(&oldnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 error = PTR_ERR(old_dentry);
3141 if (IS_ERR(old_dentry))
3142 goto exit3;
3143 /* source must exist */
3144 error = -ENOENT;
3145 if (!old_dentry->d_inode)
3146 goto exit4;
3147 /* unless the source is a directory trailing slashes give -ENOTDIR */
3148 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
3149 error = -ENOTDIR;
3150 if (oldnd.last.name[oldnd.last.len])
3151 goto exit4;
3152 if (newnd.last.name[newnd.last.len])
3153 goto exit4;
3154 }
3155 /* source should not be ancestor of target */
3156 error = -EINVAL;
3157 if (old_dentry == trap)
3158 goto exit4;
Christoph Hellwig49705b72005-11-08 21:35:06 -08003159 new_dentry = lookup_hash(&newnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 error = PTR_ERR(new_dentry);
3161 if (IS_ERR(new_dentry))
3162 goto exit4;
3163 /* target should not be an ancestor of source */
3164 error = -ENOTEMPTY;
3165 if (new_dentry == trap)
3166 goto exit5;
3167
Dave Hansen9079b1e2008-02-15 14:37:49 -08003168 error = mnt_want_write(oldnd.path.mnt);
3169 if (error)
3170 goto exit5;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003171 error = security_path_rename(&oldnd.path, old_dentry,
3172 &newnd.path, new_dentry);
3173 if (error)
3174 goto exit6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 error = vfs_rename(old_dir->d_inode, old_dentry,
3176 new_dir->d_inode, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003177exit6:
Dave Hansen9079b1e2008-02-15 14:37:49 -08003178 mnt_drop_write(oldnd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179exit5:
3180 dput(new_dentry);
3181exit4:
3182 dput(old_dentry);
3183exit3:
3184 unlock_rename(new_dir, old_dir);
3185exit2:
Jan Blunck1d957f92008-02-14 19:34:35 -08003186 path_put(&newnd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04003187 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003189 path_put(&oldnd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 putname(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04003191exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 return error;
3193}
3194
Heiko Carstensa26eab22009-01-14 14:14:17 +01003195SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003196{
3197 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
3198}
3199
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
3201{
3202 int len;
3203
3204 len = PTR_ERR(link);
3205 if (IS_ERR(link))
3206 goto out;
3207
3208 len = strlen(link);
3209 if (len > (unsigned) buflen)
3210 len = buflen;
3211 if (copy_to_user(buffer, link, len))
3212 len = -EFAULT;
3213out:
3214 return len;
3215}
3216
3217/*
3218 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
3219 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
3220 * using) it for any given inode is up to filesystem.
3221 */
3222int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3223{
3224 struct nameidata nd;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003225 void *cookie;
Marcin Slusarz694a1762008-06-09 16:40:37 -07003226 int res;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003227
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 nd.depth = 0;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003229 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
Marcin Slusarz694a1762008-06-09 16:40:37 -07003230 if (IS_ERR(cookie))
3231 return PTR_ERR(cookie);
3232
3233 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
3234 if (dentry->d_inode->i_op->put_link)
3235 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3236 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237}
3238
3239int vfs_follow_link(struct nameidata *nd, const char *link)
3240{
3241 return __vfs_follow_link(nd, link);
3242}
3243
3244/* get the link contents into pagecache */
3245static char *page_getlink(struct dentry * dentry, struct page **ppage)
3246{
Duane Griffinebd09ab2008-12-19 20:47:12 +00003247 char *kaddr;
3248 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 struct address_space *mapping = dentry->d_inode->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -07003250 page = read_mapping_page(mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 if (IS_ERR(page))
Nick Piggin6fe69002007-05-06 14:49:04 -07003252 return (char*)page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 *ppage = page;
Duane Griffinebd09ab2008-12-19 20:47:12 +00003254 kaddr = kmap(page);
3255 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3256 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257}
3258
3259int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3260{
3261 struct page *page = NULL;
3262 char *s = page_getlink(dentry, &page);
3263 int res = vfs_readlink(dentry,buffer,buflen,s);
3264 if (page) {
3265 kunmap(page);
3266 page_cache_release(page);
3267 }
3268 return res;
3269}
3270
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003271void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003273 struct page *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 nd_set_link(nd, page_getlink(dentry, &page));
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003275 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276}
3277
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003278void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003280 struct page *page = cookie;
3281
3282 if (page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 kunmap(page);
3284 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 }
3286}
3287
Nick Piggin54566b22009-01-04 12:00:53 -08003288/*
3289 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
3290 */
3291int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292{
3293 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003294 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07003295 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08003296 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 char *kaddr;
Nick Piggin54566b22009-01-04 12:00:53 -08003298 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
3299 if (nofs)
3300 flags |= AOP_FLAG_NOFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301
NeilBrown7e53cac2006-03-25 03:07:57 -08003302retry:
Nick Pigginafddba42007-10-16 01:25:01 -07003303 err = pagecache_write_begin(NULL, mapping, 0, len-1,
Nick Piggin54566b22009-01-04 12:00:53 -08003304 flags, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07003306 goto fail;
3307
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 kaddr = kmap_atomic(page, KM_USER0);
3309 memcpy(kaddr, symname, len-1);
3310 kunmap_atomic(kaddr, KM_USER0);
Nick Pigginafddba42007-10-16 01:25:01 -07003311
3312 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3313 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 if (err < 0)
3315 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07003316 if (err < len-1)
3317 goto retry;
3318
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 mark_inode_dirty(inode);
3320 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321fail:
3322 return err;
3323}
3324
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003325int page_symlink(struct inode *inode, const char *symname, int len)
3326{
3327 return __page_symlink(inode, symname, len,
Nick Piggin54566b22009-01-04 12:00:53 -08003328 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003329}
3330
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003331const struct inode_operations page_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 .readlink = generic_readlink,
3333 .follow_link = page_follow_link_light,
3334 .put_link = page_put_link,
3335};
3336
Al Viro2d8f3032008-07-22 09:59:21 -04003337EXPORT_SYMBOL(user_path_at);
David Howellscc53ce52011-01-14 18:45:26 +00003338EXPORT_SYMBOL(follow_down_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339EXPORT_SYMBOL(follow_down);
3340EXPORT_SYMBOL(follow_up);
3341EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
3342EXPORT_SYMBOL(getname);
3343EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344EXPORT_SYMBOL(lookup_one_len);
3345EXPORT_SYMBOL(page_follow_link_light);
3346EXPORT_SYMBOL(page_put_link);
3347EXPORT_SYMBOL(page_readlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003348EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349EXPORT_SYMBOL(page_symlink);
3350EXPORT_SYMBOL(page_symlink_inode_operations);
Al Viroc9c6cac2011-02-16 15:15:47 -05003351EXPORT_SYMBOL(kern_path_parent);
Al Virod1811462008-08-02 00:49:18 -04003352EXPORT_SYMBOL(kern_path);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07003353EXPORT_SYMBOL(vfs_path_lookup);
Al Virof419a2e2008-07-22 00:07:17 -04003354EXPORT_SYMBOL(inode_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355EXPORT_SYMBOL(unlock_rename);
3356EXPORT_SYMBOL(vfs_create);
3357EXPORT_SYMBOL(vfs_follow_link);
3358EXPORT_SYMBOL(vfs_link);
3359EXPORT_SYMBOL(vfs_mkdir);
3360EXPORT_SYMBOL(vfs_mknod);
3361EXPORT_SYMBOL(generic_permission);
3362EXPORT_SYMBOL(vfs_readlink);
3363EXPORT_SYMBOL(vfs_rename);
3364EXPORT_SYMBOL(vfs_rmdir);
3365EXPORT_SYMBOL(vfs_symlink);
3366EXPORT_SYMBOL(vfs_unlink);
3367EXPORT_SYMBOL(dentry_unhash);
3368EXPORT_SYMBOL(generic_readlink);