blob: 494b1caa25f8f4f28339db0028e50cd72cb7925e [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
Serge E. Hallyne795b712011-03-23 16:43:25 -0700221 if (current_user_ns() != inode_userns(inode))
222 goto other_perms;
223
Linus Torvalds14067ff2011-07-25 19:55:52 -0700224 if (likely(current_fsuid() == inode->i_uid))
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700225 mode >>= 6;
226 else {
Linus Torvaldse77819e2011-07-22 19:30:19 -0700227 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
Al Viro7e401452011-06-20 19:12:17 -0400228 int error = check_acl(inode, mask);
Nick Pigginb74c79e2011-01-07 17:49:58 +1100229 if (error != -EAGAIN)
230 return error;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700231 }
232
233 if (in_group_p(inode->i_gid))
234 mode >>= 3;
235 }
236
Serge E. Hallyne795b712011-03-23 16:43:25 -0700237other_perms:
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700238 /*
239 * If the DACs are ok we don't need any capability check.
240 */
Al Viro9c2c7032011-06-20 19:06:22 -0400241 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700242 return 0;
243 return -EACCES;
244}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246/**
Nick Pigginb74c79e2011-01-07 17:49:58 +1100247 * generic_permission - check for access rights on a Posix-like filesystem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 * @inode: inode to check access rights for
Andreas Gruenbacher8fd90c82011-10-23 23:13:30 +0530249 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 *
251 * Used to check for read/write/execute permissions on a file.
252 * We use "fsuid" for this, letting us set arbitrary permissions
253 * for filesystem access without changing the "normal" uids which
Nick Pigginb74c79e2011-01-07 17:49:58 +1100254 * are used for other things.
255 *
256 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
257 * request cannot be satisfied (eg. requires blocking or too much complexity).
258 * It would then be called again in ref-walk mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 */
Al Viro2830ba72011-06-20 19:16:29 -0400260int generic_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700262 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 /*
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530265 * Do the basic permission checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 */
Al Viro7e401452011-06-20 19:12:17 -0400267 ret = acl_permission_check(inode, mask);
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700268 if (ret != -EACCES)
269 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Al Virod594e7e2011-06-20 19:55:42 -0400271 if (S_ISDIR(inode->i_mode)) {
272 /* DACs are overridable for directories */
273 if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
274 return 0;
275 if (!(mask & MAY_WRITE))
276 if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
277 return 0;
278 return -EACCES;
279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 /*
281 * Read/write DACs are always overridable.
Al Virod594e7e2011-06-20 19:55:42 -0400282 * Executable DACs are overridable when there is
283 * at least one exec bit set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 */
Al Virod594e7e2011-06-20 19:55:42 -0400285 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
Serge E. Hallyne795b712011-03-23 16:43:25 -0700286 if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return 0;
288
289 /*
290 * Searching includes executable on directories, else just read.
291 */
Serge E. Hallyn7ea66002009-12-29 14:50:19 -0600292 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
Al Virod594e7e2011-06-20 19:55:42 -0400293 if (mask == MAY_READ)
Serge E. Hallyne795b712011-03-23 16:43:25 -0700294 if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return 0;
296
297 return -EACCES;
298}
299
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700300/*
301 * We _really_ want to just do "generic_permission()" without
302 * even looking at the inode->i_op values. So we keep a cache
303 * flag in inode->i_opflags, that says "this has not special
304 * permission function, use the fast case".
305 */
306static inline int do_inode_permission(struct inode *inode, int mask)
307{
308 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
309 if (likely(inode->i_op->permission))
310 return inode->i_op->permission(inode, mask);
311
312 /* This gets set once for the inode lifetime */
313 spin_lock(&inode->i_lock);
314 inode->i_opflags |= IOP_FASTPERM;
315 spin_unlock(&inode->i_lock);
316 }
317 return generic_permission(inode, mask);
318}
319
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200320/**
321 * inode_permission - check for access rights to a given inode
322 * @inode: inode to check permission on
Andreas Gruenbacher8fd90c82011-10-23 23:13:30 +0530323 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200324 *
325 * Used to check for read/write/execute permissions on an inode.
326 * We use "fsuid" for this, letting us set arbitrary permissions
327 * for filesystem access without changing the "normal" uids which
328 * are used for other things.
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530329 *
330 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200331 */
Al Virof419a2e2008-07-22 00:07:17 -0400332int inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
Al Viroe6305c42008-07-15 21:03:57 -0400334 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700336 if (unlikely(mask & MAY_WRITE)) {
Miklos Szeredi22590e42007-10-16 23:27:08 -0700337 umode_t mode = inode->i_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 /*
340 * Nobody gets write access to a read-only fs.
341 */
342 if (IS_RDONLY(inode) &&
343 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
344 return -EROFS;
345
346 /*
347 * Nobody gets write access to an immutable file.
348 */
349 if (IS_IMMUTABLE(inode))
350 return -EACCES;
351 }
352
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700353 retval = do_inode_permission(inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (retval)
355 return retval;
356
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700357 retval = devcgroup_inode_permission(inode, mask);
358 if (retval)
359 return retval;
360
Eric Parisd09ca732010-07-23 11:43:57 -0400361 return security_inode_permission(inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}
363
Al Virof4d6ff82011-06-19 13:14:21 -0400364/**
Jan Blunck5dd784d02008-02-14 19:34:38 -0800365 * path_get - get a reference to a path
366 * @path: path to get the reference to
367 *
368 * Given a path increment the reference count to the dentry and the vfsmount.
369 */
370void path_get(struct path *path)
371{
372 mntget(path->mnt);
373 dget(path->dentry);
374}
375EXPORT_SYMBOL(path_get);
376
377/**
Jan Blunck1d957f92008-02-14 19:34:35 -0800378 * path_put - put a reference to a path
379 * @path: path to put the reference to
380 *
381 * Given a path decrement the reference count to the dentry and the vfsmount.
382 */
383void path_put(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Jan Blunck1d957f92008-02-14 19:34:35 -0800385 dput(path->dentry);
386 mntput(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
Jan Blunck1d957f92008-02-14 19:34:35 -0800388EXPORT_SYMBOL(path_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Al Viro19660af2011-03-25 10:32:48 -0400390/*
Nick Piggin31e6b012011-01-07 17:49:52 +1100391 * Path walking has 2 modes, rcu-walk and ref-walk (see
Al Viro19660af2011-03-25 10:32:48 -0400392 * Documentation/filesystems/path-lookup.txt). In situations when we can't
393 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
394 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
395 * mode. Refcounts are grabbed at the last known good point before rcu-walk
396 * got stuck, so ref-walk may continue from there. If this is not successful
397 * (eg. a seqcount has changed), then failure is returned and it's up to caller
398 * to restart the path walk from the beginning in ref-walk mode.
Nick Piggin31e6b012011-01-07 17:49:52 +1100399 */
Nick Piggin31e6b012011-01-07 17:49:52 +1100400
401/**
Al Viro19660af2011-03-25 10:32:48 -0400402 * unlazy_walk - try to switch to ref-walk mode.
403 * @nd: nameidata pathwalk data
404 * @dentry: child of nd->path.dentry or NULL
Randy Dunlap39191622011-01-08 19:36:21 -0800405 * Returns: 0 on success, -ECHILD on failure
Nick Piggin31e6b012011-01-07 17:49:52 +1100406 *
Al Viro19660af2011-03-25 10:32:48 -0400407 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
408 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
409 * @nd or NULL. Must be called from rcu-walk context.
Nick Piggin31e6b012011-01-07 17:49:52 +1100410 */
Al Viro19660af2011-03-25 10:32:48 -0400411static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
Nick Piggin31e6b012011-01-07 17:49:52 +1100412{
413 struct fs_struct *fs = current->fs;
414 struct dentry *parent = nd->path.dentry;
Al Viro5b6ca022011-03-09 23:04:47 -0500415 int want_root = 0;
Nick Piggin31e6b012011-01-07 17:49:52 +1100416
417 BUG_ON(!(nd->flags & LOOKUP_RCU));
Al Viro5b6ca022011-03-09 23:04:47 -0500418 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
419 want_root = 1;
Nick Piggin31e6b012011-01-07 17:49:52 +1100420 spin_lock(&fs->lock);
421 if (nd->root.mnt != fs->root.mnt ||
422 nd->root.dentry != fs->root.dentry)
423 goto err_root;
424 }
425 spin_lock(&parent->d_lock);
Al Viro19660af2011-03-25 10:32:48 -0400426 if (!dentry) {
427 if (!__d_rcu_to_refcount(parent, nd->seq))
428 goto err_parent;
429 BUG_ON(nd->inode != parent->d_inode);
430 } else {
Al Viro94c0d4e2011-07-12 21:40:23 -0400431 if (dentry->d_parent != parent)
432 goto err_parent;
Al Viro19660af2011-03-25 10:32:48 -0400433 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
434 if (!__d_rcu_to_refcount(dentry, nd->seq))
435 goto err_child;
436 /*
437 * If the sequence check on the child dentry passed, then
438 * the child has not been removed from its parent. This
439 * means the parent dentry must be valid and able to take
440 * a reference at this point.
441 */
442 BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
443 BUG_ON(!parent->d_count);
444 parent->d_count++;
445 spin_unlock(&dentry->d_lock);
446 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100447 spin_unlock(&parent->d_lock);
Al Viro5b6ca022011-03-09 23:04:47 -0500448 if (want_root) {
Nick Piggin31e6b012011-01-07 17:49:52 +1100449 path_get(&nd->root);
450 spin_unlock(&fs->lock);
451 }
452 mntget(nd->path.mnt);
453
454 rcu_read_unlock();
455 br_read_unlock(vfsmount_lock);
456 nd->flags &= ~LOOKUP_RCU;
457 return 0;
Al Viro19660af2011-03-25 10:32:48 -0400458
459err_child:
Nick Piggin31e6b012011-01-07 17:49:52 +1100460 spin_unlock(&dentry->d_lock);
Al Viro19660af2011-03-25 10:32:48 -0400461err_parent:
Nick Piggin31e6b012011-01-07 17:49:52 +1100462 spin_unlock(&parent->d_lock);
463err_root:
Al Viro5b6ca022011-03-09 23:04:47 -0500464 if (want_root)
Nick Piggin31e6b012011-01-07 17:49:52 +1100465 spin_unlock(&fs->lock);
466 return -ECHILD;
467}
468
Nick Piggin31e6b012011-01-07 17:49:52 +1100469/**
Trond Myklebust834f2a42005-10-18 14:20:16 -0700470 * release_open_intent - free up open intent resources
471 * @nd: pointer to nameidata
472 */
473void release_open_intent(struct nameidata *nd)
474{
Linus Torvalds2dab5972011-02-11 15:53:38 -0800475 struct file *file = nd->intent.open.file;
476
477 if (file && !IS_ERR(file)) {
478 if (file->f_path.dentry == NULL)
479 put_filp(file);
480 else
481 fput(file);
482 }
Trond Myklebust834f2a42005-10-18 14:20:16 -0700483}
484
Al Virof60aef72011-02-15 01:35:28 -0500485static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
Nick Piggin34286d62011-01-07 17:49:57 +1100486{
Al Virof60aef72011-02-15 01:35:28 -0500487 return dentry->d_op->d_revalidate(dentry, nd);
Nick Piggin34286d62011-01-07 17:49:57 +1100488}
489
Al Viro9f1fafe2011-03-25 11:00:12 -0400490/**
491 * complete_walk - successful completion of path walk
492 * @nd: pointer nameidata
Jeff Layton39159de2009-12-07 12:01:50 -0500493 *
Al Viro9f1fafe2011-03-25 11:00:12 -0400494 * If we had been in RCU mode, drop out of it and legitimize nd->path.
495 * Revalidate the final result, unless we'd already done that during
496 * the path walk or the filesystem doesn't ask for it. Return 0 on
497 * success, -error on failure. In case of failure caller does not
498 * need to drop nd->path.
Jeff Layton39159de2009-12-07 12:01:50 -0500499 */
Al Viro9f1fafe2011-03-25 11:00:12 -0400500static int complete_walk(struct nameidata *nd)
Jeff Layton39159de2009-12-07 12:01:50 -0500501{
Al Viro16c2cd72011-02-22 15:50:10 -0500502 struct dentry *dentry = nd->path.dentry;
Jeff Layton39159de2009-12-07 12:01:50 -0500503 int status;
Jeff Layton39159de2009-12-07 12:01:50 -0500504
Al Viro9f1fafe2011-03-25 11:00:12 -0400505 if (nd->flags & LOOKUP_RCU) {
506 nd->flags &= ~LOOKUP_RCU;
507 if (!(nd->flags & LOOKUP_ROOT))
508 nd->root.mnt = NULL;
509 spin_lock(&dentry->d_lock);
510 if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
511 spin_unlock(&dentry->d_lock);
512 rcu_read_unlock();
513 br_read_unlock(vfsmount_lock);
514 return -ECHILD;
515 }
516 BUG_ON(nd->inode != dentry->d_inode);
517 spin_unlock(&dentry->d_lock);
518 mntget(nd->path.mnt);
519 rcu_read_unlock();
520 br_read_unlock(vfsmount_lock);
521 }
522
Al Viro16c2cd72011-02-22 15:50:10 -0500523 if (likely(!(nd->flags & LOOKUP_JUMPED)))
Jeff Layton39159de2009-12-07 12:01:50 -0500524 return 0;
525
Al Viro16c2cd72011-02-22 15:50:10 -0500526 if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
527 return 0;
528
529 if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
530 return 0;
531
532 /* Note: we do not d_invalidate() */
Nick Piggin34286d62011-01-07 17:49:57 +1100533 status = d_revalidate(dentry, nd);
Jeff Layton39159de2009-12-07 12:01:50 -0500534 if (status > 0)
535 return 0;
536
Al Viro16c2cd72011-02-22 15:50:10 -0500537 if (!status)
Jeff Layton39159de2009-12-07 12:01:50 -0500538 status = -ESTALE;
Al Viro16c2cd72011-02-22 15:50:10 -0500539
Al Viro9f1fafe2011-03-25 11:00:12 -0400540 path_put(&nd->path);
Jeff Layton39159de2009-12-07 12:01:50 -0500541 return status;
542}
543
Al Viro2a737872009-04-07 11:49:53 -0400544static __always_inline void set_root(struct nameidata *nd)
545{
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200546 if (!nd->root.mnt)
547 get_fs_root(current->fs, &nd->root);
Al Viro2a737872009-04-07 11:49:53 -0400548}
549
Al Viro6de88d72009-08-09 01:41:57 +0400550static int link_path_walk(const char *, struct nameidata *);
551
Nick Piggin31e6b012011-01-07 17:49:52 +1100552static __always_inline void set_root_rcu(struct nameidata *nd)
553{
554 if (!nd->root.mnt) {
555 struct fs_struct *fs = current->fs;
Nick Pigginc28cc362011-01-07 17:49:53 +1100556 unsigned seq;
557
558 do {
559 seq = read_seqcount_begin(&fs->seq);
560 nd->root = fs->root;
Tim Chenc1530012011-04-15 11:39:29 -0700561 nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
Nick Pigginc28cc362011-01-07 17:49:53 +1100562 } while (read_seqcount_retry(&fs->seq, seq));
Nick Piggin31e6b012011-01-07 17:49:52 +1100563 }
564}
565
Arjan van de Venf1662352006-01-14 13:21:31 -0800566static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Nick Piggin31e6b012011-01-07 17:49:52 +1100568 int ret;
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if (IS_ERR(link))
571 goto fail;
572
573 if (*link == '/') {
Al Viro2a737872009-04-07 11:49:53 -0400574 set_root(nd);
Jan Blunck1d957f92008-02-14 19:34:35 -0800575 path_put(&nd->path);
Al Viro2a737872009-04-07 11:49:53 -0400576 nd->path = nd->root;
577 path_get(&nd->root);
Al Viro16c2cd72011-02-22 15:50:10 -0500578 nd->flags |= LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100580 nd->inode = nd->path.dentry->d_inode;
Christoph Hellwigb4091d52008-11-05 15:07:21 +0100581
Nick Piggin31e6b012011-01-07 17:49:52 +1100582 ret = link_path_walk(link, nd);
583 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584fail:
Jan Blunck1d957f92008-02-14 19:34:35 -0800585 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 return PTR_ERR(link);
587}
588
Jan Blunck1d957f92008-02-14 19:34:35 -0800589static void path_put_conditional(struct path *path, struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700590{
591 dput(path->dentry);
Jan Blunck4ac91372008-02-14 19:34:32 -0800592 if (path->mnt != nd->path.mnt)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700593 mntput(path->mnt);
594}
595
Nick Piggin7b9337a2011-01-14 08:42:43 +0000596static inline void path_to_nameidata(const struct path *path,
597 struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700598{
Nick Piggin31e6b012011-01-07 17:49:52 +1100599 if (!(nd->flags & LOOKUP_RCU)) {
600 dput(nd->path.dentry);
601 if (nd->path.mnt != path->mnt)
602 mntput(nd->path.mnt);
Huang Shijie9a229682010-04-02 17:37:13 +0800603 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100604 nd->path.mnt = path->mnt;
Jan Blunck4ac91372008-02-14 19:34:32 -0800605 nd->path.dentry = path->dentry;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700606}
607
Al Viro574197e2011-03-14 22:20:34 -0400608static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
609{
610 struct inode *inode = link->dentry->d_inode;
611 if (!IS_ERR(cookie) && inode->i_op->put_link)
612 inode->i_op->put_link(link->dentry, nd, cookie);
613 path_put(link);
614}
615
Al Virodef4af32009-12-26 08:37:05 -0500616static __always_inline int
Al Viro574197e2011-03-14 22:20:34 -0400617follow_link(struct path *link, struct nameidata *nd, void **p)
Ian Kent051d3812006-03-27 01:14:53 -0800618{
619 int error;
Nick Piggin7b9337a2011-01-14 08:42:43 +0000620 struct dentry *dentry = link->dentry;
Ian Kent051d3812006-03-27 01:14:53 -0800621
Al Viro844a3912011-02-15 00:38:26 -0500622 BUG_ON(nd->flags & LOOKUP_RCU);
623
Al Viro0e794582011-03-16 02:45:02 -0400624 if (link->mnt == nd->path.mnt)
625 mntget(link->mnt);
626
Al Viro574197e2011-03-14 22:20:34 -0400627 if (unlikely(current->total_link_count >= 40)) {
628 *p = ERR_PTR(-ELOOP); /* no ->put_link(), please */
Al Viro574197e2011-03-14 22:20:34 -0400629 path_put(&nd->path);
630 return -ELOOP;
631 }
632 cond_resched();
633 current->total_link_count++;
634
Al Viro68ac1232012-03-15 08:21:57 -0400635 touch_atime(link);
Ian Kent051d3812006-03-27 01:14:53 -0800636 nd_set_link(nd, NULL);
637
Al Viro36f3b4f2011-02-22 21:24:38 -0500638 error = security_inode_follow_link(link->dentry, nd);
639 if (error) {
640 *p = ERR_PTR(error); /* no ->put_link(), please */
641 path_put(&nd->path);
642 return error;
643 }
644
Al Viro86acdca12009-12-22 23:45:11 -0500645 nd->last_type = LAST_BIND;
Al Virodef4af32009-12-26 08:37:05 -0500646 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
647 error = PTR_ERR(*p);
648 if (!IS_ERR(*p)) {
Ian Kent051d3812006-03-27 01:14:53 -0800649 char *s = nd_get_link(nd);
650 error = 0;
651 if (s)
652 error = __vfs_follow_link(nd, s);
Al Virobcda7652011-03-13 16:42:14 -0400653 else if (nd->last_type == LAST_BIND) {
Al Viro16c2cd72011-02-22 15:50:10 -0500654 nd->flags |= LOOKUP_JUMPED;
Al Virob21041d2011-03-14 20:01:51 -0400655 nd->inode = nd->path.dentry->d_inode;
656 if (nd->inode->i_op->follow_link) {
Al Virobcda7652011-03-13 16:42:14 -0400657 /* stepped on a _really_ weird one */
658 path_put(&nd->path);
659 error = -ELOOP;
660 }
661 }
Ian Kent051d3812006-03-27 01:14:53 -0800662 }
Ian Kent051d3812006-03-27 01:14:53 -0800663 return error;
664}
665
Nick Piggin31e6b012011-01-07 17:49:52 +1100666static int follow_up_rcu(struct path *path)
667{
Al Viro0714a532011-11-24 22:19:58 -0500668 struct mount *mnt = real_mount(path->mnt);
669 struct mount *parent;
Nick Piggin31e6b012011-01-07 17:49:52 +1100670 struct dentry *mountpoint;
671
Al Viro0714a532011-11-24 22:19:58 -0500672 parent = mnt->mnt_parent;
673 if (&parent->mnt == path->mnt)
Nick Piggin31e6b012011-01-07 17:49:52 +1100674 return 0;
Al Viroa73324d2011-11-24 22:25:07 -0500675 mountpoint = mnt->mnt_mountpoint;
Nick Piggin31e6b012011-01-07 17:49:52 +1100676 path->dentry = mountpoint;
Al Viro0714a532011-11-24 22:19:58 -0500677 path->mnt = &parent->mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +1100678 return 1;
679}
680
Al Virobab77eb2009-04-18 03:26:48 -0400681int follow_up(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
Al Viro0714a532011-11-24 22:19:58 -0500683 struct mount *mnt = real_mount(path->mnt);
684 struct mount *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 struct dentry *mountpoint;
Nick Piggin99b7db72010-08-18 04:37:39 +1000686
687 br_read_lock(vfsmount_lock);
Al Viro0714a532011-11-24 22:19:58 -0500688 parent = mnt->mnt_parent;
689 if (&parent->mnt == path->mnt) {
Nick Piggin99b7db72010-08-18 04:37:39 +1000690 br_read_unlock(vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 return 0;
692 }
Al Viro0714a532011-11-24 22:19:58 -0500693 mntget(&parent->mnt);
Al Viroa73324d2011-11-24 22:25:07 -0500694 mountpoint = dget(mnt->mnt_mountpoint);
Nick Piggin99b7db72010-08-18 04:37:39 +1000695 br_read_unlock(vfsmount_lock);
Al Virobab77eb2009-04-18 03:26:48 -0400696 dput(path->dentry);
697 path->dentry = mountpoint;
698 mntput(path->mnt);
Al Viro0714a532011-11-24 22:19:58 -0500699 path->mnt = &parent->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return 1;
701}
702
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100703/*
David Howells9875cf82011-01-14 18:45:21 +0000704 * Perform an automount
705 * - return -EISDIR to tell follow_managed() to stop and return the path we
706 * were called with.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 */
David Howells9875cf82011-01-14 18:45:21 +0000708static int follow_automount(struct path *path, unsigned flags,
709 bool *need_mntput)
Nick Piggin31e6b012011-01-07 17:49:52 +1100710{
David Howells9875cf82011-01-14 18:45:21 +0000711 struct vfsmount *mnt;
David Howellsea5b7782011-01-14 19:10:03 +0000712 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +1100713
David Howells9875cf82011-01-14 18:45:21 +0000714 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
715 return -EREMOTE;
Al Viro463ffb22005-06-06 13:36:05 -0700716
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200717 /* We don't want to mount if someone's just doing a stat -
718 * unless they're stat'ing a directory and appended a '/' to
719 * the name.
720 *
721 * We do, however, want to mount if someone wants to open or
722 * create a file of any type under the mountpoint, wants to
723 * traverse through the mountpoint or wants to open the
724 * mounted directory. Also, autofs may mark negative dentries
725 * as being automount points. These will need the attentions
726 * of the daemon to instantiate them before they can be used.
David Howells9875cf82011-01-14 18:45:21 +0000727 */
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200728 if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
Linus Torvaldsd94c1772011-09-26 17:44:55 -0700729 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200730 path->dentry->d_inode)
731 return -EISDIR;
732
David Howells9875cf82011-01-14 18:45:21 +0000733 current->total_link_count++;
734 if (current->total_link_count >= 40)
735 return -ELOOP;
736
737 mnt = path->dentry->d_op->d_automount(path);
738 if (IS_ERR(mnt)) {
739 /*
740 * The filesystem is allowed to return -EISDIR here to indicate
741 * it doesn't want to automount. For instance, autofs would do
742 * this so that its userspace daemon can mount on this dentry.
743 *
744 * However, we can only permit this if it's a terminal point in
745 * the path being looked up; if it wasn't then the remainder of
746 * the path is inaccessible and we should say so.
747 */
Al Viro49084c32011-06-25 21:59:52 -0400748 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
David Howells9875cf82011-01-14 18:45:21 +0000749 return -EREMOTE;
750 return PTR_ERR(mnt);
751 }
David Howellsea5b7782011-01-14 19:10:03 +0000752
David Howells9875cf82011-01-14 18:45:21 +0000753 if (!mnt) /* mount collision */
754 return 0;
755
Al Viro8aef1882011-06-16 15:10:06 +0100756 if (!*need_mntput) {
757 /* lock_mount() may release path->mnt on error */
758 mntget(path->mnt);
759 *need_mntput = true;
760 }
Al Viro19a167a2011-01-17 01:35:23 -0500761 err = finish_automount(mnt, path);
David Howellsea5b7782011-01-14 19:10:03 +0000762
David Howellsea5b7782011-01-14 19:10:03 +0000763 switch (err) {
764 case -EBUSY:
765 /* Someone else made a mount here whilst we were busy */
Al Viro19a167a2011-01-17 01:35:23 -0500766 return 0;
David Howellsea5b7782011-01-14 19:10:03 +0000767 case 0:
Al Viro8aef1882011-06-16 15:10:06 +0100768 path_put(path);
David Howellsea5b7782011-01-14 19:10:03 +0000769 path->mnt = mnt;
770 path->dentry = dget(mnt->mnt_root);
David Howellsea5b7782011-01-14 19:10:03 +0000771 return 0;
Al Viro19a167a2011-01-17 01:35:23 -0500772 default:
773 return err;
David Howellsea5b7782011-01-14 19:10:03 +0000774 }
Al Viro19a167a2011-01-17 01:35:23 -0500775
David Howells9875cf82011-01-14 18:45:21 +0000776}
777
778/*
779 * Handle a dentry that is managed in some way.
David Howellscc53ce52011-01-14 18:45:26 +0000780 * - Flagged for transit management (autofs)
David Howells9875cf82011-01-14 18:45:21 +0000781 * - Flagged as mountpoint
782 * - Flagged as automount point
783 *
784 * This may only be called in refwalk mode.
785 *
786 * Serialization is taken care of in namespace.c
787 */
788static int follow_managed(struct path *path, unsigned flags)
789{
Al Viro8aef1882011-06-16 15:10:06 +0100790 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
David Howells9875cf82011-01-14 18:45:21 +0000791 unsigned managed;
792 bool need_mntput = false;
Al Viro8aef1882011-06-16 15:10:06 +0100793 int ret = 0;
David Howells9875cf82011-01-14 18:45:21 +0000794
795 /* Given that we're not holding a lock here, we retain the value in a
796 * local variable for each dentry as we look at it so that we don't see
797 * the components of that value change under us */
798 while (managed = ACCESS_ONCE(path->dentry->d_flags),
799 managed &= DCACHE_MANAGED_DENTRY,
800 unlikely(managed != 0)) {
David Howellscc53ce52011-01-14 18:45:26 +0000801 /* Allow the filesystem to manage the transit without i_mutex
802 * being held. */
803 if (managed & DCACHE_MANAGE_TRANSIT) {
804 BUG_ON(!path->dentry->d_op);
805 BUG_ON(!path->dentry->d_op->d_manage);
Al Viro1aed3e42011-03-18 09:09:02 -0400806 ret = path->dentry->d_op->d_manage(path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +0000807 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +0100808 break;
David Howellscc53ce52011-01-14 18:45:26 +0000809 }
810
David Howells9875cf82011-01-14 18:45:21 +0000811 /* Transit to a mounted filesystem. */
812 if (managed & DCACHE_MOUNTED) {
813 struct vfsmount *mounted = lookup_mnt(path);
814 if (mounted) {
815 dput(path->dentry);
816 if (need_mntput)
817 mntput(path->mnt);
818 path->mnt = mounted;
819 path->dentry = dget(mounted->mnt_root);
820 need_mntput = true;
821 continue;
822 }
823
824 /* Something is mounted on this dentry in another
825 * namespace and/or whatever was mounted there in this
826 * namespace got unmounted before we managed to get the
827 * vfsmount_lock */
828 }
829
830 /* Handle an automount point */
831 if (managed & DCACHE_NEED_AUTOMOUNT) {
832 ret = follow_automount(path, flags, &need_mntput);
833 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +0100834 break;
David Howells9875cf82011-01-14 18:45:21 +0000835 continue;
836 }
837
838 /* We didn't change the current path point */
839 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
Al Viro8aef1882011-06-16 15:10:06 +0100841
842 if (need_mntput && path->mnt == mnt)
843 mntput(path->mnt);
844 if (ret == -EISDIR)
845 ret = 0;
Al Viroa3fbbde2011-11-07 21:21:26 +0000846 return ret < 0 ? ret : need_mntput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}
848
David Howellscc53ce52011-01-14 18:45:26 +0000849int follow_down_one(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
851 struct vfsmount *mounted;
852
Al Viro1c755af2009-04-18 14:06:57 -0400853 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -0400855 dput(path->dentry);
856 mntput(path->mnt);
857 path->mnt = mounted;
858 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 return 1;
860 }
861 return 0;
862}
863
Ian Kent62a73752011-03-25 01:51:02 +0800864static inline bool managed_dentry_might_block(struct dentry *dentry)
865{
866 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
867 dentry->d_op->d_manage(dentry, true) < 0);
868}
869
David Howells9875cf82011-01-14 18:45:21 +0000870/*
Al Viro287548e2011-05-27 06:50:06 -0400871 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
872 * we meet a managed dentry that would need blocking.
David Howells9875cf82011-01-14 18:45:21 +0000873 */
874static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
Al Viro287548e2011-05-27 06:50:06 -0400875 struct inode **inode)
David Howells9875cf82011-01-14 18:45:21 +0000876{
Ian Kent62a73752011-03-25 01:51:02 +0800877 for (;;) {
Al Viroc7105362011-11-24 18:22:03 -0500878 struct mount *mounted;
Ian Kent62a73752011-03-25 01:51:02 +0800879 /*
880 * Don't forget we might have a non-mountpoint managed dentry
881 * that wants to block transit.
882 */
Al Viro287548e2011-05-27 06:50:06 -0400883 if (unlikely(managed_dentry_might_block(path->dentry)))
David Howellsab909112011-01-14 18:46:51 +0000884 return false;
Ian Kent62a73752011-03-25 01:51:02 +0800885
886 if (!d_mountpoint(path->dentry))
887 break;
888
David Howells9875cf82011-01-14 18:45:21 +0000889 mounted = __lookup_mnt(path->mnt, path->dentry, 1);
890 if (!mounted)
891 break;
Al Viroc7105362011-11-24 18:22:03 -0500892 path->mnt = &mounted->mnt;
893 path->dentry = mounted->mnt.mnt_root;
Al Viroa3fbbde2011-11-07 21:21:26 +0000894 nd->flags |= LOOKUP_JUMPED;
David Howells9875cf82011-01-14 18:45:21 +0000895 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
Linus Torvalds59430262011-07-18 15:43:29 -0700896 /*
897 * Update the inode too. We don't need to re-check the
898 * dentry sequence number here after this d_inode read,
899 * because a mount-point is always pinned.
900 */
901 *inode = path->dentry->d_inode;
David Howells9875cf82011-01-14 18:45:21 +0000902 }
David Howells9875cf82011-01-14 18:45:21 +0000903 return true;
904}
905
Al Virodea39372011-05-27 06:53:39 -0400906static void follow_mount_rcu(struct nameidata *nd)
Al Viro287548e2011-05-27 06:50:06 -0400907{
Al Virodea39372011-05-27 06:53:39 -0400908 while (d_mountpoint(nd->path.dentry)) {
Al Viroc7105362011-11-24 18:22:03 -0500909 struct mount *mounted;
Al Virodea39372011-05-27 06:53:39 -0400910 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
Al Viro287548e2011-05-27 06:50:06 -0400911 if (!mounted)
912 break;
Al Viroc7105362011-11-24 18:22:03 -0500913 nd->path.mnt = &mounted->mnt;
914 nd->path.dentry = mounted->mnt.mnt_root;
Al Virodea39372011-05-27 06:53:39 -0400915 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro287548e2011-05-27 06:50:06 -0400916 }
917}
918
Nick Piggin31e6b012011-01-07 17:49:52 +1100919static int follow_dotdot_rcu(struct nameidata *nd)
920{
Nick Piggin31e6b012011-01-07 17:49:52 +1100921 set_root_rcu(nd);
922
David Howells9875cf82011-01-14 18:45:21 +0000923 while (1) {
Nick Piggin31e6b012011-01-07 17:49:52 +1100924 if (nd->path.dentry == nd->root.dentry &&
925 nd->path.mnt == nd->root.mnt) {
926 break;
927 }
928 if (nd->path.dentry != nd->path.mnt->mnt_root) {
929 struct dentry *old = nd->path.dentry;
930 struct dentry *parent = old->d_parent;
931 unsigned seq;
932
933 seq = read_seqcount_begin(&parent->d_seq);
934 if (read_seqcount_retry(&old->d_seq, nd->seq))
Al Viroef7562d2011-03-04 14:35:59 -0500935 goto failed;
Nick Piggin31e6b012011-01-07 17:49:52 +1100936 nd->path.dentry = parent;
937 nd->seq = seq;
938 break;
939 }
940 if (!follow_up_rcu(&nd->path))
941 break;
942 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Nick Piggin31e6b012011-01-07 17:49:52 +1100943 }
Al Virodea39372011-05-27 06:53:39 -0400944 follow_mount_rcu(nd);
945 nd->inode = nd->path.dentry->d_inode;
Nick Piggin31e6b012011-01-07 17:49:52 +1100946 return 0;
Al Viroef7562d2011-03-04 14:35:59 -0500947
948failed:
949 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -0500950 if (!(nd->flags & LOOKUP_ROOT))
951 nd->root.mnt = NULL;
Al Viroef7562d2011-03-04 14:35:59 -0500952 rcu_read_unlock();
953 br_read_unlock(vfsmount_lock);
954 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +1100955}
956
David Howells9875cf82011-01-14 18:45:21 +0000957/*
David Howellscc53ce52011-01-14 18:45:26 +0000958 * Follow down to the covering mount currently visible to userspace. At each
959 * point, the filesystem owning that dentry may be queried as to whether the
960 * caller is permitted to proceed or not.
David Howellscc53ce52011-01-14 18:45:26 +0000961 */
Al Viro7cc90cc2011-03-18 09:04:20 -0400962int follow_down(struct path *path)
David Howellscc53ce52011-01-14 18:45:26 +0000963{
964 unsigned managed;
965 int ret;
966
967 while (managed = ACCESS_ONCE(path->dentry->d_flags),
968 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
969 /* Allow the filesystem to manage the transit without i_mutex
970 * being held.
971 *
972 * We indicate to the filesystem if someone is trying to mount
973 * something here. This gives autofs the chance to deny anyone
974 * other than its daemon the right to mount on its
975 * superstructure.
976 *
977 * The filesystem may sleep at this point.
978 */
979 if (managed & DCACHE_MANAGE_TRANSIT) {
980 BUG_ON(!path->dentry->d_op);
981 BUG_ON(!path->dentry->d_op->d_manage);
David Howellsab909112011-01-14 18:46:51 +0000982 ret = path->dentry->d_op->d_manage(
Al Viro1aed3e42011-03-18 09:09:02 -0400983 path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +0000984 if (ret < 0)
985 return ret == -EISDIR ? 0 : ret;
986 }
987
988 /* Transit to a mounted filesystem. */
989 if (managed & DCACHE_MOUNTED) {
990 struct vfsmount *mounted = lookup_mnt(path);
991 if (!mounted)
992 break;
993 dput(path->dentry);
994 mntput(path->mnt);
995 path->mnt = mounted;
996 path->dentry = dget(mounted->mnt_root);
997 continue;
998 }
999
1000 /* Don't handle automount points here */
1001 break;
1002 }
1003 return 0;
1004}
1005
1006/*
David Howells9875cf82011-01-14 18:45:21 +00001007 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1008 */
1009static void follow_mount(struct path *path)
1010{
1011 while (d_mountpoint(path->dentry)) {
1012 struct vfsmount *mounted = lookup_mnt(path);
1013 if (!mounted)
1014 break;
1015 dput(path->dentry);
1016 mntput(path->mnt);
1017 path->mnt = mounted;
1018 path->dentry = dget(mounted->mnt_root);
1019 }
1020}
1021
Nick Piggin31e6b012011-01-07 17:49:52 +11001022static void follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
Al Viro2a737872009-04-07 11:49:53 -04001024 set_root(nd);
Andreas Mohre518ddb2006-09-29 02:01:22 -07001025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 while(1) {
Jan Blunck4ac91372008-02-14 19:34:32 -08001027 struct dentry *old = nd->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Al Viro2a737872009-04-07 11:49:53 -04001029 if (nd->path.dentry == nd->root.dentry &&
1030 nd->path.mnt == nd->root.mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 break;
1032 }
Jan Blunck4ac91372008-02-14 19:34:32 -08001033 if (nd->path.dentry != nd->path.mnt->mnt_root) {
Al Viro3088dd72010-01-30 15:47:29 -05001034 /* rare case of legitimate dget_parent()... */
1035 nd->path.dentry = dget_parent(nd->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 dput(old);
1037 break;
1038 }
Al Viro3088dd72010-01-30 15:47:29 -05001039 if (!follow_up(&nd->path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 }
Al Viro79ed0222009-04-18 13:59:41 -04001042 follow_mount(&nd->path);
Nick Piggin31e6b012011-01-07 17:49:52 +11001043 nd->inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044}
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046/*
Miklos Szeredibad61182012-03-26 12:54:24 +02001047 * This looks up the name in dcache, possibly revalidates the old dentry and
1048 * allocates a new one if not found or not valid. In the need_lookup argument
1049 * returns whether i_op->lookup is necessary.
1050 *
1051 * dir->d_inode->i_mutex must be held
Nick Pigginbaa03892010-08-18 04:37:31 +10001052 */
Miklos Szeredibad61182012-03-26 12:54:24 +02001053static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
1054 struct nameidata *nd, bool *need_lookup)
Nick Pigginbaa03892010-08-18 04:37:31 +10001055{
Nick Pigginbaa03892010-08-18 04:37:31 +10001056 struct dentry *dentry;
Miklos Szeredibad61182012-03-26 12:54:24 +02001057 int error;
Nick Pigginbaa03892010-08-18 04:37:31 +10001058
Miklos Szeredibad61182012-03-26 12:54:24 +02001059 *need_lookup = false;
1060 dentry = d_lookup(dir, name);
1061 if (dentry) {
1062 if (d_need_lookup(dentry)) {
1063 *need_lookup = true;
1064 } else if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
1065 error = d_revalidate(dentry, nd);
1066 if (unlikely(error <= 0)) {
1067 if (error < 0) {
1068 dput(dentry);
1069 return ERR_PTR(error);
1070 } else if (!d_invalidate(dentry)) {
1071 dput(dentry);
1072 dentry = NULL;
1073 }
1074 }
1075 }
1076 }
Nick Pigginbaa03892010-08-18 04:37:31 +10001077
Miklos Szeredibad61182012-03-26 12:54:24 +02001078 if (!dentry) {
1079 dentry = d_alloc(dir, name);
1080 if (unlikely(!dentry))
1081 return ERR_PTR(-ENOMEM);
Nick Pigginbaa03892010-08-18 04:37:31 +10001082
Miklos Szeredibad61182012-03-26 12:54:24 +02001083 *need_lookup = true;
Nick Pigginbaa03892010-08-18 04:37:31 +10001084 }
1085 return dentry;
1086}
1087
1088/*
Miklos Szeredibad61182012-03-26 12:54:24 +02001089 * Call i_op->lookup on the dentry. The dentry must be negative but may be
1090 * hashed if it was pouplated with DCACHE_NEED_LOOKUP.
1091 *
1092 * dir->d_inode->i_mutex must be held
Josef Bacik44396f42011-05-31 11:58:49 -04001093 */
Miklos Szeredibad61182012-03-26 12:54:24 +02001094static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
1095 struct nameidata *nd)
Josef Bacik44396f42011-05-31 11:58:49 -04001096{
Josef Bacik44396f42011-05-31 11:58:49 -04001097 struct dentry *old;
1098
1099 /* Don't create child dentry for a dead directory. */
Miklos Szeredibad61182012-03-26 12:54:24 +02001100 if (unlikely(IS_DEADDIR(dir))) {
Miklos Szeredie188dc02012-02-03 14:25:18 +01001101 dput(dentry);
Josef Bacik44396f42011-05-31 11:58:49 -04001102 return ERR_PTR(-ENOENT);
Miklos Szeredie188dc02012-02-03 14:25:18 +01001103 }
Josef Bacik44396f42011-05-31 11:58:49 -04001104
Miklos Szeredibad61182012-03-26 12:54:24 +02001105 old = dir->i_op->lookup(dir, dentry, nd);
Josef Bacik44396f42011-05-31 11:58:49 -04001106 if (unlikely(old)) {
1107 dput(dentry);
1108 dentry = old;
1109 }
1110 return dentry;
1111}
1112
Al Viroa3255542012-03-30 14:41:51 -04001113static struct dentry *__lookup_hash(struct qstr *name,
1114 struct dentry *base, struct nameidata *nd)
1115{
Miklos Szeredibad61182012-03-26 12:54:24 +02001116 bool need_lookup;
Al Viroa3255542012-03-30 14:41:51 -04001117 struct dentry *dentry;
1118
Miklos Szeredibad61182012-03-26 12:54:24 +02001119 dentry = lookup_dcache(name, base, nd, &need_lookup);
1120 if (!need_lookup)
1121 return dentry;
Al Viroa3255542012-03-30 14:41:51 -04001122
Miklos Szeredibad61182012-03-26 12:54:24 +02001123 return lookup_real(base->d_inode, dentry, nd);
Al Viroa3255542012-03-30 14:41:51 -04001124}
1125
Josef Bacik44396f42011-05-31 11:58:49 -04001126/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 * It's more convoluted than I'd like it to be, but... it's still fairly
1128 * small and for now I'd prefer to have fast path as straight as possible.
1129 * It _is_ time-critical.
1130 */
1131static int do_lookup(struct nameidata *nd, struct qstr *name,
Nick Piggin31e6b012011-01-07 17:49:52 +11001132 struct path *path, struct inode **inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133{
Jan Blunck4ac91372008-02-14 19:34:32 -08001134 struct vfsmount *mnt = nd->path.mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +11001135 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro5a18fff2011-03-11 04:44:53 -05001136 int need_reval = 1;
1137 int status = 1;
David Howells9875cf82011-01-14 18:45:21 +00001138 int err;
1139
Al Viro3cac2602009-08-13 18:27:43 +04001140 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001141 * Rename seqlock is not required here because in the off chance
1142 * of a false negative due to a concurrent rename, we're going to
1143 * do the non-racy lookup, below.
1144 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001145 if (nd->flags & LOOKUP_RCU) {
1146 unsigned seq;
Nick Piggin31e6b012011-01-07 17:49:52 +11001147 *inode = nd->inode;
1148 dentry = __d_lookup_rcu(parent, name, &seq, inode);
Al Viro5a18fff2011-03-11 04:44:53 -05001149 if (!dentry)
1150 goto unlazy;
1151
Nick Piggin31e6b012011-01-07 17:49:52 +11001152 /* Memory barrier in read_seqcount_begin of child is enough */
1153 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1154 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001155 nd->seq = seq;
Al Viro5a18fff2011-03-11 04:44:53 -05001156
Miklos Szeredifa4ee152012-03-26 12:54:19 +02001157 if (unlikely(d_need_lookup(dentry)))
1158 goto unlazy;
Al Viro24643082011-02-15 01:26:22 -05001159 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
Al Viro5a18fff2011-03-11 04:44:53 -05001160 status = d_revalidate(dentry, nd);
1161 if (unlikely(status <= 0)) {
1162 if (status != -ECHILD)
1163 need_reval = 0;
1164 goto unlazy;
1165 }
Al Viro24643082011-02-15 01:26:22 -05001166 }
Nick Piggin31e6b012011-01-07 17:49:52 +11001167 path->mnt = mnt;
1168 path->dentry = dentry;
Al Virod6e9bd22011-05-27 07:03:15 -04001169 if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1170 goto unlazy;
1171 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1172 goto unlazy;
1173 return 0;
Al Viro5a18fff2011-03-11 04:44:53 -05001174unlazy:
Al Viro19660af2011-03-25 10:32:48 -04001175 if (unlazy_walk(nd, dentry))
1176 return -ECHILD;
Al Viro5a18fff2011-03-11 04:44:53 -05001177 } else {
1178 dentry = __d_lookup(parent, name);
Nick Piggin31e6b012011-01-07 17:49:52 +11001179 }
Al Viro5a18fff2011-03-11 04:44:53 -05001180
Al Viro81e6f522012-03-30 14:48:04 -04001181 if (unlikely(!dentry))
1182 goto need_lookup;
Al Viro5a18fff2011-03-11 04:44:53 -05001183
Al Viro81e6f522012-03-30 14:48:04 -04001184 if (unlikely(d_need_lookup(dentry))) {
1185 dput(dentry);
1186 goto need_lookup;
Al Viro24643082011-02-15 01:26:22 -05001187 }
Al Viro81e6f522012-03-30 14:48:04 -04001188
Al Viro5a18fff2011-03-11 04:44:53 -05001189 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
1190 status = d_revalidate(dentry, nd);
1191 if (unlikely(status <= 0)) {
1192 if (status < 0) {
1193 dput(dentry);
1194 return status;
1195 }
1196 if (!d_invalidate(dentry)) {
1197 dput(dentry);
Al Viro81e6f522012-03-30 14:48:04 -04001198 goto need_lookup;
Al Viro5a18fff2011-03-11 04:44:53 -05001199 }
1200 }
Al Viro3f6c7c72012-03-30 14:04:16 -04001201done:
David Howells9875cf82011-01-14 18:45:21 +00001202 path->mnt = mnt;
1203 path->dentry = dentry;
1204 err = follow_managed(path, nd->flags);
Ian Kent89312212011-01-18 12:06:10 +08001205 if (unlikely(err < 0)) {
1206 path_put_conditional(path, nd);
David Howells9875cf82011-01-14 18:45:21 +00001207 return err;
Ian Kent89312212011-01-18 12:06:10 +08001208 }
Al Viroa3fbbde2011-11-07 21:21:26 +00001209 if (err)
1210 nd->flags |= LOOKUP_JUMPED;
David Howells9875cf82011-01-14 18:45:21 +00001211 *inode = path->dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 return 0;
Al Viro81e6f522012-03-30 14:48:04 -04001213
1214need_lookup:
1215 BUG_ON(nd->inode != parent->d_inode);
1216
1217 mutex_lock(&parent->d_inode->i_mutex);
1218 dentry = __lookup_hash(name, parent, nd);
1219 mutex_unlock(&parent->d_inode->i_mutex);
1220 if (IS_ERR(dentry))
1221 return PTR_ERR(dentry);
1222 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223}
1224
Al Viro52094c82011-02-21 21:34:47 -05001225static inline int may_lookup(struct nameidata *nd)
1226{
1227 if (nd->flags & LOOKUP_RCU) {
Al Viro4ad5abb2011-06-20 19:57:03 -04001228 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
Al Viro52094c82011-02-21 21:34:47 -05001229 if (err != -ECHILD)
1230 return err;
Al Viro19660af2011-03-25 10:32:48 -04001231 if (unlazy_walk(nd, NULL))
Al Viro52094c82011-02-21 21:34:47 -05001232 return -ECHILD;
1233 }
Al Viro4ad5abb2011-06-20 19:57:03 -04001234 return inode_permission(nd->inode, MAY_EXEC);
Al Viro52094c82011-02-21 21:34:47 -05001235}
1236
Al Viro9856fa12011-03-04 14:22:06 -05001237static inline int handle_dots(struct nameidata *nd, int type)
1238{
1239 if (type == LAST_DOTDOT) {
1240 if (nd->flags & LOOKUP_RCU) {
1241 if (follow_dotdot_rcu(nd))
1242 return -ECHILD;
1243 } else
1244 follow_dotdot(nd);
1245 }
1246 return 0;
1247}
1248
Al Viro951361f2011-03-04 14:44:37 -05001249static void terminate_walk(struct nameidata *nd)
1250{
1251 if (!(nd->flags & LOOKUP_RCU)) {
1252 path_put(&nd->path);
1253 } else {
1254 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001255 if (!(nd->flags & LOOKUP_ROOT))
1256 nd->root.mnt = NULL;
Al Viro951361f2011-03-04 14:44:37 -05001257 rcu_read_unlock();
1258 br_read_unlock(vfsmount_lock);
1259 }
1260}
1261
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001262/*
1263 * Do we need to follow links? We _really_ want to be able
1264 * to do this check without having to look at inode->i_op,
1265 * so we keep a cache of "no, this doesn't need follow_link"
1266 * for the common case.
1267 */
Linus Torvalds7813b942011-08-07 09:53:20 -07001268static inline int should_follow_link(struct inode *inode, int follow)
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001269{
1270 if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
1271 if (likely(inode->i_op->follow_link))
1272 return follow;
1273
1274 /* This gets set once for the inode lifetime */
1275 spin_lock(&inode->i_lock);
1276 inode->i_opflags |= IOP_NOFOLLOW;
1277 spin_unlock(&inode->i_lock);
1278 }
1279 return 0;
1280}
1281
Al Viroce57dfc2011-03-13 19:58:58 -04001282static inline int walk_component(struct nameidata *nd, struct path *path,
1283 struct qstr *name, int type, int follow)
1284{
1285 struct inode *inode;
1286 int err;
1287 /*
1288 * "." and ".." are special - ".." especially so because it has
1289 * to be able to know about the current root directory and
1290 * parent relationships.
1291 */
1292 if (unlikely(type != LAST_NORM))
1293 return handle_dots(nd, type);
1294 err = do_lookup(nd, name, path, &inode);
1295 if (unlikely(err)) {
1296 terminate_walk(nd);
1297 return err;
1298 }
1299 if (!inode) {
1300 path_to_nameidata(path, nd);
1301 terminate_walk(nd);
1302 return -ENOENT;
1303 }
Linus Torvalds7813b942011-08-07 09:53:20 -07001304 if (should_follow_link(inode, follow)) {
Al Viro19660af2011-03-25 10:32:48 -04001305 if (nd->flags & LOOKUP_RCU) {
1306 if (unlikely(unlazy_walk(nd, path->dentry))) {
1307 terminate_walk(nd);
1308 return -ECHILD;
1309 }
1310 }
Al Viroce57dfc2011-03-13 19:58:58 -04001311 BUG_ON(inode != path->dentry->d_inode);
1312 return 1;
1313 }
1314 path_to_nameidata(path, nd);
1315 nd->inode = inode;
1316 return 0;
1317}
1318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319/*
Al Virob3563792011-03-14 21:54:55 -04001320 * This limits recursive symlink follows to 8, while
1321 * limiting consecutive symlinks to 40.
1322 *
1323 * Without that kind of total limit, nasty chains of consecutive
1324 * symlinks can cause almost arbitrarily long lookups.
1325 */
1326static inline int nested_symlink(struct path *path, struct nameidata *nd)
1327{
1328 int res;
1329
Al Virob3563792011-03-14 21:54:55 -04001330 if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1331 path_put_conditional(path, nd);
1332 path_put(&nd->path);
1333 return -ELOOP;
1334 }
Erez Zadok1a4022f2011-05-21 01:19:59 -04001335 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
Al Virob3563792011-03-14 21:54:55 -04001336
1337 nd->depth++;
1338 current->link_count++;
1339
1340 do {
1341 struct path link = *path;
1342 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04001343
1344 res = follow_link(&link, nd, &cookie);
Al Virob3563792011-03-14 21:54:55 -04001345 if (!res)
1346 res = walk_component(nd, path, &nd->last,
1347 nd->last_type, LOOKUP_FOLLOW);
Al Viro574197e2011-03-14 22:20:34 -04001348 put_link(nd, &link, cookie);
Al Virob3563792011-03-14 21:54:55 -04001349 } while (res > 0);
1350
1351 current->link_count--;
1352 nd->depth--;
1353 return res;
1354}
1355
1356/*
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001357 * We really don't want to look at inode->i_op->lookup
1358 * when we don't have to. So we keep a cache bit in
1359 * the inode ->i_opflags field that says "yes, we can
1360 * do lookup on this inode".
1361 */
1362static inline int can_lookup(struct inode *inode)
1363{
1364 if (likely(inode->i_opflags & IOP_LOOKUP))
1365 return 1;
1366 if (likely(!inode->i_op->lookup))
1367 return 0;
1368
1369 /* We do this once for the lifetime of the inode */
1370 spin_lock(&inode->i_lock);
1371 inode->i_opflags |= IOP_LOOKUP;
1372 spin_unlock(&inode->i_lock);
1373 return 1;
1374}
1375
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001376/*
1377 * We can do the critical dentry name comparison and hashing
1378 * operations one word at a time, but we are limited to:
1379 *
1380 * - Architectures with fast unaligned word accesses. We could
1381 * do a "get_unaligned()" if this helps and is sufficiently
1382 * fast.
1383 *
1384 * - Little-endian machines (so that we can generate the mask
1385 * of low bytes efficiently). Again, we *could* do a byte
1386 * swapping load on big-endian architectures if that is not
1387 * expensive enough to make the optimization worthless.
1388 *
1389 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1390 * do not trap on the (extremely unlikely) case of a page
1391 * crossing operation.
1392 *
1393 * - Furthermore, we need an efficient 64-bit compile for the
1394 * 64-bit case in order to generate the "number of bytes in
1395 * the final mask". Again, that could be replaced with a
1396 * efficient population count instruction or similar.
1397 */
1398#ifdef CONFIG_DCACHE_WORD_ACCESS
1399
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001400#include <asm/word-at-a-time.h>
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001401
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001402#ifdef CONFIG_64BIT
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001403
1404static inline unsigned int fold_hash(unsigned long hash)
1405{
1406 hash += hash >> (8*sizeof(int));
1407 return hash;
1408}
1409
1410#else /* 32-bit case */
1411
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001412#define fold_hash(x) (x)
1413
1414#endif
1415
1416unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1417{
1418 unsigned long a, mask;
1419 unsigned long hash = 0;
1420
1421 for (;;) {
1422 a = *(unsigned long *)name;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001423 if (len < sizeof(unsigned long))
1424 break;
1425 hash += a;
Al Virof132c5b2012-03-22 21:59:52 +00001426 hash *= 9;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001427 name += sizeof(unsigned long);
1428 len -= sizeof(unsigned long);
1429 if (!len)
1430 goto done;
1431 }
1432 mask = ~(~0ul << len*8);
1433 hash += mask & a;
1434done:
1435 return fold_hash(hash);
1436}
1437EXPORT_SYMBOL(full_name_hash);
1438
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001439/*
1440 * Calculate the length and hash of the path component, and
1441 * return the length of the component;
1442 */
1443static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1444{
1445 unsigned long a, mask, hash, len;
1446
1447 hash = a = 0;
1448 len = -sizeof(unsigned long);
1449 do {
1450 hash = (hash + a) * 9;
1451 len += sizeof(unsigned long);
1452 a = *(unsigned long *)(name+len);
1453 /* Do we have any NUL or '/' bytes in this word? */
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001454 mask = has_zero(a) | has_zero(a ^ REPEAT_BYTE('/'));
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001455 } while (!mask);
1456
1457 /* The mask *below* the first high bit set */
1458 mask = (mask - 1) & ~mask;
1459 mask >>= 7;
1460 hash += a & mask;
1461 *hashp = fold_hash(hash);
1462
1463 return len + count_masked_bytes(mask);
1464}
1465
1466#else
1467
Linus Torvalds0145acc2012-03-02 14:32:59 -08001468unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1469{
1470 unsigned long hash = init_name_hash();
1471 while (len--)
1472 hash = partial_name_hash(*name++, hash);
1473 return end_name_hash(hash);
1474}
Linus Torvaldsae942ae2012-03-02 19:40:57 -08001475EXPORT_SYMBOL(full_name_hash);
Linus Torvalds0145acc2012-03-02 14:32:59 -08001476
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001477/*
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001478 * We know there's a real path component here of at least
1479 * one character.
1480 */
1481static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1482{
1483 unsigned long hash = init_name_hash();
1484 unsigned long len = 0, c;
1485
1486 c = (unsigned char)*name;
1487 do {
1488 len++;
1489 hash = partial_name_hash(c, hash);
1490 c = (unsigned char)name[len];
1491 } while (c && c != '/');
1492 *hashp = end_name_hash(hash);
1493 return len;
1494}
1495
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001496#endif
1497
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001498/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01001500 * This is the basic name resolution function, turning a pathname into
1501 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01001503 * Returns 0 and nd will have valid dentry and mnt on success.
1504 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 */
Al Viro6de88d72009-08-09 01:41:57 +04001506static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507{
1508 struct path next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
1511 while (*name=='/')
1512 name++;
1513 if (!*name)
Al Viro086e1832011-02-22 20:56:27 -05001514 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 /* At this point we know we have a real path component. */
1517 for(;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 struct qstr this;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001519 long len;
Al Virofe479a52011-02-22 15:10:03 -05001520 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Al Viro52094c82011-02-21 21:34:47 -05001522 err = may_lookup(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 if (err)
1524 break;
1525
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001526 len = hash_name(name, &this.hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 this.name = name;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001528 this.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Al Virofe479a52011-02-22 15:10:03 -05001530 type = LAST_NORM;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001531 if (name[0] == '.') switch (len) {
Al Virofe479a52011-02-22 15:10:03 -05001532 case 2:
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001533 if (name[1] == '.') {
Al Virofe479a52011-02-22 15:10:03 -05001534 type = LAST_DOTDOT;
Al Viro16c2cd72011-02-22 15:50:10 -05001535 nd->flags |= LOOKUP_JUMPED;
1536 }
Al Virofe479a52011-02-22 15:10:03 -05001537 break;
1538 case 1:
1539 type = LAST_DOT;
1540 }
Al Viro5a202bc2011-03-08 14:17:44 -05001541 if (likely(type == LAST_NORM)) {
1542 struct dentry *parent = nd->path.dentry;
Al Viro16c2cd72011-02-22 15:50:10 -05001543 nd->flags &= ~LOOKUP_JUMPED;
Al Viro5a202bc2011-03-08 14:17:44 -05001544 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1545 err = parent->d_op->d_hash(parent, nd->inode,
1546 &this);
1547 if (err < 0)
1548 break;
1549 }
1550 }
Al Virofe479a52011-02-22 15:10:03 -05001551
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001552 if (!name[len])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 goto last_component;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001554 /*
1555 * If it wasn't NUL, we know it was '/'. Skip that
1556 * slash, and continue until no more slashes.
1557 */
1558 do {
1559 len++;
1560 } while (unlikely(name[len] == '/'));
1561 if (!name[len])
Al Virob3563792011-03-14 21:54:55 -04001562 goto last_component;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001563 name += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
Al Viroce57dfc2011-03-13 19:58:58 -04001565 err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1566 if (err < 0)
1567 return err;
Al Virofe479a52011-02-22 15:10:03 -05001568
Al Viroce57dfc2011-03-13 19:58:58 -04001569 if (err) {
Al Virob3563792011-03-14 21:54:55 -04001570 err = nested_symlink(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 if (err)
Al Viroa7472ba2011-03-04 14:39:30 -05001572 return err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001573 }
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001574 if (can_lookup(nd->inode))
1575 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 err = -ENOTDIR;
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001577 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 /* here ends the main loop */
1579
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580last_component:
Al Virob3563792011-03-14 21:54:55 -04001581 nd->last = this;
1582 nd->last_type = type;
Al Viro086e1832011-02-22 20:56:27 -05001583 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 }
Al Viro951361f2011-03-04 14:44:37 -05001585 terminate_walk(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 return err;
1587}
1588
Al Viro70e9b352011-03-05 21:12:22 -05001589static int path_init(int dfd, const char *name, unsigned int flags,
1590 struct nameidata *nd, struct file **fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
Prasanna Medaea3834d2005-04-29 16:00:17 +01001592 int retval = 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001593 int fput_needed;
1594 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
1596 nd->last_type = LAST_ROOT; /* if there are only slashes... */
Al Viro16c2cd72011-02-22 15:50:10 -05001597 nd->flags = flags | LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 nd->depth = 0;
Al Viro5b6ca022011-03-09 23:04:47 -05001599 if (flags & LOOKUP_ROOT) {
1600 struct inode *inode = nd->root.dentry->d_inode;
Al Viro73d049a2011-03-11 12:08:24 -05001601 if (*name) {
1602 if (!inode->i_op->lookup)
1603 return -ENOTDIR;
1604 retval = inode_permission(inode, MAY_EXEC);
1605 if (retval)
1606 return retval;
1607 }
Al Viro5b6ca022011-03-09 23:04:47 -05001608 nd->path = nd->root;
1609 nd->inode = inode;
1610 if (flags & LOOKUP_RCU) {
1611 br_read_lock(vfsmount_lock);
1612 rcu_read_lock();
1613 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1614 } else {
1615 path_get(&nd->path);
1616 }
1617 return 0;
1618 }
1619
Al Viro2a737872009-04-07 11:49:53 -04001620 nd->root.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 if (*name=='/') {
Al Viroe41f7d42011-02-22 14:02:58 -05001623 if (flags & LOOKUP_RCU) {
1624 br_read_lock(vfsmount_lock);
1625 rcu_read_lock();
1626 set_root_rcu(nd);
1627 } else {
1628 set_root(nd);
1629 path_get(&nd->root);
1630 }
Al Viro2a737872009-04-07 11:49:53 -04001631 nd->path = nd->root;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001632 } else if (dfd == AT_FDCWD) {
Al Viroe41f7d42011-02-22 14:02:58 -05001633 if (flags & LOOKUP_RCU) {
1634 struct fs_struct *fs = current->fs;
1635 unsigned seq;
1636
1637 br_read_lock(vfsmount_lock);
1638 rcu_read_lock();
1639
1640 do {
1641 seq = read_seqcount_begin(&fs->seq);
1642 nd->path = fs->pwd;
1643 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1644 } while (read_seqcount_retry(&fs->seq, seq));
1645 } else {
1646 get_fs_pwd(current->fs, &nd->path);
1647 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001648 } else {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001649 struct dentry *dentry;
1650
Al Viro1abf0c72011-03-13 03:51:11 -04001651 file = fget_raw_light(dfd, &fput_needed);
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001652 retval = -EBADF;
1653 if (!file)
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001654 goto out_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001655
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001656 dentry = file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001657
Al Virof52e0c12011-03-14 18:56:51 -04001658 if (*name) {
1659 retval = -ENOTDIR;
1660 if (!S_ISDIR(dentry->d_inode->i_mode))
1661 goto fput_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001662
Al Viro4ad5abb2011-06-20 19:57:03 -04001663 retval = inode_permission(dentry->d_inode, MAY_EXEC);
Al Virof52e0c12011-03-14 18:56:51 -04001664 if (retval)
1665 goto fput_fail;
1666 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001667
Jan Blunck5dd784d02008-02-14 19:34:38 -08001668 nd->path = file->f_path;
Al Viroe41f7d42011-02-22 14:02:58 -05001669 if (flags & LOOKUP_RCU) {
1670 if (fput_needed)
Al Viro70e9b352011-03-05 21:12:22 -05001671 *fp = file;
Al Viroe41f7d42011-02-22 14:02:58 -05001672 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1673 br_read_lock(vfsmount_lock);
1674 rcu_read_lock();
1675 } else {
1676 path_get(&file->f_path);
1677 fput_light(file, fput_needed);
1678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 }
Al Viroe41f7d42011-02-22 14:02:58 -05001680
Nick Piggin31e6b012011-01-07 17:49:52 +11001681 nd->inode = nd->path.dentry->d_inode;
Al Viro9b4a9b12009-04-07 11:44:16 -04001682 return 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001683
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001684fput_fail:
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001685 fput_light(file, fput_needed);
Al Viro9b4a9b12009-04-07 11:44:16 -04001686out_fail:
1687 return retval;
1688}
1689
Al Virobd92d7f2011-03-14 19:54:59 -04001690static inline int lookup_last(struct nameidata *nd, struct path *path)
1691{
1692 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1693 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1694
1695 nd->flags &= ~LOOKUP_PARENT;
1696 return walk_component(nd, path, &nd->last, nd->last_type,
1697 nd->flags & LOOKUP_FOLLOW);
1698}
1699
Al Viro9b4a9b12009-04-07 11:44:16 -04001700/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroee0827c2011-02-21 23:38:09 -05001701static int path_lookupat(int dfd, const char *name,
Al Viro9b4a9b12009-04-07 11:44:16 -04001702 unsigned int flags, struct nameidata *nd)
1703{
Al Viro70e9b352011-03-05 21:12:22 -05001704 struct file *base = NULL;
Al Virobd92d7f2011-03-14 19:54:59 -04001705 struct path path;
1706 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001707
1708 /*
1709 * Path walking is largely split up into 2 different synchronisation
1710 * schemes, rcu-walk and ref-walk (explained in
1711 * Documentation/filesystems/path-lookup.txt). These share much of the
1712 * path walk code, but some things particularly setup, cleanup, and
1713 * following mounts are sufficiently divergent that functions are
1714 * duplicated. Typically there is a function foo(), and its RCU
1715 * analogue, foo_rcu().
1716 *
1717 * -ECHILD is the error number of choice (just to avoid clashes) that
1718 * is returned if some aspect of an rcu-walk fails. Such an error must
1719 * be handled by restarting a traditional ref-walk (which will always
1720 * be able to complete).
1721 */
Al Virobd92d7f2011-03-14 19:54:59 -04001722 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
Al Viroee0827c2011-02-21 23:38:09 -05001723
Al Virobd92d7f2011-03-14 19:54:59 -04001724 if (unlikely(err))
1725 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001726
1727 current->total_link_count = 0;
Al Virobd92d7f2011-03-14 19:54:59 -04001728 err = link_path_walk(name, nd);
1729
1730 if (!err && !(flags & LOOKUP_PARENT)) {
Al Virobd92d7f2011-03-14 19:54:59 -04001731 err = lookup_last(nd, &path);
1732 while (err > 0) {
1733 void *cookie;
1734 struct path link = path;
Al Virobd92d7f2011-03-14 19:54:59 -04001735 nd->flags |= LOOKUP_PARENT;
Al Viro574197e2011-03-14 22:20:34 -04001736 err = follow_link(&link, nd, &cookie);
Al Virobd92d7f2011-03-14 19:54:59 -04001737 if (!err)
1738 err = lookup_last(nd, &path);
Al Viro574197e2011-03-14 22:20:34 -04001739 put_link(nd, &link, cookie);
Al Virobd92d7f2011-03-14 19:54:59 -04001740 }
1741 }
Al Viroee0827c2011-02-21 23:38:09 -05001742
Al Viro9f1fafe2011-03-25 11:00:12 -04001743 if (!err)
1744 err = complete_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04001745
1746 if (!err && nd->flags & LOOKUP_DIRECTORY) {
1747 if (!nd->inode->i_op->lookup) {
1748 path_put(&nd->path);
Al Virobd23a532011-03-23 09:56:30 -04001749 err = -ENOTDIR;
Al Virobd92d7f2011-03-14 19:54:59 -04001750 }
1751 }
Al Viro16c2cd72011-02-22 15:50:10 -05001752
Al Viro70e9b352011-03-05 21:12:22 -05001753 if (base)
1754 fput(base);
Al Viroee0827c2011-02-21 23:38:09 -05001755
Al Viro5b6ca022011-03-09 23:04:47 -05001756 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
Al Viro2a737872009-04-07 11:49:53 -04001757 path_put(&nd->root);
1758 nd->root.mnt = NULL;
1759 }
Al Virobd92d7f2011-03-14 19:54:59 -04001760 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001761}
Nick Piggin31e6b012011-01-07 17:49:52 +11001762
Al Viroee0827c2011-02-21 23:38:09 -05001763static int do_path_lookup(int dfd, const char *name,
1764 unsigned int flags, struct nameidata *nd)
1765{
1766 int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1767 if (unlikely(retval == -ECHILD))
1768 retval = path_lookupat(dfd, name, flags, nd);
1769 if (unlikely(retval == -ESTALE))
1770 retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11001771
1772 if (likely(!retval)) {
1773 if (unlikely(!audit_dummy_context())) {
1774 if (nd->path.dentry && nd->inode)
1775 audit_inode(name, nd->path.dentry);
1776 }
1777 }
Al Viro9b4a9b12009-04-07 11:44:16 -04001778 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779}
1780
Al Viroc9c6cac2011-02-16 15:15:47 -05001781int kern_path_parent(const char *name, struct nameidata *nd)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001782{
Al Viroc9c6cac2011-02-16 15:15:47 -05001783 return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001784}
1785
Al Virod1811462008-08-02 00:49:18 -04001786int kern_path(const char *name, unsigned int flags, struct path *path)
1787{
1788 struct nameidata nd;
1789 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1790 if (!res)
1791 *path = nd.path;
1792 return res;
1793}
1794
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001795/**
1796 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1797 * @dentry: pointer to dentry of the base directory
1798 * @mnt: pointer to vfs mount of the base directory
1799 * @name: pointer to file name
1800 * @flags: lookup flags
Al Viroe0a01242011-06-27 17:00:37 -04001801 * @path: pointer to struct path to fill
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001802 */
1803int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
1804 const char *name, unsigned int flags,
Al Viroe0a01242011-06-27 17:00:37 -04001805 struct path *path)
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001806{
Al Viroe0a01242011-06-27 17:00:37 -04001807 struct nameidata nd;
1808 int err;
1809 nd.root.dentry = dentry;
1810 nd.root.mnt = mnt;
1811 BUG_ON(flags & LOOKUP_PARENT);
Al Viro5b6ca022011-03-09 23:04:47 -05001812 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
Al Viroe0a01242011-06-27 17:00:37 -04001813 err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
1814 if (!err)
1815 *path = nd.path;
1816 return err;
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001817}
1818
James Morris057f6c02007-04-26 00:12:05 -07001819/*
1820 * Restricted form of lookup. Doesn't follow links, single-component only,
1821 * needs parent already locked. Doesn't follow mounts.
1822 * SMP-safe.
1823 */
Adrian Bunka244e162006-03-31 02:32:11 -08001824static struct dentry *lookup_hash(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825{
Jan Blunck4ac91372008-02-14 19:34:32 -08001826 return __lookup_hash(&nd->last, nd->path.dentry, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827}
1828
Christoph Hellwigeead1912007-10-16 23:25:38 -07001829/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07001830 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07001831 * @name: pathname component to lookup
1832 * @base: base directory to lookup from
1833 * @len: maximum length @len should be interpreted to
1834 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07001835 * Note that this routine is purely a helper for filesystem usage and should
1836 * not be called by generic code. Also note that by using this function the
Christoph Hellwigeead1912007-10-16 23:25:38 -07001837 * nameidata argument is passed to the filesystem methods and a filesystem
1838 * using this helper needs to be prepared for that.
1839 */
James Morris057f6c02007-04-26 00:12:05 -07001840struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1841{
James Morris057f6c02007-04-26 00:12:05 -07001842 struct qstr this;
Al Viro6a96ba52011-03-07 23:49:20 -05001843 unsigned int c;
Miklos Szeredicda309d2012-03-26 12:54:21 +02001844 int err;
James Morris057f6c02007-04-26 00:12:05 -07001845
David Woodhouse2f9092e2009-04-20 23:18:37 +01001846 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
1847
Al Viro6a96ba52011-03-07 23:49:20 -05001848 this.name = name;
1849 this.len = len;
Linus Torvalds0145acc2012-03-02 14:32:59 -08001850 this.hash = full_name_hash(name, len);
Al Viro6a96ba52011-03-07 23:49:20 -05001851 if (!len)
1852 return ERR_PTR(-EACCES);
1853
Al Viro6a96ba52011-03-07 23:49:20 -05001854 while (len--) {
1855 c = *(const unsigned char *)name++;
1856 if (c == '/' || c == '\0')
1857 return ERR_PTR(-EACCES);
Al Viro6a96ba52011-03-07 23:49:20 -05001858 }
Al Viro5a202bc2011-03-08 14:17:44 -05001859 /*
1860 * See if the low-level filesystem might want
1861 * to use its own hash..
1862 */
1863 if (base->d_flags & DCACHE_OP_HASH) {
1864 int err = base->d_op->d_hash(base, base->d_inode, &this);
1865 if (err < 0)
1866 return ERR_PTR(err);
1867 }
Christoph Hellwigeead1912007-10-16 23:25:38 -07001868
Miklos Szeredicda309d2012-03-26 12:54:21 +02001869 err = inode_permission(base->d_inode, MAY_EXEC);
1870 if (err)
1871 return ERR_PTR(err);
1872
Christoph Hellwig49705b72005-11-08 21:35:06 -08001873 return __lookup_hash(&this, base, NULL);
James Morris057f6c02007-04-26 00:12:05 -07001874}
1875
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01001876int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
1877 struct path *path, int *empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878{
Al Viro2d8f3032008-07-22 09:59:21 -04001879 struct nameidata nd;
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01001880 char *tmp = getname_flags(name, flags, empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 int err = PTR_ERR(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 if (!IS_ERR(tmp)) {
Al Viro2d8f3032008-07-22 09:59:21 -04001883
1884 BUG_ON(flags & LOOKUP_PARENT);
1885
1886 err = do_path_lookup(dfd, tmp, flags, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 putname(tmp);
Al Viro2d8f3032008-07-22 09:59:21 -04001888 if (!err)
1889 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 }
1891 return err;
1892}
1893
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01001894int user_path_at(int dfd, const char __user *name, unsigned flags,
1895 struct path *path)
1896{
Linus Torvaldsf7493e52012-03-22 16:10:40 -07001897 return user_path_at_empty(dfd, name, flags, path, NULL);
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01001898}
1899
Al Viro2ad94ae2008-07-21 09:32:51 -04001900static int user_path_parent(int dfd, const char __user *path,
1901 struct nameidata *nd, char **name)
1902{
1903 char *s = getname(path);
1904 int error;
1905
1906 if (IS_ERR(s))
1907 return PTR_ERR(s);
1908
1909 error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
1910 if (error)
1911 putname(s);
1912 else
1913 *name = s;
1914
1915 return error;
1916}
1917
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918/*
1919 * It's inline, so penalty for filesystems that don't use sticky bit is
1920 * minimal.
1921 */
1922static inline int check_sticky(struct inode *dir, struct inode *inode)
1923{
David Howellsda9592e2008-11-14 10:39:05 +11001924 uid_t fsuid = current_fsuid();
1925
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 if (!(dir->i_mode & S_ISVTX))
1927 return 0;
Serge E. Hallyne795b712011-03-23 16:43:25 -07001928 if (current_user_ns() != inode_userns(inode))
1929 goto other_userns;
David Howellsda9592e2008-11-14 10:39:05 +11001930 if (inode->i_uid == fsuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 return 0;
David Howellsda9592e2008-11-14 10:39:05 +11001932 if (dir->i_uid == fsuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 return 0;
Serge E. Hallyne795b712011-03-23 16:43:25 -07001934
1935other_userns:
1936 return !ns_capable(inode_userns(inode), CAP_FOWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937}
1938
1939/*
1940 * Check whether we can remove a link victim from directory dir, check
1941 * whether the type of victim is right.
1942 * 1. We can't do it if dir is read-only (done in permission())
1943 * 2. We should have write and exec permissions on dir
1944 * 3. We can't remove anything from append-only dir
1945 * 4. We can't do anything with immutable dir (done in permission())
1946 * 5. If the sticky bit on dir is set we should either
1947 * a. be owner of dir, or
1948 * b. be owner of victim, or
1949 * c. have CAP_FOWNER capability
1950 * 6. If the victim is append-only or immutable we can't do antyhing with
1951 * links pointing to it.
1952 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1953 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1954 * 9. We can't remove a root or mountpoint.
1955 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1956 * nfs_async_unlink().
1957 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001958static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959{
1960 int error;
1961
1962 if (!victim->d_inode)
1963 return -ENOENT;
1964
1965 BUG_ON(victim->d_parent->d_inode != dir);
Al Virocccc6bb2009-12-25 05:07:33 -05001966 audit_inode_child(victim, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
Al Virof419a2e2008-07-22 00:07:17 -04001968 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 if (error)
1970 return error;
1971 if (IS_APPEND(dir))
1972 return -EPERM;
1973 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
Hugh Dickinsf9454542008-11-19 15:36:38 -08001974 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 return -EPERM;
1976 if (isdir) {
1977 if (!S_ISDIR(victim->d_inode->i_mode))
1978 return -ENOTDIR;
1979 if (IS_ROOT(victim))
1980 return -EBUSY;
1981 } else if (S_ISDIR(victim->d_inode->i_mode))
1982 return -EISDIR;
1983 if (IS_DEADDIR(dir))
1984 return -ENOENT;
1985 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1986 return -EBUSY;
1987 return 0;
1988}
1989
1990/* Check whether we can create an object with dentry child in directory
1991 * dir.
1992 * 1. We can't do it if child already exists (open has special treatment for
1993 * this case, but since we are inlined it's OK)
1994 * 2. We can't do it if dir is read-only (done in permission())
1995 * 3. We should have write and exec permissions on dir
1996 * 4. We can't do it if dir is immutable (done in permission())
1997 */
Miklos Szeredia95164d2008-07-30 15:08:48 +02001998static inline int may_create(struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999{
2000 if (child->d_inode)
2001 return -EEXIST;
2002 if (IS_DEADDIR(dir))
2003 return -ENOENT;
Al Virof419a2e2008-07-22 00:07:17 -04002004 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005}
2006
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007/*
2008 * p1 and p2 should be directories on the same fs.
2009 */
2010struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2011{
2012 struct dentry *p;
2013
2014 if (p1 == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07002015 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 return NULL;
2017 }
2018
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002019 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002021 p = d_ancestor(p2, p1);
2022 if (p) {
2023 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2024 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
2025 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 }
2027
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002028 p = d_ancestor(p1, p2);
2029 if (p) {
2030 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2031 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2032 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 }
2034
Ingo Molnarf2eace22006-07-03 00:25:05 -07002035 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2036 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 return NULL;
2038}
2039
2040void unlock_rename(struct dentry *p1, struct dentry *p2)
2041{
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002042 mutex_unlock(&p1->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 if (p1 != p2) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002044 mutex_unlock(&p2->d_inode->i_mutex);
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002045 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 }
2047}
2048
Al Viro4acdaf22011-07-26 01:42:34 -04002049int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 struct nameidata *nd)
2051{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002052 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
2054 if (error)
2055 return error;
2056
Al Viroacfa4382008-12-04 10:06:33 -05002057 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 return -EACCES; /* shouldn't it be ENOSYS? */
2059 mode &= S_IALLUGO;
2060 mode |= S_IFREG;
2061 error = security_inode_create(dir, dentry, mode);
2062 if (error)
2063 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 error = dir->i_op->create(dir, dentry, mode, nd);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002065 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002066 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 return error;
2068}
2069
Al Viro73d049a2011-03-11 12:08:24 -05002070static int may_open(struct path *path, int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002072 struct dentry *dentry = path->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 struct inode *inode = dentry->d_inode;
2074 int error;
2075
Al Virobcda7652011-03-13 16:42:14 -04002076 /* O_PATH? */
2077 if (!acc_mode)
2078 return 0;
2079
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 if (!inode)
2081 return -ENOENT;
2082
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002083 switch (inode->i_mode & S_IFMT) {
2084 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002086 case S_IFDIR:
2087 if (acc_mode & MAY_WRITE)
2088 return -EISDIR;
2089 break;
2090 case S_IFBLK:
2091 case S_IFCHR:
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002092 if (path->mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002094 /*FALLTHRU*/
2095 case S_IFIFO:
2096 case S_IFSOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002098 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08002099 }
Dave Hansenb41572e2007-10-16 23:31:14 -07002100
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002101 error = inode_permission(inode, acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07002102 if (error)
2103 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05002104
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 /*
2106 * An append-only file must be opened in append mode for writing.
2107 */
2108 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05002109 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05002110 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05002112 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 }
2114
2115 /* O_NOATIME can only be set by the owner or superuser */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07002116 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
Al Viro7715b522009-12-16 03:54:00 -05002117 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
J. Bruce Fieldsf3c7691e2011-09-21 10:58:13 -04002119 return 0;
Al Viro7715b522009-12-16 03:54:00 -05002120}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
Jeff Laytone1181ee2010-12-07 16:19:50 -05002122static int handle_truncate(struct file *filp)
Al Viro7715b522009-12-16 03:54:00 -05002123{
Jeff Laytone1181ee2010-12-07 16:19:50 -05002124 struct path *path = &filp->f_path;
Al Viro7715b522009-12-16 03:54:00 -05002125 struct inode *inode = path->dentry->d_inode;
2126 int error = get_write_access(inode);
2127 if (error)
2128 return error;
2129 /*
2130 * Refuse to truncate files with mandatory locks held on them.
2131 */
2132 error = locks_verify_locked(inode);
2133 if (!error)
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +09002134 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05002135 if (!error) {
2136 error = do_truncate(path->dentry, 0,
2137 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
Jeff Laytone1181ee2010-12-07 16:19:50 -05002138 filp);
Al Viro7715b522009-12-16 03:54:00 -05002139 }
2140 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04002141 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142}
2143
Dave Hansend57999e2008-02-15 14:37:27 -08002144static inline int open_to_namei_flags(int flag)
2145{
Al Viro8a5e9292011-06-25 19:15:54 -04002146 if ((flag & O_ACCMODE) == 3)
2147 flag--;
Dave Hansend57999e2008-02-15 14:37:27 -08002148 return flag;
2149}
2150
Nick Piggin31e6b012011-01-07 17:49:52 +11002151/*
Al Virofe2d35f2011-03-05 22:58:25 -05002152 * Handle the last step of open()
Nick Piggin31e6b012011-01-07 17:49:52 +11002153 */
Al Virofb1cc552009-12-24 01:58:28 -05002154static struct file *do_last(struct nameidata *nd, struct path *path,
Al Viroc3e380b2011-02-23 13:39:45 -05002155 const struct open_flags *op, const char *pathname)
Al Virofb1cc552009-12-24 01:58:28 -05002156{
Al Viroa1e28032009-12-24 02:12:06 -05002157 struct dentry *dir = nd->path.dentry;
Al Viro6c0d46c2011-03-09 00:59:59 -05002158 struct dentry *dentry;
Al Viroca344a892011-03-09 00:36:45 -05002159 int open_flag = op->open_flag;
Al Viro6c0d46c2011-03-09 00:59:59 -05002160 int will_truncate = open_flag & O_TRUNC;
Al Viroca344a892011-03-09 00:36:45 -05002161 int want_write = 0;
Al Virobcda7652011-03-13 16:42:14 -04002162 int acc_mode = op->acc_mode;
Al Virofb1cc552009-12-24 01:58:28 -05002163 struct file *filp;
Al Viro16c2cd72011-02-22 15:50:10 -05002164 int error;
Al Virofb1cc552009-12-24 01:58:28 -05002165
Al Viroc3e380b2011-02-23 13:39:45 -05002166 nd->flags &= ~LOOKUP_PARENT;
2167 nd->flags |= op->intent;
2168
Al Viro1f36f772009-12-26 10:56:19 -05002169 switch (nd->last_type) {
2170 case LAST_DOTDOT:
Neil Brown176306f2010-05-24 16:57:56 +10002171 case LAST_DOT:
Al Virofe2d35f2011-03-05 22:58:25 -05002172 error = handle_dots(nd, nd->last_type);
2173 if (error)
2174 return ERR_PTR(error);
Al Viro1f36f772009-12-26 10:56:19 -05002175 /* fallthrough */
Al Viro1f36f772009-12-26 10:56:19 -05002176 case LAST_ROOT:
Al Viro9f1fafe2011-03-25 11:00:12 -04002177 error = complete_walk(nd);
Al Viro16c2cd72011-02-22 15:50:10 -05002178 if (error)
Al Viro9f1fafe2011-03-25 11:00:12 -04002179 return ERR_PTR(error);
Al Virofe2d35f2011-03-05 22:58:25 -05002180 audit_inode(pathname, nd->path.dentry);
Al Viroca344a892011-03-09 00:36:45 -05002181 if (open_flag & O_CREAT) {
Al Virofe2d35f2011-03-05 22:58:25 -05002182 error = -EISDIR;
2183 goto exit;
2184 }
2185 goto ok;
Al Viro1f36f772009-12-26 10:56:19 -05002186 case LAST_BIND:
Al Viro9f1fafe2011-03-25 11:00:12 -04002187 error = complete_walk(nd);
Al Viro16c2cd72011-02-22 15:50:10 -05002188 if (error)
Al Viro9f1fafe2011-03-25 11:00:12 -04002189 return ERR_PTR(error);
Al Viro1f36f772009-12-26 10:56:19 -05002190 audit_inode(pathname, dir);
Al Viro67ee3ad2009-12-26 07:01:01 -05002191 goto ok;
Al Viro1f36f772009-12-26 10:56:19 -05002192 }
Al Viro67ee3ad2009-12-26 07:01:01 -05002193
Al Viroca344a892011-03-09 00:36:45 -05002194 if (!(open_flag & O_CREAT)) {
Al Virobcda7652011-03-13 16:42:14 -04002195 int symlink_ok = 0;
Al Virofe2d35f2011-03-05 22:58:25 -05002196 if (nd->last.name[nd->last.len])
2197 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
Al Virobcda7652011-03-13 16:42:14 -04002198 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2199 symlink_ok = 1;
Al Virofe2d35f2011-03-05 22:58:25 -05002200 /* we _can_ be in RCU mode here */
Al Viroce57dfc2011-03-13 19:58:58 -04002201 error = walk_component(nd, path, &nd->last, LAST_NORM,
2202 !symlink_ok);
2203 if (error < 0)
Al Virofe2d35f2011-03-05 22:58:25 -05002204 return ERR_PTR(error);
Al Viroce57dfc2011-03-13 19:58:58 -04002205 if (error) /* symlink */
Al Virofe2d35f2011-03-05 22:58:25 -05002206 return NULL;
Al Virofe2d35f2011-03-05 22:58:25 -05002207 /* sayonara */
Al Viro9f1fafe2011-03-25 11:00:12 -04002208 error = complete_walk(nd);
2209 if (error)
Miklos Szeredi7f6c7e62012-03-06 13:56:34 +01002210 return ERR_PTR(error);
Al Virofe2d35f2011-03-05 22:58:25 -05002211
2212 error = -ENOTDIR;
2213 if (nd->flags & LOOKUP_DIRECTORY) {
Al Viroce57dfc2011-03-13 19:58:58 -04002214 if (!nd->inode->i_op->lookup)
Al Virofe2d35f2011-03-05 22:58:25 -05002215 goto exit;
2216 }
2217 audit_inode(pathname, nd->path.dentry);
2218 goto ok;
2219 }
2220
2221 /* create side of things */
Al Viroa3fbbde2011-11-07 21:21:26 +00002222 /*
2223 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED has been
2224 * cleared when we got to the last component we are about to look up
2225 */
Al Viro9f1fafe2011-03-25 11:00:12 -04002226 error = complete_walk(nd);
2227 if (error)
2228 return ERR_PTR(error);
Al Virofe2d35f2011-03-05 22:58:25 -05002229
2230 audit_inode(pathname, dir);
Al Viro16c2cd72011-02-22 15:50:10 -05002231 error = -EISDIR;
Al Viro1f36f772009-12-26 10:56:19 -05002232 /* trailing slashes? */
Nick Piggin31e6b012011-01-07 17:49:52 +11002233 if (nd->last.name[nd->last.len])
2234 goto exit;
Al Viroa2c36b42009-12-24 03:39:50 -05002235
Al Viroa1e28032009-12-24 02:12:06 -05002236 mutex_lock(&dir->d_inode->i_mutex);
2237
Al Viro6c0d46c2011-03-09 00:59:59 -05002238 dentry = lookup_hash(nd);
2239 error = PTR_ERR(dentry);
2240 if (IS_ERR(dentry)) {
Al Virofb1cc552009-12-24 01:58:28 -05002241 mutex_unlock(&dir->d_inode->i_mutex);
2242 goto exit;
2243 }
2244
Al Viro6c0d46c2011-03-09 00:59:59 -05002245 path->dentry = dentry;
2246 path->mnt = nd->path.mnt;
2247
Al Virofb1cc552009-12-24 01:58:28 -05002248 /* Negative dentry, just create the file */
Al Viro6c0d46c2011-03-09 00:59:59 -05002249 if (!dentry->d_inode) {
Al Viroa218d0f2011-11-21 14:59:34 -05002250 umode_t mode = op->mode;
Al Viro6c0d46c2011-03-09 00:59:59 -05002251 if (!IS_POSIXACL(dir->d_inode))
2252 mode &= ~current_umask();
Al Virofb1cc552009-12-24 01:58:28 -05002253 /*
2254 * This write is needed to ensure that a
Al Viro6c0d46c2011-03-09 00:59:59 -05002255 * rw->ro transition does not occur between
Al Virofb1cc552009-12-24 01:58:28 -05002256 * the time when the file is created and when
2257 * a permanent write count is taken through
2258 * the 'struct file' in nameidata_to_filp().
2259 */
2260 error = mnt_want_write(nd->path.mnt);
2261 if (error)
2262 goto exit_mutex_unlock;
Al Viroca344a892011-03-09 00:36:45 -05002263 want_write = 1;
Al Viro9b44f1b2011-03-09 00:17:27 -05002264 /* Don't check for write permission, don't truncate */
Al Viroca344a892011-03-09 00:36:45 -05002265 open_flag &= ~O_TRUNC;
Al Viro6c0d46c2011-03-09 00:59:59 -05002266 will_truncate = 0;
Al Virobcda7652011-03-13 16:42:14 -04002267 acc_mode = MAY_OPEN;
Al Viro6c0d46c2011-03-09 00:59:59 -05002268 error = security_path_mknod(&nd->path, dentry, mode, 0);
2269 if (error)
2270 goto exit_mutex_unlock;
2271 error = vfs_create(dir->d_inode, dentry, mode, nd);
2272 if (error)
2273 goto exit_mutex_unlock;
2274 mutex_unlock(&dir->d_inode->i_mutex);
2275 dput(nd->path.dentry);
2276 nd->path.dentry = dentry;
Al Viroca344a892011-03-09 00:36:45 -05002277 goto common;
Al Virofb1cc552009-12-24 01:58:28 -05002278 }
2279
2280 /*
2281 * It already exists.
2282 */
2283 mutex_unlock(&dir->d_inode->i_mutex);
2284 audit_inode(pathname, path->dentry);
2285
2286 error = -EEXIST;
Al Viroca344a892011-03-09 00:36:45 -05002287 if (open_flag & O_EXCL)
Al Virofb1cc552009-12-24 01:58:28 -05002288 goto exit_dput;
2289
David Howells9875cf82011-01-14 18:45:21 +00002290 error = follow_managed(path, nd->flags);
2291 if (error < 0)
2292 goto exit_dput;
Al Virofb1cc552009-12-24 01:58:28 -05002293
Al Viroa3fbbde2011-11-07 21:21:26 +00002294 if (error)
2295 nd->flags |= LOOKUP_JUMPED;
2296
Al Virofb1cc552009-12-24 01:58:28 -05002297 error = -ENOENT;
2298 if (!path->dentry->d_inode)
2299 goto exit_dput;
Al Viro9e67f362009-12-26 07:04:50 -05002300
2301 if (path->dentry->d_inode->i_op->follow_link)
Al Virofb1cc552009-12-24 01:58:28 -05002302 return NULL;
Al Virofb1cc552009-12-24 01:58:28 -05002303
2304 path_to_nameidata(path, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11002305 nd->inode = path->dentry->d_inode;
Al Viroa3fbbde2011-11-07 21:21:26 +00002306 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
2307 error = complete_walk(nd);
2308 if (error)
Miklos Szeredi097b1802012-03-06 13:56:33 +01002309 return ERR_PTR(error);
Al Virofb1cc552009-12-24 01:58:28 -05002310 error = -EISDIR;
Nick Piggin31e6b012011-01-07 17:49:52 +11002311 if (S_ISDIR(nd->inode->i_mode))
Al Virofb1cc552009-12-24 01:58:28 -05002312 goto exit;
Al Viro67ee3ad2009-12-26 07:01:01 -05002313ok:
Al Viro6c0d46c2011-03-09 00:59:59 -05002314 if (!S_ISREG(nd->inode->i_mode))
2315 will_truncate = 0;
2316
Al Viro0f9d1a12011-03-09 00:13:14 -05002317 if (will_truncate) {
2318 error = mnt_want_write(nd->path.mnt);
2319 if (error)
2320 goto exit;
Al Viroca344a892011-03-09 00:36:45 -05002321 want_write = 1;
Al Viro0f9d1a12011-03-09 00:13:14 -05002322 }
Al Viroca344a892011-03-09 00:36:45 -05002323common:
Al Virobcda7652011-03-13 16:42:14 -04002324 error = may_open(&nd->path, acc_mode, open_flag);
Al Viroca344a892011-03-09 00:36:45 -05002325 if (error)
Al Viro0f9d1a12011-03-09 00:13:14 -05002326 goto exit;
Al Viro0f9d1a12011-03-09 00:13:14 -05002327 filp = nameidata_to_filp(nd);
2328 if (!IS_ERR(filp)) {
2329 error = ima_file_check(filp, op->acc_mode);
2330 if (error) {
2331 fput(filp);
2332 filp = ERR_PTR(error);
2333 }
2334 }
2335 if (!IS_ERR(filp)) {
2336 if (will_truncate) {
2337 error = handle_truncate(filp);
2338 if (error) {
2339 fput(filp);
2340 filp = ERR_PTR(error);
2341 }
2342 }
2343 }
Al Viroca344a892011-03-09 00:36:45 -05002344out:
2345 if (want_write)
Al Viro0f9d1a12011-03-09 00:13:14 -05002346 mnt_drop_write(nd->path.mnt);
2347 path_put(&nd->path);
Al Virofb1cc552009-12-24 01:58:28 -05002348 return filp;
2349
2350exit_mutex_unlock:
2351 mutex_unlock(&dir->d_inode->i_mutex);
2352exit_dput:
2353 path_put_conditional(path, nd);
2354exit:
Al Viroca344a892011-03-09 00:36:45 -05002355 filp = ERR_PTR(error);
2356 goto out;
Al Virofb1cc552009-12-24 01:58:28 -05002357}
2358
Al Viro13aab422011-02-23 17:54:08 -05002359static struct file *path_openat(int dfd, const char *pathname,
Al Viro73d049a2011-03-11 12:08:24 -05002360 struct nameidata *nd, const struct open_flags *op, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361{
Al Virofe2d35f2011-03-05 22:58:25 -05002362 struct file *base = NULL;
Dave Hansen4a3fd212008-02-15 14:37:48 -08002363 struct file *filp;
Al Viro9850c052010-01-13 15:01:15 -05002364 struct path path;
Al Viro13aab422011-02-23 17:54:08 -05002365 int error;
Nick Piggin31e6b012011-01-07 17:49:52 +11002366
2367 filp = get_empty_filp();
2368 if (!filp)
2369 return ERR_PTR(-ENFILE);
2370
Al Viro47c805d2011-02-23 17:44:09 -05002371 filp->f_flags = op->open_flag;
Al Viro73d049a2011-03-11 12:08:24 -05002372 nd->intent.open.file = filp;
2373 nd->intent.open.flags = open_to_namei_flags(op->open_flag);
2374 nd->intent.open.create_mode = op->mode;
Nick Piggin31e6b012011-01-07 17:49:52 +11002375
Al Viro73d049a2011-03-11 12:08:24 -05002376 error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
Nick Piggin31e6b012011-01-07 17:49:52 +11002377 if (unlikely(error))
Al Viro13aab422011-02-23 17:54:08 -05002378 goto out_filp;
Nick Piggin31e6b012011-01-07 17:49:52 +11002379
Al Virofe2d35f2011-03-05 22:58:25 -05002380 current->total_link_count = 0;
Al Viro73d049a2011-03-11 12:08:24 -05002381 error = link_path_walk(pathname, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11002382 if (unlikely(error))
2383 goto out_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
Al Viro73d049a2011-03-11 12:08:24 -05002385 filp = do_last(nd, &path, op, pathname);
Al Viro806b6812009-12-26 07:16:40 -05002386 while (unlikely(!filp)) { /* trailing symlink */
Nick Piggin7b9337a2011-01-14 08:42:43 +00002387 struct path link = path;
Al Virodef4af32009-12-26 08:37:05 -05002388 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04002389 if (!(nd->flags & LOOKUP_FOLLOW)) {
Al Viro73d049a2011-03-11 12:08:24 -05002390 path_put_conditional(&path, nd);
2391 path_put(&nd->path);
Al Viro40b39132011-03-09 16:22:18 -05002392 filp = ERR_PTR(-ELOOP);
2393 break;
2394 }
Al Viro73d049a2011-03-11 12:08:24 -05002395 nd->flags |= LOOKUP_PARENT;
2396 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
Al Viro574197e2011-03-14 22:20:34 -04002397 error = follow_link(&link, nd, &cookie);
Al Viroc3e380b2011-02-23 13:39:45 -05002398 if (unlikely(error))
Al Virof1afe9e2011-02-22 22:27:28 -05002399 filp = ERR_PTR(error);
Al Viroc3e380b2011-02-23 13:39:45 -05002400 else
Al Viro73d049a2011-03-11 12:08:24 -05002401 filp = do_last(nd, &path, op, pathname);
Al Viro574197e2011-03-14 22:20:34 -04002402 put_link(nd, &link, cookie);
Al Viro806b6812009-12-26 07:16:40 -05002403 }
Al Viro10fa8e62009-12-26 07:09:49 -05002404out:
Al Viro73d049a2011-03-11 12:08:24 -05002405 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
2406 path_put(&nd->root);
Al Virofe2d35f2011-03-05 22:58:25 -05002407 if (base)
2408 fput(base);
Al Viro73d049a2011-03-11 12:08:24 -05002409 release_open_intent(nd);
Al Viro10fa8e62009-12-26 07:09:49 -05002410 return filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
Nick Piggin31e6b012011-01-07 17:49:52 +11002412out_filp:
Al Viro806b6812009-12-26 07:16:40 -05002413 filp = ERR_PTR(error);
Al Viro10fa8e62009-12-26 07:09:49 -05002414 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415}
2416
Al Viro13aab422011-02-23 17:54:08 -05002417struct file *do_filp_open(int dfd, const char *pathname,
2418 const struct open_flags *op, int flags)
2419{
Al Viro73d049a2011-03-11 12:08:24 -05002420 struct nameidata nd;
Al Viro13aab422011-02-23 17:54:08 -05002421 struct file *filp;
2422
Al Viro73d049a2011-03-11 12:08:24 -05002423 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
Al Viro13aab422011-02-23 17:54:08 -05002424 if (unlikely(filp == ERR_PTR(-ECHILD)))
Al Viro73d049a2011-03-11 12:08:24 -05002425 filp = path_openat(dfd, pathname, &nd, op, flags);
Al Viro13aab422011-02-23 17:54:08 -05002426 if (unlikely(filp == ERR_PTR(-ESTALE)))
Al Viro73d049a2011-03-11 12:08:24 -05002427 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
Al Viro13aab422011-02-23 17:54:08 -05002428 return filp;
2429}
2430
Al Viro73d049a2011-03-11 12:08:24 -05002431struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
2432 const char *name, const struct open_flags *op, int flags)
2433{
2434 struct nameidata nd;
2435 struct file *file;
2436
2437 nd.root.mnt = mnt;
2438 nd.root.dentry = dentry;
2439
2440 flags |= LOOKUP_ROOT;
2441
Al Virobcda7652011-03-13 16:42:14 -04002442 if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
Al Viro73d049a2011-03-11 12:08:24 -05002443 return ERR_PTR(-ELOOP);
2444
2445 file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
2446 if (unlikely(file == ERR_PTR(-ECHILD)))
2447 file = path_openat(-1, name, &nd, op, flags);
2448 if (unlikely(file == ERR_PTR(-ESTALE)))
2449 file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
2450 return file;
2451}
2452
Al Viroed75e952011-06-27 16:53:43 -04002453struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002455 struct dentry *dentry = ERR_PTR(-EEXIST);
Al Viroed75e952011-06-27 16:53:43 -04002456 struct nameidata nd;
2457 int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
2458 if (error)
2459 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002461 /*
2462 * Yucky last component or no last component at all?
2463 * (foo/., foo/.., /////)
2464 */
Al Viroed75e952011-06-27 16:53:43 -04002465 if (nd.last_type != LAST_NORM)
2466 goto out;
2467 nd.flags &= ~LOOKUP_PARENT;
2468 nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
2469 nd.intent.open.flags = O_EXCL;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002470
2471 /*
2472 * Do the final lookup.
2473 */
Al Viroed75e952011-06-27 16:53:43 -04002474 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2475 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 if (IS_ERR(dentry))
2477 goto fail;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002478
Al Viroe9baf6e2008-05-15 04:49:12 -04002479 if (dentry->d_inode)
2480 goto eexist;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002481 /*
2482 * Special case - lookup gave negative, but... we had foo/bar/
2483 * From the vfs_mknod() POV we just have a negative dentry -
2484 * all is fine. Let's be bastards - you had / on the end, you've
2485 * been asking for (non-existent) directory. -ENOENT for you.
2486 */
Al Viroed75e952011-06-27 16:53:43 -04002487 if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
Al Viroe9baf6e2008-05-15 04:49:12 -04002488 dput(dentry);
2489 dentry = ERR_PTR(-ENOENT);
Al Viroed75e952011-06-27 16:53:43 -04002490 goto fail;
Al Viroe9baf6e2008-05-15 04:49:12 -04002491 }
Al Viroed75e952011-06-27 16:53:43 -04002492 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 return dentry;
Al Viroe9baf6e2008-05-15 04:49:12 -04002494eexist:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 dput(dentry);
Al Viroe9baf6e2008-05-15 04:49:12 -04002496 dentry = ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497fail:
Al Viroed75e952011-06-27 16:53:43 -04002498 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2499out:
2500 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 return dentry;
2502}
Al Virodae6ad82011-06-26 11:50:15 -04002503EXPORT_SYMBOL(kern_path_create);
2504
2505struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir)
2506{
2507 char *tmp = getname(pathname);
2508 struct dentry *res;
2509 if (IS_ERR(tmp))
2510 return ERR_CAST(tmp);
2511 res = kern_path_create(dfd, tmp, path, is_dir);
2512 putname(tmp);
2513 return res;
2514}
2515EXPORT_SYMBOL(user_path_create);
2516
Al Viro1a67aaf2011-07-26 01:52:52 -04002517int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002519 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
2521 if (error)
2522 return error;
2523
Serge E. Hallyne795b712011-03-23 16:43:25 -07002524 if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
2525 !ns_capable(inode_userns(dir), CAP_MKNOD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 return -EPERM;
2527
Al Viroacfa4382008-12-04 10:06:33 -05002528 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 return -EPERM;
2530
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07002531 error = devcgroup_inode_mknod(mode, dev);
2532 if (error)
2533 return error;
2534
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 error = security_inode_mknod(dir, dentry, mode, dev);
2536 if (error)
2537 return error;
2538
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002540 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002541 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 return error;
2543}
2544
Al Virof69aac02011-07-26 04:31:40 -04002545static int may_mknod(umode_t mode)
Dave Hansen463c3192008-02-15 14:37:57 -08002546{
2547 switch (mode & S_IFMT) {
2548 case S_IFREG:
2549 case S_IFCHR:
2550 case S_IFBLK:
2551 case S_IFIFO:
2552 case S_IFSOCK:
2553 case 0: /* zero mode translates to S_IFREG */
2554 return 0;
2555 case S_IFDIR:
2556 return -EPERM;
2557 default:
2558 return -EINVAL;
2559 }
2560}
2561
Al Viro8208a222011-07-25 17:32:17 -04002562SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002563 unsigned, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564{
Al Viro2ad94ae2008-07-21 09:32:51 -04002565 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04002566 struct path path;
2567 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568
2569 if (S_ISDIR(mode))
2570 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
Al Virodae6ad82011-06-26 11:50:15 -04002572 dentry = user_path_create(dfd, filename, &path, 0);
2573 if (IS_ERR(dentry))
2574 return PTR_ERR(dentry);
Al Viro2ad94ae2008-07-21 09:32:51 -04002575
Al Virodae6ad82011-06-26 11:50:15 -04002576 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04002577 mode &= ~current_umask();
Dave Hansen463c3192008-02-15 14:37:57 -08002578 error = may_mknod(mode);
2579 if (error)
2580 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04002581 error = mnt_want_write(path.mnt);
Dave Hansen463c3192008-02-15 14:37:57 -08002582 if (error)
2583 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04002584 error = security_path_mknod(&path, dentry, mode, dev);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002585 if (error)
2586 goto out_drop_write;
Dave Hansen463c3192008-02-15 14:37:57 -08002587 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 case 0: case S_IFREG:
Al Virodae6ad82011-06-26 11:50:15 -04002589 error = vfs_create(path.dentry->d_inode,dentry,mode,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 break;
2591 case S_IFCHR: case S_IFBLK:
Al Virodae6ad82011-06-26 11:50:15 -04002592 error = vfs_mknod(path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 new_decode_dev(dev));
2594 break;
2595 case S_IFIFO: case S_IFSOCK:
Al Virodae6ad82011-06-26 11:50:15 -04002596 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 }
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002599out_drop_write:
Al Virodae6ad82011-06-26 11:50:15 -04002600 mnt_drop_write(path.mnt);
Dave Hansen463c3192008-02-15 14:37:57 -08002601out_dput:
2602 dput(dentry);
Al Virodae6ad82011-06-26 11:50:15 -04002603 mutex_unlock(&path.dentry->d_inode->i_mutex);
2604 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
2606 return error;
2607}
2608
Al Viro8208a222011-07-25 17:32:17 -04002609SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002610{
2611 return sys_mknodat(AT_FDCWD, filename, mode, dev);
2612}
2613
Al Viro18bb1db2011-07-26 01:41:39 -04002614int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002616 int error = may_create(dir, dentry);
Al Viro8de52772012-02-06 12:45:27 -05002617 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
2619 if (error)
2620 return error;
2621
Al Viroacfa4382008-12-04 10:06:33 -05002622 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 return -EPERM;
2624
2625 mode &= (S_IRWXUGO|S_ISVTX);
2626 error = security_inode_mkdir(dir, dentry, mode);
2627 if (error)
2628 return error;
2629
Al Viro8de52772012-02-06 12:45:27 -05002630 if (max_links && dir->i_nlink >= max_links)
2631 return -EMLINK;
2632
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002634 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002635 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 return error;
2637}
2638
Al Viroa218d0f2011-11-21 14:59:34 -05002639SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640{
Dave Hansen6902d922006-09-30 23:29:01 -07002641 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04002642 struct path path;
2643 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644
Al Virodae6ad82011-06-26 11:50:15 -04002645 dentry = user_path_create(dfd, pathname, &path, 1);
Dave Hansen6902d922006-09-30 23:29:01 -07002646 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04002647 return PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002648
Al Virodae6ad82011-06-26 11:50:15 -04002649 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04002650 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04002651 error = mnt_want_write(path.mnt);
Dave Hansen463c3192008-02-15 14:37:57 -08002652 if (error)
2653 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04002654 error = security_path_mkdir(&path, dentry, mode);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002655 if (error)
2656 goto out_drop_write;
Al Virodae6ad82011-06-26 11:50:15 -04002657 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002658out_drop_write:
Al Virodae6ad82011-06-26 11:50:15 -04002659 mnt_drop_write(path.mnt);
Dave Hansen463c3192008-02-15 14:37:57 -08002660out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002661 dput(dentry);
Al Virodae6ad82011-06-26 11:50:15 -04002662 mutex_unlock(&path.dentry->d_inode->i_mutex);
2663 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 return error;
2665}
2666
Al Viroa218d0f2011-11-21 14:59:34 -05002667SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002668{
2669 return sys_mkdirat(AT_FDCWD, pathname, mode);
2670}
2671
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672/*
Sage Weila71905f2011-05-24 13:06:08 -07002673 * The dentry_unhash() helper will try to drop the dentry early: we
J. Bruce Fieldsc0d02592012-02-15 11:48:40 -05002674 * should have a usage count of 1 if we're the only user of this
Sage Weila71905f2011-05-24 13:06:08 -07002675 * dentry, and if that is true (possibly after pruning the dcache),
2676 * then we drop the dentry now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 *
2678 * A low-level filesystem can, if it choses, legally
2679 * do a
2680 *
2681 * if (!d_unhashed(dentry))
2682 * return -EBUSY;
2683 *
2684 * if it cannot handle the case of removing a directory
2685 * that is still in use by something else..
2686 */
2687void dentry_unhash(struct dentry *dentry)
2688{
Vasily Averindc168422006-12-06 20:37:07 -08002689 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 spin_lock(&dentry->d_lock);
Sage Weil64252c72011-05-24 13:06:05 -07002691 if (dentry->d_count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 __d_drop(dentry);
2693 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694}
2695
2696int vfs_rmdir(struct inode *dir, struct dentry *dentry)
2697{
2698 int error = may_delete(dir, dentry, 1);
2699
2700 if (error)
2701 return error;
2702
Al Viroacfa4382008-12-04 10:06:33 -05002703 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 return -EPERM;
2705
Al Viro1d2ef592011-09-14 18:55:41 +01002706 dget(dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002707 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708
Sage Weil912dbc12011-05-24 13:06:11 -07002709 error = -EBUSY;
2710 if (d_mountpoint(dentry))
2711 goto out;
2712
2713 error = security_inode_rmdir(dir, dentry);
2714 if (error)
2715 goto out;
2716
Sage Weil3cebde22011-05-29 21:20:59 -07002717 shrink_dcache_parent(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07002718 error = dir->i_op->rmdir(dir, dentry);
2719 if (error)
2720 goto out;
2721
2722 dentry->d_inode->i_flags |= S_DEAD;
2723 dont_mount(dentry);
2724
2725out:
2726 mutex_unlock(&dentry->d_inode->i_mutex);
Al Viro1d2ef592011-09-14 18:55:41 +01002727 dput(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07002728 if (!error)
2729 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 return error;
2731}
2732
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002733static long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734{
2735 int error = 0;
2736 char * name;
2737 struct dentry *dentry;
2738 struct nameidata nd;
2739
Al Viro2ad94ae2008-07-21 09:32:51 -04002740 error = user_path_parent(dfd, pathname, &nd, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002742 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
2744 switch(nd.last_type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002745 case LAST_DOTDOT:
2746 error = -ENOTEMPTY;
2747 goto exit1;
2748 case LAST_DOT:
2749 error = -EINVAL;
2750 goto exit1;
2751 case LAST_ROOT:
2752 error = -EBUSY;
2753 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002755
2756 nd.flags &= ~LOOKUP_PARENT;
2757
Jan Blunck4ac91372008-02-14 19:34:32 -08002758 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002759 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002761 if (IS_ERR(dentry))
2762 goto exit2;
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04002763 if (!dentry->d_inode) {
2764 error = -ENOENT;
2765 goto exit3;
2766 }
Dave Hansen06227532008-02-15 14:37:34 -08002767 error = mnt_want_write(nd.path.mnt);
2768 if (error)
2769 goto exit3;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002770 error = security_path_rmdir(&nd.path, dentry);
2771 if (error)
2772 goto exit4;
Jan Blunck4ac91372008-02-14 19:34:32 -08002773 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002774exit4:
Dave Hansen06227532008-02-15 14:37:34 -08002775 mnt_drop_write(nd.path.mnt);
2776exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07002777 dput(dentry);
2778exit2:
Jan Blunck4ac91372008-02-14 19:34:32 -08002779 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002781 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 putname(name);
2783 return error;
2784}
2785
Heiko Carstens3cdad422009-01-14 14:14:22 +01002786SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002787{
2788 return do_rmdir(AT_FDCWD, pathname);
2789}
2790
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791int vfs_unlink(struct inode *dir, struct dentry *dentry)
2792{
2793 int error = may_delete(dir, dentry, 0);
2794
2795 if (error)
2796 return error;
2797
Al Viroacfa4382008-12-04 10:06:33 -05002798 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 return -EPERM;
2800
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002801 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 if (d_mountpoint(dentry))
2803 error = -EBUSY;
2804 else {
2805 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05002806 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 error = dir->i_op->unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05002808 if (!error)
Al Virod83c49f2010-04-30 17:17:09 -04002809 dont_mount(dentry);
Al Virobec10522010-03-03 14:12:08 -05002810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002812 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813
2814 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2815 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
Jan Karaece95912008-02-06 01:37:13 -08002816 fsnotify_link_count(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 }
Robert Love0eeca282005-07-12 17:06:03 -04002819
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 return error;
2821}
2822
2823/*
2824 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002825 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 * writeout happening, and we don't want to prevent access to the directory
2827 * while waiting on the I/O.
2828 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002829static long do_unlinkat(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830{
Al Viro2ad94ae2008-07-21 09:32:51 -04002831 int error;
2832 char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 struct dentry *dentry;
2834 struct nameidata nd;
2835 struct inode *inode = NULL;
2836
Al Viro2ad94ae2008-07-21 09:32:51 -04002837 error = user_path_parent(dfd, pathname, &nd, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002839 return error;
2840
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 error = -EISDIR;
2842 if (nd.last_type != LAST_NORM)
2843 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002844
2845 nd.flags &= ~LOOKUP_PARENT;
2846
Jan Blunck4ac91372008-02-14 19:34:32 -08002847 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002848 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 error = PTR_ERR(dentry);
2850 if (!IS_ERR(dentry)) {
2851 /* Why not before? Because we want correct error value */
Török Edwin50338b82011-06-16 00:06:14 +03002852 if (nd.last.name[nd.last.len])
2853 goto slashes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 inode = dentry->d_inode;
Török Edwin50338b82011-06-16 00:06:14 +03002855 if (!inode)
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04002856 goto slashes;
2857 ihold(inode);
Dave Hansen06227532008-02-15 14:37:34 -08002858 error = mnt_want_write(nd.path.mnt);
2859 if (error)
2860 goto exit2;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002861 error = security_path_unlink(&nd.path, dentry);
2862 if (error)
2863 goto exit3;
Jan Blunck4ac91372008-02-14 19:34:32 -08002864 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002865exit3:
Dave Hansen06227532008-02-15 14:37:34 -08002866 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 exit2:
2868 dput(dentry);
2869 }
Jan Blunck4ac91372008-02-14 19:34:32 -08002870 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 if (inode)
2872 iput(inode); /* truncate the inode here */
2873exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002874 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 putname(name);
2876 return error;
2877
2878slashes:
2879 error = !dentry->d_inode ? -ENOENT :
2880 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2881 goto exit2;
2882}
2883
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002884SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002885{
2886 if ((flag & ~AT_REMOVEDIR) != 0)
2887 return -EINVAL;
2888
2889 if (flag & AT_REMOVEDIR)
2890 return do_rmdir(dfd, pathname);
2891
2892 return do_unlinkat(dfd, pathname);
2893}
2894
Heiko Carstens3480b252009-01-14 14:14:16 +01002895SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002896{
2897 return do_unlinkat(AT_FDCWD, pathname);
2898}
2899
Miklos Szeredidb2e7472008-06-24 16:50:16 +02002900int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002902 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903
2904 if (error)
2905 return error;
2906
Al Viroacfa4382008-12-04 10:06:33 -05002907 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 return -EPERM;
2909
2910 error = security_inode_symlink(dir, dentry, oldname);
2911 if (error)
2912 return error;
2913
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002915 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002916 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 return error;
2918}
2919
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002920SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
2921 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922{
Al Viro2ad94ae2008-07-21 09:32:51 -04002923 int error;
2924 char *from;
Dave Hansen6902d922006-09-30 23:29:01 -07002925 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04002926 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927
2928 from = getname(oldname);
Al Viro2ad94ae2008-07-21 09:32:51 -04002929 if (IS_ERR(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 return PTR_ERR(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04002931
Al Virodae6ad82011-06-26 11:50:15 -04002932 dentry = user_path_create(newdfd, newname, &path, 0);
Dave Hansen6902d922006-09-30 23:29:01 -07002933 error = PTR_ERR(dentry);
2934 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04002935 goto out_putname;
Dave Hansen6902d922006-09-30 23:29:01 -07002936
Al Virodae6ad82011-06-26 11:50:15 -04002937 error = mnt_want_write(path.mnt);
Dave Hansen75c3f292008-02-15 14:37:45 -08002938 if (error)
2939 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04002940 error = security_path_symlink(&path, dentry, from);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002941 if (error)
2942 goto out_drop_write;
Al Virodae6ad82011-06-26 11:50:15 -04002943 error = vfs_symlink(path.dentry->d_inode, dentry, from);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002944out_drop_write:
Al Virodae6ad82011-06-26 11:50:15 -04002945 mnt_drop_write(path.mnt);
Dave Hansen75c3f292008-02-15 14:37:45 -08002946out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002947 dput(dentry);
Al Virodae6ad82011-06-26 11:50:15 -04002948 mutex_unlock(&path.dentry->d_inode->i_mutex);
2949 path_put(&path);
Dave Hansen6902d922006-09-30 23:29:01 -07002950out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 putname(from);
2952 return error;
2953}
2954
Heiko Carstens3480b252009-01-14 14:14:16 +01002955SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002956{
2957 return sys_symlinkat(oldname, AT_FDCWD, newname);
2958}
2959
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2961{
2962 struct inode *inode = old_dentry->d_inode;
Al Viro8de52772012-02-06 12:45:27 -05002963 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 int error;
2965
2966 if (!inode)
2967 return -ENOENT;
2968
Miklos Szeredia95164d2008-07-30 15:08:48 +02002969 error = may_create(dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 if (error)
2971 return error;
2972
2973 if (dir->i_sb != inode->i_sb)
2974 return -EXDEV;
2975
2976 /*
2977 * A link to an append-only or immutable file cannot be created.
2978 */
2979 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2980 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05002981 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002983 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 return -EPERM;
2985
2986 error = security_inode_link(old_dentry, dir, new_dentry);
2987 if (error)
2988 return error;
2989
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002990 mutex_lock(&inode->i_mutex);
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05302991 /* Make sure we don't allow creating hardlink to an unlinked file */
2992 if (inode->i_nlink == 0)
2993 error = -ENOENT;
Al Viro8de52772012-02-06 12:45:27 -05002994 else if (max_links && inode->i_nlink >= max_links)
2995 error = -EMLINK;
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05302996 else
2997 error = dir->i_op->link(old_dentry, dir, new_dentry);
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002998 mutex_unlock(&inode->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07002999 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003000 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 return error;
3002}
3003
3004/*
3005 * Hardlinks are often used in delicate situations. We avoid
3006 * security-related surprises by not following symlinks on the
3007 * newname. --KAB
3008 *
3009 * We don't follow them on the oldname either to be compatible
3010 * with linux 2.0, and to avoid hard-linking to directories
3011 * and other special files. --ADM
3012 */
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003013SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
3014 int, newdfd, const char __user *, newname, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015{
3016 struct dentry *new_dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003017 struct path old_path, new_path;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303018 int how = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303021 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003022 return -EINVAL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303023 /*
3024 * To use null names we require CAP_DAC_READ_SEARCH
3025 * This ensures that not everyone will be able to create
3026 * handlink using the passed filedescriptor.
3027 */
3028 if (flags & AT_EMPTY_PATH) {
3029 if (!capable(CAP_DAC_READ_SEARCH))
3030 return -ENOENT;
3031 how = LOOKUP_EMPTY;
3032 }
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003033
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303034 if (flags & AT_SYMLINK_FOLLOW)
3035 how |= LOOKUP_FOLLOW;
3036
3037 error = user_path_at(olddfd, oldname, how, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04003039 return error;
3040
Al Virodae6ad82011-06-26 11:50:15 -04003041 new_dentry = user_path_create(newdfd, newname, &new_path, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003043 if (IS_ERR(new_dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003044 goto out;
3045
3046 error = -EXDEV;
3047 if (old_path.mnt != new_path.mnt)
3048 goto out_dput;
3049 error = mnt_want_write(new_path.mnt);
Dave Hansen75c3f292008-02-15 14:37:45 -08003050 if (error)
3051 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04003052 error = security_path_link(old_path.dentry, &new_path, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003053 if (error)
3054 goto out_drop_write;
Al Virodae6ad82011-06-26 11:50:15 -04003055 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003056out_drop_write:
Al Virodae6ad82011-06-26 11:50:15 -04003057 mnt_drop_write(new_path.mnt);
Dave Hansen75c3f292008-02-15 14:37:45 -08003058out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07003059 dput(new_dentry);
Al Virodae6ad82011-06-26 11:50:15 -04003060 mutex_unlock(&new_path.dentry->d_inode->i_mutex);
3061 path_put(&new_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062out:
Al Viro2d8f3032008-07-22 09:59:21 -04003063 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064
3065 return error;
3066}
3067
Heiko Carstens3480b252009-01-14 14:14:16 +01003068SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003069{
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003070 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003071}
3072
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073/*
3074 * The worst of all namespace operations - renaming directory. "Perverted"
3075 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
3076 * Problems:
3077 * a) we can get into loop creation. Check is done in is_subdir().
3078 * b) race potential - two innocent renames can create a loop together.
3079 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08003080 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 * story.
3082 * c) we have to lock _three_ objects - parents and victim (if it exists).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003083 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 * whether the target exists). Solution: try to be smart with locking
3085 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08003086 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 * move will be locked. Thus we can rank directories by the tree
3088 * (ancestors first) and rank all non-directories after them.
3089 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08003090 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 * HOWEVER, it relies on the assumption that any object with ->lookup()
3092 * has no more than 1 dentry. If "hybrid" objects will ever appear,
3093 * we'd better make sure that there's no link(2) for them.
Sage Weile4eaac02011-05-24 13:06:07 -07003094 * d) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003095 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05003097 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 * locking].
3099 */
Adrian Bunk75c96f82005-05-05 16:16:09 -07003100static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
3101 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102{
3103 int error = 0;
Sage Weil9055cba2011-05-24 13:06:12 -07003104 struct inode *target = new_dentry->d_inode;
Al Viro8de52772012-02-06 12:45:27 -05003105 unsigned max_links = new_dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
3107 /*
3108 * If we are going to change the parent - check write permissions,
3109 * we'll need to flip '..'.
3110 */
3111 if (new_dir != old_dir) {
Al Virof419a2e2008-07-22 00:07:17 -04003112 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 if (error)
3114 return error;
3115 }
3116
3117 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3118 if (error)
3119 return error;
3120
Al Viro1d2ef592011-09-14 18:55:41 +01003121 dget(new_dentry);
Al Virod83c49f2010-04-30 17:17:09 -04003122 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003123 mutex_lock(&target->i_mutex);
Sage Weil9055cba2011-05-24 13:06:12 -07003124
3125 error = -EBUSY;
3126 if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
3127 goto out;
3128
Al Viro8de52772012-02-06 12:45:27 -05003129 error = -EMLINK;
3130 if (max_links && !target && new_dir != old_dir &&
3131 new_dir->i_nlink >= max_links)
3132 goto out;
3133
Sage Weil3cebde22011-05-29 21:20:59 -07003134 if (target)
3135 shrink_dcache_parent(new_dentry);
Sage Weil9055cba2011-05-24 13:06:12 -07003136 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3137 if (error)
3138 goto out;
3139
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 if (target) {
Sage Weil9055cba2011-05-24 13:06:12 -07003141 target->i_flags |= S_DEAD;
3142 dont_mount(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 }
Sage Weil9055cba2011-05-24 13:06:12 -07003144out:
3145 if (target)
3146 mutex_unlock(&target->i_mutex);
Al Viro1d2ef592011-09-14 18:55:41 +01003147 dput(new_dentry);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07003148 if (!error)
Mark Fasheh349457c2006-09-08 14:22:21 -07003149 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3150 d_move(old_dentry,new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 return error;
3152}
3153
Adrian Bunk75c96f82005-05-05 16:16:09 -07003154static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
3155 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156{
Sage Weil51892bb2011-05-24 13:06:13 -07003157 struct inode *target = new_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 int error;
3159
3160 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3161 if (error)
3162 return error;
3163
3164 dget(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003166 mutex_lock(&target->i_mutex);
Sage Weil51892bb2011-05-24 13:06:13 -07003167
3168 error = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
Sage Weil51892bb2011-05-24 13:06:13 -07003170 goto out;
3171
3172 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3173 if (error)
3174 goto out;
3175
3176 if (target)
3177 dont_mount(new_dentry);
3178 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3179 d_move(old_dentry, new_dentry);
3180out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003182 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 dput(new_dentry);
3184 return error;
3185}
3186
3187int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
3188 struct inode *new_dir, struct dentry *new_dentry)
3189{
3190 int error;
3191 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
Eric Paris59b0df22010-02-08 12:53:52 -05003192 const unsigned char *old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193
3194 if (old_dentry->d_inode == new_dentry->d_inode)
3195 return 0;
3196
3197 error = may_delete(old_dir, old_dentry, is_dir);
3198 if (error)
3199 return error;
3200
3201 if (!new_dentry->d_inode)
Miklos Szeredia95164d2008-07-30 15:08:48 +02003202 error = may_create(new_dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 else
3204 error = may_delete(new_dir, new_dentry, is_dir);
3205 if (error)
3206 return error;
3207
Al Viroacfa4382008-12-04 10:06:33 -05003208 if (!old_dir->i_op->rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 return -EPERM;
3210
Robert Love0eeca282005-07-12 17:06:03 -04003211 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
3212
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 if (is_dir)
3214 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
3215 else
3216 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
Al Viro123df292009-12-25 04:57:57 -05003217 if (!error)
3218 fsnotify_move(old_dir, new_dir, old_name, is_dir,
Al Viro5a190ae2007-06-07 12:19:32 -04003219 new_dentry->d_inode, old_dentry);
Robert Love0eeca282005-07-12 17:06:03 -04003220 fsnotify_oldname_free(old_name);
3221
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 return error;
3223}
3224
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003225SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
3226 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227{
Al Viro2ad94ae2008-07-21 09:32:51 -04003228 struct dentry *old_dir, *new_dir;
3229 struct dentry *old_dentry, *new_dentry;
3230 struct dentry *trap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 struct nameidata oldnd, newnd;
Al Viro2ad94ae2008-07-21 09:32:51 -04003232 char *from;
3233 char *to;
3234 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235
Al Viro2ad94ae2008-07-21 09:32:51 -04003236 error = user_path_parent(olddfd, oldname, &oldnd, &from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 if (error)
3238 goto exit;
3239
Al Viro2ad94ae2008-07-21 09:32:51 -04003240 error = user_path_parent(newdfd, newname, &newnd, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 if (error)
3242 goto exit1;
3243
3244 error = -EXDEV;
Jan Blunck4ac91372008-02-14 19:34:32 -08003245 if (oldnd.path.mnt != newnd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 goto exit2;
3247
Jan Blunck4ac91372008-02-14 19:34:32 -08003248 old_dir = oldnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 error = -EBUSY;
3250 if (oldnd.last_type != LAST_NORM)
3251 goto exit2;
3252
Jan Blunck4ac91372008-02-14 19:34:32 -08003253 new_dir = newnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 if (newnd.last_type != LAST_NORM)
3255 goto exit2;
3256
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003257 oldnd.flags &= ~LOOKUP_PARENT;
3258 newnd.flags &= ~LOOKUP_PARENT;
OGAWA Hirofumi4e9ed2f2008-10-16 07:50:29 +09003259 newnd.flags |= LOOKUP_RENAME_TARGET;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003260
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 trap = lock_rename(new_dir, old_dir);
3262
Christoph Hellwig49705b72005-11-08 21:35:06 -08003263 old_dentry = lookup_hash(&oldnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 error = PTR_ERR(old_dentry);
3265 if (IS_ERR(old_dentry))
3266 goto exit3;
3267 /* source must exist */
3268 error = -ENOENT;
3269 if (!old_dentry->d_inode)
3270 goto exit4;
3271 /* unless the source is a directory trailing slashes give -ENOTDIR */
3272 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
3273 error = -ENOTDIR;
3274 if (oldnd.last.name[oldnd.last.len])
3275 goto exit4;
3276 if (newnd.last.name[newnd.last.len])
3277 goto exit4;
3278 }
3279 /* source should not be ancestor of target */
3280 error = -EINVAL;
3281 if (old_dentry == trap)
3282 goto exit4;
Christoph Hellwig49705b72005-11-08 21:35:06 -08003283 new_dentry = lookup_hash(&newnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 error = PTR_ERR(new_dentry);
3285 if (IS_ERR(new_dentry))
3286 goto exit4;
3287 /* target should not be an ancestor of source */
3288 error = -ENOTEMPTY;
3289 if (new_dentry == trap)
3290 goto exit5;
3291
Dave Hansen9079b1e2008-02-15 14:37:49 -08003292 error = mnt_want_write(oldnd.path.mnt);
3293 if (error)
3294 goto exit5;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003295 error = security_path_rename(&oldnd.path, old_dentry,
3296 &newnd.path, new_dentry);
3297 if (error)
3298 goto exit6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 error = vfs_rename(old_dir->d_inode, old_dentry,
3300 new_dir->d_inode, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003301exit6:
Dave Hansen9079b1e2008-02-15 14:37:49 -08003302 mnt_drop_write(oldnd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303exit5:
3304 dput(new_dentry);
3305exit4:
3306 dput(old_dentry);
3307exit3:
3308 unlock_rename(new_dir, old_dir);
3309exit2:
Jan Blunck1d957f92008-02-14 19:34:35 -08003310 path_put(&newnd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04003311 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003313 path_put(&oldnd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 putname(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04003315exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 return error;
3317}
3318
Heiko Carstensa26eab22009-01-14 14:14:17 +01003319SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003320{
3321 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
3322}
3323
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
3325{
3326 int len;
3327
3328 len = PTR_ERR(link);
3329 if (IS_ERR(link))
3330 goto out;
3331
3332 len = strlen(link);
3333 if (len > (unsigned) buflen)
3334 len = buflen;
3335 if (copy_to_user(buffer, link, len))
3336 len = -EFAULT;
3337out:
3338 return len;
3339}
3340
3341/*
3342 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
3343 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
3344 * using) it for any given inode is up to filesystem.
3345 */
3346int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3347{
3348 struct nameidata nd;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003349 void *cookie;
Marcin Slusarz694a1762008-06-09 16:40:37 -07003350 int res;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003351
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 nd.depth = 0;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003353 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
Marcin Slusarz694a1762008-06-09 16:40:37 -07003354 if (IS_ERR(cookie))
3355 return PTR_ERR(cookie);
3356
3357 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
3358 if (dentry->d_inode->i_op->put_link)
3359 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3360 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361}
3362
3363int vfs_follow_link(struct nameidata *nd, const char *link)
3364{
3365 return __vfs_follow_link(nd, link);
3366}
3367
3368/* get the link contents into pagecache */
3369static char *page_getlink(struct dentry * dentry, struct page **ppage)
3370{
Duane Griffinebd09ab2008-12-19 20:47:12 +00003371 char *kaddr;
3372 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 struct address_space *mapping = dentry->d_inode->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -07003374 page = read_mapping_page(mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 if (IS_ERR(page))
Nick Piggin6fe69002007-05-06 14:49:04 -07003376 return (char*)page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 *ppage = page;
Duane Griffinebd09ab2008-12-19 20:47:12 +00003378 kaddr = kmap(page);
3379 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3380 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381}
3382
3383int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3384{
3385 struct page *page = NULL;
3386 char *s = page_getlink(dentry, &page);
3387 int res = vfs_readlink(dentry,buffer,buflen,s);
3388 if (page) {
3389 kunmap(page);
3390 page_cache_release(page);
3391 }
3392 return res;
3393}
3394
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003395void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003397 struct page *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 nd_set_link(nd, page_getlink(dentry, &page));
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003399 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400}
3401
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003402void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003404 struct page *page = cookie;
3405
3406 if (page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 kunmap(page);
3408 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 }
3410}
3411
Nick Piggin54566b22009-01-04 12:00:53 -08003412/*
3413 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
3414 */
3415int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416{
3417 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003418 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07003419 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08003420 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 char *kaddr;
Nick Piggin54566b22009-01-04 12:00:53 -08003422 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
3423 if (nofs)
3424 flags |= AOP_FLAG_NOFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425
NeilBrown7e53cac2006-03-25 03:07:57 -08003426retry:
Nick Pigginafddba42007-10-16 01:25:01 -07003427 err = pagecache_write_begin(NULL, mapping, 0, len-1,
Nick Piggin54566b22009-01-04 12:00:53 -08003428 flags, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07003430 goto fail;
3431
Cong Wange8e3c3d2011-11-25 23:14:27 +08003432 kaddr = kmap_atomic(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 memcpy(kaddr, symname, len-1);
Cong Wange8e3c3d2011-11-25 23:14:27 +08003434 kunmap_atomic(kaddr);
Nick Pigginafddba42007-10-16 01:25:01 -07003435
3436 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3437 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 if (err < 0)
3439 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07003440 if (err < len-1)
3441 goto retry;
3442
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 mark_inode_dirty(inode);
3444 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445fail:
3446 return err;
3447}
3448
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003449int page_symlink(struct inode *inode, const char *symname, int len)
3450{
3451 return __page_symlink(inode, symname, len,
Nick Piggin54566b22009-01-04 12:00:53 -08003452 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003453}
3454
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003455const struct inode_operations page_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 .readlink = generic_readlink,
3457 .follow_link = page_follow_link_light,
3458 .put_link = page_put_link,
3459};
3460
Al Viro2d8f3032008-07-22 09:59:21 -04003461EXPORT_SYMBOL(user_path_at);
David Howellscc53ce52011-01-14 18:45:26 +00003462EXPORT_SYMBOL(follow_down_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463EXPORT_SYMBOL(follow_down);
3464EXPORT_SYMBOL(follow_up);
3465EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
3466EXPORT_SYMBOL(getname);
3467EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468EXPORT_SYMBOL(lookup_one_len);
3469EXPORT_SYMBOL(page_follow_link_light);
3470EXPORT_SYMBOL(page_put_link);
3471EXPORT_SYMBOL(page_readlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003472EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473EXPORT_SYMBOL(page_symlink);
3474EXPORT_SYMBOL(page_symlink_inode_operations);
Al Virod1811462008-08-02 00:49:18 -04003475EXPORT_SYMBOL(kern_path);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07003476EXPORT_SYMBOL(vfs_path_lookup);
Al Virof419a2e2008-07-22 00:07:17 -04003477EXPORT_SYMBOL(inode_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478EXPORT_SYMBOL(unlock_rename);
3479EXPORT_SYMBOL(vfs_create);
3480EXPORT_SYMBOL(vfs_follow_link);
3481EXPORT_SYMBOL(vfs_link);
3482EXPORT_SYMBOL(vfs_mkdir);
3483EXPORT_SYMBOL(vfs_mknod);
3484EXPORT_SYMBOL(generic_permission);
3485EXPORT_SYMBOL(vfs_readlink);
3486EXPORT_SYMBOL(vfs_rename);
3487EXPORT_SYMBOL(vfs_rmdir);
3488EXPORT_SYMBOL(vfs_symlink);
3489EXPORT_SYMBOL(vfs_unlink);
3490EXPORT_SYMBOL(dentry_unhash);
3491EXPORT_SYMBOL(generic_readlink);