blob: 8d2ba420e42fe1f7660509a18ceee35097330211 [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>
David S. Miller44696902012-05-23 20:12:50 -070019#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/slab.h>
21#include <linux/fs.h>
22#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/pagemap.h>
Robert Love0eeca282005-07-12 17:06:03 -040024#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/personality.h>
26#include <linux/security.h>
Mimi Zohar6146f0d2009-02-04 09:06:57 -050027#include <linux/ima.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/syscalls.h>
29#include <linux/mount.h>
30#include <linux/audit.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080031#include <linux/capability.h>
Trond Myklebust834f2a42005-10-18 14:20:16 -070032#include <linux/file.h>
Ulrich Drepper5590ff02006-01-18 17:43:53 -080033#include <linux/fcntl.h>
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070034#include <linux/device_cgroup.h>
Al Viro5ad4e532009-03-29 19:50:06 -040035#include <linux/fs_struct.h>
Linus Torvaldse77819e2011-07-22 19:30:19 -070036#include <linux/posix_acl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <asm/uaccess.h>
38
Eric Parise81e3f42009-12-04 15:47:36 -050039#include "internal.h"
Al Viroc7105362011-11-24 18:22:03 -050040#include "mount.h"
Eric Parise81e3f42009-12-04 15:47:36 -050041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042/* [Feb-1997 T. Schoebel-Theuer]
43 * Fundamental changes in the pathname lookup mechanisms (namei)
44 * were necessary because of omirr. The reason is that omirr needs
45 * to know the _real_ pathname, not the user-supplied one, in case
46 * of symlinks (and also when transname replacements occur).
47 *
48 * The new code replaces the old recursive symlink resolution with
49 * an iterative one (in case of non-nested symlink chains). It does
50 * this with calls to <fs>_follow_link().
51 * As a side effect, dir_namei(), _namei() and follow_link() are now
52 * replaced with a single function lookup_dentry() that can handle all
53 * the special cases of the former code.
54 *
55 * With the new dcache, the pathname is stored at each inode, at least as
56 * long as the refcount of the inode is positive. As a side effect, the
57 * size of the dcache depends on the inode cache and thus is dynamic.
58 *
59 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
60 * resolution to correspond with current state of the code.
61 *
62 * Note that the symlink resolution is not *completely* iterative.
63 * There is still a significant amount of tail- and mid- recursion in
64 * the algorithm. Also, note that <fs>_readlink() is not used in
65 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
66 * may return different results than <fs>_follow_link(). Many virtual
67 * filesystems (including /proc) exhibit this behavior.
68 */
69
70/* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
71 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
72 * and the name already exists in form of a symlink, try to create the new
73 * name indicated by the symlink. The old code always complained that the
74 * name already exists, due to not following the symlink even if its target
75 * is nonexistent. The new semantics affects also mknod() and link() when
Lucas De Marchi25985ed2011-03-30 22:57:33 -030076 * the name is a symlink pointing to a non-existent name.
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 *
78 * I don't know which semantics is the right one, since I have no access
79 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
80 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
81 * "old" one. Personally, I think the new semantics is much more logical.
82 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
83 * file does succeed in both HP-UX and SunOs, but not in Solaris
84 * and in the old Linux semantics.
85 */
86
87/* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
88 * semantics. See the comments in "open_namei" and "do_link" below.
89 *
90 * [10-Sep-98 Alan Modra] Another symlink change.
91 */
92
93/* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
94 * inside the path - always follow.
95 * in the last component in creation/removal/renaming - never follow.
96 * if LOOKUP_FOLLOW passed - follow.
97 * if the pathname has trailing slashes - follow.
98 * otherwise - don't follow.
99 * (applied in that order).
100 *
101 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
102 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
103 * During the 2.4 we need to fix the userland stuff depending on it -
104 * hopefully we will be able to get rid of that wart in 2.5. So far only
105 * XEmacs seems to be relying on it...
106 */
107/*
108 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
Arjan van de Vena11f3a02006-03-23 03:00:33 -0800109 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 * any extra contention...
111 */
112
113/* In order to reduce some races, while at the same time doing additional
114 * checking and hopefully speeding things up, we copy filenames to the
115 * kernel data space before using them..
116 *
117 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
118 * PATH_MAX includes the nul terminator --RR.
119 */
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +0100120static char *getname_flags(const char __user *filename, int flags, int *empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700122 char *result = __getname(), *err;
123 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700125 if (unlikely(!result))
Eric Paris4043cde2012-01-03 14:23:08 -0500126 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700128 len = strncpy_from_user(result, filename, PATH_MAX);
129 err = ERR_PTR(len);
130 if (unlikely(len < 0))
131 goto error;
132
133 /* The empty path is special. */
134 if (unlikely(!len)) {
135 if (empty)
Eric Paris4043cde2012-01-03 14:23:08 -0500136 *empty = 1;
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700137 err = ERR_PTR(-ENOENT);
138 if (!(flags & LOOKUP_EMPTY))
139 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 }
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700141
142 err = ERR_PTR(-ENAMETOOLONG);
143 if (likely(len < PATH_MAX)) {
144 audit_getname(result);
145 return result;
146 }
147
148error:
149 __putname(result);
150 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
Al Virof52e0c12011-03-14 18:56:51 -0400153char *getname(const char __user * filename)
154{
Linus Torvaldsf7493e52012-03-22 16:10:40 -0700155 return getname_flags(filename, 0, NULL);
Al Virof52e0c12011-03-14 18:56:51 -0400156}
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158#ifdef CONFIG_AUDITSYSCALL
159void putname(const char *name)
160{
Al Viro5ac3a9c2006-07-16 06:38:45 -0400161 if (unlikely(!audit_dummy_context()))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 audit_putname(name);
163 else
164 __putname(name);
165}
166EXPORT_SYMBOL(putname);
167#endif
168
Linus Torvaldse77819e2011-07-22 19:30:19 -0700169static int check_acl(struct inode *inode, int mask)
170{
Linus Torvalds84635d62011-07-25 22:47:03 -0700171#ifdef CONFIG_FS_POSIX_ACL
Linus Torvaldse77819e2011-07-22 19:30:19 -0700172 struct posix_acl *acl;
173
Linus Torvaldse77819e2011-07-22 19:30:19 -0700174 if (mask & MAY_NOT_BLOCK) {
Al Viro35678662011-08-02 21:32:13 -0400175 acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
176 if (!acl)
Linus Torvaldse77819e2011-07-22 19:30:19 -0700177 return -EAGAIN;
Al Viro35678662011-08-02 21:32:13 -0400178 /* no ->get_acl() calls in RCU mode... */
179 if (acl == ACL_NOT_CACHED)
180 return -ECHILD;
Ari Savolainen206b1d02011-08-06 19:43:07 +0300181 return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
Linus Torvaldse77819e2011-07-22 19:30:19 -0700182 }
183
184 acl = get_cached_acl(inode, ACL_TYPE_ACCESS);
185
186 /*
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200187 * A filesystem can force a ACL callback by just never filling the
188 * ACL cache. But normally you'd fill the cache either at inode
189 * instantiation time, or on the first ->get_acl call.
Linus Torvaldse77819e2011-07-22 19:30:19 -0700190 *
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200191 * If the filesystem doesn't have a get_acl() function at all, we'll
192 * just create the negative cache entry.
Linus Torvaldse77819e2011-07-22 19:30:19 -0700193 */
194 if (acl == ACL_NOT_CACHED) {
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200195 if (inode->i_op->get_acl) {
196 acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS);
197 if (IS_ERR(acl))
198 return PTR_ERR(acl);
199 } else {
200 set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
201 return -EAGAIN;
202 }
Linus Torvaldse77819e2011-07-22 19:30:19 -0700203 }
204
205 if (acl) {
206 int error = posix_acl_permission(inode, acl, mask);
207 posix_acl_release(acl);
208 return error;
209 }
Linus Torvalds84635d62011-07-25 22:47:03 -0700210#endif
Linus Torvaldse77819e2011-07-22 19:30:19 -0700211
212 return -EAGAIN;
213}
214
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700215/*
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530216 * This does the basic permission checking
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700217 */
Al Viro7e401452011-06-20 19:12:17 -0400218static int acl_permission_check(struct inode *inode, int mask)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700219{
Linus Torvalds26cf46b2011-05-13 11:51:01 -0700220 unsigned int mode = inode->i_mode;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700221
Serge E. Hallyne795b712011-03-23 16:43:25 -0700222 if (current_user_ns() != inode_userns(inode))
223 goto other_perms;
224
Linus Torvalds14067ff2011-07-25 19:55:52 -0700225 if (likely(current_fsuid() == inode->i_uid))
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700226 mode >>= 6;
227 else {
Linus Torvaldse77819e2011-07-22 19:30:19 -0700228 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
Al Viro7e401452011-06-20 19:12:17 -0400229 int error = check_acl(inode, mask);
Nick Pigginb74c79e2011-01-07 17:49:58 +1100230 if (error != -EAGAIN)
231 return error;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700232 }
233
234 if (in_group_p(inode->i_gid))
235 mode >>= 3;
236 }
237
Serge E. Hallyne795b712011-03-23 16:43:25 -0700238other_perms:
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700239 /*
240 * If the DACs are ok we don't need any capability check.
241 */
Al Viro9c2c7032011-06-20 19:06:22 -0400242 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700243 return 0;
244 return -EACCES;
245}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247/**
Nick Pigginb74c79e2011-01-07 17:49:58 +1100248 * generic_permission - check for access rights on a Posix-like filesystem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 * @inode: inode to check access rights for
Andreas Gruenbacher8fd90c82011-10-23 23:13:30 +0530250 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 *
252 * Used to check for read/write/execute permissions on a file.
253 * We use "fsuid" for this, letting us set arbitrary permissions
254 * for filesystem access without changing the "normal" uids which
Nick Pigginb74c79e2011-01-07 17:49:58 +1100255 * are used for other things.
256 *
257 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
258 * request cannot be satisfied (eg. requires blocking or too much complexity).
259 * It would then be called again in ref-walk mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 */
Al Viro2830ba72011-06-20 19:16:29 -0400261int generic_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700263 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 /*
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530266 * Do the basic permission checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 */
Al Viro7e401452011-06-20 19:12:17 -0400268 ret = acl_permission_check(inode, mask);
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700269 if (ret != -EACCES)
270 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Al Virod594e7e2011-06-20 19:55:42 -0400272 if (S_ISDIR(inode->i_mode)) {
273 /* DACs are overridable for directories */
274 if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
275 return 0;
276 if (!(mask & MAY_WRITE))
277 if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
278 return 0;
279 return -EACCES;
280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 /*
282 * Read/write DACs are always overridable.
Al Virod594e7e2011-06-20 19:55:42 -0400283 * Executable DACs are overridable when there is
284 * at least one exec bit set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 */
Al Virod594e7e2011-06-20 19:55:42 -0400286 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
Serge E. Hallyne795b712011-03-23 16:43:25 -0700287 if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return 0;
289
290 /*
291 * Searching includes executable on directories, else just read.
292 */
Serge E. Hallyn7ea66002009-12-29 14:50:19 -0600293 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
Al Virod594e7e2011-06-20 19:55:42 -0400294 if (mask == MAY_READ)
Serge E. Hallyne795b712011-03-23 16:43:25 -0700295 if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 return 0;
297
298 return -EACCES;
299}
300
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700301/*
302 * We _really_ want to just do "generic_permission()" without
303 * even looking at the inode->i_op values. So we keep a cache
304 * flag in inode->i_opflags, that says "this has not special
305 * permission function, use the fast case".
306 */
307static inline int do_inode_permission(struct inode *inode, int mask)
308{
309 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
310 if (likely(inode->i_op->permission))
311 return inode->i_op->permission(inode, mask);
312
313 /* This gets set once for the inode lifetime */
314 spin_lock(&inode->i_lock);
315 inode->i_opflags |= IOP_FASTPERM;
316 spin_unlock(&inode->i_lock);
317 }
318 return generic_permission(inode, mask);
319}
320
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200321/**
322 * inode_permission - check for access rights to a given inode
323 * @inode: inode to check permission on
Andreas Gruenbacher8fd90c82011-10-23 23:13:30 +0530324 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200325 *
326 * Used to check for read/write/execute permissions on an inode.
327 * We use "fsuid" for this, letting us set arbitrary permissions
328 * for filesystem access without changing the "normal" uids which
329 * are used for other things.
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530330 *
331 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200332 */
Al Virof419a2e2008-07-22 00:07:17 -0400333int inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Al Viroe6305c42008-07-15 21:03:57 -0400335 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700337 if (unlikely(mask & MAY_WRITE)) {
Miklos Szeredi22590e42007-10-16 23:27:08 -0700338 umode_t mode = inode->i_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 /*
341 * Nobody gets write access to a read-only fs.
342 */
343 if (IS_RDONLY(inode) &&
344 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
345 return -EROFS;
346
347 /*
348 * Nobody gets write access to an immutable file.
349 */
350 if (IS_IMMUTABLE(inode))
351 return -EACCES;
352 }
353
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700354 retval = do_inode_permission(inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (retval)
356 return retval;
357
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700358 retval = devcgroup_inode_permission(inode, mask);
359 if (retval)
360 return retval;
361
Eric Parisd09ca732010-07-23 11:43:57 -0400362 return security_inode_permission(inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
Al Virof4d6ff82011-06-19 13:14:21 -0400365/**
Jan Blunck5dd784d2008-02-14 19:34:38 -0800366 * path_get - get a reference to a path
367 * @path: path to get the reference to
368 *
369 * Given a path increment the reference count to the dentry and the vfsmount.
370 */
371void path_get(struct path *path)
372{
373 mntget(path->mnt);
374 dget(path->dentry);
375}
376EXPORT_SYMBOL(path_get);
377
378/**
Jan Blunck1d957f92008-02-14 19:34:35 -0800379 * path_put - put a reference to a path
380 * @path: path to put the reference to
381 *
382 * Given a path decrement the reference count to the dentry and the vfsmount.
383 */
384void path_put(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Jan Blunck1d957f92008-02-14 19:34:35 -0800386 dput(path->dentry);
387 mntput(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
Jan Blunck1d957f92008-02-14 19:34:35 -0800389EXPORT_SYMBOL(path_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Al Viro19660af2011-03-25 10:32:48 -0400391/*
Nick Piggin31e6b012011-01-07 17:49:52 +1100392 * Path walking has 2 modes, rcu-walk and ref-walk (see
Al Viro19660af2011-03-25 10:32:48 -0400393 * Documentation/filesystems/path-lookup.txt). In situations when we can't
394 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
395 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
396 * mode. Refcounts are grabbed at the last known good point before rcu-walk
397 * got stuck, so ref-walk may continue from there. If this is not successful
398 * (eg. a seqcount has changed), then failure is returned and it's up to caller
399 * to restart the path walk from the beginning in ref-walk mode.
Nick Piggin31e6b012011-01-07 17:49:52 +1100400 */
Nick Piggin31e6b012011-01-07 17:49:52 +1100401
402/**
Al Viro19660af2011-03-25 10:32:48 -0400403 * unlazy_walk - try to switch to ref-walk mode.
404 * @nd: nameidata pathwalk data
405 * @dentry: child of nd->path.dentry or NULL
Randy Dunlap39191622011-01-08 19:36:21 -0800406 * Returns: 0 on success, -ECHILD on failure
Nick Piggin31e6b012011-01-07 17:49:52 +1100407 *
Al Viro19660af2011-03-25 10:32:48 -0400408 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
409 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
410 * @nd or NULL. Must be called from rcu-walk context.
Nick Piggin31e6b012011-01-07 17:49:52 +1100411 */
Al Viro19660af2011-03-25 10:32:48 -0400412static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
Nick Piggin31e6b012011-01-07 17:49:52 +1100413{
414 struct fs_struct *fs = current->fs;
415 struct dentry *parent = nd->path.dentry;
Al Viro5b6ca022011-03-09 23:04:47 -0500416 int want_root = 0;
Nick Piggin31e6b012011-01-07 17:49:52 +1100417
418 BUG_ON(!(nd->flags & LOOKUP_RCU));
Al Viro5b6ca022011-03-09 23:04:47 -0500419 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
420 want_root = 1;
Nick Piggin31e6b012011-01-07 17:49:52 +1100421 spin_lock(&fs->lock);
422 if (nd->root.mnt != fs->root.mnt ||
423 nd->root.dentry != fs->root.dentry)
424 goto err_root;
425 }
426 spin_lock(&parent->d_lock);
Al Viro19660af2011-03-25 10:32:48 -0400427 if (!dentry) {
428 if (!__d_rcu_to_refcount(parent, nd->seq))
429 goto err_parent;
430 BUG_ON(nd->inode != parent->d_inode);
431 } else {
Al Viro94c0d4e2011-07-12 21:40:23 -0400432 if (dentry->d_parent != parent)
433 goto err_parent;
Al Viro19660af2011-03-25 10:32:48 -0400434 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
435 if (!__d_rcu_to_refcount(dentry, nd->seq))
436 goto err_child;
437 /*
438 * If the sequence check on the child dentry passed, then
439 * the child has not been removed from its parent. This
440 * means the parent dentry must be valid and able to take
441 * a reference at this point.
442 */
443 BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
444 BUG_ON(!parent->d_count);
445 parent->d_count++;
446 spin_unlock(&dentry->d_lock);
447 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100448 spin_unlock(&parent->d_lock);
Al Viro5b6ca022011-03-09 23:04:47 -0500449 if (want_root) {
Nick Piggin31e6b012011-01-07 17:49:52 +1100450 path_get(&nd->root);
451 spin_unlock(&fs->lock);
452 }
453 mntget(nd->path.mnt);
454
455 rcu_read_unlock();
456 br_read_unlock(vfsmount_lock);
457 nd->flags &= ~LOOKUP_RCU;
458 return 0;
Al Viro19660af2011-03-25 10:32:48 -0400459
460err_child:
Nick Piggin31e6b012011-01-07 17:49:52 +1100461 spin_unlock(&dentry->d_lock);
Al Viro19660af2011-03-25 10:32:48 -0400462err_parent:
Nick Piggin31e6b012011-01-07 17:49:52 +1100463 spin_unlock(&parent->d_lock);
464err_root:
Al Viro5b6ca022011-03-09 23:04:47 -0500465 if (want_root)
Nick Piggin31e6b012011-01-07 17:49:52 +1100466 spin_unlock(&fs->lock);
467 return -ECHILD;
468}
469
Nick Piggin31e6b012011-01-07 17:49:52 +1100470/**
Trond Myklebust834f2a42005-10-18 14:20:16 -0700471 * release_open_intent - free up open intent resources
472 * @nd: pointer to nameidata
473 */
474void release_open_intent(struct nameidata *nd)
475{
Linus Torvalds2dab5972011-02-11 15:53:38 -0800476 struct file *file = nd->intent.open.file;
477
478 if (file && !IS_ERR(file)) {
479 if (file->f_path.dentry == NULL)
480 put_filp(file);
481 else
482 fput(file);
483 }
Trond Myklebust834f2a42005-10-18 14:20:16 -0700484}
485
Al Virof60aef72011-02-15 01:35:28 -0500486static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
Nick Piggin34286d62011-01-07 17:49:57 +1100487{
Al Virof60aef72011-02-15 01:35:28 -0500488 return dentry->d_op->d_revalidate(dentry, nd);
Nick Piggin34286d62011-01-07 17:49:57 +1100489}
490
Al Viro9f1fafe2011-03-25 11:00:12 -0400491/**
492 * complete_walk - successful completion of path walk
493 * @nd: pointer nameidata
Jeff Layton39159de2009-12-07 12:01:50 -0500494 *
Al Viro9f1fafe2011-03-25 11:00:12 -0400495 * If we had been in RCU mode, drop out of it and legitimize nd->path.
496 * Revalidate the final result, unless we'd already done that during
497 * the path walk or the filesystem doesn't ask for it. Return 0 on
498 * success, -error on failure. In case of failure caller does not
499 * need to drop nd->path.
Jeff Layton39159de2009-12-07 12:01:50 -0500500 */
Al Viro9f1fafe2011-03-25 11:00:12 -0400501static int complete_walk(struct nameidata *nd)
Jeff Layton39159de2009-12-07 12:01:50 -0500502{
Al Viro16c2cd72011-02-22 15:50:10 -0500503 struct dentry *dentry = nd->path.dentry;
Jeff Layton39159de2009-12-07 12:01:50 -0500504 int status;
Jeff Layton39159de2009-12-07 12:01:50 -0500505
Al Viro9f1fafe2011-03-25 11:00:12 -0400506 if (nd->flags & LOOKUP_RCU) {
507 nd->flags &= ~LOOKUP_RCU;
508 if (!(nd->flags & LOOKUP_ROOT))
509 nd->root.mnt = NULL;
510 spin_lock(&dentry->d_lock);
511 if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
512 spin_unlock(&dentry->d_lock);
513 rcu_read_unlock();
514 br_read_unlock(vfsmount_lock);
515 return -ECHILD;
516 }
517 BUG_ON(nd->inode != dentry->d_inode);
518 spin_unlock(&dentry->d_lock);
519 mntget(nd->path.mnt);
520 rcu_read_unlock();
521 br_read_unlock(vfsmount_lock);
522 }
523
Al Viro16c2cd72011-02-22 15:50:10 -0500524 if (likely(!(nd->flags & LOOKUP_JUMPED)))
Jeff Layton39159de2009-12-07 12:01:50 -0500525 return 0;
526
Al Viro16c2cd72011-02-22 15:50:10 -0500527 if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
528 return 0;
529
530 if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
531 return 0;
532
533 /* Note: we do not d_invalidate() */
Nick Piggin34286d62011-01-07 17:49:57 +1100534 status = d_revalidate(dentry, nd);
Jeff Layton39159de2009-12-07 12:01:50 -0500535 if (status > 0)
536 return 0;
537
Al Viro16c2cd72011-02-22 15:50:10 -0500538 if (!status)
Jeff Layton39159de2009-12-07 12:01:50 -0500539 status = -ESTALE;
Al Viro16c2cd72011-02-22 15:50:10 -0500540
Al Viro9f1fafe2011-03-25 11:00:12 -0400541 path_put(&nd->path);
Jeff Layton39159de2009-12-07 12:01:50 -0500542 return status;
543}
544
Al Viro2a737872009-04-07 11:49:53 -0400545static __always_inline void set_root(struct nameidata *nd)
546{
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200547 if (!nd->root.mnt)
548 get_fs_root(current->fs, &nd->root);
Al Viro2a737872009-04-07 11:49:53 -0400549}
550
Al Viro6de88d72009-08-09 01:41:57 +0400551static int link_path_walk(const char *, struct nameidata *);
552
Nick Piggin31e6b012011-01-07 17:49:52 +1100553static __always_inline void set_root_rcu(struct nameidata *nd)
554{
555 if (!nd->root.mnt) {
556 struct fs_struct *fs = current->fs;
Nick Pigginc28cc362011-01-07 17:49:53 +1100557 unsigned seq;
558
559 do {
560 seq = read_seqcount_begin(&fs->seq);
561 nd->root = fs->root;
Tim Chenc1530012011-04-15 11:39:29 -0700562 nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
Nick Pigginc28cc362011-01-07 17:49:53 +1100563 } while (read_seqcount_retry(&fs->seq, seq));
Nick Piggin31e6b012011-01-07 17:49:52 +1100564 }
565}
566
Arjan van de Venf1662352006-01-14 13:21:31 -0800567static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Nick Piggin31e6b012011-01-07 17:49:52 +1100569 int ret;
570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 if (IS_ERR(link))
572 goto fail;
573
574 if (*link == '/') {
Al Viro2a737872009-04-07 11:49:53 -0400575 set_root(nd);
Jan Blunck1d957f92008-02-14 19:34:35 -0800576 path_put(&nd->path);
Al Viro2a737872009-04-07 11:49:53 -0400577 nd->path = nd->root;
578 path_get(&nd->root);
Al Viro16c2cd72011-02-22 15:50:10 -0500579 nd->flags |= LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100581 nd->inode = nd->path.dentry->d_inode;
Christoph Hellwigb4091d52008-11-05 15:07:21 +0100582
Nick Piggin31e6b012011-01-07 17:49:52 +1100583 ret = link_path_walk(link, nd);
584 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585fail:
Jan Blunck1d957f92008-02-14 19:34:35 -0800586 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 return PTR_ERR(link);
588}
589
Jan Blunck1d957f92008-02-14 19:34:35 -0800590static void path_put_conditional(struct path *path, struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700591{
592 dput(path->dentry);
Jan Blunck4ac91372008-02-14 19:34:32 -0800593 if (path->mnt != nd->path.mnt)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700594 mntput(path->mnt);
595}
596
Nick Piggin7b9337a2011-01-14 08:42:43 +0000597static inline void path_to_nameidata(const struct path *path,
598 struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700599{
Nick Piggin31e6b012011-01-07 17:49:52 +1100600 if (!(nd->flags & LOOKUP_RCU)) {
601 dput(nd->path.dentry);
602 if (nd->path.mnt != path->mnt)
603 mntput(nd->path.mnt);
Huang Shijie9a229682010-04-02 17:37:13 +0800604 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100605 nd->path.mnt = path->mnt;
Jan Blunck4ac91372008-02-14 19:34:32 -0800606 nd->path.dentry = path->dentry;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700607}
608
Al Viro574197e2011-03-14 22:20:34 -0400609static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
610{
611 struct inode *inode = link->dentry->d_inode;
612 if (!IS_ERR(cookie) && inode->i_op->put_link)
613 inode->i_op->put_link(link->dentry, nd, cookie);
614 path_put(link);
615}
616
Al Virodef4af32009-12-26 08:37:05 -0500617static __always_inline int
Al Viro574197e2011-03-14 22:20:34 -0400618follow_link(struct path *link, struct nameidata *nd, void **p)
Ian Kent051d3812006-03-27 01:14:53 -0800619{
620 int error;
Nick Piggin7b9337a2011-01-14 08:42:43 +0000621 struct dentry *dentry = link->dentry;
Ian Kent051d3812006-03-27 01:14:53 -0800622
Al Viro844a3912011-02-15 00:38:26 -0500623 BUG_ON(nd->flags & LOOKUP_RCU);
624
Al Viro0e794582011-03-16 02:45:02 -0400625 if (link->mnt == nd->path.mnt)
626 mntget(link->mnt);
627
Al Viro574197e2011-03-14 22:20:34 -0400628 if (unlikely(current->total_link_count >= 40)) {
629 *p = ERR_PTR(-ELOOP); /* no ->put_link(), please */
Al Viro574197e2011-03-14 22:20:34 -0400630 path_put(&nd->path);
631 return -ELOOP;
632 }
633 cond_resched();
634 current->total_link_count++;
635
Al Viro68ac1232012-03-15 08:21:57 -0400636 touch_atime(link);
Ian Kent051d3812006-03-27 01:14:53 -0800637 nd_set_link(nd, NULL);
638
Al Viro36f3b4f2011-02-22 21:24:38 -0500639 error = security_inode_follow_link(link->dentry, nd);
640 if (error) {
641 *p = ERR_PTR(error); /* no ->put_link(), please */
642 path_put(&nd->path);
643 return error;
644 }
645
Al Viro86acdca12009-12-22 23:45:11 -0500646 nd->last_type = LAST_BIND;
Al Virodef4af32009-12-26 08:37:05 -0500647 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
648 error = PTR_ERR(*p);
649 if (!IS_ERR(*p)) {
Ian Kent051d3812006-03-27 01:14:53 -0800650 char *s = nd_get_link(nd);
651 error = 0;
652 if (s)
653 error = __vfs_follow_link(nd, s);
Al Virobcda7652011-03-13 16:42:14 -0400654 else if (nd->last_type == LAST_BIND) {
Al Viro16c2cd72011-02-22 15:50:10 -0500655 nd->flags |= LOOKUP_JUMPED;
Al Virob21041d2011-03-14 20:01:51 -0400656 nd->inode = nd->path.dentry->d_inode;
657 if (nd->inode->i_op->follow_link) {
Al Virobcda7652011-03-13 16:42:14 -0400658 /* stepped on a _really_ weird one */
659 path_put(&nd->path);
660 error = -ELOOP;
661 }
662 }
Ian Kent051d3812006-03-27 01:14:53 -0800663 }
Ian Kent051d3812006-03-27 01:14:53 -0800664 return error;
665}
666
Nick Piggin31e6b012011-01-07 17:49:52 +1100667static int follow_up_rcu(struct path *path)
668{
Al Viro0714a532011-11-24 22:19:58 -0500669 struct mount *mnt = real_mount(path->mnt);
670 struct mount *parent;
Nick Piggin31e6b012011-01-07 17:49:52 +1100671 struct dentry *mountpoint;
672
Al Viro0714a532011-11-24 22:19:58 -0500673 parent = mnt->mnt_parent;
674 if (&parent->mnt == path->mnt)
Nick Piggin31e6b012011-01-07 17:49:52 +1100675 return 0;
Al Viroa73324d2011-11-24 22:25:07 -0500676 mountpoint = mnt->mnt_mountpoint;
Nick Piggin31e6b012011-01-07 17:49:52 +1100677 path->dentry = mountpoint;
Al Viro0714a532011-11-24 22:19:58 -0500678 path->mnt = &parent->mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +1100679 return 1;
680}
681
Al Virobab77eb2009-04-18 03:26:48 -0400682int follow_up(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Al Viro0714a532011-11-24 22:19:58 -0500684 struct mount *mnt = real_mount(path->mnt);
685 struct mount *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 struct dentry *mountpoint;
Nick Piggin99b7db72010-08-18 04:37:39 +1000687
688 br_read_lock(vfsmount_lock);
Al Viro0714a532011-11-24 22:19:58 -0500689 parent = mnt->mnt_parent;
690 if (&parent->mnt == path->mnt) {
Nick Piggin99b7db72010-08-18 04:37:39 +1000691 br_read_unlock(vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 return 0;
693 }
Al Viro0714a532011-11-24 22:19:58 -0500694 mntget(&parent->mnt);
Al Viroa73324d2011-11-24 22:25:07 -0500695 mountpoint = dget(mnt->mnt_mountpoint);
Nick Piggin99b7db72010-08-18 04:37:39 +1000696 br_read_unlock(vfsmount_lock);
Al Virobab77eb2009-04-18 03:26:48 -0400697 dput(path->dentry);
698 path->dentry = mountpoint;
699 mntput(path->mnt);
Al Viro0714a532011-11-24 22:19:58 -0500700 path->mnt = &parent->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return 1;
702}
703
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100704/*
David Howells9875cf82011-01-14 18:45:21 +0000705 * Perform an automount
706 * - return -EISDIR to tell follow_managed() to stop and return the path we
707 * were called with.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 */
David Howells9875cf82011-01-14 18:45:21 +0000709static int follow_automount(struct path *path, unsigned flags,
710 bool *need_mntput)
Nick Piggin31e6b012011-01-07 17:49:52 +1100711{
David Howells9875cf82011-01-14 18:45:21 +0000712 struct vfsmount *mnt;
David Howellsea5b7782011-01-14 19:10:03 +0000713 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +1100714
David Howells9875cf82011-01-14 18:45:21 +0000715 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
716 return -EREMOTE;
Al Viro463ffb22005-06-06 13:36:05 -0700717
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200718 /* We don't want to mount if someone's just doing a stat -
719 * unless they're stat'ing a directory and appended a '/' to
720 * the name.
721 *
722 * We do, however, want to mount if someone wants to open or
723 * create a file of any type under the mountpoint, wants to
724 * traverse through the mountpoint or wants to open the
725 * mounted directory. Also, autofs may mark negative dentries
726 * as being automount points. These will need the attentions
727 * of the daemon to instantiate them before they can be used.
David Howells9875cf82011-01-14 18:45:21 +0000728 */
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200729 if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
Linus Torvaldsd94c1772011-09-26 17:44:55 -0700730 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200731 path->dentry->d_inode)
732 return -EISDIR;
733
David Howells9875cf82011-01-14 18:45:21 +0000734 current->total_link_count++;
735 if (current->total_link_count >= 40)
736 return -ELOOP;
737
738 mnt = path->dentry->d_op->d_automount(path);
739 if (IS_ERR(mnt)) {
740 /*
741 * The filesystem is allowed to return -EISDIR here to indicate
742 * it doesn't want to automount. For instance, autofs would do
743 * this so that its userspace daemon can mount on this dentry.
744 *
745 * However, we can only permit this if it's a terminal point in
746 * the path being looked up; if it wasn't then the remainder of
747 * the path is inaccessible and we should say so.
748 */
Al Viro49084c32011-06-25 21:59:52 -0400749 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
David Howells9875cf82011-01-14 18:45:21 +0000750 return -EREMOTE;
751 return PTR_ERR(mnt);
752 }
David Howellsea5b7782011-01-14 19:10:03 +0000753
David Howells9875cf82011-01-14 18:45:21 +0000754 if (!mnt) /* mount collision */
755 return 0;
756
Al Viro8aef1882011-06-16 15:10:06 +0100757 if (!*need_mntput) {
758 /* lock_mount() may release path->mnt on error */
759 mntget(path->mnt);
760 *need_mntput = true;
761 }
Al Viro19a167a2011-01-17 01:35:23 -0500762 err = finish_automount(mnt, path);
David Howellsea5b7782011-01-14 19:10:03 +0000763
David Howellsea5b7782011-01-14 19:10:03 +0000764 switch (err) {
765 case -EBUSY:
766 /* Someone else made a mount here whilst we were busy */
Al Viro19a167a2011-01-17 01:35:23 -0500767 return 0;
David Howellsea5b7782011-01-14 19:10:03 +0000768 case 0:
Al Viro8aef1882011-06-16 15:10:06 +0100769 path_put(path);
David Howellsea5b7782011-01-14 19:10:03 +0000770 path->mnt = mnt;
771 path->dentry = dget(mnt->mnt_root);
David Howellsea5b7782011-01-14 19:10:03 +0000772 return 0;
Al Viro19a167a2011-01-17 01:35:23 -0500773 default:
774 return err;
David Howellsea5b7782011-01-14 19:10:03 +0000775 }
Al Viro19a167a2011-01-17 01:35:23 -0500776
David Howells9875cf82011-01-14 18:45:21 +0000777}
778
779/*
780 * Handle a dentry that is managed in some way.
David Howellscc53ce52011-01-14 18:45:26 +0000781 * - Flagged for transit management (autofs)
David Howells9875cf82011-01-14 18:45:21 +0000782 * - Flagged as mountpoint
783 * - Flagged as automount point
784 *
785 * This may only be called in refwalk mode.
786 *
787 * Serialization is taken care of in namespace.c
788 */
789static int follow_managed(struct path *path, unsigned flags)
790{
Al Viro8aef1882011-06-16 15:10:06 +0100791 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
David Howells9875cf82011-01-14 18:45:21 +0000792 unsigned managed;
793 bool need_mntput = false;
Al Viro8aef1882011-06-16 15:10:06 +0100794 int ret = 0;
David Howells9875cf82011-01-14 18:45:21 +0000795
796 /* Given that we're not holding a lock here, we retain the value in a
797 * local variable for each dentry as we look at it so that we don't see
798 * the components of that value change under us */
799 while (managed = ACCESS_ONCE(path->dentry->d_flags),
800 managed &= DCACHE_MANAGED_DENTRY,
801 unlikely(managed != 0)) {
David Howellscc53ce52011-01-14 18:45:26 +0000802 /* Allow the filesystem to manage the transit without i_mutex
803 * being held. */
804 if (managed & DCACHE_MANAGE_TRANSIT) {
805 BUG_ON(!path->dentry->d_op);
806 BUG_ON(!path->dentry->d_op->d_manage);
Al Viro1aed3e42011-03-18 09:09:02 -0400807 ret = path->dentry->d_op->d_manage(path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +0000808 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +0100809 break;
David Howellscc53ce52011-01-14 18:45:26 +0000810 }
811
David Howells9875cf82011-01-14 18:45:21 +0000812 /* Transit to a mounted filesystem. */
813 if (managed & DCACHE_MOUNTED) {
814 struct vfsmount *mounted = lookup_mnt(path);
815 if (mounted) {
816 dput(path->dentry);
817 if (need_mntput)
818 mntput(path->mnt);
819 path->mnt = mounted;
820 path->dentry = dget(mounted->mnt_root);
821 need_mntput = true;
822 continue;
823 }
824
825 /* Something is mounted on this dentry in another
826 * namespace and/or whatever was mounted there in this
827 * namespace got unmounted before we managed to get the
828 * vfsmount_lock */
829 }
830
831 /* Handle an automount point */
832 if (managed & DCACHE_NEED_AUTOMOUNT) {
833 ret = follow_automount(path, flags, &need_mntput);
834 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +0100835 break;
David Howells9875cf82011-01-14 18:45:21 +0000836 continue;
837 }
838
839 /* We didn't change the current path point */
840 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 }
Al Viro8aef1882011-06-16 15:10:06 +0100842
843 if (need_mntput && path->mnt == mnt)
844 mntput(path->mnt);
845 if (ret == -EISDIR)
846 ret = 0;
Al Viroa3fbbde2011-11-07 21:21:26 +0000847 return ret < 0 ? ret : need_mntput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
849
David Howellscc53ce52011-01-14 18:45:26 +0000850int follow_down_one(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
852 struct vfsmount *mounted;
853
Al Viro1c755af2009-04-18 14:06:57 -0400854 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -0400856 dput(path->dentry);
857 mntput(path->mnt);
858 path->mnt = mounted;
859 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 return 1;
861 }
862 return 0;
863}
864
Ian Kent62a73752011-03-25 01:51:02 +0800865static inline bool managed_dentry_might_block(struct dentry *dentry)
866{
867 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
868 dentry->d_op->d_manage(dentry, true) < 0);
869}
870
David Howells9875cf82011-01-14 18:45:21 +0000871/*
Al Viro287548e2011-05-27 06:50:06 -0400872 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
873 * we meet a managed dentry that would need blocking.
David Howells9875cf82011-01-14 18:45:21 +0000874 */
875static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
Al Viro287548e2011-05-27 06:50:06 -0400876 struct inode **inode)
David Howells9875cf82011-01-14 18:45:21 +0000877{
Ian Kent62a73752011-03-25 01:51:02 +0800878 for (;;) {
Al Viroc7105362011-11-24 18:22:03 -0500879 struct mount *mounted;
Ian Kent62a73752011-03-25 01:51:02 +0800880 /*
881 * Don't forget we might have a non-mountpoint managed dentry
882 * that wants to block transit.
883 */
Al Viro287548e2011-05-27 06:50:06 -0400884 if (unlikely(managed_dentry_might_block(path->dentry)))
David Howellsab909112011-01-14 18:46:51 +0000885 return false;
Ian Kent62a73752011-03-25 01:51:02 +0800886
887 if (!d_mountpoint(path->dentry))
888 break;
889
David Howells9875cf82011-01-14 18:45:21 +0000890 mounted = __lookup_mnt(path->mnt, path->dentry, 1);
891 if (!mounted)
892 break;
Al Viroc7105362011-11-24 18:22:03 -0500893 path->mnt = &mounted->mnt;
894 path->dentry = mounted->mnt.mnt_root;
Al Viroa3fbbde2011-11-07 21:21:26 +0000895 nd->flags |= LOOKUP_JUMPED;
David Howells9875cf82011-01-14 18:45:21 +0000896 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
Linus Torvalds59430262011-07-18 15:43:29 -0700897 /*
898 * Update the inode too. We don't need to re-check the
899 * dentry sequence number here after this d_inode read,
900 * because a mount-point is always pinned.
901 */
902 *inode = path->dentry->d_inode;
David Howells9875cf82011-01-14 18:45:21 +0000903 }
David Howells9875cf82011-01-14 18:45:21 +0000904 return true;
905}
906
Al Virodea39372011-05-27 06:53:39 -0400907static void follow_mount_rcu(struct nameidata *nd)
Al Viro287548e2011-05-27 06:50:06 -0400908{
Al Virodea39372011-05-27 06:53:39 -0400909 while (d_mountpoint(nd->path.dentry)) {
Al Viroc7105362011-11-24 18:22:03 -0500910 struct mount *mounted;
Al Virodea39372011-05-27 06:53:39 -0400911 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
Al Viro287548e2011-05-27 06:50:06 -0400912 if (!mounted)
913 break;
Al Viroc7105362011-11-24 18:22:03 -0500914 nd->path.mnt = &mounted->mnt;
915 nd->path.dentry = mounted->mnt.mnt_root;
Al Virodea39372011-05-27 06:53:39 -0400916 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro287548e2011-05-27 06:50:06 -0400917 }
918}
919
Nick Piggin31e6b012011-01-07 17:49:52 +1100920static int follow_dotdot_rcu(struct nameidata *nd)
921{
Nick Piggin31e6b012011-01-07 17:49:52 +1100922 set_root_rcu(nd);
923
David Howells9875cf82011-01-14 18:45:21 +0000924 while (1) {
Nick Piggin31e6b012011-01-07 17:49:52 +1100925 if (nd->path.dentry == nd->root.dentry &&
926 nd->path.mnt == nd->root.mnt) {
927 break;
928 }
929 if (nd->path.dentry != nd->path.mnt->mnt_root) {
930 struct dentry *old = nd->path.dentry;
931 struct dentry *parent = old->d_parent;
932 unsigned seq;
933
934 seq = read_seqcount_begin(&parent->d_seq);
935 if (read_seqcount_retry(&old->d_seq, nd->seq))
Al Viroef7562d2011-03-04 14:35:59 -0500936 goto failed;
Nick Piggin31e6b012011-01-07 17:49:52 +1100937 nd->path.dentry = parent;
938 nd->seq = seq;
939 break;
940 }
941 if (!follow_up_rcu(&nd->path))
942 break;
943 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Nick Piggin31e6b012011-01-07 17:49:52 +1100944 }
Al Virodea39372011-05-27 06:53:39 -0400945 follow_mount_rcu(nd);
946 nd->inode = nd->path.dentry->d_inode;
Nick Piggin31e6b012011-01-07 17:49:52 +1100947 return 0;
Al Viroef7562d2011-03-04 14:35:59 -0500948
949failed:
950 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -0500951 if (!(nd->flags & LOOKUP_ROOT))
952 nd->root.mnt = NULL;
Al Viroef7562d2011-03-04 14:35:59 -0500953 rcu_read_unlock();
954 br_read_unlock(vfsmount_lock);
955 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +1100956}
957
David Howells9875cf82011-01-14 18:45:21 +0000958/*
David Howellscc53ce52011-01-14 18:45:26 +0000959 * Follow down to the covering mount currently visible to userspace. At each
960 * point, the filesystem owning that dentry may be queried as to whether the
961 * caller is permitted to proceed or not.
David Howellscc53ce52011-01-14 18:45:26 +0000962 */
Al Viro7cc90cc2011-03-18 09:04:20 -0400963int follow_down(struct path *path)
David Howellscc53ce52011-01-14 18:45:26 +0000964{
965 unsigned managed;
966 int ret;
967
968 while (managed = ACCESS_ONCE(path->dentry->d_flags),
969 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
970 /* Allow the filesystem to manage the transit without i_mutex
971 * being held.
972 *
973 * We indicate to the filesystem if someone is trying to mount
974 * something here. This gives autofs the chance to deny anyone
975 * other than its daemon the right to mount on its
976 * superstructure.
977 *
978 * The filesystem may sleep at this point.
979 */
980 if (managed & DCACHE_MANAGE_TRANSIT) {
981 BUG_ON(!path->dentry->d_op);
982 BUG_ON(!path->dentry->d_op->d_manage);
David Howellsab909112011-01-14 18:46:51 +0000983 ret = path->dentry->d_op->d_manage(
Al Viro1aed3e42011-03-18 09:09:02 -0400984 path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +0000985 if (ret < 0)
986 return ret == -EISDIR ? 0 : ret;
987 }
988
989 /* Transit to a mounted filesystem. */
990 if (managed & DCACHE_MOUNTED) {
991 struct vfsmount *mounted = lookup_mnt(path);
992 if (!mounted)
993 break;
994 dput(path->dentry);
995 mntput(path->mnt);
996 path->mnt = mounted;
997 path->dentry = dget(mounted->mnt_root);
998 continue;
999 }
1000
1001 /* Don't handle automount points here */
1002 break;
1003 }
1004 return 0;
1005}
1006
1007/*
David Howells9875cf82011-01-14 18:45:21 +00001008 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1009 */
1010static void follow_mount(struct path *path)
1011{
1012 while (d_mountpoint(path->dentry)) {
1013 struct vfsmount *mounted = lookup_mnt(path);
1014 if (!mounted)
1015 break;
1016 dput(path->dentry);
1017 mntput(path->mnt);
1018 path->mnt = mounted;
1019 path->dentry = dget(mounted->mnt_root);
1020 }
1021}
1022
Nick Piggin31e6b012011-01-07 17:49:52 +11001023static void follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
Al Viro2a737872009-04-07 11:49:53 -04001025 set_root(nd);
Andreas Mohre518ddb2006-09-29 02:01:22 -07001026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 while(1) {
Jan Blunck4ac91372008-02-14 19:34:32 -08001028 struct dentry *old = nd->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Al Viro2a737872009-04-07 11:49:53 -04001030 if (nd->path.dentry == nd->root.dentry &&
1031 nd->path.mnt == nd->root.mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 break;
1033 }
Jan Blunck4ac91372008-02-14 19:34:32 -08001034 if (nd->path.dentry != nd->path.mnt->mnt_root) {
Al Viro3088dd72010-01-30 15:47:29 -05001035 /* rare case of legitimate dget_parent()... */
1036 nd->path.dentry = dget_parent(nd->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 dput(old);
1038 break;
1039 }
Al Viro3088dd72010-01-30 15:47:29 -05001040 if (!follow_up(&nd->path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 }
Al Viro79ed0222009-04-18 13:59:41 -04001043 follow_mount(&nd->path);
Nick Piggin31e6b012011-01-07 17:49:52 +11001044 nd->inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045}
1046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047/*
Miklos Szeredibad61182012-03-26 12:54:24 +02001048 * This looks up the name in dcache, possibly revalidates the old dentry and
1049 * allocates a new one if not found or not valid. In the need_lookup argument
1050 * returns whether i_op->lookup is necessary.
1051 *
1052 * dir->d_inode->i_mutex must be held
Nick Pigginbaa03892010-08-18 04:37:31 +10001053 */
Miklos Szeredibad61182012-03-26 12:54:24 +02001054static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
1055 struct nameidata *nd, bool *need_lookup)
Nick Pigginbaa03892010-08-18 04:37:31 +10001056{
Nick Pigginbaa03892010-08-18 04:37:31 +10001057 struct dentry *dentry;
Miklos Szeredibad61182012-03-26 12:54:24 +02001058 int error;
Nick Pigginbaa03892010-08-18 04:37:31 +10001059
Miklos Szeredibad61182012-03-26 12:54:24 +02001060 *need_lookup = false;
1061 dentry = d_lookup(dir, name);
1062 if (dentry) {
1063 if (d_need_lookup(dentry)) {
1064 *need_lookup = true;
1065 } else if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
1066 error = d_revalidate(dentry, nd);
1067 if (unlikely(error <= 0)) {
1068 if (error < 0) {
1069 dput(dentry);
1070 return ERR_PTR(error);
1071 } else if (!d_invalidate(dentry)) {
1072 dput(dentry);
1073 dentry = NULL;
1074 }
1075 }
1076 }
1077 }
Nick Pigginbaa03892010-08-18 04:37:31 +10001078
Miklos Szeredibad61182012-03-26 12:54:24 +02001079 if (!dentry) {
1080 dentry = d_alloc(dir, name);
1081 if (unlikely(!dentry))
1082 return ERR_PTR(-ENOMEM);
Nick Pigginbaa03892010-08-18 04:37:31 +10001083
Miklos Szeredibad61182012-03-26 12:54:24 +02001084 *need_lookup = true;
Nick Pigginbaa03892010-08-18 04:37:31 +10001085 }
1086 return dentry;
1087}
1088
1089/*
Miklos Szeredibad61182012-03-26 12:54:24 +02001090 * Call i_op->lookup on the dentry. The dentry must be negative but may be
1091 * hashed if it was pouplated with DCACHE_NEED_LOOKUP.
1092 *
1093 * dir->d_inode->i_mutex must be held
Josef Bacik44396f42011-05-31 11:58:49 -04001094 */
Miklos Szeredibad61182012-03-26 12:54:24 +02001095static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
1096 struct nameidata *nd)
Josef Bacik44396f42011-05-31 11:58:49 -04001097{
Josef Bacik44396f42011-05-31 11:58:49 -04001098 struct dentry *old;
1099
1100 /* Don't create child dentry for a dead directory. */
Miklos Szeredibad61182012-03-26 12:54:24 +02001101 if (unlikely(IS_DEADDIR(dir))) {
Miklos Szeredie188dc02012-02-03 14:25:18 +01001102 dput(dentry);
Josef Bacik44396f42011-05-31 11:58:49 -04001103 return ERR_PTR(-ENOENT);
Miklos Szeredie188dc02012-02-03 14:25:18 +01001104 }
Josef Bacik44396f42011-05-31 11:58:49 -04001105
Miklos Szeredibad61182012-03-26 12:54:24 +02001106 old = dir->i_op->lookup(dir, dentry, nd);
Josef Bacik44396f42011-05-31 11:58:49 -04001107 if (unlikely(old)) {
1108 dput(dentry);
1109 dentry = old;
1110 }
1111 return dentry;
1112}
1113
Al Viroa3255542012-03-30 14:41:51 -04001114static struct dentry *__lookup_hash(struct qstr *name,
1115 struct dentry *base, struct nameidata *nd)
1116{
Miklos Szeredibad61182012-03-26 12:54:24 +02001117 bool need_lookup;
Al Viroa3255542012-03-30 14:41:51 -04001118 struct dentry *dentry;
1119
Miklos Szeredibad61182012-03-26 12:54:24 +02001120 dentry = lookup_dcache(name, base, nd, &need_lookup);
1121 if (!need_lookup)
1122 return dentry;
Al Viroa3255542012-03-30 14:41:51 -04001123
Miklos Szeredibad61182012-03-26 12:54:24 +02001124 return lookup_real(base->d_inode, dentry, nd);
Al Viroa3255542012-03-30 14:41:51 -04001125}
1126
Josef Bacik44396f42011-05-31 11:58:49 -04001127/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 * It's more convoluted than I'd like it to be, but... it's still fairly
1129 * small and for now I'd prefer to have fast path as straight as possible.
1130 * It _is_ time-critical.
1131 */
1132static int do_lookup(struct nameidata *nd, struct qstr *name,
Nick Piggin31e6b012011-01-07 17:49:52 +11001133 struct path *path, struct inode **inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
Jan Blunck4ac91372008-02-14 19:34:32 -08001135 struct vfsmount *mnt = nd->path.mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +11001136 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro5a18fff2011-03-11 04:44:53 -05001137 int need_reval = 1;
1138 int status = 1;
David Howells9875cf82011-01-14 18:45:21 +00001139 int err;
1140
Al Viro3cac2602009-08-13 18:27:43 +04001141 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001142 * Rename seqlock is not required here because in the off chance
1143 * of a false negative due to a concurrent rename, we're going to
1144 * do the non-racy lookup, below.
1145 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001146 if (nd->flags & LOOKUP_RCU) {
1147 unsigned seq;
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001148 dentry = __d_lookup_rcu(parent, name, &seq, nd->inode);
Al Viro5a18fff2011-03-11 04:44:53 -05001149 if (!dentry)
1150 goto unlazy;
1151
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001152 /*
1153 * This sequence count validates that the inode matches
1154 * the dentry name information from lookup.
1155 */
1156 *inode = dentry->d_inode;
1157 if (read_seqcount_retry(&dentry->d_seq, seq))
1158 return -ECHILD;
1159
1160 /*
1161 * This sequence count validates that the parent had no
1162 * changes while we did the lookup of the dentry above.
1163 *
1164 * The memory barrier in read_seqcount_begin of child is
1165 * enough, we can use __read_seqcount_retry here.
1166 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001167 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1168 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001169 nd->seq = seq;
Al Viro5a18fff2011-03-11 04:44:53 -05001170
Miklos Szeredifa4ee152012-03-26 12:54:19 +02001171 if (unlikely(d_need_lookup(dentry)))
1172 goto unlazy;
Al Viro24643082011-02-15 01:26:22 -05001173 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
Al Viro5a18fff2011-03-11 04:44:53 -05001174 status = d_revalidate(dentry, nd);
1175 if (unlikely(status <= 0)) {
1176 if (status != -ECHILD)
1177 need_reval = 0;
1178 goto unlazy;
1179 }
Al Viro24643082011-02-15 01:26:22 -05001180 }
Nick Piggin31e6b012011-01-07 17:49:52 +11001181 path->mnt = mnt;
1182 path->dentry = dentry;
Al Virod6e9bd22011-05-27 07:03:15 -04001183 if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1184 goto unlazy;
1185 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1186 goto unlazy;
1187 return 0;
Al Viro5a18fff2011-03-11 04:44:53 -05001188unlazy:
Al Viro19660af2011-03-25 10:32:48 -04001189 if (unlazy_walk(nd, dentry))
1190 return -ECHILD;
Al Viro5a18fff2011-03-11 04:44:53 -05001191 } else {
1192 dentry = __d_lookup(parent, name);
Nick Piggin31e6b012011-01-07 17:49:52 +11001193 }
Al Viro5a18fff2011-03-11 04:44:53 -05001194
Al Viro81e6f522012-03-30 14:48:04 -04001195 if (unlikely(!dentry))
1196 goto need_lookup;
Al Viro5a18fff2011-03-11 04:44:53 -05001197
Al Viro81e6f522012-03-30 14:48:04 -04001198 if (unlikely(d_need_lookup(dentry))) {
1199 dput(dentry);
1200 goto need_lookup;
Al Viro24643082011-02-15 01:26:22 -05001201 }
Al Viro81e6f522012-03-30 14:48:04 -04001202
Al Viro5a18fff2011-03-11 04:44:53 -05001203 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
1204 status = d_revalidate(dentry, nd);
1205 if (unlikely(status <= 0)) {
1206 if (status < 0) {
1207 dput(dentry);
1208 return status;
1209 }
1210 if (!d_invalidate(dentry)) {
1211 dput(dentry);
Al Viro81e6f522012-03-30 14:48:04 -04001212 goto need_lookup;
Al Viro5a18fff2011-03-11 04:44:53 -05001213 }
1214 }
Al Viro3f6c7c72012-03-30 14:04:16 -04001215done:
David Howells9875cf82011-01-14 18:45:21 +00001216 path->mnt = mnt;
1217 path->dentry = dentry;
1218 err = follow_managed(path, nd->flags);
Ian Kent89312212011-01-18 12:06:10 +08001219 if (unlikely(err < 0)) {
1220 path_put_conditional(path, nd);
David Howells9875cf82011-01-14 18:45:21 +00001221 return err;
Ian Kent89312212011-01-18 12:06:10 +08001222 }
Al Viroa3fbbde2011-11-07 21:21:26 +00001223 if (err)
1224 nd->flags |= LOOKUP_JUMPED;
David Howells9875cf82011-01-14 18:45:21 +00001225 *inode = path->dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 return 0;
Al Viro81e6f522012-03-30 14:48:04 -04001227
1228need_lookup:
1229 BUG_ON(nd->inode != parent->d_inode);
1230
1231 mutex_lock(&parent->d_inode->i_mutex);
1232 dentry = __lookup_hash(name, parent, nd);
1233 mutex_unlock(&parent->d_inode->i_mutex);
1234 if (IS_ERR(dentry))
1235 return PTR_ERR(dentry);
1236 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237}
1238
Al Viro52094c82011-02-21 21:34:47 -05001239static inline int may_lookup(struct nameidata *nd)
1240{
1241 if (nd->flags & LOOKUP_RCU) {
Al Viro4ad5abb2011-06-20 19:57:03 -04001242 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
Al Viro52094c82011-02-21 21:34:47 -05001243 if (err != -ECHILD)
1244 return err;
Al Viro19660af2011-03-25 10:32:48 -04001245 if (unlazy_walk(nd, NULL))
Al Viro52094c82011-02-21 21:34:47 -05001246 return -ECHILD;
1247 }
Al Viro4ad5abb2011-06-20 19:57:03 -04001248 return inode_permission(nd->inode, MAY_EXEC);
Al Viro52094c82011-02-21 21:34:47 -05001249}
1250
Al Viro9856fa12011-03-04 14:22:06 -05001251static inline int handle_dots(struct nameidata *nd, int type)
1252{
1253 if (type == LAST_DOTDOT) {
1254 if (nd->flags & LOOKUP_RCU) {
1255 if (follow_dotdot_rcu(nd))
1256 return -ECHILD;
1257 } else
1258 follow_dotdot(nd);
1259 }
1260 return 0;
1261}
1262
Al Viro951361f2011-03-04 14:44:37 -05001263static void terminate_walk(struct nameidata *nd)
1264{
1265 if (!(nd->flags & LOOKUP_RCU)) {
1266 path_put(&nd->path);
1267 } else {
1268 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001269 if (!(nd->flags & LOOKUP_ROOT))
1270 nd->root.mnt = NULL;
Al Viro951361f2011-03-04 14:44:37 -05001271 rcu_read_unlock();
1272 br_read_unlock(vfsmount_lock);
1273 }
1274}
1275
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001276/*
1277 * Do we need to follow links? We _really_ want to be able
1278 * to do this check without having to look at inode->i_op,
1279 * so we keep a cache of "no, this doesn't need follow_link"
1280 * for the common case.
1281 */
Linus Torvalds7813b942011-08-07 09:53:20 -07001282static inline int should_follow_link(struct inode *inode, int follow)
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001283{
1284 if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
1285 if (likely(inode->i_op->follow_link))
1286 return follow;
1287
1288 /* This gets set once for the inode lifetime */
1289 spin_lock(&inode->i_lock);
1290 inode->i_opflags |= IOP_NOFOLLOW;
1291 spin_unlock(&inode->i_lock);
1292 }
1293 return 0;
1294}
1295
Al Viroce57dfc2011-03-13 19:58:58 -04001296static inline int walk_component(struct nameidata *nd, struct path *path,
1297 struct qstr *name, int type, int follow)
1298{
1299 struct inode *inode;
1300 int err;
1301 /*
1302 * "." and ".." are special - ".." especially so because it has
1303 * to be able to know about the current root directory and
1304 * parent relationships.
1305 */
1306 if (unlikely(type != LAST_NORM))
1307 return handle_dots(nd, type);
1308 err = do_lookup(nd, name, path, &inode);
1309 if (unlikely(err)) {
1310 terminate_walk(nd);
1311 return err;
1312 }
1313 if (!inode) {
1314 path_to_nameidata(path, nd);
1315 terminate_walk(nd);
1316 return -ENOENT;
1317 }
Linus Torvalds7813b942011-08-07 09:53:20 -07001318 if (should_follow_link(inode, follow)) {
Al Viro19660af2011-03-25 10:32:48 -04001319 if (nd->flags & LOOKUP_RCU) {
1320 if (unlikely(unlazy_walk(nd, path->dentry))) {
1321 terminate_walk(nd);
1322 return -ECHILD;
1323 }
1324 }
Al Viroce57dfc2011-03-13 19:58:58 -04001325 BUG_ON(inode != path->dentry->d_inode);
1326 return 1;
1327 }
1328 path_to_nameidata(path, nd);
1329 nd->inode = inode;
1330 return 0;
1331}
1332
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333/*
Al Virob3563792011-03-14 21:54:55 -04001334 * This limits recursive symlink follows to 8, while
1335 * limiting consecutive symlinks to 40.
1336 *
1337 * Without that kind of total limit, nasty chains of consecutive
1338 * symlinks can cause almost arbitrarily long lookups.
1339 */
1340static inline int nested_symlink(struct path *path, struct nameidata *nd)
1341{
1342 int res;
1343
Al Virob3563792011-03-14 21:54:55 -04001344 if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1345 path_put_conditional(path, nd);
1346 path_put(&nd->path);
1347 return -ELOOP;
1348 }
Erez Zadok1a4022f2011-05-21 01:19:59 -04001349 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
Al Virob3563792011-03-14 21:54:55 -04001350
1351 nd->depth++;
1352 current->link_count++;
1353
1354 do {
1355 struct path link = *path;
1356 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04001357
1358 res = follow_link(&link, nd, &cookie);
Al Virob3563792011-03-14 21:54:55 -04001359 if (!res)
1360 res = walk_component(nd, path, &nd->last,
1361 nd->last_type, LOOKUP_FOLLOW);
Al Viro574197e2011-03-14 22:20:34 -04001362 put_link(nd, &link, cookie);
Al Virob3563792011-03-14 21:54:55 -04001363 } while (res > 0);
1364
1365 current->link_count--;
1366 nd->depth--;
1367 return res;
1368}
1369
1370/*
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001371 * We really don't want to look at inode->i_op->lookup
1372 * when we don't have to. So we keep a cache bit in
1373 * the inode ->i_opflags field that says "yes, we can
1374 * do lookup on this inode".
1375 */
1376static inline int can_lookup(struct inode *inode)
1377{
1378 if (likely(inode->i_opflags & IOP_LOOKUP))
1379 return 1;
1380 if (likely(!inode->i_op->lookup))
1381 return 0;
1382
1383 /* We do this once for the lifetime of the inode */
1384 spin_lock(&inode->i_lock);
1385 inode->i_opflags |= IOP_LOOKUP;
1386 spin_unlock(&inode->i_lock);
1387 return 1;
1388}
1389
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001390/*
1391 * We can do the critical dentry name comparison and hashing
1392 * operations one word at a time, but we are limited to:
1393 *
1394 * - Architectures with fast unaligned word accesses. We could
1395 * do a "get_unaligned()" if this helps and is sufficiently
1396 * fast.
1397 *
1398 * - Little-endian machines (so that we can generate the mask
1399 * of low bytes efficiently). Again, we *could* do a byte
1400 * swapping load on big-endian architectures if that is not
1401 * expensive enough to make the optimization worthless.
1402 *
1403 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1404 * do not trap on the (extremely unlikely) case of a page
1405 * crossing operation.
1406 *
1407 * - Furthermore, we need an efficient 64-bit compile for the
1408 * 64-bit case in order to generate the "number of bytes in
1409 * the final mask". Again, that could be replaced with a
1410 * efficient population count instruction or similar.
1411 */
1412#ifdef CONFIG_DCACHE_WORD_ACCESS
1413
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001414#include <asm/word-at-a-time.h>
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001415
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001416#ifdef CONFIG_64BIT
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001417
1418static inline unsigned int fold_hash(unsigned long hash)
1419{
1420 hash += hash >> (8*sizeof(int));
1421 return hash;
1422}
1423
1424#else /* 32-bit case */
1425
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001426#define fold_hash(x) (x)
1427
1428#endif
1429
1430unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1431{
1432 unsigned long a, mask;
1433 unsigned long hash = 0;
1434
1435 for (;;) {
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001436 a = load_unaligned_zeropad(name);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001437 if (len < sizeof(unsigned long))
1438 break;
1439 hash += a;
Al Virof132c5b2012-03-22 21:59:52 +00001440 hash *= 9;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001441 name += sizeof(unsigned long);
1442 len -= sizeof(unsigned long);
1443 if (!len)
1444 goto done;
1445 }
1446 mask = ~(~0ul << len*8);
1447 hash += mask & a;
1448done:
1449 return fold_hash(hash);
1450}
1451EXPORT_SYMBOL(full_name_hash);
1452
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001453/*
1454 * Calculate the length and hash of the path component, and
1455 * return the length of the component;
1456 */
1457static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1458{
1459 unsigned long a, mask, hash, len;
1460
1461 hash = a = 0;
1462 len = -sizeof(unsigned long);
1463 do {
1464 hash = (hash + a) * 9;
1465 len += sizeof(unsigned long);
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001466 a = load_unaligned_zeropad(name+len);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001467 /* Do we have any NUL or '/' bytes in this word? */
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001468 mask = has_zero(a) | has_zero(a ^ REPEAT_BYTE('/'));
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001469 } while (!mask);
1470
1471 /* The mask *below* the first high bit set */
1472 mask = (mask - 1) & ~mask;
1473 mask >>= 7;
1474 hash += a & mask;
1475 *hashp = fold_hash(hash);
1476
1477 return len + count_masked_bytes(mask);
1478}
1479
1480#else
1481
Linus Torvalds0145acc2012-03-02 14:32:59 -08001482unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1483{
1484 unsigned long hash = init_name_hash();
1485 while (len--)
1486 hash = partial_name_hash(*name++, hash);
1487 return end_name_hash(hash);
1488}
Linus Torvaldsae942ae2012-03-02 19:40:57 -08001489EXPORT_SYMBOL(full_name_hash);
Linus Torvalds0145acc2012-03-02 14:32:59 -08001490
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001491/*
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001492 * We know there's a real path component here of at least
1493 * one character.
1494 */
1495static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1496{
1497 unsigned long hash = init_name_hash();
1498 unsigned long len = 0, c;
1499
1500 c = (unsigned char)*name;
1501 do {
1502 len++;
1503 hash = partial_name_hash(c, hash);
1504 c = (unsigned char)name[len];
1505 } while (c && c != '/');
1506 *hashp = end_name_hash(hash);
1507 return len;
1508}
1509
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001510#endif
1511
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001512/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01001514 * This is the basic name resolution function, turning a pathname into
1515 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01001517 * Returns 0 and nd will have valid dentry and mnt on success.
1518 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 */
Al Viro6de88d72009-08-09 01:41:57 +04001520static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
1522 struct path next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
1525 while (*name=='/')
1526 name++;
1527 if (!*name)
Al Viro086e1832011-02-22 20:56:27 -05001528 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 /* At this point we know we have a real path component. */
1531 for(;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 struct qstr this;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001533 long len;
Al Virofe479a52011-02-22 15:10:03 -05001534 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
Al Viro52094c82011-02-21 21:34:47 -05001536 err = may_lookup(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 if (err)
1538 break;
1539
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001540 len = hash_name(name, &this.hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 this.name = name;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001542 this.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Al Virofe479a52011-02-22 15:10:03 -05001544 type = LAST_NORM;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001545 if (name[0] == '.') switch (len) {
Al Virofe479a52011-02-22 15:10:03 -05001546 case 2:
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001547 if (name[1] == '.') {
Al Virofe479a52011-02-22 15:10:03 -05001548 type = LAST_DOTDOT;
Al Viro16c2cd72011-02-22 15:50:10 -05001549 nd->flags |= LOOKUP_JUMPED;
1550 }
Al Virofe479a52011-02-22 15:10:03 -05001551 break;
1552 case 1:
1553 type = LAST_DOT;
1554 }
Al Viro5a202bc2011-03-08 14:17:44 -05001555 if (likely(type == LAST_NORM)) {
1556 struct dentry *parent = nd->path.dentry;
Al Viro16c2cd72011-02-22 15:50:10 -05001557 nd->flags &= ~LOOKUP_JUMPED;
Al Viro5a202bc2011-03-08 14:17:44 -05001558 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1559 err = parent->d_op->d_hash(parent, nd->inode,
1560 &this);
1561 if (err < 0)
1562 break;
1563 }
1564 }
Al Virofe479a52011-02-22 15:10:03 -05001565
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001566 if (!name[len])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 goto last_component;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001568 /*
1569 * If it wasn't NUL, we know it was '/'. Skip that
1570 * slash, and continue until no more slashes.
1571 */
1572 do {
1573 len++;
1574 } while (unlikely(name[len] == '/'));
1575 if (!name[len])
Al Virob3563792011-03-14 21:54:55 -04001576 goto last_component;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001577 name += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Al Viroce57dfc2011-03-13 19:58:58 -04001579 err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1580 if (err < 0)
1581 return err;
Al Virofe479a52011-02-22 15:10:03 -05001582
Al Viroce57dfc2011-03-13 19:58:58 -04001583 if (err) {
Al Virob3563792011-03-14 21:54:55 -04001584 err = nested_symlink(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 if (err)
Al Viroa7472ba2011-03-04 14:39:30 -05001586 return err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001587 }
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001588 if (can_lookup(nd->inode))
1589 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 err = -ENOTDIR;
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001591 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 /* here ends the main loop */
1593
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594last_component:
Al Virob3563792011-03-14 21:54:55 -04001595 nd->last = this;
1596 nd->last_type = type;
Al Viro086e1832011-02-22 20:56:27 -05001597 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 }
Al Viro951361f2011-03-04 14:44:37 -05001599 terminate_walk(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 return err;
1601}
1602
Al Viro70e9b352011-03-05 21:12:22 -05001603static int path_init(int dfd, const char *name, unsigned int flags,
1604 struct nameidata *nd, struct file **fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605{
Prasanna Medaea3834d2005-04-29 16:00:17 +01001606 int retval = 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001607 int fput_needed;
1608 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
1610 nd->last_type = LAST_ROOT; /* if there are only slashes... */
Al Viro16c2cd72011-02-22 15:50:10 -05001611 nd->flags = flags | LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 nd->depth = 0;
Al Viro5b6ca022011-03-09 23:04:47 -05001613 if (flags & LOOKUP_ROOT) {
1614 struct inode *inode = nd->root.dentry->d_inode;
Al Viro73d049a2011-03-11 12:08:24 -05001615 if (*name) {
1616 if (!inode->i_op->lookup)
1617 return -ENOTDIR;
1618 retval = inode_permission(inode, MAY_EXEC);
1619 if (retval)
1620 return retval;
1621 }
Al Viro5b6ca022011-03-09 23:04:47 -05001622 nd->path = nd->root;
1623 nd->inode = inode;
1624 if (flags & LOOKUP_RCU) {
1625 br_read_lock(vfsmount_lock);
1626 rcu_read_lock();
1627 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1628 } else {
1629 path_get(&nd->path);
1630 }
1631 return 0;
1632 }
1633
Al Viro2a737872009-04-07 11:49:53 -04001634 nd->root.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 if (*name=='/') {
Al Viroe41f7d42011-02-22 14:02:58 -05001637 if (flags & LOOKUP_RCU) {
1638 br_read_lock(vfsmount_lock);
1639 rcu_read_lock();
1640 set_root_rcu(nd);
1641 } else {
1642 set_root(nd);
1643 path_get(&nd->root);
1644 }
Al Viro2a737872009-04-07 11:49:53 -04001645 nd->path = nd->root;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001646 } else if (dfd == AT_FDCWD) {
Al Viroe41f7d42011-02-22 14:02:58 -05001647 if (flags & LOOKUP_RCU) {
1648 struct fs_struct *fs = current->fs;
1649 unsigned seq;
1650
1651 br_read_lock(vfsmount_lock);
1652 rcu_read_lock();
1653
1654 do {
1655 seq = read_seqcount_begin(&fs->seq);
1656 nd->path = fs->pwd;
1657 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1658 } while (read_seqcount_retry(&fs->seq, seq));
1659 } else {
1660 get_fs_pwd(current->fs, &nd->path);
1661 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001662 } else {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001663 struct dentry *dentry;
1664
Al Viro1abf0c72011-03-13 03:51:11 -04001665 file = fget_raw_light(dfd, &fput_needed);
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001666 retval = -EBADF;
1667 if (!file)
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001668 goto out_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001669
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001670 dentry = file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001671
Al Virof52e0c12011-03-14 18:56:51 -04001672 if (*name) {
1673 retval = -ENOTDIR;
1674 if (!S_ISDIR(dentry->d_inode->i_mode))
1675 goto fput_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001676
Al Viro4ad5abb2011-06-20 19:57:03 -04001677 retval = inode_permission(dentry->d_inode, MAY_EXEC);
Al Virof52e0c12011-03-14 18:56:51 -04001678 if (retval)
1679 goto fput_fail;
1680 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001681
Jan Blunck5dd784d2008-02-14 19:34:38 -08001682 nd->path = file->f_path;
Al Viroe41f7d42011-02-22 14:02:58 -05001683 if (flags & LOOKUP_RCU) {
1684 if (fput_needed)
Al Viro70e9b352011-03-05 21:12:22 -05001685 *fp = file;
Al Viroe41f7d42011-02-22 14:02:58 -05001686 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1687 br_read_lock(vfsmount_lock);
1688 rcu_read_lock();
1689 } else {
1690 path_get(&file->f_path);
1691 fput_light(file, fput_needed);
1692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 }
Al Viroe41f7d42011-02-22 14:02:58 -05001694
Nick Piggin31e6b012011-01-07 17:49:52 +11001695 nd->inode = nd->path.dentry->d_inode;
Al Viro9b4a9b12009-04-07 11:44:16 -04001696 return 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001697
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001698fput_fail:
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001699 fput_light(file, fput_needed);
Al Viro9b4a9b12009-04-07 11:44:16 -04001700out_fail:
1701 return retval;
1702}
1703
Al Virobd92d7f2011-03-14 19:54:59 -04001704static inline int lookup_last(struct nameidata *nd, struct path *path)
1705{
1706 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1707 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1708
1709 nd->flags &= ~LOOKUP_PARENT;
1710 return walk_component(nd, path, &nd->last, nd->last_type,
1711 nd->flags & LOOKUP_FOLLOW);
1712}
1713
Al Viro9b4a9b12009-04-07 11:44:16 -04001714/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroee0827c2011-02-21 23:38:09 -05001715static int path_lookupat(int dfd, const char *name,
Al Viro9b4a9b12009-04-07 11:44:16 -04001716 unsigned int flags, struct nameidata *nd)
1717{
Al Viro70e9b352011-03-05 21:12:22 -05001718 struct file *base = NULL;
Al Virobd92d7f2011-03-14 19:54:59 -04001719 struct path path;
1720 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001721
1722 /*
1723 * Path walking is largely split up into 2 different synchronisation
1724 * schemes, rcu-walk and ref-walk (explained in
1725 * Documentation/filesystems/path-lookup.txt). These share much of the
1726 * path walk code, but some things particularly setup, cleanup, and
1727 * following mounts are sufficiently divergent that functions are
1728 * duplicated. Typically there is a function foo(), and its RCU
1729 * analogue, foo_rcu().
1730 *
1731 * -ECHILD is the error number of choice (just to avoid clashes) that
1732 * is returned if some aspect of an rcu-walk fails. Such an error must
1733 * be handled by restarting a traditional ref-walk (which will always
1734 * be able to complete).
1735 */
Al Virobd92d7f2011-03-14 19:54:59 -04001736 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
Al Viroee0827c2011-02-21 23:38:09 -05001737
Al Virobd92d7f2011-03-14 19:54:59 -04001738 if (unlikely(err))
1739 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001740
1741 current->total_link_count = 0;
Al Virobd92d7f2011-03-14 19:54:59 -04001742 err = link_path_walk(name, nd);
1743
1744 if (!err && !(flags & LOOKUP_PARENT)) {
Al Virobd92d7f2011-03-14 19:54:59 -04001745 err = lookup_last(nd, &path);
1746 while (err > 0) {
1747 void *cookie;
1748 struct path link = path;
Al Virobd92d7f2011-03-14 19:54:59 -04001749 nd->flags |= LOOKUP_PARENT;
Al Viro574197e2011-03-14 22:20:34 -04001750 err = follow_link(&link, nd, &cookie);
Al Virobd92d7f2011-03-14 19:54:59 -04001751 if (!err)
1752 err = lookup_last(nd, &path);
Al Viro574197e2011-03-14 22:20:34 -04001753 put_link(nd, &link, cookie);
Al Virobd92d7f2011-03-14 19:54:59 -04001754 }
1755 }
Al Viroee0827c2011-02-21 23:38:09 -05001756
Al Viro9f1fafe2011-03-25 11:00:12 -04001757 if (!err)
1758 err = complete_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04001759
1760 if (!err && nd->flags & LOOKUP_DIRECTORY) {
1761 if (!nd->inode->i_op->lookup) {
1762 path_put(&nd->path);
Al Virobd23a532011-03-23 09:56:30 -04001763 err = -ENOTDIR;
Al Virobd92d7f2011-03-14 19:54:59 -04001764 }
1765 }
Al Viro16c2cd72011-02-22 15:50:10 -05001766
Al Viro70e9b352011-03-05 21:12:22 -05001767 if (base)
1768 fput(base);
Al Viroee0827c2011-02-21 23:38:09 -05001769
Al Viro5b6ca022011-03-09 23:04:47 -05001770 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
Al Viro2a737872009-04-07 11:49:53 -04001771 path_put(&nd->root);
1772 nd->root.mnt = NULL;
1773 }
Al Virobd92d7f2011-03-14 19:54:59 -04001774 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001775}
Nick Piggin31e6b012011-01-07 17:49:52 +11001776
Al Viroee0827c2011-02-21 23:38:09 -05001777static int do_path_lookup(int dfd, const char *name,
1778 unsigned int flags, struct nameidata *nd)
1779{
1780 int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1781 if (unlikely(retval == -ECHILD))
1782 retval = path_lookupat(dfd, name, flags, nd);
1783 if (unlikely(retval == -ESTALE))
1784 retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11001785
1786 if (likely(!retval)) {
1787 if (unlikely(!audit_dummy_context())) {
1788 if (nd->path.dentry && nd->inode)
1789 audit_inode(name, nd->path.dentry);
1790 }
1791 }
Al Viro9b4a9b12009-04-07 11:44:16 -04001792 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793}
1794
Al Viroc9c6cac2011-02-16 15:15:47 -05001795int kern_path_parent(const char *name, struct nameidata *nd)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001796{
Al Viroc9c6cac2011-02-16 15:15:47 -05001797 return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001798}
1799
Al Virod1811462008-08-02 00:49:18 -04001800int kern_path(const char *name, unsigned int flags, struct path *path)
1801{
1802 struct nameidata nd;
1803 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1804 if (!res)
1805 *path = nd.path;
1806 return res;
1807}
1808
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001809/**
1810 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1811 * @dentry: pointer to dentry of the base directory
1812 * @mnt: pointer to vfs mount of the base directory
1813 * @name: pointer to file name
1814 * @flags: lookup flags
Al Viroe0a01242011-06-27 17:00:37 -04001815 * @path: pointer to struct path to fill
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001816 */
1817int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
1818 const char *name, unsigned int flags,
Al Viroe0a01242011-06-27 17:00:37 -04001819 struct path *path)
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001820{
Al Viroe0a01242011-06-27 17:00:37 -04001821 struct nameidata nd;
1822 int err;
1823 nd.root.dentry = dentry;
1824 nd.root.mnt = mnt;
1825 BUG_ON(flags & LOOKUP_PARENT);
Al Viro5b6ca022011-03-09 23:04:47 -05001826 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
Al Viroe0a01242011-06-27 17:00:37 -04001827 err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
1828 if (!err)
1829 *path = nd.path;
1830 return err;
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001831}
1832
James Morris057f6c02007-04-26 00:12:05 -07001833/*
1834 * Restricted form of lookup. Doesn't follow links, single-component only,
1835 * needs parent already locked. Doesn't follow mounts.
1836 * SMP-safe.
1837 */
Adrian Bunka244e162006-03-31 02:32:11 -08001838static struct dentry *lookup_hash(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839{
Jan Blunck4ac91372008-02-14 19:34:32 -08001840 return __lookup_hash(&nd->last, nd->path.dentry, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841}
1842
Christoph Hellwigeead1912007-10-16 23:25:38 -07001843/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07001844 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07001845 * @name: pathname component to lookup
1846 * @base: base directory to lookup from
1847 * @len: maximum length @len should be interpreted to
1848 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07001849 * Note that this routine is purely a helper for filesystem usage and should
1850 * not be called by generic code. Also note that by using this function the
Christoph Hellwigeead1912007-10-16 23:25:38 -07001851 * nameidata argument is passed to the filesystem methods and a filesystem
1852 * using this helper needs to be prepared for that.
1853 */
James Morris057f6c02007-04-26 00:12:05 -07001854struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1855{
James Morris057f6c02007-04-26 00:12:05 -07001856 struct qstr this;
Al Viro6a96ba52011-03-07 23:49:20 -05001857 unsigned int c;
Miklos Szeredicda309d2012-03-26 12:54:21 +02001858 int err;
James Morris057f6c02007-04-26 00:12:05 -07001859
David Woodhouse2f9092e2009-04-20 23:18:37 +01001860 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
1861
Al Viro6a96ba52011-03-07 23:49:20 -05001862 this.name = name;
1863 this.len = len;
Linus Torvalds0145acc2012-03-02 14:32:59 -08001864 this.hash = full_name_hash(name, len);
Al Viro6a96ba52011-03-07 23:49:20 -05001865 if (!len)
1866 return ERR_PTR(-EACCES);
1867
Al Viro6a96ba52011-03-07 23:49:20 -05001868 while (len--) {
1869 c = *(const unsigned char *)name++;
1870 if (c == '/' || c == '\0')
1871 return ERR_PTR(-EACCES);
Al Viro6a96ba52011-03-07 23:49:20 -05001872 }
Al Viro5a202bc2011-03-08 14:17:44 -05001873 /*
1874 * See if the low-level filesystem might want
1875 * to use its own hash..
1876 */
1877 if (base->d_flags & DCACHE_OP_HASH) {
1878 int err = base->d_op->d_hash(base, base->d_inode, &this);
1879 if (err < 0)
1880 return ERR_PTR(err);
1881 }
Christoph Hellwigeead1912007-10-16 23:25:38 -07001882
Miklos Szeredicda309d2012-03-26 12:54:21 +02001883 err = inode_permission(base->d_inode, MAY_EXEC);
1884 if (err)
1885 return ERR_PTR(err);
1886
Christoph Hellwig49705b72005-11-08 21:35:06 -08001887 return __lookup_hash(&this, base, NULL);
James Morris057f6c02007-04-26 00:12:05 -07001888}
1889
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01001890int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
1891 struct path *path, int *empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892{
Al Viro2d8f3032008-07-22 09:59:21 -04001893 struct nameidata nd;
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01001894 char *tmp = getname_flags(name, flags, empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 int err = PTR_ERR(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 if (!IS_ERR(tmp)) {
Al Viro2d8f3032008-07-22 09:59:21 -04001897
1898 BUG_ON(flags & LOOKUP_PARENT);
1899
1900 err = do_path_lookup(dfd, tmp, flags, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 putname(tmp);
Al Viro2d8f3032008-07-22 09:59:21 -04001902 if (!err)
1903 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 }
1905 return err;
1906}
1907
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01001908int user_path_at(int dfd, const char __user *name, unsigned flags,
1909 struct path *path)
1910{
Linus Torvaldsf7493e52012-03-22 16:10:40 -07001911 return user_path_at_empty(dfd, name, flags, path, NULL);
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01001912}
1913
Al Viro2ad94ae2008-07-21 09:32:51 -04001914static int user_path_parent(int dfd, const char __user *path,
1915 struct nameidata *nd, char **name)
1916{
1917 char *s = getname(path);
1918 int error;
1919
1920 if (IS_ERR(s))
1921 return PTR_ERR(s);
1922
1923 error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
1924 if (error)
1925 putname(s);
1926 else
1927 *name = s;
1928
1929 return error;
1930}
1931
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932/*
1933 * It's inline, so penalty for filesystems that don't use sticky bit is
1934 * minimal.
1935 */
1936static inline int check_sticky(struct inode *dir, struct inode *inode)
1937{
David Howellsda9592e2008-11-14 10:39:05 +11001938 uid_t fsuid = current_fsuid();
1939
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 if (!(dir->i_mode & S_ISVTX))
1941 return 0;
Serge E. Hallyne795b712011-03-23 16:43:25 -07001942 if (current_user_ns() != inode_userns(inode))
1943 goto other_userns;
David Howellsda9592e2008-11-14 10:39:05 +11001944 if (inode->i_uid == fsuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 return 0;
David Howellsda9592e2008-11-14 10:39:05 +11001946 if (dir->i_uid == fsuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 return 0;
Serge E. Hallyne795b712011-03-23 16:43:25 -07001948
1949other_userns:
1950 return !ns_capable(inode_userns(inode), CAP_FOWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951}
1952
1953/*
1954 * Check whether we can remove a link victim from directory dir, check
1955 * whether the type of victim is right.
1956 * 1. We can't do it if dir is read-only (done in permission())
1957 * 2. We should have write and exec permissions on dir
1958 * 3. We can't remove anything from append-only dir
1959 * 4. We can't do anything with immutable dir (done in permission())
1960 * 5. If the sticky bit on dir is set we should either
1961 * a. be owner of dir, or
1962 * b. be owner of victim, or
1963 * c. have CAP_FOWNER capability
1964 * 6. If the victim is append-only or immutable we can't do antyhing with
1965 * links pointing to it.
1966 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1967 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1968 * 9. We can't remove a root or mountpoint.
1969 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1970 * nfs_async_unlink().
1971 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001972static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973{
1974 int error;
1975
1976 if (!victim->d_inode)
1977 return -ENOENT;
1978
1979 BUG_ON(victim->d_parent->d_inode != dir);
Al Virocccc6bb2009-12-25 05:07:33 -05001980 audit_inode_child(victim, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Al Virof419a2e2008-07-22 00:07:17 -04001982 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 if (error)
1984 return error;
1985 if (IS_APPEND(dir))
1986 return -EPERM;
1987 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
Hugh Dickinsf9454542008-11-19 15:36:38 -08001988 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 return -EPERM;
1990 if (isdir) {
1991 if (!S_ISDIR(victim->d_inode->i_mode))
1992 return -ENOTDIR;
1993 if (IS_ROOT(victim))
1994 return -EBUSY;
1995 } else if (S_ISDIR(victim->d_inode->i_mode))
1996 return -EISDIR;
1997 if (IS_DEADDIR(dir))
1998 return -ENOENT;
1999 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2000 return -EBUSY;
2001 return 0;
2002}
2003
2004/* Check whether we can create an object with dentry child in directory
2005 * dir.
2006 * 1. We can't do it if child already exists (open has special treatment for
2007 * this case, but since we are inlined it's OK)
2008 * 2. We can't do it if dir is read-only (done in permission())
2009 * 3. We should have write and exec permissions on dir
2010 * 4. We can't do it if dir is immutable (done in permission())
2011 */
Miklos Szeredia95164d2008-07-30 15:08:48 +02002012static inline int may_create(struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013{
2014 if (child->d_inode)
2015 return -EEXIST;
2016 if (IS_DEADDIR(dir))
2017 return -ENOENT;
Al Virof419a2e2008-07-22 00:07:17 -04002018 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019}
2020
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021/*
2022 * p1 and p2 should be directories on the same fs.
2023 */
2024struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2025{
2026 struct dentry *p;
2027
2028 if (p1 == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07002029 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 return NULL;
2031 }
2032
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002033 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002035 p = d_ancestor(p2, p1);
2036 if (p) {
2037 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2038 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
2039 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 }
2041
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002042 p = d_ancestor(p1, p2);
2043 if (p) {
2044 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2045 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2046 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 }
2048
Ingo Molnarf2eace22006-07-03 00:25:05 -07002049 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2050 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 return NULL;
2052}
2053
2054void unlock_rename(struct dentry *p1, struct dentry *p2)
2055{
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002056 mutex_unlock(&p1->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 if (p1 != p2) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002058 mutex_unlock(&p2->d_inode->i_mutex);
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002059 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 }
2061}
2062
Al Viro4acdaf22011-07-26 01:42:34 -04002063int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 struct nameidata *nd)
2065{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002066 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
2068 if (error)
2069 return error;
2070
Al Viroacfa4382008-12-04 10:06:33 -05002071 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 return -EACCES; /* shouldn't it be ENOSYS? */
2073 mode &= S_IALLUGO;
2074 mode |= S_IFREG;
2075 error = security_inode_create(dir, dentry, mode);
2076 if (error)
2077 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 error = dir->i_op->create(dir, dentry, mode, nd);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002079 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002080 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 return error;
2082}
2083
Al Viro73d049a2011-03-11 12:08:24 -05002084static int may_open(struct path *path, int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002086 struct dentry *dentry = path->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 struct inode *inode = dentry->d_inode;
2088 int error;
2089
Al Virobcda7652011-03-13 16:42:14 -04002090 /* O_PATH? */
2091 if (!acc_mode)
2092 return 0;
2093
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 if (!inode)
2095 return -ENOENT;
2096
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002097 switch (inode->i_mode & S_IFMT) {
2098 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002100 case S_IFDIR:
2101 if (acc_mode & MAY_WRITE)
2102 return -EISDIR;
2103 break;
2104 case S_IFBLK:
2105 case S_IFCHR:
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002106 if (path->mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002108 /*FALLTHRU*/
2109 case S_IFIFO:
2110 case S_IFSOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002112 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08002113 }
Dave Hansenb41572e2007-10-16 23:31:14 -07002114
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002115 error = inode_permission(inode, acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07002116 if (error)
2117 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05002118
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 /*
2120 * An append-only file must be opened in append mode for writing.
2121 */
2122 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05002123 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05002124 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05002126 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 }
2128
2129 /* O_NOATIME can only be set by the owner or superuser */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07002130 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
Al Viro7715b522009-12-16 03:54:00 -05002131 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
J. Bruce Fieldsf3c7691e2011-09-21 10:58:13 -04002133 return 0;
Al Viro7715b522009-12-16 03:54:00 -05002134}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
Jeff Laytone1181ee2010-12-07 16:19:50 -05002136static int handle_truncate(struct file *filp)
Al Viro7715b522009-12-16 03:54:00 -05002137{
Jeff Laytone1181ee2010-12-07 16:19:50 -05002138 struct path *path = &filp->f_path;
Al Viro7715b522009-12-16 03:54:00 -05002139 struct inode *inode = path->dentry->d_inode;
2140 int error = get_write_access(inode);
2141 if (error)
2142 return error;
2143 /*
2144 * Refuse to truncate files with mandatory locks held on them.
2145 */
2146 error = locks_verify_locked(inode);
2147 if (!error)
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +09002148 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05002149 if (!error) {
2150 error = do_truncate(path->dentry, 0,
2151 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
Jeff Laytone1181ee2010-12-07 16:19:50 -05002152 filp);
Al Viro7715b522009-12-16 03:54:00 -05002153 }
2154 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04002155 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156}
2157
Dave Hansend57999e2008-02-15 14:37:27 -08002158static inline int open_to_namei_flags(int flag)
2159{
Al Viro8a5e9292011-06-25 19:15:54 -04002160 if ((flag & O_ACCMODE) == 3)
2161 flag--;
Dave Hansend57999e2008-02-15 14:37:27 -08002162 return flag;
2163}
2164
Nick Piggin31e6b012011-01-07 17:49:52 +11002165/*
Al Virofe2d35f2011-03-05 22:58:25 -05002166 * Handle the last step of open()
Nick Piggin31e6b012011-01-07 17:49:52 +11002167 */
Al Virofb1cc552009-12-24 01:58:28 -05002168static struct file *do_last(struct nameidata *nd, struct path *path,
Al Viroc3e380b2011-02-23 13:39:45 -05002169 const struct open_flags *op, const char *pathname)
Al Virofb1cc552009-12-24 01:58:28 -05002170{
Al Viroa1e28032009-12-24 02:12:06 -05002171 struct dentry *dir = nd->path.dentry;
Al Viro6c0d46c2011-03-09 00:59:59 -05002172 struct dentry *dentry;
Al Viroca344a892011-03-09 00:36:45 -05002173 int open_flag = op->open_flag;
Al Viro6c0d46c2011-03-09 00:59:59 -05002174 int will_truncate = open_flag & O_TRUNC;
Al Viroca344a892011-03-09 00:36:45 -05002175 int want_write = 0;
Al Virobcda7652011-03-13 16:42:14 -04002176 int acc_mode = op->acc_mode;
Al Virofb1cc552009-12-24 01:58:28 -05002177 struct file *filp;
Al Viro16c2cd72011-02-22 15:50:10 -05002178 int error;
Al Virofb1cc552009-12-24 01:58:28 -05002179
Al Viroc3e380b2011-02-23 13:39:45 -05002180 nd->flags &= ~LOOKUP_PARENT;
2181 nd->flags |= op->intent;
2182
Al Viro1f36f772009-12-26 10:56:19 -05002183 switch (nd->last_type) {
2184 case LAST_DOTDOT:
Neil Brown176306f2010-05-24 16:57:56 +10002185 case LAST_DOT:
Al Virofe2d35f2011-03-05 22:58:25 -05002186 error = handle_dots(nd, nd->last_type);
2187 if (error)
2188 return ERR_PTR(error);
Al Viro1f36f772009-12-26 10:56:19 -05002189 /* fallthrough */
Al Viro1f36f772009-12-26 10:56:19 -05002190 case LAST_ROOT:
Al Viro9f1fafe2011-03-25 11:00:12 -04002191 error = complete_walk(nd);
Al Viro16c2cd72011-02-22 15:50:10 -05002192 if (error)
Al Viro9f1fafe2011-03-25 11:00:12 -04002193 return ERR_PTR(error);
Al Virofe2d35f2011-03-05 22:58:25 -05002194 audit_inode(pathname, nd->path.dentry);
Al Viroca344a892011-03-09 00:36:45 -05002195 if (open_flag & O_CREAT) {
Al Virofe2d35f2011-03-05 22:58:25 -05002196 error = -EISDIR;
2197 goto exit;
2198 }
2199 goto ok;
Al Viro1f36f772009-12-26 10:56:19 -05002200 case LAST_BIND:
Al Viro9f1fafe2011-03-25 11:00:12 -04002201 error = complete_walk(nd);
Al Viro16c2cd72011-02-22 15:50:10 -05002202 if (error)
Al Viro9f1fafe2011-03-25 11:00:12 -04002203 return ERR_PTR(error);
Al Viro1f36f772009-12-26 10:56:19 -05002204 audit_inode(pathname, dir);
Al Viro67ee3ad2009-12-26 07:01:01 -05002205 goto ok;
Al Viro1f36f772009-12-26 10:56:19 -05002206 }
Al Viro67ee3ad2009-12-26 07:01:01 -05002207
Al Viroca344a892011-03-09 00:36:45 -05002208 if (!(open_flag & O_CREAT)) {
Al Virobcda7652011-03-13 16:42:14 -04002209 int symlink_ok = 0;
Al Virofe2d35f2011-03-05 22:58:25 -05002210 if (nd->last.name[nd->last.len])
2211 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
Al Virobcda7652011-03-13 16:42:14 -04002212 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2213 symlink_ok = 1;
Al Virofe2d35f2011-03-05 22:58:25 -05002214 /* we _can_ be in RCU mode here */
Al Viroce57dfc2011-03-13 19:58:58 -04002215 error = walk_component(nd, path, &nd->last, LAST_NORM,
2216 !symlink_ok);
2217 if (error < 0)
Al Virofe2d35f2011-03-05 22:58:25 -05002218 return ERR_PTR(error);
Al Viroce57dfc2011-03-13 19:58:58 -04002219 if (error) /* symlink */
Al Virofe2d35f2011-03-05 22:58:25 -05002220 return NULL;
Al Virofe2d35f2011-03-05 22:58:25 -05002221 /* sayonara */
Al Viro9f1fafe2011-03-25 11:00:12 -04002222 error = complete_walk(nd);
2223 if (error)
Miklos Szeredi7f6c7e62012-03-06 13:56:34 +01002224 return ERR_PTR(error);
Al Virofe2d35f2011-03-05 22:58:25 -05002225
2226 error = -ENOTDIR;
2227 if (nd->flags & LOOKUP_DIRECTORY) {
Al Viroce57dfc2011-03-13 19:58:58 -04002228 if (!nd->inode->i_op->lookup)
Al Virofe2d35f2011-03-05 22:58:25 -05002229 goto exit;
2230 }
2231 audit_inode(pathname, nd->path.dentry);
2232 goto ok;
2233 }
2234
2235 /* create side of things */
Al Viroa3fbbde2011-11-07 21:21:26 +00002236 /*
2237 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED has been
2238 * cleared when we got to the last component we are about to look up
2239 */
Al Viro9f1fafe2011-03-25 11:00:12 -04002240 error = complete_walk(nd);
2241 if (error)
2242 return ERR_PTR(error);
Al Virofe2d35f2011-03-05 22:58:25 -05002243
2244 audit_inode(pathname, dir);
Al Viro16c2cd72011-02-22 15:50:10 -05002245 error = -EISDIR;
Al Viro1f36f772009-12-26 10:56:19 -05002246 /* trailing slashes? */
Nick Piggin31e6b012011-01-07 17:49:52 +11002247 if (nd->last.name[nd->last.len])
2248 goto exit;
Al Viroa2c36b42009-12-24 03:39:50 -05002249
Al Viroa1e28032009-12-24 02:12:06 -05002250 mutex_lock(&dir->d_inode->i_mutex);
2251
Al Viro6c0d46c2011-03-09 00:59:59 -05002252 dentry = lookup_hash(nd);
2253 error = PTR_ERR(dentry);
2254 if (IS_ERR(dentry)) {
Al Virofb1cc552009-12-24 01:58:28 -05002255 mutex_unlock(&dir->d_inode->i_mutex);
2256 goto exit;
2257 }
2258
Al Viro6c0d46c2011-03-09 00:59:59 -05002259 path->dentry = dentry;
2260 path->mnt = nd->path.mnt;
2261
Al Virofb1cc552009-12-24 01:58:28 -05002262 /* Negative dentry, just create the file */
Al Viro6c0d46c2011-03-09 00:59:59 -05002263 if (!dentry->d_inode) {
Al Viroa218d0f2011-11-21 14:59:34 -05002264 umode_t mode = op->mode;
Al Viro6c0d46c2011-03-09 00:59:59 -05002265 if (!IS_POSIXACL(dir->d_inode))
2266 mode &= ~current_umask();
Al Virofb1cc552009-12-24 01:58:28 -05002267 /*
2268 * This write is needed to ensure that a
Al Viro6c0d46c2011-03-09 00:59:59 -05002269 * rw->ro transition does not occur between
Al Virofb1cc552009-12-24 01:58:28 -05002270 * the time when the file is created and when
2271 * a permanent write count is taken through
2272 * the 'struct file' in nameidata_to_filp().
2273 */
2274 error = mnt_want_write(nd->path.mnt);
2275 if (error)
2276 goto exit_mutex_unlock;
Al Viroca344a892011-03-09 00:36:45 -05002277 want_write = 1;
Al Viro9b44f1b2011-03-09 00:17:27 -05002278 /* Don't check for write permission, don't truncate */
Al Viroca344a892011-03-09 00:36:45 -05002279 open_flag &= ~O_TRUNC;
Al Viro6c0d46c2011-03-09 00:59:59 -05002280 will_truncate = 0;
Al Virobcda7652011-03-13 16:42:14 -04002281 acc_mode = MAY_OPEN;
Al Viro6c0d46c2011-03-09 00:59:59 -05002282 error = security_path_mknod(&nd->path, dentry, mode, 0);
2283 if (error)
2284 goto exit_mutex_unlock;
2285 error = vfs_create(dir->d_inode, dentry, mode, nd);
2286 if (error)
2287 goto exit_mutex_unlock;
2288 mutex_unlock(&dir->d_inode->i_mutex);
2289 dput(nd->path.dentry);
2290 nd->path.dentry = dentry;
Al Viroca344a892011-03-09 00:36:45 -05002291 goto common;
Al Virofb1cc552009-12-24 01:58:28 -05002292 }
2293
2294 /*
2295 * It already exists.
2296 */
2297 mutex_unlock(&dir->d_inode->i_mutex);
2298 audit_inode(pathname, path->dentry);
2299
2300 error = -EEXIST;
Al Viroca344a892011-03-09 00:36:45 -05002301 if (open_flag & O_EXCL)
Al Virofb1cc552009-12-24 01:58:28 -05002302 goto exit_dput;
2303
David Howells9875cf82011-01-14 18:45:21 +00002304 error = follow_managed(path, nd->flags);
2305 if (error < 0)
2306 goto exit_dput;
Al Virofb1cc552009-12-24 01:58:28 -05002307
Al Viroa3fbbde2011-11-07 21:21:26 +00002308 if (error)
2309 nd->flags |= LOOKUP_JUMPED;
2310
Al Virofb1cc552009-12-24 01:58:28 -05002311 error = -ENOENT;
2312 if (!path->dentry->d_inode)
2313 goto exit_dput;
Al Viro9e67f362009-12-26 07:04:50 -05002314
2315 if (path->dentry->d_inode->i_op->follow_link)
Al Virofb1cc552009-12-24 01:58:28 -05002316 return NULL;
Al Virofb1cc552009-12-24 01:58:28 -05002317
2318 path_to_nameidata(path, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11002319 nd->inode = path->dentry->d_inode;
Al Viroa3fbbde2011-11-07 21:21:26 +00002320 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
2321 error = complete_walk(nd);
2322 if (error)
Miklos Szeredi097b1802012-03-06 13:56:33 +01002323 return ERR_PTR(error);
Al Virofb1cc552009-12-24 01:58:28 -05002324 error = -EISDIR;
Nick Piggin31e6b012011-01-07 17:49:52 +11002325 if (S_ISDIR(nd->inode->i_mode))
Al Virofb1cc552009-12-24 01:58:28 -05002326 goto exit;
Al Viro67ee3ad2009-12-26 07:01:01 -05002327ok:
Al Viro6c0d46c2011-03-09 00:59:59 -05002328 if (!S_ISREG(nd->inode->i_mode))
2329 will_truncate = 0;
2330
Al Viro0f9d1a12011-03-09 00:13:14 -05002331 if (will_truncate) {
2332 error = mnt_want_write(nd->path.mnt);
2333 if (error)
2334 goto exit;
Al Viroca344a892011-03-09 00:36:45 -05002335 want_write = 1;
Al Viro0f9d1a12011-03-09 00:13:14 -05002336 }
Al Viroca344a892011-03-09 00:36:45 -05002337common:
Al Virobcda7652011-03-13 16:42:14 -04002338 error = may_open(&nd->path, acc_mode, open_flag);
Al Viroca344a892011-03-09 00:36:45 -05002339 if (error)
Al Viro0f9d1a12011-03-09 00:13:14 -05002340 goto exit;
Al Viro0f9d1a12011-03-09 00:13:14 -05002341 filp = nameidata_to_filp(nd);
2342 if (!IS_ERR(filp)) {
2343 error = ima_file_check(filp, op->acc_mode);
2344 if (error) {
2345 fput(filp);
2346 filp = ERR_PTR(error);
2347 }
2348 }
2349 if (!IS_ERR(filp)) {
2350 if (will_truncate) {
2351 error = handle_truncate(filp);
2352 if (error) {
2353 fput(filp);
2354 filp = ERR_PTR(error);
2355 }
2356 }
2357 }
Al Viroca344a892011-03-09 00:36:45 -05002358out:
2359 if (want_write)
Al Viro0f9d1a12011-03-09 00:13:14 -05002360 mnt_drop_write(nd->path.mnt);
2361 path_put(&nd->path);
Al Virofb1cc552009-12-24 01:58:28 -05002362 return filp;
2363
2364exit_mutex_unlock:
2365 mutex_unlock(&dir->d_inode->i_mutex);
2366exit_dput:
2367 path_put_conditional(path, nd);
2368exit:
Al Viroca344a892011-03-09 00:36:45 -05002369 filp = ERR_PTR(error);
2370 goto out;
Al Virofb1cc552009-12-24 01:58:28 -05002371}
2372
Al Viro13aab422011-02-23 17:54:08 -05002373static struct file *path_openat(int dfd, const char *pathname,
Al Viro73d049a2011-03-11 12:08:24 -05002374 struct nameidata *nd, const struct open_flags *op, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375{
Al Virofe2d35f2011-03-05 22:58:25 -05002376 struct file *base = NULL;
Dave Hansen4a3fd212008-02-15 14:37:48 -08002377 struct file *filp;
Al Viro9850c052010-01-13 15:01:15 -05002378 struct path path;
Al Viro13aab422011-02-23 17:54:08 -05002379 int error;
Nick Piggin31e6b012011-01-07 17:49:52 +11002380
2381 filp = get_empty_filp();
2382 if (!filp)
2383 return ERR_PTR(-ENFILE);
2384
Al Viro47c805d2011-02-23 17:44:09 -05002385 filp->f_flags = op->open_flag;
Al Viro73d049a2011-03-11 12:08:24 -05002386 nd->intent.open.file = filp;
2387 nd->intent.open.flags = open_to_namei_flags(op->open_flag);
2388 nd->intent.open.create_mode = op->mode;
Nick Piggin31e6b012011-01-07 17:49:52 +11002389
Al Viro73d049a2011-03-11 12:08:24 -05002390 error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
Nick Piggin31e6b012011-01-07 17:49:52 +11002391 if (unlikely(error))
Al Viro13aab422011-02-23 17:54:08 -05002392 goto out_filp;
Nick Piggin31e6b012011-01-07 17:49:52 +11002393
Al Virofe2d35f2011-03-05 22:58:25 -05002394 current->total_link_count = 0;
Al Viro73d049a2011-03-11 12:08:24 -05002395 error = link_path_walk(pathname, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11002396 if (unlikely(error))
2397 goto out_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
Al Viro73d049a2011-03-11 12:08:24 -05002399 filp = do_last(nd, &path, op, pathname);
Al Viro806b6812009-12-26 07:16:40 -05002400 while (unlikely(!filp)) { /* trailing symlink */
Nick Piggin7b9337a2011-01-14 08:42:43 +00002401 struct path link = path;
Al Virodef4af32009-12-26 08:37:05 -05002402 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04002403 if (!(nd->flags & LOOKUP_FOLLOW)) {
Al Viro73d049a2011-03-11 12:08:24 -05002404 path_put_conditional(&path, nd);
2405 path_put(&nd->path);
Al Viro40b39132011-03-09 16:22:18 -05002406 filp = ERR_PTR(-ELOOP);
2407 break;
2408 }
Al Viro73d049a2011-03-11 12:08:24 -05002409 nd->flags |= LOOKUP_PARENT;
2410 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
Al Viro574197e2011-03-14 22:20:34 -04002411 error = follow_link(&link, nd, &cookie);
Al Viroc3e380b2011-02-23 13:39:45 -05002412 if (unlikely(error))
Al Virof1afe9e2011-02-22 22:27:28 -05002413 filp = ERR_PTR(error);
Al Viroc3e380b2011-02-23 13:39:45 -05002414 else
Al Viro73d049a2011-03-11 12:08:24 -05002415 filp = do_last(nd, &path, op, pathname);
Al Viro574197e2011-03-14 22:20:34 -04002416 put_link(nd, &link, cookie);
Al Viro806b6812009-12-26 07:16:40 -05002417 }
Al Viro10fa8e62009-12-26 07:09:49 -05002418out:
Al Viro73d049a2011-03-11 12:08:24 -05002419 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
2420 path_put(&nd->root);
Al Virofe2d35f2011-03-05 22:58:25 -05002421 if (base)
2422 fput(base);
Al Viro73d049a2011-03-11 12:08:24 -05002423 release_open_intent(nd);
Al Viro10fa8e62009-12-26 07:09:49 -05002424 return filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
Nick Piggin31e6b012011-01-07 17:49:52 +11002426out_filp:
Al Viro806b6812009-12-26 07:16:40 -05002427 filp = ERR_PTR(error);
Al Viro10fa8e62009-12-26 07:09:49 -05002428 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429}
2430
Al Viro13aab422011-02-23 17:54:08 -05002431struct file *do_filp_open(int dfd, const char *pathname,
2432 const struct open_flags *op, int flags)
2433{
Al Viro73d049a2011-03-11 12:08:24 -05002434 struct nameidata nd;
Al Viro13aab422011-02-23 17:54:08 -05002435 struct file *filp;
2436
Al Viro73d049a2011-03-11 12:08:24 -05002437 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
Al Viro13aab422011-02-23 17:54:08 -05002438 if (unlikely(filp == ERR_PTR(-ECHILD)))
Al Viro73d049a2011-03-11 12:08:24 -05002439 filp = path_openat(dfd, pathname, &nd, op, flags);
Al Viro13aab422011-02-23 17:54:08 -05002440 if (unlikely(filp == ERR_PTR(-ESTALE)))
Al Viro73d049a2011-03-11 12:08:24 -05002441 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
Al Viro13aab422011-02-23 17:54:08 -05002442 return filp;
2443}
2444
Al Viro73d049a2011-03-11 12:08:24 -05002445struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
2446 const char *name, const struct open_flags *op, int flags)
2447{
2448 struct nameidata nd;
2449 struct file *file;
2450
2451 nd.root.mnt = mnt;
2452 nd.root.dentry = dentry;
2453
2454 flags |= LOOKUP_ROOT;
2455
Al Virobcda7652011-03-13 16:42:14 -04002456 if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
Al Viro73d049a2011-03-11 12:08:24 -05002457 return ERR_PTR(-ELOOP);
2458
2459 file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
2460 if (unlikely(file == ERR_PTR(-ECHILD)))
2461 file = path_openat(-1, name, &nd, op, flags);
2462 if (unlikely(file == ERR_PTR(-ESTALE)))
2463 file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
2464 return file;
2465}
2466
Al Viroed75e952011-06-27 16:53:43 -04002467struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002469 struct dentry *dentry = ERR_PTR(-EEXIST);
Al Viroed75e952011-06-27 16:53:43 -04002470 struct nameidata nd;
2471 int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
2472 if (error)
2473 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002475 /*
2476 * Yucky last component or no last component at all?
2477 * (foo/., foo/.., /////)
2478 */
Al Viroed75e952011-06-27 16:53:43 -04002479 if (nd.last_type != LAST_NORM)
2480 goto out;
2481 nd.flags &= ~LOOKUP_PARENT;
2482 nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
2483 nd.intent.open.flags = O_EXCL;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002484
2485 /*
2486 * Do the final lookup.
2487 */
Al Viroed75e952011-06-27 16:53:43 -04002488 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2489 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 if (IS_ERR(dentry))
2491 goto fail;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002492
Al Viroe9baf6e2008-05-15 04:49:12 -04002493 if (dentry->d_inode)
2494 goto eexist;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002495 /*
2496 * Special case - lookup gave negative, but... we had foo/bar/
2497 * From the vfs_mknod() POV we just have a negative dentry -
2498 * all is fine. Let's be bastards - you had / on the end, you've
2499 * been asking for (non-existent) directory. -ENOENT for you.
2500 */
Al Viroed75e952011-06-27 16:53:43 -04002501 if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
Al Viroe9baf6e2008-05-15 04:49:12 -04002502 dput(dentry);
2503 dentry = ERR_PTR(-ENOENT);
Al Viroed75e952011-06-27 16:53:43 -04002504 goto fail;
Al Viroe9baf6e2008-05-15 04:49:12 -04002505 }
Al Viroed75e952011-06-27 16:53:43 -04002506 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 return dentry;
Al Viroe9baf6e2008-05-15 04:49:12 -04002508eexist:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 dput(dentry);
Al Viroe9baf6e2008-05-15 04:49:12 -04002510 dentry = ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511fail:
Al Viroed75e952011-06-27 16:53:43 -04002512 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2513out:
2514 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 return dentry;
2516}
Al Virodae6ad82011-06-26 11:50:15 -04002517EXPORT_SYMBOL(kern_path_create);
2518
2519struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir)
2520{
2521 char *tmp = getname(pathname);
2522 struct dentry *res;
2523 if (IS_ERR(tmp))
2524 return ERR_CAST(tmp);
2525 res = kern_path_create(dfd, tmp, path, is_dir);
2526 putname(tmp);
2527 return res;
2528}
2529EXPORT_SYMBOL(user_path_create);
2530
Al Viro1a67aaf2011-07-26 01:52:52 -04002531int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002533 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534
2535 if (error)
2536 return error;
2537
Serge E. Hallyne795b712011-03-23 16:43:25 -07002538 if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
2539 !ns_capable(inode_userns(dir), CAP_MKNOD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 return -EPERM;
2541
Al Viroacfa4382008-12-04 10:06:33 -05002542 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 return -EPERM;
2544
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07002545 error = devcgroup_inode_mknod(mode, dev);
2546 if (error)
2547 return error;
2548
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 error = security_inode_mknod(dir, dentry, mode, dev);
2550 if (error)
2551 return error;
2552
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002554 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002555 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 return error;
2557}
2558
Al Virof69aac02011-07-26 04:31:40 -04002559static int may_mknod(umode_t mode)
Dave Hansen463c3192008-02-15 14:37:57 -08002560{
2561 switch (mode & S_IFMT) {
2562 case S_IFREG:
2563 case S_IFCHR:
2564 case S_IFBLK:
2565 case S_IFIFO:
2566 case S_IFSOCK:
2567 case 0: /* zero mode translates to S_IFREG */
2568 return 0;
2569 case S_IFDIR:
2570 return -EPERM;
2571 default:
2572 return -EINVAL;
2573 }
2574}
2575
Al Viro8208a222011-07-25 17:32:17 -04002576SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002577 unsigned, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578{
Al Viro2ad94ae2008-07-21 09:32:51 -04002579 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04002580 struct path path;
2581 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582
2583 if (S_ISDIR(mode))
2584 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585
Al Virodae6ad82011-06-26 11:50:15 -04002586 dentry = user_path_create(dfd, filename, &path, 0);
2587 if (IS_ERR(dentry))
2588 return PTR_ERR(dentry);
Al Viro2ad94ae2008-07-21 09:32:51 -04002589
Al Virodae6ad82011-06-26 11:50:15 -04002590 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04002591 mode &= ~current_umask();
Dave Hansen463c3192008-02-15 14:37:57 -08002592 error = may_mknod(mode);
2593 if (error)
2594 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04002595 error = mnt_want_write(path.mnt);
Dave Hansen463c3192008-02-15 14:37:57 -08002596 if (error)
2597 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04002598 error = security_path_mknod(&path, dentry, mode, dev);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002599 if (error)
2600 goto out_drop_write;
Dave Hansen463c3192008-02-15 14:37:57 -08002601 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 case 0: case S_IFREG:
Al Virodae6ad82011-06-26 11:50:15 -04002603 error = vfs_create(path.dentry->d_inode,dentry,mode,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 break;
2605 case S_IFCHR: case S_IFBLK:
Al Virodae6ad82011-06-26 11:50:15 -04002606 error = vfs_mknod(path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 new_decode_dev(dev));
2608 break;
2609 case S_IFIFO: case S_IFSOCK:
Al Virodae6ad82011-06-26 11:50:15 -04002610 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 }
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002613out_drop_write:
Al Virodae6ad82011-06-26 11:50:15 -04002614 mnt_drop_write(path.mnt);
Dave Hansen463c3192008-02-15 14:37:57 -08002615out_dput:
2616 dput(dentry);
Al Virodae6ad82011-06-26 11:50:15 -04002617 mutex_unlock(&path.dentry->d_inode->i_mutex);
2618 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619
2620 return error;
2621}
2622
Al Viro8208a222011-07-25 17:32:17 -04002623SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002624{
2625 return sys_mknodat(AT_FDCWD, filename, mode, dev);
2626}
2627
Al Viro18bb1db2011-07-26 01:41:39 -04002628int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002630 int error = may_create(dir, dentry);
Al Viro8de52772012-02-06 12:45:27 -05002631 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632
2633 if (error)
2634 return error;
2635
Al Viroacfa4382008-12-04 10:06:33 -05002636 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 return -EPERM;
2638
2639 mode &= (S_IRWXUGO|S_ISVTX);
2640 error = security_inode_mkdir(dir, dentry, mode);
2641 if (error)
2642 return error;
2643
Al Viro8de52772012-02-06 12:45:27 -05002644 if (max_links && dir->i_nlink >= max_links)
2645 return -EMLINK;
2646
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002648 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002649 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 return error;
2651}
2652
Al Viroa218d0f2011-11-21 14:59:34 -05002653SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654{
Dave Hansen6902d922006-09-30 23:29:01 -07002655 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04002656 struct path path;
2657 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658
Al Virodae6ad82011-06-26 11:50:15 -04002659 dentry = user_path_create(dfd, pathname, &path, 1);
Dave Hansen6902d922006-09-30 23:29:01 -07002660 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04002661 return PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002662
Al Virodae6ad82011-06-26 11:50:15 -04002663 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04002664 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04002665 error = mnt_want_write(path.mnt);
Dave Hansen463c3192008-02-15 14:37:57 -08002666 if (error)
2667 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04002668 error = security_path_mkdir(&path, dentry, mode);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002669 if (error)
2670 goto out_drop_write;
Al Virodae6ad82011-06-26 11:50:15 -04002671 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002672out_drop_write:
Al Virodae6ad82011-06-26 11:50:15 -04002673 mnt_drop_write(path.mnt);
Dave Hansen463c3192008-02-15 14:37:57 -08002674out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002675 dput(dentry);
Al Virodae6ad82011-06-26 11:50:15 -04002676 mutex_unlock(&path.dentry->d_inode->i_mutex);
2677 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 return error;
2679}
2680
Al Viroa218d0f2011-11-21 14:59:34 -05002681SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002682{
2683 return sys_mkdirat(AT_FDCWD, pathname, mode);
2684}
2685
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686/*
Sage Weila71905f2011-05-24 13:06:08 -07002687 * The dentry_unhash() helper will try to drop the dentry early: we
J. Bruce Fieldsc0d02592012-02-15 11:48:40 -05002688 * should have a usage count of 1 if we're the only user of this
Sage Weila71905f2011-05-24 13:06:08 -07002689 * dentry, and if that is true (possibly after pruning the dcache),
2690 * then we drop the dentry now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 *
2692 * A low-level filesystem can, if it choses, legally
2693 * do a
2694 *
2695 * if (!d_unhashed(dentry))
2696 * return -EBUSY;
2697 *
2698 * if it cannot handle the case of removing a directory
2699 * that is still in use by something else..
2700 */
2701void dentry_unhash(struct dentry *dentry)
2702{
Vasily Averindc168422006-12-06 20:37:07 -08002703 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 spin_lock(&dentry->d_lock);
Sage Weil64252c72011-05-24 13:06:05 -07002705 if (dentry->d_count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 __d_drop(dentry);
2707 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708}
2709
2710int vfs_rmdir(struct inode *dir, struct dentry *dentry)
2711{
2712 int error = may_delete(dir, dentry, 1);
2713
2714 if (error)
2715 return error;
2716
Al Viroacfa4382008-12-04 10:06:33 -05002717 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 return -EPERM;
2719
Al Viro1d2ef592011-09-14 18:55:41 +01002720 dget(dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002721 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722
Sage Weil912dbc12011-05-24 13:06:11 -07002723 error = -EBUSY;
2724 if (d_mountpoint(dentry))
2725 goto out;
2726
2727 error = security_inode_rmdir(dir, dentry);
2728 if (error)
2729 goto out;
2730
Sage Weil3cebde22011-05-29 21:20:59 -07002731 shrink_dcache_parent(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07002732 error = dir->i_op->rmdir(dir, dentry);
2733 if (error)
2734 goto out;
2735
2736 dentry->d_inode->i_flags |= S_DEAD;
2737 dont_mount(dentry);
2738
2739out:
2740 mutex_unlock(&dentry->d_inode->i_mutex);
Al Viro1d2ef592011-09-14 18:55:41 +01002741 dput(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07002742 if (!error)
2743 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 return error;
2745}
2746
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002747static long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748{
2749 int error = 0;
2750 char * name;
2751 struct dentry *dentry;
2752 struct nameidata nd;
2753
Al Viro2ad94ae2008-07-21 09:32:51 -04002754 error = user_path_parent(dfd, pathname, &nd, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002756 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
2758 switch(nd.last_type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002759 case LAST_DOTDOT:
2760 error = -ENOTEMPTY;
2761 goto exit1;
2762 case LAST_DOT:
2763 error = -EINVAL;
2764 goto exit1;
2765 case LAST_ROOT:
2766 error = -EBUSY;
2767 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002769
2770 nd.flags &= ~LOOKUP_PARENT;
2771
Jan Blunck4ac91372008-02-14 19:34:32 -08002772 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002773 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002775 if (IS_ERR(dentry))
2776 goto exit2;
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04002777 if (!dentry->d_inode) {
2778 error = -ENOENT;
2779 goto exit3;
2780 }
Dave Hansen06227532008-02-15 14:37:34 -08002781 error = mnt_want_write(nd.path.mnt);
2782 if (error)
2783 goto exit3;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002784 error = security_path_rmdir(&nd.path, dentry);
2785 if (error)
2786 goto exit4;
Jan Blunck4ac91372008-02-14 19:34:32 -08002787 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002788exit4:
Dave Hansen06227532008-02-15 14:37:34 -08002789 mnt_drop_write(nd.path.mnt);
2790exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07002791 dput(dentry);
2792exit2:
Jan Blunck4ac91372008-02-14 19:34:32 -08002793 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002795 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 putname(name);
2797 return error;
2798}
2799
Heiko Carstens3cdad422009-01-14 14:14:22 +01002800SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002801{
2802 return do_rmdir(AT_FDCWD, pathname);
2803}
2804
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805int vfs_unlink(struct inode *dir, struct dentry *dentry)
2806{
2807 int error = may_delete(dir, dentry, 0);
2808
2809 if (error)
2810 return error;
2811
Al Viroacfa4382008-12-04 10:06:33 -05002812 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 return -EPERM;
2814
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002815 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 if (d_mountpoint(dentry))
2817 error = -EBUSY;
2818 else {
2819 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05002820 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 error = dir->i_op->unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05002822 if (!error)
Al Virod83c49f2010-04-30 17:17:09 -04002823 dont_mount(dentry);
Al Virobec10522010-03-03 14:12:08 -05002824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002826 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827
2828 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2829 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
Jan Karaece95912008-02-06 01:37:13 -08002830 fsnotify_link_count(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 }
Robert Love0eeca282005-07-12 17:06:03 -04002833
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 return error;
2835}
2836
2837/*
2838 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002839 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 * writeout happening, and we don't want to prevent access to the directory
2841 * while waiting on the I/O.
2842 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002843static long do_unlinkat(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844{
Al Viro2ad94ae2008-07-21 09:32:51 -04002845 int error;
2846 char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 struct dentry *dentry;
2848 struct nameidata nd;
2849 struct inode *inode = NULL;
2850
Al Viro2ad94ae2008-07-21 09:32:51 -04002851 error = user_path_parent(dfd, pathname, &nd, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002853 return error;
2854
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 error = -EISDIR;
2856 if (nd.last_type != LAST_NORM)
2857 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002858
2859 nd.flags &= ~LOOKUP_PARENT;
2860
Jan Blunck4ac91372008-02-14 19:34:32 -08002861 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002862 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 error = PTR_ERR(dentry);
2864 if (!IS_ERR(dentry)) {
2865 /* Why not before? Because we want correct error value */
Török Edwin50338b82011-06-16 00:06:14 +03002866 if (nd.last.name[nd.last.len])
2867 goto slashes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 inode = dentry->d_inode;
Török Edwin50338b82011-06-16 00:06:14 +03002869 if (!inode)
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04002870 goto slashes;
2871 ihold(inode);
Dave Hansen06227532008-02-15 14:37:34 -08002872 error = mnt_want_write(nd.path.mnt);
2873 if (error)
2874 goto exit2;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002875 error = security_path_unlink(&nd.path, dentry);
2876 if (error)
2877 goto exit3;
Jan Blunck4ac91372008-02-14 19:34:32 -08002878 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002879exit3:
Dave Hansen06227532008-02-15 14:37:34 -08002880 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 exit2:
2882 dput(dentry);
2883 }
Jan Blunck4ac91372008-02-14 19:34:32 -08002884 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 if (inode)
2886 iput(inode); /* truncate the inode here */
2887exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002888 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 putname(name);
2890 return error;
2891
2892slashes:
2893 error = !dentry->d_inode ? -ENOENT :
2894 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2895 goto exit2;
2896}
2897
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002898SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002899{
2900 if ((flag & ~AT_REMOVEDIR) != 0)
2901 return -EINVAL;
2902
2903 if (flag & AT_REMOVEDIR)
2904 return do_rmdir(dfd, pathname);
2905
2906 return do_unlinkat(dfd, pathname);
2907}
2908
Heiko Carstens3480b252009-01-14 14:14:16 +01002909SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002910{
2911 return do_unlinkat(AT_FDCWD, pathname);
2912}
2913
Miklos Szeredidb2e7472008-06-24 16:50:16 +02002914int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002916 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917
2918 if (error)
2919 return error;
2920
Al Viroacfa4382008-12-04 10:06:33 -05002921 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 return -EPERM;
2923
2924 error = security_inode_symlink(dir, dentry, oldname);
2925 if (error)
2926 return error;
2927
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002929 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002930 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 return error;
2932}
2933
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002934SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
2935 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936{
Al Viro2ad94ae2008-07-21 09:32:51 -04002937 int error;
2938 char *from;
Dave Hansen6902d922006-09-30 23:29:01 -07002939 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04002940 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941
2942 from = getname(oldname);
Al Viro2ad94ae2008-07-21 09:32:51 -04002943 if (IS_ERR(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 return PTR_ERR(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04002945
Al Virodae6ad82011-06-26 11:50:15 -04002946 dentry = user_path_create(newdfd, newname, &path, 0);
Dave Hansen6902d922006-09-30 23:29:01 -07002947 error = PTR_ERR(dentry);
2948 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04002949 goto out_putname;
Dave Hansen6902d922006-09-30 23:29:01 -07002950
Al Virodae6ad82011-06-26 11:50:15 -04002951 error = mnt_want_write(path.mnt);
Dave Hansen75c3f292008-02-15 14:37:45 -08002952 if (error)
2953 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04002954 error = security_path_symlink(&path, dentry, from);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002955 if (error)
2956 goto out_drop_write;
Al Virodae6ad82011-06-26 11:50:15 -04002957 error = vfs_symlink(path.dentry->d_inode, dentry, from);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002958out_drop_write:
Al Virodae6ad82011-06-26 11:50:15 -04002959 mnt_drop_write(path.mnt);
Dave Hansen75c3f292008-02-15 14:37:45 -08002960out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002961 dput(dentry);
Al Virodae6ad82011-06-26 11:50:15 -04002962 mutex_unlock(&path.dentry->d_inode->i_mutex);
2963 path_put(&path);
Dave Hansen6902d922006-09-30 23:29:01 -07002964out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 putname(from);
2966 return error;
2967}
2968
Heiko Carstens3480b252009-01-14 14:14:16 +01002969SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002970{
2971 return sys_symlinkat(oldname, AT_FDCWD, newname);
2972}
2973
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2975{
2976 struct inode *inode = old_dentry->d_inode;
Al Viro8de52772012-02-06 12:45:27 -05002977 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 int error;
2979
2980 if (!inode)
2981 return -ENOENT;
2982
Miklos Szeredia95164d2008-07-30 15:08:48 +02002983 error = may_create(dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 if (error)
2985 return error;
2986
2987 if (dir->i_sb != inode->i_sb)
2988 return -EXDEV;
2989
2990 /*
2991 * A link to an append-only or immutable file cannot be created.
2992 */
2993 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2994 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05002995 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002997 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 return -EPERM;
2999
3000 error = security_inode_link(old_dentry, dir, new_dentry);
3001 if (error)
3002 return error;
3003
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003004 mutex_lock(&inode->i_mutex);
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05303005 /* Make sure we don't allow creating hardlink to an unlinked file */
3006 if (inode->i_nlink == 0)
3007 error = -ENOENT;
Al Viro8de52772012-02-06 12:45:27 -05003008 else if (max_links && inode->i_nlink >= max_links)
3009 error = -EMLINK;
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05303010 else
3011 error = dir->i_op->link(old_dentry, dir, new_dentry);
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003012 mutex_unlock(&inode->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07003013 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003014 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 return error;
3016}
3017
3018/*
3019 * Hardlinks are often used in delicate situations. We avoid
3020 * security-related surprises by not following symlinks on the
3021 * newname. --KAB
3022 *
3023 * We don't follow them on the oldname either to be compatible
3024 * with linux 2.0, and to avoid hard-linking to directories
3025 * and other special files. --ADM
3026 */
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003027SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
3028 int, newdfd, const char __user *, newname, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029{
3030 struct dentry *new_dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003031 struct path old_path, new_path;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303032 int how = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303035 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003036 return -EINVAL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303037 /*
3038 * To use null names we require CAP_DAC_READ_SEARCH
3039 * This ensures that not everyone will be able to create
3040 * handlink using the passed filedescriptor.
3041 */
3042 if (flags & AT_EMPTY_PATH) {
3043 if (!capable(CAP_DAC_READ_SEARCH))
3044 return -ENOENT;
3045 how = LOOKUP_EMPTY;
3046 }
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003047
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303048 if (flags & AT_SYMLINK_FOLLOW)
3049 how |= LOOKUP_FOLLOW;
3050
3051 error = user_path_at(olddfd, oldname, how, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04003053 return error;
3054
Al Virodae6ad82011-06-26 11:50:15 -04003055 new_dentry = user_path_create(newdfd, newname, &new_path, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003057 if (IS_ERR(new_dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003058 goto out;
3059
3060 error = -EXDEV;
3061 if (old_path.mnt != new_path.mnt)
3062 goto out_dput;
3063 error = mnt_want_write(new_path.mnt);
Dave Hansen75c3f292008-02-15 14:37:45 -08003064 if (error)
3065 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04003066 error = security_path_link(old_path.dentry, &new_path, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003067 if (error)
3068 goto out_drop_write;
Al Virodae6ad82011-06-26 11:50:15 -04003069 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003070out_drop_write:
Al Virodae6ad82011-06-26 11:50:15 -04003071 mnt_drop_write(new_path.mnt);
Dave Hansen75c3f292008-02-15 14:37:45 -08003072out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07003073 dput(new_dentry);
Al Virodae6ad82011-06-26 11:50:15 -04003074 mutex_unlock(&new_path.dentry->d_inode->i_mutex);
3075 path_put(&new_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076out:
Al Viro2d8f3032008-07-22 09:59:21 -04003077 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078
3079 return error;
3080}
3081
Heiko Carstens3480b252009-01-14 14:14:16 +01003082SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003083{
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003084 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003085}
3086
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087/*
3088 * The worst of all namespace operations - renaming directory. "Perverted"
3089 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
3090 * Problems:
3091 * a) we can get into loop creation. Check is done in is_subdir().
3092 * b) race potential - two innocent renames can create a loop together.
3093 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08003094 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 * story.
3096 * c) we have to lock _three_ objects - parents and victim (if it exists).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003097 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 * whether the target exists). Solution: try to be smart with locking
3099 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08003100 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 * move will be locked. Thus we can rank directories by the tree
3102 * (ancestors first) and rank all non-directories after them.
3103 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08003104 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 * HOWEVER, it relies on the assumption that any object with ->lookup()
3106 * has no more than 1 dentry. If "hybrid" objects will ever appear,
3107 * we'd better make sure that there's no link(2) for them.
Sage Weile4eaac02011-05-24 13:06:07 -07003108 * d) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003109 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05003111 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 * locking].
3113 */
Adrian Bunk75c96f82005-05-05 16:16:09 -07003114static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
3115 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116{
3117 int error = 0;
Sage Weil9055cba2011-05-24 13:06:12 -07003118 struct inode *target = new_dentry->d_inode;
Al Viro8de52772012-02-06 12:45:27 -05003119 unsigned max_links = new_dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120
3121 /*
3122 * If we are going to change the parent - check write permissions,
3123 * we'll need to flip '..'.
3124 */
3125 if (new_dir != old_dir) {
Al Virof419a2e2008-07-22 00:07:17 -04003126 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 if (error)
3128 return error;
3129 }
3130
3131 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3132 if (error)
3133 return error;
3134
Al Viro1d2ef592011-09-14 18:55:41 +01003135 dget(new_dentry);
Al Virod83c49f2010-04-30 17:17:09 -04003136 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003137 mutex_lock(&target->i_mutex);
Sage Weil9055cba2011-05-24 13:06:12 -07003138
3139 error = -EBUSY;
3140 if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
3141 goto out;
3142
Al Viro8de52772012-02-06 12:45:27 -05003143 error = -EMLINK;
3144 if (max_links && !target && new_dir != old_dir &&
3145 new_dir->i_nlink >= max_links)
3146 goto out;
3147
Sage Weil3cebde22011-05-29 21:20:59 -07003148 if (target)
3149 shrink_dcache_parent(new_dentry);
Sage Weil9055cba2011-05-24 13:06:12 -07003150 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3151 if (error)
3152 goto out;
3153
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 if (target) {
Sage Weil9055cba2011-05-24 13:06:12 -07003155 target->i_flags |= S_DEAD;
3156 dont_mount(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 }
Sage Weil9055cba2011-05-24 13:06:12 -07003158out:
3159 if (target)
3160 mutex_unlock(&target->i_mutex);
Al Viro1d2ef592011-09-14 18:55:41 +01003161 dput(new_dentry);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07003162 if (!error)
Mark Fasheh349457c2006-09-08 14:22:21 -07003163 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3164 d_move(old_dentry,new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 return error;
3166}
3167
Adrian Bunk75c96f82005-05-05 16:16:09 -07003168static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
3169 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170{
Sage Weil51892bb2011-05-24 13:06:13 -07003171 struct inode *target = new_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 int error;
3173
3174 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3175 if (error)
3176 return error;
3177
3178 dget(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003180 mutex_lock(&target->i_mutex);
Sage Weil51892bb2011-05-24 13:06:13 -07003181
3182 error = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
Sage Weil51892bb2011-05-24 13:06:13 -07003184 goto out;
3185
3186 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3187 if (error)
3188 goto out;
3189
3190 if (target)
3191 dont_mount(new_dentry);
3192 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3193 d_move(old_dentry, new_dentry);
3194out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003196 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 dput(new_dentry);
3198 return error;
3199}
3200
3201int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
3202 struct inode *new_dir, struct dentry *new_dentry)
3203{
3204 int error;
3205 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
Eric Paris59b0df22010-02-08 12:53:52 -05003206 const unsigned char *old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207
3208 if (old_dentry->d_inode == new_dentry->d_inode)
3209 return 0;
3210
3211 error = may_delete(old_dir, old_dentry, is_dir);
3212 if (error)
3213 return error;
3214
3215 if (!new_dentry->d_inode)
Miklos Szeredia95164d2008-07-30 15:08:48 +02003216 error = may_create(new_dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 else
3218 error = may_delete(new_dir, new_dentry, is_dir);
3219 if (error)
3220 return error;
3221
Al Viroacfa4382008-12-04 10:06:33 -05003222 if (!old_dir->i_op->rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 return -EPERM;
3224
Robert Love0eeca282005-07-12 17:06:03 -04003225 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
3226
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 if (is_dir)
3228 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
3229 else
3230 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
Al Viro123df292009-12-25 04:57:57 -05003231 if (!error)
3232 fsnotify_move(old_dir, new_dir, old_name, is_dir,
Al Viro5a190ae2007-06-07 12:19:32 -04003233 new_dentry->d_inode, old_dentry);
Robert Love0eeca282005-07-12 17:06:03 -04003234 fsnotify_oldname_free(old_name);
3235
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 return error;
3237}
3238
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003239SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
3240 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241{
Al Viro2ad94ae2008-07-21 09:32:51 -04003242 struct dentry *old_dir, *new_dir;
3243 struct dentry *old_dentry, *new_dentry;
3244 struct dentry *trap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 struct nameidata oldnd, newnd;
Al Viro2ad94ae2008-07-21 09:32:51 -04003246 char *from;
3247 char *to;
3248 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249
Al Viro2ad94ae2008-07-21 09:32:51 -04003250 error = user_path_parent(olddfd, oldname, &oldnd, &from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 if (error)
3252 goto exit;
3253
Al Viro2ad94ae2008-07-21 09:32:51 -04003254 error = user_path_parent(newdfd, newname, &newnd, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 if (error)
3256 goto exit1;
3257
3258 error = -EXDEV;
Jan Blunck4ac91372008-02-14 19:34:32 -08003259 if (oldnd.path.mnt != newnd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 goto exit2;
3261
Jan Blunck4ac91372008-02-14 19:34:32 -08003262 old_dir = oldnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 error = -EBUSY;
3264 if (oldnd.last_type != LAST_NORM)
3265 goto exit2;
3266
Jan Blunck4ac91372008-02-14 19:34:32 -08003267 new_dir = newnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 if (newnd.last_type != LAST_NORM)
3269 goto exit2;
3270
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003271 oldnd.flags &= ~LOOKUP_PARENT;
3272 newnd.flags &= ~LOOKUP_PARENT;
OGAWA Hirofumi4e9ed2f2008-10-16 07:50:29 +09003273 newnd.flags |= LOOKUP_RENAME_TARGET;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003274
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 trap = lock_rename(new_dir, old_dir);
3276
Christoph Hellwig49705b72005-11-08 21:35:06 -08003277 old_dentry = lookup_hash(&oldnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 error = PTR_ERR(old_dentry);
3279 if (IS_ERR(old_dentry))
3280 goto exit3;
3281 /* source must exist */
3282 error = -ENOENT;
3283 if (!old_dentry->d_inode)
3284 goto exit4;
3285 /* unless the source is a directory trailing slashes give -ENOTDIR */
3286 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
3287 error = -ENOTDIR;
3288 if (oldnd.last.name[oldnd.last.len])
3289 goto exit4;
3290 if (newnd.last.name[newnd.last.len])
3291 goto exit4;
3292 }
3293 /* source should not be ancestor of target */
3294 error = -EINVAL;
3295 if (old_dentry == trap)
3296 goto exit4;
Christoph Hellwig49705b72005-11-08 21:35:06 -08003297 new_dentry = lookup_hash(&newnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 error = PTR_ERR(new_dentry);
3299 if (IS_ERR(new_dentry))
3300 goto exit4;
3301 /* target should not be an ancestor of source */
3302 error = -ENOTEMPTY;
3303 if (new_dentry == trap)
3304 goto exit5;
3305
Dave Hansen9079b1e2008-02-15 14:37:49 -08003306 error = mnt_want_write(oldnd.path.mnt);
3307 if (error)
3308 goto exit5;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003309 error = security_path_rename(&oldnd.path, old_dentry,
3310 &newnd.path, new_dentry);
3311 if (error)
3312 goto exit6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 error = vfs_rename(old_dir->d_inode, old_dentry,
3314 new_dir->d_inode, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003315exit6:
Dave Hansen9079b1e2008-02-15 14:37:49 -08003316 mnt_drop_write(oldnd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317exit5:
3318 dput(new_dentry);
3319exit4:
3320 dput(old_dentry);
3321exit3:
3322 unlock_rename(new_dir, old_dir);
3323exit2:
Jan Blunck1d957f92008-02-14 19:34:35 -08003324 path_put(&newnd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04003325 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003327 path_put(&oldnd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 putname(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04003329exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 return error;
3331}
3332
Heiko Carstensa26eab22009-01-14 14:14:17 +01003333SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003334{
3335 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
3336}
3337
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
3339{
3340 int len;
3341
3342 len = PTR_ERR(link);
3343 if (IS_ERR(link))
3344 goto out;
3345
3346 len = strlen(link);
3347 if (len > (unsigned) buflen)
3348 len = buflen;
3349 if (copy_to_user(buffer, link, len))
3350 len = -EFAULT;
3351out:
3352 return len;
3353}
3354
3355/*
3356 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
3357 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
3358 * using) it for any given inode is up to filesystem.
3359 */
3360int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3361{
3362 struct nameidata nd;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003363 void *cookie;
Marcin Slusarz694a1762008-06-09 16:40:37 -07003364 int res;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003365
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 nd.depth = 0;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003367 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
Marcin Slusarz694a1762008-06-09 16:40:37 -07003368 if (IS_ERR(cookie))
3369 return PTR_ERR(cookie);
3370
3371 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
3372 if (dentry->d_inode->i_op->put_link)
3373 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3374 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375}
3376
3377int vfs_follow_link(struct nameidata *nd, const char *link)
3378{
3379 return __vfs_follow_link(nd, link);
3380}
3381
3382/* get the link contents into pagecache */
3383static char *page_getlink(struct dentry * dentry, struct page **ppage)
3384{
Duane Griffinebd09ab2008-12-19 20:47:12 +00003385 char *kaddr;
3386 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 struct address_space *mapping = dentry->d_inode->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -07003388 page = read_mapping_page(mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 if (IS_ERR(page))
Nick Piggin6fe69002007-05-06 14:49:04 -07003390 return (char*)page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 *ppage = page;
Duane Griffinebd09ab2008-12-19 20:47:12 +00003392 kaddr = kmap(page);
3393 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3394 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395}
3396
3397int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3398{
3399 struct page *page = NULL;
3400 char *s = page_getlink(dentry, &page);
3401 int res = vfs_readlink(dentry,buffer,buflen,s);
3402 if (page) {
3403 kunmap(page);
3404 page_cache_release(page);
3405 }
3406 return res;
3407}
3408
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003409void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003411 struct page *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 nd_set_link(nd, page_getlink(dentry, &page));
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003413 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414}
3415
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003416void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003418 struct page *page = cookie;
3419
3420 if (page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 kunmap(page);
3422 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 }
3424}
3425
Nick Piggin54566b22009-01-04 12:00:53 -08003426/*
3427 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
3428 */
3429int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430{
3431 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003432 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07003433 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08003434 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 char *kaddr;
Nick Piggin54566b22009-01-04 12:00:53 -08003436 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
3437 if (nofs)
3438 flags |= AOP_FLAG_NOFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439
NeilBrown7e53cac2006-03-25 03:07:57 -08003440retry:
Nick Pigginafddba42007-10-16 01:25:01 -07003441 err = pagecache_write_begin(NULL, mapping, 0, len-1,
Nick Piggin54566b22009-01-04 12:00:53 -08003442 flags, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07003444 goto fail;
3445
Cong Wange8e3c3d2011-11-25 23:14:27 +08003446 kaddr = kmap_atomic(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 memcpy(kaddr, symname, len-1);
Cong Wange8e3c3d2011-11-25 23:14:27 +08003448 kunmap_atomic(kaddr);
Nick Pigginafddba42007-10-16 01:25:01 -07003449
3450 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3451 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 if (err < 0)
3453 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07003454 if (err < len-1)
3455 goto retry;
3456
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 mark_inode_dirty(inode);
3458 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459fail:
3460 return err;
3461}
3462
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003463int page_symlink(struct inode *inode, const char *symname, int len)
3464{
3465 return __page_symlink(inode, symname, len,
Nick Piggin54566b22009-01-04 12:00:53 -08003466 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003467}
3468
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003469const struct inode_operations page_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 .readlink = generic_readlink,
3471 .follow_link = page_follow_link_light,
3472 .put_link = page_put_link,
3473};
3474
Al Viro2d8f3032008-07-22 09:59:21 -04003475EXPORT_SYMBOL(user_path_at);
David Howellscc53ce52011-01-14 18:45:26 +00003476EXPORT_SYMBOL(follow_down_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477EXPORT_SYMBOL(follow_down);
3478EXPORT_SYMBOL(follow_up);
3479EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
3480EXPORT_SYMBOL(getname);
3481EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482EXPORT_SYMBOL(lookup_one_len);
3483EXPORT_SYMBOL(page_follow_link_light);
3484EXPORT_SYMBOL(page_put_link);
3485EXPORT_SYMBOL(page_readlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003486EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487EXPORT_SYMBOL(page_symlink);
3488EXPORT_SYMBOL(page_symlink_inode_operations);
Al Virod1811462008-08-02 00:49:18 -04003489EXPORT_SYMBOL(kern_path);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07003490EXPORT_SYMBOL(vfs_path_lookup);
Al Virof419a2e2008-07-22 00:07:17 -04003491EXPORT_SYMBOL(inode_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492EXPORT_SYMBOL(unlock_rename);
3493EXPORT_SYMBOL(vfs_create);
3494EXPORT_SYMBOL(vfs_follow_link);
3495EXPORT_SYMBOL(vfs_link);
3496EXPORT_SYMBOL(vfs_mkdir);
3497EXPORT_SYMBOL(vfs_mknod);
3498EXPORT_SYMBOL(generic_permission);
3499EXPORT_SYMBOL(vfs_readlink);
3500EXPORT_SYMBOL(vfs_rename);
3501EXPORT_SYMBOL(vfs_rmdir);
3502EXPORT_SYMBOL(vfs_symlink);
3503EXPORT_SYMBOL(vfs_unlink);
3504EXPORT_SYMBOL(dentry_unhash);
3505EXPORT_SYMBOL(generic_readlink);