blob: 5b811e72ac372fd910200e0ead47257907838e1e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/namei.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7/*
8 * Some corrections by tytso.
9 */
10
11/* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
12 * lookup logic.
13 */
14/* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
15 */
16
17#include <linux/init.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050018#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/slab.h>
20#include <linux/fs.h>
21#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/pagemap.h>
Robert Love0eeca282005-07-12 17:06:03 -040023#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/personality.h>
25#include <linux/security.h>
Mimi Zohar6146f0d2009-02-04 09:06:57 -050026#include <linux/ima.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/syscalls.h>
28#include <linux/mount.h>
29#include <linux/audit.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080030#include <linux/capability.h>
Trond Myklebust834f2a42005-10-18 14:20:16 -070031#include <linux/file.h>
Ulrich Drepper5590ff02006-01-18 17:43:53 -080032#include <linux/fcntl.h>
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070033#include <linux/device_cgroup.h>
Al Viro5ad4e532009-03-29 19:50:06 -040034#include <linux/fs_struct.h>
Linus Torvaldse77819e2011-07-22 19:30:19 -070035#include <linux/posix_acl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/uaccess.h>
37
Eric Parise81e3f42009-12-04 15:47:36 -050038#include "internal.h"
Al Viroc7105362011-11-24 18:22:03 -050039#include "mount.h"
Eric Parise81e3f42009-12-04 15:47:36 -050040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041/* [Feb-1997 T. Schoebel-Theuer]
42 * Fundamental changes in the pathname lookup mechanisms (namei)
43 * were necessary because of omirr. The reason is that omirr needs
44 * to know the _real_ pathname, not the user-supplied one, in case
45 * of symlinks (and also when transname replacements occur).
46 *
47 * The new code replaces the old recursive symlink resolution with
48 * an iterative one (in case of non-nested symlink chains). It does
49 * this with calls to <fs>_follow_link().
50 * As a side effect, dir_namei(), _namei() and follow_link() are now
51 * replaced with a single function lookup_dentry() that can handle all
52 * the special cases of the former code.
53 *
54 * With the new dcache, the pathname is stored at each inode, at least as
55 * long as the refcount of the inode is positive. As a side effect, the
56 * size of the dcache depends on the inode cache and thus is dynamic.
57 *
58 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
59 * resolution to correspond with current state of the code.
60 *
61 * Note that the symlink resolution is not *completely* iterative.
62 * There is still a significant amount of tail- and mid- recursion in
63 * the algorithm. Also, note that <fs>_readlink() is not used in
64 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
65 * may return different results than <fs>_follow_link(). Many virtual
66 * filesystems (including /proc) exhibit this behavior.
67 */
68
69/* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
70 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
71 * and the name already exists in form of a symlink, try to create the new
72 * name indicated by the symlink. The old code always complained that the
73 * name already exists, due to not following the symlink even if its target
74 * is nonexistent. The new semantics affects also mknod() and link() when
Lucas De Marchi25985ed2011-03-30 22:57:33 -030075 * the name is a symlink pointing to a non-existent name.
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 *
77 * I don't know which semantics is the right one, since I have no access
78 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
79 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
80 * "old" one. Personally, I think the new semantics is much more logical.
81 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
82 * file does succeed in both HP-UX and SunOs, but not in Solaris
83 * and in the old Linux semantics.
84 */
85
86/* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
87 * semantics. See the comments in "open_namei" and "do_link" below.
88 *
89 * [10-Sep-98 Alan Modra] Another symlink change.
90 */
91
92/* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
93 * inside the path - always follow.
94 * in the last component in creation/removal/renaming - never follow.
95 * if LOOKUP_FOLLOW passed - follow.
96 * if the pathname has trailing slashes - follow.
97 * otherwise - don't follow.
98 * (applied in that order).
99 *
100 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
101 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
102 * During the 2.4 we need to fix the userland stuff depending on it -
103 * hopefully we will be able to get rid of that wart in 2.5. So far only
104 * XEmacs seems to be relying on it...
105 */
106/*
107 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
Arjan van de Vena11f3a02006-03-23 03:00:33 -0800108 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 * any extra contention...
110 */
111
112/* In order to reduce some races, while at the same time doing additional
113 * checking and hopefully speeding things up, we copy filenames to the
114 * kernel data space before using them..
115 *
116 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
117 * PATH_MAX includes the nul terminator --RR.
118 */
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +0100119static char *getname_flags(const char __user *filename, int flags, int *empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
Linus Torvalds256a73d2012-04-28 14:38:32 -0700121 char *result = __getname(), *err;
122 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Linus Torvalds256a73d2012-04-28 14:38:32 -0700124 if (unlikely(!result))
Eric Paris4043cde2012-01-03 14:23:08 -0500125 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Linus Torvalds256a73d2012-04-28 14:38:32 -0700127 len = strncpy_from_user(result, filename, PATH_MAX);
128 err = ERR_PTR(len);
129 if (unlikely(len < 0))
130 goto error;
131
132 /* The empty path is special. */
133 if (unlikely(!len)) {
134 if (empty)
Eric Paris4043cde2012-01-03 14:23:08 -0500135 *empty = 1;
Linus Torvalds256a73d2012-04-28 14:38:32 -0700136 err = ERR_PTR(-ENOENT);
137 if (!(flags & LOOKUP_EMPTY))
138 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 }
Linus Torvalds256a73d2012-04-28 14:38:32 -0700140
141 err = ERR_PTR(-ENAMETOOLONG);
142 if (likely(len < PATH_MAX)) {
143 audit_getname(result);
144 return result;
145 }
146
147error:
148 __putname(result);
149 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
Al Virof52e0c12011-03-14 18:56:51 -0400152char *getname(const char __user * filename)
153{
Linus Torvaldsf7493e52012-03-22 16:10:40 -0700154 return getname_flags(filename, 0, NULL);
Al Virof52e0c12011-03-14 18:56:51 -0400155}
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157#ifdef CONFIG_AUDITSYSCALL
158void putname(const char *name)
159{
Al Viro5ac3a9c2006-07-16 06:38:45 -0400160 if (unlikely(!audit_dummy_context()))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 audit_putname(name);
162 else
163 __putname(name);
164}
165EXPORT_SYMBOL(putname);
166#endif
167
Linus Torvaldse77819e2011-07-22 19:30:19 -0700168static int check_acl(struct inode *inode, int mask)
169{
Linus Torvalds84635d62011-07-25 22:47:03 -0700170#ifdef CONFIG_FS_POSIX_ACL
Linus Torvaldse77819e2011-07-22 19:30:19 -0700171 struct posix_acl *acl;
172
Linus Torvaldse77819e2011-07-22 19:30:19 -0700173 if (mask & MAY_NOT_BLOCK) {
Al Viro35678662011-08-02 21:32:13 -0400174 acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
175 if (!acl)
Linus Torvaldse77819e2011-07-22 19:30:19 -0700176 return -EAGAIN;
Al Viro35678662011-08-02 21:32:13 -0400177 /* no ->get_acl() calls in RCU mode... */
178 if (acl == ACL_NOT_CACHED)
179 return -ECHILD;
Ari Savolainen206b1d02011-08-06 19:43:07 +0300180 return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
Linus Torvaldse77819e2011-07-22 19:30:19 -0700181 }
182
183 acl = get_cached_acl(inode, ACL_TYPE_ACCESS);
184
185 /*
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200186 * A filesystem can force a ACL callback by just never filling the
187 * ACL cache. But normally you'd fill the cache either at inode
188 * instantiation time, or on the first ->get_acl call.
Linus Torvaldse77819e2011-07-22 19:30:19 -0700189 *
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200190 * If the filesystem doesn't have a get_acl() function at all, we'll
191 * just create the negative cache entry.
Linus Torvaldse77819e2011-07-22 19:30:19 -0700192 */
193 if (acl == ACL_NOT_CACHED) {
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200194 if (inode->i_op->get_acl) {
195 acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS);
196 if (IS_ERR(acl))
197 return PTR_ERR(acl);
198 } else {
199 set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
200 return -EAGAIN;
201 }
Linus Torvaldse77819e2011-07-22 19:30:19 -0700202 }
203
204 if (acl) {
205 int error = posix_acl_permission(inode, acl, mask);
206 posix_acl_release(acl);
207 return error;
208 }
Linus Torvalds84635d62011-07-25 22:47:03 -0700209#endif
Linus Torvaldse77819e2011-07-22 19:30:19 -0700210
211 return -EAGAIN;
212}
213
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700214/*
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530215 * This does the basic permission checking
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700216 */
Al Viro7e401452011-06-20 19:12:17 -0400217static int acl_permission_check(struct inode *inode, int mask)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700218{
Linus Torvalds26cf46b2011-05-13 11:51:01 -0700219 unsigned int mode = inode->i_mode;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700220
Serge E. Hallyne795b712011-03-23 16:43:25 -0700221 if (current_user_ns() != inode_userns(inode))
222 goto other_perms;
223
Linus Torvalds14067ff2011-07-25 19:55:52 -0700224 if (likely(current_fsuid() == inode->i_uid))
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700225 mode >>= 6;
226 else {
Linus Torvaldse77819e2011-07-22 19:30:19 -0700227 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
Al Viro7e401452011-06-20 19:12:17 -0400228 int error = check_acl(inode, mask);
Nick Pigginb74c79e2011-01-07 17:49:58 +1100229 if (error != -EAGAIN)
230 return error;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700231 }
232
233 if (in_group_p(inode->i_gid))
234 mode >>= 3;
235 }
236
Serge E. Hallyne795b712011-03-23 16:43:25 -0700237other_perms:
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700238 /*
239 * If the DACs are ok we don't need any capability check.
240 */
Al Viro9c2c7032011-06-20 19:06:22 -0400241 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700242 return 0;
243 return -EACCES;
244}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246/**
Nick Pigginb74c79e2011-01-07 17:49:58 +1100247 * generic_permission - check for access rights on a Posix-like filesystem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 * @inode: inode to check access rights for
Andreas Gruenbacher8fd90c82011-10-23 23:13:30 +0530249 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 *
251 * Used to check for read/write/execute permissions on a file.
252 * We use "fsuid" for this, letting us set arbitrary permissions
253 * for filesystem access without changing the "normal" uids which
Nick Pigginb74c79e2011-01-07 17:49:58 +1100254 * are used for other things.
255 *
256 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
257 * request cannot be satisfied (eg. requires blocking or too much complexity).
258 * It would then be called again in ref-walk mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 */
Al Viro2830ba72011-06-20 19:16:29 -0400260int generic_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700262 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 /*
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530265 * Do the basic permission checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 */
Al Viro7e401452011-06-20 19:12:17 -0400267 ret = acl_permission_check(inode, mask);
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700268 if (ret != -EACCES)
269 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Al Virod594e7e2011-06-20 19:55:42 -0400271 if (S_ISDIR(inode->i_mode)) {
272 /* DACs are overridable for directories */
273 if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
274 return 0;
275 if (!(mask & MAY_WRITE))
276 if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
277 return 0;
278 return -EACCES;
279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 /*
281 * Read/write DACs are always overridable.
Al Virod594e7e2011-06-20 19:55:42 -0400282 * Executable DACs are overridable when there is
283 * at least one exec bit set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 */
Al Virod594e7e2011-06-20 19:55:42 -0400285 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
Serge E. Hallyne795b712011-03-23 16:43:25 -0700286 if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return 0;
288
289 /*
290 * Searching includes executable on directories, else just read.
291 */
Serge E. Hallyn7ea66002009-12-29 14:50:19 -0600292 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
Al Virod594e7e2011-06-20 19:55:42 -0400293 if (mask == MAY_READ)
Serge E. Hallyne795b712011-03-23 16:43:25 -0700294 if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return 0;
296
297 return -EACCES;
298}
299
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700300/*
301 * We _really_ want to just do "generic_permission()" without
302 * even looking at the inode->i_op values. So we keep a cache
303 * flag in inode->i_opflags, that says "this has not special
304 * permission function, use the fast case".
305 */
Daniel Rosenberg17285112016-10-26 16:27:45 -0700306static inline int do_inode_permission(struct vfsmount *mnt, struct inode *inode, int mask)
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700307{
308 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
Daniel Rosenberg17285112016-10-26 16:27:45 -0700309 if (likely(mnt && inode->i_op->permission2))
310 return inode->i_op->permission2(mnt, inode, mask);
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700311 if (likely(inode->i_op->permission))
312 return inode->i_op->permission(inode, mask);
313
314 /* This gets set once for the inode lifetime */
315 spin_lock(&inode->i_lock);
316 inode->i_opflags |= IOP_FASTPERM;
317 spin_unlock(&inode->i_lock);
318 }
319 return generic_permission(inode, mask);
320}
321
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200322/**
323 * inode_permission - check for access rights to a given inode
324 * @inode: inode to check permission on
Andreas Gruenbacher8fd90c82011-10-23 23:13:30 +0530325 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200326 *
327 * Used to check for read/write/execute permissions on an inode.
328 * We use "fsuid" for this, letting us set arbitrary permissions
329 * for filesystem access without changing the "normal" uids which
330 * are used for other things.
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530331 *
332 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200333 */
Daniel Rosenberg17285112016-10-26 16:27:45 -0700334int inode_permission2(struct vfsmount *mnt, struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Al Viroe6305c42008-07-15 21:03:57 -0400336 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700338 if (unlikely(mask & MAY_WRITE)) {
Miklos Szeredi22590e42007-10-16 23:27:08 -0700339 umode_t mode = inode->i_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 /*
342 * Nobody gets write access to a read-only fs.
343 */
344 if (IS_RDONLY(inode) &&
345 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
346 return -EROFS;
347
348 /*
349 * Nobody gets write access to an immutable file.
350 */
351 if (IS_IMMUTABLE(inode))
352 return -EACCES;
353 }
354
Daniel Rosenberg17285112016-10-26 16:27:45 -0700355 retval = do_inode_permission(mnt, inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (retval)
357 return retval;
358
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700359 retval = devcgroup_inode_permission(inode, mask);
360 if (retval)
361 return retval;
362
Eric Parisd09ca732010-07-23 11:43:57 -0400363 return security_inode_permission(inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
Daniel Rosenberg17285112016-10-26 16:27:45 -0700365EXPORT_SYMBOL(inode_permission2);
366
367int inode_permission(struct inode *inode, int mask)
368{
369 return inode_permission2(NULL, inode, mask);
370}
371EXPORT_SYMBOL(inode_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Al Virof4d6ff82011-06-19 13:14:21 -0400373/**
Jan Blunck5dd784d2008-02-14 19:34:38 -0800374 * path_get - get a reference to a path
375 * @path: path to get the reference to
376 *
377 * Given a path increment the reference count to the dentry and the vfsmount.
378 */
379void path_get(struct path *path)
380{
381 mntget(path->mnt);
382 dget(path->dentry);
383}
384EXPORT_SYMBOL(path_get);
385
386/**
Jan Blunck1d957f92008-02-14 19:34:35 -0800387 * path_put - put a reference to a path
388 * @path: path to put the reference to
389 *
390 * Given a path decrement the reference count to the dentry and the vfsmount.
391 */
392void path_put(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
Jan Blunck1d957f92008-02-14 19:34:35 -0800394 dput(path->dentry);
395 mntput(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
Jan Blunck1d957f92008-02-14 19:34:35 -0800397EXPORT_SYMBOL(path_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Eric W. Biedermanbf2e9fa2015-08-15 20:27:13 -0500399/**
400 * path_connected - Verify that a path->dentry is below path->mnt.mnt_root
401 * @path: nameidate to verify
402 *
403 * Rename can sometimes move a file or directory outside of a bind
404 * mount, path_connected allows those cases to be detected.
405 */
406static bool path_connected(const struct path *path)
407{
408 struct vfsmount *mnt = path->mnt;
409
410 /* Only bind mounts can have disconnected paths */
411 if (mnt->mnt_root == mnt->mnt_sb->s_root)
412 return true;
413
414 return is_subdir(path->dentry, mnt->mnt_root);
415}
416
Al Viro19660af2011-03-25 10:32:48 -0400417/*
Nick Piggin31e6b012011-01-07 17:49:52 +1100418 * Path walking has 2 modes, rcu-walk and ref-walk (see
Al Viro19660af2011-03-25 10:32:48 -0400419 * Documentation/filesystems/path-lookup.txt). In situations when we can't
420 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
421 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
422 * mode. Refcounts are grabbed at the last known good point before rcu-walk
423 * got stuck, so ref-walk may continue from there. If this is not successful
424 * (eg. a seqcount has changed), then failure is returned and it's up to caller
425 * to restart the path walk from the beginning in ref-walk mode.
Nick Piggin31e6b012011-01-07 17:49:52 +1100426 */
Nick Piggin31e6b012011-01-07 17:49:52 +1100427
428/**
Al Viro19660af2011-03-25 10:32:48 -0400429 * unlazy_walk - try to switch to ref-walk mode.
430 * @nd: nameidata pathwalk data
431 * @dentry: child of nd->path.dentry or NULL
Randy Dunlap39191622011-01-08 19:36:21 -0800432 * Returns: 0 on success, -ECHILD on failure
Nick Piggin31e6b012011-01-07 17:49:52 +1100433 *
Al Viro19660af2011-03-25 10:32:48 -0400434 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
435 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
436 * @nd or NULL. Must be called from rcu-walk context.
Nick Piggin31e6b012011-01-07 17:49:52 +1100437 */
Al Viro19660af2011-03-25 10:32:48 -0400438static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
Nick Piggin31e6b012011-01-07 17:49:52 +1100439{
440 struct fs_struct *fs = current->fs;
441 struct dentry *parent = nd->path.dentry;
Al Viro5b6ca022011-03-09 23:04:47 -0500442 int want_root = 0;
Nick Piggin31e6b012011-01-07 17:49:52 +1100443
444 BUG_ON(!(nd->flags & LOOKUP_RCU));
Al Viro5b6ca022011-03-09 23:04:47 -0500445 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
446 want_root = 1;
Nick Piggin31e6b012011-01-07 17:49:52 +1100447 spin_lock(&fs->lock);
448 if (nd->root.mnt != fs->root.mnt ||
449 nd->root.dentry != fs->root.dentry)
450 goto err_root;
451 }
452 spin_lock(&parent->d_lock);
Al Viro19660af2011-03-25 10:32:48 -0400453 if (!dentry) {
454 if (!__d_rcu_to_refcount(parent, nd->seq))
455 goto err_parent;
456 BUG_ON(nd->inode != parent->d_inode);
457 } else {
Al Viro94c0d4e2011-07-12 21:40:23 -0400458 if (dentry->d_parent != parent)
459 goto err_parent;
Al Viro19660af2011-03-25 10:32:48 -0400460 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
461 if (!__d_rcu_to_refcount(dentry, nd->seq))
462 goto err_child;
463 /*
464 * If the sequence check on the child dentry passed, then
465 * the child has not been removed from its parent. This
466 * means the parent dentry must be valid and able to take
467 * a reference at this point.
468 */
469 BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
470 BUG_ON(!parent->d_count);
471 parent->d_count++;
472 spin_unlock(&dentry->d_lock);
473 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100474 spin_unlock(&parent->d_lock);
Al Viro5b6ca022011-03-09 23:04:47 -0500475 if (want_root) {
Nick Piggin31e6b012011-01-07 17:49:52 +1100476 path_get(&nd->root);
477 spin_unlock(&fs->lock);
478 }
479 mntget(nd->path.mnt);
480
481 rcu_read_unlock();
Andi Kleenf0769502012-05-08 13:32:02 +0930482 br_read_unlock(&vfsmount_lock);
Nick Piggin31e6b012011-01-07 17:49:52 +1100483 nd->flags &= ~LOOKUP_RCU;
484 return 0;
Al Viro19660af2011-03-25 10:32:48 -0400485
486err_child:
Nick Piggin31e6b012011-01-07 17:49:52 +1100487 spin_unlock(&dentry->d_lock);
Al Viro19660af2011-03-25 10:32:48 -0400488err_parent:
Nick Piggin31e6b012011-01-07 17:49:52 +1100489 spin_unlock(&parent->d_lock);
490err_root:
Al Viro5b6ca022011-03-09 23:04:47 -0500491 if (want_root)
Nick Piggin31e6b012011-01-07 17:49:52 +1100492 spin_unlock(&fs->lock);
493 return -ECHILD;
494}
495
Al Virof60aef72011-02-15 01:35:28 -0500496static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
Nick Piggin34286d62011-01-07 17:49:57 +1100497{
Al Virof60aef72011-02-15 01:35:28 -0500498 return dentry->d_op->d_revalidate(dentry, nd);
Nick Piggin34286d62011-01-07 17:49:57 +1100499}
500
Al Viro9f1fafe2011-03-25 11:00:12 -0400501/**
502 * complete_walk - successful completion of path walk
503 * @nd: pointer nameidata
Jeff Layton39159de2009-12-07 12:01:50 -0500504 *
Al Viro9f1fafe2011-03-25 11:00:12 -0400505 * If we had been in RCU mode, drop out of it and legitimize nd->path.
506 * Revalidate the final result, unless we'd already done that during
507 * the path walk or the filesystem doesn't ask for it. Return 0 on
508 * success, -error on failure. In case of failure caller does not
509 * need to drop nd->path.
Jeff Layton39159de2009-12-07 12:01:50 -0500510 */
Al Viro9f1fafe2011-03-25 11:00:12 -0400511static int complete_walk(struct nameidata *nd)
Jeff Layton39159de2009-12-07 12:01:50 -0500512{
Al Viro16c2cd72011-02-22 15:50:10 -0500513 struct dentry *dentry = nd->path.dentry;
Jeff Layton39159de2009-12-07 12:01:50 -0500514 int status;
Jeff Layton39159de2009-12-07 12:01:50 -0500515
Al Viro9f1fafe2011-03-25 11:00:12 -0400516 if (nd->flags & LOOKUP_RCU) {
517 nd->flags &= ~LOOKUP_RCU;
518 if (!(nd->flags & LOOKUP_ROOT))
519 nd->root.mnt = NULL;
520 spin_lock(&dentry->d_lock);
521 if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
522 spin_unlock(&dentry->d_lock);
523 rcu_read_unlock();
Andi Kleenf0769502012-05-08 13:32:02 +0930524 br_read_unlock(&vfsmount_lock);
Al Viro9f1fafe2011-03-25 11:00:12 -0400525 return -ECHILD;
526 }
527 BUG_ON(nd->inode != dentry->d_inode);
528 spin_unlock(&dentry->d_lock);
529 mntget(nd->path.mnt);
530 rcu_read_unlock();
Andi Kleenf0769502012-05-08 13:32:02 +0930531 br_read_unlock(&vfsmount_lock);
Al Viro9f1fafe2011-03-25 11:00:12 -0400532 }
533
Al Viro16c2cd72011-02-22 15:50:10 -0500534 if (likely(!(nd->flags & LOOKUP_JUMPED)))
Jeff Layton39159de2009-12-07 12:01:50 -0500535 return 0;
536
Al Viro16c2cd72011-02-22 15:50:10 -0500537 if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
538 return 0;
539
540 if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
541 return 0;
542
543 /* Note: we do not d_invalidate() */
Nick Piggin34286d62011-01-07 17:49:57 +1100544 status = d_revalidate(dentry, nd);
Jeff Layton39159de2009-12-07 12:01:50 -0500545 if (status > 0)
546 return 0;
547
Al Viro16c2cd72011-02-22 15:50:10 -0500548 if (!status)
Jeff Layton39159de2009-12-07 12:01:50 -0500549 status = -ESTALE;
Al Viro16c2cd72011-02-22 15:50:10 -0500550
Al Viro9f1fafe2011-03-25 11:00:12 -0400551 path_put(&nd->path);
Jeff Layton39159de2009-12-07 12:01:50 -0500552 return status;
553}
554
Al Viro2a737872009-04-07 11:49:53 -0400555static __always_inline void set_root(struct nameidata *nd)
556{
Al Viro589a49f2014-09-13 21:55:46 -0400557 get_fs_root(current->fs, &nd->root);
Al Viro2a737872009-04-07 11:49:53 -0400558}
559
Al Viro6de88d72009-08-09 01:41:57 +0400560static int link_path_walk(const char *, struct nameidata *);
561
Al Viro589a49f2014-09-13 21:55:46 -0400562static __always_inline unsigned set_root_rcu(struct nameidata *nd)
Nick Piggin31e6b012011-01-07 17:49:52 +1100563{
Al Viro589a49f2014-09-13 21:55:46 -0400564 struct fs_struct *fs = current->fs;
565 unsigned seq, res;
Nick Pigginc28cc362011-01-07 17:49:53 +1100566
Al Viro589a49f2014-09-13 21:55:46 -0400567 do {
568 seq = read_seqcount_begin(&fs->seq);
569 nd->root = fs->root;
570 res = __read_seqcount_begin(&nd->root.dentry->d_seq);
571 } while (read_seqcount_retry(&fs->seq, seq));
572 return res;
Nick Piggin31e6b012011-01-07 17:49:52 +1100573}
574
Arjan van de Venf1662352006-01-14 13:21:31 -0800575static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
Nick Piggin31e6b012011-01-07 17:49:52 +1100577 int ret;
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (IS_ERR(link))
580 goto fail;
581
582 if (*link == '/') {
Al Viro92214262014-09-13 21:55:46 -0400583 if (!nd->root.mnt)
584 set_root(nd);
Jan Blunck1d957f92008-02-14 19:34:35 -0800585 path_put(&nd->path);
Al Viro2a737872009-04-07 11:49:53 -0400586 nd->path = nd->root;
587 path_get(&nd->root);
Al Viro16c2cd72011-02-22 15:50:10 -0500588 nd->flags |= LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100590 nd->inode = nd->path.dentry->d_inode;
Christoph Hellwigb4091d52008-11-05 15:07:21 +0100591
Nick Piggin31e6b012011-01-07 17:49:52 +1100592 ret = link_path_walk(link, nd);
593 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594fail:
Jan Blunck1d957f92008-02-14 19:34:35 -0800595 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 return PTR_ERR(link);
597}
598
Jan Blunck1d957f92008-02-14 19:34:35 -0800599static void path_put_conditional(struct path *path, struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700600{
601 dput(path->dentry);
Jan Blunck4ac91372008-02-14 19:34:32 -0800602 if (path->mnt != nd->path.mnt)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700603 mntput(path->mnt);
604}
605
Nick Piggin7b9337a2011-01-14 08:42:43 +0000606static inline void path_to_nameidata(const struct path *path,
607 struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700608{
Nick Piggin31e6b012011-01-07 17:49:52 +1100609 if (!(nd->flags & LOOKUP_RCU)) {
610 dput(nd->path.dentry);
611 if (nd->path.mnt != path->mnt)
612 mntput(nd->path.mnt);
Huang Shijie9a229682010-04-02 17:37:13 +0800613 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100614 nd->path.mnt = path->mnt;
Jan Blunck4ac91372008-02-14 19:34:32 -0800615 nd->path.dentry = path->dentry;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700616}
617
Al Viro574197e2011-03-14 22:20:34 -0400618static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
619{
620 struct inode *inode = link->dentry->d_inode;
Al Viro2c4d7392012-06-10 04:15:17 -0400621 if (inode->i_op->put_link)
Al Viro574197e2011-03-14 22:20:34 -0400622 inode->i_op->put_link(link->dentry, nd, cookie);
623 path_put(link);
624}
625
Al Virodef4af32009-12-26 08:37:05 -0500626static __always_inline int
Al Viro574197e2011-03-14 22:20:34 -0400627follow_link(struct path *link, struct nameidata *nd, void **p)
Ian Kent051d3812006-03-27 01:14:53 -0800628{
Nick Piggin7b9337a2011-01-14 08:42:43 +0000629 struct dentry *dentry = link->dentry;
Al Viro2c4d7392012-06-10 04:15:17 -0400630 int error;
631 char *s;
Ian Kent051d3812006-03-27 01:14:53 -0800632
Al Viro844a3912011-02-15 00:38:26 -0500633 BUG_ON(nd->flags & LOOKUP_RCU);
634
Al Viro0e794582011-03-16 02:45:02 -0400635 if (link->mnt == nd->path.mnt)
636 mntget(link->mnt);
637
Al Viro2c4d7392012-06-10 04:15:17 -0400638 error = -ELOOP;
639 if (unlikely(current->total_link_count >= 40))
640 goto out_put_nd_path;
641
Al Viro574197e2011-03-14 22:20:34 -0400642 cond_resched();
643 current->total_link_count++;
644
Al Viro68ac1232012-03-15 08:21:57 -0400645 touch_atime(link);
Ian Kent051d3812006-03-27 01:14:53 -0800646 nd_set_link(nd, NULL);
647
Al Viro36f3b4f2011-02-22 21:24:38 -0500648 error = security_inode_follow_link(link->dentry, nd);
Al Viro2c4d7392012-06-10 04:15:17 -0400649 if (error)
650 goto out_put_nd_path;
Al Viro36f3b4f2011-02-22 21:24:38 -0500651
Al Viro86acdca12009-12-22 23:45:11 -0500652 nd->last_type = LAST_BIND;
Al Virodef4af32009-12-26 08:37:05 -0500653 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
654 error = PTR_ERR(*p);
Al Viro2c4d7392012-06-10 04:15:17 -0400655 if (IS_ERR(*p))
656 goto out_put_link;
657
658 error = 0;
659 s = nd_get_link(nd);
660 if (s) {
661 error = __vfs_follow_link(nd, s);
662 } else if (nd->last_type == LAST_BIND) {
663 nd->flags |= LOOKUP_JUMPED;
664 nd->inode = nd->path.dentry->d_inode;
665 if (nd->inode->i_op->follow_link) {
666 /* stepped on a _really_ weird one */
667 path_put(&nd->path);
668 error = -ELOOP;
Al Virobcda7652011-03-13 16:42:14 -0400669 }
Ian Kent051d3812006-03-27 01:14:53 -0800670 }
Al Viro2c4d7392012-06-10 04:15:17 -0400671 if (unlikely(error))
672 put_link(nd, link, *p);
673
674 return error;
675
676out_put_nd_path:
677 path_put(&nd->path);
678out_put_link:
679 path_put(link);
Ian Kent051d3812006-03-27 01:14:53 -0800680 return error;
681}
682
Nick Piggin31e6b012011-01-07 17:49:52 +1100683static int follow_up_rcu(struct path *path)
684{
Al Viro0714a532011-11-24 22:19:58 -0500685 struct mount *mnt = real_mount(path->mnt);
686 struct mount *parent;
Nick Piggin31e6b012011-01-07 17:49:52 +1100687 struct dentry *mountpoint;
688
Al Viro0714a532011-11-24 22:19:58 -0500689 parent = mnt->mnt_parent;
690 if (&parent->mnt == path->mnt)
Nick Piggin31e6b012011-01-07 17:49:52 +1100691 return 0;
Al Viroa73324d2011-11-24 22:25:07 -0500692 mountpoint = mnt->mnt_mountpoint;
Nick Piggin31e6b012011-01-07 17:49:52 +1100693 path->dentry = mountpoint;
Al Viro0714a532011-11-24 22:19:58 -0500694 path->mnt = &parent->mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +1100695 return 1;
696}
697
David Howells105a0cd2012-06-25 12:55:28 +0100698/*
699 * follow_up - Find the mountpoint of path's vfsmount
700 *
701 * Given a path, find the mountpoint of its source file system.
702 * Replace @path with the path of the mountpoint in the parent mount.
703 * Up is towards /.
704 *
705 * Return 1 if we went up a level and 0 if we were already at the
706 * root.
707 */
Al Virobab77eb2009-04-18 03:26:48 -0400708int follow_up(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Al Viro0714a532011-11-24 22:19:58 -0500710 struct mount *mnt = real_mount(path->mnt);
711 struct mount *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 struct dentry *mountpoint;
Nick Piggin99b7db72010-08-18 04:37:39 +1000713
Andi Kleenf0769502012-05-08 13:32:02 +0930714 br_read_lock(&vfsmount_lock);
Al Viro0714a532011-11-24 22:19:58 -0500715 parent = mnt->mnt_parent;
716 if (&parent->mnt == path->mnt) {
Andi Kleenf0769502012-05-08 13:32:02 +0930717 br_read_unlock(&vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return 0;
719 }
Al Viro0714a532011-11-24 22:19:58 -0500720 mntget(&parent->mnt);
Al Viroa73324d2011-11-24 22:25:07 -0500721 mountpoint = dget(mnt->mnt_mountpoint);
Andi Kleenf0769502012-05-08 13:32:02 +0930722 br_read_unlock(&vfsmount_lock);
Al Virobab77eb2009-04-18 03:26:48 -0400723 dput(path->dentry);
724 path->dentry = mountpoint;
725 mntput(path->mnt);
Al Viro0714a532011-11-24 22:19:58 -0500726 path->mnt = &parent->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 return 1;
728}
729
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100730/*
David Howells9875cf82011-01-14 18:45:21 +0000731 * Perform an automount
732 * - return -EISDIR to tell follow_managed() to stop and return the path we
733 * were called with.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 */
David Howells9875cf82011-01-14 18:45:21 +0000735static int follow_automount(struct path *path, unsigned flags,
736 bool *need_mntput)
Nick Piggin31e6b012011-01-07 17:49:52 +1100737{
David Howells9875cf82011-01-14 18:45:21 +0000738 struct vfsmount *mnt;
David Howellsea5b7782011-01-14 19:10:03 +0000739 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +1100740
David Howells9875cf82011-01-14 18:45:21 +0000741 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
742 return -EREMOTE;
Al Viro463ffb22005-06-06 13:36:05 -0700743
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200744 /* We don't want to mount if someone's just doing a stat -
745 * unless they're stat'ing a directory and appended a '/' to
746 * the name.
747 *
748 * We do, however, want to mount if someone wants to open or
749 * create a file of any type under the mountpoint, wants to
750 * traverse through the mountpoint or wants to open the
751 * mounted directory. Also, autofs may mark negative dentries
752 * as being automount points. These will need the attentions
753 * of the daemon to instantiate them before they can be used.
David Howells9875cf82011-01-14 18:45:21 +0000754 */
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200755 if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
Linus Torvaldsd94c1772011-09-26 17:44:55 -0700756 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200757 path->dentry->d_inode)
758 return -EISDIR;
759
David Howells9875cf82011-01-14 18:45:21 +0000760 current->total_link_count++;
761 if (current->total_link_count >= 40)
762 return -ELOOP;
763
764 mnt = path->dentry->d_op->d_automount(path);
765 if (IS_ERR(mnt)) {
766 /*
767 * The filesystem is allowed to return -EISDIR here to indicate
768 * it doesn't want to automount. For instance, autofs would do
769 * this so that its userspace daemon can mount on this dentry.
770 *
771 * However, we can only permit this if it's a terminal point in
772 * the path being looked up; if it wasn't then the remainder of
773 * the path is inaccessible and we should say so.
774 */
Al Viro49084c32011-06-25 21:59:52 -0400775 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
David Howells9875cf82011-01-14 18:45:21 +0000776 return -EREMOTE;
777 return PTR_ERR(mnt);
778 }
David Howellsea5b7782011-01-14 19:10:03 +0000779
David Howells9875cf82011-01-14 18:45:21 +0000780 if (!mnt) /* mount collision */
781 return 0;
782
Al Viro8aef1882011-06-16 15:10:06 +0100783 if (!*need_mntput) {
784 /* lock_mount() may release path->mnt on error */
785 mntget(path->mnt);
786 *need_mntput = true;
787 }
Al Viro19a167a2011-01-17 01:35:23 -0500788 err = finish_automount(mnt, path);
David Howellsea5b7782011-01-14 19:10:03 +0000789
David Howellsea5b7782011-01-14 19:10:03 +0000790 switch (err) {
791 case -EBUSY:
792 /* Someone else made a mount here whilst we were busy */
Al Viro19a167a2011-01-17 01:35:23 -0500793 return 0;
David Howellsea5b7782011-01-14 19:10:03 +0000794 case 0:
Al Viro8aef1882011-06-16 15:10:06 +0100795 path_put(path);
David Howellsea5b7782011-01-14 19:10:03 +0000796 path->mnt = mnt;
797 path->dentry = dget(mnt->mnt_root);
David Howellsea5b7782011-01-14 19:10:03 +0000798 return 0;
Al Viro19a167a2011-01-17 01:35:23 -0500799 default:
800 return err;
David Howellsea5b7782011-01-14 19:10:03 +0000801 }
Al Viro19a167a2011-01-17 01:35:23 -0500802
David Howells9875cf82011-01-14 18:45:21 +0000803}
804
805/*
806 * Handle a dentry that is managed in some way.
David Howellscc53ce52011-01-14 18:45:26 +0000807 * - Flagged for transit management (autofs)
David Howells9875cf82011-01-14 18:45:21 +0000808 * - Flagged as mountpoint
809 * - Flagged as automount point
810 *
811 * This may only be called in refwalk mode.
812 *
813 * Serialization is taken care of in namespace.c
814 */
815static int follow_managed(struct path *path, unsigned flags)
816{
Al Viro8aef1882011-06-16 15:10:06 +0100817 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
David Howells9875cf82011-01-14 18:45:21 +0000818 unsigned managed;
819 bool need_mntput = false;
Al Viro8aef1882011-06-16 15:10:06 +0100820 int ret = 0;
David Howells9875cf82011-01-14 18:45:21 +0000821
822 /* Given that we're not holding a lock here, we retain the value in a
823 * local variable for each dentry as we look at it so that we don't see
824 * the components of that value change under us */
825 while (managed = ACCESS_ONCE(path->dentry->d_flags),
826 managed &= DCACHE_MANAGED_DENTRY,
827 unlikely(managed != 0)) {
David Howellscc53ce52011-01-14 18:45:26 +0000828 /* Allow the filesystem to manage the transit without i_mutex
829 * being held. */
830 if (managed & DCACHE_MANAGE_TRANSIT) {
831 BUG_ON(!path->dentry->d_op);
832 BUG_ON(!path->dentry->d_op->d_manage);
Al Viro1aed3e42011-03-18 09:09:02 -0400833 ret = path->dentry->d_op->d_manage(path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +0000834 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +0100835 break;
David Howellscc53ce52011-01-14 18:45:26 +0000836 }
837
David Howells9875cf82011-01-14 18:45:21 +0000838 /* Transit to a mounted filesystem. */
839 if (managed & DCACHE_MOUNTED) {
840 struct vfsmount *mounted = lookup_mnt(path);
841 if (mounted) {
842 dput(path->dentry);
843 if (need_mntput)
844 mntput(path->mnt);
845 path->mnt = mounted;
846 path->dentry = dget(mounted->mnt_root);
847 need_mntput = true;
848 continue;
849 }
850
851 /* Something is mounted on this dentry in another
852 * namespace and/or whatever was mounted there in this
853 * namespace got unmounted before we managed to get the
854 * vfsmount_lock */
855 }
856
857 /* Handle an automount point */
858 if (managed & DCACHE_NEED_AUTOMOUNT) {
859 ret = follow_automount(path, flags, &need_mntput);
860 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +0100861 break;
David Howells9875cf82011-01-14 18:45:21 +0000862 continue;
863 }
864
865 /* We didn't change the current path point */
866 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 }
Al Viro8aef1882011-06-16 15:10:06 +0100868
869 if (need_mntput && path->mnt == mnt)
870 mntput(path->mnt);
871 if (ret == -EISDIR)
872 ret = 0;
Al Viroa3fbbde2011-11-07 21:21:26 +0000873 return ret < 0 ? ret : need_mntput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874}
875
David Howellscc53ce52011-01-14 18:45:26 +0000876int follow_down_one(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
878 struct vfsmount *mounted;
879
Al Viro1c755af2009-04-18 14:06:57 -0400880 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -0400882 dput(path->dentry);
883 mntput(path->mnt);
884 path->mnt = mounted;
885 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return 1;
887 }
888 return 0;
889}
890
Ian Kent62a73752011-03-25 01:51:02 +0800891static inline bool managed_dentry_might_block(struct dentry *dentry)
892{
893 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
894 dentry->d_op->d_manage(dentry, true) < 0);
895}
896
David Howells9875cf82011-01-14 18:45:21 +0000897/*
Al Viro287548e2011-05-27 06:50:06 -0400898 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
899 * we meet a managed dentry that would need blocking.
David Howells9875cf82011-01-14 18:45:21 +0000900 */
901static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
Al Viro287548e2011-05-27 06:50:06 -0400902 struct inode **inode)
David Howells9875cf82011-01-14 18:45:21 +0000903{
Ian Kent62a73752011-03-25 01:51:02 +0800904 for (;;) {
Al Viroc7105362011-11-24 18:22:03 -0500905 struct mount *mounted;
Ian Kent62a73752011-03-25 01:51:02 +0800906 /*
907 * Don't forget we might have a non-mountpoint managed dentry
908 * that wants to block transit.
909 */
Al Viro287548e2011-05-27 06:50:06 -0400910 if (unlikely(managed_dentry_might_block(path->dentry)))
David Howellsab909112011-01-14 18:46:51 +0000911 return false;
Ian Kent62a73752011-03-25 01:51:02 +0800912
913 if (!d_mountpoint(path->dentry))
914 break;
915
David Howells9875cf82011-01-14 18:45:21 +0000916 mounted = __lookup_mnt(path->mnt, path->dentry, 1);
917 if (!mounted)
918 break;
Al Viroc7105362011-11-24 18:22:03 -0500919 path->mnt = &mounted->mnt;
920 path->dentry = mounted->mnt.mnt_root;
Al Viroa3fbbde2011-11-07 21:21:26 +0000921 nd->flags |= LOOKUP_JUMPED;
David Howells9875cf82011-01-14 18:45:21 +0000922 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
Linus Torvalds59430262011-07-18 15:43:29 -0700923 /*
924 * Update the inode too. We don't need to re-check the
925 * dentry sequence number here after this d_inode read,
926 * because a mount-point is always pinned.
927 */
928 *inode = path->dentry->d_inode;
David Howells9875cf82011-01-14 18:45:21 +0000929 }
David Howells9875cf82011-01-14 18:45:21 +0000930 return true;
931}
932
Al Virodea39372011-05-27 06:53:39 -0400933static void follow_mount_rcu(struct nameidata *nd)
Al Viro287548e2011-05-27 06:50:06 -0400934{
Al Virodea39372011-05-27 06:53:39 -0400935 while (d_mountpoint(nd->path.dentry)) {
Al Viroc7105362011-11-24 18:22:03 -0500936 struct mount *mounted;
Al Virodea39372011-05-27 06:53:39 -0400937 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
Al Viro287548e2011-05-27 06:50:06 -0400938 if (!mounted)
939 break;
Al Viroc7105362011-11-24 18:22:03 -0500940 nd->path.mnt = &mounted->mnt;
941 nd->path.dentry = mounted->mnt.mnt_root;
Al Virodea39372011-05-27 06:53:39 -0400942 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro287548e2011-05-27 06:50:06 -0400943 }
944}
945
Nick Piggin31e6b012011-01-07 17:49:52 +1100946static int follow_dotdot_rcu(struct nameidata *nd)
947{
Al Viro589a49f2014-09-13 21:55:46 -0400948 if (!nd->root.mnt)
949 set_root_rcu(nd);
Nick Piggin31e6b012011-01-07 17:49:52 +1100950
David Howells9875cf82011-01-14 18:45:21 +0000951 while (1) {
Nick Piggin31e6b012011-01-07 17:49:52 +1100952 if (nd->path.dentry == nd->root.dentry &&
953 nd->path.mnt == nd->root.mnt) {
954 break;
955 }
956 if (nd->path.dentry != nd->path.mnt->mnt_root) {
957 struct dentry *old = nd->path.dentry;
958 struct dentry *parent = old->d_parent;
959 unsigned seq;
960
961 seq = read_seqcount_begin(&parent->d_seq);
962 if (read_seqcount_retry(&old->d_seq, nd->seq))
Al Viroef7562d2011-03-04 14:35:59 -0500963 goto failed;
Nick Piggin31e6b012011-01-07 17:49:52 +1100964 nd->path.dentry = parent;
965 nd->seq = seq;
Eric W. Biedermanbf2e9fa2015-08-15 20:27:13 -0500966 if (unlikely(!path_connected(&nd->path)))
967 goto failed;
Nick Piggin31e6b012011-01-07 17:49:52 +1100968 break;
969 }
970 if (!follow_up_rcu(&nd->path))
971 break;
972 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Nick Piggin31e6b012011-01-07 17:49:52 +1100973 }
Al Virodea39372011-05-27 06:53:39 -0400974 follow_mount_rcu(nd);
975 nd->inode = nd->path.dentry->d_inode;
Nick Piggin31e6b012011-01-07 17:49:52 +1100976 return 0;
Al Viroef7562d2011-03-04 14:35:59 -0500977
978failed:
979 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -0500980 if (!(nd->flags & LOOKUP_ROOT))
981 nd->root.mnt = NULL;
Al Viroef7562d2011-03-04 14:35:59 -0500982 rcu_read_unlock();
Andi Kleenf0769502012-05-08 13:32:02 +0930983 br_read_unlock(&vfsmount_lock);
Al Viroef7562d2011-03-04 14:35:59 -0500984 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +1100985}
986
David Howells9875cf82011-01-14 18:45:21 +0000987/*
David Howellscc53ce52011-01-14 18:45:26 +0000988 * Follow down to the covering mount currently visible to userspace. At each
989 * point, the filesystem owning that dentry may be queried as to whether the
990 * caller is permitted to proceed or not.
David Howellscc53ce52011-01-14 18:45:26 +0000991 */
Al Viro7cc90cc2011-03-18 09:04:20 -0400992int follow_down(struct path *path)
David Howellscc53ce52011-01-14 18:45:26 +0000993{
994 unsigned managed;
995 int ret;
996
997 while (managed = ACCESS_ONCE(path->dentry->d_flags),
998 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
999 /* Allow the filesystem to manage the transit without i_mutex
1000 * being held.
1001 *
1002 * We indicate to the filesystem if someone is trying to mount
1003 * something here. This gives autofs the chance to deny anyone
1004 * other than its daemon the right to mount on its
1005 * superstructure.
1006 *
1007 * The filesystem may sleep at this point.
1008 */
1009 if (managed & DCACHE_MANAGE_TRANSIT) {
1010 BUG_ON(!path->dentry->d_op);
1011 BUG_ON(!path->dentry->d_op->d_manage);
David Howellsab909112011-01-14 18:46:51 +00001012 ret = path->dentry->d_op->d_manage(
Al Viro1aed3e42011-03-18 09:09:02 -04001013 path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +00001014 if (ret < 0)
1015 return ret == -EISDIR ? 0 : ret;
1016 }
1017
1018 /* Transit to a mounted filesystem. */
1019 if (managed & DCACHE_MOUNTED) {
1020 struct vfsmount *mounted = lookup_mnt(path);
1021 if (!mounted)
1022 break;
1023 dput(path->dentry);
1024 mntput(path->mnt);
1025 path->mnt = mounted;
1026 path->dentry = dget(mounted->mnt_root);
1027 continue;
1028 }
1029
1030 /* Don't handle automount points here */
1031 break;
1032 }
1033 return 0;
1034}
1035
1036/*
David Howells9875cf82011-01-14 18:45:21 +00001037 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1038 */
1039static void follow_mount(struct path *path)
1040{
1041 while (d_mountpoint(path->dentry)) {
1042 struct vfsmount *mounted = lookup_mnt(path);
1043 if (!mounted)
1044 break;
1045 dput(path->dentry);
1046 mntput(path->mnt);
1047 path->mnt = mounted;
1048 path->dentry = dget(mounted->mnt_root);
1049 }
1050}
1051
Eric W. Biedermanbf2e9fa2015-08-15 20:27:13 -05001052static int follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
Al Viro589a49f2014-09-13 21:55:46 -04001054 if (!nd->root.mnt)
1055 set_root(nd);
Andreas Mohre518ddb2006-09-29 02:01:22 -07001056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 while(1) {
Jan Blunck4ac91372008-02-14 19:34:32 -08001058 struct dentry *old = nd->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Al Viro2a737872009-04-07 11:49:53 -04001060 if (nd->path.dentry == nd->root.dentry &&
1061 nd->path.mnt == nd->root.mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 break;
1063 }
Jan Blunck4ac91372008-02-14 19:34:32 -08001064 if (nd->path.dentry != nd->path.mnt->mnt_root) {
Al Viro3088dd72010-01-30 15:47:29 -05001065 /* rare case of legitimate dget_parent()... */
1066 nd->path.dentry = dget_parent(nd->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 dput(old);
Eric W. Biedermanbf2e9fa2015-08-15 20:27:13 -05001068 if (unlikely(!path_connected(&nd->path))) {
1069 path_put(&nd->path);
1070 return -ENOENT;
1071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 break;
1073 }
Al Viro3088dd72010-01-30 15:47:29 -05001074 if (!follow_up(&nd->path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 }
Al Viro79ed0222009-04-18 13:59:41 -04001077 follow_mount(&nd->path);
Nick Piggin31e6b012011-01-07 17:49:52 +11001078 nd->inode = nd->path.dentry->d_inode;
Eric W. Biedermanbf2e9fa2015-08-15 20:27:13 -05001079 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080}
1081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082/*
Miklos Szeredibad61182012-03-26 12:54:24 +02001083 * This looks up the name in dcache, possibly revalidates the old dentry and
1084 * allocates a new one if not found or not valid. In the need_lookup argument
1085 * returns whether i_op->lookup is necessary.
1086 *
1087 * dir->d_inode->i_mutex must be held
Nick Pigginbaa03892010-08-18 04:37:31 +10001088 */
Miklos Szeredibad61182012-03-26 12:54:24 +02001089static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
1090 struct nameidata *nd, bool *need_lookup)
Nick Pigginbaa03892010-08-18 04:37:31 +10001091{
Nick Pigginbaa03892010-08-18 04:37:31 +10001092 struct dentry *dentry;
Miklos Szeredibad61182012-03-26 12:54:24 +02001093 int error;
Nick Pigginbaa03892010-08-18 04:37:31 +10001094
Miklos Szeredibad61182012-03-26 12:54:24 +02001095 *need_lookup = false;
1096 dentry = d_lookup(dir, name);
1097 if (dentry) {
1098 if (d_need_lookup(dentry)) {
1099 *need_lookup = true;
1100 } else if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
1101 error = d_revalidate(dentry, nd);
1102 if (unlikely(error <= 0)) {
1103 if (error < 0) {
1104 dput(dentry);
1105 return ERR_PTR(error);
1106 } else if (!d_invalidate(dentry)) {
1107 dput(dentry);
1108 dentry = NULL;
1109 }
1110 }
1111 }
1112 }
Nick Pigginbaa03892010-08-18 04:37:31 +10001113
Miklos Szeredibad61182012-03-26 12:54:24 +02001114 if (!dentry) {
1115 dentry = d_alloc(dir, name);
1116 if (unlikely(!dentry))
1117 return ERR_PTR(-ENOMEM);
Nick Pigginbaa03892010-08-18 04:37:31 +10001118
Miklos Szeredibad61182012-03-26 12:54:24 +02001119 *need_lookup = true;
Nick Pigginbaa03892010-08-18 04:37:31 +10001120 }
1121 return dentry;
1122}
1123
1124/*
Miklos Szeredibad61182012-03-26 12:54:24 +02001125 * Call i_op->lookup on the dentry. The dentry must be negative but may be
1126 * hashed if it was pouplated with DCACHE_NEED_LOOKUP.
1127 *
1128 * dir->d_inode->i_mutex must be held
Josef Bacik44396f42011-05-31 11:58:49 -04001129 */
Miklos Szeredibad61182012-03-26 12:54:24 +02001130static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
1131 struct nameidata *nd)
Josef Bacik44396f42011-05-31 11:58:49 -04001132{
Josef Bacik44396f42011-05-31 11:58:49 -04001133 struct dentry *old;
1134
1135 /* Don't create child dentry for a dead directory. */
Miklos Szeredibad61182012-03-26 12:54:24 +02001136 if (unlikely(IS_DEADDIR(dir))) {
Miklos Szeredie188dc02012-02-03 14:25:18 +01001137 dput(dentry);
Josef Bacik44396f42011-05-31 11:58:49 -04001138 return ERR_PTR(-ENOENT);
Miklos Szeredie188dc02012-02-03 14:25:18 +01001139 }
Josef Bacik44396f42011-05-31 11:58:49 -04001140
Miklos Szeredibad61182012-03-26 12:54:24 +02001141 old = dir->i_op->lookup(dir, dentry, nd);
Josef Bacik44396f42011-05-31 11:58:49 -04001142 if (unlikely(old)) {
1143 dput(dentry);
1144 dentry = old;
1145 }
1146 return dentry;
1147}
1148
Al Viroa3255542012-03-30 14:41:51 -04001149static struct dentry *__lookup_hash(struct qstr *name,
1150 struct dentry *base, struct nameidata *nd)
1151{
Miklos Szeredibad61182012-03-26 12:54:24 +02001152 bool need_lookup;
Al Viroa3255542012-03-30 14:41:51 -04001153 struct dentry *dentry;
1154
Miklos Szeredibad61182012-03-26 12:54:24 +02001155 dentry = lookup_dcache(name, base, nd, &need_lookup);
1156 if (!need_lookup)
1157 return dentry;
Al Viroa3255542012-03-30 14:41:51 -04001158
Miklos Szeredibad61182012-03-26 12:54:24 +02001159 return lookup_real(base->d_inode, dentry, nd);
Al Viroa3255542012-03-30 14:41:51 -04001160}
1161
Josef Bacik44396f42011-05-31 11:58:49 -04001162/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 * It's more convoluted than I'd like it to be, but... it's still fairly
1164 * small and for now I'd prefer to have fast path as straight as possible.
1165 * It _is_ time-critical.
1166 */
Miklos Szeredidb77d762012-05-21 17:30:05 +02001167static int lookup_fast(struct nameidata *nd, struct qstr *name,
1168 struct path *path, struct inode **inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169{
Jan Blunck4ac91372008-02-14 19:34:32 -08001170 struct vfsmount *mnt = nd->path.mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +11001171 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro5a18fff2011-03-11 04:44:53 -05001172 int need_reval = 1;
1173 int status = 1;
David Howells9875cf82011-01-14 18:45:21 +00001174 int err;
1175
Al Viro3cac2602009-08-13 18:27:43 +04001176 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001177 * Rename seqlock is not required here because in the off chance
1178 * of a false negative due to a concurrent rename, we're going to
1179 * do the non-racy lookup, below.
1180 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001181 if (nd->flags & LOOKUP_RCU) {
1182 unsigned seq;
Linus Torvaldsb7d3740d2012-05-04 14:59:14 -07001183 dentry = __d_lookup_rcu(parent, name, &seq, nd->inode);
Al Viro5a18fff2011-03-11 04:44:53 -05001184 if (!dentry)
1185 goto unlazy;
1186
Linus Torvaldsb7d3740d2012-05-04 14:59:14 -07001187 /*
1188 * This sequence count validates that the inode matches
1189 * the dentry name information from lookup.
1190 */
1191 *inode = dentry->d_inode;
1192 if (read_seqcount_retry(&dentry->d_seq, seq))
1193 return -ECHILD;
1194
1195 /*
1196 * This sequence count validates that the parent had no
1197 * changes while we did the lookup of the dentry above.
1198 *
1199 * The memory barrier in read_seqcount_begin of child is
1200 * enough, we can use __read_seqcount_retry here.
1201 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001202 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1203 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001204 nd->seq = seq;
Al Viro5a18fff2011-03-11 04:44:53 -05001205
Miklos Szeredifa4ee152012-03-26 12:54:19 +02001206 if (unlikely(d_need_lookup(dentry)))
1207 goto unlazy;
Al Viro24643082011-02-15 01:26:22 -05001208 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
Al Viro5a18fff2011-03-11 04:44:53 -05001209 status = d_revalidate(dentry, nd);
1210 if (unlikely(status <= 0)) {
1211 if (status != -ECHILD)
1212 need_reval = 0;
1213 goto unlazy;
1214 }
Al Viro24643082011-02-15 01:26:22 -05001215 }
Nick Piggin31e6b012011-01-07 17:49:52 +11001216 path->mnt = mnt;
1217 path->dentry = dentry;
Al Virod6e9bd22011-05-27 07:03:15 -04001218 if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1219 goto unlazy;
1220 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1221 goto unlazy;
1222 return 0;
Al Viro5a18fff2011-03-11 04:44:53 -05001223unlazy:
Al Viro19660af2011-03-25 10:32:48 -04001224 if (unlazy_walk(nd, dentry))
1225 return -ECHILD;
Al Viro5a18fff2011-03-11 04:44:53 -05001226 } else {
1227 dentry = __d_lookup(parent, name);
Nick Piggin31e6b012011-01-07 17:49:52 +11001228 }
Al Viro5a18fff2011-03-11 04:44:53 -05001229
Al Viro81e6f522012-03-30 14:48:04 -04001230 if (unlikely(!dentry))
1231 goto need_lookup;
Al Viro5a18fff2011-03-11 04:44:53 -05001232
Al Viro81e6f522012-03-30 14:48:04 -04001233 if (unlikely(d_need_lookup(dentry))) {
1234 dput(dentry);
1235 goto need_lookup;
Al Viro24643082011-02-15 01:26:22 -05001236 }
Al Viro81e6f522012-03-30 14:48:04 -04001237
Al Viro5a18fff2011-03-11 04:44:53 -05001238 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
1239 status = d_revalidate(dentry, nd);
1240 if (unlikely(status <= 0)) {
1241 if (status < 0) {
1242 dput(dentry);
1243 return status;
1244 }
1245 if (!d_invalidate(dentry)) {
1246 dput(dentry);
Al Viro81e6f522012-03-30 14:48:04 -04001247 goto need_lookup;
Al Viro5a18fff2011-03-11 04:44:53 -05001248 }
1249 }
Miklos Szeredidb77d762012-05-21 17:30:05 +02001250
David Howells9875cf82011-01-14 18:45:21 +00001251 path->mnt = mnt;
1252 path->dentry = dentry;
1253 err = follow_managed(path, nd->flags);
Ian Kent89312212011-01-18 12:06:10 +08001254 if (unlikely(err < 0)) {
1255 path_put_conditional(path, nd);
David Howells9875cf82011-01-14 18:45:21 +00001256 return err;
Ian Kent89312212011-01-18 12:06:10 +08001257 }
Al Viroa3fbbde2011-11-07 21:21:26 +00001258 if (err)
1259 nd->flags |= LOOKUP_JUMPED;
David Howells9875cf82011-01-14 18:45:21 +00001260 *inode = path->dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 return 0;
Al Viro81e6f522012-03-30 14:48:04 -04001262
1263need_lookup:
Miklos Szeredidb77d762012-05-21 17:30:05 +02001264 return 1;
1265}
1266
1267/* Fast lookup failed, do it the slow way */
1268static int lookup_slow(struct nameidata *nd, struct qstr *name,
1269 struct path *path)
1270{
1271 struct dentry *dentry, *parent;
1272 int err;
1273
1274 parent = nd->path.dentry;
Al Viro81e6f522012-03-30 14:48:04 -04001275 BUG_ON(nd->inode != parent->d_inode);
1276
1277 mutex_lock(&parent->d_inode->i_mutex);
1278 dentry = __lookup_hash(name, parent, nd);
1279 mutex_unlock(&parent->d_inode->i_mutex);
1280 if (IS_ERR(dentry))
1281 return PTR_ERR(dentry);
Miklos Szeredidb77d762012-05-21 17:30:05 +02001282 path->mnt = nd->path.mnt;
1283 path->dentry = dentry;
1284 err = follow_managed(path, nd->flags);
1285 if (unlikely(err < 0)) {
1286 path_put_conditional(path, nd);
1287 return err;
1288 }
1289 if (err)
1290 nd->flags |= LOOKUP_JUMPED;
1291 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292}
1293
Al Viro52094c82011-02-21 21:34:47 -05001294static inline int may_lookup(struct nameidata *nd)
1295{
1296 if (nd->flags & LOOKUP_RCU) {
Daniel Rosenberg17285112016-10-26 16:27:45 -07001297 int err = inode_permission2(nd->path.mnt, nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
Al Viro52094c82011-02-21 21:34:47 -05001298 if (err != -ECHILD)
1299 return err;
Al Viro19660af2011-03-25 10:32:48 -04001300 if (unlazy_walk(nd, NULL))
Al Viro52094c82011-02-21 21:34:47 -05001301 return -ECHILD;
1302 }
Daniel Rosenberg17285112016-10-26 16:27:45 -07001303 return inode_permission2(nd->path.mnt, nd->inode, MAY_EXEC);
Al Viro52094c82011-02-21 21:34:47 -05001304}
1305
Al Viro9856fa12011-03-04 14:22:06 -05001306static inline int handle_dots(struct nameidata *nd, int type)
1307{
1308 if (type == LAST_DOTDOT) {
1309 if (nd->flags & LOOKUP_RCU) {
1310 if (follow_dotdot_rcu(nd))
1311 return -ECHILD;
1312 } else
Eric W. Biedermanbf2e9fa2015-08-15 20:27:13 -05001313 return follow_dotdot(nd);
Al Viro9856fa12011-03-04 14:22:06 -05001314 }
1315 return 0;
1316}
1317
Al Viro951361f2011-03-04 14:44:37 -05001318static void terminate_walk(struct nameidata *nd)
1319{
1320 if (!(nd->flags & LOOKUP_RCU)) {
1321 path_put(&nd->path);
1322 } else {
1323 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001324 if (!(nd->flags & LOOKUP_ROOT))
1325 nd->root.mnt = NULL;
Al Viro951361f2011-03-04 14:44:37 -05001326 rcu_read_unlock();
Andi Kleenf0769502012-05-08 13:32:02 +09301327 br_read_unlock(&vfsmount_lock);
Al Viro951361f2011-03-04 14:44:37 -05001328 }
1329}
1330
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001331/*
1332 * Do we need to follow links? We _really_ want to be able
1333 * to do this check without having to look at inode->i_op,
1334 * so we keep a cache of "no, this doesn't need follow_link"
1335 * for the common case.
1336 */
Linus Torvalds7813b942011-08-07 09:53:20 -07001337static inline int should_follow_link(struct inode *inode, int follow)
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001338{
1339 if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
1340 if (likely(inode->i_op->follow_link))
1341 return follow;
1342
1343 /* This gets set once for the inode lifetime */
1344 spin_lock(&inode->i_lock);
1345 inode->i_opflags |= IOP_NOFOLLOW;
1346 spin_unlock(&inode->i_lock);
1347 }
1348 return 0;
1349}
1350
Al Viroce57dfc2011-03-13 19:58:58 -04001351static inline int walk_component(struct nameidata *nd, struct path *path,
1352 struct qstr *name, int type, int follow)
1353{
1354 struct inode *inode;
1355 int err;
1356 /*
1357 * "." and ".." are special - ".." especially so because it has
1358 * to be able to know about the current root directory and
1359 * parent relationships.
1360 */
1361 if (unlikely(type != LAST_NORM))
1362 return handle_dots(nd, type);
Miklos Szeredidb77d762012-05-21 17:30:05 +02001363 err = lookup_fast(nd, name, path, &inode);
Al Viroce57dfc2011-03-13 19:58:58 -04001364 if (unlikely(err)) {
Miklos Szeredidb77d762012-05-21 17:30:05 +02001365 if (err < 0)
1366 goto out_err;
1367
1368 err = lookup_slow(nd, name, path);
1369 if (err < 0)
1370 goto out_err;
1371
1372 inode = path->dentry->d_inode;
Al Viroce57dfc2011-03-13 19:58:58 -04001373 }
Miklos Szeredidb77d762012-05-21 17:30:05 +02001374 err = -ENOENT;
1375 if (!inode)
1376 goto out_path_put;
1377
Linus Torvalds7813b942011-08-07 09:53:20 -07001378 if (should_follow_link(inode, follow)) {
Al Viro19660af2011-03-25 10:32:48 -04001379 if (nd->flags & LOOKUP_RCU) {
Al Viroa377bbd2015-04-24 15:47:07 -04001380 if (unlikely(nd->path.mnt != path->mnt ||
1381 unlazy_walk(nd, path->dentry))) {
Miklos Szeredidb77d762012-05-21 17:30:05 +02001382 err = -ECHILD;
1383 goto out_err;
Al Viro19660af2011-03-25 10:32:48 -04001384 }
1385 }
Al Viroce57dfc2011-03-13 19:58:58 -04001386 BUG_ON(inode != path->dentry->d_inode);
1387 return 1;
1388 }
1389 path_to_nameidata(path, nd);
1390 nd->inode = inode;
1391 return 0;
Miklos Szeredidb77d762012-05-21 17:30:05 +02001392
1393out_path_put:
1394 path_to_nameidata(path, nd);
1395out_err:
1396 terminate_walk(nd);
1397 return err;
Al Viroce57dfc2011-03-13 19:58:58 -04001398}
1399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400/*
Al Virob3563792011-03-14 21:54:55 -04001401 * This limits recursive symlink follows to 8, while
1402 * limiting consecutive symlinks to 40.
1403 *
1404 * Without that kind of total limit, nasty chains of consecutive
1405 * symlinks can cause almost arbitrarily long lookups.
1406 */
1407static inline int nested_symlink(struct path *path, struct nameidata *nd)
1408{
1409 int res;
1410
Al Virob3563792011-03-14 21:54:55 -04001411 if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1412 path_put_conditional(path, nd);
1413 path_put(&nd->path);
1414 return -ELOOP;
1415 }
Erez Zadok1a4022f2011-05-21 01:19:59 -04001416 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
Al Virob3563792011-03-14 21:54:55 -04001417
1418 nd->depth++;
1419 current->link_count++;
1420
1421 do {
1422 struct path link = *path;
1423 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04001424
1425 res = follow_link(&link, nd, &cookie);
Al Viro2c4d7392012-06-10 04:15:17 -04001426 if (res)
1427 break;
1428 res = walk_component(nd, path, &nd->last,
1429 nd->last_type, LOOKUP_FOLLOW);
Al Viro574197e2011-03-14 22:20:34 -04001430 put_link(nd, &link, cookie);
Al Virob3563792011-03-14 21:54:55 -04001431 } while (res > 0);
1432
1433 current->link_count--;
1434 nd->depth--;
1435 return res;
1436}
1437
1438/*
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001439 * We really don't want to look at inode->i_op->lookup
1440 * when we don't have to. So we keep a cache bit in
1441 * the inode ->i_opflags field that says "yes, we can
1442 * do lookup on this inode".
1443 */
1444static inline int can_lookup(struct inode *inode)
1445{
1446 if (likely(inode->i_opflags & IOP_LOOKUP))
1447 return 1;
1448 if (likely(!inode->i_op->lookup))
1449 return 0;
1450
1451 /* We do this once for the lifetime of the inode */
1452 spin_lock(&inode->i_lock);
1453 inode->i_opflags |= IOP_LOOKUP;
1454 spin_unlock(&inode->i_lock);
1455 return 1;
1456}
1457
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001458/*
1459 * We can do the critical dentry name comparison and hashing
1460 * operations one word at a time, but we are limited to:
1461 *
1462 * - Architectures with fast unaligned word accesses. We could
1463 * do a "get_unaligned()" if this helps and is sufficiently
1464 * fast.
1465 *
1466 * - Little-endian machines (so that we can generate the mask
1467 * of low bytes efficiently). Again, we *could* do a byte
1468 * swapping load on big-endian architectures if that is not
1469 * expensive enough to make the optimization worthless.
1470 *
1471 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1472 * do not trap on the (extremely unlikely) case of a page
1473 * crossing operation.
1474 *
1475 * - Furthermore, we need an efficient 64-bit compile for the
1476 * 64-bit case in order to generate the "number of bytes in
1477 * the final mask". Again, that could be replaced with a
1478 * efficient population count instruction or similar.
1479 */
1480#ifdef CONFIG_DCACHE_WORD_ACCESS
1481
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001482#include <asm/word-at-a-time.h>
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001483
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001484#ifdef CONFIG_64BIT
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001485
1486static inline unsigned int fold_hash(unsigned long hash)
1487{
1488 hash += hash >> (8*sizeof(int));
1489 return hash;
1490}
1491
1492#else /* 32-bit case */
1493
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001494#define fold_hash(x) (x)
1495
1496#endif
1497
1498unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1499{
1500 unsigned long a, mask;
1501 unsigned long hash = 0;
1502
1503 for (;;) {
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001504 a = load_unaligned_zeropad(name);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001505 if (len < sizeof(unsigned long))
1506 break;
1507 hash += a;
Al Virof132c5b2012-03-22 21:59:52 +00001508 hash *= 9;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001509 name += sizeof(unsigned long);
1510 len -= sizeof(unsigned long);
1511 if (!len)
1512 goto done;
1513 }
1514 mask = ~(~0ul << len*8);
1515 hash += mask & a;
1516done:
1517 return fold_hash(hash);
1518}
1519EXPORT_SYMBOL(full_name_hash);
1520
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001521/*
1522 * Calculate the length and hash of the path component, and
1523 * return the length of the component;
1524 */
1525static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1526{
1527 unsigned long a, mask, hash, len;
1528
1529 hash = a = 0;
1530 len = -sizeof(unsigned long);
1531 do {
1532 hash = (hash + a) * 9;
1533 len += sizeof(unsigned long);
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001534 a = load_unaligned_zeropad(name+len);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001535 /* Do we have any NUL or '/' bytes in this word? */
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001536 mask = has_zero(a) | has_zero(a ^ REPEAT_BYTE('/'));
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001537 } while (!mask);
1538
1539 /* The mask *below* the first high bit set */
1540 mask = (mask - 1) & ~mask;
1541 mask >>= 7;
1542 hash += a & mask;
1543 *hashp = fold_hash(hash);
1544
1545 return len + count_masked_bytes(mask);
1546}
1547
1548#else
1549
Linus Torvalds0145acc2012-03-02 14:32:59 -08001550unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1551{
1552 unsigned long hash = init_name_hash();
1553 while (len--)
1554 hash = partial_name_hash(*name++, hash);
1555 return end_name_hash(hash);
1556}
Linus Torvaldsae942ae2012-03-02 19:40:57 -08001557EXPORT_SYMBOL(full_name_hash);
Linus Torvalds0145acc2012-03-02 14:32:59 -08001558
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001559/*
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001560 * We know there's a real path component here of at least
1561 * one character.
1562 */
1563static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1564{
1565 unsigned long hash = init_name_hash();
1566 unsigned long len = 0, c;
1567
1568 c = (unsigned char)*name;
1569 do {
1570 len++;
1571 hash = partial_name_hash(c, hash);
1572 c = (unsigned char)name[len];
1573 } while (c && c != '/');
1574 *hashp = end_name_hash(hash);
1575 return len;
1576}
1577
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001578#endif
1579
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001580/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01001582 * This is the basic name resolution function, turning a pathname into
1583 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01001585 * Returns 0 and nd will have valid dentry and mnt on success.
1586 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 */
Al Viro6de88d72009-08-09 01:41:57 +04001588static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589{
1590 struct path next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
1593 while (*name=='/')
1594 name++;
1595 if (!*name)
Al Viro086e1832011-02-22 20:56:27 -05001596 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 /* At this point we know we have a real path component. */
1599 for(;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 struct qstr this;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001601 long len;
Al Virofe479a52011-02-22 15:10:03 -05001602 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Al Viro52094c82011-02-21 21:34:47 -05001604 err = may_lookup(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 if (err)
1606 break;
1607
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001608 len = hash_name(name, &this.hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 this.name = name;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001610 this.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
Al Virofe479a52011-02-22 15:10:03 -05001612 type = LAST_NORM;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001613 if (name[0] == '.') switch (len) {
Al Virofe479a52011-02-22 15:10:03 -05001614 case 2:
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001615 if (name[1] == '.') {
Al Virofe479a52011-02-22 15:10:03 -05001616 type = LAST_DOTDOT;
Al Viro16c2cd72011-02-22 15:50:10 -05001617 nd->flags |= LOOKUP_JUMPED;
1618 }
Al Virofe479a52011-02-22 15:10:03 -05001619 break;
1620 case 1:
1621 type = LAST_DOT;
1622 }
Al Viro5a202bc2011-03-08 14:17:44 -05001623 if (likely(type == LAST_NORM)) {
1624 struct dentry *parent = nd->path.dentry;
Al Viro16c2cd72011-02-22 15:50:10 -05001625 nd->flags &= ~LOOKUP_JUMPED;
Al Viro5a202bc2011-03-08 14:17:44 -05001626 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1627 err = parent->d_op->d_hash(parent, nd->inode,
1628 &this);
1629 if (err < 0)
1630 break;
1631 }
1632 }
Al Virofe479a52011-02-22 15:10:03 -05001633
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001634 if (!name[len])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 goto last_component;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001636 /*
1637 * If it wasn't NUL, we know it was '/'. Skip that
1638 * slash, and continue until no more slashes.
1639 */
1640 do {
1641 len++;
1642 } while (unlikely(name[len] == '/'));
1643 if (!name[len])
Al Virob3563792011-03-14 21:54:55 -04001644 goto last_component;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001645 name += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Al Viroce57dfc2011-03-13 19:58:58 -04001647 err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1648 if (err < 0)
1649 return err;
Al Virofe479a52011-02-22 15:10:03 -05001650
Al Viroce57dfc2011-03-13 19:58:58 -04001651 if (err) {
Al Virob3563792011-03-14 21:54:55 -04001652 err = nested_symlink(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 if (err)
Al Viroa7472ba2011-03-04 14:39:30 -05001654 return err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001655 }
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001656 if (can_lookup(nd->inode))
1657 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 err = -ENOTDIR;
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001659 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 /* here ends the main loop */
1661
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662last_component:
Al Virob3563792011-03-14 21:54:55 -04001663 nd->last = this;
1664 nd->last_type = type;
Al Viro086e1832011-02-22 20:56:27 -05001665 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 }
Al Viro951361f2011-03-04 14:44:37 -05001667 terminate_walk(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 return err;
1669}
1670
Al Viro70e9b352011-03-05 21:12:22 -05001671static int path_init(int dfd, const char *name, unsigned int flags,
1672 struct nameidata *nd, struct file **fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673{
Prasanna Medaea3834d2005-04-29 16:00:17 +01001674 int retval = 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001675 int fput_needed;
1676 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
1678 nd->last_type = LAST_ROOT; /* if there are only slashes... */
Al Viro16c2cd72011-02-22 15:50:10 -05001679 nd->flags = flags | LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 nd->depth = 0;
Al Viro5b6ca022011-03-09 23:04:47 -05001681 if (flags & LOOKUP_ROOT) {
1682 struct inode *inode = nd->root.dentry->d_inode;
Daniel Rosenberg17285112016-10-26 16:27:45 -07001683 struct vfsmount *mnt = nd->root.mnt;
Al Viro73d049a2011-03-11 12:08:24 -05001684 if (*name) {
1685 if (!inode->i_op->lookup)
1686 return -ENOTDIR;
Daniel Rosenberg17285112016-10-26 16:27:45 -07001687 retval = inode_permission2(mnt, inode, MAY_EXEC);
Al Viro73d049a2011-03-11 12:08:24 -05001688 if (retval)
1689 return retval;
1690 }
Al Viro5b6ca022011-03-09 23:04:47 -05001691 nd->path = nd->root;
1692 nd->inode = inode;
1693 if (flags & LOOKUP_RCU) {
Andi Kleenf0769502012-05-08 13:32:02 +09301694 br_read_lock(&vfsmount_lock);
Al Viro5b6ca022011-03-09 23:04:47 -05001695 rcu_read_lock();
1696 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1697 } else {
1698 path_get(&nd->path);
1699 }
1700 return 0;
1701 }
1702
Al Viro2a737872009-04-07 11:49:53 -04001703 nd->root.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 if (*name=='/') {
Al Viroe41f7d42011-02-22 14:02:58 -05001706 if (flags & LOOKUP_RCU) {
Andi Kleenf0769502012-05-08 13:32:02 +09301707 br_read_lock(&vfsmount_lock);
Al Viroe41f7d42011-02-22 14:02:58 -05001708 rcu_read_lock();
Al Viro589a49f2014-09-13 21:55:46 -04001709 nd->seq = set_root_rcu(nd);
Al Viroe41f7d42011-02-22 14:02:58 -05001710 } else {
1711 set_root(nd);
1712 path_get(&nd->root);
1713 }
Al Viro2a737872009-04-07 11:49:53 -04001714 nd->path = nd->root;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001715 } else if (dfd == AT_FDCWD) {
Al Viroe41f7d42011-02-22 14:02:58 -05001716 if (flags & LOOKUP_RCU) {
1717 struct fs_struct *fs = current->fs;
1718 unsigned seq;
1719
Andi Kleenf0769502012-05-08 13:32:02 +09301720 br_read_lock(&vfsmount_lock);
Al Viroe41f7d42011-02-22 14:02:58 -05001721 rcu_read_lock();
1722
1723 do {
1724 seq = read_seqcount_begin(&fs->seq);
1725 nd->path = fs->pwd;
1726 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1727 } while (read_seqcount_retry(&fs->seq, seq));
1728 } else {
1729 get_fs_pwd(current->fs, &nd->path);
1730 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001731 } else {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001732 struct dentry *dentry;
Daniel Rosenberg17285112016-10-26 16:27:45 -07001733 struct vfsmount *mnt;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001734
Al Viro1abf0c72011-03-13 03:51:11 -04001735 file = fget_raw_light(dfd, &fput_needed);
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001736 retval = -EBADF;
1737 if (!file)
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001738 goto out_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001739
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001740 dentry = file->f_path.dentry;
Daniel Rosenberg17285112016-10-26 16:27:45 -07001741 mnt = file->f_path.mnt;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001742
Al Virof52e0c12011-03-14 18:56:51 -04001743 if (*name) {
1744 retval = -ENOTDIR;
1745 if (!S_ISDIR(dentry->d_inode->i_mode))
1746 goto fput_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001747
Daniel Rosenberg17285112016-10-26 16:27:45 -07001748 retval = inode_permission2(mnt, dentry->d_inode, MAY_EXEC);
Al Virof52e0c12011-03-14 18:56:51 -04001749 if (retval)
1750 goto fput_fail;
1751 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001752
Jan Blunck5dd784d2008-02-14 19:34:38 -08001753 nd->path = file->f_path;
Al Viroe41f7d42011-02-22 14:02:58 -05001754 if (flags & LOOKUP_RCU) {
1755 if (fput_needed)
Al Viro70e9b352011-03-05 21:12:22 -05001756 *fp = file;
Al Viroe41f7d42011-02-22 14:02:58 -05001757 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
Andi Kleenf0769502012-05-08 13:32:02 +09301758 br_read_lock(&vfsmount_lock);
Al Viroe41f7d42011-02-22 14:02:58 -05001759 rcu_read_lock();
1760 } else {
1761 path_get(&file->f_path);
1762 fput_light(file, fput_needed);
1763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 }
Al Viroe41f7d42011-02-22 14:02:58 -05001765
Nick Piggin31e6b012011-01-07 17:49:52 +11001766 nd->inode = nd->path.dentry->d_inode;
Al Viro9b4a9b12009-04-07 11:44:16 -04001767 return 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001768
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001769fput_fail:
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001770 fput_light(file, fput_needed);
Al Viro9b4a9b12009-04-07 11:44:16 -04001771out_fail:
1772 return retval;
1773}
1774
Al Virobd92d7f2011-03-14 19:54:59 -04001775static inline int lookup_last(struct nameidata *nd, struct path *path)
1776{
1777 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1778 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1779
1780 nd->flags &= ~LOOKUP_PARENT;
1781 return walk_component(nd, path, &nd->last, nd->last_type,
1782 nd->flags & LOOKUP_FOLLOW);
1783}
1784
Al Viro9b4a9b12009-04-07 11:44:16 -04001785/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroee0827c2011-02-21 23:38:09 -05001786static int path_lookupat(int dfd, const char *name,
Al Viro9b4a9b12009-04-07 11:44:16 -04001787 unsigned int flags, struct nameidata *nd)
1788{
Al Viro70e9b352011-03-05 21:12:22 -05001789 struct file *base = NULL;
Al Virobd92d7f2011-03-14 19:54:59 -04001790 struct path path;
1791 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001792
1793 /*
1794 * Path walking is largely split up into 2 different synchronisation
1795 * schemes, rcu-walk and ref-walk (explained in
1796 * Documentation/filesystems/path-lookup.txt). These share much of the
1797 * path walk code, but some things particularly setup, cleanup, and
1798 * following mounts are sufficiently divergent that functions are
1799 * duplicated. Typically there is a function foo(), and its RCU
1800 * analogue, foo_rcu().
1801 *
1802 * -ECHILD is the error number of choice (just to avoid clashes) that
1803 * is returned if some aspect of an rcu-walk fails. Such an error must
1804 * be handled by restarting a traditional ref-walk (which will always
1805 * be able to complete).
1806 */
Al Virobd92d7f2011-03-14 19:54:59 -04001807 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
Al Viroee0827c2011-02-21 23:38:09 -05001808
Al Virobd92d7f2011-03-14 19:54:59 -04001809 if (unlikely(err))
1810 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001811
1812 current->total_link_count = 0;
Al Virobd92d7f2011-03-14 19:54:59 -04001813 err = link_path_walk(name, nd);
1814
1815 if (!err && !(flags & LOOKUP_PARENT)) {
Al Virobd92d7f2011-03-14 19:54:59 -04001816 err = lookup_last(nd, &path);
1817 while (err > 0) {
1818 void *cookie;
1819 struct path link = path;
Al Virobd92d7f2011-03-14 19:54:59 -04001820 nd->flags |= LOOKUP_PARENT;
Al Viro574197e2011-03-14 22:20:34 -04001821 err = follow_link(&link, nd, &cookie);
Al Viro2c4d7392012-06-10 04:15:17 -04001822 if (err)
1823 break;
1824 err = lookup_last(nd, &path);
Al Viro574197e2011-03-14 22:20:34 -04001825 put_link(nd, &link, cookie);
Al Virobd92d7f2011-03-14 19:54:59 -04001826 }
1827 }
Al Viroee0827c2011-02-21 23:38:09 -05001828
Al Viro9f1fafe2011-03-25 11:00:12 -04001829 if (!err)
1830 err = complete_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04001831
1832 if (!err && nd->flags & LOOKUP_DIRECTORY) {
1833 if (!nd->inode->i_op->lookup) {
1834 path_put(&nd->path);
Al Virobd23a532011-03-23 09:56:30 -04001835 err = -ENOTDIR;
Al Virobd92d7f2011-03-14 19:54:59 -04001836 }
1837 }
Al Viro16c2cd72011-02-22 15:50:10 -05001838
Tom Marshall384972f2017-04-28 22:46:37 +00001839 if (!err) {
1840 struct super_block *sb = nd->inode->i_sb;
1841 if (sb->s_flags & MS_RDONLY) {
Tom Marshall1a4cd212017-05-19 18:24:49 +00001842 if (d_is_su(nd->path.dentry) && !su_visible()) {
1843 path_put(&nd->path);
Tom Marshall384972f2017-04-28 22:46:37 +00001844 err = -ENOENT;
Tom Marshall1a4cd212017-05-19 18:24:49 +00001845 }
Tom Marshall384972f2017-04-28 22:46:37 +00001846 }
1847 }
1848
Al Viro70e9b352011-03-05 21:12:22 -05001849 if (base)
1850 fput(base);
Al Viroee0827c2011-02-21 23:38:09 -05001851
Al Viro5b6ca022011-03-09 23:04:47 -05001852 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
Al Viro2a737872009-04-07 11:49:53 -04001853 path_put(&nd->root);
1854 nd->root.mnt = NULL;
1855 }
Al Virobd92d7f2011-03-14 19:54:59 -04001856 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001857}
Nick Piggin31e6b012011-01-07 17:49:52 +11001858
Al Viroee0827c2011-02-21 23:38:09 -05001859static int do_path_lookup(int dfd, const char *name,
1860 unsigned int flags, struct nameidata *nd)
1861{
1862 int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1863 if (unlikely(retval == -ECHILD))
1864 retval = path_lookupat(dfd, name, flags, nd);
1865 if (unlikely(retval == -ESTALE))
1866 retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11001867
1868 if (likely(!retval)) {
1869 if (unlikely(!audit_dummy_context())) {
1870 if (nd->path.dentry && nd->inode)
1871 audit_inode(name, nd->path.dentry);
1872 }
1873 }
Al Viro9b4a9b12009-04-07 11:44:16 -04001874 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875}
1876
Al Viro0613fb82012-06-15 03:01:42 +04001877/* does lookup, returns the object with parent locked */
1878struct dentry *kern_path_locked(const char *name, struct path *path)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001879{
Al Viro0613fb82012-06-15 03:01:42 +04001880 struct nameidata nd;
1881 struct dentry *d;
1882 int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd);
1883 if (err)
1884 return ERR_PTR(err);
1885 if (nd.last_type != LAST_NORM) {
1886 path_put(&nd.path);
1887 return ERR_PTR(-EINVAL);
1888 }
1889 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
1890 d = lookup_one_len(nd.last.name, nd.path.dentry, nd.last.len);
1891 if (IS_ERR(d)) {
1892 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
1893 path_put(&nd.path);
1894 return d;
1895 }
1896 *path = nd.path;
1897 return d;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001898}
1899
Al Virod1811462008-08-02 00:49:18 -04001900int kern_path(const char *name, unsigned int flags, struct path *path)
1901{
1902 struct nameidata nd;
1903 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1904 if (!res)
1905 *path = nd.path;
1906 return res;
1907}
1908
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001909/**
1910 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1911 * @dentry: pointer to dentry of the base directory
1912 * @mnt: pointer to vfs mount of the base directory
1913 * @name: pointer to file name
1914 * @flags: lookup flags
Al Viroe0a01242011-06-27 17:00:37 -04001915 * @path: pointer to struct path to fill
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001916 */
1917int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
1918 const char *name, unsigned int flags,
Al Viroe0a01242011-06-27 17:00:37 -04001919 struct path *path)
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001920{
Al Viroe0a01242011-06-27 17:00:37 -04001921 struct nameidata nd;
1922 int err;
1923 nd.root.dentry = dentry;
1924 nd.root.mnt = mnt;
1925 BUG_ON(flags & LOOKUP_PARENT);
Al Viro5b6ca022011-03-09 23:04:47 -05001926 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
Al Viroe0a01242011-06-27 17:00:37 -04001927 err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
1928 if (!err)
1929 *path = nd.path;
1930 return err;
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001931}
1932
James Morris057f6c02007-04-26 00:12:05 -07001933/*
1934 * Restricted form of lookup. Doesn't follow links, single-component only,
1935 * needs parent already locked. Doesn't follow mounts.
1936 * SMP-safe.
1937 */
Adrian Bunka244e162006-03-31 02:32:11 -08001938static struct dentry *lookup_hash(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939{
Jan Blunck4ac91372008-02-14 19:34:32 -08001940 return __lookup_hash(&nd->last, nd->path.dentry, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941}
1942
Christoph Hellwigeead1912007-10-16 23:25:38 -07001943/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07001944 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07001945 * @name: pathname component to lookup
Daniel Rosenberg17285112016-10-26 16:27:45 -07001946 * @mnt: mount we are looking up on
Christoph Hellwigeead1912007-10-16 23:25:38 -07001947 * @base: base directory to lookup from
1948 * @len: maximum length @len should be interpreted to
1949 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07001950 * Note that this routine is purely a helper for filesystem usage and should
1951 * not be called by generic code. Also note that by using this function the
Christoph Hellwigeead1912007-10-16 23:25:38 -07001952 * nameidata argument is passed to the filesystem methods and a filesystem
1953 * using this helper needs to be prepared for that.
1954 */
Daniel Rosenberg17285112016-10-26 16:27:45 -07001955struct dentry *lookup_one_len2(const char *name, struct vfsmount *mnt, struct dentry *base, int len)
James Morris057f6c02007-04-26 00:12:05 -07001956{
James Morris057f6c02007-04-26 00:12:05 -07001957 struct qstr this;
Al Viro6a96ba52011-03-07 23:49:20 -05001958 unsigned int c;
Miklos Szeredicda309d2012-03-26 12:54:21 +02001959 int err;
James Morris057f6c02007-04-26 00:12:05 -07001960
David Woodhouse2f9092e2009-04-20 23:18:37 +01001961 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
1962
Al Viro6a96ba52011-03-07 23:49:20 -05001963 this.name = name;
1964 this.len = len;
Linus Torvalds0145acc2012-03-02 14:32:59 -08001965 this.hash = full_name_hash(name, len);
Al Viro6a96ba52011-03-07 23:49:20 -05001966 if (!len)
1967 return ERR_PTR(-EACCES);
1968
Al Viro6a96ba52011-03-07 23:49:20 -05001969 while (len--) {
1970 c = *(const unsigned char *)name++;
1971 if (c == '/' || c == '\0')
1972 return ERR_PTR(-EACCES);
Al Viro6a96ba52011-03-07 23:49:20 -05001973 }
Al Viro5a202bc2011-03-08 14:17:44 -05001974 /*
1975 * See if the low-level filesystem might want
1976 * to use its own hash..
1977 */
1978 if (base->d_flags & DCACHE_OP_HASH) {
1979 int err = base->d_op->d_hash(base, base->d_inode, &this);
1980 if (err < 0)
1981 return ERR_PTR(err);
1982 }
Christoph Hellwigeead1912007-10-16 23:25:38 -07001983
Daniel Rosenberg17285112016-10-26 16:27:45 -07001984 err = inode_permission2(mnt, base->d_inode, MAY_EXEC);
Miklos Szeredicda309d2012-03-26 12:54:21 +02001985 if (err)
1986 return ERR_PTR(err);
1987
Christoph Hellwig49705b72005-11-08 21:35:06 -08001988 return __lookup_hash(&this, base, NULL);
James Morris057f6c02007-04-26 00:12:05 -07001989}
Daniel Rosenberg17285112016-10-26 16:27:45 -07001990EXPORT_SYMBOL(lookup_one_len2);
1991
1992struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1993{
1994 return lookup_one_len2(name, NULL, base, len);
1995}
1996EXPORT_SYMBOL(lookup_one_len);
James Morris057f6c02007-04-26 00:12:05 -07001997
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01001998int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
1999 struct path *path, int *empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000{
Al Viro2d8f3032008-07-22 09:59:21 -04002001 struct nameidata nd;
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002002 char *tmp = getname_flags(name, flags, empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 int err = PTR_ERR(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 if (!IS_ERR(tmp)) {
Al Viro2d8f3032008-07-22 09:59:21 -04002005
2006 BUG_ON(flags & LOOKUP_PARENT);
2007
2008 err = do_path_lookup(dfd, tmp, flags, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 putname(tmp);
Al Viro2d8f3032008-07-22 09:59:21 -04002010 if (!err)
2011 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 }
2013 return err;
2014}
2015
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002016int user_path_at(int dfd, const char __user *name, unsigned flags,
2017 struct path *path)
2018{
Linus Torvaldsf7493e52012-03-22 16:10:40 -07002019 return user_path_at_empty(dfd, name, flags, path, NULL);
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002020}
2021
Al Viro2ad94ae2008-07-21 09:32:51 -04002022static int user_path_parent(int dfd, const char __user *path,
2023 struct nameidata *nd, char **name)
2024{
2025 char *s = getname(path);
2026 int error;
2027
2028 if (IS_ERR(s))
2029 return PTR_ERR(s);
2030
2031 error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
2032 if (error)
2033 putname(s);
2034 else
2035 *name = s;
2036
2037 return error;
2038}
2039
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040/*
2041 * It's inline, so penalty for filesystems that don't use sticky bit is
2042 * minimal.
2043 */
2044static inline int check_sticky(struct inode *dir, struct inode *inode)
2045{
David Howellsda9592e2008-11-14 10:39:05 +11002046 uid_t fsuid = current_fsuid();
2047
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 if (!(dir->i_mode & S_ISVTX))
2049 return 0;
Serge E. Hallyne795b712011-03-23 16:43:25 -07002050 if (current_user_ns() != inode_userns(inode))
2051 goto other_userns;
David Howellsda9592e2008-11-14 10:39:05 +11002052 if (inode->i_uid == fsuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 return 0;
David Howellsda9592e2008-11-14 10:39:05 +11002054 if (dir->i_uid == fsuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 return 0;
Serge E. Hallyne795b712011-03-23 16:43:25 -07002056
2057other_userns:
2058 return !ns_capable(inode_userns(inode), CAP_FOWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059}
2060
2061/*
2062 * Check whether we can remove a link victim from directory dir, check
2063 * whether the type of victim is right.
2064 * 1. We can't do it if dir is read-only (done in permission())
2065 * 2. We should have write and exec permissions on dir
2066 * 3. We can't remove anything from append-only dir
2067 * 4. We can't do anything with immutable dir (done in permission())
2068 * 5. If the sticky bit on dir is set we should either
2069 * a. be owner of dir, or
2070 * b. be owner of victim, or
2071 * c. have CAP_FOWNER capability
2072 * 6. If the victim is append-only or immutable we can't do antyhing with
2073 * links pointing to it.
2074 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2075 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2076 * 9. We can't remove a root or mountpoint.
2077 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
2078 * nfs_async_unlink().
2079 */
Daniel Rosenberg17285112016-10-26 16:27:45 -07002080static int may_delete(struct vfsmount *mnt, struct inode *dir,struct dentry *victim,int isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081{
2082 int error;
2083
2084 if (!victim->d_inode)
2085 return -ENOENT;
2086
2087 BUG_ON(victim->d_parent->d_inode != dir);
Al Virocccc6bb2009-12-25 05:07:33 -05002088 audit_inode_child(victim, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
Daniel Rosenberg17285112016-10-26 16:27:45 -07002090 error = inode_permission2(mnt, dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 if (error)
2092 return error;
2093 if (IS_APPEND(dir))
2094 return -EPERM;
2095 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
Hugh Dickinsf9454542008-11-19 15:36:38 -08002096 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 return -EPERM;
2098 if (isdir) {
2099 if (!S_ISDIR(victim->d_inode->i_mode))
2100 return -ENOTDIR;
2101 if (IS_ROOT(victim))
2102 return -EBUSY;
2103 } else if (S_ISDIR(victim->d_inode->i_mode))
2104 return -EISDIR;
2105 if (IS_DEADDIR(dir))
2106 return -ENOENT;
2107 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2108 return -EBUSY;
2109 return 0;
2110}
2111
2112/* Check whether we can create an object with dentry child in directory
2113 * dir.
2114 * 1. We can't do it if child already exists (open has special treatment for
2115 * this case, but since we are inlined it's OK)
2116 * 2. We can't do it if dir is read-only (done in permission())
2117 * 3. We should have write and exec permissions on dir
2118 * 4. We can't do it if dir is immutable (done in permission())
2119 */
Daniel Rosenberg17285112016-10-26 16:27:45 -07002120static inline int may_create(struct vfsmount *mnt, struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121{
2122 if (child->d_inode)
2123 return -EEXIST;
2124 if (IS_DEADDIR(dir))
2125 return -ENOENT;
Daniel Rosenberg17285112016-10-26 16:27:45 -07002126 return inode_permission2(mnt, dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127}
2128
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129/*
2130 * p1 and p2 should be directories on the same fs.
2131 */
2132struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2133{
2134 struct dentry *p;
2135
2136 if (p1 == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07002137 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 return NULL;
2139 }
2140
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002141 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002143 p = d_ancestor(p2, p1);
2144 if (p) {
2145 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2146 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
2147 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 }
2149
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002150 p = d_ancestor(p1, p2);
2151 if (p) {
2152 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2153 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2154 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 }
2156
Ingo Molnarf2eace22006-07-03 00:25:05 -07002157 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2158 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 return NULL;
2160}
2161
2162void unlock_rename(struct dentry *p1, struct dentry *p2)
2163{
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002164 mutex_unlock(&p1->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 if (p1 != p2) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002166 mutex_unlock(&p2->d_inode->i_mutex);
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002167 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 }
2169}
2170
Daniel Rosenberg17285112016-10-26 16:27:45 -07002171int vfs_create2(struct vfsmount *mnt, struct inode *dir, struct dentry *dentry,
2172 umode_t mode, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173{
Daniel Rosenberg17285112016-10-26 16:27:45 -07002174 int error = may_create(mnt, dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
2176 if (error)
2177 return error;
2178
Al Viroacfa4382008-12-04 10:06:33 -05002179 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 return -EACCES; /* shouldn't it be ENOSYS? */
2181 mode &= S_IALLUGO;
2182 mode |= S_IFREG;
2183 error = security_inode_create(dir, dentry, mode);
2184 if (error)
2185 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 error = dir->i_op->create(dir, dentry, mode, nd);
Amir Samuelov6a22e462014-05-26 11:44:06 +03002187 if (error)
2188 return error;
2189
2190 error = security_inode_post_create(dir, dentry, mode);
2191 if (error)
2192 return error;
2193
Stephen Smalleya74574a2005-09-09 13:01:44 -07002194 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002195 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 return error;
2197}
Daniel Rosenberg17285112016-10-26 16:27:45 -07002198EXPORT_SYMBOL(vfs_create2);
2199
2200int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2201 struct nameidata *nd)
2202{
2203 return vfs_create2(NULL, dir, dentry, mode, nd);
2204}
2205EXPORT_SYMBOL(vfs_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
Al Viro73d049a2011-03-11 12:08:24 -05002207static int may_open(struct path *path, int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002209 struct dentry *dentry = path->dentry;
Daniel Rosenberg17285112016-10-26 16:27:45 -07002210 struct vfsmount *mnt = path->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 struct inode *inode = dentry->d_inode;
2212 int error;
2213
Al Virobcda7652011-03-13 16:42:14 -04002214 /* O_PATH? */
2215 if (!acc_mode)
2216 return 0;
2217
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 if (!inode)
2219 return -ENOENT;
2220
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002221 switch (inode->i_mode & S_IFMT) {
2222 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002224 case S_IFDIR:
2225 if (acc_mode & MAY_WRITE)
2226 return -EISDIR;
2227 break;
2228 case S_IFBLK:
2229 case S_IFCHR:
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002230 if (path->mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002232 /*FALLTHRU*/
2233 case S_IFIFO:
2234 case S_IFSOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002236 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08002237 }
Dave Hansenb41572e2007-10-16 23:31:14 -07002238
Daniel Rosenberg17285112016-10-26 16:27:45 -07002239 error = inode_permission2(mnt, inode, acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07002240 if (error)
2241 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05002242
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 /*
2244 * An append-only file must be opened in append mode for writing.
2245 */
2246 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05002247 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05002248 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05002250 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 }
2252
2253 /* O_NOATIME can only be set by the owner or superuser */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07002254 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
Al Viro7715b522009-12-16 03:54:00 -05002255 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
J. Bruce Fieldsf3c7691e2011-09-21 10:58:13 -04002257 return 0;
Al Viro7715b522009-12-16 03:54:00 -05002258}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
Jeff Laytone1181ee2010-12-07 16:19:50 -05002260static int handle_truncate(struct file *filp)
Al Viro7715b522009-12-16 03:54:00 -05002261{
Jeff Laytone1181ee2010-12-07 16:19:50 -05002262 struct path *path = &filp->f_path;
Al Viro7715b522009-12-16 03:54:00 -05002263 struct inode *inode = path->dentry->d_inode;
2264 int error = get_write_access(inode);
2265 if (error)
2266 return error;
2267 /*
2268 * Refuse to truncate files with mandatory locks held on them.
2269 */
2270 error = locks_verify_locked(inode);
2271 if (!error)
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +09002272 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05002273 if (!error) {
Daniel Rosenberga88a18f2016-12-27 16:36:40 -08002274 error = do_truncate2(path->mnt, path->dentry, 0,
Al Viro7715b522009-12-16 03:54:00 -05002275 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
Jeff Laytone1181ee2010-12-07 16:19:50 -05002276 filp);
Al Viro7715b522009-12-16 03:54:00 -05002277 }
2278 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04002279 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280}
2281
Dave Hansend57999e2008-02-15 14:37:27 -08002282static inline int open_to_namei_flags(int flag)
2283{
Al Viro8a5e9292011-06-25 19:15:54 -04002284 if ((flag & O_ACCMODE) == 3)
2285 flag--;
Dave Hansend57999e2008-02-15 14:37:27 -08002286 return flag;
2287}
2288
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002289static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode)
2290{
2291 int error = security_path_mknod(dir, dentry, mode, 0);
2292 if (error)
2293 return error;
2294
2295 error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
2296 if (error)
2297 return error;
2298
2299 return security_inode_create(dir->dentry->d_inode, dentry, mode);
2300}
2301
2302static struct file *atomic_open(struct nameidata *nd, struct dentry *dentry,
Al Virob2ebdc52012-06-22 12:40:19 +04002303 struct path *path, struct file *file,
Miklos Szeredi2f80ad32012-06-05 15:10:27 +02002304 const struct open_flags *op,
Miklos Szeredid6a4ce32012-06-05 15:10:30 +02002305 bool *want_write, bool need_lookup,
Al Viro8915ef32012-06-10 05:01:45 -04002306 int *opened)
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002307{
2308 struct inode *dir = nd->path.dentry->d_inode;
2309 unsigned open_flag = open_to_namei_flags(op->open_flag);
2310 umode_t mode;
2311 int error;
2312 int acc_mode;
Al Viro50ed02d2012-06-22 12:39:14 +04002313 struct file *filp = NULL;
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002314 int create_error = 0;
2315 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
2316
2317 BUG_ON(dentry->d_inode);
2318
2319 /* Don't create child dentry for a dead directory. */
2320 if (unlikely(IS_DEADDIR(dir))) {
2321 filp = ERR_PTR(-ENOENT);
2322 goto out;
2323 }
2324
2325 mode = op->mode & S_IALLUGO;
2326 if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
2327 mode &= ~current_umask();
2328
2329 if (open_flag & O_EXCL) {
2330 open_flag &= ~O_TRUNC;
Al Viro8915ef32012-06-10 05:01:45 -04002331 *opened |= FILE_CREATED;
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002332 }
2333
2334 /*
2335 * Checking write permission is tricky, bacuse we don't know if we are
2336 * going to actually need it: O_CREAT opens should work as long as the
2337 * file exists. But checking existence breaks atomicity. The trick is
2338 * to check access and if not granted clear O_CREAT from the flags.
2339 *
2340 * Another problem is returing the "right" error value (e.g. for an
2341 * O_EXCL open we want to return EEXIST not EROFS).
2342 */
2343 if ((open_flag & (O_CREAT | O_TRUNC)) ||
2344 (open_flag & O_ACCMODE) != O_RDONLY) {
2345 error = mnt_want_write(nd->path.mnt);
2346 if (!error) {
Miklos Szeredid6a4ce32012-06-05 15:10:30 +02002347 *want_write = true;
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002348 } else if (!(open_flag & O_CREAT)) {
2349 /*
2350 * No O_CREATE -> atomicity not a requirement -> fall
2351 * back to lookup + open
2352 */
2353 goto no_open;
2354 } else if (open_flag & (O_EXCL | O_TRUNC)) {
2355 /* Fall back and fail with the right error */
2356 create_error = error;
2357 goto no_open;
2358 } else {
2359 /* No side effects, safe to clear O_CREAT */
2360 create_error = error;
2361 open_flag &= ~O_CREAT;
2362 }
2363 }
2364
2365 if (open_flag & O_CREAT) {
2366 error = may_o_create(&nd->path, dentry, op->mode);
2367 if (error) {
2368 create_error = error;
2369 if (open_flag & O_EXCL)
2370 goto no_open;
2371 open_flag &= ~O_CREAT;
2372 }
2373 }
2374
2375 if (nd->flags & LOOKUP_DIRECTORY)
2376 open_flag |= O_DIRECTORY;
2377
Al Virob2ebdc52012-06-22 12:40:19 +04002378 file->f_path.dentry = DENTRY_NOT_SET;
2379 file->f_path.mnt = nd->path.mnt;
2380 error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode,
Al Viro8915ef32012-06-10 05:01:45 -04002381 opened);
Al Viro50ed02d2012-06-22 12:39:14 +04002382 if (error < 0) {
Al Viro50ed02d2012-06-22 12:39:14 +04002383 if (create_error && error == -ENOENT)
2384 error = create_error;
2385 filp = ERR_PTR(error);
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002386 goto out;
2387 }
2388
2389 acc_mode = op->acc_mode;
Al Viro8915ef32012-06-10 05:01:45 -04002390 if (*opened & FILE_CREATED) {
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002391 fsnotify_create(dir, dentry);
2392 acc_mode = MAY_OPEN;
2393 }
2394
Al Viro50ed02d2012-06-22 12:39:14 +04002395 if (error) { /* returned 1, that is */
Al Virob2ebdc52012-06-22 12:40:19 +04002396 if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002397 filp = ERR_PTR(-EIO);
2398 goto out;
2399 }
Al Virob2ebdc52012-06-22 12:40:19 +04002400 if (file->f_path.dentry) {
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002401 dput(dentry);
Al Virob2ebdc52012-06-22 12:40:19 +04002402 dentry = file->f_path.dentry;
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002403 }
2404 goto looked_up;
2405 }
2406
2407 /*
2408 * We didn't have the inode before the open, so check open permission
2409 * here.
2410 */
Al Virob2ebdc52012-06-22 12:40:19 +04002411 filp = file;
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002412 error = may_open(&filp->f_path, acc_mode, open_flag);
Miklos Szeredi2de99c02012-06-05 15:10:32 +02002413 if (error) {
2414 fput(filp);
2415 filp = ERR_PTR(error);
2416 }
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002417
2418out:
2419 dput(dentry);
2420 return filp;
2421
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002422no_open:
2423 if (need_lookup) {
2424 dentry = lookup_real(dir, dentry, nd);
2425 if (IS_ERR(dentry))
2426 return ERR_CAST(dentry);
2427
2428 if (create_error) {
2429 int open_flag = op->open_flag;
2430
2431 filp = ERR_PTR(create_error);
2432 if ((open_flag & O_EXCL)) {
2433 if (!dentry->d_inode)
2434 goto out;
2435 } else if (!dentry->d_inode) {
2436 goto out;
2437 } else if ((open_flag & O_TRUNC) &&
2438 S_ISREG(dentry->d_inode->i_mode)) {
2439 goto out;
2440 }
2441 /* will fail later, go on to get the right error */
2442 }
2443 }
2444looked_up:
2445 path->dentry = dentry;
2446 path->mnt = nd->path.mnt;
2447 return NULL;
2448}
2449
Nick Piggin31e6b012011-01-07 17:49:52 +11002450/*
Miklos Szeredi8632d4d2012-06-05 15:10:15 +02002451 * Lookup, maybe create and open the last component
2452 *
2453 * Must be called with i_mutex held on parent.
2454 *
2455 * Returns open file or NULL on success, error otherwise. NULL means no open
2456 * was performed, only lookup.
2457 */
2458static struct file *lookup_open(struct nameidata *nd, struct path *path,
Al Virob2ebdc52012-06-22 12:40:19 +04002459 struct file *file,
Miklos Szeredi8632d4d2012-06-05 15:10:15 +02002460 const struct open_flags *op,
Al Viro8915ef32012-06-10 05:01:45 -04002461 bool *want_write, int *opened)
Miklos Szeredi8632d4d2012-06-05 15:10:15 +02002462{
2463 struct dentry *dir = nd->path.dentry;
Miklos Szeredi10606642012-06-05 15:10:16 +02002464 struct inode *dir_inode = dir->d_inode;
Miklos Szeredi8632d4d2012-06-05 15:10:15 +02002465 struct vfsmount *mnt = nd->path.mnt;
2466 struct dentry *dentry;
2467 int error;
Miklos Szeredi10606642012-06-05 15:10:16 +02002468 bool need_lookup;
Miklos Szeredi8632d4d2012-06-05 15:10:15 +02002469
Al Viro8915ef32012-06-10 05:01:45 -04002470 *opened &= ~FILE_CREATED;
Miklos Szeredi10606642012-06-05 15:10:16 +02002471 dentry = lookup_dcache(&nd->last, dir, nd, &need_lookup);
Miklos Szeredi8632d4d2012-06-05 15:10:15 +02002472 if (IS_ERR(dentry))
2473 return ERR_CAST(dentry);
2474
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002475 /* Cached positive dentry: will open in f_op->open */
2476 if (!need_lookup && dentry->d_inode)
2477 goto out_no_open;
2478
2479 if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) {
Al Virob2ebdc52012-06-22 12:40:19 +04002480 return atomic_open(nd, dentry, path, file, op, want_write,
Al Viro8915ef32012-06-10 05:01:45 -04002481 need_lookup, opened);
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002482 }
2483
Miklos Szeredi10606642012-06-05 15:10:16 +02002484 if (need_lookup) {
2485 BUG_ON(dentry->d_inode);
2486
2487 dentry = lookup_real(dir_inode, dentry, nd);
2488 if (IS_ERR(dentry))
2489 return ERR_CAST(dentry);
2490 }
2491
Miklos Szeredi8632d4d2012-06-05 15:10:15 +02002492 /* Negative dentry, just create the file */
2493 if (!dentry->d_inode && (op->open_flag & O_CREAT)) {
2494 umode_t mode = op->mode;
2495 if (!IS_POSIXACL(dir->d_inode))
2496 mode &= ~current_umask();
2497 /*
2498 * This write is needed to ensure that a
2499 * rw->ro transition does not occur between
2500 * the time when the file is created and when
2501 * a permanent write count is taken through
Miklos Szeredi2f80ad32012-06-05 15:10:27 +02002502 * the 'struct file' in finish_open().
Miklos Szeredi8632d4d2012-06-05 15:10:15 +02002503 */
2504 error = mnt_want_write(nd->path.mnt);
2505 if (error)
2506 goto out_dput;
Miklos Szeredid6a4ce32012-06-05 15:10:30 +02002507 *want_write = true;
Al Viro8915ef32012-06-10 05:01:45 -04002508 *opened |= FILE_CREATED;
Miklos Szeredi8632d4d2012-06-05 15:10:15 +02002509 error = security_path_mknod(&nd->path, dentry, mode, 0);
2510 if (error)
2511 goto out_dput;
2512 error = vfs_create2(mnt, dir->d_inode, dentry, mode, nd);
2513 if (error)
2514 goto out_dput;
2515 }
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002516out_no_open:
Miklos Szeredi8632d4d2012-06-05 15:10:15 +02002517 path->dentry = dentry;
2518 path->mnt = nd->path.mnt;
2519 return NULL;
2520
2521out_dput:
2522 dput(dentry);
2523 return ERR_PTR(error);
2524}
2525
2526/*
Al Virofe2d35f2011-03-05 22:58:25 -05002527 * Handle the last step of open()
Nick Piggin31e6b012011-01-07 17:49:52 +11002528 */
Al Virofb1cc552009-12-24 01:58:28 -05002529static struct file *do_last(struct nameidata *nd, struct path *path,
Al Virob2ebdc52012-06-22 12:40:19 +04002530 struct file *file, const struct open_flags *op,
Al Viro8915ef32012-06-10 05:01:45 -04002531 int *opened, const char *pathname)
Al Virofb1cc552009-12-24 01:58:28 -05002532{
Al Viroa1e28032009-12-24 02:12:06 -05002533 struct dentry *dir = nd->path.dentry;
Al Viroca344a892011-03-09 00:36:45 -05002534 int open_flag = op->open_flag;
Miklos Szeredid6a4ce32012-06-05 15:10:30 +02002535 bool will_truncate = (open_flag & O_TRUNC) != 0;
2536 bool want_write = false;
Al Virobcda7652011-03-13 16:42:14 -04002537 int acc_mode = op->acc_mode;
Al Virofb1cc552009-12-24 01:58:28 -05002538 struct file *filp;
Miklos Szeredia5f84da2012-05-21 17:30:07 +02002539 struct inode *inode;
Miklos Szeredid6a4ce32012-06-05 15:10:30 +02002540 bool symlink_ok = false;
Miklos Szerediefbfa632012-05-21 17:30:19 +02002541 struct path save_parent = { .dentry = NULL, .mnt = NULL };
2542 bool retried = false;
Al Viro16c2cd72011-02-22 15:50:10 -05002543 int error;
Al Virofb1cc552009-12-24 01:58:28 -05002544
Al Viroc3e380b2011-02-23 13:39:45 -05002545 nd->flags &= ~LOOKUP_PARENT;
2546 nd->flags |= op->intent;
2547
Al Viro1f36f772009-12-26 10:56:19 -05002548 switch (nd->last_type) {
2549 case LAST_DOTDOT:
Neil Brown176306f2010-05-24 16:57:56 +10002550 case LAST_DOT:
Al Virofe2d35f2011-03-05 22:58:25 -05002551 error = handle_dots(nd, nd->last_type);
2552 if (error)
2553 return ERR_PTR(error);
Al Viro1f36f772009-12-26 10:56:19 -05002554 /* fallthrough */
Al Viro1f36f772009-12-26 10:56:19 -05002555 case LAST_ROOT:
Al Viro9f1fafe2011-03-25 11:00:12 -04002556 error = complete_walk(nd);
Al Viro16c2cd72011-02-22 15:50:10 -05002557 if (error)
Al Viro9f1fafe2011-03-25 11:00:12 -04002558 return ERR_PTR(error);
Al Virofe2d35f2011-03-05 22:58:25 -05002559 audit_inode(pathname, nd->path.dentry);
Al Viroca344a892011-03-09 00:36:45 -05002560 if (open_flag & O_CREAT) {
Al Virofe2d35f2011-03-05 22:58:25 -05002561 error = -EISDIR;
2562 goto exit;
2563 }
Miklos Szeredi8d49e6e2012-06-05 15:10:29 +02002564 goto finish_open;
Al Viro1f36f772009-12-26 10:56:19 -05002565 case LAST_BIND:
Al Viro9f1fafe2011-03-25 11:00:12 -04002566 error = complete_walk(nd);
Al Viro16c2cd72011-02-22 15:50:10 -05002567 if (error)
Al Viro9f1fafe2011-03-25 11:00:12 -04002568 return ERR_PTR(error);
Al Viro1f36f772009-12-26 10:56:19 -05002569 audit_inode(pathname, dir);
Miklos Szeredi8d49e6e2012-06-05 15:10:29 +02002570 goto finish_open;
Al Viro1f36f772009-12-26 10:56:19 -05002571 }
Al Viro67ee3ad2009-12-26 07:01:01 -05002572
Al Viroca344a892011-03-09 00:36:45 -05002573 if (!(open_flag & O_CREAT)) {
Al Virofe2d35f2011-03-05 22:58:25 -05002574 if (nd->last.name[nd->last.len])
2575 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
Al Virobcda7652011-03-13 16:42:14 -04002576 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
Miklos Szeredid6a4ce32012-06-05 15:10:30 +02002577 symlink_ok = true;
Al Virofe2d35f2011-03-05 22:58:25 -05002578 /* we _can_ be in RCU mode here */
Miklos Szeredia5f84da2012-05-21 17:30:07 +02002579 error = lookup_fast(nd, &nd->last, path, &inode);
Miklos Szeredia2ab3642012-06-05 15:10:14 +02002580 if (likely(!error))
2581 goto finish_lookup;
Miklos Szeredia5f84da2012-05-21 17:30:07 +02002582
Miklos Szeredia2ab3642012-06-05 15:10:14 +02002583 if (error < 0)
2584 goto exit;
Miklos Szeredi513072f2012-06-05 15:10:12 +02002585
Miklos Szeredia2ab3642012-06-05 15:10:14 +02002586 BUG_ON(nd->inode != dir->d_inode);
Miklos Szeredi441a4582012-06-05 15:10:13 +02002587 } else {
2588 /* create side of things */
2589 /*
2590 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
2591 * has been cleared when we got to the last component we are
2592 * about to look up
2593 */
2594 error = complete_walk(nd);
2595 if (error)
2596 return ERR_PTR(error);
2597
2598 audit_inode(pathname, dir);
2599 error = -EISDIR;
2600 /* trailing slashes? */
2601 if (nd->last.name[nd->last.len])
2602 goto exit;
Al Virofe2d35f2011-03-05 22:58:25 -05002603 }
2604
Miklos Szerediefbfa632012-05-21 17:30:19 +02002605retry_lookup:
Al Viroa1e28032009-12-24 02:12:06 -05002606 mutex_lock(&dir->d_inode->i_mutex);
Al Virob2ebdc52012-06-22 12:40:19 +04002607 filp = lookup_open(nd, path, file, op, &want_write, opened);
Miklos Szeredi8632d4d2012-06-05 15:10:15 +02002608 mutex_unlock(&dir->d_inode->i_mutex);
Al Viroa1e28032009-12-24 02:12:06 -05002609
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002610 if (filp) {
2611 if (IS_ERR(filp))
2612 goto out;
2613
Al Viro8915ef32012-06-10 05:01:45 -04002614 if ((*opened & FILE_CREATED) ||
2615 !S_ISREG(filp->f_path.dentry->d_inode->i_mode))
Miklos Szeredid6a4ce32012-06-05 15:10:30 +02002616 will_truncate = false;
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002617
2618 audit_inode(pathname, filp->f_path.dentry);
2619 goto opened;
2620 }
Al Virofb1cc552009-12-24 01:58:28 -05002621
Al Viro8915ef32012-06-10 05:01:45 -04002622 if (*opened & FILE_CREATED) {
Al Viro9b44f1b2011-03-09 00:17:27 -05002623 /* Don't check for write permission, don't truncate */
Al Viroca344a892011-03-09 00:36:45 -05002624 open_flag &= ~O_TRUNC;
Miklos Szeredid6a4ce32012-06-05 15:10:30 +02002625 will_truncate = false;
Al Virobcda7652011-03-13 16:42:14 -04002626 acc_mode = MAY_OPEN;
Miklos Szeredi8632d4d2012-06-05 15:10:15 +02002627 path_to_nameidata(path, nd);
Miklos Szeredi8d49e6e2012-06-05 15:10:29 +02002628 goto finish_open_created;
Al Virofb1cc552009-12-24 01:58:28 -05002629 }
2630
2631 /*
2632 * It already exists.
2633 */
Al Virofb1cc552009-12-24 01:58:28 -05002634 audit_inode(pathname, path->dentry);
2635
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002636 /*
2637 * If atomic_open() acquired write access it is dropped now due to
2638 * possible mount and symlink following (this might be optimized away if
2639 * necessary...)
2640 */
2641 if (want_write) {
2642 mnt_drop_write(nd->path.mnt);
Miklos Szeredid6a4ce32012-06-05 15:10:30 +02002643 want_write = false;
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002644 }
2645
Al Virofb1cc552009-12-24 01:58:28 -05002646 error = -EEXIST;
Al Viroca344a892011-03-09 00:36:45 -05002647 if (open_flag & O_EXCL)
Al Virofb1cc552009-12-24 01:58:28 -05002648 goto exit_dput;
2649
David Howells9875cf82011-01-14 18:45:21 +00002650 error = follow_managed(path, nd->flags);
2651 if (error < 0)
2652 goto exit_dput;
Al Virofb1cc552009-12-24 01:58:28 -05002653
Al Viroa3fbbde2011-11-07 21:21:26 +00002654 if (error)
2655 nd->flags |= LOOKUP_JUMPED;
2656
Miklos Szeredi5c7f4512012-05-21 17:30:08 +02002657 BUG_ON(nd->flags & LOOKUP_RCU);
2658 inode = path->dentry->d_inode;
Miklos Szeredi99ec9802012-05-21 17:30:14 +02002659finish_lookup:
2660 /* we _can_ be in RCU mode here */
Al Virofb1cc552009-12-24 01:58:28 -05002661 error = -ENOENT;
Miklos Szeredi16785062012-05-21 17:30:10 +02002662 if (!inode) {
2663 path_to_nameidata(path, nd);
2664 goto exit;
2665 }
Al Viro9e67f362009-12-26 07:04:50 -05002666
Miklos Szeredi458abf22012-05-21 17:30:09 +02002667 if (should_follow_link(inode, !symlink_ok)) {
2668 if (nd->flags & LOOKUP_RCU) {
2669 if (unlikely(unlazy_walk(nd, path->dentry))) {
2670 error = -ECHILD;
2671 goto exit;
2672 }
2673 }
2674 BUG_ON(inode != path->dentry->d_inode);
Al Virofb1cc552009-12-24 01:58:28 -05002675 return NULL;
Miklos Szeredi458abf22012-05-21 17:30:09 +02002676 }
Al Virofb1cc552009-12-24 01:58:28 -05002677
Miklos Szerediefbfa632012-05-21 17:30:19 +02002678 if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) {
2679 path_to_nameidata(path, nd);
2680 } else {
2681 save_parent.dentry = nd->path.dentry;
2682 save_parent.mnt = mntget(path->mnt);
2683 nd->path.dentry = path->dentry;
2684
2685 }
Miklos Szeredi5c7f4512012-05-21 17:30:08 +02002686 nd->inode = inode;
Al Viroa3fbbde2011-11-07 21:21:26 +00002687 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
2688 error = complete_walk(nd);
Miklos Szerediefbfa632012-05-21 17:30:19 +02002689 if (error) {
2690 path_put(&save_parent);
Miklos Szeredi097b1802012-03-06 13:56:33 +01002691 return ERR_PTR(error);
Miklos Szerediefbfa632012-05-21 17:30:19 +02002692 }
Al Virofb1cc552009-12-24 01:58:28 -05002693 error = -EISDIR;
Miklos Szeredic6317212012-05-21 17:30:12 +02002694 if ((open_flag & O_CREAT) && S_ISDIR(nd->inode->i_mode))
Al Virofb1cc552009-12-24 01:58:28 -05002695 goto exit;
Miklos Szerediaba9dce2012-05-21 17:30:11 +02002696 error = -ENOTDIR;
2697 if ((nd->flags & LOOKUP_DIRECTORY) && !nd->inode->i_op->lookup)
2698 goto exit;
Miklos Szeredi60fc85b2012-05-21 17:30:13 +02002699 audit_inode(pathname, nd->path.dentry);
Miklos Szeredi8d49e6e2012-06-05 15:10:29 +02002700finish_open:
Al Viro6c0d46c2011-03-09 00:59:59 -05002701 if (!S_ISREG(nd->inode->i_mode))
Miklos Szeredid6a4ce32012-06-05 15:10:30 +02002702 will_truncate = false;
Al Viro6c0d46c2011-03-09 00:59:59 -05002703
Al Viro0f9d1a12011-03-09 00:13:14 -05002704 if (will_truncate) {
2705 error = mnt_want_write(nd->path.mnt);
2706 if (error)
2707 goto exit;
Miklos Szeredid6a4ce32012-06-05 15:10:30 +02002708 want_write = true;
Al Viro0f9d1a12011-03-09 00:13:14 -05002709 }
Miklos Szeredi8d49e6e2012-06-05 15:10:29 +02002710finish_open_created:
Al Virobcda7652011-03-13 16:42:14 -04002711 error = may_open(&nd->path, acc_mode, open_flag);
Al Viroca344a892011-03-09 00:36:45 -05002712 if (error)
Al Viro0f9d1a12011-03-09 00:13:14 -05002713 goto exit;
Al Virob2ebdc52012-06-22 12:40:19 +04002714 file->f_path.mnt = nd->path.mnt;
2715 error = finish_open(file, nd->path.dentry, NULL, opened);
2716 if (error) {
2717 filp = ERR_PTR(error);
2718 if (error == -EOPENSTALE)
Miklos Szeredi36c27522012-06-05 15:10:31 +02002719 goto stale_open;
Miklos Szeredi2f80ad32012-06-05 15:10:27 +02002720 goto out;
Miklos Szeredi36c27522012-06-05 15:10:31 +02002721 }
Al Virob2ebdc52012-06-22 12:40:19 +04002722 filp = file;
Miklos Szeredi2de99c02012-06-05 15:10:32 +02002723opened:
Miklos Szeredi2f80ad32012-06-05 15:10:27 +02002724 error = open_check_o_direct(filp);
2725 if (error)
2726 goto exit_fput;
Miklos Szeredi4233c212012-06-05 15:10:28 +02002727 error = ima_file_check(filp, op->acc_mode);
2728 if (error)
2729 goto exit_fput;
2730
2731 if (will_truncate) {
2732 error = handle_truncate(filp);
2733 if (error)
2734 goto exit_fput;
Al Viro0f9d1a12011-03-09 00:13:14 -05002735 }
Al Viroca344a892011-03-09 00:36:45 -05002736out:
2737 if (want_write)
Al Viro0f9d1a12011-03-09 00:13:14 -05002738 mnt_drop_write(nd->path.mnt);
Miklos Szerediefbfa632012-05-21 17:30:19 +02002739 path_put(&save_parent);
Miklos Szeredi2aabf6d2012-05-21 17:30:06 +02002740 terminate_walk(nd);
Al Virofb1cc552009-12-24 01:58:28 -05002741 return filp;
2742
Al Virofb1cc552009-12-24 01:58:28 -05002743exit_dput:
2744 path_put_conditional(path, nd);
2745exit:
Al Viroca344a892011-03-09 00:36:45 -05002746 filp = ERR_PTR(error);
2747 goto out;
Miklos Szeredi2f80ad32012-06-05 15:10:27 +02002748exit_fput:
2749 fput(filp);
2750 goto exit;
2751
Miklos Szeredi36c27522012-06-05 15:10:31 +02002752stale_open:
2753 /* If no saved parent or already retried then can't retry */
2754 if (!save_parent.dentry || retried)
2755 goto out;
2756
2757 BUG_ON(save_parent.dentry != dir);
2758 path_put(&nd->path);
2759 nd->path = save_parent;
2760 nd->inode = dir->d_inode;
2761 save_parent.mnt = NULL;
2762 save_parent.dentry = NULL;
2763 if (want_write) {
2764 mnt_drop_write(nd->path.mnt);
2765 want_write = false;
2766 }
2767 retried = true;
2768 goto retry_lookup;
Al Virofb1cc552009-12-24 01:58:28 -05002769}
2770
Al Viro13aab422011-02-23 17:54:08 -05002771static struct file *path_openat(int dfd, const char *pathname,
Al Viro73d049a2011-03-11 12:08:24 -05002772 struct nameidata *nd, const struct open_flags *op, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773{
Al Virofe2d35f2011-03-05 22:58:25 -05002774 struct file *base = NULL;
Al Virob2ebdc52012-06-22 12:40:19 +04002775 struct file *file;
Miklos Szeredi2f80ad32012-06-05 15:10:27 +02002776 struct file *res;
Al Viro9850c052010-01-13 15:01:15 -05002777 struct path path;
Al Viro8915ef32012-06-10 05:01:45 -04002778 int opened = 0;
Al Viro13aab422011-02-23 17:54:08 -05002779 int error;
Nick Piggin31e6b012011-01-07 17:49:52 +11002780
Al Virob2ebdc52012-06-22 12:40:19 +04002781 file = get_empty_filp();
2782 if (!file)
Nick Piggin31e6b012011-01-07 17:49:52 +11002783 return ERR_PTR(-ENFILE);
2784
Al Virob2ebdc52012-06-22 12:40:19 +04002785 file->f_flags = op->open_flag;
Nick Piggin31e6b012011-01-07 17:49:52 +11002786
Al Viro73d049a2011-03-11 12:08:24 -05002787 error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
Nick Piggin31e6b012011-01-07 17:49:52 +11002788 if (unlikely(error))
Al Viro13aab422011-02-23 17:54:08 -05002789 goto out_filp;
Nick Piggin31e6b012011-01-07 17:49:52 +11002790
Al Virofe2d35f2011-03-05 22:58:25 -05002791 current->total_link_count = 0;
Al Viro73d049a2011-03-11 12:08:24 -05002792 error = link_path_walk(pathname, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11002793 if (unlikely(error))
2794 goto out_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795
Al Virob2ebdc52012-06-22 12:40:19 +04002796 res = do_last(nd, &path, file, op, &opened, pathname);
Miklos Szeredi2f80ad32012-06-05 15:10:27 +02002797 while (unlikely(!res)) { /* trailing symlink */
Nick Piggin7b9337a2011-01-14 08:42:43 +00002798 struct path link = path;
Al Virodef4af32009-12-26 08:37:05 -05002799 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04002800 if (!(nd->flags & LOOKUP_FOLLOW)) {
Al Viro73d049a2011-03-11 12:08:24 -05002801 path_put_conditional(&path, nd);
2802 path_put(&nd->path);
Miklos Szeredi2f80ad32012-06-05 15:10:27 +02002803 res = ERR_PTR(-ELOOP);
Al Viro40b39132011-03-09 16:22:18 -05002804 break;
2805 }
Al Viro73d049a2011-03-11 12:08:24 -05002806 nd->flags |= LOOKUP_PARENT;
2807 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
Al Viro574197e2011-03-14 22:20:34 -04002808 error = follow_link(&link, nd, &cookie);
Al Viroc3e380b2011-02-23 13:39:45 -05002809 if (unlikely(error))
Al Viro2c4d7392012-06-10 04:15:17 -04002810 goto out_filp;
Al Virob2ebdc52012-06-22 12:40:19 +04002811 res = do_last(nd, &path, file, op, &opened, pathname);
Al Viro574197e2011-03-14 22:20:34 -04002812 put_link(nd, &link, cookie);
Al Viro806b6812009-12-26 07:16:40 -05002813 }
Al Viro10fa8e62009-12-26 07:09:49 -05002814out:
Al Viro73d049a2011-03-11 12:08:24 -05002815 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
2816 path_put(&nd->root);
Al Virofe2d35f2011-03-05 22:58:25 -05002817 if (base)
2818 fput(base);
Al Viroc3130802012-06-10 05:04:43 -04002819 if (!(opened & FILE_OPENED))
Al Virob2ebdc52012-06-22 12:40:19 +04002820 put_filp(file);
Miklos Szeredi2f80ad32012-06-05 15:10:27 +02002821 if (res == ERR_PTR(-EOPENSTALE)) {
2822 if (flags & LOOKUP_RCU)
2823 res = ERR_PTR(-ECHILD);
2824 else
2825 res = ERR_PTR(-ESTALE);
2826 }
2827 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
Nick Piggin31e6b012011-01-07 17:49:52 +11002829out_filp:
Miklos Szeredi2f80ad32012-06-05 15:10:27 +02002830 res = ERR_PTR(error);
Al Viro10fa8e62009-12-26 07:09:49 -05002831 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832}
2833
Al Viro13aab422011-02-23 17:54:08 -05002834struct file *do_filp_open(int dfd, const char *pathname,
2835 const struct open_flags *op, int flags)
2836{
Al Viro73d049a2011-03-11 12:08:24 -05002837 struct nameidata nd;
Al Viro13aab422011-02-23 17:54:08 -05002838 struct file *filp;
2839
Al Viro73d049a2011-03-11 12:08:24 -05002840 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
Al Viro13aab422011-02-23 17:54:08 -05002841 if (unlikely(filp == ERR_PTR(-ECHILD)))
Al Viro73d049a2011-03-11 12:08:24 -05002842 filp = path_openat(dfd, pathname, &nd, op, flags);
Al Viro13aab422011-02-23 17:54:08 -05002843 if (unlikely(filp == ERR_PTR(-ESTALE)))
Al Viro73d049a2011-03-11 12:08:24 -05002844 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
Al Viro13aab422011-02-23 17:54:08 -05002845 return filp;
2846}
2847
Al Viro73d049a2011-03-11 12:08:24 -05002848struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
2849 const char *name, const struct open_flags *op, int flags)
2850{
2851 struct nameidata nd;
2852 struct file *file;
2853
2854 nd.root.mnt = mnt;
2855 nd.root.dentry = dentry;
2856
2857 flags |= LOOKUP_ROOT;
2858
Al Virobcda7652011-03-13 16:42:14 -04002859 if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
Al Viro73d049a2011-03-11 12:08:24 -05002860 return ERR_PTR(-ELOOP);
2861
2862 file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
2863 if (unlikely(file == ERR_PTR(-ECHILD)))
2864 file = path_openat(-1, name, &nd, op, flags);
2865 if (unlikely(file == ERR_PTR(-ESTALE)))
2866 file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
2867 return file;
2868}
2869
Al Viroed75e952011-06-27 16:53:43 -04002870struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002872 struct dentry *dentry = ERR_PTR(-EEXIST);
Al Viroed75e952011-06-27 16:53:43 -04002873 struct nameidata nd;
2874 int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
2875 if (error)
2876 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002878 /*
2879 * Yucky last component or no last component at all?
2880 * (foo/., foo/.., /////)
2881 */
Al Viroed75e952011-06-27 16:53:43 -04002882 if (nd.last_type != LAST_NORM)
2883 goto out;
2884 nd.flags &= ~LOOKUP_PARENT;
2885 nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002886
2887 /*
2888 * Do the final lookup.
2889 */
Al Viroed75e952011-06-27 16:53:43 -04002890 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2891 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 if (IS_ERR(dentry))
2893 goto fail;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002894
Al Viroe9baf6e2008-05-15 04:49:12 -04002895 if (dentry->d_inode)
2896 goto eexist;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002897 /*
2898 * Special case - lookup gave negative, but... we had foo/bar/
2899 * From the vfs_mknod() POV we just have a negative dentry -
2900 * all is fine. Let's be bastards - you had / on the end, you've
2901 * been asking for (non-existent) directory. -ENOENT for you.
2902 */
Al Viroed75e952011-06-27 16:53:43 -04002903 if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
Al Viroe9baf6e2008-05-15 04:49:12 -04002904 dput(dentry);
2905 dentry = ERR_PTR(-ENOENT);
Al Viroed75e952011-06-27 16:53:43 -04002906 goto fail;
Al Viroe9baf6e2008-05-15 04:49:12 -04002907 }
Al Viroed75e952011-06-27 16:53:43 -04002908 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 return dentry;
Al Viroe9baf6e2008-05-15 04:49:12 -04002910eexist:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 dput(dentry);
Al Viroe9baf6e2008-05-15 04:49:12 -04002912 dentry = ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913fail:
Al Viroed75e952011-06-27 16:53:43 -04002914 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2915out:
2916 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 return dentry;
2918}
Al Virodae6ad82011-06-26 11:50:15 -04002919EXPORT_SYMBOL(kern_path_create);
2920
2921struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir)
2922{
2923 char *tmp = getname(pathname);
2924 struct dentry *res;
2925 if (IS_ERR(tmp))
2926 return ERR_CAST(tmp);
2927 res = kern_path_create(dfd, tmp, path, is_dir);
2928 putname(tmp);
2929 return res;
2930}
2931EXPORT_SYMBOL(user_path_create);
2932
Daniel Rosenberg17285112016-10-26 16:27:45 -07002933int vfs_mknod2(struct vfsmount *mnt, struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934{
Daniel Rosenberg17285112016-10-26 16:27:45 -07002935 int error = may_create(mnt, dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936
2937 if (error)
2938 return error;
2939
Eric W. Biedermanb95e2122011-11-13 12:16:43 -08002940 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 return -EPERM;
2942
Al Viroacfa4382008-12-04 10:06:33 -05002943 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 return -EPERM;
2945
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07002946 error = devcgroup_inode_mknod(mode, dev);
2947 if (error)
2948 return error;
2949
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 error = security_inode_mknod(dir, dentry, mode, dev);
2951 if (error)
2952 return error;
2953
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 error = dir->i_op->mknod(dir, dentry, mode, dev);
Amir Samuelov6a22e462014-05-26 11:44:06 +03002955 if (error)
2956 return error;
2957
2958 error = security_inode_post_create(dir, dentry, mode);
2959 if (error)
2960 return error;
2961
Stephen Smalleya74574a2005-09-09 13:01:44 -07002962 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002963 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 return error;
2965}
Daniel Rosenberg17285112016-10-26 16:27:45 -07002966EXPORT_SYMBOL(vfs_mknod2);
2967
2968int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
2969{
2970 return vfs_mknod2(NULL, dir, dentry, mode, dev);
2971}
2972EXPORT_SYMBOL(vfs_mknod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973
Al Virof69aac02011-07-26 04:31:40 -04002974static int may_mknod(umode_t mode)
Dave Hansen463c3192008-02-15 14:37:57 -08002975{
2976 switch (mode & S_IFMT) {
2977 case S_IFREG:
2978 case S_IFCHR:
2979 case S_IFBLK:
2980 case S_IFIFO:
2981 case S_IFSOCK:
2982 case 0: /* zero mode translates to S_IFREG */
2983 return 0;
2984 case S_IFDIR:
2985 return -EPERM;
2986 default:
2987 return -EINVAL;
2988 }
2989}
2990
Al Viro8208a222011-07-25 17:32:17 -04002991SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002992 unsigned, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993{
Al Viro2ad94ae2008-07-21 09:32:51 -04002994 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04002995 struct path path;
2996 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997
2998 if (S_ISDIR(mode))
2999 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000
Al Virodae6ad82011-06-26 11:50:15 -04003001 dentry = user_path_create(dfd, filename, &path, 0);
3002 if (IS_ERR(dentry))
3003 return PTR_ERR(dentry);
Al Viro2ad94ae2008-07-21 09:32:51 -04003004
Al Virodae6ad82011-06-26 11:50:15 -04003005 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003006 mode &= ~current_umask();
Dave Hansen463c3192008-02-15 14:37:57 -08003007 error = may_mknod(mode);
3008 if (error)
3009 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04003010 error = mnt_want_write(path.mnt);
Dave Hansen463c3192008-02-15 14:37:57 -08003011 if (error)
3012 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04003013 error = security_path_mknod(&path, dentry, mode, dev);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003014 if (error)
3015 goto out_drop_write;
Dave Hansen463c3192008-02-15 14:37:57 -08003016 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 case 0: case S_IFREG:
Daniel Rosenberg17285112016-10-26 16:27:45 -07003018 error = vfs_create2(path.mnt, path.dentry->d_inode,dentry,mode,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 break;
3020 case S_IFCHR: case S_IFBLK:
Daniel Rosenberg17285112016-10-26 16:27:45 -07003021 error = vfs_mknod2(path.mnt, path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 new_decode_dev(dev));
3023 break;
3024 case S_IFIFO: case S_IFSOCK:
Al Virodae6ad82011-06-26 11:50:15 -04003025 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 }
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003028out_drop_write:
Al Virodae6ad82011-06-26 11:50:15 -04003029 mnt_drop_write(path.mnt);
Dave Hansen463c3192008-02-15 14:37:57 -08003030out_dput:
3031 dput(dentry);
Al Virodae6ad82011-06-26 11:50:15 -04003032 mutex_unlock(&path.dentry->d_inode->i_mutex);
3033 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034
3035 return error;
3036}
3037
Al Viro8208a222011-07-25 17:32:17 -04003038SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003039{
3040 return sys_mknodat(AT_FDCWD, filename, mode, dev);
3041}
3042
Daniel Rosenberg17285112016-10-26 16:27:45 -07003043int vfs_mkdir2(struct vfsmount *mnt, struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044{
Daniel Rosenberg17285112016-10-26 16:27:45 -07003045 int error = may_create(mnt, dir, dentry);
Al Viro8de52772012-02-06 12:45:27 -05003046 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047
3048 if (error)
3049 return error;
3050
Al Viroacfa4382008-12-04 10:06:33 -05003051 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 return -EPERM;
3053
3054 mode &= (S_IRWXUGO|S_ISVTX);
3055 error = security_inode_mkdir(dir, dentry, mode);
3056 if (error)
3057 return error;
3058
Al Viro8de52772012-02-06 12:45:27 -05003059 if (max_links && dir->i_nlink >= max_links)
3060 return -EMLINK;
3061
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003063 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003064 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 return error;
3066}
Daniel Rosenberg17285112016-10-26 16:27:45 -07003067EXPORT_SYMBOL(vfs_mkdir2);
3068
3069int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
3070{
3071 return vfs_mkdir2(NULL, dir, dentry, mode);
3072}
3073EXPORT_SYMBOL(vfs_mkdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074
Al Viroa218d0f2011-11-21 14:59:34 -05003075SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076{
Dave Hansen6902d922006-09-30 23:29:01 -07003077 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003078 struct path path;
3079 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080
Al Virodae6ad82011-06-26 11:50:15 -04003081 dentry = user_path_create(dfd, pathname, &path, 1);
Dave Hansen6902d922006-09-30 23:29:01 -07003082 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003083 return PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003084
Al Virodae6ad82011-06-26 11:50:15 -04003085 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003086 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003087 error = mnt_want_write(path.mnt);
Dave Hansen463c3192008-02-15 14:37:57 -08003088 if (error)
3089 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04003090 error = security_path_mkdir(&path, dentry, mode);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003091 if (error)
3092 goto out_drop_write;
Daniel Rosenberg17285112016-10-26 16:27:45 -07003093 error = vfs_mkdir2(path.mnt, path.dentry->d_inode, dentry, mode);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003094out_drop_write:
Al Virodae6ad82011-06-26 11:50:15 -04003095 mnt_drop_write(path.mnt);
Dave Hansen463c3192008-02-15 14:37:57 -08003096out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07003097 dput(dentry);
Al Virodae6ad82011-06-26 11:50:15 -04003098 mutex_unlock(&path.dentry->d_inode->i_mutex);
3099 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 return error;
3101}
3102
Al Viroa218d0f2011-11-21 14:59:34 -05003103SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003104{
3105 return sys_mkdirat(AT_FDCWD, pathname, mode);
3106}
3107
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108/*
Sage Weila71905f2011-05-24 13:06:08 -07003109 * The dentry_unhash() helper will try to drop the dentry early: we
J. Bruce Fieldsc0d02592012-02-15 11:48:40 -05003110 * should have a usage count of 1 if we're the only user of this
Sage Weila71905f2011-05-24 13:06:08 -07003111 * dentry, and if that is true (possibly after pruning the dcache),
3112 * then we drop the dentry now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 *
3114 * A low-level filesystem can, if it choses, legally
3115 * do a
3116 *
3117 * if (!d_unhashed(dentry))
3118 * return -EBUSY;
3119 *
3120 * if it cannot handle the case of removing a directory
3121 * that is still in use by something else..
3122 */
3123void dentry_unhash(struct dentry *dentry)
3124{
Vasily Averindc168422006-12-06 20:37:07 -08003125 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 spin_lock(&dentry->d_lock);
Sage Weil64252c72011-05-24 13:06:05 -07003127 if (dentry->d_count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 __d_drop(dentry);
3129 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130}
3131
Daniel Rosenberg17285112016-10-26 16:27:45 -07003132int vfs_rmdir2(struct vfsmount *mnt, struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133{
Daniel Rosenberg17285112016-10-26 16:27:45 -07003134 int error = may_delete(mnt, dir, dentry, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135
3136 if (error)
3137 return error;
3138
Al Viroacfa4382008-12-04 10:06:33 -05003139 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 return -EPERM;
3141
Al Viro1d2ef592011-09-14 18:55:41 +01003142 dget(dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003143 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144
Sage Weil912dbc12011-05-24 13:06:11 -07003145 error = -EBUSY;
3146 if (d_mountpoint(dentry))
3147 goto out;
3148
3149 error = security_inode_rmdir(dir, dentry);
3150 if (error)
3151 goto out;
3152
Sage Weil3cebde22011-05-29 21:20:59 -07003153 shrink_dcache_parent(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003154 error = dir->i_op->rmdir(dir, dentry);
3155 if (error)
3156 goto out;
3157
3158 dentry->d_inode->i_flags |= S_DEAD;
3159 dont_mount(dentry);
3160
3161out:
3162 mutex_unlock(&dentry->d_inode->i_mutex);
Al Viro1d2ef592011-09-14 18:55:41 +01003163 dput(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003164 if (!error)
3165 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 return error;
3167}
Daniel Rosenberg17285112016-10-26 16:27:45 -07003168EXPORT_SYMBOL(vfs_rmdir2);
3169
3170int vfs_rmdir(struct inode *dir, struct dentry *dentry)
3171{
3172 return vfs_rmdir2(NULL, dir, dentry);
3173}
3174EXPORT_SYMBOL(vfs_rmdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003176static long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177{
3178 int error = 0;
3179 char * name;
3180 struct dentry *dentry;
3181 struct nameidata nd;
3182
Al Viro2ad94ae2008-07-21 09:32:51 -04003183 error = user_path_parent(dfd, pathname, &nd, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04003185 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186
3187 switch(nd.last_type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003188 case LAST_DOTDOT:
3189 error = -ENOTEMPTY;
3190 goto exit1;
3191 case LAST_DOT:
3192 error = -EINVAL;
3193 goto exit1;
3194 case LAST_ROOT:
3195 error = -EBUSY;
3196 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003198
3199 nd.flags &= ~LOOKUP_PARENT;
3200
Jan Blunck4ac91372008-02-14 19:34:32 -08003201 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08003202 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003204 if (IS_ERR(dentry))
3205 goto exit2;
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04003206 if (!dentry->d_inode) {
3207 error = -ENOENT;
3208 goto exit3;
3209 }
Dave Hansen06227532008-02-15 14:37:34 -08003210 error = mnt_want_write(nd.path.mnt);
3211 if (error)
3212 goto exit3;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003213 error = security_path_rmdir(&nd.path, dentry);
3214 if (error)
3215 goto exit4;
Daniel Rosenberg17285112016-10-26 16:27:45 -07003216 error = vfs_rmdir2(nd.path.mnt, nd.path.dentry->d_inode, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003217exit4:
Dave Hansen06227532008-02-15 14:37:34 -08003218 mnt_drop_write(nd.path.mnt);
3219exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07003220 dput(dentry);
3221exit2:
Jan Blunck4ac91372008-02-14 19:34:32 -08003222 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003224 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 putname(name);
3226 return error;
3227}
3228
Heiko Carstens3cdad422009-01-14 14:14:22 +01003229SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003230{
3231 return do_rmdir(AT_FDCWD, pathname);
3232}
3233
Daniel Rosenberg17285112016-10-26 16:27:45 -07003234int vfs_unlink2(struct vfsmount *mnt, struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235{
Daniel Rosenberg17285112016-10-26 16:27:45 -07003236 int error = may_delete(mnt, dir, dentry, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237
3238 if (error)
3239 return error;
3240
Al Viroacfa4382008-12-04 10:06:33 -05003241 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 return -EPERM;
3243
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003244 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 if (d_mountpoint(dentry))
3246 error = -EBUSY;
3247 else {
3248 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05003249 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 error = dir->i_op->unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05003251 if (!error)
Al Virod83c49f2010-04-30 17:17:09 -04003252 dont_mount(dentry);
Al Virobec10522010-03-03 14:12:08 -05003253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003255 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256
3257 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
3258 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
Jan Karaece95912008-02-06 01:37:13 -08003259 fsnotify_link_count(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 }
Robert Love0eeca282005-07-12 17:06:03 -04003262
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 return error;
3264}
Daniel Rosenberg17285112016-10-26 16:27:45 -07003265EXPORT_SYMBOL(vfs_unlink2);
3266
3267int vfs_unlink(struct inode *dir, struct dentry *dentry)
3268{
3269 return vfs_unlink2(NULL, dir, dentry);
3270}
3271EXPORT_SYMBOL(vfs_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272
3273/*
3274 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003275 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 * writeout happening, and we don't want to prevent access to the directory
3277 * while waiting on the I/O.
3278 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003279static long do_unlinkat(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280{
Al Viro2ad94ae2008-07-21 09:32:51 -04003281 int error;
3282 char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 struct dentry *dentry;
3284 struct nameidata nd;
3285 struct inode *inode = NULL;
3286
Al Viro2ad94ae2008-07-21 09:32:51 -04003287 error = user_path_parent(dfd, pathname, &nd, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04003289 return error;
3290
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 error = -EISDIR;
3292 if (nd.last_type != LAST_NORM)
3293 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003294
3295 nd.flags &= ~LOOKUP_PARENT;
3296
Jan Blunck4ac91372008-02-14 19:34:32 -08003297 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08003298 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 error = PTR_ERR(dentry);
3300 if (!IS_ERR(dentry)) {
3301 /* Why not before? Because we want correct error value */
Török Edwin50338b82011-06-16 00:06:14 +03003302 if (nd.last.name[nd.last.len])
3303 goto slashes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 inode = dentry->d_inode;
Török Edwin50338b82011-06-16 00:06:14 +03003305 if (!inode)
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04003306 goto slashes;
3307 ihold(inode);
Dave Hansen06227532008-02-15 14:37:34 -08003308 error = mnt_want_write(nd.path.mnt);
3309 if (error)
3310 goto exit2;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003311 error = security_path_unlink(&nd.path, dentry);
3312 if (error)
3313 goto exit3;
Daniel Rosenberg17285112016-10-26 16:27:45 -07003314 error = vfs_unlink2(nd.path.mnt, nd.path.dentry->d_inode, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003315exit3:
Dave Hansen06227532008-02-15 14:37:34 -08003316 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 exit2:
3318 dput(dentry);
3319 }
Jan Blunck4ac91372008-02-14 19:34:32 -08003320 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 if (inode)
3322 iput(inode); /* truncate the inode here */
3323exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003324 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 putname(name);
3326 return error;
3327
3328slashes:
3329 error = !dentry->d_inode ? -ENOENT :
3330 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
3331 goto exit2;
3332}
3333
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003334SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003335{
3336 if ((flag & ~AT_REMOVEDIR) != 0)
3337 return -EINVAL;
3338
3339 if (flag & AT_REMOVEDIR)
3340 return do_rmdir(dfd, pathname);
3341
3342 return do_unlinkat(dfd, pathname);
3343}
3344
Heiko Carstens3480b252009-01-14 14:14:16 +01003345SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003346{
3347 return do_unlinkat(AT_FDCWD, pathname);
3348}
3349
Daniel Rosenberg17285112016-10-26 16:27:45 -07003350int vfs_symlink2(struct vfsmount *mnt, struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351{
Daniel Rosenberg17285112016-10-26 16:27:45 -07003352 int error = may_create(mnt, dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353
3354 if (error)
3355 return error;
3356
Al Viroacfa4382008-12-04 10:06:33 -05003357 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 return -EPERM;
3359
3360 error = security_inode_symlink(dir, dentry, oldname);
3361 if (error)
3362 return error;
3363
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003365 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003366 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 return error;
3368}
Daniel Rosenberg17285112016-10-26 16:27:45 -07003369EXPORT_SYMBOL(vfs_symlink2);
3370
3371int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
3372{
3373 return vfs_symlink2(NULL, dir, dentry, oldname);
3374}
3375EXPORT_SYMBOL(vfs_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003377SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
3378 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379{
Al Viro2ad94ae2008-07-21 09:32:51 -04003380 int error;
3381 char *from;
Dave Hansen6902d922006-09-30 23:29:01 -07003382 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003383 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384
3385 from = getname(oldname);
Al Viro2ad94ae2008-07-21 09:32:51 -04003386 if (IS_ERR(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 return PTR_ERR(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04003388
Al Virodae6ad82011-06-26 11:50:15 -04003389 dentry = user_path_create(newdfd, newname, &path, 0);
Dave Hansen6902d922006-09-30 23:29:01 -07003390 error = PTR_ERR(dentry);
3391 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003392 goto out_putname;
Dave Hansen6902d922006-09-30 23:29:01 -07003393
Al Virodae6ad82011-06-26 11:50:15 -04003394 error = mnt_want_write(path.mnt);
Dave Hansen75c3f292008-02-15 14:37:45 -08003395 if (error)
3396 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04003397 error = security_path_symlink(&path, dentry, from);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003398 if (error)
3399 goto out_drop_write;
Daniel Rosenberg17285112016-10-26 16:27:45 -07003400 error = vfs_symlink2(path.mnt, path.dentry->d_inode, dentry, from);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003401out_drop_write:
Al Virodae6ad82011-06-26 11:50:15 -04003402 mnt_drop_write(path.mnt);
Dave Hansen75c3f292008-02-15 14:37:45 -08003403out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07003404 dput(dentry);
Al Virodae6ad82011-06-26 11:50:15 -04003405 mutex_unlock(&path.dentry->d_inode->i_mutex);
3406 path_put(&path);
Dave Hansen6902d922006-09-30 23:29:01 -07003407out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 putname(from);
3409 return error;
3410}
3411
Heiko Carstens3480b252009-01-14 14:14:16 +01003412SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003413{
3414 return sys_symlinkat(oldname, AT_FDCWD, newname);
3415}
3416
Daniel Rosenberg17285112016-10-26 16:27:45 -07003417int vfs_link2(struct vfsmount *mnt, struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418{
3419 struct inode *inode = old_dentry->d_inode;
Al Viro8de52772012-02-06 12:45:27 -05003420 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 int error;
3422
3423 if (!inode)
3424 return -ENOENT;
3425
Daniel Rosenberg17285112016-10-26 16:27:45 -07003426 error = may_create(mnt, dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 if (error)
3428 return error;
3429
3430 if (dir->i_sb != inode->i_sb)
3431 return -EXDEV;
3432
3433 /*
3434 * A link to an append-only or immutable file cannot be created.
3435 */
3436 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3437 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05003438 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003440 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 return -EPERM;
3442
3443 error = security_inode_link(old_dentry, dir, new_dentry);
3444 if (error)
3445 return error;
3446
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003447 mutex_lock(&inode->i_mutex);
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05303448 /* Make sure we don't allow creating hardlink to an unlinked file */
3449 if (inode->i_nlink == 0)
3450 error = -ENOENT;
Al Viro8de52772012-02-06 12:45:27 -05003451 else if (max_links && inode->i_nlink >= max_links)
3452 error = -EMLINK;
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05303453 else
3454 error = dir->i_op->link(old_dentry, dir, new_dentry);
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003455 mutex_unlock(&inode->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07003456 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003457 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 return error;
3459}
Daniel Rosenberg17285112016-10-26 16:27:45 -07003460EXPORT_SYMBOL(vfs_link2);
3461
3462int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
3463{
3464 return vfs_link2(NULL, old_dentry, dir, new_dentry);
3465}
3466EXPORT_SYMBOL(vfs_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467
3468/*
3469 * Hardlinks are often used in delicate situations. We avoid
3470 * security-related surprises by not following symlinks on the
3471 * newname. --KAB
3472 *
3473 * We don't follow them on the oldname either to be compatible
3474 * with linux 2.0, and to avoid hard-linking to directories
3475 * and other special files. --ADM
3476 */
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003477SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
3478 int, newdfd, const char __user *, newname, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479{
3480 struct dentry *new_dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003481 struct path old_path, new_path;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303482 int how = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303485 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003486 return -EINVAL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303487 /*
3488 * To use null names we require CAP_DAC_READ_SEARCH
3489 * This ensures that not everyone will be able to create
3490 * handlink using the passed filedescriptor.
3491 */
3492 if (flags & AT_EMPTY_PATH) {
3493 if (!capable(CAP_DAC_READ_SEARCH))
3494 return -ENOENT;
3495 how = LOOKUP_EMPTY;
3496 }
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003497
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303498 if (flags & AT_SYMLINK_FOLLOW)
3499 how |= LOOKUP_FOLLOW;
3500
3501 error = user_path_at(olddfd, oldname, how, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04003503 return error;
3504
Al Virodae6ad82011-06-26 11:50:15 -04003505 new_dentry = user_path_create(newdfd, newname, &new_path, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003507 if (IS_ERR(new_dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003508 goto out;
3509
3510 error = -EXDEV;
3511 if (old_path.mnt != new_path.mnt)
3512 goto out_dput;
3513 error = mnt_want_write(new_path.mnt);
Dave Hansen75c3f292008-02-15 14:37:45 -08003514 if (error)
3515 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04003516 error = security_path_link(old_path.dentry, &new_path, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003517 if (error)
3518 goto out_drop_write;
Daniel Rosenberg17285112016-10-26 16:27:45 -07003519 error = vfs_link2(old_path.mnt, old_path.dentry, new_path.dentry->d_inode, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003520out_drop_write:
Al Virodae6ad82011-06-26 11:50:15 -04003521 mnt_drop_write(new_path.mnt);
Dave Hansen75c3f292008-02-15 14:37:45 -08003522out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07003523 dput(new_dentry);
Al Virodae6ad82011-06-26 11:50:15 -04003524 mutex_unlock(&new_path.dentry->d_inode->i_mutex);
3525 path_put(&new_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526out:
Al Viro2d8f3032008-07-22 09:59:21 -04003527 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528
3529 return error;
3530}
3531
Heiko Carstens3480b252009-01-14 14:14:16 +01003532SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003533{
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003534 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003535}
3536
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537/*
3538 * The worst of all namespace operations - renaming directory. "Perverted"
3539 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
3540 * Problems:
3541 * a) we can get into loop creation. Check is done in is_subdir().
3542 * b) race potential - two innocent renames can create a loop together.
3543 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08003544 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 * story.
3546 * c) we have to lock _three_ objects - parents and victim (if it exists).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003547 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 * whether the target exists). Solution: try to be smart with locking
3549 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08003550 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 * move will be locked. Thus we can rank directories by the tree
3552 * (ancestors first) and rank all non-directories after them.
3553 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08003554 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 * HOWEVER, it relies on the assumption that any object with ->lookup()
3556 * has no more than 1 dentry. If "hybrid" objects will ever appear,
3557 * we'd better make sure that there's no link(2) for them.
Sage Weile4eaac02011-05-24 13:06:07 -07003558 * d) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003559 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05003561 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 * locking].
3563 */
Daniel Rosenberg17285112016-10-26 16:27:45 -07003564static int vfs_rename_dir(struct vfsmount *mnt,
3565 struct inode *old_dir, struct dentry *old_dentry,
3566 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567{
3568 int error = 0;
Sage Weil9055cba2011-05-24 13:06:12 -07003569 struct inode *target = new_dentry->d_inode;
Al Viro8de52772012-02-06 12:45:27 -05003570 unsigned max_links = new_dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571
3572 /*
3573 * If we are going to change the parent - check write permissions,
3574 * we'll need to flip '..'.
3575 */
3576 if (new_dir != old_dir) {
Daniel Rosenberg17285112016-10-26 16:27:45 -07003577 error = inode_permission2(mnt, old_dentry->d_inode, MAY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 if (error)
3579 return error;
3580 }
3581
3582 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3583 if (error)
3584 return error;
3585
Al Viro1d2ef592011-09-14 18:55:41 +01003586 dget(new_dentry);
Al Virod83c49f2010-04-30 17:17:09 -04003587 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003588 mutex_lock(&target->i_mutex);
Sage Weil9055cba2011-05-24 13:06:12 -07003589
3590 error = -EBUSY;
3591 if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
3592 goto out;
3593
Al Viro8de52772012-02-06 12:45:27 -05003594 error = -EMLINK;
3595 if (max_links && !target && new_dir != old_dir &&
3596 new_dir->i_nlink >= max_links)
3597 goto out;
3598
Sage Weil3cebde22011-05-29 21:20:59 -07003599 if (target)
3600 shrink_dcache_parent(new_dentry);
Sage Weil9055cba2011-05-24 13:06:12 -07003601 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3602 if (error)
3603 goto out;
3604
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 if (target) {
Sage Weil9055cba2011-05-24 13:06:12 -07003606 target->i_flags |= S_DEAD;
3607 dont_mount(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 }
Sage Weil9055cba2011-05-24 13:06:12 -07003609out:
3610 if (target)
3611 mutex_unlock(&target->i_mutex);
Al Viro1d2ef592011-09-14 18:55:41 +01003612 dput(new_dentry);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07003613 if (!error)
Mark Fasheh349457c2006-09-08 14:22:21 -07003614 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3615 d_move(old_dentry,new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 return error;
3617}
3618
Adrian Bunk75c96f82005-05-05 16:16:09 -07003619static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
3620 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621{
Sage Weil51892bb2011-05-24 13:06:13 -07003622 struct inode *target = new_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 int error;
3624
3625 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3626 if (error)
3627 return error;
3628
3629 dget(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003631 mutex_lock(&target->i_mutex);
Sage Weil51892bb2011-05-24 13:06:13 -07003632
3633 error = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
Sage Weil51892bb2011-05-24 13:06:13 -07003635 goto out;
3636
3637 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3638 if (error)
3639 goto out;
3640
3641 if (target)
3642 dont_mount(new_dentry);
3643 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3644 d_move(old_dentry, new_dentry);
3645out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003647 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 dput(new_dentry);
3649 return error;
3650}
3651
Daniel Rosenberg17285112016-10-26 16:27:45 -07003652int vfs_rename2(struct vfsmount *mnt,
3653 struct inode *old_dir, struct dentry *old_dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 struct inode *new_dir, struct dentry *new_dentry)
3655{
3656 int error;
3657 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
Eric Paris59b0df22010-02-08 12:53:52 -05003658 const unsigned char *old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659
3660 if (old_dentry->d_inode == new_dentry->d_inode)
3661 return 0;
3662
Daniel Rosenberg17285112016-10-26 16:27:45 -07003663 error = may_delete(mnt, old_dir, old_dentry, is_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 if (error)
3665 return error;
3666
3667 if (!new_dentry->d_inode)
Daniel Rosenberg17285112016-10-26 16:27:45 -07003668 error = may_create(mnt, new_dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 else
Daniel Rosenberg17285112016-10-26 16:27:45 -07003670 error = may_delete(mnt, new_dir, new_dentry, is_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 if (error)
3672 return error;
3673
Al Viroacfa4382008-12-04 10:06:33 -05003674 if (!old_dir->i_op->rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 return -EPERM;
3676
Robert Love0eeca282005-07-12 17:06:03 -04003677 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
3678
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 if (is_dir)
Daniel Rosenberg17285112016-10-26 16:27:45 -07003680 error = vfs_rename_dir(mnt, old_dir,old_dentry,new_dir,new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 else
3682 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
Al Viro123df292009-12-25 04:57:57 -05003683 if (!error)
3684 fsnotify_move(old_dir, new_dir, old_name, is_dir,
Al Viro5a190ae2007-06-07 12:19:32 -04003685 new_dentry->d_inode, old_dentry);
Robert Love0eeca282005-07-12 17:06:03 -04003686 fsnotify_oldname_free(old_name);
3687
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 return error;
3689}
Daniel Rosenberg17285112016-10-26 16:27:45 -07003690EXPORT_SYMBOL(vfs_rename2);
3691
3692int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
3693 struct inode *new_dir, struct dentry *new_dentry)
3694{
3695 return vfs_rename2(NULL, old_dir, old_dentry, new_dir, new_dentry);
3696}
3697EXPORT_SYMBOL(vfs_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003699SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
3700 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701{
Al Viro2ad94ae2008-07-21 09:32:51 -04003702 struct dentry *old_dir, *new_dir;
3703 struct dentry *old_dentry, *new_dentry;
3704 struct dentry *trap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 struct nameidata oldnd, newnd;
Al Viro2ad94ae2008-07-21 09:32:51 -04003706 char *from;
3707 char *to;
3708 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709
Al Viro2ad94ae2008-07-21 09:32:51 -04003710 error = user_path_parent(olddfd, oldname, &oldnd, &from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 if (error)
3712 goto exit;
3713
Al Viro2ad94ae2008-07-21 09:32:51 -04003714 error = user_path_parent(newdfd, newname, &newnd, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 if (error)
3716 goto exit1;
3717
3718 error = -EXDEV;
Jan Blunck4ac91372008-02-14 19:34:32 -08003719 if (oldnd.path.mnt != newnd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 goto exit2;
3721
Jan Blunck4ac91372008-02-14 19:34:32 -08003722 old_dir = oldnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 error = -EBUSY;
3724 if (oldnd.last_type != LAST_NORM)
3725 goto exit2;
3726
Jan Blunck4ac91372008-02-14 19:34:32 -08003727 new_dir = newnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 if (newnd.last_type != LAST_NORM)
3729 goto exit2;
3730
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003731 oldnd.flags &= ~LOOKUP_PARENT;
3732 newnd.flags &= ~LOOKUP_PARENT;
OGAWA Hirofumi4e9ed2f2008-10-16 07:50:29 +09003733 newnd.flags |= LOOKUP_RENAME_TARGET;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003734
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 trap = lock_rename(new_dir, old_dir);
3736
Christoph Hellwig49705b72005-11-08 21:35:06 -08003737 old_dentry = lookup_hash(&oldnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 error = PTR_ERR(old_dentry);
3739 if (IS_ERR(old_dentry))
3740 goto exit3;
3741 /* source must exist */
3742 error = -ENOENT;
3743 if (!old_dentry->d_inode)
3744 goto exit4;
3745 /* unless the source is a directory trailing slashes give -ENOTDIR */
3746 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
3747 error = -ENOTDIR;
3748 if (oldnd.last.name[oldnd.last.len])
3749 goto exit4;
3750 if (newnd.last.name[newnd.last.len])
3751 goto exit4;
3752 }
3753 /* source should not be ancestor of target */
3754 error = -EINVAL;
3755 if (old_dentry == trap)
3756 goto exit4;
Christoph Hellwig49705b72005-11-08 21:35:06 -08003757 new_dentry = lookup_hash(&newnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 error = PTR_ERR(new_dentry);
3759 if (IS_ERR(new_dentry))
3760 goto exit4;
3761 /* target should not be an ancestor of source */
3762 error = -ENOTEMPTY;
3763 if (new_dentry == trap)
3764 goto exit5;
3765
Dave Hansen9079b1e2008-02-15 14:37:49 -08003766 error = mnt_want_write(oldnd.path.mnt);
3767 if (error)
3768 goto exit5;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003769 error = security_path_rename(&oldnd.path, old_dentry,
3770 &newnd.path, new_dentry);
3771 if (error)
3772 goto exit6;
Daniel Rosenberg17285112016-10-26 16:27:45 -07003773 error = vfs_rename2(oldnd.path.mnt, old_dir->d_inode, old_dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 new_dir->d_inode, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003775exit6:
Dave Hansen9079b1e2008-02-15 14:37:49 -08003776 mnt_drop_write(oldnd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777exit5:
3778 dput(new_dentry);
3779exit4:
3780 dput(old_dentry);
3781exit3:
3782 unlock_rename(new_dir, old_dir);
3783exit2:
Jan Blunck1d957f92008-02-14 19:34:35 -08003784 path_put(&newnd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04003785 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003787 path_put(&oldnd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 putname(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04003789exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 return error;
3791}
3792
Heiko Carstensa26eab22009-01-14 14:14:17 +01003793SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003794{
3795 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
3796}
3797
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
3799{
3800 int len;
3801
3802 len = PTR_ERR(link);
3803 if (IS_ERR(link))
3804 goto out;
3805
3806 len = strlen(link);
3807 if (len > (unsigned) buflen)
3808 len = buflen;
3809 if (copy_to_user(buffer, link, len))
3810 len = -EFAULT;
3811out:
3812 return len;
3813}
3814
3815/*
3816 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
3817 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
3818 * using) it for any given inode is up to filesystem.
3819 */
3820int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3821{
3822 struct nameidata nd;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003823 void *cookie;
Marcin Slusarz694a1762008-06-09 16:40:37 -07003824 int res;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003825
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 nd.depth = 0;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003827 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
Marcin Slusarz694a1762008-06-09 16:40:37 -07003828 if (IS_ERR(cookie))
3829 return PTR_ERR(cookie);
3830
3831 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
3832 if (dentry->d_inode->i_op->put_link)
3833 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3834 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835}
3836
3837int vfs_follow_link(struct nameidata *nd, const char *link)
3838{
3839 return __vfs_follow_link(nd, link);
3840}
3841
3842/* get the link contents into pagecache */
3843static char *page_getlink(struct dentry * dentry, struct page **ppage)
3844{
Duane Griffinebd09ab2008-12-19 20:47:12 +00003845 char *kaddr;
3846 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 struct address_space *mapping = dentry->d_inode->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -07003848 page = read_mapping_page(mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 if (IS_ERR(page))
Nick Piggin6fe69002007-05-06 14:49:04 -07003850 return (char*)page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 *ppage = page;
Duane Griffinebd09ab2008-12-19 20:47:12 +00003852 kaddr = kmap(page);
3853 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3854 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855}
3856
3857int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3858{
3859 struct page *page = NULL;
3860 char *s = page_getlink(dentry, &page);
3861 int res = vfs_readlink(dentry,buffer,buflen,s);
3862 if (page) {
3863 kunmap(page);
3864 page_cache_release(page);
3865 }
3866 return res;
3867}
3868
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003869void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003871 struct page *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 nd_set_link(nd, page_getlink(dentry, &page));
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003873 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874}
3875
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003876void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003878 struct page *page = cookie;
3879
3880 if (page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 kunmap(page);
3882 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 }
3884}
3885
Nick Piggin54566b22009-01-04 12:00:53 -08003886/*
3887 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
3888 */
3889int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890{
3891 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003892 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07003893 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08003894 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 char *kaddr;
Nick Piggin54566b22009-01-04 12:00:53 -08003896 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
3897 if (nofs)
3898 flags |= AOP_FLAG_NOFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899
NeilBrown7e53cac2006-03-25 03:07:57 -08003900retry:
Nick Pigginafddba42007-10-16 01:25:01 -07003901 err = pagecache_write_begin(NULL, mapping, 0, len-1,
Nick Piggin54566b22009-01-04 12:00:53 -08003902 flags, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07003904 goto fail;
3905
Cong Wange8e3c3d2011-11-25 23:14:27 +08003906 kaddr = kmap_atomic(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 memcpy(kaddr, symname, len-1);
Cong Wange8e3c3d2011-11-25 23:14:27 +08003908 kunmap_atomic(kaddr);
Nick Pigginafddba42007-10-16 01:25:01 -07003909
3910 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3911 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 if (err < 0)
3913 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07003914 if (err < len-1)
3915 goto retry;
3916
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 mark_inode_dirty(inode);
3918 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919fail:
3920 return err;
3921}
3922
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003923int page_symlink(struct inode *inode, const char *symname, int len)
3924{
3925 return __page_symlink(inode, symname, len,
Nick Piggin54566b22009-01-04 12:00:53 -08003926 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003927}
3928
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003929const struct inode_operations page_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 .readlink = generic_readlink,
3931 .follow_link = page_follow_link_light,
3932 .put_link = page_put_link,
3933};
3934
Al Viro2d8f3032008-07-22 09:59:21 -04003935EXPORT_SYMBOL(user_path_at);
David Howellscc53ce52011-01-14 18:45:26 +00003936EXPORT_SYMBOL(follow_down_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937EXPORT_SYMBOL(follow_down);
3938EXPORT_SYMBOL(follow_up);
3939EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
3940EXPORT_SYMBOL(getname);
3941EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942EXPORT_SYMBOL(page_follow_link_light);
3943EXPORT_SYMBOL(page_put_link);
3944EXPORT_SYMBOL(page_readlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003945EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946EXPORT_SYMBOL(page_symlink);
3947EXPORT_SYMBOL(page_symlink_inode_operations);
Al Virod1811462008-08-02 00:49:18 -04003948EXPORT_SYMBOL(kern_path);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07003949EXPORT_SYMBOL(vfs_path_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950EXPORT_SYMBOL(unlock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951EXPORT_SYMBOL(vfs_follow_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952EXPORT_SYMBOL(generic_permission);
3953EXPORT_SYMBOL(vfs_readlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954EXPORT_SYMBOL(dentry_unhash);
3955EXPORT_SYMBOL(generic_readlink);