blob: ec2e5656b444038e57e37e44ad491b8511c7df19 [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 Torvaldse77819e2011-07-22 19:30:19 -070035#include <linux/posix_acl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/uaccess.h>
37
Eric Parise81e3f42009-12-04 15:47:36 -050038#include "internal.h"
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040/* [Feb-1997 T. Schoebel-Theuer]
41 * Fundamental changes in the pathname lookup mechanisms (namei)
42 * were necessary because of omirr. The reason is that omirr needs
43 * to know the _real_ pathname, not the user-supplied one, in case
44 * of symlinks (and also when transname replacements occur).
45 *
46 * The new code replaces the old recursive symlink resolution with
47 * an iterative one (in case of non-nested symlink chains). It does
48 * this with calls to <fs>_follow_link().
49 * As a side effect, dir_namei(), _namei() and follow_link() are now
50 * replaced with a single function lookup_dentry() that can handle all
51 * the special cases of the former code.
52 *
53 * With the new dcache, the pathname is stored at each inode, at least as
54 * long as the refcount of the inode is positive. As a side effect, the
55 * size of the dcache depends on the inode cache and thus is dynamic.
56 *
57 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
58 * resolution to correspond with current state of the code.
59 *
60 * Note that the symlink resolution is not *completely* iterative.
61 * There is still a significant amount of tail- and mid- recursion in
62 * the algorithm. Also, note that <fs>_readlink() is not used in
63 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
64 * may return different results than <fs>_follow_link(). Many virtual
65 * filesystems (including /proc) exhibit this behavior.
66 */
67
68/* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
69 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
70 * and the name already exists in form of a symlink, try to create the new
71 * name indicated by the symlink. The old code always complained that the
72 * name already exists, due to not following the symlink even if its target
73 * is nonexistent. The new semantics affects also mknod() and link() when
Lucas De Marchi25985ed2011-03-30 22:57:33 -030074 * the name is a symlink pointing to a non-existent name.
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 *
76 * I don't know which semantics is the right one, since I have no access
77 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
78 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
79 * "old" one. Personally, I think the new semantics is much more logical.
80 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
81 * file does succeed in both HP-UX and SunOs, but not in Solaris
82 * and in the old Linux semantics.
83 */
84
85/* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
86 * semantics. See the comments in "open_namei" and "do_link" below.
87 *
88 * [10-Sep-98 Alan Modra] Another symlink change.
89 */
90
91/* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
92 * inside the path - always follow.
93 * in the last component in creation/removal/renaming - never follow.
94 * if LOOKUP_FOLLOW passed - follow.
95 * if the pathname has trailing slashes - follow.
96 * otherwise - don't follow.
97 * (applied in that order).
98 *
99 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
100 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
101 * During the 2.4 we need to fix the userland stuff depending on it -
102 * hopefully we will be able to get rid of that wart in 2.5. So far only
103 * XEmacs seems to be relying on it...
104 */
105/*
106 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
Arjan van de Vena11f3a02006-03-23 03:00:33 -0800107 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 * any extra contention...
109 */
110
111/* In order to reduce some races, while at the same time doing additional
112 * checking and hopefully speeding things up, we copy filenames to the
113 * kernel data space before using them..
114 *
115 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
116 * PATH_MAX includes the nul terminator --RR.
117 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800118static int do_getname(const char __user *filename, char *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
120 int retval;
121 unsigned long len = PATH_MAX;
122
123 if (!segment_eq(get_fs(), KERNEL_DS)) {
124 if ((unsigned long) filename >= TASK_SIZE)
125 return -EFAULT;
126 if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
127 len = TASK_SIZE - (unsigned long) filename;
128 }
129
130 retval = strncpy_from_user(page, filename, len);
131 if (retval > 0) {
132 if (retval < len)
133 return 0;
134 return -ENAMETOOLONG;
135 } else if (!retval)
136 retval = -ENOENT;
137 return retval;
138}
139
Al Virof52e0c12011-03-14 18:56:51 -0400140static char *getname_flags(const char __user * filename, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 char *tmp, *result;
143
144 result = ERR_PTR(-ENOMEM);
145 tmp = __getname();
146 if (tmp) {
147 int retval = do_getname(filename, tmp);
148
149 result = tmp;
150 if (retval < 0) {
Al Virof52e0c12011-03-14 18:56:51 -0400151 if (retval != -ENOENT || !(flags & LOOKUP_EMPTY)) {
152 __putname(tmp);
153 result = ERR_PTR(retval);
154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 }
156 }
157 audit_getname(result);
158 return result;
159}
160
Al Virof52e0c12011-03-14 18:56:51 -0400161char *getname(const char __user * filename)
162{
163 return getname_flags(filename, 0);
164}
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166#ifdef CONFIG_AUDITSYSCALL
167void putname(const char *name)
168{
Al Viro5ac3a9c2006-07-16 06:38:45 -0400169 if (unlikely(!audit_dummy_context()))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 audit_putname(name);
171 else
172 __putname(name);
173}
174EXPORT_SYMBOL(putname);
175#endif
176
Linus Torvaldse77819e2011-07-22 19:30:19 -0700177static int check_acl(struct inode *inode, int mask)
178{
179 struct posix_acl *acl;
180
181 /*
182 * Under RCU walk, we cannot even do a "get_cached_acl()",
183 * because that involves locking and getting a refcount on
184 * a cached ACL.
185 *
186 * So the only case we handle during RCU walking is the
187 * case of a cached "no ACL at all", which needs no locks
188 * or refcounts.
189 */
190 if (mask & MAY_NOT_BLOCK) {
191 if (negative_cached_acl(inode, ACL_TYPE_ACCESS))
192 return -EAGAIN;
193 return -ECHILD;
194 }
195
196 acl = get_cached_acl(inode, ACL_TYPE_ACCESS);
197
198 /*
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200199 * A filesystem can force a ACL callback by just never filling the
200 * ACL cache. But normally you'd fill the cache either at inode
201 * instantiation time, or on the first ->get_acl call.
Linus Torvaldse77819e2011-07-22 19:30:19 -0700202 *
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200203 * If the filesystem doesn't have a get_acl() function at all, we'll
204 * just create the negative cache entry.
Linus Torvaldse77819e2011-07-22 19:30:19 -0700205 */
206 if (acl == ACL_NOT_CACHED) {
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200207 if (inode->i_op->get_acl) {
208 acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS);
209 if (IS_ERR(acl))
210 return PTR_ERR(acl);
211 } else {
212 set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
213 return -EAGAIN;
214 }
Linus Torvaldse77819e2011-07-22 19:30:19 -0700215 }
216
217 if (acl) {
218 int error = posix_acl_permission(inode, acl, mask);
219 posix_acl_release(acl);
220 return error;
221 }
222
223 return -EAGAIN;
224}
225
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700226/*
227 * This does basic POSIX ACL permission checking
228 */
Al Viro7e401452011-06-20 19:12:17 -0400229static int acl_permission_check(struct inode *inode, int mask)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700230{
Linus Torvalds26cf46b2011-05-13 11:51:01 -0700231 unsigned int mode = inode->i_mode;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700232
Al Viro9c2c7032011-06-20 19:06:22 -0400233 mask &= MAY_READ | MAY_WRITE | MAY_EXEC | MAY_NOT_BLOCK;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700234
Serge E. Hallyne795b712011-03-23 16:43:25 -0700235 if (current_user_ns() != inode_userns(inode))
236 goto other_perms;
237
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700238 if (current_fsuid() == inode->i_uid)
239 mode >>= 6;
240 else {
Linus Torvaldse77819e2011-07-22 19:30:19 -0700241 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
Al Viro7e401452011-06-20 19:12:17 -0400242 int error = check_acl(inode, mask);
Nick Pigginb74c79e2011-01-07 17:49:58 +1100243 if (error != -EAGAIN)
244 return error;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700245 }
246
247 if (in_group_p(inode->i_gid))
248 mode >>= 3;
249 }
250
Serge E. Hallyne795b712011-03-23 16:43:25 -0700251other_perms:
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700252 /*
253 * If the DACs are ok we don't need any capability check.
254 */
Al Viro9c2c7032011-06-20 19:06:22 -0400255 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700256 return 0;
257 return -EACCES;
258}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260/**
Nick Pigginb74c79e2011-01-07 17:49:58 +1100261 * generic_permission - check for access rights on a Posix-like filesystem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 * @inode: inode to check access rights for
263 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 *
265 * Used to check for read/write/execute permissions on a file.
266 * We use "fsuid" for this, letting us set arbitrary permissions
267 * for filesystem access without changing the "normal" uids which
Nick Pigginb74c79e2011-01-07 17:49:58 +1100268 * are used for other things.
269 *
270 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
271 * request cannot be satisfied (eg. requires blocking or too much complexity).
272 * It would then be called again in ref-walk mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 */
Al Viro2830ba72011-06-20 19:16:29 -0400274int generic_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700276 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 /*
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700279 * Do the basic POSIX ACL permission checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 */
Al Viro7e401452011-06-20 19:12:17 -0400281 ret = acl_permission_check(inode, mask);
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700282 if (ret != -EACCES)
283 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Al Virod594e7e2011-06-20 19:55:42 -0400285 if (S_ISDIR(inode->i_mode)) {
286 /* DACs are overridable for directories */
287 if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
288 return 0;
289 if (!(mask & MAY_WRITE))
290 if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
291 return 0;
292 return -EACCES;
293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 /*
295 * Read/write DACs are always overridable.
Al Virod594e7e2011-06-20 19:55:42 -0400296 * Executable DACs are overridable when there is
297 * at least one exec bit set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 */
Al Virod594e7e2011-06-20 19:55:42 -0400299 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
Serge E. Hallyne795b712011-03-23 16:43:25 -0700300 if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 return 0;
302
303 /*
304 * Searching includes executable on directories, else just read.
305 */
Serge E. Hallyn7ea66002009-12-29 14:50:19 -0600306 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
Al Virod594e7e2011-06-20 19:55:42 -0400307 if (mask == MAY_READ)
Serge E. Hallyne795b712011-03-23 16:43:25 -0700308 if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 return 0;
310
311 return -EACCES;
312}
313
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200314/**
315 * inode_permission - check for access rights to a given inode
316 * @inode: inode to check permission on
317 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
318 *
319 * Used to check for read/write/execute permissions on an inode.
320 * We use "fsuid" for this, letting us set arbitrary permissions
321 * for filesystem access without changing the "normal" uids which
322 * are used for other things.
323 */
Al Virof419a2e2008-07-22 00:07:17 -0400324int inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Al Viroe6305c42008-07-15 21:03:57 -0400326 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 if (mask & MAY_WRITE) {
Miklos Szeredi22590e42007-10-16 23:27:08 -0700329 umode_t mode = inode->i_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 /*
332 * Nobody gets write access to a read-only fs.
333 */
334 if (IS_RDONLY(inode) &&
335 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
336 return -EROFS;
337
338 /*
339 * Nobody gets write access to an immutable file.
340 */
341 if (IS_IMMUTABLE(inode))
342 return -EACCES;
343 }
344
Al Viroacfa4382008-12-04 10:06:33 -0500345 if (inode->i_op->permission)
Al Viro10556cb2011-06-20 19:28:19 -0400346 retval = inode->i_op->permission(inode, mask);
Miklos Szeredif696a362008-07-31 13:41:58 +0200347 else
Al Viro2830ba72011-06-20 19:16:29 -0400348 retval = generic_permission(inode, mask);
Miklos Szeredif696a362008-07-31 13:41:58 +0200349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if (retval)
351 return retval;
352
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700353 retval = devcgroup_inode_permission(inode, mask);
354 if (retval)
355 return retval;
356
Eric Parisd09ca732010-07-23 11:43:57 -0400357 return security_inode_permission(inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
Al Virof4d6ff82011-06-19 13:14:21 -0400360/**
Jan Blunck5dd784d02008-02-14 19:34:38 -0800361 * path_get - get a reference to a path
362 * @path: path to get the reference to
363 *
364 * Given a path increment the reference count to the dentry and the vfsmount.
365 */
366void path_get(struct path *path)
367{
368 mntget(path->mnt);
369 dget(path->dentry);
370}
371EXPORT_SYMBOL(path_get);
372
373/**
Jan Blunck1d957f92008-02-14 19:34:35 -0800374 * path_put - put a reference to a path
375 * @path: path to put the reference to
376 *
377 * Given a path decrement the reference count to the dentry and the vfsmount.
378 */
379void path_put(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Jan Blunck1d957f92008-02-14 19:34:35 -0800381 dput(path->dentry);
382 mntput(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
Jan Blunck1d957f92008-02-14 19:34:35 -0800384EXPORT_SYMBOL(path_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Al Viro19660af2011-03-25 10:32:48 -0400386/*
Nick Piggin31e6b012011-01-07 17:49:52 +1100387 * Path walking has 2 modes, rcu-walk and ref-walk (see
Al Viro19660af2011-03-25 10:32:48 -0400388 * Documentation/filesystems/path-lookup.txt). In situations when we can't
389 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
390 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
391 * mode. Refcounts are grabbed at the last known good point before rcu-walk
392 * got stuck, so ref-walk may continue from there. If this is not successful
393 * (eg. a seqcount has changed), then failure is returned and it's up to caller
394 * to restart the path walk from the beginning in ref-walk mode.
Nick Piggin31e6b012011-01-07 17:49:52 +1100395 */
Nick Piggin31e6b012011-01-07 17:49:52 +1100396
397/**
Al Viro19660af2011-03-25 10:32:48 -0400398 * unlazy_walk - try to switch to ref-walk mode.
399 * @nd: nameidata pathwalk data
400 * @dentry: child of nd->path.dentry or NULL
Randy Dunlap39191622011-01-08 19:36:21 -0800401 * Returns: 0 on success, -ECHILD on failure
Nick Piggin31e6b012011-01-07 17:49:52 +1100402 *
Al Viro19660af2011-03-25 10:32:48 -0400403 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
404 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
405 * @nd or NULL. Must be called from rcu-walk context.
Nick Piggin31e6b012011-01-07 17:49:52 +1100406 */
Al Viro19660af2011-03-25 10:32:48 -0400407static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
Nick Piggin31e6b012011-01-07 17:49:52 +1100408{
409 struct fs_struct *fs = current->fs;
410 struct dentry *parent = nd->path.dentry;
Al Viro5b6ca022011-03-09 23:04:47 -0500411 int want_root = 0;
Nick Piggin31e6b012011-01-07 17:49:52 +1100412
413 BUG_ON(!(nd->flags & LOOKUP_RCU));
Al Viro5b6ca022011-03-09 23:04:47 -0500414 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
415 want_root = 1;
Nick Piggin31e6b012011-01-07 17:49:52 +1100416 spin_lock(&fs->lock);
417 if (nd->root.mnt != fs->root.mnt ||
418 nd->root.dentry != fs->root.dentry)
419 goto err_root;
420 }
421 spin_lock(&parent->d_lock);
Al Viro19660af2011-03-25 10:32:48 -0400422 if (!dentry) {
423 if (!__d_rcu_to_refcount(parent, nd->seq))
424 goto err_parent;
425 BUG_ON(nd->inode != parent->d_inode);
426 } else {
Al Viro94c0d4e2011-07-12 21:40:23 -0400427 if (dentry->d_parent != parent)
428 goto err_parent;
Al Viro19660af2011-03-25 10:32:48 -0400429 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
430 if (!__d_rcu_to_refcount(dentry, nd->seq))
431 goto err_child;
432 /*
433 * If the sequence check on the child dentry passed, then
434 * the child has not been removed from its parent. This
435 * means the parent dentry must be valid and able to take
436 * a reference at this point.
437 */
438 BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
439 BUG_ON(!parent->d_count);
440 parent->d_count++;
441 spin_unlock(&dentry->d_lock);
442 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100443 spin_unlock(&parent->d_lock);
Al Viro5b6ca022011-03-09 23:04:47 -0500444 if (want_root) {
Nick Piggin31e6b012011-01-07 17:49:52 +1100445 path_get(&nd->root);
446 spin_unlock(&fs->lock);
447 }
448 mntget(nd->path.mnt);
449
450 rcu_read_unlock();
451 br_read_unlock(vfsmount_lock);
452 nd->flags &= ~LOOKUP_RCU;
453 return 0;
Al Viro19660af2011-03-25 10:32:48 -0400454
455err_child:
Nick Piggin31e6b012011-01-07 17:49:52 +1100456 spin_unlock(&dentry->d_lock);
Al Viro19660af2011-03-25 10:32:48 -0400457err_parent:
Nick Piggin31e6b012011-01-07 17:49:52 +1100458 spin_unlock(&parent->d_lock);
459err_root:
Al Viro5b6ca022011-03-09 23:04:47 -0500460 if (want_root)
Nick Piggin31e6b012011-01-07 17:49:52 +1100461 spin_unlock(&fs->lock);
462 return -ECHILD;
463}
464
Nick Piggin31e6b012011-01-07 17:49:52 +1100465/**
Trond Myklebust834f2a42005-10-18 14:20:16 -0700466 * release_open_intent - free up open intent resources
467 * @nd: pointer to nameidata
468 */
469void release_open_intent(struct nameidata *nd)
470{
Linus Torvalds2dab5972011-02-11 15:53:38 -0800471 struct file *file = nd->intent.open.file;
472
473 if (file && !IS_ERR(file)) {
474 if (file->f_path.dentry == NULL)
475 put_filp(file);
476 else
477 fput(file);
478 }
Trond Myklebust834f2a42005-10-18 14:20:16 -0700479}
480
Al Virof60aef72011-02-15 01:35:28 -0500481static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
Nick Piggin34286d62011-01-07 17:49:57 +1100482{
Al Virof60aef72011-02-15 01:35:28 -0500483 return dentry->d_op->d_revalidate(dentry, nd);
Nick Piggin34286d62011-01-07 17:49:57 +1100484}
485
Al Viro9f1fafe2011-03-25 11:00:12 -0400486/**
487 * complete_walk - successful completion of path walk
488 * @nd: pointer nameidata
Jeff Layton39159de2009-12-07 12:01:50 -0500489 *
Al Viro9f1fafe2011-03-25 11:00:12 -0400490 * If we had been in RCU mode, drop out of it and legitimize nd->path.
491 * Revalidate the final result, unless we'd already done that during
492 * the path walk or the filesystem doesn't ask for it. Return 0 on
493 * success, -error on failure. In case of failure caller does not
494 * need to drop nd->path.
Jeff Layton39159de2009-12-07 12:01:50 -0500495 */
Al Viro9f1fafe2011-03-25 11:00:12 -0400496static int complete_walk(struct nameidata *nd)
Jeff Layton39159de2009-12-07 12:01:50 -0500497{
Al Viro16c2cd72011-02-22 15:50:10 -0500498 struct dentry *dentry = nd->path.dentry;
Jeff Layton39159de2009-12-07 12:01:50 -0500499 int status;
Jeff Layton39159de2009-12-07 12:01:50 -0500500
Al Viro9f1fafe2011-03-25 11:00:12 -0400501 if (nd->flags & LOOKUP_RCU) {
502 nd->flags &= ~LOOKUP_RCU;
503 if (!(nd->flags & LOOKUP_ROOT))
504 nd->root.mnt = NULL;
505 spin_lock(&dentry->d_lock);
506 if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
507 spin_unlock(&dentry->d_lock);
508 rcu_read_unlock();
509 br_read_unlock(vfsmount_lock);
510 return -ECHILD;
511 }
512 BUG_ON(nd->inode != dentry->d_inode);
513 spin_unlock(&dentry->d_lock);
514 mntget(nd->path.mnt);
515 rcu_read_unlock();
516 br_read_unlock(vfsmount_lock);
517 }
518
Al Viro16c2cd72011-02-22 15:50:10 -0500519 if (likely(!(nd->flags & LOOKUP_JUMPED)))
Jeff Layton39159de2009-12-07 12:01:50 -0500520 return 0;
521
Al Viro16c2cd72011-02-22 15:50:10 -0500522 if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
523 return 0;
524
525 if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
526 return 0;
527
528 /* Note: we do not d_invalidate() */
Nick Piggin34286d62011-01-07 17:49:57 +1100529 status = d_revalidate(dentry, nd);
Jeff Layton39159de2009-12-07 12:01:50 -0500530 if (status > 0)
531 return 0;
532
Al Viro16c2cd72011-02-22 15:50:10 -0500533 if (!status)
Jeff Layton39159de2009-12-07 12:01:50 -0500534 status = -ESTALE;
Al Viro16c2cd72011-02-22 15:50:10 -0500535
Al Viro9f1fafe2011-03-25 11:00:12 -0400536 path_put(&nd->path);
Jeff Layton39159de2009-12-07 12:01:50 -0500537 return status;
538}
539
Al Viro2a737872009-04-07 11:49:53 -0400540static __always_inline void set_root(struct nameidata *nd)
541{
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200542 if (!nd->root.mnt)
543 get_fs_root(current->fs, &nd->root);
Al Viro2a737872009-04-07 11:49:53 -0400544}
545
Al Viro6de88d72009-08-09 01:41:57 +0400546static int link_path_walk(const char *, struct nameidata *);
547
Nick Piggin31e6b012011-01-07 17:49:52 +1100548static __always_inline void set_root_rcu(struct nameidata *nd)
549{
550 if (!nd->root.mnt) {
551 struct fs_struct *fs = current->fs;
Nick Pigginc28cc362011-01-07 17:49:53 +1100552 unsigned seq;
553
554 do {
555 seq = read_seqcount_begin(&fs->seq);
556 nd->root = fs->root;
Tim Chenc1530012011-04-15 11:39:29 -0700557 nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
Nick Pigginc28cc362011-01-07 17:49:53 +1100558 } while (read_seqcount_retry(&fs->seq, seq));
Nick Piggin31e6b012011-01-07 17:49:52 +1100559 }
560}
561
Arjan van de Venf1662352006-01-14 13:21:31 -0800562static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Nick Piggin31e6b012011-01-07 17:49:52 +1100564 int ret;
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (IS_ERR(link))
567 goto fail;
568
569 if (*link == '/') {
Al Viro2a737872009-04-07 11:49:53 -0400570 set_root(nd);
Jan Blunck1d957f92008-02-14 19:34:35 -0800571 path_put(&nd->path);
Al Viro2a737872009-04-07 11:49:53 -0400572 nd->path = nd->root;
573 path_get(&nd->root);
Al Viro16c2cd72011-02-22 15:50:10 -0500574 nd->flags |= LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100576 nd->inode = nd->path.dentry->d_inode;
Christoph Hellwigb4091d52008-11-05 15:07:21 +0100577
Nick Piggin31e6b012011-01-07 17:49:52 +1100578 ret = link_path_walk(link, nd);
579 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580fail:
Jan Blunck1d957f92008-02-14 19:34:35 -0800581 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 return PTR_ERR(link);
583}
584
Jan Blunck1d957f92008-02-14 19:34:35 -0800585static void path_put_conditional(struct path *path, struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700586{
587 dput(path->dentry);
Jan Blunck4ac91372008-02-14 19:34:32 -0800588 if (path->mnt != nd->path.mnt)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700589 mntput(path->mnt);
590}
591
Nick Piggin7b9337a2011-01-14 08:42:43 +0000592static inline void path_to_nameidata(const struct path *path,
593 struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700594{
Nick Piggin31e6b012011-01-07 17:49:52 +1100595 if (!(nd->flags & LOOKUP_RCU)) {
596 dput(nd->path.dentry);
597 if (nd->path.mnt != path->mnt)
598 mntput(nd->path.mnt);
Huang Shijie9a229682010-04-02 17:37:13 +0800599 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100600 nd->path.mnt = path->mnt;
Jan Blunck4ac91372008-02-14 19:34:32 -0800601 nd->path.dentry = path->dentry;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700602}
603
Al Viro574197e2011-03-14 22:20:34 -0400604static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
605{
606 struct inode *inode = link->dentry->d_inode;
607 if (!IS_ERR(cookie) && inode->i_op->put_link)
608 inode->i_op->put_link(link->dentry, nd, cookie);
609 path_put(link);
610}
611
Al Virodef4af32009-12-26 08:37:05 -0500612static __always_inline int
Al Viro574197e2011-03-14 22:20:34 -0400613follow_link(struct path *link, struct nameidata *nd, void **p)
Ian Kent051d3812006-03-27 01:14:53 -0800614{
615 int error;
Nick Piggin7b9337a2011-01-14 08:42:43 +0000616 struct dentry *dentry = link->dentry;
Ian Kent051d3812006-03-27 01:14:53 -0800617
Al Viro844a3912011-02-15 00:38:26 -0500618 BUG_ON(nd->flags & LOOKUP_RCU);
619
Al Viro0e794582011-03-16 02:45:02 -0400620 if (link->mnt == nd->path.mnt)
621 mntget(link->mnt);
622
Al Viro574197e2011-03-14 22:20:34 -0400623 if (unlikely(current->total_link_count >= 40)) {
624 *p = ERR_PTR(-ELOOP); /* no ->put_link(), please */
Al Viro574197e2011-03-14 22:20:34 -0400625 path_put(&nd->path);
626 return -ELOOP;
627 }
628 cond_resched();
629 current->total_link_count++;
630
Nick Piggin7b9337a2011-01-14 08:42:43 +0000631 touch_atime(link->mnt, dentry);
Ian Kent051d3812006-03-27 01:14:53 -0800632 nd_set_link(nd, NULL);
633
Al Viro36f3b4f2011-02-22 21:24:38 -0500634 error = security_inode_follow_link(link->dentry, nd);
635 if (error) {
636 *p = ERR_PTR(error); /* no ->put_link(), please */
637 path_put(&nd->path);
638 return error;
639 }
640
Al Viro86acdca12009-12-22 23:45:11 -0500641 nd->last_type = LAST_BIND;
Al Virodef4af32009-12-26 08:37:05 -0500642 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
643 error = PTR_ERR(*p);
644 if (!IS_ERR(*p)) {
Ian Kent051d3812006-03-27 01:14:53 -0800645 char *s = nd_get_link(nd);
646 error = 0;
647 if (s)
648 error = __vfs_follow_link(nd, s);
Al Virobcda7652011-03-13 16:42:14 -0400649 else if (nd->last_type == LAST_BIND) {
Al Viro16c2cd72011-02-22 15:50:10 -0500650 nd->flags |= LOOKUP_JUMPED;
Al Virob21041d2011-03-14 20:01:51 -0400651 nd->inode = nd->path.dentry->d_inode;
652 if (nd->inode->i_op->follow_link) {
Al Virobcda7652011-03-13 16:42:14 -0400653 /* stepped on a _really_ weird one */
654 path_put(&nd->path);
655 error = -ELOOP;
656 }
657 }
Ian Kent051d3812006-03-27 01:14:53 -0800658 }
Ian Kent051d3812006-03-27 01:14:53 -0800659 return error;
660}
661
Nick Piggin31e6b012011-01-07 17:49:52 +1100662static int follow_up_rcu(struct path *path)
663{
664 struct vfsmount *parent;
665 struct dentry *mountpoint;
666
667 parent = path->mnt->mnt_parent;
668 if (parent == path->mnt)
669 return 0;
670 mountpoint = path->mnt->mnt_mountpoint;
671 path->dentry = mountpoint;
672 path->mnt = parent;
673 return 1;
674}
675
Al Virobab77eb2009-04-18 03:26:48 -0400676int follow_up(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
678 struct vfsmount *parent;
679 struct dentry *mountpoint;
Nick Piggin99b7db72010-08-18 04:37:39 +1000680
681 br_read_lock(vfsmount_lock);
Al Virobab77eb2009-04-18 03:26:48 -0400682 parent = path->mnt->mnt_parent;
683 if (parent == path->mnt) {
Nick Piggin99b7db72010-08-18 04:37:39 +1000684 br_read_unlock(vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 return 0;
686 }
687 mntget(parent);
Al Virobab77eb2009-04-18 03:26:48 -0400688 mountpoint = dget(path->mnt->mnt_mountpoint);
Nick Piggin99b7db72010-08-18 04:37:39 +1000689 br_read_unlock(vfsmount_lock);
Al Virobab77eb2009-04-18 03:26:48 -0400690 dput(path->dentry);
691 path->dentry = mountpoint;
692 mntput(path->mnt);
693 path->mnt = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 return 1;
695}
696
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100697/*
David Howells9875cf82011-01-14 18:45:21 +0000698 * Perform an automount
699 * - return -EISDIR to tell follow_managed() to stop and return the path we
700 * were called with.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 */
David Howells9875cf82011-01-14 18:45:21 +0000702static int follow_automount(struct path *path, unsigned flags,
703 bool *need_mntput)
Nick Piggin31e6b012011-01-07 17:49:52 +1100704{
David Howells9875cf82011-01-14 18:45:21 +0000705 struct vfsmount *mnt;
David Howellsea5b7782011-01-14 19:10:03 +0000706 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +1100707
David Howells9875cf82011-01-14 18:45:21 +0000708 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
709 return -EREMOTE;
Al Viro463ffb22005-06-06 13:36:05 -0700710
David Howells6f45b652011-01-14 18:45:31 +0000711 /* We don't want to mount if someone supplied AT_NO_AUTOMOUNT
712 * and this is the terminal part of the path.
713 */
Al Viro49084c32011-06-25 21:59:52 -0400714 if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_PARENT))
David Howells6f45b652011-01-14 18:45:31 +0000715 return -EISDIR; /* we actually want to stop here */
716
David Howells9875cf82011-01-14 18:45:21 +0000717 /* We want to mount if someone is trying to open/create a file of any
718 * type under the mountpoint, wants to traverse through the mountpoint
719 * or wants to open the mounted directory.
720 *
721 * We don't want to mount if someone's just doing a stat and they've
722 * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and
723 * appended a '/' to the name.
724 */
725 if (!(flags & LOOKUP_FOLLOW) &&
Al Viro49084c32011-06-25 21:59:52 -0400726 !(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
David Howells9875cf82011-01-14 18:45:21 +0000727 LOOKUP_OPEN | LOOKUP_CREATE)))
728 return -EISDIR;
729
730 current->total_link_count++;
731 if (current->total_link_count >= 40)
732 return -ELOOP;
733
734 mnt = path->dentry->d_op->d_automount(path);
735 if (IS_ERR(mnt)) {
736 /*
737 * The filesystem is allowed to return -EISDIR here to indicate
738 * it doesn't want to automount. For instance, autofs would do
739 * this so that its userspace daemon can mount on this dentry.
740 *
741 * However, we can only permit this if it's a terminal point in
742 * the path being looked up; if it wasn't then the remainder of
743 * the path is inaccessible and we should say so.
744 */
Al Viro49084c32011-06-25 21:59:52 -0400745 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
David Howells9875cf82011-01-14 18:45:21 +0000746 return -EREMOTE;
747 return PTR_ERR(mnt);
748 }
David Howellsea5b7782011-01-14 19:10:03 +0000749
David Howells9875cf82011-01-14 18:45:21 +0000750 if (!mnt) /* mount collision */
751 return 0;
752
Al Viro8aef1882011-06-16 15:10:06 +0100753 if (!*need_mntput) {
754 /* lock_mount() may release path->mnt on error */
755 mntget(path->mnt);
756 *need_mntput = true;
757 }
Al Viro19a167a2011-01-17 01:35:23 -0500758 err = finish_automount(mnt, path);
David Howellsea5b7782011-01-14 19:10:03 +0000759
David Howellsea5b7782011-01-14 19:10:03 +0000760 switch (err) {
761 case -EBUSY:
762 /* Someone else made a mount here whilst we were busy */
Al Viro19a167a2011-01-17 01:35:23 -0500763 return 0;
David Howellsea5b7782011-01-14 19:10:03 +0000764 case 0:
Al Viro8aef1882011-06-16 15:10:06 +0100765 path_put(path);
David Howellsea5b7782011-01-14 19:10:03 +0000766 path->mnt = mnt;
767 path->dentry = dget(mnt->mnt_root);
David Howellsea5b7782011-01-14 19:10:03 +0000768 return 0;
Al Viro19a167a2011-01-17 01:35:23 -0500769 default:
770 return err;
David Howellsea5b7782011-01-14 19:10:03 +0000771 }
Al Viro19a167a2011-01-17 01:35:23 -0500772
David Howells9875cf82011-01-14 18:45:21 +0000773}
774
775/*
776 * Handle a dentry that is managed in some way.
David Howellscc53ce52011-01-14 18:45:26 +0000777 * - Flagged for transit management (autofs)
David Howells9875cf82011-01-14 18:45:21 +0000778 * - Flagged as mountpoint
779 * - Flagged as automount point
780 *
781 * This may only be called in refwalk mode.
782 *
783 * Serialization is taken care of in namespace.c
784 */
785static int follow_managed(struct path *path, unsigned flags)
786{
Al Viro8aef1882011-06-16 15:10:06 +0100787 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
David Howells9875cf82011-01-14 18:45:21 +0000788 unsigned managed;
789 bool need_mntput = false;
Al Viro8aef1882011-06-16 15:10:06 +0100790 int ret = 0;
David Howells9875cf82011-01-14 18:45:21 +0000791
792 /* Given that we're not holding a lock here, we retain the value in a
793 * local variable for each dentry as we look at it so that we don't see
794 * the components of that value change under us */
795 while (managed = ACCESS_ONCE(path->dentry->d_flags),
796 managed &= DCACHE_MANAGED_DENTRY,
797 unlikely(managed != 0)) {
David Howellscc53ce52011-01-14 18:45:26 +0000798 /* Allow the filesystem to manage the transit without i_mutex
799 * being held. */
800 if (managed & DCACHE_MANAGE_TRANSIT) {
801 BUG_ON(!path->dentry->d_op);
802 BUG_ON(!path->dentry->d_op->d_manage);
Al Viro1aed3e42011-03-18 09:09:02 -0400803 ret = path->dentry->d_op->d_manage(path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +0000804 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +0100805 break;
David Howellscc53ce52011-01-14 18:45:26 +0000806 }
807
David Howells9875cf82011-01-14 18:45:21 +0000808 /* Transit to a mounted filesystem. */
809 if (managed & DCACHE_MOUNTED) {
810 struct vfsmount *mounted = lookup_mnt(path);
811 if (mounted) {
812 dput(path->dentry);
813 if (need_mntput)
814 mntput(path->mnt);
815 path->mnt = mounted;
816 path->dentry = dget(mounted->mnt_root);
817 need_mntput = true;
818 continue;
819 }
820
821 /* Something is mounted on this dentry in another
822 * namespace and/or whatever was mounted there in this
823 * namespace got unmounted before we managed to get the
824 * vfsmount_lock */
825 }
826
827 /* Handle an automount point */
828 if (managed & DCACHE_NEED_AUTOMOUNT) {
829 ret = follow_automount(path, flags, &need_mntput);
830 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +0100831 break;
David Howells9875cf82011-01-14 18:45:21 +0000832 continue;
833 }
834
835 /* We didn't change the current path point */
836 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
Al Viro8aef1882011-06-16 15:10:06 +0100838
839 if (need_mntput && path->mnt == mnt)
840 mntput(path->mnt);
841 if (ret == -EISDIR)
842 ret = 0;
843 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
845
David Howellscc53ce52011-01-14 18:45:26 +0000846int follow_down_one(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
848 struct vfsmount *mounted;
849
Al Viro1c755af2009-04-18 14:06:57 -0400850 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -0400852 dput(path->dentry);
853 mntput(path->mnt);
854 path->mnt = mounted;
855 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 return 1;
857 }
858 return 0;
859}
860
Ian Kent62a73752011-03-25 01:51:02 +0800861static inline bool managed_dentry_might_block(struct dentry *dentry)
862{
863 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
864 dentry->d_op->d_manage(dentry, true) < 0);
865}
866
David Howells9875cf82011-01-14 18:45:21 +0000867/*
Al Viro287548e2011-05-27 06:50:06 -0400868 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
869 * we meet a managed dentry that would need blocking.
David Howells9875cf82011-01-14 18:45:21 +0000870 */
871static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
Al Viro287548e2011-05-27 06:50:06 -0400872 struct inode **inode)
David Howells9875cf82011-01-14 18:45:21 +0000873{
Ian Kent62a73752011-03-25 01:51:02 +0800874 for (;;) {
David Howells9875cf82011-01-14 18:45:21 +0000875 struct vfsmount *mounted;
Ian Kent62a73752011-03-25 01:51:02 +0800876 /*
877 * Don't forget we might have a non-mountpoint managed dentry
878 * that wants to block transit.
879 */
Al Viro287548e2011-05-27 06:50:06 -0400880 if (unlikely(managed_dentry_might_block(path->dentry)))
David Howellsab909112011-01-14 18:46:51 +0000881 return false;
Ian Kent62a73752011-03-25 01:51:02 +0800882
883 if (!d_mountpoint(path->dentry))
884 break;
885
David Howells9875cf82011-01-14 18:45:21 +0000886 mounted = __lookup_mnt(path->mnt, path->dentry, 1);
887 if (!mounted)
888 break;
889 path->mnt = mounted;
890 path->dentry = mounted->mnt_root;
891 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
Linus Torvalds59430262011-07-18 15:43:29 -0700892 /*
893 * Update the inode too. We don't need to re-check the
894 * dentry sequence number here after this d_inode read,
895 * because a mount-point is always pinned.
896 */
897 *inode = path->dentry->d_inode;
David Howells9875cf82011-01-14 18:45:21 +0000898 }
David Howells9875cf82011-01-14 18:45:21 +0000899 return true;
900}
901
Al Virodea39372011-05-27 06:53:39 -0400902static void follow_mount_rcu(struct nameidata *nd)
Al Viro287548e2011-05-27 06:50:06 -0400903{
Al Virodea39372011-05-27 06:53:39 -0400904 while (d_mountpoint(nd->path.dentry)) {
Al Viro287548e2011-05-27 06:50:06 -0400905 struct vfsmount *mounted;
Al Virodea39372011-05-27 06:53:39 -0400906 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
Al Viro287548e2011-05-27 06:50:06 -0400907 if (!mounted)
908 break;
Al Virodea39372011-05-27 06:53:39 -0400909 nd->path.mnt = mounted;
910 nd->path.dentry = mounted->mnt_root;
911 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro287548e2011-05-27 06:50:06 -0400912 }
913}
914
Nick Piggin31e6b012011-01-07 17:49:52 +1100915static int follow_dotdot_rcu(struct nameidata *nd)
916{
Nick Piggin31e6b012011-01-07 17:49:52 +1100917 set_root_rcu(nd);
918
David Howells9875cf82011-01-14 18:45:21 +0000919 while (1) {
Nick Piggin31e6b012011-01-07 17:49:52 +1100920 if (nd->path.dentry == nd->root.dentry &&
921 nd->path.mnt == nd->root.mnt) {
922 break;
923 }
924 if (nd->path.dentry != nd->path.mnt->mnt_root) {
925 struct dentry *old = nd->path.dentry;
926 struct dentry *parent = old->d_parent;
927 unsigned seq;
928
929 seq = read_seqcount_begin(&parent->d_seq);
930 if (read_seqcount_retry(&old->d_seq, nd->seq))
Al Viroef7562d2011-03-04 14:35:59 -0500931 goto failed;
Nick Piggin31e6b012011-01-07 17:49:52 +1100932 nd->path.dentry = parent;
933 nd->seq = seq;
934 break;
935 }
936 if (!follow_up_rcu(&nd->path))
937 break;
938 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Nick Piggin31e6b012011-01-07 17:49:52 +1100939 }
Al Virodea39372011-05-27 06:53:39 -0400940 follow_mount_rcu(nd);
941 nd->inode = nd->path.dentry->d_inode;
Nick Piggin31e6b012011-01-07 17:49:52 +1100942 return 0;
Al Viroef7562d2011-03-04 14:35:59 -0500943
944failed:
945 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -0500946 if (!(nd->flags & LOOKUP_ROOT))
947 nd->root.mnt = NULL;
Al Viroef7562d2011-03-04 14:35:59 -0500948 rcu_read_unlock();
949 br_read_unlock(vfsmount_lock);
950 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +1100951}
952
David Howells9875cf82011-01-14 18:45:21 +0000953/*
David Howellscc53ce52011-01-14 18:45:26 +0000954 * Follow down to the covering mount currently visible to userspace. At each
955 * point, the filesystem owning that dentry may be queried as to whether the
956 * caller is permitted to proceed or not.
David Howellscc53ce52011-01-14 18:45:26 +0000957 */
Al Viro7cc90cc2011-03-18 09:04:20 -0400958int follow_down(struct path *path)
David Howellscc53ce52011-01-14 18:45:26 +0000959{
960 unsigned managed;
961 int ret;
962
963 while (managed = ACCESS_ONCE(path->dentry->d_flags),
964 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
965 /* Allow the filesystem to manage the transit without i_mutex
966 * being held.
967 *
968 * We indicate to the filesystem if someone is trying to mount
969 * something here. This gives autofs the chance to deny anyone
970 * other than its daemon the right to mount on its
971 * superstructure.
972 *
973 * The filesystem may sleep at this point.
974 */
975 if (managed & DCACHE_MANAGE_TRANSIT) {
976 BUG_ON(!path->dentry->d_op);
977 BUG_ON(!path->dentry->d_op->d_manage);
David Howellsab909112011-01-14 18:46:51 +0000978 ret = path->dentry->d_op->d_manage(
Al Viro1aed3e42011-03-18 09:09:02 -0400979 path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +0000980 if (ret < 0)
981 return ret == -EISDIR ? 0 : ret;
982 }
983
984 /* Transit to a mounted filesystem. */
985 if (managed & DCACHE_MOUNTED) {
986 struct vfsmount *mounted = lookup_mnt(path);
987 if (!mounted)
988 break;
989 dput(path->dentry);
990 mntput(path->mnt);
991 path->mnt = mounted;
992 path->dentry = dget(mounted->mnt_root);
993 continue;
994 }
995
996 /* Don't handle automount points here */
997 break;
998 }
999 return 0;
1000}
1001
1002/*
David Howells9875cf82011-01-14 18:45:21 +00001003 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1004 */
1005static void follow_mount(struct path *path)
1006{
1007 while (d_mountpoint(path->dentry)) {
1008 struct vfsmount *mounted = lookup_mnt(path);
1009 if (!mounted)
1010 break;
1011 dput(path->dentry);
1012 mntput(path->mnt);
1013 path->mnt = mounted;
1014 path->dentry = dget(mounted->mnt_root);
1015 }
1016}
1017
Nick Piggin31e6b012011-01-07 17:49:52 +11001018static void follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
Al Viro2a737872009-04-07 11:49:53 -04001020 set_root(nd);
Andreas Mohre518ddb2006-09-29 02:01:22 -07001021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 while(1) {
Jan Blunck4ac91372008-02-14 19:34:32 -08001023 struct dentry *old = nd->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Al Viro2a737872009-04-07 11:49:53 -04001025 if (nd->path.dentry == nd->root.dentry &&
1026 nd->path.mnt == nd->root.mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 break;
1028 }
Jan Blunck4ac91372008-02-14 19:34:32 -08001029 if (nd->path.dentry != nd->path.mnt->mnt_root) {
Al Viro3088dd72010-01-30 15:47:29 -05001030 /* rare case of legitimate dget_parent()... */
1031 nd->path.dentry = dget_parent(nd->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 dput(old);
1033 break;
1034 }
Al Viro3088dd72010-01-30 15:47:29 -05001035 if (!follow_up(&nd->path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
Al Viro79ed0222009-04-18 13:59:41 -04001038 follow_mount(&nd->path);
Nick Piggin31e6b012011-01-07 17:49:52 +11001039 nd->inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040}
1041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042/*
Nick Pigginbaa03892010-08-18 04:37:31 +10001043 * Allocate a dentry with name and parent, and perform a parent
1044 * directory ->lookup on it. Returns the new dentry, or ERR_PTR
1045 * on error. parent->d_inode->i_mutex must be held. d_lookup must
1046 * have verified that no child exists while under i_mutex.
1047 */
1048static struct dentry *d_alloc_and_lookup(struct dentry *parent,
1049 struct qstr *name, struct nameidata *nd)
1050{
1051 struct inode *inode = parent->d_inode;
1052 struct dentry *dentry;
1053 struct dentry *old;
1054
1055 /* Don't create child dentry for a dead directory. */
1056 if (unlikely(IS_DEADDIR(inode)))
1057 return ERR_PTR(-ENOENT);
1058
1059 dentry = d_alloc(parent, name);
1060 if (unlikely(!dentry))
1061 return ERR_PTR(-ENOMEM);
1062
1063 old = inode->i_op->lookup(inode, dentry, nd);
1064 if (unlikely(old)) {
1065 dput(dentry);
1066 dentry = old;
1067 }
1068 return dentry;
1069}
1070
1071/*
Josef Bacik44396f42011-05-31 11:58:49 -04001072 * We already have a dentry, but require a lookup to be performed on the parent
1073 * directory to fill in d_inode. Returns the new dentry, or ERR_PTR on error.
1074 * parent->d_inode->i_mutex must be held. d_lookup must have verified that no
1075 * child exists while under i_mutex.
1076 */
1077static struct dentry *d_inode_lookup(struct dentry *parent, struct dentry *dentry,
1078 struct nameidata *nd)
1079{
1080 struct inode *inode = parent->d_inode;
1081 struct dentry *old;
1082
1083 /* Don't create child dentry for a dead directory. */
1084 if (unlikely(IS_DEADDIR(inode)))
1085 return ERR_PTR(-ENOENT);
1086
1087 old = inode->i_op->lookup(inode, dentry, nd);
1088 if (unlikely(old)) {
1089 dput(dentry);
1090 dentry = old;
1091 }
1092 return dentry;
1093}
1094
1095/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 * It's more convoluted than I'd like it to be, but... it's still fairly
1097 * small and for now I'd prefer to have fast path as straight as possible.
1098 * It _is_ time-critical.
1099 */
1100static int do_lookup(struct nameidata *nd, struct qstr *name,
Nick Piggin31e6b012011-01-07 17:49:52 +11001101 struct path *path, struct inode **inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
Jan Blunck4ac91372008-02-14 19:34:32 -08001103 struct vfsmount *mnt = nd->path.mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +11001104 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro5a18fff2011-03-11 04:44:53 -05001105 int need_reval = 1;
1106 int status = 1;
David Howells9875cf82011-01-14 18:45:21 +00001107 int err;
1108
Al Viro3cac2602009-08-13 18:27:43 +04001109 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001110 * Rename seqlock is not required here because in the off chance
1111 * of a false negative due to a concurrent rename, we're going to
1112 * do the non-racy lookup, below.
1113 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001114 if (nd->flags & LOOKUP_RCU) {
1115 unsigned seq;
Nick Piggin31e6b012011-01-07 17:49:52 +11001116 *inode = nd->inode;
1117 dentry = __d_lookup_rcu(parent, name, &seq, inode);
Al Viro5a18fff2011-03-11 04:44:53 -05001118 if (!dentry)
1119 goto unlazy;
1120
Nick Piggin31e6b012011-01-07 17:49:52 +11001121 /* Memory barrier in read_seqcount_begin of child is enough */
1122 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1123 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001124 nd->seq = seq;
Al Viro5a18fff2011-03-11 04:44:53 -05001125
Al Viro24643082011-02-15 01:26:22 -05001126 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
Al Viro5a18fff2011-03-11 04:44:53 -05001127 status = d_revalidate(dentry, nd);
1128 if (unlikely(status <= 0)) {
1129 if (status != -ECHILD)
1130 need_reval = 0;
1131 goto unlazy;
1132 }
Al Viro24643082011-02-15 01:26:22 -05001133 }
Josef Bacik44396f42011-05-31 11:58:49 -04001134 if (unlikely(d_need_lookup(dentry)))
1135 goto unlazy;
Nick Piggin31e6b012011-01-07 17:49:52 +11001136 path->mnt = mnt;
1137 path->dentry = dentry;
Al Virod6e9bd22011-05-27 07:03:15 -04001138 if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1139 goto unlazy;
1140 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1141 goto unlazy;
1142 return 0;
Al Viro5a18fff2011-03-11 04:44:53 -05001143unlazy:
Al Viro19660af2011-03-25 10:32:48 -04001144 if (unlazy_walk(nd, dentry))
1145 return -ECHILD;
Al Viro5a18fff2011-03-11 04:44:53 -05001146 } else {
1147 dentry = __d_lookup(parent, name);
Nick Piggin31e6b012011-01-07 17:49:52 +11001148 }
Al Viro5a18fff2011-03-11 04:44:53 -05001149
Josef Bacik44396f42011-05-31 11:58:49 -04001150 if (dentry && unlikely(d_need_lookup(dentry))) {
1151 dput(dentry);
1152 dentry = NULL;
1153 }
Al Viro5a18fff2011-03-11 04:44:53 -05001154retry:
1155 if (unlikely(!dentry)) {
1156 struct inode *dir = parent->d_inode;
1157 BUG_ON(nd->inode != dir);
1158
1159 mutex_lock(&dir->i_mutex);
1160 dentry = d_lookup(parent, name);
1161 if (likely(!dentry)) {
1162 dentry = d_alloc_and_lookup(parent, name, nd);
1163 if (IS_ERR(dentry)) {
1164 mutex_unlock(&dir->i_mutex);
1165 return PTR_ERR(dentry);
1166 }
1167 /* known good */
1168 need_reval = 0;
1169 status = 1;
Josef Bacik44396f42011-05-31 11:58:49 -04001170 } else if (unlikely(d_need_lookup(dentry))) {
1171 dentry = d_inode_lookup(parent, dentry, nd);
1172 if (IS_ERR(dentry)) {
1173 mutex_unlock(&dir->i_mutex);
1174 return PTR_ERR(dentry);
1175 }
1176 /* known good */
1177 need_reval = 0;
1178 status = 1;
Al Viro5a18fff2011-03-11 04:44:53 -05001179 }
1180 mutex_unlock(&dir->i_mutex);
Al Viro24643082011-02-15 01:26:22 -05001181 }
Al Viro5a18fff2011-03-11 04:44:53 -05001182 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
1183 status = d_revalidate(dentry, nd);
1184 if (unlikely(status <= 0)) {
1185 if (status < 0) {
1186 dput(dentry);
1187 return status;
1188 }
1189 if (!d_invalidate(dentry)) {
1190 dput(dentry);
1191 dentry = NULL;
1192 need_reval = 1;
1193 goto retry;
1194 }
1195 }
1196
David Howells9875cf82011-01-14 18:45:21 +00001197 path->mnt = mnt;
1198 path->dentry = dentry;
1199 err = follow_managed(path, nd->flags);
Ian Kent89312212011-01-18 12:06:10 +08001200 if (unlikely(err < 0)) {
1201 path_put_conditional(path, nd);
David Howells9875cf82011-01-14 18:45:21 +00001202 return err;
Ian Kent89312212011-01-18 12:06:10 +08001203 }
David Howells9875cf82011-01-14 18:45:21 +00001204 *inode = path->dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206}
1207
Al Viro52094c82011-02-21 21:34:47 -05001208static inline int may_lookup(struct nameidata *nd)
1209{
1210 if (nd->flags & LOOKUP_RCU) {
Al Viro4ad5abb2011-06-20 19:57:03 -04001211 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
Al Viro52094c82011-02-21 21:34:47 -05001212 if (err != -ECHILD)
1213 return err;
Al Viro19660af2011-03-25 10:32:48 -04001214 if (unlazy_walk(nd, NULL))
Al Viro52094c82011-02-21 21:34:47 -05001215 return -ECHILD;
1216 }
Al Viro4ad5abb2011-06-20 19:57:03 -04001217 return inode_permission(nd->inode, MAY_EXEC);
Al Viro52094c82011-02-21 21:34:47 -05001218}
1219
Al Viro9856fa12011-03-04 14:22:06 -05001220static inline int handle_dots(struct nameidata *nd, int type)
1221{
1222 if (type == LAST_DOTDOT) {
1223 if (nd->flags & LOOKUP_RCU) {
1224 if (follow_dotdot_rcu(nd))
1225 return -ECHILD;
1226 } else
1227 follow_dotdot(nd);
1228 }
1229 return 0;
1230}
1231
Al Viro951361f2011-03-04 14:44:37 -05001232static void terminate_walk(struct nameidata *nd)
1233{
1234 if (!(nd->flags & LOOKUP_RCU)) {
1235 path_put(&nd->path);
1236 } else {
1237 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001238 if (!(nd->flags & LOOKUP_ROOT))
1239 nd->root.mnt = NULL;
Al Viro951361f2011-03-04 14:44:37 -05001240 rcu_read_unlock();
1241 br_read_unlock(vfsmount_lock);
1242 }
1243}
1244
Al Viroce57dfc2011-03-13 19:58:58 -04001245static inline int walk_component(struct nameidata *nd, struct path *path,
1246 struct qstr *name, int type, int follow)
1247{
1248 struct inode *inode;
1249 int err;
1250 /*
1251 * "." and ".." are special - ".." especially so because it has
1252 * to be able to know about the current root directory and
1253 * parent relationships.
1254 */
1255 if (unlikely(type != LAST_NORM))
1256 return handle_dots(nd, type);
1257 err = do_lookup(nd, name, path, &inode);
1258 if (unlikely(err)) {
1259 terminate_walk(nd);
1260 return err;
1261 }
1262 if (!inode) {
1263 path_to_nameidata(path, nd);
1264 terminate_walk(nd);
1265 return -ENOENT;
1266 }
1267 if (unlikely(inode->i_op->follow_link) && follow) {
Al Viro19660af2011-03-25 10:32:48 -04001268 if (nd->flags & LOOKUP_RCU) {
1269 if (unlikely(unlazy_walk(nd, path->dentry))) {
1270 terminate_walk(nd);
1271 return -ECHILD;
1272 }
1273 }
Al Viroce57dfc2011-03-13 19:58:58 -04001274 BUG_ON(inode != path->dentry->d_inode);
1275 return 1;
1276 }
1277 path_to_nameidata(path, nd);
1278 nd->inode = inode;
1279 return 0;
1280}
1281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282/*
Al Virob3563792011-03-14 21:54:55 -04001283 * This limits recursive symlink follows to 8, while
1284 * limiting consecutive symlinks to 40.
1285 *
1286 * Without that kind of total limit, nasty chains of consecutive
1287 * symlinks can cause almost arbitrarily long lookups.
1288 */
1289static inline int nested_symlink(struct path *path, struct nameidata *nd)
1290{
1291 int res;
1292
Al Virob3563792011-03-14 21:54:55 -04001293 if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1294 path_put_conditional(path, nd);
1295 path_put(&nd->path);
1296 return -ELOOP;
1297 }
Erez Zadok1a4022f2011-05-21 01:19:59 -04001298 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
Al Virob3563792011-03-14 21:54:55 -04001299
1300 nd->depth++;
1301 current->link_count++;
1302
1303 do {
1304 struct path link = *path;
1305 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04001306
1307 res = follow_link(&link, nd, &cookie);
Al Virob3563792011-03-14 21:54:55 -04001308 if (!res)
1309 res = walk_component(nd, path, &nd->last,
1310 nd->last_type, LOOKUP_FOLLOW);
Al Viro574197e2011-03-14 22:20:34 -04001311 put_link(nd, &link, cookie);
Al Virob3563792011-03-14 21:54:55 -04001312 } while (res > 0);
1313
1314 current->link_count--;
1315 nd->depth--;
1316 return res;
1317}
1318
1319/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01001321 * This is the basic name resolution function, turning a pathname into
1322 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01001324 * Returns 0 and nd will have valid dentry and mnt on success.
1325 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 */
Al Viro6de88d72009-08-09 01:41:57 +04001327static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
1329 struct path next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
1332 while (*name=='/')
1333 name++;
1334 if (!*name)
Al Viro086e1832011-02-22 20:56:27 -05001335 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 /* At this point we know we have a real path component. */
1338 for(;;) {
1339 unsigned long hash;
1340 struct qstr this;
1341 unsigned int c;
Al Virofe479a52011-02-22 15:10:03 -05001342 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Al Viro52094c82011-02-21 21:34:47 -05001344 err = may_lookup(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 if (err)
1346 break;
1347
1348 this.name = name;
1349 c = *(const unsigned char *)name;
1350
1351 hash = init_name_hash();
1352 do {
1353 name++;
1354 hash = partial_name_hash(c, hash);
1355 c = *(const unsigned char *)name;
1356 } while (c && (c != '/'));
1357 this.len = name - (const char *) this.name;
1358 this.hash = end_name_hash(hash);
1359
Al Virofe479a52011-02-22 15:10:03 -05001360 type = LAST_NORM;
1361 if (this.name[0] == '.') switch (this.len) {
1362 case 2:
Al Viro16c2cd72011-02-22 15:50:10 -05001363 if (this.name[1] == '.') {
Al Virofe479a52011-02-22 15:10:03 -05001364 type = LAST_DOTDOT;
Al Viro16c2cd72011-02-22 15:50:10 -05001365 nd->flags |= LOOKUP_JUMPED;
1366 }
Al Virofe479a52011-02-22 15:10:03 -05001367 break;
1368 case 1:
1369 type = LAST_DOT;
1370 }
Al Viro5a202bc2011-03-08 14:17:44 -05001371 if (likely(type == LAST_NORM)) {
1372 struct dentry *parent = nd->path.dentry;
Al Viro16c2cd72011-02-22 15:50:10 -05001373 nd->flags &= ~LOOKUP_JUMPED;
Al Viro5a202bc2011-03-08 14:17:44 -05001374 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1375 err = parent->d_op->d_hash(parent, nd->inode,
1376 &this);
1377 if (err < 0)
1378 break;
1379 }
1380 }
Al Virofe479a52011-02-22 15:10:03 -05001381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 /* remove trailing slashes? */
1383 if (!c)
1384 goto last_component;
1385 while (*++name == '/');
1386 if (!*name)
Al Virob3563792011-03-14 21:54:55 -04001387 goto last_component;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Al Viroce57dfc2011-03-13 19:58:58 -04001389 err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1390 if (err < 0)
1391 return err;
Al Virofe479a52011-02-22 15:10:03 -05001392
Al Viroce57dfc2011-03-13 19:58:58 -04001393 if (err) {
Al Virob3563792011-03-14 21:54:55 -04001394 err = nested_symlink(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 if (err)
Al Viroa7472ba2011-03-04 14:39:30 -05001396 return err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 err = -ENOTDIR;
Nick Piggin31e6b012011-01-07 17:49:52 +11001399 if (!nd->inode->i_op->lookup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 break;
1401 continue;
1402 /* here ends the main loop */
1403
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404last_component:
Al Virob3563792011-03-14 21:54:55 -04001405 nd->last = this;
1406 nd->last_type = type;
Al Viro086e1832011-02-22 20:56:27 -05001407 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 }
Al Viro951361f2011-03-04 14:44:37 -05001409 terminate_walk(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 return err;
1411}
1412
Al Viro70e9b352011-03-05 21:12:22 -05001413static int path_init(int dfd, const char *name, unsigned int flags,
1414 struct nameidata *nd, struct file **fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
Prasanna Medaea3834d2005-04-29 16:00:17 +01001416 int retval = 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001417 int fput_needed;
1418 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
1420 nd->last_type = LAST_ROOT; /* if there are only slashes... */
Al Viro16c2cd72011-02-22 15:50:10 -05001421 nd->flags = flags | LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 nd->depth = 0;
Al Viro5b6ca022011-03-09 23:04:47 -05001423 if (flags & LOOKUP_ROOT) {
1424 struct inode *inode = nd->root.dentry->d_inode;
Al Viro73d049a2011-03-11 12:08:24 -05001425 if (*name) {
1426 if (!inode->i_op->lookup)
1427 return -ENOTDIR;
1428 retval = inode_permission(inode, MAY_EXEC);
1429 if (retval)
1430 return retval;
1431 }
Al Viro5b6ca022011-03-09 23:04:47 -05001432 nd->path = nd->root;
1433 nd->inode = inode;
1434 if (flags & LOOKUP_RCU) {
1435 br_read_lock(vfsmount_lock);
1436 rcu_read_lock();
1437 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1438 } else {
1439 path_get(&nd->path);
1440 }
1441 return 0;
1442 }
1443
Al Viro2a737872009-04-07 11:49:53 -04001444 nd->root.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 if (*name=='/') {
Al Viroe41f7d42011-02-22 14:02:58 -05001447 if (flags & LOOKUP_RCU) {
1448 br_read_lock(vfsmount_lock);
1449 rcu_read_lock();
1450 set_root_rcu(nd);
1451 } else {
1452 set_root(nd);
1453 path_get(&nd->root);
1454 }
Al Viro2a737872009-04-07 11:49:53 -04001455 nd->path = nd->root;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001456 } else if (dfd == AT_FDCWD) {
Al Viroe41f7d42011-02-22 14:02:58 -05001457 if (flags & LOOKUP_RCU) {
1458 struct fs_struct *fs = current->fs;
1459 unsigned seq;
1460
1461 br_read_lock(vfsmount_lock);
1462 rcu_read_lock();
1463
1464 do {
1465 seq = read_seqcount_begin(&fs->seq);
1466 nd->path = fs->pwd;
1467 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1468 } while (read_seqcount_retry(&fs->seq, seq));
1469 } else {
1470 get_fs_pwd(current->fs, &nd->path);
1471 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001472 } else {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001473 struct dentry *dentry;
1474
Al Viro1abf0c72011-03-13 03:51:11 -04001475 file = fget_raw_light(dfd, &fput_needed);
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001476 retval = -EBADF;
1477 if (!file)
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001478 goto out_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001479
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001480 dentry = file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001481
Al Virof52e0c12011-03-14 18:56:51 -04001482 if (*name) {
1483 retval = -ENOTDIR;
1484 if (!S_ISDIR(dentry->d_inode->i_mode))
1485 goto fput_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001486
Al Viro4ad5abb2011-06-20 19:57:03 -04001487 retval = inode_permission(dentry->d_inode, MAY_EXEC);
Al Virof52e0c12011-03-14 18:56:51 -04001488 if (retval)
1489 goto fput_fail;
1490 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001491
Jan Blunck5dd784d02008-02-14 19:34:38 -08001492 nd->path = file->f_path;
Al Viroe41f7d42011-02-22 14:02:58 -05001493 if (flags & LOOKUP_RCU) {
1494 if (fput_needed)
Al Viro70e9b352011-03-05 21:12:22 -05001495 *fp = file;
Al Viroe41f7d42011-02-22 14:02:58 -05001496 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1497 br_read_lock(vfsmount_lock);
1498 rcu_read_lock();
1499 } else {
1500 path_get(&file->f_path);
1501 fput_light(file, fput_needed);
1502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 }
Al Viroe41f7d42011-02-22 14:02:58 -05001504
Nick Piggin31e6b012011-01-07 17:49:52 +11001505 nd->inode = nd->path.dentry->d_inode;
Al Viro9b4a9b12009-04-07 11:44:16 -04001506 return 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001507
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001508fput_fail:
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001509 fput_light(file, fput_needed);
Al Viro9b4a9b12009-04-07 11:44:16 -04001510out_fail:
1511 return retval;
1512}
1513
Al Virobd92d7f2011-03-14 19:54:59 -04001514static inline int lookup_last(struct nameidata *nd, struct path *path)
1515{
1516 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1517 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1518
1519 nd->flags &= ~LOOKUP_PARENT;
1520 return walk_component(nd, path, &nd->last, nd->last_type,
1521 nd->flags & LOOKUP_FOLLOW);
1522}
1523
Al Viro9b4a9b12009-04-07 11:44:16 -04001524/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroee0827c2011-02-21 23:38:09 -05001525static int path_lookupat(int dfd, const char *name,
Al Viro9b4a9b12009-04-07 11:44:16 -04001526 unsigned int flags, struct nameidata *nd)
1527{
Al Viro70e9b352011-03-05 21:12:22 -05001528 struct file *base = NULL;
Al Virobd92d7f2011-03-14 19:54:59 -04001529 struct path path;
1530 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001531
1532 /*
1533 * Path walking is largely split up into 2 different synchronisation
1534 * schemes, rcu-walk and ref-walk (explained in
1535 * Documentation/filesystems/path-lookup.txt). These share much of the
1536 * path walk code, but some things particularly setup, cleanup, and
1537 * following mounts are sufficiently divergent that functions are
1538 * duplicated. Typically there is a function foo(), and its RCU
1539 * analogue, foo_rcu().
1540 *
1541 * -ECHILD is the error number of choice (just to avoid clashes) that
1542 * is returned if some aspect of an rcu-walk fails. Such an error must
1543 * be handled by restarting a traditional ref-walk (which will always
1544 * be able to complete).
1545 */
Al Virobd92d7f2011-03-14 19:54:59 -04001546 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
Al Viroee0827c2011-02-21 23:38:09 -05001547
Al Virobd92d7f2011-03-14 19:54:59 -04001548 if (unlikely(err))
1549 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001550
1551 current->total_link_count = 0;
Al Virobd92d7f2011-03-14 19:54:59 -04001552 err = link_path_walk(name, nd);
1553
1554 if (!err && !(flags & LOOKUP_PARENT)) {
Al Virobd92d7f2011-03-14 19:54:59 -04001555 err = lookup_last(nd, &path);
1556 while (err > 0) {
1557 void *cookie;
1558 struct path link = path;
Al Virobd92d7f2011-03-14 19:54:59 -04001559 nd->flags |= LOOKUP_PARENT;
Al Viro574197e2011-03-14 22:20:34 -04001560 err = follow_link(&link, nd, &cookie);
Al Virobd92d7f2011-03-14 19:54:59 -04001561 if (!err)
1562 err = lookup_last(nd, &path);
Al Viro574197e2011-03-14 22:20:34 -04001563 put_link(nd, &link, cookie);
Al Virobd92d7f2011-03-14 19:54:59 -04001564 }
1565 }
Al Viroee0827c2011-02-21 23:38:09 -05001566
Al Viro9f1fafe2011-03-25 11:00:12 -04001567 if (!err)
1568 err = complete_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04001569
1570 if (!err && nd->flags & LOOKUP_DIRECTORY) {
1571 if (!nd->inode->i_op->lookup) {
1572 path_put(&nd->path);
Al Virobd23a532011-03-23 09:56:30 -04001573 err = -ENOTDIR;
Al Virobd92d7f2011-03-14 19:54:59 -04001574 }
1575 }
Al Viro16c2cd72011-02-22 15:50:10 -05001576
Al Viro70e9b352011-03-05 21:12:22 -05001577 if (base)
1578 fput(base);
Al Viroee0827c2011-02-21 23:38:09 -05001579
Al Viro5b6ca022011-03-09 23:04:47 -05001580 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
Al Viro2a737872009-04-07 11:49:53 -04001581 path_put(&nd->root);
1582 nd->root.mnt = NULL;
1583 }
Al Virobd92d7f2011-03-14 19:54:59 -04001584 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001585}
Nick Piggin31e6b012011-01-07 17:49:52 +11001586
Al Viroee0827c2011-02-21 23:38:09 -05001587static int do_path_lookup(int dfd, const char *name,
1588 unsigned int flags, struct nameidata *nd)
1589{
1590 int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1591 if (unlikely(retval == -ECHILD))
1592 retval = path_lookupat(dfd, name, flags, nd);
1593 if (unlikely(retval == -ESTALE))
1594 retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11001595
1596 if (likely(!retval)) {
1597 if (unlikely(!audit_dummy_context())) {
1598 if (nd->path.dentry && nd->inode)
1599 audit_inode(name, nd->path.dentry);
1600 }
1601 }
Al Viro9b4a9b12009-04-07 11:44:16 -04001602 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603}
1604
Al Viroc9c6cac2011-02-16 15:15:47 -05001605int kern_path_parent(const char *name, struct nameidata *nd)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001606{
Al Viroc9c6cac2011-02-16 15:15:47 -05001607 return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001608}
1609
Al Virod1811462008-08-02 00:49:18 -04001610int kern_path(const char *name, unsigned int flags, struct path *path)
1611{
1612 struct nameidata nd;
1613 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1614 if (!res)
1615 *path = nd.path;
1616 return res;
1617}
1618
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001619/**
1620 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1621 * @dentry: pointer to dentry of the base directory
1622 * @mnt: pointer to vfs mount of the base directory
1623 * @name: pointer to file name
1624 * @flags: lookup flags
Al Viroe0a01242011-06-27 17:00:37 -04001625 * @path: pointer to struct path to fill
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001626 */
1627int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
1628 const char *name, unsigned int flags,
Al Viroe0a01242011-06-27 17:00:37 -04001629 struct path *path)
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001630{
Al Viroe0a01242011-06-27 17:00:37 -04001631 struct nameidata nd;
1632 int err;
1633 nd.root.dentry = dentry;
1634 nd.root.mnt = mnt;
1635 BUG_ON(flags & LOOKUP_PARENT);
Al Viro5b6ca022011-03-09 23:04:47 -05001636 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
Al Viroe0a01242011-06-27 17:00:37 -04001637 err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
1638 if (!err)
1639 *path = nd.path;
1640 return err;
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001641}
1642
Christoph Hellwigeead1912007-10-16 23:25:38 -07001643static struct dentry *__lookup_hash(struct qstr *name,
1644 struct dentry *base, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645{
Christoph Hellwig81fca442010-10-06 10:47:47 +02001646 struct inode *inode = base->d_inode;
James Morris057f6c02007-04-26 00:12:05 -07001647 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 int err;
1649
Al Viro4ad5abb2011-06-20 19:57:03 -04001650 err = inode_permission(inode, MAY_EXEC);
Christoph Hellwig81fca442010-10-06 10:47:47 +02001651 if (err)
1652 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
1654 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001655 * Don't bother with __d_lookup: callers are for creat as
1656 * well as unlink, so a lot of the time it would cost
1657 * a double lookup.
Al Viro6e6b1bd2009-08-13 23:38:37 +04001658 */
Nick Pigginb04f7842010-08-18 04:37:34 +10001659 dentry = d_lookup(base, name);
Al Viro6e6b1bd2009-08-13 23:38:37 +04001660
Josef Bacik44396f42011-05-31 11:58:49 -04001661 if (dentry && d_need_lookup(dentry)) {
1662 /*
1663 * __lookup_hash is called with the parent dir's i_mutex already
1664 * held, so we are good to go here.
1665 */
1666 dentry = d_inode_lookup(base, dentry, nd);
1667 if (IS_ERR(dentry))
1668 return dentry;
1669 }
1670
Al Virod2d9e9f2011-06-20 10:55:26 -04001671 if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE)) {
1672 int status = d_revalidate(dentry, nd);
1673 if (unlikely(status <= 0)) {
1674 /*
1675 * The dentry failed validation.
1676 * If d_revalidate returned 0 attempt to invalidate
1677 * the dentry otherwise d_revalidate is asking us
1678 * to return a fail status.
1679 */
1680 if (status < 0) {
1681 dput(dentry);
1682 return ERR_PTR(status);
1683 } else if (!d_invalidate(dentry)) {
1684 dput(dentry);
1685 dentry = NULL;
1686 }
1687 }
1688 }
Al Viro6e6b1bd2009-08-13 23:38:37 +04001689
Nick Pigginbaa03892010-08-18 04:37:31 +10001690 if (!dentry)
1691 dentry = d_alloc_and_lookup(base, name, nd);
Al Viro5a202bc2011-03-08 14:17:44 -05001692
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 return dentry;
1694}
1695
James Morris057f6c02007-04-26 00:12:05 -07001696/*
1697 * Restricted form of lookup. Doesn't follow links, single-component only,
1698 * needs parent already locked. Doesn't follow mounts.
1699 * SMP-safe.
1700 */
Adrian Bunka244e162006-03-31 02:32:11 -08001701static struct dentry *lookup_hash(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702{
Jan Blunck4ac91372008-02-14 19:34:32 -08001703 return __lookup_hash(&nd->last, nd->path.dentry, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704}
1705
Christoph Hellwigeead1912007-10-16 23:25:38 -07001706/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07001707 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07001708 * @name: pathname component to lookup
1709 * @base: base directory to lookup from
1710 * @len: maximum length @len should be interpreted to
1711 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07001712 * Note that this routine is purely a helper for filesystem usage and should
1713 * not be called by generic code. Also note that by using this function the
Christoph Hellwigeead1912007-10-16 23:25:38 -07001714 * nameidata argument is passed to the filesystem methods and a filesystem
1715 * using this helper needs to be prepared for that.
1716 */
James Morris057f6c02007-04-26 00:12:05 -07001717struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1718{
James Morris057f6c02007-04-26 00:12:05 -07001719 struct qstr this;
Al Viro6a96ba52011-03-07 23:49:20 -05001720 unsigned long hash;
1721 unsigned int c;
James Morris057f6c02007-04-26 00:12:05 -07001722
David Woodhouse2f9092e2009-04-20 23:18:37 +01001723 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
1724
Al Viro6a96ba52011-03-07 23:49:20 -05001725 this.name = name;
1726 this.len = len;
1727 if (!len)
1728 return ERR_PTR(-EACCES);
1729
1730 hash = init_name_hash();
1731 while (len--) {
1732 c = *(const unsigned char *)name++;
1733 if (c == '/' || c == '\0')
1734 return ERR_PTR(-EACCES);
1735 hash = partial_name_hash(c, hash);
1736 }
1737 this.hash = end_name_hash(hash);
Al Viro5a202bc2011-03-08 14:17:44 -05001738 /*
1739 * See if the low-level filesystem might want
1740 * to use its own hash..
1741 */
1742 if (base->d_flags & DCACHE_OP_HASH) {
1743 int err = base->d_op->d_hash(base, base->d_inode, &this);
1744 if (err < 0)
1745 return ERR_PTR(err);
1746 }
Christoph Hellwigeead1912007-10-16 23:25:38 -07001747
Christoph Hellwig49705b72005-11-08 21:35:06 -08001748 return __lookup_hash(&this, base, NULL);
James Morris057f6c02007-04-26 00:12:05 -07001749}
1750
Al Viro2d8f3032008-07-22 09:59:21 -04001751int user_path_at(int dfd, const char __user *name, unsigned flags,
1752 struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753{
Al Viro2d8f3032008-07-22 09:59:21 -04001754 struct nameidata nd;
Al Virof52e0c12011-03-14 18:56:51 -04001755 char *tmp = getname_flags(name, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 int err = PTR_ERR(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 if (!IS_ERR(tmp)) {
Al Viro2d8f3032008-07-22 09:59:21 -04001758
1759 BUG_ON(flags & LOOKUP_PARENT);
1760
1761 err = do_path_lookup(dfd, tmp, flags, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 putname(tmp);
Al Viro2d8f3032008-07-22 09:59:21 -04001763 if (!err)
1764 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 }
1766 return err;
1767}
1768
Al Viro2ad94ae2008-07-21 09:32:51 -04001769static int user_path_parent(int dfd, const char __user *path,
1770 struct nameidata *nd, char **name)
1771{
1772 char *s = getname(path);
1773 int error;
1774
1775 if (IS_ERR(s))
1776 return PTR_ERR(s);
1777
1778 error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
1779 if (error)
1780 putname(s);
1781 else
1782 *name = s;
1783
1784 return error;
1785}
1786
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787/*
1788 * It's inline, so penalty for filesystems that don't use sticky bit is
1789 * minimal.
1790 */
1791static inline int check_sticky(struct inode *dir, struct inode *inode)
1792{
David Howellsda9592e2008-11-14 10:39:05 +11001793 uid_t fsuid = current_fsuid();
1794
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 if (!(dir->i_mode & S_ISVTX))
1796 return 0;
Serge E. Hallyne795b712011-03-23 16:43:25 -07001797 if (current_user_ns() != inode_userns(inode))
1798 goto other_userns;
David Howellsda9592e2008-11-14 10:39:05 +11001799 if (inode->i_uid == fsuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 return 0;
David Howellsda9592e2008-11-14 10:39:05 +11001801 if (dir->i_uid == fsuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 return 0;
Serge E. Hallyne795b712011-03-23 16:43:25 -07001803
1804other_userns:
1805 return !ns_capable(inode_userns(inode), CAP_FOWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806}
1807
1808/*
1809 * Check whether we can remove a link victim from directory dir, check
1810 * whether the type of victim is right.
1811 * 1. We can't do it if dir is read-only (done in permission())
1812 * 2. We should have write and exec permissions on dir
1813 * 3. We can't remove anything from append-only dir
1814 * 4. We can't do anything with immutable dir (done in permission())
1815 * 5. If the sticky bit on dir is set we should either
1816 * a. be owner of dir, or
1817 * b. be owner of victim, or
1818 * c. have CAP_FOWNER capability
1819 * 6. If the victim is append-only or immutable we can't do antyhing with
1820 * links pointing to it.
1821 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1822 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1823 * 9. We can't remove a root or mountpoint.
1824 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1825 * nfs_async_unlink().
1826 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001827static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828{
1829 int error;
1830
1831 if (!victim->d_inode)
1832 return -ENOENT;
1833
1834 BUG_ON(victim->d_parent->d_inode != dir);
Al Virocccc6bb2009-12-25 05:07:33 -05001835 audit_inode_child(victim, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
Al Virof419a2e2008-07-22 00:07:17 -04001837 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 if (error)
1839 return error;
1840 if (IS_APPEND(dir))
1841 return -EPERM;
1842 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
Hugh Dickinsf9454542008-11-19 15:36:38 -08001843 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 return -EPERM;
1845 if (isdir) {
1846 if (!S_ISDIR(victim->d_inode->i_mode))
1847 return -ENOTDIR;
1848 if (IS_ROOT(victim))
1849 return -EBUSY;
1850 } else if (S_ISDIR(victim->d_inode->i_mode))
1851 return -EISDIR;
1852 if (IS_DEADDIR(dir))
1853 return -ENOENT;
1854 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1855 return -EBUSY;
1856 return 0;
1857}
1858
1859/* Check whether we can create an object with dentry child in directory
1860 * dir.
1861 * 1. We can't do it if child already exists (open has special treatment for
1862 * this case, but since we are inlined it's OK)
1863 * 2. We can't do it if dir is read-only (done in permission())
1864 * 3. We should have write and exec permissions on dir
1865 * 4. We can't do it if dir is immutable (done in permission())
1866 */
Miklos Szeredia95164d2008-07-30 15:08:48 +02001867static inline int may_create(struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868{
1869 if (child->d_inode)
1870 return -EEXIST;
1871 if (IS_DEADDIR(dir))
1872 return -ENOENT;
Al Virof419a2e2008-07-22 00:07:17 -04001873 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874}
1875
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876/*
1877 * p1 and p2 should be directories on the same fs.
1878 */
1879struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1880{
1881 struct dentry *p;
1882
1883 if (p1 == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07001884 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 return NULL;
1886 }
1887
Arjan van de Vena11f3a02006-03-23 03:00:33 -08001888 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001890 p = d_ancestor(p2, p1);
1891 if (p) {
1892 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1893 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
1894 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 }
1896
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001897 p = d_ancestor(p1, p2);
1898 if (p) {
1899 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1900 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1901 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 }
1903
Ingo Molnarf2eace22006-07-03 00:25:05 -07001904 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1905 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 return NULL;
1907}
1908
1909void unlock_rename(struct dentry *p1, struct dentry *p2)
1910{
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001911 mutex_unlock(&p1->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 if (p1 != p2) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001913 mutex_unlock(&p2->d_inode->i_mutex);
Arjan van de Vena11f3a02006-03-23 03:00:33 -08001914 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 }
1916}
1917
1918int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1919 struct nameidata *nd)
1920{
Miklos Szeredia95164d2008-07-30 15:08:48 +02001921 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
1923 if (error)
1924 return error;
1925
Al Viroacfa4382008-12-04 10:06:33 -05001926 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 return -EACCES; /* shouldn't it be ENOSYS? */
1928 mode &= S_IALLUGO;
1929 mode |= S_IFREG;
1930 error = security_inode_create(dir, dentry, mode);
1931 if (error)
1932 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 error = dir->i_op->create(dir, dentry, mode, nd);
Stephen Smalleya74574a2005-09-09 13:01:44 -07001934 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00001935 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 return error;
1937}
1938
Al Viro73d049a2011-03-11 12:08:24 -05001939static int may_open(struct path *path, int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001941 struct dentry *dentry = path->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 struct inode *inode = dentry->d_inode;
1943 int error;
1944
Al Virobcda7652011-03-13 16:42:14 -04001945 /* O_PATH? */
1946 if (!acc_mode)
1947 return 0;
1948
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 if (!inode)
1950 return -ENOENT;
1951
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001952 switch (inode->i_mode & S_IFMT) {
1953 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001955 case S_IFDIR:
1956 if (acc_mode & MAY_WRITE)
1957 return -EISDIR;
1958 break;
1959 case S_IFBLK:
1960 case S_IFCHR:
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001961 if (path->mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001963 /*FALLTHRU*/
1964 case S_IFIFO:
1965 case S_IFSOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001967 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08001968 }
Dave Hansenb41572e2007-10-16 23:31:14 -07001969
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001970 error = inode_permission(inode, acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07001971 if (error)
1972 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05001973
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 /*
1975 * An append-only file must be opened in append mode for writing.
1976 */
1977 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05001978 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05001979 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05001981 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 }
1983
1984 /* O_NOATIME can only be set by the owner or superuser */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001985 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
Al Viro7715b522009-12-16 03:54:00 -05001986 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
1988 /*
1989 * Ensure there are no outstanding leases on the file.
1990 */
Al Virob65a9cf2009-12-16 06:27:40 -05001991 return break_lease(inode, flag);
Al Viro7715b522009-12-16 03:54:00 -05001992}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
Jeff Laytone1181ee2010-12-07 16:19:50 -05001994static int handle_truncate(struct file *filp)
Al Viro7715b522009-12-16 03:54:00 -05001995{
Jeff Laytone1181ee2010-12-07 16:19:50 -05001996 struct path *path = &filp->f_path;
Al Viro7715b522009-12-16 03:54:00 -05001997 struct inode *inode = path->dentry->d_inode;
1998 int error = get_write_access(inode);
1999 if (error)
2000 return error;
2001 /*
2002 * Refuse to truncate files with mandatory locks held on them.
2003 */
2004 error = locks_verify_locked(inode);
2005 if (!error)
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +09002006 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05002007 if (!error) {
2008 error = do_truncate(path->dentry, 0,
2009 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
Jeff Laytone1181ee2010-12-07 16:19:50 -05002010 filp);
Al Viro7715b522009-12-16 03:54:00 -05002011 }
2012 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04002013 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014}
2015
Dave Hansend57999e2008-02-15 14:37:27 -08002016static inline int open_to_namei_flags(int flag)
2017{
Al Viro8a5e9292011-06-25 19:15:54 -04002018 if ((flag & O_ACCMODE) == 3)
2019 flag--;
Dave Hansend57999e2008-02-15 14:37:27 -08002020 return flag;
2021}
2022
Nick Piggin31e6b012011-01-07 17:49:52 +11002023/*
Al Virofe2d35f2011-03-05 22:58:25 -05002024 * Handle the last step of open()
Nick Piggin31e6b012011-01-07 17:49:52 +11002025 */
Al Virofb1cc552009-12-24 01:58:28 -05002026static struct file *do_last(struct nameidata *nd, struct path *path,
Al Viroc3e380b2011-02-23 13:39:45 -05002027 const struct open_flags *op, const char *pathname)
Al Virofb1cc552009-12-24 01:58:28 -05002028{
Al Viroa1e28032009-12-24 02:12:06 -05002029 struct dentry *dir = nd->path.dentry;
Al Viro6c0d46c2011-03-09 00:59:59 -05002030 struct dentry *dentry;
Al Viroca344a892011-03-09 00:36:45 -05002031 int open_flag = op->open_flag;
Al Viro6c0d46c2011-03-09 00:59:59 -05002032 int will_truncate = open_flag & O_TRUNC;
Al Viroca344a892011-03-09 00:36:45 -05002033 int want_write = 0;
Al Virobcda7652011-03-13 16:42:14 -04002034 int acc_mode = op->acc_mode;
Al Virofb1cc552009-12-24 01:58:28 -05002035 struct file *filp;
Al Viro16c2cd72011-02-22 15:50:10 -05002036 int error;
Al Virofb1cc552009-12-24 01:58:28 -05002037
Al Viroc3e380b2011-02-23 13:39:45 -05002038 nd->flags &= ~LOOKUP_PARENT;
2039 nd->flags |= op->intent;
2040
Al Viro1f36f772009-12-26 10:56:19 -05002041 switch (nd->last_type) {
2042 case LAST_DOTDOT:
Neil Brown176306f2010-05-24 16:57:56 +10002043 case LAST_DOT:
Al Virofe2d35f2011-03-05 22:58:25 -05002044 error = handle_dots(nd, nd->last_type);
2045 if (error)
2046 return ERR_PTR(error);
Al Viro1f36f772009-12-26 10:56:19 -05002047 /* fallthrough */
Al Viro1f36f772009-12-26 10:56:19 -05002048 case LAST_ROOT:
Al Viro9f1fafe2011-03-25 11:00:12 -04002049 error = complete_walk(nd);
Al Viro16c2cd72011-02-22 15:50:10 -05002050 if (error)
Al Viro9f1fafe2011-03-25 11:00:12 -04002051 return ERR_PTR(error);
Al Virofe2d35f2011-03-05 22:58:25 -05002052 audit_inode(pathname, nd->path.dentry);
Al Viroca344a892011-03-09 00:36:45 -05002053 if (open_flag & O_CREAT) {
Al Virofe2d35f2011-03-05 22:58:25 -05002054 error = -EISDIR;
2055 goto exit;
2056 }
2057 goto ok;
Al Viro1f36f772009-12-26 10:56:19 -05002058 case LAST_BIND:
Al Viro9f1fafe2011-03-25 11:00:12 -04002059 error = complete_walk(nd);
Al Viro16c2cd72011-02-22 15:50:10 -05002060 if (error)
Al Viro9f1fafe2011-03-25 11:00:12 -04002061 return ERR_PTR(error);
Al Viro1f36f772009-12-26 10:56:19 -05002062 audit_inode(pathname, dir);
Al Viro67ee3ad2009-12-26 07:01:01 -05002063 goto ok;
Al Viro1f36f772009-12-26 10:56:19 -05002064 }
Al Viro67ee3ad2009-12-26 07:01:01 -05002065
Al Viroca344a892011-03-09 00:36:45 -05002066 if (!(open_flag & O_CREAT)) {
Al Virobcda7652011-03-13 16:42:14 -04002067 int symlink_ok = 0;
Al Virofe2d35f2011-03-05 22:58:25 -05002068 if (nd->last.name[nd->last.len])
2069 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
Al Virobcda7652011-03-13 16:42:14 -04002070 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2071 symlink_ok = 1;
Al Virofe2d35f2011-03-05 22:58:25 -05002072 /* we _can_ be in RCU mode here */
Al Viroce57dfc2011-03-13 19:58:58 -04002073 error = walk_component(nd, path, &nd->last, LAST_NORM,
2074 !symlink_ok);
2075 if (error < 0)
Al Virofe2d35f2011-03-05 22:58:25 -05002076 return ERR_PTR(error);
Al Viroce57dfc2011-03-13 19:58:58 -04002077 if (error) /* symlink */
Al Virofe2d35f2011-03-05 22:58:25 -05002078 return NULL;
Al Virofe2d35f2011-03-05 22:58:25 -05002079 /* sayonara */
Al Viro9f1fafe2011-03-25 11:00:12 -04002080 error = complete_walk(nd);
2081 if (error)
2082 return ERR_PTR(-ECHILD);
Al Virofe2d35f2011-03-05 22:58:25 -05002083
2084 error = -ENOTDIR;
2085 if (nd->flags & LOOKUP_DIRECTORY) {
Al Viroce57dfc2011-03-13 19:58:58 -04002086 if (!nd->inode->i_op->lookup)
Al Virofe2d35f2011-03-05 22:58:25 -05002087 goto exit;
2088 }
2089 audit_inode(pathname, nd->path.dentry);
2090 goto ok;
2091 }
2092
2093 /* create side of things */
Al Viro9f1fafe2011-03-25 11:00:12 -04002094 error = complete_walk(nd);
2095 if (error)
2096 return ERR_PTR(error);
Al Virofe2d35f2011-03-05 22:58:25 -05002097
2098 audit_inode(pathname, dir);
Al Viro16c2cd72011-02-22 15:50:10 -05002099 error = -EISDIR;
Al Viro1f36f772009-12-26 10:56:19 -05002100 /* trailing slashes? */
Nick Piggin31e6b012011-01-07 17:49:52 +11002101 if (nd->last.name[nd->last.len])
2102 goto exit;
Al Viroa2c36b42009-12-24 03:39:50 -05002103
Al Viroa1e28032009-12-24 02:12:06 -05002104 mutex_lock(&dir->d_inode->i_mutex);
2105
Al Viro6c0d46c2011-03-09 00:59:59 -05002106 dentry = lookup_hash(nd);
2107 error = PTR_ERR(dentry);
2108 if (IS_ERR(dentry)) {
Al Virofb1cc552009-12-24 01:58:28 -05002109 mutex_unlock(&dir->d_inode->i_mutex);
2110 goto exit;
2111 }
2112
Al Viro6c0d46c2011-03-09 00:59:59 -05002113 path->dentry = dentry;
2114 path->mnt = nd->path.mnt;
2115
Al Virofb1cc552009-12-24 01:58:28 -05002116 /* Negative dentry, just create the file */
Al Viro6c0d46c2011-03-09 00:59:59 -05002117 if (!dentry->d_inode) {
2118 int mode = op->mode;
2119 if (!IS_POSIXACL(dir->d_inode))
2120 mode &= ~current_umask();
Al Virofb1cc552009-12-24 01:58:28 -05002121 /*
2122 * This write is needed to ensure that a
Al Viro6c0d46c2011-03-09 00:59:59 -05002123 * rw->ro transition does not occur between
Al Virofb1cc552009-12-24 01:58:28 -05002124 * the time when the file is created and when
2125 * a permanent write count is taken through
2126 * the 'struct file' in nameidata_to_filp().
2127 */
2128 error = mnt_want_write(nd->path.mnt);
2129 if (error)
2130 goto exit_mutex_unlock;
Al Viroca344a892011-03-09 00:36:45 -05002131 want_write = 1;
Al Viro9b44f1b2011-03-09 00:17:27 -05002132 /* Don't check for write permission, don't truncate */
Al Viroca344a892011-03-09 00:36:45 -05002133 open_flag &= ~O_TRUNC;
Al Viro6c0d46c2011-03-09 00:59:59 -05002134 will_truncate = 0;
Al Virobcda7652011-03-13 16:42:14 -04002135 acc_mode = MAY_OPEN;
Al Viro6c0d46c2011-03-09 00:59:59 -05002136 error = security_path_mknod(&nd->path, dentry, mode, 0);
2137 if (error)
2138 goto exit_mutex_unlock;
2139 error = vfs_create(dir->d_inode, dentry, mode, nd);
2140 if (error)
2141 goto exit_mutex_unlock;
2142 mutex_unlock(&dir->d_inode->i_mutex);
2143 dput(nd->path.dentry);
2144 nd->path.dentry = dentry;
Al Viroca344a892011-03-09 00:36:45 -05002145 goto common;
Al Virofb1cc552009-12-24 01:58:28 -05002146 }
2147
2148 /*
2149 * It already exists.
2150 */
2151 mutex_unlock(&dir->d_inode->i_mutex);
2152 audit_inode(pathname, path->dentry);
2153
2154 error = -EEXIST;
Al Viroca344a892011-03-09 00:36:45 -05002155 if (open_flag & O_EXCL)
Al Virofb1cc552009-12-24 01:58:28 -05002156 goto exit_dput;
2157
David Howells9875cf82011-01-14 18:45:21 +00002158 error = follow_managed(path, nd->flags);
2159 if (error < 0)
2160 goto exit_dput;
Al Virofb1cc552009-12-24 01:58:28 -05002161
2162 error = -ENOENT;
2163 if (!path->dentry->d_inode)
2164 goto exit_dput;
Al Viro9e67f362009-12-26 07:04:50 -05002165
2166 if (path->dentry->d_inode->i_op->follow_link)
Al Virofb1cc552009-12-24 01:58:28 -05002167 return NULL;
Al Virofb1cc552009-12-24 01:58:28 -05002168
2169 path_to_nameidata(path, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11002170 nd->inode = path->dentry->d_inode;
Al Virofb1cc552009-12-24 01:58:28 -05002171 error = -EISDIR;
Nick Piggin31e6b012011-01-07 17:49:52 +11002172 if (S_ISDIR(nd->inode->i_mode))
Al Virofb1cc552009-12-24 01:58:28 -05002173 goto exit;
Al Viro67ee3ad2009-12-26 07:01:01 -05002174ok:
Al Viro6c0d46c2011-03-09 00:59:59 -05002175 if (!S_ISREG(nd->inode->i_mode))
2176 will_truncate = 0;
2177
Al Viro0f9d1a12011-03-09 00:13:14 -05002178 if (will_truncate) {
2179 error = mnt_want_write(nd->path.mnt);
2180 if (error)
2181 goto exit;
Al Viroca344a892011-03-09 00:36:45 -05002182 want_write = 1;
Al Viro0f9d1a12011-03-09 00:13:14 -05002183 }
Al Viroca344a892011-03-09 00:36:45 -05002184common:
Al Virobcda7652011-03-13 16:42:14 -04002185 error = may_open(&nd->path, acc_mode, open_flag);
Al Viroca344a892011-03-09 00:36:45 -05002186 if (error)
Al Viro0f9d1a12011-03-09 00:13:14 -05002187 goto exit;
Al Viro0f9d1a12011-03-09 00:13:14 -05002188 filp = nameidata_to_filp(nd);
2189 if (!IS_ERR(filp)) {
2190 error = ima_file_check(filp, op->acc_mode);
2191 if (error) {
2192 fput(filp);
2193 filp = ERR_PTR(error);
2194 }
2195 }
2196 if (!IS_ERR(filp)) {
2197 if (will_truncate) {
2198 error = handle_truncate(filp);
2199 if (error) {
2200 fput(filp);
2201 filp = ERR_PTR(error);
2202 }
2203 }
2204 }
Al Viroca344a892011-03-09 00:36:45 -05002205out:
2206 if (want_write)
Al Viro0f9d1a12011-03-09 00:13:14 -05002207 mnt_drop_write(nd->path.mnt);
2208 path_put(&nd->path);
Al Virofb1cc552009-12-24 01:58:28 -05002209 return filp;
2210
2211exit_mutex_unlock:
2212 mutex_unlock(&dir->d_inode->i_mutex);
2213exit_dput:
2214 path_put_conditional(path, nd);
2215exit:
Al Viroca344a892011-03-09 00:36:45 -05002216 filp = ERR_PTR(error);
2217 goto out;
Al Virofb1cc552009-12-24 01:58:28 -05002218}
2219
Al Viro13aab422011-02-23 17:54:08 -05002220static struct file *path_openat(int dfd, const char *pathname,
Al Viro73d049a2011-03-11 12:08:24 -05002221 struct nameidata *nd, const struct open_flags *op, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222{
Al Virofe2d35f2011-03-05 22:58:25 -05002223 struct file *base = NULL;
Dave Hansen4a3fd212008-02-15 14:37:48 -08002224 struct file *filp;
Al Viro9850c052010-01-13 15:01:15 -05002225 struct path path;
Al Viro13aab422011-02-23 17:54:08 -05002226 int error;
Nick Piggin31e6b012011-01-07 17:49:52 +11002227
2228 filp = get_empty_filp();
2229 if (!filp)
2230 return ERR_PTR(-ENFILE);
2231
Al Viro47c805d2011-02-23 17:44:09 -05002232 filp->f_flags = op->open_flag;
Al Viro73d049a2011-03-11 12:08:24 -05002233 nd->intent.open.file = filp;
2234 nd->intent.open.flags = open_to_namei_flags(op->open_flag);
2235 nd->intent.open.create_mode = op->mode;
Nick Piggin31e6b012011-01-07 17:49:52 +11002236
Al Viro73d049a2011-03-11 12:08:24 -05002237 error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
Nick Piggin31e6b012011-01-07 17:49:52 +11002238 if (unlikely(error))
Al Viro13aab422011-02-23 17:54:08 -05002239 goto out_filp;
Nick Piggin31e6b012011-01-07 17:49:52 +11002240
Al Virofe2d35f2011-03-05 22:58:25 -05002241 current->total_link_count = 0;
Al Viro73d049a2011-03-11 12:08:24 -05002242 error = link_path_walk(pathname, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11002243 if (unlikely(error))
2244 goto out_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
Al Viro73d049a2011-03-11 12:08:24 -05002246 filp = do_last(nd, &path, op, pathname);
Al Viro806b6812009-12-26 07:16:40 -05002247 while (unlikely(!filp)) { /* trailing symlink */
Nick Piggin7b9337a2011-01-14 08:42:43 +00002248 struct path link = path;
Al Virodef4af32009-12-26 08:37:05 -05002249 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04002250 if (!(nd->flags & LOOKUP_FOLLOW)) {
Al Viro73d049a2011-03-11 12:08:24 -05002251 path_put_conditional(&path, nd);
2252 path_put(&nd->path);
Al Viro40b39132011-03-09 16:22:18 -05002253 filp = ERR_PTR(-ELOOP);
2254 break;
2255 }
Al Viro73d049a2011-03-11 12:08:24 -05002256 nd->flags |= LOOKUP_PARENT;
2257 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
Al Viro574197e2011-03-14 22:20:34 -04002258 error = follow_link(&link, nd, &cookie);
Al Viroc3e380b2011-02-23 13:39:45 -05002259 if (unlikely(error))
Al Virof1afe9e2011-02-22 22:27:28 -05002260 filp = ERR_PTR(error);
Al Viroc3e380b2011-02-23 13:39:45 -05002261 else
Al Viro73d049a2011-03-11 12:08:24 -05002262 filp = do_last(nd, &path, op, pathname);
Al Viro574197e2011-03-14 22:20:34 -04002263 put_link(nd, &link, cookie);
Al Viro806b6812009-12-26 07:16:40 -05002264 }
Al Viro10fa8e62009-12-26 07:09:49 -05002265out:
Al Viro73d049a2011-03-11 12:08:24 -05002266 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
2267 path_put(&nd->root);
Al Virofe2d35f2011-03-05 22:58:25 -05002268 if (base)
2269 fput(base);
Al Viro73d049a2011-03-11 12:08:24 -05002270 release_open_intent(nd);
Al Viro10fa8e62009-12-26 07:09:49 -05002271 return filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
Nick Piggin31e6b012011-01-07 17:49:52 +11002273out_filp:
Al Viro806b6812009-12-26 07:16:40 -05002274 filp = ERR_PTR(error);
Al Viro10fa8e62009-12-26 07:09:49 -05002275 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276}
2277
Al Viro13aab422011-02-23 17:54:08 -05002278struct file *do_filp_open(int dfd, const char *pathname,
2279 const struct open_flags *op, int flags)
2280{
Al Viro73d049a2011-03-11 12:08:24 -05002281 struct nameidata nd;
Al Viro13aab422011-02-23 17:54:08 -05002282 struct file *filp;
2283
Al Viro73d049a2011-03-11 12:08:24 -05002284 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
Al Viro13aab422011-02-23 17:54:08 -05002285 if (unlikely(filp == ERR_PTR(-ECHILD)))
Al Viro73d049a2011-03-11 12:08:24 -05002286 filp = path_openat(dfd, pathname, &nd, op, flags);
Al Viro13aab422011-02-23 17:54:08 -05002287 if (unlikely(filp == ERR_PTR(-ESTALE)))
Al Viro73d049a2011-03-11 12:08:24 -05002288 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
Al Viro13aab422011-02-23 17:54:08 -05002289 return filp;
2290}
2291
Al Viro73d049a2011-03-11 12:08:24 -05002292struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
2293 const char *name, const struct open_flags *op, int flags)
2294{
2295 struct nameidata nd;
2296 struct file *file;
2297
2298 nd.root.mnt = mnt;
2299 nd.root.dentry = dentry;
2300
2301 flags |= LOOKUP_ROOT;
2302
Al Virobcda7652011-03-13 16:42:14 -04002303 if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
Al Viro73d049a2011-03-11 12:08:24 -05002304 return ERR_PTR(-ELOOP);
2305
2306 file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
2307 if (unlikely(file == ERR_PTR(-ECHILD)))
2308 file = path_openat(-1, name, &nd, op, flags);
2309 if (unlikely(file == ERR_PTR(-ESTALE)))
2310 file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
2311 return file;
2312}
2313
Al Viroed75e952011-06-27 16:53:43 -04002314struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002316 struct dentry *dentry = ERR_PTR(-EEXIST);
Al Viroed75e952011-06-27 16:53:43 -04002317 struct nameidata nd;
2318 int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
2319 if (error)
2320 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002322 /*
2323 * Yucky last component or no last component at all?
2324 * (foo/., foo/.., /////)
2325 */
Al Viroed75e952011-06-27 16:53:43 -04002326 if (nd.last_type != LAST_NORM)
2327 goto out;
2328 nd.flags &= ~LOOKUP_PARENT;
2329 nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
2330 nd.intent.open.flags = O_EXCL;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002331
2332 /*
2333 * Do the final lookup.
2334 */
Al Viroed75e952011-06-27 16:53:43 -04002335 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2336 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 if (IS_ERR(dentry))
2338 goto fail;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002339
Al Viroe9baf6e2008-05-15 04:49:12 -04002340 if (dentry->d_inode)
2341 goto eexist;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002342 /*
2343 * Special case - lookup gave negative, but... we had foo/bar/
2344 * From the vfs_mknod() POV we just have a negative dentry -
2345 * all is fine. Let's be bastards - you had / on the end, you've
2346 * been asking for (non-existent) directory. -ENOENT for you.
2347 */
Al Viroed75e952011-06-27 16:53:43 -04002348 if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
Al Viroe9baf6e2008-05-15 04:49:12 -04002349 dput(dentry);
2350 dentry = ERR_PTR(-ENOENT);
Al Viroed75e952011-06-27 16:53:43 -04002351 goto fail;
Al Viroe9baf6e2008-05-15 04:49:12 -04002352 }
Al Viroed75e952011-06-27 16:53:43 -04002353 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 return dentry;
Al Viroe9baf6e2008-05-15 04:49:12 -04002355eexist:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 dput(dentry);
Al Viroe9baf6e2008-05-15 04:49:12 -04002357 dentry = ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358fail:
Al Viroed75e952011-06-27 16:53:43 -04002359 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2360out:
2361 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 return dentry;
2363}
Al Virodae6ad82011-06-26 11:50:15 -04002364EXPORT_SYMBOL(kern_path_create);
2365
2366struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir)
2367{
2368 char *tmp = getname(pathname);
2369 struct dentry *res;
2370 if (IS_ERR(tmp))
2371 return ERR_CAST(tmp);
2372 res = kern_path_create(dfd, tmp, path, is_dir);
2373 putname(tmp);
2374 return res;
2375}
2376EXPORT_SYMBOL(user_path_create);
2377
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
2379{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002380 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
2382 if (error)
2383 return error;
2384
Serge E. Hallyne795b712011-03-23 16:43:25 -07002385 if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
2386 !ns_capable(inode_userns(dir), CAP_MKNOD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 return -EPERM;
2388
Al Viroacfa4382008-12-04 10:06:33 -05002389 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 return -EPERM;
2391
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07002392 error = devcgroup_inode_mknod(mode, dev);
2393 if (error)
2394 return error;
2395
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 error = security_inode_mknod(dir, dentry, mode, dev);
2397 if (error)
2398 return error;
2399
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002401 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002402 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 return error;
2404}
2405
Dave Hansen463c3192008-02-15 14:37:57 -08002406static int may_mknod(mode_t mode)
2407{
2408 switch (mode & S_IFMT) {
2409 case S_IFREG:
2410 case S_IFCHR:
2411 case S_IFBLK:
2412 case S_IFIFO:
2413 case S_IFSOCK:
2414 case 0: /* zero mode translates to S_IFREG */
2415 return 0;
2416 case S_IFDIR:
2417 return -EPERM;
2418 default:
2419 return -EINVAL;
2420 }
2421}
2422
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002423SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
2424 unsigned, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425{
Al Viro2ad94ae2008-07-21 09:32:51 -04002426 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04002427 struct path path;
2428 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
2430 if (S_ISDIR(mode))
2431 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432
Al Virodae6ad82011-06-26 11:50:15 -04002433 dentry = user_path_create(dfd, filename, &path, 0);
2434 if (IS_ERR(dentry))
2435 return PTR_ERR(dentry);
Al Viro2ad94ae2008-07-21 09:32:51 -04002436
Al Virodae6ad82011-06-26 11:50:15 -04002437 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04002438 mode &= ~current_umask();
Dave Hansen463c3192008-02-15 14:37:57 -08002439 error = may_mknod(mode);
2440 if (error)
2441 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04002442 error = mnt_want_write(path.mnt);
Dave Hansen463c3192008-02-15 14:37:57 -08002443 if (error)
2444 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04002445 error = security_path_mknod(&path, dentry, mode, dev);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002446 if (error)
2447 goto out_drop_write;
Dave Hansen463c3192008-02-15 14:37:57 -08002448 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 case 0: case S_IFREG:
Al Virodae6ad82011-06-26 11:50:15 -04002450 error = vfs_create(path.dentry->d_inode,dentry,mode,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 break;
2452 case S_IFCHR: case S_IFBLK:
Al Virodae6ad82011-06-26 11:50:15 -04002453 error = vfs_mknod(path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 new_decode_dev(dev));
2455 break;
2456 case S_IFIFO: case S_IFSOCK:
Al Virodae6ad82011-06-26 11:50:15 -04002457 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 }
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002460out_drop_write:
Al Virodae6ad82011-06-26 11:50:15 -04002461 mnt_drop_write(path.mnt);
Dave Hansen463c3192008-02-15 14:37:57 -08002462out_dput:
2463 dput(dentry);
Al Virodae6ad82011-06-26 11:50:15 -04002464 mutex_unlock(&path.dentry->d_inode->i_mutex);
2465 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
2467 return error;
2468}
2469
Heiko Carstens3480b252009-01-14 14:14:16 +01002470SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002471{
2472 return sys_mknodat(AT_FDCWD, filename, mode, dev);
2473}
2474
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2476{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002477 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
2479 if (error)
2480 return error;
2481
Al Viroacfa4382008-12-04 10:06:33 -05002482 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 return -EPERM;
2484
2485 mode &= (S_IRWXUGO|S_ISVTX);
2486 error = security_inode_mkdir(dir, dentry, mode);
2487 if (error)
2488 return error;
2489
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002491 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002492 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 return error;
2494}
2495
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002496SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497{
Dave Hansen6902d922006-09-30 23:29:01 -07002498 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04002499 struct path path;
2500 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
Al Virodae6ad82011-06-26 11:50:15 -04002502 dentry = user_path_create(dfd, pathname, &path, 1);
Dave Hansen6902d922006-09-30 23:29:01 -07002503 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04002504 return PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002505
Al Virodae6ad82011-06-26 11:50:15 -04002506 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04002507 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04002508 error = mnt_want_write(path.mnt);
Dave Hansen463c3192008-02-15 14:37:57 -08002509 if (error)
2510 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04002511 error = security_path_mkdir(&path, dentry, mode);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002512 if (error)
2513 goto out_drop_write;
Al Virodae6ad82011-06-26 11:50:15 -04002514 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002515out_drop_write:
Al Virodae6ad82011-06-26 11:50:15 -04002516 mnt_drop_write(path.mnt);
Dave Hansen463c3192008-02-15 14:37:57 -08002517out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002518 dput(dentry);
Al Virodae6ad82011-06-26 11:50:15 -04002519 mutex_unlock(&path.dentry->d_inode->i_mutex);
2520 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 return error;
2522}
2523
Heiko Carstens3cdad422009-01-14 14:14:22 +01002524SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002525{
2526 return sys_mkdirat(AT_FDCWD, pathname, mode);
2527}
2528
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529/*
Sage Weila71905f2011-05-24 13:06:08 -07002530 * The dentry_unhash() helper will try to drop the dentry early: we
2531 * should have a usage count of 2 if we're the only user of this
2532 * dentry, and if that is true (possibly after pruning the dcache),
2533 * then we drop the dentry now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 *
2535 * A low-level filesystem can, if it choses, legally
2536 * do a
2537 *
2538 * if (!d_unhashed(dentry))
2539 * return -EBUSY;
2540 *
2541 * if it cannot handle the case of removing a directory
2542 * that is still in use by something else..
2543 */
2544void dentry_unhash(struct dentry *dentry)
2545{
Vasily Averindc168422006-12-06 20:37:07 -08002546 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 spin_lock(&dentry->d_lock);
Sage Weil64252c72011-05-24 13:06:05 -07002548 if (dentry->d_count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 __d_drop(dentry);
2550 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551}
2552
2553int vfs_rmdir(struct inode *dir, struct dentry *dentry)
2554{
2555 int error = may_delete(dir, dentry, 1);
2556
2557 if (error)
2558 return error;
2559
Al Viroacfa4382008-12-04 10:06:33 -05002560 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 return -EPERM;
2562
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002563 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
Sage Weil912dbc12011-05-24 13:06:11 -07002565 error = -EBUSY;
2566 if (d_mountpoint(dentry))
2567 goto out;
2568
2569 error = security_inode_rmdir(dir, dentry);
2570 if (error)
2571 goto out;
2572
Sage Weil3cebde22011-05-29 21:20:59 -07002573 shrink_dcache_parent(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07002574 error = dir->i_op->rmdir(dir, dentry);
2575 if (error)
2576 goto out;
2577
2578 dentry->d_inode->i_flags |= S_DEAD;
2579 dont_mount(dentry);
2580
2581out:
2582 mutex_unlock(&dentry->d_inode->i_mutex);
2583 if (!error)
2584 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 return error;
2586}
2587
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002588static long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589{
2590 int error = 0;
2591 char * name;
2592 struct dentry *dentry;
2593 struct nameidata nd;
2594
Al Viro2ad94ae2008-07-21 09:32:51 -04002595 error = user_path_parent(dfd, pathname, &nd, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002597 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598
2599 switch(nd.last_type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002600 case LAST_DOTDOT:
2601 error = -ENOTEMPTY;
2602 goto exit1;
2603 case LAST_DOT:
2604 error = -EINVAL;
2605 goto exit1;
2606 case LAST_ROOT:
2607 error = -EBUSY;
2608 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002610
2611 nd.flags &= ~LOOKUP_PARENT;
2612
Jan Blunck4ac91372008-02-14 19:34:32 -08002613 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002614 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002616 if (IS_ERR(dentry))
2617 goto exit2;
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04002618 if (!dentry->d_inode) {
2619 error = -ENOENT;
2620 goto exit3;
2621 }
Dave Hansen06227532008-02-15 14:37:34 -08002622 error = mnt_want_write(nd.path.mnt);
2623 if (error)
2624 goto exit3;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002625 error = security_path_rmdir(&nd.path, dentry);
2626 if (error)
2627 goto exit4;
Jan Blunck4ac91372008-02-14 19:34:32 -08002628 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002629exit4:
Dave Hansen06227532008-02-15 14:37:34 -08002630 mnt_drop_write(nd.path.mnt);
2631exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07002632 dput(dentry);
2633exit2:
Jan Blunck4ac91372008-02-14 19:34:32 -08002634 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002636 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 putname(name);
2638 return error;
2639}
2640
Heiko Carstens3cdad422009-01-14 14:14:22 +01002641SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002642{
2643 return do_rmdir(AT_FDCWD, pathname);
2644}
2645
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646int vfs_unlink(struct inode *dir, struct dentry *dentry)
2647{
2648 int error = may_delete(dir, dentry, 0);
2649
2650 if (error)
2651 return error;
2652
Al Viroacfa4382008-12-04 10:06:33 -05002653 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 return -EPERM;
2655
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002656 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 if (d_mountpoint(dentry))
2658 error = -EBUSY;
2659 else {
2660 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05002661 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 error = dir->i_op->unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05002663 if (!error)
Al Virod83c49f2010-04-30 17:17:09 -04002664 dont_mount(dentry);
Al Virobec10522010-03-03 14:12:08 -05002665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002667 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668
2669 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2670 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
Jan Karaece95912008-02-06 01:37:13 -08002671 fsnotify_link_count(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 }
Robert Love0eeca282005-07-12 17:06:03 -04002674
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 return error;
2676}
2677
2678/*
2679 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002680 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 * writeout happening, and we don't want to prevent access to the directory
2682 * while waiting on the I/O.
2683 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002684static long do_unlinkat(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685{
Al Viro2ad94ae2008-07-21 09:32:51 -04002686 int error;
2687 char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 struct dentry *dentry;
2689 struct nameidata nd;
2690 struct inode *inode = NULL;
2691
Al Viro2ad94ae2008-07-21 09:32:51 -04002692 error = user_path_parent(dfd, pathname, &nd, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002694 return error;
2695
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 error = -EISDIR;
2697 if (nd.last_type != LAST_NORM)
2698 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002699
2700 nd.flags &= ~LOOKUP_PARENT;
2701
Jan Blunck4ac91372008-02-14 19:34:32 -08002702 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002703 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 error = PTR_ERR(dentry);
2705 if (!IS_ERR(dentry)) {
2706 /* Why not before? Because we want correct error value */
Török Edwin50338b82011-06-16 00:06:14 +03002707 if (nd.last.name[nd.last.len])
2708 goto slashes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 inode = dentry->d_inode;
Török Edwin50338b82011-06-16 00:06:14 +03002710 if (!inode)
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04002711 goto slashes;
2712 ihold(inode);
Dave Hansen06227532008-02-15 14:37:34 -08002713 error = mnt_want_write(nd.path.mnt);
2714 if (error)
2715 goto exit2;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002716 error = security_path_unlink(&nd.path, dentry);
2717 if (error)
2718 goto exit3;
Jan Blunck4ac91372008-02-14 19:34:32 -08002719 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002720exit3:
Dave Hansen06227532008-02-15 14:37:34 -08002721 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 exit2:
2723 dput(dentry);
2724 }
Jan Blunck4ac91372008-02-14 19:34:32 -08002725 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 if (inode)
2727 iput(inode); /* truncate the inode here */
2728exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002729 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 putname(name);
2731 return error;
2732
2733slashes:
2734 error = !dentry->d_inode ? -ENOENT :
2735 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2736 goto exit2;
2737}
2738
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002739SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002740{
2741 if ((flag & ~AT_REMOVEDIR) != 0)
2742 return -EINVAL;
2743
2744 if (flag & AT_REMOVEDIR)
2745 return do_rmdir(dfd, pathname);
2746
2747 return do_unlinkat(dfd, pathname);
2748}
2749
Heiko Carstens3480b252009-01-14 14:14:16 +01002750SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002751{
2752 return do_unlinkat(AT_FDCWD, pathname);
2753}
2754
Miklos Szeredidb2e7472008-06-24 16:50:16 +02002755int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002757 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758
2759 if (error)
2760 return error;
2761
Al Viroacfa4382008-12-04 10:06:33 -05002762 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 return -EPERM;
2764
2765 error = security_inode_symlink(dir, dentry, oldname);
2766 if (error)
2767 return error;
2768
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002770 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002771 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 return error;
2773}
2774
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002775SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
2776 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777{
Al Viro2ad94ae2008-07-21 09:32:51 -04002778 int error;
2779 char *from;
Dave Hansen6902d922006-09-30 23:29:01 -07002780 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04002781 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782
2783 from = getname(oldname);
Al Viro2ad94ae2008-07-21 09:32:51 -04002784 if (IS_ERR(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 return PTR_ERR(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04002786
Al Virodae6ad82011-06-26 11:50:15 -04002787 dentry = user_path_create(newdfd, newname, &path, 0);
Dave Hansen6902d922006-09-30 23:29:01 -07002788 error = PTR_ERR(dentry);
2789 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04002790 goto out_putname;
Dave Hansen6902d922006-09-30 23:29:01 -07002791
Al Virodae6ad82011-06-26 11:50:15 -04002792 error = mnt_want_write(path.mnt);
Dave Hansen75c3f292008-02-15 14:37:45 -08002793 if (error)
2794 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04002795 error = security_path_symlink(&path, dentry, from);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002796 if (error)
2797 goto out_drop_write;
Al Virodae6ad82011-06-26 11:50:15 -04002798 error = vfs_symlink(path.dentry->d_inode, dentry, from);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002799out_drop_write:
Al Virodae6ad82011-06-26 11:50:15 -04002800 mnt_drop_write(path.mnt);
Dave Hansen75c3f292008-02-15 14:37:45 -08002801out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002802 dput(dentry);
Al Virodae6ad82011-06-26 11:50:15 -04002803 mutex_unlock(&path.dentry->d_inode->i_mutex);
2804 path_put(&path);
Dave Hansen6902d922006-09-30 23:29:01 -07002805out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 putname(from);
2807 return error;
2808}
2809
Heiko Carstens3480b252009-01-14 14:14:16 +01002810SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002811{
2812 return sys_symlinkat(oldname, AT_FDCWD, newname);
2813}
2814
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2816{
2817 struct inode *inode = old_dentry->d_inode;
2818 int error;
2819
2820 if (!inode)
2821 return -ENOENT;
2822
Miklos Szeredia95164d2008-07-30 15:08:48 +02002823 error = may_create(dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 if (error)
2825 return error;
2826
2827 if (dir->i_sb != inode->i_sb)
2828 return -EXDEV;
2829
2830 /*
2831 * A link to an append-only or immutable file cannot be created.
2832 */
2833 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2834 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05002835 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002837 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 return -EPERM;
2839
2840 error = security_inode_link(old_dentry, dir, new_dentry);
2841 if (error)
2842 return error;
2843
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002844 mutex_lock(&inode->i_mutex);
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05302845 /* Make sure we don't allow creating hardlink to an unlinked file */
2846 if (inode->i_nlink == 0)
2847 error = -ENOENT;
2848 else
2849 error = dir->i_op->link(old_dentry, dir, new_dentry);
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002850 mutex_unlock(&inode->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07002851 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002852 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 return error;
2854}
2855
2856/*
2857 * Hardlinks are often used in delicate situations. We avoid
2858 * security-related surprises by not following symlinks on the
2859 * newname. --KAB
2860 *
2861 * We don't follow them on the oldname either to be compatible
2862 * with linux 2.0, and to avoid hard-linking to directories
2863 * and other special files. --ADM
2864 */
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002865SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
2866 int, newdfd, const char __user *, newname, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867{
2868 struct dentry *new_dentry;
Al Virodae6ad82011-06-26 11:50:15 -04002869 struct path old_path, new_path;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05302870 int how = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05302873 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002874 return -EINVAL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05302875 /*
2876 * To use null names we require CAP_DAC_READ_SEARCH
2877 * This ensures that not everyone will be able to create
2878 * handlink using the passed filedescriptor.
2879 */
2880 if (flags & AT_EMPTY_PATH) {
2881 if (!capable(CAP_DAC_READ_SEARCH))
2882 return -ENOENT;
2883 how = LOOKUP_EMPTY;
2884 }
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002885
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05302886 if (flags & AT_SYMLINK_FOLLOW)
2887 how |= LOOKUP_FOLLOW;
2888
2889 error = user_path_at(olddfd, oldname, how, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002891 return error;
2892
Al Virodae6ad82011-06-26 11:50:15 -04002893 new_dentry = user_path_create(newdfd, newname, &new_path, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002895 if (IS_ERR(new_dentry))
Al Virodae6ad82011-06-26 11:50:15 -04002896 goto out;
2897
2898 error = -EXDEV;
2899 if (old_path.mnt != new_path.mnt)
2900 goto out_dput;
2901 error = mnt_want_write(new_path.mnt);
Dave Hansen75c3f292008-02-15 14:37:45 -08002902 if (error)
2903 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04002904 error = security_path_link(old_path.dentry, &new_path, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002905 if (error)
2906 goto out_drop_write;
Al Virodae6ad82011-06-26 11:50:15 -04002907 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002908out_drop_write:
Al Virodae6ad82011-06-26 11:50:15 -04002909 mnt_drop_write(new_path.mnt);
Dave Hansen75c3f292008-02-15 14:37:45 -08002910out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002911 dput(new_dentry);
Al Virodae6ad82011-06-26 11:50:15 -04002912 mutex_unlock(&new_path.dentry->d_inode->i_mutex);
2913 path_put(&new_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914out:
Al Viro2d8f3032008-07-22 09:59:21 -04002915 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916
2917 return error;
2918}
2919
Heiko Carstens3480b252009-01-14 14:14:16 +01002920SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002921{
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002922 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002923}
2924
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925/*
2926 * The worst of all namespace operations - renaming directory. "Perverted"
2927 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2928 * Problems:
2929 * a) we can get into loop creation. Check is done in is_subdir().
2930 * b) race potential - two innocent renames can create a loop together.
2931 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002932 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 * story.
2934 * c) we have to lock _three_ objects - parents and victim (if it exists).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002935 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 * whether the target exists). Solution: try to be smart with locking
2937 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002938 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 * move will be locked. Thus we can rank directories by the tree
2940 * (ancestors first) and rank all non-directories after them.
2941 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002942 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 * HOWEVER, it relies on the assumption that any object with ->lookup()
2944 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2945 * we'd better make sure that there's no link(2) for them.
Sage Weile4eaac02011-05-24 13:06:07 -07002946 * d) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002947 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05002949 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 * locking].
2951 */
Adrian Bunk75c96f82005-05-05 16:16:09 -07002952static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
2953 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954{
2955 int error = 0;
Sage Weil9055cba2011-05-24 13:06:12 -07002956 struct inode *target = new_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957
2958 /*
2959 * If we are going to change the parent - check write permissions,
2960 * we'll need to flip '..'.
2961 */
2962 if (new_dir != old_dir) {
Al Virof419a2e2008-07-22 00:07:17 -04002963 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 if (error)
2965 return error;
2966 }
2967
2968 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2969 if (error)
2970 return error;
2971
Al Virod83c49f2010-04-30 17:17:09 -04002972 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002973 mutex_lock(&target->i_mutex);
Sage Weil9055cba2011-05-24 13:06:12 -07002974
2975 error = -EBUSY;
2976 if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
2977 goto out;
2978
Sage Weil3cebde22011-05-29 21:20:59 -07002979 if (target)
2980 shrink_dcache_parent(new_dentry);
Sage Weil9055cba2011-05-24 13:06:12 -07002981 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2982 if (error)
2983 goto out;
2984
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 if (target) {
Sage Weil9055cba2011-05-24 13:06:12 -07002986 target->i_flags |= S_DEAD;
2987 dont_mount(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 }
Sage Weil9055cba2011-05-24 13:06:12 -07002989out:
2990 if (target)
2991 mutex_unlock(&target->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07002992 if (!error)
Mark Fasheh349457c2006-09-08 14:22:21 -07002993 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
2994 d_move(old_dentry,new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 return error;
2996}
2997
Adrian Bunk75c96f82005-05-05 16:16:09 -07002998static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
2999 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000{
Sage Weil51892bb2011-05-24 13:06:13 -07003001 struct inode *target = new_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 int error;
3003
3004 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3005 if (error)
3006 return error;
3007
3008 dget(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003010 mutex_lock(&target->i_mutex);
Sage Weil51892bb2011-05-24 13:06:13 -07003011
3012 error = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
Sage Weil51892bb2011-05-24 13:06:13 -07003014 goto out;
3015
3016 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3017 if (error)
3018 goto out;
3019
3020 if (target)
3021 dont_mount(new_dentry);
3022 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3023 d_move(old_dentry, new_dentry);
3024out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003026 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 dput(new_dentry);
3028 return error;
3029}
3030
3031int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
3032 struct inode *new_dir, struct dentry *new_dentry)
3033{
3034 int error;
3035 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
Eric Paris59b0df22010-02-08 12:53:52 -05003036 const unsigned char *old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037
3038 if (old_dentry->d_inode == new_dentry->d_inode)
3039 return 0;
3040
3041 error = may_delete(old_dir, old_dentry, is_dir);
3042 if (error)
3043 return error;
3044
3045 if (!new_dentry->d_inode)
Miklos Szeredia95164d2008-07-30 15:08:48 +02003046 error = may_create(new_dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 else
3048 error = may_delete(new_dir, new_dentry, is_dir);
3049 if (error)
3050 return error;
3051
Al Viroacfa4382008-12-04 10:06:33 -05003052 if (!old_dir->i_op->rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 return -EPERM;
3054
Robert Love0eeca282005-07-12 17:06:03 -04003055 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
3056
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 if (is_dir)
3058 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
3059 else
3060 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
Al Viro123df292009-12-25 04:57:57 -05003061 if (!error)
3062 fsnotify_move(old_dir, new_dir, old_name, is_dir,
Al Viro5a190ae2007-06-07 12:19:32 -04003063 new_dentry->d_inode, old_dentry);
Robert Love0eeca282005-07-12 17:06:03 -04003064 fsnotify_oldname_free(old_name);
3065
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 return error;
3067}
3068
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003069SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
3070 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071{
Al Viro2ad94ae2008-07-21 09:32:51 -04003072 struct dentry *old_dir, *new_dir;
3073 struct dentry *old_dentry, *new_dentry;
3074 struct dentry *trap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 struct nameidata oldnd, newnd;
Al Viro2ad94ae2008-07-21 09:32:51 -04003076 char *from;
3077 char *to;
3078 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079
Al Viro2ad94ae2008-07-21 09:32:51 -04003080 error = user_path_parent(olddfd, oldname, &oldnd, &from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 if (error)
3082 goto exit;
3083
Al Viro2ad94ae2008-07-21 09:32:51 -04003084 error = user_path_parent(newdfd, newname, &newnd, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 if (error)
3086 goto exit1;
3087
3088 error = -EXDEV;
Jan Blunck4ac91372008-02-14 19:34:32 -08003089 if (oldnd.path.mnt != newnd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 goto exit2;
3091
Jan Blunck4ac91372008-02-14 19:34:32 -08003092 old_dir = oldnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 error = -EBUSY;
3094 if (oldnd.last_type != LAST_NORM)
3095 goto exit2;
3096
Jan Blunck4ac91372008-02-14 19:34:32 -08003097 new_dir = newnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 if (newnd.last_type != LAST_NORM)
3099 goto exit2;
3100
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003101 oldnd.flags &= ~LOOKUP_PARENT;
3102 newnd.flags &= ~LOOKUP_PARENT;
OGAWA Hirofumi4e9ed2f2008-10-16 07:50:29 +09003103 newnd.flags |= LOOKUP_RENAME_TARGET;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003104
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 trap = lock_rename(new_dir, old_dir);
3106
Christoph Hellwig49705b72005-11-08 21:35:06 -08003107 old_dentry = lookup_hash(&oldnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 error = PTR_ERR(old_dentry);
3109 if (IS_ERR(old_dentry))
3110 goto exit3;
3111 /* source must exist */
3112 error = -ENOENT;
3113 if (!old_dentry->d_inode)
3114 goto exit4;
3115 /* unless the source is a directory trailing slashes give -ENOTDIR */
3116 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
3117 error = -ENOTDIR;
3118 if (oldnd.last.name[oldnd.last.len])
3119 goto exit4;
3120 if (newnd.last.name[newnd.last.len])
3121 goto exit4;
3122 }
3123 /* source should not be ancestor of target */
3124 error = -EINVAL;
3125 if (old_dentry == trap)
3126 goto exit4;
Christoph Hellwig49705b72005-11-08 21:35:06 -08003127 new_dentry = lookup_hash(&newnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 error = PTR_ERR(new_dentry);
3129 if (IS_ERR(new_dentry))
3130 goto exit4;
3131 /* target should not be an ancestor of source */
3132 error = -ENOTEMPTY;
3133 if (new_dentry == trap)
3134 goto exit5;
3135
Dave Hansen9079b1e2008-02-15 14:37:49 -08003136 error = mnt_want_write(oldnd.path.mnt);
3137 if (error)
3138 goto exit5;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003139 error = security_path_rename(&oldnd.path, old_dentry,
3140 &newnd.path, new_dentry);
3141 if (error)
3142 goto exit6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 error = vfs_rename(old_dir->d_inode, old_dentry,
3144 new_dir->d_inode, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003145exit6:
Dave Hansen9079b1e2008-02-15 14:37:49 -08003146 mnt_drop_write(oldnd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147exit5:
3148 dput(new_dentry);
3149exit4:
3150 dput(old_dentry);
3151exit3:
3152 unlock_rename(new_dir, old_dir);
3153exit2:
Jan Blunck1d957f92008-02-14 19:34:35 -08003154 path_put(&newnd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04003155 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003157 path_put(&oldnd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 putname(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04003159exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 return error;
3161}
3162
Heiko Carstensa26eab22009-01-14 14:14:17 +01003163SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003164{
3165 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
3166}
3167
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
3169{
3170 int len;
3171
3172 len = PTR_ERR(link);
3173 if (IS_ERR(link))
3174 goto out;
3175
3176 len = strlen(link);
3177 if (len > (unsigned) buflen)
3178 len = buflen;
3179 if (copy_to_user(buffer, link, len))
3180 len = -EFAULT;
3181out:
3182 return len;
3183}
3184
3185/*
3186 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
3187 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
3188 * using) it for any given inode is up to filesystem.
3189 */
3190int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3191{
3192 struct nameidata nd;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003193 void *cookie;
Marcin Slusarz694a1762008-06-09 16:40:37 -07003194 int res;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003195
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 nd.depth = 0;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003197 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
Marcin Slusarz694a1762008-06-09 16:40:37 -07003198 if (IS_ERR(cookie))
3199 return PTR_ERR(cookie);
3200
3201 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
3202 if (dentry->d_inode->i_op->put_link)
3203 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3204 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205}
3206
3207int vfs_follow_link(struct nameidata *nd, const char *link)
3208{
3209 return __vfs_follow_link(nd, link);
3210}
3211
3212/* get the link contents into pagecache */
3213static char *page_getlink(struct dentry * dentry, struct page **ppage)
3214{
Duane Griffinebd09ab2008-12-19 20:47:12 +00003215 char *kaddr;
3216 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 struct address_space *mapping = dentry->d_inode->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -07003218 page = read_mapping_page(mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 if (IS_ERR(page))
Nick Piggin6fe69002007-05-06 14:49:04 -07003220 return (char*)page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 *ppage = page;
Duane Griffinebd09ab2008-12-19 20:47:12 +00003222 kaddr = kmap(page);
3223 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3224 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225}
3226
3227int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3228{
3229 struct page *page = NULL;
3230 char *s = page_getlink(dentry, &page);
3231 int res = vfs_readlink(dentry,buffer,buflen,s);
3232 if (page) {
3233 kunmap(page);
3234 page_cache_release(page);
3235 }
3236 return res;
3237}
3238
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003239void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003241 struct page *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 nd_set_link(nd, page_getlink(dentry, &page));
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003243 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244}
3245
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003246void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003248 struct page *page = cookie;
3249
3250 if (page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 kunmap(page);
3252 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 }
3254}
3255
Nick Piggin54566b22009-01-04 12:00:53 -08003256/*
3257 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
3258 */
3259int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260{
3261 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003262 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07003263 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08003264 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 char *kaddr;
Nick Piggin54566b22009-01-04 12:00:53 -08003266 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
3267 if (nofs)
3268 flags |= AOP_FLAG_NOFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269
NeilBrown7e53cac2006-03-25 03:07:57 -08003270retry:
Nick Pigginafddba42007-10-16 01:25:01 -07003271 err = pagecache_write_begin(NULL, mapping, 0, len-1,
Nick Piggin54566b22009-01-04 12:00:53 -08003272 flags, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07003274 goto fail;
3275
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 kaddr = kmap_atomic(page, KM_USER0);
3277 memcpy(kaddr, symname, len-1);
3278 kunmap_atomic(kaddr, KM_USER0);
Nick Pigginafddba42007-10-16 01:25:01 -07003279
3280 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3281 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 if (err < 0)
3283 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07003284 if (err < len-1)
3285 goto retry;
3286
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 mark_inode_dirty(inode);
3288 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289fail:
3290 return err;
3291}
3292
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003293int page_symlink(struct inode *inode, const char *symname, int len)
3294{
3295 return __page_symlink(inode, symname, len,
Nick Piggin54566b22009-01-04 12:00:53 -08003296 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003297}
3298
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003299const struct inode_operations page_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 .readlink = generic_readlink,
3301 .follow_link = page_follow_link_light,
3302 .put_link = page_put_link,
3303};
3304
Al Viro2d8f3032008-07-22 09:59:21 -04003305EXPORT_SYMBOL(user_path_at);
David Howellscc53ce52011-01-14 18:45:26 +00003306EXPORT_SYMBOL(follow_down_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307EXPORT_SYMBOL(follow_down);
3308EXPORT_SYMBOL(follow_up);
3309EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
3310EXPORT_SYMBOL(getname);
3311EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312EXPORT_SYMBOL(lookup_one_len);
3313EXPORT_SYMBOL(page_follow_link_light);
3314EXPORT_SYMBOL(page_put_link);
3315EXPORT_SYMBOL(page_readlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003316EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317EXPORT_SYMBOL(page_symlink);
3318EXPORT_SYMBOL(page_symlink_inode_operations);
Al Virod1811462008-08-02 00:49:18 -04003319EXPORT_SYMBOL(kern_path);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07003320EXPORT_SYMBOL(vfs_path_lookup);
Al Virof419a2e2008-07-22 00:07:17 -04003321EXPORT_SYMBOL(inode_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322EXPORT_SYMBOL(unlock_rename);
3323EXPORT_SYMBOL(vfs_create);
3324EXPORT_SYMBOL(vfs_follow_link);
3325EXPORT_SYMBOL(vfs_link);
3326EXPORT_SYMBOL(vfs_mkdir);
3327EXPORT_SYMBOL(vfs_mknod);
3328EXPORT_SYMBOL(generic_permission);
3329EXPORT_SYMBOL(vfs_readlink);
3330EXPORT_SYMBOL(vfs_rename);
3331EXPORT_SYMBOL(vfs_rmdir);
3332EXPORT_SYMBOL(vfs_symlink);
3333EXPORT_SYMBOL(vfs_unlink);
3334EXPORT_SYMBOL(dentry_unhash);
3335EXPORT_SYMBOL(generic_readlink);