blob: 5e65f67ee92616257c8819d36533e5dc5906a998 [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 Viro9f1fafe2011-03-25 11:00:12 -0400439/**
440 * complete_walk - successful completion of path walk
441 * @nd: pointer nameidata
Jeff Layton39159de2009-12-07 12:01:50 -0500442 *
Al Viro9f1fafe2011-03-25 11:00:12 -0400443 * If we had been in RCU mode, drop out of it and legitimize nd->path.
444 * Revalidate the final result, unless we'd already done that during
445 * the path walk or the filesystem doesn't ask for it. Return 0 on
446 * success, -error on failure. In case of failure caller does not
447 * need to drop nd->path.
Jeff Layton39159de2009-12-07 12:01:50 -0500448 */
Al Viro9f1fafe2011-03-25 11:00:12 -0400449static int complete_walk(struct nameidata *nd)
Jeff Layton39159de2009-12-07 12:01:50 -0500450{
Al Viro16c2cd72011-02-22 15:50:10 -0500451 struct dentry *dentry = nd->path.dentry;
Jeff Layton39159de2009-12-07 12:01:50 -0500452 int status;
Jeff Layton39159de2009-12-07 12:01:50 -0500453
Al Viro9f1fafe2011-03-25 11:00:12 -0400454 if (nd->flags & LOOKUP_RCU) {
455 nd->flags &= ~LOOKUP_RCU;
456 if (!(nd->flags & LOOKUP_ROOT))
457 nd->root.mnt = NULL;
458 spin_lock(&dentry->d_lock);
459 if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
460 spin_unlock(&dentry->d_lock);
461 rcu_read_unlock();
462 br_read_unlock(vfsmount_lock);
463 return -ECHILD;
464 }
465 BUG_ON(nd->inode != dentry->d_inode);
466 spin_unlock(&dentry->d_lock);
467 mntget(nd->path.mnt);
468 rcu_read_unlock();
469 br_read_unlock(vfsmount_lock);
470 }
471
Al Viro16c2cd72011-02-22 15:50:10 -0500472 if (likely(!(nd->flags & LOOKUP_JUMPED)))
Jeff Layton39159de2009-12-07 12:01:50 -0500473 return 0;
474
Al Viro16c2cd72011-02-22 15:50:10 -0500475 if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
476 return 0;
477
478 if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
479 return 0;
480
481 /* Note: we do not d_invalidate() */
Nick Piggin34286d62011-01-07 17:49:57 +1100482 status = d_revalidate(dentry, nd);
Jeff Layton39159de2009-12-07 12:01:50 -0500483 if (status > 0)
484 return 0;
485
Al Viro16c2cd72011-02-22 15:50:10 -0500486 if (!status)
Jeff Layton39159de2009-12-07 12:01:50 -0500487 status = -ESTALE;
Al Viro16c2cd72011-02-22 15:50:10 -0500488
Al Viro9f1fafe2011-03-25 11:00:12 -0400489 path_put(&nd->path);
Jeff Layton39159de2009-12-07 12:01:50 -0500490 return status;
491}
492
Al Viro2a737872009-04-07 11:49:53 -0400493static __always_inline void set_root(struct nameidata *nd)
494{
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200495 if (!nd->root.mnt)
496 get_fs_root(current->fs, &nd->root);
Al Viro2a737872009-04-07 11:49:53 -0400497}
498
Al Viro6de88d72009-08-09 01:41:57 +0400499static int link_path_walk(const char *, struct nameidata *);
500
Nick Piggin31e6b012011-01-07 17:49:52 +1100501static __always_inline void set_root_rcu(struct nameidata *nd)
502{
503 if (!nd->root.mnt) {
504 struct fs_struct *fs = current->fs;
Nick Pigginc28cc362011-01-07 17:49:53 +1100505 unsigned seq;
506
507 do {
508 seq = read_seqcount_begin(&fs->seq);
509 nd->root = fs->root;
Tim Chenc1530012011-04-15 11:39:29 -0700510 nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
Nick Pigginc28cc362011-01-07 17:49:53 +1100511 } while (read_seqcount_retry(&fs->seq, seq));
Nick Piggin31e6b012011-01-07 17:49:52 +1100512 }
513}
514
Arjan van de Venf1662352006-01-14 13:21:31 -0800515static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
Nick Piggin31e6b012011-01-07 17:49:52 +1100517 int ret;
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (IS_ERR(link))
520 goto fail;
521
522 if (*link == '/') {
Al Viro2a737872009-04-07 11:49:53 -0400523 set_root(nd);
Jan Blunck1d957f92008-02-14 19:34:35 -0800524 path_put(&nd->path);
Al Viro2a737872009-04-07 11:49:53 -0400525 nd->path = nd->root;
526 path_get(&nd->root);
Al Viro16c2cd72011-02-22 15:50:10 -0500527 nd->flags |= LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100529 nd->inode = nd->path.dentry->d_inode;
Christoph Hellwigb4091d52008-11-05 15:07:21 +0100530
Nick Piggin31e6b012011-01-07 17:49:52 +1100531 ret = link_path_walk(link, nd);
532 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533fail:
Jan Blunck1d957f92008-02-14 19:34:35 -0800534 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 return PTR_ERR(link);
536}
537
Jan Blunck1d957f92008-02-14 19:34:35 -0800538static void path_put_conditional(struct path *path, struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700539{
540 dput(path->dentry);
Jan Blunck4ac91372008-02-14 19:34:32 -0800541 if (path->mnt != nd->path.mnt)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700542 mntput(path->mnt);
543}
544
Nick Piggin7b9337a2011-01-14 08:42:43 +0000545static inline void path_to_nameidata(const struct path *path,
546 struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700547{
Nick Piggin31e6b012011-01-07 17:49:52 +1100548 if (!(nd->flags & LOOKUP_RCU)) {
549 dput(nd->path.dentry);
550 if (nd->path.mnt != path->mnt)
551 mntput(nd->path.mnt);
Huang Shijie9a229682010-04-02 17:37:13 +0800552 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100553 nd->path.mnt = path->mnt;
Jan Blunck4ac91372008-02-14 19:34:32 -0800554 nd->path.dentry = path->dentry;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700555}
556
Al Viro574197e2011-03-14 22:20:34 -0400557static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
558{
559 struct inode *inode = link->dentry->d_inode;
560 if (!IS_ERR(cookie) && inode->i_op->put_link)
561 inode->i_op->put_link(link->dentry, nd, cookie);
562 path_put(link);
563}
564
Al Virodef4af32009-12-26 08:37:05 -0500565static __always_inline int
Al Viro574197e2011-03-14 22:20:34 -0400566follow_link(struct path *link, struct nameidata *nd, void **p)
Ian Kent051d3812006-03-27 01:14:53 -0800567{
568 int error;
Nick Piggin7b9337a2011-01-14 08:42:43 +0000569 struct dentry *dentry = link->dentry;
Ian Kent051d3812006-03-27 01:14:53 -0800570
Al Viro844a3912011-02-15 00:38:26 -0500571 BUG_ON(nd->flags & LOOKUP_RCU);
572
Al Viro0e794582011-03-16 02:45:02 -0400573 if (link->mnt == nd->path.mnt)
574 mntget(link->mnt);
575
Al Viro574197e2011-03-14 22:20:34 -0400576 if (unlikely(current->total_link_count >= 40)) {
577 *p = ERR_PTR(-ELOOP); /* no ->put_link(), please */
Al Viro574197e2011-03-14 22:20:34 -0400578 path_put(&nd->path);
579 return -ELOOP;
580 }
581 cond_resched();
582 current->total_link_count++;
583
Nick Piggin7b9337a2011-01-14 08:42:43 +0000584 touch_atime(link->mnt, dentry);
Ian Kent051d3812006-03-27 01:14:53 -0800585 nd_set_link(nd, NULL);
586
Al Viro36f3b4f2011-02-22 21:24:38 -0500587 error = security_inode_follow_link(link->dentry, nd);
588 if (error) {
589 *p = ERR_PTR(error); /* no ->put_link(), please */
590 path_put(&nd->path);
591 return error;
592 }
593
Al Viro86acdca12009-12-22 23:45:11 -0500594 nd->last_type = LAST_BIND;
Al Virodef4af32009-12-26 08:37:05 -0500595 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
596 error = PTR_ERR(*p);
597 if (!IS_ERR(*p)) {
Ian Kent051d3812006-03-27 01:14:53 -0800598 char *s = nd_get_link(nd);
599 error = 0;
600 if (s)
601 error = __vfs_follow_link(nd, s);
Al Virobcda7652011-03-13 16:42:14 -0400602 else if (nd->last_type == LAST_BIND) {
Al Viro16c2cd72011-02-22 15:50:10 -0500603 nd->flags |= LOOKUP_JUMPED;
Al Virob21041d2011-03-14 20:01:51 -0400604 nd->inode = nd->path.dentry->d_inode;
605 if (nd->inode->i_op->follow_link) {
Al Virobcda7652011-03-13 16:42:14 -0400606 /* stepped on a _really_ weird one */
607 path_put(&nd->path);
608 error = -ELOOP;
609 }
610 }
Ian Kent051d3812006-03-27 01:14:53 -0800611 }
Ian Kent051d3812006-03-27 01:14:53 -0800612 return error;
613}
614
Nick Piggin31e6b012011-01-07 17:49:52 +1100615static int follow_up_rcu(struct path *path)
616{
617 struct vfsmount *parent;
618 struct dentry *mountpoint;
619
620 parent = path->mnt->mnt_parent;
621 if (parent == path->mnt)
622 return 0;
623 mountpoint = path->mnt->mnt_mountpoint;
624 path->dentry = mountpoint;
625 path->mnt = parent;
626 return 1;
627}
628
Al Virobab77eb2009-04-18 03:26:48 -0400629int follow_up(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
631 struct vfsmount *parent;
632 struct dentry *mountpoint;
Nick Piggin99b7db72010-08-18 04:37:39 +1000633
634 br_read_lock(vfsmount_lock);
Al Virobab77eb2009-04-18 03:26:48 -0400635 parent = path->mnt->mnt_parent;
636 if (parent == path->mnt) {
Nick Piggin99b7db72010-08-18 04:37:39 +1000637 br_read_unlock(vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return 0;
639 }
640 mntget(parent);
Al Virobab77eb2009-04-18 03:26:48 -0400641 mountpoint = dget(path->mnt->mnt_mountpoint);
Nick Piggin99b7db72010-08-18 04:37:39 +1000642 br_read_unlock(vfsmount_lock);
Al Virobab77eb2009-04-18 03:26:48 -0400643 dput(path->dentry);
644 path->dentry = mountpoint;
645 mntput(path->mnt);
646 path->mnt = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 return 1;
648}
649
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100650/*
David Howells9875cf82011-01-14 18:45:21 +0000651 * Perform an automount
652 * - return -EISDIR to tell follow_managed() to stop and return the path we
653 * were called with.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 */
David Howells9875cf82011-01-14 18:45:21 +0000655static int follow_automount(struct path *path, unsigned flags,
656 bool *need_mntput)
Nick Piggin31e6b012011-01-07 17:49:52 +1100657{
David Howells9875cf82011-01-14 18:45:21 +0000658 struct vfsmount *mnt;
David Howellsea5b7782011-01-14 19:10:03 +0000659 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +1100660
David Howells9875cf82011-01-14 18:45:21 +0000661 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
662 return -EREMOTE;
Al Viro463ffb22005-06-06 13:36:05 -0700663
David Howells6f45b652011-01-14 18:45:31 +0000664 /* We don't want to mount if someone supplied AT_NO_AUTOMOUNT
665 * and this is the terminal part of the path.
666 */
667 if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_CONTINUE))
668 return -EISDIR; /* we actually want to stop here */
669
David Howells9875cf82011-01-14 18:45:21 +0000670 /* We want to mount if someone is trying to open/create a file of any
671 * type under the mountpoint, wants to traverse through the mountpoint
672 * or wants to open the mounted directory.
673 *
674 * We don't want to mount if someone's just doing a stat and they've
675 * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and
676 * appended a '/' to the name.
677 */
678 if (!(flags & LOOKUP_FOLLOW) &&
679 !(flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY |
680 LOOKUP_OPEN | LOOKUP_CREATE)))
681 return -EISDIR;
682
683 current->total_link_count++;
684 if (current->total_link_count >= 40)
685 return -ELOOP;
686
687 mnt = path->dentry->d_op->d_automount(path);
688 if (IS_ERR(mnt)) {
689 /*
690 * The filesystem is allowed to return -EISDIR here to indicate
691 * it doesn't want to automount. For instance, autofs would do
692 * this so that its userspace daemon can mount on this dentry.
693 *
694 * However, we can only permit this if it's a terminal point in
695 * the path being looked up; if it wasn't then the remainder of
696 * the path is inaccessible and we should say so.
697 */
698 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_CONTINUE))
699 return -EREMOTE;
700 return PTR_ERR(mnt);
701 }
David Howellsea5b7782011-01-14 19:10:03 +0000702
David Howells9875cf82011-01-14 18:45:21 +0000703 if (!mnt) /* mount collision */
704 return 0;
705
Al Viro8aef1882011-06-16 15:10:06 +0100706 if (!*need_mntput) {
707 /* lock_mount() may release path->mnt on error */
708 mntget(path->mnt);
709 *need_mntput = true;
710 }
Al Viro19a167a2011-01-17 01:35:23 -0500711 err = finish_automount(mnt, path);
David Howellsea5b7782011-01-14 19:10:03 +0000712
David Howellsea5b7782011-01-14 19:10:03 +0000713 switch (err) {
714 case -EBUSY:
715 /* Someone else made a mount here whilst we were busy */
Al Viro19a167a2011-01-17 01:35:23 -0500716 return 0;
David Howellsea5b7782011-01-14 19:10:03 +0000717 case 0:
Al Viro8aef1882011-06-16 15:10:06 +0100718 path_put(path);
David Howellsea5b7782011-01-14 19:10:03 +0000719 path->mnt = mnt;
720 path->dentry = dget(mnt->mnt_root);
David Howellsea5b7782011-01-14 19:10:03 +0000721 return 0;
Al Viro19a167a2011-01-17 01:35:23 -0500722 default:
723 return err;
David Howellsea5b7782011-01-14 19:10:03 +0000724 }
Al Viro19a167a2011-01-17 01:35:23 -0500725
David Howells9875cf82011-01-14 18:45:21 +0000726}
727
728/*
729 * Handle a dentry that is managed in some way.
David Howellscc53ce52011-01-14 18:45:26 +0000730 * - Flagged for transit management (autofs)
David Howells9875cf82011-01-14 18:45:21 +0000731 * - Flagged as mountpoint
732 * - Flagged as automount point
733 *
734 * This may only be called in refwalk mode.
735 *
736 * Serialization is taken care of in namespace.c
737 */
738static int follow_managed(struct path *path, unsigned flags)
739{
Al Viro8aef1882011-06-16 15:10:06 +0100740 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
David Howells9875cf82011-01-14 18:45:21 +0000741 unsigned managed;
742 bool need_mntput = false;
Al Viro8aef1882011-06-16 15:10:06 +0100743 int ret = 0;
David Howells9875cf82011-01-14 18:45:21 +0000744
745 /* Given that we're not holding a lock here, we retain the value in a
746 * local variable for each dentry as we look at it so that we don't see
747 * the components of that value change under us */
748 while (managed = ACCESS_ONCE(path->dentry->d_flags),
749 managed &= DCACHE_MANAGED_DENTRY,
750 unlikely(managed != 0)) {
David Howellscc53ce52011-01-14 18:45:26 +0000751 /* Allow the filesystem to manage the transit without i_mutex
752 * being held. */
753 if (managed & DCACHE_MANAGE_TRANSIT) {
754 BUG_ON(!path->dentry->d_op);
755 BUG_ON(!path->dentry->d_op->d_manage);
Al Viro1aed3e42011-03-18 09:09:02 -0400756 ret = path->dentry->d_op->d_manage(path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +0000757 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +0100758 break;
David Howellscc53ce52011-01-14 18:45:26 +0000759 }
760
David Howells9875cf82011-01-14 18:45:21 +0000761 /* Transit to a mounted filesystem. */
762 if (managed & DCACHE_MOUNTED) {
763 struct vfsmount *mounted = lookup_mnt(path);
764 if (mounted) {
765 dput(path->dentry);
766 if (need_mntput)
767 mntput(path->mnt);
768 path->mnt = mounted;
769 path->dentry = dget(mounted->mnt_root);
770 need_mntput = true;
771 continue;
772 }
773
774 /* Something is mounted on this dentry in another
775 * namespace and/or whatever was mounted there in this
776 * namespace got unmounted before we managed to get the
777 * vfsmount_lock */
778 }
779
780 /* Handle an automount point */
781 if (managed & DCACHE_NEED_AUTOMOUNT) {
782 ret = follow_automount(path, flags, &need_mntput);
783 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +0100784 break;
David Howells9875cf82011-01-14 18:45:21 +0000785 continue;
786 }
787
788 /* We didn't change the current path point */
789 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
Al Viro8aef1882011-06-16 15:10:06 +0100791
792 if (need_mntput && path->mnt == mnt)
793 mntput(path->mnt);
794 if (ret == -EISDIR)
795 ret = 0;
796 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797}
798
David Howellscc53ce52011-01-14 18:45:26 +0000799int follow_down_one(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
801 struct vfsmount *mounted;
802
Al Viro1c755af2009-04-18 14:06:57 -0400803 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -0400805 dput(path->dentry);
806 mntput(path->mnt);
807 path->mnt = mounted;
808 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 return 1;
810 }
811 return 0;
812}
813
Ian Kent62a73752011-03-25 01:51:02 +0800814static inline bool managed_dentry_might_block(struct dentry *dentry)
815{
816 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
817 dentry->d_op->d_manage(dentry, true) < 0);
818}
819
David Howells9875cf82011-01-14 18:45:21 +0000820/*
Al Viro287548e2011-05-27 06:50:06 -0400821 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
822 * we meet a managed dentry that would need blocking.
David Howells9875cf82011-01-14 18:45:21 +0000823 */
824static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
Al Viro287548e2011-05-27 06:50:06 -0400825 struct inode **inode)
David Howells9875cf82011-01-14 18:45:21 +0000826{
Ian Kent62a73752011-03-25 01:51:02 +0800827 for (;;) {
David Howells9875cf82011-01-14 18:45:21 +0000828 struct vfsmount *mounted;
Ian Kent62a73752011-03-25 01:51:02 +0800829 /*
830 * Don't forget we might have a non-mountpoint managed dentry
831 * that wants to block transit.
832 */
Al Viro287548e2011-05-27 06:50:06 -0400833 if (unlikely(managed_dentry_might_block(path->dentry)))
David Howellsab909112011-01-14 18:46:51 +0000834 return false;
Ian Kent62a73752011-03-25 01:51:02 +0800835
836 if (!d_mountpoint(path->dentry))
837 break;
838
David Howells9875cf82011-01-14 18:45:21 +0000839 mounted = __lookup_mnt(path->mnt, path->dentry, 1);
840 if (!mounted)
841 break;
842 path->mnt = mounted;
843 path->dentry = mounted->mnt_root;
844 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
Linus Torvalds59430262011-07-18 15:43:29 -0700845 /*
846 * Update the inode too. We don't need to re-check the
847 * dentry sequence number here after this d_inode read,
848 * because a mount-point is always pinned.
849 */
850 *inode = path->dentry->d_inode;
David Howells9875cf82011-01-14 18:45:21 +0000851 }
David Howells9875cf82011-01-14 18:45:21 +0000852 return true;
853}
854
Al Virodea39372011-05-27 06:53:39 -0400855static void follow_mount_rcu(struct nameidata *nd)
Al Viro287548e2011-05-27 06:50:06 -0400856{
Al Virodea39372011-05-27 06:53:39 -0400857 while (d_mountpoint(nd->path.dentry)) {
Al Viro287548e2011-05-27 06:50:06 -0400858 struct vfsmount *mounted;
Al Virodea39372011-05-27 06:53:39 -0400859 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
Al Viro287548e2011-05-27 06:50:06 -0400860 if (!mounted)
861 break;
Al Virodea39372011-05-27 06:53:39 -0400862 nd->path.mnt = mounted;
863 nd->path.dentry = mounted->mnt_root;
864 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro287548e2011-05-27 06:50:06 -0400865 }
866}
867
Nick Piggin31e6b012011-01-07 17:49:52 +1100868static int follow_dotdot_rcu(struct nameidata *nd)
869{
Nick Piggin31e6b012011-01-07 17:49:52 +1100870 set_root_rcu(nd);
871
David Howells9875cf82011-01-14 18:45:21 +0000872 while (1) {
Nick Piggin31e6b012011-01-07 17:49:52 +1100873 if (nd->path.dentry == nd->root.dentry &&
874 nd->path.mnt == nd->root.mnt) {
875 break;
876 }
877 if (nd->path.dentry != nd->path.mnt->mnt_root) {
878 struct dentry *old = nd->path.dentry;
879 struct dentry *parent = old->d_parent;
880 unsigned seq;
881
882 seq = read_seqcount_begin(&parent->d_seq);
883 if (read_seqcount_retry(&old->d_seq, nd->seq))
Al Viroef7562d2011-03-04 14:35:59 -0500884 goto failed;
Nick Piggin31e6b012011-01-07 17:49:52 +1100885 nd->path.dentry = parent;
886 nd->seq = seq;
887 break;
888 }
889 if (!follow_up_rcu(&nd->path))
890 break;
891 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Nick Piggin31e6b012011-01-07 17:49:52 +1100892 }
Al Virodea39372011-05-27 06:53:39 -0400893 follow_mount_rcu(nd);
894 nd->inode = nd->path.dentry->d_inode;
Nick Piggin31e6b012011-01-07 17:49:52 +1100895 return 0;
Al Viroef7562d2011-03-04 14:35:59 -0500896
897failed:
898 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -0500899 if (!(nd->flags & LOOKUP_ROOT))
900 nd->root.mnt = NULL;
Al Viroef7562d2011-03-04 14:35:59 -0500901 rcu_read_unlock();
902 br_read_unlock(vfsmount_lock);
903 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +1100904}
905
David Howells9875cf82011-01-14 18:45:21 +0000906/*
David Howellscc53ce52011-01-14 18:45:26 +0000907 * Follow down to the covering mount currently visible to userspace. At each
908 * point, the filesystem owning that dentry may be queried as to whether the
909 * caller is permitted to proceed or not.
David Howellscc53ce52011-01-14 18:45:26 +0000910 */
Al Viro7cc90cc2011-03-18 09:04:20 -0400911int follow_down(struct path *path)
David Howellscc53ce52011-01-14 18:45:26 +0000912{
913 unsigned managed;
914 int ret;
915
916 while (managed = ACCESS_ONCE(path->dentry->d_flags),
917 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
918 /* Allow the filesystem to manage the transit without i_mutex
919 * being held.
920 *
921 * We indicate to the filesystem if someone is trying to mount
922 * something here. This gives autofs the chance to deny anyone
923 * other than its daemon the right to mount on its
924 * superstructure.
925 *
926 * The filesystem may sleep at this point.
927 */
928 if (managed & DCACHE_MANAGE_TRANSIT) {
929 BUG_ON(!path->dentry->d_op);
930 BUG_ON(!path->dentry->d_op->d_manage);
David Howellsab909112011-01-14 18:46:51 +0000931 ret = path->dentry->d_op->d_manage(
Al Viro1aed3e42011-03-18 09:09:02 -0400932 path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +0000933 if (ret < 0)
934 return ret == -EISDIR ? 0 : ret;
935 }
936
937 /* Transit to a mounted filesystem. */
938 if (managed & DCACHE_MOUNTED) {
939 struct vfsmount *mounted = lookup_mnt(path);
940 if (!mounted)
941 break;
942 dput(path->dentry);
943 mntput(path->mnt);
944 path->mnt = mounted;
945 path->dentry = dget(mounted->mnt_root);
946 continue;
947 }
948
949 /* Don't handle automount points here */
950 break;
951 }
952 return 0;
953}
954
955/*
David Howells9875cf82011-01-14 18:45:21 +0000956 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
957 */
958static void follow_mount(struct path *path)
959{
960 while (d_mountpoint(path->dentry)) {
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 }
969}
970
Nick Piggin31e6b012011-01-07 17:49:52 +1100971static void follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
Al Viro2a737872009-04-07 11:49:53 -0400973 set_root(nd);
Andreas Mohre518ddb2006-09-29 02:01:22 -0700974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 while(1) {
Jan Blunck4ac91372008-02-14 19:34:32 -0800976 struct dentry *old = nd->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Al Viro2a737872009-04-07 11:49:53 -0400978 if (nd->path.dentry == nd->root.dentry &&
979 nd->path.mnt == nd->root.mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 break;
981 }
Jan Blunck4ac91372008-02-14 19:34:32 -0800982 if (nd->path.dentry != nd->path.mnt->mnt_root) {
Al Viro3088dd72010-01-30 15:47:29 -0500983 /* rare case of legitimate dget_parent()... */
984 nd->path.dentry = dget_parent(nd->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 dput(old);
986 break;
987 }
Al Viro3088dd72010-01-30 15:47:29 -0500988 if (!follow_up(&nd->path))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
Al Viro79ed0222009-04-18 13:59:41 -0400991 follow_mount(&nd->path);
Nick Piggin31e6b012011-01-07 17:49:52 +1100992 nd->inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993}
994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995/*
Nick Pigginbaa03892010-08-18 04:37:31 +1000996 * Allocate a dentry with name and parent, and perform a parent
997 * directory ->lookup on it. Returns the new dentry, or ERR_PTR
998 * on error. parent->d_inode->i_mutex must be held. d_lookup must
999 * have verified that no child exists while under i_mutex.
1000 */
1001static struct dentry *d_alloc_and_lookup(struct dentry *parent,
1002 struct qstr *name, struct nameidata *nd)
1003{
1004 struct inode *inode = parent->d_inode;
1005 struct dentry *dentry;
1006 struct dentry *old;
1007
1008 /* Don't create child dentry for a dead directory. */
1009 if (unlikely(IS_DEADDIR(inode)))
1010 return ERR_PTR(-ENOENT);
1011
1012 dentry = d_alloc(parent, name);
1013 if (unlikely(!dentry))
1014 return ERR_PTR(-ENOMEM);
1015
1016 old = inode->i_op->lookup(inode, dentry, nd);
1017 if (unlikely(old)) {
1018 dput(dentry);
1019 dentry = old;
1020 }
1021 return dentry;
1022}
1023
1024/*
Josef Bacik44396f42011-05-31 11:58:49 -04001025 * We already have a dentry, but require a lookup to be performed on the parent
1026 * directory to fill in d_inode. Returns the new dentry, or ERR_PTR on error.
1027 * parent->d_inode->i_mutex must be held. d_lookup must have verified that no
1028 * child exists while under i_mutex.
1029 */
1030static struct dentry *d_inode_lookup(struct dentry *parent, struct dentry *dentry,
1031 struct nameidata *nd)
1032{
1033 struct inode *inode = parent->d_inode;
1034 struct dentry *old;
1035
1036 /* Don't create child dentry for a dead directory. */
1037 if (unlikely(IS_DEADDIR(inode)))
1038 return ERR_PTR(-ENOENT);
1039
1040 old = inode->i_op->lookup(inode, dentry, nd);
1041 if (unlikely(old)) {
1042 dput(dentry);
1043 dentry = old;
1044 }
1045 return dentry;
1046}
1047
1048/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 * It's more convoluted than I'd like it to be, but... it's still fairly
1050 * small and for now I'd prefer to have fast path as straight as possible.
1051 * It _is_ time-critical.
1052 */
1053static int do_lookup(struct nameidata *nd, struct qstr *name,
Nick Piggin31e6b012011-01-07 17:49:52 +11001054 struct path *path, struct inode **inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055{
Jan Blunck4ac91372008-02-14 19:34:32 -08001056 struct vfsmount *mnt = nd->path.mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +11001057 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro5a18fff2011-03-11 04:44:53 -05001058 int need_reval = 1;
1059 int status = 1;
David Howells9875cf82011-01-14 18:45:21 +00001060 int err;
1061
Al Viro3cac2602009-08-13 18:27:43 +04001062 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001063 * Rename seqlock is not required here because in the off chance
1064 * of a false negative due to a concurrent rename, we're going to
1065 * do the non-racy lookup, below.
1066 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001067 if (nd->flags & LOOKUP_RCU) {
1068 unsigned seq;
Nick Piggin31e6b012011-01-07 17:49:52 +11001069 *inode = nd->inode;
1070 dentry = __d_lookup_rcu(parent, name, &seq, inode);
Al Viro5a18fff2011-03-11 04:44:53 -05001071 if (!dentry)
1072 goto unlazy;
1073
Nick Piggin31e6b012011-01-07 17:49:52 +11001074 /* Memory barrier in read_seqcount_begin of child is enough */
1075 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1076 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001077 nd->seq = seq;
Al Viro5a18fff2011-03-11 04:44:53 -05001078
Al Viro24643082011-02-15 01:26:22 -05001079 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
Al Viro5a18fff2011-03-11 04:44:53 -05001080 status = d_revalidate(dentry, nd);
1081 if (unlikely(status <= 0)) {
1082 if (status != -ECHILD)
1083 need_reval = 0;
1084 goto unlazy;
1085 }
Al Viro24643082011-02-15 01:26:22 -05001086 }
Josef Bacik44396f42011-05-31 11:58:49 -04001087 if (unlikely(d_need_lookup(dentry)))
1088 goto unlazy;
Nick Piggin31e6b012011-01-07 17:49:52 +11001089 path->mnt = mnt;
1090 path->dentry = dentry;
Al Virod6e9bd22011-05-27 07:03:15 -04001091 if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1092 goto unlazy;
1093 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1094 goto unlazy;
1095 return 0;
Al Viro5a18fff2011-03-11 04:44:53 -05001096unlazy:
Al Viro19660af2011-03-25 10:32:48 -04001097 if (unlazy_walk(nd, dentry))
1098 return -ECHILD;
Al Viro5a18fff2011-03-11 04:44:53 -05001099 } else {
1100 dentry = __d_lookup(parent, name);
Nick Piggin31e6b012011-01-07 17:49:52 +11001101 }
Al Viro5a18fff2011-03-11 04:44:53 -05001102
Josef Bacik44396f42011-05-31 11:58:49 -04001103 if (dentry && unlikely(d_need_lookup(dentry))) {
1104 dput(dentry);
1105 dentry = NULL;
1106 }
Al Viro5a18fff2011-03-11 04:44:53 -05001107retry:
1108 if (unlikely(!dentry)) {
1109 struct inode *dir = parent->d_inode;
1110 BUG_ON(nd->inode != dir);
1111
1112 mutex_lock(&dir->i_mutex);
1113 dentry = d_lookup(parent, name);
1114 if (likely(!dentry)) {
1115 dentry = d_alloc_and_lookup(parent, name, nd);
1116 if (IS_ERR(dentry)) {
1117 mutex_unlock(&dir->i_mutex);
1118 return PTR_ERR(dentry);
1119 }
1120 /* known good */
1121 need_reval = 0;
1122 status = 1;
Josef Bacik44396f42011-05-31 11:58:49 -04001123 } else if (unlikely(d_need_lookup(dentry))) {
1124 dentry = d_inode_lookup(parent, dentry, nd);
1125 if (IS_ERR(dentry)) {
1126 mutex_unlock(&dir->i_mutex);
1127 return PTR_ERR(dentry);
1128 }
1129 /* known good */
1130 need_reval = 0;
1131 status = 1;
Al Viro5a18fff2011-03-11 04:44:53 -05001132 }
1133 mutex_unlock(&dir->i_mutex);
Al Viro24643082011-02-15 01:26:22 -05001134 }
Al Viro5a18fff2011-03-11 04:44:53 -05001135 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
1136 status = d_revalidate(dentry, nd);
1137 if (unlikely(status <= 0)) {
1138 if (status < 0) {
1139 dput(dentry);
1140 return status;
1141 }
1142 if (!d_invalidate(dentry)) {
1143 dput(dentry);
1144 dentry = NULL;
1145 need_reval = 1;
1146 goto retry;
1147 }
1148 }
1149
David Howells9875cf82011-01-14 18:45:21 +00001150 path->mnt = mnt;
1151 path->dentry = dentry;
1152 err = follow_managed(path, nd->flags);
Ian Kent89312212011-01-18 12:06:10 +08001153 if (unlikely(err < 0)) {
1154 path_put_conditional(path, nd);
David Howells9875cf82011-01-14 18:45:21 +00001155 return err;
Ian Kent89312212011-01-18 12:06:10 +08001156 }
David Howells9875cf82011-01-14 18:45:21 +00001157 *inode = path->dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159}
1160
Al Viro52094c82011-02-21 21:34:47 -05001161static inline int may_lookup(struct nameidata *nd)
1162{
1163 if (nd->flags & LOOKUP_RCU) {
Al Viro4ad5abb2011-06-20 19:57:03 -04001164 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
Al Viro52094c82011-02-21 21:34:47 -05001165 if (err != -ECHILD)
1166 return err;
Al Viro19660af2011-03-25 10:32:48 -04001167 if (unlazy_walk(nd, NULL))
Al Viro52094c82011-02-21 21:34:47 -05001168 return -ECHILD;
1169 }
Al Viro4ad5abb2011-06-20 19:57:03 -04001170 return inode_permission(nd->inode, MAY_EXEC);
Al Viro52094c82011-02-21 21:34:47 -05001171}
1172
Al Viro9856fa12011-03-04 14:22:06 -05001173static inline int handle_dots(struct nameidata *nd, int type)
1174{
1175 if (type == LAST_DOTDOT) {
1176 if (nd->flags & LOOKUP_RCU) {
1177 if (follow_dotdot_rcu(nd))
1178 return -ECHILD;
1179 } else
1180 follow_dotdot(nd);
1181 }
1182 return 0;
1183}
1184
Al Viro951361f2011-03-04 14:44:37 -05001185static void terminate_walk(struct nameidata *nd)
1186{
1187 if (!(nd->flags & LOOKUP_RCU)) {
1188 path_put(&nd->path);
1189 } else {
1190 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001191 if (!(nd->flags & LOOKUP_ROOT))
1192 nd->root.mnt = NULL;
Al Viro951361f2011-03-04 14:44:37 -05001193 rcu_read_unlock();
1194 br_read_unlock(vfsmount_lock);
1195 }
1196}
1197
Al Viroce57dfc2011-03-13 19:58:58 -04001198static inline int walk_component(struct nameidata *nd, struct path *path,
1199 struct qstr *name, int type, int follow)
1200{
1201 struct inode *inode;
1202 int err;
1203 /*
1204 * "." and ".." are special - ".." especially so because it has
1205 * to be able to know about the current root directory and
1206 * parent relationships.
1207 */
1208 if (unlikely(type != LAST_NORM))
1209 return handle_dots(nd, type);
1210 err = do_lookup(nd, name, path, &inode);
1211 if (unlikely(err)) {
1212 terminate_walk(nd);
1213 return err;
1214 }
1215 if (!inode) {
1216 path_to_nameidata(path, nd);
1217 terminate_walk(nd);
1218 return -ENOENT;
1219 }
1220 if (unlikely(inode->i_op->follow_link) && follow) {
Al Viro19660af2011-03-25 10:32:48 -04001221 if (nd->flags & LOOKUP_RCU) {
1222 if (unlikely(unlazy_walk(nd, path->dentry))) {
1223 terminate_walk(nd);
1224 return -ECHILD;
1225 }
1226 }
Al Viroce57dfc2011-03-13 19:58:58 -04001227 BUG_ON(inode != path->dentry->d_inode);
1228 return 1;
1229 }
1230 path_to_nameidata(path, nd);
1231 nd->inode = inode;
1232 return 0;
1233}
1234
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235/*
Al Virob3563792011-03-14 21:54:55 -04001236 * This limits recursive symlink follows to 8, while
1237 * limiting consecutive symlinks to 40.
1238 *
1239 * Without that kind of total limit, nasty chains of consecutive
1240 * symlinks can cause almost arbitrarily long lookups.
1241 */
1242static inline int nested_symlink(struct path *path, struct nameidata *nd)
1243{
1244 int res;
1245
Al Virob3563792011-03-14 21:54:55 -04001246 if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1247 path_put_conditional(path, nd);
1248 path_put(&nd->path);
1249 return -ELOOP;
1250 }
Erez Zadok1a4022f2011-05-21 01:19:59 -04001251 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
Al Virob3563792011-03-14 21:54:55 -04001252
1253 nd->depth++;
1254 current->link_count++;
1255
1256 do {
1257 struct path link = *path;
1258 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04001259
1260 res = follow_link(&link, nd, &cookie);
Al Virob3563792011-03-14 21:54:55 -04001261 if (!res)
1262 res = walk_component(nd, path, &nd->last,
1263 nd->last_type, LOOKUP_FOLLOW);
Al Viro574197e2011-03-14 22:20:34 -04001264 put_link(nd, &link, cookie);
Al Virob3563792011-03-14 21:54:55 -04001265 } while (res > 0);
1266
1267 current->link_count--;
1268 nd->depth--;
1269 return res;
1270}
1271
1272/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01001274 * This is the basic name resolution function, turning a pathname into
1275 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01001277 * Returns 0 and nd will have valid dentry and mnt on success.
1278 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 */
Al Viro6de88d72009-08-09 01:41:57 +04001280static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
1282 struct path next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 int err;
1284 unsigned int lookup_flags = nd->flags;
1285
1286 while (*name=='/')
1287 name++;
1288 if (!*name)
Al Viro086e1832011-02-22 20:56:27 -05001289 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 /* At this point we know we have a real path component. */
1292 for(;;) {
1293 unsigned long hash;
1294 struct qstr this;
1295 unsigned int c;
Al Virofe479a52011-02-22 15:10:03 -05001296 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Trond Myklebustcdce5d62005-10-18 14:20:18 -07001298 nd->flags |= LOOKUP_CONTINUE;
Al Viro52094c82011-02-21 21:34:47 -05001299
1300 err = may_lookup(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 if (err)
1302 break;
1303
1304 this.name = name;
1305 c = *(const unsigned char *)name;
1306
1307 hash = init_name_hash();
1308 do {
1309 name++;
1310 hash = partial_name_hash(c, hash);
1311 c = *(const unsigned char *)name;
1312 } while (c && (c != '/'));
1313 this.len = name - (const char *) this.name;
1314 this.hash = end_name_hash(hash);
1315
Al Virofe479a52011-02-22 15:10:03 -05001316 type = LAST_NORM;
1317 if (this.name[0] == '.') switch (this.len) {
1318 case 2:
Al Viro16c2cd72011-02-22 15:50:10 -05001319 if (this.name[1] == '.') {
Al Virofe479a52011-02-22 15:10:03 -05001320 type = LAST_DOTDOT;
Al Viro16c2cd72011-02-22 15:50:10 -05001321 nd->flags |= LOOKUP_JUMPED;
1322 }
Al Virofe479a52011-02-22 15:10:03 -05001323 break;
1324 case 1:
1325 type = LAST_DOT;
1326 }
Al Viro5a202bc2011-03-08 14:17:44 -05001327 if (likely(type == LAST_NORM)) {
1328 struct dentry *parent = nd->path.dentry;
Al Viro16c2cd72011-02-22 15:50:10 -05001329 nd->flags &= ~LOOKUP_JUMPED;
Al Viro5a202bc2011-03-08 14:17:44 -05001330 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1331 err = parent->d_op->d_hash(parent, nd->inode,
1332 &this);
1333 if (err < 0)
1334 break;
1335 }
1336 }
Al Virofe479a52011-02-22 15:10:03 -05001337
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 /* remove trailing slashes? */
1339 if (!c)
1340 goto last_component;
1341 while (*++name == '/');
1342 if (!*name)
Al Virob3563792011-03-14 21:54:55 -04001343 goto last_component;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
Al Viroce57dfc2011-03-13 19:58:58 -04001345 err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1346 if (err < 0)
1347 return err;
Al Virofe479a52011-02-22 15:10:03 -05001348
Al Viroce57dfc2011-03-13 19:58:58 -04001349 if (err) {
Al Virob3563792011-03-14 21:54:55 -04001350 err = nested_symlink(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 if (err)
Al Viroa7472ba2011-03-04 14:39:30 -05001352 return err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 err = -ENOTDIR;
Nick Piggin31e6b012011-01-07 17:49:52 +11001355 if (!nd->inode->i_op->lookup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 break;
1357 continue;
1358 /* here ends the main loop */
1359
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360last_component:
Trond Myklebustf55eab82006-02-04 23:28:01 -08001361 /* Clear LOOKUP_CONTINUE iff it was previously unset */
1362 nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
Al Virob3563792011-03-14 21:54:55 -04001363 nd->last = this;
1364 nd->last_type = type;
Al Viro086e1832011-02-22 20:56:27 -05001365 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 }
Al Viro951361f2011-03-04 14:44:37 -05001367 terminate_walk(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 return err;
1369}
1370
Al Viro70e9b352011-03-05 21:12:22 -05001371static int path_init(int dfd, const char *name, unsigned int flags,
1372 struct nameidata *nd, struct file **fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
Prasanna Medaea3834d2005-04-29 16:00:17 +01001374 int retval = 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001375 int fput_needed;
1376 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
1378 nd->last_type = LAST_ROOT; /* if there are only slashes... */
Al Viro16c2cd72011-02-22 15:50:10 -05001379 nd->flags = flags | LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 nd->depth = 0;
Al Viro5b6ca022011-03-09 23:04:47 -05001381 if (flags & LOOKUP_ROOT) {
1382 struct inode *inode = nd->root.dentry->d_inode;
Al Viro73d049a2011-03-11 12:08:24 -05001383 if (*name) {
1384 if (!inode->i_op->lookup)
1385 return -ENOTDIR;
1386 retval = inode_permission(inode, MAY_EXEC);
1387 if (retval)
1388 return retval;
1389 }
Al Viro5b6ca022011-03-09 23:04:47 -05001390 nd->path = nd->root;
1391 nd->inode = inode;
1392 if (flags & LOOKUP_RCU) {
1393 br_read_lock(vfsmount_lock);
1394 rcu_read_lock();
1395 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1396 } else {
1397 path_get(&nd->path);
1398 }
1399 return 0;
1400 }
1401
Al Viro2a737872009-04-07 11:49:53 -04001402 nd->root.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 if (*name=='/') {
Al Viroe41f7d42011-02-22 14:02:58 -05001405 if (flags & LOOKUP_RCU) {
1406 br_read_lock(vfsmount_lock);
1407 rcu_read_lock();
1408 set_root_rcu(nd);
1409 } else {
1410 set_root(nd);
1411 path_get(&nd->root);
1412 }
Al Viro2a737872009-04-07 11:49:53 -04001413 nd->path = nd->root;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001414 } else if (dfd == AT_FDCWD) {
Al Viroe41f7d42011-02-22 14:02:58 -05001415 if (flags & LOOKUP_RCU) {
1416 struct fs_struct *fs = current->fs;
1417 unsigned seq;
1418
1419 br_read_lock(vfsmount_lock);
1420 rcu_read_lock();
1421
1422 do {
1423 seq = read_seqcount_begin(&fs->seq);
1424 nd->path = fs->pwd;
1425 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1426 } while (read_seqcount_retry(&fs->seq, seq));
1427 } else {
1428 get_fs_pwd(current->fs, &nd->path);
1429 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001430 } else {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001431 struct dentry *dentry;
1432
Al Viro1abf0c72011-03-13 03:51:11 -04001433 file = fget_raw_light(dfd, &fput_needed);
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001434 retval = -EBADF;
1435 if (!file)
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001436 goto out_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001437
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001438 dentry = file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001439
Al Virof52e0c12011-03-14 18:56:51 -04001440 if (*name) {
1441 retval = -ENOTDIR;
1442 if (!S_ISDIR(dentry->d_inode->i_mode))
1443 goto fput_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001444
Al Viro4ad5abb2011-06-20 19:57:03 -04001445 retval = inode_permission(dentry->d_inode, MAY_EXEC);
Al Virof52e0c12011-03-14 18:56:51 -04001446 if (retval)
1447 goto fput_fail;
1448 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001449
Jan Blunck5dd784d2008-02-14 19:34:38 -08001450 nd->path = file->f_path;
Al Viroe41f7d42011-02-22 14:02:58 -05001451 if (flags & LOOKUP_RCU) {
1452 if (fput_needed)
Al Viro70e9b352011-03-05 21:12:22 -05001453 *fp = file;
Al Viroe41f7d42011-02-22 14:02:58 -05001454 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1455 br_read_lock(vfsmount_lock);
1456 rcu_read_lock();
1457 } else {
1458 path_get(&file->f_path);
1459 fput_light(file, fput_needed);
1460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 }
Al Viroe41f7d42011-02-22 14:02:58 -05001462
Nick Piggin31e6b012011-01-07 17:49:52 +11001463 nd->inode = nd->path.dentry->d_inode;
Al Viro9b4a9b12009-04-07 11:44:16 -04001464 return 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001465
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001466fput_fail:
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001467 fput_light(file, fput_needed);
Al Viro9b4a9b12009-04-07 11:44:16 -04001468out_fail:
1469 return retval;
1470}
1471
Al Virobd92d7f2011-03-14 19:54:59 -04001472static inline int lookup_last(struct nameidata *nd, struct path *path)
1473{
1474 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1475 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1476
1477 nd->flags &= ~LOOKUP_PARENT;
1478 return walk_component(nd, path, &nd->last, nd->last_type,
1479 nd->flags & LOOKUP_FOLLOW);
1480}
1481
Al Viro9b4a9b12009-04-07 11:44:16 -04001482/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroee0827c2011-02-21 23:38:09 -05001483static int path_lookupat(int dfd, const char *name,
Al Viro9b4a9b12009-04-07 11:44:16 -04001484 unsigned int flags, struct nameidata *nd)
1485{
Al Viro70e9b352011-03-05 21:12:22 -05001486 struct file *base = NULL;
Al Virobd92d7f2011-03-14 19:54:59 -04001487 struct path path;
1488 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001489
1490 /*
1491 * Path walking is largely split up into 2 different synchronisation
1492 * schemes, rcu-walk and ref-walk (explained in
1493 * Documentation/filesystems/path-lookup.txt). These share much of the
1494 * path walk code, but some things particularly setup, cleanup, and
1495 * following mounts are sufficiently divergent that functions are
1496 * duplicated. Typically there is a function foo(), and its RCU
1497 * analogue, foo_rcu().
1498 *
1499 * -ECHILD is the error number of choice (just to avoid clashes) that
1500 * is returned if some aspect of an rcu-walk fails. Such an error must
1501 * be handled by restarting a traditional ref-walk (which will always
1502 * be able to complete).
1503 */
Al Virobd92d7f2011-03-14 19:54:59 -04001504 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
Al Viroee0827c2011-02-21 23:38:09 -05001505
Al Virobd92d7f2011-03-14 19:54:59 -04001506 if (unlikely(err))
1507 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001508
1509 current->total_link_count = 0;
Al Virobd92d7f2011-03-14 19:54:59 -04001510 err = link_path_walk(name, nd);
1511
1512 if (!err && !(flags & LOOKUP_PARENT)) {
Al Virobd92d7f2011-03-14 19:54:59 -04001513 err = lookup_last(nd, &path);
1514 while (err > 0) {
1515 void *cookie;
1516 struct path link = path;
Al Virobd92d7f2011-03-14 19:54:59 -04001517 nd->flags |= LOOKUP_PARENT;
Al Viro574197e2011-03-14 22:20:34 -04001518 err = follow_link(&link, nd, &cookie);
Al Virobd92d7f2011-03-14 19:54:59 -04001519 if (!err)
1520 err = lookup_last(nd, &path);
Al Viro574197e2011-03-14 22:20:34 -04001521 put_link(nd, &link, cookie);
Al Virobd92d7f2011-03-14 19:54:59 -04001522 }
1523 }
Al Viroee0827c2011-02-21 23:38:09 -05001524
Al Viro9f1fafe2011-03-25 11:00:12 -04001525 if (!err)
1526 err = complete_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04001527
1528 if (!err && nd->flags & LOOKUP_DIRECTORY) {
1529 if (!nd->inode->i_op->lookup) {
1530 path_put(&nd->path);
Al Virobd23a532011-03-23 09:56:30 -04001531 err = -ENOTDIR;
Al Virobd92d7f2011-03-14 19:54:59 -04001532 }
1533 }
Al Viro16c2cd72011-02-22 15:50:10 -05001534
Al Viro70e9b352011-03-05 21:12:22 -05001535 if (base)
1536 fput(base);
Al Viroee0827c2011-02-21 23:38:09 -05001537
Al Viro5b6ca022011-03-09 23:04:47 -05001538 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
Al Viro2a737872009-04-07 11:49:53 -04001539 path_put(&nd->root);
1540 nd->root.mnt = NULL;
1541 }
Al Virobd92d7f2011-03-14 19:54:59 -04001542 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001543}
Nick Piggin31e6b012011-01-07 17:49:52 +11001544
Al Viroee0827c2011-02-21 23:38:09 -05001545static int do_path_lookup(int dfd, const char *name,
1546 unsigned int flags, struct nameidata *nd)
1547{
1548 int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1549 if (unlikely(retval == -ECHILD))
1550 retval = path_lookupat(dfd, name, flags, nd);
1551 if (unlikely(retval == -ESTALE))
1552 retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11001553
1554 if (likely(!retval)) {
1555 if (unlikely(!audit_dummy_context())) {
1556 if (nd->path.dentry && nd->inode)
1557 audit_inode(name, nd->path.dentry);
1558 }
1559 }
Al Viro9b4a9b12009-04-07 11:44:16 -04001560 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561}
1562
Al Viroc9c6cac2011-02-16 15:15:47 -05001563int kern_path_parent(const char *name, struct nameidata *nd)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001564{
Al Viroc9c6cac2011-02-16 15:15:47 -05001565 return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001566}
1567
Al Virod1811462008-08-02 00:49:18 -04001568int kern_path(const char *name, unsigned int flags, struct path *path)
1569{
1570 struct nameidata nd;
1571 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1572 if (!res)
1573 *path = nd.path;
1574 return res;
1575}
1576
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001577/**
1578 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1579 * @dentry: pointer to dentry of the base directory
1580 * @mnt: pointer to vfs mount of the base directory
1581 * @name: pointer to file name
1582 * @flags: lookup flags
1583 * @nd: pointer to nameidata
1584 */
1585int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
1586 const char *name, unsigned int flags,
1587 struct nameidata *nd)
1588{
Al Viro5b6ca022011-03-09 23:04:47 -05001589 nd->root.dentry = dentry;
1590 nd->root.mnt = mnt;
1591 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
1592 return do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, nd);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001593}
1594
Christoph Hellwigeead1912007-10-16 23:25:38 -07001595static struct dentry *__lookup_hash(struct qstr *name,
1596 struct dentry *base, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597{
Christoph Hellwig81fca442010-10-06 10:47:47 +02001598 struct inode *inode = base->d_inode;
James Morris057f6c02007-04-26 00:12:05 -07001599 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 int err;
1601
Al Viro4ad5abb2011-06-20 19:57:03 -04001602 err = inode_permission(inode, MAY_EXEC);
Christoph Hellwig81fca442010-10-06 10:47:47 +02001603 if (err)
1604 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
1606 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001607 * Don't bother with __d_lookup: callers are for creat as
1608 * well as unlink, so a lot of the time it would cost
1609 * a double lookup.
Al Viro6e6b1bd2009-08-13 23:38:37 +04001610 */
Nick Pigginb04f7842010-08-18 04:37:34 +10001611 dentry = d_lookup(base, name);
Al Viro6e6b1bd2009-08-13 23:38:37 +04001612
Josef Bacik44396f42011-05-31 11:58:49 -04001613 if (dentry && d_need_lookup(dentry)) {
1614 /*
1615 * __lookup_hash is called with the parent dir's i_mutex already
1616 * held, so we are good to go here.
1617 */
1618 dentry = d_inode_lookup(base, dentry, nd);
1619 if (IS_ERR(dentry))
1620 return dentry;
1621 }
1622
Al Virod2d9e9f2011-06-20 10:55:26 -04001623 if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE)) {
1624 int status = d_revalidate(dentry, nd);
1625 if (unlikely(status <= 0)) {
1626 /*
1627 * The dentry failed validation.
1628 * If d_revalidate returned 0 attempt to invalidate
1629 * the dentry otherwise d_revalidate is asking us
1630 * to return a fail status.
1631 */
1632 if (status < 0) {
1633 dput(dentry);
1634 return ERR_PTR(status);
1635 } else if (!d_invalidate(dentry)) {
1636 dput(dentry);
1637 dentry = NULL;
1638 }
1639 }
1640 }
Al Viro6e6b1bd2009-08-13 23:38:37 +04001641
Nick Pigginbaa03892010-08-18 04:37:31 +10001642 if (!dentry)
1643 dentry = d_alloc_and_lookup(base, name, nd);
Al Viro5a202bc2011-03-08 14:17:44 -05001644
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 return dentry;
1646}
1647
James Morris057f6c02007-04-26 00:12:05 -07001648/*
1649 * Restricted form of lookup. Doesn't follow links, single-component only,
1650 * needs parent already locked. Doesn't follow mounts.
1651 * SMP-safe.
1652 */
Adrian Bunka244e162006-03-31 02:32:11 -08001653static struct dentry *lookup_hash(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654{
Jan Blunck4ac91372008-02-14 19:34:32 -08001655 return __lookup_hash(&nd->last, nd->path.dentry, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656}
1657
Christoph Hellwigeead1912007-10-16 23:25:38 -07001658/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07001659 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07001660 * @name: pathname component to lookup
1661 * @base: base directory to lookup from
1662 * @len: maximum length @len should be interpreted to
1663 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07001664 * Note that this routine is purely a helper for filesystem usage and should
1665 * not be called by generic code. Also note that by using this function the
Christoph Hellwigeead1912007-10-16 23:25:38 -07001666 * nameidata argument is passed to the filesystem methods and a filesystem
1667 * using this helper needs to be prepared for that.
1668 */
James Morris057f6c02007-04-26 00:12:05 -07001669struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1670{
James Morris057f6c02007-04-26 00:12:05 -07001671 struct qstr this;
Al Viro6a96ba52011-03-07 23:49:20 -05001672 unsigned long hash;
1673 unsigned int c;
James Morris057f6c02007-04-26 00:12:05 -07001674
David Woodhouse2f9092e2009-04-20 23:18:37 +01001675 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
1676
Al Viro6a96ba52011-03-07 23:49:20 -05001677 this.name = name;
1678 this.len = len;
1679 if (!len)
1680 return ERR_PTR(-EACCES);
1681
1682 hash = init_name_hash();
1683 while (len--) {
1684 c = *(const unsigned char *)name++;
1685 if (c == '/' || c == '\0')
1686 return ERR_PTR(-EACCES);
1687 hash = partial_name_hash(c, hash);
1688 }
1689 this.hash = end_name_hash(hash);
Al Viro5a202bc2011-03-08 14:17:44 -05001690 /*
1691 * See if the low-level filesystem might want
1692 * to use its own hash..
1693 */
1694 if (base->d_flags & DCACHE_OP_HASH) {
1695 int err = base->d_op->d_hash(base, base->d_inode, &this);
1696 if (err < 0)
1697 return ERR_PTR(err);
1698 }
Christoph Hellwigeead1912007-10-16 23:25:38 -07001699
Christoph Hellwig49705b72005-11-08 21:35:06 -08001700 return __lookup_hash(&this, base, NULL);
James Morris057f6c02007-04-26 00:12:05 -07001701}
1702
Al Viro2d8f3032008-07-22 09:59:21 -04001703int user_path_at(int dfd, const char __user *name, unsigned flags,
1704 struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705{
Al Viro2d8f3032008-07-22 09:59:21 -04001706 struct nameidata nd;
Al Virof52e0c12011-03-14 18:56:51 -04001707 char *tmp = getname_flags(name, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 int err = PTR_ERR(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 if (!IS_ERR(tmp)) {
Al Viro2d8f3032008-07-22 09:59:21 -04001710
1711 BUG_ON(flags & LOOKUP_PARENT);
1712
1713 err = do_path_lookup(dfd, tmp, flags, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 putname(tmp);
Al Viro2d8f3032008-07-22 09:59:21 -04001715 if (!err)
1716 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 }
1718 return err;
1719}
1720
Al Viro2ad94ae2008-07-21 09:32:51 -04001721static int user_path_parent(int dfd, const char __user *path,
1722 struct nameidata *nd, char **name)
1723{
1724 char *s = getname(path);
1725 int error;
1726
1727 if (IS_ERR(s))
1728 return PTR_ERR(s);
1729
1730 error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
1731 if (error)
1732 putname(s);
1733 else
1734 *name = s;
1735
1736 return error;
1737}
1738
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739/*
1740 * It's inline, so penalty for filesystems that don't use sticky bit is
1741 * minimal.
1742 */
1743static inline int check_sticky(struct inode *dir, struct inode *inode)
1744{
David Howellsda9592e2008-11-14 10:39:05 +11001745 uid_t fsuid = current_fsuid();
1746
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 if (!(dir->i_mode & S_ISVTX))
1748 return 0;
Serge E. Hallyne795b712011-03-23 16:43:25 -07001749 if (current_user_ns() != inode_userns(inode))
1750 goto other_userns;
David Howellsda9592e2008-11-14 10:39:05 +11001751 if (inode->i_uid == fsuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 return 0;
David Howellsda9592e2008-11-14 10:39:05 +11001753 if (dir->i_uid == fsuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 return 0;
Serge E. Hallyne795b712011-03-23 16:43:25 -07001755
1756other_userns:
1757 return !ns_capable(inode_userns(inode), CAP_FOWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758}
1759
1760/*
1761 * Check whether we can remove a link victim from directory dir, check
1762 * whether the type of victim is right.
1763 * 1. We can't do it if dir is read-only (done in permission())
1764 * 2. We should have write and exec permissions on dir
1765 * 3. We can't remove anything from append-only dir
1766 * 4. We can't do anything with immutable dir (done in permission())
1767 * 5. If the sticky bit on dir is set we should either
1768 * a. be owner of dir, or
1769 * b. be owner of victim, or
1770 * c. have CAP_FOWNER capability
1771 * 6. If the victim is append-only or immutable we can't do antyhing with
1772 * links pointing to it.
1773 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1774 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1775 * 9. We can't remove a root or mountpoint.
1776 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1777 * nfs_async_unlink().
1778 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001779static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780{
1781 int error;
1782
1783 if (!victim->d_inode)
1784 return -ENOENT;
1785
1786 BUG_ON(victim->d_parent->d_inode != dir);
Al Virocccc6bb2009-12-25 05:07:33 -05001787 audit_inode_child(victim, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Al Virof419a2e2008-07-22 00:07:17 -04001789 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 if (error)
1791 return error;
1792 if (IS_APPEND(dir))
1793 return -EPERM;
1794 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
Hugh Dickinsf9454542008-11-19 15:36:38 -08001795 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 return -EPERM;
1797 if (isdir) {
1798 if (!S_ISDIR(victim->d_inode->i_mode))
1799 return -ENOTDIR;
1800 if (IS_ROOT(victim))
1801 return -EBUSY;
1802 } else if (S_ISDIR(victim->d_inode->i_mode))
1803 return -EISDIR;
1804 if (IS_DEADDIR(dir))
1805 return -ENOENT;
1806 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1807 return -EBUSY;
1808 return 0;
1809}
1810
1811/* Check whether we can create an object with dentry child in directory
1812 * dir.
1813 * 1. We can't do it if child already exists (open has special treatment for
1814 * this case, but since we are inlined it's OK)
1815 * 2. We can't do it if dir is read-only (done in permission())
1816 * 3. We should have write and exec permissions on dir
1817 * 4. We can't do it if dir is immutable (done in permission())
1818 */
Miklos Szeredia95164d2008-07-30 15:08:48 +02001819static inline int may_create(struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820{
1821 if (child->d_inode)
1822 return -EEXIST;
1823 if (IS_DEADDIR(dir))
1824 return -ENOENT;
Al Virof419a2e2008-07-22 00:07:17 -04001825 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826}
1827
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828/*
1829 * p1 and p2 should be directories on the same fs.
1830 */
1831struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1832{
1833 struct dentry *p;
1834
1835 if (p1 == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07001836 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 return NULL;
1838 }
1839
Arjan van de Vena11f3a02006-03-23 03:00:33 -08001840 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001842 p = d_ancestor(p2, p1);
1843 if (p) {
1844 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1845 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
1846 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 }
1848
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001849 p = d_ancestor(p1, p2);
1850 if (p) {
1851 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1852 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1853 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 }
1855
Ingo Molnarf2eace22006-07-03 00:25:05 -07001856 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1857 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 return NULL;
1859}
1860
1861void unlock_rename(struct dentry *p1, struct dentry *p2)
1862{
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001863 mutex_unlock(&p1->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 if (p1 != p2) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001865 mutex_unlock(&p2->d_inode->i_mutex);
Arjan van de Vena11f3a02006-03-23 03:00:33 -08001866 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 }
1868}
1869
1870int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1871 struct nameidata *nd)
1872{
Miklos Szeredia95164d2008-07-30 15:08:48 +02001873 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
1875 if (error)
1876 return error;
1877
Al Viroacfa4382008-12-04 10:06:33 -05001878 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 return -EACCES; /* shouldn't it be ENOSYS? */
1880 mode &= S_IALLUGO;
1881 mode |= S_IFREG;
1882 error = security_inode_create(dir, dentry, mode);
1883 if (error)
1884 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 error = dir->i_op->create(dir, dentry, mode, nd);
Stephen Smalleya74574a2005-09-09 13:01:44 -07001886 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00001887 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 return error;
1889}
1890
Al Viro73d049a2011-03-11 12:08:24 -05001891static int may_open(struct path *path, int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001893 struct dentry *dentry = path->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 struct inode *inode = dentry->d_inode;
1895 int error;
1896
Al Virobcda7652011-03-13 16:42:14 -04001897 /* O_PATH? */
1898 if (!acc_mode)
1899 return 0;
1900
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 if (!inode)
1902 return -ENOENT;
1903
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001904 switch (inode->i_mode & S_IFMT) {
1905 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001907 case S_IFDIR:
1908 if (acc_mode & MAY_WRITE)
1909 return -EISDIR;
1910 break;
1911 case S_IFBLK:
1912 case S_IFCHR:
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001913 if (path->mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001915 /*FALLTHRU*/
1916 case S_IFIFO:
1917 case S_IFSOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001919 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08001920 }
Dave Hansenb41572e2007-10-16 23:31:14 -07001921
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001922 error = inode_permission(inode, acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07001923 if (error)
1924 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05001925
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 /*
1927 * An append-only file must be opened in append mode for writing.
1928 */
1929 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05001930 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05001931 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05001933 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 }
1935
1936 /* O_NOATIME can only be set by the owner or superuser */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001937 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
Al Viro7715b522009-12-16 03:54:00 -05001938 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
1940 /*
1941 * Ensure there are no outstanding leases on the file.
1942 */
Al Virob65a9cf2009-12-16 06:27:40 -05001943 return break_lease(inode, flag);
Al Viro7715b522009-12-16 03:54:00 -05001944}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
Jeff Laytone1181ee2010-12-07 16:19:50 -05001946static int handle_truncate(struct file *filp)
Al Viro7715b522009-12-16 03:54:00 -05001947{
Jeff Laytone1181ee2010-12-07 16:19:50 -05001948 struct path *path = &filp->f_path;
Al Viro7715b522009-12-16 03:54:00 -05001949 struct inode *inode = path->dentry->d_inode;
1950 int error = get_write_access(inode);
1951 if (error)
1952 return error;
1953 /*
1954 * Refuse to truncate files with mandatory locks held on them.
1955 */
1956 error = locks_verify_locked(inode);
1957 if (!error)
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +09001958 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05001959 if (!error) {
1960 error = do_truncate(path->dentry, 0,
1961 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
Jeff Laytone1181ee2010-12-07 16:19:50 -05001962 filp);
Al Viro7715b522009-12-16 03:54:00 -05001963 }
1964 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04001965 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966}
1967
Dave Hansend57999e2008-02-15 14:37:27 -08001968static inline int open_to_namei_flags(int flag)
1969{
Al Viro8a5e9292011-06-25 19:15:54 -04001970 if ((flag & O_ACCMODE) == 3)
1971 flag--;
Dave Hansend57999e2008-02-15 14:37:27 -08001972 return flag;
1973}
1974
Nick Piggin31e6b012011-01-07 17:49:52 +11001975/*
Al Virofe2d35f2011-03-05 22:58:25 -05001976 * Handle the last step of open()
Nick Piggin31e6b012011-01-07 17:49:52 +11001977 */
Al Virofb1cc552009-12-24 01:58:28 -05001978static struct file *do_last(struct nameidata *nd, struct path *path,
Al Viroc3e380b2011-02-23 13:39:45 -05001979 const struct open_flags *op, const char *pathname)
Al Virofb1cc552009-12-24 01:58:28 -05001980{
Al Viroa1e28032009-12-24 02:12:06 -05001981 struct dentry *dir = nd->path.dentry;
Al Viro6c0d46c2011-03-09 00:59:59 -05001982 struct dentry *dentry;
Al Viroca344a892011-03-09 00:36:45 -05001983 int open_flag = op->open_flag;
Al Viro6c0d46c2011-03-09 00:59:59 -05001984 int will_truncate = open_flag & O_TRUNC;
Al Viroca344a892011-03-09 00:36:45 -05001985 int want_write = 0;
Al Virobcda7652011-03-13 16:42:14 -04001986 int acc_mode = op->acc_mode;
Al Virofb1cc552009-12-24 01:58:28 -05001987 struct file *filp;
Al Viro16c2cd72011-02-22 15:50:10 -05001988 int error;
Al Virofb1cc552009-12-24 01:58:28 -05001989
Al Viroc3e380b2011-02-23 13:39:45 -05001990 nd->flags &= ~LOOKUP_PARENT;
1991 nd->flags |= op->intent;
1992
Al Viro1f36f772009-12-26 10:56:19 -05001993 switch (nd->last_type) {
1994 case LAST_DOTDOT:
Neil Brown176306f2010-05-24 16:57:56 +10001995 case LAST_DOT:
Al Virofe2d35f2011-03-05 22:58:25 -05001996 error = handle_dots(nd, nd->last_type);
1997 if (error)
1998 return ERR_PTR(error);
Al Viro1f36f772009-12-26 10:56:19 -05001999 /* fallthrough */
Al Viro1f36f772009-12-26 10:56:19 -05002000 case LAST_ROOT:
Al Viro9f1fafe2011-03-25 11:00:12 -04002001 error = complete_walk(nd);
Al Viro16c2cd72011-02-22 15:50:10 -05002002 if (error)
Al Viro9f1fafe2011-03-25 11:00:12 -04002003 return ERR_PTR(error);
Al Virofe2d35f2011-03-05 22:58:25 -05002004 audit_inode(pathname, nd->path.dentry);
Al Viroca344a892011-03-09 00:36:45 -05002005 if (open_flag & O_CREAT) {
Al Virofe2d35f2011-03-05 22:58:25 -05002006 error = -EISDIR;
2007 goto exit;
2008 }
2009 goto ok;
Al Viro1f36f772009-12-26 10:56:19 -05002010 case LAST_BIND:
Al Viro9f1fafe2011-03-25 11:00:12 -04002011 error = complete_walk(nd);
Al Viro16c2cd72011-02-22 15:50:10 -05002012 if (error)
Al Viro9f1fafe2011-03-25 11:00:12 -04002013 return ERR_PTR(error);
Al Viro1f36f772009-12-26 10:56:19 -05002014 audit_inode(pathname, dir);
Al Viro67ee3ad2009-12-26 07:01:01 -05002015 goto ok;
Al Viro1f36f772009-12-26 10:56:19 -05002016 }
Al Viro67ee3ad2009-12-26 07:01:01 -05002017
Al Viroca344a892011-03-09 00:36:45 -05002018 if (!(open_flag & O_CREAT)) {
Al Virobcda7652011-03-13 16:42:14 -04002019 int symlink_ok = 0;
Al Virofe2d35f2011-03-05 22:58:25 -05002020 if (nd->last.name[nd->last.len])
2021 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
Al Virobcda7652011-03-13 16:42:14 -04002022 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2023 symlink_ok = 1;
Al Virofe2d35f2011-03-05 22:58:25 -05002024 /* we _can_ be in RCU mode here */
Al Viroce57dfc2011-03-13 19:58:58 -04002025 error = walk_component(nd, path, &nd->last, LAST_NORM,
2026 !symlink_ok);
2027 if (error < 0)
Al Virofe2d35f2011-03-05 22:58:25 -05002028 return ERR_PTR(error);
Al Viroce57dfc2011-03-13 19:58:58 -04002029 if (error) /* symlink */
Al Virofe2d35f2011-03-05 22:58:25 -05002030 return NULL;
Al Virofe2d35f2011-03-05 22:58:25 -05002031 /* sayonara */
Al Viro9f1fafe2011-03-25 11:00:12 -04002032 error = complete_walk(nd);
2033 if (error)
2034 return ERR_PTR(-ECHILD);
Al Virofe2d35f2011-03-05 22:58:25 -05002035
2036 error = -ENOTDIR;
2037 if (nd->flags & LOOKUP_DIRECTORY) {
Al Viroce57dfc2011-03-13 19:58:58 -04002038 if (!nd->inode->i_op->lookup)
Al Virofe2d35f2011-03-05 22:58:25 -05002039 goto exit;
2040 }
2041 audit_inode(pathname, nd->path.dentry);
2042 goto ok;
2043 }
2044
2045 /* create side of things */
Al Viro9f1fafe2011-03-25 11:00:12 -04002046 error = complete_walk(nd);
2047 if (error)
2048 return ERR_PTR(error);
Al Virofe2d35f2011-03-05 22:58:25 -05002049
2050 audit_inode(pathname, dir);
Al Viro16c2cd72011-02-22 15:50:10 -05002051 error = -EISDIR;
Al Viro1f36f772009-12-26 10:56:19 -05002052 /* trailing slashes? */
Nick Piggin31e6b012011-01-07 17:49:52 +11002053 if (nd->last.name[nd->last.len])
2054 goto exit;
Al Viroa2c36b42009-12-24 03:39:50 -05002055
Al Viroa1e28032009-12-24 02:12:06 -05002056 mutex_lock(&dir->d_inode->i_mutex);
2057
Al Viro6c0d46c2011-03-09 00:59:59 -05002058 dentry = lookup_hash(nd);
2059 error = PTR_ERR(dentry);
2060 if (IS_ERR(dentry)) {
Al Virofb1cc552009-12-24 01:58:28 -05002061 mutex_unlock(&dir->d_inode->i_mutex);
2062 goto exit;
2063 }
2064
Al Viro6c0d46c2011-03-09 00:59:59 -05002065 path->dentry = dentry;
2066 path->mnt = nd->path.mnt;
2067
Al Virofb1cc552009-12-24 01:58:28 -05002068 /* Negative dentry, just create the file */
Al Viro6c0d46c2011-03-09 00:59:59 -05002069 if (!dentry->d_inode) {
2070 int mode = op->mode;
2071 if (!IS_POSIXACL(dir->d_inode))
2072 mode &= ~current_umask();
Al Virofb1cc552009-12-24 01:58:28 -05002073 /*
2074 * This write is needed to ensure that a
Al Viro6c0d46c2011-03-09 00:59:59 -05002075 * rw->ro transition does not occur between
Al Virofb1cc552009-12-24 01:58:28 -05002076 * the time when the file is created and when
2077 * a permanent write count is taken through
2078 * the 'struct file' in nameidata_to_filp().
2079 */
2080 error = mnt_want_write(nd->path.mnt);
2081 if (error)
2082 goto exit_mutex_unlock;
Al Viroca344a892011-03-09 00:36:45 -05002083 want_write = 1;
Al Viro9b44f1b2011-03-09 00:17:27 -05002084 /* Don't check for write permission, don't truncate */
Al Viroca344a892011-03-09 00:36:45 -05002085 open_flag &= ~O_TRUNC;
Al Viro6c0d46c2011-03-09 00:59:59 -05002086 will_truncate = 0;
Al Virobcda7652011-03-13 16:42:14 -04002087 acc_mode = MAY_OPEN;
Al Viro6c0d46c2011-03-09 00:59:59 -05002088 error = security_path_mknod(&nd->path, dentry, mode, 0);
2089 if (error)
2090 goto exit_mutex_unlock;
2091 error = vfs_create(dir->d_inode, dentry, mode, nd);
2092 if (error)
2093 goto exit_mutex_unlock;
2094 mutex_unlock(&dir->d_inode->i_mutex);
2095 dput(nd->path.dentry);
2096 nd->path.dentry = dentry;
Al Viroca344a892011-03-09 00:36:45 -05002097 goto common;
Al Virofb1cc552009-12-24 01:58:28 -05002098 }
2099
2100 /*
2101 * It already exists.
2102 */
2103 mutex_unlock(&dir->d_inode->i_mutex);
2104 audit_inode(pathname, path->dentry);
2105
2106 error = -EEXIST;
Al Viroca344a892011-03-09 00:36:45 -05002107 if (open_flag & O_EXCL)
Al Virofb1cc552009-12-24 01:58:28 -05002108 goto exit_dput;
2109
David Howells9875cf82011-01-14 18:45:21 +00002110 error = follow_managed(path, nd->flags);
2111 if (error < 0)
2112 goto exit_dput;
Al Virofb1cc552009-12-24 01:58:28 -05002113
2114 error = -ENOENT;
2115 if (!path->dentry->d_inode)
2116 goto exit_dput;
Al Viro9e67f362009-12-26 07:04:50 -05002117
2118 if (path->dentry->d_inode->i_op->follow_link)
Al Virofb1cc552009-12-24 01:58:28 -05002119 return NULL;
Al Virofb1cc552009-12-24 01:58:28 -05002120
2121 path_to_nameidata(path, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11002122 nd->inode = path->dentry->d_inode;
Al Virofb1cc552009-12-24 01:58:28 -05002123 error = -EISDIR;
Nick Piggin31e6b012011-01-07 17:49:52 +11002124 if (S_ISDIR(nd->inode->i_mode))
Al Virofb1cc552009-12-24 01:58:28 -05002125 goto exit;
Al Viro67ee3ad2009-12-26 07:01:01 -05002126ok:
Al Viro6c0d46c2011-03-09 00:59:59 -05002127 if (!S_ISREG(nd->inode->i_mode))
2128 will_truncate = 0;
2129
Al Viro0f9d1a12011-03-09 00:13:14 -05002130 if (will_truncate) {
2131 error = mnt_want_write(nd->path.mnt);
2132 if (error)
2133 goto exit;
Al Viroca344a892011-03-09 00:36:45 -05002134 want_write = 1;
Al Viro0f9d1a12011-03-09 00:13:14 -05002135 }
Al Viroca344a892011-03-09 00:36:45 -05002136common:
Al Virobcda7652011-03-13 16:42:14 -04002137 error = may_open(&nd->path, acc_mode, open_flag);
Al Viroca344a892011-03-09 00:36:45 -05002138 if (error)
Al Viro0f9d1a12011-03-09 00:13:14 -05002139 goto exit;
Al Viro0f9d1a12011-03-09 00:13:14 -05002140 filp = nameidata_to_filp(nd);
2141 if (!IS_ERR(filp)) {
2142 error = ima_file_check(filp, op->acc_mode);
2143 if (error) {
2144 fput(filp);
2145 filp = ERR_PTR(error);
2146 }
2147 }
2148 if (!IS_ERR(filp)) {
2149 if (will_truncate) {
2150 error = handle_truncate(filp);
2151 if (error) {
2152 fput(filp);
2153 filp = ERR_PTR(error);
2154 }
2155 }
2156 }
Al Viroca344a892011-03-09 00:36:45 -05002157out:
2158 if (want_write)
Al Viro0f9d1a12011-03-09 00:13:14 -05002159 mnt_drop_write(nd->path.mnt);
2160 path_put(&nd->path);
Al Virofb1cc552009-12-24 01:58:28 -05002161 return filp;
2162
2163exit_mutex_unlock:
2164 mutex_unlock(&dir->d_inode->i_mutex);
2165exit_dput:
2166 path_put_conditional(path, nd);
2167exit:
Al Viroca344a892011-03-09 00:36:45 -05002168 filp = ERR_PTR(error);
2169 goto out;
Al Virofb1cc552009-12-24 01:58:28 -05002170}
2171
Al Viro13aab422011-02-23 17:54:08 -05002172static struct file *path_openat(int dfd, const char *pathname,
Al Viro73d049a2011-03-11 12:08:24 -05002173 struct nameidata *nd, const struct open_flags *op, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174{
Al Virofe2d35f2011-03-05 22:58:25 -05002175 struct file *base = NULL;
Dave Hansen4a3fd212008-02-15 14:37:48 -08002176 struct file *filp;
Al Viro9850c052010-01-13 15:01:15 -05002177 struct path path;
Al Viro13aab422011-02-23 17:54:08 -05002178 int error;
Nick Piggin31e6b012011-01-07 17:49:52 +11002179
2180 filp = get_empty_filp();
2181 if (!filp)
2182 return ERR_PTR(-ENFILE);
2183
Al Viro47c805d2011-02-23 17:44:09 -05002184 filp->f_flags = op->open_flag;
Al Viro73d049a2011-03-11 12:08:24 -05002185 nd->intent.open.file = filp;
2186 nd->intent.open.flags = open_to_namei_flags(op->open_flag);
2187 nd->intent.open.create_mode = op->mode;
Nick Piggin31e6b012011-01-07 17:49:52 +11002188
Al Viro73d049a2011-03-11 12:08:24 -05002189 error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
Nick Piggin31e6b012011-01-07 17:49:52 +11002190 if (unlikely(error))
Al Viro13aab422011-02-23 17:54:08 -05002191 goto out_filp;
Nick Piggin31e6b012011-01-07 17:49:52 +11002192
Al Virofe2d35f2011-03-05 22:58:25 -05002193 current->total_link_count = 0;
Al Viro73d049a2011-03-11 12:08:24 -05002194 error = link_path_walk(pathname, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11002195 if (unlikely(error))
2196 goto out_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
Al Viro73d049a2011-03-11 12:08:24 -05002198 filp = do_last(nd, &path, op, pathname);
Al Viro806b6812009-12-26 07:16:40 -05002199 while (unlikely(!filp)) { /* trailing symlink */
Nick Piggin7b9337a2011-01-14 08:42:43 +00002200 struct path link = path;
Al Virodef4af32009-12-26 08:37:05 -05002201 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04002202 if (!(nd->flags & LOOKUP_FOLLOW)) {
Al Viro73d049a2011-03-11 12:08:24 -05002203 path_put_conditional(&path, nd);
2204 path_put(&nd->path);
Al Viro40b39132011-03-09 16:22:18 -05002205 filp = ERR_PTR(-ELOOP);
2206 break;
2207 }
Al Viro73d049a2011-03-11 12:08:24 -05002208 nd->flags |= LOOKUP_PARENT;
2209 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
Al Viro574197e2011-03-14 22:20:34 -04002210 error = follow_link(&link, nd, &cookie);
Al Viroc3e380b2011-02-23 13:39:45 -05002211 if (unlikely(error))
Al Virof1afe9e2011-02-22 22:27:28 -05002212 filp = ERR_PTR(error);
Al Viroc3e380b2011-02-23 13:39:45 -05002213 else
Al Viro73d049a2011-03-11 12:08:24 -05002214 filp = do_last(nd, &path, op, pathname);
Al Viro574197e2011-03-14 22:20:34 -04002215 put_link(nd, &link, cookie);
Al Viro806b6812009-12-26 07:16:40 -05002216 }
Al Viro10fa8e62009-12-26 07:09:49 -05002217out:
Al Viro73d049a2011-03-11 12:08:24 -05002218 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
2219 path_put(&nd->root);
Al Virofe2d35f2011-03-05 22:58:25 -05002220 if (base)
2221 fput(base);
Al Viro73d049a2011-03-11 12:08:24 -05002222 release_open_intent(nd);
Al Viro10fa8e62009-12-26 07:09:49 -05002223 return filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
Nick Piggin31e6b012011-01-07 17:49:52 +11002225out_filp:
Al Viro806b6812009-12-26 07:16:40 -05002226 filp = ERR_PTR(error);
Al Viro10fa8e62009-12-26 07:09:49 -05002227 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228}
2229
Al Viro13aab422011-02-23 17:54:08 -05002230struct file *do_filp_open(int dfd, const char *pathname,
2231 const struct open_flags *op, int flags)
2232{
Al Viro73d049a2011-03-11 12:08:24 -05002233 struct nameidata nd;
Al Viro13aab422011-02-23 17:54:08 -05002234 struct file *filp;
2235
Al Viro73d049a2011-03-11 12:08:24 -05002236 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
Al Viro13aab422011-02-23 17:54:08 -05002237 if (unlikely(filp == ERR_PTR(-ECHILD)))
Al Viro73d049a2011-03-11 12:08:24 -05002238 filp = path_openat(dfd, pathname, &nd, op, flags);
Al Viro13aab422011-02-23 17:54:08 -05002239 if (unlikely(filp == ERR_PTR(-ESTALE)))
Al Viro73d049a2011-03-11 12:08:24 -05002240 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
Al Viro13aab422011-02-23 17:54:08 -05002241 return filp;
2242}
2243
Al Viro73d049a2011-03-11 12:08:24 -05002244struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
2245 const char *name, const struct open_flags *op, int flags)
2246{
2247 struct nameidata nd;
2248 struct file *file;
2249
2250 nd.root.mnt = mnt;
2251 nd.root.dentry = dentry;
2252
2253 flags |= LOOKUP_ROOT;
2254
Al Virobcda7652011-03-13 16:42:14 -04002255 if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
Al Viro73d049a2011-03-11 12:08:24 -05002256 return ERR_PTR(-ELOOP);
2257
2258 file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
2259 if (unlikely(file == ERR_PTR(-ECHILD)))
2260 file = path_openat(-1, name, &nd, op, flags);
2261 if (unlikely(file == ERR_PTR(-ESTALE)))
2262 file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
2263 return file;
2264}
2265
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266/**
2267 * lookup_create - lookup a dentry, creating it if it doesn't exist
2268 * @nd: nameidata info
2269 * @is_dir: directory flag
2270 *
2271 * Simple function to lookup and return a dentry and create it
2272 * if it doesn't exist. Is SMP-safe.
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002273 *
Jan Blunck4ac91372008-02-14 19:34:32 -08002274 * Returns with nd->path.dentry->d_inode->i_mutex locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 */
2276struct dentry *lookup_create(struct nameidata *nd, int is_dir)
2277{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002278 struct dentry *dentry = ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
Jan Blunck4ac91372008-02-14 19:34:32 -08002280 mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002281 /*
2282 * Yucky last component or no last component at all?
2283 * (foo/., foo/.., /////)
2284 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 if (nd->last_type != LAST_NORM)
2286 goto fail;
2287 nd->flags &= ~LOOKUP_PARENT;
Al Viro35165862008-08-05 03:00:49 -04002288 nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
ASANO Masahiroa6349042006-08-22 20:06:02 -04002289 nd->intent.open.flags = O_EXCL;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002290
2291 /*
2292 * Do the final lookup.
2293 */
Christoph Hellwig49705b72005-11-08 21:35:06 -08002294 dentry = lookup_hash(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 if (IS_ERR(dentry))
2296 goto fail;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002297
Al Viroe9baf6e2008-05-15 04:49:12 -04002298 if (dentry->d_inode)
2299 goto eexist;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002300 /*
2301 * Special case - lookup gave negative, but... we had foo/bar/
2302 * From the vfs_mknod() POV we just have a negative dentry -
2303 * all is fine. Let's be bastards - you had / on the end, you've
2304 * been asking for (non-existent) directory. -ENOENT for you.
2305 */
Al Viroe9baf6e2008-05-15 04:49:12 -04002306 if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
2307 dput(dentry);
2308 dentry = ERR_PTR(-ENOENT);
2309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 return dentry;
Al Viroe9baf6e2008-05-15 04:49:12 -04002311eexist:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 dput(dentry);
Al Viroe9baf6e2008-05-15 04:49:12 -04002313 dentry = ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314fail:
2315 return dentry;
2316}
Christoph Hellwigf81a0bf2005-05-19 12:26:43 -07002317EXPORT_SYMBOL_GPL(lookup_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
2319int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
2320{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002321 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
2323 if (error)
2324 return error;
2325
Serge E. Hallyne795b712011-03-23 16:43:25 -07002326 if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
2327 !ns_capable(inode_userns(dir), CAP_MKNOD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 return -EPERM;
2329
Al Viroacfa4382008-12-04 10:06:33 -05002330 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 return -EPERM;
2332
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07002333 error = devcgroup_inode_mknod(mode, dev);
2334 if (error)
2335 return error;
2336
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 error = security_inode_mknod(dir, dentry, mode, dev);
2338 if (error)
2339 return error;
2340
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002342 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002343 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 return error;
2345}
2346
Dave Hansen463c3192008-02-15 14:37:57 -08002347static int may_mknod(mode_t mode)
2348{
2349 switch (mode & S_IFMT) {
2350 case S_IFREG:
2351 case S_IFCHR:
2352 case S_IFBLK:
2353 case S_IFIFO:
2354 case S_IFSOCK:
2355 case 0: /* zero mode translates to S_IFREG */
2356 return 0;
2357 case S_IFDIR:
2358 return -EPERM;
2359 default:
2360 return -EINVAL;
2361 }
2362}
2363
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002364SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
2365 unsigned, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366{
Al Viro2ad94ae2008-07-21 09:32:51 -04002367 int error;
2368 char *tmp;
2369 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 struct nameidata nd;
2371
2372 if (S_ISDIR(mode))
2373 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
Al Viro2ad94ae2008-07-21 09:32:51 -04002375 error = user_path_parent(dfd, filename, &nd, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002377 return error;
2378
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 dentry = lookup_create(&nd, 0);
Dave Hansen463c3192008-02-15 14:37:57 -08002380 if (IS_ERR(dentry)) {
2381 error = PTR_ERR(dentry);
2382 goto out_unlock;
2383 }
Jan Blunck4ac91372008-02-14 19:34:32 -08002384 if (!IS_POSIXACL(nd.path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04002385 mode &= ~current_umask();
Dave Hansen463c3192008-02-15 14:37:57 -08002386 error = may_mknod(mode);
2387 if (error)
2388 goto out_dput;
2389 error = mnt_want_write(nd.path.mnt);
2390 if (error)
2391 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002392 error = security_path_mknod(&nd.path, dentry, mode, dev);
2393 if (error)
2394 goto out_drop_write;
Dave Hansen463c3192008-02-15 14:37:57 -08002395 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 case 0: case S_IFREG:
Al Viro554a8b92011-06-23 12:35:50 -04002397 error = vfs_create(nd.path.dentry->d_inode,dentry,mode,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 break;
2399 case S_IFCHR: case S_IFBLK:
Jan Blunck4ac91372008-02-14 19:34:32 -08002400 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 new_decode_dev(dev));
2402 break;
2403 case S_IFIFO: case S_IFSOCK:
Jan Blunck4ac91372008-02-14 19:34:32 -08002404 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 }
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002407out_drop_write:
Dave Hansen463c3192008-02-15 14:37:57 -08002408 mnt_drop_write(nd.path.mnt);
2409out_dput:
2410 dput(dentry);
2411out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002412 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002413 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 putname(tmp);
2415
2416 return error;
2417}
2418
Heiko Carstens3480b252009-01-14 14:14:16 +01002419SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002420{
2421 return sys_mknodat(AT_FDCWD, filename, mode, dev);
2422}
2423
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2425{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002426 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
2428 if (error)
2429 return error;
2430
Al Viroacfa4382008-12-04 10:06:33 -05002431 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 return -EPERM;
2433
2434 mode &= (S_IRWXUGO|S_ISVTX);
2435 error = security_inode_mkdir(dir, dentry, mode);
2436 if (error)
2437 return error;
2438
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002440 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002441 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 return error;
2443}
2444
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002445SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446{
2447 int error = 0;
2448 char * tmp;
Dave Hansen6902d922006-09-30 23:29:01 -07002449 struct dentry *dentry;
2450 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451
Al Viro2ad94ae2008-07-21 09:32:51 -04002452 error = user_path_parent(dfd, pathname, &nd, &tmp);
2453 if (error)
Dave Hansen6902d922006-09-30 23:29:01 -07002454 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
Dave Hansen6902d922006-09-30 23:29:01 -07002456 dentry = lookup_create(&nd, 1);
2457 error = PTR_ERR(dentry);
2458 if (IS_ERR(dentry))
2459 goto out_unlock;
2460
Jan Blunck4ac91372008-02-14 19:34:32 -08002461 if (!IS_POSIXACL(nd.path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04002462 mode &= ~current_umask();
Dave Hansen463c3192008-02-15 14:37:57 -08002463 error = mnt_want_write(nd.path.mnt);
2464 if (error)
2465 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002466 error = security_path_mkdir(&nd.path, dentry, mode);
2467 if (error)
2468 goto out_drop_write;
Jan Blunck4ac91372008-02-14 19:34:32 -08002469 error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002470out_drop_write:
Dave Hansen463c3192008-02-15 14:37:57 -08002471 mnt_drop_write(nd.path.mnt);
2472out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002473 dput(dentry);
2474out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002475 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002476 path_put(&nd.path);
Dave Hansen6902d922006-09-30 23:29:01 -07002477 putname(tmp);
2478out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 return error;
2480}
2481
Heiko Carstens3cdad422009-01-14 14:14:22 +01002482SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002483{
2484 return sys_mkdirat(AT_FDCWD, pathname, mode);
2485}
2486
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487/*
Sage Weila71905f2011-05-24 13:06:08 -07002488 * The dentry_unhash() helper will try to drop the dentry early: we
2489 * should have a usage count of 2 if we're the only user of this
2490 * dentry, and if that is true (possibly after pruning the dcache),
2491 * then we drop the dentry now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 *
2493 * A low-level filesystem can, if it choses, legally
2494 * do a
2495 *
2496 * if (!d_unhashed(dentry))
2497 * return -EBUSY;
2498 *
2499 * if it cannot handle the case of removing a directory
2500 * that is still in use by something else..
2501 */
2502void dentry_unhash(struct dentry *dentry)
2503{
Vasily Averindc168422006-12-06 20:37:07 -08002504 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 spin_lock(&dentry->d_lock);
Sage Weil64252c72011-05-24 13:06:05 -07002506 if (dentry->d_count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 __d_drop(dentry);
2508 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509}
2510
2511int vfs_rmdir(struct inode *dir, struct dentry *dentry)
2512{
2513 int error = may_delete(dir, dentry, 1);
2514
2515 if (error)
2516 return error;
2517
Al Viroacfa4382008-12-04 10:06:33 -05002518 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 return -EPERM;
2520
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002521 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
Sage Weil912dbc12011-05-24 13:06:11 -07002523 error = -EBUSY;
2524 if (d_mountpoint(dentry))
2525 goto out;
2526
2527 error = security_inode_rmdir(dir, dentry);
2528 if (error)
2529 goto out;
2530
Sage Weil3cebde22011-05-29 21:20:59 -07002531 shrink_dcache_parent(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07002532 error = dir->i_op->rmdir(dir, dentry);
2533 if (error)
2534 goto out;
2535
2536 dentry->d_inode->i_flags |= S_DEAD;
2537 dont_mount(dentry);
2538
2539out:
2540 mutex_unlock(&dentry->d_inode->i_mutex);
2541 if (!error)
2542 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 return error;
2544}
2545
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002546static long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547{
2548 int error = 0;
2549 char * name;
2550 struct dentry *dentry;
2551 struct nameidata nd;
2552
Al Viro2ad94ae2008-07-21 09:32:51 -04002553 error = user_path_parent(dfd, pathname, &nd, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002555 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
2557 switch(nd.last_type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002558 case LAST_DOTDOT:
2559 error = -ENOTEMPTY;
2560 goto exit1;
2561 case LAST_DOT:
2562 error = -EINVAL;
2563 goto exit1;
2564 case LAST_ROOT:
2565 error = -EBUSY;
2566 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002568
2569 nd.flags &= ~LOOKUP_PARENT;
2570
Jan Blunck4ac91372008-02-14 19:34:32 -08002571 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002572 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002574 if (IS_ERR(dentry))
2575 goto exit2;
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04002576 if (!dentry->d_inode) {
2577 error = -ENOENT;
2578 goto exit3;
2579 }
Dave Hansen06227532008-02-15 14:37:34 -08002580 error = mnt_want_write(nd.path.mnt);
2581 if (error)
2582 goto exit3;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002583 error = security_path_rmdir(&nd.path, dentry);
2584 if (error)
2585 goto exit4;
Jan Blunck4ac91372008-02-14 19:34:32 -08002586 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002587exit4:
Dave Hansen06227532008-02-15 14:37:34 -08002588 mnt_drop_write(nd.path.mnt);
2589exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07002590 dput(dentry);
2591exit2:
Jan Blunck4ac91372008-02-14 19:34:32 -08002592 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002594 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 putname(name);
2596 return error;
2597}
2598
Heiko Carstens3cdad422009-01-14 14:14:22 +01002599SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002600{
2601 return do_rmdir(AT_FDCWD, pathname);
2602}
2603
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604int vfs_unlink(struct inode *dir, struct dentry *dentry)
2605{
2606 int error = may_delete(dir, dentry, 0);
2607
2608 if (error)
2609 return error;
2610
Al Viroacfa4382008-12-04 10:06:33 -05002611 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 return -EPERM;
2613
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002614 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 if (d_mountpoint(dentry))
2616 error = -EBUSY;
2617 else {
2618 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05002619 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 error = dir->i_op->unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05002621 if (!error)
Al Virod83c49f2010-04-30 17:17:09 -04002622 dont_mount(dentry);
Al Virobec10522010-03-03 14:12:08 -05002623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002625 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626
2627 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2628 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
Jan Karaece95912008-02-06 01:37:13 -08002629 fsnotify_link_count(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 }
Robert Love0eeca282005-07-12 17:06:03 -04002632
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 return error;
2634}
2635
2636/*
2637 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002638 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 * writeout happening, and we don't want to prevent access to the directory
2640 * while waiting on the I/O.
2641 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002642static long do_unlinkat(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643{
Al Viro2ad94ae2008-07-21 09:32:51 -04002644 int error;
2645 char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 struct dentry *dentry;
2647 struct nameidata nd;
2648 struct inode *inode = NULL;
2649
Al Viro2ad94ae2008-07-21 09:32:51 -04002650 error = user_path_parent(dfd, pathname, &nd, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002652 return error;
2653
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 error = -EISDIR;
2655 if (nd.last_type != LAST_NORM)
2656 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002657
2658 nd.flags &= ~LOOKUP_PARENT;
2659
Jan Blunck4ac91372008-02-14 19:34:32 -08002660 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002661 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 error = PTR_ERR(dentry);
2663 if (!IS_ERR(dentry)) {
2664 /* Why not before? Because we want correct error value */
Török Edwin50338b82011-06-16 00:06:14 +03002665 if (nd.last.name[nd.last.len])
2666 goto slashes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 inode = dentry->d_inode;
Török Edwin50338b82011-06-16 00:06:14 +03002668 if (!inode)
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04002669 goto slashes;
2670 ihold(inode);
Dave Hansen06227532008-02-15 14:37:34 -08002671 error = mnt_want_write(nd.path.mnt);
2672 if (error)
2673 goto exit2;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002674 error = security_path_unlink(&nd.path, dentry);
2675 if (error)
2676 goto exit3;
Jan Blunck4ac91372008-02-14 19:34:32 -08002677 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002678exit3:
Dave Hansen06227532008-02-15 14:37:34 -08002679 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 exit2:
2681 dput(dentry);
2682 }
Jan Blunck4ac91372008-02-14 19:34:32 -08002683 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 if (inode)
2685 iput(inode); /* truncate the inode here */
2686exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002687 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 putname(name);
2689 return error;
2690
2691slashes:
2692 error = !dentry->d_inode ? -ENOENT :
2693 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2694 goto exit2;
2695}
2696
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002697SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002698{
2699 if ((flag & ~AT_REMOVEDIR) != 0)
2700 return -EINVAL;
2701
2702 if (flag & AT_REMOVEDIR)
2703 return do_rmdir(dfd, pathname);
2704
2705 return do_unlinkat(dfd, pathname);
2706}
2707
Heiko Carstens3480b252009-01-14 14:14:16 +01002708SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002709{
2710 return do_unlinkat(AT_FDCWD, pathname);
2711}
2712
Miklos Szeredidb2e7472008-06-24 16:50:16 +02002713int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002715 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716
2717 if (error)
2718 return error;
2719
Al Viroacfa4382008-12-04 10:06:33 -05002720 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 return -EPERM;
2722
2723 error = security_inode_symlink(dir, dentry, oldname);
2724 if (error)
2725 return error;
2726
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002728 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002729 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 return error;
2731}
2732
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002733SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
2734 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735{
Al Viro2ad94ae2008-07-21 09:32:51 -04002736 int error;
2737 char *from;
2738 char *to;
Dave Hansen6902d922006-09-30 23:29:01 -07002739 struct dentry *dentry;
2740 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741
2742 from = getname(oldname);
Al Viro2ad94ae2008-07-21 09:32:51 -04002743 if (IS_ERR(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 return PTR_ERR(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04002745
2746 error = user_path_parent(newdfd, newname, &nd, &to);
2747 if (error)
Dave Hansen6902d922006-09-30 23:29:01 -07002748 goto out_putname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749
Dave Hansen6902d922006-09-30 23:29:01 -07002750 dentry = lookup_create(&nd, 0);
2751 error = PTR_ERR(dentry);
2752 if (IS_ERR(dentry))
2753 goto out_unlock;
2754
Dave Hansen75c3f292008-02-15 14:37:45 -08002755 error = mnt_want_write(nd.path.mnt);
2756 if (error)
2757 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002758 error = security_path_symlink(&nd.path, dentry, from);
2759 if (error)
2760 goto out_drop_write;
Miklos Szeredidb2e7472008-06-24 16:50:16 +02002761 error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002762out_drop_write:
Dave Hansen75c3f292008-02-15 14:37:45 -08002763 mnt_drop_write(nd.path.mnt);
2764out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002765 dput(dentry);
2766out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002767 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002768 path_put(&nd.path);
Dave Hansen6902d922006-09-30 23:29:01 -07002769 putname(to);
2770out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 putname(from);
2772 return error;
2773}
2774
Heiko Carstens3480b252009-01-14 14:14:16 +01002775SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002776{
2777 return sys_symlinkat(oldname, AT_FDCWD, newname);
2778}
2779
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2781{
2782 struct inode *inode = old_dentry->d_inode;
2783 int error;
2784
2785 if (!inode)
2786 return -ENOENT;
2787
Miklos Szeredia95164d2008-07-30 15:08:48 +02002788 error = may_create(dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 if (error)
2790 return error;
2791
2792 if (dir->i_sb != inode->i_sb)
2793 return -EXDEV;
2794
2795 /*
2796 * A link to an append-only or immutable file cannot be created.
2797 */
2798 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2799 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05002800 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002802 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 return -EPERM;
2804
2805 error = security_inode_link(old_dentry, dir, new_dentry);
2806 if (error)
2807 return error;
2808
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002809 mutex_lock(&inode->i_mutex);
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05302810 /* Make sure we don't allow creating hardlink to an unlinked file */
2811 if (inode->i_nlink == 0)
2812 error = -ENOENT;
2813 else
2814 error = dir->i_op->link(old_dentry, dir, new_dentry);
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002815 mutex_unlock(&inode->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07002816 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002817 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 return error;
2819}
2820
2821/*
2822 * Hardlinks are often used in delicate situations. We avoid
2823 * security-related surprises by not following symlinks on the
2824 * newname. --KAB
2825 *
2826 * We don't follow them on the oldname either to be compatible
2827 * with linux 2.0, and to avoid hard-linking to directories
2828 * and other special files. --ADM
2829 */
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002830SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
2831 int, newdfd, const char __user *, newname, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832{
2833 struct dentry *new_dentry;
Al Viro2d8f3032008-07-22 09:59:21 -04002834 struct nameidata nd;
2835 struct path old_path;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05302836 int how = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 int error;
Al Viro2ad94ae2008-07-21 09:32:51 -04002838 char *to;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05302840 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002841 return -EINVAL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05302842 /*
2843 * To use null names we require CAP_DAC_READ_SEARCH
2844 * This ensures that not everyone will be able to create
2845 * handlink using the passed filedescriptor.
2846 */
2847 if (flags & AT_EMPTY_PATH) {
2848 if (!capable(CAP_DAC_READ_SEARCH))
2849 return -ENOENT;
2850 how = LOOKUP_EMPTY;
2851 }
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002852
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05302853 if (flags & AT_SYMLINK_FOLLOW)
2854 how |= LOOKUP_FOLLOW;
2855
2856 error = user_path_at(olddfd, oldname, how, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002858 return error;
2859
2860 error = user_path_parent(newdfd, newname, &nd, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 if (error)
2862 goto out;
2863 error = -EXDEV;
Al Viro2d8f3032008-07-22 09:59:21 -04002864 if (old_path.mnt != nd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 goto out_release;
2866 new_dentry = lookup_create(&nd, 0);
2867 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002868 if (IS_ERR(new_dentry))
2869 goto out_unlock;
Dave Hansen75c3f292008-02-15 14:37:45 -08002870 error = mnt_want_write(nd.path.mnt);
2871 if (error)
2872 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002873 error = security_path_link(old_path.dentry, &nd.path, new_dentry);
2874 if (error)
2875 goto out_drop_write;
Al Viro2d8f3032008-07-22 09:59:21 -04002876 error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002877out_drop_write:
Dave Hansen75c3f292008-02-15 14:37:45 -08002878 mnt_drop_write(nd.path.mnt);
2879out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002880 dput(new_dentry);
2881out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002882 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883out_release:
Jan Blunck1d957f92008-02-14 19:34:35 -08002884 path_put(&nd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04002885 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886out:
Al Viro2d8f3032008-07-22 09:59:21 -04002887 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888
2889 return error;
2890}
2891
Heiko Carstens3480b252009-01-14 14:14:16 +01002892SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002893{
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002894 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002895}
2896
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897/*
2898 * The worst of all namespace operations - renaming directory. "Perverted"
2899 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2900 * Problems:
2901 * a) we can get into loop creation. Check is done in is_subdir().
2902 * b) race potential - two innocent renames can create a loop together.
2903 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002904 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 * story.
2906 * c) we have to lock _three_ objects - parents and victim (if it exists).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002907 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 * whether the target exists). Solution: try to be smart with locking
2909 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002910 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 * move will be locked. Thus we can rank directories by the tree
2912 * (ancestors first) and rank all non-directories after them.
2913 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002914 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 * HOWEVER, it relies on the assumption that any object with ->lookup()
2916 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2917 * we'd better make sure that there's no link(2) for them.
Sage Weile4eaac02011-05-24 13:06:07 -07002918 * d) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002919 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05002921 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 * locking].
2923 */
Adrian Bunk75c96f82005-05-05 16:16:09 -07002924static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
2925 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926{
2927 int error = 0;
Sage Weil9055cba2011-05-24 13:06:12 -07002928 struct inode *target = new_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929
2930 /*
2931 * If we are going to change the parent - check write permissions,
2932 * we'll need to flip '..'.
2933 */
2934 if (new_dir != old_dir) {
Al Virof419a2e2008-07-22 00:07:17 -04002935 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 if (error)
2937 return error;
2938 }
2939
2940 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2941 if (error)
2942 return error;
2943
Al Virod83c49f2010-04-30 17:17:09 -04002944 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002945 mutex_lock(&target->i_mutex);
Sage Weil9055cba2011-05-24 13:06:12 -07002946
2947 error = -EBUSY;
2948 if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
2949 goto out;
2950
Sage Weil3cebde22011-05-29 21:20:59 -07002951 if (target)
2952 shrink_dcache_parent(new_dentry);
Sage Weil9055cba2011-05-24 13:06:12 -07002953 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2954 if (error)
2955 goto out;
2956
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 if (target) {
Sage Weil9055cba2011-05-24 13:06:12 -07002958 target->i_flags |= S_DEAD;
2959 dont_mount(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 }
Sage Weil9055cba2011-05-24 13:06:12 -07002961out:
2962 if (target)
2963 mutex_unlock(&target->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07002964 if (!error)
Mark Fasheh349457c2006-09-08 14:22:21 -07002965 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
2966 d_move(old_dentry,new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 return error;
2968}
2969
Adrian Bunk75c96f82005-05-05 16:16:09 -07002970static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
2971 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972{
Sage Weil51892bb2011-05-24 13:06:13 -07002973 struct inode *target = new_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 int error;
2975
2976 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2977 if (error)
2978 return error;
2979
2980 dget(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002982 mutex_lock(&target->i_mutex);
Sage Weil51892bb2011-05-24 13:06:13 -07002983
2984 error = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
Sage Weil51892bb2011-05-24 13:06:13 -07002986 goto out;
2987
2988 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2989 if (error)
2990 goto out;
2991
2992 if (target)
2993 dont_mount(new_dentry);
2994 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
2995 d_move(old_dentry, new_dentry);
2996out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002998 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 dput(new_dentry);
3000 return error;
3001}
3002
3003int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
3004 struct inode *new_dir, struct dentry *new_dentry)
3005{
3006 int error;
3007 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
Eric Paris59b0df22010-02-08 12:53:52 -05003008 const unsigned char *old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
3010 if (old_dentry->d_inode == new_dentry->d_inode)
3011 return 0;
3012
3013 error = may_delete(old_dir, old_dentry, is_dir);
3014 if (error)
3015 return error;
3016
3017 if (!new_dentry->d_inode)
Miklos Szeredia95164d2008-07-30 15:08:48 +02003018 error = may_create(new_dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 else
3020 error = may_delete(new_dir, new_dentry, is_dir);
3021 if (error)
3022 return error;
3023
Al Viroacfa4382008-12-04 10:06:33 -05003024 if (!old_dir->i_op->rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 return -EPERM;
3026
Robert Love0eeca282005-07-12 17:06:03 -04003027 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
3028
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 if (is_dir)
3030 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
3031 else
3032 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
Al Viro123df292009-12-25 04:57:57 -05003033 if (!error)
3034 fsnotify_move(old_dir, new_dir, old_name, is_dir,
Al Viro5a190ae2007-06-07 12:19:32 -04003035 new_dentry->d_inode, old_dentry);
Robert Love0eeca282005-07-12 17:06:03 -04003036 fsnotify_oldname_free(old_name);
3037
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 return error;
3039}
3040
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003041SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
3042 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043{
Al Viro2ad94ae2008-07-21 09:32:51 -04003044 struct dentry *old_dir, *new_dir;
3045 struct dentry *old_dentry, *new_dentry;
3046 struct dentry *trap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 struct nameidata oldnd, newnd;
Al Viro2ad94ae2008-07-21 09:32:51 -04003048 char *from;
3049 char *to;
3050 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051
Al Viro2ad94ae2008-07-21 09:32:51 -04003052 error = user_path_parent(olddfd, oldname, &oldnd, &from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 if (error)
3054 goto exit;
3055
Al Viro2ad94ae2008-07-21 09:32:51 -04003056 error = user_path_parent(newdfd, newname, &newnd, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 if (error)
3058 goto exit1;
3059
3060 error = -EXDEV;
Jan Blunck4ac91372008-02-14 19:34:32 -08003061 if (oldnd.path.mnt != newnd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 goto exit2;
3063
Jan Blunck4ac91372008-02-14 19:34:32 -08003064 old_dir = oldnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 error = -EBUSY;
3066 if (oldnd.last_type != LAST_NORM)
3067 goto exit2;
3068
Jan Blunck4ac91372008-02-14 19:34:32 -08003069 new_dir = newnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 if (newnd.last_type != LAST_NORM)
3071 goto exit2;
3072
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003073 oldnd.flags &= ~LOOKUP_PARENT;
3074 newnd.flags &= ~LOOKUP_PARENT;
OGAWA Hirofumi4e9ed2f2008-10-16 07:50:29 +09003075 newnd.flags |= LOOKUP_RENAME_TARGET;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003076
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 trap = lock_rename(new_dir, old_dir);
3078
Christoph Hellwig49705b72005-11-08 21:35:06 -08003079 old_dentry = lookup_hash(&oldnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 error = PTR_ERR(old_dentry);
3081 if (IS_ERR(old_dentry))
3082 goto exit3;
3083 /* source must exist */
3084 error = -ENOENT;
3085 if (!old_dentry->d_inode)
3086 goto exit4;
3087 /* unless the source is a directory trailing slashes give -ENOTDIR */
3088 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
3089 error = -ENOTDIR;
3090 if (oldnd.last.name[oldnd.last.len])
3091 goto exit4;
3092 if (newnd.last.name[newnd.last.len])
3093 goto exit4;
3094 }
3095 /* source should not be ancestor of target */
3096 error = -EINVAL;
3097 if (old_dentry == trap)
3098 goto exit4;
Christoph Hellwig49705b72005-11-08 21:35:06 -08003099 new_dentry = lookup_hash(&newnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 error = PTR_ERR(new_dentry);
3101 if (IS_ERR(new_dentry))
3102 goto exit4;
3103 /* target should not be an ancestor of source */
3104 error = -ENOTEMPTY;
3105 if (new_dentry == trap)
3106 goto exit5;
3107
Dave Hansen9079b1e2008-02-15 14:37:49 -08003108 error = mnt_want_write(oldnd.path.mnt);
3109 if (error)
3110 goto exit5;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003111 error = security_path_rename(&oldnd.path, old_dentry,
3112 &newnd.path, new_dentry);
3113 if (error)
3114 goto exit6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 error = vfs_rename(old_dir->d_inode, old_dentry,
3116 new_dir->d_inode, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003117exit6:
Dave Hansen9079b1e2008-02-15 14:37:49 -08003118 mnt_drop_write(oldnd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119exit5:
3120 dput(new_dentry);
3121exit4:
3122 dput(old_dentry);
3123exit3:
3124 unlock_rename(new_dir, old_dir);
3125exit2:
Jan Blunck1d957f92008-02-14 19:34:35 -08003126 path_put(&newnd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04003127 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003129 path_put(&oldnd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 putname(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04003131exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 return error;
3133}
3134
Heiko Carstensa26eab22009-01-14 14:14:17 +01003135SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003136{
3137 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
3138}
3139
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
3141{
3142 int len;
3143
3144 len = PTR_ERR(link);
3145 if (IS_ERR(link))
3146 goto out;
3147
3148 len = strlen(link);
3149 if (len > (unsigned) buflen)
3150 len = buflen;
3151 if (copy_to_user(buffer, link, len))
3152 len = -EFAULT;
3153out:
3154 return len;
3155}
3156
3157/*
3158 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
3159 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
3160 * using) it for any given inode is up to filesystem.
3161 */
3162int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3163{
3164 struct nameidata nd;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003165 void *cookie;
Marcin Slusarz694a1762008-06-09 16:40:37 -07003166 int res;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003167
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 nd.depth = 0;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003169 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
Marcin Slusarz694a1762008-06-09 16:40:37 -07003170 if (IS_ERR(cookie))
3171 return PTR_ERR(cookie);
3172
3173 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
3174 if (dentry->d_inode->i_op->put_link)
3175 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3176 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177}
3178
3179int vfs_follow_link(struct nameidata *nd, const char *link)
3180{
3181 return __vfs_follow_link(nd, link);
3182}
3183
3184/* get the link contents into pagecache */
3185static char *page_getlink(struct dentry * dentry, struct page **ppage)
3186{
Duane Griffinebd09ab2008-12-19 20:47:12 +00003187 char *kaddr;
3188 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 struct address_space *mapping = dentry->d_inode->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -07003190 page = read_mapping_page(mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 if (IS_ERR(page))
Nick Piggin6fe69002007-05-06 14:49:04 -07003192 return (char*)page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 *ppage = page;
Duane Griffinebd09ab2008-12-19 20:47:12 +00003194 kaddr = kmap(page);
3195 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3196 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197}
3198
3199int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3200{
3201 struct page *page = NULL;
3202 char *s = page_getlink(dentry, &page);
3203 int res = vfs_readlink(dentry,buffer,buflen,s);
3204 if (page) {
3205 kunmap(page);
3206 page_cache_release(page);
3207 }
3208 return res;
3209}
3210
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003211void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003213 struct page *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 nd_set_link(nd, page_getlink(dentry, &page));
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003215 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216}
3217
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003218void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003220 struct page *page = cookie;
3221
3222 if (page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 kunmap(page);
3224 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 }
3226}
3227
Nick Piggin54566b22009-01-04 12:00:53 -08003228/*
3229 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
3230 */
3231int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232{
3233 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003234 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07003235 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08003236 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 char *kaddr;
Nick Piggin54566b22009-01-04 12:00:53 -08003238 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
3239 if (nofs)
3240 flags |= AOP_FLAG_NOFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241
NeilBrown7e53cac2006-03-25 03:07:57 -08003242retry:
Nick Pigginafddba42007-10-16 01:25:01 -07003243 err = pagecache_write_begin(NULL, mapping, 0, len-1,
Nick Piggin54566b22009-01-04 12:00:53 -08003244 flags, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07003246 goto fail;
3247
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 kaddr = kmap_atomic(page, KM_USER0);
3249 memcpy(kaddr, symname, len-1);
3250 kunmap_atomic(kaddr, KM_USER0);
Nick Pigginafddba42007-10-16 01:25:01 -07003251
3252 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3253 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 if (err < 0)
3255 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07003256 if (err < len-1)
3257 goto retry;
3258
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 mark_inode_dirty(inode);
3260 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261fail:
3262 return err;
3263}
3264
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003265int page_symlink(struct inode *inode, const char *symname, int len)
3266{
3267 return __page_symlink(inode, symname, len,
Nick Piggin54566b22009-01-04 12:00:53 -08003268 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003269}
3270
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003271const struct inode_operations page_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 .readlink = generic_readlink,
3273 .follow_link = page_follow_link_light,
3274 .put_link = page_put_link,
3275};
3276
Al Viro2d8f3032008-07-22 09:59:21 -04003277EXPORT_SYMBOL(user_path_at);
David Howellscc53ce52011-01-14 18:45:26 +00003278EXPORT_SYMBOL(follow_down_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279EXPORT_SYMBOL(follow_down);
3280EXPORT_SYMBOL(follow_up);
3281EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
3282EXPORT_SYMBOL(getname);
3283EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284EXPORT_SYMBOL(lookup_one_len);
3285EXPORT_SYMBOL(page_follow_link_light);
3286EXPORT_SYMBOL(page_put_link);
3287EXPORT_SYMBOL(page_readlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003288EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289EXPORT_SYMBOL(page_symlink);
3290EXPORT_SYMBOL(page_symlink_inode_operations);
Al Viroc9c6cac2011-02-16 15:15:47 -05003291EXPORT_SYMBOL(kern_path_parent);
Al Virod1811462008-08-02 00:49:18 -04003292EXPORT_SYMBOL(kern_path);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07003293EXPORT_SYMBOL(vfs_path_lookup);
Al Virof419a2e2008-07-22 00:07:17 -04003294EXPORT_SYMBOL(inode_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295EXPORT_SYMBOL(unlock_rename);
3296EXPORT_SYMBOL(vfs_create);
3297EXPORT_SYMBOL(vfs_follow_link);
3298EXPORT_SYMBOL(vfs_link);
3299EXPORT_SYMBOL(vfs_mkdir);
3300EXPORT_SYMBOL(vfs_mknod);
3301EXPORT_SYMBOL(generic_permission);
3302EXPORT_SYMBOL(vfs_readlink);
3303EXPORT_SYMBOL(vfs_rename);
3304EXPORT_SYMBOL(vfs_rmdir);
3305EXPORT_SYMBOL(vfs_symlink);
3306EXPORT_SYMBOL(vfs_unlink);
3307EXPORT_SYMBOL(dentry_unhash);
3308EXPORT_SYMBOL(generic_readlink);