blob: 445fd5da11fa9f366458247c842d68dacb147c87 [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 Blunck5dd784d2008-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 Howells5a30d8a2011-07-11 14:20:57 +0100719 /*
David Howells9875cf82011-01-14 18:45:21 +0000720 * We don't want to mount if someone's just doing a stat and they've
721 * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and
722 * appended a '/' to the name.
723 */
David Howells5a30d8a2011-07-11 14:20:57 +0100724 if (!(flags & LOOKUP_FOLLOW)) {
725 /* We do, however, want to mount if someone wants to open or
726 * create a file of any type under the mountpoint, wants to
727 * traverse through the mountpoint or wants to open the mounted
728 * directory.
729 * Also, autofs may mark negative dentries as being automount
730 * points. These will need the attentions of the daemon to
731 * instantiate them before they can be used.
732 */
733 if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
734 LOOKUP_OPEN | LOOKUP_CREATE)) &&
735 path->dentry->d_inode)
736 return -EISDIR;
737 }
David Howells9875cf82011-01-14 18:45:21 +0000738 current->total_link_count++;
739 if (current->total_link_count >= 40)
740 return -ELOOP;
741
742 mnt = path->dentry->d_op->d_automount(path);
743 if (IS_ERR(mnt)) {
744 /*
745 * The filesystem is allowed to return -EISDIR here to indicate
746 * it doesn't want to automount. For instance, autofs would do
747 * this so that its userspace daemon can mount on this dentry.
748 *
749 * However, we can only permit this if it's a terminal point in
750 * the path being looked up; if it wasn't then the remainder of
751 * the path is inaccessible and we should say so.
752 */
Al Viro49084c32011-06-25 21:59:52 -0400753 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
David Howells9875cf82011-01-14 18:45:21 +0000754 return -EREMOTE;
755 return PTR_ERR(mnt);
756 }
David Howellsea5b7782011-01-14 19:10:03 +0000757
David Howells9875cf82011-01-14 18:45:21 +0000758 if (!mnt) /* mount collision */
759 return 0;
760
Al Viro8aef1882011-06-16 15:10:06 +0100761 if (!*need_mntput) {
762 /* lock_mount() may release path->mnt on error */
763 mntget(path->mnt);
764 *need_mntput = true;
765 }
Al Viro19a167a2011-01-17 01:35:23 -0500766 err = finish_automount(mnt, path);
David Howellsea5b7782011-01-14 19:10:03 +0000767
David Howellsea5b7782011-01-14 19:10:03 +0000768 switch (err) {
769 case -EBUSY:
770 /* Someone else made a mount here whilst we were busy */
Al Viro19a167a2011-01-17 01:35:23 -0500771 return 0;
David Howellsea5b7782011-01-14 19:10:03 +0000772 case 0:
Al Viro8aef1882011-06-16 15:10:06 +0100773 path_put(path);
David Howellsea5b7782011-01-14 19:10:03 +0000774 path->mnt = mnt;
775 path->dentry = dget(mnt->mnt_root);
David Howellsea5b7782011-01-14 19:10:03 +0000776 return 0;
Al Viro19a167a2011-01-17 01:35:23 -0500777 default:
778 return err;
David Howellsea5b7782011-01-14 19:10:03 +0000779 }
Al Viro19a167a2011-01-17 01:35:23 -0500780
David Howells9875cf82011-01-14 18:45:21 +0000781}
782
783/*
784 * Handle a dentry that is managed in some way.
David Howellscc53ce52011-01-14 18:45:26 +0000785 * - Flagged for transit management (autofs)
David Howells9875cf82011-01-14 18:45:21 +0000786 * - Flagged as mountpoint
787 * - Flagged as automount point
788 *
789 * This may only be called in refwalk mode.
790 *
791 * Serialization is taken care of in namespace.c
792 */
793static int follow_managed(struct path *path, unsigned flags)
794{
Al Viro8aef1882011-06-16 15:10:06 +0100795 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
David Howells9875cf82011-01-14 18:45:21 +0000796 unsigned managed;
797 bool need_mntput = false;
Al Viro8aef1882011-06-16 15:10:06 +0100798 int ret = 0;
David Howells9875cf82011-01-14 18:45:21 +0000799
800 /* Given that we're not holding a lock here, we retain the value in a
801 * local variable for each dentry as we look at it so that we don't see
802 * the components of that value change under us */
803 while (managed = ACCESS_ONCE(path->dentry->d_flags),
804 managed &= DCACHE_MANAGED_DENTRY,
805 unlikely(managed != 0)) {
David Howellscc53ce52011-01-14 18:45:26 +0000806 /* Allow the filesystem to manage the transit without i_mutex
807 * being held. */
808 if (managed & DCACHE_MANAGE_TRANSIT) {
809 BUG_ON(!path->dentry->d_op);
810 BUG_ON(!path->dentry->d_op->d_manage);
Al Viro1aed3e42011-03-18 09:09:02 -0400811 ret = path->dentry->d_op->d_manage(path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +0000812 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +0100813 break;
David Howellscc53ce52011-01-14 18:45:26 +0000814 }
815
David Howells9875cf82011-01-14 18:45:21 +0000816 /* Transit to a mounted filesystem. */
817 if (managed & DCACHE_MOUNTED) {
818 struct vfsmount *mounted = lookup_mnt(path);
819 if (mounted) {
820 dput(path->dentry);
821 if (need_mntput)
822 mntput(path->mnt);
823 path->mnt = mounted;
824 path->dentry = dget(mounted->mnt_root);
825 need_mntput = true;
826 continue;
827 }
828
829 /* Something is mounted on this dentry in another
830 * namespace and/or whatever was mounted there in this
831 * namespace got unmounted before we managed to get the
832 * vfsmount_lock */
833 }
834
835 /* Handle an automount point */
836 if (managed & DCACHE_NEED_AUTOMOUNT) {
837 ret = follow_automount(path, flags, &need_mntput);
838 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +0100839 break;
David Howells9875cf82011-01-14 18:45:21 +0000840 continue;
841 }
842
843 /* We didn't change the current path point */
844 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 }
Al Viro8aef1882011-06-16 15:10:06 +0100846
847 if (need_mntput && path->mnt == mnt)
848 mntput(path->mnt);
849 if (ret == -EISDIR)
850 ret = 0;
851 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853
David Howellscc53ce52011-01-14 18:45:26 +0000854int follow_down_one(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
856 struct vfsmount *mounted;
857
Al Viro1c755af2009-04-18 14:06:57 -0400858 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -0400860 dput(path->dentry);
861 mntput(path->mnt);
862 path->mnt = mounted;
863 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 return 1;
865 }
866 return 0;
867}
868
Ian Kent62a73752011-03-25 01:51:02 +0800869static inline bool managed_dentry_might_block(struct dentry *dentry)
870{
871 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
872 dentry->d_op->d_manage(dentry, true) < 0);
873}
874
David Howells9875cf82011-01-14 18:45:21 +0000875/*
Al Viro287548e2011-05-27 06:50:06 -0400876 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
877 * we meet a managed dentry that would need blocking.
David Howells9875cf82011-01-14 18:45:21 +0000878 */
879static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
Al Viro287548e2011-05-27 06:50:06 -0400880 struct inode **inode)
David Howells9875cf82011-01-14 18:45:21 +0000881{
Ian Kent62a73752011-03-25 01:51:02 +0800882 for (;;) {
David Howells9875cf82011-01-14 18:45:21 +0000883 struct vfsmount *mounted;
Ian Kent62a73752011-03-25 01:51:02 +0800884 /*
885 * Don't forget we might have a non-mountpoint managed dentry
886 * that wants to block transit.
887 */
Al Viro287548e2011-05-27 06:50:06 -0400888 if (unlikely(managed_dentry_might_block(path->dentry)))
David Howellsab909112011-01-14 18:46:51 +0000889 return false;
Ian Kent62a73752011-03-25 01:51:02 +0800890
891 if (!d_mountpoint(path->dentry))
892 break;
893
David Howells9875cf82011-01-14 18:45:21 +0000894 mounted = __lookup_mnt(path->mnt, path->dentry, 1);
895 if (!mounted)
896 break;
897 path->mnt = mounted;
898 path->dentry = mounted->mnt_root;
899 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
Linus Torvalds59430262011-07-18 15:43:29 -0700900 /*
901 * Update the inode too. We don't need to re-check the
902 * dentry sequence number here after this d_inode read,
903 * because a mount-point is always pinned.
904 */
905 *inode = path->dentry->d_inode;
David Howells9875cf82011-01-14 18:45:21 +0000906 }
David Howells9875cf82011-01-14 18:45:21 +0000907 return true;
908}
909
Al Virodea39372011-05-27 06:53:39 -0400910static void follow_mount_rcu(struct nameidata *nd)
Al Viro287548e2011-05-27 06:50:06 -0400911{
Al Virodea39372011-05-27 06:53:39 -0400912 while (d_mountpoint(nd->path.dentry)) {
Al Viro287548e2011-05-27 06:50:06 -0400913 struct vfsmount *mounted;
Al Virodea39372011-05-27 06:53:39 -0400914 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
Al Viro287548e2011-05-27 06:50:06 -0400915 if (!mounted)
916 break;
Al Virodea39372011-05-27 06:53:39 -0400917 nd->path.mnt = mounted;
918 nd->path.dentry = mounted->mnt_root;
919 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro287548e2011-05-27 06:50:06 -0400920 }
921}
922
Nick Piggin31e6b012011-01-07 17:49:52 +1100923static int follow_dotdot_rcu(struct nameidata *nd)
924{
Nick Piggin31e6b012011-01-07 17:49:52 +1100925 set_root_rcu(nd);
926
David Howells9875cf82011-01-14 18:45:21 +0000927 while (1) {
Nick Piggin31e6b012011-01-07 17:49:52 +1100928 if (nd->path.dentry == nd->root.dentry &&
929 nd->path.mnt == nd->root.mnt) {
930 break;
931 }
932 if (nd->path.dentry != nd->path.mnt->mnt_root) {
933 struct dentry *old = nd->path.dentry;
934 struct dentry *parent = old->d_parent;
935 unsigned seq;
936
937 seq = read_seqcount_begin(&parent->d_seq);
938 if (read_seqcount_retry(&old->d_seq, nd->seq))
Al Viroef7562d2011-03-04 14:35:59 -0500939 goto failed;
Nick Piggin31e6b012011-01-07 17:49:52 +1100940 nd->path.dentry = parent;
941 nd->seq = seq;
942 break;
943 }
944 if (!follow_up_rcu(&nd->path))
945 break;
946 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Nick Piggin31e6b012011-01-07 17:49:52 +1100947 }
Al Virodea39372011-05-27 06:53:39 -0400948 follow_mount_rcu(nd);
949 nd->inode = nd->path.dentry->d_inode;
Nick Piggin31e6b012011-01-07 17:49:52 +1100950 return 0;
Al Viroef7562d2011-03-04 14:35:59 -0500951
952failed:
953 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -0500954 if (!(nd->flags & LOOKUP_ROOT))
955 nd->root.mnt = NULL;
Al Viroef7562d2011-03-04 14:35:59 -0500956 rcu_read_unlock();
957 br_read_unlock(vfsmount_lock);
958 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +1100959}
960
David Howells9875cf82011-01-14 18:45:21 +0000961/*
David Howellscc53ce52011-01-14 18:45:26 +0000962 * Follow down to the covering mount currently visible to userspace. At each
963 * point, the filesystem owning that dentry may be queried as to whether the
964 * caller is permitted to proceed or not.
David Howellscc53ce52011-01-14 18:45:26 +0000965 */
Al Viro7cc90cc2011-03-18 09:04:20 -0400966int follow_down(struct path *path)
David Howellscc53ce52011-01-14 18:45:26 +0000967{
968 unsigned managed;
969 int ret;
970
971 while (managed = ACCESS_ONCE(path->dentry->d_flags),
972 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
973 /* Allow the filesystem to manage the transit without i_mutex
974 * being held.
975 *
976 * We indicate to the filesystem if someone is trying to mount
977 * something here. This gives autofs the chance to deny anyone
978 * other than its daemon the right to mount on its
979 * superstructure.
980 *
981 * The filesystem may sleep at this point.
982 */
983 if (managed & DCACHE_MANAGE_TRANSIT) {
984 BUG_ON(!path->dentry->d_op);
985 BUG_ON(!path->dentry->d_op->d_manage);
David Howellsab909112011-01-14 18:46:51 +0000986 ret = path->dentry->d_op->d_manage(
Al Viro1aed3e42011-03-18 09:09:02 -0400987 path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +0000988 if (ret < 0)
989 return ret == -EISDIR ? 0 : ret;
990 }
991
992 /* Transit to a mounted filesystem. */
993 if (managed & DCACHE_MOUNTED) {
994 struct vfsmount *mounted = lookup_mnt(path);
995 if (!mounted)
996 break;
997 dput(path->dentry);
998 mntput(path->mnt);
999 path->mnt = mounted;
1000 path->dentry = dget(mounted->mnt_root);
1001 continue;
1002 }
1003
1004 /* Don't handle automount points here */
1005 break;
1006 }
1007 return 0;
1008}
1009
1010/*
David Howells9875cf82011-01-14 18:45:21 +00001011 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1012 */
1013static void follow_mount(struct path *path)
1014{
1015 while (d_mountpoint(path->dentry)) {
1016 struct vfsmount *mounted = lookup_mnt(path);
1017 if (!mounted)
1018 break;
1019 dput(path->dentry);
1020 mntput(path->mnt);
1021 path->mnt = mounted;
1022 path->dentry = dget(mounted->mnt_root);
1023 }
1024}
1025
Nick Piggin31e6b012011-01-07 17:49:52 +11001026static void follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
Al Viro2a737872009-04-07 11:49:53 -04001028 set_root(nd);
Andreas Mohre518ddb2006-09-29 02:01:22 -07001029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 while(1) {
Jan Blunck4ac91372008-02-14 19:34:32 -08001031 struct dentry *old = nd->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Al Viro2a737872009-04-07 11:49:53 -04001033 if (nd->path.dentry == nd->root.dentry &&
1034 nd->path.mnt == nd->root.mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 break;
1036 }
Jan Blunck4ac91372008-02-14 19:34:32 -08001037 if (nd->path.dentry != nd->path.mnt->mnt_root) {
Al Viro3088dd72010-01-30 15:47:29 -05001038 /* rare case of legitimate dget_parent()... */
1039 nd->path.dentry = dget_parent(nd->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 dput(old);
1041 break;
1042 }
Al Viro3088dd72010-01-30 15:47:29 -05001043 if (!follow_up(&nd->path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 }
Al Viro79ed0222009-04-18 13:59:41 -04001046 follow_mount(&nd->path);
Nick Piggin31e6b012011-01-07 17:49:52 +11001047 nd->inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048}
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050/*
Nick Pigginbaa03892010-08-18 04:37:31 +10001051 * Allocate a dentry with name and parent, and perform a parent
1052 * directory ->lookup on it. Returns the new dentry, or ERR_PTR
1053 * on error. parent->d_inode->i_mutex must be held. d_lookup must
1054 * have verified that no child exists while under i_mutex.
1055 */
1056static struct dentry *d_alloc_and_lookup(struct dentry *parent,
1057 struct qstr *name, struct nameidata *nd)
1058{
1059 struct inode *inode = parent->d_inode;
1060 struct dentry *dentry;
1061 struct dentry *old;
1062
1063 /* Don't create child dentry for a dead directory. */
1064 if (unlikely(IS_DEADDIR(inode)))
1065 return ERR_PTR(-ENOENT);
1066
1067 dentry = d_alloc(parent, name);
1068 if (unlikely(!dentry))
1069 return ERR_PTR(-ENOMEM);
1070
1071 old = inode->i_op->lookup(inode, dentry, nd);
1072 if (unlikely(old)) {
1073 dput(dentry);
1074 dentry = old;
1075 }
1076 return dentry;
1077}
1078
1079/*
Josef Bacik44396f42011-05-31 11:58:49 -04001080 * We already have a dentry, but require a lookup to be performed on the parent
1081 * directory to fill in d_inode. Returns the new dentry, or ERR_PTR on error.
1082 * parent->d_inode->i_mutex must be held. d_lookup must have verified that no
1083 * child exists while under i_mutex.
1084 */
1085static struct dentry *d_inode_lookup(struct dentry *parent, struct dentry *dentry,
1086 struct nameidata *nd)
1087{
1088 struct inode *inode = parent->d_inode;
1089 struct dentry *old;
1090
1091 /* Don't create child dentry for a dead directory. */
1092 if (unlikely(IS_DEADDIR(inode)))
1093 return ERR_PTR(-ENOENT);
1094
1095 old = inode->i_op->lookup(inode, dentry, nd);
1096 if (unlikely(old)) {
1097 dput(dentry);
1098 dentry = old;
1099 }
1100 return dentry;
1101}
1102
1103/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 * It's more convoluted than I'd like it to be, but... it's still fairly
1105 * small and for now I'd prefer to have fast path as straight as possible.
1106 * It _is_ time-critical.
1107 */
1108static int do_lookup(struct nameidata *nd, struct qstr *name,
Nick Piggin31e6b012011-01-07 17:49:52 +11001109 struct path *path, struct inode **inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
Jan Blunck4ac91372008-02-14 19:34:32 -08001111 struct vfsmount *mnt = nd->path.mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +11001112 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro5a18fff2011-03-11 04:44:53 -05001113 int need_reval = 1;
1114 int status = 1;
David Howells9875cf82011-01-14 18:45:21 +00001115 int err;
1116
Al Viro3cac2602009-08-13 18:27:43 +04001117 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001118 * Rename seqlock is not required here because in the off chance
1119 * of a false negative due to a concurrent rename, we're going to
1120 * do the non-racy lookup, below.
1121 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001122 if (nd->flags & LOOKUP_RCU) {
1123 unsigned seq;
Nick Piggin31e6b012011-01-07 17:49:52 +11001124 *inode = nd->inode;
1125 dentry = __d_lookup_rcu(parent, name, &seq, inode);
Al Viro5a18fff2011-03-11 04:44:53 -05001126 if (!dentry)
1127 goto unlazy;
1128
Nick Piggin31e6b012011-01-07 17:49:52 +11001129 /* Memory barrier in read_seqcount_begin of child is enough */
1130 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1131 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001132 nd->seq = seq;
Al Viro5a18fff2011-03-11 04:44:53 -05001133
Al Viro24643082011-02-15 01:26:22 -05001134 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
Al Viro5a18fff2011-03-11 04:44:53 -05001135 status = d_revalidate(dentry, nd);
1136 if (unlikely(status <= 0)) {
1137 if (status != -ECHILD)
1138 need_reval = 0;
1139 goto unlazy;
1140 }
Al Viro24643082011-02-15 01:26:22 -05001141 }
Josef Bacik44396f42011-05-31 11:58:49 -04001142 if (unlikely(d_need_lookup(dentry)))
1143 goto unlazy;
Nick Piggin31e6b012011-01-07 17:49:52 +11001144 path->mnt = mnt;
1145 path->dentry = dentry;
Al Virod6e9bd22011-05-27 07:03:15 -04001146 if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1147 goto unlazy;
1148 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1149 goto unlazy;
1150 return 0;
Al Viro5a18fff2011-03-11 04:44:53 -05001151unlazy:
Al Viro19660af2011-03-25 10:32:48 -04001152 if (unlazy_walk(nd, dentry))
1153 return -ECHILD;
Al Viro5a18fff2011-03-11 04:44:53 -05001154 } else {
1155 dentry = __d_lookup(parent, name);
Nick Piggin31e6b012011-01-07 17:49:52 +11001156 }
Al Viro5a18fff2011-03-11 04:44:53 -05001157
Josef Bacik44396f42011-05-31 11:58:49 -04001158 if (dentry && unlikely(d_need_lookup(dentry))) {
1159 dput(dentry);
1160 dentry = NULL;
1161 }
Al Viro5a18fff2011-03-11 04:44:53 -05001162retry:
1163 if (unlikely(!dentry)) {
1164 struct inode *dir = parent->d_inode;
1165 BUG_ON(nd->inode != dir);
1166
1167 mutex_lock(&dir->i_mutex);
1168 dentry = d_lookup(parent, name);
1169 if (likely(!dentry)) {
1170 dentry = d_alloc_and_lookup(parent, name, nd);
1171 if (IS_ERR(dentry)) {
1172 mutex_unlock(&dir->i_mutex);
1173 return PTR_ERR(dentry);
1174 }
1175 /* known good */
1176 need_reval = 0;
1177 status = 1;
Josef Bacik44396f42011-05-31 11:58:49 -04001178 } else if (unlikely(d_need_lookup(dentry))) {
1179 dentry = d_inode_lookup(parent, dentry, nd);
1180 if (IS_ERR(dentry)) {
1181 mutex_unlock(&dir->i_mutex);
1182 return PTR_ERR(dentry);
1183 }
1184 /* known good */
1185 need_reval = 0;
1186 status = 1;
Al Viro5a18fff2011-03-11 04:44:53 -05001187 }
1188 mutex_unlock(&dir->i_mutex);
Al Viro24643082011-02-15 01:26:22 -05001189 }
Al Viro5a18fff2011-03-11 04:44:53 -05001190 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
1191 status = d_revalidate(dentry, nd);
1192 if (unlikely(status <= 0)) {
1193 if (status < 0) {
1194 dput(dentry);
1195 return status;
1196 }
1197 if (!d_invalidate(dentry)) {
1198 dput(dentry);
1199 dentry = NULL;
1200 need_reval = 1;
1201 goto retry;
1202 }
1203 }
1204
David Howells9875cf82011-01-14 18:45:21 +00001205 path->mnt = mnt;
1206 path->dentry = dentry;
1207 err = follow_managed(path, nd->flags);
Ian Kent89312212011-01-18 12:06:10 +08001208 if (unlikely(err < 0)) {
1209 path_put_conditional(path, nd);
David Howells9875cf82011-01-14 18:45:21 +00001210 return err;
Ian Kent89312212011-01-18 12:06:10 +08001211 }
David Howells9875cf82011-01-14 18:45:21 +00001212 *inode = path->dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214}
1215
Al Viro52094c82011-02-21 21:34:47 -05001216static inline int may_lookup(struct nameidata *nd)
1217{
1218 if (nd->flags & LOOKUP_RCU) {
Al Viro4ad5abb2011-06-20 19:57:03 -04001219 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
Al Viro52094c82011-02-21 21:34:47 -05001220 if (err != -ECHILD)
1221 return err;
Al Viro19660af2011-03-25 10:32:48 -04001222 if (unlazy_walk(nd, NULL))
Al Viro52094c82011-02-21 21:34:47 -05001223 return -ECHILD;
1224 }
Al Viro4ad5abb2011-06-20 19:57:03 -04001225 return inode_permission(nd->inode, MAY_EXEC);
Al Viro52094c82011-02-21 21:34:47 -05001226}
1227
Al Viro9856fa12011-03-04 14:22:06 -05001228static inline int handle_dots(struct nameidata *nd, int type)
1229{
1230 if (type == LAST_DOTDOT) {
1231 if (nd->flags & LOOKUP_RCU) {
1232 if (follow_dotdot_rcu(nd))
1233 return -ECHILD;
1234 } else
1235 follow_dotdot(nd);
1236 }
1237 return 0;
1238}
1239
Al Viro951361f2011-03-04 14:44:37 -05001240static void terminate_walk(struct nameidata *nd)
1241{
1242 if (!(nd->flags & LOOKUP_RCU)) {
1243 path_put(&nd->path);
1244 } else {
1245 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001246 if (!(nd->flags & LOOKUP_ROOT))
1247 nd->root.mnt = NULL;
Al Viro951361f2011-03-04 14:44:37 -05001248 rcu_read_unlock();
1249 br_read_unlock(vfsmount_lock);
1250 }
1251}
1252
Al Viroce57dfc2011-03-13 19:58:58 -04001253static inline int walk_component(struct nameidata *nd, struct path *path,
1254 struct qstr *name, int type, int follow)
1255{
1256 struct inode *inode;
1257 int err;
1258 /*
1259 * "." and ".." are special - ".." especially so because it has
1260 * to be able to know about the current root directory and
1261 * parent relationships.
1262 */
1263 if (unlikely(type != LAST_NORM))
1264 return handle_dots(nd, type);
1265 err = do_lookup(nd, name, path, &inode);
1266 if (unlikely(err)) {
1267 terminate_walk(nd);
1268 return err;
1269 }
1270 if (!inode) {
1271 path_to_nameidata(path, nd);
1272 terminate_walk(nd);
1273 return -ENOENT;
1274 }
1275 if (unlikely(inode->i_op->follow_link) && follow) {
Al Viro19660af2011-03-25 10:32:48 -04001276 if (nd->flags & LOOKUP_RCU) {
1277 if (unlikely(unlazy_walk(nd, path->dentry))) {
1278 terminate_walk(nd);
1279 return -ECHILD;
1280 }
1281 }
Al Viroce57dfc2011-03-13 19:58:58 -04001282 BUG_ON(inode != path->dentry->d_inode);
1283 return 1;
1284 }
1285 path_to_nameidata(path, nd);
1286 nd->inode = inode;
1287 return 0;
1288}
1289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290/*
Al Virob3563792011-03-14 21:54:55 -04001291 * This limits recursive symlink follows to 8, while
1292 * limiting consecutive symlinks to 40.
1293 *
1294 * Without that kind of total limit, nasty chains of consecutive
1295 * symlinks can cause almost arbitrarily long lookups.
1296 */
1297static inline int nested_symlink(struct path *path, struct nameidata *nd)
1298{
1299 int res;
1300
Al Virob3563792011-03-14 21:54:55 -04001301 if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1302 path_put_conditional(path, nd);
1303 path_put(&nd->path);
1304 return -ELOOP;
1305 }
Erez Zadok1a4022f2011-05-21 01:19:59 -04001306 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
Al Virob3563792011-03-14 21:54:55 -04001307
1308 nd->depth++;
1309 current->link_count++;
1310
1311 do {
1312 struct path link = *path;
1313 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04001314
1315 res = follow_link(&link, nd, &cookie);
Al Virob3563792011-03-14 21:54:55 -04001316 if (!res)
1317 res = walk_component(nd, path, &nd->last,
1318 nd->last_type, LOOKUP_FOLLOW);
Al Viro574197e2011-03-14 22:20:34 -04001319 put_link(nd, &link, cookie);
Al Virob3563792011-03-14 21:54:55 -04001320 } while (res > 0);
1321
1322 current->link_count--;
1323 nd->depth--;
1324 return res;
1325}
1326
1327/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01001329 * This is the basic name resolution function, turning a pathname into
1330 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01001332 * Returns 0 and nd will have valid dentry and mnt on success.
1333 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 */
Al Viro6de88d72009-08-09 01:41:57 +04001335static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
1337 struct path next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
1340 while (*name=='/')
1341 name++;
1342 if (!*name)
Al Viro086e1832011-02-22 20:56:27 -05001343 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 /* At this point we know we have a real path component. */
1346 for(;;) {
1347 unsigned long hash;
1348 struct qstr this;
1349 unsigned int c;
Al Virofe479a52011-02-22 15:10:03 -05001350 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Al Viro52094c82011-02-21 21:34:47 -05001352 err = may_lookup(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 if (err)
1354 break;
1355
1356 this.name = name;
1357 c = *(const unsigned char *)name;
1358
1359 hash = init_name_hash();
1360 do {
1361 name++;
1362 hash = partial_name_hash(c, hash);
1363 c = *(const unsigned char *)name;
1364 } while (c && (c != '/'));
1365 this.len = name - (const char *) this.name;
1366 this.hash = end_name_hash(hash);
1367
Al Virofe479a52011-02-22 15:10:03 -05001368 type = LAST_NORM;
1369 if (this.name[0] == '.') switch (this.len) {
1370 case 2:
Al Viro16c2cd72011-02-22 15:50:10 -05001371 if (this.name[1] == '.') {
Al Virofe479a52011-02-22 15:10:03 -05001372 type = LAST_DOTDOT;
Al Viro16c2cd72011-02-22 15:50:10 -05001373 nd->flags |= LOOKUP_JUMPED;
1374 }
Al Virofe479a52011-02-22 15:10:03 -05001375 break;
1376 case 1:
1377 type = LAST_DOT;
1378 }
Al Viro5a202bc2011-03-08 14:17:44 -05001379 if (likely(type == LAST_NORM)) {
1380 struct dentry *parent = nd->path.dentry;
Al Viro16c2cd72011-02-22 15:50:10 -05001381 nd->flags &= ~LOOKUP_JUMPED;
Al Viro5a202bc2011-03-08 14:17:44 -05001382 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1383 err = parent->d_op->d_hash(parent, nd->inode,
1384 &this);
1385 if (err < 0)
1386 break;
1387 }
1388 }
Al Virofe479a52011-02-22 15:10:03 -05001389
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 /* remove trailing slashes? */
1391 if (!c)
1392 goto last_component;
1393 while (*++name == '/');
1394 if (!*name)
Al Virob3563792011-03-14 21:54:55 -04001395 goto last_component;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Al Viroce57dfc2011-03-13 19:58:58 -04001397 err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1398 if (err < 0)
1399 return err;
Al Virofe479a52011-02-22 15:10:03 -05001400
Al Viroce57dfc2011-03-13 19:58:58 -04001401 if (err) {
Al Virob3563792011-03-14 21:54:55 -04001402 err = nested_symlink(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 if (err)
Al Viroa7472ba2011-03-04 14:39:30 -05001404 return err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 err = -ENOTDIR;
Nick Piggin31e6b012011-01-07 17:49:52 +11001407 if (!nd->inode->i_op->lookup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 break;
1409 continue;
1410 /* here ends the main loop */
1411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412last_component:
Al Virob3563792011-03-14 21:54:55 -04001413 nd->last = this;
1414 nd->last_type = type;
Al Viro086e1832011-02-22 20:56:27 -05001415 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 }
Al Viro951361f2011-03-04 14:44:37 -05001417 terminate_walk(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 return err;
1419}
1420
Al Viro70e9b352011-03-05 21:12:22 -05001421static int path_init(int dfd, const char *name, unsigned int flags,
1422 struct nameidata *nd, struct file **fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423{
Prasanna Medaea3834d2005-04-29 16:00:17 +01001424 int retval = 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001425 int fput_needed;
1426 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
1428 nd->last_type = LAST_ROOT; /* if there are only slashes... */
Al Viro16c2cd72011-02-22 15:50:10 -05001429 nd->flags = flags | LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 nd->depth = 0;
Al Viro5b6ca022011-03-09 23:04:47 -05001431 if (flags & LOOKUP_ROOT) {
1432 struct inode *inode = nd->root.dentry->d_inode;
Al Viro73d049a2011-03-11 12:08:24 -05001433 if (*name) {
1434 if (!inode->i_op->lookup)
1435 return -ENOTDIR;
1436 retval = inode_permission(inode, MAY_EXEC);
1437 if (retval)
1438 return retval;
1439 }
Al Viro5b6ca022011-03-09 23:04:47 -05001440 nd->path = nd->root;
1441 nd->inode = inode;
1442 if (flags & LOOKUP_RCU) {
1443 br_read_lock(vfsmount_lock);
1444 rcu_read_lock();
1445 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1446 } else {
1447 path_get(&nd->path);
1448 }
1449 return 0;
1450 }
1451
Al Viro2a737872009-04-07 11:49:53 -04001452 nd->root.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 if (*name=='/') {
Al Viroe41f7d42011-02-22 14:02:58 -05001455 if (flags & LOOKUP_RCU) {
1456 br_read_lock(vfsmount_lock);
1457 rcu_read_lock();
1458 set_root_rcu(nd);
1459 } else {
1460 set_root(nd);
1461 path_get(&nd->root);
1462 }
Al Viro2a737872009-04-07 11:49:53 -04001463 nd->path = nd->root;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001464 } else if (dfd == AT_FDCWD) {
Al Viroe41f7d42011-02-22 14:02:58 -05001465 if (flags & LOOKUP_RCU) {
1466 struct fs_struct *fs = current->fs;
1467 unsigned seq;
1468
1469 br_read_lock(vfsmount_lock);
1470 rcu_read_lock();
1471
1472 do {
1473 seq = read_seqcount_begin(&fs->seq);
1474 nd->path = fs->pwd;
1475 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1476 } while (read_seqcount_retry(&fs->seq, seq));
1477 } else {
1478 get_fs_pwd(current->fs, &nd->path);
1479 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001480 } else {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001481 struct dentry *dentry;
1482
Al Viro1abf0c72011-03-13 03:51:11 -04001483 file = fget_raw_light(dfd, &fput_needed);
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001484 retval = -EBADF;
1485 if (!file)
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001486 goto out_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001487
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001488 dentry = file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001489
Al Virof52e0c12011-03-14 18:56:51 -04001490 if (*name) {
1491 retval = -ENOTDIR;
1492 if (!S_ISDIR(dentry->d_inode->i_mode))
1493 goto fput_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001494
Al Viro4ad5abb2011-06-20 19:57:03 -04001495 retval = inode_permission(dentry->d_inode, MAY_EXEC);
Al Virof52e0c12011-03-14 18:56:51 -04001496 if (retval)
1497 goto fput_fail;
1498 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001499
Jan Blunck5dd784d2008-02-14 19:34:38 -08001500 nd->path = file->f_path;
Al Viroe41f7d42011-02-22 14:02:58 -05001501 if (flags & LOOKUP_RCU) {
1502 if (fput_needed)
Al Viro70e9b352011-03-05 21:12:22 -05001503 *fp = file;
Al Viroe41f7d42011-02-22 14:02:58 -05001504 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1505 br_read_lock(vfsmount_lock);
1506 rcu_read_lock();
1507 } else {
1508 path_get(&file->f_path);
1509 fput_light(file, fput_needed);
1510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 }
Al Viroe41f7d42011-02-22 14:02:58 -05001512
Nick Piggin31e6b012011-01-07 17:49:52 +11001513 nd->inode = nd->path.dentry->d_inode;
Al Viro9b4a9b12009-04-07 11:44:16 -04001514 return 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001515
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001516fput_fail:
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001517 fput_light(file, fput_needed);
Al Viro9b4a9b12009-04-07 11:44:16 -04001518out_fail:
1519 return retval;
1520}
1521
Al Virobd92d7f2011-03-14 19:54:59 -04001522static inline int lookup_last(struct nameidata *nd, struct path *path)
1523{
1524 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1525 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1526
1527 nd->flags &= ~LOOKUP_PARENT;
1528 return walk_component(nd, path, &nd->last, nd->last_type,
1529 nd->flags & LOOKUP_FOLLOW);
1530}
1531
Al Viro9b4a9b12009-04-07 11:44:16 -04001532/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroee0827c2011-02-21 23:38:09 -05001533static int path_lookupat(int dfd, const char *name,
Al Viro9b4a9b12009-04-07 11:44:16 -04001534 unsigned int flags, struct nameidata *nd)
1535{
Al Viro70e9b352011-03-05 21:12:22 -05001536 struct file *base = NULL;
Al Virobd92d7f2011-03-14 19:54:59 -04001537 struct path path;
1538 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001539
1540 /*
1541 * Path walking is largely split up into 2 different synchronisation
1542 * schemes, rcu-walk and ref-walk (explained in
1543 * Documentation/filesystems/path-lookup.txt). These share much of the
1544 * path walk code, but some things particularly setup, cleanup, and
1545 * following mounts are sufficiently divergent that functions are
1546 * duplicated. Typically there is a function foo(), and its RCU
1547 * analogue, foo_rcu().
1548 *
1549 * -ECHILD is the error number of choice (just to avoid clashes) that
1550 * is returned if some aspect of an rcu-walk fails. Such an error must
1551 * be handled by restarting a traditional ref-walk (which will always
1552 * be able to complete).
1553 */
Al Virobd92d7f2011-03-14 19:54:59 -04001554 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
Al Viroee0827c2011-02-21 23:38:09 -05001555
Al Virobd92d7f2011-03-14 19:54:59 -04001556 if (unlikely(err))
1557 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001558
1559 current->total_link_count = 0;
Al Virobd92d7f2011-03-14 19:54:59 -04001560 err = link_path_walk(name, nd);
1561
1562 if (!err && !(flags & LOOKUP_PARENT)) {
Al Virobd92d7f2011-03-14 19:54:59 -04001563 err = lookup_last(nd, &path);
1564 while (err > 0) {
1565 void *cookie;
1566 struct path link = path;
Al Virobd92d7f2011-03-14 19:54:59 -04001567 nd->flags |= LOOKUP_PARENT;
Al Viro574197e2011-03-14 22:20:34 -04001568 err = follow_link(&link, nd, &cookie);
Al Virobd92d7f2011-03-14 19:54:59 -04001569 if (!err)
1570 err = lookup_last(nd, &path);
Al Viro574197e2011-03-14 22:20:34 -04001571 put_link(nd, &link, cookie);
Al Virobd92d7f2011-03-14 19:54:59 -04001572 }
1573 }
Al Viroee0827c2011-02-21 23:38:09 -05001574
Al Viro9f1fafe2011-03-25 11:00:12 -04001575 if (!err)
1576 err = complete_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04001577
1578 if (!err && nd->flags & LOOKUP_DIRECTORY) {
1579 if (!nd->inode->i_op->lookup) {
1580 path_put(&nd->path);
Al Virobd23a532011-03-23 09:56:30 -04001581 err = -ENOTDIR;
Al Virobd92d7f2011-03-14 19:54:59 -04001582 }
1583 }
Al Viro16c2cd72011-02-22 15:50:10 -05001584
Al Viro70e9b352011-03-05 21:12:22 -05001585 if (base)
1586 fput(base);
Al Viroee0827c2011-02-21 23:38:09 -05001587
Al Viro5b6ca022011-03-09 23:04:47 -05001588 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
Al Viro2a737872009-04-07 11:49:53 -04001589 path_put(&nd->root);
1590 nd->root.mnt = NULL;
1591 }
Al Virobd92d7f2011-03-14 19:54:59 -04001592 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001593}
Nick Piggin31e6b012011-01-07 17:49:52 +11001594
Al Viroee0827c2011-02-21 23:38:09 -05001595static int do_path_lookup(int dfd, const char *name,
1596 unsigned int flags, struct nameidata *nd)
1597{
1598 int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1599 if (unlikely(retval == -ECHILD))
1600 retval = path_lookupat(dfd, name, flags, nd);
1601 if (unlikely(retval == -ESTALE))
1602 retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11001603
1604 if (likely(!retval)) {
1605 if (unlikely(!audit_dummy_context())) {
1606 if (nd->path.dentry && nd->inode)
1607 audit_inode(name, nd->path.dentry);
1608 }
1609 }
Al Viro9b4a9b12009-04-07 11:44:16 -04001610 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611}
1612
Al Viroc9c6cac2011-02-16 15:15:47 -05001613int kern_path_parent(const char *name, struct nameidata *nd)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001614{
Al Viroc9c6cac2011-02-16 15:15:47 -05001615 return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001616}
1617
Al Virod1811462008-08-02 00:49:18 -04001618int kern_path(const char *name, unsigned int flags, struct path *path)
1619{
1620 struct nameidata nd;
1621 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1622 if (!res)
1623 *path = nd.path;
1624 return res;
1625}
1626
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001627/**
1628 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1629 * @dentry: pointer to dentry of the base directory
1630 * @mnt: pointer to vfs mount of the base directory
1631 * @name: pointer to file name
1632 * @flags: lookup flags
Al Viroe0a01242011-06-27 17:00:37 -04001633 * @path: pointer to struct path to fill
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001634 */
1635int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
1636 const char *name, unsigned int flags,
Al Viroe0a01242011-06-27 17:00:37 -04001637 struct path *path)
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001638{
Al Viroe0a01242011-06-27 17:00:37 -04001639 struct nameidata nd;
1640 int err;
1641 nd.root.dentry = dentry;
1642 nd.root.mnt = mnt;
1643 BUG_ON(flags & LOOKUP_PARENT);
Al Viro5b6ca022011-03-09 23:04:47 -05001644 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
Al Viroe0a01242011-06-27 17:00:37 -04001645 err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
1646 if (!err)
1647 *path = nd.path;
1648 return err;
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001649}
1650
Christoph Hellwigeead1912007-10-16 23:25:38 -07001651static struct dentry *__lookup_hash(struct qstr *name,
1652 struct dentry *base, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653{
Christoph Hellwig81fca442010-10-06 10:47:47 +02001654 struct inode *inode = base->d_inode;
James Morris057f6c02007-04-26 00:12:05 -07001655 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 int err;
1657
Al Viro4ad5abb2011-06-20 19:57:03 -04001658 err = inode_permission(inode, MAY_EXEC);
Christoph Hellwig81fca442010-10-06 10:47:47 +02001659 if (err)
1660 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
1662 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001663 * Don't bother with __d_lookup: callers are for creat as
1664 * well as unlink, so a lot of the time it would cost
1665 * a double lookup.
Al Viro6e6b1bd2009-08-13 23:38:37 +04001666 */
Nick Pigginb04f7842010-08-18 04:37:34 +10001667 dentry = d_lookup(base, name);
Al Viro6e6b1bd2009-08-13 23:38:37 +04001668
Josef Bacik44396f42011-05-31 11:58:49 -04001669 if (dentry && d_need_lookup(dentry)) {
1670 /*
1671 * __lookup_hash is called with the parent dir's i_mutex already
1672 * held, so we are good to go here.
1673 */
1674 dentry = d_inode_lookup(base, dentry, nd);
1675 if (IS_ERR(dentry))
1676 return dentry;
1677 }
1678
Al Virod2d9e9f2011-06-20 10:55:26 -04001679 if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE)) {
1680 int status = d_revalidate(dentry, nd);
1681 if (unlikely(status <= 0)) {
1682 /*
1683 * The dentry failed validation.
1684 * If d_revalidate returned 0 attempt to invalidate
1685 * the dentry otherwise d_revalidate is asking us
1686 * to return a fail status.
1687 */
1688 if (status < 0) {
1689 dput(dentry);
1690 return ERR_PTR(status);
1691 } else if (!d_invalidate(dentry)) {
1692 dput(dentry);
1693 dentry = NULL;
1694 }
1695 }
1696 }
Al Viro6e6b1bd2009-08-13 23:38:37 +04001697
Nick Pigginbaa03892010-08-18 04:37:31 +10001698 if (!dentry)
1699 dentry = d_alloc_and_lookup(base, name, nd);
Al Viro5a202bc2011-03-08 14:17:44 -05001700
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 return dentry;
1702}
1703
James Morris057f6c02007-04-26 00:12:05 -07001704/*
1705 * Restricted form of lookup. Doesn't follow links, single-component only,
1706 * needs parent already locked. Doesn't follow mounts.
1707 * SMP-safe.
1708 */
Adrian Bunka244e162006-03-31 02:32:11 -08001709static struct dentry *lookup_hash(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710{
Jan Blunck4ac91372008-02-14 19:34:32 -08001711 return __lookup_hash(&nd->last, nd->path.dentry, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712}
1713
Christoph Hellwigeead1912007-10-16 23:25:38 -07001714/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07001715 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07001716 * @name: pathname component to lookup
1717 * @base: base directory to lookup from
1718 * @len: maximum length @len should be interpreted to
1719 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07001720 * Note that this routine is purely a helper for filesystem usage and should
1721 * not be called by generic code. Also note that by using this function the
Christoph Hellwigeead1912007-10-16 23:25:38 -07001722 * nameidata argument is passed to the filesystem methods and a filesystem
1723 * using this helper needs to be prepared for that.
1724 */
James Morris057f6c02007-04-26 00:12:05 -07001725struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1726{
James Morris057f6c02007-04-26 00:12:05 -07001727 struct qstr this;
Al Viro6a96ba52011-03-07 23:49:20 -05001728 unsigned long hash;
1729 unsigned int c;
James Morris057f6c02007-04-26 00:12:05 -07001730
David Woodhouse2f9092e2009-04-20 23:18:37 +01001731 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
1732
Al Viro6a96ba52011-03-07 23:49:20 -05001733 this.name = name;
1734 this.len = len;
1735 if (!len)
1736 return ERR_PTR(-EACCES);
1737
1738 hash = init_name_hash();
1739 while (len--) {
1740 c = *(const unsigned char *)name++;
1741 if (c == '/' || c == '\0')
1742 return ERR_PTR(-EACCES);
1743 hash = partial_name_hash(c, hash);
1744 }
1745 this.hash = end_name_hash(hash);
Al Viro5a202bc2011-03-08 14:17:44 -05001746 /*
1747 * See if the low-level filesystem might want
1748 * to use its own hash..
1749 */
1750 if (base->d_flags & DCACHE_OP_HASH) {
1751 int err = base->d_op->d_hash(base, base->d_inode, &this);
1752 if (err < 0)
1753 return ERR_PTR(err);
1754 }
Christoph Hellwigeead1912007-10-16 23:25:38 -07001755
Christoph Hellwig49705b72005-11-08 21:35:06 -08001756 return __lookup_hash(&this, base, NULL);
James Morris057f6c02007-04-26 00:12:05 -07001757}
1758
Al Viro2d8f3032008-07-22 09:59:21 -04001759int user_path_at(int dfd, const char __user *name, unsigned flags,
1760 struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761{
Al Viro2d8f3032008-07-22 09:59:21 -04001762 struct nameidata nd;
Al Virof52e0c12011-03-14 18:56:51 -04001763 char *tmp = getname_flags(name, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 int err = PTR_ERR(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 if (!IS_ERR(tmp)) {
Al Viro2d8f3032008-07-22 09:59:21 -04001766
1767 BUG_ON(flags & LOOKUP_PARENT);
1768
1769 err = do_path_lookup(dfd, tmp, flags, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 putname(tmp);
Al Viro2d8f3032008-07-22 09:59:21 -04001771 if (!err)
1772 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 }
1774 return err;
1775}
1776
Al Viro2ad94ae2008-07-21 09:32:51 -04001777static int user_path_parent(int dfd, const char __user *path,
1778 struct nameidata *nd, char **name)
1779{
1780 char *s = getname(path);
1781 int error;
1782
1783 if (IS_ERR(s))
1784 return PTR_ERR(s);
1785
1786 error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
1787 if (error)
1788 putname(s);
1789 else
1790 *name = s;
1791
1792 return error;
1793}
1794
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795/*
1796 * It's inline, so penalty for filesystems that don't use sticky bit is
1797 * minimal.
1798 */
1799static inline int check_sticky(struct inode *dir, struct inode *inode)
1800{
David Howellsda9592e2008-11-14 10:39:05 +11001801 uid_t fsuid = current_fsuid();
1802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 if (!(dir->i_mode & S_ISVTX))
1804 return 0;
Serge E. Hallyne795b712011-03-23 16:43:25 -07001805 if (current_user_ns() != inode_userns(inode))
1806 goto other_userns;
David Howellsda9592e2008-11-14 10:39:05 +11001807 if (inode->i_uid == fsuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 return 0;
David Howellsda9592e2008-11-14 10:39:05 +11001809 if (dir->i_uid == fsuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 return 0;
Serge E. Hallyne795b712011-03-23 16:43:25 -07001811
1812other_userns:
1813 return !ns_capable(inode_userns(inode), CAP_FOWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814}
1815
1816/*
1817 * Check whether we can remove a link victim from directory dir, check
1818 * whether the type of victim is right.
1819 * 1. We can't do it if dir is read-only (done in permission())
1820 * 2. We should have write and exec permissions on dir
1821 * 3. We can't remove anything from append-only dir
1822 * 4. We can't do anything with immutable dir (done in permission())
1823 * 5. If the sticky bit on dir is set we should either
1824 * a. be owner of dir, or
1825 * b. be owner of victim, or
1826 * c. have CAP_FOWNER capability
1827 * 6. If the victim is append-only or immutable we can't do antyhing with
1828 * links pointing to it.
1829 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1830 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1831 * 9. We can't remove a root or mountpoint.
1832 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1833 * nfs_async_unlink().
1834 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001835static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836{
1837 int error;
1838
1839 if (!victim->d_inode)
1840 return -ENOENT;
1841
1842 BUG_ON(victim->d_parent->d_inode != dir);
Al Virocccc6bb2009-12-25 05:07:33 -05001843 audit_inode_child(victim, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Al Virof419a2e2008-07-22 00:07:17 -04001845 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 if (error)
1847 return error;
1848 if (IS_APPEND(dir))
1849 return -EPERM;
1850 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
Hugh Dickinsf9454542008-11-19 15:36:38 -08001851 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 return -EPERM;
1853 if (isdir) {
1854 if (!S_ISDIR(victim->d_inode->i_mode))
1855 return -ENOTDIR;
1856 if (IS_ROOT(victim))
1857 return -EBUSY;
1858 } else if (S_ISDIR(victim->d_inode->i_mode))
1859 return -EISDIR;
1860 if (IS_DEADDIR(dir))
1861 return -ENOENT;
1862 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1863 return -EBUSY;
1864 return 0;
1865}
1866
1867/* Check whether we can create an object with dentry child in directory
1868 * dir.
1869 * 1. We can't do it if child already exists (open has special treatment for
1870 * this case, but since we are inlined it's OK)
1871 * 2. We can't do it if dir is read-only (done in permission())
1872 * 3. We should have write and exec permissions on dir
1873 * 4. We can't do it if dir is immutable (done in permission())
1874 */
Miklos Szeredia95164d2008-07-30 15:08:48 +02001875static inline int may_create(struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876{
1877 if (child->d_inode)
1878 return -EEXIST;
1879 if (IS_DEADDIR(dir))
1880 return -ENOENT;
Al Virof419a2e2008-07-22 00:07:17 -04001881 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882}
1883
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884/*
1885 * p1 and p2 should be directories on the same fs.
1886 */
1887struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1888{
1889 struct dentry *p;
1890
1891 if (p1 == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07001892 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 return NULL;
1894 }
1895
Arjan van de Vena11f3a02006-03-23 03:00:33 -08001896 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001898 p = d_ancestor(p2, p1);
1899 if (p) {
1900 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1901 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
1902 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 }
1904
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001905 p = d_ancestor(p1, p2);
1906 if (p) {
1907 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1908 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1909 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 }
1911
Ingo Molnarf2eace22006-07-03 00:25:05 -07001912 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1913 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 return NULL;
1915}
1916
1917void unlock_rename(struct dentry *p1, struct dentry *p2)
1918{
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001919 mutex_unlock(&p1->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 if (p1 != p2) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001921 mutex_unlock(&p2->d_inode->i_mutex);
Arjan van de Vena11f3a02006-03-23 03:00:33 -08001922 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 }
1924}
1925
1926int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1927 struct nameidata *nd)
1928{
Miklos Szeredia95164d2008-07-30 15:08:48 +02001929 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
1931 if (error)
1932 return error;
1933
Al Viroacfa4382008-12-04 10:06:33 -05001934 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 return -EACCES; /* shouldn't it be ENOSYS? */
1936 mode &= S_IALLUGO;
1937 mode |= S_IFREG;
1938 error = security_inode_create(dir, dentry, mode);
1939 if (error)
1940 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 error = dir->i_op->create(dir, dentry, mode, nd);
Stephen Smalleya74574a2005-09-09 13:01:44 -07001942 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00001943 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 return error;
1945}
1946
Al Viro73d049a2011-03-11 12:08:24 -05001947static int may_open(struct path *path, int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001949 struct dentry *dentry = path->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 struct inode *inode = dentry->d_inode;
1951 int error;
1952
Al Virobcda7652011-03-13 16:42:14 -04001953 /* O_PATH? */
1954 if (!acc_mode)
1955 return 0;
1956
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 if (!inode)
1958 return -ENOENT;
1959
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001960 switch (inode->i_mode & S_IFMT) {
1961 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001963 case S_IFDIR:
1964 if (acc_mode & MAY_WRITE)
1965 return -EISDIR;
1966 break;
1967 case S_IFBLK:
1968 case S_IFCHR:
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001969 if (path->mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001971 /*FALLTHRU*/
1972 case S_IFIFO:
1973 case S_IFSOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001975 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08001976 }
Dave Hansenb41572e2007-10-16 23:31:14 -07001977
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001978 error = inode_permission(inode, acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07001979 if (error)
1980 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05001981
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 /*
1983 * An append-only file must be opened in append mode for writing.
1984 */
1985 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05001986 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05001987 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05001989 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 }
1991
1992 /* O_NOATIME can only be set by the owner or superuser */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001993 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
Al Viro7715b522009-12-16 03:54:00 -05001994 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
1996 /*
1997 * Ensure there are no outstanding leases on the file.
1998 */
Al Virob65a9cf2009-12-16 06:27:40 -05001999 return break_lease(inode, flag);
Al Viro7715b522009-12-16 03:54:00 -05002000}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
Jeff Laytone1181ee2010-12-07 16:19:50 -05002002static int handle_truncate(struct file *filp)
Al Viro7715b522009-12-16 03:54:00 -05002003{
Jeff Laytone1181ee2010-12-07 16:19:50 -05002004 struct path *path = &filp->f_path;
Al Viro7715b522009-12-16 03:54:00 -05002005 struct inode *inode = path->dentry->d_inode;
2006 int error = get_write_access(inode);
2007 if (error)
2008 return error;
2009 /*
2010 * Refuse to truncate files with mandatory locks held on them.
2011 */
2012 error = locks_verify_locked(inode);
2013 if (!error)
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +09002014 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05002015 if (!error) {
2016 error = do_truncate(path->dentry, 0,
2017 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
Jeff Laytone1181ee2010-12-07 16:19:50 -05002018 filp);
Al Viro7715b522009-12-16 03:54:00 -05002019 }
2020 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04002021 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022}
2023
Dave Hansend57999e2008-02-15 14:37:27 -08002024static inline int open_to_namei_flags(int flag)
2025{
Al Viro8a5e9292011-06-25 19:15:54 -04002026 if ((flag & O_ACCMODE) == 3)
2027 flag--;
Dave Hansend57999e2008-02-15 14:37:27 -08002028 return flag;
2029}
2030
Nick Piggin31e6b012011-01-07 17:49:52 +11002031/*
Al Virofe2d35f2011-03-05 22:58:25 -05002032 * Handle the last step of open()
Nick Piggin31e6b012011-01-07 17:49:52 +11002033 */
Al Virofb1cc552009-12-24 01:58:28 -05002034static struct file *do_last(struct nameidata *nd, struct path *path,
Al Viroc3e380b2011-02-23 13:39:45 -05002035 const struct open_flags *op, const char *pathname)
Al Virofb1cc552009-12-24 01:58:28 -05002036{
Al Viroa1e28032009-12-24 02:12:06 -05002037 struct dentry *dir = nd->path.dentry;
Al Viro6c0d46c2011-03-09 00:59:59 -05002038 struct dentry *dentry;
Al Viroca344a892011-03-09 00:36:45 -05002039 int open_flag = op->open_flag;
Al Viro6c0d46c2011-03-09 00:59:59 -05002040 int will_truncate = open_flag & O_TRUNC;
Al Viroca344a892011-03-09 00:36:45 -05002041 int want_write = 0;
Al Virobcda7652011-03-13 16:42:14 -04002042 int acc_mode = op->acc_mode;
Al Virofb1cc552009-12-24 01:58:28 -05002043 struct file *filp;
Al Viro16c2cd72011-02-22 15:50:10 -05002044 int error;
Al Virofb1cc552009-12-24 01:58:28 -05002045
Al Viroc3e380b2011-02-23 13:39:45 -05002046 nd->flags &= ~LOOKUP_PARENT;
2047 nd->flags |= op->intent;
2048
Al Viro1f36f772009-12-26 10:56:19 -05002049 switch (nd->last_type) {
2050 case LAST_DOTDOT:
Neil Brown176306f2010-05-24 16:57:56 +10002051 case LAST_DOT:
Al Virofe2d35f2011-03-05 22:58:25 -05002052 error = handle_dots(nd, nd->last_type);
2053 if (error)
2054 return ERR_PTR(error);
Al Viro1f36f772009-12-26 10:56:19 -05002055 /* fallthrough */
Al Viro1f36f772009-12-26 10:56:19 -05002056 case LAST_ROOT:
Al Viro9f1fafe2011-03-25 11:00:12 -04002057 error = complete_walk(nd);
Al Viro16c2cd72011-02-22 15:50:10 -05002058 if (error)
Al Viro9f1fafe2011-03-25 11:00:12 -04002059 return ERR_PTR(error);
Al Virofe2d35f2011-03-05 22:58:25 -05002060 audit_inode(pathname, nd->path.dentry);
Al Viroca344a892011-03-09 00:36:45 -05002061 if (open_flag & O_CREAT) {
Al Virofe2d35f2011-03-05 22:58:25 -05002062 error = -EISDIR;
2063 goto exit;
2064 }
2065 goto ok;
Al Viro1f36f772009-12-26 10:56:19 -05002066 case LAST_BIND:
Al Viro9f1fafe2011-03-25 11:00:12 -04002067 error = complete_walk(nd);
Al Viro16c2cd72011-02-22 15:50:10 -05002068 if (error)
Al Viro9f1fafe2011-03-25 11:00:12 -04002069 return ERR_PTR(error);
Al Viro1f36f772009-12-26 10:56:19 -05002070 audit_inode(pathname, dir);
Al Viro67ee3ad2009-12-26 07:01:01 -05002071 goto ok;
Al Viro1f36f772009-12-26 10:56:19 -05002072 }
Al Viro67ee3ad2009-12-26 07:01:01 -05002073
Al Viroca344a892011-03-09 00:36:45 -05002074 if (!(open_flag & O_CREAT)) {
Al Virobcda7652011-03-13 16:42:14 -04002075 int symlink_ok = 0;
Al Virofe2d35f2011-03-05 22:58:25 -05002076 if (nd->last.name[nd->last.len])
2077 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
Al Virobcda7652011-03-13 16:42:14 -04002078 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2079 symlink_ok = 1;
Al Virofe2d35f2011-03-05 22:58:25 -05002080 /* we _can_ be in RCU mode here */
Al Viroce57dfc2011-03-13 19:58:58 -04002081 error = walk_component(nd, path, &nd->last, LAST_NORM,
2082 !symlink_ok);
2083 if (error < 0)
Al Virofe2d35f2011-03-05 22:58:25 -05002084 return ERR_PTR(error);
Al Viroce57dfc2011-03-13 19:58:58 -04002085 if (error) /* symlink */
Al Virofe2d35f2011-03-05 22:58:25 -05002086 return NULL;
Al Virofe2d35f2011-03-05 22:58:25 -05002087 /* sayonara */
Al Viro9f1fafe2011-03-25 11:00:12 -04002088 error = complete_walk(nd);
2089 if (error)
2090 return ERR_PTR(-ECHILD);
Al Virofe2d35f2011-03-05 22:58:25 -05002091
2092 error = -ENOTDIR;
2093 if (nd->flags & LOOKUP_DIRECTORY) {
Al Viroce57dfc2011-03-13 19:58:58 -04002094 if (!nd->inode->i_op->lookup)
Al Virofe2d35f2011-03-05 22:58:25 -05002095 goto exit;
2096 }
2097 audit_inode(pathname, nd->path.dentry);
2098 goto ok;
2099 }
2100
2101 /* create side of things */
Al Viro9f1fafe2011-03-25 11:00:12 -04002102 error = complete_walk(nd);
2103 if (error)
2104 return ERR_PTR(error);
Al Virofe2d35f2011-03-05 22:58:25 -05002105
2106 audit_inode(pathname, dir);
Al Viro16c2cd72011-02-22 15:50:10 -05002107 error = -EISDIR;
Al Viro1f36f772009-12-26 10:56:19 -05002108 /* trailing slashes? */
Nick Piggin31e6b012011-01-07 17:49:52 +11002109 if (nd->last.name[nd->last.len])
2110 goto exit;
Al Viroa2c36b42009-12-24 03:39:50 -05002111
Al Viroa1e28032009-12-24 02:12:06 -05002112 mutex_lock(&dir->d_inode->i_mutex);
2113
Al Viro6c0d46c2011-03-09 00:59:59 -05002114 dentry = lookup_hash(nd);
2115 error = PTR_ERR(dentry);
2116 if (IS_ERR(dentry)) {
Al Virofb1cc552009-12-24 01:58:28 -05002117 mutex_unlock(&dir->d_inode->i_mutex);
2118 goto exit;
2119 }
2120
Al Viro6c0d46c2011-03-09 00:59:59 -05002121 path->dentry = dentry;
2122 path->mnt = nd->path.mnt;
2123
Al Virofb1cc552009-12-24 01:58:28 -05002124 /* Negative dentry, just create the file */
Al Viro6c0d46c2011-03-09 00:59:59 -05002125 if (!dentry->d_inode) {
2126 int mode = op->mode;
2127 if (!IS_POSIXACL(dir->d_inode))
2128 mode &= ~current_umask();
Al Virofb1cc552009-12-24 01:58:28 -05002129 /*
2130 * This write is needed to ensure that a
Al Viro6c0d46c2011-03-09 00:59:59 -05002131 * rw->ro transition does not occur between
Al Virofb1cc552009-12-24 01:58:28 -05002132 * the time when the file is created and when
2133 * a permanent write count is taken through
2134 * the 'struct file' in nameidata_to_filp().
2135 */
2136 error = mnt_want_write(nd->path.mnt);
2137 if (error)
2138 goto exit_mutex_unlock;
Al Viroca344a892011-03-09 00:36:45 -05002139 want_write = 1;
Al Viro9b44f1b2011-03-09 00:17:27 -05002140 /* Don't check for write permission, don't truncate */
Al Viroca344a892011-03-09 00:36:45 -05002141 open_flag &= ~O_TRUNC;
Al Viro6c0d46c2011-03-09 00:59:59 -05002142 will_truncate = 0;
Al Virobcda7652011-03-13 16:42:14 -04002143 acc_mode = MAY_OPEN;
Al Viro6c0d46c2011-03-09 00:59:59 -05002144 error = security_path_mknod(&nd->path, dentry, mode, 0);
2145 if (error)
2146 goto exit_mutex_unlock;
2147 error = vfs_create(dir->d_inode, dentry, mode, nd);
2148 if (error)
2149 goto exit_mutex_unlock;
2150 mutex_unlock(&dir->d_inode->i_mutex);
2151 dput(nd->path.dentry);
2152 nd->path.dentry = dentry;
Al Viroca344a892011-03-09 00:36:45 -05002153 goto common;
Al Virofb1cc552009-12-24 01:58:28 -05002154 }
2155
2156 /*
2157 * It already exists.
2158 */
2159 mutex_unlock(&dir->d_inode->i_mutex);
2160 audit_inode(pathname, path->dentry);
2161
2162 error = -EEXIST;
Al Viroca344a892011-03-09 00:36:45 -05002163 if (open_flag & O_EXCL)
Al Virofb1cc552009-12-24 01:58:28 -05002164 goto exit_dput;
2165
David Howells9875cf82011-01-14 18:45:21 +00002166 error = follow_managed(path, nd->flags);
2167 if (error < 0)
2168 goto exit_dput;
Al Virofb1cc552009-12-24 01:58:28 -05002169
2170 error = -ENOENT;
2171 if (!path->dentry->d_inode)
2172 goto exit_dput;
Al Viro9e67f362009-12-26 07:04:50 -05002173
2174 if (path->dentry->d_inode->i_op->follow_link)
Al Virofb1cc552009-12-24 01:58:28 -05002175 return NULL;
Al Virofb1cc552009-12-24 01:58:28 -05002176
2177 path_to_nameidata(path, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11002178 nd->inode = path->dentry->d_inode;
Al Virofb1cc552009-12-24 01:58:28 -05002179 error = -EISDIR;
Nick Piggin31e6b012011-01-07 17:49:52 +11002180 if (S_ISDIR(nd->inode->i_mode))
Al Virofb1cc552009-12-24 01:58:28 -05002181 goto exit;
Al Viro67ee3ad2009-12-26 07:01:01 -05002182ok:
Al Viro6c0d46c2011-03-09 00:59:59 -05002183 if (!S_ISREG(nd->inode->i_mode))
2184 will_truncate = 0;
2185
Al Viro0f9d1a12011-03-09 00:13:14 -05002186 if (will_truncate) {
2187 error = mnt_want_write(nd->path.mnt);
2188 if (error)
2189 goto exit;
Al Viroca344a892011-03-09 00:36:45 -05002190 want_write = 1;
Al Viro0f9d1a12011-03-09 00:13:14 -05002191 }
Al Viroca344a892011-03-09 00:36:45 -05002192common:
Al Virobcda7652011-03-13 16:42:14 -04002193 error = may_open(&nd->path, acc_mode, open_flag);
Al Viroca344a892011-03-09 00:36:45 -05002194 if (error)
Al Viro0f9d1a12011-03-09 00:13:14 -05002195 goto exit;
Al Viro0f9d1a12011-03-09 00:13:14 -05002196 filp = nameidata_to_filp(nd);
2197 if (!IS_ERR(filp)) {
2198 error = ima_file_check(filp, op->acc_mode);
2199 if (error) {
2200 fput(filp);
2201 filp = ERR_PTR(error);
2202 }
2203 }
2204 if (!IS_ERR(filp)) {
2205 if (will_truncate) {
2206 error = handle_truncate(filp);
2207 if (error) {
2208 fput(filp);
2209 filp = ERR_PTR(error);
2210 }
2211 }
2212 }
Al Viroca344a892011-03-09 00:36:45 -05002213out:
2214 if (want_write)
Al Viro0f9d1a12011-03-09 00:13:14 -05002215 mnt_drop_write(nd->path.mnt);
2216 path_put(&nd->path);
Al Virofb1cc552009-12-24 01:58:28 -05002217 return filp;
2218
2219exit_mutex_unlock:
2220 mutex_unlock(&dir->d_inode->i_mutex);
2221exit_dput:
2222 path_put_conditional(path, nd);
2223exit:
Al Viroca344a892011-03-09 00:36:45 -05002224 filp = ERR_PTR(error);
2225 goto out;
Al Virofb1cc552009-12-24 01:58:28 -05002226}
2227
Al Viro13aab422011-02-23 17:54:08 -05002228static struct file *path_openat(int dfd, const char *pathname,
Al Viro73d049a2011-03-11 12:08:24 -05002229 struct nameidata *nd, const struct open_flags *op, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230{
Al Virofe2d35f2011-03-05 22:58:25 -05002231 struct file *base = NULL;
Dave Hansen4a3fd212008-02-15 14:37:48 -08002232 struct file *filp;
Al Viro9850c052010-01-13 15:01:15 -05002233 struct path path;
Al Viro13aab422011-02-23 17:54:08 -05002234 int error;
Nick Piggin31e6b012011-01-07 17:49:52 +11002235
2236 filp = get_empty_filp();
2237 if (!filp)
2238 return ERR_PTR(-ENFILE);
2239
Al Viro47c805d2011-02-23 17:44:09 -05002240 filp->f_flags = op->open_flag;
Al Viro73d049a2011-03-11 12:08:24 -05002241 nd->intent.open.file = filp;
2242 nd->intent.open.flags = open_to_namei_flags(op->open_flag);
2243 nd->intent.open.create_mode = op->mode;
Nick Piggin31e6b012011-01-07 17:49:52 +11002244
Al Viro73d049a2011-03-11 12:08:24 -05002245 error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
Nick Piggin31e6b012011-01-07 17:49:52 +11002246 if (unlikely(error))
Al Viro13aab422011-02-23 17:54:08 -05002247 goto out_filp;
Nick Piggin31e6b012011-01-07 17:49:52 +11002248
Al Virofe2d35f2011-03-05 22:58:25 -05002249 current->total_link_count = 0;
Al Viro73d049a2011-03-11 12:08:24 -05002250 error = link_path_walk(pathname, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11002251 if (unlikely(error))
2252 goto out_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
Al Viro73d049a2011-03-11 12:08:24 -05002254 filp = do_last(nd, &path, op, pathname);
Al Viro806b6812009-12-26 07:16:40 -05002255 while (unlikely(!filp)) { /* trailing symlink */
Nick Piggin7b9337a2011-01-14 08:42:43 +00002256 struct path link = path;
Al Virodef4af32009-12-26 08:37:05 -05002257 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04002258 if (!(nd->flags & LOOKUP_FOLLOW)) {
Al Viro73d049a2011-03-11 12:08:24 -05002259 path_put_conditional(&path, nd);
2260 path_put(&nd->path);
Al Viro40b39132011-03-09 16:22:18 -05002261 filp = ERR_PTR(-ELOOP);
2262 break;
2263 }
Al Viro73d049a2011-03-11 12:08:24 -05002264 nd->flags |= LOOKUP_PARENT;
2265 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
Al Viro574197e2011-03-14 22:20:34 -04002266 error = follow_link(&link, nd, &cookie);
Al Viroc3e380b2011-02-23 13:39:45 -05002267 if (unlikely(error))
Al Virof1afe9e2011-02-22 22:27:28 -05002268 filp = ERR_PTR(error);
Al Viroc3e380b2011-02-23 13:39:45 -05002269 else
Al Viro73d049a2011-03-11 12:08:24 -05002270 filp = do_last(nd, &path, op, pathname);
Al Viro574197e2011-03-14 22:20:34 -04002271 put_link(nd, &link, cookie);
Al Viro806b6812009-12-26 07:16:40 -05002272 }
Al Viro10fa8e62009-12-26 07:09:49 -05002273out:
Al Viro73d049a2011-03-11 12:08:24 -05002274 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
2275 path_put(&nd->root);
Al Virofe2d35f2011-03-05 22:58:25 -05002276 if (base)
2277 fput(base);
Al Viro73d049a2011-03-11 12:08:24 -05002278 release_open_intent(nd);
Al Viro10fa8e62009-12-26 07:09:49 -05002279 return filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
Nick Piggin31e6b012011-01-07 17:49:52 +11002281out_filp:
Al Viro806b6812009-12-26 07:16:40 -05002282 filp = ERR_PTR(error);
Al Viro10fa8e62009-12-26 07:09:49 -05002283 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284}
2285
Al Viro13aab422011-02-23 17:54:08 -05002286struct file *do_filp_open(int dfd, const char *pathname,
2287 const struct open_flags *op, int flags)
2288{
Al Viro73d049a2011-03-11 12:08:24 -05002289 struct nameidata nd;
Al Viro13aab422011-02-23 17:54:08 -05002290 struct file *filp;
2291
Al Viro73d049a2011-03-11 12:08:24 -05002292 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
Al Viro13aab422011-02-23 17:54:08 -05002293 if (unlikely(filp == ERR_PTR(-ECHILD)))
Al Viro73d049a2011-03-11 12:08:24 -05002294 filp = path_openat(dfd, pathname, &nd, op, flags);
Al Viro13aab422011-02-23 17:54:08 -05002295 if (unlikely(filp == ERR_PTR(-ESTALE)))
Al Viro73d049a2011-03-11 12:08:24 -05002296 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
Al Viro13aab422011-02-23 17:54:08 -05002297 return filp;
2298}
2299
Al Viro73d049a2011-03-11 12:08:24 -05002300struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
2301 const char *name, const struct open_flags *op, int flags)
2302{
2303 struct nameidata nd;
2304 struct file *file;
2305
2306 nd.root.mnt = mnt;
2307 nd.root.dentry = dentry;
2308
2309 flags |= LOOKUP_ROOT;
2310
Al Virobcda7652011-03-13 16:42:14 -04002311 if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
Al Viro73d049a2011-03-11 12:08:24 -05002312 return ERR_PTR(-ELOOP);
2313
2314 file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
2315 if (unlikely(file == ERR_PTR(-ECHILD)))
2316 file = path_openat(-1, name, &nd, op, flags);
2317 if (unlikely(file == ERR_PTR(-ESTALE)))
2318 file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
2319 return file;
2320}
2321
Al Viroed75e952011-06-27 16:53:43 -04002322struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002324 struct dentry *dentry = ERR_PTR(-EEXIST);
Al Viroed75e952011-06-27 16:53:43 -04002325 struct nameidata nd;
2326 int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
2327 if (error)
2328 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002330 /*
2331 * Yucky last component or no last component at all?
2332 * (foo/., foo/.., /////)
2333 */
Al Viroed75e952011-06-27 16:53:43 -04002334 if (nd.last_type != LAST_NORM)
2335 goto out;
2336 nd.flags &= ~LOOKUP_PARENT;
2337 nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
2338 nd.intent.open.flags = O_EXCL;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002339
2340 /*
2341 * Do the final lookup.
2342 */
Al Viroed75e952011-06-27 16:53:43 -04002343 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2344 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 if (IS_ERR(dentry))
2346 goto fail;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002347
Al Viroe9baf6e2008-05-15 04:49:12 -04002348 if (dentry->d_inode)
2349 goto eexist;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002350 /*
2351 * Special case - lookup gave negative, but... we had foo/bar/
2352 * From the vfs_mknod() POV we just have a negative dentry -
2353 * all is fine. Let's be bastards - you had / on the end, you've
2354 * been asking for (non-existent) directory. -ENOENT for you.
2355 */
Al Viroed75e952011-06-27 16:53:43 -04002356 if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
Al Viroe9baf6e2008-05-15 04:49:12 -04002357 dput(dentry);
2358 dentry = ERR_PTR(-ENOENT);
Al Viroed75e952011-06-27 16:53:43 -04002359 goto fail;
Al Viroe9baf6e2008-05-15 04:49:12 -04002360 }
Al Viroed75e952011-06-27 16:53:43 -04002361 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 return dentry;
Al Viroe9baf6e2008-05-15 04:49:12 -04002363eexist:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 dput(dentry);
Al Viroe9baf6e2008-05-15 04:49:12 -04002365 dentry = ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366fail:
Al Viroed75e952011-06-27 16:53:43 -04002367 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2368out:
2369 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 return dentry;
2371}
Al Virodae6ad82011-06-26 11:50:15 -04002372EXPORT_SYMBOL(kern_path_create);
2373
2374struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir)
2375{
2376 char *tmp = getname(pathname);
2377 struct dentry *res;
2378 if (IS_ERR(tmp))
2379 return ERR_CAST(tmp);
2380 res = kern_path_create(dfd, tmp, path, is_dir);
2381 putname(tmp);
2382 return res;
2383}
2384EXPORT_SYMBOL(user_path_create);
2385
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
2387{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002388 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
2390 if (error)
2391 return error;
2392
Serge E. Hallyne795b712011-03-23 16:43:25 -07002393 if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
2394 !ns_capable(inode_userns(dir), CAP_MKNOD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 return -EPERM;
2396
Al Viroacfa4382008-12-04 10:06:33 -05002397 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 return -EPERM;
2399
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07002400 error = devcgroup_inode_mknod(mode, dev);
2401 if (error)
2402 return error;
2403
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 error = security_inode_mknod(dir, dentry, mode, dev);
2405 if (error)
2406 return error;
2407
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002409 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002410 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 return error;
2412}
2413
Dave Hansen463c3192008-02-15 14:37:57 -08002414static int may_mknod(mode_t mode)
2415{
2416 switch (mode & S_IFMT) {
2417 case S_IFREG:
2418 case S_IFCHR:
2419 case S_IFBLK:
2420 case S_IFIFO:
2421 case S_IFSOCK:
2422 case 0: /* zero mode translates to S_IFREG */
2423 return 0;
2424 case S_IFDIR:
2425 return -EPERM;
2426 default:
2427 return -EINVAL;
2428 }
2429}
2430
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002431SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
2432 unsigned, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433{
Al Viro2ad94ae2008-07-21 09:32:51 -04002434 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04002435 struct path path;
2436 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
2438 if (S_ISDIR(mode))
2439 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440
Al Virodae6ad82011-06-26 11:50:15 -04002441 dentry = user_path_create(dfd, filename, &path, 0);
2442 if (IS_ERR(dentry))
2443 return PTR_ERR(dentry);
Al Viro2ad94ae2008-07-21 09:32:51 -04002444
Al Virodae6ad82011-06-26 11:50:15 -04002445 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04002446 mode &= ~current_umask();
Dave Hansen463c3192008-02-15 14:37:57 -08002447 error = may_mknod(mode);
2448 if (error)
2449 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04002450 error = mnt_want_write(path.mnt);
Dave Hansen463c3192008-02-15 14:37:57 -08002451 if (error)
2452 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04002453 error = security_path_mknod(&path, dentry, mode, dev);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002454 if (error)
2455 goto out_drop_write;
Dave Hansen463c3192008-02-15 14:37:57 -08002456 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 case 0: case S_IFREG:
Al Virodae6ad82011-06-26 11:50:15 -04002458 error = vfs_create(path.dentry->d_inode,dentry,mode,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 break;
2460 case S_IFCHR: case S_IFBLK:
Al Virodae6ad82011-06-26 11:50:15 -04002461 error = vfs_mknod(path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 new_decode_dev(dev));
2463 break;
2464 case S_IFIFO: case S_IFSOCK:
Al Virodae6ad82011-06-26 11:50:15 -04002465 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 }
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002468out_drop_write:
Al Virodae6ad82011-06-26 11:50:15 -04002469 mnt_drop_write(path.mnt);
Dave Hansen463c3192008-02-15 14:37:57 -08002470out_dput:
2471 dput(dentry);
Al Virodae6ad82011-06-26 11:50:15 -04002472 mutex_unlock(&path.dentry->d_inode->i_mutex);
2473 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
2475 return error;
2476}
2477
Heiko Carstens3480b252009-01-14 14:14:16 +01002478SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002479{
2480 return sys_mknodat(AT_FDCWD, filename, mode, dev);
2481}
2482
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2484{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002485 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
2487 if (error)
2488 return error;
2489
Al Viroacfa4382008-12-04 10:06:33 -05002490 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 return -EPERM;
2492
2493 mode &= (S_IRWXUGO|S_ISVTX);
2494 error = security_inode_mkdir(dir, dentry, mode);
2495 if (error)
2496 return error;
2497
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002499 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002500 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 return error;
2502}
2503
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002504SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505{
Dave Hansen6902d922006-09-30 23:29:01 -07002506 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04002507 struct path path;
2508 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509
Al Virodae6ad82011-06-26 11:50:15 -04002510 dentry = user_path_create(dfd, pathname, &path, 1);
Dave Hansen6902d922006-09-30 23:29:01 -07002511 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04002512 return PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002513
Al Virodae6ad82011-06-26 11:50:15 -04002514 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04002515 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04002516 error = mnt_want_write(path.mnt);
Dave Hansen463c3192008-02-15 14:37:57 -08002517 if (error)
2518 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04002519 error = security_path_mkdir(&path, dentry, mode);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002520 if (error)
2521 goto out_drop_write;
Al Virodae6ad82011-06-26 11:50:15 -04002522 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002523out_drop_write:
Al Virodae6ad82011-06-26 11:50:15 -04002524 mnt_drop_write(path.mnt);
Dave Hansen463c3192008-02-15 14:37:57 -08002525out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002526 dput(dentry);
Al Virodae6ad82011-06-26 11:50:15 -04002527 mutex_unlock(&path.dentry->d_inode->i_mutex);
2528 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 return error;
2530}
2531
Heiko Carstens3cdad422009-01-14 14:14:22 +01002532SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002533{
2534 return sys_mkdirat(AT_FDCWD, pathname, mode);
2535}
2536
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537/*
Sage Weila71905f2011-05-24 13:06:08 -07002538 * The dentry_unhash() helper will try to drop the dentry early: we
2539 * should have a usage count of 2 if we're the only user of this
2540 * dentry, and if that is true (possibly after pruning the dcache),
2541 * then we drop the dentry now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 *
2543 * A low-level filesystem can, if it choses, legally
2544 * do a
2545 *
2546 * if (!d_unhashed(dentry))
2547 * return -EBUSY;
2548 *
2549 * if it cannot handle the case of removing a directory
2550 * that is still in use by something else..
2551 */
2552void dentry_unhash(struct dentry *dentry)
2553{
Vasily Averindc168422006-12-06 20:37:07 -08002554 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 spin_lock(&dentry->d_lock);
Sage Weil64252c72011-05-24 13:06:05 -07002556 if (dentry->d_count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 __d_drop(dentry);
2558 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559}
2560
2561int vfs_rmdir(struct inode *dir, struct dentry *dentry)
2562{
2563 int error = may_delete(dir, dentry, 1);
2564
2565 if (error)
2566 return error;
2567
Al Viroacfa4382008-12-04 10:06:33 -05002568 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 return -EPERM;
2570
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002571 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
Sage Weil912dbc12011-05-24 13:06:11 -07002573 error = -EBUSY;
2574 if (d_mountpoint(dentry))
2575 goto out;
2576
2577 error = security_inode_rmdir(dir, dentry);
2578 if (error)
2579 goto out;
2580
Sage Weil3cebde22011-05-29 21:20:59 -07002581 shrink_dcache_parent(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07002582 error = dir->i_op->rmdir(dir, dentry);
2583 if (error)
2584 goto out;
2585
2586 dentry->d_inode->i_flags |= S_DEAD;
2587 dont_mount(dentry);
2588
2589out:
2590 mutex_unlock(&dentry->d_inode->i_mutex);
2591 if (!error)
2592 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 return error;
2594}
2595
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002596static long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597{
2598 int error = 0;
2599 char * name;
2600 struct dentry *dentry;
2601 struct nameidata nd;
2602
Al Viro2ad94ae2008-07-21 09:32:51 -04002603 error = user_path_parent(dfd, pathname, &nd, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002605 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
2607 switch(nd.last_type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002608 case LAST_DOTDOT:
2609 error = -ENOTEMPTY;
2610 goto exit1;
2611 case LAST_DOT:
2612 error = -EINVAL;
2613 goto exit1;
2614 case LAST_ROOT:
2615 error = -EBUSY;
2616 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002618
2619 nd.flags &= ~LOOKUP_PARENT;
2620
Jan Blunck4ac91372008-02-14 19:34:32 -08002621 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002622 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002624 if (IS_ERR(dentry))
2625 goto exit2;
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04002626 if (!dentry->d_inode) {
2627 error = -ENOENT;
2628 goto exit3;
2629 }
Dave Hansen06227532008-02-15 14:37:34 -08002630 error = mnt_want_write(nd.path.mnt);
2631 if (error)
2632 goto exit3;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002633 error = security_path_rmdir(&nd.path, dentry);
2634 if (error)
2635 goto exit4;
Jan Blunck4ac91372008-02-14 19:34:32 -08002636 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002637exit4:
Dave Hansen06227532008-02-15 14:37:34 -08002638 mnt_drop_write(nd.path.mnt);
2639exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07002640 dput(dentry);
2641exit2:
Jan Blunck4ac91372008-02-14 19:34:32 -08002642 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002644 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 putname(name);
2646 return error;
2647}
2648
Heiko Carstens3cdad422009-01-14 14:14:22 +01002649SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002650{
2651 return do_rmdir(AT_FDCWD, pathname);
2652}
2653
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654int vfs_unlink(struct inode *dir, struct dentry *dentry)
2655{
2656 int error = may_delete(dir, dentry, 0);
2657
2658 if (error)
2659 return error;
2660
Al Viroacfa4382008-12-04 10:06:33 -05002661 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 return -EPERM;
2663
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002664 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 if (d_mountpoint(dentry))
2666 error = -EBUSY;
2667 else {
2668 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05002669 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 error = dir->i_op->unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05002671 if (!error)
Al Virod83c49f2010-04-30 17:17:09 -04002672 dont_mount(dentry);
Al Virobec10522010-03-03 14:12:08 -05002673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002675 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676
2677 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2678 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
Jan Karaece95912008-02-06 01:37:13 -08002679 fsnotify_link_count(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 }
Robert Love0eeca282005-07-12 17:06:03 -04002682
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 return error;
2684}
2685
2686/*
2687 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002688 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 * writeout happening, and we don't want to prevent access to the directory
2690 * while waiting on the I/O.
2691 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002692static long do_unlinkat(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693{
Al Viro2ad94ae2008-07-21 09:32:51 -04002694 int error;
2695 char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 struct dentry *dentry;
2697 struct nameidata nd;
2698 struct inode *inode = NULL;
2699
Al Viro2ad94ae2008-07-21 09:32:51 -04002700 error = user_path_parent(dfd, pathname, &nd, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002702 return error;
2703
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 error = -EISDIR;
2705 if (nd.last_type != LAST_NORM)
2706 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002707
2708 nd.flags &= ~LOOKUP_PARENT;
2709
Jan Blunck4ac91372008-02-14 19:34:32 -08002710 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002711 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 error = PTR_ERR(dentry);
2713 if (!IS_ERR(dentry)) {
2714 /* Why not before? Because we want correct error value */
Török Edwin50338b82011-06-16 00:06:14 +03002715 if (nd.last.name[nd.last.len])
2716 goto slashes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 inode = dentry->d_inode;
Török Edwin50338b82011-06-16 00:06:14 +03002718 if (!inode)
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04002719 goto slashes;
2720 ihold(inode);
Dave Hansen06227532008-02-15 14:37:34 -08002721 error = mnt_want_write(nd.path.mnt);
2722 if (error)
2723 goto exit2;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002724 error = security_path_unlink(&nd.path, dentry);
2725 if (error)
2726 goto exit3;
Jan Blunck4ac91372008-02-14 19:34:32 -08002727 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002728exit3:
Dave Hansen06227532008-02-15 14:37:34 -08002729 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 exit2:
2731 dput(dentry);
2732 }
Jan Blunck4ac91372008-02-14 19:34:32 -08002733 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 if (inode)
2735 iput(inode); /* truncate the inode here */
2736exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002737 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 putname(name);
2739 return error;
2740
2741slashes:
2742 error = !dentry->d_inode ? -ENOENT :
2743 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2744 goto exit2;
2745}
2746
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002747SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002748{
2749 if ((flag & ~AT_REMOVEDIR) != 0)
2750 return -EINVAL;
2751
2752 if (flag & AT_REMOVEDIR)
2753 return do_rmdir(dfd, pathname);
2754
2755 return do_unlinkat(dfd, pathname);
2756}
2757
Heiko Carstens3480b252009-01-14 14:14:16 +01002758SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002759{
2760 return do_unlinkat(AT_FDCWD, pathname);
2761}
2762
Miklos Szeredidb2e7472008-06-24 16:50:16 +02002763int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002765 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766
2767 if (error)
2768 return error;
2769
Al Viroacfa4382008-12-04 10:06:33 -05002770 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 return -EPERM;
2772
2773 error = security_inode_symlink(dir, dentry, oldname);
2774 if (error)
2775 return error;
2776
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002778 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002779 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 return error;
2781}
2782
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002783SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
2784 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785{
Al Viro2ad94ae2008-07-21 09:32:51 -04002786 int error;
2787 char *from;
Dave Hansen6902d922006-09-30 23:29:01 -07002788 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04002789 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790
2791 from = getname(oldname);
Al Viro2ad94ae2008-07-21 09:32:51 -04002792 if (IS_ERR(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 return PTR_ERR(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04002794
Al Virodae6ad82011-06-26 11:50:15 -04002795 dentry = user_path_create(newdfd, newname, &path, 0);
Dave Hansen6902d922006-09-30 23:29:01 -07002796 error = PTR_ERR(dentry);
2797 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04002798 goto out_putname;
Dave Hansen6902d922006-09-30 23:29:01 -07002799
Al Virodae6ad82011-06-26 11:50:15 -04002800 error = mnt_want_write(path.mnt);
Dave Hansen75c3f292008-02-15 14:37:45 -08002801 if (error)
2802 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04002803 error = security_path_symlink(&path, dentry, from);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002804 if (error)
2805 goto out_drop_write;
Al Virodae6ad82011-06-26 11:50:15 -04002806 error = vfs_symlink(path.dentry->d_inode, dentry, from);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002807out_drop_write:
Al Virodae6ad82011-06-26 11:50:15 -04002808 mnt_drop_write(path.mnt);
Dave Hansen75c3f292008-02-15 14:37:45 -08002809out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002810 dput(dentry);
Al Virodae6ad82011-06-26 11:50:15 -04002811 mutex_unlock(&path.dentry->d_inode->i_mutex);
2812 path_put(&path);
Dave Hansen6902d922006-09-30 23:29:01 -07002813out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 putname(from);
2815 return error;
2816}
2817
Heiko Carstens3480b252009-01-14 14:14:16 +01002818SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002819{
2820 return sys_symlinkat(oldname, AT_FDCWD, newname);
2821}
2822
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2824{
2825 struct inode *inode = old_dentry->d_inode;
2826 int error;
2827
2828 if (!inode)
2829 return -ENOENT;
2830
Miklos Szeredia95164d2008-07-30 15:08:48 +02002831 error = may_create(dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 if (error)
2833 return error;
2834
2835 if (dir->i_sb != inode->i_sb)
2836 return -EXDEV;
2837
2838 /*
2839 * A link to an append-only or immutable file cannot be created.
2840 */
2841 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2842 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05002843 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002845 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 return -EPERM;
2847
2848 error = security_inode_link(old_dentry, dir, new_dentry);
2849 if (error)
2850 return error;
2851
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002852 mutex_lock(&inode->i_mutex);
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05302853 /* Make sure we don't allow creating hardlink to an unlinked file */
2854 if (inode->i_nlink == 0)
2855 error = -ENOENT;
2856 else
2857 error = dir->i_op->link(old_dentry, dir, new_dentry);
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002858 mutex_unlock(&inode->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07002859 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002860 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 return error;
2862}
2863
2864/*
2865 * Hardlinks are often used in delicate situations. We avoid
2866 * security-related surprises by not following symlinks on the
2867 * newname. --KAB
2868 *
2869 * We don't follow them on the oldname either to be compatible
2870 * with linux 2.0, and to avoid hard-linking to directories
2871 * and other special files. --ADM
2872 */
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002873SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
2874 int, newdfd, const char __user *, newname, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875{
2876 struct dentry *new_dentry;
Al Virodae6ad82011-06-26 11:50:15 -04002877 struct path old_path, new_path;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05302878 int how = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05302881 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002882 return -EINVAL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05302883 /*
2884 * To use null names we require CAP_DAC_READ_SEARCH
2885 * This ensures that not everyone will be able to create
2886 * handlink using the passed filedescriptor.
2887 */
2888 if (flags & AT_EMPTY_PATH) {
2889 if (!capable(CAP_DAC_READ_SEARCH))
2890 return -ENOENT;
2891 how = LOOKUP_EMPTY;
2892 }
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002893
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05302894 if (flags & AT_SYMLINK_FOLLOW)
2895 how |= LOOKUP_FOLLOW;
2896
2897 error = user_path_at(olddfd, oldname, how, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002899 return error;
2900
Al Virodae6ad82011-06-26 11:50:15 -04002901 new_dentry = user_path_create(newdfd, newname, &new_path, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002903 if (IS_ERR(new_dentry))
Al Virodae6ad82011-06-26 11:50:15 -04002904 goto out;
2905
2906 error = -EXDEV;
2907 if (old_path.mnt != new_path.mnt)
2908 goto out_dput;
2909 error = mnt_want_write(new_path.mnt);
Dave Hansen75c3f292008-02-15 14:37:45 -08002910 if (error)
2911 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04002912 error = security_path_link(old_path.dentry, &new_path, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002913 if (error)
2914 goto out_drop_write;
Al Virodae6ad82011-06-26 11:50:15 -04002915 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002916out_drop_write:
Al Virodae6ad82011-06-26 11:50:15 -04002917 mnt_drop_write(new_path.mnt);
Dave Hansen75c3f292008-02-15 14:37:45 -08002918out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002919 dput(new_dentry);
Al Virodae6ad82011-06-26 11:50:15 -04002920 mutex_unlock(&new_path.dentry->d_inode->i_mutex);
2921 path_put(&new_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922out:
Al Viro2d8f3032008-07-22 09:59:21 -04002923 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924
2925 return error;
2926}
2927
Heiko Carstens3480b252009-01-14 14:14:16 +01002928SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002929{
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002930 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002931}
2932
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933/*
2934 * The worst of all namespace operations - renaming directory. "Perverted"
2935 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2936 * Problems:
2937 * a) we can get into loop creation. Check is done in is_subdir().
2938 * b) race potential - two innocent renames can create a loop together.
2939 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002940 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 * story.
2942 * c) we have to lock _three_ objects - parents and victim (if it exists).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002943 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 * whether the target exists). Solution: try to be smart with locking
2945 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002946 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 * move will be locked. Thus we can rank directories by the tree
2948 * (ancestors first) and rank all non-directories after them.
2949 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002950 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 * HOWEVER, it relies on the assumption that any object with ->lookup()
2952 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2953 * we'd better make sure that there's no link(2) for them.
Sage Weile4eaac02011-05-24 13:06:07 -07002954 * d) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002955 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05002957 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 * locking].
2959 */
Adrian Bunk75c96f82005-05-05 16:16:09 -07002960static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
2961 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962{
2963 int error = 0;
Sage Weil9055cba2011-05-24 13:06:12 -07002964 struct inode *target = new_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965
2966 /*
2967 * If we are going to change the parent - check write permissions,
2968 * we'll need to flip '..'.
2969 */
2970 if (new_dir != old_dir) {
Al Virof419a2e2008-07-22 00:07:17 -04002971 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 if (error)
2973 return error;
2974 }
2975
2976 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2977 if (error)
2978 return error;
2979
Al Virod83c49f2010-04-30 17:17:09 -04002980 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002981 mutex_lock(&target->i_mutex);
Sage Weil9055cba2011-05-24 13:06:12 -07002982
2983 error = -EBUSY;
2984 if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
2985 goto out;
2986
Sage Weil3cebde22011-05-29 21:20:59 -07002987 if (target)
2988 shrink_dcache_parent(new_dentry);
Sage Weil9055cba2011-05-24 13:06:12 -07002989 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2990 if (error)
2991 goto out;
2992
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 if (target) {
Sage Weil9055cba2011-05-24 13:06:12 -07002994 target->i_flags |= S_DEAD;
2995 dont_mount(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 }
Sage Weil9055cba2011-05-24 13:06:12 -07002997out:
2998 if (target)
2999 mutex_unlock(&target->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07003000 if (!error)
Mark Fasheh349457c2006-09-08 14:22:21 -07003001 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3002 d_move(old_dentry,new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 return error;
3004}
3005
Adrian Bunk75c96f82005-05-05 16:16:09 -07003006static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
3007 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008{
Sage Weil51892bb2011-05-24 13:06:13 -07003009 struct inode *target = new_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 int error;
3011
3012 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3013 if (error)
3014 return error;
3015
3016 dget(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003018 mutex_lock(&target->i_mutex);
Sage Weil51892bb2011-05-24 13:06:13 -07003019
3020 error = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
Sage Weil51892bb2011-05-24 13:06:13 -07003022 goto out;
3023
3024 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3025 if (error)
3026 goto out;
3027
3028 if (target)
3029 dont_mount(new_dentry);
3030 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3031 d_move(old_dentry, new_dentry);
3032out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003034 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 dput(new_dentry);
3036 return error;
3037}
3038
3039int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
3040 struct inode *new_dir, struct dentry *new_dentry)
3041{
3042 int error;
3043 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
Eric Paris59b0df22010-02-08 12:53:52 -05003044 const unsigned char *old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045
3046 if (old_dentry->d_inode == new_dentry->d_inode)
3047 return 0;
3048
3049 error = may_delete(old_dir, old_dentry, is_dir);
3050 if (error)
3051 return error;
3052
3053 if (!new_dentry->d_inode)
Miklos Szeredia95164d2008-07-30 15:08:48 +02003054 error = may_create(new_dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 else
3056 error = may_delete(new_dir, new_dentry, is_dir);
3057 if (error)
3058 return error;
3059
Al Viroacfa4382008-12-04 10:06:33 -05003060 if (!old_dir->i_op->rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 return -EPERM;
3062
Robert Love0eeca282005-07-12 17:06:03 -04003063 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
3064
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 if (is_dir)
3066 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
3067 else
3068 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
Al Viro123df292009-12-25 04:57:57 -05003069 if (!error)
3070 fsnotify_move(old_dir, new_dir, old_name, is_dir,
Al Viro5a190ae2007-06-07 12:19:32 -04003071 new_dentry->d_inode, old_dentry);
Robert Love0eeca282005-07-12 17:06:03 -04003072 fsnotify_oldname_free(old_name);
3073
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 return error;
3075}
3076
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003077SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
3078 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079{
Al Viro2ad94ae2008-07-21 09:32:51 -04003080 struct dentry *old_dir, *new_dir;
3081 struct dentry *old_dentry, *new_dentry;
3082 struct dentry *trap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 struct nameidata oldnd, newnd;
Al Viro2ad94ae2008-07-21 09:32:51 -04003084 char *from;
3085 char *to;
3086 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087
Al Viro2ad94ae2008-07-21 09:32:51 -04003088 error = user_path_parent(olddfd, oldname, &oldnd, &from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 if (error)
3090 goto exit;
3091
Al Viro2ad94ae2008-07-21 09:32:51 -04003092 error = user_path_parent(newdfd, newname, &newnd, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 if (error)
3094 goto exit1;
3095
3096 error = -EXDEV;
Jan Blunck4ac91372008-02-14 19:34:32 -08003097 if (oldnd.path.mnt != newnd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 goto exit2;
3099
Jan Blunck4ac91372008-02-14 19:34:32 -08003100 old_dir = oldnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 error = -EBUSY;
3102 if (oldnd.last_type != LAST_NORM)
3103 goto exit2;
3104
Jan Blunck4ac91372008-02-14 19:34:32 -08003105 new_dir = newnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 if (newnd.last_type != LAST_NORM)
3107 goto exit2;
3108
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003109 oldnd.flags &= ~LOOKUP_PARENT;
3110 newnd.flags &= ~LOOKUP_PARENT;
OGAWA Hirofumi4e9ed2f2008-10-16 07:50:29 +09003111 newnd.flags |= LOOKUP_RENAME_TARGET;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003112
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 trap = lock_rename(new_dir, old_dir);
3114
Christoph Hellwig49705b72005-11-08 21:35:06 -08003115 old_dentry = lookup_hash(&oldnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 error = PTR_ERR(old_dentry);
3117 if (IS_ERR(old_dentry))
3118 goto exit3;
3119 /* source must exist */
3120 error = -ENOENT;
3121 if (!old_dentry->d_inode)
3122 goto exit4;
3123 /* unless the source is a directory trailing slashes give -ENOTDIR */
3124 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
3125 error = -ENOTDIR;
3126 if (oldnd.last.name[oldnd.last.len])
3127 goto exit4;
3128 if (newnd.last.name[newnd.last.len])
3129 goto exit4;
3130 }
3131 /* source should not be ancestor of target */
3132 error = -EINVAL;
3133 if (old_dentry == trap)
3134 goto exit4;
Christoph Hellwig49705b72005-11-08 21:35:06 -08003135 new_dentry = lookup_hash(&newnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 error = PTR_ERR(new_dentry);
3137 if (IS_ERR(new_dentry))
3138 goto exit4;
3139 /* target should not be an ancestor of source */
3140 error = -ENOTEMPTY;
3141 if (new_dentry == trap)
3142 goto exit5;
3143
Dave Hansen9079b1e2008-02-15 14:37:49 -08003144 error = mnt_want_write(oldnd.path.mnt);
3145 if (error)
3146 goto exit5;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003147 error = security_path_rename(&oldnd.path, old_dentry,
3148 &newnd.path, new_dentry);
3149 if (error)
3150 goto exit6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 error = vfs_rename(old_dir->d_inode, old_dentry,
3152 new_dir->d_inode, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003153exit6:
Dave Hansen9079b1e2008-02-15 14:37:49 -08003154 mnt_drop_write(oldnd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155exit5:
3156 dput(new_dentry);
3157exit4:
3158 dput(old_dentry);
3159exit3:
3160 unlock_rename(new_dir, old_dir);
3161exit2:
Jan Blunck1d957f92008-02-14 19:34:35 -08003162 path_put(&newnd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04003163 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003165 path_put(&oldnd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 putname(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04003167exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 return error;
3169}
3170
Heiko Carstensa26eab22009-01-14 14:14:17 +01003171SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003172{
3173 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
3174}
3175
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
3177{
3178 int len;
3179
3180 len = PTR_ERR(link);
3181 if (IS_ERR(link))
3182 goto out;
3183
3184 len = strlen(link);
3185 if (len > (unsigned) buflen)
3186 len = buflen;
3187 if (copy_to_user(buffer, link, len))
3188 len = -EFAULT;
3189out:
3190 return len;
3191}
3192
3193/*
3194 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
3195 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
3196 * using) it for any given inode is up to filesystem.
3197 */
3198int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3199{
3200 struct nameidata nd;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003201 void *cookie;
Marcin Slusarz694a1762008-06-09 16:40:37 -07003202 int res;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003203
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 nd.depth = 0;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003205 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
Marcin Slusarz694a1762008-06-09 16:40:37 -07003206 if (IS_ERR(cookie))
3207 return PTR_ERR(cookie);
3208
3209 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
3210 if (dentry->d_inode->i_op->put_link)
3211 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3212 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213}
3214
3215int vfs_follow_link(struct nameidata *nd, const char *link)
3216{
3217 return __vfs_follow_link(nd, link);
3218}
3219
3220/* get the link contents into pagecache */
3221static char *page_getlink(struct dentry * dentry, struct page **ppage)
3222{
Duane Griffinebd09ab2008-12-19 20:47:12 +00003223 char *kaddr;
3224 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 struct address_space *mapping = dentry->d_inode->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -07003226 page = read_mapping_page(mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 if (IS_ERR(page))
Nick Piggin6fe69002007-05-06 14:49:04 -07003228 return (char*)page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 *ppage = page;
Duane Griffinebd09ab2008-12-19 20:47:12 +00003230 kaddr = kmap(page);
3231 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3232 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233}
3234
3235int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3236{
3237 struct page *page = NULL;
3238 char *s = page_getlink(dentry, &page);
3239 int res = vfs_readlink(dentry,buffer,buflen,s);
3240 if (page) {
3241 kunmap(page);
3242 page_cache_release(page);
3243 }
3244 return res;
3245}
3246
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003247void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003249 struct page *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 nd_set_link(nd, page_getlink(dentry, &page));
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003251 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252}
3253
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003254void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003256 struct page *page = cookie;
3257
3258 if (page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 kunmap(page);
3260 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 }
3262}
3263
Nick Piggin54566b22009-01-04 12:00:53 -08003264/*
3265 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
3266 */
3267int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268{
3269 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003270 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07003271 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08003272 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 char *kaddr;
Nick Piggin54566b22009-01-04 12:00:53 -08003274 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
3275 if (nofs)
3276 flags |= AOP_FLAG_NOFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277
NeilBrown7e53cac2006-03-25 03:07:57 -08003278retry:
Nick Pigginafddba42007-10-16 01:25:01 -07003279 err = pagecache_write_begin(NULL, mapping, 0, len-1,
Nick Piggin54566b22009-01-04 12:00:53 -08003280 flags, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07003282 goto fail;
3283
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 kaddr = kmap_atomic(page, KM_USER0);
3285 memcpy(kaddr, symname, len-1);
3286 kunmap_atomic(kaddr, KM_USER0);
Nick Pigginafddba42007-10-16 01:25:01 -07003287
3288 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3289 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 if (err < 0)
3291 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07003292 if (err < len-1)
3293 goto retry;
3294
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 mark_inode_dirty(inode);
3296 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297fail:
3298 return err;
3299}
3300
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003301int page_symlink(struct inode *inode, const char *symname, int len)
3302{
3303 return __page_symlink(inode, symname, len,
Nick Piggin54566b22009-01-04 12:00:53 -08003304 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003305}
3306
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003307const struct inode_operations page_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 .readlink = generic_readlink,
3309 .follow_link = page_follow_link_light,
3310 .put_link = page_put_link,
3311};
3312
Al Viro2d8f3032008-07-22 09:59:21 -04003313EXPORT_SYMBOL(user_path_at);
David Howellscc53ce52011-01-14 18:45:26 +00003314EXPORT_SYMBOL(follow_down_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315EXPORT_SYMBOL(follow_down);
3316EXPORT_SYMBOL(follow_up);
3317EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
3318EXPORT_SYMBOL(getname);
3319EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320EXPORT_SYMBOL(lookup_one_len);
3321EXPORT_SYMBOL(page_follow_link_light);
3322EXPORT_SYMBOL(page_put_link);
3323EXPORT_SYMBOL(page_readlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003324EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325EXPORT_SYMBOL(page_symlink);
3326EXPORT_SYMBOL(page_symlink_inode_operations);
Al Virod1811462008-08-02 00:49:18 -04003327EXPORT_SYMBOL(kern_path);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07003328EXPORT_SYMBOL(vfs_path_lookup);
Al Virof419a2e2008-07-22 00:07:17 -04003329EXPORT_SYMBOL(inode_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330EXPORT_SYMBOL(unlock_rename);
3331EXPORT_SYMBOL(vfs_create);
3332EXPORT_SYMBOL(vfs_follow_link);
3333EXPORT_SYMBOL(vfs_link);
3334EXPORT_SYMBOL(vfs_mkdir);
3335EXPORT_SYMBOL(vfs_mknod);
3336EXPORT_SYMBOL(generic_permission);
3337EXPORT_SYMBOL(vfs_readlink);
3338EXPORT_SYMBOL(vfs_rename);
3339EXPORT_SYMBOL(vfs_rmdir);
3340EXPORT_SYMBOL(vfs_symlink);
3341EXPORT_SYMBOL(vfs_unlink);
3342EXPORT_SYMBOL(dentry_unhash);
3343EXPORT_SYMBOL(generic_readlink);