blob: 9de8683fa4a052801429f6dfe8c1b4f5b2906e91 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/namei.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7/*
8 * Some corrections by tytso.
9 */
10
11/* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
12 * lookup logic.
13 */
14/* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
15 */
16
17#include <linux/init.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050018#include <linux/export.h>
David S. Miller91cd1b02012-05-23 20:12:50 -070019#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/slab.h>
21#include <linux/fs.h>
22#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/pagemap.h>
Robert Love0eeca282005-07-12 17:06:03 -040024#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/personality.h>
26#include <linux/security.h>
Mimi Zohar6146f0d2009-02-04 09:06:57 -050027#include <linux/ima.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/syscalls.h>
29#include <linux/mount.h>
30#include <linux/audit.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080031#include <linux/capability.h>
Trond Myklebust834f2a42005-10-18 14:20:16 -070032#include <linux/file.h>
Ulrich Drepper5590ff02006-01-18 17:43:53 -080033#include <linux/fcntl.h>
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070034#include <linux/device_cgroup.h>
Al Viro5ad4e532009-03-29 19:50:06 -040035#include <linux/fs_struct.h>
Linus Torvaldse77819e2011-07-22 19:30:19 -070036#include <linux/posix_acl.h>
Linus Torvalds07e997d2014-09-13 11:30:10 -070037#include <linux/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/uaccess.h>
39
Eric Parise81e3f42009-12-04 15:47:36 -050040#include "internal.h"
Al Viroc7105362011-11-24 18:22:03 -050041#include "mount.h"
Eric Parise81e3f42009-12-04 15:47:36 -050042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/* [Feb-1997 T. Schoebel-Theuer]
44 * Fundamental changes in the pathname lookup mechanisms (namei)
45 * were necessary because of omirr. The reason is that omirr needs
46 * to know the _real_ pathname, not the user-supplied one, in case
47 * of symlinks (and also when transname replacements occur).
48 *
49 * The new code replaces the old recursive symlink resolution with
50 * an iterative one (in case of non-nested symlink chains). It does
51 * this with calls to <fs>_follow_link().
52 * As a side effect, dir_namei(), _namei() and follow_link() are now
53 * replaced with a single function lookup_dentry() that can handle all
54 * the special cases of the former code.
55 *
56 * With the new dcache, the pathname is stored at each inode, at least as
57 * long as the refcount of the inode is positive. As a side effect, the
58 * size of the dcache depends on the inode cache and thus is dynamic.
59 *
60 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
61 * resolution to correspond with current state of the code.
62 *
63 * Note that the symlink resolution is not *completely* iterative.
64 * There is still a significant amount of tail- and mid- recursion in
65 * the algorithm. Also, note that <fs>_readlink() is not used in
66 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
67 * may return different results than <fs>_follow_link(). Many virtual
68 * filesystems (including /proc) exhibit this behavior.
69 */
70
71/* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
72 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
73 * and the name already exists in form of a symlink, try to create the new
74 * name indicated by the symlink. The old code always complained that the
75 * name already exists, due to not following the symlink even if its target
76 * is nonexistent. The new semantics affects also mknod() and link() when
Lucas De Marchi25985ed2011-03-30 22:57:33 -030077 * the name is a symlink pointing to a non-existent name.
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 *
79 * I don't know which semantics is the right one, since I have no access
80 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
81 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
82 * "old" one. Personally, I think the new semantics is much more logical.
83 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
84 * file does succeed in both HP-UX and SunOs, but not in Solaris
85 * and in the old Linux semantics.
86 */
87
88/* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
89 * semantics. See the comments in "open_namei" and "do_link" below.
90 *
91 * [10-Sep-98 Alan Modra] Another symlink change.
92 */
93
94/* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
95 * inside the path - always follow.
96 * in the last component in creation/removal/renaming - never follow.
97 * if LOOKUP_FOLLOW passed - follow.
98 * if the pathname has trailing slashes - follow.
99 * otherwise - don't follow.
100 * (applied in that order).
101 *
102 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
103 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
104 * During the 2.4 we need to fix the userland stuff depending on it -
105 * hopefully we will be able to get rid of that wart in 2.5. So far only
106 * XEmacs seems to be relying on it...
107 */
108/*
109 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
Arjan van de Vena11f3a02006-03-23 03:00:33 -0800110 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 * any extra contention...
112 */
113
114/* In order to reduce some races, while at the same time doing additional
115 * checking and hopefully speeding things up, we copy filenames to the
116 * kernel data space before using them..
117 *
118 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
119 * PATH_MAX includes the nul terminator --RR.
120 */
Jeff Layton9cee5ca2012-10-10 15:25:28 -0400121void final_putname(struct filename *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Jeff Layton869724e2012-10-10 16:43:13 -0400123 if (name->separate) {
124 __putname(name->name);
125 kfree(name);
126 } else {
127 __putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
Jeff Layton869724e2012-10-10 16:43:13 -0400131#define EMBEDDED_NAME_MAX (PATH_MAX - sizeof(struct filename))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Jeff Layton9cee5ca2012-10-10 15:25:28 -0400133static struct filename *
134getname_flags(const char __user *filename, int flags, int *empty)
135{
136 struct filename *result, *err;
Linus Torvalds256a73d2012-04-28 14:38:32 -0700137 int len;
Jeff Layton869724e2012-10-10 16:43:13 -0400138 long max;
139 char *kname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Jeff Layton869724e2012-10-10 16:43:13 -0400141 result = __getname();
Linus Torvalds256a73d2012-04-28 14:38:32 -0700142 if (unlikely(!result))
Eric Paris4043cde2012-01-03 14:23:08 -0500143 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Jeff Layton869724e2012-10-10 16:43:13 -0400145 /*
146 * First, try to embed the struct filename inside the names_cache
147 * allocation
148 */
149 kname = (char *)result + sizeof(*result);
Jeff Layton9cee5ca2012-10-10 15:25:28 -0400150 result->name = kname;
Jeff Layton869724e2012-10-10 16:43:13 -0400151 result->separate = false;
152 max = EMBEDDED_NAME_MAX;
153
154recopy:
155 len = strncpy_from_user(kname, filename, max);
Jeff Layton9cee5ca2012-10-10 15:25:28 -0400156 if (unlikely(len < 0)) {
157 err = ERR_PTR(len);
Linus Torvalds256a73d2012-04-28 14:38:32 -0700158 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 }
Linus Torvalds256a73d2012-04-28 14:38:32 -0700160
Jeff Layton869724e2012-10-10 16:43:13 -0400161 /*
162 * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
163 * separate struct filename so we can dedicate the entire
164 * names_cache allocation for the pathname, and re-do the copy from
165 * userland.
166 */
167 if (len == EMBEDDED_NAME_MAX && max == EMBEDDED_NAME_MAX) {
168 kname = (char *)result;
169
170 result = kzalloc(sizeof(*result), GFP_KERNEL);
171 if (!result) {
172 err = ERR_PTR(-ENOMEM);
173 result = (struct filename *)kname;
174 goto error;
175 }
176 result->name = kname;
177 result->separate = true;
178 max = PATH_MAX;
179 goto recopy;
180 }
181
Linus Torvalds256a73d2012-04-28 14:38:32 -0700182 /* The empty path is special. */
183 if (unlikely(!len)) {
184 if (empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 *empty = 1;
Linus Torvalds256a73d2012-04-28 14:38:32 -0700186 err = ERR_PTR(-ENOENT);
187 if (!(flags & LOOKUP_EMPTY))
188 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 }
Linus Torvalds256a73d2012-04-28 14:38:32 -0700190
191 err = ERR_PTR(-ENAMETOOLONG);
Jeff Layton869724e2012-10-10 16:43:13 -0400192 if (unlikely(len >= PATH_MAX))
193 goto error;
194
195 result->uptr = filename;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 audit_getname(result);
197 return result;
Linus Torvalds256a73d2012-04-28 14:38:32 -0700198
199error:
Jeff Layton869724e2012-10-10 16:43:13 -0400200 final_putname(result);
Linus Torvalds256a73d2012-04-28 14:38:32 -0700201 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
203
Jeff Layton9cee5ca2012-10-10 15:25:28 -0400204struct filename *
205getname(const char __user * filename)
Al Virof52e0c12011-03-14 18:56:51 -0400206{
Linus Torvaldsf7493e52012-03-22 16:10:40 -0700207 return getname_flags(filename, 0, NULL);
Al Virof52e0c12011-03-14 18:56:51 -0400208}
Jeff Layton9cee5ca2012-10-10 15:25:28 -0400209EXPORT_SYMBOL(getname);
Al Virof52e0c12011-03-14 18:56:51 -0400210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211#ifdef CONFIG_AUDITSYSCALL
Jeff Layton9cee5ca2012-10-10 15:25:28 -0400212void putname(struct filename *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
Al Viro5ac3a9c2006-07-16 06:38:45 -0400214 if (unlikely(!audit_dummy_context()))
Jeff Layton9cee5ca2012-10-10 15:25:28 -0400215 return audit_putname(name);
216 final_putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218#endif
219
Linus Torvaldse77819e2011-07-22 19:30:19 -0700220static int check_acl(struct inode *inode, int mask)
221{
Linus Torvalds84635d62011-07-25 22:47:03 -0700222#ifdef CONFIG_FS_POSIX_ACL
Linus Torvaldse77819e2011-07-22 19:30:19 -0700223 struct posix_acl *acl;
224
Linus Torvaldse77819e2011-07-22 19:30:19 -0700225 if (mask & MAY_NOT_BLOCK) {
Al Viro35678662011-08-02 21:32:13 -0400226 acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
227 if (!acl)
Linus Torvaldse77819e2011-07-22 19:30:19 -0700228 return -EAGAIN;
Al Viro35678662011-08-02 21:32:13 -0400229 /* no ->get_acl() calls in RCU mode... */
230 if (acl == ACL_NOT_CACHED)
231 return -ECHILD;
Ari Savolainen206b1d02011-08-06 19:43:07 +0300232 return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
Linus Torvaldse77819e2011-07-22 19:30:19 -0700233 }
234
235 acl = get_cached_acl(inode, ACL_TYPE_ACCESS);
236
237 /*
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200238 * A filesystem can force a ACL callback by just never filling the
239 * ACL cache. But normally you'd fill the cache either at inode
240 * instantiation time, or on the first ->get_acl call.
Linus Torvaldse77819e2011-07-22 19:30:19 -0700241 *
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200242 * If the filesystem doesn't have a get_acl() function at all, we'll
243 * just create the negative cache entry.
Linus Torvaldse77819e2011-07-22 19:30:19 -0700244 */
245 if (acl == ACL_NOT_CACHED) {
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200246 if (inode->i_op->get_acl) {
247 acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS);
248 if (IS_ERR(acl))
249 return PTR_ERR(acl);
250 } else {
251 set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
252 return -EAGAIN;
253 }
Linus Torvaldse77819e2011-07-22 19:30:19 -0700254 }
255
256 if (acl) {
257 int error = posix_acl_permission(inode, acl, mask);
258 posix_acl_release(acl);
259 return error;
260 }
Linus Torvalds84635d62011-07-25 22:47:03 -0700261#endif
Linus Torvaldse77819e2011-07-22 19:30:19 -0700262
263 return -EAGAIN;
264}
265
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700266/*
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530267 * This does the basic permission checking
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700268 */
Al Viro7e401452011-06-20 19:12:17 -0400269static int acl_permission_check(struct inode *inode, int mask)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700270{
Linus Torvalds26cf46b2011-05-13 11:51:01 -0700271 unsigned int mode = inode->i_mode;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700272
Linus Torvalds14067ff2011-07-25 19:55:52 -0700273 if (likely(current_fsuid() == inode->i_uid))
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700274 mode >>= 6;
275 else {
Linus Torvaldse77819e2011-07-22 19:30:19 -0700276 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
Al Viro7e401452011-06-20 19:12:17 -0400277 int error = check_acl(inode, mask);
Nick Pigginb74c79e2011-01-07 17:49:58 +1100278 if (error != -EAGAIN)
279 return error;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700280 }
281
282 if (in_group_p(inode->i_gid))
283 mode >>= 3;
284 }
285
286 /*
287 * If the DACs are ok we don't need any capability check.
288 */
Al Viro9c2c7032011-06-20 19:06:22 -0400289 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700290 return 0;
291 return -EACCES;
292}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294/**
Nick Pigginb74c79e2011-01-07 17:49:58 +1100295 * generic_permission - check for access rights on a Posix-like filesystem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 * @inode: inode to check access rights for
Andreas Gruenbacher8fd90c82011-10-23 23:13:30 +0530297 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 *
299 * Used to check for read/write/execute permissions on a file.
300 * We use "fsuid" for this, letting us set arbitrary permissions
301 * for filesystem access without changing the "normal" uids which
Nick Pigginb74c79e2011-01-07 17:49:58 +1100302 * are used for other things.
303 *
304 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
305 * request cannot be satisfied (eg. requires blocking or too much complexity).
306 * It would then be called again in ref-walk mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 */
Al Viro2830ba72011-06-20 19:16:29 -0400308int generic_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700310 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 /*
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530313 * Do the basic permission checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 */
Al Viro7e401452011-06-20 19:12:17 -0400315 ret = acl_permission_check(inode, mask);
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700316 if (ret != -EACCES)
317 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Al Virod594e7e2011-06-20 19:55:42 -0400319 if (S_ISDIR(inode->i_mode)) {
320 /* DACs are overridable for directories */
Eric W. Biedermand6814e262011-11-14 16:24:06 -0800321 if (inode_capable(inode, CAP_DAC_OVERRIDE))
Al Virod594e7e2011-06-20 19:55:42 -0400322 return 0;
323 if (!(mask & MAY_WRITE))
Eric W. Biedermand6814e262011-11-14 16:24:06 -0800324 if (inode_capable(inode, CAP_DAC_READ_SEARCH))
Al Virod594e7e2011-06-20 19:55:42 -0400325 return 0;
326 return -EACCES;
327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 /*
329 * Read/write DACs are always overridable.
Al Virod594e7e2011-06-20 19:55:42 -0400330 * Executable DACs are overridable when there is
331 * at least one exec bit set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 */
Al Virod594e7e2011-06-20 19:55:42 -0400333 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
Eric W. Biedermand6814e262011-11-14 16:24:06 -0800334 if (inode_capable(inode, CAP_DAC_OVERRIDE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 return 0;
336
337 /*
338 * Searching includes executable on directories, else just read.
339 */
Serge E. Hallyn7ea66002009-12-29 14:50:19 -0600340 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
Al Virod594e7e2011-06-20 19:55:42 -0400341 if (mask == MAY_READ)
Eric W. Biedermand6814e262011-11-14 16:24:06 -0800342 if (inode_capable(inode, CAP_DAC_READ_SEARCH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return 0;
344
345 return -EACCES;
346}
347
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700348/*
349 * We _really_ want to just do "generic_permission()" without
350 * even looking at the inode->i_op values. So we keep a cache
351 * flag in inode->i_opflags, that says "this has not special
352 * permission function, use the fast case".
353 */
Daniel Rosenberg17285112016-10-26 16:27:45 -0700354static inline int do_inode_permission(struct vfsmount *mnt, struct inode *inode, int mask)
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700355{
356 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
Daniel Rosenberg17285112016-10-26 16:27:45 -0700357 if (likely(mnt && inode->i_op->permission2))
358 return inode->i_op->permission2(mnt, inode, mask);
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700359 if (likely(inode->i_op->permission))
360 return inode->i_op->permission(inode, mask);
361
362 /* This gets set once for the inode lifetime */
363 spin_lock(&inode->i_lock);
364 inode->i_opflags |= IOP_FASTPERM;
365 spin_unlock(&inode->i_lock);
366 }
367 return generic_permission(inode, mask);
368}
369
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200370/**
371 * inode_permission - check for access rights to a given inode
372 * @inode: inode to check permission on
Andreas Gruenbacher8fd90c82011-10-23 23:13:30 +0530373 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200374 *
375 * Used to check for read/write/execute permissions on an inode.
376 * We use "fsuid" for this, letting us set arbitrary permissions
377 * for filesystem access without changing the "normal" uids which
378 * are used for other things.
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530379 *
380 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200381 */
Daniel Rosenberg17285112016-10-26 16:27:45 -0700382int inode_permission2(struct vfsmount *mnt, struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
Al Viroe6305c42008-07-15 21:03:57 -0400384 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700386 if (unlikely(mask & MAY_WRITE)) {
Miklos Szeredi22590e42007-10-16 23:27:08 -0700387 umode_t mode = inode->i_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 /*
390 * Nobody gets write access to a read-only fs.
391 */
392 if (IS_RDONLY(inode) &&
393 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
394 return -EROFS;
395
396 /*
397 * Nobody gets write access to an immutable file.
398 */
399 if (IS_IMMUTABLE(inode))
400 return -EACCES;
401 }
402
Daniel Rosenberg17285112016-10-26 16:27:45 -0700403 retval = do_inode_permission(mnt, inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 if (retval)
405 return retval;
406
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700407 retval = devcgroup_inode_permission(inode, mask);
408 if (retval)
409 return retval;
410
Eric Parisd09ca732010-07-23 11:43:57 -0400411 return security_inode_permission(inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
Daniel Rosenberg17285112016-10-26 16:27:45 -0700413EXPORT_SYMBOL(inode_permission2);
414
415int inode_permission(struct inode *inode, int mask)
416{
417 return inode_permission2(NULL, inode, mask);
418}
419EXPORT_SYMBOL(inode_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Al Virof4d6ff82011-06-19 13:14:21 -0400421/**
Jan Blunck5dd784d2008-02-14 19:34:38 -0800422 * path_get - get a reference to a path
423 * @path: path to get the reference to
424 *
425 * Given a path increment the reference count to the dentry and the vfsmount.
426 */
427void path_get(struct path *path)
428{
429 mntget(path->mnt);
430 dget(path->dentry);
431}
432EXPORT_SYMBOL(path_get);
433
434/**
Jan Blunck1d957f92008-02-14 19:34:35 -0800435 * path_put - put a reference to a path
436 * @path: path to put the reference to
437 *
438 * Given a path decrement the reference count to the dentry and the vfsmount.
439 */
440void path_put(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
Jan Blunck1d957f92008-02-14 19:34:35 -0800442 dput(path->dentry);
443 mntput(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
Jan Blunck1d957f92008-02-14 19:34:35 -0800445EXPORT_SYMBOL(path_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Eric W. Biedermanbf2e9fa2015-08-15 20:27:13 -0500447/**
448 * path_connected - Verify that a path->dentry is below path->mnt.mnt_root
449 * @path: nameidate to verify
450 *
451 * Rename can sometimes move a file or directory outside of a bind
452 * mount, path_connected allows those cases to be detected.
453 */
454static bool path_connected(const struct path *path)
455{
456 struct vfsmount *mnt = path->mnt;
457
458 /* Only bind mounts can have disconnected paths */
459 if (mnt->mnt_root == mnt->mnt_sb->s_root)
460 return true;
461
462 return is_subdir(path->dentry, mnt->mnt_root);
463}
464
Al Viro19660af2011-03-25 10:32:48 -0400465/*
Nick Piggin31e6b012011-01-07 17:49:52 +1100466 * Path walking has 2 modes, rcu-walk and ref-walk (see
Al Viro19660af2011-03-25 10:32:48 -0400467 * Documentation/filesystems/path-lookup.txt). In situations when we can't
468 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
469 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
470 * mode. Refcounts are grabbed at the last known good point before rcu-walk
471 * got stuck, so ref-walk may continue from there. If this is not successful
472 * (eg. a seqcount has changed), then failure is returned and it's up to caller
473 * to restart the path walk from the beginning in ref-walk mode.
Nick Piggin31e6b012011-01-07 17:49:52 +1100474 */
Nick Piggin31e6b012011-01-07 17:49:52 +1100475
Al Viro9caac002012-07-18 20:43:19 +0400476static inline void lock_rcu_walk(void)
477{
478 br_read_lock(&vfsmount_lock);
479 rcu_read_lock();
480}
481
482static inline void unlock_rcu_walk(void)
483{
484 rcu_read_unlock();
485 br_read_unlock(&vfsmount_lock);
486}
487
Nick Piggin31e6b012011-01-07 17:49:52 +1100488/**
Al Viro19660af2011-03-25 10:32:48 -0400489 * unlazy_walk - try to switch to ref-walk mode.
490 * @nd: nameidata pathwalk data
491 * @dentry: child of nd->path.dentry or NULL
Randy Dunlap39191622011-01-08 19:36:21 -0800492 * Returns: 0 on success, -ECHILD on failure
Nick Piggin31e6b012011-01-07 17:49:52 +1100493 *
Al Viro19660af2011-03-25 10:32:48 -0400494 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
495 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
496 * @nd or NULL. Must be called from rcu-walk context.
Nick Piggin31e6b012011-01-07 17:49:52 +1100497 */
Al Viro19660af2011-03-25 10:32:48 -0400498static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
Nick Piggin31e6b012011-01-07 17:49:52 +1100499{
500 struct fs_struct *fs = current->fs;
501 struct dentry *parent = nd->path.dentry;
Al Viro5b6ca022011-03-09 23:04:47 -0500502 int want_root = 0;
Nick Piggin31e6b012011-01-07 17:49:52 +1100503
504 BUG_ON(!(nd->flags & LOOKUP_RCU));
Al Viro5b6ca022011-03-09 23:04:47 -0500505 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
506 want_root = 1;
Nick Piggin31e6b012011-01-07 17:49:52 +1100507 spin_lock(&fs->lock);
508 if (nd->root.mnt != fs->root.mnt ||
509 nd->root.dentry != fs->root.dentry)
510 goto err_root;
511 }
512 spin_lock(&parent->d_lock);
Al Viro19660af2011-03-25 10:32:48 -0400513 if (!dentry) {
514 if (!__d_rcu_to_refcount(parent, nd->seq))
515 goto err_parent;
516 BUG_ON(nd->inode != parent->d_inode);
517 } else {
Al Viro94c0d4e2011-07-12 21:40:23 -0400518 if (dentry->d_parent != parent)
519 goto err_parent;
Al Viro19660af2011-03-25 10:32:48 -0400520 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
521 if (!__d_rcu_to_refcount(dentry, nd->seq))
522 goto err_child;
523 /*
524 * If the sequence check on the child dentry passed, then
525 * the child has not been removed from its parent. This
526 * means the parent dentry must be valid and able to take
527 * a reference at this point.
528 */
529 BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
530 BUG_ON(!parent->d_count);
531 parent->d_count++;
532 spin_unlock(&dentry->d_lock);
533 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100534 spin_unlock(&parent->d_lock);
Al Viro5b6ca022011-03-09 23:04:47 -0500535 if (want_root) {
Nick Piggin31e6b012011-01-07 17:49:52 +1100536 path_get(&nd->root);
537 spin_unlock(&fs->lock);
538 }
539 mntget(nd->path.mnt);
540
Al Viro9caac002012-07-18 20:43:19 +0400541 unlock_rcu_walk();
Nick Piggin31e6b012011-01-07 17:49:52 +1100542 nd->flags &= ~LOOKUP_RCU;
543 return 0;
Al Viro19660af2011-03-25 10:32:48 -0400544
545err_child:
Nick Piggin31e6b012011-01-07 17:49:52 +1100546 spin_unlock(&dentry->d_lock);
Al Viro19660af2011-03-25 10:32:48 -0400547err_parent:
Nick Piggin31e6b012011-01-07 17:49:52 +1100548 spin_unlock(&parent->d_lock);
549err_root:
Al Viro5b6ca022011-03-09 23:04:47 -0500550 if (want_root)
Nick Piggin31e6b012011-01-07 17:49:52 +1100551 spin_unlock(&fs->lock);
552 return -ECHILD;
553}
554
Al Viro28ca3262012-06-10 16:10:59 -0400555static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
Trond Myklebust834f2a42005-10-18 14:20:16 -0700556{
Al Viro28ca3262012-06-10 16:10:59 -0400557 return dentry->d_op->d_revalidate(dentry, flags);
Nick Piggin34286d62011-01-07 17:49:57 +1100558}
559
Al Viro9f1fafe2011-03-25 11:00:12 -0400560/**
561 * complete_walk - successful completion of path walk
562 * @nd: pointer nameidata
Jeff Layton39159de2009-12-07 12:01:50 -0500563 *
Al Viro9f1fafe2011-03-25 11:00:12 -0400564 * If we had been in RCU mode, drop out of it and legitimize nd->path.
565 * Revalidate the final result, unless we'd already done that during
566 * the path walk or the filesystem doesn't ask for it. Return 0 on
567 * success, -error on failure. In case of failure caller does not
568 * need to drop nd->path.
Jeff Layton39159de2009-12-07 12:01:50 -0500569 */
Al Viro9f1fafe2011-03-25 11:00:12 -0400570static int complete_walk(struct nameidata *nd)
Jeff Layton39159de2009-12-07 12:01:50 -0500571{
Al Viro16c2cd72011-02-22 15:50:10 -0500572 struct dentry *dentry = nd->path.dentry;
Jeff Layton39159de2009-12-07 12:01:50 -0500573 int status;
Jeff Layton39159de2009-12-07 12:01:50 -0500574
Al Viro9f1fafe2011-03-25 11:00:12 -0400575 if (nd->flags & LOOKUP_RCU) {
576 nd->flags &= ~LOOKUP_RCU;
577 if (!(nd->flags & LOOKUP_ROOT))
578 nd->root.mnt = NULL;
579 spin_lock(&dentry->d_lock);
580 if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
581 spin_unlock(&dentry->d_lock);
Al Viro9caac002012-07-18 20:43:19 +0400582 unlock_rcu_walk();
Al Viro9f1fafe2011-03-25 11:00:12 -0400583 return -ECHILD;
584 }
585 BUG_ON(nd->inode != dentry->d_inode);
586 spin_unlock(&dentry->d_lock);
587 mntget(nd->path.mnt);
Al Viro9caac002012-07-18 20:43:19 +0400588 unlock_rcu_walk();
Al Viro9f1fafe2011-03-25 11:00:12 -0400589 }
590
Al Viro16c2cd72011-02-22 15:50:10 -0500591 if (likely(!(nd->flags & LOOKUP_JUMPED)))
Jeff Layton39159de2009-12-07 12:01:50 -0500592 return 0;
593
Al Viro16c2cd72011-02-22 15:50:10 -0500594 if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
595 return 0;
596
597 if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
598 return 0;
599
600 /* Note: we do not d_invalidate() */
Al Viro28ca3262012-06-10 16:10:59 -0400601 status = d_revalidate(dentry, nd->flags);
Jeff Layton39159de2009-12-07 12:01:50 -0500602 if (status > 0)
603 return 0;
604
Al Viro16c2cd72011-02-22 15:50:10 -0500605 if (!status)
Jeff Layton39159de2009-12-07 12:01:50 -0500606 status = -ESTALE;
Al Viro16c2cd72011-02-22 15:50:10 -0500607
Al Viro9f1fafe2011-03-25 11:00:12 -0400608 path_put(&nd->path);
Jeff Layton39159de2009-12-07 12:01:50 -0500609 return status;
610}
611
Al Viro2a737872009-04-07 11:49:53 -0400612static __always_inline void set_root(struct nameidata *nd)
613{
Al Viro589a49f2014-09-13 21:55:46 -0400614 get_fs_root(current->fs, &nd->root);
Al Viro2a737872009-04-07 11:49:53 -0400615}
616
Al Viro6de88d72009-08-09 01:41:57 +0400617static int link_path_walk(const char *, struct nameidata *);
618
Al Viro589a49f2014-09-13 21:55:46 -0400619static __always_inline unsigned set_root_rcu(struct nameidata *nd)
Nick Piggin31e6b012011-01-07 17:49:52 +1100620{
Al Viro589a49f2014-09-13 21:55:46 -0400621 struct fs_struct *fs = current->fs;
622 unsigned seq, res;
Nick Pigginc28cc362011-01-07 17:49:53 +1100623
Al Viro589a49f2014-09-13 21:55:46 -0400624 do {
625 seq = read_seqcount_begin(&fs->seq);
626 nd->root = fs->root;
627 res = __read_seqcount_begin(&nd->root.dentry->d_seq);
628 } while (read_seqcount_retry(&fs->seq, seq));
629 return res;
Nick Piggin31e6b012011-01-07 17:49:52 +1100630}
631
Arjan van de Venf1662352006-01-14 13:21:31 -0800632static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
Nick Piggin31e6b012011-01-07 17:49:52 +1100634 int ret;
635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 if (IS_ERR(link))
637 goto fail;
638
639 if (*link == '/') {
Al Viro92214262014-09-13 21:55:46 -0400640 if (!nd->root.mnt)
641 set_root(nd);
Jan Blunck1d957f92008-02-14 19:34:35 -0800642 path_put(&nd->path);
Al Viro2a737872009-04-07 11:49:53 -0400643 nd->path = nd->root;
644 path_get(&nd->root);
Al Viro16c2cd72011-02-22 15:50:10 -0500645 nd->flags |= LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100647 nd->inode = nd->path.dentry->d_inode;
Christoph Hellwigb4091d52008-11-05 15:07:21 +0100648
Nick Piggin31e6b012011-01-07 17:49:52 +1100649 ret = link_path_walk(link, nd);
650 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651fail:
Jan Blunck1d957f92008-02-14 19:34:35 -0800652 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return PTR_ERR(link);
654}
655
Jan Blunck1d957f92008-02-14 19:34:35 -0800656static void path_put_conditional(struct path *path, struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700657{
658 dput(path->dentry);
Jan Blunck4ac91372008-02-14 19:34:32 -0800659 if (path->mnt != nd->path.mnt)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700660 mntput(path->mnt);
661}
662
Nick Piggin7b9337a2011-01-14 08:42:43 +0000663static inline void path_to_nameidata(const struct path *path,
664 struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700665{
Nick Piggin31e6b012011-01-07 17:49:52 +1100666 if (!(nd->flags & LOOKUP_RCU)) {
667 dput(nd->path.dentry);
668 if (nd->path.mnt != path->mnt)
669 mntput(nd->path.mnt);
Huang Shijie9a229682010-04-02 17:37:13 +0800670 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100671 nd->path.mnt = path->mnt;
Jan Blunck4ac91372008-02-14 19:34:32 -0800672 nd->path.dentry = path->dentry;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700673}
674
Christoph Hellwigfac26022012-06-18 10:47:04 -0400675/*
676 * Helper to directly jump to a known parsed path from ->follow_link,
677 * caller must have taken a reference to path beforehand.
678 */
679void nd_jump_link(struct nameidata *nd, struct path *path)
680{
681 path_put(&nd->path);
682
683 nd->path = *path;
684 nd->inode = nd->path.dentry->d_inode;
685 nd->flags |= LOOKUP_JUMPED;
Christoph Hellwigfac26022012-06-18 10:47:04 -0400686}
687
Al Viro574197e2011-03-14 22:20:34 -0400688static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
689{
690 struct inode *inode = link->dentry->d_inode;
Al Viro2c4d7392012-06-10 04:15:17 -0400691 if (inode->i_op->put_link)
Al Viro574197e2011-03-14 22:20:34 -0400692 inode->i_op->put_link(link->dentry, nd, cookie);
693 path_put(link);
694}
695
Linus Torvaldsfcf25fb2012-10-26 10:05:07 -0700696int sysctl_protected_symlinks __read_mostly = 0;
697int sysctl_protected_hardlinks __read_mostly = 0;
Salvatore Mesoraca063d55a2018-08-23 17:00:35 -0700698int sysctl_protected_fifos __read_mostly;
699int sysctl_protected_regular __read_mostly;
Kees Cook5092e9c2012-07-25 17:29:07 -0700700
701/**
702 * may_follow_link - Check symlink following for unsafe situations
703 * @link: The path of the symlink
704 *
705 * In the case of the sysctl_protected_symlinks sysctl being enabled,
706 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
707 * in a sticky world-writable directory. This is to protect privileged
708 * processes from failing races against path names that may change out
709 * from under them by way of other users creating malicious symlinks.
710 * It will permit symlinks to be followed only when outside a sticky
711 * world-writable directory, or when the uid of the symlink and follower
712 * match, or when the directory owner matches the symlink's owner.
713 *
714 * Returns 0 if following the symlink is allowed, -ve on error.
715 */
716static inline int may_follow_link(struct path *link, struct nameidata *nd)
717{
718 const struct inode *inode;
719 const struct inode *parent;
720
721 if (!sysctl_protected_symlinks)
722 return 0;
723
724 /* Allowed if owner and follower match. */
725 inode = link->dentry->d_inode;
726 if (current_cred()->fsuid == inode->i_uid)
727 return 0;
728
729 /* Allowed if parent directory not sticky and world-writable. */
730 parent = nd->path.dentry->d_inode;
731 if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
732 return 0;
733
734 /* Allowed if parent directory and link owner match. */
735 if (parent->i_uid == inode->i_uid)
736 return 0;
737
Sasha Levind4def9b2012-10-04 19:56:40 -0400738 audit_log_link_denied("follow_link", link);
Kees Cook5092e9c2012-07-25 17:29:07 -0700739 path_put_conditional(link, nd);
740 path_put(&nd->path);
741 return -EACCES;
742}
743
744/**
745 * safe_hardlink_source - Check for safe hardlink conditions
746 * @inode: the source inode to hardlink from
747 *
748 * Return false if at least one of the following conditions:
749 * - inode is not a regular file
750 * - inode is setuid
751 * - inode is setgid and group-exec
752 * - access failure for read and write
753 *
754 * Otherwise returns true.
755 */
756static bool safe_hardlink_source(struct inode *inode)
757{
758 umode_t mode = inode->i_mode;
759
760 /* Special files should not get pinned to the filesystem. */
761 if (!S_ISREG(mode))
762 return false;
763
764 /* Setuid files should not get pinned to the filesystem. */
765 if (mode & S_ISUID)
766 return false;
767
768 /* Executable setgid files should not get pinned to the filesystem. */
769 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
770 return false;
771
772 /* Hardlinking to unreadable or unwritable sources is dangerous. */
773 if (inode_permission(inode, MAY_READ | MAY_WRITE))
774 return false;
775
776 return true;
777}
778
779/**
780 * may_linkat - Check permissions for creating a hardlink
781 * @link: the source to hardlink from
782 *
783 * Block hardlink when all of:
784 * - sysctl_protected_hardlinks enabled
785 * - fsuid does not match inode
786 * - hardlink source is unsafe (see safe_hardlink_source() above)
787 * - not CAP_FOWNER
788 *
789 * Returns 0 if successful, -ve on error.
790 */
791static int may_linkat(struct path *link)
792{
793 const struct cred *cred;
794 struct inode *inode;
795
796 if (!sysctl_protected_hardlinks)
797 return 0;
798
799 cred = current_cred();
800 inode = link->dentry->d_inode;
801
802 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
803 * otherwise, it must be a safe source.
804 */
805 if (cred->fsuid == inode->i_uid || safe_hardlink_source(inode) ||
806 capable(CAP_FOWNER))
807 return 0;
808
Kees Cookccdd0902012-07-25 17:29:08 -0700809 audit_log_link_denied("linkat", link);
Kees Cook5092e9c2012-07-25 17:29:07 -0700810 return -EPERM;
811}
812
Salvatore Mesoraca063d55a2018-08-23 17:00:35 -0700813/**
814 * may_create_in_sticky - Check whether an O_CREAT open in a sticky directory
815 * should be allowed, or not, on files that already
816 * exist.
Al Viroa2434112020-01-26 09:29:34 -0500817 * @dir_mode: mode bits of directory
818 * @dir_uid: owner of directory
Salvatore Mesoraca063d55a2018-08-23 17:00:35 -0700819 * @inode: the inode of the file to open
820 *
821 * Block an O_CREAT open of a FIFO (or a regular file) when:
822 * - sysctl_protected_fifos (or sysctl_protected_regular) is enabled
823 * - the file already exists
824 * - we are in a sticky directory
825 * - we don't own the file
826 * - the owner of the directory doesn't own the file
827 * - the directory is world writable
828 * If the sysctl_protected_fifos (or sysctl_protected_regular) is set to 2
829 * the directory doesn't have to be world writable: being group writable will
830 * be enough.
831 *
832 * Returns 0 if the open is allowed, -ve on error.
833 */
Al Viroa2434112020-01-26 09:29:34 -0500834static int may_create_in_sticky(umode_t dir_mode, kuid_t dir_uid,
Salvatore Mesoraca063d55a2018-08-23 17:00:35 -0700835 struct inode * const inode)
836{
837 if ((!sysctl_protected_fifos && S_ISFIFO(inode->i_mode)) ||
838 (!sysctl_protected_regular && S_ISREG(inode->i_mode)) ||
Al Viroa2434112020-01-26 09:29:34 -0500839 likely(!(dir_mode & S_ISVTX)) ||
840 uid_eq(inode->i_uid, dir_uid) ||
Salvatore Mesoraca063d55a2018-08-23 17:00:35 -0700841 uid_eq(current_fsuid(), inode->i_uid))
842 return 0;
843
Al Viroa2434112020-01-26 09:29:34 -0500844 if (likely(dir_mode & 0002) ||
845 (dir_mode & 0020 &&
Salvatore Mesoraca063d55a2018-08-23 17:00:35 -0700846 ((sysctl_protected_fifos >= 2 && S_ISFIFO(inode->i_mode)) ||
847 (sysctl_protected_regular >= 2 && S_ISREG(inode->i_mode))))) {
848 return -EACCES;
849 }
850 return 0;
851}
852
Al Virodef4af32009-12-26 08:37:05 -0500853static __always_inline int
Al Viro574197e2011-03-14 22:20:34 -0400854follow_link(struct path *link, struct nameidata *nd, void **p)
Ian Kent051d3812006-03-27 01:14:53 -0800855{
Nick Piggin7b9337a2011-01-14 08:42:43 +0000856 struct dentry *dentry = link->dentry;
Al Viro2c4d7392012-06-10 04:15:17 -0400857 int error;
858 char *s;
Ian Kent051d3812006-03-27 01:14:53 -0800859
Al Viro844a3912011-02-15 00:38:26 -0500860 BUG_ON(nd->flags & LOOKUP_RCU);
861
Al Viro0e794582011-03-16 02:45:02 -0400862 if (link->mnt == nd->path.mnt)
863 mntget(link->mnt);
864
Al Viro2c4d7392012-06-10 04:15:17 -0400865 error = -ELOOP;
866 if (unlikely(current->total_link_count >= 40))
867 goto out_put_nd_path;
868
Al Viro574197e2011-03-14 22:20:34 -0400869 cond_resched();
870 current->total_link_count++;
871
Al Viro68ac1232012-03-15 08:21:57 -0400872 touch_atime(link);
Ian Kent051d3812006-03-27 01:14:53 -0800873 nd_set_link(nd, NULL);
874
Al Viro36f3b4f2011-02-22 21:24:38 -0500875 error = security_inode_follow_link(link->dentry, nd);
Al Viro2c4d7392012-06-10 04:15:17 -0400876 if (error)
877 goto out_put_nd_path;
Al Viro36f3b4f2011-02-22 21:24:38 -0500878
Al Viro86acdca12009-12-22 23:45:11 -0500879 nd->last_type = LAST_BIND;
Al Virodef4af32009-12-26 08:37:05 -0500880 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
881 error = PTR_ERR(*p);
Al Viro2c4d7392012-06-10 04:15:17 -0400882 if (IS_ERR(*p))
Christoph Hellwigda51d392012-06-18 10:47:03 -0400883 goto out_put_nd_path;
Al Viro2c4d7392012-06-10 04:15:17 -0400884
885 error = 0;
886 s = nd_get_link(nd);
887 if (s) {
888 error = __vfs_follow_link(nd, s);
Christoph Hellwigfac26022012-06-18 10:47:04 -0400889 if (unlikely(error))
890 put_link(nd, link, *p);
Ian Kent051d3812006-03-27 01:14:53 -0800891 }
Al Viro2c4d7392012-06-10 04:15:17 -0400892
893 return error;
894
895out_put_nd_path:
Arnd Bergmanne1031ba2012-10-11 13:20:00 +0000896 *p = NULL;
Al Viro2c4d7392012-06-10 04:15:17 -0400897 path_put(&nd->path);
Al Viro2c4d7392012-06-10 04:15:17 -0400898 path_put(link);
Ian Kent051d3812006-03-27 01:14:53 -0800899 return error;
900}
901
Nick Piggin31e6b012011-01-07 17:49:52 +1100902static int follow_up_rcu(struct path *path)
903{
Al Viro0714a532011-11-24 22:19:58 -0500904 struct mount *mnt = real_mount(path->mnt);
905 struct mount *parent;
Nick Piggin31e6b012011-01-07 17:49:52 +1100906 struct dentry *mountpoint;
907
Al Viro0714a532011-11-24 22:19:58 -0500908 parent = mnt->mnt_parent;
909 if (&parent->mnt == path->mnt)
Nick Piggin31e6b012011-01-07 17:49:52 +1100910 return 0;
Al Viroa73324d2011-11-24 22:25:07 -0500911 mountpoint = mnt->mnt_mountpoint;
Nick Piggin31e6b012011-01-07 17:49:52 +1100912 path->dentry = mountpoint;
Al Viro0714a532011-11-24 22:19:58 -0500913 path->mnt = &parent->mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +1100914 return 1;
915}
916
David Howells105a0cd2012-06-25 12:55:28 +0100917/*
918 * follow_up - Find the mountpoint of path's vfsmount
919 *
920 * Given a path, find the mountpoint of its source file system.
921 * Replace @path with the path of the mountpoint in the parent mount.
922 * Up is towards /.
923 *
924 * Return 1 if we went up a level and 0 if we were already at the
925 * root.
926 */
Al Virobab77eb2009-04-18 03:26:48 -0400927int follow_up(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
Al Viro0714a532011-11-24 22:19:58 -0500929 struct mount *mnt = real_mount(path->mnt);
930 struct mount *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 struct dentry *mountpoint;
Nick Piggin99b7db72010-08-18 04:37:39 +1000932
Andi Kleenf0769502012-05-08 13:32:02 +0930933 br_read_lock(&vfsmount_lock);
Al Viro0714a532011-11-24 22:19:58 -0500934 parent = mnt->mnt_parent;
Al Virodab53a72012-07-18 17:32:50 +0400935 if (parent == mnt) {
Andi Kleenf0769502012-05-08 13:32:02 +0930936 br_read_unlock(&vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return 0;
938 }
Al Viro0714a532011-11-24 22:19:58 -0500939 mntget(&parent->mnt);
Al Viroa73324d2011-11-24 22:25:07 -0500940 mountpoint = dget(mnt->mnt_mountpoint);
Andi Kleenf0769502012-05-08 13:32:02 +0930941 br_read_unlock(&vfsmount_lock);
Al Virobab77eb2009-04-18 03:26:48 -0400942 dput(path->dentry);
943 path->dentry = mountpoint;
944 mntput(path->mnt);
Al Viro0714a532011-11-24 22:19:58 -0500945 path->mnt = &parent->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 return 1;
947}
948
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100949/*
David Howells9875cf82011-01-14 18:45:21 +0000950 * Perform an automount
951 * - return -EISDIR to tell follow_managed() to stop and return the path we
952 * were called with.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 */
David Howells9875cf82011-01-14 18:45:21 +0000954static int follow_automount(struct path *path, unsigned flags,
955 bool *need_mntput)
Nick Piggin31e6b012011-01-07 17:49:52 +1100956{
David Howells9875cf82011-01-14 18:45:21 +0000957 struct vfsmount *mnt;
David Howellsea5b7782011-01-14 19:10:03 +0000958 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +1100959
David Howells9875cf82011-01-14 18:45:21 +0000960 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
961 return -EREMOTE;
Al Viro463ffb22005-06-06 13:36:05 -0700962
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200963 /* We don't want to mount if someone's just doing a stat -
964 * unless they're stat'ing a directory and appended a '/' to
965 * the name.
966 *
967 * We do, however, want to mount if someone wants to open or
968 * create a file of any type under the mountpoint, wants to
969 * traverse through the mountpoint or wants to open the
970 * mounted directory. Also, autofs may mark negative dentries
971 * as being automount points. These will need the attentions
972 * of the daemon to instantiate them before they can be used.
David Howells9875cf82011-01-14 18:45:21 +0000973 */
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200974 if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
Linus Torvaldsd94c1772011-09-26 17:44:55 -0700975 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +0200976 path->dentry->d_inode)
977 return -EISDIR;
978
David Howells9875cf82011-01-14 18:45:21 +0000979 current->total_link_count++;
980 if (current->total_link_count >= 40)
981 return -ELOOP;
982
983 mnt = path->dentry->d_op->d_automount(path);
984 if (IS_ERR(mnt)) {
985 /*
986 * The filesystem is allowed to return -EISDIR here to indicate
987 * it doesn't want to automount. For instance, autofs would do
988 * this so that its userspace daemon can mount on this dentry.
989 *
990 * However, we can only permit this if it's a terminal point in
991 * the path being looked up; if it wasn't then the remainder of
992 * the path is inaccessible and we should say so.
993 */
Al Viro49084c32011-06-25 21:59:52 -0400994 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
David Howells9875cf82011-01-14 18:45:21 +0000995 return -EREMOTE;
996 return PTR_ERR(mnt);
997 }
David Howellsea5b7782011-01-14 19:10:03 +0000998
David Howells9875cf82011-01-14 18:45:21 +0000999 if (!mnt) /* mount collision */
1000 return 0;
1001
Al Viro8aef1882011-06-16 15:10:06 +01001002 if (!*need_mntput) {
1003 /* lock_mount() may release path->mnt on error */
1004 mntget(path->mnt);
1005 *need_mntput = true;
1006 }
Al Viro19a167a2011-01-17 01:35:23 -05001007 err = finish_automount(mnt, path);
David Howellsea5b7782011-01-14 19:10:03 +00001008
David Howellsea5b7782011-01-14 19:10:03 +00001009 switch (err) {
1010 case -EBUSY:
1011 /* Someone else made a mount here whilst we were busy */
Al Viro19a167a2011-01-17 01:35:23 -05001012 return 0;
David Howellsea5b7782011-01-14 19:10:03 +00001013 case 0:
Al Viro8aef1882011-06-16 15:10:06 +01001014 path_put(path);
David Howellsea5b7782011-01-14 19:10:03 +00001015 path->mnt = mnt;
1016 path->dentry = dget(mnt->mnt_root);
David Howellsea5b7782011-01-14 19:10:03 +00001017 return 0;
Al Viro19a167a2011-01-17 01:35:23 -05001018 default:
1019 return err;
David Howellsea5b7782011-01-14 19:10:03 +00001020 }
Al Viro19a167a2011-01-17 01:35:23 -05001021
David Howells9875cf82011-01-14 18:45:21 +00001022}
1023
1024/*
1025 * Handle a dentry that is managed in some way.
David Howellscc53ce52011-01-14 18:45:26 +00001026 * - Flagged for transit management (autofs)
David Howells9875cf82011-01-14 18:45:21 +00001027 * - Flagged as mountpoint
1028 * - Flagged as automount point
1029 *
1030 * This may only be called in refwalk mode.
1031 *
1032 * Serialization is taken care of in namespace.c
1033 */
1034static int follow_managed(struct path *path, unsigned flags)
1035{
Al Viro8aef1882011-06-16 15:10:06 +01001036 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
David Howells9875cf82011-01-14 18:45:21 +00001037 unsigned managed;
1038 bool need_mntput = false;
Al Viro8aef1882011-06-16 15:10:06 +01001039 int ret = 0;
David Howells9875cf82011-01-14 18:45:21 +00001040
1041 /* Given that we're not holding a lock here, we retain the value in a
1042 * local variable for each dentry as we look at it so that we don't see
1043 * the components of that value change under us */
1044 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1045 managed &= DCACHE_MANAGED_DENTRY,
1046 unlikely(managed != 0)) {
David Howellscc53ce52011-01-14 18:45:26 +00001047 /* Allow the filesystem to manage the transit without i_mutex
1048 * being held. */
1049 if (managed & DCACHE_MANAGE_TRANSIT) {
1050 BUG_ON(!path->dentry->d_op);
1051 BUG_ON(!path->dentry->d_op->d_manage);
Al Viro1aed3e42011-03-18 09:09:02 -04001052 ret = path->dentry->d_op->d_manage(path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +00001053 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001054 break;
David Howellscc53ce52011-01-14 18:45:26 +00001055 }
1056
David Howells9875cf82011-01-14 18:45:21 +00001057 /* Transit to a mounted filesystem. */
1058 if (managed & DCACHE_MOUNTED) {
1059 struct vfsmount *mounted = lookup_mnt(path);
1060 if (mounted) {
1061 dput(path->dentry);
1062 if (need_mntput)
1063 mntput(path->mnt);
1064 path->mnt = mounted;
1065 path->dentry = dget(mounted->mnt_root);
1066 need_mntput = true;
1067 continue;
1068 }
1069
1070 /* Something is mounted on this dentry in another
1071 * namespace and/or whatever was mounted there in this
1072 * namespace got unmounted before we managed to get the
1073 * vfsmount_lock */
1074 }
1075
1076 /* Handle an automount point */
1077 if (managed & DCACHE_NEED_AUTOMOUNT) {
1078 ret = follow_automount(path, flags, &need_mntput);
1079 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001080 break;
David Howells9875cf82011-01-14 18:45:21 +00001081 continue;
1082 }
1083
1084 /* We didn't change the current path point */
1085 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 }
Al Viro8aef1882011-06-16 15:10:06 +01001087
1088 if (need_mntput && path->mnt == mnt)
1089 mntput(path->mnt);
1090 if (ret == -EISDIR)
1091 ret = 0;
Al Viroa3fbbde2011-11-07 21:21:26 +00001092 return ret < 0 ? ret : need_mntput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093}
1094
David Howellscc53ce52011-01-14 18:45:26 +00001095int follow_down_one(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
1097 struct vfsmount *mounted;
1098
Al Viro1c755af2009-04-18 14:06:57 -04001099 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -04001101 dput(path->dentry);
1102 mntput(path->mnt);
1103 path->mnt = mounted;
1104 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 return 1;
1106 }
1107 return 0;
1108}
1109
Ian Kent62a73752011-03-25 01:51:02 +08001110static inline bool managed_dentry_might_block(struct dentry *dentry)
1111{
1112 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
1113 dentry->d_op->d_manage(dentry, true) < 0);
1114}
1115
David Howells9875cf82011-01-14 18:45:21 +00001116/*
Al Viro287548e2011-05-27 06:50:06 -04001117 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1118 * we meet a managed dentry that would need blocking.
David Howells9875cf82011-01-14 18:45:21 +00001119 */
1120static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
Al Viro287548e2011-05-27 06:50:06 -04001121 struct inode **inode)
David Howells9875cf82011-01-14 18:45:21 +00001122{
Ian Kent62a73752011-03-25 01:51:02 +08001123 for (;;) {
Al Viroc7105362011-11-24 18:22:03 -05001124 struct mount *mounted;
Ian Kent62a73752011-03-25 01:51:02 +08001125 /*
1126 * Don't forget we might have a non-mountpoint managed dentry
1127 * that wants to block transit.
1128 */
Al Viro287548e2011-05-27 06:50:06 -04001129 if (unlikely(managed_dentry_might_block(path->dentry)))
David Howellsab909112011-01-14 18:46:51 +00001130 return false;
Ian Kent62a73752011-03-25 01:51:02 +08001131
1132 if (!d_mountpoint(path->dentry))
1133 break;
1134
David Howells9875cf82011-01-14 18:45:21 +00001135 mounted = __lookup_mnt(path->mnt, path->dentry, 1);
1136 if (!mounted)
1137 break;
Al Viroc7105362011-11-24 18:22:03 -05001138 path->mnt = &mounted->mnt;
1139 path->dentry = mounted->mnt.mnt_root;
Al Viroa3fbbde2011-11-07 21:21:26 +00001140 nd->flags |= LOOKUP_JUMPED;
David Howells9875cf82011-01-14 18:45:21 +00001141 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
Linus Torvalds59430262011-07-18 15:43:29 -07001142 /*
1143 * Update the inode too. We don't need to re-check the
1144 * dentry sequence number here after this d_inode read,
1145 * because a mount-point is always pinned.
1146 */
1147 *inode = path->dentry->d_inode;
David Howells9875cf82011-01-14 18:45:21 +00001148 }
David Howells9875cf82011-01-14 18:45:21 +00001149 return true;
1150}
1151
Al Virodea39372011-05-27 06:53:39 -04001152static void follow_mount_rcu(struct nameidata *nd)
Al Viro287548e2011-05-27 06:50:06 -04001153{
Al Virodea39372011-05-27 06:53:39 -04001154 while (d_mountpoint(nd->path.dentry)) {
Al Viroc7105362011-11-24 18:22:03 -05001155 struct mount *mounted;
Al Virodea39372011-05-27 06:53:39 -04001156 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
Al Viro287548e2011-05-27 06:50:06 -04001157 if (!mounted)
1158 break;
Al Viroc7105362011-11-24 18:22:03 -05001159 nd->path.mnt = &mounted->mnt;
1160 nd->path.dentry = mounted->mnt.mnt_root;
Al Virodea39372011-05-27 06:53:39 -04001161 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro287548e2011-05-27 06:50:06 -04001162 }
1163}
1164
Nick Piggin31e6b012011-01-07 17:49:52 +11001165static int follow_dotdot_rcu(struct nameidata *nd)
1166{
Al Viro589a49f2014-09-13 21:55:46 -04001167 if (!nd->root.mnt)
1168 set_root_rcu(nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11001169
David Howells9875cf82011-01-14 18:45:21 +00001170 while (1) {
Nick Piggin31e6b012011-01-07 17:49:52 +11001171 if (nd->path.dentry == nd->root.dentry &&
1172 nd->path.mnt == nd->root.mnt) {
1173 break;
1174 }
1175 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1176 struct dentry *old = nd->path.dentry;
1177 struct dentry *parent = old->d_parent;
1178 unsigned seq;
1179
1180 seq = read_seqcount_begin(&parent->d_seq);
1181 if (read_seqcount_retry(&old->d_seq, nd->seq))
Al Viroef7562d2011-03-04 14:35:59 -05001182 goto failed;
Nick Piggin31e6b012011-01-07 17:49:52 +11001183 nd->path.dentry = parent;
1184 nd->seq = seq;
Eric W. Biedermanbf2e9fa2015-08-15 20:27:13 -05001185 if (unlikely(!path_connected(&nd->path)))
1186 goto failed;
Nick Piggin31e6b012011-01-07 17:49:52 +11001187 break;
1188 }
1189 if (!follow_up_rcu(&nd->path))
1190 break;
1191 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Nick Piggin31e6b012011-01-07 17:49:52 +11001192 }
Al Virodea39372011-05-27 06:53:39 -04001193 follow_mount_rcu(nd);
1194 nd->inode = nd->path.dentry->d_inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001195 return 0;
Al Viroef7562d2011-03-04 14:35:59 -05001196
1197failed:
1198 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001199 if (!(nd->flags & LOOKUP_ROOT))
1200 nd->root.mnt = NULL;
Al Viro9caac002012-07-18 20:43:19 +04001201 unlock_rcu_walk();
Al Viroef7562d2011-03-04 14:35:59 -05001202 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001203}
1204
David Howells9875cf82011-01-14 18:45:21 +00001205/*
David Howellscc53ce52011-01-14 18:45:26 +00001206 * Follow down to the covering mount currently visible to userspace. At each
1207 * point, the filesystem owning that dentry may be queried as to whether the
1208 * caller is permitted to proceed or not.
David Howellscc53ce52011-01-14 18:45:26 +00001209 */
Al Viro7cc90cc2011-03-18 09:04:20 -04001210int follow_down(struct path *path)
David Howellscc53ce52011-01-14 18:45:26 +00001211{
1212 unsigned managed;
1213 int ret;
1214
1215 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1216 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1217 /* Allow the filesystem to manage the transit without i_mutex
1218 * being held.
1219 *
1220 * We indicate to the filesystem if someone is trying to mount
1221 * something here. This gives autofs the chance to deny anyone
1222 * other than its daemon the right to mount on its
1223 * superstructure.
1224 *
1225 * The filesystem may sleep at this point.
1226 */
1227 if (managed & DCACHE_MANAGE_TRANSIT) {
1228 BUG_ON(!path->dentry->d_op);
1229 BUG_ON(!path->dentry->d_op->d_manage);
David Howellsab909112011-01-14 18:46:51 +00001230 ret = path->dentry->d_op->d_manage(
Al Viro1aed3e42011-03-18 09:09:02 -04001231 path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +00001232 if (ret < 0)
1233 return ret == -EISDIR ? 0 : ret;
1234 }
1235
1236 /* Transit to a mounted filesystem. */
1237 if (managed & DCACHE_MOUNTED) {
1238 struct vfsmount *mounted = lookup_mnt(path);
1239 if (!mounted)
1240 break;
1241 dput(path->dentry);
1242 mntput(path->mnt);
1243 path->mnt = mounted;
1244 path->dentry = dget(mounted->mnt_root);
1245 continue;
1246 }
1247
1248 /* Don't handle automount points here */
1249 break;
1250 }
1251 return 0;
1252}
1253
1254/*
David Howells9875cf82011-01-14 18:45:21 +00001255 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1256 */
1257static void follow_mount(struct path *path)
1258{
1259 while (d_mountpoint(path->dentry)) {
1260 struct vfsmount *mounted = lookup_mnt(path);
1261 if (!mounted)
1262 break;
1263 dput(path->dentry);
1264 mntput(path->mnt);
1265 path->mnt = mounted;
1266 path->dentry = dget(mounted->mnt_root);
1267 }
1268}
1269
Eric W. Biedermanbf2e9fa2015-08-15 20:27:13 -05001270static int follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
Al Viro589a49f2014-09-13 21:55:46 -04001272 if (!nd->root.mnt)
1273 set_root(nd);
Andreas Mohre518ddb2006-09-29 02:01:22 -07001274
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 while(1) {
Jan Blunck4ac91372008-02-14 19:34:32 -08001276 struct dentry *old = nd->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Al Viro2a737872009-04-07 11:49:53 -04001278 if (nd->path.dentry == nd->root.dentry &&
1279 nd->path.mnt == nd->root.mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 break;
1281 }
Jan Blunck4ac91372008-02-14 19:34:32 -08001282 if (nd->path.dentry != nd->path.mnt->mnt_root) {
Al Viro3088dd72010-01-30 15:47:29 -05001283 /* rare case of legitimate dget_parent()... */
1284 nd->path.dentry = dget_parent(nd->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 dput(old);
Eric W. Biedermanbf2e9fa2015-08-15 20:27:13 -05001286 if (unlikely(!path_connected(&nd->path))) {
1287 path_put(&nd->path);
1288 return -ENOENT;
1289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 break;
1291 }
Al Viro3088dd72010-01-30 15:47:29 -05001292 if (!follow_up(&nd->path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 }
Al Viro79ed0222009-04-18 13:59:41 -04001295 follow_mount(&nd->path);
Nick Piggin31e6b012011-01-07 17:49:52 +11001296 nd->inode = nd->path.dentry->d_inode;
Eric W. Biedermanbf2e9fa2015-08-15 20:27:13 -05001297 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298}
1299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300/*
Miklos Szeredibad61182012-03-26 12:54:24 +02001301 * This looks up the name in dcache, possibly revalidates the old dentry and
1302 * allocates a new one if not found or not valid. In the need_lookup argument
1303 * returns whether i_op->lookup is necessary.
1304 *
1305 * dir->d_inode->i_mutex must be held
Nick Pigginbaa03892010-08-18 04:37:31 +10001306 */
Miklos Szeredibad61182012-03-26 12:54:24 +02001307static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
Al Viro61b61da2012-06-22 12:42:10 +04001308 unsigned int flags, bool *need_lookup)
Nick Pigginbaa03892010-08-18 04:37:31 +10001309{
Nick Pigginbaa03892010-08-18 04:37:31 +10001310 struct dentry *dentry;
Miklos Szeredibad61182012-03-26 12:54:24 +02001311 int error;
Nick Pigginbaa03892010-08-18 04:37:31 +10001312
Miklos Szeredibad61182012-03-26 12:54:24 +02001313 *need_lookup = false;
1314 dentry = d_lookup(dir, name);
1315 if (dentry) {
1316 if (d_need_lookup(dentry)) {
1317 *need_lookup = true;
1318 } else if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
Al Viro61b61da2012-06-22 12:42:10 +04001319 error = d_revalidate(dentry, flags);
Miklos Szeredibad61182012-03-26 12:54:24 +02001320 if (unlikely(error <= 0)) {
1321 if (error < 0) {
1322 dput(dentry);
1323 return ERR_PTR(error);
1324 } else if (!d_invalidate(dentry)) {
1325 dput(dentry);
1326 dentry = NULL;
1327 }
1328 }
1329 }
1330 }
Nick Pigginbaa03892010-08-18 04:37:31 +10001331
Miklos Szeredibad61182012-03-26 12:54:24 +02001332 if (!dentry) {
1333 dentry = d_alloc(dir, name);
1334 if (unlikely(!dentry))
1335 return ERR_PTR(-ENOMEM);
Nick Pigginbaa03892010-08-18 04:37:31 +10001336
Miklos Szeredibad61182012-03-26 12:54:24 +02001337 *need_lookup = true;
Nick Pigginbaa03892010-08-18 04:37:31 +10001338 }
1339 return dentry;
1340}
1341
1342/*
Miklos Szeredibad61182012-03-26 12:54:24 +02001343 * Call i_op->lookup on the dentry. The dentry must be negative but may be
1344 * hashed if it was pouplated with DCACHE_NEED_LOOKUP.
1345 *
1346 * dir->d_inode->i_mutex must be held
Josef Bacik44396f42011-05-31 11:58:49 -04001347 */
Miklos Szeredibad61182012-03-26 12:54:24 +02001348static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
Al Viro292d3af2012-06-10 17:17:17 -04001349 unsigned int flags)
Josef Bacik44396f42011-05-31 11:58:49 -04001350{
Josef Bacik44396f42011-05-31 11:58:49 -04001351 struct dentry *old;
1352
1353 /* Don't create child dentry for a dead directory. */
Miklos Szeredibad61182012-03-26 12:54:24 +02001354 if (unlikely(IS_DEADDIR(dir))) {
Miklos Szeredie188dc02012-02-03 14:25:18 +01001355 dput(dentry);
Josef Bacik44396f42011-05-31 11:58:49 -04001356 return ERR_PTR(-ENOENT);
Miklos Szeredie188dc02012-02-03 14:25:18 +01001357 }
Josef Bacik44396f42011-05-31 11:58:49 -04001358
Al Viro292d3af2012-06-10 17:17:17 -04001359 old = dir->i_op->lookup(dir, dentry, flags);
Josef Bacik44396f42011-05-31 11:58:49 -04001360 if (unlikely(old)) {
1361 dput(dentry);
1362 dentry = old;
1363 }
1364 return dentry;
1365}
1366
Al Viroa3255542012-03-30 14:41:51 -04001367static struct dentry *__lookup_hash(struct qstr *name,
Al Viro292d3af2012-06-10 17:17:17 -04001368 struct dentry *base, unsigned int flags)
Al Viroa3255542012-03-30 14:41:51 -04001369{
Miklos Szeredibad61182012-03-26 12:54:24 +02001370 bool need_lookup;
Al Viroa3255542012-03-30 14:41:51 -04001371 struct dentry *dentry;
1372
Al Viro292d3af2012-06-10 17:17:17 -04001373 dentry = lookup_dcache(name, base, flags, &need_lookup);
Miklos Szeredibad61182012-03-26 12:54:24 +02001374 if (!need_lookup)
1375 return dentry;
Al Viroa3255542012-03-30 14:41:51 -04001376
Al Viro292d3af2012-06-10 17:17:17 -04001377 return lookup_real(base->d_inode, dentry, flags);
Al Viroa3255542012-03-30 14:41:51 -04001378}
1379
Josef Bacik44396f42011-05-31 11:58:49 -04001380/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 * It's more convoluted than I'd like it to be, but... it's still fairly
1382 * small and for now I'd prefer to have fast path as straight as possible.
1383 * It _is_ time-critical.
1384 */
Miklos Szeredidb77d762012-05-21 17:30:05 +02001385static int lookup_fast(struct nameidata *nd, struct qstr *name,
1386 struct path *path, struct inode **inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
Jan Blunck4ac91372008-02-14 19:34:32 -08001388 struct vfsmount *mnt = nd->path.mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +11001389 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro5a18fff2011-03-11 04:44:53 -05001390 int need_reval = 1;
1391 int status = 1;
David Howells9875cf82011-01-14 18:45:21 +00001392 int err;
1393
Al Viro3cac2602009-08-13 18:27:43 +04001394 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001395 * Rename seqlock is not required here because in the off chance
1396 * of a false negative due to a concurrent rename, we're going to
1397 * do the non-racy lookup, below.
1398 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001399 if (nd->flags & LOOKUP_RCU) {
1400 unsigned seq;
Linus Torvaldsb7d3740d2012-05-04 14:59:14 -07001401 dentry = __d_lookup_rcu(parent, name, &seq, nd->inode);
Al Viro5a18fff2011-03-11 04:44:53 -05001402 if (!dentry)
1403 goto unlazy;
1404
Linus Torvaldsb7d3740d2012-05-04 14:59:14 -07001405 /*
1406 * This sequence count validates that the inode matches
1407 * the dentry name information from lookup.
1408 */
1409 *inode = dentry->d_inode;
1410 if (read_seqcount_retry(&dentry->d_seq, seq))
1411 return -ECHILD;
1412
1413 /*
1414 * This sequence count validates that the parent had no
1415 * changes while we did the lookup of the dentry above.
1416 *
1417 * The memory barrier in read_seqcount_begin of child is
1418 * enough, we can use __read_seqcount_retry here.
1419 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001420 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1421 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001422 nd->seq = seq;
Al Viro5a18fff2011-03-11 04:44:53 -05001423
Miklos Szeredifa4ee152012-03-26 12:54:19 +02001424 if (unlikely(d_need_lookup(dentry)))
1425 goto unlazy;
Al Viro24643082011-02-15 01:26:22 -05001426 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
Al Viro28ca3262012-06-10 16:10:59 -04001427 status = d_revalidate(dentry, nd->flags);
Al Viro5a18fff2011-03-11 04:44:53 -05001428 if (unlikely(status <= 0)) {
1429 if (status != -ECHILD)
1430 need_reval = 0;
1431 goto unlazy;
1432 }
Al Viro24643082011-02-15 01:26:22 -05001433 }
Nick Piggin31e6b012011-01-07 17:49:52 +11001434 path->mnt = mnt;
1435 path->dentry = dentry;
Al Virod6e9bd22011-05-27 07:03:15 -04001436 if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1437 goto unlazy;
1438 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1439 goto unlazy;
1440 return 0;
Al Viro5a18fff2011-03-11 04:44:53 -05001441unlazy:
Al Viro19660af2011-03-25 10:32:48 -04001442 if (unlazy_walk(nd, dentry))
1443 return -ECHILD;
Al Viro5a18fff2011-03-11 04:44:53 -05001444 } else {
1445 dentry = __d_lookup(parent, name);
Nick Piggin31e6b012011-01-07 17:49:52 +11001446 }
Al Viro5a18fff2011-03-11 04:44:53 -05001447
Al Viro81e6f522012-03-30 14:48:04 -04001448 if (unlikely(!dentry))
1449 goto need_lookup;
Al Viro5a18fff2011-03-11 04:44:53 -05001450
Al Viro81e6f522012-03-30 14:48:04 -04001451 if (unlikely(d_need_lookup(dentry))) {
1452 dput(dentry);
1453 goto need_lookup;
Al Viro24643082011-02-15 01:26:22 -05001454 }
Al Viro81e6f522012-03-30 14:48:04 -04001455
Al Viro5a18fff2011-03-11 04:44:53 -05001456 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
Al Viro28ca3262012-06-10 16:10:59 -04001457 status = d_revalidate(dentry, nd->flags);
Al Viro5a18fff2011-03-11 04:44:53 -05001458 if (unlikely(status <= 0)) {
1459 if (status < 0) {
1460 dput(dentry);
1461 return status;
1462 }
1463 if (!d_invalidate(dentry)) {
1464 dput(dentry);
Al Viro81e6f522012-03-30 14:48:04 -04001465 goto need_lookup;
Al Viro5a18fff2011-03-11 04:44:53 -05001466 }
1467 }
Miklos Szeredidb77d762012-05-21 17:30:05 +02001468
David Howells9875cf82011-01-14 18:45:21 +00001469 path->mnt = mnt;
1470 path->dentry = dentry;
1471 err = follow_managed(path, nd->flags);
Ian Kent89312212011-01-18 12:06:10 +08001472 if (unlikely(err < 0)) {
1473 path_put_conditional(path, nd);
David Howells9875cf82011-01-14 18:45:21 +00001474 return err;
Ian Kent89312212011-01-18 12:06:10 +08001475 }
Al Viroa3fbbde2011-11-07 21:21:26 +00001476 if (err)
1477 nd->flags |= LOOKUP_JUMPED;
David Howells9875cf82011-01-14 18:45:21 +00001478 *inode = path->dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 return 0;
Al Viro81e6f522012-03-30 14:48:04 -04001480
1481need_lookup:
Miklos Szeredidb77d762012-05-21 17:30:05 +02001482 return 1;
1483}
1484
1485/* Fast lookup failed, do it the slow way */
1486static int lookup_slow(struct nameidata *nd, struct qstr *name,
1487 struct path *path)
1488{
1489 struct dentry *dentry, *parent;
1490 int err;
1491
1492 parent = nd->path.dentry;
Al Viro81e6f522012-03-30 14:48:04 -04001493 BUG_ON(nd->inode != parent->d_inode);
1494
1495 mutex_lock(&parent->d_inode->i_mutex);
Al Viro292d3af2012-06-10 17:17:17 -04001496 dentry = __lookup_hash(name, parent, nd->flags);
Al Viro81e6f522012-03-30 14:48:04 -04001497 mutex_unlock(&parent->d_inode->i_mutex);
1498 if (IS_ERR(dentry))
1499 return PTR_ERR(dentry);
Miklos Szeredidb77d762012-05-21 17:30:05 +02001500 path->mnt = nd->path.mnt;
1501 path->dentry = dentry;
1502 err = follow_managed(path, nd->flags);
1503 if (unlikely(err < 0)) {
1504 path_put_conditional(path, nd);
1505 return err;
1506 }
1507 if (err)
1508 nd->flags |= LOOKUP_JUMPED;
1509 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510}
1511
Al Viro52094c82011-02-21 21:34:47 -05001512static inline int may_lookup(struct nameidata *nd)
1513{
1514 if (nd->flags & LOOKUP_RCU) {
Daniel Rosenberg17285112016-10-26 16:27:45 -07001515 int err = inode_permission2(nd->path.mnt, nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
Al Viro52094c82011-02-21 21:34:47 -05001516 if (err != -ECHILD)
1517 return err;
Al Viro19660af2011-03-25 10:32:48 -04001518 if (unlazy_walk(nd, NULL))
Al Viro52094c82011-02-21 21:34:47 -05001519 return -ECHILD;
1520 }
Daniel Rosenberg17285112016-10-26 16:27:45 -07001521 return inode_permission2(nd->path.mnt, nd->inode, MAY_EXEC);
Al Viro52094c82011-02-21 21:34:47 -05001522}
1523
Al Viro9856fa12011-03-04 14:22:06 -05001524static inline int handle_dots(struct nameidata *nd, int type)
1525{
1526 if (type == LAST_DOTDOT) {
1527 if (nd->flags & LOOKUP_RCU) {
1528 if (follow_dotdot_rcu(nd))
1529 return -ECHILD;
1530 } else
Eric W. Biedermanbf2e9fa2015-08-15 20:27:13 -05001531 return follow_dotdot(nd);
Al Viro9856fa12011-03-04 14:22:06 -05001532 }
1533 return 0;
1534}
1535
Al Viro951361f2011-03-04 14:44:37 -05001536static void terminate_walk(struct nameidata *nd)
1537{
1538 if (!(nd->flags & LOOKUP_RCU)) {
1539 path_put(&nd->path);
1540 } else {
1541 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001542 if (!(nd->flags & LOOKUP_ROOT))
1543 nd->root.mnt = NULL;
Al Viro9caac002012-07-18 20:43:19 +04001544 unlock_rcu_walk();
Al Viro951361f2011-03-04 14:44:37 -05001545 }
1546}
1547
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001548/*
1549 * Do we need to follow links? We _really_ want to be able
1550 * to do this check without having to look at inode->i_op,
1551 * so we keep a cache of "no, this doesn't need follow_link"
1552 * for the common case.
1553 */
Linus Torvalds7813b942011-08-07 09:53:20 -07001554static inline int should_follow_link(struct inode *inode, int follow)
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001555{
1556 if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
1557 if (likely(inode->i_op->follow_link))
1558 return follow;
1559
1560 /* This gets set once for the inode lifetime */
1561 spin_lock(&inode->i_lock);
1562 inode->i_opflags |= IOP_NOFOLLOW;
1563 spin_unlock(&inode->i_lock);
1564 }
1565 return 0;
1566}
1567
Al Viroce57dfc2011-03-13 19:58:58 -04001568static inline int walk_component(struct nameidata *nd, struct path *path,
1569 struct qstr *name, int type, int follow)
1570{
1571 struct inode *inode;
1572 int err;
1573 /*
1574 * "." and ".." are special - ".." especially so because it has
1575 * to be able to know about the current root directory and
1576 * parent relationships.
1577 */
1578 if (unlikely(type != LAST_NORM))
1579 return handle_dots(nd, type);
Miklos Szeredidb77d762012-05-21 17:30:05 +02001580 err = lookup_fast(nd, name, path, &inode);
Al Viroce57dfc2011-03-13 19:58:58 -04001581 if (unlikely(err)) {
Miklos Szeredidb77d762012-05-21 17:30:05 +02001582 if (err < 0)
1583 goto out_err;
1584
1585 err = lookup_slow(nd, name, path);
1586 if (err < 0)
1587 goto out_err;
1588
1589 inode = path->dentry->d_inode;
Al Viroce57dfc2011-03-13 19:58:58 -04001590 }
Miklos Szeredidb77d762012-05-21 17:30:05 +02001591 err = -ENOENT;
1592 if (!inode)
1593 goto out_path_put;
1594
Linus Torvalds7813b942011-08-07 09:53:20 -07001595 if (should_follow_link(inode, follow)) {
Al Viro19660af2011-03-25 10:32:48 -04001596 if (nd->flags & LOOKUP_RCU) {
Al Viroa377bbd2015-04-24 15:47:07 -04001597 if (unlikely(nd->path.mnt != path->mnt ||
1598 unlazy_walk(nd, path->dentry))) {
Miklos Szeredidb77d762012-05-21 17:30:05 +02001599 err = -ECHILD;
1600 goto out_err;
Al Viro19660af2011-03-25 10:32:48 -04001601 }
1602 }
Al Viroce57dfc2011-03-13 19:58:58 -04001603 BUG_ON(inode != path->dentry->d_inode);
1604 return 1;
1605 }
1606 path_to_nameidata(path, nd);
1607 nd->inode = inode;
1608 return 0;
Miklos Szeredidb77d762012-05-21 17:30:05 +02001609
1610out_path_put:
1611 path_to_nameidata(path, nd);
1612out_err:
1613 terminate_walk(nd);
1614 return err;
Al Viroce57dfc2011-03-13 19:58:58 -04001615}
1616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617/*
Al Virob3563792011-03-14 21:54:55 -04001618 * This limits recursive symlink follows to 8, while
1619 * limiting consecutive symlinks to 40.
1620 *
1621 * Without that kind of total limit, nasty chains of consecutive
1622 * symlinks can cause almost arbitrarily long lookups.
1623 */
1624static inline int nested_symlink(struct path *path, struct nameidata *nd)
1625{
1626 int res;
1627
Al Virob3563792011-03-14 21:54:55 -04001628 if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1629 path_put_conditional(path, nd);
1630 path_put(&nd->path);
1631 return -ELOOP;
1632 }
Erez Zadok1a4022f2011-05-21 01:19:59 -04001633 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
Al Virob3563792011-03-14 21:54:55 -04001634
1635 nd->depth++;
1636 current->link_count++;
1637
1638 do {
1639 struct path link = *path;
1640 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04001641
1642 res = follow_link(&link, nd, &cookie);
Al Viro2c4d7392012-06-10 04:15:17 -04001643 if (res)
1644 break;
1645 res = walk_component(nd, path, &nd->last,
1646 nd->last_type, LOOKUP_FOLLOW);
Al Viro574197e2011-03-14 22:20:34 -04001647 put_link(nd, &link, cookie);
Al Virob3563792011-03-14 21:54:55 -04001648 } while (res > 0);
1649
1650 current->link_count--;
1651 nd->depth--;
1652 return res;
1653}
1654
1655/*
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001656 * We really don't want to look at inode->i_op->lookup
1657 * when we don't have to. So we keep a cache bit in
1658 * the inode ->i_opflags field that says "yes, we can
1659 * do lookup on this inode".
1660 */
1661static inline int can_lookup(struct inode *inode)
1662{
1663 if (likely(inode->i_opflags & IOP_LOOKUP))
1664 return 1;
1665 if (likely(!inode->i_op->lookup))
1666 return 0;
1667
1668 /* We do this once for the lifetime of the inode */
1669 spin_lock(&inode->i_lock);
1670 inode->i_opflags |= IOP_LOOKUP;
1671 spin_unlock(&inode->i_lock);
1672 return 1;
1673}
1674
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001675/*
1676 * We can do the critical dentry name comparison and hashing
1677 * operations one word at a time, but we are limited to:
1678 *
1679 * - Architectures with fast unaligned word accesses. We could
1680 * do a "get_unaligned()" if this helps and is sufficiently
1681 * fast.
1682 *
1683 * - Little-endian machines (so that we can generate the mask
1684 * of low bytes efficiently). Again, we *could* do a byte
1685 * swapping load on big-endian architectures if that is not
1686 * expensive enough to make the optimization worthless.
1687 *
1688 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1689 * do not trap on the (extremely unlikely) case of a page
1690 * crossing operation.
1691 *
1692 * - Furthermore, we need an efficient 64-bit compile for the
1693 * 64-bit case in order to generate the "number of bytes in
1694 * the final mask". Again, that could be replaced with a
1695 * efficient population count instruction or similar.
1696 */
1697#ifdef CONFIG_DCACHE_WORD_ACCESS
1698
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001699#include <asm/word-at-a-time.h>
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001700
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001701#ifdef CONFIG_64BIT
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001702
1703static inline unsigned int fold_hash(unsigned long hash)
1704{
Linus Torvalds07e997d2014-09-13 11:30:10 -07001705 return hash_64(hash, 32);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001706}
1707
1708#else /* 32-bit case */
1709
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001710#define fold_hash(x) (x)
1711
1712#endif
1713
1714unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1715{
1716 unsigned long a, mask;
1717 unsigned long hash = 0;
1718
1719 for (;;) {
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001720 a = load_unaligned_zeropad(name);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001721 if (len < sizeof(unsigned long))
1722 break;
1723 hash += a;
Al Virof132c5b2012-03-22 21:59:52 +00001724 hash *= 9;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001725 name += sizeof(unsigned long);
1726 len -= sizeof(unsigned long);
1727 if (!len)
1728 goto done;
1729 }
1730 mask = ~(~0ul << len*8);
1731 hash += mask & a;
1732done:
1733 return fold_hash(hash);
1734}
1735EXPORT_SYMBOL(full_name_hash);
1736
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001737/*
1738 * Calculate the length and hash of the path component, and
1739 * return the length of the component;
1740 */
1741static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1742{
1743 unsigned long a, mask, hash, len;
1744
1745 hash = a = 0;
1746 len = -sizeof(unsigned long);
1747 do {
1748 hash = (hash + a) * 9;
1749 len += sizeof(unsigned long);
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001750 a = load_unaligned_zeropad(name+len);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001751 /* Do we have any NUL or '/' bytes in this word? */
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001752 mask = has_zero(a) | has_zero(a ^ REPEAT_BYTE('/'));
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001753 } while (!mask);
1754
1755 /* The mask *below* the first high bit set */
1756 mask = (mask - 1) & ~mask;
1757 mask >>= 7;
1758 hash += a & mask;
1759 *hashp = fold_hash(hash);
1760
1761 return len + count_masked_bytes(mask);
1762}
1763
1764#else
1765
Linus Torvalds0145acc2012-03-02 14:32:59 -08001766unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1767{
1768 unsigned long hash = init_name_hash();
1769 while (len--)
1770 hash = partial_name_hash(*name++, hash);
1771 return end_name_hash(hash);
1772}
Linus Torvaldsae942ae2012-03-02 19:40:57 -08001773EXPORT_SYMBOL(full_name_hash);
Linus Torvalds0145acc2012-03-02 14:32:59 -08001774
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001775/*
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001776 * We know there's a real path component here of at least
1777 * one character.
1778 */
1779static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1780{
1781 unsigned long hash = init_name_hash();
1782 unsigned long len = 0, c;
1783
1784 c = (unsigned char)*name;
1785 do {
1786 len++;
1787 hash = partial_name_hash(c, hash);
1788 c = (unsigned char)name[len];
1789 } while (c && c != '/');
1790 *hashp = end_name_hash(hash);
1791 return len;
1792}
1793
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001794#endif
1795
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001796/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01001798 * This is the basic name resolution function, turning a pathname into
1799 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01001801 * Returns 0 and nd will have valid dentry and mnt on success.
1802 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 */
Al Viro6de88d72009-08-09 01:41:57 +04001804static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
1806 struct path next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
1809 while (*name=='/')
1810 name++;
1811 if (!*name)
Al Viro086e1832011-02-22 20:56:27 -05001812 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 /* At this point we know we have a real path component. */
1815 for(;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 struct qstr this;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001817 long len;
Al Virofe479a52011-02-22 15:10:03 -05001818 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
Al Viro52094c82011-02-21 21:34:47 -05001820 err = may_lookup(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 if (err)
1822 break;
1823
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001824 len = hash_name(name, &this.hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 this.name = name;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001826 this.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
Al Virofe479a52011-02-22 15:10:03 -05001828 type = LAST_NORM;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001829 if (name[0] == '.') switch (len) {
Al Virofe479a52011-02-22 15:10:03 -05001830 case 2:
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001831 if (name[1] == '.') {
Al Virofe479a52011-02-22 15:10:03 -05001832 type = LAST_DOTDOT;
Al Viro16c2cd72011-02-22 15:50:10 -05001833 nd->flags |= LOOKUP_JUMPED;
1834 }
Al Virofe479a52011-02-22 15:10:03 -05001835 break;
1836 case 1:
1837 type = LAST_DOT;
1838 }
Al Viro5a202bc2011-03-08 14:17:44 -05001839 if (likely(type == LAST_NORM)) {
1840 struct dentry *parent = nd->path.dentry;
Al Viro16c2cd72011-02-22 15:50:10 -05001841 nd->flags &= ~LOOKUP_JUMPED;
Al Viro5a202bc2011-03-08 14:17:44 -05001842 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1843 err = parent->d_op->d_hash(parent, nd->inode,
1844 &this);
1845 if (err < 0)
1846 break;
1847 }
1848 }
Al Virofe479a52011-02-22 15:10:03 -05001849
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001850 if (!name[len])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 goto last_component;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001852 /*
1853 * If it wasn't NUL, we know it was '/'. Skip that
1854 * slash, and continue until no more slashes.
1855 */
1856 do {
1857 len++;
1858 } while (unlikely(name[len] == '/'));
1859 if (!name[len])
Al Virob3563792011-03-14 21:54:55 -04001860 goto last_component;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001861 name += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
Al Viroce57dfc2011-03-13 19:58:58 -04001863 err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1864 if (err < 0)
1865 return err;
Al Virofe479a52011-02-22 15:10:03 -05001866
Al Viroce57dfc2011-03-13 19:58:58 -04001867 if (err) {
Al Virob3563792011-03-14 21:54:55 -04001868 err = nested_symlink(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 if (err)
Al Viroa7472ba2011-03-04 14:39:30 -05001870 return err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001871 }
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001872 if (can_lookup(nd->inode))
1873 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 err = -ENOTDIR;
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001875 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 /* here ends the main loop */
1877
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878last_component:
Al Virob3563792011-03-14 21:54:55 -04001879 nd->last = this;
1880 nd->last_type = type;
Al Viro086e1832011-02-22 20:56:27 -05001881 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 }
Al Viro951361f2011-03-04 14:44:37 -05001883 terminate_walk(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 return err;
1885}
1886
Al Viro70e9b352011-03-05 21:12:22 -05001887static int path_init(int dfd, const char *name, unsigned int flags,
1888 struct nameidata *nd, struct file **fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889{
Prasanna Medaea3834d2005-04-29 16:00:17 +01001890 int retval = 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001891 int fput_needed;
1892 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
1894 nd->last_type = LAST_ROOT; /* if there are only slashes... */
Al Viro16c2cd72011-02-22 15:50:10 -05001895 nd->flags = flags | LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 nd->depth = 0;
Al Viro5b6ca022011-03-09 23:04:47 -05001897 if (flags & LOOKUP_ROOT) {
1898 struct inode *inode = nd->root.dentry->d_inode;
Daniel Rosenberg17285112016-10-26 16:27:45 -07001899 struct vfsmount *mnt = nd->root.mnt;
Al Viro73d049a2011-03-11 12:08:24 -05001900 if (*name) {
1901 if (!inode->i_op->lookup)
1902 return -ENOTDIR;
Daniel Rosenberg17285112016-10-26 16:27:45 -07001903 retval = inode_permission2(mnt, inode, MAY_EXEC);
Al Viro73d049a2011-03-11 12:08:24 -05001904 if (retval)
1905 return retval;
1906 }
Al Viro5b6ca022011-03-09 23:04:47 -05001907 nd->path = nd->root;
1908 nd->inode = inode;
1909 if (flags & LOOKUP_RCU) {
Al Viro9caac002012-07-18 20:43:19 +04001910 lock_rcu_walk();
Al Viro5b6ca022011-03-09 23:04:47 -05001911 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1912 } else {
1913 path_get(&nd->path);
1914 }
1915 return 0;
1916 }
1917
Al Viro2a737872009-04-07 11:49:53 -04001918 nd->root.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 if (*name=='/') {
Al Viroe41f7d42011-02-22 14:02:58 -05001921 if (flags & LOOKUP_RCU) {
Al Viro9caac002012-07-18 20:43:19 +04001922 lock_rcu_walk();
Al Viro589a49f2014-09-13 21:55:46 -04001923 nd->seq = set_root_rcu(nd);
Al Viroe41f7d42011-02-22 14:02:58 -05001924 } else {
1925 set_root(nd);
1926 path_get(&nd->root);
1927 }
Al Viro2a737872009-04-07 11:49:53 -04001928 nd->path = nd->root;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001929 } else if (dfd == AT_FDCWD) {
Al Viroe41f7d42011-02-22 14:02:58 -05001930 if (flags & LOOKUP_RCU) {
1931 struct fs_struct *fs = current->fs;
1932 unsigned seq;
1933
Al Viro9caac002012-07-18 20:43:19 +04001934 lock_rcu_walk();
Al Viroe41f7d42011-02-22 14:02:58 -05001935
1936 do {
1937 seq = read_seqcount_begin(&fs->seq);
1938 nd->path = fs->pwd;
1939 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1940 } while (read_seqcount_retry(&fs->seq, seq));
1941 } else {
1942 get_fs_pwd(current->fs, &nd->path);
1943 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001944 } else {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001945 struct dentry *dentry;
Daniel Rosenberg17285112016-10-26 16:27:45 -07001946 struct vfsmount *mnt;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001947
Al Viro1abf0c72011-03-13 03:51:11 -04001948 file = fget_raw_light(dfd, &fput_needed);
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001949 retval = -EBADF;
1950 if (!file)
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001951 goto out_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001952
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001953 dentry = file->f_path.dentry;
Daniel Rosenberg17285112016-10-26 16:27:45 -07001954 mnt = file->f_path.mnt;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001955
Al Virof52e0c12011-03-14 18:56:51 -04001956 if (*name) {
1957 retval = -ENOTDIR;
1958 if (!S_ISDIR(dentry->d_inode->i_mode))
1959 goto fput_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001960
Daniel Rosenberg17285112016-10-26 16:27:45 -07001961 retval = inode_permission2(mnt, dentry->d_inode, MAY_EXEC);
Al Virof52e0c12011-03-14 18:56:51 -04001962 if (retval)
1963 goto fput_fail;
1964 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001965
Jan Blunck5dd784d2008-02-14 19:34:38 -08001966 nd->path = file->f_path;
Al Viroe41f7d42011-02-22 14:02:58 -05001967 if (flags & LOOKUP_RCU) {
1968 if (fput_needed)
Al Viro70e9b352011-03-05 21:12:22 -05001969 *fp = file;
Al Viroe41f7d42011-02-22 14:02:58 -05001970 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro9caac002012-07-18 20:43:19 +04001971 lock_rcu_walk();
Al Viroe41f7d42011-02-22 14:02:58 -05001972 } else {
1973 path_get(&file->f_path);
1974 fput_light(file, fput_needed);
1975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 }
Al Viroe41f7d42011-02-22 14:02:58 -05001977
Nick Piggin31e6b012011-01-07 17:49:52 +11001978 nd->inode = nd->path.dentry->d_inode;
Al Viro9b4a9b12009-04-07 11:44:16 -04001979 return 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001980
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001981fput_fail:
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001982 fput_light(file, fput_needed);
Al Viro9b4a9b12009-04-07 11:44:16 -04001983out_fail:
1984 return retval;
1985}
1986
Al Virobd92d7f2011-03-14 19:54:59 -04001987static inline int lookup_last(struct nameidata *nd, struct path *path)
1988{
1989 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1990 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1991
1992 nd->flags &= ~LOOKUP_PARENT;
1993 return walk_component(nd, path, &nd->last, nd->last_type,
1994 nd->flags & LOOKUP_FOLLOW);
1995}
1996
Al Viro9b4a9b12009-04-07 11:44:16 -04001997/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroee0827c2011-02-21 23:38:09 -05001998static int path_lookupat(int dfd, const char *name,
Al Viro9b4a9b12009-04-07 11:44:16 -04001999 unsigned int flags, struct nameidata *nd)
2000{
Al Viro70e9b352011-03-05 21:12:22 -05002001 struct file *base = NULL;
Al Virobd92d7f2011-03-14 19:54:59 -04002002 struct path path;
2003 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11002004
2005 /*
2006 * Path walking is largely split up into 2 different synchronisation
2007 * schemes, rcu-walk and ref-walk (explained in
2008 * Documentation/filesystems/path-lookup.txt). These share much of the
2009 * path walk code, but some things particularly setup, cleanup, and
2010 * following mounts are sufficiently divergent that functions are
2011 * duplicated. Typically there is a function foo(), and its RCU
2012 * analogue, foo_rcu().
2013 *
2014 * -ECHILD is the error number of choice (just to avoid clashes) that
2015 * is returned if some aspect of an rcu-walk fails. Such an error must
2016 * be handled by restarting a traditional ref-walk (which will always
2017 * be able to complete).
2018 */
Al Virobd92d7f2011-03-14 19:54:59 -04002019 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
Al Viroee0827c2011-02-21 23:38:09 -05002020
Al Virobd92d7f2011-03-14 19:54:59 -04002021 if (unlikely(err))
2022 return err;
Al Viroee0827c2011-02-21 23:38:09 -05002023
2024 current->total_link_count = 0;
Al Virobd92d7f2011-03-14 19:54:59 -04002025 err = link_path_walk(name, nd);
2026
2027 if (!err && !(flags & LOOKUP_PARENT)) {
Al Virobd92d7f2011-03-14 19:54:59 -04002028 err = lookup_last(nd, &path);
2029 while (err > 0) {
2030 void *cookie;
2031 struct path link = path;
Kees Cook5092e9c2012-07-25 17:29:07 -07002032 err = may_follow_link(&link, nd);
2033 if (unlikely(err))
2034 break;
Al Virobd92d7f2011-03-14 19:54:59 -04002035 nd->flags |= LOOKUP_PARENT;
Al Viro574197e2011-03-14 22:20:34 -04002036 err = follow_link(&link, nd, &cookie);
Al Viro2c4d7392012-06-10 04:15:17 -04002037 if (err)
2038 break;
2039 err = lookup_last(nd, &path);
Al Viro574197e2011-03-14 22:20:34 -04002040 put_link(nd, &link, cookie);
Al Virobd92d7f2011-03-14 19:54:59 -04002041 }
2042 }
Al Viroee0827c2011-02-21 23:38:09 -05002043
Al Viro9f1fafe2011-03-25 11:00:12 -04002044 if (!err)
2045 err = complete_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04002046
2047 if (!err && nd->flags & LOOKUP_DIRECTORY) {
Al Viroa1e12632013-06-06 19:33:47 -04002048 if (!can_lookup(nd->inode)) {
Al Virobd92d7f2011-03-14 19:54:59 -04002049 path_put(&nd->path);
Al Virobd23a532011-03-23 09:56:30 -04002050 err = -ENOTDIR;
Al Virobd92d7f2011-03-14 19:54:59 -04002051 }
2052 }
Al Viro16c2cd72011-02-22 15:50:10 -05002053
Tom Marshall384972f2017-04-28 22:46:37 +00002054 if (!err) {
2055 struct super_block *sb = nd->inode->i_sb;
2056 if (sb->s_flags & MS_RDONLY) {
Tom Marshall1a4cd212017-05-19 18:24:49 +00002057 if (d_is_su(nd->path.dentry) && !su_visible()) {
2058 path_put(&nd->path);
Tom Marshall384972f2017-04-28 22:46:37 +00002059 err = -ENOENT;
Tom Marshall1a4cd212017-05-19 18:24:49 +00002060 }
Tom Marshall384972f2017-04-28 22:46:37 +00002061 }
2062 }
2063
Al Viro70e9b352011-03-05 21:12:22 -05002064 if (base)
2065 fput(base);
Al Viroee0827c2011-02-21 23:38:09 -05002066
Al Viro5b6ca022011-03-09 23:04:47 -05002067 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
Al Viro2a737872009-04-07 11:49:53 -04002068 path_put(&nd->root);
2069 nd->root.mnt = NULL;
2070 }
Al Virobd92d7f2011-03-14 19:54:59 -04002071 return err;
Al Viroee0827c2011-02-21 23:38:09 -05002072}
Nick Piggin31e6b012011-01-07 17:49:52 +11002073
Jeff Layton3a9b94c2012-10-10 15:25:29 -04002074static int filename_lookup(int dfd, struct filename *name,
Al Viroee0827c2011-02-21 23:38:09 -05002075 unsigned int flags, struct nameidata *nd)
2076{
Jeff Layton3a9b94c2012-10-10 15:25:29 -04002077 int retval = path_lookupat(dfd, name->name, flags | LOOKUP_RCU, nd);
Al Viroee0827c2011-02-21 23:38:09 -05002078 if (unlikely(retval == -ECHILD))
Jeff Layton3a9b94c2012-10-10 15:25:29 -04002079 retval = path_lookupat(dfd, name->name, flags, nd);
Al Viroee0827c2011-02-21 23:38:09 -05002080 if (unlikely(retval == -ESTALE))
Jeff Layton3a9b94c2012-10-10 15:25:29 -04002081 retval = path_lookupat(dfd, name->name,
2082 flags | LOOKUP_REVAL, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11002083
2084 if (likely(!retval)) {
2085 if (unlikely(!audit_dummy_context())) {
2086 if (nd->path.dentry && nd->inode)
Jeff Layton3a9b94c2012-10-10 15:25:29 -04002087 audit_inode(name->name, nd->path.dentry);
Nick Piggin31e6b012011-01-07 17:49:52 +11002088 }
2089 }
Al Viro9b4a9b12009-04-07 11:44:16 -04002090 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091}
2092
Jeff Layton3a9b94c2012-10-10 15:25:29 -04002093static int do_path_lookup(int dfd, const char *name,
2094 unsigned int flags, struct nameidata *nd)
2095{
2096 struct filename filename = { .name = name };
2097
2098 return filename_lookup(dfd, &filename, flags, nd);
2099}
2100
Al Viro0613fb82012-06-15 03:01:42 +04002101/* does lookup, returns the object with parent locked */
2102struct dentry *kern_path_locked(const char *name, struct path *path)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002103{
Al Viro0613fb82012-06-15 03:01:42 +04002104 struct nameidata nd;
2105 struct dentry *d;
2106 int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd);
2107 if (err)
2108 return ERR_PTR(err);
2109 if (nd.last_type != LAST_NORM) {
2110 path_put(&nd.path);
2111 return ERR_PTR(-EINVAL);
2112 }
2113 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Al Viro59efa862012-07-22 23:46:21 +04002114 d = __lookup_hash(&nd.last, nd.path.dentry, 0);
Al Viro0613fb82012-06-15 03:01:42 +04002115 if (IS_ERR(d)) {
2116 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2117 path_put(&nd.path);
2118 return d;
2119 }
2120 *path = nd.path;
2121 return d;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002122}
2123
Al Virod1811462008-08-02 00:49:18 -04002124int kern_path(const char *name, unsigned int flags, struct path *path)
2125{
2126 struct nameidata nd;
2127 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
2128 if (!res)
2129 *path = nd.path;
2130 return res;
2131}
2132
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002133/**
2134 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2135 * @dentry: pointer to dentry of the base directory
2136 * @mnt: pointer to vfs mount of the base directory
2137 * @name: pointer to file name
2138 * @flags: lookup flags
Al Viroe0a01242011-06-27 17:00:37 -04002139 * @path: pointer to struct path to fill
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002140 */
2141int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2142 const char *name, unsigned int flags,
Al Viroe0a01242011-06-27 17:00:37 -04002143 struct path *path)
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002144{
Al Viroe0a01242011-06-27 17:00:37 -04002145 struct nameidata nd;
2146 int err;
2147 nd.root.dentry = dentry;
2148 nd.root.mnt = mnt;
2149 BUG_ON(flags & LOOKUP_PARENT);
Al Viro5b6ca022011-03-09 23:04:47 -05002150 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
Al Viroe0a01242011-06-27 17:00:37 -04002151 err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
2152 if (!err)
2153 *path = nd.path;
2154 return err;
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002155}
2156
James Morris057f6c02007-04-26 00:12:05 -07002157/*
2158 * Restricted form of lookup. Doesn't follow links, single-component only,
2159 * needs parent already locked. Doesn't follow mounts.
2160 * SMP-safe.
2161 */
Adrian Bunka244e162006-03-31 02:32:11 -08002162static struct dentry *lookup_hash(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163{
Al Viro292d3af2012-06-10 17:17:17 -04002164 return __lookup_hash(&nd->last, nd->path.dentry, nd->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165}
2166
Christoph Hellwigeead1912007-10-16 23:25:38 -07002167/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07002168 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07002169 * @name: pathname component to lookup
Daniel Rosenberg17285112016-10-26 16:27:45 -07002170 * @mnt: mount we are looking up on
Christoph Hellwigeead1912007-10-16 23:25:38 -07002171 * @base: base directory to lookup from
2172 * @len: maximum length @len should be interpreted to
2173 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07002174 * Note that this routine is purely a helper for filesystem usage and should
2175 * not be called by generic code. Also note that by using this function the
Christoph Hellwigeead1912007-10-16 23:25:38 -07002176 * nameidata argument is passed to the filesystem methods and a filesystem
2177 * using this helper needs to be prepared for that.
2178 */
Daniel Rosenberg17285112016-10-26 16:27:45 -07002179struct dentry *lookup_one_len2(const char *name, struct vfsmount *mnt, struct dentry *base, int len)
James Morris057f6c02007-04-26 00:12:05 -07002180{
James Morris057f6c02007-04-26 00:12:05 -07002181 struct qstr this;
Al Viro6a96ba52011-03-07 23:49:20 -05002182 unsigned int c;
Miklos Szeredicda309d2012-03-26 12:54:21 +02002183 int err;
James Morris057f6c02007-04-26 00:12:05 -07002184
David Woodhouse2f9092e2009-04-20 23:18:37 +01002185 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
2186
Al Viro6a96ba52011-03-07 23:49:20 -05002187 this.name = name;
2188 this.len = len;
Linus Torvalds0145acc2012-03-02 14:32:59 -08002189 this.hash = full_name_hash(name, len);
Al Viro6a96ba52011-03-07 23:49:20 -05002190 if (!len)
2191 return ERR_PTR(-EACCES);
2192
Al Viro04618fb2012-11-29 22:17:21 -05002193 if (unlikely(name[0] == '.')) {
2194 if (len < 2 || (len == 2 && name[1] == '.'))
2195 return ERR_PTR(-EACCES);
2196 }
2197
Al Viro6a96ba52011-03-07 23:49:20 -05002198 while (len--) {
2199 c = *(const unsigned char *)name++;
2200 if (c == '/' || c == '\0')
2201 return ERR_PTR(-EACCES);
Al Viro6a96ba52011-03-07 23:49:20 -05002202 }
Al Viro5a202bc2011-03-08 14:17:44 -05002203 /*
2204 * See if the low-level filesystem might want
2205 * to use its own hash..
2206 */
2207 if (base->d_flags & DCACHE_OP_HASH) {
2208 int err = base->d_op->d_hash(base, base->d_inode, &this);
2209 if (err < 0)
2210 return ERR_PTR(err);
2211 }
Christoph Hellwigeead1912007-10-16 23:25:38 -07002212
Daniel Rosenberg17285112016-10-26 16:27:45 -07002213 err = inode_permission2(mnt, base->d_inode, MAY_EXEC);
Miklos Szeredicda309d2012-03-26 12:54:21 +02002214 if (err)
2215 return ERR_PTR(err);
2216
Al Viro292d3af2012-06-10 17:17:17 -04002217 return __lookup_hash(&this, base, 0);
James Morris057f6c02007-04-26 00:12:05 -07002218}
Daniel Rosenberg17285112016-10-26 16:27:45 -07002219EXPORT_SYMBOL(lookup_one_len2);
2220
2221struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2222{
2223 return lookup_one_len2(name, NULL, base, len);
2224}
2225EXPORT_SYMBOL(lookup_one_len);
James Morris057f6c02007-04-26 00:12:05 -07002226
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002227int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2228 struct path *path, int *empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229{
Al Viro2d8f3032008-07-22 09:59:21 -04002230 struct nameidata nd;
Jeff Layton9cee5ca2012-10-10 15:25:28 -04002231 struct filename *tmp = getname_flags(name, flags, empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 int err = PTR_ERR(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 if (!IS_ERR(tmp)) {
Al Viro2d8f3032008-07-22 09:59:21 -04002234
2235 BUG_ON(flags & LOOKUP_PARENT);
2236
Jeff Layton3a9b94c2012-10-10 15:25:29 -04002237 err = filename_lookup(dfd, tmp, flags, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 putname(tmp);
Al Viro2d8f3032008-07-22 09:59:21 -04002239 if (!err)
2240 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 }
2242 return err;
2243}
2244
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002245int user_path_at(int dfd, const char __user *name, unsigned flags,
2246 struct path *path)
2247{
Linus Torvaldsf7493e52012-03-22 16:10:40 -07002248 return user_path_at_empty(dfd, name, flags, path, NULL);
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002249}
2250
Jeff Layton3a9b94c2012-10-10 15:25:29 -04002251/*
2252 * NB: most callers don't do anything directly with the reference to the
2253 * to struct filename, but the nd->last pointer points into the name string
2254 * allocated by getname. So we must hold the reference to it until all
2255 * path-walking is complete.
2256 */
Jeff Layton9cee5ca2012-10-10 15:25:28 -04002257static struct filename *
2258user_path_parent(int dfd, const char __user *path, struct nameidata *nd)
Al Viro2ad94ae2008-07-21 09:32:51 -04002259{
Jeff Layton9cee5ca2012-10-10 15:25:28 -04002260 struct filename *s = getname(path);
Al Viro2ad94ae2008-07-21 09:32:51 -04002261 int error;
2262
2263 if (IS_ERR(s))
Jeff Layton9cee5ca2012-10-10 15:25:28 -04002264 return s;
Al Viro2ad94ae2008-07-21 09:32:51 -04002265
Jeff Layton3a9b94c2012-10-10 15:25:29 -04002266 error = filename_lookup(dfd, s, LOOKUP_PARENT, nd);
Jeff Layton9cee5ca2012-10-10 15:25:28 -04002267 if (error) {
Al Viro2ad94ae2008-07-21 09:32:51 -04002268 putname(s);
Jeff Layton9cee5ca2012-10-10 15:25:28 -04002269 return ERR_PTR(error);
2270 }
Al Viro2ad94ae2008-07-21 09:32:51 -04002271
Jeff Layton9cee5ca2012-10-10 15:25:28 -04002272 return s;
Al Viro2ad94ae2008-07-21 09:32:51 -04002273}
2274
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275/*
2276 * It's inline, so penalty for filesystems that don't use sticky bit is
2277 * minimal.
2278 */
2279static inline int check_sticky(struct inode *dir, struct inode *inode)
2280{
David Howellsda9592e2008-11-14 10:39:05 +11002281 uid_t fsuid = current_fsuid();
2282
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 if (!(dir->i_mode & S_ISVTX))
2284 return 0;
David Howellsda9592e2008-11-14 10:39:05 +11002285 if (inode->i_uid == fsuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 return 0;
David Howellsda9592e2008-11-14 10:39:05 +11002287 if (dir->i_uid == fsuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 return 0;
Eric W. Biedermand6814e262011-11-14 16:24:06 -08002289 return !inode_capable(inode, CAP_FOWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290}
2291
2292/*
2293 * Check whether we can remove a link victim from directory dir, check
2294 * whether the type of victim is right.
2295 * 1. We can't do it if dir is read-only (done in permission())
2296 * 2. We should have write and exec permissions on dir
2297 * 3. We can't remove anything from append-only dir
2298 * 4. We can't do anything with immutable dir (done in permission())
2299 * 5. If the sticky bit on dir is set we should either
2300 * a. be owner of dir, or
2301 * b. be owner of victim, or
2302 * c. have CAP_FOWNER capability
2303 * 6. If the victim is append-only or immutable we can't do antyhing with
2304 * links pointing to it.
2305 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2306 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2307 * 9. We can't remove a root or mountpoint.
2308 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
2309 * nfs_async_unlink().
2310 */
Daniel Rosenberg17285112016-10-26 16:27:45 -07002311static int may_delete(struct vfsmount *mnt, struct inode *dir,struct dentry *victim,int isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312{
2313 int error;
2314
2315 if (!victim->d_inode)
2316 return -ENOENT;
2317
2318 BUG_ON(victim->d_parent->d_inode != dir);
Al Virocccc6bb2009-12-25 05:07:33 -05002319 audit_inode_child(victim, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Daniel Rosenberg17285112016-10-26 16:27:45 -07002321 error = inode_permission2(mnt, dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 if (error)
2323 return error;
2324 if (IS_APPEND(dir))
2325 return -EPERM;
2326 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
Hugh Dickinsf9454542008-11-19 15:36:38 -08002327 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 return -EPERM;
2329 if (isdir) {
2330 if (!S_ISDIR(victim->d_inode->i_mode))
2331 return -ENOTDIR;
2332 if (IS_ROOT(victim))
2333 return -EBUSY;
2334 } else if (S_ISDIR(victim->d_inode->i_mode))
2335 return -EISDIR;
2336 if (IS_DEADDIR(dir))
2337 return -ENOENT;
2338 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2339 return -EBUSY;
2340 return 0;
2341}
2342
2343/* Check whether we can create an object with dentry child in directory
2344 * dir.
2345 * 1. We can't do it if child already exists (open has special treatment for
2346 * this case, but since we are inlined it's OK)
2347 * 2. We can't do it if dir is read-only (done in permission())
2348 * 3. We should have write and exec permissions on dir
2349 * 4. We can't do it if dir is immutable (done in permission())
2350 */
Daniel Rosenberg17285112016-10-26 16:27:45 -07002351static inline int may_create(struct vfsmount *mnt, struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352{
2353 if (child->d_inode)
2354 return -EEXIST;
2355 if (IS_DEADDIR(dir))
2356 return -ENOENT;
Daniel Rosenberg17285112016-10-26 16:27:45 -07002357 return inode_permission2(mnt, dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358}
2359
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360/*
2361 * p1 and p2 should be directories on the same fs.
2362 */
2363struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2364{
2365 struct dentry *p;
2366
2367 if (p1 == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07002368 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 return NULL;
2370 }
2371
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002372 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002374 p = d_ancestor(p2, p1);
2375 if (p) {
2376 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2377 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
2378 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 }
2380
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002381 p = d_ancestor(p1, p2);
2382 if (p) {
2383 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2384 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2385 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 }
2387
Ingo Molnarf2eace22006-07-03 00:25:05 -07002388 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2389 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 return NULL;
2391}
2392
2393void unlock_rename(struct dentry *p1, struct dentry *p2)
2394{
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002395 mutex_unlock(&p1->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 if (p1 != p2) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002397 mutex_unlock(&p2->d_inode->i_mutex);
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002398 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 }
2400}
2401
Daniel Rosenberg17285112016-10-26 16:27:45 -07002402int vfs_create2(struct vfsmount *mnt, struct inode *dir, struct dentry *dentry,
Al Viro8da0eaf2012-06-10 18:09:36 -04002403 umode_t mode, bool want_excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404{
Daniel Rosenberg17285112016-10-26 16:27:45 -07002405 int error = may_create(mnt, dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 if (error)
2407 return error;
2408
Al Viroacfa4382008-12-04 10:06:33 -05002409 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 return -EACCES; /* shouldn't it be ENOSYS? */
2411 mode &= S_IALLUGO;
2412 mode |= S_IFREG;
2413 error = security_inode_create(dir, dentry, mode);
2414 if (error)
2415 return error;
Al Viro8da0eaf2012-06-10 18:09:36 -04002416 error = dir->i_op->create(dir, dentry, mode, want_excl);
Amir Samuelov6a22e462014-05-26 11:44:06 +03002417 if (error)
2418 return error;
2419
2420 error = security_inode_post_create(dir, dentry, mode);
2421 if (error)
2422 return error;
2423
Stephen Smalleya74574a2005-09-09 13:01:44 -07002424 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002425 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 return error;
2427}
Daniel Rosenberg17285112016-10-26 16:27:45 -07002428EXPORT_SYMBOL(vfs_create2);
2429
2430int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viro8da0eaf2012-06-10 18:09:36 -04002431 bool want_excl)
Daniel Rosenberg17285112016-10-26 16:27:45 -07002432{
Al Viro8da0eaf2012-06-10 18:09:36 -04002433 return vfs_create2(NULL, dir, dentry, mode, want_excl);
Daniel Rosenberg17285112016-10-26 16:27:45 -07002434}
2435EXPORT_SYMBOL(vfs_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
Al Viro73d049a2011-03-11 12:08:24 -05002437static int may_open(struct path *path, int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002439 struct dentry *dentry = path->dentry;
Daniel Rosenberg17285112016-10-26 16:27:45 -07002440 struct vfsmount *mnt = path->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 struct inode *inode = dentry->d_inode;
2442 int error;
2443
Al Virobcda7652011-03-13 16:42:14 -04002444 /* O_PATH? */
2445 if (!acc_mode)
2446 return 0;
2447
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 if (!inode)
2449 return -ENOENT;
2450
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002451 switch (inode->i_mode & S_IFMT) {
2452 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002454 case S_IFDIR:
2455 if (acc_mode & MAY_WRITE)
2456 return -EISDIR;
2457 break;
2458 case S_IFBLK:
2459 case S_IFCHR:
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002460 if (path->mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002462 /*FALLTHRU*/
2463 case S_IFIFO:
2464 case S_IFSOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002466 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08002467 }
Dave Hansenb41572e2007-10-16 23:31:14 -07002468
Daniel Rosenberg17285112016-10-26 16:27:45 -07002469 error = inode_permission2(mnt, inode, acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07002470 if (error)
2471 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05002472
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 /*
2474 * An append-only file must be opened in append mode for writing.
2475 */
2476 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05002477 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05002478 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05002480 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 }
2482
2483 /* O_NOATIME can only be set by the owner or superuser */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07002484 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
Al Viro7715b522009-12-16 03:54:00 -05002485 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
J. Bruce Fieldsf3c7691e2011-09-21 10:58:13 -04002487 return 0;
Al Viro7715b522009-12-16 03:54:00 -05002488}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
Jeff Laytone1181ee2010-12-07 16:19:50 -05002490static int handle_truncate(struct file *filp)
Al Viro7715b522009-12-16 03:54:00 -05002491{
Jeff Laytone1181ee2010-12-07 16:19:50 -05002492 struct path *path = &filp->f_path;
Al Viro7715b522009-12-16 03:54:00 -05002493 struct inode *inode = path->dentry->d_inode;
2494 int error = get_write_access(inode);
2495 if (error)
2496 return error;
2497 /*
2498 * Refuse to truncate files with mandatory locks held on them.
2499 */
2500 error = locks_verify_locked(inode);
2501 if (!error)
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +09002502 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05002503 if (!error) {
Daniel Rosenberga88a18f2016-12-27 16:36:40 -08002504 error = do_truncate2(path->mnt, path->dentry, 0,
Al Viro7715b522009-12-16 03:54:00 -05002505 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
Jeff Laytone1181ee2010-12-07 16:19:50 -05002506 filp);
Al Viro7715b522009-12-16 03:54:00 -05002507 }
2508 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04002509 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510}
2511
Dave Hansend57999e2008-02-15 14:37:27 -08002512static inline int open_to_namei_flags(int flag)
2513{
Al Viro8a5e9292011-06-25 19:15:54 -04002514 if ((flag & O_ACCMODE) == 3)
2515 flag--;
Dave Hansend57999e2008-02-15 14:37:27 -08002516 return flag;
2517}
2518
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002519static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode)
2520{
2521 int error = security_path_mknod(dir, dentry, mode, 0);
2522 if (error)
2523 return error;
2524
2525 error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
2526 if (error)
2527 return error;
2528
2529 return security_inode_create(dir->dentry->d_inode, dentry, mode);
2530}
2531
David Howells9898cc82012-06-14 16:13:46 +01002532/*
2533 * Attempt to atomically look up, create and open a file from a negative
2534 * dentry.
2535 *
2536 * Returns 0 if successful. The file will have been created and attached to
2537 * @file by the filesystem calling finish_open().
2538 *
2539 * Returns 1 if the file was looked up only or didn't need creating. The
2540 * caller will need to perform the open themselves. @path will have been
2541 * updated to point to the new dentry. This may be negative.
2542 *
2543 * Returns an error code otherwise.
2544 */
Al Viro70806c32012-06-22 12:41:10 +04002545static int atomic_open(struct nameidata *nd, struct dentry *dentry,
2546 struct path *path, struct file *file,
2547 const struct open_flags *op,
Al Viro8c162b92012-07-31 00:53:35 +04002548 bool got_write, bool need_lookup,
Al Viro70806c32012-06-22 12:41:10 +04002549 int *opened)
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002550{
2551 struct inode *dir = nd->path.dentry->d_inode;
2552 unsigned open_flag = open_to_namei_flags(op->open_flag);
2553 umode_t mode;
2554 int error;
2555 int acc_mode;
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002556 int create_error = 0;
2557 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
2558
2559 BUG_ON(dentry->d_inode);
2560
2561 /* Don't create child dentry for a dead directory. */
2562 if (unlikely(IS_DEADDIR(dir))) {
Al Viro70806c32012-06-22 12:41:10 +04002563 error = -ENOENT;
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002564 goto out;
2565 }
2566
Miklos Szeredi99fdd3a2012-08-15 13:01:24 +02002567 mode = op->mode;
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002568 if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
2569 mode &= ~current_umask();
2570
Al Viro5b2d9ff2012-07-30 11:50:30 +04002571 if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT)) {
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002572 open_flag &= ~O_TRUNC;
Al Viro8915ef32012-06-10 05:01:45 -04002573 *opened |= FILE_CREATED;
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002574 }
2575
2576 /*
2577 * Checking write permission is tricky, bacuse we don't know if we are
2578 * going to actually need it: O_CREAT opens should work as long as the
2579 * file exists. But checking existence breaks atomicity. The trick is
2580 * to check access and if not granted clear O_CREAT from the flags.
2581 *
2582 * Another problem is returing the "right" error value (e.g. for an
2583 * O_EXCL open we want to return EEXIST not EROFS).
2584 */
Al Viro8c162b92012-07-31 00:53:35 +04002585 if (((open_flag & (O_CREAT | O_TRUNC)) ||
2586 (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) {
2587 if (!(open_flag & O_CREAT)) {
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002588 /*
2589 * No O_CREATE -> atomicity not a requirement -> fall
2590 * back to lookup + open
2591 */
2592 goto no_open;
2593 } else if (open_flag & (O_EXCL | O_TRUNC)) {
2594 /* Fall back and fail with the right error */
Al Viro8c162b92012-07-31 00:53:35 +04002595 create_error = -EROFS;
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002596 goto no_open;
2597 } else {
2598 /* No side effects, safe to clear O_CREAT */
Al Viro8c162b92012-07-31 00:53:35 +04002599 create_error = -EROFS;
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002600 open_flag &= ~O_CREAT;
2601 }
2602 }
2603
2604 if (open_flag & O_CREAT) {
Miklos Szeredi63bab5b2012-08-15 13:01:24 +02002605 error = may_o_create(&nd->path, dentry, mode);
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002606 if (error) {
2607 create_error = error;
2608 if (open_flag & O_EXCL)
2609 goto no_open;
2610 open_flag &= ~O_CREAT;
2611 }
2612 }
2613
2614 if (nd->flags & LOOKUP_DIRECTORY)
2615 open_flag |= O_DIRECTORY;
2616
Al Virob2ebdc52012-06-22 12:40:19 +04002617 file->f_path.dentry = DENTRY_NOT_SET;
2618 file->f_path.mnt = nd->path.mnt;
2619 error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode,
Al Viro8915ef32012-06-10 05:01:45 -04002620 opened);
Al Viro50ed02d2012-06-22 12:39:14 +04002621 if (error < 0) {
Al Viro50ed02d2012-06-22 12:39:14 +04002622 if (create_error && error == -ENOENT)
2623 error = create_error;
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002624 goto out;
2625 }
2626
2627 acc_mode = op->acc_mode;
Al Viro8915ef32012-06-10 05:01:45 -04002628 if (*opened & FILE_CREATED) {
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002629 fsnotify_create(dir, dentry);
2630 acc_mode = MAY_OPEN;
2631 }
2632
Al Viro50ed02d2012-06-22 12:39:14 +04002633 if (error) { /* returned 1, that is */
Al Virob2ebdc52012-06-22 12:40:19 +04002634 if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
Al Viro70806c32012-06-22 12:41:10 +04002635 error = -EIO;
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002636 goto out;
2637 }
Al Virob2ebdc52012-06-22 12:40:19 +04002638 if (file->f_path.dentry) {
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002639 dput(dentry);
Al Virob2ebdc52012-06-22 12:40:19 +04002640 dentry = file->f_path.dentry;
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002641 }
Sage Weilb5e96de2012-08-15 13:30:12 -07002642 if (create_error && dentry->d_inode == NULL) {
2643 error = create_error;
2644 goto out;
2645 }
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002646 goto looked_up;
2647 }
2648
2649 /*
2650 * We didn't have the inode before the open, so check open permission
2651 * here.
2652 */
Al Viro70806c32012-06-22 12:41:10 +04002653 error = may_open(&file->f_path, acc_mode, open_flag);
2654 if (error)
2655 fput(file);
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002656
2657out:
2658 dput(dentry);
Al Viro70806c32012-06-22 12:41:10 +04002659 return error;
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002660
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002661no_open:
2662 if (need_lookup) {
Al Viro292d3af2012-06-10 17:17:17 -04002663 dentry = lookup_real(dir, dentry, nd->flags);
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002664 if (IS_ERR(dentry))
Al Viro70806c32012-06-22 12:41:10 +04002665 return PTR_ERR(dentry);
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002666
2667 if (create_error) {
2668 int open_flag = op->open_flag;
2669
Al Viro70806c32012-06-22 12:41:10 +04002670 error = create_error;
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002671 if ((open_flag & O_EXCL)) {
2672 if (!dentry->d_inode)
2673 goto out;
2674 } else if (!dentry->d_inode) {
2675 goto out;
2676 } else if ((open_flag & O_TRUNC) &&
2677 S_ISREG(dentry->d_inode->i_mode)) {
2678 goto out;
2679 }
2680 /* will fail later, go on to get the right error */
2681 }
2682 }
2683looked_up:
2684 path->dentry = dentry;
2685 path->mnt = nd->path.mnt;
Al Viro70806c32012-06-22 12:41:10 +04002686 return 1;
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002687}
2688
Nick Piggin31e6b012011-01-07 17:49:52 +11002689/*
David Howells9898cc82012-06-14 16:13:46 +01002690 * Look up and maybe create and open the last component.
Miklos Szeredi8632d4d2012-06-05 15:10:15 +02002691 *
2692 * Must be called with i_mutex held on parent.
2693 *
David Howells9898cc82012-06-14 16:13:46 +01002694 * Returns 0 if the file was successfully atomically created (if necessary) and
2695 * opened. In this case the file will be returned attached to @file.
2696 *
2697 * Returns 1 if the file was not completely opened at this time, though lookups
2698 * and creations will have been performed and the dentry returned in @path will
2699 * be positive upon return if O_CREAT was specified. If O_CREAT wasn't
2700 * specified then a negative dentry may be returned.
2701 *
2702 * An error code is returned otherwise.
2703 *
2704 * FILE_CREATE will be set in @*opened if the dentry was created and will be
2705 * cleared otherwise prior to returning.
Miklos Szeredi8632d4d2012-06-05 15:10:15 +02002706 */
Al Viro70806c32012-06-22 12:41:10 +04002707static int lookup_open(struct nameidata *nd, struct path *path,
2708 struct file *file,
2709 const struct open_flags *op,
Al Viro8c162b92012-07-31 00:53:35 +04002710 bool got_write, int *opened)
Miklos Szeredi8632d4d2012-06-05 15:10:15 +02002711{
2712 struct dentry *dir = nd->path.dentry;
Miklos Szeredi10606642012-06-05 15:10:16 +02002713 struct inode *dir_inode = dir->d_inode;
Miklos Szeredi8632d4d2012-06-05 15:10:15 +02002714 struct vfsmount *mnt = nd->path.mnt;
2715 struct dentry *dentry;
2716 int error;
Miklos Szeredi10606642012-06-05 15:10:16 +02002717 bool need_lookup;
Miklos Szeredi8632d4d2012-06-05 15:10:15 +02002718
Al Viro8915ef32012-06-10 05:01:45 -04002719 *opened &= ~FILE_CREATED;
Al Viro61b61da2012-06-22 12:42:10 +04002720 dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup);
Miklos Szeredi8632d4d2012-06-05 15:10:15 +02002721 if (IS_ERR(dentry))
Al Viro70806c32012-06-22 12:41:10 +04002722 return PTR_ERR(dentry);
Miklos Szeredi8632d4d2012-06-05 15:10:15 +02002723
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002724 /* Cached positive dentry: will open in f_op->open */
2725 if (!need_lookup && dentry->d_inode)
2726 goto out_no_open;
2727
2728 if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) {
Al Viro8c162b92012-07-31 00:53:35 +04002729 return atomic_open(nd, dentry, path, file, op, got_write,
Al Viro8915ef32012-06-10 05:01:45 -04002730 need_lookup, opened);
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002731 }
2732
Miklos Szeredi10606642012-06-05 15:10:16 +02002733 if (need_lookup) {
2734 BUG_ON(dentry->d_inode);
2735
Al Viro292d3af2012-06-10 17:17:17 -04002736 dentry = lookup_real(dir_inode, dentry, nd->flags);
Miklos Szeredi10606642012-06-05 15:10:16 +02002737 if (IS_ERR(dentry))
Al Viro70806c32012-06-22 12:41:10 +04002738 return PTR_ERR(dentry);
Miklos Szeredi10606642012-06-05 15:10:16 +02002739 }
2740
Miklos Szeredi8632d4d2012-06-05 15:10:15 +02002741 /* Negative dentry, just create the file */
2742 if (!dentry->d_inode && (op->open_flag & O_CREAT)) {
2743 umode_t mode = op->mode;
2744 if (!IS_POSIXACL(dir->d_inode))
2745 mode &= ~current_umask();
2746 /*
2747 * This write is needed to ensure that a
2748 * rw->ro transition does not occur between
2749 * the time when the file is created and when
2750 * a permanent write count is taken through
Miklos Szeredi2f80ad32012-06-05 15:10:27 +02002751 * the 'struct file' in finish_open().
Miklos Szeredi8632d4d2012-06-05 15:10:15 +02002752 */
Al Viro8c162b92012-07-31 00:53:35 +04002753 if (!got_write) {
2754 error = -EROFS;
Miklos Szeredi8632d4d2012-06-05 15:10:15 +02002755 goto out_dput;
Al Viro8c162b92012-07-31 00:53:35 +04002756 }
Al Viro8915ef32012-06-10 05:01:45 -04002757 *opened |= FILE_CREATED;
Miklos Szeredi8632d4d2012-06-05 15:10:15 +02002758 error = security_path_mknod(&nd->path, dentry, mode, 0);
2759 if (error)
2760 goto out_dput;
Al Viro8da0eaf2012-06-10 18:09:36 -04002761 error = vfs_create2(mnt, dir->d_inode, dentry, mode,
2762 nd->flags & LOOKUP_EXCL);
Miklos Szeredi8632d4d2012-06-05 15:10:15 +02002763 if (error)
2764 goto out_dput;
2765 }
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002766out_no_open:
Miklos Szeredi8632d4d2012-06-05 15:10:15 +02002767 path->dentry = dentry;
2768 path->mnt = nd->path.mnt;
Al Viro70806c32012-06-22 12:41:10 +04002769 return 1;
Miklos Szeredi8632d4d2012-06-05 15:10:15 +02002770
2771out_dput:
2772 dput(dentry);
Al Viro70806c32012-06-22 12:41:10 +04002773 return error;
Miklos Szeredi8632d4d2012-06-05 15:10:15 +02002774}
2775
Nick Piggin31e6b012011-01-07 17:49:52 +11002776/*
Al Virofe2d35f2011-03-05 22:58:25 -05002777 * Handle the last step of open()
Nick Piggin31e6b012011-01-07 17:49:52 +11002778 */
Al Viro70806c32012-06-22 12:41:10 +04002779static int do_last(struct nameidata *nd, struct path *path,
2780 struct file *file, const struct open_flags *op,
Jeff Layton631423f2012-10-10 16:43:10 -04002781 int *opened, struct filename *name)
Al Virofb1cc552009-12-24 01:58:28 -05002782{
Al Viroa1e28032009-12-24 02:12:06 -05002783 struct dentry *dir = nd->path.dentry;
Al Viro8a14c342020-02-01 16:26:45 +00002784 kuid_t dir_uid = nd->inode->i_uid;
2785 umode_t dir_mode = nd->inode->i_mode;
Al Viroca344a892011-03-09 00:36:45 -05002786 int open_flag = op->open_flag;
Miklos Szeredid6a4ce32012-06-05 15:10:30 +02002787 bool will_truncate = (open_flag & O_TRUNC) != 0;
Al Viro8c162b92012-07-31 00:53:35 +04002788 bool got_write = false;
Al Virobcda7652011-03-13 16:42:14 -04002789 int acc_mode = op->acc_mode;
Miklos Szeredia5f84da2012-05-21 17:30:07 +02002790 struct inode *inode;
Miklos Szeredid6a4ce32012-06-05 15:10:30 +02002791 bool symlink_ok = false;
Miklos Szerediefbfa632012-05-21 17:30:19 +02002792 struct path save_parent = { .dentry = NULL, .mnt = NULL };
2793 bool retried = false;
Al Viro16c2cd72011-02-22 15:50:10 -05002794 int error;
Jeff Layton631423f2012-10-10 16:43:10 -04002795 const char *pathname = name->name;
Al Virofb1cc552009-12-24 01:58:28 -05002796
Al Viroc3e380b2011-02-23 13:39:45 -05002797 nd->flags &= ~LOOKUP_PARENT;
2798 nd->flags |= op->intent;
2799
Al Virof9103902013-06-06 09:12:33 -04002800 if (nd->last_type != LAST_NORM) {
Al Virofe2d35f2011-03-05 22:58:25 -05002801 error = handle_dots(nd, nd->last_type);
2802 if (error)
Al Viro70806c32012-06-22 12:41:10 +04002803 return error;
Miklos Szeredi8d49e6e2012-06-05 15:10:29 +02002804 goto finish_open;
Al Viro1f36f772009-12-26 10:56:19 -05002805 }
Al Viro67ee3ad2009-12-26 07:01:01 -05002806
Al Viroca344a892011-03-09 00:36:45 -05002807 if (!(open_flag & O_CREAT)) {
Al Virofe2d35f2011-03-05 22:58:25 -05002808 if (nd->last.name[nd->last.len])
2809 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
Al Virobcda7652011-03-13 16:42:14 -04002810 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
Miklos Szeredid6a4ce32012-06-05 15:10:30 +02002811 symlink_ok = true;
Al Virofe2d35f2011-03-05 22:58:25 -05002812 /* we _can_ be in RCU mode here */
Miklos Szeredia5f84da2012-05-21 17:30:07 +02002813 error = lookup_fast(nd, &nd->last, path, &inode);
Miklos Szeredia2ab3642012-06-05 15:10:14 +02002814 if (likely(!error))
2815 goto finish_lookup;
Miklos Szeredia5f84da2012-05-21 17:30:07 +02002816
Al Viroce57dfc2011-03-13 19:58:58 -04002817 if (error < 0)
Al Viro70806c32012-06-22 12:41:10 +04002818 goto out;
Miklos Szeredi513072f2012-06-05 15:10:12 +02002819
Miklos Szeredia2ab3642012-06-05 15:10:14 +02002820 BUG_ON(nd->inode != dir->d_inode);
Miklos Szeredi441a4582012-06-05 15:10:13 +02002821 } else {
2822 /* create side of things */
2823 /*
2824 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
2825 * has been cleared when we got to the last component we are
2826 * about to look up
2827 */
Al Viro9f1fafe2011-03-25 11:00:12 -04002828 error = complete_walk(nd);
2829 if (error)
Al Viro70806c32012-06-22 12:41:10 +04002830 return error;
Al Virofe2d35f2011-03-05 22:58:25 -05002831
Miklos Szeredi441a4582012-06-05 15:10:13 +02002832 audit_inode(pathname, dir);
2833 error = -EISDIR;
2834 /* trailing slashes? */
2835 if (nd->last.name[nd->last.len])
Al Viro70806c32012-06-22 12:41:10 +04002836 goto out;
Al Virofe2d35f2011-03-05 22:58:25 -05002837 }
2838
Miklos Szerediefbfa632012-05-21 17:30:19 +02002839retry_lookup:
Al Viro8c162b92012-07-31 00:53:35 +04002840 if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
Al Virofb1cc552009-12-24 01:58:28 -05002841 error = mnt_want_write(nd->path.mnt);
Al Viro8c162b92012-07-31 00:53:35 +04002842 if (!error)
2843 got_write = true;
2844 /*
2845 * do _not_ fail yet - we might not need that or fail with
2846 * a different error; let lookup_open() decide; we'll be
2847 * dropping this one anyway.
2848 */
2849 }
Al Virofb1cc552009-12-24 01:58:28 -05002850 mutex_lock(&dir->d_inode->i_mutex);
Al Viro8c162b92012-07-31 00:53:35 +04002851 error = lookup_open(nd, path, file, op, got_write, opened);
Miklos Szeredi8632d4d2012-06-05 15:10:15 +02002852 mutex_unlock(&dir->d_inode->i_mutex);
Al Virofb1cc552009-12-24 01:58:28 -05002853
Al Viro70806c32012-06-22 12:41:10 +04002854 if (error <= 0) {
Al Virofb1cc552009-12-24 01:58:28 -05002855 if (error)
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002856 goto out;
2857
Al Viro8915ef32012-06-10 05:01:45 -04002858 if ((*opened & FILE_CREATED) ||
Al Viro70806c32012-06-22 12:41:10 +04002859 !S_ISREG(file->f_path.dentry->d_inode->i_mode))
Miklos Szeredid6a4ce32012-06-05 15:10:30 +02002860 will_truncate = false;
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002861
Al Viro70806c32012-06-22 12:41:10 +04002862 audit_inode(pathname, file->f_path.dentry);
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002863 goto opened;
2864 }
Al Virofb1cc552009-12-24 01:58:28 -05002865
Al Viro8915ef32012-06-10 05:01:45 -04002866 if (*opened & FILE_CREATED) {
Al Viro9b44f1b2011-03-09 00:17:27 -05002867 /* Don't check for write permission, don't truncate */
Al Viroca344a892011-03-09 00:36:45 -05002868 open_flag &= ~O_TRUNC;
Miklos Szeredid6a4ce32012-06-05 15:10:30 +02002869 will_truncate = false;
Al Virobcda7652011-03-13 16:42:14 -04002870 acc_mode = MAY_OPEN;
Miklos Szeredi8632d4d2012-06-05 15:10:15 +02002871 path_to_nameidata(path, nd);
Miklos Szeredi8d49e6e2012-06-05 15:10:29 +02002872 goto finish_open_created;
Al Virofb1cc552009-12-24 01:58:28 -05002873 }
2874
2875 /*
Jeff Layton968c5ab2012-07-25 10:19:47 -04002876 * create/update audit record if it already exists.
Al Virofb1cc552009-12-24 01:58:28 -05002877 */
Jeff Layton968c5ab2012-07-25 10:19:47 -04002878 if (path->dentry->d_inode)
2879 audit_inode(pathname, path->dentry);
Al Virofb1cc552009-12-24 01:58:28 -05002880
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002881 /*
2882 * If atomic_open() acquired write access it is dropped now due to
2883 * possible mount and symlink following (this might be optimized away if
2884 * necessary...)
2885 */
Al Viro8c162b92012-07-31 00:53:35 +04002886 if (got_write) {
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002887 mnt_drop_write(nd->path.mnt);
Al Viro8c162b92012-07-31 00:53:35 +04002888 got_write = false;
Miklos Szeredi48b105f2012-06-05 15:10:17 +02002889 }
Al Virofb1cc552009-12-24 01:58:28 -05002890
2891 error = -EEXIST;
Al Viro5b2d9ff2012-07-30 11:50:30 +04002892 if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))
Al Virofb1cc552009-12-24 01:58:28 -05002893 goto exit_dput;
2894
David Howells9875cf82011-01-14 18:45:21 +00002895 error = follow_managed(path, nd->flags);
2896 if (error < 0)
2897 goto exit_dput;
Al Virofb1cc552009-12-24 01:58:28 -05002898
Al Viroa3fbbde2011-11-07 21:21:26 +00002899 if (error)
2900 nd->flags |= LOOKUP_JUMPED;
2901
Miklos Szeredi5c7f4512012-05-21 17:30:08 +02002902 BUG_ON(nd->flags & LOOKUP_RCU);
2903 inode = path->dentry->d_inode;
Miklos Szeredi99ec9802012-05-21 17:30:14 +02002904finish_lookup:
2905 /* we _can_ be in RCU mode here */
Al Virofb1cc552009-12-24 01:58:28 -05002906 error = -ENOENT;
Miklos Szeredi16785062012-05-21 17:30:10 +02002907 if (!inode) {
2908 path_to_nameidata(path, nd);
Al Viro70806c32012-06-22 12:41:10 +04002909 goto out;
Miklos Szeredi16785062012-05-21 17:30:10 +02002910 }
Al Viro9e67f362009-12-26 07:04:50 -05002911
Miklos Szeredi458abf22012-05-21 17:30:09 +02002912 if (should_follow_link(inode, !symlink_ok)) {
2913 if (nd->flags & LOOKUP_RCU) {
2914 if (unlikely(unlazy_walk(nd, path->dentry))) {
2915 error = -ECHILD;
Al Viro70806c32012-06-22 12:41:10 +04002916 goto out;
Miklos Szeredi458abf22012-05-21 17:30:09 +02002917 }
2918 }
2919 BUG_ON(inode != path->dentry->d_inode);
Al Viro70806c32012-06-22 12:41:10 +04002920 return 1;
Miklos Szeredi458abf22012-05-21 17:30:09 +02002921 }
Al Virofb1cc552009-12-24 01:58:28 -05002922
Miklos Szerediefbfa632012-05-21 17:30:19 +02002923 if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) {
2924 path_to_nameidata(path, nd);
2925 } else {
2926 save_parent.dentry = nd->path.dentry;
2927 save_parent.mnt = mntget(path->mnt);
2928 nd->path.dentry = path->dentry;
2929
2930 }
Miklos Szeredi5c7f4512012-05-21 17:30:08 +02002931 nd->inode = inode;
Al Viroa3fbbde2011-11-07 21:21:26 +00002932 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
Al Virof9103902013-06-06 09:12:33 -04002933finish_open:
Al Viroa3fbbde2011-11-07 21:21:26 +00002934 error = complete_walk(nd);
Miklos Szerediefbfa632012-05-21 17:30:19 +02002935 if (error) {
2936 path_put(&save_parent);
Al Viro70806c32012-06-22 12:41:10 +04002937 return error;
Miklos Szerediefbfa632012-05-21 17:30:19 +02002938 }
Al Virof9103902013-06-06 09:12:33 -04002939 audit_inode(pathname, nd->path.dentry);
Salvatore Mesoraca063d55a2018-08-23 17:00:35 -07002940 if (open_flag & O_CREAT) {
2941 error = -EISDIR;
2942 if (S_ISDIR(nd->inode->i_mode))
2943 goto out;
Al Viroa2434112020-01-26 09:29:34 -05002944 error = may_create_in_sticky(dir_mode, dir_uid,
Salvatore Mesoraca063d55a2018-08-23 17:00:35 -07002945 d_backing_inode(nd->path.dentry));
2946 if (unlikely(error))
2947 goto out;
2948 }
Miklos Szerediaba9dce2012-05-21 17:30:11 +02002949 error = -ENOTDIR;
Al Viroa1e12632013-06-06 19:33:47 -04002950 if ((nd->flags & LOOKUP_DIRECTORY) && !can_lookup(nd->inode))
Al Viro70806c32012-06-22 12:41:10 +04002951 goto out;
Al Viro6c0d46c2011-03-09 00:59:59 -05002952 if (!S_ISREG(nd->inode->i_mode))
Miklos Szeredid6a4ce32012-06-05 15:10:30 +02002953 will_truncate = false;
Al Viro6c0d46c2011-03-09 00:59:59 -05002954
Al Viro0f9d1a12011-03-09 00:13:14 -05002955 if (will_truncate) {
2956 error = mnt_want_write(nd->path.mnt);
2957 if (error)
Al Viro70806c32012-06-22 12:41:10 +04002958 goto out;
Al Viro8c162b92012-07-31 00:53:35 +04002959 got_write = true;
Al Viro0f9d1a12011-03-09 00:13:14 -05002960 }
Miklos Szeredi8d49e6e2012-06-05 15:10:29 +02002961finish_open_created:
Al Virobcda7652011-03-13 16:42:14 -04002962 error = may_open(&nd->path, acc_mode, open_flag);
Al Viroca344a892011-03-09 00:36:45 -05002963 if (error)
Al Viro70806c32012-06-22 12:41:10 +04002964 goto out;
Al Virob2ebdc52012-06-22 12:40:19 +04002965 file->f_path.mnt = nd->path.mnt;
2966 error = finish_open(file, nd->path.dentry, NULL, opened);
2967 if (error) {
Al Virob2ebdc52012-06-22 12:40:19 +04002968 if (error == -EOPENSTALE)
Miklos Szeredi36c27522012-06-05 15:10:31 +02002969 goto stale_open;
Miklos Szeredi2f80ad32012-06-05 15:10:27 +02002970 goto out;
Al Viro0f9d1a12011-03-09 00:13:14 -05002971 }
Miklos Szeredi2de99c02012-06-05 15:10:32 +02002972opened:
Al Viro70806c32012-06-22 12:41:10 +04002973 error = open_check_o_direct(file);
Miklos Szeredi2f80ad32012-06-05 15:10:27 +02002974 if (error)
2975 goto exit_fput;
Al Viro70806c32012-06-22 12:41:10 +04002976 error = ima_file_check(file, op->acc_mode);
Miklos Szeredi4233c212012-06-05 15:10:28 +02002977 if (error)
2978 goto exit_fput;
2979
2980 if (will_truncate) {
Al Viro70806c32012-06-22 12:41:10 +04002981 error = handle_truncate(file);
Miklos Szeredi4233c212012-06-05 15:10:28 +02002982 if (error)
2983 goto exit_fput;
Al Viro0f9d1a12011-03-09 00:13:14 -05002984 }
Al Viroca344a892011-03-09 00:36:45 -05002985out:
Al Viro8c162b92012-07-31 00:53:35 +04002986 if (got_write)
Al Viro0f9d1a12011-03-09 00:13:14 -05002987 mnt_drop_write(nd->path.mnt);
Miklos Szerediefbfa632012-05-21 17:30:19 +02002988 path_put(&save_parent);
Miklos Szeredi2aabf6d2012-05-21 17:30:06 +02002989 terminate_walk(nd);
Al Viro70806c32012-06-22 12:41:10 +04002990 return error;
Al Virofb1cc552009-12-24 01:58:28 -05002991
Al Virofb1cc552009-12-24 01:58:28 -05002992exit_dput:
2993 path_put_conditional(path, nd);
Al Viroca344a892011-03-09 00:36:45 -05002994 goto out;
Miklos Szeredi2f80ad32012-06-05 15:10:27 +02002995exit_fput:
Al Viro70806c32012-06-22 12:41:10 +04002996 fput(file);
2997 goto out;
Miklos Szeredi2f80ad32012-06-05 15:10:27 +02002998
Miklos Szeredi36c27522012-06-05 15:10:31 +02002999stale_open:
3000 /* If no saved parent or already retried then can't retry */
3001 if (!save_parent.dentry || retried)
3002 goto out;
3003
3004 BUG_ON(save_parent.dentry != dir);
3005 path_put(&nd->path);
3006 nd->path = save_parent;
3007 nd->inode = dir->d_inode;
3008 save_parent.mnt = NULL;
3009 save_parent.dentry = NULL;
Al Viro8c162b92012-07-31 00:53:35 +04003010 if (got_write) {
Miklos Szeredi36c27522012-06-05 15:10:31 +02003011 mnt_drop_write(nd->path.mnt);
Al Viro8c162b92012-07-31 00:53:35 +04003012 got_write = false;
Miklos Szeredi36c27522012-06-05 15:10:31 +02003013 }
3014 retried = true;
3015 goto retry_lookup;
Al Virofb1cc552009-12-24 01:58:28 -05003016}
3017
Al Virob0d54302013-06-07 01:20:27 -04003018static int do_tmpfile(int dfd, struct filename *pathname,
3019 struct nameidata *nd, int flags,
3020 const struct open_flags *op,
3021 struct file *file, int *opened)
3022{
3023 static const struct qstr name = QSTR_INIT("/", 1);
3024 struct dentry *dentry, *child;
3025 struct inode *dir;
3026 int error = path_lookupat(dfd, pathname->name,
3027 flags | LOOKUP_DIRECTORY, nd);
3028 if (unlikely(error))
3029 return error;
3030 error = mnt_want_write(nd->path.mnt);
3031 if (unlikely(error))
3032 goto out;
3033 /* we want directory to be writable */
3034 error = inode_permission(nd->inode, MAY_WRITE | MAY_EXEC);
3035 if (error)
3036 goto out2;
3037 dentry = nd->path.dentry;
3038 dir = dentry->d_inode;
3039 if (!dir->i_op->tmpfile) {
3040 error = -EOPNOTSUPP;
3041 goto out2;
3042 }
3043 child = d_alloc(dentry, &name);
3044 if (unlikely(!child)) {
3045 error = -ENOMEM;
3046 goto out2;
3047 }
3048 nd->flags &= ~LOOKUP_DIRECTORY;
3049 nd->flags |= op->intent;
3050 dput(nd->path.dentry);
3051 nd->path.dentry = child;
3052 error = dir->i_op->tmpfile(dir, nd->path.dentry, op->mode);
3053 if (error)
3054 goto out2;
3055 audit_inode(pathname->name, nd->path.dentry);
Eric Rannaud78cb8e02014-10-30 01:51:01 -07003056 /* Don't check for other permissions, the inode was just created */
3057 error = may_open(&nd->path, MAY_OPEN, op->open_flag);
Al Virob0d54302013-06-07 01:20:27 -04003058 if (error)
3059 goto out2;
3060 file->f_path.mnt = nd->path.mnt;
3061 error = finish_open(file, nd->path.dentry, NULL, opened);
3062 if (error)
3063 goto out2;
3064 error = open_check_o_direct(file);
Al Virof6f34262013-06-11 08:34:36 +04003065 if (error) {
Al Virob0d54302013-06-07 01:20:27 -04003066 fput(file);
Al Virof6f34262013-06-11 08:34:36 +04003067 } else if (!(op->open_flag & O_EXCL)) {
3068 struct inode *inode = file->f_path.dentry->d_inode;
3069 spin_lock(&inode->i_lock);
3070 inode->i_state |= I_LINKABLE;
3071 spin_unlock(&inode->i_lock);
3072 }
Al Virob0d54302013-06-07 01:20:27 -04003073out2:
3074 mnt_drop_write(nd->path.mnt);
3075out:
3076 path_put(&nd->path);
3077 return error;
3078}
3079
Jeff Layton631423f2012-10-10 16:43:10 -04003080static struct file *path_openat(int dfd, struct filename *pathname,
Al Viro73d049a2011-03-11 12:08:24 -05003081 struct nameidata *nd, const struct open_flags *op, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082{
Al Virofe2d35f2011-03-05 22:58:25 -05003083 struct file *base = NULL;
Al Virob2ebdc52012-06-22 12:40:19 +04003084 struct file *file;
Al Viro9850c052010-01-13 15:01:15 -05003085 struct path path;
Al Viro8915ef32012-06-10 05:01:45 -04003086 int opened = 0;
Al Viro13aab422011-02-23 17:54:08 -05003087 int error;
Nick Piggin31e6b012011-01-07 17:49:52 +11003088
Al Virob2ebdc52012-06-22 12:40:19 +04003089 file = get_empty_filp();
3090 if (!file)
Nick Piggin31e6b012011-01-07 17:49:52 +11003091 return ERR_PTR(-ENFILE);
3092
Al Virob2ebdc52012-06-22 12:40:19 +04003093 file->f_flags = op->open_flag;
Nick Piggin31e6b012011-01-07 17:49:52 +11003094
Al Virod50bcfe2013-07-13 13:26:37 +04003095 if (unlikely(file->f_flags & __O_TMPFILE)) {
Al Virob0d54302013-06-07 01:20:27 -04003096 error = do_tmpfile(dfd, pathname, nd, flags, op, file, &opened);
Al Viro8b411372015-05-08 22:53:15 -04003097 goto out2;
Al Virob0d54302013-06-07 01:20:27 -04003098 }
3099
Jeff Layton631423f2012-10-10 16:43:10 -04003100 error = path_init(dfd, pathname->name, flags | LOOKUP_PARENT, nd, &base);
Nick Piggin31e6b012011-01-07 17:49:52 +11003101 if (unlikely(error))
Al Viro70806c32012-06-22 12:41:10 +04003102 goto out;
Nick Piggin31e6b012011-01-07 17:49:52 +11003103
Al Virofe2d35f2011-03-05 22:58:25 -05003104 current->total_link_count = 0;
Jeff Layton631423f2012-10-10 16:43:10 -04003105 error = link_path_walk(pathname->name, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11003106 if (unlikely(error))
Al Viro70806c32012-06-22 12:41:10 +04003107 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108
Al Viro70806c32012-06-22 12:41:10 +04003109 error = do_last(nd, &path, file, op, &opened, pathname);
3110 while (unlikely(error > 0)) { /* trailing symlink */
Nick Piggin7b9337a2011-01-14 08:42:43 +00003111 struct path link = path;
Al Virodef4af32009-12-26 08:37:05 -05003112 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04003113 if (!(nd->flags & LOOKUP_FOLLOW)) {
Al Viro73d049a2011-03-11 12:08:24 -05003114 path_put_conditional(&path, nd);
3115 path_put(&nd->path);
Al Viro70806c32012-06-22 12:41:10 +04003116 error = -ELOOP;
Al Viro40b39132011-03-09 16:22:18 -05003117 break;
3118 }
Kees Cook5092e9c2012-07-25 17:29:07 -07003119 error = may_follow_link(&link, nd);
3120 if (unlikely(error))
3121 break;
Al Viro73d049a2011-03-11 12:08:24 -05003122 nd->flags |= LOOKUP_PARENT;
3123 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
Al Viro574197e2011-03-14 22:20:34 -04003124 error = follow_link(&link, nd, &cookie);
Al Viroc3e380b2011-02-23 13:39:45 -05003125 if (unlikely(error))
Al Viro70806c32012-06-22 12:41:10 +04003126 break;
3127 error = do_last(nd, &path, file, op, &opened, pathname);
Al Viro574197e2011-03-14 22:20:34 -04003128 put_link(nd, &link, cookie);
Al Viro806b6812009-12-26 07:16:40 -05003129 }
Al Viro10fa8e62009-12-26 07:09:49 -05003130out:
Al Viro73d049a2011-03-11 12:08:24 -05003131 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
3132 path_put(&nd->root);
Al Virofe2d35f2011-03-05 22:58:25 -05003133 if (base)
3134 fput(base);
Al Viro8b411372015-05-08 22:53:15 -04003135out2:
Al Viro70806c32012-06-22 12:41:10 +04003136 if (!(opened & FILE_OPENED)) {
3137 BUG_ON(!error);
Al Virob2ebdc52012-06-22 12:40:19 +04003138 put_filp(file);
Miklos Szeredi2f80ad32012-06-05 15:10:27 +02003139 }
Al Viro70806c32012-06-22 12:41:10 +04003140 if (unlikely(error)) {
3141 if (error == -EOPENSTALE) {
3142 if (flags & LOOKUP_RCU)
3143 error = -ECHILD;
3144 else
3145 error = -ESTALE;
3146 }
3147 file = ERR_PTR(error);
3148 }
3149 return file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150}
3151
Jeff Layton631423f2012-10-10 16:43:10 -04003152struct file *do_filp_open(int dfd, struct filename *pathname,
Al Viro13aab422011-02-23 17:54:08 -05003153 const struct open_flags *op, int flags)
3154{
Al Viro73d049a2011-03-11 12:08:24 -05003155 struct nameidata nd;
Al Viro13aab422011-02-23 17:54:08 -05003156 struct file *filp;
3157
Al Viro73d049a2011-03-11 12:08:24 -05003158 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
Al Viro13aab422011-02-23 17:54:08 -05003159 if (unlikely(filp == ERR_PTR(-ECHILD)))
Al Viro73d049a2011-03-11 12:08:24 -05003160 filp = path_openat(dfd, pathname, &nd, op, flags);
Al Viro13aab422011-02-23 17:54:08 -05003161 if (unlikely(filp == ERR_PTR(-ESTALE)))
Al Viro73d049a2011-03-11 12:08:24 -05003162 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
Al Viro13aab422011-02-23 17:54:08 -05003163 return filp;
3164}
3165
Al Viro73d049a2011-03-11 12:08:24 -05003166struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
3167 const char *name, const struct open_flags *op, int flags)
3168{
3169 struct nameidata nd;
3170 struct file *file;
Jeff Layton631423f2012-10-10 16:43:10 -04003171 struct filename filename = { .name = name };
Al Viro73d049a2011-03-11 12:08:24 -05003172
3173 nd.root.mnt = mnt;
3174 nd.root.dentry = dentry;
3175
3176 flags |= LOOKUP_ROOT;
3177
Al Virobcda7652011-03-13 16:42:14 -04003178 if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
Al Viro73d049a2011-03-11 12:08:24 -05003179 return ERR_PTR(-ELOOP);
3180
Jeff Layton631423f2012-10-10 16:43:10 -04003181 file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_RCU);
Al Viro73d049a2011-03-11 12:08:24 -05003182 if (unlikely(file == ERR_PTR(-ECHILD)))
Jeff Layton631423f2012-10-10 16:43:10 -04003183 file = path_openat(-1, &filename, &nd, op, flags);
Al Viro73d049a2011-03-11 12:08:24 -05003184 if (unlikely(file == ERR_PTR(-ESTALE)))
Jeff Layton631423f2012-10-10 16:43:10 -04003185 file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_REVAL);
Al Viro73d049a2011-03-11 12:08:24 -05003186 return file;
3187}
3188
Al Viroed75e952011-06-27 16:53:43 -04003189struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003191 struct dentry *dentry = ERR_PTR(-EEXIST);
Al Viroed75e952011-06-27 16:53:43 -04003192 struct nameidata nd;
Jan Kara7d140392012-06-12 16:20:30 +02003193 int err2;
Al Viroed75e952011-06-27 16:53:43 -04003194 int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
3195 if (error)
3196 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003198 /*
3199 * Yucky last component or no last component at all?
3200 * (foo/., foo/.., /////)
3201 */
Al Viroed75e952011-06-27 16:53:43 -04003202 if (nd.last_type != LAST_NORM)
3203 goto out;
3204 nd.flags &= ~LOOKUP_PARENT;
3205 nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003206
Jan Kara7d140392012-06-12 16:20:30 +02003207 /* don't fail immediately if it's r/o, at least try to report other errors */
3208 err2 = mnt_want_write(nd.path.mnt);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003209 /*
3210 * Do the final lookup.
3211 */
Al Viroed75e952011-06-27 16:53:43 -04003212 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3213 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 if (IS_ERR(dentry))
Al Viroeb8b9ae2012-07-20 02:25:00 +04003215 goto unlock;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003216
Al Viroeb8b9ae2012-07-20 02:25:00 +04003217 error = -EEXIST;
Al Viroe9baf6e2008-05-15 04:49:12 -04003218 if (dentry->d_inode)
Al Viroeb8b9ae2012-07-20 02:25:00 +04003219 goto fail;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003220 /*
3221 * Special case - lookup gave negative, but... we had foo/bar/
3222 * From the vfs_mknod() POV we just have a negative dentry -
3223 * all is fine. Let's be bastards - you had / on the end, you've
3224 * been asking for (non-existent) directory. -ENOENT for you.
3225 */
Al Viroed75e952011-06-27 16:53:43 -04003226 if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
Al Viroeb8b9ae2012-07-20 02:25:00 +04003227 error = -ENOENT;
Al Viroed75e952011-06-27 16:53:43 -04003228 goto fail;
Al Viroe9baf6e2008-05-15 04:49:12 -04003229 }
Jan Kara7d140392012-06-12 16:20:30 +02003230 if (unlikely(err2)) {
3231 error = err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 goto fail;
3233 }
Al Viroed75e952011-06-27 16:53:43 -04003234 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236fail:
Al Viroeb8b9ae2012-07-20 02:25:00 +04003237 dput(dentry);
3238 dentry = ERR_PTR(error);
3239unlock:
Al Viroed75e952011-06-27 16:53:43 -04003240 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Kara7d140392012-06-12 16:20:30 +02003241 if (!err2)
3242 mnt_drop_write(nd.path.mnt);
Al Viroed75e952011-06-27 16:53:43 -04003243out:
3244 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 return dentry;
3246}
Al Virodae6ad82011-06-26 11:50:15 -04003247EXPORT_SYMBOL(kern_path_create);
3248
Al Viro58cc3382012-07-20 01:15:31 +04003249void done_path_create(struct path *path, struct dentry *dentry)
3250{
3251 dput(dentry);
3252 mutex_unlock(&path->dentry->d_inode->i_mutex);
Al Viroeb8b9ae2012-07-20 02:25:00 +04003253 mnt_drop_write(path->mnt);
Al Viro58cc3382012-07-20 01:15:31 +04003254 path_put(path);
3255}
3256EXPORT_SYMBOL(done_path_create);
3257
Al Virodae6ad82011-06-26 11:50:15 -04003258struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir)
3259{
Jeff Layton9cee5ca2012-10-10 15:25:28 -04003260 struct filename *tmp = getname(pathname);
Al Virodae6ad82011-06-26 11:50:15 -04003261 struct dentry *res;
3262 if (IS_ERR(tmp))
3263 return ERR_CAST(tmp);
Jeff Layton9cee5ca2012-10-10 15:25:28 -04003264 res = kern_path_create(dfd, tmp->name, path, is_dir);
Al Virodae6ad82011-06-26 11:50:15 -04003265 putname(tmp);
3266 return res;
3267}
3268EXPORT_SYMBOL(user_path_create);
3269
Daniel Rosenberg17285112016-10-26 16:27:45 -07003270int vfs_mknod2(struct vfsmount *mnt, struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271{
Daniel Rosenberg17285112016-10-26 16:27:45 -07003272 int error = may_create(mnt, dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273
3274 if (error)
3275 return error;
3276
Eric W. Biedermanb95e2122011-11-13 12:16:43 -08003277 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 return -EPERM;
3279
Al Viroacfa4382008-12-04 10:06:33 -05003280 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 return -EPERM;
3282
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07003283 error = devcgroup_inode_mknod(mode, dev);
3284 if (error)
3285 return error;
3286
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 error = security_inode_mknod(dir, dentry, mode, dev);
3288 if (error)
3289 return error;
3290
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 error = dir->i_op->mknod(dir, dentry, mode, dev);
Amir Samuelov6a22e462014-05-26 11:44:06 +03003292 if (error)
3293 return error;
3294
3295 error = security_inode_post_create(dir, dentry, mode);
3296 if (error)
3297 return error;
3298
Stephen Smalleya74574a2005-09-09 13:01:44 -07003299 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003300 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 return error;
3302}
Daniel Rosenberg17285112016-10-26 16:27:45 -07003303EXPORT_SYMBOL(vfs_mknod2);
3304
3305int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
3306{
3307 return vfs_mknod2(NULL, dir, dentry, mode, dev);
3308}
3309EXPORT_SYMBOL(vfs_mknod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310
Al Virof69aac02011-07-26 04:31:40 -04003311static int may_mknod(umode_t mode)
Dave Hansen463c3192008-02-15 14:37:57 -08003312{
3313 switch (mode & S_IFMT) {
3314 case S_IFREG:
3315 case S_IFCHR:
3316 case S_IFBLK:
3317 case S_IFIFO:
3318 case S_IFSOCK:
3319 case 0: /* zero mode translates to S_IFREG */
3320 return 0;
3321 case S_IFDIR:
3322 return -EPERM;
3323 default:
3324 return -EINVAL;
3325 }
3326}
3327
Al Viro8208a222011-07-25 17:32:17 -04003328SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003329 unsigned, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330{
Al Viro2ad94ae2008-07-21 09:32:51 -04003331 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003332 struct path path;
3333 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334
Al Viro1db2b392012-07-20 01:17:26 +04003335 error = may_mknod(mode);
3336 if (error)
3337 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338
Al Virodae6ad82011-06-26 11:50:15 -04003339 dentry = user_path_create(dfd, filename, &path, 0);
3340 if (IS_ERR(dentry))
3341 return PTR_ERR(dentry);
Al Viro2ad94ae2008-07-21 09:32:51 -04003342
Al Virodae6ad82011-06-26 11:50:15 -04003343 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003344 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003345 error = security_path_mknod(&path, dentry, mode, dev);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003346 if (error)
Al Viroeb8b9ae2012-07-20 02:25:00 +04003347 goto out;
Dave Hansen463c3192008-02-15 14:37:57 -08003348 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 case 0: case S_IFREG:
Al Viro8da0eaf2012-06-10 18:09:36 -04003350 error = vfs_create2(path.mnt, path.dentry->d_inode,dentry,mode,true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 break;
3352 case S_IFCHR: case S_IFBLK:
Daniel Rosenberg17285112016-10-26 16:27:45 -07003353 error = vfs_mknod2(path.mnt, path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 new_decode_dev(dev));
3355 break;
3356 case S_IFIFO: case S_IFSOCK:
Al Virodae6ad82011-06-26 11:50:15 -04003357 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 }
Al Viroeb8b9ae2012-07-20 02:25:00 +04003360out:
Al Viro58cc3382012-07-20 01:15:31 +04003361 done_path_create(&path, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362
3363 return error;
3364}
3365
Al Viro8208a222011-07-25 17:32:17 -04003366SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003367{
3368 return sys_mknodat(AT_FDCWD, filename, mode, dev);
3369}
3370
Daniel Rosenberg17285112016-10-26 16:27:45 -07003371int vfs_mkdir2(struct vfsmount *mnt, struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372{
Daniel Rosenberg17285112016-10-26 16:27:45 -07003373 int error = may_create(mnt, dir, dentry);
Al Viro8de52772012-02-06 12:45:27 -05003374 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375
3376 if (error)
3377 return error;
3378
Al Viroacfa4382008-12-04 10:06:33 -05003379 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 return -EPERM;
3381
3382 mode &= (S_IRWXUGO|S_ISVTX);
3383 error = security_inode_mkdir(dir, dentry, mode);
3384 if (error)
3385 return error;
3386
Al Viro8de52772012-02-06 12:45:27 -05003387 if (max_links && dir->i_nlink >= max_links)
3388 return -EMLINK;
3389
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003391 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003392 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 return error;
3394}
Daniel Rosenberg17285112016-10-26 16:27:45 -07003395EXPORT_SYMBOL(vfs_mkdir2);
3396
3397int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
3398{
3399 return vfs_mkdir2(NULL, dir, dentry, mode);
3400}
3401EXPORT_SYMBOL(vfs_mkdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402
Al Viroa218d0f2011-11-21 14:59:34 -05003403SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404{
Dave Hansen6902d922006-09-30 23:29:01 -07003405 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003406 struct path path;
3407 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408
Al Virodae6ad82011-06-26 11:50:15 -04003409 dentry = user_path_create(dfd, pathname, &path, 1);
Dave Hansen6902d922006-09-30 23:29:01 -07003410 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003411 return PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003412
Al Virodae6ad82011-06-26 11:50:15 -04003413 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003414 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003415 error = security_path_mkdir(&path, dentry, mode);
Al Viroeb8b9ae2012-07-20 02:25:00 +04003416 if (!error)
3417 error = vfs_mkdir2(path.mnt, path.dentry->d_inode, dentry, mode);
Al Viro58cc3382012-07-20 01:15:31 +04003418 done_path_create(&path, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 return error;
3420}
3421
Al Viroa218d0f2011-11-21 14:59:34 -05003422SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003423{
3424 return sys_mkdirat(AT_FDCWD, pathname, mode);
3425}
3426
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427/*
Sage Weila71905f2011-05-24 13:06:08 -07003428 * The dentry_unhash() helper will try to drop the dentry early: we
J. Bruce Fieldsc0d02592012-02-15 11:48:40 -05003429 * should have a usage count of 1 if we're the only user of this
Sage Weila71905f2011-05-24 13:06:08 -07003430 * dentry, and if that is true (possibly after pruning the dcache),
3431 * then we drop the dentry now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 *
3433 * A low-level filesystem can, if it choses, legally
3434 * do a
3435 *
3436 * if (!d_unhashed(dentry))
3437 * return -EBUSY;
3438 *
3439 * if it cannot handle the case of removing a directory
3440 * that is still in use by something else..
3441 */
3442void dentry_unhash(struct dentry *dentry)
3443{
Vasily Averindc168422006-12-06 20:37:07 -08003444 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 spin_lock(&dentry->d_lock);
Sage Weil64252c72011-05-24 13:06:05 -07003446 if (dentry->d_count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 __d_drop(dentry);
3448 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449}
3450
Daniel Rosenberg17285112016-10-26 16:27:45 -07003451int vfs_rmdir2(struct vfsmount *mnt, struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452{
Daniel Rosenberg17285112016-10-26 16:27:45 -07003453 int error = may_delete(mnt, dir, dentry, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454
3455 if (error)
3456 return error;
3457
Al Viroacfa4382008-12-04 10:06:33 -05003458 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 return -EPERM;
3460
Al Viro1d2ef592011-09-14 18:55:41 +01003461 dget(dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003462 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463
Sage Weil912dbc12011-05-24 13:06:11 -07003464 error = -EBUSY;
3465 if (d_mountpoint(dentry))
3466 goto out;
3467
3468 error = security_inode_rmdir(dir, dentry);
3469 if (error)
3470 goto out;
3471
Sage Weil3cebde22011-05-29 21:20:59 -07003472 shrink_dcache_parent(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003473 error = dir->i_op->rmdir(dir, dentry);
3474 if (error)
3475 goto out;
3476
3477 dentry->d_inode->i_flags |= S_DEAD;
3478 dont_mount(dentry);
3479
3480out:
3481 mutex_unlock(&dentry->d_inode->i_mutex);
Al Viro1d2ef592011-09-14 18:55:41 +01003482 dput(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003483 if (!error)
3484 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 return error;
3486}
Daniel Rosenberg17285112016-10-26 16:27:45 -07003487EXPORT_SYMBOL(vfs_rmdir2);
3488
3489int vfs_rmdir(struct inode *dir, struct dentry *dentry)
3490{
3491 return vfs_rmdir2(NULL, dir, dentry);
3492}
3493EXPORT_SYMBOL(vfs_rmdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003495static long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496{
3497 int error = 0;
Jeff Layton9cee5ca2012-10-10 15:25:28 -04003498 struct filename *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 struct dentry *dentry;
3500 struct nameidata nd;
3501
Jeff Layton9cee5ca2012-10-10 15:25:28 -04003502 name = user_path_parent(dfd, pathname, &nd);
3503 if (IS_ERR(name))
3504 return PTR_ERR(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505
3506 switch(nd.last_type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003507 case LAST_DOTDOT:
3508 error = -ENOTEMPTY;
3509 goto exit1;
3510 case LAST_DOT:
3511 error = -EINVAL;
3512 goto exit1;
3513 case LAST_ROOT:
3514 error = -EBUSY;
3515 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003517
3518 nd.flags &= ~LOOKUP_PARENT;
Jan Kara7d140392012-06-12 16:20:30 +02003519 error = mnt_want_write(nd.path.mnt);
3520 if (error)
3521 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003522
Jan Blunck4ac91372008-02-14 19:34:32 -08003523 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08003524 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003526 if (IS_ERR(dentry))
3527 goto exit2;
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04003528 if (!dentry->d_inode) {
3529 error = -ENOENT;
3530 goto exit3;
3531 }
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003532 error = security_path_rmdir(&nd.path, dentry);
3533 if (error)
Jan Kara7d140392012-06-12 16:20:30 +02003534 goto exit3;
Daniel Rosenberg17285112016-10-26 16:27:45 -07003535 error = vfs_rmdir2(nd.path.mnt, nd.path.dentry->d_inode, dentry);
Dave Hansen06227532008-02-15 14:37:34 -08003536exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07003537 dput(dentry);
3538exit2:
Jan Blunck4ac91372008-02-14 19:34:32 -08003539 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Kara7d140392012-06-12 16:20:30 +02003540 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003542 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 putname(name);
3544 return error;
3545}
3546
Heiko Carstens3cdad422009-01-14 14:14:22 +01003547SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003548{
3549 return do_rmdir(AT_FDCWD, pathname);
3550}
3551
Daniel Rosenberg17285112016-10-26 16:27:45 -07003552int vfs_unlink2(struct vfsmount *mnt, struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553{
Daniel Rosenberg17285112016-10-26 16:27:45 -07003554 int error = may_delete(mnt, dir, dentry, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555
3556 if (error)
3557 return error;
3558
Al Viroacfa4382008-12-04 10:06:33 -05003559 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 return -EPERM;
3561
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003562 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 if (d_mountpoint(dentry))
3564 error = -EBUSY;
3565 else {
3566 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05003567 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 error = dir->i_op->unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05003569 if (!error)
Al Virod83c49f2010-04-30 17:17:09 -04003570 dont_mount(dentry);
Al Virobec10522010-03-03 14:12:08 -05003571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003573 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574
3575 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
3576 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
Jan Karaece95912008-02-06 01:37:13 -08003577 fsnotify_link_count(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 }
Robert Love0eeca282005-07-12 17:06:03 -04003580
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 return error;
3582}
Daniel Rosenberg17285112016-10-26 16:27:45 -07003583EXPORT_SYMBOL(vfs_unlink2);
3584
3585int vfs_unlink(struct inode *dir, struct dentry *dentry)
3586{
3587 return vfs_unlink2(NULL, dir, dentry);
3588}
3589EXPORT_SYMBOL(vfs_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590
3591/*
3592 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003593 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 * writeout happening, and we don't want to prevent access to the directory
3595 * while waiting on the I/O.
3596 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003597static long do_unlinkat(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598{
Al Viro2ad94ae2008-07-21 09:32:51 -04003599 int error;
Jeff Layton9cee5ca2012-10-10 15:25:28 -04003600 struct filename *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 struct dentry *dentry;
3602 struct nameidata nd;
3603 struct inode *inode = NULL;
3604
Jeff Layton9cee5ca2012-10-10 15:25:28 -04003605 name = user_path_parent(dfd, pathname, &nd);
3606 if (IS_ERR(name))
3607 return PTR_ERR(name);
Al Viro2ad94ae2008-07-21 09:32:51 -04003608
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 error = -EISDIR;
3610 if (nd.last_type != LAST_NORM)
3611 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003612
3613 nd.flags &= ~LOOKUP_PARENT;
Jan Kara7d140392012-06-12 16:20:30 +02003614 error = mnt_want_write(nd.path.mnt);
3615 if (error)
3616 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003617
Jan Blunck4ac91372008-02-14 19:34:32 -08003618 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08003619 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 error = PTR_ERR(dentry);
3621 if (!IS_ERR(dentry)) {
3622 /* Why not before? Because we want correct error value */
Török Edwin50338b82011-06-16 00:06:14 +03003623 if (nd.last.name[nd.last.len])
3624 goto slashes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 inode = dentry->d_inode;
Török Edwin50338b82011-06-16 00:06:14 +03003626 if (!inode)
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04003627 goto slashes;
3628 ihold(inode);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003629 error = security_path_unlink(&nd.path, dentry);
3630 if (error)
Jan Kara7d140392012-06-12 16:20:30 +02003631 goto exit2;
Daniel Rosenberg17285112016-10-26 16:27:45 -07003632 error = vfs_unlink2(nd.path.mnt, nd.path.dentry->d_inode, dentry);
Jan Kara7d140392012-06-12 16:20:30 +02003633exit2:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 dput(dentry);
3635 }
Jan Blunck4ac91372008-02-14 19:34:32 -08003636 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 if (inode)
3638 iput(inode); /* truncate the inode here */
Jan Kara7d140392012-06-12 16:20:30 +02003639 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003641 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 putname(name);
3643 return error;
3644
3645slashes:
3646 error = !dentry->d_inode ? -ENOENT :
3647 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
3648 goto exit2;
3649}
3650
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003651SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003652{
3653 if ((flag & ~AT_REMOVEDIR) != 0)
3654 return -EINVAL;
3655
3656 if (flag & AT_REMOVEDIR)
3657 return do_rmdir(dfd, pathname);
3658
3659 return do_unlinkat(dfd, pathname);
3660}
3661
Heiko Carstens3480b252009-01-14 14:14:16 +01003662SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003663{
3664 return do_unlinkat(AT_FDCWD, pathname);
3665}
3666
Daniel Rosenberg17285112016-10-26 16:27:45 -07003667int vfs_symlink2(struct vfsmount *mnt, struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668{
Daniel Rosenberg17285112016-10-26 16:27:45 -07003669 int error = may_create(mnt, dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670
3671 if (error)
3672 return error;
3673
Al Viroacfa4382008-12-04 10:06:33 -05003674 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 return -EPERM;
3676
3677 error = security_inode_symlink(dir, dentry, oldname);
3678 if (error)
3679 return error;
3680
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003682 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003683 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 return error;
3685}
Daniel Rosenberg17285112016-10-26 16:27:45 -07003686EXPORT_SYMBOL(vfs_symlink2);
3687
3688int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
3689{
3690 return vfs_symlink2(NULL, dir, dentry, oldname);
3691}
3692EXPORT_SYMBOL(vfs_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003694SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
3695 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696{
Al Viro2ad94ae2008-07-21 09:32:51 -04003697 int error;
Jeff Layton9cee5ca2012-10-10 15:25:28 -04003698 struct filename *from;
Dave Hansen6902d922006-09-30 23:29:01 -07003699 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003700 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701
3702 from = getname(oldname);
Al Viro2ad94ae2008-07-21 09:32:51 -04003703 if (IS_ERR(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 return PTR_ERR(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04003705
Al Virodae6ad82011-06-26 11:50:15 -04003706 dentry = user_path_create(newdfd, newname, &path, 0);
Dave Hansen6902d922006-09-30 23:29:01 -07003707 error = PTR_ERR(dentry);
3708 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003709 goto out_putname;
Dave Hansen6902d922006-09-30 23:29:01 -07003710
Jeff Layton9cee5ca2012-10-10 15:25:28 -04003711 error = security_path_symlink(&path, dentry, from->name);
Al Viroeb8b9ae2012-07-20 02:25:00 +04003712 if (!error)
Jeff Layton9cee5ca2012-10-10 15:25:28 -04003713 error = vfs_symlink2(path.mnt, path.dentry->d_inode, dentry, from->name);
Al Viro58cc3382012-07-20 01:15:31 +04003714 done_path_create(&path, dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003715out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 putname(from);
3717 return error;
3718}
3719
Heiko Carstens3480b252009-01-14 14:14:16 +01003720SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003721{
3722 return sys_symlinkat(oldname, AT_FDCWD, newname);
3723}
3724
Daniel Rosenberg17285112016-10-26 16:27:45 -07003725int vfs_link2(struct vfsmount *mnt, struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726{
3727 struct inode *inode = old_dentry->d_inode;
Al Viro8de52772012-02-06 12:45:27 -05003728 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 int error;
3730
3731 if (!inode)
3732 return -ENOENT;
3733
Daniel Rosenberg17285112016-10-26 16:27:45 -07003734 error = may_create(mnt, dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 if (error)
3736 return error;
3737
3738 if (dir->i_sb != inode->i_sb)
3739 return -EXDEV;
3740
3741 /*
3742 * A link to an append-only or immutable file cannot be created.
3743 */
3744 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3745 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05003746 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003748 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 return -EPERM;
3750
3751 error = security_inode_link(old_dentry, dir, new_dentry);
3752 if (error)
3753 return error;
3754
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003755 mutex_lock(&inode->i_mutex);
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05303756 /* Make sure we don't allow creating hardlink to an unlinked file */
Al Virof6f34262013-06-11 08:34:36 +04003757 if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05303758 error = -ENOENT;
Al Viro8de52772012-02-06 12:45:27 -05003759 else if (max_links && inode->i_nlink >= max_links)
3760 error = -EMLINK;
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05303761 else
3762 error = dir->i_op->link(old_dentry, dir, new_dentry);
Al Virof6f34262013-06-11 08:34:36 +04003763
3764 if (!error && (inode->i_state & I_LINKABLE)) {
3765 spin_lock(&inode->i_lock);
3766 inode->i_state &= ~I_LINKABLE;
3767 spin_unlock(&inode->i_lock);
3768 }
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003769 mutex_unlock(&inode->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07003770 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02003771 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 return error;
3773}
Daniel Rosenberg17285112016-10-26 16:27:45 -07003774EXPORT_SYMBOL(vfs_link2);
3775
3776int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
3777{
3778 return vfs_link2(NULL, old_dentry, dir, new_dentry);
3779}
3780EXPORT_SYMBOL(vfs_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781
3782/*
3783 * Hardlinks are often used in delicate situations. We avoid
3784 * security-related surprises by not following symlinks on the
3785 * newname. --KAB
3786 *
3787 * We don't follow them on the oldname either to be compatible
3788 * with linux 2.0, and to avoid hard-linking to directories
3789 * and other special files. --ADM
3790 */
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003791SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
3792 int, newdfd, const char __user *, newname, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793{
3794 struct dentry *new_dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003795 struct path old_path, new_path;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303796 int how = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303799 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003800 return -EINVAL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303801 /*
3802 * To use null names we require CAP_DAC_READ_SEARCH
3803 * This ensures that not everyone will be able to create
3804 * handlink using the passed filedescriptor.
3805 */
3806 if (flags & AT_EMPTY_PATH) {
3807 if (!capable(CAP_DAC_READ_SEARCH))
3808 return -ENOENT;
3809 how = LOOKUP_EMPTY;
3810 }
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003811
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05303812 if (flags & AT_SYMLINK_FOLLOW)
3813 how |= LOOKUP_FOLLOW;
3814
3815 error = user_path_at(olddfd, oldname, how, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04003817 return error;
3818
Al Virodae6ad82011-06-26 11:50:15 -04003819 new_dentry = user_path_create(newdfd, newname, &new_path, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003821 if (IS_ERR(new_dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003822 goto out;
3823
3824 error = -EXDEV;
3825 if (old_path.mnt != new_path.mnt)
3826 goto out_dput;
Kees Cook5092e9c2012-07-25 17:29:07 -07003827 error = may_linkat(&old_path);
3828 if (unlikely(error))
Dave Hansen75c3f292008-02-15 14:37:45 -08003829 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04003830 error = security_path_link(old_path.dentry, &new_path, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003831 if (error)
Al Viroeb8b9ae2012-07-20 02:25:00 +04003832 goto out_dput;
Daniel Rosenberg17285112016-10-26 16:27:45 -07003833 error = vfs_link2(old_path.mnt, old_path.dentry, new_path.dentry->d_inode, new_dentry);
Dave Hansen75c3f292008-02-15 14:37:45 -08003834out_dput:
Al Viro58cc3382012-07-20 01:15:31 +04003835 done_path_create(&new_path, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836out:
Al Viro2d8f3032008-07-22 09:59:21 -04003837 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838
3839 return error;
3840}
3841
Heiko Carstens3480b252009-01-14 14:14:16 +01003842SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003843{
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003844 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003845}
3846
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847/*
3848 * The worst of all namespace operations - renaming directory. "Perverted"
3849 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
3850 * Problems:
3851 * a) we can get into loop creation. Check is done in is_subdir().
3852 * b) race potential - two innocent renames can create a loop together.
3853 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08003854 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 * story.
3856 * c) we have to lock _three_ objects - parents and victim (if it exists).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003857 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 * whether the target exists). Solution: try to be smart with locking
3859 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08003860 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 * move will be locked. Thus we can rank directories by the tree
3862 * (ancestors first) and rank all non-directories after them.
3863 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08003864 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 * HOWEVER, it relies on the assumption that any object with ->lookup()
3866 * has no more than 1 dentry. If "hybrid" objects will ever appear,
3867 * we'd better make sure that there's no link(2) for them.
Sage Weile4eaac02011-05-24 13:06:07 -07003868 * d) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003869 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05003871 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 * locking].
3873 */
Daniel Rosenberg17285112016-10-26 16:27:45 -07003874static int vfs_rename_dir(struct vfsmount *mnt,
3875 struct inode *old_dir, struct dentry *old_dentry,
3876 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877{
3878 int error = 0;
Sage Weil9055cba2011-05-24 13:06:12 -07003879 struct inode *target = new_dentry->d_inode;
Al Viro8de52772012-02-06 12:45:27 -05003880 unsigned max_links = new_dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881
3882 /*
3883 * If we are going to change the parent - check write permissions,
3884 * we'll need to flip '..'.
3885 */
3886 if (new_dir != old_dir) {
Daniel Rosenberg17285112016-10-26 16:27:45 -07003887 error = inode_permission2(mnt, old_dentry->d_inode, MAY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 if (error)
3889 return error;
3890 }
3891
3892 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3893 if (error)
3894 return error;
3895
Al Viro1d2ef592011-09-14 18:55:41 +01003896 dget(new_dentry);
Al Virod83c49f2010-04-30 17:17:09 -04003897 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003898 mutex_lock(&target->i_mutex);
Sage Weil9055cba2011-05-24 13:06:12 -07003899
3900 error = -EBUSY;
3901 if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
3902 goto out;
3903
Al Viro8de52772012-02-06 12:45:27 -05003904 error = -EMLINK;
3905 if (max_links && !target && new_dir != old_dir &&
3906 new_dir->i_nlink >= max_links)
3907 goto out;
3908
Sage Weil3cebde22011-05-29 21:20:59 -07003909 if (target)
3910 shrink_dcache_parent(new_dentry);
Sage Weil9055cba2011-05-24 13:06:12 -07003911 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3912 if (error)
3913 goto out;
3914
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 if (target) {
Sage Weil9055cba2011-05-24 13:06:12 -07003916 target->i_flags |= S_DEAD;
3917 dont_mount(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 }
Sage Weil9055cba2011-05-24 13:06:12 -07003919out:
3920 if (target)
3921 mutex_unlock(&target->i_mutex);
Al Viro1d2ef592011-09-14 18:55:41 +01003922 dput(new_dentry);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07003923 if (!error)
Mark Fasheh349457c2006-09-08 14:22:21 -07003924 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3925 d_move(old_dentry,new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 return error;
3927}
3928
Adrian Bunk75c96f82005-05-05 16:16:09 -07003929static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
3930 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931{
Sage Weil51892bb2011-05-24 13:06:13 -07003932 struct inode *target = new_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 int error;
3934
3935 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3936 if (error)
3937 return error;
3938
3939 dget(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003941 mutex_lock(&target->i_mutex);
Sage Weil51892bb2011-05-24 13:06:13 -07003942
3943 error = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
Sage Weil51892bb2011-05-24 13:06:13 -07003945 goto out;
3946
3947 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3948 if (error)
3949 goto out;
3950
3951 if (target)
3952 dont_mount(new_dentry);
3953 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3954 d_move(old_dentry, new_dentry);
3955out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003957 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 dput(new_dentry);
3959 return error;
3960}
3961
Daniel Rosenberg17285112016-10-26 16:27:45 -07003962int vfs_rename2(struct vfsmount *mnt,
3963 struct inode *old_dir, struct dentry *old_dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 struct inode *new_dir, struct dentry *new_dentry)
3965{
3966 int error;
3967 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
Al Viro6e71d852017-07-07 14:51:19 -04003968 struct name_snapshot old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969
3970 if (old_dentry->d_inode == new_dentry->d_inode)
3971 return 0;
3972
Daniel Rosenberg17285112016-10-26 16:27:45 -07003973 error = may_delete(mnt, old_dir, old_dentry, is_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 if (error)
3975 return error;
3976
3977 if (!new_dentry->d_inode)
Daniel Rosenberg17285112016-10-26 16:27:45 -07003978 error = may_create(mnt, new_dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 else
Daniel Rosenberg17285112016-10-26 16:27:45 -07003980 error = may_delete(mnt, new_dir, new_dentry, is_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 if (error)
3982 return error;
3983
Al Viroacfa4382008-12-04 10:06:33 -05003984 if (!old_dir->i_op->rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 return -EPERM;
3986
Al Viro6e71d852017-07-07 14:51:19 -04003987 take_dentry_name_snapshot(&old_name, old_dentry);
Robert Love0eeca282005-07-12 17:06:03 -04003988
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 if (is_dir)
Daniel Rosenberg17285112016-10-26 16:27:45 -07003990 error = vfs_rename_dir(mnt, old_dir,old_dentry,new_dir,new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 else
3992 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
Al Viro123df292009-12-25 04:57:57 -05003993 if (!error)
Al Viro6e71d852017-07-07 14:51:19 -04003994 fsnotify_move(old_dir, new_dir, old_name.name, is_dir,
Al Viro5a190ae2007-06-07 12:19:32 -04003995 new_dentry->d_inode, old_dentry);
Al Viro6e71d852017-07-07 14:51:19 -04003996 release_dentry_name_snapshot(&old_name);
Robert Love0eeca282005-07-12 17:06:03 -04003997
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 return error;
3999}
Daniel Rosenberg17285112016-10-26 16:27:45 -07004000EXPORT_SYMBOL(vfs_rename2);
4001
4002int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
4003 struct inode *new_dir, struct dentry *new_dentry)
4004{
4005 return vfs_rename2(NULL, old_dir, old_dentry, new_dir, new_dentry);
4006}
4007EXPORT_SYMBOL(vfs_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008
Heiko Carstens2e4d0922009-01-14 14:14:31 +01004009SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4010 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011{
Al Viro2ad94ae2008-07-21 09:32:51 -04004012 struct dentry *old_dir, *new_dir;
4013 struct dentry *old_dentry, *new_dentry;
4014 struct dentry *trap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 struct nameidata oldnd, newnd;
Jeff Layton9cee5ca2012-10-10 15:25:28 -04004016 struct filename *from;
4017 struct filename *to;
Al Viro2ad94ae2008-07-21 09:32:51 -04004018 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019
Jeff Layton9cee5ca2012-10-10 15:25:28 -04004020 from = user_path_parent(olddfd, oldname, &oldnd);
4021 if (IS_ERR(from)) {
4022 error = PTR_ERR(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 goto exit;
Jeff Layton9cee5ca2012-10-10 15:25:28 -04004024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025
Jeff Layton9cee5ca2012-10-10 15:25:28 -04004026 to = user_path_parent(newdfd, newname, &newnd);
4027 if (IS_ERR(to)) {
4028 error = PTR_ERR(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 goto exit1;
Jeff Layton9cee5ca2012-10-10 15:25:28 -04004030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031
4032 error = -EXDEV;
Jan Blunck4ac91372008-02-14 19:34:32 -08004033 if (oldnd.path.mnt != newnd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 goto exit2;
4035
Jan Blunck4ac91372008-02-14 19:34:32 -08004036 old_dir = oldnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 error = -EBUSY;
4038 if (oldnd.last_type != LAST_NORM)
4039 goto exit2;
4040
Jan Blunck4ac91372008-02-14 19:34:32 -08004041 new_dir = newnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 if (newnd.last_type != LAST_NORM)
4043 goto exit2;
4044
Jan Kara7d140392012-06-12 16:20:30 +02004045 error = mnt_want_write(oldnd.path.mnt);
4046 if (error)
4047 goto exit2;
4048
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004049 oldnd.flags &= ~LOOKUP_PARENT;
4050 newnd.flags &= ~LOOKUP_PARENT;
OGAWA Hirofumi4e9ed2f2008-10-16 07:50:29 +09004051 newnd.flags |= LOOKUP_RENAME_TARGET;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004052
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 trap = lock_rename(new_dir, old_dir);
4054
Christoph Hellwig49705b72005-11-08 21:35:06 -08004055 old_dentry = lookup_hash(&oldnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 error = PTR_ERR(old_dentry);
4057 if (IS_ERR(old_dentry))
4058 goto exit3;
4059 /* source must exist */
4060 error = -ENOENT;
4061 if (!old_dentry->d_inode)
4062 goto exit4;
4063 /* unless the source is a directory trailing slashes give -ENOTDIR */
4064 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
4065 error = -ENOTDIR;
4066 if (oldnd.last.name[oldnd.last.len])
4067 goto exit4;
4068 if (newnd.last.name[newnd.last.len])
4069 goto exit4;
4070 }
4071 /* source should not be ancestor of target */
4072 error = -EINVAL;
4073 if (old_dentry == trap)
4074 goto exit4;
Christoph Hellwig49705b72005-11-08 21:35:06 -08004075 new_dentry = lookup_hash(&newnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 error = PTR_ERR(new_dentry);
4077 if (IS_ERR(new_dentry))
4078 goto exit4;
4079 /* target should not be an ancestor of source */
4080 error = -ENOTEMPTY;
4081 if (new_dentry == trap)
4082 goto exit5;
4083
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004084 error = security_path_rename(&oldnd.path, old_dentry,
4085 &newnd.path, new_dentry);
4086 if (error)
Jan Kara7d140392012-06-12 16:20:30 +02004087 goto exit5;
Daniel Rosenberg17285112016-10-26 16:27:45 -07004088 error = vfs_rename2(oldnd.path.mnt, old_dir->d_inode, old_dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 new_dir->d_inode, new_dentry);
4090exit5:
4091 dput(new_dentry);
4092exit4:
4093 dput(old_dentry);
4094exit3:
4095 unlock_rename(new_dir, old_dir);
Jan Kara7d140392012-06-12 16:20:30 +02004096 mnt_drop_write(oldnd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097exit2:
Jan Blunck1d957f92008-02-14 19:34:35 -08004098 path_put(&newnd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04004099 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08004101 path_put(&oldnd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 putname(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04004103exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 return error;
4105}
4106
Heiko Carstensa26eab22009-01-14 14:14:17 +01004107SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004108{
4109 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
4110}
4111
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
4113{
4114 int len;
4115
4116 len = PTR_ERR(link);
4117 if (IS_ERR(link))
4118 goto out;
4119
4120 len = strlen(link);
4121 if (len > (unsigned) buflen)
4122 len = buflen;
4123 if (copy_to_user(buffer, link, len))
4124 len = -EFAULT;
4125out:
4126 return len;
4127}
4128
4129/*
4130 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
4131 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
4132 * using) it for any given inode is up to filesystem.
4133 */
4134int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4135{
4136 struct nameidata nd;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004137 void *cookie;
Marcin Slusarz694a1762008-06-09 16:40:37 -07004138 int res;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004139
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 nd.depth = 0;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004141 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
Marcin Slusarz694a1762008-06-09 16:40:37 -07004142 if (IS_ERR(cookie))
4143 return PTR_ERR(cookie);
4144
4145 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
4146 if (dentry->d_inode->i_op->put_link)
4147 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
4148 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149}
4150
4151int vfs_follow_link(struct nameidata *nd, const char *link)
4152{
4153 return __vfs_follow_link(nd, link);
4154}
4155
4156/* get the link contents into pagecache */
4157static char *page_getlink(struct dentry * dentry, struct page **ppage)
4158{
Duane Griffinebd09ab2008-12-19 20:47:12 +00004159 char *kaddr;
4160 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 struct address_space *mapping = dentry->d_inode->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -07004162 page = read_mapping_page(mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 if (IS_ERR(page))
Nick Piggin6fe69002007-05-06 14:49:04 -07004164 return (char*)page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 *ppage = page;
Duane Griffinebd09ab2008-12-19 20:47:12 +00004166 kaddr = kmap(page);
4167 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
4168 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169}
4170
4171int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4172{
4173 struct page *page = NULL;
4174 char *s = page_getlink(dentry, &page);
4175 int res = vfs_readlink(dentry,buffer,buflen,s);
4176 if (page) {
4177 kunmap(page);
4178 page_cache_release(page);
4179 }
4180 return res;
4181}
4182
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004183void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004185 struct page *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 nd_set_link(nd, page_getlink(dentry, &page));
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004187 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188}
4189
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004190void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004192 struct page *page = cookie;
4193
4194 if (page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 kunmap(page);
4196 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 }
4198}
4199
Nick Piggin54566b22009-01-04 12:00:53 -08004200/*
4201 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
4202 */
4203int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204{
4205 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004206 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07004207 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08004208 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 char *kaddr;
Nick Piggin54566b22009-01-04 12:00:53 -08004210 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
4211 if (nofs)
4212 flags |= AOP_FLAG_NOFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213
NeilBrown7e53cac2006-03-25 03:07:57 -08004214retry:
Nick Pigginafddba42007-10-16 01:25:01 -07004215 err = pagecache_write_begin(NULL, mapping, 0, len-1,
Nick Piggin54566b22009-01-04 12:00:53 -08004216 flags, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07004218 goto fail;
4219
Cong Wange8e3c3d2011-11-25 23:14:27 +08004220 kaddr = kmap_atomic(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 memcpy(kaddr, symname, len-1);
Cong Wange8e3c3d2011-11-25 23:14:27 +08004222 kunmap_atomic(kaddr);
Nick Pigginafddba42007-10-16 01:25:01 -07004223
4224 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4225 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 if (err < 0)
4227 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07004228 if (err < len-1)
4229 goto retry;
4230
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 mark_inode_dirty(inode);
4232 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233fail:
4234 return err;
4235}
4236
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004237int page_symlink(struct inode *inode, const char *symname, int len)
4238{
4239 return __page_symlink(inode, symname, len,
Nick Piggin54566b22009-01-04 12:00:53 -08004240 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004241}
4242
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08004243const struct inode_operations page_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 .readlink = generic_readlink,
4245 .follow_link = page_follow_link_light,
4246 .put_link = page_put_link,
4247};
4248
Al Viro2d8f3032008-07-22 09:59:21 -04004249EXPORT_SYMBOL(user_path_at);
David Howellscc53ce52011-01-14 18:45:26 +00004250EXPORT_SYMBOL(follow_down_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251EXPORT_SYMBOL(follow_down);
4252EXPORT_SYMBOL(follow_up);
Al Viro66cc1a22012-08-26 12:55:54 -04004253EXPORT_SYMBOL(get_write_access); /* nfsd */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255EXPORT_SYMBOL(page_follow_link_light);
4256EXPORT_SYMBOL(page_put_link);
4257EXPORT_SYMBOL(page_readlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004258EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259EXPORT_SYMBOL(page_symlink);
4260EXPORT_SYMBOL(page_symlink_inode_operations);
Al Virod1811462008-08-02 00:49:18 -04004261EXPORT_SYMBOL(kern_path);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07004262EXPORT_SYMBOL(vfs_path_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263EXPORT_SYMBOL(unlock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264EXPORT_SYMBOL(vfs_follow_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265EXPORT_SYMBOL(generic_permission);
4266EXPORT_SYMBOL(vfs_readlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267EXPORT_SYMBOL(dentry_unhash);
4268EXPORT_SYMBOL(generic_readlink);