blob: b3c1a975c83421c1ab45ac05436c6f3dfc91f11a [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/**
Jan Blunck5dd784d2008-02-14 19:34:38 -0800314 * path_get - get a reference to a path
315 * @path: path to get the reference to
316 *
317 * Given a path increment the reference count to the dentry and the vfsmount.
318 */
319void path_get(struct path *path)
320{
321 mntget(path->mnt);
322 dget(path->dentry);
323}
324EXPORT_SYMBOL(path_get);
325
326/**
Jan Blunck1d957f92008-02-14 19:34:35 -0800327 * path_put - put a reference to a path
328 * @path: path to put the reference to
329 *
330 * Given a path decrement the reference count to the dentry and the vfsmount.
331 */
332void path_put(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
Jan Blunck1d957f92008-02-14 19:34:35 -0800334 dput(path->dentry);
335 mntput(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
Jan Blunck1d957f92008-02-14 19:34:35 -0800337EXPORT_SYMBOL(path_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Al Viro19660af2011-03-25 10:32:48 -0400339/*
Nick Piggin31e6b012011-01-07 17:49:52 +1100340 * Path walking has 2 modes, rcu-walk and ref-walk (see
Al Viro19660af2011-03-25 10:32:48 -0400341 * Documentation/filesystems/path-lookup.txt). In situations when we can't
342 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
343 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
344 * mode. Refcounts are grabbed at the last known good point before rcu-walk
345 * got stuck, so ref-walk may continue from there. If this is not successful
346 * (eg. a seqcount has changed), then failure is returned and it's up to caller
347 * to restart the path walk from the beginning in ref-walk mode.
Nick Piggin31e6b012011-01-07 17:49:52 +1100348 */
Nick Piggin31e6b012011-01-07 17:49:52 +1100349
350/**
Al Viro19660af2011-03-25 10:32:48 -0400351 * unlazy_walk - try to switch to ref-walk mode.
352 * @nd: nameidata pathwalk data
353 * @dentry: child of nd->path.dentry or NULL
Randy Dunlap39191622011-01-08 19:36:21 -0800354 * Returns: 0 on success, -ECHILD on failure
Nick Piggin31e6b012011-01-07 17:49:52 +1100355 *
Al Viro19660af2011-03-25 10:32:48 -0400356 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
357 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
358 * @nd or NULL. Must be called from rcu-walk context.
Nick Piggin31e6b012011-01-07 17:49:52 +1100359 */
Al Viro19660af2011-03-25 10:32:48 -0400360static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
Nick Piggin31e6b012011-01-07 17:49:52 +1100361{
362 struct fs_struct *fs = current->fs;
363 struct dentry *parent = nd->path.dentry;
Al Viro5b6ca022011-03-09 23:04:47 -0500364 int want_root = 0;
Nick Piggin31e6b012011-01-07 17:49:52 +1100365
366 BUG_ON(!(nd->flags & LOOKUP_RCU));
Al Viro5b6ca022011-03-09 23:04:47 -0500367 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
368 want_root = 1;
Nick Piggin31e6b012011-01-07 17:49:52 +1100369 spin_lock(&fs->lock);
370 if (nd->root.mnt != fs->root.mnt ||
371 nd->root.dentry != fs->root.dentry)
372 goto err_root;
373 }
374 spin_lock(&parent->d_lock);
Al Viro19660af2011-03-25 10:32:48 -0400375 if (!dentry) {
376 if (!__d_rcu_to_refcount(parent, nd->seq))
377 goto err_parent;
378 BUG_ON(nd->inode != parent->d_inode);
379 } else {
Al Viro94c0d4e2011-07-12 21:40:23 -0400380 if (dentry->d_parent != parent)
381 goto err_parent;
Al Viro19660af2011-03-25 10:32:48 -0400382 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
383 if (!__d_rcu_to_refcount(dentry, nd->seq))
384 goto err_child;
385 /*
386 * If the sequence check on the child dentry passed, then
387 * the child has not been removed from its parent. This
388 * means the parent dentry must be valid and able to take
389 * a reference at this point.
390 */
391 BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
392 BUG_ON(!parent->d_count);
393 parent->d_count++;
394 spin_unlock(&dentry->d_lock);
395 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100396 spin_unlock(&parent->d_lock);
Al Viro5b6ca022011-03-09 23:04:47 -0500397 if (want_root) {
Nick Piggin31e6b012011-01-07 17:49:52 +1100398 path_get(&nd->root);
399 spin_unlock(&fs->lock);
400 }
401 mntget(nd->path.mnt);
402
403 rcu_read_unlock();
404 br_read_unlock(vfsmount_lock);
405 nd->flags &= ~LOOKUP_RCU;
406 return 0;
Al Viro19660af2011-03-25 10:32:48 -0400407
408err_child:
Nick Piggin31e6b012011-01-07 17:49:52 +1100409 spin_unlock(&dentry->d_lock);
Al Viro19660af2011-03-25 10:32:48 -0400410err_parent:
Nick Piggin31e6b012011-01-07 17:49:52 +1100411 spin_unlock(&parent->d_lock);
412err_root:
Al Viro5b6ca022011-03-09 23:04:47 -0500413 if (want_root)
Nick Piggin31e6b012011-01-07 17:49:52 +1100414 spin_unlock(&fs->lock);
415 return -ECHILD;
416}
417
Nick Piggin31e6b012011-01-07 17:49:52 +1100418/**
Trond Myklebust834f2a42005-10-18 14:20:16 -0700419 * release_open_intent - free up open intent resources
420 * @nd: pointer to nameidata
421 */
422void release_open_intent(struct nameidata *nd)
423{
Linus Torvalds2dab5972011-02-11 15:53:38 -0800424 struct file *file = nd->intent.open.file;
425
426 if (file && !IS_ERR(file)) {
427 if (file->f_path.dentry == NULL)
428 put_filp(file);
429 else
430 fput(file);
431 }
Trond Myklebust834f2a42005-10-18 14:20:16 -0700432}
433
Al Virof60aef72011-02-15 01:35:28 -0500434static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
Nick Piggin34286d62011-01-07 17:49:57 +1100435{
Al Virof60aef72011-02-15 01:35:28 -0500436 return dentry->d_op->d_revalidate(dentry, nd);
Nick Piggin34286d62011-01-07 17:49:57 +1100437}
438
Al Virof5e1c1c2011-02-15 01:32:55 -0500439static struct dentry *
Ian Kentbcdc5e02006-09-27 01:50:44 -0700440do_revalidate(struct dentry *dentry, struct nameidata *nd)
441{
Al Virof5e1c1c2011-02-15 01:32:55 -0500442 int status = d_revalidate(dentry, nd);
Ian Kentbcdc5e02006-09-27 01:50:44 -0700443 if (unlikely(status <= 0)) {
444 /*
445 * The dentry failed validation.
446 * If d_revalidate returned 0 attempt to invalidate
447 * the dentry otherwise d_revalidate is asking us
448 * to return a fail status.
449 */
Nick Piggin34286d62011-01-07 17:49:57 +1100450 if (status < 0) {
Al Virof5e1c1c2011-02-15 01:32:55 -0500451 dput(dentry);
Nick Piggin34286d62011-01-07 17:49:57 +1100452 dentry = ERR_PTR(status);
Al Virof5e1c1c2011-02-15 01:32:55 -0500453 } else if (!d_invalidate(dentry)) {
454 dput(dentry);
455 dentry = NULL;
Ian Kentbcdc5e02006-09-27 01:50:44 -0700456 }
457 }
458 return dentry;
459}
460
Al Viro9f1fafe2011-03-25 11:00:12 -0400461/**
462 * complete_walk - successful completion of path walk
463 * @nd: pointer nameidata
Jeff Layton39159de2009-12-07 12:01:50 -0500464 *
Al Viro9f1fafe2011-03-25 11:00:12 -0400465 * If we had been in RCU mode, drop out of it and legitimize nd->path.
466 * Revalidate the final result, unless we'd already done that during
467 * the path walk or the filesystem doesn't ask for it. Return 0 on
468 * success, -error on failure. In case of failure caller does not
469 * need to drop nd->path.
Jeff Layton39159de2009-12-07 12:01:50 -0500470 */
Al Viro9f1fafe2011-03-25 11:00:12 -0400471static int complete_walk(struct nameidata *nd)
Jeff Layton39159de2009-12-07 12:01:50 -0500472{
Al Viro16c2cd72011-02-22 15:50:10 -0500473 struct dentry *dentry = nd->path.dentry;
Jeff Layton39159de2009-12-07 12:01:50 -0500474 int status;
Jeff Layton39159de2009-12-07 12:01:50 -0500475
Al Viro9f1fafe2011-03-25 11:00:12 -0400476 if (nd->flags & LOOKUP_RCU) {
477 nd->flags &= ~LOOKUP_RCU;
478 if (!(nd->flags & LOOKUP_ROOT))
479 nd->root.mnt = NULL;
480 spin_lock(&dentry->d_lock);
481 if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
482 spin_unlock(&dentry->d_lock);
483 rcu_read_unlock();
484 br_read_unlock(vfsmount_lock);
485 return -ECHILD;
486 }
487 BUG_ON(nd->inode != dentry->d_inode);
488 spin_unlock(&dentry->d_lock);
489 mntget(nd->path.mnt);
490 rcu_read_unlock();
491 br_read_unlock(vfsmount_lock);
492 }
493
Al Viro16c2cd72011-02-22 15:50:10 -0500494 if (likely(!(nd->flags & LOOKUP_JUMPED)))
Jeff Layton39159de2009-12-07 12:01:50 -0500495 return 0;
496
Al Viro16c2cd72011-02-22 15:50:10 -0500497 if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
498 return 0;
499
500 if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
501 return 0;
502
503 /* Note: we do not d_invalidate() */
Nick Piggin34286d62011-01-07 17:49:57 +1100504 status = d_revalidate(dentry, nd);
Jeff Layton39159de2009-12-07 12:01:50 -0500505 if (status > 0)
506 return 0;
507
Al Viro16c2cd72011-02-22 15:50:10 -0500508 if (!status)
Jeff Layton39159de2009-12-07 12:01:50 -0500509 status = -ESTALE;
Al Viro16c2cd72011-02-22 15:50:10 -0500510
Al Viro9f1fafe2011-03-25 11:00:12 -0400511 path_put(&nd->path);
Jeff Layton39159de2009-12-07 12:01:50 -0500512 return status;
513}
514
Al Viro2a737872009-04-07 11:49:53 -0400515static __always_inline void set_root(struct nameidata *nd)
516{
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200517 if (!nd->root.mnt)
518 get_fs_root(current->fs, &nd->root);
Al Viro2a737872009-04-07 11:49:53 -0400519}
520
Al Viro6de88d72009-08-09 01:41:57 +0400521static int link_path_walk(const char *, struct nameidata *);
522
Nick Piggin31e6b012011-01-07 17:49:52 +1100523static __always_inline void set_root_rcu(struct nameidata *nd)
524{
525 if (!nd->root.mnt) {
526 struct fs_struct *fs = current->fs;
Nick Pigginc28cc362011-01-07 17:49:53 +1100527 unsigned seq;
528
529 do {
530 seq = read_seqcount_begin(&fs->seq);
531 nd->root = fs->root;
Tim Chenc1530012011-04-15 11:39:29 -0700532 nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
Nick Pigginc28cc362011-01-07 17:49:53 +1100533 } while (read_seqcount_retry(&fs->seq, seq));
Nick Piggin31e6b012011-01-07 17:49:52 +1100534 }
535}
536
Arjan van de Venf1662352006-01-14 13:21:31 -0800537static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
Nick Piggin31e6b012011-01-07 17:49:52 +1100539 int ret;
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (IS_ERR(link))
542 goto fail;
543
544 if (*link == '/') {
Al Viro2a737872009-04-07 11:49:53 -0400545 set_root(nd);
Jan Blunck1d957f92008-02-14 19:34:35 -0800546 path_put(&nd->path);
Al Viro2a737872009-04-07 11:49:53 -0400547 nd->path = nd->root;
548 path_get(&nd->root);
Al Viro16c2cd72011-02-22 15:50:10 -0500549 nd->flags |= LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100551 nd->inode = nd->path.dentry->d_inode;
Christoph Hellwigb4091d52008-11-05 15:07:21 +0100552
Nick Piggin31e6b012011-01-07 17:49:52 +1100553 ret = link_path_walk(link, nd);
554 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555fail:
Jan Blunck1d957f92008-02-14 19:34:35 -0800556 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return PTR_ERR(link);
558}
559
Jan Blunck1d957f92008-02-14 19:34:35 -0800560static void path_put_conditional(struct path *path, struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700561{
562 dput(path->dentry);
Jan Blunck4ac91372008-02-14 19:34:32 -0800563 if (path->mnt != nd->path.mnt)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700564 mntput(path->mnt);
565}
566
Nick Piggin7b9337a2011-01-14 08:42:43 +0000567static inline void path_to_nameidata(const struct path *path,
568 struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700569{
Nick Piggin31e6b012011-01-07 17:49:52 +1100570 if (!(nd->flags & LOOKUP_RCU)) {
571 dput(nd->path.dentry);
572 if (nd->path.mnt != path->mnt)
573 mntput(nd->path.mnt);
Huang Shijie9a229682010-04-02 17:37:13 +0800574 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100575 nd->path.mnt = path->mnt;
Jan Blunck4ac91372008-02-14 19:34:32 -0800576 nd->path.dentry = path->dentry;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700577}
578
Al Viro574197e2011-03-14 22:20:34 -0400579static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
580{
581 struct inode *inode = link->dentry->d_inode;
582 if (!IS_ERR(cookie) && inode->i_op->put_link)
583 inode->i_op->put_link(link->dentry, nd, cookie);
584 path_put(link);
585}
586
Al Virodef4af32009-12-26 08:37:05 -0500587static __always_inline int
Al Viro574197e2011-03-14 22:20:34 -0400588follow_link(struct path *link, struct nameidata *nd, void **p)
Ian Kent051d3812006-03-27 01:14:53 -0800589{
590 int error;
Nick Piggin7b9337a2011-01-14 08:42:43 +0000591 struct dentry *dentry = link->dentry;
Ian Kent051d3812006-03-27 01:14:53 -0800592
Al Viro844a3912011-02-15 00:38:26 -0500593 BUG_ON(nd->flags & LOOKUP_RCU);
594
Al Viro0e794582011-03-16 02:45:02 -0400595 if (link->mnt == nd->path.mnt)
596 mntget(link->mnt);
597
Al Viro574197e2011-03-14 22:20:34 -0400598 if (unlikely(current->total_link_count >= 40)) {
599 *p = ERR_PTR(-ELOOP); /* no ->put_link(), please */
Al Viro574197e2011-03-14 22:20:34 -0400600 path_put(&nd->path);
601 return -ELOOP;
602 }
603 cond_resched();
604 current->total_link_count++;
605
Nick Piggin7b9337a2011-01-14 08:42:43 +0000606 touch_atime(link->mnt, dentry);
Ian Kent051d3812006-03-27 01:14:53 -0800607 nd_set_link(nd, NULL);
608
Al Viro36f3b4f2011-02-22 21:24:38 -0500609 error = security_inode_follow_link(link->dentry, nd);
610 if (error) {
611 *p = ERR_PTR(error); /* no ->put_link(), please */
612 path_put(&nd->path);
613 return error;
614 }
615
Al Viro86acdca12009-12-22 23:45:11 -0500616 nd->last_type = LAST_BIND;
Al Virodef4af32009-12-26 08:37:05 -0500617 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
618 error = PTR_ERR(*p);
619 if (!IS_ERR(*p)) {
Ian Kent051d3812006-03-27 01:14:53 -0800620 char *s = nd_get_link(nd);
621 error = 0;
622 if (s)
623 error = __vfs_follow_link(nd, s);
Al Virobcda7652011-03-13 16:42:14 -0400624 else if (nd->last_type == LAST_BIND) {
Al Viro16c2cd72011-02-22 15:50:10 -0500625 nd->flags |= LOOKUP_JUMPED;
Al Virob21041d2011-03-14 20:01:51 -0400626 nd->inode = nd->path.dentry->d_inode;
627 if (nd->inode->i_op->follow_link) {
Al Virobcda7652011-03-13 16:42:14 -0400628 /* stepped on a _really_ weird one */
629 path_put(&nd->path);
630 error = -ELOOP;
631 }
632 }
Ian Kent051d3812006-03-27 01:14:53 -0800633 }
Ian Kent051d3812006-03-27 01:14:53 -0800634 return error;
635}
636
Nick Piggin31e6b012011-01-07 17:49:52 +1100637static int follow_up_rcu(struct path *path)
638{
639 struct vfsmount *parent;
640 struct dentry *mountpoint;
641
642 parent = path->mnt->mnt_parent;
643 if (parent == path->mnt)
644 return 0;
645 mountpoint = path->mnt->mnt_mountpoint;
646 path->dentry = mountpoint;
647 path->mnt = parent;
648 return 1;
649}
650
Al Virobab77eb2009-04-18 03:26:48 -0400651int follow_up(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
653 struct vfsmount *parent;
654 struct dentry *mountpoint;
Nick Piggin99b7db72010-08-18 04:37:39 +1000655
656 br_read_lock(vfsmount_lock);
Al Virobab77eb2009-04-18 03:26:48 -0400657 parent = path->mnt->mnt_parent;
658 if (parent == path->mnt) {
Nick Piggin99b7db72010-08-18 04:37:39 +1000659 br_read_unlock(vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return 0;
661 }
662 mntget(parent);
Al Virobab77eb2009-04-18 03:26:48 -0400663 mountpoint = dget(path->mnt->mnt_mountpoint);
Nick Piggin99b7db72010-08-18 04:37:39 +1000664 br_read_unlock(vfsmount_lock);
Al Virobab77eb2009-04-18 03:26:48 -0400665 dput(path->dentry);
666 path->dentry = mountpoint;
667 mntput(path->mnt);
668 path->mnt = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return 1;
670}
671
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100672/*
David Howells9875cf82011-01-14 18:45:21 +0000673 * Perform an automount
674 * - return -EISDIR to tell follow_managed() to stop and return the path we
675 * were called with.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 */
David Howells9875cf82011-01-14 18:45:21 +0000677static int follow_automount(struct path *path, unsigned flags,
678 bool *need_mntput)
Nick Piggin31e6b012011-01-07 17:49:52 +1100679{
David Howells9875cf82011-01-14 18:45:21 +0000680 struct vfsmount *mnt;
David Howellsea5b7782011-01-14 19:10:03 +0000681 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +1100682
David Howells9875cf82011-01-14 18:45:21 +0000683 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
684 return -EREMOTE;
Al Viro463ffb22005-06-06 13:36:05 -0700685
David Howells6f45b652011-01-14 18:45:31 +0000686 /* We don't want to mount if someone supplied AT_NO_AUTOMOUNT
687 * and this is the terminal part of the path.
688 */
689 if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_CONTINUE))
690 return -EISDIR; /* we actually want to stop here */
691
David Howells9875cf82011-01-14 18:45:21 +0000692 /* We want to mount if someone is trying to open/create a file of any
693 * type under the mountpoint, wants to traverse through the mountpoint
694 * or wants to open the mounted directory.
695 *
696 * We don't want to mount if someone's just doing a stat and they've
697 * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and
698 * appended a '/' to the name.
699 */
700 if (!(flags & LOOKUP_FOLLOW) &&
701 !(flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY |
702 LOOKUP_OPEN | LOOKUP_CREATE)))
703 return -EISDIR;
704
705 current->total_link_count++;
706 if (current->total_link_count >= 40)
707 return -ELOOP;
708
709 mnt = path->dentry->d_op->d_automount(path);
710 if (IS_ERR(mnt)) {
711 /*
712 * The filesystem is allowed to return -EISDIR here to indicate
713 * it doesn't want to automount. For instance, autofs would do
714 * this so that its userspace daemon can mount on this dentry.
715 *
716 * However, we can only permit this if it's a terminal point in
717 * the path being looked up; if it wasn't then the remainder of
718 * the path is inaccessible and we should say so.
719 */
720 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_CONTINUE))
721 return -EREMOTE;
722 return PTR_ERR(mnt);
723 }
David Howellsea5b7782011-01-14 19:10:03 +0000724
David Howells9875cf82011-01-14 18:45:21 +0000725 if (!mnt) /* mount collision */
726 return 0;
727
Al Viro8aef1882011-06-16 15:10:06 +0100728 if (!*need_mntput) {
729 /* lock_mount() may release path->mnt on error */
730 mntget(path->mnt);
731 *need_mntput = true;
732 }
Al Viro19a167a2011-01-17 01:35:23 -0500733 err = finish_automount(mnt, path);
David Howellsea5b7782011-01-14 19:10:03 +0000734
David Howellsea5b7782011-01-14 19:10:03 +0000735 switch (err) {
736 case -EBUSY:
737 /* Someone else made a mount here whilst we were busy */
Al Viro19a167a2011-01-17 01:35:23 -0500738 return 0;
David Howellsea5b7782011-01-14 19:10:03 +0000739 case 0:
Al Viro8aef1882011-06-16 15:10:06 +0100740 path_put(path);
David Howellsea5b7782011-01-14 19:10:03 +0000741 path->mnt = mnt;
742 path->dentry = dget(mnt->mnt_root);
David Howellsea5b7782011-01-14 19:10:03 +0000743 return 0;
Al Viro19a167a2011-01-17 01:35:23 -0500744 default:
745 return err;
David Howellsea5b7782011-01-14 19:10:03 +0000746 }
Al Viro19a167a2011-01-17 01:35:23 -0500747
David Howells9875cf82011-01-14 18:45:21 +0000748}
749
750/*
751 * Handle a dentry that is managed in some way.
David Howellscc53ce52011-01-14 18:45:26 +0000752 * - Flagged for transit management (autofs)
David Howells9875cf82011-01-14 18:45:21 +0000753 * - Flagged as mountpoint
754 * - Flagged as automount point
755 *
756 * This may only be called in refwalk mode.
757 *
758 * Serialization is taken care of in namespace.c
759 */
760static int follow_managed(struct path *path, unsigned flags)
761{
Al Viro8aef1882011-06-16 15:10:06 +0100762 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
David Howells9875cf82011-01-14 18:45:21 +0000763 unsigned managed;
764 bool need_mntput = false;
Al Viro8aef1882011-06-16 15:10:06 +0100765 int ret = 0;
David Howells9875cf82011-01-14 18:45:21 +0000766
767 /* Given that we're not holding a lock here, we retain the value in a
768 * local variable for each dentry as we look at it so that we don't see
769 * the components of that value change under us */
770 while (managed = ACCESS_ONCE(path->dentry->d_flags),
771 managed &= DCACHE_MANAGED_DENTRY,
772 unlikely(managed != 0)) {
David Howellscc53ce52011-01-14 18:45:26 +0000773 /* Allow the filesystem to manage the transit without i_mutex
774 * being held. */
775 if (managed & DCACHE_MANAGE_TRANSIT) {
776 BUG_ON(!path->dentry->d_op);
777 BUG_ON(!path->dentry->d_op->d_manage);
Al Viro1aed3e42011-03-18 09:09:02 -0400778 ret = path->dentry->d_op->d_manage(path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +0000779 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +0100780 break;
David Howellscc53ce52011-01-14 18:45:26 +0000781 }
782
David Howells9875cf82011-01-14 18:45:21 +0000783 /* Transit to a mounted filesystem. */
784 if (managed & DCACHE_MOUNTED) {
785 struct vfsmount *mounted = lookup_mnt(path);
786 if (mounted) {
787 dput(path->dentry);
788 if (need_mntput)
789 mntput(path->mnt);
790 path->mnt = mounted;
791 path->dentry = dget(mounted->mnt_root);
792 need_mntput = true;
793 continue;
794 }
795
796 /* Something is mounted on this dentry in another
797 * namespace and/or whatever was mounted there in this
798 * namespace got unmounted before we managed to get the
799 * vfsmount_lock */
800 }
801
802 /* Handle an automount point */
803 if (managed & DCACHE_NEED_AUTOMOUNT) {
804 ret = follow_automount(path, flags, &need_mntput);
805 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +0100806 break;
David Howells9875cf82011-01-14 18:45:21 +0000807 continue;
808 }
809
810 /* We didn't change the current path point */
811 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 }
Al Viro8aef1882011-06-16 15:10:06 +0100813
814 if (need_mntput && path->mnt == mnt)
815 mntput(path->mnt);
816 if (ret == -EISDIR)
817 ret = 0;
818 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819}
820
David Howellscc53ce52011-01-14 18:45:26 +0000821int follow_down_one(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
823 struct vfsmount *mounted;
824
Al Viro1c755af2009-04-18 14:06:57 -0400825 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -0400827 dput(path->dentry);
828 mntput(path->mnt);
829 path->mnt = mounted;
830 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 return 1;
832 }
833 return 0;
834}
835
Ian Kent62a73752011-03-25 01:51:02 +0800836static inline bool managed_dentry_might_block(struct dentry *dentry)
837{
838 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
839 dentry->d_op->d_manage(dentry, true) < 0);
840}
841
David Howells9875cf82011-01-14 18:45:21 +0000842/*
Al Viro287548e2011-05-27 06:50:06 -0400843 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
844 * we meet a managed dentry that would need blocking.
David Howells9875cf82011-01-14 18:45:21 +0000845 */
846static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
Al Viro287548e2011-05-27 06:50:06 -0400847 struct inode **inode)
David Howells9875cf82011-01-14 18:45:21 +0000848{
Ian Kent62a73752011-03-25 01:51:02 +0800849 for (;;) {
David Howells9875cf82011-01-14 18:45:21 +0000850 struct vfsmount *mounted;
Ian Kent62a73752011-03-25 01:51:02 +0800851 /*
852 * Don't forget we might have a non-mountpoint managed dentry
853 * that wants to block transit.
854 */
Al Viro287548e2011-05-27 06:50:06 -0400855 if (unlikely(managed_dentry_might_block(path->dentry)))
David Howellsab909112011-01-14 18:46:51 +0000856 return false;
Ian Kent62a73752011-03-25 01:51:02 +0800857
858 if (!d_mountpoint(path->dentry))
859 break;
860
David Howells9875cf82011-01-14 18:45:21 +0000861 mounted = __lookup_mnt(path->mnt, path->dentry, 1);
862 if (!mounted)
863 break;
864 path->mnt = mounted;
865 path->dentry = mounted->mnt_root;
866 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
Linus Torvalds59430262011-07-18 15:43:29 -0700867 /*
868 * Update the inode too. We don't need to re-check the
869 * dentry sequence number here after this d_inode read,
870 * because a mount-point is always pinned.
871 */
872 *inode = path->dentry->d_inode;
David Howells9875cf82011-01-14 18:45:21 +0000873 }
David Howells9875cf82011-01-14 18:45:21 +0000874 return true;
875}
876
Al Virodea39372011-05-27 06:53:39 -0400877static void follow_mount_rcu(struct nameidata *nd)
Al Viro287548e2011-05-27 06:50:06 -0400878{
Al Virodea39372011-05-27 06:53:39 -0400879 while (d_mountpoint(nd->path.dentry)) {
Al Viro287548e2011-05-27 06:50:06 -0400880 struct vfsmount *mounted;
Al Virodea39372011-05-27 06:53:39 -0400881 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
Al Viro287548e2011-05-27 06:50:06 -0400882 if (!mounted)
883 break;
Al Virodea39372011-05-27 06:53:39 -0400884 nd->path.mnt = mounted;
885 nd->path.dentry = mounted->mnt_root;
886 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro287548e2011-05-27 06:50:06 -0400887 }
888}
889
Nick Piggin31e6b012011-01-07 17:49:52 +1100890static int follow_dotdot_rcu(struct nameidata *nd)
891{
Nick Piggin31e6b012011-01-07 17:49:52 +1100892 set_root_rcu(nd);
893
David Howells9875cf82011-01-14 18:45:21 +0000894 while (1) {
Nick Piggin31e6b012011-01-07 17:49:52 +1100895 if (nd->path.dentry == nd->root.dentry &&
896 nd->path.mnt == nd->root.mnt) {
897 break;
898 }
899 if (nd->path.dentry != nd->path.mnt->mnt_root) {
900 struct dentry *old = nd->path.dentry;
901 struct dentry *parent = old->d_parent;
902 unsigned seq;
903
904 seq = read_seqcount_begin(&parent->d_seq);
905 if (read_seqcount_retry(&old->d_seq, nd->seq))
Al Viroef7562d2011-03-04 14:35:59 -0500906 goto failed;
Nick Piggin31e6b012011-01-07 17:49:52 +1100907 nd->path.dentry = parent;
908 nd->seq = seq;
909 break;
910 }
911 if (!follow_up_rcu(&nd->path))
912 break;
913 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Nick Piggin31e6b012011-01-07 17:49:52 +1100914 }
Al Virodea39372011-05-27 06:53:39 -0400915 follow_mount_rcu(nd);
916 nd->inode = nd->path.dentry->d_inode;
Nick Piggin31e6b012011-01-07 17:49:52 +1100917 return 0;
Al Viroef7562d2011-03-04 14:35:59 -0500918
919failed:
920 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -0500921 if (!(nd->flags & LOOKUP_ROOT))
922 nd->root.mnt = NULL;
Al Viroef7562d2011-03-04 14:35:59 -0500923 rcu_read_unlock();
924 br_read_unlock(vfsmount_lock);
925 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +1100926}
927
David Howells9875cf82011-01-14 18:45:21 +0000928/*
David Howellscc53ce52011-01-14 18:45:26 +0000929 * Follow down to the covering mount currently visible to userspace. At each
930 * point, the filesystem owning that dentry may be queried as to whether the
931 * caller is permitted to proceed or not.
David Howellscc53ce52011-01-14 18:45:26 +0000932 */
Al Viro7cc90cc2011-03-18 09:04:20 -0400933int follow_down(struct path *path)
David Howellscc53ce52011-01-14 18:45:26 +0000934{
935 unsigned managed;
936 int ret;
937
938 while (managed = ACCESS_ONCE(path->dentry->d_flags),
939 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
940 /* Allow the filesystem to manage the transit without i_mutex
941 * being held.
942 *
943 * We indicate to the filesystem if someone is trying to mount
944 * something here. This gives autofs the chance to deny anyone
945 * other than its daemon the right to mount on its
946 * superstructure.
947 *
948 * The filesystem may sleep at this point.
949 */
950 if (managed & DCACHE_MANAGE_TRANSIT) {
951 BUG_ON(!path->dentry->d_op);
952 BUG_ON(!path->dentry->d_op->d_manage);
David Howellsab909112011-01-14 18:46:51 +0000953 ret = path->dentry->d_op->d_manage(
Al Viro1aed3e42011-03-18 09:09:02 -0400954 path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +0000955 if (ret < 0)
956 return ret == -EISDIR ? 0 : ret;
957 }
958
959 /* Transit to a mounted filesystem. */
960 if (managed & DCACHE_MOUNTED) {
961 struct vfsmount *mounted = lookup_mnt(path);
962 if (!mounted)
963 break;
964 dput(path->dentry);
965 mntput(path->mnt);
966 path->mnt = mounted;
967 path->dentry = dget(mounted->mnt_root);
968 continue;
969 }
970
971 /* Don't handle automount points here */
972 break;
973 }
974 return 0;
975}
976
977/*
David Howells9875cf82011-01-14 18:45:21 +0000978 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
979 */
980static void follow_mount(struct path *path)
981{
982 while (d_mountpoint(path->dentry)) {
983 struct vfsmount *mounted = lookup_mnt(path);
984 if (!mounted)
985 break;
986 dput(path->dentry);
987 mntput(path->mnt);
988 path->mnt = mounted;
989 path->dentry = dget(mounted->mnt_root);
990 }
991}
992
Nick Piggin31e6b012011-01-07 17:49:52 +1100993static void follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
Al Viro2a737872009-04-07 11:49:53 -0400995 set_root(nd);
Andreas Mohre518ddb2006-09-29 02:01:22 -0700996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 while(1) {
Jan Blunck4ac91372008-02-14 19:34:32 -0800998 struct dentry *old = nd->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Al Viro2a737872009-04-07 11:49:53 -04001000 if (nd->path.dentry == nd->root.dentry &&
1001 nd->path.mnt == nd->root.mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 break;
1003 }
Jan Blunck4ac91372008-02-14 19:34:32 -08001004 if (nd->path.dentry != nd->path.mnt->mnt_root) {
Al Viro3088dd72010-01-30 15:47:29 -05001005 /* rare case of legitimate dget_parent()... */
1006 nd->path.dentry = dget_parent(nd->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 dput(old);
1008 break;
1009 }
Al Viro3088dd72010-01-30 15:47:29 -05001010 if (!follow_up(&nd->path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 }
Al Viro79ed0222009-04-18 13:59:41 -04001013 follow_mount(&nd->path);
Nick Piggin31e6b012011-01-07 17:49:52 +11001014 nd->inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015}
1016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017/*
Nick Pigginbaa03892010-08-18 04:37:31 +10001018 * Allocate a dentry with name and parent, and perform a parent
1019 * directory ->lookup on it. Returns the new dentry, or ERR_PTR
1020 * on error. parent->d_inode->i_mutex must be held. d_lookup must
1021 * have verified that no child exists while under i_mutex.
1022 */
1023static struct dentry *d_alloc_and_lookup(struct dentry *parent,
1024 struct qstr *name, struct nameidata *nd)
1025{
1026 struct inode *inode = parent->d_inode;
1027 struct dentry *dentry;
1028 struct dentry *old;
1029
1030 /* Don't create child dentry for a dead directory. */
1031 if (unlikely(IS_DEADDIR(inode)))
1032 return ERR_PTR(-ENOENT);
1033
1034 dentry = d_alloc(parent, name);
1035 if (unlikely(!dentry))
1036 return ERR_PTR(-ENOMEM);
1037
1038 old = inode->i_op->lookup(inode, dentry, nd);
1039 if (unlikely(old)) {
1040 dput(dentry);
1041 dentry = old;
1042 }
1043 return dentry;
1044}
1045
1046/*
Josef Bacik44396f42011-05-31 11:58:49 -04001047 * We already have a dentry, but require a lookup to be performed on the parent
1048 * directory to fill in d_inode. Returns the new dentry, or ERR_PTR on error.
1049 * parent->d_inode->i_mutex must be held. d_lookup must have verified that no
1050 * child exists while under i_mutex.
1051 */
1052static struct dentry *d_inode_lookup(struct dentry *parent, struct dentry *dentry,
1053 struct nameidata *nd)
1054{
1055 struct inode *inode = parent->d_inode;
1056 struct dentry *old;
1057
1058 /* Don't create child dentry for a dead directory. */
1059 if (unlikely(IS_DEADDIR(inode)))
1060 return ERR_PTR(-ENOENT);
1061
1062 old = inode->i_op->lookup(inode, dentry, nd);
1063 if (unlikely(old)) {
1064 dput(dentry);
1065 dentry = old;
1066 }
1067 return dentry;
1068}
1069
1070/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 * It's more convoluted than I'd like it to be, but... it's still fairly
1072 * small and for now I'd prefer to have fast path as straight as possible.
1073 * It _is_ time-critical.
1074 */
1075static int do_lookup(struct nameidata *nd, struct qstr *name,
Nick Piggin31e6b012011-01-07 17:49:52 +11001076 struct path *path, struct inode **inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
Jan Blunck4ac91372008-02-14 19:34:32 -08001078 struct vfsmount *mnt = nd->path.mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +11001079 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro5a18fff2011-03-11 04:44:53 -05001080 int need_reval = 1;
1081 int status = 1;
David Howells9875cf82011-01-14 18:45:21 +00001082 int err;
1083
Al Viro3cac2602009-08-13 18:27:43 +04001084 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001085 * Rename seqlock is not required here because in the off chance
1086 * of a false negative due to a concurrent rename, we're going to
1087 * do the non-racy lookup, below.
1088 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001089 if (nd->flags & LOOKUP_RCU) {
1090 unsigned seq;
Nick Piggin31e6b012011-01-07 17:49:52 +11001091 *inode = nd->inode;
1092 dentry = __d_lookup_rcu(parent, name, &seq, inode);
Al Viro5a18fff2011-03-11 04:44:53 -05001093 if (!dentry)
1094 goto unlazy;
1095
Nick Piggin31e6b012011-01-07 17:49:52 +11001096 /* Memory barrier in read_seqcount_begin of child is enough */
1097 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1098 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001099 nd->seq = seq;
Al Viro5a18fff2011-03-11 04:44:53 -05001100
Al Viro24643082011-02-15 01:26:22 -05001101 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
Al Viro5a18fff2011-03-11 04:44:53 -05001102 status = d_revalidate(dentry, nd);
1103 if (unlikely(status <= 0)) {
1104 if (status != -ECHILD)
1105 need_reval = 0;
1106 goto unlazy;
1107 }
Al Viro24643082011-02-15 01:26:22 -05001108 }
Josef Bacik44396f42011-05-31 11:58:49 -04001109 if (unlikely(d_need_lookup(dentry)))
1110 goto unlazy;
Nick Piggin31e6b012011-01-07 17:49:52 +11001111 path->mnt = mnt;
1112 path->dentry = dentry;
Al Virod6e9bd22011-05-27 07:03:15 -04001113 if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1114 goto unlazy;
1115 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1116 goto unlazy;
1117 return 0;
Al Viro5a18fff2011-03-11 04:44:53 -05001118unlazy:
Al Viro19660af2011-03-25 10:32:48 -04001119 if (unlazy_walk(nd, dentry))
1120 return -ECHILD;
Al Viro5a18fff2011-03-11 04:44:53 -05001121 } else {
1122 dentry = __d_lookup(parent, name);
Nick Piggin31e6b012011-01-07 17:49:52 +11001123 }
Al Viro5a18fff2011-03-11 04:44:53 -05001124
Josef Bacik44396f42011-05-31 11:58:49 -04001125 if (dentry && unlikely(d_need_lookup(dentry))) {
1126 dput(dentry);
1127 dentry = NULL;
1128 }
Al Viro5a18fff2011-03-11 04:44:53 -05001129retry:
1130 if (unlikely(!dentry)) {
1131 struct inode *dir = parent->d_inode;
1132 BUG_ON(nd->inode != dir);
1133
1134 mutex_lock(&dir->i_mutex);
1135 dentry = d_lookup(parent, name);
1136 if (likely(!dentry)) {
1137 dentry = d_alloc_and_lookup(parent, name, nd);
1138 if (IS_ERR(dentry)) {
1139 mutex_unlock(&dir->i_mutex);
1140 return PTR_ERR(dentry);
1141 }
1142 /* known good */
1143 need_reval = 0;
1144 status = 1;
Josef Bacik44396f42011-05-31 11:58:49 -04001145 } else if (unlikely(d_need_lookup(dentry))) {
1146 dentry = d_inode_lookup(parent, dentry, nd);
1147 if (IS_ERR(dentry)) {
1148 mutex_unlock(&dir->i_mutex);
1149 return PTR_ERR(dentry);
1150 }
1151 /* known good */
1152 need_reval = 0;
1153 status = 1;
Al Viro5a18fff2011-03-11 04:44:53 -05001154 }
1155 mutex_unlock(&dir->i_mutex);
Al Viro24643082011-02-15 01:26:22 -05001156 }
Al Viro5a18fff2011-03-11 04:44:53 -05001157 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
1158 status = d_revalidate(dentry, nd);
1159 if (unlikely(status <= 0)) {
1160 if (status < 0) {
1161 dput(dentry);
1162 return status;
1163 }
1164 if (!d_invalidate(dentry)) {
1165 dput(dentry);
1166 dentry = NULL;
1167 need_reval = 1;
1168 goto retry;
1169 }
1170 }
1171
David Howells9875cf82011-01-14 18:45:21 +00001172 path->mnt = mnt;
1173 path->dentry = dentry;
1174 err = follow_managed(path, nd->flags);
Ian Kent89312212011-01-18 12:06:10 +08001175 if (unlikely(err < 0)) {
1176 path_put_conditional(path, nd);
David Howells9875cf82011-01-14 18:45:21 +00001177 return err;
Ian Kent89312212011-01-18 12:06:10 +08001178 }
David Howells9875cf82011-01-14 18:45:21 +00001179 *inode = path->dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181}
1182
Al Viro52094c82011-02-21 21:34:47 -05001183static inline int may_lookup(struct nameidata *nd)
1184{
1185 if (nd->flags & LOOKUP_RCU) {
Al Viro4ad5abb2011-06-20 19:57:03 -04001186 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
Al Viro52094c82011-02-21 21:34:47 -05001187 if (err != -ECHILD)
1188 return err;
Al Viro19660af2011-03-25 10:32:48 -04001189 if (unlazy_walk(nd, NULL))
Al Viro52094c82011-02-21 21:34:47 -05001190 return -ECHILD;
1191 }
Al Viro4ad5abb2011-06-20 19:57:03 -04001192 return inode_permission(nd->inode, MAY_EXEC);
Al Viro52094c82011-02-21 21:34:47 -05001193}
1194
Al Viro9856fa12011-03-04 14:22:06 -05001195static inline int handle_dots(struct nameidata *nd, int type)
1196{
1197 if (type == LAST_DOTDOT) {
1198 if (nd->flags & LOOKUP_RCU) {
1199 if (follow_dotdot_rcu(nd))
1200 return -ECHILD;
1201 } else
1202 follow_dotdot(nd);
1203 }
1204 return 0;
1205}
1206
Al Viro951361f2011-03-04 14:44:37 -05001207static void terminate_walk(struct nameidata *nd)
1208{
1209 if (!(nd->flags & LOOKUP_RCU)) {
1210 path_put(&nd->path);
1211 } else {
1212 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001213 if (!(nd->flags & LOOKUP_ROOT))
1214 nd->root.mnt = NULL;
Al Viro951361f2011-03-04 14:44:37 -05001215 rcu_read_unlock();
1216 br_read_unlock(vfsmount_lock);
1217 }
1218}
1219
Al Viroce57dfc2011-03-13 19:58:58 -04001220static inline int walk_component(struct nameidata *nd, struct path *path,
1221 struct qstr *name, int type, int follow)
1222{
1223 struct inode *inode;
1224 int err;
1225 /*
1226 * "." and ".." are special - ".." especially so because it has
1227 * to be able to know about the current root directory and
1228 * parent relationships.
1229 */
1230 if (unlikely(type != LAST_NORM))
1231 return handle_dots(nd, type);
1232 err = do_lookup(nd, name, path, &inode);
1233 if (unlikely(err)) {
1234 terminate_walk(nd);
1235 return err;
1236 }
1237 if (!inode) {
1238 path_to_nameidata(path, nd);
1239 terminate_walk(nd);
1240 return -ENOENT;
1241 }
1242 if (unlikely(inode->i_op->follow_link) && follow) {
Al Viro19660af2011-03-25 10:32:48 -04001243 if (nd->flags & LOOKUP_RCU) {
1244 if (unlikely(unlazy_walk(nd, path->dentry))) {
1245 terminate_walk(nd);
1246 return -ECHILD;
1247 }
1248 }
Al Viroce57dfc2011-03-13 19:58:58 -04001249 BUG_ON(inode != path->dentry->d_inode);
1250 return 1;
1251 }
1252 path_to_nameidata(path, nd);
1253 nd->inode = inode;
1254 return 0;
1255}
1256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257/*
Al Virob3563792011-03-14 21:54:55 -04001258 * This limits recursive symlink follows to 8, while
1259 * limiting consecutive symlinks to 40.
1260 *
1261 * Without that kind of total limit, nasty chains of consecutive
1262 * symlinks can cause almost arbitrarily long lookups.
1263 */
1264static inline int nested_symlink(struct path *path, struct nameidata *nd)
1265{
1266 int res;
1267
Al Virob3563792011-03-14 21:54:55 -04001268 if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1269 path_put_conditional(path, nd);
1270 path_put(&nd->path);
1271 return -ELOOP;
1272 }
Erez Zadok1a4022f2011-05-21 01:19:59 -04001273 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
Al Virob3563792011-03-14 21:54:55 -04001274
1275 nd->depth++;
1276 current->link_count++;
1277
1278 do {
1279 struct path link = *path;
1280 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04001281
1282 res = follow_link(&link, nd, &cookie);
Al Virob3563792011-03-14 21:54:55 -04001283 if (!res)
1284 res = walk_component(nd, path, &nd->last,
1285 nd->last_type, LOOKUP_FOLLOW);
Al Viro574197e2011-03-14 22:20:34 -04001286 put_link(nd, &link, cookie);
Al Virob3563792011-03-14 21:54:55 -04001287 } while (res > 0);
1288
1289 current->link_count--;
1290 nd->depth--;
1291 return res;
1292}
1293
1294/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01001296 * This is the basic name resolution function, turning a pathname into
1297 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01001299 * Returns 0 and nd will have valid dentry and mnt on success.
1300 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 */
Al Viro6de88d72009-08-09 01:41:57 +04001302static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
1304 struct path next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 int err;
1306 unsigned int lookup_flags = nd->flags;
1307
1308 while (*name=='/')
1309 name++;
1310 if (!*name)
Al Viro086e1832011-02-22 20:56:27 -05001311 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 /* At this point we know we have a real path component. */
1314 for(;;) {
1315 unsigned long hash;
1316 struct qstr this;
1317 unsigned int c;
Al Virofe479a52011-02-22 15:10:03 -05001318 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Trond Myklebustcdce5d62005-10-18 14:20:18 -07001320 nd->flags |= LOOKUP_CONTINUE;
Al Viro52094c82011-02-21 21:34:47 -05001321
1322 err = may_lookup(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 if (err)
1324 break;
1325
1326 this.name = name;
1327 c = *(const unsigned char *)name;
1328
1329 hash = init_name_hash();
1330 do {
1331 name++;
1332 hash = partial_name_hash(c, hash);
1333 c = *(const unsigned char *)name;
1334 } while (c && (c != '/'));
1335 this.len = name - (const char *) this.name;
1336 this.hash = end_name_hash(hash);
1337
Al Virofe479a52011-02-22 15:10:03 -05001338 type = LAST_NORM;
1339 if (this.name[0] == '.') switch (this.len) {
1340 case 2:
Al Viro16c2cd72011-02-22 15:50:10 -05001341 if (this.name[1] == '.') {
Al Virofe479a52011-02-22 15:10:03 -05001342 type = LAST_DOTDOT;
Al Viro16c2cd72011-02-22 15:50:10 -05001343 nd->flags |= LOOKUP_JUMPED;
1344 }
Al Virofe479a52011-02-22 15:10:03 -05001345 break;
1346 case 1:
1347 type = LAST_DOT;
1348 }
Al Viro5a202bc2011-03-08 14:17:44 -05001349 if (likely(type == LAST_NORM)) {
1350 struct dentry *parent = nd->path.dentry;
Al Viro16c2cd72011-02-22 15:50:10 -05001351 nd->flags &= ~LOOKUP_JUMPED;
Al Viro5a202bc2011-03-08 14:17:44 -05001352 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1353 err = parent->d_op->d_hash(parent, nd->inode,
1354 &this);
1355 if (err < 0)
1356 break;
1357 }
1358 }
Al Virofe479a52011-02-22 15:10:03 -05001359
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 /* remove trailing slashes? */
1361 if (!c)
1362 goto last_component;
1363 while (*++name == '/');
1364 if (!*name)
Al Virob3563792011-03-14 21:54:55 -04001365 goto last_component;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Al Viroce57dfc2011-03-13 19:58:58 -04001367 err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1368 if (err < 0)
1369 return err;
Al Virofe479a52011-02-22 15:10:03 -05001370
Al Viroce57dfc2011-03-13 19:58:58 -04001371 if (err) {
Al Virob3563792011-03-14 21:54:55 -04001372 err = nested_symlink(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 if (err)
Al Viroa7472ba2011-03-04 14:39:30 -05001374 return err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 err = -ENOTDIR;
Nick Piggin31e6b012011-01-07 17:49:52 +11001377 if (!nd->inode->i_op->lookup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 break;
1379 continue;
1380 /* here ends the main loop */
1381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382last_component:
Trond Myklebustf55eab82006-02-04 23:28:01 -08001383 /* Clear LOOKUP_CONTINUE iff it was previously unset */
1384 nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
Al Virob3563792011-03-14 21:54:55 -04001385 nd->last = this;
1386 nd->last_type = type;
Al Viro086e1832011-02-22 20:56:27 -05001387 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 }
Al Viro951361f2011-03-04 14:44:37 -05001389 terminate_walk(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 return err;
1391}
1392
Al Viro70e9b352011-03-05 21:12:22 -05001393static int path_init(int dfd, const char *name, unsigned int flags,
1394 struct nameidata *nd, struct file **fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
Prasanna Medaea3834d2005-04-29 16:00:17 +01001396 int retval = 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001397 int fput_needed;
1398 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
1400 nd->last_type = LAST_ROOT; /* if there are only slashes... */
Al Viro16c2cd72011-02-22 15:50:10 -05001401 nd->flags = flags | LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 nd->depth = 0;
Al Viro5b6ca022011-03-09 23:04:47 -05001403 if (flags & LOOKUP_ROOT) {
1404 struct inode *inode = nd->root.dentry->d_inode;
Al Viro73d049a2011-03-11 12:08:24 -05001405 if (*name) {
1406 if (!inode->i_op->lookup)
1407 return -ENOTDIR;
1408 retval = inode_permission(inode, MAY_EXEC);
1409 if (retval)
1410 return retval;
1411 }
Al Viro5b6ca022011-03-09 23:04:47 -05001412 nd->path = nd->root;
1413 nd->inode = inode;
1414 if (flags & LOOKUP_RCU) {
1415 br_read_lock(vfsmount_lock);
1416 rcu_read_lock();
1417 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1418 } else {
1419 path_get(&nd->path);
1420 }
1421 return 0;
1422 }
1423
Al Viro2a737872009-04-07 11:49:53 -04001424 nd->root.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 if (*name=='/') {
Al Viroe41f7d42011-02-22 14:02:58 -05001427 if (flags & LOOKUP_RCU) {
1428 br_read_lock(vfsmount_lock);
1429 rcu_read_lock();
1430 set_root_rcu(nd);
1431 } else {
1432 set_root(nd);
1433 path_get(&nd->root);
1434 }
Al Viro2a737872009-04-07 11:49:53 -04001435 nd->path = nd->root;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001436 } else if (dfd == AT_FDCWD) {
Al Viroe41f7d42011-02-22 14:02:58 -05001437 if (flags & LOOKUP_RCU) {
1438 struct fs_struct *fs = current->fs;
1439 unsigned seq;
1440
1441 br_read_lock(vfsmount_lock);
1442 rcu_read_lock();
1443
1444 do {
1445 seq = read_seqcount_begin(&fs->seq);
1446 nd->path = fs->pwd;
1447 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1448 } while (read_seqcount_retry(&fs->seq, seq));
1449 } else {
1450 get_fs_pwd(current->fs, &nd->path);
1451 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001452 } else {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001453 struct dentry *dentry;
1454
Al Viro1abf0c72011-03-13 03:51:11 -04001455 file = fget_raw_light(dfd, &fput_needed);
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001456 retval = -EBADF;
1457 if (!file)
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001458 goto out_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001459
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001460 dentry = file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001461
Al Virof52e0c12011-03-14 18:56:51 -04001462 if (*name) {
1463 retval = -ENOTDIR;
1464 if (!S_ISDIR(dentry->d_inode->i_mode))
1465 goto fput_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001466
Al Viro4ad5abb2011-06-20 19:57:03 -04001467 retval = inode_permission(dentry->d_inode, MAY_EXEC);
Al Virof52e0c12011-03-14 18:56:51 -04001468 if (retval)
1469 goto fput_fail;
1470 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001471
Jan Blunck5dd784d2008-02-14 19:34:38 -08001472 nd->path = file->f_path;
Al Viroe41f7d42011-02-22 14:02:58 -05001473 if (flags & LOOKUP_RCU) {
1474 if (fput_needed)
Al Viro70e9b352011-03-05 21:12:22 -05001475 *fp = file;
Al Viroe41f7d42011-02-22 14:02:58 -05001476 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1477 br_read_lock(vfsmount_lock);
1478 rcu_read_lock();
1479 } else {
1480 path_get(&file->f_path);
1481 fput_light(file, fput_needed);
1482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 }
Al Viroe41f7d42011-02-22 14:02:58 -05001484
Nick Piggin31e6b012011-01-07 17:49:52 +11001485 nd->inode = nd->path.dentry->d_inode;
Al Viro9b4a9b12009-04-07 11:44:16 -04001486 return 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001487
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001488fput_fail:
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001489 fput_light(file, fput_needed);
Al Viro9b4a9b12009-04-07 11:44:16 -04001490out_fail:
1491 return retval;
1492}
1493
Al Virobd92d7f2011-03-14 19:54:59 -04001494static inline int lookup_last(struct nameidata *nd, struct path *path)
1495{
1496 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1497 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1498
1499 nd->flags &= ~LOOKUP_PARENT;
1500 return walk_component(nd, path, &nd->last, nd->last_type,
1501 nd->flags & LOOKUP_FOLLOW);
1502}
1503
Al Viro9b4a9b12009-04-07 11:44:16 -04001504/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroee0827c2011-02-21 23:38:09 -05001505static int path_lookupat(int dfd, const char *name,
Al Viro9b4a9b12009-04-07 11:44:16 -04001506 unsigned int flags, struct nameidata *nd)
1507{
Al Viro70e9b352011-03-05 21:12:22 -05001508 struct file *base = NULL;
Al Virobd92d7f2011-03-14 19:54:59 -04001509 struct path path;
1510 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001511
1512 /*
1513 * Path walking is largely split up into 2 different synchronisation
1514 * schemes, rcu-walk and ref-walk (explained in
1515 * Documentation/filesystems/path-lookup.txt). These share much of the
1516 * path walk code, but some things particularly setup, cleanup, and
1517 * following mounts are sufficiently divergent that functions are
1518 * duplicated. Typically there is a function foo(), and its RCU
1519 * analogue, foo_rcu().
1520 *
1521 * -ECHILD is the error number of choice (just to avoid clashes) that
1522 * is returned if some aspect of an rcu-walk fails. Such an error must
1523 * be handled by restarting a traditional ref-walk (which will always
1524 * be able to complete).
1525 */
Al Virobd92d7f2011-03-14 19:54:59 -04001526 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
Al Viroee0827c2011-02-21 23:38:09 -05001527
Al Virobd92d7f2011-03-14 19:54:59 -04001528 if (unlikely(err))
1529 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001530
1531 current->total_link_count = 0;
Al Virobd92d7f2011-03-14 19:54:59 -04001532 err = link_path_walk(name, nd);
1533
1534 if (!err && !(flags & LOOKUP_PARENT)) {
Al Virobd92d7f2011-03-14 19:54:59 -04001535 err = lookup_last(nd, &path);
1536 while (err > 0) {
1537 void *cookie;
1538 struct path link = path;
Al Virobd92d7f2011-03-14 19:54:59 -04001539 nd->flags |= LOOKUP_PARENT;
Al Viro574197e2011-03-14 22:20:34 -04001540 err = follow_link(&link, nd, &cookie);
Al Virobd92d7f2011-03-14 19:54:59 -04001541 if (!err)
1542 err = lookup_last(nd, &path);
Al Viro574197e2011-03-14 22:20:34 -04001543 put_link(nd, &link, cookie);
Al Virobd92d7f2011-03-14 19:54:59 -04001544 }
1545 }
Al Viroee0827c2011-02-21 23:38:09 -05001546
Al Viro9f1fafe2011-03-25 11:00:12 -04001547 if (!err)
1548 err = complete_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04001549
1550 if (!err && nd->flags & LOOKUP_DIRECTORY) {
1551 if (!nd->inode->i_op->lookup) {
1552 path_put(&nd->path);
Al Virobd23a532011-03-23 09:56:30 -04001553 err = -ENOTDIR;
Al Virobd92d7f2011-03-14 19:54:59 -04001554 }
1555 }
Al Viro16c2cd72011-02-22 15:50:10 -05001556
Al Viro70e9b352011-03-05 21:12:22 -05001557 if (base)
1558 fput(base);
Al Viroee0827c2011-02-21 23:38:09 -05001559
Al Viro5b6ca022011-03-09 23:04:47 -05001560 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
Al Viro2a737872009-04-07 11:49:53 -04001561 path_put(&nd->root);
1562 nd->root.mnt = NULL;
1563 }
Al Virobd92d7f2011-03-14 19:54:59 -04001564 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001565}
Nick Piggin31e6b012011-01-07 17:49:52 +11001566
Al Viroee0827c2011-02-21 23:38:09 -05001567static int do_path_lookup(int dfd, const char *name,
1568 unsigned int flags, struct nameidata *nd)
1569{
1570 int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1571 if (unlikely(retval == -ECHILD))
1572 retval = path_lookupat(dfd, name, flags, nd);
1573 if (unlikely(retval == -ESTALE))
1574 retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11001575
1576 if (likely(!retval)) {
1577 if (unlikely(!audit_dummy_context())) {
1578 if (nd->path.dentry && nd->inode)
1579 audit_inode(name, nd->path.dentry);
1580 }
1581 }
Al Viro9b4a9b12009-04-07 11:44:16 -04001582 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583}
1584
Al Viroc9c6cac2011-02-16 15:15:47 -05001585int kern_path_parent(const char *name, struct nameidata *nd)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001586{
Al Viroc9c6cac2011-02-16 15:15:47 -05001587 return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001588}
1589
Al Virod1811462008-08-02 00:49:18 -04001590int kern_path(const char *name, unsigned int flags, struct path *path)
1591{
1592 struct nameidata nd;
1593 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1594 if (!res)
1595 *path = nd.path;
1596 return res;
1597}
1598
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001599/**
1600 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1601 * @dentry: pointer to dentry of the base directory
1602 * @mnt: pointer to vfs mount of the base directory
1603 * @name: pointer to file name
1604 * @flags: lookup flags
1605 * @nd: pointer to nameidata
1606 */
1607int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
1608 const char *name, unsigned int flags,
1609 struct nameidata *nd)
1610{
Al Viro5b6ca022011-03-09 23:04:47 -05001611 nd->root.dentry = dentry;
1612 nd->root.mnt = mnt;
1613 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
1614 return do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, nd);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001615}
1616
Christoph Hellwigeead1912007-10-16 23:25:38 -07001617static struct dentry *__lookup_hash(struct qstr *name,
1618 struct dentry *base, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619{
Christoph Hellwig81fca442010-10-06 10:47:47 +02001620 struct inode *inode = base->d_inode;
James Morris057f6c02007-04-26 00:12:05 -07001621 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 int err;
1623
Al Viro4ad5abb2011-06-20 19:57:03 -04001624 err = inode_permission(inode, MAY_EXEC);
Christoph Hellwig81fca442010-10-06 10:47:47 +02001625 if (err)
1626 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
1628 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001629 * Don't bother with __d_lookup: callers are for creat as
1630 * well as unlink, so a lot of the time it would cost
1631 * a double lookup.
Al Viro6e6b1bd2009-08-13 23:38:37 +04001632 */
Nick Pigginb04f7842010-08-18 04:37:34 +10001633 dentry = d_lookup(base, name);
Al Viro6e6b1bd2009-08-13 23:38:37 +04001634
Josef Bacik44396f42011-05-31 11:58:49 -04001635 if (dentry && d_need_lookup(dentry)) {
1636 /*
1637 * __lookup_hash is called with the parent dir's i_mutex already
1638 * held, so we are good to go here.
1639 */
1640 dentry = d_inode_lookup(base, dentry, nd);
1641 if (IS_ERR(dentry))
1642 return dentry;
1643 }
1644
Nick Pigginfb045ad2011-01-07 17:49:55 +11001645 if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE))
Al Viro6e6b1bd2009-08-13 23:38:37 +04001646 dentry = do_revalidate(dentry, nd);
1647
Nick Pigginbaa03892010-08-18 04:37:31 +10001648 if (!dentry)
1649 dentry = d_alloc_and_lookup(base, name, nd);
Al Viro5a202bc2011-03-08 14:17:44 -05001650
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 return dentry;
1652}
1653
James Morris057f6c02007-04-26 00:12:05 -07001654/*
1655 * Restricted form of lookup. Doesn't follow links, single-component only,
1656 * needs parent already locked. Doesn't follow mounts.
1657 * SMP-safe.
1658 */
Adrian Bunka244e162006-03-31 02:32:11 -08001659static struct dentry *lookup_hash(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660{
Jan Blunck4ac91372008-02-14 19:34:32 -08001661 return __lookup_hash(&nd->last, nd->path.dentry, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662}
1663
Christoph Hellwigeead1912007-10-16 23:25:38 -07001664/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07001665 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07001666 * @name: pathname component to lookup
1667 * @base: base directory to lookup from
1668 * @len: maximum length @len should be interpreted to
1669 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07001670 * Note that this routine is purely a helper for filesystem usage and should
1671 * not be called by generic code. Also note that by using this function the
Christoph Hellwigeead1912007-10-16 23:25:38 -07001672 * nameidata argument is passed to the filesystem methods and a filesystem
1673 * using this helper needs to be prepared for that.
1674 */
James Morris057f6c02007-04-26 00:12:05 -07001675struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1676{
James Morris057f6c02007-04-26 00:12:05 -07001677 struct qstr this;
Al Viro6a96ba52011-03-07 23:49:20 -05001678 unsigned long hash;
1679 unsigned int c;
James Morris057f6c02007-04-26 00:12:05 -07001680
David Woodhouse2f9092e2009-04-20 23:18:37 +01001681 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
1682
Al Viro6a96ba52011-03-07 23:49:20 -05001683 this.name = name;
1684 this.len = len;
1685 if (!len)
1686 return ERR_PTR(-EACCES);
1687
1688 hash = init_name_hash();
1689 while (len--) {
1690 c = *(const unsigned char *)name++;
1691 if (c == '/' || c == '\0')
1692 return ERR_PTR(-EACCES);
1693 hash = partial_name_hash(c, hash);
1694 }
1695 this.hash = end_name_hash(hash);
Al Viro5a202bc2011-03-08 14:17:44 -05001696 /*
1697 * See if the low-level filesystem might want
1698 * to use its own hash..
1699 */
1700 if (base->d_flags & DCACHE_OP_HASH) {
1701 int err = base->d_op->d_hash(base, base->d_inode, &this);
1702 if (err < 0)
1703 return ERR_PTR(err);
1704 }
Christoph Hellwigeead1912007-10-16 23:25:38 -07001705
Christoph Hellwig49705b72005-11-08 21:35:06 -08001706 return __lookup_hash(&this, base, NULL);
James Morris057f6c02007-04-26 00:12:05 -07001707}
1708
Al Viro2d8f3032008-07-22 09:59:21 -04001709int user_path_at(int dfd, const char __user *name, unsigned flags,
1710 struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711{
Al Viro2d8f3032008-07-22 09:59:21 -04001712 struct nameidata nd;
Al Virof52e0c12011-03-14 18:56:51 -04001713 char *tmp = getname_flags(name, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 int err = PTR_ERR(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 if (!IS_ERR(tmp)) {
Al Viro2d8f3032008-07-22 09:59:21 -04001716
1717 BUG_ON(flags & LOOKUP_PARENT);
1718
1719 err = do_path_lookup(dfd, tmp, flags, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 putname(tmp);
Al Viro2d8f3032008-07-22 09:59:21 -04001721 if (!err)
1722 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 }
1724 return err;
1725}
1726
Al Viro2ad94ae2008-07-21 09:32:51 -04001727static int user_path_parent(int dfd, const char __user *path,
1728 struct nameidata *nd, char **name)
1729{
1730 char *s = getname(path);
1731 int error;
1732
1733 if (IS_ERR(s))
1734 return PTR_ERR(s);
1735
1736 error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
1737 if (error)
1738 putname(s);
1739 else
1740 *name = s;
1741
1742 return error;
1743}
1744
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745/*
1746 * It's inline, so penalty for filesystems that don't use sticky bit is
1747 * minimal.
1748 */
1749static inline int check_sticky(struct inode *dir, struct inode *inode)
1750{
David Howellsda9592e2008-11-14 10:39:05 +11001751 uid_t fsuid = current_fsuid();
1752
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 if (!(dir->i_mode & S_ISVTX))
1754 return 0;
Serge E. Hallyne795b712011-03-23 16:43:25 -07001755 if (current_user_ns() != inode_userns(inode))
1756 goto other_userns;
David Howellsda9592e2008-11-14 10:39:05 +11001757 if (inode->i_uid == fsuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 return 0;
David Howellsda9592e2008-11-14 10:39:05 +11001759 if (dir->i_uid == fsuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 return 0;
Serge E. Hallyne795b712011-03-23 16:43:25 -07001761
1762other_userns:
1763 return !ns_capable(inode_userns(inode), CAP_FOWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764}
1765
1766/*
1767 * Check whether we can remove a link victim from directory dir, check
1768 * whether the type of victim is right.
1769 * 1. We can't do it if dir is read-only (done in permission())
1770 * 2. We should have write and exec permissions on dir
1771 * 3. We can't remove anything from append-only dir
1772 * 4. We can't do anything with immutable dir (done in permission())
1773 * 5. If the sticky bit on dir is set we should either
1774 * a. be owner of dir, or
1775 * b. be owner of victim, or
1776 * c. have CAP_FOWNER capability
1777 * 6. If the victim is append-only or immutable we can't do antyhing with
1778 * links pointing to it.
1779 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1780 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1781 * 9. We can't remove a root or mountpoint.
1782 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1783 * nfs_async_unlink().
1784 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001785static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786{
1787 int error;
1788
1789 if (!victim->d_inode)
1790 return -ENOENT;
1791
1792 BUG_ON(victim->d_parent->d_inode != dir);
Al Virocccc6bb2009-12-25 05:07:33 -05001793 audit_inode_child(victim, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
Al Virof419a2e2008-07-22 00:07:17 -04001795 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 if (error)
1797 return error;
1798 if (IS_APPEND(dir))
1799 return -EPERM;
1800 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
Hugh Dickinsf9454542008-11-19 15:36:38 -08001801 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 return -EPERM;
1803 if (isdir) {
1804 if (!S_ISDIR(victim->d_inode->i_mode))
1805 return -ENOTDIR;
1806 if (IS_ROOT(victim))
1807 return -EBUSY;
1808 } else if (S_ISDIR(victim->d_inode->i_mode))
1809 return -EISDIR;
1810 if (IS_DEADDIR(dir))
1811 return -ENOENT;
1812 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1813 return -EBUSY;
1814 return 0;
1815}
1816
1817/* Check whether we can create an object with dentry child in directory
1818 * dir.
1819 * 1. We can't do it if child already exists (open has special treatment for
1820 * this case, but since we are inlined it's OK)
1821 * 2. We can't do it if dir is read-only (done in permission())
1822 * 3. We should have write and exec permissions on dir
1823 * 4. We can't do it if dir is immutable (done in permission())
1824 */
Miklos Szeredia95164d2008-07-30 15:08:48 +02001825static inline int may_create(struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826{
1827 if (child->d_inode)
1828 return -EEXIST;
1829 if (IS_DEADDIR(dir))
1830 return -ENOENT;
Al Virof419a2e2008-07-22 00:07:17 -04001831 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832}
1833
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834/*
1835 * p1 and p2 should be directories on the same fs.
1836 */
1837struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1838{
1839 struct dentry *p;
1840
1841 if (p1 == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07001842 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 return NULL;
1844 }
1845
Arjan van de Vena11f3a02006-03-23 03:00:33 -08001846 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001848 p = d_ancestor(p2, p1);
1849 if (p) {
1850 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1851 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
1852 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 }
1854
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001855 p = d_ancestor(p1, p2);
1856 if (p) {
1857 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1858 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1859 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 }
1861
Ingo Molnarf2eace22006-07-03 00:25:05 -07001862 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1863 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 return NULL;
1865}
1866
1867void unlock_rename(struct dentry *p1, struct dentry *p2)
1868{
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001869 mutex_unlock(&p1->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 if (p1 != p2) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001871 mutex_unlock(&p2->d_inode->i_mutex);
Arjan van de Vena11f3a02006-03-23 03:00:33 -08001872 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 }
1874}
1875
1876int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1877 struct nameidata *nd)
1878{
Miklos Szeredia95164d2008-07-30 15:08:48 +02001879 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
1881 if (error)
1882 return error;
1883
Al Viroacfa4382008-12-04 10:06:33 -05001884 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 return -EACCES; /* shouldn't it be ENOSYS? */
1886 mode &= S_IALLUGO;
1887 mode |= S_IFREG;
1888 error = security_inode_create(dir, dentry, mode);
1889 if (error)
1890 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 error = dir->i_op->create(dir, dentry, mode, nd);
Stephen Smalleya74574a2005-09-09 13:01:44 -07001892 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00001893 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 return error;
1895}
1896
Al Viro73d049a2011-03-11 12:08:24 -05001897static int may_open(struct path *path, int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001899 struct dentry *dentry = path->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 struct inode *inode = dentry->d_inode;
1901 int error;
1902
Al Virobcda7652011-03-13 16:42:14 -04001903 /* O_PATH? */
1904 if (!acc_mode)
1905 return 0;
1906
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 if (!inode)
1908 return -ENOENT;
1909
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001910 switch (inode->i_mode & S_IFMT) {
1911 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001913 case S_IFDIR:
1914 if (acc_mode & MAY_WRITE)
1915 return -EISDIR;
1916 break;
1917 case S_IFBLK:
1918 case S_IFCHR:
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001919 if (path->mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001921 /*FALLTHRU*/
1922 case S_IFIFO:
1923 case S_IFSOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001925 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08001926 }
Dave Hansenb41572e2007-10-16 23:31:14 -07001927
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001928 error = inode_permission(inode, acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07001929 if (error)
1930 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05001931
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 /*
1933 * An append-only file must be opened in append mode for writing.
1934 */
1935 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05001936 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05001937 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05001939 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 }
1941
1942 /* O_NOATIME can only be set by the owner or superuser */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001943 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
Al Viro7715b522009-12-16 03:54:00 -05001944 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
1946 /*
1947 * Ensure there are no outstanding leases on the file.
1948 */
Al Virob65a9cf2009-12-16 06:27:40 -05001949 return break_lease(inode, flag);
Al Viro7715b522009-12-16 03:54:00 -05001950}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
Jeff Laytone1181ee2010-12-07 16:19:50 -05001952static int handle_truncate(struct file *filp)
Al Viro7715b522009-12-16 03:54:00 -05001953{
Jeff Laytone1181ee2010-12-07 16:19:50 -05001954 struct path *path = &filp->f_path;
Al Viro7715b522009-12-16 03:54:00 -05001955 struct inode *inode = path->dentry->d_inode;
1956 int error = get_write_access(inode);
1957 if (error)
1958 return error;
1959 /*
1960 * Refuse to truncate files with mandatory locks held on them.
1961 */
1962 error = locks_verify_locked(inode);
1963 if (!error)
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +09001964 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05001965 if (!error) {
1966 error = do_truncate(path->dentry, 0,
1967 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
Jeff Laytone1181ee2010-12-07 16:19:50 -05001968 filp);
Al Viro7715b522009-12-16 03:54:00 -05001969 }
1970 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04001971 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972}
1973
Dave Hansend57999e2008-02-15 14:37:27 -08001974/*
Dave Hansend57999e2008-02-15 14:37:27 -08001975 * Note that while the flag value (low two bits) for sys_open means:
1976 * 00 - read-only
1977 * 01 - write-only
1978 * 10 - read-write
1979 * 11 - special
1980 * it is changed into
1981 * 00 - no permissions needed
1982 * 01 - read-permission
1983 * 10 - write-permission
1984 * 11 - read-write
1985 * for the internal routines (ie open_namei()/follow_link() etc)
1986 * This is more logical, and also allows the 00 "no perm needed"
1987 * to be used for symlinks (where the permissions are checked
1988 * later).
1989 *
1990*/
1991static inline int open_to_namei_flags(int flag)
1992{
1993 if ((flag+1) & O_ACCMODE)
1994 flag++;
1995 return flag;
1996}
1997
Nick Piggin31e6b012011-01-07 17:49:52 +11001998/*
Al Virofe2d35f2011-03-05 22:58:25 -05001999 * Handle the last step of open()
Nick Piggin31e6b012011-01-07 17:49:52 +11002000 */
Al Virofb1cc552009-12-24 01:58:28 -05002001static struct file *do_last(struct nameidata *nd, struct path *path,
Al Viroc3e380b2011-02-23 13:39:45 -05002002 const struct open_flags *op, const char *pathname)
Al Virofb1cc552009-12-24 01:58:28 -05002003{
Al Viroa1e28032009-12-24 02:12:06 -05002004 struct dentry *dir = nd->path.dentry;
Al Viro6c0d46c2011-03-09 00:59:59 -05002005 struct dentry *dentry;
Al Viroca344a892011-03-09 00:36:45 -05002006 int open_flag = op->open_flag;
Al Viro6c0d46c2011-03-09 00:59:59 -05002007 int will_truncate = open_flag & O_TRUNC;
Al Viroca344a892011-03-09 00:36:45 -05002008 int want_write = 0;
Al Virobcda7652011-03-13 16:42:14 -04002009 int acc_mode = op->acc_mode;
Al Virofb1cc552009-12-24 01:58:28 -05002010 struct file *filp;
Al Viro16c2cd72011-02-22 15:50:10 -05002011 int error;
Al Virofb1cc552009-12-24 01:58:28 -05002012
Al Viroc3e380b2011-02-23 13:39:45 -05002013 nd->flags &= ~LOOKUP_PARENT;
2014 nd->flags |= op->intent;
2015
Al Viro1f36f772009-12-26 10:56:19 -05002016 switch (nd->last_type) {
2017 case LAST_DOTDOT:
Neil Brown176306f2010-05-24 16:57:56 +10002018 case LAST_DOT:
Al Virofe2d35f2011-03-05 22:58:25 -05002019 error = handle_dots(nd, nd->last_type);
2020 if (error)
2021 return ERR_PTR(error);
Al Viro1f36f772009-12-26 10:56:19 -05002022 /* fallthrough */
Al Viro1f36f772009-12-26 10:56:19 -05002023 case LAST_ROOT:
Al Viro9f1fafe2011-03-25 11:00:12 -04002024 error = complete_walk(nd);
Al Viro16c2cd72011-02-22 15:50:10 -05002025 if (error)
Al Viro9f1fafe2011-03-25 11:00:12 -04002026 return ERR_PTR(error);
Al Virofe2d35f2011-03-05 22:58:25 -05002027 audit_inode(pathname, nd->path.dentry);
Al Viroca344a892011-03-09 00:36:45 -05002028 if (open_flag & O_CREAT) {
Al Virofe2d35f2011-03-05 22:58:25 -05002029 error = -EISDIR;
2030 goto exit;
2031 }
2032 goto ok;
Al Viro1f36f772009-12-26 10:56:19 -05002033 case LAST_BIND:
Al Viro9f1fafe2011-03-25 11:00:12 -04002034 error = complete_walk(nd);
Al Viro16c2cd72011-02-22 15:50:10 -05002035 if (error)
Al Viro9f1fafe2011-03-25 11:00:12 -04002036 return ERR_PTR(error);
Al Viro1f36f772009-12-26 10:56:19 -05002037 audit_inode(pathname, dir);
Al Viro67ee3ad2009-12-26 07:01:01 -05002038 goto ok;
Al Viro1f36f772009-12-26 10:56:19 -05002039 }
Al Viro67ee3ad2009-12-26 07:01:01 -05002040
Al Viroca344a892011-03-09 00:36:45 -05002041 if (!(open_flag & O_CREAT)) {
Al Virobcda7652011-03-13 16:42:14 -04002042 int symlink_ok = 0;
Al Virofe2d35f2011-03-05 22:58:25 -05002043 if (nd->last.name[nd->last.len])
2044 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
Al Virobcda7652011-03-13 16:42:14 -04002045 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2046 symlink_ok = 1;
Al Virofe2d35f2011-03-05 22:58:25 -05002047 /* we _can_ be in RCU mode here */
Al Viroce57dfc2011-03-13 19:58:58 -04002048 error = walk_component(nd, path, &nd->last, LAST_NORM,
2049 !symlink_ok);
2050 if (error < 0)
Al Virofe2d35f2011-03-05 22:58:25 -05002051 return ERR_PTR(error);
Al Viroce57dfc2011-03-13 19:58:58 -04002052 if (error) /* symlink */
Al Virofe2d35f2011-03-05 22:58:25 -05002053 return NULL;
Al Virofe2d35f2011-03-05 22:58:25 -05002054 /* sayonara */
Al Viro9f1fafe2011-03-25 11:00:12 -04002055 error = complete_walk(nd);
2056 if (error)
2057 return ERR_PTR(-ECHILD);
Al Virofe2d35f2011-03-05 22:58:25 -05002058
2059 error = -ENOTDIR;
2060 if (nd->flags & LOOKUP_DIRECTORY) {
Al Viroce57dfc2011-03-13 19:58:58 -04002061 if (!nd->inode->i_op->lookup)
Al Virofe2d35f2011-03-05 22:58:25 -05002062 goto exit;
2063 }
2064 audit_inode(pathname, nd->path.dentry);
2065 goto ok;
2066 }
2067
2068 /* create side of things */
Al Viro9f1fafe2011-03-25 11:00:12 -04002069 error = complete_walk(nd);
2070 if (error)
2071 return ERR_PTR(error);
Al Virofe2d35f2011-03-05 22:58:25 -05002072
2073 audit_inode(pathname, dir);
Al Viro16c2cd72011-02-22 15:50:10 -05002074 error = -EISDIR;
Al Viro1f36f772009-12-26 10:56:19 -05002075 /* trailing slashes? */
Nick Piggin31e6b012011-01-07 17:49:52 +11002076 if (nd->last.name[nd->last.len])
2077 goto exit;
Al Viroa2c36b42009-12-24 03:39:50 -05002078
Al Viroa1e28032009-12-24 02:12:06 -05002079 mutex_lock(&dir->d_inode->i_mutex);
2080
Al Viro6c0d46c2011-03-09 00:59:59 -05002081 dentry = lookup_hash(nd);
2082 error = PTR_ERR(dentry);
2083 if (IS_ERR(dentry)) {
Al Virofb1cc552009-12-24 01:58:28 -05002084 mutex_unlock(&dir->d_inode->i_mutex);
2085 goto exit;
2086 }
2087
Al Viro6c0d46c2011-03-09 00:59:59 -05002088 path->dentry = dentry;
2089 path->mnt = nd->path.mnt;
2090
Al Virofb1cc552009-12-24 01:58:28 -05002091 /* Negative dentry, just create the file */
Al Viro6c0d46c2011-03-09 00:59:59 -05002092 if (!dentry->d_inode) {
2093 int mode = op->mode;
2094 if (!IS_POSIXACL(dir->d_inode))
2095 mode &= ~current_umask();
Al Virofb1cc552009-12-24 01:58:28 -05002096 /*
2097 * This write is needed to ensure that a
Al Viro6c0d46c2011-03-09 00:59:59 -05002098 * rw->ro transition does not occur between
Al Virofb1cc552009-12-24 01:58:28 -05002099 * the time when the file is created and when
2100 * a permanent write count is taken through
2101 * the 'struct file' in nameidata_to_filp().
2102 */
2103 error = mnt_want_write(nd->path.mnt);
2104 if (error)
2105 goto exit_mutex_unlock;
Al Viroca344a892011-03-09 00:36:45 -05002106 want_write = 1;
Al Viro9b44f1b2011-03-09 00:17:27 -05002107 /* Don't check for write permission, don't truncate */
Al Viroca344a892011-03-09 00:36:45 -05002108 open_flag &= ~O_TRUNC;
Al Viro6c0d46c2011-03-09 00:59:59 -05002109 will_truncate = 0;
Al Virobcda7652011-03-13 16:42:14 -04002110 acc_mode = MAY_OPEN;
Al Viro6c0d46c2011-03-09 00:59:59 -05002111 error = security_path_mknod(&nd->path, dentry, mode, 0);
2112 if (error)
2113 goto exit_mutex_unlock;
2114 error = vfs_create(dir->d_inode, dentry, mode, nd);
2115 if (error)
2116 goto exit_mutex_unlock;
2117 mutex_unlock(&dir->d_inode->i_mutex);
2118 dput(nd->path.dentry);
2119 nd->path.dentry = dentry;
Al Viroca344a892011-03-09 00:36:45 -05002120 goto common;
Al Virofb1cc552009-12-24 01:58:28 -05002121 }
2122
2123 /*
2124 * It already exists.
2125 */
2126 mutex_unlock(&dir->d_inode->i_mutex);
2127 audit_inode(pathname, path->dentry);
2128
2129 error = -EEXIST;
Al Viroca344a892011-03-09 00:36:45 -05002130 if (open_flag & O_EXCL)
Al Virofb1cc552009-12-24 01:58:28 -05002131 goto exit_dput;
2132
David Howells9875cf82011-01-14 18:45:21 +00002133 error = follow_managed(path, nd->flags);
2134 if (error < 0)
2135 goto exit_dput;
Al Virofb1cc552009-12-24 01:58:28 -05002136
2137 error = -ENOENT;
2138 if (!path->dentry->d_inode)
2139 goto exit_dput;
Al Viro9e67f362009-12-26 07:04:50 -05002140
2141 if (path->dentry->d_inode->i_op->follow_link)
Al Virofb1cc552009-12-24 01:58:28 -05002142 return NULL;
Al Virofb1cc552009-12-24 01:58:28 -05002143
2144 path_to_nameidata(path, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11002145 nd->inode = path->dentry->d_inode;
Al Virofb1cc552009-12-24 01:58:28 -05002146 error = -EISDIR;
Nick Piggin31e6b012011-01-07 17:49:52 +11002147 if (S_ISDIR(nd->inode->i_mode))
Al Virofb1cc552009-12-24 01:58:28 -05002148 goto exit;
Al Viro67ee3ad2009-12-26 07:01:01 -05002149ok:
Al Viro6c0d46c2011-03-09 00:59:59 -05002150 if (!S_ISREG(nd->inode->i_mode))
2151 will_truncate = 0;
2152
Al Viro0f9d1a12011-03-09 00:13:14 -05002153 if (will_truncate) {
2154 error = mnt_want_write(nd->path.mnt);
2155 if (error)
2156 goto exit;
Al Viroca344a892011-03-09 00:36:45 -05002157 want_write = 1;
Al Viro0f9d1a12011-03-09 00:13:14 -05002158 }
Al Viroca344a892011-03-09 00:36:45 -05002159common:
Al Virobcda7652011-03-13 16:42:14 -04002160 error = may_open(&nd->path, acc_mode, open_flag);
Al Viroca344a892011-03-09 00:36:45 -05002161 if (error)
Al Viro0f9d1a12011-03-09 00:13:14 -05002162 goto exit;
Al Viro0f9d1a12011-03-09 00:13:14 -05002163 filp = nameidata_to_filp(nd);
2164 if (!IS_ERR(filp)) {
2165 error = ima_file_check(filp, op->acc_mode);
2166 if (error) {
2167 fput(filp);
2168 filp = ERR_PTR(error);
2169 }
2170 }
2171 if (!IS_ERR(filp)) {
2172 if (will_truncate) {
2173 error = handle_truncate(filp);
2174 if (error) {
2175 fput(filp);
2176 filp = ERR_PTR(error);
2177 }
2178 }
2179 }
Al Viroca344a892011-03-09 00:36:45 -05002180out:
2181 if (want_write)
Al Viro0f9d1a12011-03-09 00:13:14 -05002182 mnt_drop_write(nd->path.mnt);
2183 path_put(&nd->path);
Al Virofb1cc552009-12-24 01:58:28 -05002184 return filp;
2185
2186exit_mutex_unlock:
2187 mutex_unlock(&dir->d_inode->i_mutex);
2188exit_dput:
2189 path_put_conditional(path, nd);
2190exit:
Al Viroca344a892011-03-09 00:36:45 -05002191 filp = ERR_PTR(error);
2192 goto out;
Al Virofb1cc552009-12-24 01:58:28 -05002193}
2194
Al Viro13aab422011-02-23 17:54:08 -05002195static struct file *path_openat(int dfd, const char *pathname,
Al Viro73d049a2011-03-11 12:08:24 -05002196 struct nameidata *nd, const struct open_flags *op, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197{
Al Virofe2d35f2011-03-05 22:58:25 -05002198 struct file *base = NULL;
Dave Hansen4a3fd212008-02-15 14:37:48 -08002199 struct file *filp;
Al Viro9850c052010-01-13 15:01:15 -05002200 struct path path;
Al Viro13aab422011-02-23 17:54:08 -05002201 int error;
Nick Piggin31e6b012011-01-07 17:49:52 +11002202
2203 filp = get_empty_filp();
2204 if (!filp)
2205 return ERR_PTR(-ENFILE);
2206
Al Viro47c805d2011-02-23 17:44:09 -05002207 filp->f_flags = op->open_flag;
Al Viro73d049a2011-03-11 12:08:24 -05002208 nd->intent.open.file = filp;
2209 nd->intent.open.flags = open_to_namei_flags(op->open_flag);
2210 nd->intent.open.create_mode = op->mode;
Nick Piggin31e6b012011-01-07 17:49:52 +11002211
Al Viro73d049a2011-03-11 12:08:24 -05002212 error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
Nick Piggin31e6b012011-01-07 17:49:52 +11002213 if (unlikely(error))
Al Viro13aab422011-02-23 17:54:08 -05002214 goto out_filp;
Nick Piggin31e6b012011-01-07 17:49:52 +11002215
Al Virofe2d35f2011-03-05 22:58:25 -05002216 current->total_link_count = 0;
Al Viro73d049a2011-03-11 12:08:24 -05002217 error = link_path_walk(pathname, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11002218 if (unlikely(error))
2219 goto out_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
Al Viro73d049a2011-03-11 12:08:24 -05002221 filp = do_last(nd, &path, op, pathname);
Al Viro806b6812009-12-26 07:16:40 -05002222 while (unlikely(!filp)) { /* trailing symlink */
Nick Piggin7b9337a2011-01-14 08:42:43 +00002223 struct path link = path;
Al Virodef4af32009-12-26 08:37:05 -05002224 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04002225 if (!(nd->flags & LOOKUP_FOLLOW)) {
Al Viro73d049a2011-03-11 12:08:24 -05002226 path_put_conditional(&path, nd);
2227 path_put(&nd->path);
Al Viro40b39132011-03-09 16:22:18 -05002228 filp = ERR_PTR(-ELOOP);
2229 break;
2230 }
Al Viro73d049a2011-03-11 12:08:24 -05002231 nd->flags |= LOOKUP_PARENT;
2232 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
Al Viro574197e2011-03-14 22:20:34 -04002233 error = follow_link(&link, nd, &cookie);
Al Viroc3e380b2011-02-23 13:39:45 -05002234 if (unlikely(error))
Al Virof1afe9e2011-02-22 22:27:28 -05002235 filp = ERR_PTR(error);
Al Viroc3e380b2011-02-23 13:39:45 -05002236 else
Al Viro73d049a2011-03-11 12:08:24 -05002237 filp = do_last(nd, &path, op, pathname);
Al Viro574197e2011-03-14 22:20:34 -04002238 put_link(nd, &link, cookie);
Al Viro806b6812009-12-26 07:16:40 -05002239 }
Al Viro10fa8e62009-12-26 07:09:49 -05002240out:
Al Viro73d049a2011-03-11 12:08:24 -05002241 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
2242 path_put(&nd->root);
Al Virofe2d35f2011-03-05 22:58:25 -05002243 if (base)
2244 fput(base);
Al Viro73d049a2011-03-11 12:08:24 -05002245 release_open_intent(nd);
Al Viro10fa8e62009-12-26 07:09:49 -05002246 return filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247
Nick Piggin31e6b012011-01-07 17:49:52 +11002248out_filp:
Al Viro806b6812009-12-26 07:16:40 -05002249 filp = ERR_PTR(error);
Al Viro10fa8e62009-12-26 07:09:49 -05002250 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251}
2252
Al Viro13aab422011-02-23 17:54:08 -05002253struct file *do_filp_open(int dfd, const char *pathname,
2254 const struct open_flags *op, int flags)
2255{
Al Viro73d049a2011-03-11 12:08:24 -05002256 struct nameidata nd;
Al Viro13aab422011-02-23 17:54:08 -05002257 struct file *filp;
2258
Al Viro73d049a2011-03-11 12:08:24 -05002259 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
Al Viro13aab422011-02-23 17:54:08 -05002260 if (unlikely(filp == ERR_PTR(-ECHILD)))
Al Viro73d049a2011-03-11 12:08:24 -05002261 filp = path_openat(dfd, pathname, &nd, op, flags);
Al Viro13aab422011-02-23 17:54:08 -05002262 if (unlikely(filp == ERR_PTR(-ESTALE)))
Al Viro73d049a2011-03-11 12:08:24 -05002263 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
Al Viro13aab422011-02-23 17:54:08 -05002264 return filp;
2265}
2266
Al Viro73d049a2011-03-11 12:08:24 -05002267struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
2268 const char *name, const struct open_flags *op, int flags)
2269{
2270 struct nameidata nd;
2271 struct file *file;
2272
2273 nd.root.mnt = mnt;
2274 nd.root.dentry = dentry;
2275
2276 flags |= LOOKUP_ROOT;
2277
Al Virobcda7652011-03-13 16:42:14 -04002278 if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
Al Viro73d049a2011-03-11 12:08:24 -05002279 return ERR_PTR(-ELOOP);
2280
2281 file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
2282 if (unlikely(file == ERR_PTR(-ECHILD)))
2283 file = path_openat(-1, name, &nd, op, flags);
2284 if (unlikely(file == ERR_PTR(-ESTALE)))
2285 file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
2286 return file;
2287}
2288
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289/**
2290 * lookup_create - lookup a dentry, creating it if it doesn't exist
2291 * @nd: nameidata info
2292 * @is_dir: directory flag
2293 *
2294 * Simple function to lookup and return a dentry and create it
2295 * if it doesn't exist. Is SMP-safe.
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002296 *
Jan Blunck4ac91372008-02-14 19:34:32 -08002297 * Returns with nd->path.dentry->d_inode->i_mutex locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 */
2299struct dentry *lookup_create(struct nameidata *nd, int is_dir)
2300{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002301 struct dentry *dentry = ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
Jan Blunck4ac91372008-02-14 19:34:32 -08002303 mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002304 /*
2305 * Yucky last component or no last component at all?
2306 * (foo/., foo/.., /////)
2307 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 if (nd->last_type != LAST_NORM)
2309 goto fail;
2310 nd->flags &= ~LOOKUP_PARENT;
Al Viro35165862008-08-05 03:00:49 -04002311 nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
ASANO Masahiroa6349042006-08-22 20:06:02 -04002312 nd->intent.open.flags = O_EXCL;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002313
2314 /*
2315 * Do the final lookup.
2316 */
Christoph Hellwig49705b72005-11-08 21:35:06 -08002317 dentry = lookup_hash(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 if (IS_ERR(dentry))
2319 goto fail;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002320
Al Viroe9baf6e2008-05-15 04:49:12 -04002321 if (dentry->d_inode)
2322 goto eexist;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002323 /*
2324 * Special case - lookup gave negative, but... we had foo/bar/
2325 * From the vfs_mknod() POV we just have a negative dentry -
2326 * all is fine. Let's be bastards - you had / on the end, you've
2327 * been asking for (non-existent) directory. -ENOENT for you.
2328 */
Al Viroe9baf6e2008-05-15 04:49:12 -04002329 if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
2330 dput(dentry);
2331 dentry = ERR_PTR(-ENOENT);
2332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 return dentry;
Al Viroe9baf6e2008-05-15 04:49:12 -04002334eexist:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 dput(dentry);
Al Viroe9baf6e2008-05-15 04:49:12 -04002336 dentry = ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337fail:
2338 return dentry;
2339}
Christoph Hellwigf81a0bf2005-05-19 12:26:43 -07002340EXPORT_SYMBOL_GPL(lookup_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
2342int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
2343{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002344 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
2346 if (error)
2347 return error;
2348
Serge E. Hallyne795b712011-03-23 16:43:25 -07002349 if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
2350 !ns_capable(inode_userns(dir), CAP_MKNOD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 return -EPERM;
2352
Al Viroacfa4382008-12-04 10:06:33 -05002353 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 return -EPERM;
2355
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07002356 error = devcgroup_inode_mknod(mode, dev);
2357 if (error)
2358 return error;
2359
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 error = security_inode_mknod(dir, dentry, mode, dev);
2361 if (error)
2362 return error;
2363
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002365 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002366 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 return error;
2368}
2369
Dave Hansen463c3192008-02-15 14:37:57 -08002370static int may_mknod(mode_t mode)
2371{
2372 switch (mode & S_IFMT) {
2373 case S_IFREG:
2374 case S_IFCHR:
2375 case S_IFBLK:
2376 case S_IFIFO:
2377 case S_IFSOCK:
2378 case 0: /* zero mode translates to S_IFREG */
2379 return 0;
2380 case S_IFDIR:
2381 return -EPERM;
2382 default:
2383 return -EINVAL;
2384 }
2385}
2386
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002387SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
2388 unsigned, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389{
Al Viro2ad94ae2008-07-21 09:32:51 -04002390 int error;
2391 char *tmp;
2392 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 struct nameidata nd;
2394
2395 if (S_ISDIR(mode))
2396 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
Al Viro2ad94ae2008-07-21 09:32:51 -04002398 error = user_path_parent(dfd, filename, &nd, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002400 return error;
2401
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 dentry = lookup_create(&nd, 0);
Dave Hansen463c3192008-02-15 14:37:57 -08002403 if (IS_ERR(dentry)) {
2404 error = PTR_ERR(dentry);
2405 goto out_unlock;
2406 }
Jan Blunck4ac91372008-02-14 19:34:32 -08002407 if (!IS_POSIXACL(nd.path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04002408 mode &= ~current_umask();
Dave Hansen463c3192008-02-15 14:37:57 -08002409 error = may_mknod(mode);
2410 if (error)
2411 goto out_dput;
2412 error = mnt_want_write(nd.path.mnt);
2413 if (error)
2414 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002415 error = security_path_mknod(&nd.path, dentry, mode, dev);
2416 if (error)
2417 goto out_drop_write;
Dave Hansen463c3192008-02-15 14:37:57 -08002418 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 case 0: case S_IFREG:
Jan Blunck4ac91372008-02-14 19:34:32 -08002420 error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 break;
2422 case S_IFCHR: case S_IFBLK:
Jan Blunck4ac91372008-02-14 19:34:32 -08002423 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 new_decode_dev(dev));
2425 break;
2426 case S_IFIFO: case S_IFSOCK:
Jan Blunck4ac91372008-02-14 19:34:32 -08002427 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 }
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002430out_drop_write:
Dave Hansen463c3192008-02-15 14:37:57 -08002431 mnt_drop_write(nd.path.mnt);
2432out_dput:
2433 dput(dentry);
2434out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002435 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002436 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 putname(tmp);
2438
2439 return error;
2440}
2441
Heiko Carstens3480b252009-01-14 14:14:16 +01002442SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002443{
2444 return sys_mknodat(AT_FDCWD, filename, mode, dev);
2445}
2446
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2448{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002449 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
2451 if (error)
2452 return error;
2453
Al Viroacfa4382008-12-04 10:06:33 -05002454 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 return -EPERM;
2456
2457 mode &= (S_IRWXUGO|S_ISVTX);
2458 error = security_inode_mkdir(dir, dentry, mode);
2459 if (error)
2460 return error;
2461
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002463 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002464 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 return error;
2466}
2467
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002468SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469{
2470 int error = 0;
2471 char * tmp;
Dave Hansen6902d922006-09-30 23:29:01 -07002472 struct dentry *dentry;
2473 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
Al Viro2ad94ae2008-07-21 09:32:51 -04002475 error = user_path_parent(dfd, pathname, &nd, &tmp);
2476 if (error)
Dave Hansen6902d922006-09-30 23:29:01 -07002477 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
Dave Hansen6902d922006-09-30 23:29:01 -07002479 dentry = lookup_create(&nd, 1);
2480 error = PTR_ERR(dentry);
2481 if (IS_ERR(dentry))
2482 goto out_unlock;
2483
Jan Blunck4ac91372008-02-14 19:34:32 -08002484 if (!IS_POSIXACL(nd.path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04002485 mode &= ~current_umask();
Dave Hansen463c3192008-02-15 14:37:57 -08002486 error = mnt_want_write(nd.path.mnt);
2487 if (error)
2488 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002489 error = security_path_mkdir(&nd.path, dentry, mode);
2490 if (error)
2491 goto out_drop_write;
Jan Blunck4ac91372008-02-14 19:34:32 -08002492 error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002493out_drop_write:
Dave Hansen463c3192008-02-15 14:37:57 -08002494 mnt_drop_write(nd.path.mnt);
2495out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002496 dput(dentry);
2497out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002498 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002499 path_put(&nd.path);
Dave Hansen6902d922006-09-30 23:29:01 -07002500 putname(tmp);
2501out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 return error;
2503}
2504
Heiko Carstens3cdad422009-01-14 14:14:22 +01002505SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002506{
2507 return sys_mkdirat(AT_FDCWD, pathname, mode);
2508}
2509
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510/*
Sage Weila71905f2011-05-24 13:06:08 -07002511 * The dentry_unhash() helper will try to drop the dentry early: we
2512 * should have a usage count of 2 if we're the only user of this
2513 * dentry, and if that is true (possibly after pruning the dcache),
2514 * then we drop the dentry now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 *
2516 * A low-level filesystem can, if it choses, legally
2517 * do a
2518 *
2519 * if (!d_unhashed(dentry))
2520 * return -EBUSY;
2521 *
2522 * if it cannot handle the case of removing a directory
2523 * that is still in use by something else..
2524 */
2525void dentry_unhash(struct dentry *dentry)
2526{
Vasily Averindc168422006-12-06 20:37:07 -08002527 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 spin_lock(&dentry->d_lock);
Sage Weil64252c72011-05-24 13:06:05 -07002529 if (dentry->d_count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 __d_drop(dentry);
2531 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532}
2533
2534int vfs_rmdir(struct inode *dir, struct dentry *dentry)
2535{
2536 int error = may_delete(dir, dentry, 1);
2537
2538 if (error)
2539 return error;
2540
Al Viroacfa4382008-12-04 10:06:33 -05002541 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 return -EPERM;
2543
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002544 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545
Sage Weil912dbc12011-05-24 13:06:11 -07002546 error = -EBUSY;
2547 if (d_mountpoint(dentry))
2548 goto out;
2549
2550 error = security_inode_rmdir(dir, dentry);
2551 if (error)
2552 goto out;
2553
Sage Weil3cebde22011-05-29 21:20:59 -07002554 shrink_dcache_parent(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07002555 error = dir->i_op->rmdir(dir, dentry);
2556 if (error)
2557 goto out;
2558
2559 dentry->d_inode->i_flags |= S_DEAD;
2560 dont_mount(dentry);
2561
2562out:
2563 mutex_unlock(&dentry->d_inode->i_mutex);
2564 if (!error)
2565 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 return error;
2567}
2568
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002569static long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570{
2571 int error = 0;
2572 char * name;
2573 struct dentry *dentry;
2574 struct nameidata nd;
2575
Al Viro2ad94ae2008-07-21 09:32:51 -04002576 error = user_path_parent(dfd, pathname, &nd, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002578 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579
2580 switch(nd.last_type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002581 case LAST_DOTDOT:
2582 error = -ENOTEMPTY;
2583 goto exit1;
2584 case LAST_DOT:
2585 error = -EINVAL;
2586 goto exit1;
2587 case LAST_ROOT:
2588 error = -EBUSY;
2589 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002591
2592 nd.flags &= ~LOOKUP_PARENT;
2593
Jan Blunck4ac91372008-02-14 19:34:32 -08002594 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002595 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002597 if (IS_ERR(dentry))
2598 goto exit2;
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04002599 if (!dentry->d_inode) {
2600 error = -ENOENT;
2601 goto exit3;
2602 }
Dave Hansen06227532008-02-15 14:37:34 -08002603 error = mnt_want_write(nd.path.mnt);
2604 if (error)
2605 goto exit3;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002606 error = security_path_rmdir(&nd.path, dentry);
2607 if (error)
2608 goto exit4;
Jan Blunck4ac91372008-02-14 19:34:32 -08002609 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002610exit4:
Dave Hansen06227532008-02-15 14:37:34 -08002611 mnt_drop_write(nd.path.mnt);
2612exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07002613 dput(dentry);
2614exit2:
Jan Blunck4ac91372008-02-14 19:34:32 -08002615 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002617 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 putname(name);
2619 return error;
2620}
2621
Heiko Carstens3cdad422009-01-14 14:14:22 +01002622SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002623{
2624 return do_rmdir(AT_FDCWD, pathname);
2625}
2626
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627int vfs_unlink(struct inode *dir, struct dentry *dentry)
2628{
2629 int error = may_delete(dir, dentry, 0);
2630
2631 if (error)
2632 return error;
2633
Al Viroacfa4382008-12-04 10:06:33 -05002634 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 return -EPERM;
2636
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002637 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 if (d_mountpoint(dentry))
2639 error = -EBUSY;
2640 else {
2641 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05002642 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 error = dir->i_op->unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05002644 if (!error)
Al Virod83c49f2010-04-30 17:17:09 -04002645 dont_mount(dentry);
Al Virobec10522010-03-03 14:12:08 -05002646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002648 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649
2650 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2651 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
Jan Karaece95912008-02-06 01:37:13 -08002652 fsnotify_link_count(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 }
Robert Love0eeca282005-07-12 17:06:03 -04002655
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 return error;
2657}
2658
2659/*
2660 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002661 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 * writeout happening, and we don't want to prevent access to the directory
2663 * while waiting on the I/O.
2664 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002665static long do_unlinkat(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666{
Al Viro2ad94ae2008-07-21 09:32:51 -04002667 int error;
2668 char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 struct dentry *dentry;
2670 struct nameidata nd;
2671 struct inode *inode = NULL;
2672
Al Viro2ad94ae2008-07-21 09:32:51 -04002673 error = user_path_parent(dfd, pathname, &nd, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002675 return error;
2676
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 error = -EISDIR;
2678 if (nd.last_type != LAST_NORM)
2679 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002680
2681 nd.flags &= ~LOOKUP_PARENT;
2682
Jan Blunck4ac91372008-02-14 19:34:32 -08002683 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002684 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 error = PTR_ERR(dentry);
2686 if (!IS_ERR(dentry)) {
2687 /* Why not before? Because we want correct error value */
Török Edwin50338b82011-06-16 00:06:14 +03002688 if (nd.last.name[nd.last.len])
2689 goto slashes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 inode = dentry->d_inode;
Török Edwin50338b82011-06-16 00:06:14 +03002691 if (!inode)
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04002692 goto slashes;
2693 ihold(inode);
Dave Hansen06227532008-02-15 14:37:34 -08002694 error = mnt_want_write(nd.path.mnt);
2695 if (error)
2696 goto exit2;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002697 error = security_path_unlink(&nd.path, dentry);
2698 if (error)
2699 goto exit3;
Jan Blunck4ac91372008-02-14 19:34:32 -08002700 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002701exit3:
Dave Hansen06227532008-02-15 14:37:34 -08002702 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 exit2:
2704 dput(dentry);
2705 }
Jan Blunck4ac91372008-02-14 19:34:32 -08002706 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 if (inode)
2708 iput(inode); /* truncate the inode here */
2709exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002710 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 putname(name);
2712 return error;
2713
2714slashes:
2715 error = !dentry->d_inode ? -ENOENT :
2716 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2717 goto exit2;
2718}
2719
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002720SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002721{
2722 if ((flag & ~AT_REMOVEDIR) != 0)
2723 return -EINVAL;
2724
2725 if (flag & AT_REMOVEDIR)
2726 return do_rmdir(dfd, pathname);
2727
2728 return do_unlinkat(dfd, pathname);
2729}
2730
Heiko Carstens3480b252009-01-14 14:14:16 +01002731SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002732{
2733 return do_unlinkat(AT_FDCWD, pathname);
2734}
2735
Miklos Szeredidb2e7472008-06-24 16:50:16 +02002736int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002738 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739
2740 if (error)
2741 return error;
2742
Al Viroacfa4382008-12-04 10:06:33 -05002743 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 return -EPERM;
2745
2746 error = security_inode_symlink(dir, dentry, oldname);
2747 if (error)
2748 return error;
2749
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002751 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002752 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 return error;
2754}
2755
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002756SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
2757 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758{
Al Viro2ad94ae2008-07-21 09:32:51 -04002759 int error;
2760 char *from;
2761 char *to;
Dave Hansen6902d922006-09-30 23:29:01 -07002762 struct dentry *dentry;
2763 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764
2765 from = getname(oldname);
Al Viro2ad94ae2008-07-21 09:32:51 -04002766 if (IS_ERR(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 return PTR_ERR(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04002768
2769 error = user_path_parent(newdfd, newname, &nd, &to);
2770 if (error)
Dave Hansen6902d922006-09-30 23:29:01 -07002771 goto out_putname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772
Dave Hansen6902d922006-09-30 23:29:01 -07002773 dentry = lookup_create(&nd, 0);
2774 error = PTR_ERR(dentry);
2775 if (IS_ERR(dentry))
2776 goto out_unlock;
2777
Dave Hansen75c3f292008-02-15 14:37:45 -08002778 error = mnt_want_write(nd.path.mnt);
2779 if (error)
2780 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002781 error = security_path_symlink(&nd.path, dentry, from);
2782 if (error)
2783 goto out_drop_write;
Miklos Szeredidb2e7472008-06-24 16:50:16 +02002784 error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002785out_drop_write:
Dave Hansen75c3f292008-02-15 14:37:45 -08002786 mnt_drop_write(nd.path.mnt);
2787out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002788 dput(dentry);
2789out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002790 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002791 path_put(&nd.path);
Dave Hansen6902d922006-09-30 23:29:01 -07002792 putname(to);
2793out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 putname(from);
2795 return error;
2796}
2797
Heiko Carstens3480b252009-01-14 14:14:16 +01002798SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002799{
2800 return sys_symlinkat(oldname, AT_FDCWD, newname);
2801}
2802
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2804{
2805 struct inode *inode = old_dentry->d_inode;
2806 int error;
2807
2808 if (!inode)
2809 return -ENOENT;
2810
Miklos Szeredia95164d2008-07-30 15:08:48 +02002811 error = may_create(dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 if (error)
2813 return error;
2814
2815 if (dir->i_sb != inode->i_sb)
2816 return -EXDEV;
2817
2818 /*
2819 * A link to an append-only or immutable file cannot be created.
2820 */
2821 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2822 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05002823 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002825 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 return -EPERM;
2827
2828 error = security_inode_link(old_dentry, dir, new_dentry);
2829 if (error)
2830 return error;
2831
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002832 mutex_lock(&inode->i_mutex);
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05302833 /* Make sure we don't allow creating hardlink to an unlinked file */
2834 if (inode->i_nlink == 0)
2835 error = -ENOENT;
2836 else
2837 error = dir->i_op->link(old_dentry, dir, new_dentry);
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002838 mutex_unlock(&inode->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07002839 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002840 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 return error;
2842}
2843
2844/*
2845 * Hardlinks are often used in delicate situations. We avoid
2846 * security-related surprises by not following symlinks on the
2847 * newname. --KAB
2848 *
2849 * We don't follow them on the oldname either to be compatible
2850 * with linux 2.0, and to avoid hard-linking to directories
2851 * and other special files. --ADM
2852 */
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002853SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
2854 int, newdfd, const char __user *, newname, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855{
2856 struct dentry *new_dentry;
Al Viro2d8f3032008-07-22 09:59:21 -04002857 struct nameidata nd;
2858 struct path old_path;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05302859 int how = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 int error;
Al Viro2ad94ae2008-07-21 09:32:51 -04002861 char *to;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05302863 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002864 return -EINVAL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05302865 /*
2866 * To use null names we require CAP_DAC_READ_SEARCH
2867 * This ensures that not everyone will be able to create
2868 * handlink using the passed filedescriptor.
2869 */
2870 if (flags & AT_EMPTY_PATH) {
2871 if (!capable(CAP_DAC_READ_SEARCH))
2872 return -ENOENT;
2873 how = LOOKUP_EMPTY;
2874 }
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002875
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05302876 if (flags & AT_SYMLINK_FOLLOW)
2877 how |= LOOKUP_FOLLOW;
2878
2879 error = user_path_at(olddfd, oldname, how, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002881 return error;
2882
2883 error = user_path_parent(newdfd, newname, &nd, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 if (error)
2885 goto out;
2886 error = -EXDEV;
Al Viro2d8f3032008-07-22 09:59:21 -04002887 if (old_path.mnt != nd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 goto out_release;
2889 new_dentry = lookup_create(&nd, 0);
2890 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002891 if (IS_ERR(new_dentry))
2892 goto out_unlock;
Dave Hansen75c3f292008-02-15 14:37:45 -08002893 error = mnt_want_write(nd.path.mnt);
2894 if (error)
2895 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002896 error = security_path_link(old_path.dentry, &nd.path, new_dentry);
2897 if (error)
2898 goto out_drop_write;
Al Viro2d8f3032008-07-22 09:59:21 -04002899 error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002900out_drop_write:
Dave Hansen75c3f292008-02-15 14:37:45 -08002901 mnt_drop_write(nd.path.mnt);
2902out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002903 dput(new_dentry);
2904out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002905 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906out_release:
Jan Blunck1d957f92008-02-14 19:34:35 -08002907 path_put(&nd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04002908 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909out:
Al Viro2d8f3032008-07-22 09:59:21 -04002910 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911
2912 return error;
2913}
2914
Heiko Carstens3480b252009-01-14 14:14:16 +01002915SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002916{
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002917 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002918}
2919
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920/*
2921 * The worst of all namespace operations - renaming directory. "Perverted"
2922 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2923 * Problems:
2924 * a) we can get into loop creation. Check is done in is_subdir().
2925 * b) race potential - two innocent renames can create a loop together.
2926 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002927 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 * story.
2929 * c) we have to lock _three_ objects - parents and victim (if it exists).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002930 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 * whether the target exists). Solution: try to be smart with locking
2932 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002933 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 * move will be locked. Thus we can rank directories by the tree
2935 * (ancestors first) and rank all non-directories after them.
2936 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002937 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 * HOWEVER, it relies on the assumption that any object with ->lookup()
2939 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2940 * we'd better make sure that there's no link(2) for them.
Sage Weile4eaac02011-05-24 13:06:07 -07002941 * d) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002942 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05002944 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 * locking].
2946 */
Adrian Bunk75c96f82005-05-05 16:16:09 -07002947static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
2948 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949{
2950 int error = 0;
Sage Weil9055cba2011-05-24 13:06:12 -07002951 struct inode *target = new_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952
2953 /*
2954 * If we are going to change the parent - check write permissions,
2955 * we'll need to flip '..'.
2956 */
2957 if (new_dir != old_dir) {
Al Virof419a2e2008-07-22 00:07:17 -04002958 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 if (error)
2960 return error;
2961 }
2962
2963 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2964 if (error)
2965 return error;
2966
Al Virod83c49f2010-04-30 17:17:09 -04002967 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002968 mutex_lock(&target->i_mutex);
Sage Weil9055cba2011-05-24 13:06:12 -07002969
2970 error = -EBUSY;
2971 if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
2972 goto out;
2973
Sage Weil3cebde22011-05-29 21:20:59 -07002974 if (target)
2975 shrink_dcache_parent(new_dentry);
Sage Weil9055cba2011-05-24 13:06:12 -07002976 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2977 if (error)
2978 goto out;
2979
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 if (target) {
Sage Weil9055cba2011-05-24 13:06:12 -07002981 target->i_flags |= S_DEAD;
2982 dont_mount(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 }
Sage Weil9055cba2011-05-24 13:06:12 -07002984out:
2985 if (target)
2986 mutex_unlock(&target->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07002987 if (!error)
Mark Fasheh349457c2006-09-08 14:22:21 -07002988 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
2989 d_move(old_dentry,new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 return error;
2991}
2992
Adrian Bunk75c96f82005-05-05 16:16:09 -07002993static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
2994 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995{
Sage Weil51892bb2011-05-24 13:06:13 -07002996 struct inode *target = new_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 int error;
2998
2999 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3000 if (error)
3001 return error;
3002
3003 dget(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003005 mutex_lock(&target->i_mutex);
Sage Weil51892bb2011-05-24 13:06:13 -07003006
3007 error = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
Sage Weil51892bb2011-05-24 13:06:13 -07003009 goto out;
3010
3011 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3012 if (error)
3013 goto out;
3014
3015 if (target)
3016 dont_mount(new_dentry);
3017 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3018 d_move(old_dentry, new_dentry);
3019out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003021 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 dput(new_dentry);
3023 return error;
3024}
3025
3026int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
3027 struct inode *new_dir, struct dentry *new_dentry)
3028{
3029 int error;
3030 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
Eric Paris59b0df22010-02-08 12:53:52 -05003031 const unsigned char *old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032
3033 if (old_dentry->d_inode == new_dentry->d_inode)
3034 return 0;
3035
3036 error = may_delete(old_dir, old_dentry, is_dir);
3037 if (error)
3038 return error;
3039
3040 if (!new_dentry->d_inode)
Miklos Szeredia95164d2008-07-30 15:08:48 +02003041 error = may_create(new_dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 else
3043 error = may_delete(new_dir, new_dentry, is_dir);
3044 if (error)
3045 return error;
3046
Al Viroacfa4382008-12-04 10:06:33 -05003047 if (!old_dir->i_op->rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 return -EPERM;
3049
Robert Love0eeca282005-07-12 17:06:03 -04003050 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
3051
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 if (is_dir)
3053 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
3054 else
3055 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
Al Viro123df292009-12-25 04:57:57 -05003056 if (!error)
3057 fsnotify_move(old_dir, new_dir, old_name, is_dir,
Al Viro5a190ae2007-06-07 12:19:32 -04003058 new_dentry->d_inode, old_dentry);
Robert Love0eeca282005-07-12 17:06:03 -04003059 fsnotify_oldname_free(old_name);
3060
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 return error;
3062}
3063
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003064SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
3065 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066{
Al Viro2ad94ae2008-07-21 09:32:51 -04003067 struct dentry *old_dir, *new_dir;
3068 struct dentry *old_dentry, *new_dentry;
3069 struct dentry *trap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 struct nameidata oldnd, newnd;
Al Viro2ad94ae2008-07-21 09:32:51 -04003071 char *from;
3072 char *to;
3073 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074
Al Viro2ad94ae2008-07-21 09:32:51 -04003075 error = user_path_parent(olddfd, oldname, &oldnd, &from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 if (error)
3077 goto exit;
3078
Al Viro2ad94ae2008-07-21 09:32:51 -04003079 error = user_path_parent(newdfd, newname, &newnd, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 if (error)
3081 goto exit1;
3082
3083 error = -EXDEV;
Jan Blunck4ac91372008-02-14 19:34:32 -08003084 if (oldnd.path.mnt != newnd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 goto exit2;
3086
Jan Blunck4ac91372008-02-14 19:34:32 -08003087 old_dir = oldnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 error = -EBUSY;
3089 if (oldnd.last_type != LAST_NORM)
3090 goto exit2;
3091
Jan Blunck4ac91372008-02-14 19:34:32 -08003092 new_dir = newnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 if (newnd.last_type != LAST_NORM)
3094 goto exit2;
3095
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003096 oldnd.flags &= ~LOOKUP_PARENT;
3097 newnd.flags &= ~LOOKUP_PARENT;
OGAWA Hirofumi4e9ed2f2008-10-16 07:50:29 +09003098 newnd.flags |= LOOKUP_RENAME_TARGET;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003099
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 trap = lock_rename(new_dir, old_dir);
3101
Christoph Hellwig49705b72005-11-08 21:35:06 -08003102 old_dentry = lookup_hash(&oldnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 error = PTR_ERR(old_dentry);
3104 if (IS_ERR(old_dentry))
3105 goto exit3;
3106 /* source must exist */
3107 error = -ENOENT;
3108 if (!old_dentry->d_inode)
3109 goto exit4;
3110 /* unless the source is a directory trailing slashes give -ENOTDIR */
3111 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
3112 error = -ENOTDIR;
3113 if (oldnd.last.name[oldnd.last.len])
3114 goto exit4;
3115 if (newnd.last.name[newnd.last.len])
3116 goto exit4;
3117 }
3118 /* source should not be ancestor of target */
3119 error = -EINVAL;
3120 if (old_dentry == trap)
3121 goto exit4;
Christoph Hellwig49705b72005-11-08 21:35:06 -08003122 new_dentry = lookup_hash(&newnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 error = PTR_ERR(new_dentry);
3124 if (IS_ERR(new_dentry))
3125 goto exit4;
3126 /* target should not be an ancestor of source */
3127 error = -ENOTEMPTY;
3128 if (new_dentry == trap)
3129 goto exit5;
3130
Dave Hansen9079b1e2008-02-15 14:37:49 -08003131 error = mnt_want_write(oldnd.path.mnt);
3132 if (error)
3133 goto exit5;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003134 error = security_path_rename(&oldnd.path, old_dentry,
3135 &newnd.path, new_dentry);
3136 if (error)
3137 goto exit6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 error = vfs_rename(old_dir->d_inode, old_dentry,
3139 new_dir->d_inode, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003140exit6:
Dave Hansen9079b1e2008-02-15 14:37:49 -08003141 mnt_drop_write(oldnd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142exit5:
3143 dput(new_dentry);
3144exit4:
3145 dput(old_dentry);
3146exit3:
3147 unlock_rename(new_dir, old_dir);
3148exit2:
Jan Blunck1d957f92008-02-14 19:34:35 -08003149 path_put(&newnd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04003150 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003152 path_put(&oldnd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 putname(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04003154exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 return error;
3156}
3157
Heiko Carstensa26eab22009-01-14 14:14:17 +01003158SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003159{
3160 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
3161}
3162
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
3164{
3165 int len;
3166
3167 len = PTR_ERR(link);
3168 if (IS_ERR(link))
3169 goto out;
3170
3171 len = strlen(link);
3172 if (len > (unsigned) buflen)
3173 len = buflen;
3174 if (copy_to_user(buffer, link, len))
3175 len = -EFAULT;
3176out:
3177 return len;
3178}
3179
3180/*
3181 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
3182 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
3183 * using) it for any given inode is up to filesystem.
3184 */
3185int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3186{
3187 struct nameidata nd;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003188 void *cookie;
Marcin Slusarz694a1762008-06-09 16:40:37 -07003189 int res;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003190
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 nd.depth = 0;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003192 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
Marcin Slusarz694a1762008-06-09 16:40:37 -07003193 if (IS_ERR(cookie))
3194 return PTR_ERR(cookie);
3195
3196 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
3197 if (dentry->d_inode->i_op->put_link)
3198 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3199 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200}
3201
3202int vfs_follow_link(struct nameidata *nd, const char *link)
3203{
3204 return __vfs_follow_link(nd, link);
3205}
3206
3207/* get the link contents into pagecache */
3208static char *page_getlink(struct dentry * dentry, struct page **ppage)
3209{
Duane Griffinebd09ab2008-12-19 20:47:12 +00003210 char *kaddr;
3211 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 struct address_space *mapping = dentry->d_inode->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -07003213 page = read_mapping_page(mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 if (IS_ERR(page))
Nick Piggin6fe69002007-05-06 14:49:04 -07003215 return (char*)page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 *ppage = page;
Duane Griffinebd09ab2008-12-19 20:47:12 +00003217 kaddr = kmap(page);
3218 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3219 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220}
3221
3222int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3223{
3224 struct page *page = NULL;
3225 char *s = page_getlink(dentry, &page);
3226 int res = vfs_readlink(dentry,buffer,buflen,s);
3227 if (page) {
3228 kunmap(page);
3229 page_cache_release(page);
3230 }
3231 return res;
3232}
3233
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003234void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003236 struct page *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 nd_set_link(nd, page_getlink(dentry, &page));
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003238 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239}
3240
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003241void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003243 struct page *page = cookie;
3244
3245 if (page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 kunmap(page);
3247 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 }
3249}
3250
Nick Piggin54566b22009-01-04 12:00:53 -08003251/*
3252 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
3253 */
3254int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255{
3256 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003257 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07003258 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08003259 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 char *kaddr;
Nick Piggin54566b22009-01-04 12:00:53 -08003261 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
3262 if (nofs)
3263 flags |= AOP_FLAG_NOFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264
NeilBrown7e53cac2006-03-25 03:07:57 -08003265retry:
Nick Pigginafddba42007-10-16 01:25:01 -07003266 err = pagecache_write_begin(NULL, mapping, 0, len-1,
Nick Piggin54566b22009-01-04 12:00:53 -08003267 flags, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07003269 goto fail;
3270
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 kaddr = kmap_atomic(page, KM_USER0);
3272 memcpy(kaddr, symname, len-1);
3273 kunmap_atomic(kaddr, KM_USER0);
Nick Pigginafddba42007-10-16 01:25:01 -07003274
3275 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3276 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 if (err < 0)
3278 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07003279 if (err < len-1)
3280 goto retry;
3281
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 mark_inode_dirty(inode);
3283 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284fail:
3285 return err;
3286}
3287
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003288int page_symlink(struct inode *inode, const char *symname, int len)
3289{
3290 return __page_symlink(inode, symname, len,
Nick Piggin54566b22009-01-04 12:00:53 -08003291 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003292}
3293
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003294const struct inode_operations page_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 .readlink = generic_readlink,
3296 .follow_link = page_follow_link_light,
3297 .put_link = page_put_link,
3298};
3299
Al Viro2d8f3032008-07-22 09:59:21 -04003300EXPORT_SYMBOL(user_path_at);
David Howellscc53ce52011-01-14 18:45:26 +00003301EXPORT_SYMBOL(follow_down_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302EXPORT_SYMBOL(follow_down);
3303EXPORT_SYMBOL(follow_up);
3304EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
3305EXPORT_SYMBOL(getname);
3306EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307EXPORT_SYMBOL(lookup_one_len);
3308EXPORT_SYMBOL(page_follow_link_light);
3309EXPORT_SYMBOL(page_put_link);
3310EXPORT_SYMBOL(page_readlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003311EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312EXPORT_SYMBOL(page_symlink);
3313EXPORT_SYMBOL(page_symlink_inode_operations);
Al Viroc9c6cac2011-02-16 15:15:47 -05003314EXPORT_SYMBOL(kern_path_parent);
Al Virod1811462008-08-02 00:49:18 -04003315EXPORT_SYMBOL(kern_path);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07003316EXPORT_SYMBOL(vfs_path_lookup);
Al Virof419a2e2008-07-22 00:07:17 -04003317EXPORT_SYMBOL(inode_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318EXPORT_SYMBOL(unlock_rename);
3319EXPORT_SYMBOL(vfs_create);
3320EXPORT_SYMBOL(vfs_follow_link);
3321EXPORT_SYMBOL(vfs_link);
3322EXPORT_SYMBOL(vfs_mkdir);
3323EXPORT_SYMBOL(vfs_mknod);
3324EXPORT_SYMBOL(generic_permission);
3325EXPORT_SYMBOL(vfs_readlink);
3326EXPORT_SYMBOL(vfs_rename);
3327EXPORT_SYMBOL(vfs_rmdir);
3328EXPORT_SYMBOL(vfs_symlink);
3329EXPORT_SYMBOL(vfs_unlink);
3330EXPORT_SYMBOL(dentry_unhash);
3331EXPORT_SYMBOL(generic_readlink);