blob: f8c69d37379348973d2a12e055dbc6c61dc4cbf4 [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{
Linus Torvalds84635d62011-07-25 22:47:03 -0700179#ifdef CONFIG_FS_POSIX_ACL
Linus Torvaldse77819e2011-07-22 19:30:19 -0700180 struct posix_acl *acl;
181
182 /*
183 * Under RCU walk, we cannot even do a "get_cached_acl()",
184 * because that involves locking and getting a refcount on
185 * a cached ACL.
186 *
187 * So the only case we handle during RCU walking is the
188 * case of a cached "no ACL at all", which needs no locks
189 * or refcounts.
190 */
191 if (mask & MAY_NOT_BLOCK) {
192 if (negative_cached_acl(inode, ACL_TYPE_ACCESS))
193 return -EAGAIN;
194 return -ECHILD;
195 }
196
197 acl = get_cached_acl(inode, ACL_TYPE_ACCESS);
198
199 /*
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200200 * A filesystem can force a ACL callback by just never filling the
201 * ACL cache. But normally you'd fill the cache either at inode
202 * instantiation time, or on the first ->get_acl call.
Linus Torvaldse77819e2011-07-22 19:30:19 -0700203 *
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200204 * If the filesystem doesn't have a get_acl() function at all, we'll
205 * just create the negative cache entry.
Linus Torvaldse77819e2011-07-22 19:30:19 -0700206 */
207 if (acl == ACL_NOT_CACHED) {
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200208 if (inode->i_op->get_acl) {
209 acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS);
210 if (IS_ERR(acl))
211 return PTR_ERR(acl);
212 } else {
213 set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
214 return -EAGAIN;
215 }
Linus Torvaldse77819e2011-07-22 19:30:19 -0700216 }
217
218 if (acl) {
219 int error = posix_acl_permission(inode, acl, mask);
220 posix_acl_release(acl);
221 return error;
222 }
Linus Torvalds84635d62011-07-25 22:47:03 -0700223#endif
Linus Torvaldse77819e2011-07-22 19:30:19 -0700224
225 return -EAGAIN;
226}
227
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700228/*
229 * This does basic POSIX ACL permission checking
230 */
Al Viro7e401452011-06-20 19:12:17 -0400231static int acl_permission_check(struct inode *inode, int mask)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700232{
Linus Torvalds26cf46b2011-05-13 11:51:01 -0700233 unsigned int mode = inode->i_mode;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700234
Al Viro9c2c7032011-06-20 19:06:22 -0400235 mask &= MAY_READ | MAY_WRITE | MAY_EXEC | MAY_NOT_BLOCK;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700236
Serge E. Hallyne795b712011-03-23 16:43:25 -0700237 if (current_user_ns() != inode_userns(inode))
238 goto other_perms;
239
Linus Torvalds14067ff2011-07-25 19:55:52 -0700240 if (likely(current_fsuid() == inode->i_uid))
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700241 mode >>= 6;
242 else {
Linus Torvaldse77819e2011-07-22 19:30:19 -0700243 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
Al Viro7e401452011-06-20 19:12:17 -0400244 int error = check_acl(inode, mask);
Nick Pigginb74c79e2011-01-07 17:49:58 +1100245 if (error != -EAGAIN)
246 return error;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700247 }
248
249 if (in_group_p(inode->i_gid))
250 mode >>= 3;
251 }
252
Serge E. Hallyne795b712011-03-23 16:43:25 -0700253other_perms:
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700254 /*
255 * If the DACs are ok we don't need any capability check.
256 */
Al Viro9c2c7032011-06-20 19:06:22 -0400257 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700258 return 0;
259 return -EACCES;
260}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262/**
Nick Pigginb74c79e2011-01-07 17:49:58 +1100263 * generic_permission - check for access rights on a Posix-like filesystem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 * @inode: inode to check access rights for
265 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 *
267 * Used to check for read/write/execute permissions on a file.
268 * We use "fsuid" for this, letting us set arbitrary permissions
269 * for filesystem access without changing the "normal" uids which
Nick Pigginb74c79e2011-01-07 17:49:58 +1100270 * are used for other things.
271 *
272 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
273 * request cannot be satisfied (eg. requires blocking or too much complexity).
274 * It would then be called again in ref-walk mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 */
Al Viro2830ba72011-06-20 19:16:29 -0400276int generic_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700278 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 /*
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700281 * Do the basic POSIX ACL permission checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 */
Al Viro7e401452011-06-20 19:12:17 -0400283 ret = acl_permission_check(inode, mask);
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700284 if (ret != -EACCES)
285 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Al Virod594e7e2011-06-20 19:55:42 -0400287 if (S_ISDIR(inode->i_mode)) {
288 /* DACs are overridable for directories */
289 if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
290 return 0;
291 if (!(mask & MAY_WRITE))
292 if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
293 return 0;
294 return -EACCES;
295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 /*
297 * Read/write DACs are always overridable.
Al Virod594e7e2011-06-20 19:55:42 -0400298 * Executable DACs are overridable when there is
299 * at least one exec bit set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 */
Al Virod594e7e2011-06-20 19:55:42 -0400301 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
Serge E. Hallyne795b712011-03-23 16:43:25 -0700302 if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 return 0;
304
305 /*
306 * Searching includes executable on directories, else just read.
307 */
Serge E. Hallyn7ea66002009-12-29 14:50:19 -0600308 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
Al Virod594e7e2011-06-20 19:55:42 -0400309 if (mask == MAY_READ)
Serge E. Hallyne795b712011-03-23 16:43:25 -0700310 if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 return 0;
312
313 return -EACCES;
314}
315
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200316/**
317 * inode_permission - check for access rights to a given inode
318 * @inode: inode to check permission on
319 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
320 *
321 * Used to check for read/write/execute permissions on an inode.
322 * We use "fsuid" for this, letting us set arbitrary permissions
323 * for filesystem access without changing the "normal" uids which
324 * are used for other things.
325 */
Al Virof419a2e2008-07-22 00:07:17 -0400326int inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
Al Viroe6305c42008-07-15 21:03:57 -0400328 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 if (mask & MAY_WRITE) {
Miklos Szeredi22590e42007-10-16 23:27:08 -0700331 umode_t mode = inode->i_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 /*
334 * Nobody gets write access to a read-only fs.
335 */
336 if (IS_RDONLY(inode) &&
337 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
338 return -EROFS;
339
340 /*
341 * Nobody gets write access to an immutable file.
342 */
343 if (IS_IMMUTABLE(inode))
344 return -EACCES;
345 }
346
Al Viroacfa4382008-12-04 10:06:33 -0500347 if (inode->i_op->permission)
Al Viro10556cb2011-06-20 19:28:19 -0400348 retval = inode->i_op->permission(inode, mask);
Miklos Szeredif696a362008-07-31 13:41:58 +0200349 else
Al Viro2830ba72011-06-20 19:16:29 -0400350 retval = generic_permission(inode, mask);
Miklos Szeredif696a362008-07-31 13:41:58 +0200351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 if (retval)
353 return retval;
354
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700355 retval = devcgroup_inode_permission(inode, mask);
356 if (retval)
357 return retval;
358
Eric Parisd09ca732010-07-23 11:43:57 -0400359 return security_inode_permission(inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
Al Virof4d6ff82011-06-19 13:14:21 -0400362/**
Jan Blunck5dd784d02008-02-14 19:34:38 -0800363 * path_get - get a reference to a path
364 * @path: path to get the reference to
365 *
366 * Given a path increment the reference count to the dentry and the vfsmount.
367 */
368void path_get(struct path *path)
369{
370 mntget(path->mnt);
371 dget(path->dentry);
372}
373EXPORT_SYMBOL(path_get);
374
375/**
Jan Blunck1d957f92008-02-14 19:34:35 -0800376 * path_put - put a reference to a path
377 * @path: path to put the reference to
378 *
379 * Given a path decrement the reference count to the dentry and the vfsmount.
380 */
381void path_put(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Jan Blunck1d957f92008-02-14 19:34:35 -0800383 dput(path->dentry);
384 mntput(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
Jan Blunck1d957f92008-02-14 19:34:35 -0800386EXPORT_SYMBOL(path_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Al Viro19660af2011-03-25 10:32:48 -0400388/*
Nick Piggin31e6b012011-01-07 17:49:52 +1100389 * Path walking has 2 modes, rcu-walk and ref-walk (see
Al Viro19660af2011-03-25 10:32:48 -0400390 * Documentation/filesystems/path-lookup.txt). In situations when we can't
391 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
392 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
393 * mode. Refcounts are grabbed at the last known good point before rcu-walk
394 * got stuck, so ref-walk may continue from there. If this is not successful
395 * (eg. a seqcount has changed), then failure is returned and it's up to caller
396 * to restart the path walk from the beginning in ref-walk mode.
Nick Piggin31e6b012011-01-07 17:49:52 +1100397 */
Nick Piggin31e6b012011-01-07 17:49:52 +1100398
399/**
Al Viro19660af2011-03-25 10:32:48 -0400400 * unlazy_walk - try to switch to ref-walk mode.
401 * @nd: nameidata pathwalk data
402 * @dentry: child of nd->path.dentry or NULL
Randy Dunlap39191622011-01-08 19:36:21 -0800403 * Returns: 0 on success, -ECHILD on failure
Nick Piggin31e6b012011-01-07 17:49:52 +1100404 *
Al Viro19660af2011-03-25 10:32:48 -0400405 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
406 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
407 * @nd or NULL. Must be called from rcu-walk context.
Nick Piggin31e6b012011-01-07 17:49:52 +1100408 */
Al Viro19660af2011-03-25 10:32:48 -0400409static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
Nick Piggin31e6b012011-01-07 17:49:52 +1100410{
411 struct fs_struct *fs = current->fs;
412 struct dentry *parent = nd->path.dentry;
Al Viro5b6ca022011-03-09 23:04:47 -0500413 int want_root = 0;
Nick Piggin31e6b012011-01-07 17:49:52 +1100414
415 BUG_ON(!(nd->flags & LOOKUP_RCU));
Al Viro5b6ca022011-03-09 23:04:47 -0500416 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
417 want_root = 1;
Nick Piggin31e6b012011-01-07 17:49:52 +1100418 spin_lock(&fs->lock);
419 if (nd->root.mnt != fs->root.mnt ||
420 nd->root.dentry != fs->root.dentry)
421 goto err_root;
422 }
423 spin_lock(&parent->d_lock);
Al Viro19660af2011-03-25 10:32:48 -0400424 if (!dentry) {
425 if (!__d_rcu_to_refcount(parent, nd->seq))
426 goto err_parent;
427 BUG_ON(nd->inode != parent->d_inode);
428 } else {
Al Viro94c0d4e2011-07-12 21:40:23 -0400429 if (dentry->d_parent != parent)
430 goto err_parent;
Al Viro19660af2011-03-25 10:32:48 -0400431 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
432 if (!__d_rcu_to_refcount(dentry, nd->seq))
433 goto err_child;
434 /*
435 * If the sequence check on the child dentry passed, then
436 * the child has not been removed from its parent. This
437 * means the parent dentry must be valid and able to take
438 * a reference at this point.
439 */
440 BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
441 BUG_ON(!parent->d_count);
442 parent->d_count++;
443 spin_unlock(&dentry->d_lock);
444 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100445 spin_unlock(&parent->d_lock);
Al Viro5b6ca022011-03-09 23:04:47 -0500446 if (want_root) {
Nick Piggin31e6b012011-01-07 17:49:52 +1100447 path_get(&nd->root);
448 spin_unlock(&fs->lock);
449 }
450 mntget(nd->path.mnt);
451
452 rcu_read_unlock();
453 br_read_unlock(vfsmount_lock);
454 nd->flags &= ~LOOKUP_RCU;
455 return 0;
Al Viro19660af2011-03-25 10:32:48 -0400456
457err_child:
Nick Piggin31e6b012011-01-07 17:49:52 +1100458 spin_unlock(&dentry->d_lock);
Al Viro19660af2011-03-25 10:32:48 -0400459err_parent:
Nick Piggin31e6b012011-01-07 17:49:52 +1100460 spin_unlock(&parent->d_lock);
461err_root:
Al Viro5b6ca022011-03-09 23:04:47 -0500462 if (want_root)
Nick Piggin31e6b012011-01-07 17:49:52 +1100463 spin_unlock(&fs->lock);
464 return -ECHILD;
465}
466
Nick Piggin31e6b012011-01-07 17:49:52 +1100467/**
Trond Myklebust834f2a42005-10-18 14:20:16 -0700468 * release_open_intent - free up open intent resources
469 * @nd: pointer to nameidata
470 */
471void release_open_intent(struct nameidata *nd)
472{
Linus Torvalds2dab5972011-02-11 15:53:38 -0800473 struct file *file = nd->intent.open.file;
474
475 if (file && !IS_ERR(file)) {
476 if (file->f_path.dentry == NULL)
477 put_filp(file);
478 else
479 fput(file);
480 }
Trond Myklebust834f2a42005-10-18 14:20:16 -0700481}
482
Al Virof60aef72011-02-15 01:35:28 -0500483static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
Nick Piggin34286d62011-01-07 17:49:57 +1100484{
Al Virof60aef72011-02-15 01:35:28 -0500485 return dentry->d_op->d_revalidate(dentry, nd);
Nick Piggin34286d62011-01-07 17:49:57 +1100486}
487
Al Viro9f1fafe2011-03-25 11:00:12 -0400488/**
489 * complete_walk - successful completion of path walk
490 * @nd: pointer nameidata
Jeff Layton39159de2009-12-07 12:01:50 -0500491 *
Al Viro9f1fafe2011-03-25 11:00:12 -0400492 * If we had been in RCU mode, drop out of it and legitimize nd->path.
493 * Revalidate the final result, unless we'd already done that during
494 * the path walk or the filesystem doesn't ask for it. Return 0 on
495 * success, -error on failure. In case of failure caller does not
496 * need to drop nd->path.
Jeff Layton39159de2009-12-07 12:01:50 -0500497 */
Al Viro9f1fafe2011-03-25 11:00:12 -0400498static int complete_walk(struct nameidata *nd)
Jeff Layton39159de2009-12-07 12:01:50 -0500499{
Al Viro16c2cd72011-02-22 15:50:10 -0500500 struct dentry *dentry = nd->path.dentry;
Jeff Layton39159de2009-12-07 12:01:50 -0500501 int status;
Jeff Layton39159de2009-12-07 12:01:50 -0500502
Al Viro9f1fafe2011-03-25 11:00:12 -0400503 if (nd->flags & LOOKUP_RCU) {
504 nd->flags &= ~LOOKUP_RCU;
505 if (!(nd->flags & LOOKUP_ROOT))
506 nd->root.mnt = NULL;
507 spin_lock(&dentry->d_lock);
508 if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
509 spin_unlock(&dentry->d_lock);
510 rcu_read_unlock();
511 br_read_unlock(vfsmount_lock);
512 return -ECHILD;
513 }
514 BUG_ON(nd->inode != dentry->d_inode);
515 spin_unlock(&dentry->d_lock);
516 mntget(nd->path.mnt);
517 rcu_read_unlock();
518 br_read_unlock(vfsmount_lock);
519 }
520
Al Viro16c2cd72011-02-22 15:50:10 -0500521 if (likely(!(nd->flags & LOOKUP_JUMPED)))
Jeff Layton39159de2009-12-07 12:01:50 -0500522 return 0;
523
Al Viro16c2cd72011-02-22 15:50:10 -0500524 if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
525 return 0;
526
527 if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
528 return 0;
529
530 /* Note: we do not d_invalidate() */
Nick Piggin34286d62011-01-07 17:49:57 +1100531 status = d_revalidate(dentry, nd);
Jeff Layton39159de2009-12-07 12:01:50 -0500532 if (status > 0)
533 return 0;
534
Al Viro16c2cd72011-02-22 15:50:10 -0500535 if (!status)
Jeff Layton39159de2009-12-07 12:01:50 -0500536 status = -ESTALE;
Al Viro16c2cd72011-02-22 15:50:10 -0500537
Al Viro9f1fafe2011-03-25 11:00:12 -0400538 path_put(&nd->path);
Jeff Layton39159de2009-12-07 12:01:50 -0500539 return status;
540}
541
Al Viro2a737872009-04-07 11:49:53 -0400542static __always_inline void set_root(struct nameidata *nd)
543{
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200544 if (!nd->root.mnt)
545 get_fs_root(current->fs, &nd->root);
Al Viro2a737872009-04-07 11:49:53 -0400546}
547
Al Viro6de88d72009-08-09 01:41:57 +0400548static int link_path_walk(const char *, struct nameidata *);
549
Nick Piggin31e6b012011-01-07 17:49:52 +1100550static __always_inline void set_root_rcu(struct nameidata *nd)
551{
552 if (!nd->root.mnt) {
553 struct fs_struct *fs = current->fs;
Nick Pigginc28cc362011-01-07 17:49:53 +1100554 unsigned seq;
555
556 do {
557 seq = read_seqcount_begin(&fs->seq);
558 nd->root = fs->root;
Tim Chenc1530012011-04-15 11:39:29 -0700559 nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
Nick Pigginc28cc362011-01-07 17:49:53 +1100560 } while (read_seqcount_retry(&fs->seq, seq));
Nick Piggin31e6b012011-01-07 17:49:52 +1100561 }
562}
563
Arjan van de Venf1662352006-01-14 13:21:31 -0800564static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
Nick Piggin31e6b012011-01-07 17:49:52 +1100566 int ret;
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (IS_ERR(link))
569 goto fail;
570
571 if (*link == '/') {
Al Viro2a737872009-04-07 11:49:53 -0400572 set_root(nd);
Jan Blunck1d957f92008-02-14 19:34:35 -0800573 path_put(&nd->path);
Al Viro2a737872009-04-07 11:49:53 -0400574 nd->path = nd->root;
575 path_get(&nd->root);
Al Viro16c2cd72011-02-22 15:50:10 -0500576 nd->flags |= LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100578 nd->inode = nd->path.dentry->d_inode;
Christoph Hellwigb4091d52008-11-05 15:07:21 +0100579
Nick Piggin31e6b012011-01-07 17:49:52 +1100580 ret = link_path_walk(link, nd);
581 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582fail:
Jan Blunck1d957f92008-02-14 19:34:35 -0800583 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 return PTR_ERR(link);
585}
586
Jan Blunck1d957f92008-02-14 19:34:35 -0800587static void path_put_conditional(struct path *path, struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700588{
589 dput(path->dentry);
Jan Blunck4ac91372008-02-14 19:34:32 -0800590 if (path->mnt != nd->path.mnt)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700591 mntput(path->mnt);
592}
593
Nick Piggin7b9337a2011-01-14 08:42:43 +0000594static inline void path_to_nameidata(const struct path *path,
595 struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700596{
Nick Piggin31e6b012011-01-07 17:49:52 +1100597 if (!(nd->flags & LOOKUP_RCU)) {
598 dput(nd->path.dentry);
599 if (nd->path.mnt != path->mnt)
600 mntput(nd->path.mnt);
Huang Shijie9a229682010-04-02 17:37:13 +0800601 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100602 nd->path.mnt = path->mnt;
Jan Blunck4ac91372008-02-14 19:34:32 -0800603 nd->path.dentry = path->dentry;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700604}
605
Al Viro574197e2011-03-14 22:20:34 -0400606static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
607{
608 struct inode *inode = link->dentry->d_inode;
609 if (!IS_ERR(cookie) && inode->i_op->put_link)
610 inode->i_op->put_link(link->dentry, nd, cookie);
611 path_put(link);
612}
613
Al Virodef4af32009-12-26 08:37:05 -0500614static __always_inline int
Al Viro574197e2011-03-14 22:20:34 -0400615follow_link(struct path *link, struct nameidata *nd, void **p)
Ian Kent051d3812006-03-27 01:14:53 -0800616{
617 int error;
Nick Piggin7b9337a2011-01-14 08:42:43 +0000618 struct dentry *dentry = link->dentry;
Ian Kent051d3812006-03-27 01:14:53 -0800619
Al Viro844a3912011-02-15 00:38:26 -0500620 BUG_ON(nd->flags & LOOKUP_RCU);
621
Al Viro0e794582011-03-16 02:45:02 -0400622 if (link->mnt == nd->path.mnt)
623 mntget(link->mnt);
624
Al Viro574197e2011-03-14 22:20:34 -0400625 if (unlikely(current->total_link_count >= 40)) {
626 *p = ERR_PTR(-ELOOP); /* no ->put_link(), please */
Al Viro574197e2011-03-14 22:20:34 -0400627 path_put(&nd->path);
628 return -ELOOP;
629 }
630 cond_resched();
631 current->total_link_count++;
632
Nick Piggin7b9337a2011-01-14 08:42:43 +0000633 touch_atime(link->mnt, dentry);
Ian Kent051d3812006-03-27 01:14:53 -0800634 nd_set_link(nd, NULL);
635
Al Viro36f3b4f2011-02-22 21:24:38 -0500636 error = security_inode_follow_link(link->dentry, nd);
637 if (error) {
638 *p = ERR_PTR(error); /* no ->put_link(), please */
639 path_put(&nd->path);
640 return error;
641 }
642
Al Viro86acdca12009-12-22 23:45:11 -0500643 nd->last_type = LAST_BIND;
Al Virodef4af32009-12-26 08:37:05 -0500644 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
645 error = PTR_ERR(*p);
646 if (!IS_ERR(*p)) {
Ian Kent051d3812006-03-27 01:14:53 -0800647 char *s = nd_get_link(nd);
648 error = 0;
649 if (s)
650 error = __vfs_follow_link(nd, s);
Al Virobcda7652011-03-13 16:42:14 -0400651 else if (nd->last_type == LAST_BIND) {
Al Viro16c2cd72011-02-22 15:50:10 -0500652 nd->flags |= LOOKUP_JUMPED;
Al Virob21041d2011-03-14 20:01:51 -0400653 nd->inode = nd->path.dentry->d_inode;
654 if (nd->inode->i_op->follow_link) {
Al Virobcda7652011-03-13 16:42:14 -0400655 /* stepped on a _really_ weird one */
656 path_put(&nd->path);
657 error = -ELOOP;
658 }
659 }
Ian Kent051d3812006-03-27 01:14:53 -0800660 }
Ian Kent051d3812006-03-27 01:14:53 -0800661 return error;
662}
663
Nick Piggin31e6b012011-01-07 17:49:52 +1100664static int follow_up_rcu(struct path *path)
665{
666 struct vfsmount *parent;
667 struct dentry *mountpoint;
668
669 parent = path->mnt->mnt_parent;
670 if (parent == path->mnt)
671 return 0;
672 mountpoint = path->mnt->mnt_mountpoint;
673 path->dentry = mountpoint;
674 path->mnt = parent;
675 return 1;
676}
677
Al Virobab77eb2009-04-18 03:26:48 -0400678int follow_up(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
680 struct vfsmount *parent;
681 struct dentry *mountpoint;
Nick Piggin99b7db72010-08-18 04:37:39 +1000682
683 br_read_lock(vfsmount_lock);
Al Virobab77eb2009-04-18 03:26:48 -0400684 parent = path->mnt->mnt_parent;
685 if (parent == path->mnt) {
Nick Piggin99b7db72010-08-18 04:37:39 +1000686 br_read_unlock(vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return 0;
688 }
689 mntget(parent);
Al Virobab77eb2009-04-18 03:26:48 -0400690 mountpoint = dget(path->mnt->mnt_mountpoint);
Nick Piggin99b7db72010-08-18 04:37:39 +1000691 br_read_unlock(vfsmount_lock);
Al Virobab77eb2009-04-18 03:26:48 -0400692 dput(path->dentry);
693 path->dentry = mountpoint;
694 mntput(path->mnt);
695 path->mnt = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return 1;
697}
698
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100699/*
David Howells9875cf82011-01-14 18:45:21 +0000700 * Perform an automount
701 * - return -EISDIR to tell follow_managed() to stop and return the path we
702 * were called with.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 */
David Howells9875cf82011-01-14 18:45:21 +0000704static int follow_automount(struct path *path, unsigned flags,
705 bool *need_mntput)
Nick Piggin31e6b012011-01-07 17:49:52 +1100706{
David Howells9875cf82011-01-14 18:45:21 +0000707 struct vfsmount *mnt;
David Howellsea5b7782011-01-14 19:10:03 +0000708 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +1100709
David Howells9875cf82011-01-14 18:45:21 +0000710 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
711 return -EREMOTE;
Al Viro463ffb22005-06-06 13:36:05 -0700712
David Howells6f45b652011-01-14 18:45:31 +0000713 /* We don't want to mount if someone supplied AT_NO_AUTOMOUNT
714 * and this is the terminal part of the path.
715 */
Al Viro49084c32011-06-25 21:59:52 -0400716 if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_PARENT))
David Howells6f45b652011-01-14 18:45:31 +0000717 return -EISDIR; /* we actually want to stop here */
718
David Howells9875cf82011-01-14 18:45:21 +0000719 /* We want to mount if someone is trying to open/create a file of any
720 * type under the mountpoint, wants to traverse through the mountpoint
721 * or wants to open the mounted directory.
722 *
723 * We don't want to mount if someone's just doing a stat and they've
724 * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and
725 * appended a '/' to the name.
726 */
727 if (!(flags & LOOKUP_FOLLOW) &&
Al Viro49084c32011-06-25 21:59:52 -0400728 !(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
David Howells9875cf82011-01-14 18:45:21 +0000729 LOOKUP_OPEN | LOOKUP_CREATE)))
730 return -EISDIR;
731
732 current->total_link_count++;
733 if (current->total_link_count >= 40)
734 return -ELOOP;
735
736 mnt = path->dentry->d_op->d_automount(path);
737 if (IS_ERR(mnt)) {
738 /*
739 * The filesystem is allowed to return -EISDIR here to indicate
740 * it doesn't want to automount. For instance, autofs would do
741 * this so that its userspace daemon can mount on this dentry.
742 *
743 * However, we can only permit this if it's a terminal point in
744 * the path being looked up; if it wasn't then the remainder of
745 * the path is inaccessible and we should say so.
746 */
Al Viro49084c32011-06-25 21:59:52 -0400747 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
David Howells9875cf82011-01-14 18:45:21 +0000748 return -EREMOTE;
749 return PTR_ERR(mnt);
750 }
David Howellsea5b7782011-01-14 19:10:03 +0000751
David Howells9875cf82011-01-14 18:45:21 +0000752 if (!mnt) /* mount collision */
753 return 0;
754
Al Viro8aef1882011-06-16 15:10:06 +0100755 if (!*need_mntput) {
756 /* lock_mount() may release path->mnt on error */
757 mntget(path->mnt);
758 *need_mntput = true;
759 }
Al Viro19a167a2011-01-17 01:35:23 -0500760 err = finish_automount(mnt, path);
David Howellsea5b7782011-01-14 19:10:03 +0000761
David Howellsea5b7782011-01-14 19:10:03 +0000762 switch (err) {
763 case -EBUSY:
764 /* Someone else made a mount here whilst we were busy */
Al Viro19a167a2011-01-17 01:35:23 -0500765 return 0;
David Howellsea5b7782011-01-14 19:10:03 +0000766 case 0:
Al Viro8aef1882011-06-16 15:10:06 +0100767 path_put(path);
David Howellsea5b7782011-01-14 19:10:03 +0000768 path->mnt = mnt;
769 path->dentry = dget(mnt->mnt_root);
David Howellsea5b7782011-01-14 19:10:03 +0000770 return 0;
Al Viro19a167a2011-01-17 01:35:23 -0500771 default:
772 return err;
David Howellsea5b7782011-01-14 19:10:03 +0000773 }
Al Viro19a167a2011-01-17 01:35:23 -0500774
David Howells9875cf82011-01-14 18:45:21 +0000775}
776
777/*
778 * Handle a dentry that is managed in some way.
David Howellscc53ce52011-01-14 18:45:26 +0000779 * - Flagged for transit management (autofs)
David Howells9875cf82011-01-14 18:45:21 +0000780 * - Flagged as mountpoint
781 * - Flagged as automount point
782 *
783 * This may only be called in refwalk mode.
784 *
785 * Serialization is taken care of in namespace.c
786 */
787static int follow_managed(struct path *path, unsigned flags)
788{
Al Viro8aef1882011-06-16 15:10:06 +0100789 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
David Howells9875cf82011-01-14 18:45:21 +0000790 unsigned managed;
791 bool need_mntput = false;
Al Viro8aef1882011-06-16 15:10:06 +0100792 int ret = 0;
David Howells9875cf82011-01-14 18:45:21 +0000793
794 /* Given that we're not holding a lock here, we retain the value in a
795 * local variable for each dentry as we look at it so that we don't see
796 * the components of that value change under us */
797 while (managed = ACCESS_ONCE(path->dentry->d_flags),
798 managed &= DCACHE_MANAGED_DENTRY,
799 unlikely(managed != 0)) {
David Howellscc53ce52011-01-14 18:45:26 +0000800 /* Allow the filesystem to manage the transit without i_mutex
801 * being held. */
802 if (managed & DCACHE_MANAGE_TRANSIT) {
803 BUG_ON(!path->dentry->d_op);
804 BUG_ON(!path->dentry->d_op->d_manage);
Al Viro1aed3e42011-03-18 09:09:02 -0400805 ret = path->dentry->d_op->d_manage(path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +0000806 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +0100807 break;
David Howellscc53ce52011-01-14 18:45:26 +0000808 }
809
David Howells9875cf82011-01-14 18:45:21 +0000810 /* Transit to a mounted filesystem. */
811 if (managed & DCACHE_MOUNTED) {
812 struct vfsmount *mounted = lookup_mnt(path);
813 if (mounted) {
814 dput(path->dentry);
815 if (need_mntput)
816 mntput(path->mnt);
817 path->mnt = mounted;
818 path->dentry = dget(mounted->mnt_root);
819 need_mntput = true;
820 continue;
821 }
822
823 /* Something is mounted on this dentry in another
824 * namespace and/or whatever was mounted there in this
825 * namespace got unmounted before we managed to get the
826 * vfsmount_lock */
827 }
828
829 /* Handle an automount point */
830 if (managed & DCACHE_NEED_AUTOMOUNT) {
831 ret = follow_automount(path, flags, &need_mntput);
832 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +0100833 break;
David Howells9875cf82011-01-14 18:45:21 +0000834 continue;
835 }
836
837 /* We didn't change the current path point */
838 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
Al Viro8aef1882011-06-16 15:10:06 +0100840
841 if (need_mntput && path->mnt == mnt)
842 mntput(path->mnt);
843 if (ret == -EISDIR)
844 ret = 0;
845 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
847
David Howellscc53ce52011-01-14 18:45:26 +0000848int follow_down_one(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
850 struct vfsmount *mounted;
851
Al Viro1c755af2009-04-18 14:06:57 -0400852 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -0400854 dput(path->dentry);
855 mntput(path->mnt);
856 path->mnt = mounted;
857 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return 1;
859 }
860 return 0;
861}
862
Ian Kent62a73752011-03-25 01:51:02 +0800863static inline bool managed_dentry_might_block(struct dentry *dentry)
864{
865 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
866 dentry->d_op->d_manage(dentry, true) < 0);
867}
868
David Howells9875cf82011-01-14 18:45:21 +0000869/*
Al Viro287548e2011-05-27 06:50:06 -0400870 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
871 * we meet a managed dentry that would need blocking.
David Howells9875cf82011-01-14 18:45:21 +0000872 */
873static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
Al Viro287548e2011-05-27 06:50:06 -0400874 struct inode **inode)
David Howells9875cf82011-01-14 18:45:21 +0000875{
Ian Kent62a73752011-03-25 01:51:02 +0800876 for (;;) {
David Howells9875cf82011-01-14 18:45:21 +0000877 struct vfsmount *mounted;
Ian Kent62a73752011-03-25 01:51:02 +0800878 /*
879 * Don't forget we might have a non-mountpoint managed dentry
880 * that wants to block transit.
881 */
Al Viro287548e2011-05-27 06:50:06 -0400882 if (unlikely(managed_dentry_might_block(path->dentry)))
David Howellsab909112011-01-14 18:46:51 +0000883 return false;
Ian Kent62a73752011-03-25 01:51:02 +0800884
885 if (!d_mountpoint(path->dentry))
886 break;
887
David Howells9875cf82011-01-14 18:45:21 +0000888 mounted = __lookup_mnt(path->mnt, path->dentry, 1);
889 if (!mounted)
890 break;
891 path->mnt = mounted;
892 path->dentry = mounted->mnt_root;
893 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
Linus Torvalds59430262011-07-18 15:43:29 -0700894 /*
895 * Update the inode too. We don't need to re-check the
896 * dentry sequence number here after this d_inode read,
897 * because a mount-point is always pinned.
898 */
899 *inode = path->dentry->d_inode;
David Howells9875cf82011-01-14 18:45:21 +0000900 }
David Howells9875cf82011-01-14 18:45:21 +0000901 return true;
902}
903
Al Virodea39372011-05-27 06:53:39 -0400904static void follow_mount_rcu(struct nameidata *nd)
Al Viro287548e2011-05-27 06:50:06 -0400905{
Al Virodea39372011-05-27 06:53:39 -0400906 while (d_mountpoint(nd->path.dentry)) {
Al Viro287548e2011-05-27 06:50:06 -0400907 struct vfsmount *mounted;
Al Virodea39372011-05-27 06:53:39 -0400908 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
Al Viro287548e2011-05-27 06:50:06 -0400909 if (!mounted)
910 break;
Al Virodea39372011-05-27 06:53:39 -0400911 nd->path.mnt = mounted;
912 nd->path.dentry = mounted->mnt_root;
913 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro287548e2011-05-27 06:50:06 -0400914 }
915}
916
Nick Piggin31e6b012011-01-07 17:49:52 +1100917static int follow_dotdot_rcu(struct nameidata *nd)
918{
Nick Piggin31e6b012011-01-07 17:49:52 +1100919 set_root_rcu(nd);
920
David Howells9875cf82011-01-14 18:45:21 +0000921 while (1) {
Nick Piggin31e6b012011-01-07 17:49:52 +1100922 if (nd->path.dentry == nd->root.dentry &&
923 nd->path.mnt == nd->root.mnt) {
924 break;
925 }
926 if (nd->path.dentry != nd->path.mnt->mnt_root) {
927 struct dentry *old = nd->path.dentry;
928 struct dentry *parent = old->d_parent;
929 unsigned seq;
930
931 seq = read_seqcount_begin(&parent->d_seq);
932 if (read_seqcount_retry(&old->d_seq, nd->seq))
Al Viroef7562d2011-03-04 14:35:59 -0500933 goto failed;
Nick Piggin31e6b012011-01-07 17:49:52 +1100934 nd->path.dentry = parent;
935 nd->seq = seq;
936 break;
937 }
938 if (!follow_up_rcu(&nd->path))
939 break;
940 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Nick Piggin31e6b012011-01-07 17:49:52 +1100941 }
Al Virodea39372011-05-27 06:53:39 -0400942 follow_mount_rcu(nd);
943 nd->inode = nd->path.dentry->d_inode;
Nick Piggin31e6b012011-01-07 17:49:52 +1100944 return 0;
Al Viroef7562d2011-03-04 14:35:59 -0500945
946failed:
947 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -0500948 if (!(nd->flags & LOOKUP_ROOT))
949 nd->root.mnt = NULL;
Al Viroef7562d2011-03-04 14:35:59 -0500950 rcu_read_unlock();
951 br_read_unlock(vfsmount_lock);
952 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +1100953}
954
David Howells9875cf82011-01-14 18:45:21 +0000955/*
David Howellscc53ce52011-01-14 18:45:26 +0000956 * Follow down to the covering mount currently visible to userspace. At each
957 * point, the filesystem owning that dentry may be queried as to whether the
958 * caller is permitted to proceed or not.
David Howellscc53ce52011-01-14 18:45:26 +0000959 */
Al Viro7cc90cc2011-03-18 09:04:20 -0400960int follow_down(struct path *path)
David Howellscc53ce52011-01-14 18:45:26 +0000961{
962 unsigned managed;
963 int ret;
964
965 while (managed = ACCESS_ONCE(path->dentry->d_flags),
966 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
967 /* Allow the filesystem to manage the transit without i_mutex
968 * being held.
969 *
970 * We indicate to the filesystem if someone is trying to mount
971 * something here. This gives autofs the chance to deny anyone
972 * other than its daemon the right to mount on its
973 * superstructure.
974 *
975 * The filesystem may sleep at this point.
976 */
977 if (managed & DCACHE_MANAGE_TRANSIT) {
978 BUG_ON(!path->dentry->d_op);
979 BUG_ON(!path->dentry->d_op->d_manage);
David Howellsab909112011-01-14 18:46:51 +0000980 ret = path->dentry->d_op->d_manage(
Al Viro1aed3e42011-03-18 09:09:02 -0400981 path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +0000982 if (ret < 0)
983 return ret == -EISDIR ? 0 : ret;
984 }
985
986 /* Transit to a mounted filesystem. */
987 if (managed & DCACHE_MOUNTED) {
988 struct vfsmount *mounted = lookup_mnt(path);
989 if (!mounted)
990 break;
991 dput(path->dentry);
992 mntput(path->mnt);
993 path->mnt = mounted;
994 path->dentry = dget(mounted->mnt_root);
995 continue;
996 }
997
998 /* Don't handle automount points here */
999 break;
1000 }
1001 return 0;
1002}
1003
1004/*
David Howells9875cf82011-01-14 18:45:21 +00001005 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1006 */
1007static void follow_mount(struct path *path)
1008{
1009 while (d_mountpoint(path->dentry)) {
1010 struct vfsmount *mounted = lookup_mnt(path);
1011 if (!mounted)
1012 break;
1013 dput(path->dentry);
1014 mntput(path->mnt);
1015 path->mnt = mounted;
1016 path->dentry = dget(mounted->mnt_root);
1017 }
1018}
1019
Nick Piggin31e6b012011-01-07 17:49:52 +11001020static void follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
Al Viro2a737872009-04-07 11:49:53 -04001022 set_root(nd);
Andreas Mohre518ddb2006-09-29 02:01:22 -07001023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 while(1) {
Jan Blunck4ac91372008-02-14 19:34:32 -08001025 struct dentry *old = nd->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Al Viro2a737872009-04-07 11:49:53 -04001027 if (nd->path.dentry == nd->root.dentry &&
1028 nd->path.mnt == nd->root.mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 break;
1030 }
Jan Blunck4ac91372008-02-14 19:34:32 -08001031 if (nd->path.dentry != nd->path.mnt->mnt_root) {
Al Viro3088dd72010-01-30 15:47:29 -05001032 /* rare case of legitimate dget_parent()... */
1033 nd->path.dentry = dget_parent(nd->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 dput(old);
1035 break;
1036 }
Al Viro3088dd72010-01-30 15:47:29 -05001037 if (!follow_up(&nd->path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 }
Al Viro79ed0222009-04-18 13:59:41 -04001040 follow_mount(&nd->path);
Nick Piggin31e6b012011-01-07 17:49:52 +11001041 nd->inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042}
1043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044/*
Nick Pigginbaa03892010-08-18 04:37:31 +10001045 * Allocate a dentry with name and parent, and perform a parent
1046 * directory ->lookup on it. Returns the new dentry, or ERR_PTR
1047 * on error. parent->d_inode->i_mutex must be held. d_lookup must
1048 * have verified that no child exists while under i_mutex.
1049 */
1050static struct dentry *d_alloc_and_lookup(struct dentry *parent,
1051 struct qstr *name, struct nameidata *nd)
1052{
1053 struct inode *inode = parent->d_inode;
1054 struct dentry *dentry;
1055 struct dentry *old;
1056
1057 /* Don't create child dentry for a dead directory. */
1058 if (unlikely(IS_DEADDIR(inode)))
1059 return ERR_PTR(-ENOENT);
1060
1061 dentry = d_alloc(parent, name);
1062 if (unlikely(!dentry))
1063 return ERR_PTR(-ENOMEM);
1064
1065 old = inode->i_op->lookup(inode, dentry, nd);
1066 if (unlikely(old)) {
1067 dput(dentry);
1068 dentry = old;
1069 }
1070 return dentry;
1071}
1072
1073/*
Josef Bacik44396f42011-05-31 11:58:49 -04001074 * We already have a dentry, but require a lookup to be performed on the parent
1075 * directory to fill in d_inode. Returns the new dentry, or ERR_PTR on error.
1076 * parent->d_inode->i_mutex must be held. d_lookup must have verified that no
1077 * child exists while under i_mutex.
1078 */
1079static struct dentry *d_inode_lookup(struct dentry *parent, struct dentry *dentry,
1080 struct nameidata *nd)
1081{
1082 struct inode *inode = parent->d_inode;
1083 struct dentry *old;
1084
1085 /* Don't create child dentry for a dead directory. */
1086 if (unlikely(IS_DEADDIR(inode)))
1087 return ERR_PTR(-ENOENT);
1088
1089 old = inode->i_op->lookup(inode, dentry, nd);
1090 if (unlikely(old)) {
1091 dput(dentry);
1092 dentry = old;
1093 }
1094 return dentry;
1095}
1096
1097/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 * It's more convoluted than I'd like it to be, but... it's still fairly
1099 * small and for now I'd prefer to have fast path as straight as possible.
1100 * It _is_ time-critical.
1101 */
1102static int do_lookup(struct nameidata *nd, struct qstr *name,
Nick Piggin31e6b012011-01-07 17:49:52 +11001103 struct path *path, struct inode **inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
Jan Blunck4ac91372008-02-14 19:34:32 -08001105 struct vfsmount *mnt = nd->path.mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +11001106 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro5a18fff2011-03-11 04:44:53 -05001107 int need_reval = 1;
1108 int status = 1;
David Howells9875cf82011-01-14 18:45:21 +00001109 int err;
1110
Al Viro3cac2602009-08-13 18:27:43 +04001111 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001112 * Rename seqlock is not required here because in the off chance
1113 * of a false negative due to a concurrent rename, we're going to
1114 * do the non-racy lookup, below.
1115 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001116 if (nd->flags & LOOKUP_RCU) {
1117 unsigned seq;
Nick Piggin31e6b012011-01-07 17:49:52 +11001118 *inode = nd->inode;
1119 dentry = __d_lookup_rcu(parent, name, &seq, inode);
Al Viro5a18fff2011-03-11 04:44:53 -05001120 if (!dentry)
1121 goto unlazy;
1122
Nick Piggin31e6b012011-01-07 17:49:52 +11001123 /* Memory barrier in read_seqcount_begin of child is enough */
1124 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1125 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001126 nd->seq = seq;
Al Viro5a18fff2011-03-11 04:44:53 -05001127
Al Viro24643082011-02-15 01:26:22 -05001128 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
Al Viro5a18fff2011-03-11 04:44:53 -05001129 status = d_revalidate(dentry, nd);
1130 if (unlikely(status <= 0)) {
1131 if (status != -ECHILD)
1132 need_reval = 0;
1133 goto unlazy;
1134 }
Al Viro24643082011-02-15 01:26:22 -05001135 }
Josef Bacik44396f42011-05-31 11:58:49 -04001136 if (unlikely(d_need_lookup(dentry)))
1137 goto unlazy;
Nick Piggin31e6b012011-01-07 17:49:52 +11001138 path->mnt = mnt;
1139 path->dentry = dentry;
Al Virod6e9bd22011-05-27 07:03:15 -04001140 if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1141 goto unlazy;
1142 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1143 goto unlazy;
1144 return 0;
Al Viro5a18fff2011-03-11 04:44:53 -05001145unlazy:
Al Viro19660af2011-03-25 10:32:48 -04001146 if (unlazy_walk(nd, dentry))
1147 return -ECHILD;
Al Viro5a18fff2011-03-11 04:44:53 -05001148 } else {
1149 dentry = __d_lookup(parent, name);
Nick Piggin31e6b012011-01-07 17:49:52 +11001150 }
Al Viro5a18fff2011-03-11 04:44:53 -05001151
Josef Bacik44396f42011-05-31 11:58:49 -04001152 if (dentry && unlikely(d_need_lookup(dentry))) {
1153 dput(dentry);
1154 dentry = NULL;
1155 }
Al Viro5a18fff2011-03-11 04:44:53 -05001156retry:
1157 if (unlikely(!dentry)) {
1158 struct inode *dir = parent->d_inode;
1159 BUG_ON(nd->inode != dir);
1160
1161 mutex_lock(&dir->i_mutex);
1162 dentry = d_lookup(parent, name);
1163 if (likely(!dentry)) {
1164 dentry = d_alloc_and_lookup(parent, name, nd);
1165 if (IS_ERR(dentry)) {
1166 mutex_unlock(&dir->i_mutex);
1167 return PTR_ERR(dentry);
1168 }
1169 /* known good */
1170 need_reval = 0;
1171 status = 1;
Josef Bacik44396f42011-05-31 11:58:49 -04001172 } else if (unlikely(d_need_lookup(dentry))) {
1173 dentry = d_inode_lookup(parent, dentry, nd);
1174 if (IS_ERR(dentry)) {
1175 mutex_unlock(&dir->i_mutex);
1176 return PTR_ERR(dentry);
1177 }
1178 /* known good */
1179 need_reval = 0;
1180 status = 1;
Al Viro5a18fff2011-03-11 04:44:53 -05001181 }
1182 mutex_unlock(&dir->i_mutex);
Al Viro24643082011-02-15 01:26:22 -05001183 }
Al Viro5a18fff2011-03-11 04:44:53 -05001184 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
1185 status = d_revalidate(dentry, nd);
1186 if (unlikely(status <= 0)) {
1187 if (status < 0) {
1188 dput(dentry);
1189 return status;
1190 }
1191 if (!d_invalidate(dentry)) {
1192 dput(dentry);
1193 dentry = NULL;
1194 need_reval = 1;
1195 goto retry;
1196 }
1197 }
1198
David Howells9875cf82011-01-14 18:45:21 +00001199 path->mnt = mnt;
1200 path->dentry = dentry;
1201 err = follow_managed(path, nd->flags);
Ian Kent89312212011-01-18 12:06:10 +08001202 if (unlikely(err < 0)) {
1203 path_put_conditional(path, nd);
David Howells9875cf82011-01-14 18:45:21 +00001204 return err;
Ian Kent89312212011-01-18 12:06:10 +08001205 }
David Howells9875cf82011-01-14 18:45:21 +00001206 *inode = path->dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
1209
Al Viro52094c82011-02-21 21:34:47 -05001210static inline int may_lookup(struct nameidata *nd)
1211{
1212 if (nd->flags & LOOKUP_RCU) {
Al Viro4ad5abb2011-06-20 19:57:03 -04001213 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
Al Viro52094c82011-02-21 21:34:47 -05001214 if (err != -ECHILD)
1215 return err;
Al Viro19660af2011-03-25 10:32:48 -04001216 if (unlazy_walk(nd, NULL))
Al Viro52094c82011-02-21 21:34:47 -05001217 return -ECHILD;
1218 }
Al Viro4ad5abb2011-06-20 19:57:03 -04001219 return inode_permission(nd->inode, MAY_EXEC);
Al Viro52094c82011-02-21 21:34:47 -05001220}
1221
Al Viro9856fa12011-03-04 14:22:06 -05001222static inline int handle_dots(struct nameidata *nd, int type)
1223{
1224 if (type == LAST_DOTDOT) {
1225 if (nd->flags & LOOKUP_RCU) {
1226 if (follow_dotdot_rcu(nd))
1227 return -ECHILD;
1228 } else
1229 follow_dotdot(nd);
1230 }
1231 return 0;
1232}
1233
Al Viro951361f2011-03-04 14:44:37 -05001234static void terminate_walk(struct nameidata *nd)
1235{
1236 if (!(nd->flags & LOOKUP_RCU)) {
1237 path_put(&nd->path);
1238 } else {
1239 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001240 if (!(nd->flags & LOOKUP_ROOT))
1241 nd->root.mnt = NULL;
Al Viro951361f2011-03-04 14:44:37 -05001242 rcu_read_unlock();
1243 br_read_unlock(vfsmount_lock);
1244 }
1245}
1246
Al Viroce57dfc2011-03-13 19:58:58 -04001247static inline int walk_component(struct nameidata *nd, struct path *path,
1248 struct qstr *name, int type, int follow)
1249{
1250 struct inode *inode;
1251 int err;
1252 /*
1253 * "." and ".." are special - ".." especially so because it has
1254 * to be able to know about the current root directory and
1255 * parent relationships.
1256 */
1257 if (unlikely(type != LAST_NORM))
1258 return handle_dots(nd, type);
1259 err = do_lookup(nd, name, path, &inode);
1260 if (unlikely(err)) {
1261 terminate_walk(nd);
1262 return err;
1263 }
1264 if (!inode) {
1265 path_to_nameidata(path, nd);
1266 terminate_walk(nd);
1267 return -ENOENT;
1268 }
1269 if (unlikely(inode->i_op->follow_link) && follow) {
Al Viro19660af2011-03-25 10:32:48 -04001270 if (nd->flags & LOOKUP_RCU) {
1271 if (unlikely(unlazy_walk(nd, path->dentry))) {
1272 terminate_walk(nd);
1273 return -ECHILD;
1274 }
1275 }
Al Viroce57dfc2011-03-13 19:58:58 -04001276 BUG_ON(inode != path->dentry->d_inode);
1277 return 1;
1278 }
1279 path_to_nameidata(path, nd);
1280 nd->inode = inode;
1281 return 0;
1282}
1283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284/*
Al Virob3563792011-03-14 21:54:55 -04001285 * This limits recursive symlink follows to 8, while
1286 * limiting consecutive symlinks to 40.
1287 *
1288 * Without that kind of total limit, nasty chains of consecutive
1289 * symlinks can cause almost arbitrarily long lookups.
1290 */
1291static inline int nested_symlink(struct path *path, struct nameidata *nd)
1292{
1293 int res;
1294
Al Virob3563792011-03-14 21:54:55 -04001295 if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1296 path_put_conditional(path, nd);
1297 path_put(&nd->path);
1298 return -ELOOP;
1299 }
Erez Zadok1a4022f2011-05-21 01:19:59 -04001300 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
Al Virob3563792011-03-14 21:54:55 -04001301
1302 nd->depth++;
1303 current->link_count++;
1304
1305 do {
1306 struct path link = *path;
1307 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04001308
1309 res = follow_link(&link, nd, &cookie);
Al Virob3563792011-03-14 21:54:55 -04001310 if (!res)
1311 res = walk_component(nd, path, &nd->last,
1312 nd->last_type, LOOKUP_FOLLOW);
Al Viro574197e2011-03-14 22:20:34 -04001313 put_link(nd, &link, cookie);
Al Virob3563792011-03-14 21:54:55 -04001314 } while (res > 0);
1315
1316 current->link_count--;
1317 nd->depth--;
1318 return res;
1319}
1320
1321/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01001323 * This is the basic name resolution function, turning a pathname into
1324 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01001326 * Returns 0 and nd will have valid dentry and mnt on success.
1327 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 */
Al Viro6de88d72009-08-09 01:41:57 +04001329static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330{
1331 struct path next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334 while (*name=='/')
1335 name++;
1336 if (!*name)
Al Viro086e1832011-02-22 20:56:27 -05001337 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 /* At this point we know we have a real path component. */
1340 for(;;) {
1341 unsigned long hash;
1342 struct qstr this;
1343 unsigned int c;
Al Virofe479a52011-02-22 15:10:03 -05001344 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Al Viro52094c82011-02-21 21:34:47 -05001346 err = may_lookup(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 if (err)
1348 break;
1349
1350 this.name = name;
1351 c = *(const unsigned char *)name;
1352
1353 hash = init_name_hash();
1354 do {
1355 name++;
1356 hash = partial_name_hash(c, hash);
1357 c = *(const unsigned char *)name;
1358 } while (c && (c != '/'));
1359 this.len = name - (const char *) this.name;
1360 this.hash = end_name_hash(hash);
1361
Al Virofe479a52011-02-22 15:10:03 -05001362 type = LAST_NORM;
1363 if (this.name[0] == '.') switch (this.len) {
1364 case 2:
Al Viro16c2cd72011-02-22 15:50:10 -05001365 if (this.name[1] == '.') {
Al Virofe479a52011-02-22 15:10:03 -05001366 type = LAST_DOTDOT;
Al Viro16c2cd72011-02-22 15:50:10 -05001367 nd->flags |= LOOKUP_JUMPED;
1368 }
Al Virofe479a52011-02-22 15:10:03 -05001369 break;
1370 case 1:
1371 type = LAST_DOT;
1372 }
Al Viro5a202bc2011-03-08 14:17:44 -05001373 if (likely(type == LAST_NORM)) {
1374 struct dentry *parent = nd->path.dentry;
Al Viro16c2cd72011-02-22 15:50:10 -05001375 nd->flags &= ~LOOKUP_JUMPED;
Al Viro5a202bc2011-03-08 14:17:44 -05001376 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1377 err = parent->d_op->d_hash(parent, nd->inode,
1378 &this);
1379 if (err < 0)
1380 break;
1381 }
1382 }
Al Virofe479a52011-02-22 15:10:03 -05001383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 /* remove trailing slashes? */
1385 if (!c)
1386 goto last_component;
1387 while (*++name == '/');
1388 if (!*name)
Al Virob3563792011-03-14 21:54:55 -04001389 goto last_component;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Al Viroce57dfc2011-03-13 19:58:58 -04001391 err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1392 if (err < 0)
1393 return err;
Al Virofe479a52011-02-22 15:10:03 -05001394
Al Viroce57dfc2011-03-13 19:58:58 -04001395 if (err) {
Al Virob3563792011-03-14 21:54:55 -04001396 err = nested_symlink(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 if (err)
Al Viroa7472ba2011-03-04 14:39:30 -05001398 return err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 err = -ENOTDIR;
Nick Piggin31e6b012011-01-07 17:49:52 +11001401 if (!nd->inode->i_op->lookup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 break;
1403 continue;
1404 /* here ends the main loop */
1405
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406last_component:
Al Virob3563792011-03-14 21:54:55 -04001407 nd->last = this;
1408 nd->last_type = type;
Al Viro086e1832011-02-22 20:56:27 -05001409 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 }
Al Viro951361f2011-03-04 14:44:37 -05001411 terminate_walk(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 return err;
1413}
1414
Al Viro70e9b352011-03-05 21:12:22 -05001415static int path_init(int dfd, const char *name, unsigned int flags,
1416 struct nameidata *nd, struct file **fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417{
Prasanna Medaea3834d2005-04-29 16:00:17 +01001418 int retval = 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001419 int fput_needed;
1420 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422 nd->last_type = LAST_ROOT; /* if there are only slashes... */
Al Viro16c2cd72011-02-22 15:50:10 -05001423 nd->flags = flags | LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 nd->depth = 0;
Al Viro5b6ca022011-03-09 23:04:47 -05001425 if (flags & LOOKUP_ROOT) {
1426 struct inode *inode = nd->root.dentry->d_inode;
Al Viro73d049a2011-03-11 12:08:24 -05001427 if (*name) {
1428 if (!inode->i_op->lookup)
1429 return -ENOTDIR;
1430 retval = inode_permission(inode, MAY_EXEC);
1431 if (retval)
1432 return retval;
1433 }
Al Viro5b6ca022011-03-09 23:04:47 -05001434 nd->path = nd->root;
1435 nd->inode = inode;
1436 if (flags & LOOKUP_RCU) {
1437 br_read_lock(vfsmount_lock);
1438 rcu_read_lock();
1439 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1440 } else {
1441 path_get(&nd->path);
1442 }
1443 return 0;
1444 }
1445
Al Viro2a737872009-04-07 11:49:53 -04001446 nd->root.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 if (*name=='/') {
Al Viroe41f7d42011-02-22 14:02:58 -05001449 if (flags & LOOKUP_RCU) {
1450 br_read_lock(vfsmount_lock);
1451 rcu_read_lock();
1452 set_root_rcu(nd);
1453 } else {
1454 set_root(nd);
1455 path_get(&nd->root);
1456 }
Al Viro2a737872009-04-07 11:49:53 -04001457 nd->path = nd->root;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001458 } else if (dfd == AT_FDCWD) {
Al Viroe41f7d42011-02-22 14:02:58 -05001459 if (flags & LOOKUP_RCU) {
1460 struct fs_struct *fs = current->fs;
1461 unsigned seq;
1462
1463 br_read_lock(vfsmount_lock);
1464 rcu_read_lock();
1465
1466 do {
1467 seq = read_seqcount_begin(&fs->seq);
1468 nd->path = fs->pwd;
1469 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1470 } while (read_seqcount_retry(&fs->seq, seq));
1471 } else {
1472 get_fs_pwd(current->fs, &nd->path);
1473 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001474 } else {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001475 struct dentry *dentry;
1476
Al Viro1abf0c72011-03-13 03:51:11 -04001477 file = fget_raw_light(dfd, &fput_needed);
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001478 retval = -EBADF;
1479 if (!file)
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001480 goto out_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001481
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001482 dentry = file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001483
Al Virof52e0c12011-03-14 18:56:51 -04001484 if (*name) {
1485 retval = -ENOTDIR;
1486 if (!S_ISDIR(dentry->d_inode->i_mode))
1487 goto fput_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001488
Al Viro4ad5abb2011-06-20 19:57:03 -04001489 retval = inode_permission(dentry->d_inode, MAY_EXEC);
Al Virof52e0c12011-03-14 18:56:51 -04001490 if (retval)
1491 goto fput_fail;
1492 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001493
Jan Blunck5dd784d02008-02-14 19:34:38 -08001494 nd->path = file->f_path;
Al Viroe41f7d42011-02-22 14:02:58 -05001495 if (flags & LOOKUP_RCU) {
1496 if (fput_needed)
Al Viro70e9b352011-03-05 21:12:22 -05001497 *fp = file;
Al Viroe41f7d42011-02-22 14:02:58 -05001498 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1499 br_read_lock(vfsmount_lock);
1500 rcu_read_lock();
1501 } else {
1502 path_get(&file->f_path);
1503 fput_light(file, fput_needed);
1504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 }
Al Viroe41f7d42011-02-22 14:02:58 -05001506
Nick Piggin31e6b012011-01-07 17:49:52 +11001507 nd->inode = nd->path.dentry->d_inode;
Al Viro9b4a9b12009-04-07 11:44:16 -04001508 return 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001509
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001510fput_fail:
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001511 fput_light(file, fput_needed);
Al Viro9b4a9b12009-04-07 11:44:16 -04001512out_fail:
1513 return retval;
1514}
1515
Al Virobd92d7f2011-03-14 19:54:59 -04001516static inline int lookup_last(struct nameidata *nd, struct path *path)
1517{
1518 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1519 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1520
1521 nd->flags &= ~LOOKUP_PARENT;
1522 return walk_component(nd, path, &nd->last, nd->last_type,
1523 nd->flags & LOOKUP_FOLLOW);
1524}
1525
Al Viro9b4a9b12009-04-07 11:44:16 -04001526/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroee0827c2011-02-21 23:38:09 -05001527static int path_lookupat(int dfd, const char *name,
Al Viro9b4a9b12009-04-07 11:44:16 -04001528 unsigned int flags, struct nameidata *nd)
1529{
Al Viro70e9b352011-03-05 21:12:22 -05001530 struct file *base = NULL;
Al Virobd92d7f2011-03-14 19:54:59 -04001531 struct path path;
1532 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001533
1534 /*
1535 * Path walking is largely split up into 2 different synchronisation
1536 * schemes, rcu-walk and ref-walk (explained in
1537 * Documentation/filesystems/path-lookup.txt). These share much of the
1538 * path walk code, but some things particularly setup, cleanup, and
1539 * following mounts are sufficiently divergent that functions are
1540 * duplicated. Typically there is a function foo(), and its RCU
1541 * analogue, foo_rcu().
1542 *
1543 * -ECHILD is the error number of choice (just to avoid clashes) that
1544 * is returned if some aspect of an rcu-walk fails. Such an error must
1545 * be handled by restarting a traditional ref-walk (which will always
1546 * be able to complete).
1547 */
Al Virobd92d7f2011-03-14 19:54:59 -04001548 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
Al Viroee0827c2011-02-21 23:38:09 -05001549
Al Virobd92d7f2011-03-14 19:54:59 -04001550 if (unlikely(err))
1551 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001552
1553 current->total_link_count = 0;
Al Virobd92d7f2011-03-14 19:54:59 -04001554 err = link_path_walk(name, nd);
1555
1556 if (!err && !(flags & LOOKUP_PARENT)) {
Al Virobd92d7f2011-03-14 19:54:59 -04001557 err = lookup_last(nd, &path);
1558 while (err > 0) {
1559 void *cookie;
1560 struct path link = path;
Al Virobd92d7f2011-03-14 19:54:59 -04001561 nd->flags |= LOOKUP_PARENT;
Al Viro574197e2011-03-14 22:20:34 -04001562 err = follow_link(&link, nd, &cookie);
Al Virobd92d7f2011-03-14 19:54:59 -04001563 if (!err)
1564 err = lookup_last(nd, &path);
Al Viro574197e2011-03-14 22:20:34 -04001565 put_link(nd, &link, cookie);
Al Virobd92d7f2011-03-14 19:54:59 -04001566 }
1567 }
Al Viroee0827c2011-02-21 23:38:09 -05001568
Al Viro9f1fafe2011-03-25 11:00:12 -04001569 if (!err)
1570 err = complete_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04001571
1572 if (!err && nd->flags & LOOKUP_DIRECTORY) {
1573 if (!nd->inode->i_op->lookup) {
1574 path_put(&nd->path);
Al Virobd23a532011-03-23 09:56:30 -04001575 err = -ENOTDIR;
Al Virobd92d7f2011-03-14 19:54:59 -04001576 }
1577 }
Al Viro16c2cd72011-02-22 15:50:10 -05001578
Al Viro70e9b352011-03-05 21:12:22 -05001579 if (base)
1580 fput(base);
Al Viroee0827c2011-02-21 23:38:09 -05001581
Al Viro5b6ca022011-03-09 23:04:47 -05001582 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
Al Viro2a737872009-04-07 11:49:53 -04001583 path_put(&nd->root);
1584 nd->root.mnt = NULL;
1585 }
Al Virobd92d7f2011-03-14 19:54:59 -04001586 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001587}
Nick Piggin31e6b012011-01-07 17:49:52 +11001588
Al Viroee0827c2011-02-21 23:38:09 -05001589static int do_path_lookup(int dfd, const char *name,
1590 unsigned int flags, struct nameidata *nd)
1591{
1592 int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1593 if (unlikely(retval == -ECHILD))
1594 retval = path_lookupat(dfd, name, flags, nd);
1595 if (unlikely(retval == -ESTALE))
1596 retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11001597
1598 if (likely(!retval)) {
1599 if (unlikely(!audit_dummy_context())) {
1600 if (nd->path.dentry && nd->inode)
1601 audit_inode(name, nd->path.dentry);
1602 }
1603 }
Al Viro9b4a9b12009-04-07 11:44:16 -04001604 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605}
1606
Al Viroc9c6cac2011-02-16 15:15:47 -05001607int kern_path_parent(const char *name, struct nameidata *nd)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001608{
Al Viroc9c6cac2011-02-16 15:15:47 -05001609 return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001610}
1611
Al Virod1811462008-08-02 00:49:18 -04001612int kern_path(const char *name, unsigned int flags, struct path *path)
1613{
1614 struct nameidata nd;
1615 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1616 if (!res)
1617 *path = nd.path;
1618 return res;
1619}
1620
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001621/**
1622 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1623 * @dentry: pointer to dentry of the base directory
1624 * @mnt: pointer to vfs mount of the base directory
1625 * @name: pointer to file name
1626 * @flags: lookup flags
Al Viroe0a01242011-06-27 17:00:37 -04001627 * @path: pointer to struct path to fill
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001628 */
1629int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
1630 const char *name, unsigned int flags,
Al Viroe0a01242011-06-27 17:00:37 -04001631 struct path *path)
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001632{
Al Viroe0a01242011-06-27 17:00:37 -04001633 struct nameidata nd;
1634 int err;
1635 nd.root.dentry = dentry;
1636 nd.root.mnt = mnt;
1637 BUG_ON(flags & LOOKUP_PARENT);
Al Viro5b6ca022011-03-09 23:04:47 -05001638 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
Al Viroe0a01242011-06-27 17:00:37 -04001639 err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
1640 if (!err)
1641 *path = nd.path;
1642 return err;
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001643}
1644
Christoph Hellwigeead1912007-10-16 23:25:38 -07001645static struct dentry *__lookup_hash(struct qstr *name,
1646 struct dentry *base, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647{
Christoph Hellwig81fca442010-10-06 10:47:47 +02001648 struct inode *inode = base->d_inode;
James Morris057f6c02007-04-26 00:12:05 -07001649 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 int err;
1651
Al Viro4ad5abb2011-06-20 19:57:03 -04001652 err = inode_permission(inode, MAY_EXEC);
Christoph Hellwig81fca442010-10-06 10:47:47 +02001653 if (err)
1654 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
1656 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001657 * Don't bother with __d_lookup: callers are for creat as
1658 * well as unlink, so a lot of the time it would cost
1659 * a double lookup.
Al Viro6e6b1bd2009-08-13 23:38:37 +04001660 */
Nick Pigginb04f7842010-08-18 04:37:34 +10001661 dentry = d_lookup(base, name);
Al Viro6e6b1bd2009-08-13 23:38:37 +04001662
Josef Bacik44396f42011-05-31 11:58:49 -04001663 if (dentry && d_need_lookup(dentry)) {
1664 /*
1665 * __lookup_hash is called with the parent dir's i_mutex already
1666 * held, so we are good to go here.
1667 */
1668 dentry = d_inode_lookup(base, dentry, nd);
1669 if (IS_ERR(dentry))
1670 return dentry;
1671 }
1672
Al Virod2d9e9f2011-06-20 10:55:26 -04001673 if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE)) {
1674 int status = d_revalidate(dentry, nd);
1675 if (unlikely(status <= 0)) {
1676 /*
1677 * The dentry failed validation.
1678 * If d_revalidate returned 0 attempt to invalidate
1679 * the dentry otherwise d_revalidate is asking us
1680 * to return a fail status.
1681 */
1682 if (status < 0) {
1683 dput(dentry);
1684 return ERR_PTR(status);
1685 } else if (!d_invalidate(dentry)) {
1686 dput(dentry);
1687 dentry = NULL;
1688 }
1689 }
1690 }
Al Viro6e6b1bd2009-08-13 23:38:37 +04001691
Nick Pigginbaa03892010-08-18 04:37:31 +10001692 if (!dentry)
1693 dentry = d_alloc_and_lookup(base, name, nd);
Al Viro5a202bc2011-03-08 14:17:44 -05001694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 return dentry;
1696}
1697
James Morris057f6c02007-04-26 00:12:05 -07001698/*
1699 * Restricted form of lookup. Doesn't follow links, single-component only,
1700 * needs parent already locked. Doesn't follow mounts.
1701 * SMP-safe.
1702 */
Adrian Bunka244e162006-03-31 02:32:11 -08001703static struct dentry *lookup_hash(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704{
Jan Blunck4ac91372008-02-14 19:34:32 -08001705 return __lookup_hash(&nd->last, nd->path.dentry, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706}
1707
Christoph Hellwigeead1912007-10-16 23:25:38 -07001708/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07001709 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07001710 * @name: pathname component to lookup
1711 * @base: base directory to lookup from
1712 * @len: maximum length @len should be interpreted to
1713 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07001714 * Note that this routine is purely a helper for filesystem usage and should
1715 * not be called by generic code. Also note that by using this function the
Christoph Hellwigeead1912007-10-16 23:25:38 -07001716 * nameidata argument is passed to the filesystem methods and a filesystem
1717 * using this helper needs to be prepared for that.
1718 */
James Morris057f6c02007-04-26 00:12:05 -07001719struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1720{
James Morris057f6c02007-04-26 00:12:05 -07001721 struct qstr this;
Al Viro6a96ba52011-03-07 23:49:20 -05001722 unsigned long hash;
1723 unsigned int c;
James Morris057f6c02007-04-26 00:12:05 -07001724
David Woodhouse2f9092e2009-04-20 23:18:37 +01001725 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
1726
Al Viro6a96ba52011-03-07 23:49:20 -05001727 this.name = name;
1728 this.len = len;
1729 if (!len)
1730 return ERR_PTR(-EACCES);
1731
1732 hash = init_name_hash();
1733 while (len--) {
1734 c = *(const unsigned char *)name++;
1735 if (c == '/' || c == '\0')
1736 return ERR_PTR(-EACCES);
1737 hash = partial_name_hash(c, hash);
1738 }
1739 this.hash = end_name_hash(hash);
Al Viro5a202bc2011-03-08 14:17:44 -05001740 /*
1741 * See if the low-level filesystem might want
1742 * to use its own hash..
1743 */
1744 if (base->d_flags & DCACHE_OP_HASH) {
1745 int err = base->d_op->d_hash(base, base->d_inode, &this);
1746 if (err < 0)
1747 return ERR_PTR(err);
1748 }
Christoph Hellwigeead1912007-10-16 23:25:38 -07001749
Christoph Hellwig49705b72005-11-08 21:35:06 -08001750 return __lookup_hash(&this, base, NULL);
James Morris057f6c02007-04-26 00:12:05 -07001751}
1752
Al Viro2d8f3032008-07-22 09:59:21 -04001753int user_path_at(int dfd, const char __user *name, unsigned flags,
1754 struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755{
Al Viro2d8f3032008-07-22 09:59:21 -04001756 struct nameidata nd;
Al Virof52e0c12011-03-14 18:56:51 -04001757 char *tmp = getname_flags(name, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 int err = PTR_ERR(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 if (!IS_ERR(tmp)) {
Al Viro2d8f3032008-07-22 09:59:21 -04001760
1761 BUG_ON(flags & LOOKUP_PARENT);
1762
1763 err = do_path_lookup(dfd, tmp, flags, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 putname(tmp);
Al Viro2d8f3032008-07-22 09:59:21 -04001765 if (!err)
1766 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 }
1768 return err;
1769}
1770
Al Viro2ad94ae2008-07-21 09:32:51 -04001771static int user_path_parent(int dfd, const char __user *path,
1772 struct nameidata *nd, char **name)
1773{
1774 char *s = getname(path);
1775 int error;
1776
1777 if (IS_ERR(s))
1778 return PTR_ERR(s);
1779
1780 error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
1781 if (error)
1782 putname(s);
1783 else
1784 *name = s;
1785
1786 return error;
1787}
1788
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789/*
1790 * It's inline, so penalty for filesystems that don't use sticky bit is
1791 * minimal.
1792 */
1793static inline int check_sticky(struct inode *dir, struct inode *inode)
1794{
David Howellsda9592e2008-11-14 10:39:05 +11001795 uid_t fsuid = current_fsuid();
1796
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 if (!(dir->i_mode & S_ISVTX))
1798 return 0;
Serge E. Hallyne795b712011-03-23 16:43:25 -07001799 if (current_user_ns() != inode_userns(inode))
1800 goto other_userns;
David Howellsda9592e2008-11-14 10:39:05 +11001801 if (inode->i_uid == fsuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 return 0;
David Howellsda9592e2008-11-14 10:39:05 +11001803 if (dir->i_uid == fsuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 return 0;
Serge E. Hallyne795b712011-03-23 16:43:25 -07001805
1806other_userns:
1807 return !ns_capable(inode_userns(inode), CAP_FOWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808}
1809
1810/*
1811 * Check whether we can remove a link victim from directory dir, check
1812 * whether the type of victim is right.
1813 * 1. We can't do it if dir is read-only (done in permission())
1814 * 2. We should have write and exec permissions on dir
1815 * 3. We can't remove anything from append-only dir
1816 * 4. We can't do anything with immutable dir (done in permission())
1817 * 5. If the sticky bit on dir is set we should either
1818 * a. be owner of dir, or
1819 * b. be owner of victim, or
1820 * c. have CAP_FOWNER capability
1821 * 6. If the victim is append-only or immutable we can't do antyhing with
1822 * links pointing to it.
1823 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1824 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1825 * 9. We can't remove a root or mountpoint.
1826 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1827 * nfs_async_unlink().
1828 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001829static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830{
1831 int error;
1832
1833 if (!victim->d_inode)
1834 return -ENOENT;
1835
1836 BUG_ON(victim->d_parent->d_inode != dir);
Al Virocccc6bb2009-12-25 05:07:33 -05001837 audit_inode_child(victim, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
Al Virof419a2e2008-07-22 00:07:17 -04001839 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 if (error)
1841 return error;
1842 if (IS_APPEND(dir))
1843 return -EPERM;
1844 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
Hugh Dickinsf9454542008-11-19 15:36:38 -08001845 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 return -EPERM;
1847 if (isdir) {
1848 if (!S_ISDIR(victim->d_inode->i_mode))
1849 return -ENOTDIR;
1850 if (IS_ROOT(victim))
1851 return -EBUSY;
1852 } else if (S_ISDIR(victim->d_inode->i_mode))
1853 return -EISDIR;
1854 if (IS_DEADDIR(dir))
1855 return -ENOENT;
1856 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1857 return -EBUSY;
1858 return 0;
1859}
1860
1861/* Check whether we can create an object with dentry child in directory
1862 * dir.
1863 * 1. We can't do it if child already exists (open has special treatment for
1864 * this case, but since we are inlined it's OK)
1865 * 2. We can't do it if dir is read-only (done in permission())
1866 * 3. We should have write and exec permissions on dir
1867 * 4. We can't do it if dir is immutable (done in permission())
1868 */
Miklos Szeredia95164d2008-07-30 15:08:48 +02001869static inline int may_create(struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870{
1871 if (child->d_inode)
1872 return -EEXIST;
1873 if (IS_DEADDIR(dir))
1874 return -ENOENT;
Al Virof419a2e2008-07-22 00:07:17 -04001875 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876}
1877
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878/*
1879 * p1 and p2 should be directories on the same fs.
1880 */
1881struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1882{
1883 struct dentry *p;
1884
1885 if (p1 == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07001886 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 return NULL;
1888 }
1889
Arjan van de Vena11f3a02006-03-23 03:00:33 -08001890 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001892 p = d_ancestor(p2, p1);
1893 if (p) {
1894 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1895 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
1896 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 }
1898
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001899 p = d_ancestor(p1, p2);
1900 if (p) {
1901 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1902 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1903 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 }
1905
Ingo Molnarf2eace22006-07-03 00:25:05 -07001906 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1907 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 return NULL;
1909}
1910
1911void unlock_rename(struct dentry *p1, struct dentry *p2)
1912{
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001913 mutex_unlock(&p1->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 if (p1 != p2) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001915 mutex_unlock(&p2->d_inode->i_mutex);
Arjan van de Vena11f3a02006-03-23 03:00:33 -08001916 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 }
1918}
1919
1920int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1921 struct nameidata *nd)
1922{
Miklos Szeredia95164d2008-07-30 15:08:48 +02001923 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
1925 if (error)
1926 return error;
1927
Al Viroacfa4382008-12-04 10:06:33 -05001928 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 return -EACCES; /* shouldn't it be ENOSYS? */
1930 mode &= S_IALLUGO;
1931 mode |= S_IFREG;
1932 error = security_inode_create(dir, dentry, mode);
1933 if (error)
1934 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 error = dir->i_op->create(dir, dentry, mode, nd);
Stephen Smalleya74574a2005-09-09 13:01:44 -07001936 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00001937 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 return error;
1939}
1940
Al Viro73d049a2011-03-11 12:08:24 -05001941static int may_open(struct path *path, int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001943 struct dentry *dentry = path->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 struct inode *inode = dentry->d_inode;
1945 int error;
1946
Al Virobcda7652011-03-13 16:42:14 -04001947 /* O_PATH? */
1948 if (!acc_mode)
1949 return 0;
1950
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 if (!inode)
1952 return -ENOENT;
1953
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001954 switch (inode->i_mode & S_IFMT) {
1955 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001957 case S_IFDIR:
1958 if (acc_mode & MAY_WRITE)
1959 return -EISDIR;
1960 break;
1961 case S_IFBLK:
1962 case S_IFCHR:
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001963 if (path->mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001965 /*FALLTHRU*/
1966 case S_IFIFO:
1967 case S_IFSOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001969 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08001970 }
Dave Hansenb41572e2007-10-16 23:31:14 -07001971
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001972 error = inode_permission(inode, acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07001973 if (error)
1974 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05001975
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 /*
1977 * An append-only file must be opened in append mode for writing.
1978 */
1979 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05001980 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05001981 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05001983 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 }
1985
1986 /* O_NOATIME can only be set by the owner or superuser */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001987 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
Al Viro7715b522009-12-16 03:54:00 -05001988 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
1990 /*
1991 * Ensure there are no outstanding leases on the file.
1992 */
Al Virob65a9cf2009-12-16 06:27:40 -05001993 return break_lease(inode, flag);
Al Viro7715b522009-12-16 03:54:00 -05001994}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
Jeff Laytone1181ee2010-12-07 16:19:50 -05001996static int handle_truncate(struct file *filp)
Al Viro7715b522009-12-16 03:54:00 -05001997{
Jeff Laytone1181ee2010-12-07 16:19:50 -05001998 struct path *path = &filp->f_path;
Al Viro7715b522009-12-16 03:54:00 -05001999 struct inode *inode = path->dentry->d_inode;
2000 int error = get_write_access(inode);
2001 if (error)
2002 return error;
2003 /*
2004 * Refuse to truncate files with mandatory locks held on them.
2005 */
2006 error = locks_verify_locked(inode);
2007 if (!error)
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +09002008 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05002009 if (!error) {
2010 error = do_truncate(path->dentry, 0,
2011 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
Jeff Laytone1181ee2010-12-07 16:19:50 -05002012 filp);
Al Viro7715b522009-12-16 03:54:00 -05002013 }
2014 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04002015 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016}
2017
Dave Hansend57999e2008-02-15 14:37:27 -08002018static inline int open_to_namei_flags(int flag)
2019{
Al Viro8a5e9292011-06-25 19:15:54 -04002020 if ((flag & O_ACCMODE) == 3)
2021 flag--;
Dave Hansend57999e2008-02-15 14:37:27 -08002022 return flag;
2023}
2024
Nick Piggin31e6b012011-01-07 17:49:52 +11002025/*
Al Virofe2d35f2011-03-05 22:58:25 -05002026 * Handle the last step of open()
Nick Piggin31e6b012011-01-07 17:49:52 +11002027 */
Al Virofb1cc552009-12-24 01:58:28 -05002028static struct file *do_last(struct nameidata *nd, struct path *path,
Al Viroc3e380b2011-02-23 13:39:45 -05002029 const struct open_flags *op, const char *pathname)
Al Virofb1cc552009-12-24 01:58:28 -05002030{
Al Viroa1e28032009-12-24 02:12:06 -05002031 struct dentry *dir = nd->path.dentry;
Al Viro6c0d46c2011-03-09 00:59:59 -05002032 struct dentry *dentry;
Al Viroca344a892011-03-09 00:36:45 -05002033 int open_flag = op->open_flag;
Al Viro6c0d46c2011-03-09 00:59:59 -05002034 int will_truncate = open_flag & O_TRUNC;
Al Viroca344a892011-03-09 00:36:45 -05002035 int want_write = 0;
Al Virobcda7652011-03-13 16:42:14 -04002036 int acc_mode = op->acc_mode;
Al Virofb1cc552009-12-24 01:58:28 -05002037 struct file *filp;
Al Viro16c2cd72011-02-22 15:50:10 -05002038 int error;
Al Virofb1cc552009-12-24 01:58:28 -05002039
Al Viroc3e380b2011-02-23 13:39:45 -05002040 nd->flags &= ~LOOKUP_PARENT;
2041 nd->flags |= op->intent;
2042
Al Viro1f36f772009-12-26 10:56:19 -05002043 switch (nd->last_type) {
2044 case LAST_DOTDOT:
Neil Brown176306f2010-05-24 16:57:56 +10002045 case LAST_DOT:
Al Virofe2d35f2011-03-05 22:58:25 -05002046 error = handle_dots(nd, nd->last_type);
2047 if (error)
2048 return ERR_PTR(error);
Al Viro1f36f772009-12-26 10:56:19 -05002049 /* fallthrough */
Al Viro1f36f772009-12-26 10:56:19 -05002050 case LAST_ROOT:
Al Viro9f1fafe2011-03-25 11:00:12 -04002051 error = complete_walk(nd);
Al Viro16c2cd72011-02-22 15:50:10 -05002052 if (error)
Al Viro9f1fafe2011-03-25 11:00:12 -04002053 return ERR_PTR(error);
Al Virofe2d35f2011-03-05 22:58:25 -05002054 audit_inode(pathname, nd->path.dentry);
Al Viroca344a892011-03-09 00:36:45 -05002055 if (open_flag & O_CREAT) {
Al Virofe2d35f2011-03-05 22:58:25 -05002056 error = -EISDIR;
2057 goto exit;
2058 }
2059 goto ok;
Al Viro1f36f772009-12-26 10:56:19 -05002060 case LAST_BIND:
Al Viro9f1fafe2011-03-25 11:00:12 -04002061 error = complete_walk(nd);
Al Viro16c2cd72011-02-22 15:50:10 -05002062 if (error)
Al Viro9f1fafe2011-03-25 11:00:12 -04002063 return ERR_PTR(error);
Al Viro1f36f772009-12-26 10:56:19 -05002064 audit_inode(pathname, dir);
Al Viro67ee3ad2009-12-26 07:01:01 -05002065 goto ok;
Al Viro1f36f772009-12-26 10:56:19 -05002066 }
Al Viro67ee3ad2009-12-26 07:01:01 -05002067
Al Viroca344a892011-03-09 00:36:45 -05002068 if (!(open_flag & O_CREAT)) {
Al Virobcda7652011-03-13 16:42:14 -04002069 int symlink_ok = 0;
Al Virofe2d35f2011-03-05 22:58:25 -05002070 if (nd->last.name[nd->last.len])
2071 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
Al Virobcda7652011-03-13 16:42:14 -04002072 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2073 symlink_ok = 1;
Al Virofe2d35f2011-03-05 22:58:25 -05002074 /* we _can_ be in RCU mode here */
Al Viroce57dfc2011-03-13 19:58:58 -04002075 error = walk_component(nd, path, &nd->last, LAST_NORM,
2076 !symlink_ok);
2077 if (error < 0)
Al Virofe2d35f2011-03-05 22:58:25 -05002078 return ERR_PTR(error);
Al Viroce57dfc2011-03-13 19:58:58 -04002079 if (error) /* symlink */
Al Virofe2d35f2011-03-05 22:58:25 -05002080 return NULL;
Al Virofe2d35f2011-03-05 22:58:25 -05002081 /* sayonara */
Al Viro9f1fafe2011-03-25 11:00:12 -04002082 error = complete_walk(nd);
2083 if (error)
2084 return ERR_PTR(-ECHILD);
Al Virofe2d35f2011-03-05 22:58:25 -05002085
2086 error = -ENOTDIR;
2087 if (nd->flags & LOOKUP_DIRECTORY) {
Al Viroce57dfc2011-03-13 19:58:58 -04002088 if (!nd->inode->i_op->lookup)
Al Virofe2d35f2011-03-05 22:58:25 -05002089 goto exit;
2090 }
2091 audit_inode(pathname, nd->path.dentry);
2092 goto ok;
2093 }
2094
2095 /* create side of things */
Al Viro9f1fafe2011-03-25 11:00:12 -04002096 error = complete_walk(nd);
2097 if (error)
2098 return ERR_PTR(error);
Al Virofe2d35f2011-03-05 22:58:25 -05002099
2100 audit_inode(pathname, dir);
Al Viro16c2cd72011-02-22 15:50:10 -05002101 error = -EISDIR;
Al Viro1f36f772009-12-26 10:56:19 -05002102 /* trailing slashes? */
Nick Piggin31e6b012011-01-07 17:49:52 +11002103 if (nd->last.name[nd->last.len])
2104 goto exit;
Al Viroa2c36b42009-12-24 03:39:50 -05002105
Al Viroa1e28032009-12-24 02:12:06 -05002106 mutex_lock(&dir->d_inode->i_mutex);
2107
Al Viro6c0d46c2011-03-09 00:59:59 -05002108 dentry = lookup_hash(nd);
2109 error = PTR_ERR(dentry);
2110 if (IS_ERR(dentry)) {
Al Virofb1cc552009-12-24 01:58:28 -05002111 mutex_unlock(&dir->d_inode->i_mutex);
2112 goto exit;
2113 }
2114
Al Viro6c0d46c2011-03-09 00:59:59 -05002115 path->dentry = dentry;
2116 path->mnt = nd->path.mnt;
2117
Al Virofb1cc552009-12-24 01:58:28 -05002118 /* Negative dentry, just create the file */
Al Viro6c0d46c2011-03-09 00:59:59 -05002119 if (!dentry->d_inode) {
2120 int mode = op->mode;
2121 if (!IS_POSIXACL(dir->d_inode))
2122 mode &= ~current_umask();
Al Virofb1cc552009-12-24 01:58:28 -05002123 /*
2124 * This write is needed to ensure that a
Al Viro6c0d46c2011-03-09 00:59:59 -05002125 * rw->ro transition does not occur between
Al Virofb1cc552009-12-24 01:58:28 -05002126 * the time when the file is created and when
2127 * a permanent write count is taken through
2128 * the 'struct file' in nameidata_to_filp().
2129 */
2130 error = mnt_want_write(nd->path.mnt);
2131 if (error)
2132 goto exit_mutex_unlock;
Al Viroca344a892011-03-09 00:36:45 -05002133 want_write = 1;
Al Viro9b44f1b2011-03-09 00:17:27 -05002134 /* Don't check for write permission, don't truncate */
Al Viroca344a892011-03-09 00:36:45 -05002135 open_flag &= ~O_TRUNC;
Al Viro6c0d46c2011-03-09 00:59:59 -05002136 will_truncate = 0;
Al Virobcda7652011-03-13 16:42:14 -04002137 acc_mode = MAY_OPEN;
Al Viro6c0d46c2011-03-09 00:59:59 -05002138 error = security_path_mknod(&nd->path, dentry, mode, 0);
2139 if (error)
2140 goto exit_mutex_unlock;
2141 error = vfs_create(dir->d_inode, dentry, mode, nd);
2142 if (error)
2143 goto exit_mutex_unlock;
2144 mutex_unlock(&dir->d_inode->i_mutex);
2145 dput(nd->path.dentry);
2146 nd->path.dentry = dentry;
Al Viroca344a892011-03-09 00:36:45 -05002147 goto common;
Al Virofb1cc552009-12-24 01:58:28 -05002148 }
2149
2150 /*
2151 * It already exists.
2152 */
2153 mutex_unlock(&dir->d_inode->i_mutex);
2154 audit_inode(pathname, path->dentry);
2155
2156 error = -EEXIST;
Al Viroca344a892011-03-09 00:36:45 -05002157 if (open_flag & O_EXCL)
Al Virofb1cc552009-12-24 01:58:28 -05002158 goto exit_dput;
2159
David Howells9875cf82011-01-14 18:45:21 +00002160 error = follow_managed(path, nd->flags);
2161 if (error < 0)
2162 goto exit_dput;
Al Virofb1cc552009-12-24 01:58:28 -05002163
2164 error = -ENOENT;
2165 if (!path->dentry->d_inode)
2166 goto exit_dput;
Al Viro9e67f362009-12-26 07:04:50 -05002167
2168 if (path->dentry->d_inode->i_op->follow_link)
Al Virofb1cc552009-12-24 01:58:28 -05002169 return NULL;
Al Virofb1cc552009-12-24 01:58:28 -05002170
2171 path_to_nameidata(path, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11002172 nd->inode = path->dentry->d_inode;
Al Virofb1cc552009-12-24 01:58:28 -05002173 error = -EISDIR;
Nick Piggin31e6b012011-01-07 17:49:52 +11002174 if (S_ISDIR(nd->inode->i_mode))
Al Virofb1cc552009-12-24 01:58:28 -05002175 goto exit;
Al Viro67ee3ad2009-12-26 07:01:01 -05002176ok:
Al Viro6c0d46c2011-03-09 00:59:59 -05002177 if (!S_ISREG(nd->inode->i_mode))
2178 will_truncate = 0;
2179
Al Viro0f9d1a12011-03-09 00:13:14 -05002180 if (will_truncate) {
2181 error = mnt_want_write(nd->path.mnt);
2182 if (error)
2183 goto exit;
Al Viroca344a892011-03-09 00:36:45 -05002184 want_write = 1;
Al Viro0f9d1a12011-03-09 00:13:14 -05002185 }
Al Viroca344a892011-03-09 00:36:45 -05002186common:
Al Virobcda7652011-03-13 16:42:14 -04002187 error = may_open(&nd->path, acc_mode, open_flag);
Al Viroca344a892011-03-09 00:36:45 -05002188 if (error)
Al Viro0f9d1a12011-03-09 00:13:14 -05002189 goto exit;
Al Viro0f9d1a12011-03-09 00:13:14 -05002190 filp = nameidata_to_filp(nd);
2191 if (!IS_ERR(filp)) {
2192 error = ima_file_check(filp, op->acc_mode);
2193 if (error) {
2194 fput(filp);
2195 filp = ERR_PTR(error);
2196 }
2197 }
2198 if (!IS_ERR(filp)) {
2199 if (will_truncate) {
2200 error = handle_truncate(filp);
2201 if (error) {
2202 fput(filp);
2203 filp = ERR_PTR(error);
2204 }
2205 }
2206 }
Al Viroca344a892011-03-09 00:36:45 -05002207out:
2208 if (want_write)
Al Viro0f9d1a12011-03-09 00:13:14 -05002209 mnt_drop_write(nd->path.mnt);
2210 path_put(&nd->path);
Al Virofb1cc552009-12-24 01:58:28 -05002211 return filp;
2212
2213exit_mutex_unlock:
2214 mutex_unlock(&dir->d_inode->i_mutex);
2215exit_dput:
2216 path_put_conditional(path, nd);
2217exit:
Al Viroca344a892011-03-09 00:36:45 -05002218 filp = ERR_PTR(error);
2219 goto out;
Al Virofb1cc552009-12-24 01:58:28 -05002220}
2221
Al Viro13aab422011-02-23 17:54:08 -05002222static struct file *path_openat(int dfd, const char *pathname,
Al Viro73d049a2011-03-11 12:08:24 -05002223 struct nameidata *nd, const struct open_flags *op, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224{
Al Virofe2d35f2011-03-05 22:58:25 -05002225 struct file *base = NULL;
Dave Hansen4a3fd212008-02-15 14:37:48 -08002226 struct file *filp;
Al Viro9850c052010-01-13 15:01:15 -05002227 struct path path;
Al Viro13aab422011-02-23 17:54:08 -05002228 int error;
Nick Piggin31e6b012011-01-07 17:49:52 +11002229
2230 filp = get_empty_filp();
2231 if (!filp)
2232 return ERR_PTR(-ENFILE);
2233
Al Viro47c805d2011-02-23 17:44:09 -05002234 filp->f_flags = op->open_flag;
Al Viro73d049a2011-03-11 12:08:24 -05002235 nd->intent.open.file = filp;
2236 nd->intent.open.flags = open_to_namei_flags(op->open_flag);
2237 nd->intent.open.create_mode = op->mode;
Nick Piggin31e6b012011-01-07 17:49:52 +11002238
Al Viro73d049a2011-03-11 12:08:24 -05002239 error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
Nick Piggin31e6b012011-01-07 17:49:52 +11002240 if (unlikely(error))
Al Viro13aab422011-02-23 17:54:08 -05002241 goto out_filp;
Nick Piggin31e6b012011-01-07 17:49:52 +11002242
Al Virofe2d35f2011-03-05 22:58:25 -05002243 current->total_link_count = 0;
Al Viro73d049a2011-03-11 12:08:24 -05002244 error = link_path_walk(pathname, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11002245 if (unlikely(error))
2246 goto out_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247
Al Viro73d049a2011-03-11 12:08:24 -05002248 filp = do_last(nd, &path, op, pathname);
Al Viro806b6812009-12-26 07:16:40 -05002249 while (unlikely(!filp)) { /* trailing symlink */
Nick Piggin7b9337a2011-01-14 08:42:43 +00002250 struct path link = path;
Al Virodef4af32009-12-26 08:37:05 -05002251 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04002252 if (!(nd->flags & LOOKUP_FOLLOW)) {
Al Viro73d049a2011-03-11 12:08:24 -05002253 path_put_conditional(&path, nd);
2254 path_put(&nd->path);
Al Viro40b39132011-03-09 16:22:18 -05002255 filp = ERR_PTR(-ELOOP);
2256 break;
2257 }
Al Viro73d049a2011-03-11 12:08:24 -05002258 nd->flags |= LOOKUP_PARENT;
2259 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
Al Viro574197e2011-03-14 22:20:34 -04002260 error = follow_link(&link, nd, &cookie);
Al Viroc3e380b2011-02-23 13:39:45 -05002261 if (unlikely(error))
Al Virof1afe9e2011-02-22 22:27:28 -05002262 filp = ERR_PTR(error);
Al Viroc3e380b2011-02-23 13:39:45 -05002263 else
Al Viro73d049a2011-03-11 12:08:24 -05002264 filp = do_last(nd, &path, op, pathname);
Al Viro574197e2011-03-14 22:20:34 -04002265 put_link(nd, &link, cookie);
Al Viro806b6812009-12-26 07:16:40 -05002266 }
Al Viro10fa8e62009-12-26 07:09:49 -05002267out:
Al Viro73d049a2011-03-11 12:08:24 -05002268 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
2269 path_put(&nd->root);
Al Virofe2d35f2011-03-05 22:58:25 -05002270 if (base)
2271 fput(base);
Al Viro73d049a2011-03-11 12:08:24 -05002272 release_open_intent(nd);
Al Viro10fa8e62009-12-26 07:09:49 -05002273 return filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
Nick Piggin31e6b012011-01-07 17:49:52 +11002275out_filp:
Al Viro806b6812009-12-26 07:16:40 -05002276 filp = ERR_PTR(error);
Al Viro10fa8e62009-12-26 07:09:49 -05002277 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278}
2279
Al Viro13aab422011-02-23 17:54:08 -05002280struct file *do_filp_open(int dfd, const char *pathname,
2281 const struct open_flags *op, int flags)
2282{
Al Viro73d049a2011-03-11 12:08:24 -05002283 struct nameidata nd;
Al Viro13aab422011-02-23 17:54:08 -05002284 struct file *filp;
2285
Al Viro73d049a2011-03-11 12:08:24 -05002286 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
Al Viro13aab422011-02-23 17:54:08 -05002287 if (unlikely(filp == ERR_PTR(-ECHILD)))
Al Viro73d049a2011-03-11 12:08:24 -05002288 filp = path_openat(dfd, pathname, &nd, op, flags);
Al Viro13aab422011-02-23 17:54:08 -05002289 if (unlikely(filp == ERR_PTR(-ESTALE)))
Al Viro73d049a2011-03-11 12:08:24 -05002290 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
Al Viro13aab422011-02-23 17:54:08 -05002291 return filp;
2292}
2293
Al Viro73d049a2011-03-11 12:08:24 -05002294struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
2295 const char *name, const struct open_flags *op, int flags)
2296{
2297 struct nameidata nd;
2298 struct file *file;
2299
2300 nd.root.mnt = mnt;
2301 nd.root.dentry = dentry;
2302
2303 flags |= LOOKUP_ROOT;
2304
Al Virobcda7652011-03-13 16:42:14 -04002305 if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
Al Viro73d049a2011-03-11 12:08:24 -05002306 return ERR_PTR(-ELOOP);
2307
2308 file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
2309 if (unlikely(file == ERR_PTR(-ECHILD)))
2310 file = path_openat(-1, name, &nd, op, flags);
2311 if (unlikely(file == ERR_PTR(-ESTALE)))
2312 file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
2313 return file;
2314}
2315
Al Viroed75e952011-06-27 16:53:43 -04002316struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002318 struct dentry *dentry = ERR_PTR(-EEXIST);
Al Viroed75e952011-06-27 16:53:43 -04002319 struct nameidata nd;
2320 int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
2321 if (error)
2322 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002324 /*
2325 * Yucky last component or no last component at all?
2326 * (foo/., foo/.., /////)
2327 */
Al Viroed75e952011-06-27 16:53:43 -04002328 if (nd.last_type != LAST_NORM)
2329 goto out;
2330 nd.flags &= ~LOOKUP_PARENT;
2331 nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
2332 nd.intent.open.flags = O_EXCL;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002333
2334 /*
2335 * Do the final lookup.
2336 */
Al Viroed75e952011-06-27 16:53:43 -04002337 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2338 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 if (IS_ERR(dentry))
2340 goto fail;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002341
Al Viroe9baf6e2008-05-15 04:49:12 -04002342 if (dentry->d_inode)
2343 goto eexist;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002344 /*
2345 * Special case - lookup gave negative, but... we had foo/bar/
2346 * From the vfs_mknod() POV we just have a negative dentry -
2347 * all is fine. Let's be bastards - you had / on the end, you've
2348 * been asking for (non-existent) directory. -ENOENT for you.
2349 */
Al Viroed75e952011-06-27 16:53:43 -04002350 if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
Al Viroe9baf6e2008-05-15 04:49:12 -04002351 dput(dentry);
2352 dentry = ERR_PTR(-ENOENT);
Al Viroed75e952011-06-27 16:53:43 -04002353 goto fail;
Al Viroe9baf6e2008-05-15 04:49:12 -04002354 }
Al Viroed75e952011-06-27 16:53:43 -04002355 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 return dentry;
Al Viroe9baf6e2008-05-15 04:49:12 -04002357eexist:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 dput(dentry);
Al Viroe9baf6e2008-05-15 04:49:12 -04002359 dentry = ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360fail:
Al Viroed75e952011-06-27 16:53:43 -04002361 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2362out:
2363 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 return dentry;
2365}
Al Virodae6ad82011-06-26 11:50:15 -04002366EXPORT_SYMBOL(kern_path_create);
2367
2368struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir)
2369{
2370 char *tmp = getname(pathname);
2371 struct dentry *res;
2372 if (IS_ERR(tmp))
2373 return ERR_CAST(tmp);
2374 res = kern_path_create(dfd, tmp, path, is_dir);
2375 putname(tmp);
2376 return res;
2377}
2378EXPORT_SYMBOL(user_path_create);
2379
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
2381{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002382 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
2384 if (error)
2385 return error;
2386
Serge E. Hallyne795b712011-03-23 16:43:25 -07002387 if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
2388 !ns_capable(inode_userns(dir), CAP_MKNOD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 return -EPERM;
2390
Al Viroacfa4382008-12-04 10:06:33 -05002391 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 return -EPERM;
2393
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07002394 error = devcgroup_inode_mknod(mode, dev);
2395 if (error)
2396 return error;
2397
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 error = security_inode_mknod(dir, dentry, mode, dev);
2399 if (error)
2400 return error;
2401
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002403 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002404 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 return error;
2406}
2407
Dave Hansen463c3192008-02-15 14:37:57 -08002408static int may_mknod(mode_t mode)
2409{
2410 switch (mode & S_IFMT) {
2411 case S_IFREG:
2412 case S_IFCHR:
2413 case S_IFBLK:
2414 case S_IFIFO:
2415 case S_IFSOCK:
2416 case 0: /* zero mode translates to S_IFREG */
2417 return 0;
2418 case S_IFDIR:
2419 return -EPERM;
2420 default:
2421 return -EINVAL;
2422 }
2423}
2424
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002425SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
2426 unsigned, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427{
Al Viro2ad94ae2008-07-21 09:32:51 -04002428 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04002429 struct path path;
2430 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
2432 if (S_ISDIR(mode))
2433 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
Al Virodae6ad82011-06-26 11:50:15 -04002435 dentry = user_path_create(dfd, filename, &path, 0);
2436 if (IS_ERR(dentry))
2437 return PTR_ERR(dentry);
Al Viro2ad94ae2008-07-21 09:32:51 -04002438
Al Virodae6ad82011-06-26 11:50:15 -04002439 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04002440 mode &= ~current_umask();
Dave Hansen463c3192008-02-15 14:37:57 -08002441 error = may_mknod(mode);
2442 if (error)
2443 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04002444 error = mnt_want_write(path.mnt);
Dave Hansen463c3192008-02-15 14:37:57 -08002445 if (error)
2446 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04002447 error = security_path_mknod(&path, dentry, mode, dev);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002448 if (error)
2449 goto out_drop_write;
Dave Hansen463c3192008-02-15 14:37:57 -08002450 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 case 0: case S_IFREG:
Al Virodae6ad82011-06-26 11:50:15 -04002452 error = vfs_create(path.dentry->d_inode,dentry,mode,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 break;
2454 case S_IFCHR: case S_IFBLK:
Al Virodae6ad82011-06-26 11:50:15 -04002455 error = vfs_mknod(path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 new_decode_dev(dev));
2457 break;
2458 case S_IFIFO: case S_IFSOCK:
Al Virodae6ad82011-06-26 11:50:15 -04002459 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 }
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002462out_drop_write:
Al Virodae6ad82011-06-26 11:50:15 -04002463 mnt_drop_write(path.mnt);
Dave Hansen463c3192008-02-15 14:37:57 -08002464out_dput:
2465 dput(dentry);
Al Virodae6ad82011-06-26 11:50:15 -04002466 mutex_unlock(&path.dentry->d_inode->i_mutex);
2467 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
2469 return error;
2470}
2471
Heiko Carstens3480b252009-01-14 14:14:16 +01002472SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002473{
2474 return sys_mknodat(AT_FDCWD, filename, mode, dev);
2475}
2476
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2478{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002479 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
2481 if (error)
2482 return error;
2483
Al Viroacfa4382008-12-04 10:06:33 -05002484 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 return -EPERM;
2486
2487 mode &= (S_IRWXUGO|S_ISVTX);
2488 error = security_inode_mkdir(dir, dentry, mode);
2489 if (error)
2490 return error;
2491
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002493 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002494 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 return error;
2496}
2497
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002498SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499{
Dave Hansen6902d922006-09-30 23:29:01 -07002500 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04002501 struct path path;
2502 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503
Al Virodae6ad82011-06-26 11:50:15 -04002504 dentry = user_path_create(dfd, pathname, &path, 1);
Dave Hansen6902d922006-09-30 23:29:01 -07002505 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04002506 return PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002507
Al Virodae6ad82011-06-26 11:50:15 -04002508 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04002509 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04002510 error = mnt_want_write(path.mnt);
Dave Hansen463c3192008-02-15 14:37:57 -08002511 if (error)
2512 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04002513 error = security_path_mkdir(&path, dentry, mode);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002514 if (error)
2515 goto out_drop_write;
Al Virodae6ad82011-06-26 11:50:15 -04002516 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002517out_drop_write:
Al Virodae6ad82011-06-26 11:50:15 -04002518 mnt_drop_write(path.mnt);
Dave Hansen463c3192008-02-15 14:37:57 -08002519out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002520 dput(dentry);
Al Virodae6ad82011-06-26 11:50:15 -04002521 mutex_unlock(&path.dentry->d_inode->i_mutex);
2522 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 return error;
2524}
2525
Heiko Carstens3cdad422009-01-14 14:14:22 +01002526SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002527{
2528 return sys_mkdirat(AT_FDCWD, pathname, mode);
2529}
2530
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531/*
Sage Weila71905f2011-05-24 13:06:08 -07002532 * The dentry_unhash() helper will try to drop the dentry early: we
2533 * should have a usage count of 2 if we're the only user of this
2534 * dentry, and if that is true (possibly after pruning the dcache),
2535 * then we drop the dentry now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 *
2537 * A low-level filesystem can, if it choses, legally
2538 * do a
2539 *
2540 * if (!d_unhashed(dentry))
2541 * return -EBUSY;
2542 *
2543 * if it cannot handle the case of removing a directory
2544 * that is still in use by something else..
2545 */
2546void dentry_unhash(struct dentry *dentry)
2547{
Vasily Averindc168422006-12-06 20:37:07 -08002548 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 spin_lock(&dentry->d_lock);
Sage Weil64252c72011-05-24 13:06:05 -07002550 if (dentry->d_count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 __d_drop(dentry);
2552 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553}
2554
2555int vfs_rmdir(struct inode *dir, struct dentry *dentry)
2556{
2557 int error = may_delete(dir, dentry, 1);
2558
2559 if (error)
2560 return error;
2561
Al Viroacfa4382008-12-04 10:06:33 -05002562 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 return -EPERM;
2564
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002565 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566
Sage Weil912dbc12011-05-24 13:06:11 -07002567 error = -EBUSY;
2568 if (d_mountpoint(dentry))
2569 goto out;
2570
2571 error = security_inode_rmdir(dir, dentry);
2572 if (error)
2573 goto out;
2574
Sage Weil3cebde22011-05-29 21:20:59 -07002575 shrink_dcache_parent(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07002576 error = dir->i_op->rmdir(dir, dentry);
2577 if (error)
2578 goto out;
2579
2580 dentry->d_inode->i_flags |= S_DEAD;
2581 dont_mount(dentry);
2582
2583out:
2584 mutex_unlock(&dentry->d_inode->i_mutex);
2585 if (!error)
2586 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 return error;
2588}
2589
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002590static long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591{
2592 int error = 0;
2593 char * name;
2594 struct dentry *dentry;
2595 struct nameidata nd;
2596
Al Viro2ad94ae2008-07-21 09:32:51 -04002597 error = user_path_parent(dfd, pathname, &nd, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002599 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600
2601 switch(nd.last_type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002602 case LAST_DOTDOT:
2603 error = -ENOTEMPTY;
2604 goto exit1;
2605 case LAST_DOT:
2606 error = -EINVAL;
2607 goto exit1;
2608 case LAST_ROOT:
2609 error = -EBUSY;
2610 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002612
2613 nd.flags &= ~LOOKUP_PARENT;
2614
Jan Blunck4ac91372008-02-14 19:34:32 -08002615 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002616 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002618 if (IS_ERR(dentry))
2619 goto exit2;
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04002620 if (!dentry->d_inode) {
2621 error = -ENOENT;
2622 goto exit3;
2623 }
Dave Hansen06227532008-02-15 14:37:34 -08002624 error = mnt_want_write(nd.path.mnt);
2625 if (error)
2626 goto exit3;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002627 error = security_path_rmdir(&nd.path, dentry);
2628 if (error)
2629 goto exit4;
Jan Blunck4ac91372008-02-14 19:34:32 -08002630 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002631exit4:
Dave Hansen06227532008-02-15 14:37:34 -08002632 mnt_drop_write(nd.path.mnt);
2633exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07002634 dput(dentry);
2635exit2:
Jan Blunck4ac91372008-02-14 19:34:32 -08002636 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002638 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 putname(name);
2640 return error;
2641}
2642
Heiko Carstens3cdad422009-01-14 14:14:22 +01002643SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002644{
2645 return do_rmdir(AT_FDCWD, pathname);
2646}
2647
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648int vfs_unlink(struct inode *dir, struct dentry *dentry)
2649{
2650 int error = may_delete(dir, dentry, 0);
2651
2652 if (error)
2653 return error;
2654
Al Viroacfa4382008-12-04 10:06:33 -05002655 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 return -EPERM;
2657
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002658 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 if (d_mountpoint(dentry))
2660 error = -EBUSY;
2661 else {
2662 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05002663 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 error = dir->i_op->unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05002665 if (!error)
Al Virod83c49f2010-04-30 17:17:09 -04002666 dont_mount(dentry);
Al Virobec10522010-03-03 14:12:08 -05002667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002669 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670
2671 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2672 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
Jan Karaece95912008-02-06 01:37:13 -08002673 fsnotify_link_count(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 }
Robert Love0eeca282005-07-12 17:06:03 -04002676
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 return error;
2678}
2679
2680/*
2681 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002682 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 * writeout happening, and we don't want to prevent access to the directory
2684 * while waiting on the I/O.
2685 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002686static long do_unlinkat(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687{
Al Viro2ad94ae2008-07-21 09:32:51 -04002688 int error;
2689 char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 struct dentry *dentry;
2691 struct nameidata nd;
2692 struct inode *inode = NULL;
2693
Al Viro2ad94ae2008-07-21 09:32:51 -04002694 error = user_path_parent(dfd, pathname, &nd, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002696 return error;
2697
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 error = -EISDIR;
2699 if (nd.last_type != LAST_NORM)
2700 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002701
2702 nd.flags &= ~LOOKUP_PARENT;
2703
Jan Blunck4ac91372008-02-14 19:34:32 -08002704 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002705 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 error = PTR_ERR(dentry);
2707 if (!IS_ERR(dentry)) {
2708 /* Why not before? Because we want correct error value */
Török Edwin50338b82011-06-16 00:06:14 +03002709 if (nd.last.name[nd.last.len])
2710 goto slashes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 inode = dentry->d_inode;
Török Edwin50338b82011-06-16 00:06:14 +03002712 if (!inode)
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04002713 goto slashes;
2714 ihold(inode);
Dave Hansen06227532008-02-15 14:37:34 -08002715 error = mnt_want_write(nd.path.mnt);
2716 if (error)
2717 goto exit2;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002718 error = security_path_unlink(&nd.path, dentry);
2719 if (error)
2720 goto exit3;
Jan Blunck4ac91372008-02-14 19:34:32 -08002721 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002722exit3:
Dave Hansen06227532008-02-15 14:37:34 -08002723 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 exit2:
2725 dput(dentry);
2726 }
Jan Blunck4ac91372008-02-14 19:34:32 -08002727 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 if (inode)
2729 iput(inode); /* truncate the inode here */
2730exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002731 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 putname(name);
2733 return error;
2734
2735slashes:
2736 error = !dentry->d_inode ? -ENOENT :
2737 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2738 goto exit2;
2739}
2740
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002741SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002742{
2743 if ((flag & ~AT_REMOVEDIR) != 0)
2744 return -EINVAL;
2745
2746 if (flag & AT_REMOVEDIR)
2747 return do_rmdir(dfd, pathname);
2748
2749 return do_unlinkat(dfd, pathname);
2750}
2751
Heiko Carstens3480b252009-01-14 14:14:16 +01002752SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002753{
2754 return do_unlinkat(AT_FDCWD, pathname);
2755}
2756
Miklos Szeredidb2e7472008-06-24 16:50:16 +02002757int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002759 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760
2761 if (error)
2762 return error;
2763
Al Viroacfa4382008-12-04 10:06:33 -05002764 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 return -EPERM;
2766
2767 error = security_inode_symlink(dir, dentry, oldname);
2768 if (error)
2769 return error;
2770
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002772 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002773 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 return error;
2775}
2776
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002777SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
2778 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779{
Al Viro2ad94ae2008-07-21 09:32:51 -04002780 int error;
2781 char *from;
Dave Hansen6902d922006-09-30 23:29:01 -07002782 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04002783 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
2785 from = getname(oldname);
Al Viro2ad94ae2008-07-21 09:32:51 -04002786 if (IS_ERR(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 return PTR_ERR(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04002788
Al Virodae6ad82011-06-26 11:50:15 -04002789 dentry = user_path_create(newdfd, newname, &path, 0);
Dave Hansen6902d922006-09-30 23:29:01 -07002790 error = PTR_ERR(dentry);
2791 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04002792 goto out_putname;
Dave Hansen6902d922006-09-30 23:29:01 -07002793
Al Virodae6ad82011-06-26 11:50:15 -04002794 error = mnt_want_write(path.mnt);
Dave Hansen75c3f292008-02-15 14:37:45 -08002795 if (error)
2796 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04002797 error = security_path_symlink(&path, dentry, from);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002798 if (error)
2799 goto out_drop_write;
Al Virodae6ad82011-06-26 11:50:15 -04002800 error = vfs_symlink(path.dentry->d_inode, dentry, from);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002801out_drop_write:
Al Virodae6ad82011-06-26 11:50:15 -04002802 mnt_drop_write(path.mnt);
Dave Hansen75c3f292008-02-15 14:37:45 -08002803out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002804 dput(dentry);
Al Virodae6ad82011-06-26 11:50:15 -04002805 mutex_unlock(&path.dentry->d_inode->i_mutex);
2806 path_put(&path);
Dave Hansen6902d922006-09-30 23:29:01 -07002807out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 putname(from);
2809 return error;
2810}
2811
Heiko Carstens3480b252009-01-14 14:14:16 +01002812SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002813{
2814 return sys_symlinkat(oldname, AT_FDCWD, newname);
2815}
2816
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2818{
2819 struct inode *inode = old_dentry->d_inode;
2820 int error;
2821
2822 if (!inode)
2823 return -ENOENT;
2824
Miklos Szeredia95164d2008-07-30 15:08:48 +02002825 error = may_create(dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 if (error)
2827 return error;
2828
2829 if (dir->i_sb != inode->i_sb)
2830 return -EXDEV;
2831
2832 /*
2833 * A link to an append-only or immutable file cannot be created.
2834 */
2835 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2836 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05002837 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002839 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 return -EPERM;
2841
2842 error = security_inode_link(old_dentry, dir, new_dentry);
2843 if (error)
2844 return error;
2845
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002846 mutex_lock(&inode->i_mutex);
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05302847 /* Make sure we don't allow creating hardlink to an unlinked file */
2848 if (inode->i_nlink == 0)
2849 error = -ENOENT;
2850 else
2851 error = dir->i_op->link(old_dentry, dir, new_dentry);
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002852 mutex_unlock(&inode->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07002853 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002854 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 return error;
2856}
2857
2858/*
2859 * Hardlinks are often used in delicate situations. We avoid
2860 * security-related surprises by not following symlinks on the
2861 * newname. --KAB
2862 *
2863 * We don't follow them on the oldname either to be compatible
2864 * with linux 2.0, and to avoid hard-linking to directories
2865 * and other special files. --ADM
2866 */
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002867SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
2868 int, newdfd, const char __user *, newname, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869{
2870 struct dentry *new_dentry;
Al Virodae6ad82011-06-26 11:50:15 -04002871 struct path old_path, new_path;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05302872 int how = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05302875 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002876 return -EINVAL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05302877 /*
2878 * To use null names we require CAP_DAC_READ_SEARCH
2879 * This ensures that not everyone will be able to create
2880 * handlink using the passed filedescriptor.
2881 */
2882 if (flags & AT_EMPTY_PATH) {
2883 if (!capable(CAP_DAC_READ_SEARCH))
2884 return -ENOENT;
2885 how = LOOKUP_EMPTY;
2886 }
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002887
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05302888 if (flags & AT_SYMLINK_FOLLOW)
2889 how |= LOOKUP_FOLLOW;
2890
2891 error = user_path_at(olddfd, oldname, how, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002893 return error;
2894
Al Virodae6ad82011-06-26 11:50:15 -04002895 new_dentry = user_path_create(newdfd, newname, &new_path, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002897 if (IS_ERR(new_dentry))
Al Virodae6ad82011-06-26 11:50:15 -04002898 goto out;
2899
2900 error = -EXDEV;
2901 if (old_path.mnt != new_path.mnt)
2902 goto out_dput;
2903 error = mnt_want_write(new_path.mnt);
Dave Hansen75c3f292008-02-15 14:37:45 -08002904 if (error)
2905 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04002906 error = security_path_link(old_path.dentry, &new_path, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002907 if (error)
2908 goto out_drop_write;
Al Virodae6ad82011-06-26 11:50:15 -04002909 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002910out_drop_write:
Al Virodae6ad82011-06-26 11:50:15 -04002911 mnt_drop_write(new_path.mnt);
Dave Hansen75c3f292008-02-15 14:37:45 -08002912out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002913 dput(new_dentry);
Al Virodae6ad82011-06-26 11:50:15 -04002914 mutex_unlock(&new_path.dentry->d_inode->i_mutex);
2915 path_put(&new_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916out:
Al Viro2d8f3032008-07-22 09:59:21 -04002917 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918
2919 return error;
2920}
2921
Heiko Carstens3480b252009-01-14 14:14:16 +01002922SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002923{
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002924 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002925}
2926
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927/*
2928 * The worst of all namespace operations - renaming directory. "Perverted"
2929 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2930 * Problems:
2931 * a) we can get into loop creation. Check is done in is_subdir().
2932 * b) race potential - two innocent renames can create a loop together.
2933 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002934 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 * story.
2936 * c) we have to lock _three_ objects - parents and victim (if it exists).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002937 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 * whether the target exists). Solution: try to be smart with locking
2939 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002940 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 * move will be locked. Thus we can rank directories by the tree
2942 * (ancestors first) and rank all non-directories after them.
2943 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002944 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 * HOWEVER, it relies on the assumption that any object with ->lookup()
2946 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2947 * we'd better make sure that there's no link(2) for them.
Sage Weile4eaac02011-05-24 13:06:07 -07002948 * d) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002949 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05002951 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 * locking].
2953 */
Adrian Bunk75c96f82005-05-05 16:16:09 -07002954static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
2955 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956{
2957 int error = 0;
Sage Weil9055cba2011-05-24 13:06:12 -07002958 struct inode *target = new_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959
2960 /*
2961 * If we are going to change the parent - check write permissions,
2962 * we'll need to flip '..'.
2963 */
2964 if (new_dir != old_dir) {
Al Virof419a2e2008-07-22 00:07:17 -04002965 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 if (error)
2967 return error;
2968 }
2969
2970 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2971 if (error)
2972 return error;
2973
Al Virod83c49f2010-04-30 17:17:09 -04002974 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002975 mutex_lock(&target->i_mutex);
Sage Weil9055cba2011-05-24 13:06:12 -07002976
2977 error = -EBUSY;
2978 if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
2979 goto out;
2980
Sage Weil3cebde22011-05-29 21:20:59 -07002981 if (target)
2982 shrink_dcache_parent(new_dentry);
Sage Weil9055cba2011-05-24 13:06:12 -07002983 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2984 if (error)
2985 goto out;
2986
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 if (target) {
Sage Weil9055cba2011-05-24 13:06:12 -07002988 target->i_flags |= S_DEAD;
2989 dont_mount(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 }
Sage Weil9055cba2011-05-24 13:06:12 -07002991out:
2992 if (target)
2993 mutex_unlock(&target->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07002994 if (!error)
Mark Fasheh349457c2006-09-08 14:22:21 -07002995 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
2996 d_move(old_dentry,new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 return error;
2998}
2999
Adrian Bunk75c96f82005-05-05 16:16:09 -07003000static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
3001 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002{
Sage Weil51892bb2011-05-24 13:06:13 -07003003 struct inode *target = new_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 int error;
3005
3006 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3007 if (error)
3008 return error;
3009
3010 dget(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003012 mutex_lock(&target->i_mutex);
Sage Weil51892bb2011-05-24 13:06:13 -07003013
3014 error = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
Sage Weil51892bb2011-05-24 13:06:13 -07003016 goto out;
3017
3018 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3019 if (error)
3020 goto out;
3021
3022 if (target)
3023 dont_mount(new_dentry);
3024 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3025 d_move(old_dentry, new_dentry);
3026out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003028 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 dput(new_dentry);
3030 return error;
3031}
3032
3033int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
3034 struct inode *new_dir, struct dentry *new_dentry)
3035{
3036 int error;
3037 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
Eric Paris59b0df22010-02-08 12:53:52 -05003038 const unsigned char *old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039
3040 if (old_dentry->d_inode == new_dentry->d_inode)
3041 return 0;
3042
3043 error = may_delete(old_dir, old_dentry, is_dir);
3044 if (error)
3045 return error;
3046
3047 if (!new_dentry->d_inode)
Miklos Szeredia95164d2008-07-30 15:08:48 +02003048 error = may_create(new_dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 else
3050 error = may_delete(new_dir, new_dentry, is_dir);
3051 if (error)
3052 return error;
3053
Al Viroacfa4382008-12-04 10:06:33 -05003054 if (!old_dir->i_op->rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 return -EPERM;
3056
Robert Love0eeca282005-07-12 17:06:03 -04003057 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
3058
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 if (is_dir)
3060 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
3061 else
3062 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
Al Viro123df292009-12-25 04:57:57 -05003063 if (!error)
3064 fsnotify_move(old_dir, new_dir, old_name, is_dir,
Al Viro5a190ae2007-06-07 12:19:32 -04003065 new_dentry->d_inode, old_dentry);
Robert Love0eeca282005-07-12 17:06:03 -04003066 fsnotify_oldname_free(old_name);
3067
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 return error;
3069}
3070
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003071SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
3072 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073{
Al Viro2ad94ae2008-07-21 09:32:51 -04003074 struct dentry *old_dir, *new_dir;
3075 struct dentry *old_dentry, *new_dentry;
3076 struct dentry *trap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 struct nameidata oldnd, newnd;
Al Viro2ad94ae2008-07-21 09:32:51 -04003078 char *from;
3079 char *to;
3080 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081
Al Viro2ad94ae2008-07-21 09:32:51 -04003082 error = user_path_parent(olddfd, oldname, &oldnd, &from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 if (error)
3084 goto exit;
3085
Al Viro2ad94ae2008-07-21 09:32:51 -04003086 error = user_path_parent(newdfd, newname, &newnd, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 if (error)
3088 goto exit1;
3089
3090 error = -EXDEV;
Jan Blunck4ac91372008-02-14 19:34:32 -08003091 if (oldnd.path.mnt != newnd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 goto exit2;
3093
Jan Blunck4ac91372008-02-14 19:34:32 -08003094 old_dir = oldnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 error = -EBUSY;
3096 if (oldnd.last_type != LAST_NORM)
3097 goto exit2;
3098
Jan Blunck4ac91372008-02-14 19:34:32 -08003099 new_dir = newnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 if (newnd.last_type != LAST_NORM)
3101 goto exit2;
3102
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003103 oldnd.flags &= ~LOOKUP_PARENT;
3104 newnd.flags &= ~LOOKUP_PARENT;
OGAWA Hirofumi4e9ed2f2008-10-16 07:50:29 +09003105 newnd.flags |= LOOKUP_RENAME_TARGET;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003106
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 trap = lock_rename(new_dir, old_dir);
3108
Christoph Hellwig49705b72005-11-08 21:35:06 -08003109 old_dentry = lookup_hash(&oldnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 error = PTR_ERR(old_dentry);
3111 if (IS_ERR(old_dentry))
3112 goto exit3;
3113 /* source must exist */
3114 error = -ENOENT;
3115 if (!old_dentry->d_inode)
3116 goto exit4;
3117 /* unless the source is a directory trailing slashes give -ENOTDIR */
3118 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
3119 error = -ENOTDIR;
3120 if (oldnd.last.name[oldnd.last.len])
3121 goto exit4;
3122 if (newnd.last.name[newnd.last.len])
3123 goto exit4;
3124 }
3125 /* source should not be ancestor of target */
3126 error = -EINVAL;
3127 if (old_dentry == trap)
3128 goto exit4;
Christoph Hellwig49705b72005-11-08 21:35:06 -08003129 new_dentry = lookup_hash(&newnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 error = PTR_ERR(new_dentry);
3131 if (IS_ERR(new_dentry))
3132 goto exit4;
3133 /* target should not be an ancestor of source */
3134 error = -ENOTEMPTY;
3135 if (new_dentry == trap)
3136 goto exit5;
3137
Dave Hansen9079b1e2008-02-15 14:37:49 -08003138 error = mnt_want_write(oldnd.path.mnt);
3139 if (error)
3140 goto exit5;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003141 error = security_path_rename(&oldnd.path, old_dentry,
3142 &newnd.path, new_dentry);
3143 if (error)
3144 goto exit6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 error = vfs_rename(old_dir->d_inode, old_dentry,
3146 new_dir->d_inode, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003147exit6:
Dave Hansen9079b1e2008-02-15 14:37:49 -08003148 mnt_drop_write(oldnd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149exit5:
3150 dput(new_dentry);
3151exit4:
3152 dput(old_dentry);
3153exit3:
3154 unlock_rename(new_dir, old_dir);
3155exit2:
Jan Blunck1d957f92008-02-14 19:34:35 -08003156 path_put(&newnd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04003157 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003159 path_put(&oldnd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 putname(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04003161exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 return error;
3163}
3164
Heiko Carstensa26eab22009-01-14 14:14:17 +01003165SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003166{
3167 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
3168}
3169
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
3171{
3172 int len;
3173
3174 len = PTR_ERR(link);
3175 if (IS_ERR(link))
3176 goto out;
3177
3178 len = strlen(link);
3179 if (len > (unsigned) buflen)
3180 len = buflen;
3181 if (copy_to_user(buffer, link, len))
3182 len = -EFAULT;
3183out:
3184 return len;
3185}
3186
3187/*
3188 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
3189 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
3190 * using) it for any given inode is up to filesystem.
3191 */
3192int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3193{
3194 struct nameidata nd;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003195 void *cookie;
Marcin Slusarz694a1762008-06-09 16:40:37 -07003196 int res;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003197
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 nd.depth = 0;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003199 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
Marcin Slusarz694a1762008-06-09 16:40:37 -07003200 if (IS_ERR(cookie))
3201 return PTR_ERR(cookie);
3202
3203 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
3204 if (dentry->d_inode->i_op->put_link)
3205 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3206 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207}
3208
3209int vfs_follow_link(struct nameidata *nd, const char *link)
3210{
3211 return __vfs_follow_link(nd, link);
3212}
3213
3214/* get the link contents into pagecache */
3215static char *page_getlink(struct dentry * dentry, struct page **ppage)
3216{
Duane Griffinebd09ab2008-12-19 20:47:12 +00003217 char *kaddr;
3218 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 struct address_space *mapping = dentry->d_inode->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -07003220 page = read_mapping_page(mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 if (IS_ERR(page))
Nick Piggin6fe69002007-05-06 14:49:04 -07003222 return (char*)page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 *ppage = page;
Duane Griffinebd09ab2008-12-19 20:47:12 +00003224 kaddr = kmap(page);
3225 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3226 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227}
3228
3229int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3230{
3231 struct page *page = NULL;
3232 char *s = page_getlink(dentry, &page);
3233 int res = vfs_readlink(dentry,buffer,buflen,s);
3234 if (page) {
3235 kunmap(page);
3236 page_cache_release(page);
3237 }
3238 return res;
3239}
3240
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003241void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003243 struct page *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 nd_set_link(nd, page_getlink(dentry, &page));
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003245 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246}
3247
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003248void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003250 struct page *page = cookie;
3251
3252 if (page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 kunmap(page);
3254 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 }
3256}
3257
Nick Piggin54566b22009-01-04 12:00:53 -08003258/*
3259 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
3260 */
3261int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262{
3263 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003264 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07003265 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08003266 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 char *kaddr;
Nick Piggin54566b22009-01-04 12:00:53 -08003268 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
3269 if (nofs)
3270 flags |= AOP_FLAG_NOFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271
NeilBrown7e53cac2006-03-25 03:07:57 -08003272retry:
Nick Pigginafddba42007-10-16 01:25:01 -07003273 err = pagecache_write_begin(NULL, mapping, 0, len-1,
Nick Piggin54566b22009-01-04 12:00:53 -08003274 flags, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07003276 goto fail;
3277
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 kaddr = kmap_atomic(page, KM_USER0);
3279 memcpy(kaddr, symname, len-1);
3280 kunmap_atomic(kaddr, KM_USER0);
Nick Pigginafddba42007-10-16 01:25:01 -07003281
3282 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3283 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 if (err < 0)
3285 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07003286 if (err < len-1)
3287 goto retry;
3288
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 mark_inode_dirty(inode);
3290 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291fail:
3292 return err;
3293}
3294
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003295int page_symlink(struct inode *inode, const char *symname, int len)
3296{
3297 return __page_symlink(inode, symname, len,
Nick Piggin54566b22009-01-04 12:00:53 -08003298 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003299}
3300
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003301const struct inode_operations page_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 .readlink = generic_readlink,
3303 .follow_link = page_follow_link_light,
3304 .put_link = page_put_link,
3305};
3306
Al Viro2d8f3032008-07-22 09:59:21 -04003307EXPORT_SYMBOL(user_path_at);
David Howellscc53ce52011-01-14 18:45:26 +00003308EXPORT_SYMBOL(follow_down_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309EXPORT_SYMBOL(follow_down);
3310EXPORT_SYMBOL(follow_up);
3311EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
3312EXPORT_SYMBOL(getname);
3313EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314EXPORT_SYMBOL(lookup_one_len);
3315EXPORT_SYMBOL(page_follow_link_light);
3316EXPORT_SYMBOL(page_put_link);
3317EXPORT_SYMBOL(page_readlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003318EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319EXPORT_SYMBOL(page_symlink);
3320EXPORT_SYMBOL(page_symlink_inode_operations);
Al Virod1811462008-08-02 00:49:18 -04003321EXPORT_SYMBOL(kern_path);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07003322EXPORT_SYMBOL(vfs_path_lookup);
Al Virof419a2e2008-07-22 00:07:17 -04003323EXPORT_SYMBOL(inode_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324EXPORT_SYMBOL(unlock_rename);
3325EXPORT_SYMBOL(vfs_create);
3326EXPORT_SYMBOL(vfs_follow_link);
3327EXPORT_SYMBOL(vfs_link);
3328EXPORT_SYMBOL(vfs_mkdir);
3329EXPORT_SYMBOL(vfs_mknod);
3330EXPORT_SYMBOL(generic_permission);
3331EXPORT_SYMBOL(vfs_readlink);
3332EXPORT_SYMBOL(vfs_rename);
3333EXPORT_SYMBOL(vfs_rmdir);
3334EXPORT_SYMBOL(vfs_symlink);
3335EXPORT_SYMBOL(vfs_unlink);
3336EXPORT_SYMBOL(dentry_unhash);
3337EXPORT_SYMBOL(generic_readlink);