blob: e70ebab9624bb8666b9c0b70598c83be14bfc63c [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>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050018#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#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"
Al Viroc7105362011-11-24 18:22:03 -050039#include "mount.h"
Eric Parise81e3f42009-12-04 15:47:36 -050040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041/* [Feb-1997 T. Schoebel-Theuer]
42 * Fundamental changes in the pathname lookup mechanisms (namei)
43 * were necessary because of omirr. The reason is that omirr needs
44 * to know the _real_ pathname, not the user-supplied one, in case
45 * of symlinks (and also when transname replacements occur).
46 *
47 * The new code replaces the old recursive symlink resolution with
48 * an iterative one (in case of non-nested symlink chains). It does
49 * this with calls to <fs>_follow_link().
50 * As a side effect, dir_namei(), _namei() and follow_link() are now
51 * replaced with a single function lookup_dentry() that can handle all
52 * the special cases of the former code.
53 *
54 * With the new dcache, the pathname is stored at each inode, at least as
55 * long as the refcount of the inode is positive. As a side effect, the
56 * size of the dcache depends on the inode cache and thus is dynamic.
57 *
58 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
59 * resolution to correspond with current state of the code.
60 *
61 * Note that the symlink resolution is not *completely* iterative.
62 * There is still a significant amount of tail- and mid- recursion in
63 * the algorithm. Also, note that <fs>_readlink() is not used in
64 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
65 * may return different results than <fs>_follow_link(). Many virtual
66 * filesystems (including /proc) exhibit this behavior.
67 */
68
69/* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
70 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
71 * and the name already exists in form of a symlink, try to create the new
72 * name indicated by the symlink. The old code always complained that the
73 * name already exists, due to not following the symlink even if its target
74 * is nonexistent. The new semantics affects also mknod() and link() when
Lucas De Marchi25985ed2011-03-30 22:57:33 -030075 * the name is a symlink pointing to a non-existent name.
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 *
77 * I don't know which semantics is the right one, since I have no access
78 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
79 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
80 * "old" one. Personally, I think the new semantics is much more logical.
81 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
82 * file does succeed in both HP-UX and SunOs, but not in Solaris
83 * and in the old Linux semantics.
84 */
85
86/* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
87 * semantics. See the comments in "open_namei" and "do_link" below.
88 *
89 * [10-Sep-98 Alan Modra] Another symlink change.
90 */
91
92/* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
93 * inside the path - always follow.
94 * in the last component in creation/removal/renaming - never follow.
95 * if LOOKUP_FOLLOW passed - follow.
96 * if the pathname has trailing slashes - follow.
97 * otherwise - don't follow.
98 * (applied in that order).
99 *
100 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
101 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
102 * During the 2.4 we need to fix the userland stuff depending on it -
103 * hopefully we will be able to get rid of that wart in 2.5. So far only
104 * XEmacs seems to be relying on it...
105 */
106/*
107 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
Arjan van de Vena11f3a02006-03-23 03:00:33 -0800108 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 * any extra contention...
110 */
111
112/* In order to reduce some races, while at the same time doing additional
113 * checking and hopefully speeding things up, we copy filenames to the
114 * kernel data space before using them..
115 *
116 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
117 * PATH_MAX includes the nul terminator --RR.
118 */
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +0100119static char *getname_flags(const char __user *filename, int flags, int *empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700121 char *result = __getname(), *err;
122 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700124 if (unlikely(!result))
Eric Paris4043cde2012-01-03 14:23:08 -0500125 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700127 len = strncpy_from_user(result, filename, PATH_MAX);
128 err = ERR_PTR(len);
129 if (unlikely(len < 0))
130 goto error;
131
132 /* The empty path is special. */
133 if (unlikely(!len)) {
134 if (empty)
Eric Paris4043cde2012-01-03 14:23:08 -0500135 *empty = 1;
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700136 err = ERR_PTR(-ENOENT);
137 if (!(flags & LOOKUP_EMPTY))
138 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 }
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700140
141 err = ERR_PTR(-ENAMETOOLONG);
142 if (likely(len < PATH_MAX)) {
143 audit_getname(result);
144 return result;
145 }
146
147error:
148 __putname(result);
149 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
Al Virof52e0c12011-03-14 18:56:51 -0400152char *getname(const char __user * filename)
153{
Linus Torvaldsf7493e52012-03-22 16:10:40 -0700154 return getname_flags(filename, 0, NULL);
Al Virof52e0c12011-03-14 18:56:51 -0400155}
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157#ifdef CONFIG_AUDITSYSCALL
158void putname(const char *name)
159{
Al Viro5ac3a9c2006-07-16 06:38:45 -0400160 if (unlikely(!audit_dummy_context()))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 audit_putname(name);
162 else
163 __putname(name);
164}
165EXPORT_SYMBOL(putname);
166#endif
167
Linus Torvaldse77819e2011-07-22 19:30:19 -0700168static int check_acl(struct inode *inode, int mask)
169{
Linus Torvalds84635d62011-07-25 22:47:03 -0700170#ifdef CONFIG_FS_POSIX_ACL
Linus Torvaldse77819e2011-07-22 19:30:19 -0700171 struct posix_acl *acl;
172
Linus Torvaldse77819e2011-07-22 19:30:19 -0700173 if (mask & MAY_NOT_BLOCK) {
Al Viro35678662011-08-02 21:32:13 -0400174 acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
175 if (!acl)
Linus Torvaldse77819e2011-07-22 19:30:19 -0700176 return -EAGAIN;
Al Viro35678662011-08-02 21:32:13 -0400177 /* no ->get_acl() calls in RCU mode... */
178 if (acl == ACL_NOT_CACHED)
179 return -ECHILD;
Ari Savolainen206b1d02011-08-06 19:43:07 +0300180 return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
Linus Torvaldse77819e2011-07-22 19:30:19 -0700181 }
182
183 acl = get_cached_acl(inode, ACL_TYPE_ACCESS);
184
185 /*
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200186 * A filesystem can force a ACL callback by just never filling the
187 * ACL cache. But normally you'd fill the cache either at inode
188 * instantiation time, or on the first ->get_acl call.
Linus Torvaldse77819e2011-07-22 19:30:19 -0700189 *
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200190 * If the filesystem doesn't have a get_acl() function at all, we'll
191 * just create the negative cache entry.
Linus Torvaldse77819e2011-07-22 19:30:19 -0700192 */
193 if (acl == ACL_NOT_CACHED) {
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200194 if (inode->i_op->get_acl) {
195 acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS);
196 if (IS_ERR(acl))
197 return PTR_ERR(acl);
198 } else {
199 set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
200 return -EAGAIN;
201 }
Linus Torvaldse77819e2011-07-22 19:30:19 -0700202 }
203
204 if (acl) {
205 int error = posix_acl_permission(inode, acl, mask);
206 posix_acl_release(acl);
207 return error;
208 }
Linus Torvalds84635d62011-07-25 22:47:03 -0700209#endif
Linus Torvaldse77819e2011-07-22 19:30:19 -0700210
211 return -EAGAIN;
212}
213
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700214/*
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530215 * This does the basic permission checking
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700216 */
Al Viro7e401452011-06-20 19:12:17 -0400217static int acl_permission_check(struct inode *inode, int mask)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700218{
Linus Torvalds26cf46b2011-05-13 11:51:01 -0700219 unsigned int mode = inode->i_mode;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700220
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -0800221 if (likely(uid_eq(current_fsuid(), inode->i_uid)))
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700222 mode >>= 6;
223 else {
Linus Torvaldse77819e2011-07-22 19:30:19 -0700224 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
Al Viro7e401452011-06-20 19:12:17 -0400225 int error = check_acl(inode, mask);
Nick Pigginb74c79e2011-01-07 17:49:58 +1100226 if (error != -EAGAIN)
227 return error;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700228 }
229
230 if (in_group_p(inode->i_gid))
231 mode >>= 3;
232 }
233
234 /*
235 * If the DACs are ok we don't need any capability check.
236 */
Al Viro9c2c7032011-06-20 19:06:22 -0400237 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700238 return 0;
239 return -EACCES;
240}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242/**
Nick Pigginb74c79e2011-01-07 17:49:58 +1100243 * generic_permission - check for access rights on a Posix-like filesystem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 * @inode: inode to check access rights for
Andreas Gruenbacher8fd90c82011-10-23 23:13:30 +0530245 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 *
247 * Used to check for read/write/execute permissions on a file.
248 * We use "fsuid" for this, letting us set arbitrary permissions
249 * for filesystem access without changing the "normal" uids which
Nick Pigginb74c79e2011-01-07 17:49:58 +1100250 * are used for other things.
251 *
252 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
253 * request cannot be satisfied (eg. requires blocking or too much complexity).
254 * It would then be called again in ref-walk mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 */
Al Viro2830ba72011-06-20 19:16:29 -0400256int generic_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700258 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 /*
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530261 * Do the basic permission checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 */
Al Viro7e401452011-06-20 19:12:17 -0400263 ret = acl_permission_check(inode, mask);
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700264 if (ret != -EACCES)
265 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Al Virod594e7e2011-06-20 19:55:42 -0400267 if (S_ISDIR(inode->i_mode)) {
268 /* DACs are overridable for directories */
Eric W. Biederman1a48e2a2011-11-14 16:24:06 -0800269 if (inode_capable(inode, CAP_DAC_OVERRIDE))
Al Virod594e7e2011-06-20 19:55:42 -0400270 return 0;
271 if (!(mask & MAY_WRITE))
Eric W. Biederman1a48e2a2011-11-14 16:24:06 -0800272 if (inode_capable(inode, CAP_DAC_READ_SEARCH))
Al Virod594e7e2011-06-20 19:55:42 -0400273 return 0;
274 return -EACCES;
275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 /*
277 * Read/write DACs are always overridable.
Al Virod594e7e2011-06-20 19:55:42 -0400278 * Executable DACs are overridable when there is
279 * at least one exec bit set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 */
Al Virod594e7e2011-06-20 19:55:42 -0400281 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
Eric W. Biederman1a48e2a2011-11-14 16:24:06 -0800282 if (inode_capable(inode, CAP_DAC_OVERRIDE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 return 0;
284
285 /*
286 * Searching includes executable on directories, else just read.
287 */
Serge E. Hallyn7ea66002009-12-29 14:50:19 -0600288 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
Al Virod594e7e2011-06-20 19:55:42 -0400289 if (mask == MAY_READ)
Eric W. Biederman1a48e2a2011-11-14 16:24:06 -0800290 if (inode_capable(inode, CAP_DAC_READ_SEARCH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return 0;
292
293 return -EACCES;
294}
295
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700296/*
297 * We _really_ want to just do "generic_permission()" without
298 * even looking at the inode->i_op values. So we keep a cache
299 * flag in inode->i_opflags, that says "this has not special
300 * permission function, use the fast case".
301 */
302static inline int do_inode_permission(struct inode *inode, int mask)
303{
304 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
305 if (likely(inode->i_op->permission))
306 return inode->i_op->permission(inode, mask);
307
308 /* This gets set once for the inode lifetime */
309 spin_lock(&inode->i_lock);
310 inode->i_opflags |= IOP_FASTPERM;
311 spin_unlock(&inode->i_lock);
312 }
313 return generic_permission(inode, mask);
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
Andreas Gruenbacher8fd90c82011-10-23 23:13:30 +0530319 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200320 *
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.
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530325 *
326 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200327 */
Al Virof419a2e2008-07-22 00:07:17 -0400328int inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
Al Viroe6305c42008-07-15 21:03:57 -0400330 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700332 if (unlikely(mask & MAY_WRITE)) {
Miklos Szeredi22590e42007-10-16 23:27:08 -0700333 umode_t mode = inode->i_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 /*
336 * Nobody gets write access to a read-only fs.
337 */
338 if (IS_RDONLY(inode) &&
339 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
340 return -EROFS;
341
342 /*
343 * Nobody gets write access to an immutable file.
344 */
345 if (IS_IMMUTABLE(inode))
346 return -EACCES;
347 }
348
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700349 retval = do_inode_permission(inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if (retval)
351 return retval;
352
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700353 retval = devcgroup_inode_permission(inode, mask);
354 if (retval)
355 return retval;
356
Eric Parisd09ca732010-07-23 11:43:57 -0400357 return security_inode_permission(inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
Al Virof4d6ff82011-06-19 13:14:21 -0400360/**
Jan Blunck5dd784d02008-02-14 19:34:38 -0800361 * path_get - get a reference to a path
362 * @path: path to get the reference to
363 *
364 * Given a path increment the reference count to the dentry and the vfsmount.
365 */
366void path_get(struct path *path)
367{
368 mntget(path->mnt);
369 dget(path->dentry);
370}
371EXPORT_SYMBOL(path_get);
372
373/**
Jan Blunck1d957f92008-02-14 19:34:35 -0800374 * path_put - put a reference to a path
375 * @path: path to put the reference to
376 *
377 * Given a path decrement the reference count to the dentry and the vfsmount.
378 */
379void path_put(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Jan Blunck1d957f92008-02-14 19:34:35 -0800381 dput(path->dentry);
382 mntput(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
Jan Blunck1d957f92008-02-14 19:34:35 -0800384EXPORT_SYMBOL(path_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Al Viro19660af2011-03-25 10:32:48 -0400386/*
Nick Piggin31e6b012011-01-07 17:49:52 +1100387 * Path walking has 2 modes, rcu-walk and ref-walk (see
Al Viro19660af2011-03-25 10:32:48 -0400388 * Documentation/filesystems/path-lookup.txt). In situations when we can't
389 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
390 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
391 * mode. Refcounts are grabbed at the last known good point before rcu-walk
392 * got stuck, so ref-walk may continue from there. If this is not successful
393 * (eg. a seqcount has changed), then failure is returned and it's up to caller
394 * to restart the path walk from the beginning in ref-walk mode.
Nick Piggin31e6b012011-01-07 17:49:52 +1100395 */
Nick Piggin31e6b012011-01-07 17:49:52 +1100396
397/**
Al Viro19660af2011-03-25 10:32:48 -0400398 * unlazy_walk - try to switch to ref-walk mode.
399 * @nd: nameidata pathwalk data
400 * @dentry: child of nd->path.dentry or NULL
Randy Dunlap39191622011-01-08 19:36:21 -0800401 * Returns: 0 on success, -ECHILD on failure
Nick Piggin31e6b012011-01-07 17:49:52 +1100402 *
Al Viro19660af2011-03-25 10:32:48 -0400403 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
404 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
405 * @nd or NULL. Must be called from rcu-walk context.
Nick Piggin31e6b012011-01-07 17:49:52 +1100406 */
Al Viro19660af2011-03-25 10:32:48 -0400407static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
Nick Piggin31e6b012011-01-07 17:49:52 +1100408{
409 struct fs_struct *fs = current->fs;
410 struct dentry *parent = nd->path.dentry;
Al Viro5b6ca022011-03-09 23:04:47 -0500411 int want_root = 0;
Nick Piggin31e6b012011-01-07 17:49:52 +1100412
413 BUG_ON(!(nd->flags & LOOKUP_RCU));
Al Viro5b6ca022011-03-09 23:04:47 -0500414 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
415 want_root = 1;
Nick Piggin31e6b012011-01-07 17:49:52 +1100416 spin_lock(&fs->lock);
417 if (nd->root.mnt != fs->root.mnt ||
418 nd->root.dentry != fs->root.dentry)
419 goto err_root;
420 }
421 spin_lock(&parent->d_lock);
Al Viro19660af2011-03-25 10:32:48 -0400422 if (!dentry) {
423 if (!__d_rcu_to_refcount(parent, nd->seq))
424 goto err_parent;
425 BUG_ON(nd->inode != parent->d_inode);
426 } else {
Al Viro94c0d4e2011-07-12 21:40:23 -0400427 if (dentry->d_parent != parent)
428 goto err_parent;
Al Viro19660af2011-03-25 10:32:48 -0400429 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
430 if (!__d_rcu_to_refcount(dentry, nd->seq))
431 goto err_child;
432 /*
433 * If the sequence check on the child dentry passed, then
434 * the child has not been removed from its parent. This
435 * means the parent dentry must be valid and able to take
436 * a reference at this point.
437 */
438 BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
439 BUG_ON(!parent->d_count);
440 parent->d_count++;
441 spin_unlock(&dentry->d_lock);
442 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100443 spin_unlock(&parent->d_lock);
Al Viro5b6ca022011-03-09 23:04:47 -0500444 if (want_root) {
Nick Piggin31e6b012011-01-07 17:49:52 +1100445 path_get(&nd->root);
446 spin_unlock(&fs->lock);
447 }
448 mntget(nd->path.mnt);
449
450 rcu_read_unlock();
451 br_read_unlock(vfsmount_lock);
452 nd->flags &= ~LOOKUP_RCU;
453 return 0;
Al Viro19660af2011-03-25 10:32:48 -0400454
455err_child:
Nick Piggin31e6b012011-01-07 17:49:52 +1100456 spin_unlock(&dentry->d_lock);
Al Viro19660af2011-03-25 10:32:48 -0400457err_parent:
Nick Piggin31e6b012011-01-07 17:49:52 +1100458 spin_unlock(&parent->d_lock);
459err_root:
Al Viro5b6ca022011-03-09 23:04:47 -0500460 if (want_root)
Nick Piggin31e6b012011-01-07 17:49:52 +1100461 spin_unlock(&fs->lock);
462 return -ECHILD;
463}
464
Nick Piggin31e6b012011-01-07 17:49:52 +1100465/**
Trond Myklebust834f2a42005-10-18 14:20:16 -0700466 * release_open_intent - free up open intent resources
467 * @nd: pointer to nameidata
468 */
469void release_open_intent(struct nameidata *nd)
470{
Linus Torvalds2dab5972011-02-11 15:53:38 -0800471 struct file *file = nd->intent.open.file;
472
473 if (file && !IS_ERR(file)) {
474 if (file->f_path.dentry == NULL)
475 put_filp(file);
476 else
477 fput(file);
478 }
Trond Myklebust834f2a42005-10-18 14:20:16 -0700479}
480
Al Virof60aef72011-02-15 01:35:28 -0500481static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
Nick Piggin34286d62011-01-07 17:49:57 +1100482{
Al Virof60aef72011-02-15 01:35:28 -0500483 return dentry->d_op->d_revalidate(dentry, nd);
Nick Piggin34286d62011-01-07 17:49:57 +1100484}
485
Al Viro9f1fafe2011-03-25 11:00:12 -0400486/**
487 * complete_walk - successful completion of path walk
488 * @nd: pointer nameidata
Jeff Layton39159de2009-12-07 12:01:50 -0500489 *
Al Viro9f1fafe2011-03-25 11:00:12 -0400490 * If we had been in RCU mode, drop out of it and legitimize nd->path.
491 * Revalidate the final result, unless we'd already done that during
492 * the path walk or the filesystem doesn't ask for it. Return 0 on
493 * success, -error on failure. In case of failure caller does not
494 * need to drop nd->path.
Jeff Layton39159de2009-12-07 12:01:50 -0500495 */
Al Viro9f1fafe2011-03-25 11:00:12 -0400496static int complete_walk(struct nameidata *nd)
Jeff Layton39159de2009-12-07 12:01:50 -0500497{
Al Viro16c2cd72011-02-22 15:50:10 -0500498 struct dentry *dentry = nd->path.dentry;
Jeff Layton39159de2009-12-07 12:01:50 -0500499 int status;
Jeff Layton39159de2009-12-07 12:01:50 -0500500
Al Viro9f1fafe2011-03-25 11:00:12 -0400501 if (nd->flags & LOOKUP_RCU) {
502 nd->flags &= ~LOOKUP_RCU;
503 if (!(nd->flags & LOOKUP_ROOT))
504 nd->root.mnt = NULL;
505 spin_lock(&dentry->d_lock);
506 if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
507 spin_unlock(&dentry->d_lock);
508 rcu_read_unlock();
509 br_read_unlock(vfsmount_lock);
510 return -ECHILD;
511 }
512 BUG_ON(nd->inode != dentry->d_inode);
513 spin_unlock(&dentry->d_lock);
514 mntget(nd->path.mnt);
515 rcu_read_unlock();
516 br_read_unlock(vfsmount_lock);
517 }
518
Al Viro16c2cd72011-02-22 15:50:10 -0500519 if (likely(!(nd->flags & LOOKUP_JUMPED)))
Jeff Layton39159de2009-12-07 12:01:50 -0500520 return 0;
521
Al Viro16c2cd72011-02-22 15:50:10 -0500522 if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
523 return 0;
524
525 if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
526 return 0;
527
528 /* Note: we do not d_invalidate() */
Nick Piggin34286d62011-01-07 17:49:57 +1100529 status = d_revalidate(dentry, nd);
Jeff Layton39159de2009-12-07 12:01:50 -0500530 if (status > 0)
531 return 0;
532
Al Viro16c2cd72011-02-22 15:50:10 -0500533 if (!status)
Jeff Layton39159de2009-12-07 12:01:50 -0500534 status = -ESTALE;
Al Viro16c2cd72011-02-22 15:50:10 -0500535
Al Viro9f1fafe2011-03-25 11:00:12 -0400536 path_put(&nd->path);
Jeff Layton39159de2009-12-07 12:01:50 -0500537 return status;
538}
539
Al Viro2a737872009-04-07 11:49:53 -0400540static __always_inline void set_root(struct nameidata *nd)
541{
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200542 if (!nd->root.mnt)
543 get_fs_root(current->fs, &nd->root);
Al Viro2a737872009-04-07 11:49:53 -0400544}
545
Al Viro6de88d72009-08-09 01:41:57 +0400546static int link_path_walk(const char *, struct nameidata *);
547
Nick Piggin31e6b012011-01-07 17:49:52 +1100548static __always_inline void set_root_rcu(struct nameidata *nd)
549{
550 if (!nd->root.mnt) {
551 struct fs_struct *fs = current->fs;
Nick Pigginc28cc362011-01-07 17:49:53 +1100552 unsigned seq;
553
554 do {
555 seq = read_seqcount_begin(&fs->seq);
556 nd->root = fs->root;
Tim Chenc1530012011-04-15 11:39:29 -0700557 nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
Nick Pigginc28cc362011-01-07 17:49:53 +1100558 } while (read_seqcount_retry(&fs->seq, seq));
Nick Piggin31e6b012011-01-07 17:49:52 +1100559 }
560}
561
Arjan van de Venf1662352006-01-14 13:21:31 -0800562static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Nick Piggin31e6b012011-01-07 17:49:52 +1100564 int ret;
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (IS_ERR(link))
567 goto fail;
568
569 if (*link == '/') {
Al Viro2a737872009-04-07 11:49:53 -0400570 set_root(nd);
Jan Blunck1d957f92008-02-14 19:34:35 -0800571 path_put(&nd->path);
Al Viro2a737872009-04-07 11:49:53 -0400572 nd->path = nd->root;
573 path_get(&nd->root);
Al Viro16c2cd72011-02-22 15:50:10 -0500574 nd->flags |= LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100576 nd->inode = nd->path.dentry->d_inode;
Christoph Hellwigb4091d52008-11-05 15:07:21 +0100577
Nick Piggin31e6b012011-01-07 17:49:52 +1100578 ret = link_path_walk(link, nd);
579 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580fail:
Jan Blunck1d957f92008-02-14 19:34:35 -0800581 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 return PTR_ERR(link);
583}
584
Jan Blunck1d957f92008-02-14 19:34:35 -0800585static void path_put_conditional(struct path *path, struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700586{
587 dput(path->dentry);
Jan Blunck4ac91372008-02-14 19:34:32 -0800588 if (path->mnt != nd->path.mnt)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700589 mntput(path->mnt);
590}
591
Nick Piggin7b9337a2011-01-14 08:42:43 +0000592static inline void path_to_nameidata(const struct path *path,
593 struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700594{
Nick Piggin31e6b012011-01-07 17:49:52 +1100595 if (!(nd->flags & LOOKUP_RCU)) {
596 dput(nd->path.dentry);
597 if (nd->path.mnt != path->mnt)
598 mntput(nd->path.mnt);
Huang Shijie9a229682010-04-02 17:37:13 +0800599 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100600 nd->path.mnt = path->mnt;
Jan Blunck4ac91372008-02-14 19:34:32 -0800601 nd->path.dentry = path->dentry;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700602}
603
Al Viro574197e2011-03-14 22:20:34 -0400604static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
605{
606 struct inode *inode = link->dentry->d_inode;
607 if (!IS_ERR(cookie) && inode->i_op->put_link)
608 inode->i_op->put_link(link->dentry, nd, cookie);
609 path_put(link);
610}
611
Al Virodef4af32009-12-26 08:37:05 -0500612static __always_inline int
Al Viro574197e2011-03-14 22:20:34 -0400613follow_link(struct path *link, struct nameidata *nd, void **p)
Ian Kent051d3812006-03-27 01:14:53 -0800614{
615 int error;
Nick Piggin7b9337a2011-01-14 08:42:43 +0000616 struct dentry *dentry = link->dentry;
Ian Kent051d3812006-03-27 01:14:53 -0800617
Al Viro844a3912011-02-15 00:38:26 -0500618 BUG_ON(nd->flags & LOOKUP_RCU);
619
Al Viro0e794582011-03-16 02:45:02 -0400620 if (link->mnt == nd->path.mnt)
621 mntget(link->mnt);
622
Al Viro574197e2011-03-14 22:20:34 -0400623 if (unlikely(current->total_link_count >= 40)) {
624 *p = ERR_PTR(-ELOOP); /* no ->put_link(), please */
Al Viro574197e2011-03-14 22:20:34 -0400625 path_put(&nd->path);
626 return -ELOOP;
627 }
628 cond_resched();
629 current->total_link_count++;
630
Al Viro68ac1232012-03-15 08:21:57 -0400631 touch_atime(link);
Ian Kent051d3812006-03-27 01:14:53 -0800632 nd_set_link(nd, NULL);
633
Al Viro36f3b4f2011-02-22 21:24:38 -0500634 error = security_inode_follow_link(link->dentry, nd);
635 if (error) {
636 *p = ERR_PTR(error); /* no ->put_link(), please */
637 path_put(&nd->path);
638 return error;
639 }
640
Al Viro86acdca12009-12-22 23:45:11 -0500641 nd->last_type = LAST_BIND;
Al Virodef4af32009-12-26 08:37:05 -0500642 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
643 error = PTR_ERR(*p);
644 if (!IS_ERR(*p)) {
Ian Kent051d3812006-03-27 01:14:53 -0800645 char *s = nd_get_link(nd);
646 error = 0;
647 if (s)
648 error = __vfs_follow_link(nd, s);
Al Virobcda7652011-03-13 16:42:14 -0400649 else if (nd->last_type == LAST_BIND) {
Al Viro16c2cd72011-02-22 15:50:10 -0500650 nd->flags |= LOOKUP_JUMPED;
Al Virob21041d2011-03-14 20:01:51 -0400651 nd->inode = nd->path.dentry->d_inode;
652 if (nd->inode->i_op->follow_link) {
Al Virobcda7652011-03-13 16:42:14 -0400653 /* stepped on a _really_ weird one */
654 path_put(&nd->path);
655 error = -ELOOP;
656 }
657 }
Ian Kent051d3812006-03-27 01:14:53 -0800658 }
Ian Kent051d3812006-03-27 01:14:53 -0800659 return error;
660}
661
Nick Piggin31e6b012011-01-07 17:49:52 +1100662static int follow_up_rcu(struct path *path)
663{
Al Viro0714a532011-11-24 22:19:58 -0500664 struct mount *mnt = real_mount(path->mnt);
665 struct mount *parent;
Nick Piggin31e6b012011-01-07 17:49:52 +1100666 struct dentry *mountpoint;
667
Al Viro0714a532011-11-24 22:19:58 -0500668 parent = mnt->mnt_parent;
669 if (&parent->mnt == path->mnt)
Nick Piggin31e6b012011-01-07 17:49:52 +1100670 return 0;
Al Viroa73324d2011-11-24 22:25:07 -0500671 mountpoint = mnt->mnt_mountpoint;
Nick Piggin31e6b012011-01-07 17:49:52 +1100672 path->dentry = mountpoint;
Al Viro0714a532011-11-24 22:19:58 -0500673 path->mnt = &parent->mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +1100674 return 1;
675}
676
Al Virobab77eb2009-04-18 03:26:48 -0400677int follow_up(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
Al Viro0714a532011-11-24 22:19:58 -0500679 struct mount *mnt = real_mount(path->mnt);
680 struct mount *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 struct dentry *mountpoint;
Nick Piggin99b7db72010-08-18 04:37:39 +1000682
683 br_read_lock(vfsmount_lock);
Al Viro0714a532011-11-24 22:19:58 -0500684 parent = mnt->mnt_parent;
685 if (&parent->mnt == 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 }
Al Viro0714a532011-11-24 22:19:58 -0500689 mntget(&parent->mnt);
Al Viroa73324d2011-11-24 22:25:07 -0500690 mountpoint = dget(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);
Al Viro0714a532011-11-24 22:19:58 -0500695 path->mnt = &parent->mnt;
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
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200713 /* We don't want to mount if someone's just doing a stat -
714 * unless they're stat'ing a directory and appended a '/' to
715 * the name.
716 *
717 * We do, however, want to mount if someone wants to open or
718 * create a file of any type under the mountpoint, wants to
719 * traverse through the mountpoint or wants to open the
720 * mounted directory. Also, autofs may mark negative dentries
721 * as being automount points. These will need the attentions
722 * of the daemon to instantiate them before they can be used.
David Howells9875cf82011-01-14 18:45:21 +0000723 */
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200724 if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
Linus Torvaldsd94c1772011-09-26 17:44:55 -0700725 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200726 path->dentry->d_inode)
727 return -EISDIR;
728
David Howells9875cf82011-01-14 18:45:21 +0000729 current->total_link_count++;
730 if (current->total_link_count >= 40)
731 return -ELOOP;
732
733 mnt = path->dentry->d_op->d_automount(path);
734 if (IS_ERR(mnt)) {
735 /*
736 * The filesystem is allowed to return -EISDIR here to indicate
737 * it doesn't want to automount. For instance, autofs would do
738 * this so that its userspace daemon can mount on this dentry.
739 *
740 * However, we can only permit this if it's a terminal point in
741 * the path being looked up; if it wasn't then the remainder of
742 * the path is inaccessible and we should say so.
743 */
Al Viro49084c32011-06-25 21:59:52 -0400744 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
David Howells9875cf82011-01-14 18:45:21 +0000745 return -EREMOTE;
746 return PTR_ERR(mnt);
747 }
David Howellsea5b7782011-01-14 19:10:03 +0000748
David Howells9875cf82011-01-14 18:45:21 +0000749 if (!mnt) /* mount collision */
750 return 0;
751
Al Viro8aef1882011-06-16 15:10:06 +0100752 if (!*need_mntput) {
753 /* lock_mount() may release path->mnt on error */
754 mntget(path->mnt);
755 *need_mntput = true;
756 }
Al Viro19a167a2011-01-17 01:35:23 -0500757 err = finish_automount(mnt, path);
David Howellsea5b7782011-01-14 19:10:03 +0000758
David Howellsea5b7782011-01-14 19:10:03 +0000759 switch (err) {
760 case -EBUSY:
761 /* Someone else made a mount here whilst we were busy */
Al Viro19a167a2011-01-17 01:35:23 -0500762 return 0;
David Howellsea5b7782011-01-14 19:10:03 +0000763 case 0:
Al Viro8aef1882011-06-16 15:10:06 +0100764 path_put(path);
David Howellsea5b7782011-01-14 19:10:03 +0000765 path->mnt = mnt;
766 path->dentry = dget(mnt->mnt_root);
David Howellsea5b7782011-01-14 19:10:03 +0000767 return 0;
Al Viro19a167a2011-01-17 01:35:23 -0500768 default:
769 return err;
David Howellsea5b7782011-01-14 19:10:03 +0000770 }
Al Viro19a167a2011-01-17 01:35:23 -0500771
David Howells9875cf82011-01-14 18:45:21 +0000772}
773
774/*
775 * Handle a dentry that is managed in some way.
David Howellscc53ce52011-01-14 18:45:26 +0000776 * - Flagged for transit management (autofs)
David Howells9875cf82011-01-14 18:45:21 +0000777 * - Flagged as mountpoint
778 * - Flagged as automount point
779 *
780 * This may only be called in refwalk mode.
781 *
782 * Serialization is taken care of in namespace.c
783 */
784static int follow_managed(struct path *path, unsigned flags)
785{
Al Viro8aef1882011-06-16 15:10:06 +0100786 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
David Howells9875cf82011-01-14 18:45:21 +0000787 unsigned managed;
788 bool need_mntput = false;
Al Viro8aef1882011-06-16 15:10:06 +0100789 int ret = 0;
David Howells9875cf82011-01-14 18:45:21 +0000790
791 /* Given that we're not holding a lock here, we retain the value in a
792 * local variable for each dentry as we look at it so that we don't see
793 * the components of that value change under us */
794 while (managed = ACCESS_ONCE(path->dentry->d_flags),
795 managed &= DCACHE_MANAGED_DENTRY,
796 unlikely(managed != 0)) {
David Howellscc53ce52011-01-14 18:45:26 +0000797 /* Allow the filesystem to manage the transit without i_mutex
798 * being held. */
799 if (managed & DCACHE_MANAGE_TRANSIT) {
800 BUG_ON(!path->dentry->d_op);
801 BUG_ON(!path->dentry->d_op->d_manage);
Al Viro1aed3e42011-03-18 09:09:02 -0400802 ret = path->dentry->d_op->d_manage(path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +0000803 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +0100804 break;
David Howellscc53ce52011-01-14 18:45:26 +0000805 }
806
David Howells9875cf82011-01-14 18:45:21 +0000807 /* Transit to a mounted filesystem. */
808 if (managed & DCACHE_MOUNTED) {
809 struct vfsmount *mounted = lookup_mnt(path);
810 if (mounted) {
811 dput(path->dentry);
812 if (need_mntput)
813 mntput(path->mnt);
814 path->mnt = mounted;
815 path->dentry = dget(mounted->mnt_root);
816 need_mntput = true;
817 continue;
818 }
819
820 /* Something is mounted on this dentry in another
821 * namespace and/or whatever was mounted there in this
822 * namespace got unmounted before we managed to get the
823 * vfsmount_lock */
824 }
825
826 /* Handle an automount point */
827 if (managed & DCACHE_NEED_AUTOMOUNT) {
828 ret = follow_automount(path, flags, &need_mntput);
829 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +0100830 break;
David Howells9875cf82011-01-14 18:45:21 +0000831 continue;
832 }
833
834 /* We didn't change the current path point */
835 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
Al Viro8aef1882011-06-16 15:10:06 +0100837
838 if (need_mntput && path->mnt == mnt)
839 mntput(path->mnt);
840 if (ret == -EISDIR)
841 ret = 0;
Al Viroa3fbbde2011-11-07 21:21:26 +0000842 return ret < 0 ? ret : need_mntput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843}
844
David Howellscc53ce52011-01-14 18:45:26 +0000845int follow_down_one(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
847 struct vfsmount *mounted;
848
Al Viro1c755af2009-04-18 14:06:57 -0400849 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -0400851 dput(path->dentry);
852 mntput(path->mnt);
853 path->mnt = mounted;
854 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 return 1;
856 }
857 return 0;
858}
859
Ian Kent62a73752011-03-25 01:51:02 +0800860static inline bool managed_dentry_might_block(struct dentry *dentry)
861{
862 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
863 dentry->d_op->d_manage(dentry, true) < 0);
864}
865
David Howells9875cf82011-01-14 18:45:21 +0000866/*
Al Viro287548e2011-05-27 06:50:06 -0400867 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
868 * we meet a managed dentry that would need blocking.
David Howells9875cf82011-01-14 18:45:21 +0000869 */
870static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
Al Viro287548e2011-05-27 06:50:06 -0400871 struct inode **inode)
David Howells9875cf82011-01-14 18:45:21 +0000872{
Ian Kent62a73752011-03-25 01:51:02 +0800873 for (;;) {
Al Viroc7105362011-11-24 18:22:03 -0500874 struct mount *mounted;
Ian Kent62a73752011-03-25 01:51:02 +0800875 /*
876 * Don't forget we might have a non-mountpoint managed dentry
877 * that wants to block transit.
878 */
Al Viro287548e2011-05-27 06:50:06 -0400879 if (unlikely(managed_dentry_might_block(path->dentry)))
David Howellsab909112011-01-14 18:46:51 +0000880 return false;
Ian Kent62a73752011-03-25 01:51:02 +0800881
882 if (!d_mountpoint(path->dentry))
883 break;
884
David Howells9875cf82011-01-14 18:45:21 +0000885 mounted = __lookup_mnt(path->mnt, path->dentry, 1);
886 if (!mounted)
887 break;
Al Viroc7105362011-11-24 18:22:03 -0500888 path->mnt = &mounted->mnt;
889 path->dentry = mounted->mnt.mnt_root;
Al Viroa3fbbde2011-11-07 21:21:26 +0000890 nd->flags |= LOOKUP_JUMPED;
David Howells9875cf82011-01-14 18:45:21 +0000891 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
Linus Torvalds59430262011-07-18 15:43:29 -0700892 /*
893 * Update the inode too. We don't need to re-check the
894 * dentry sequence number here after this d_inode read,
895 * because a mount-point is always pinned.
896 */
897 *inode = path->dentry->d_inode;
David Howells9875cf82011-01-14 18:45:21 +0000898 }
David Howells9875cf82011-01-14 18:45:21 +0000899 return true;
900}
901
Al Virodea39372011-05-27 06:53:39 -0400902static void follow_mount_rcu(struct nameidata *nd)
Al Viro287548e2011-05-27 06:50:06 -0400903{
Al Virodea39372011-05-27 06:53:39 -0400904 while (d_mountpoint(nd->path.dentry)) {
Al Viroc7105362011-11-24 18:22:03 -0500905 struct mount *mounted;
Al Virodea39372011-05-27 06:53:39 -0400906 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
Al Viro287548e2011-05-27 06:50:06 -0400907 if (!mounted)
908 break;
Al Viroc7105362011-11-24 18:22:03 -0500909 nd->path.mnt = &mounted->mnt;
910 nd->path.dentry = mounted->mnt.mnt_root;
Al Virodea39372011-05-27 06:53:39 -0400911 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro287548e2011-05-27 06:50:06 -0400912 }
913}
914
Nick Piggin31e6b012011-01-07 17:49:52 +1100915static int follow_dotdot_rcu(struct nameidata *nd)
916{
Nick Piggin31e6b012011-01-07 17:49:52 +1100917 set_root_rcu(nd);
918
David Howells9875cf82011-01-14 18:45:21 +0000919 while (1) {
Nick Piggin31e6b012011-01-07 17:49:52 +1100920 if (nd->path.dentry == nd->root.dentry &&
921 nd->path.mnt == nd->root.mnt) {
922 break;
923 }
924 if (nd->path.dentry != nd->path.mnt->mnt_root) {
925 struct dentry *old = nd->path.dentry;
926 struct dentry *parent = old->d_parent;
927 unsigned seq;
928
929 seq = read_seqcount_begin(&parent->d_seq);
930 if (read_seqcount_retry(&old->d_seq, nd->seq))
Al Viroef7562d2011-03-04 14:35:59 -0500931 goto failed;
Nick Piggin31e6b012011-01-07 17:49:52 +1100932 nd->path.dentry = parent;
933 nd->seq = seq;
934 break;
935 }
936 if (!follow_up_rcu(&nd->path))
937 break;
938 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Nick Piggin31e6b012011-01-07 17:49:52 +1100939 }
Al Virodea39372011-05-27 06:53:39 -0400940 follow_mount_rcu(nd);
941 nd->inode = nd->path.dentry->d_inode;
Nick Piggin31e6b012011-01-07 17:49:52 +1100942 return 0;
Al Viroef7562d2011-03-04 14:35:59 -0500943
944failed:
945 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -0500946 if (!(nd->flags & LOOKUP_ROOT))
947 nd->root.mnt = NULL;
Al Viroef7562d2011-03-04 14:35:59 -0500948 rcu_read_unlock();
949 br_read_unlock(vfsmount_lock);
950 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +1100951}
952
David Howells9875cf82011-01-14 18:45:21 +0000953/*
David Howellscc53ce52011-01-14 18:45:26 +0000954 * Follow down to the covering mount currently visible to userspace. At each
955 * point, the filesystem owning that dentry may be queried as to whether the
956 * caller is permitted to proceed or not.
David Howellscc53ce52011-01-14 18:45:26 +0000957 */
Al Viro7cc90cc2011-03-18 09:04:20 -0400958int follow_down(struct path *path)
David Howellscc53ce52011-01-14 18:45:26 +0000959{
960 unsigned managed;
961 int ret;
962
963 while (managed = ACCESS_ONCE(path->dentry->d_flags),
964 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
965 /* Allow the filesystem to manage the transit without i_mutex
966 * being held.
967 *
968 * We indicate to the filesystem if someone is trying to mount
969 * something here. This gives autofs the chance to deny anyone
970 * other than its daemon the right to mount on its
971 * superstructure.
972 *
973 * The filesystem may sleep at this point.
974 */
975 if (managed & DCACHE_MANAGE_TRANSIT) {
976 BUG_ON(!path->dentry->d_op);
977 BUG_ON(!path->dentry->d_op->d_manage);
David Howellsab909112011-01-14 18:46:51 +0000978 ret = path->dentry->d_op->d_manage(
Al Viro1aed3e42011-03-18 09:09:02 -0400979 path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +0000980 if (ret < 0)
981 return ret == -EISDIR ? 0 : ret;
982 }
983
984 /* Transit to a mounted filesystem. */
985 if (managed & DCACHE_MOUNTED) {
986 struct vfsmount *mounted = lookup_mnt(path);
987 if (!mounted)
988 break;
989 dput(path->dentry);
990 mntput(path->mnt);
991 path->mnt = mounted;
992 path->dentry = dget(mounted->mnt_root);
993 continue;
994 }
995
996 /* Don't handle automount points here */
997 break;
998 }
999 return 0;
1000}
1001
1002/*
David Howells9875cf82011-01-14 18:45:21 +00001003 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1004 */
1005static void follow_mount(struct path *path)
1006{
1007 while (d_mountpoint(path->dentry)) {
1008 struct vfsmount *mounted = lookup_mnt(path);
1009 if (!mounted)
1010 break;
1011 dput(path->dentry);
1012 mntput(path->mnt);
1013 path->mnt = mounted;
1014 path->dentry = dget(mounted->mnt_root);
1015 }
1016}
1017
Nick Piggin31e6b012011-01-07 17:49:52 +11001018static void follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
Al Viro2a737872009-04-07 11:49:53 -04001020 set_root(nd);
Andreas Mohre518ddb2006-09-29 02:01:22 -07001021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 while(1) {
Jan Blunck4ac91372008-02-14 19:34:32 -08001023 struct dentry *old = nd->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Al Viro2a737872009-04-07 11:49:53 -04001025 if (nd->path.dentry == nd->root.dentry &&
1026 nd->path.mnt == nd->root.mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 break;
1028 }
Jan Blunck4ac91372008-02-14 19:34:32 -08001029 if (nd->path.dentry != nd->path.mnt->mnt_root) {
Al Viro3088dd72010-01-30 15:47:29 -05001030 /* rare case of legitimate dget_parent()... */
1031 nd->path.dentry = dget_parent(nd->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 dput(old);
1033 break;
1034 }
Al Viro3088dd72010-01-30 15:47:29 -05001035 if (!follow_up(&nd->path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
Al Viro79ed0222009-04-18 13:59:41 -04001038 follow_mount(&nd->path);
Nick Piggin31e6b012011-01-07 17:49:52 +11001039 nd->inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040}
1041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042/*
Miklos Szeredibad61182012-03-26 12:54:24 +02001043 * This looks up the name in dcache, possibly revalidates the old dentry and
1044 * allocates a new one if not found or not valid. In the need_lookup argument
1045 * returns whether i_op->lookup is necessary.
1046 *
1047 * dir->d_inode->i_mutex must be held
Nick Pigginbaa03892010-08-18 04:37:31 +10001048 */
Miklos Szeredibad61182012-03-26 12:54:24 +02001049static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
1050 struct nameidata *nd, bool *need_lookup)
Nick Pigginbaa03892010-08-18 04:37:31 +10001051{
Nick Pigginbaa03892010-08-18 04:37:31 +10001052 struct dentry *dentry;
Miklos Szeredibad61182012-03-26 12:54:24 +02001053 int error;
Nick Pigginbaa03892010-08-18 04:37:31 +10001054
Miklos Szeredibad61182012-03-26 12:54:24 +02001055 *need_lookup = false;
1056 dentry = d_lookup(dir, name);
1057 if (dentry) {
1058 if (d_need_lookup(dentry)) {
1059 *need_lookup = true;
1060 } else if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
1061 error = d_revalidate(dentry, nd);
1062 if (unlikely(error <= 0)) {
1063 if (error < 0) {
1064 dput(dentry);
1065 return ERR_PTR(error);
1066 } else if (!d_invalidate(dentry)) {
1067 dput(dentry);
1068 dentry = NULL;
1069 }
1070 }
1071 }
1072 }
Nick Pigginbaa03892010-08-18 04:37:31 +10001073
Miklos Szeredibad61182012-03-26 12:54:24 +02001074 if (!dentry) {
1075 dentry = d_alloc(dir, name);
1076 if (unlikely(!dentry))
1077 return ERR_PTR(-ENOMEM);
Nick Pigginbaa03892010-08-18 04:37:31 +10001078
Miklos Szeredibad61182012-03-26 12:54:24 +02001079 *need_lookup = true;
Nick Pigginbaa03892010-08-18 04:37:31 +10001080 }
1081 return dentry;
1082}
1083
1084/*
Miklos Szeredibad61182012-03-26 12:54:24 +02001085 * Call i_op->lookup on the dentry. The dentry must be negative but may be
1086 * hashed if it was pouplated with DCACHE_NEED_LOOKUP.
1087 *
1088 * dir->d_inode->i_mutex must be held
Josef Bacik44396f42011-05-31 11:58:49 -04001089 */
Miklos Szeredibad61182012-03-26 12:54:24 +02001090static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
1091 struct nameidata *nd)
Josef Bacik44396f42011-05-31 11:58:49 -04001092{
Josef Bacik44396f42011-05-31 11:58:49 -04001093 struct dentry *old;
1094
1095 /* Don't create child dentry for a dead directory. */
Miklos Szeredibad61182012-03-26 12:54:24 +02001096 if (unlikely(IS_DEADDIR(dir))) {
Miklos Szeredie188dc02012-02-03 14:25:18 +01001097 dput(dentry);
Josef Bacik44396f42011-05-31 11:58:49 -04001098 return ERR_PTR(-ENOENT);
Miklos Szeredie188dc02012-02-03 14:25:18 +01001099 }
Josef Bacik44396f42011-05-31 11:58:49 -04001100
Miklos Szeredibad61182012-03-26 12:54:24 +02001101 old = dir->i_op->lookup(dir, dentry, nd);
Josef Bacik44396f42011-05-31 11:58:49 -04001102 if (unlikely(old)) {
1103 dput(dentry);
1104 dentry = old;
1105 }
1106 return dentry;
1107}
1108
Al Viroa3255542012-03-30 14:41:51 -04001109static struct dentry *__lookup_hash(struct qstr *name,
1110 struct dentry *base, struct nameidata *nd)
1111{
Miklos Szeredibad61182012-03-26 12:54:24 +02001112 bool need_lookup;
Al Viroa3255542012-03-30 14:41:51 -04001113 struct dentry *dentry;
1114
Miklos Szeredibad61182012-03-26 12:54:24 +02001115 dentry = lookup_dcache(name, base, nd, &need_lookup);
1116 if (!need_lookup)
1117 return dentry;
Al Viroa3255542012-03-30 14:41:51 -04001118
Miklos Szeredibad61182012-03-26 12:54:24 +02001119 return lookup_real(base->d_inode, dentry, nd);
Al Viroa3255542012-03-30 14:41:51 -04001120}
1121
Josef Bacik44396f42011-05-31 11:58:49 -04001122/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 * It's more convoluted than I'd like it to be, but... it's still fairly
1124 * small and for now I'd prefer to have fast path as straight as possible.
1125 * It _is_ time-critical.
1126 */
1127static int do_lookup(struct nameidata *nd, struct qstr *name,
Nick Piggin31e6b012011-01-07 17:49:52 +11001128 struct path *path, struct inode **inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129{
Jan Blunck4ac91372008-02-14 19:34:32 -08001130 struct vfsmount *mnt = nd->path.mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +11001131 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro5a18fff2011-03-11 04:44:53 -05001132 int need_reval = 1;
1133 int status = 1;
David Howells9875cf82011-01-14 18:45:21 +00001134 int err;
1135
Al Viro3cac2602009-08-13 18:27:43 +04001136 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001137 * Rename seqlock is not required here because in the off chance
1138 * of a false negative due to a concurrent rename, we're going to
1139 * do the non-racy lookup, below.
1140 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001141 if (nd->flags & LOOKUP_RCU) {
1142 unsigned seq;
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001143 dentry = __d_lookup_rcu(parent, name, &seq, nd->inode);
Al Viro5a18fff2011-03-11 04:44:53 -05001144 if (!dentry)
1145 goto unlazy;
1146
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001147 /*
1148 * This sequence count validates that the inode matches
1149 * the dentry name information from lookup.
1150 */
1151 *inode = dentry->d_inode;
1152 if (read_seqcount_retry(&dentry->d_seq, seq))
1153 return -ECHILD;
1154
1155 /*
1156 * This sequence count validates that the parent had no
1157 * changes while we did the lookup of the dentry above.
1158 *
1159 * The memory barrier in read_seqcount_begin of child is
1160 * enough, we can use __read_seqcount_retry here.
1161 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001162 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1163 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001164 nd->seq = seq;
Al Viro5a18fff2011-03-11 04:44:53 -05001165
Miklos Szeredifa4ee152012-03-26 12:54:19 +02001166 if (unlikely(d_need_lookup(dentry)))
1167 goto unlazy;
Al Viro24643082011-02-15 01:26:22 -05001168 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
Al Viro5a18fff2011-03-11 04:44:53 -05001169 status = d_revalidate(dentry, nd);
1170 if (unlikely(status <= 0)) {
1171 if (status != -ECHILD)
1172 need_reval = 0;
1173 goto unlazy;
1174 }
Al Viro24643082011-02-15 01:26:22 -05001175 }
Nick Piggin31e6b012011-01-07 17:49:52 +11001176 path->mnt = mnt;
1177 path->dentry = dentry;
Al Virod6e9bd22011-05-27 07:03:15 -04001178 if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1179 goto unlazy;
1180 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1181 goto unlazy;
1182 return 0;
Al Viro5a18fff2011-03-11 04:44:53 -05001183unlazy:
Al Viro19660af2011-03-25 10:32:48 -04001184 if (unlazy_walk(nd, dentry))
1185 return -ECHILD;
Al Viro5a18fff2011-03-11 04:44:53 -05001186 } else {
1187 dentry = __d_lookup(parent, name);
Nick Piggin31e6b012011-01-07 17:49:52 +11001188 }
Al Viro5a18fff2011-03-11 04:44:53 -05001189
Al Viro81e6f522012-03-30 14:48:04 -04001190 if (unlikely(!dentry))
1191 goto need_lookup;
Al Viro5a18fff2011-03-11 04:44:53 -05001192
Al Viro81e6f522012-03-30 14:48:04 -04001193 if (unlikely(d_need_lookup(dentry))) {
1194 dput(dentry);
1195 goto need_lookup;
Al Viro24643082011-02-15 01:26:22 -05001196 }
Al Viro81e6f522012-03-30 14:48:04 -04001197
Al Viro5a18fff2011-03-11 04:44:53 -05001198 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
1199 status = d_revalidate(dentry, nd);
1200 if (unlikely(status <= 0)) {
1201 if (status < 0) {
1202 dput(dentry);
1203 return status;
1204 }
1205 if (!d_invalidate(dentry)) {
1206 dput(dentry);
Al Viro81e6f522012-03-30 14:48:04 -04001207 goto need_lookup;
Al Viro5a18fff2011-03-11 04:44:53 -05001208 }
1209 }
Al Viro3f6c7c72012-03-30 14:04:16 -04001210done:
David Howells9875cf82011-01-14 18:45:21 +00001211 path->mnt = mnt;
1212 path->dentry = dentry;
1213 err = follow_managed(path, nd->flags);
Ian Kent89312212011-01-18 12:06:10 +08001214 if (unlikely(err < 0)) {
1215 path_put_conditional(path, nd);
David Howells9875cf82011-01-14 18:45:21 +00001216 return err;
Ian Kent89312212011-01-18 12:06:10 +08001217 }
Al Viroa3fbbde2011-11-07 21:21:26 +00001218 if (err)
1219 nd->flags |= LOOKUP_JUMPED;
David Howells9875cf82011-01-14 18:45:21 +00001220 *inode = path->dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 return 0;
Al Viro81e6f522012-03-30 14:48:04 -04001222
1223need_lookup:
1224 BUG_ON(nd->inode != parent->d_inode);
1225
1226 mutex_lock(&parent->d_inode->i_mutex);
1227 dentry = __lookup_hash(name, parent, nd);
1228 mutex_unlock(&parent->d_inode->i_mutex);
1229 if (IS_ERR(dentry))
1230 return PTR_ERR(dentry);
1231 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232}
1233
Al Viro52094c82011-02-21 21:34:47 -05001234static inline int may_lookup(struct nameidata *nd)
1235{
1236 if (nd->flags & LOOKUP_RCU) {
Al Viro4ad5abb2011-06-20 19:57:03 -04001237 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
Al Viro52094c82011-02-21 21:34:47 -05001238 if (err != -ECHILD)
1239 return err;
Al Viro19660af2011-03-25 10:32:48 -04001240 if (unlazy_walk(nd, NULL))
Al Viro52094c82011-02-21 21:34:47 -05001241 return -ECHILD;
1242 }
Al Viro4ad5abb2011-06-20 19:57:03 -04001243 return inode_permission(nd->inode, MAY_EXEC);
Al Viro52094c82011-02-21 21:34:47 -05001244}
1245
Al Viro9856fa12011-03-04 14:22:06 -05001246static inline int handle_dots(struct nameidata *nd, int type)
1247{
1248 if (type == LAST_DOTDOT) {
1249 if (nd->flags & LOOKUP_RCU) {
1250 if (follow_dotdot_rcu(nd))
1251 return -ECHILD;
1252 } else
1253 follow_dotdot(nd);
1254 }
1255 return 0;
1256}
1257
Al Viro951361f2011-03-04 14:44:37 -05001258static void terminate_walk(struct nameidata *nd)
1259{
1260 if (!(nd->flags & LOOKUP_RCU)) {
1261 path_put(&nd->path);
1262 } else {
1263 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001264 if (!(nd->flags & LOOKUP_ROOT))
1265 nd->root.mnt = NULL;
Al Viro951361f2011-03-04 14:44:37 -05001266 rcu_read_unlock();
1267 br_read_unlock(vfsmount_lock);
1268 }
1269}
1270
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001271/*
1272 * Do we need to follow links? We _really_ want to be able
1273 * to do this check without having to look at inode->i_op,
1274 * so we keep a cache of "no, this doesn't need follow_link"
1275 * for the common case.
1276 */
Linus Torvalds7813b942011-08-07 09:53:20 -07001277static inline int should_follow_link(struct inode *inode, int follow)
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001278{
1279 if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
1280 if (likely(inode->i_op->follow_link))
1281 return follow;
1282
1283 /* This gets set once for the inode lifetime */
1284 spin_lock(&inode->i_lock);
1285 inode->i_opflags |= IOP_NOFOLLOW;
1286 spin_unlock(&inode->i_lock);
1287 }
1288 return 0;
1289}
1290
Al Viroce57dfc2011-03-13 19:58:58 -04001291static inline int walk_component(struct nameidata *nd, struct path *path,
1292 struct qstr *name, int type, int follow)
1293{
1294 struct inode *inode;
1295 int err;
1296 /*
1297 * "." and ".." are special - ".." especially so because it has
1298 * to be able to know about the current root directory and
1299 * parent relationships.
1300 */
1301 if (unlikely(type != LAST_NORM))
1302 return handle_dots(nd, type);
1303 err = do_lookup(nd, name, path, &inode);
1304 if (unlikely(err)) {
1305 terminate_walk(nd);
1306 return err;
1307 }
1308 if (!inode) {
1309 path_to_nameidata(path, nd);
1310 terminate_walk(nd);
1311 return -ENOENT;
1312 }
Linus Torvalds7813b942011-08-07 09:53:20 -07001313 if (should_follow_link(inode, follow)) {
Al Viro19660af2011-03-25 10:32:48 -04001314 if (nd->flags & LOOKUP_RCU) {
1315 if (unlikely(unlazy_walk(nd, path->dentry))) {
1316 terminate_walk(nd);
1317 return -ECHILD;
1318 }
1319 }
Al Viroce57dfc2011-03-13 19:58:58 -04001320 BUG_ON(inode != path->dentry->d_inode);
1321 return 1;
1322 }
1323 path_to_nameidata(path, nd);
1324 nd->inode = inode;
1325 return 0;
1326}
1327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328/*
Al Virob3563792011-03-14 21:54:55 -04001329 * This limits recursive symlink follows to 8, while
1330 * limiting consecutive symlinks to 40.
1331 *
1332 * Without that kind of total limit, nasty chains of consecutive
1333 * symlinks can cause almost arbitrarily long lookups.
1334 */
1335static inline int nested_symlink(struct path *path, struct nameidata *nd)
1336{
1337 int res;
1338
Al Virob3563792011-03-14 21:54:55 -04001339 if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1340 path_put_conditional(path, nd);
1341 path_put(&nd->path);
1342 return -ELOOP;
1343 }
Erez Zadok1a4022f2011-05-21 01:19:59 -04001344 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
Al Virob3563792011-03-14 21:54:55 -04001345
1346 nd->depth++;
1347 current->link_count++;
1348
1349 do {
1350 struct path link = *path;
1351 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04001352
1353 res = follow_link(&link, nd, &cookie);
Al Virob3563792011-03-14 21:54:55 -04001354 if (!res)
1355 res = walk_component(nd, path, &nd->last,
1356 nd->last_type, LOOKUP_FOLLOW);
Al Viro574197e2011-03-14 22:20:34 -04001357 put_link(nd, &link, cookie);
Al Virob3563792011-03-14 21:54:55 -04001358 } while (res > 0);
1359
1360 current->link_count--;
1361 nd->depth--;
1362 return res;
1363}
1364
1365/*
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001366 * We really don't want to look at inode->i_op->lookup
1367 * when we don't have to. So we keep a cache bit in
1368 * the inode ->i_opflags field that says "yes, we can
1369 * do lookup on this inode".
1370 */
1371static inline int can_lookup(struct inode *inode)
1372{
1373 if (likely(inode->i_opflags & IOP_LOOKUP))
1374 return 1;
1375 if (likely(!inode->i_op->lookup))
1376 return 0;
1377
1378 /* We do this once for the lifetime of the inode */
1379 spin_lock(&inode->i_lock);
1380 inode->i_opflags |= IOP_LOOKUP;
1381 spin_unlock(&inode->i_lock);
1382 return 1;
1383}
1384
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001385/*
1386 * We can do the critical dentry name comparison and hashing
1387 * operations one word at a time, but we are limited to:
1388 *
1389 * - Architectures with fast unaligned word accesses. We could
1390 * do a "get_unaligned()" if this helps and is sufficiently
1391 * fast.
1392 *
1393 * - Little-endian machines (so that we can generate the mask
1394 * of low bytes efficiently). Again, we *could* do a byte
1395 * swapping load on big-endian architectures if that is not
1396 * expensive enough to make the optimization worthless.
1397 *
1398 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1399 * do not trap on the (extremely unlikely) case of a page
1400 * crossing operation.
1401 *
1402 * - Furthermore, we need an efficient 64-bit compile for the
1403 * 64-bit case in order to generate the "number of bytes in
1404 * the final mask". Again, that could be replaced with a
1405 * efficient population count instruction or similar.
1406 */
1407#ifdef CONFIG_DCACHE_WORD_ACCESS
1408
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001409#include <asm/word-at-a-time.h>
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001410
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001411#ifdef CONFIG_64BIT
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001412
1413static inline unsigned int fold_hash(unsigned long hash)
1414{
1415 hash += hash >> (8*sizeof(int));
1416 return hash;
1417}
1418
1419#else /* 32-bit case */
1420
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001421#define fold_hash(x) (x)
1422
1423#endif
1424
1425unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1426{
1427 unsigned long a, mask;
1428 unsigned long hash = 0;
1429
1430 for (;;) {
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001431 a = load_unaligned_zeropad(name);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001432 if (len < sizeof(unsigned long))
1433 break;
1434 hash += a;
Al Virof132c5b2012-03-22 21:59:52 +00001435 hash *= 9;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001436 name += sizeof(unsigned long);
1437 len -= sizeof(unsigned long);
1438 if (!len)
1439 goto done;
1440 }
1441 mask = ~(~0ul << len*8);
1442 hash += mask & a;
1443done:
1444 return fold_hash(hash);
1445}
1446EXPORT_SYMBOL(full_name_hash);
1447
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001448/*
1449 * Calculate the length and hash of the path component, and
1450 * return the length of the component;
1451 */
1452static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1453{
1454 unsigned long a, mask, hash, len;
1455
1456 hash = a = 0;
1457 len = -sizeof(unsigned long);
1458 do {
1459 hash = (hash + a) * 9;
1460 len += sizeof(unsigned long);
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001461 a = load_unaligned_zeropad(name+len);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001462 /* Do we have any NUL or '/' bytes in this word? */
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001463 mask = has_zero(a) | has_zero(a ^ REPEAT_BYTE('/'));
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001464 } while (!mask);
1465
1466 /* The mask *below* the first high bit set */
1467 mask = (mask - 1) & ~mask;
1468 mask >>= 7;
1469 hash += a & mask;
1470 *hashp = fold_hash(hash);
1471
1472 return len + count_masked_bytes(mask);
1473}
1474
1475#else
1476
Linus Torvalds0145acc2012-03-02 14:32:59 -08001477unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1478{
1479 unsigned long hash = init_name_hash();
1480 while (len--)
1481 hash = partial_name_hash(*name++, hash);
1482 return end_name_hash(hash);
1483}
Linus Torvaldsae942ae2012-03-02 19:40:57 -08001484EXPORT_SYMBOL(full_name_hash);
Linus Torvalds0145acc2012-03-02 14:32:59 -08001485
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001486/*
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001487 * We know there's a real path component here of at least
1488 * one character.
1489 */
1490static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1491{
1492 unsigned long hash = init_name_hash();
1493 unsigned long len = 0, c;
1494
1495 c = (unsigned char)*name;
1496 do {
1497 len++;
1498 hash = partial_name_hash(c, hash);
1499 c = (unsigned char)name[len];
1500 } while (c && c != '/');
1501 *hashp = end_name_hash(hash);
1502 return len;
1503}
1504
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001505#endif
1506
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001507/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01001509 * This is the basic name resolution function, turning a pathname into
1510 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01001512 * Returns 0 and nd will have valid dentry and mnt on success.
1513 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 */
Al Viro6de88d72009-08-09 01:41:57 +04001515static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
1517 struct path next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
1520 while (*name=='/')
1521 name++;
1522 if (!*name)
Al Viro086e1832011-02-22 20:56:27 -05001523 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 /* At this point we know we have a real path component. */
1526 for(;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 struct qstr this;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001528 long len;
Al Virofe479a52011-02-22 15:10:03 -05001529 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Al Viro52094c82011-02-21 21:34:47 -05001531 err = may_lookup(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 if (err)
1533 break;
1534
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001535 len = hash_name(name, &this.hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 this.name = name;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001537 this.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
Al Virofe479a52011-02-22 15:10:03 -05001539 type = LAST_NORM;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001540 if (name[0] == '.') switch (len) {
Al Virofe479a52011-02-22 15:10:03 -05001541 case 2:
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001542 if (name[1] == '.') {
Al Virofe479a52011-02-22 15:10:03 -05001543 type = LAST_DOTDOT;
Al Viro16c2cd72011-02-22 15:50:10 -05001544 nd->flags |= LOOKUP_JUMPED;
1545 }
Al Virofe479a52011-02-22 15:10:03 -05001546 break;
1547 case 1:
1548 type = LAST_DOT;
1549 }
Al Viro5a202bc2011-03-08 14:17:44 -05001550 if (likely(type == LAST_NORM)) {
1551 struct dentry *parent = nd->path.dentry;
Al Viro16c2cd72011-02-22 15:50:10 -05001552 nd->flags &= ~LOOKUP_JUMPED;
Al Viro5a202bc2011-03-08 14:17:44 -05001553 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1554 err = parent->d_op->d_hash(parent, nd->inode,
1555 &this);
1556 if (err < 0)
1557 break;
1558 }
1559 }
Al Virofe479a52011-02-22 15:10:03 -05001560
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001561 if (!name[len])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 goto last_component;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001563 /*
1564 * If it wasn't NUL, we know it was '/'. Skip that
1565 * slash, and continue until no more slashes.
1566 */
1567 do {
1568 len++;
1569 } while (unlikely(name[len] == '/'));
1570 if (!name[len])
Al Virob3563792011-03-14 21:54:55 -04001571 goto last_component;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001572 name += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
Al Viroce57dfc2011-03-13 19:58:58 -04001574 err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1575 if (err < 0)
1576 return err;
Al Virofe479a52011-02-22 15:10:03 -05001577
Al Viroce57dfc2011-03-13 19:58:58 -04001578 if (err) {
Al Virob3563792011-03-14 21:54:55 -04001579 err = nested_symlink(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 if (err)
Al Viroa7472ba2011-03-04 14:39:30 -05001581 return err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001582 }
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001583 if (can_lookup(nd->inode))
1584 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 err = -ENOTDIR;
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001586 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 /* here ends the main loop */
1588
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589last_component:
Al Virob3563792011-03-14 21:54:55 -04001590 nd->last = this;
1591 nd->last_type = type;
Al Viro086e1832011-02-22 20:56:27 -05001592 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 }
Al Viro951361f2011-03-04 14:44:37 -05001594 terminate_walk(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 return err;
1596}
1597
Al Viro70e9b352011-03-05 21:12:22 -05001598static int path_init(int dfd, const char *name, unsigned int flags,
1599 struct nameidata *nd, struct file **fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600{
Prasanna Medaea3834d2005-04-29 16:00:17 +01001601 int retval = 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001602 int fput_needed;
1603 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
1605 nd->last_type = LAST_ROOT; /* if there are only slashes... */
Al Viro16c2cd72011-02-22 15:50:10 -05001606 nd->flags = flags | LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 nd->depth = 0;
Al Viro5b6ca022011-03-09 23:04:47 -05001608 if (flags & LOOKUP_ROOT) {
1609 struct inode *inode = nd->root.dentry->d_inode;
Al Viro73d049a2011-03-11 12:08:24 -05001610 if (*name) {
1611 if (!inode->i_op->lookup)
1612 return -ENOTDIR;
1613 retval = inode_permission(inode, MAY_EXEC);
1614 if (retval)
1615 return retval;
1616 }
Al Viro5b6ca022011-03-09 23:04:47 -05001617 nd->path = nd->root;
1618 nd->inode = inode;
1619 if (flags & LOOKUP_RCU) {
1620 br_read_lock(vfsmount_lock);
1621 rcu_read_lock();
1622 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1623 } else {
1624 path_get(&nd->path);
1625 }
1626 return 0;
1627 }
1628
Al Viro2a737872009-04-07 11:49:53 -04001629 nd->root.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 if (*name=='/') {
Al Viroe41f7d42011-02-22 14:02:58 -05001632 if (flags & LOOKUP_RCU) {
1633 br_read_lock(vfsmount_lock);
1634 rcu_read_lock();
1635 set_root_rcu(nd);
1636 } else {
1637 set_root(nd);
1638 path_get(&nd->root);
1639 }
Al Viro2a737872009-04-07 11:49:53 -04001640 nd->path = nd->root;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001641 } else if (dfd == AT_FDCWD) {
Al Viroe41f7d42011-02-22 14:02:58 -05001642 if (flags & LOOKUP_RCU) {
1643 struct fs_struct *fs = current->fs;
1644 unsigned seq;
1645
1646 br_read_lock(vfsmount_lock);
1647 rcu_read_lock();
1648
1649 do {
1650 seq = read_seqcount_begin(&fs->seq);
1651 nd->path = fs->pwd;
1652 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1653 } while (read_seqcount_retry(&fs->seq, seq));
1654 } else {
1655 get_fs_pwd(current->fs, &nd->path);
1656 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001657 } else {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001658 struct dentry *dentry;
1659
Al Viro1abf0c72011-03-13 03:51:11 -04001660 file = fget_raw_light(dfd, &fput_needed);
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001661 retval = -EBADF;
1662 if (!file)
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001663 goto out_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001664
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001665 dentry = file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001666
Al Virof52e0c12011-03-14 18:56:51 -04001667 if (*name) {
1668 retval = -ENOTDIR;
1669 if (!S_ISDIR(dentry->d_inode->i_mode))
1670 goto fput_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001671
Al Viro4ad5abb2011-06-20 19:57:03 -04001672 retval = inode_permission(dentry->d_inode, MAY_EXEC);
Al Virof52e0c12011-03-14 18:56:51 -04001673 if (retval)
1674 goto fput_fail;
1675 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001676
Jan Blunck5dd784d02008-02-14 19:34:38 -08001677 nd->path = file->f_path;
Al Viroe41f7d42011-02-22 14:02:58 -05001678 if (flags & LOOKUP_RCU) {
1679 if (fput_needed)
Al Viro70e9b352011-03-05 21:12:22 -05001680 *fp = file;
Al Viroe41f7d42011-02-22 14:02:58 -05001681 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1682 br_read_lock(vfsmount_lock);
1683 rcu_read_lock();
1684 } else {
1685 path_get(&file->f_path);
1686 fput_light(file, fput_needed);
1687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 }
Al Viroe41f7d42011-02-22 14:02:58 -05001689
Nick Piggin31e6b012011-01-07 17:49:52 +11001690 nd->inode = nd->path.dentry->d_inode;
Al Viro9b4a9b12009-04-07 11:44:16 -04001691 return 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001692
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001693fput_fail:
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001694 fput_light(file, fput_needed);
Al Viro9b4a9b12009-04-07 11:44:16 -04001695out_fail:
1696 return retval;
1697}
1698
Al Virobd92d7f2011-03-14 19:54:59 -04001699static inline int lookup_last(struct nameidata *nd, struct path *path)
1700{
1701 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1702 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1703
1704 nd->flags &= ~LOOKUP_PARENT;
1705 return walk_component(nd, path, &nd->last, nd->last_type,
1706 nd->flags & LOOKUP_FOLLOW);
1707}
1708
Al Viro9b4a9b12009-04-07 11:44:16 -04001709/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroee0827c2011-02-21 23:38:09 -05001710static int path_lookupat(int dfd, const char *name,
Al Viro9b4a9b12009-04-07 11:44:16 -04001711 unsigned int flags, struct nameidata *nd)
1712{
Al Viro70e9b352011-03-05 21:12:22 -05001713 struct file *base = NULL;
Al Virobd92d7f2011-03-14 19:54:59 -04001714 struct path path;
1715 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001716
1717 /*
1718 * Path walking is largely split up into 2 different synchronisation
1719 * schemes, rcu-walk and ref-walk (explained in
1720 * Documentation/filesystems/path-lookup.txt). These share much of the
1721 * path walk code, but some things particularly setup, cleanup, and
1722 * following mounts are sufficiently divergent that functions are
1723 * duplicated. Typically there is a function foo(), and its RCU
1724 * analogue, foo_rcu().
1725 *
1726 * -ECHILD is the error number of choice (just to avoid clashes) that
1727 * is returned if some aspect of an rcu-walk fails. Such an error must
1728 * be handled by restarting a traditional ref-walk (which will always
1729 * be able to complete).
1730 */
Al Virobd92d7f2011-03-14 19:54:59 -04001731 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
Al Viroee0827c2011-02-21 23:38:09 -05001732
Al Virobd92d7f2011-03-14 19:54:59 -04001733 if (unlikely(err))
1734 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001735
1736 current->total_link_count = 0;
Al Virobd92d7f2011-03-14 19:54:59 -04001737 err = link_path_walk(name, nd);
1738
1739 if (!err && !(flags & LOOKUP_PARENT)) {
Al Virobd92d7f2011-03-14 19:54:59 -04001740 err = lookup_last(nd, &path);
1741 while (err > 0) {
1742 void *cookie;
1743 struct path link = path;
Al Virobd92d7f2011-03-14 19:54:59 -04001744 nd->flags |= LOOKUP_PARENT;
Al Viro574197e2011-03-14 22:20:34 -04001745 err = follow_link(&link, nd, &cookie);
Al Virobd92d7f2011-03-14 19:54:59 -04001746 if (!err)
1747 err = lookup_last(nd, &path);
Al Viro574197e2011-03-14 22:20:34 -04001748 put_link(nd, &link, cookie);
Al Virobd92d7f2011-03-14 19:54:59 -04001749 }
1750 }
Al Viroee0827c2011-02-21 23:38:09 -05001751
Al Viro9f1fafe2011-03-25 11:00:12 -04001752 if (!err)
1753 err = complete_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04001754
1755 if (!err && nd->flags & LOOKUP_DIRECTORY) {
1756 if (!nd->inode->i_op->lookup) {
1757 path_put(&nd->path);
Al Virobd23a532011-03-23 09:56:30 -04001758 err = -ENOTDIR;
Al Virobd92d7f2011-03-14 19:54:59 -04001759 }
1760 }
Al Viro16c2cd72011-02-22 15:50:10 -05001761
Al Viro70e9b352011-03-05 21:12:22 -05001762 if (base)
1763 fput(base);
Al Viroee0827c2011-02-21 23:38:09 -05001764
Al Viro5b6ca022011-03-09 23:04:47 -05001765 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
Al Viro2a737872009-04-07 11:49:53 -04001766 path_put(&nd->root);
1767 nd->root.mnt = NULL;
1768 }
Al Virobd92d7f2011-03-14 19:54:59 -04001769 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001770}
Nick Piggin31e6b012011-01-07 17:49:52 +11001771
Al Viroee0827c2011-02-21 23:38:09 -05001772static int do_path_lookup(int dfd, const char *name,
1773 unsigned int flags, struct nameidata *nd)
1774{
1775 int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1776 if (unlikely(retval == -ECHILD))
1777 retval = path_lookupat(dfd, name, flags, nd);
1778 if (unlikely(retval == -ESTALE))
1779 retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11001780
1781 if (likely(!retval)) {
1782 if (unlikely(!audit_dummy_context())) {
1783 if (nd->path.dentry && nd->inode)
1784 audit_inode(name, nd->path.dentry);
1785 }
1786 }
Al Viro9b4a9b12009-04-07 11:44:16 -04001787 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788}
1789
Al Viroc9c6cac2011-02-16 15:15:47 -05001790int kern_path_parent(const char *name, struct nameidata *nd)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001791{
Al Viroc9c6cac2011-02-16 15:15:47 -05001792 return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001793}
1794
Al Virod1811462008-08-02 00:49:18 -04001795int kern_path(const char *name, unsigned int flags, struct path *path)
1796{
1797 struct nameidata nd;
1798 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1799 if (!res)
1800 *path = nd.path;
1801 return res;
1802}
1803
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001804/**
1805 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1806 * @dentry: pointer to dentry of the base directory
1807 * @mnt: pointer to vfs mount of the base directory
1808 * @name: pointer to file name
1809 * @flags: lookup flags
Al Viroe0a01242011-06-27 17:00:37 -04001810 * @path: pointer to struct path to fill
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001811 */
1812int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
1813 const char *name, unsigned int flags,
Al Viroe0a01242011-06-27 17:00:37 -04001814 struct path *path)
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001815{
Al Viroe0a01242011-06-27 17:00:37 -04001816 struct nameidata nd;
1817 int err;
1818 nd.root.dentry = dentry;
1819 nd.root.mnt = mnt;
1820 BUG_ON(flags & LOOKUP_PARENT);
Al Viro5b6ca022011-03-09 23:04:47 -05001821 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
Al Viroe0a01242011-06-27 17:00:37 -04001822 err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
1823 if (!err)
1824 *path = nd.path;
1825 return err;
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001826}
1827
James Morris057f6c02007-04-26 00:12:05 -07001828/*
1829 * Restricted form of lookup. Doesn't follow links, single-component only,
1830 * needs parent already locked. Doesn't follow mounts.
1831 * SMP-safe.
1832 */
Adrian Bunka244e162006-03-31 02:32:11 -08001833static struct dentry *lookup_hash(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834{
Jan Blunck4ac91372008-02-14 19:34:32 -08001835 return __lookup_hash(&nd->last, nd->path.dentry, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836}
1837
Christoph Hellwigeead1912007-10-16 23:25:38 -07001838/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07001839 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07001840 * @name: pathname component to lookup
1841 * @base: base directory to lookup from
1842 * @len: maximum length @len should be interpreted to
1843 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07001844 * Note that this routine is purely a helper for filesystem usage and should
1845 * not be called by generic code. Also note that by using this function the
Christoph Hellwigeead1912007-10-16 23:25:38 -07001846 * nameidata argument is passed to the filesystem methods and a filesystem
1847 * using this helper needs to be prepared for that.
1848 */
James Morris057f6c02007-04-26 00:12:05 -07001849struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1850{
James Morris057f6c02007-04-26 00:12:05 -07001851 struct qstr this;
Al Viro6a96ba52011-03-07 23:49:20 -05001852 unsigned int c;
Miklos Szeredicda309d2012-03-26 12:54:21 +02001853 int err;
James Morris057f6c02007-04-26 00:12:05 -07001854
David Woodhouse2f9092e2009-04-20 23:18:37 +01001855 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
1856
Al Viro6a96ba52011-03-07 23:49:20 -05001857 this.name = name;
1858 this.len = len;
Linus Torvalds0145acc2012-03-02 14:32:59 -08001859 this.hash = full_name_hash(name, len);
Al Viro6a96ba52011-03-07 23:49:20 -05001860 if (!len)
1861 return ERR_PTR(-EACCES);
1862
Al Viro6a96ba52011-03-07 23:49:20 -05001863 while (len--) {
1864 c = *(const unsigned char *)name++;
1865 if (c == '/' || c == '\0')
1866 return ERR_PTR(-EACCES);
Al Viro6a96ba52011-03-07 23:49:20 -05001867 }
Al Viro5a202bc2011-03-08 14:17:44 -05001868 /*
1869 * See if the low-level filesystem might want
1870 * to use its own hash..
1871 */
1872 if (base->d_flags & DCACHE_OP_HASH) {
1873 int err = base->d_op->d_hash(base, base->d_inode, &this);
1874 if (err < 0)
1875 return ERR_PTR(err);
1876 }
Christoph Hellwigeead1912007-10-16 23:25:38 -07001877
Miklos Szeredicda309d2012-03-26 12:54:21 +02001878 err = inode_permission(base->d_inode, MAY_EXEC);
1879 if (err)
1880 return ERR_PTR(err);
1881
Christoph Hellwig49705b72005-11-08 21:35:06 -08001882 return __lookup_hash(&this, base, NULL);
James Morris057f6c02007-04-26 00:12:05 -07001883}
1884
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01001885int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
1886 struct path *path, int *empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887{
Al Viro2d8f3032008-07-22 09:59:21 -04001888 struct nameidata nd;
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01001889 char *tmp = getname_flags(name, flags, empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 int err = PTR_ERR(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 if (!IS_ERR(tmp)) {
Al Viro2d8f3032008-07-22 09:59:21 -04001892
1893 BUG_ON(flags & LOOKUP_PARENT);
1894
1895 err = do_path_lookup(dfd, tmp, flags, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 putname(tmp);
Al Viro2d8f3032008-07-22 09:59:21 -04001897 if (!err)
1898 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 }
1900 return err;
1901}
1902
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01001903int user_path_at(int dfd, const char __user *name, unsigned flags,
1904 struct path *path)
1905{
Linus Torvaldsf7493e52012-03-22 16:10:40 -07001906 return user_path_at_empty(dfd, name, flags, path, NULL);
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01001907}
1908
Al Viro2ad94ae2008-07-21 09:32:51 -04001909static int user_path_parent(int dfd, const char __user *path,
1910 struct nameidata *nd, char **name)
1911{
1912 char *s = getname(path);
1913 int error;
1914
1915 if (IS_ERR(s))
1916 return PTR_ERR(s);
1917
1918 error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
1919 if (error)
1920 putname(s);
1921 else
1922 *name = s;
1923
1924 return error;
1925}
1926
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927/*
1928 * It's inline, so penalty for filesystems that don't use sticky bit is
1929 * minimal.
1930 */
1931static inline int check_sticky(struct inode *dir, struct inode *inode)
1932{
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08001933 kuid_t fsuid = current_fsuid();
David Howellsda9592e2008-11-14 10:39:05 +11001934
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 if (!(dir->i_mode & S_ISVTX))
1936 return 0;
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08001937 if (uid_eq(inode->i_uid, fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 return 0;
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08001939 if (uid_eq(dir->i_uid, fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 return 0;
Eric W. Biederman1a48e2a2011-11-14 16:24:06 -08001941 return !inode_capable(inode, CAP_FOWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942}
1943
1944/*
1945 * Check whether we can remove a link victim from directory dir, check
1946 * whether the type of victim is right.
1947 * 1. We can't do it if dir is read-only (done in permission())
1948 * 2. We should have write and exec permissions on dir
1949 * 3. We can't remove anything from append-only dir
1950 * 4. We can't do anything with immutable dir (done in permission())
1951 * 5. If the sticky bit on dir is set we should either
1952 * a. be owner of dir, or
1953 * b. be owner of victim, or
1954 * c. have CAP_FOWNER capability
1955 * 6. If the victim is append-only or immutable we can't do antyhing with
1956 * links pointing to it.
1957 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1958 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1959 * 9. We can't remove a root or mountpoint.
1960 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1961 * nfs_async_unlink().
1962 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001963static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964{
1965 int error;
1966
1967 if (!victim->d_inode)
1968 return -ENOENT;
1969
1970 BUG_ON(victim->d_parent->d_inode != dir);
Al Virocccc6bb2009-12-25 05:07:33 -05001971 audit_inode_child(victim, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
Al Virof419a2e2008-07-22 00:07:17 -04001973 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 if (error)
1975 return error;
1976 if (IS_APPEND(dir))
1977 return -EPERM;
1978 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
Hugh Dickinsf9454542008-11-19 15:36:38 -08001979 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 return -EPERM;
1981 if (isdir) {
1982 if (!S_ISDIR(victim->d_inode->i_mode))
1983 return -ENOTDIR;
1984 if (IS_ROOT(victim))
1985 return -EBUSY;
1986 } else if (S_ISDIR(victim->d_inode->i_mode))
1987 return -EISDIR;
1988 if (IS_DEADDIR(dir))
1989 return -ENOENT;
1990 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1991 return -EBUSY;
1992 return 0;
1993}
1994
1995/* Check whether we can create an object with dentry child in directory
1996 * dir.
1997 * 1. We can't do it if child already exists (open has special treatment for
1998 * this case, but since we are inlined it's OK)
1999 * 2. We can't do it if dir is read-only (done in permission())
2000 * 3. We should have write and exec permissions on dir
2001 * 4. We can't do it if dir is immutable (done in permission())
2002 */
Miklos Szeredia95164d2008-07-30 15:08:48 +02002003static inline int may_create(struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004{
2005 if (child->d_inode)
2006 return -EEXIST;
2007 if (IS_DEADDIR(dir))
2008 return -ENOENT;
Al Virof419a2e2008-07-22 00:07:17 -04002009 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010}
2011
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012/*
2013 * p1 and p2 should be directories on the same fs.
2014 */
2015struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2016{
2017 struct dentry *p;
2018
2019 if (p1 == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07002020 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 return NULL;
2022 }
2023
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002024 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002026 p = d_ancestor(p2, p1);
2027 if (p) {
2028 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2029 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
2030 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 }
2032
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002033 p = d_ancestor(p1, p2);
2034 if (p) {
2035 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2036 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2037 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 }
2039
Ingo Molnarf2eace22006-07-03 00:25:05 -07002040 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2041 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 return NULL;
2043}
2044
2045void unlock_rename(struct dentry *p1, struct dentry *p2)
2046{
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002047 mutex_unlock(&p1->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 if (p1 != p2) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002049 mutex_unlock(&p2->d_inode->i_mutex);
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002050 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 }
2052}
2053
Al Viro4acdaf22011-07-26 01:42:34 -04002054int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 struct nameidata *nd)
2056{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002057 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
2059 if (error)
2060 return error;
2061
Al Viroacfa4382008-12-04 10:06:33 -05002062 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 return -EACCES; /* shouldn't it be ENOSYS? */
2064 mode &= S_IALLUGO;
2065 mode |= S_IFREG;
2066 error = security_inode_create(dir, dentry, mode);
2067 if (error)
2068 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 error = dir->i_op->create(dir, dentry, mode, nd);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002070 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002071 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 return error;
2073}
2074
Al Viro73d049a2011-03-11 12:08:24 -05002075static int may_open(struct path *path, int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002077 struct dentry *dentry = path->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 struct inode *inode = dentry->d_inode;
2079 int error;
2080
Al Virobcda7652011-03-13 16:42:14 -04002081 /* O_PATH? */
2082 if (!acc_mode)
2083 return 0;
2084
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 if (!inode)
2086 return -ENOENT;
2087
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002088 switch (inode->i_mode & S_IFMT) {
2089 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002091 case S_IFDIR:
2092 if (acc_mode & MAY_WRITE)
2093 return -EISDIR;
2094 break;
2095 case S_IFBLK:
2096 case S_IFCHR:
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002097 if (path->mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002099 /*FALLTHRU*/
2100 case S_IFIFO:
2101 case S_IFSOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002103 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08002104 }
Dave Hansenb41572e2007-10-16 23:31:14 -07002105
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002106 error = inode_permission(inode, acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07002107 if (error)
2108 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05002109
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 /*
2111 * An append-only file must be opened in append mode for writing.
2112 */
2113 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05002114 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05002115 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05002117 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 }
2119
2120 /* O_NOATIME can only be set by the owner or superuser */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07002121 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
Al Viro7715b522009-12-16 03:54:00 -05002122 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
J. Bruce Fieldsf3c7691e2011-09-21 10:58:13 -04002124 return 0;
Al Viro7715b522009-12-16 03:54:00 -05002125}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
Jeff Laytone1181ee2010-12-07 16:19:50 -05002127static int handle_truncate(struct file *filp)
Al Viro7715b522009-12-16 03:54:00 -05002128{
Jeff Laytone1181ee2010-12-07 16:19:50 -05002129 struct path *path = &filp->f_path;
Al Viro7715b522009-12-16 03:54:00 -05002130 struct inode *inode = path->dentry->d_inode;
2131 int error = get_write_access(inode);
2132 if (error)
2133 return error;
2134 /*
2135 * Refuse to truncate files with mandatory locks held on them.
2136 */
2137 error = locks_verify_locked(inode);
2138 if (!error)
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +09002139 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05002140 if (!error) {
2141 error = do_truncate(path->dentry, 0,
2142 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
Jeff Laytone1181ee2010-12-07 16:19:50 -05002143 filp);
Al Viro7715b522009-12-16 03:54:00 -05002144 }
2145 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04002146 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147}
2148
Dave Hansend57999e2008-02-15 14:37:27 -08002149static inline int open_to_namei_flags(int flag)
2150{
Al Viro8a5e9292011-06-25 19:15:54 -04002151 if ((flag & O_ACCMODE) == 3)
2152 flag--;
Dave Hansend57999e2008-02-15 14:37:27 -08002153 return flag;
2154}
2155
Nick Piggin31e6b012011-01-07 17:49:52 +11002156/*
Al Virofe2d35f2011-03-05 22:58:25 -05002157 * Handle the last step of open()
Nick Piggin31e6b012011-01-07 17:49:52 +11002158 */
Al Virofb1cc552009-12-24 01:58:28 -05002159static struct file *do_last(struct nameidata *nd, struct path *path,
Al Viroc3e380b2011-02-23 13:39:45 -05002160 const struct open_flags *op, const char *pathname)
Al Virofb1cc552009-12-24 01:58:28 -05002161{
Al Viroa1e28032009-12-24 02:12:06 -05002162 struct dentry *dir = nd->path.dentry;
Al Viro6c0d46c2011-03-09 00:59:59 -05002163 struct dentry *dentry;
Al Viroca344a892011-03-09 00:36:45 -05002164 int open_flag = op->open_flag;
Al Viro6c0d46c2011-03-09 00:59:59 -05002165 int will_truncate = open_flag & O_TRUNC;
Al Viroca344a892011-03-09 00:36:45 -05002166 int want_write = 0;
Al Virobcda7652011-03-13 16:42:14 -04002167 int acc_mode = op->acc_mode;
Al Virofb1cc552009-12-24 01:58:28 -05002168 struct file *filp;
Al Viro16c2cd72011-02-22 15:50:10 -05002169 int error;
Al Virofb1cc552009-12-24 01:58:28 -05002170
Al Viroc3e380b2011-02-23 13:39:45 -05002171 nd->flags &= ~LOOKUP_PARENT;
2172 nd->flags |= op->intent;
2173
Al Viro1f36f772009-12-26 10:56:19 -05002174 switch (nd->last_type) {
2175 case LAST_DOTDOT:
Neil Brown176306f2010-05-24 16:57:56 +10002176 case LAST_DOT:
Al Virofe2d35f2011-03-05 22:58:25 -05002177 error = handle_dots(nd, nd->last_type);
2178 if (error)
2179 return ERR_PTR(error);
Al Viro1f36f772009-12-26 10:56:19 -05002180 /* fallthrough */
Al Viro1f36f772009-12-26 10:56:19 -05002181 case LAST_ROOT:
Al Viro9f1fafe2011-03-25 11:00:12 -04002182 error = complete_walk(nd);
Al Viro16c2cd72011-02-22 15:50:10 -05002183 if (error)
Al Viro9f1fafe2011-03-25 11:00:12 -04002184 return ERR_PTR(error);
Al Virofe2d35f2011-03-05 22:58:25 -05002185 audit_inode(pathname, nd->path.dentry);
Al Viroca344a892011-03-09 00:36:45 -05002186 if (open_flag & O_CREAT) {
Al Virofe2d35f2011-03-05 22:58:25 -05002187 error = -EISDIR;
2188 goto exit;
2189 }
2190 goto ok;
Al Viro1f36f772009-12-26 10:56:19 -05002191 case LAST_BIND:
Al Viro9f1fafe2011-03-25 11:00:12 -04002192 error = complete_walk(nd);
Al Viro16c2cd72011-02-22 15:50:10 -05002193 if (error)
Al Viro9f1fafe2011-03-25 11:00:12 -04002194 return ERR_PTR(error);
Al Viro1f36f772009-12-26 10:56:19 -05002195 audit_inode(pathname, dir);
Al Viro67ee3ad2009-12-26 07:01:01 -05002196 goto ok;
Al Viro1f36f772009-12-26 10:56:19 -05002197 }
Al Viro67ee3ad2009-12-26 07:01:01 -05002198
Al Viroca344a892011-03-09 00:36:45 -05002199 if (!(open_flag & O_CREAT)) {
Al Virobcda7652011-03-13 16:42:14 -04002200 int symlink_ok = 0;
Al Virofe2d35f2011-03-05 22:58:25 -05002201 if (nd->last.name[nd->last.len])
2202 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
Al Virobcda7652011-03-13 16:42:14 -04002203 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2204 symlink_ok = 1;
Al Virofe2d35f2011-03-05 22:58:25 -05002205 /* we _can_ be in RCU mode here */
Al Viroce57dfc2011-03-13 19:58:58 -04002206 error = walk_component(nd, path, &nd->last, LAST_NORM,
2207 !symlink_ok);
2208 if (error < 0)
Al Virofe2d35f2011-03-05 22:58:25 -05002209 return ERR_PTR(error);
Al Viroce57dfc2011-03-13 19:58:58 -04002210 if (error) /* symlink */
Al Virofe2d35f2011-03-05 22:58:25 -05002211 return NULL;
Al Virofe2d35f2011-03-05 22:58:25 -05002212 /* sayonara */
Al Viro9f1fafe2011-03-25 11:00:12 -04002213 error = complete_walk(nd);
2214 if (error)
Miklos Szeredi7f6c7e62012-03-06 13:56:34 +01002215 return ERR_PTR(error);
Al Virofe2d35f2011-03-05 22:58:25 -05002216
2217 error = -ENOTDIR;
2218 if (nd->flags & LOOKUP_DIRECTORY) {
Al Viroce57dfc2011-03-13 19:58:58 -04002219 if (!nd->inode->i_op->lookup)
Al Virofe2d35f2011-03-05 22:58:25 -05002220 goto exit;
2221 }
2222 audit_inode(pathname, nd->path.dentry);
2223 goto ok;
2224 }
2225
2226 /* create side of things */
Al Viroa3fbbde2011-11-07 21:21:26 +00002227 /*
2228 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED has been
2229 * cleared when we got to the last component we are about to look up
2230 */
Al Viro9f1fafe2011-03-25 11:00:12 -04002231 error = complete_walk(nd);
2232 if (error)
2233 return ERR_PTR(error);
Al Virofe2d35f2011-03-05 22:58:25 -05002234
2235 audit_inode(pathname, dir);
Al Viro16c2cd72011-02-22 15:50:10 -05002236 error = -EISDIR;
Al Viro1f36f772009-12-26 10:56:19 -05002237 /* trailing slashes? */
Nick Piggin31e6b012011-01-07 17:49:52 +11002238 if (nd->last.name[nd->last.len])
2239 goto exit;
Al Viroa2c36b42009-12-24 03:39:50 -05002240
Al Viroa1e28032009-12-24 02:12:06 -05002241 mutex_lock(&dir->d_inode->i_mutex);
2242
Al Viro6c0d46c2011-03-09 00:59:59 -05002243 dentry = lookup_hash(nd);
2244 error = PTR_ERR(dentry);
2245 if (IS_ERR(dentry)) {
Al Virofb1cc552009-12-24 01:58:28 -05002246 mutex_unlock(&dir->d_inode->i_mutex);
2247 goto exit;
2248 }
2249
Al Viro6c0d46c2011-03-09 00:59:59 -05002250 path->dentry = dentry;
2251 path->mnt = nd->path.mnt;
2252
Al Virofb1cc552009-12-24 01:58:28 -05002253 /* Negative dentry, just create the file */
Al Viro6c0d46c2011-03-09 00:59:59 -05002254 if (!dentry->d_inode) {
Al Viroa218d0f2011-11-21 14:59:34 -05002255 umode_t mode = op->mode;
Al Viro6c0d46c2011-03-09 00:59:59 -05002256 if (!IS_POSIXACL(dir->d_inode))
2257 mode &= ~current_umask();
Al Virofb1cc552009-12-24 01:58:28 -05002258 /*
2259 * This write is needed to ensure that a
Al Viro6c0d46c2011-03-09 00:59:59 -05002260 * rw->ro transition does not occur between
Al Virofb1cc552009-12-24 01:58:28 -05002261 * the time when the file is created and when
2262 * a permanent write count is taken through
2263 * the 'struct file' in nameidata_to_filp().
2264 */
2265 error = mnt_want_write(nd->path.mnt);
2266 if (error)
2267 goto exit_mutex_unlock;
Al Viroca344a892011-03-09 00:36:45 -05002268 want_write = 1;
Al Viro9b44f1b2011-03-09 00:17:27 -05002269 /* Don't check for write permission, don't truncate */
Al Viroca344a892011-03-09 00:36:45 -05002270 open_flag &= ~O_TRUNC;
Al Viro6c0d46c2011-03-09 00:59:59 -05002271 will_truncate = 0;
Al Virobcda7652011-03-13 16:42:14 -04002272 acc_mode = MAY_OPEN;
Al Viro6c0d46c2011-03-09 00:59:59 -05002273 error = security_path_mknod(&nd->path, dentry, mode, 0);
2274 if (error)
2275 goto exit_mutex_unlock;
2276 error = vfs_create(dir->d_inode, dentry, mode, nd);
2277 if (error)
2278 goto exit_mutex_unlock;
2279 mutex_unlock(&dir->d_inode->i_mutex);
2280 dput(nd->path.dentry);
2281 nd->path.dentry = dentry;
Al Viroca344a892011-03-09 00:36:45 -05002282 goto common;
Al Virofb1cc552009-12-24 01:58:28 -05002283 }
2284
2285 /*
2286 * It already exists.
2287 */
2288 mutex_unlock(&dir->d_inode->i_mutex);
2289 audit_inode(pathname, path->dentry);
2290
2291 error = -EEXIST;
Al Viroca344a892011-03-09 00:36:45 -05002292 if (open_flag & O_EXCL)
Al Virofb1cc552009-12-24 01:58:28 -05002293 goto exit_dput;
2294
David Howells9875cf82011-01-14 18:45:21 +00002295 error = follow_managed(path, nd->flags);
2296 if (error < 0)
2297 goto exit_dput;
Al Virofb1cc552009-12-24 01:58:28 -05002298
Al Viroa3fbbde2011-11-07 21:21:26 +00002299 if (error)
2300 nd->flags |= LOOKUP_JUMPED;
2301
Al Virofb1cc552009-12-24 01:58:28 -05002302 error = -ENOENT;
2303 if (!path->dentry->d_inode)
2304 goto exit_dput;
Al Viro9e67f362009-12-26 07:04:50 -05002305
2306 if (path->dentry->d_inode->i_op->follow_link)
Al Virofb1cc552009-12-24 01:58:28 -05002307 return NULL;
Al Virofb1cc552009-12-24 01:58:28 -05002308
2309 path_to_nameidata(path, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11002310 nd->inode = path->dentry->d_inode;
Al Viroa3fbbde2011-11-07 21:21:26 +00002311 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
2312 error = complete_walk(nd);
2313 if (error)
Miklos Szeredi097b1802012-03-06 13:56:33 +01002314 return ERR_PTR(error);
Al Virofb1cc552009-12-24 01:58:28 -05002315 error = -EISDIR;
Nick Piggin31e6b012011-01-07 17:49:52 +11002316 if (S_ISDIR(nd->inode->i_mode))
Al Virofb1cc552009-12-24 01:58:28 -05002317 goto exit;
Al Viro67ee3ad2009-12-26 07:01:01 -05002318ok:
Al Viro6c0d46c2011-03-09 00:59:59 -05002319 if (!S_ISREG(nd->inode->i_mode))
2320 will_truncate = 0;
2321
Al Viro0f9d1a12011-03-09 00:13:14 -05002322 if (will_truncate) {
2323 error = mnt_want_write(nd->path.mnt);
2324 if (error)
2325 goto exit;
Al Viroca344a892011-03-09 00:36:45 -05002326 want_write = 1;
Al Viro0f9d1a12011-03-09 00:13:14 -05002327 }
Al Viroca344a892011-03-09 00:36:45 -05002328common:
Al Virobcda7652011-03-13 16:42:14 -04002329 error = may_open(&nd->path, acc_mode, open_flag);
Al Viroca344a892011-03-09 00:36:45 -05002330 if (error)
Al Viro0f9d1a12011-03-09 00:13:14 -05002331 goto exit;
Al Viro0f9d1a12011-03-09 00:13:14 -05002332 filp = nameidata_to_filp(nd);
2333 if (!IS_ERR(filp)) {
2334 error = ima_file_check(filp, op->acc_mode);
2335 if (error) {
2336 fput(filp);
2337 filp = ERR_PTR(error);
2338 }
2339 }
2340 if (!IS_ERR(filp)) {
2341 if (will_truncate) {
2342 error = handle_truncate(filp);
2343 if (error) {
2344 fput(filp);
2345 filp = ERR_PTR(error);
2346 }
2347 }
2348 }
Al Viroca344a892011-03-09 00:36:45 -05002349out:
2350 if (want_write)
Al Viro0f9d1a12011-03-09 00:13:14 -05002351 mnt_drop_write(nd->path.mnt);
2352 path_put(&nd->path);
Al Virofb1cc552009-12-24 01:58:28 -05002353 return filp;
2354
2355exit_mutex_unlock:
2356 mutex_unlock(&dir->d_inode->i_mutex);
2357exit_dput:
2358 path_put_conditional(path, nd);
2359exit:
Al Viroca344a892011-03-09 00:36:45 -05002360 filp = ERR_PTR(error);
2361 goto out;
Al Virofb1cc552009-12-24 01:58:28 -05002362}
2363
Al Viro13aab422011-02-23 17:54:08 -05002364static struct file *path_openat(int dfd, const char *pathname,
Al Viro73d049a2011-03-11 12:08:24 -05002365 struct nameidata *nd, const struct open_flags *op, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366{
Al Virofe2d35f2011-03-05 22:58:25 -05002367 struct file *base = NULL;
Dave Hansen4a3fd212008-02-15 14:37:48 -08002368 struct file *filp;
Al Viro9850c052010-01-13 15:01:15 -05002369 struct path path;
Al Viro13aab422011-02-23 17:54:08 -05002370 int error;
Nick Piggin31e6b012011-01-07 17:49:52 +11002371
2372 filp = get_empty_filp();
2373 if (!filp)
2374 return ERR_PTR(-ENFILE);
2375
Al Viro47c805d2011-02-23 17:44:09 -05002376 filp->f_flags = op->open_flag;
Al Viro73d049a2011-03-11 12:08:24 -05002377 nd->intent.open.file = filp;
2378 nd->intent.open.flags = open_to_namei_flags(op->open_flag);
2379 nd->intent.open.create_mode = op->mode;
Nick Piggin31e6b012011-01-07 17:49:52 +11002380
Al Viro73d049a2011-03-11 12:08:24 -05002381 error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
Nick Piggin31e6b012011-01-07 17:49:52 +11002382 if (unlikely(error))
Al Viro13aab422011-02-23 17:54:08 -05002383 goto out_filp;
Nick Piggin31e6b012011-01-07 17:49:52 +11002384
Al Virofe2d35f2011-03-05 22:58:25 -05002385 current->total_link_count = 0;
Al Viro73d049a2011-03-11 12:08:24 -05002386 error = link_path_walk(pathname, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11002387 if (unlikely(error))
2388 goto out_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
Al Viro73d049a2011-03-11 12:08:24 -05002390 filp = do_last(nd, &path, op, pathname);
Al Viro806b6812009-12-26 07:16:40 -05002391 while (unlikely(!filp)) { /* trailing symlink */
Nick Piggin7b9337a2011-01-14 08:42:43 +00002392 struct path link = path;
Al Virodef4af32009-12-26 08:37:05 -05002393 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04002394 if (!(nd->flags & LOOKUP_FOLLOW)) {
Al Viro73d049a2011-03-11 12:08:24 -05002395 path_put_conditional(&path, nd);
2396 path_put(&nd->path);
Al Viro40b39132011-03-09 16:22:18 -05002397 filp = ERR_PTR(-ELOOP);
2398 break;
2399 }
Al Viro73d049a2011-03-11 12:08:24 -05002400 nd->flags |= LOOKUP_PARENT;
2401 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
Al Viro574197e2011-03-14 22:20:34 -04002402 error = follow_link(&link, nd, &cookie);
Al Viroc3e380b2011-02-23 13:39:45 -05002403 if (unlikely(error))
Al Virof1afe9e2011-02-22 22:27:28 -05002404 filp = ERR_PTR(error);
Al Viroc3e380b2011-02-23 13:39:45 -05002405 else
Al Viro73d049a2011-03-11 12:08:24 -05002406 filp = do_last(nd, &path, op, pathname);
Al Viro574197e2011-03-14 22:20:34 -04002407 put_link(nd, &link, cookie);
Al Viro806b6812009-12-26 07:16:40 -05002408 }
Al Viro10fa8e62009-12-26 07:09:49 -05002409out:
Al Viro73d049a2011-03-11 12:08:24 -05002410 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
2411 path_put(&nd->root);
Al Virofe2d35f2011-03-05 22:58:25 -05002412 if (base)
2413 fput(base);
Al Viro73d049a2011-03-11 12:08:24 -05002414 release_open_intent(nd);
Al Viro10fa8e62009-12-26 07:09:49 -05002415 return filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
Nick Piggin31e6b012011-01-07 17:49:52 +11002417out_filp:
Al Viro806b6812009-12-26 07:16:40 -05002418 filp = ERR_PTR(error);
Al Viro10fa8e62009-12-26 07:09:49 -05002419 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420}
2421
Al Viro13aab422011-02-23 17:54:08 -05002422struct file *do_filp_open(int dfd, const char *pathname,
2423 const struct open_flags *op, int flags)
2424{
Al Viro73d049a2011-03-11 12:08:24 -05002425 struct nameidata nd;
Al Viro13aab422011-02-23 17:54:08 -05002426 struct file *filp;
2427
Al Viro73d049a2011-03-11 12:08:24 -05002428 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
Al Viro13aab422011-02-23 17:54:08 -05002429 if (unlikely(filp == ERR_PTR(-ECHILD)))
Al Viro73d049a2011-03-11 12:08:24 -05002430 filp = path_openat(dfd, pathname, &nd, op, flags);
Al Viro13aab422011-02-23 17:54:08 -05002431 if (unlikely(filp == ERR_PTR(-ESTALE)))
Al Viro73d049a2011-03-11 12:08:24 -05002432 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
Al Viro13aab422011-02-23 17:54:08 -05002433 return filp;
2434}
2435
Al Viro73d049a2011-03-11 12:08:24 -05002436struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
2437 const char *name, const struct open_flags *op, int flags)
2438{
2439 struct nameidata nd;
2440 struct file *file;
2441
2442 nd.root.mnt = mnt;
2443 nd.root.dentry = dentry;
2444
2445 flags |= LOOKUP_ROOT;
2446
Al Virobcda7652011-03-13 16:42:14 -04002447 if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
Al Viro73d049a2011-03-11 12:08:24 -05002448 return ERR_PTR(-ELOOP);
2449
2450 file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
2451 if (unlikely(file == ERR_PTR(-ECHILD)))
2452 file = path_openat(-1, name, &nd, op, flags);
2453 if (unlikely(file == ERR_PTR(-ESTALE)))
2454 file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
2455 return file;
2456}
2457
Al Viroed75e952011-06-27 16:53:43 -04002458struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002460 struct dentry *dentry = ERR_PTR(-EEXIST);
Al Viroed75e952011-06-27 16:53:43 -04002461 struct nameidata nd;
2462 int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
2463 if (error)
2464 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002466 /*
2467 * Yucky last component or no last component at all?
2468 * (foo/., foo/.., /////)
2469 */
Al Viroed75e952011-06-27 16:53:43 -04002470 if (nd.last_type != LAST_NORM)
2471 goto out;
2472 nd.flags &= ~LOOKUP_PARENT;
2473 nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
2474 nd.intent.open.flags = O_EXCL;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002475
2476 /*
2477 * Do the final lookup.
2478 */
Al Viroed75e952011-06-27 16:53:43 -04002479 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2480 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 if (IS_ERR(dentry))
2482 goto fail;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002483
Al Viroe9baf6e2008-05-15 04:49:12 -04002484 if (dentry->d_inode)
2485 goto eexist;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002486 /*
2487 * Special case - lookup gave negative, but... we had foo/bar/
2488 * From the vfs_mknod() POV we just have a negative dentry -
2489 * all is fine. Let's be bastards - you had / on the end, you've
2490 * been asking for (non-existent) directory. -ENOENT for you.
2491 */
Al Viroed75e952011-06-27 16:53:43 -04002492 if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
Al Viroe9baf6e2008-05-15 04:49:12 -04002493 dput(dentry);
2494 dentry = ERR_PTR(-ENOENT);
Al Viroed75e952011-06-27 16:53:43 -04002495 goto fail;
Al Viroe9baf6e2008-05-15 04:49:12 -04002496 }
Al Viroed75e952011-06-27 16:53:43 -04002497 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 return dentry;
Al Viroe9baf6e2008-05-15 04:49:12 -04002499eexist:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 dput(dentry);
Al Viroe9baf6e2008-05-15 04:49:12 -04002501 dentry = ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502fail:
Al Viroed75e952011-06-27 16:53:43 -04002503 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2504out:
2505 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 return dentry;
2507}
Al Virodae6ad82011-06-26 11:50:15 -04002508EXPORT_SYMBOL(kern_path_create);
2509
2510struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir)
2511{
2512 char *tmp = getname(pathname);
2513 struct dentry *res;
2514 if (IS_ERR(tmp))
2515 return ERR_CAST(tmp);
2516 res = kern_path_create(dfd, tmp, path, is_dir);
2517 putname(tmp);
2518 return res;
2519}
2520EXPORT_SYMBOL(user_path_create);
2521
Al Viro1a67aaf2011-07-26 01:52:52 -04002522int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002524 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
2526 if (error)
2527 return error;
2528
Eric W. Biederman975d6b32011-11-13 12:16:43 -08002529 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 return -EPERM;
2531
Al Viroacfa4382008-12-04 10:06:33 -05002532 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 return -EPERM;
2534
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07002535 error = devcgroup_inode_mknod(mode, dev);
2536 if (error)
2537 return error;
2538
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 error = security_inode_mknod(dir, dentry, mode, dev);
2540 if (error)
2541 return error;
2542
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002544 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002545 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 return error;
2547}
2548
Al Virof69aac02011-07-26 04:31:40 -04002549static int may_mknod(umode_t mode)
Dave Hansen463c3192008-02-15 14:37:57 -08002550{
2551 switch (mode & S_IFMT) {
2552 case S_IFREG:
2553 case S_IFCHR:
2554 case S_IFBLK:
2555 case S_IFIFO:
2556 case S_IFSOCK:
2557 case 0: /* zero mode translates to S_IFREG */
2558 return 0;
2559 case S_IFDIR:
2560 return -EPERM;
2561 default:
2562 return -EINVAL;
2563 }
2564}
2565
Al Viro8208a222011-07-25 17:32:17 -04002566SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002567 unsigned, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568{
Al Viro2ad94ae2008-07-21 09:32:51 -04002569 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04002570 struct path path;
2571 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
2573 if (S_ISDIR(mode))
2574 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
Al Virodae6ad82011-06-26 11:50:15 -04002576 dentry = user_path_create(dfd, filename, &path, 0);
2577 if (IS_ERR(dentry))
2578 return PTR_ERR(dentry);
Al Viro2ad94ae2008-07-21 09:32:51 -04002579
Al Virodae6ad82011-06-26 11:50:15 -04002580 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04002581 mode &= ~current_umask();
Dave Hansen463c3192008-02-15 14:37:57 -08002582 error = may_mknod(mode);
2583 if (error)
2584 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04002585 error = mnt_want_write(path.mnt);
Dave Hansen463c3192008-02-15 14:37:57 -08002586 if (error)
2587 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04002588 error = security_path_mknod(&path, dentry, mode, dev);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002589 if (error)
2590 goto out_drop_write;
Dave Hansen463c3192008-02-15 14:37:57 -08002591 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 case 0: case S_IFREG:
Al Virodae6ad82011-06-26 11:50:15 -04002593 error = vfs_create(path.dentry->d_inode,dentry,mode,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 break;
2595 case S_IFCHR: case S_IFBLK:
Al Virodae6ad82011-06-26 11:50:15 -04002596 error = vfs_mknod(path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 new_decode_dev(dev));
2598 break;
2599 case S_IFIFO: case S_IFSOCK:
Al Virodae6ad82011-06-26 11:50:15 -04002600 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 }
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002603out_drop_write:
Al Virodae6ad82011-06-26 11:50:15 -04002604 mnt_drop_write(path.mnt);
Dave Hansen463c3192008-02-15 14:37:57 -08002605out_dput:
2606 dput(dentry);
Al Virodae6ad82011-06-26 11:50:15 -04002607 mutex_unlock(&path.dentry->d_inode->i_mutex);
2608 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609
2610 return error;
2611}
2612
Al Viro8208a222011-07-25 17:32:17 -04002613SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002614{
2615 return sys_mknodat(AT_FDCWD, filename, mode, dev);
2616}
2617
Al Viro18bb1db2011-07-26 01:41:39 -04002618int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002620 int error = may_create(dir, dentry);
Al Viro8de52772012-02-06 12:45:27 -05002621 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
2623 if (error)
2624 return error;
2625
Al Viroacfa4382008-12-04 10:06:33 -05002626 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 return -EPERM;
2628
2629 mode &= (S_IRWXUGO|S_ISVTX);
2630 error = security_inode_mkdir(dir, dentry, mode);
2631 if (error)
2632 return error;
2633
Al Viro8de52772012-02-06 12:45:27 -05002634 if (max_links && dir->i_nlink >= max_links)
2635 return -EMLINK;
2636
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002638 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002639 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 return error;
2641}
2642
Al Viroa218d0f2011-11-21 14:59:34 -05002643SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644{
Dave Hansen6902d922006-09-30 23:29:01 -07002645 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04002646 struct path path;
2647 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
Al Virodae6ad82011-06-26 11:50:15 -04002649 dentry = user_path_create(dfd, pathname, &path, 1);
Dave Hansen6902d922006-09-30 23:29:01 -07002650 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04002651 return PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002652
Al Virodae6ad82011-06-26 11:50:15 -04002653 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04002654 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04002655 error = mnt_want_write(path.mnt);
Dave Hansen463c3192008-02-15 14:37:57 -08002656 if (error)
2657 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04002658 error = security_path_mkdir(&path, dentry, mode);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002659 if (error)
2660 goto out_drop_write;
Al Virodae6ad82011-06-26 11:50:15 -04002661 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002662out_drop_write:
Al Virodae6ad82011-06-26 11:50:15 -04002663 mnt_drop_write(path.mnt);
Dave Hansen463c3192008-02-15 14:37:57 -08002664out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002665 dput(dentry);
Al Virodae6ad82011-06-26 11:50:15 -04002666 mutex_unlock(&path.dentry->d_inode->i_mutex);
2667 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 return error;
2669}
2670
Al Viroa218d0f2011-11-21 14:59:34 -05002671SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002672{
2673 return sys_mkdirat(AT_FDCWD, pathname, mode);
2674}
2675
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676/*
Sage Weila71905f2011-05-24 13:06:08 -07002677 * The dentry_unhash() helper will try to drop the dentry early: we
J. Bruce Fieldsc0d02592012-02-15 11:48:40 -05002678 * should have a usage count of 1 if we're the only user of this
Sage Weila71905f2011-05-24 13:06:08 -07002679 * dentry, and if that is true (possibly after pruning the dcache),
2680 * then we drop the dentry now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 *
2682 * A low-level filesystem can, if it choses, legally
2683 * do a
2684 *
2685 * if (!d_unhashed(dentry))
2686 * return -EBUSY;
2687 *
2688 * if it cannot handle the case of removing a directory
2689 * that is still in use by something else..
2690 */
2691void dentry_unhash(struct dentry *dentry)
2692{
Vasily Averindc168422006-12-06 20:37:07 -08002693 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 spin_lock(&dentry->d_lock);
Sage Weil64252c72011-05-24 13:06:05 -07002695 if (dentry->d_count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 __d_drop(dentry);
2697 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698}
2699
2700int vfs_rmdir(struct inode *dir, struct dentry *dentry)
2701{
2702 int error = may_delete(dir, dentry, 1);
2703
2704 if (error)
2705 return error;
2706
Al Viroacfa4382008-12-04 10:06:33 -05002707 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 return -EPERM;
2709
Al Viro1d2ef592011-09-14 18:55:41 +01002710 dget(dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002711 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712
Sage Weil912dbc12011-05-24 13:06:11 -07002713 error = -EBUSY;
2714 if (d_mountpoint(dentry))
2715 goto out;
2716
2717 error = security_inode_rmdir(dir, dentry);
2718 if (error)
2719 goto out;
2720
Sage Weil3cebde22011-05-29 21:20:59 -07002721 shrink_dcache_parent(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07002722 error = dir->i_op->rmdir(dir, dentry);
2723 if (error)
2724 goto out;
2725
2726 dentry->d_inode->i_flags |= S_DEAD;
2727 dont_mount(dentry);
2728
2729out:
2730 mutex_unlock(&dentry->d_inode->i_mutex);
Al Viro1d2ef592011-09-14 18:55:41 +01002731 dput(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07002732 if (!error)
2733 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 return error;
2735}
2736
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002737static long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738{
2739 int error = 0;
2740 char * name;
2741 struct dentry *dentry;
2742 struct nameidata nd;
2743
Al Viro2ad94ae2008-07-21 09:32:51 -04002744 error = user_path_parent(dfd, pathname, &nd, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002746 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747
2748 switch(nd.last_type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002749 case LAST_DOTDOT:
2750 error = -ENOTEMPTY;
2751 goto exit1;
2752 case LAST_DOT:
2753 error = -EINVAL;
2754 goto exit1;
2755 case LAST_ROOT:
2756 error = -EBUSY;
2757 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002759
2760 nd.flags &= ~LOOKUP_PARENT;
2761
Jan Blunck4ac91372008-02-14 19:34:32 -08002762 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002763 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002765 if (IS_ERR(dentry))
2766 goto exit2;
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04002767 if (!dentry->d_inode) {
2768 error = -ENOENT;
2769 goto exit3;
2770 }
Dave Hansen06227532008-02-15 14:37:34 -08002771 error = mnt_want_write(nd.path.mnt);
2772 if (error)
2773 goto exit3;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002774 error = security_path_rmdir(&nd.path, dentry);
2775 if (error)
2776 goto exit4;
Jan Blunck4ac91372008-02-14 19:34:32 -08002777 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002778exit4:
Dave Hansen06227532008-02-15 14:37:34 -08002779 mnt_drop_write(nd.path.mnt);
2780exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07002781 dput(dentry);
2782exit2:
Jan Blunck4ac91372008-02-14 19:34:32 -08002783 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002785 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 putname(name);
2787 return error;
2788}
2789
Heiko Carstens3cdad422009-01-14 14:14:22 +01002790SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002791{
2792 return do_rmdir(AT_FDCWD, pathname);
2793}
2794
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795int vfs_unlink(struct inode *dir, struct dentry *dentry)
2796{
2797 int error = may_delete(dir, dentry, 0);
2798
2799 if (error)
2800 return error;
2801
Al Viroacfa4382008-12-04 10:06:33 -05002802 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 return -EPERM;
2804
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002805 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 if (d_mountpoint(dentry))
2807 error = -EBUSY;
2808 else {
2809 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05002810 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 error = dir->i_op->unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05002812 if (!error)
Al Virod83c49f2010-04-30 17:17:09 -04002813 dont_mount(dentry);
Al Virobec10522010-03-03 14:12:08 -05002814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002816 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817
2818 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2819 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
Jan Karaece95912008-02-06 01:37:13 -08002820 fsnotify_link_count(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 }
Robert Love0eeca282005-07-12 17:06:03 -04002823
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 return error;
2825}
2826
2827/*
2828 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002829 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 * writeout happening, and we don't want to prevent access to the directory
2831 * while waiting on the I/O.
2832 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002833static long do_unlinkat(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834{
Al Viro2ad94ae2008-07-21 09:32:51 -04002835 int error;
2836 char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 struct dentry *dentry;
2838 struct nameidata nd;
2839 struct inode *inode = NULL;
2840
Al Viro2ad94ae2008-07-21 09:32:51 -04002841 error = user_path_parent(dfd, pathname, &nd, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002843 return error;
2844
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 error = -EISDIR;
2846 if (nd.last_type != LAST_NORM)
2847 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002848
2849 nd.flags &= ~LOOKUP_PARENT;
2850
Jan Blunck4ac91372008-02-14 19:34:32 -08002851 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002852 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 error = PTR_ERR(dentry);
2854 if (!IS_ERR(dentry)) {
2855 /* Why not before? Because we want correct error value */
Török Edwin50338b82011-06-16 00:06:14 +03002856 if (nd.last.name[nd.last.len])
2857 goto slashes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 inode = dentry->d_inode;
Török Edwin50338b82011-06-16 00:06:14 +03002859 if (!inode)
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04002860 goto slashes;
2861 ihold(inode);
Dave Hansen06227532008-02-15 14:37:34 -08002862 error = mnt_want_write(nd.path.mnt);
2863 if (error)
2864 goto exit2;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002865 error = security_path_unlink(&nd.path, dentry);
2866 if (error)
2867 goto exit3;
Jan Blunck4ac91372008-02-14 19:34:32 -08002868 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002869exit3:
Dave Hansen06227532008-02-15 14:37:34 -08002870 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 exit2:
2872 dput(dentry);
2873 }
Jan Blunck4ac91372008-02-14 19:34:32 -08002874 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 if (inode)
2876 iput(inode); /* truncate the inode here */
2877exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002878 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 putname(name);
2880 return error;
2881
2882slashes:
2883 error = !dentry->d_inode ? -ENOENT :
2884 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2885 goto exit2;
2886}
2887
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002888SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002889{
2890 if ((flag & ~AT_REMOVEDIR) != 0)
2891 return -EINVAL;
2892
2893 if (flag & AT_REMOVEDIR)
2894 return do_rmdir(dfd, pathname);
2895
2896 return do_unlinkat(dfd, pathname);
2897}
2898
Heiko Carstens3480b252009-01-14 14:14:16 +01002899SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002900{
2901 return do_unlinkat(AT_FDCWD, pathname);
2902}
2903
Miklos Szeredidb2e7472008-06-24 16:50:16 +02002904int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002906 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907
2908 if (error)
2909 return error;
2910
Al Viroacfa4382008-12-04 10:06:33 -05002911 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 return -EPERM;
2913
2914 error = security_inode_symlink(dir, dentry, oldname);
2915 if (error)
2916 return error;
2917
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002919 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002920 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 return error;
2922}
2923
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002924SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
2925 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926{
Al Viro2ad94ae2008-07-21 09:32:51 -04002927 int error;
2928 char *from;
Dave Hansen6902d922006-09-30 23:29:01 -07002929 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04002930 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931
2932 from = getname(oldname);
Al Viro2ad94ae2008-07-21 09:32:51 -04002933 if (IS_ERR(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 return PTR_ERR(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04002935
Al Virodae6ad82011-06-26 11:50:15 -04002936 dentry = user_path_create(newdfd, newname, &path, 0);
Dave Hansen6902d922006-09-30 23:29:01 -07002937 error = PTR_ERR(dentry);
2938 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04002939 goto out_putname;
Dave Hansen6902d922006-09-30 23:29:01 -07002940
Al Virodae6ad82011-06-26 11:50:15 -04002941 error = mnt_want_write(path.mnt);
Dave Hansen75c3f292008-02-15 14:37:45 -08002942 if (error)
2943 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04002944 error = security_path_symlink(&path, dentry, from);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002945 if (error)
2946 goto out_drop_write;
Al Virodae6ad82011-06-26 11:50:15 -04002947 error = vfs_symlink(path.dentry->d_inode, dentry, from);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002948out_drop_write:
Al Virodae6ad82011-06-26 11:50:15 -04002949 mnt_drop_write(path.mnt);
Dave Hansen75c3f292008-02-15 14:37:45 -08002950out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002951 dput(dentry);
Al Virodae6ad82011-06-26 11:50:15 -04002952 mutex_unlock(&path.dentry->d_inode->i_mutex);
2953 path_put(&path);
Dave Hansen6902d922006-09-30 23:29:01 -07002954out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 putname(from);
2956 return error;
2957}
2958
Heiko Carstens3480b252009-01-14 14:14:16 +01002959SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002960{
2961 return sys_symlinkat(oldname, AT_FDCWD, newname);
2962}
2963
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2965{
2966 struct inode *inode = old_dentry->d_inode;
Al Viro8de52772012-02-06 12:45:27 -05002967 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 int error;
2969
2970 if (!inode)
2971 return -ENOENT;
2972
Miklos Szeredia95164d2008-07-30 15:08:48 +02002973 error = may_create(dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 if (error)
2975 return error;
2976
2977 if (dir->i_sb != inode->i_sb)
2978 return -EXDEV;
2979
2980 /*
2981 * A link to an append-only or immutable file cannot be created.
2982 */
2983 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2984 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05002985 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002987 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 return -EPERM;
2989
2990 error = security_inode_link(old_dentry, dir, new_dentry);
2991 if (error)
2992 return error;
2993
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002994 mutex_lock(&inode->i_mutex);
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05302995 /* Make sure we don't allow creating hardlink to an unlinked file */
2996 if (inode->i_nlink == 0)
2997 error = -ENOENT;
Al Viro8de52772012-02-06 12:45:27 -05002998 else if (max_links && inode->i_nlink >= max_links)
2999 error = -EMLINK;
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05303000 else
3001 error = dir->i_op->link(old_dentry, dir, new_dentry);
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003002 mutex_unlock(&inode->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07003003 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003004 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 return error;
3006}
3007
3008/*
3009 * Hardlinks are often used in delicate situations. We avoid
3010 * security-related surprises by not following symlinks on the
3011 * newname. --KAB
3012 *
3013 * We don't follow them on the oldname either to be compatible
3014 * with linux 2.0, and to avoid hard-linking to directories
3015 * and other special files. --ADM
3016 */
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003017SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
3018 int, newdfd, const char __user *, newname, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019{
3020 struct dentry *new_dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003021 struct path old_path, new_path;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303022 int how = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303025 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003026 return -EINVAL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303027 /*
3028 * To use null names we require CAP_DAC_READ_SEARCH
3029 * This ensures that not everyone will be able to create
3030 * handlink using the passed filedescriptor.
3031 */
3032 if (flags & AT_EMPTY_PATH) {
3033 if (!capable(CAP_DAC_READ_SEARCH))
3034 return -ENOENT;
3035 how = LOOKUP_EMPTY;
3036 }
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003037
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303038 if (flags & AT_SYMLINK_FOLLOW)
3039 how |= LOOKUP_FOLLOW;
3040
3041 error = user_path_at(olddfd, oldname, how, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04003043 return error;
3044
Al Virodae6ad82011-06-26 11:50:15 -04003045 new_dentry = user_path_create(newdfd, newname, &new_path, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003047 if (IS_ERR(new_dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003048 goto out;
3049
3050 error = -EXDEV;
3051 if (old_path.mnt != new_path.mnt)
3052 goto out_dput;
3053 error = mnt_want_write(new_path.mnt);
Dave Hansen75c3f292008-02-15 14:37:45 -08003054 if (error)
3055 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04003056 error = security_path_link(old_path.dentry, &new_path, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003057 if (error)
3058 goto out_drop_write;
Al Virodae6ad82011-06-26 11:50:15 -04003059 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003060out_drop_write:
Al Virodae6ad82011-06-26 11:50:15 -04003061 mnt_drop_write(new_path.mnt);
Dave Hansen75c3f292008-02-15 14:37:45 -08003062out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07003063 dput(new_dentry);
Al Virodae6ad82011-06-26 11:50:15 -04003064 mutex_unlock(&new_path.dentry->d_inode->i_mutex);
3065 path_put(&new_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066out:
Al Viro2d8f3032008-07-22 09:59:21 -04003067 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068
3069 return error;
3070}
3071
Heiko Carstens3480b252009-01-14 14:14:16 +01003072SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003073{
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003074 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003075}
3076
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077/*
3078 * The worst of all namespace operations - renaming directory. "Perverted"
3079 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
3080 * Problems:
3081 * a) we can get into loop creation. Check is done in is_subdir().
3082 * b) race potential - two innocent renames can create a loop together.
3083 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08003084 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 * story.
3086 * c) we have to lock _three_ objects - parents and victim (if it exists).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003087 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 * whether the target exists). Solution: try to be smart with locking
3089 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08003090 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 * move will be locked. Thus we can rank directories by the tree
3092 * (ancestors first) and rank all non-directories after them.
3093 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08003094 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 * HOWEVER, it relies on the assumption that any object with ->lookup()
3096 * has no more than 1 dentry. If "hybrid" objects will ever appear,
3097 * we'd better make sure that there's no link(2) for them.
Sage Weile4eaac02011-05-24 13:06:07 -07003098 * d) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003099 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05003101 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 * locking].
3103 */
Adrian Bunk75c96f82005-05-05 16:16:09 -07003104static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
3105 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106{
3107 int error = 0;
Sage Weil9055cba2011-05-24 13:06:12 -07003108 struct inode *target = new_dentry->d_inode;
Al Viro8de52772012-02-06 12:45:27 -05003109 unsigned max_links = new_dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110
3111 /*
3112 * If we are going to change the parent - check write permissions,
3113 * we'll need to flip '..'.
3114 */
3115 if (new_dir != old_dir) {
Al Virof419a2e2008-07-22 00:07:17 -04003116 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 if (error)
3118 return error;
3119 }
3120
3121 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3122 if (error)
3123 return error;
3124
Al Viro1d2ef592011-09-14 18:55:41 +01003125 dget(new_dentry);
Al Virod83c49f2010-04-30 17:17:09 -04003126 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003127 mutex_lock(&target->i_mutex);
Sage Weil9055cba2011-05-24 13:06:12 -07003128
3129 error = -EBUSY;
3130 if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
3131 goto out;
3132
Al Viro8de52772012-02-06 12:45:27 -05003133 error = -EMLINK;
3134 if (max_links && !target && new_dir != old_dir &&
3135 new_dir->i_nlink >= max_links)
3136 goto out;
3137
Sage Weil3cebde22011-05-29 21:20:59 -07003138 if (target)
3139 shrink_dcache_parent(new_dentry);
Sage Weil9055cba2011-05-24 13:06:12 -07003140 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3141 if (error)
3142 goto out;
3143
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 if (target) {
Sage Weil9055cba2011-05-24 13:06:12 -07003145 target->i_flags |= S_DEAD;
3146 dont_mount(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 }
Sage Weil9055cba2011-05-24 13:06:12 -07003148out:
3149 if (target)
3150 mutex_unlock(&target->i_mutex);
Al Viro1d2ef592011-09-14 18:55:41 +01003151 dput(new_dentry);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07003152 if (!error)
Mark Fasheh349457c2006-09-08 14:22:21 -07003153 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3154 d_move(old_dentry,new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 return error;
3156}
3157
Adrian Bunk75c96f82005-05-05 16:16:09 -07003158static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
3159 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160{
Sage Weil51892bb2011-05-24 13:06:13 -07003161 struct inode *target = new_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 int error;
3163
3164 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3165 if (error)
3166 return error;
3167
3168 dget(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003170 mutex_lock(&target->i_mutex);
Sage Weil51892bb2011-05-24 13:06:13 -07003171
3172 error = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
Sage Weil51892bb2011-05-24 13:06:13 -07003174 goto out;
3175
3176 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3177 if (error)
3178 goto out;
3179
3180 if (target)
3181 dont_mount(new_dentry);
3182 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3183 d_move(old_dentry, new_dentry);
3184out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003186 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 dput(new_dentry);
3188 return error;
3189}
3190
3191int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
3192 struct inode *new_dir, struct dentry *new_dentry)
3193{
3194 int error;
3195 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
Eric Paris59b0df22010-02-08 12:53:52 -05003196 const unsigned char *old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197
3198 if (old_dentry->d_inode == new_dentry->d_inode)
3199 return 0;
3200
3201 error = may_delete(old_dir, old_dentry, is_dir);
3202 if (error)
3203 return error;
3204
3205 if (!new_dentry->d_inode)
Miklos Szeredia95164d2008-07-30 15:08:48 +02003206 error = may_create(new_dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 else
3208 error = may_delete(new_dir, new_dentry, is_dir);
3209 if (error)
3210 return error;
3211
Al Viroacfa4382008-12-04 10:06:33 -05003212 if (!old_dir->i_op->rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 return -EPERM;
3214
Robert Love0eeca282005-07-12 17:06:03 -04003215 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
3216
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 if (is_dir)
3218 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
3219 else
3220 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
Al Viro123df292009-12-25 04:57:57 -05003221 if (!error)
3222 fsnotify_move(old_dir, new_dir, old_name, is_dir,
Al Viro5a190ae2007-06-07 12:19:32 -04003223 new_dentry->d_inode, old_dentry);
Robert Love0eeca282005-07-12 17:06:03 -04003224 fsnotify_oldname_free(old_name);
3225
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 return error;
3227}
3228
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003229SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
3230 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231{
Al Viro2ad94ae2008-07-21 09:32:51 -04003232 struct dentry *old_dir, *new_dir;
3233 struct dentry *old_dentry, *new_dentry;
3234 struct dentry *trap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 struct nameidata oldnd, newnd;
Al Viro2ad94ae2008-07-21 09:32:51 -04003236 char *from;
3237 char *to;
3238 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239
Al Viro2ad94ae2008-07-21 09:32:51 -04003240 error = user_path_parent(olddfd, oldname, &oldnd, &from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 if (error)
3242 goto exit;
3243
Al Viro2ad94ae2008-07-21 09:32:51 -04003244 error = user_path_parent(newdfd, newname, &newnd, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 if (error)
3246 goto exit1;
3247
3248 error = -EXDEV;
Jan Blunck4ac91372008-02-14 19:34:32 -08003249 if (oldnd.path.mnt != newnd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 goto exit2;
3251
Jan Blunck4ac91372008-02-14 19:34:32 -08003252 old_dir = oldnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 error = -EBUSY;
3254 if (oldnd.last_type != LAST_NORM)
3255 goto exit2;
3256
Jan Blunck4ac91372008-02-14 19:34:32 -08003257 new_dir = newnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 if (newnd.last_type != LAST_NORM)
3259 goto exit2;
3260
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003261 oldnd.flags &= ~LOOKUP_PARENT;
3262 newnd.flags &= ~LOOKUP_PARENT;
OGAWA Hirofumi4e9ed2f2008-10-16 07:50:29 +09003263 newnd.flags |= LOOKUP_RENAME_TARGET;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003264
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 trap = lock_rename(new_dir, old_dir);
3266
Christoph Hellwig49705b72005-11-08 21:35:06 -08003267 old_dentry = lookup_hash(&oldnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 error = PTR_ERR(old_dentry);
3269 if (IS_ERR(old_dentry))
3270 goto exit3;
3271 /* source must exist */
3272 error = -ENOENT;
3273 if (!old_dentry->d_inode)
3274 goto exit4;
3275 /* unless the source is a directory trailing slashes give -ENOTDIR */
3276 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
3277 error = -ENOTDIR;
3278 if (oldnd.last.name[oldnd.last.len])
3279 goto exit4;
3280 if (newnd.last.name[newnd.last.len])
3281 goto exit4;
3282 }
3283 /* source should not be ancestor of target */
3284 error = -EINVAL;
3285 if (old_dentry == trap)
3286 goto exit4;
Christoph Hellwig49705b72005-11-08 21:35:06 -08003287 new_dentry = lookup_hash(&newnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 error = PTR_ERR(new_dentry);
3289 if (IS_ERR(new_dentry))
3290 goto exit4;
3291 /* target should not be an ancestor of source */
3292 error = -ENOTEMPTY;
3293 if (new_dentry == trap)
3294 goto exit5;
3295
Dave Hansen9079b1e2008-02-15 14:37:49 -08003296 error = mnt_want_write(oldnd.path.mnt);
3297 if (error)
3298 goto exit5;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003299 error = security_path_rename(&oldnd.path, old_dentry,
3300 &newnd.path, new_dentry);
3301 if (error)
3302 goto exit6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 error = vfs_rename(old_dir->d_inode, old_dentry,
3304 new_dir->d_inode, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003305exit6:
Dave Hansen9079b1e2008-02-15 14:37:49 -08003306 mnt_drop_write(oldnd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307exit5:
3308 dput(new_dentry);
3309exit4:
3310 dput(old_dentry);
3311exit3:
3312 unlock_rename(new_dir, old_dir);
3313exit2:
Jan Blunck1d957f92008-02-14 19:34:35 -08003314 path_put(&newnd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04003315 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003317 path_put(&oldnd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 putname(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04003319exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 return error;
3321}
3322
Heiko Carstensa26eab22009-01-14 14:14:17 +01003323SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003324{
3325 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
3326}
3327
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
3329{
3330 int len;
3331
3332 len = PTR_ERR(link);
3333 if (IS_ERR(link))
3334 goto out;
3335
3336 len = strlen(link);
3337 if (len > (unsigned) buflen)
3338 len = buflen;
3339 if (copy_to_user(buffer, link, len))
3340 len = -EFAULT;
3341out:
3342 return len;
3343}
3344
3345/*
3346 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
3347 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
3348 * using) it for any given inode is up to filesystem.
3349 */
3350int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3351{
3352 struct nameidata nd;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003353 void *cookie;
Marcin Slusarz694a1762008-06-09 16:40:37 -07003354 int res;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003355
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 nd.depth = 0;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003357 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
Marcin Slusarz694a1762008-06-09 16:40:37 -07003358 if (IS_ERR(cookie))
3359 return PTR_ERR(cookie);
3360
3361 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
3362 if (dentry->d_inode->i_op->put_link)
3363 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3364 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365}
3366
3367int vfs_follow_link(struct nameidata *nd, const char *link)
3368{
3369 return __vfs_follow_link(nd, link);
3370}
3371
3372/* get the link contents into pagecache */
3373static char *page_getlink(struct dentry * dentry, struct page **ppage)
3374{
Duane Griffinebd09ab2008-12-19 20:47:12 +00003375 char *kaddr;
3376 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 struct address_space *mapping = dentry->d_inode->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -07003378 page = read_mapping_page(mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 if (IS_ERR(page))
Nick Piggin6fe69002007-05-06 14:49:04 -07003380 return (char*)page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 *ppage = page;
Duane Griffinebd09ab2008-12-19 20:47:12 +00003382 kaddr = kmap(page);
3383 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3384 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385}
3386
3387int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3388{
3389 struct page *page = NULL;
3390 char *s = page_getlink(dentry, &page);
3391 int res = vfs_readlink(dentry,buffer,buflen,s);
3392 if (page) {
3393 kunmap(page);
3394 page_cache_release(page);
3395 }
3396 return res;
3397}
3398
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003399void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003401 struct page *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 nd_set_link(nd, page_getlink(dentry, &page));
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003403 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404}
3405
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003406void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003408 struct page *page = cookie;
3409
3410 if (page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 kunmap(page);
3412 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 }
3414}
3415
Nick Piggin54566b22009-01-04 12:00:53 -08003416/*
3417 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
3418 */
3419int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420{
3421 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003422 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07003423 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08003424 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 char *kaddr;
Nick Piggin54566b22009-01-04 12:00:53 -08003426 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
3427 if (nofs)
3428 flags |= AOP_FLAG_NOFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429
NeilBrown7e53cac2006-03-25 03:07:57 -08003430retry:
Nick Pigginafddba42007-10-16 01:25:01 -07003431 err = pagecache_write_begin(NULL, mapping, 0, len-1,
Nick Piggin54566b22009-01-04 12:00:53 -08003432 flags, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07003434 goto fail;
3435
Cong Wange8e3c3d2011-11-25 23:14:27 +08003436 kaddr = kmap_atomic(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 memcpy(kaddr, symname, len-1);
Cong Wange8e3c3d2011-11-25 23:14:27 +08003438 kunmap_atomic(kaddr);
Nick Pigginafddba42007-10-16 01:25:01 -07003439
3440 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3441 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 if (err < 0)
3443 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07003444 if (err < len-1)
3445 goto retry;
3446
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 mark_inode_dirty(inode);
3448 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449fail:
3450 return err;
3451}
3452
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003453int page_symlink(struct inode *inode, const char *symname, int len)
3454{
3455 return __page_symlink(inode, symname, len,
Nick Piggin54566b22009-01-04 12:00:53 -08003456 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003457}
3458
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003459const struct inode_operations page_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 .readlink = generic_readlink,
3461 .follow_link = page_follow_link_light,
3462 .put_link = page_put_link,
3463};
3464
Al Viro2d8f3032008-07-22 09:59:21 -04003465EXPORT_SYMBOL(user_path_at);
David Howellscc53ce52011-01-14 18:45:26 +00003466EXPORT_SYMBOL(follow_down_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467EXPORT_SYMBOL(follow_down);
3468EXPORT_SYMBOL(follow_up);
3469EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
3470EXPORT_SYMBOL(getname);
3471EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472EXPORT_SYMBOL(lookup_one_len);
3473EXPORT_SYMBOL(page_follow_link_light);
3474EXPORT_SYMBOL(page_put_link);
3475EXPORT_SYMBOL(page_readlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003476EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477EXPORT_SYMBOL(page_symlink);
3478EXPORT_SYMBOL(page_symlink_inode_operations);
Al Virod1811462008-08-02 00:49:18 -04003479EXPORT_SYMBOL(kern_path);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07003480EXPORT_SYMBOL(vfs_path_lookup);
Al Virof419a2e2008-07-22 00:07:17 -04003481EXPORT_SYMBOL(inode_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482EXPORT_SYMBOL(unlock_rename);
3483EXPORT_SYMBOL(vfs_create);
3484EXPORT_SYMBOL(vfs_follow_link);
3485EXPORT_SYMBOL(vfs_link);
3486EXPORT_SYMBOL(vfs_mkdir);
3487EXPORT_SYMBOL(vfs_mknod);
3488EXPORT_SYMBOL(generic_permission);
3489EXPORT_SYMBOL(vfs_readlink);
3490EXPORT_SYMBOL(vfs_rename);
3491EXPORT_SYMBOL(vfs_rmdir);
3492EXPORT_SYMBOL(vfs_symlink);
3493EXPORT_SYMBOL(vfs_unlink);
3494EXPORT_SYMBOL(dentry_unhash);
3495EXPORT_SYMBOL(generic_readlink);