blob: 5c867dd1c0b34c52f03b558e9dc804d3dfddfbbe [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>
18#include <linux/module.h>
19#include <linux/slab.h>
20#include <linux/fs.h>
21#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/pagemap.h>
Robert Love0eeca282005-07-12 17:06:03 -040023#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/personality.h>
25#include <linux/security.h>
Mimi Zohar6146f0d2009-02-04 09:06:57 -050026#include <linux/ima.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/syscalls.h>
28#include <linux/mount.h>
29#include <linux/audit.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080030#include <linux/capability.h>
Trond Myklebust834f2a42005-10-18 14:20:16 -070031#include <linux/file.h>
Ulrich Drepper5590ff02006-01-18 17:43:53 -080032#include <linux/fcntl.h>
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070033#include <linux/device_cgroup.h>
Al Viro5ad4e532009-03-29 19:50:06 -040034#include <linux/fs_struct.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/uaccess.h>
36
Eric Parise81e3f42009-12-04 15:47:36 -050037#include "internal.h"
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039/* [Feb-1997 T. Schoebel-Theuer]
40 * Fundamental changes in the pathname lookup mechanisms (namei)
41 * were necessary because of omirr. The reason is that omirr needs
42 * to know the _real_ pathname, not the user-supplied one, in case
43 * of symlinks (and also when transname replacements occur).
44 *
45 * The new code replaces the old recursive symlink resolution with
46 * an iterative one (in case of non-nested symlink chains). It does
47 * this with calls to <fs>_follow_link().
48 * As a side effect, dir_namei(), _namei() and follow_link() are now
49 * replaced with a single function lookup_dentry() that can handle all
50 * the special cases of the former code.
51 *
52 * With the new dcache, the pathname is stored at each inode, at least as
53 * long as the refcount of the inode is positive. As a side effect, the
54 * size of the dcache depends on the inode cache and thus is dynamic.
55 *
56 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
57 * resolution to correspond with current state of the code.
58 *
59 * Note that the symlink resolution is not *completely* iterative.
60 * There is still a significant amount of tail- and mid- recursion in
61 * the algorithm. Also, note that <fs>_readlink() is not used in
62 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
63 * may return different results than <fs>_follow_link(). Many virtual
64 * filesystems (including /proc) exhibit this behavior.
65 */
66
67/* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
68 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
69 * and the name already exists in form of a symlink, try to create the new
70 * name indicated by the symlink. The old code always complained that the
71 * name already exists, due to not following the symlink even if its target
72 * is nonexistent. The new semantics affects also mknod() and link() when
Lucas De Marchi25985ed2011-03-30 22:57:33 -030073 * the name is a symlink pointing to a non-existent name.
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 *
75 * I don't know which semantics is the right one, since I have no access
76 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
77 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
78 * "old" one. Personally, I think the new semantics is much more logical.
79 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
80 * file does succeed in both HP-UX and SunOs, but not in Solaris
81 * and in the old Linux semantics.
82 */
83
84/* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
85 * semantics. See the comments in "open_namei" and "do_link" below.
86 *
87 * [10-Sep-98 Alan Modra] Another symlink change.
88 */
89
90/* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
91 * inside the path - always follow.
92 * in the last component in creation/removal/renaming - never follow.
93 * if LOOKUP_FOLLOW passed - follow.
94 * if the pathname has trailing slashes - follow.
95 * otherwise - don't follow.
96 * (applied in that order).
97 *
98 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
99 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
100 * During the 2.4 we need to fix the userland stuff depending on it -
101 * hopefully we will be able to get rid of that wart in 2.5. So far only
102 * XEmacs seems to be relying on it...
103 */
104/*
105 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
Arjan van de Vena11f3a02006-03-23 03:00:33 -0800106 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 * any extra contention...
108 */
109
110/* In order to reduce some races, while at the same time doing additional
111 * checking and hopefully speeding things up, we copy filenames to the
112 * kernel data space before using them..
113 *
114 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
115 * PATH_MAX includes the nul terminator --RR.
116 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800117static int do_getname(const char __user *filename, char *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
119 int retval;
120 unsigned long len = PATH_MAX;
121
122 if (!segment_eq(get_fs(), KERNEL_DS)) {
123 if ((unsigned long) filename >= TASK_SIZE)
124 return -EFAULT;
125 if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
126 len = TASK_SIZE - (unsigned long) filename;
127 }
128
129 retval = strncpy_from_user(page, filename, len);
130 if (retval > 0) {
131 if (retval < len)
132 return 0;
133 return -ENAMETOOLONG;
134 } else if (!retval)
135 retval = -ENOENT;
136 return retval;
137}
138
Al Virof52e0c12011-03-14 18:56:51 -0400139static char *getname_flags(const char __user * filename, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
141 char *tmp, *result;
142
143 result = ERR_PTR(-ENOMEM);
144 tmp = __getname();
145 if (tmp) {
146 int retval = do_getname(filename, tmp);
147
148 result = tmp;
149 if (retval < 0) {
Al Virof52e0c12011-03-14 18:56:51 -0400150 if (retval != -ENOENT || !(flags & LOOKUP_EMPTY)) {
151 __putname(tmp);
152 result = ERR_PTR(retval);
153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 }
155 }
156 audit_getname(result);
157 return result;
158}
159
Al Virof52e0c12011-03-14 18:56:51 -0400160char *getname(const char __user * filename)
161{
162 return getname_flags(filename, 0);
163}
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165#ifdef CONFIG_AUDITSYSCALL
166void putname(const char *name)
167{
Al Viro5ac3a9c2006-07-16 06:38:45 -0400168 if (unlikely(!audit_dummy_context()))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 audit_putname(name);
170 else
171 __putname(name);
172}
173EXPORT_SYMBOL(putname);
174#endif
175
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700176/*
177 * This does basic POSIX ACL permission checking
178 */
Nick Pigginb74c79e2011-01-07 17:49:58 +1100179static int acl_permission_check(struct inode *inode, int mask, unsigned int flags,
180 int (*check_acl)(struct inode *inode, int mask, unsigned int flags))
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700181{
Linus Torvalds26cf46b2011-05-13 11:51:01 -0700182 unsigned int mode = inode->i_mode;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700183
184 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
185
Serge E. Hallyne795b712011-03-23 16:43:25 -0700186 if (current_user_ns() != inode_userns(inode))
187 goto other_perms;
188
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700189 if (current_fsuid() == inode->i_uid)
190 mode >>= 6;
191 else {
192 if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
Nick Pigginb74c79e2011-01-07 17:49:58 +1100193 int error = check_acl(inode, mask, flags);
194 if (error != -EAGAIN)
195 return error;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700196 }
197
198 if (in_group_p(inode->i_gid))
199 mode >>= 3;
200 }
201
Serge E. Hallyne795b712011-03-23 16:43:25 -0700202other_perms:
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700203 /*
204 * If the DACs are ok we don't need any capability check.
205 */
206 if ((mask & ~mode) == 0)
207 return 0;
208 return -EACCES;
209}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211/**
Nick Pigginb74c79e2011-01-07 17:49:58 +1100212 * generic_permission - check for access rights on a Posix-like filesystem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 * @inode: inode to check access rights for
214 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
215 * @check_acl: optional callback to check for Posix ACLs
Randy Dunlap39191622011-01-08 19:36:21 -0800216 * @flags: IPERM_FLAG_ flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 *
218 * Used to check for read/write/execute permissions on a file.
219 * We use "fsuid" for this, letting us set arbitrary permissions
220 * for filesystem access without changing the "normal" uids which
Nick Pigginb74c79e2011-01-07 17:49:58 +1100221 * are used for other things.
222 *
223 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
224 * request cannot be satisfied (eg. requires blocking or too much complexity).
225 * It would then be called again in ref-walk mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 */
Nick Pigginb74c79e2011-01-07 17:49:58 +1100227int generic_permission(struct inode *inode, int mask, unsigned int flags,
228 int (*check_acl)(struct inode *inode, int mask, unsigned int flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700230 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 /*
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700233 * Do the basic POSIX ACL permission checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 */
Nick Pigginb74c79e2011-01-07 17:49:58 +1100235 ret = acl_permission_check(inode, mask, flags, check_acl);
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700236 if (ret != -EACCES)
237 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 /*
240 * Read/write DACs are always overridable.
Al Viro8e833fd2011-06-19 01:56:53 -0400241 * Executable DACs are overridable for all directories and
242 * for non-directories that have least one exec bit set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 */
Miklos Szeredif696a362008-07-31 13:41:58 +0200244 if (!(mask & MAY_EXEC) || execute_ok(inode))
Serge E. Hallyne795b712011-03-23 16:43:25 -0700245 if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return 0;
247
248 /*
249 * Searching includes executable on directories, else just read.
250 */
Serge E. Hallyn7ea66002009-12-29 14:50:19 -0600251 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
Serge E. Hallyne795b712011-03-23 16:43:25 -0700253 if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return 0;
255
256 return -EACCES;
257}
258
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200259/**
260 * inode_permission - check for access rights to a given inode
261 * @inode: inode to check permission on
262 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
263 *
264 * Used to check for read/write/execute permissions on an inode.
265 * We use "fsuid" for this, letting us set arbitrary permissions
266 * for filesystem access without changing the "normal" uids which
267 * are used for other things.
268 */
Al Virof419a2e2008-07-22 00:07:17 -0400269int inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Al Viroe6305c42008-07-15 21:03:57 -0400271 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 if (mask & MAY_WRITE) {
Miklos Szeredi22590e42007-10-16 23:27:08 -0700274 umode_t mode = inode->i_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 /*
277 * Nobody gets write access to a read-only fs.
278 */
279 if (IS_RDONLY(inode) &&
280 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
281 return -EROFS;
282
283 /*
284 * Nobody gets write access to an immutable file.
285 */
286 if (IS_IMMUTABLE(inode))
287 return -EACCES;
288 }
289
Al Viroacfa4382008-12-04 10:06:33 -0500290 if (inode->i_op->permission)
Nick Pigginb74c79e2011-01-07 17:49:58 +1100291 retval = inode->i_op->permission(inode, mask, 0);
Miklos Szeredif696a362008-07-31 13:41:58 +0200292 else
Nick Pigginb74c79e2011-01-07 17:49:58 +1100293 retval = generic_permission(inode, mask, 0,
294 inode->i_op->check_acl);
Miklos Szeredif696a362008-07-31 13:41:58 +0200295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 if (retval)
297 return retval;
298
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700299 retval = devcgroup_inode_permission(inode, mask);
300 if (retval)
301 return retval;
302
Eric Parisd09ca732010-07-23 11:43:57 -0400303 return security_inode_permission(inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800306/**
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800307 * file_permission - check for additional access rights to a given file
308 * @file: file to check access rights for
309 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
310 *
311 * Used to check for read/write/execute permissions on an already opened
312 * file.
313 *
314 * Note:
315 * Do not use this function in new code. All access checks should
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200316 * be done using inode_permission().
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800317 */
318int file_permission(struct file *file, int mask)
319{
Al Virof419a2e2008-07-22 00:07:17 -0400320 return inode_permission(file->f_path.dentry->d_inode, mask);
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800321}
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323/*
324 * get_write_access() gets write permission for a file.
325 * put_write_access() releases this write permission.
326 * This is used for regular files.
327 * We cannot support write (and maybe mmap read-write shared) accesses and
328 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
329 * can have the following values:
330 * 0: no writers, no VM_DENYWRITE mappings
331 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
332 * > 0: (i_writecount) users are writing to the file.
333 *
334 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
335 * except for the cases where we don't hold i_writecount yet. Then we need to
336 * use {get,deny}_write_access() - these functions check the sign and refuse
337 * to do the change if sign is wrong. Exclusion between them is provided by
338 * the inode->i_lock spinlock.
339 */
340
341int get_write_access(struct inode * inode)
342{
343 spin_lock(&inode->i_lock);
344 if (atomic_read(&inode->i_writecount) < 0) {
345 spin_unlock(&inode->i_lock);
346 return -ETXTBSY;
347 }
348 atomic_inc(&inode->i_writecount);
349 spin_unlock(&inode->i_lock);
350
351 return 0;
352}
353
354int deny_write_access(struct file * file)
355{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800356 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 spin_lock(&inode->i_lock);
359 if (atomic_read(&inode->i_writecount) > 0) {
360 spin_unlock(&inode->i_lock);
361 return -ETXTBSY;
362 }
363 atomic_dec(&inode->i_writecount);
364 spin_unlock(&inode->i_lock);
365
366 return 0;
367}
368
Jan Blunck1d957f92008-02-14 19:34:35 -0800369/**
Jan Blunck5dd784d02008-02-14 19:34:38 -0800370 * path_get - get a reference to a path
371 * @path: path to get the reference to
372 *
373 * Given a path increment the reference count to the dentry and the vfsmount.
374 */
375void path_get(struct path *path)
376{
377 mntget(path->mnt);
378 dget(path->dentry);
379}
380EXPORT_SYMBOL(path_get);
381
382/**
Jan Blunck1d957f92008-02-14 19:34:35 -0800383 * path_put - put a reference to a path
384 * @path: path to put the reference to
385 *
386 * Given a path decrement the reference count to the dentry and the vfsmount.
387 */
388void path_put(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
Jan Blunck1d957f92008-02-14 19:34:35 -0800390 dput(path->dentry);
391 mntput(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
Jan Blunck1d957f92008-02-14 19:34:35 -0800393EXPORT_SYMBOL(path_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Al Viro19660af2011-03-25 10:32:48 -0400395/*
Nick Piggin31e6b012011-01-07 17:49:52 +1100396 * Path walking has 2 modes, rcu-walk and ref-walk (see
Al Viro19660af2011-03-25 10:32:48 -0400397 * Documentation/filesystems/path-lookup.txt). In situations when we can't
398 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
399 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
400 * mode. Refcounts are grabbed at the last known good point before rcu-walk
401 * got stuck, so ref-walk may continue from there. If this is not successful
402 * (eg. a seqcount has changed), then failure is returned and it's up to caller
403 * to restart the path walk from the beginning in ref-walk mode.
Nick Piggin31e6b012011-01-07 17:49:52 +1100404 */
Nick Piggin31e6b012011-01-07 17:49:52 +1100405
406/**
Al Viro19660af2011-03-25 10:32:48 -0400407 * unlazy_walk - try to switch to ref-walk mode.
408 * @nd: nameidata pathwalk data
409 * @dentry: child of nd->path.dentry or NULL
Randy Dunlap39191622011-01-08 19:36:21 -0800410 * Returns: 0 on success, -ECHILD on failure
Nick Piggin31e6b012011-01-07 17:49:52 +1100411 *
Al Viro19660af2011-03-25 10:32:48 -0400412 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
413 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
414 * @nd or NULL. Must be called from rcu-walk context.
Nick Piggin31e6b012011-01-07 17:49:52 +1100415 */
Al Viro19660af2011-03-25 10:32:48 -0400416static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
Nick Piggin31e6b012011-01-07 17:49:52 +1100417{
418 struct fs_struct *fs = current->fs;
419 struct dentry *parent = nd->path.dentry;
Al Viro5b6ca022011-03-09 23:04:47 -0500420 int want_root = 0;
Nick Piggin31e6b012011-01-07 17:49:52 +1100421
422 BUG_ON(!(nd->flags & LOOKUP_RCU));
Al Viro5b6ca022011-03-09 23:04:47 -0500423 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
424 want_root = 1;
Nick Piggin31e6b012011-01-07 17:49:52 +1100425 spin_lock(&fs->lock);
426 if (nd->root.mnt != fs->root.mnt ||
427 nd->root.dentry != fs->root.dentry)
428 goto err_root;
429 }
430 spin_lock(&parent->d_lock);
Al Viro19660af2011-03-25 10:32:48 -0400431 if (!dentry) {
432 if (!__d_rcu_to_refcount(parent, nd->seq))
433 goto err_parent;
434 BUG_ON(nd->inode != parent->d_inode);
435 } else {
Al Viro94c0d4e2011-07-12 21:40:23 -0400436 if (dentry->d_parent != parent)
437 goto err_parent;
Al Viro19660af2011-03-25 10:32:48 -0400438 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
439 if (!__d_rcu_to_refcount(dentry, nd->seq))
440 goto err_child;
441 /*
442 * If the sequence check on the child dentry passed, then
443 * the child has not been removed from its parent. This
444 * means the parent dentry must be valid and able to take
445 * a reference at this point.
446 */
447 BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
448 BUG_ON(!parent->d_count);
449 parent->d_count++;
450 spin_unlock(&dentry->d_lock);
451 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100452 spin_unlock(&parent->d_lock);
Al Viro5b6ca022011-03-09 23:04:47 -0500453 if (want_root) {
Nick Piggin31e6b012011-01-07 17:49:52 +1100454 path_get(&nd->root);
455 spin_unlock(&fs->lock);
456 }
457 mntget(nd->path.mnt);
458
459 rcu_read_unlock();
460 br_read_unlock(vfsmount_lock);
461 nd->flags &= ~LOOKUP_RCU;
462 return 0;
Al Viro19660af2011-03-25 10:32:48 -0400463
464err_child:
Nick Piggin31e6b012011-01-07 17:49:52 +1100465 spin_unlock(&dentry->d_lock);
Al Viro19660af2011-03-25 10:32:48 -0400466err_parent:
Nick Piggin31e6b012011-01-07 17:49:52 +1100467 spin_unlock(&parent->d_lock);
468err_root:
Al Viro5b6ca022011-03-09 23:04:47 -0500469 if (want_root)
Nick Piggin31e6b012011-01-07 17:49:52 +1100470 spin_unlock(&fs->lock);
471 return -ECHILD;
472}
473
Nick Piggin31e6b012011-01-07 17:49:52 +1100474/**
Trond Myklebust834f2a42005-10-18 14:20:16 -0700475 * release_open_intent - free up open intent resources
476 * @nd: pointer to nameidata
477 */
478void release_open_intent(struct nameidata *nd)
479{
Linus Torvalds2dab5972011-02-11 15:53:38 -0800480 struct file *file = nd->intent.open.file;
481
482 if (file && !IS_ERR(file)) {
483 if (file->f_path.dentry == NULL)
484 put_filp(file);
485 else
486 fput(file);
487 }
Trond Myklebust834f2a42005-10-18 14:20:16 -0700488}
489
Al Virof60aef72011-02-15 01:35:28 -0500490static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
Nick Piggin34286d62011-01-07 17:49:57 +1100491{
Al Virof60aef72011-02-15 01:35:28 -0500492 return dentry->d_op->d_revalidate(dentry, nd);
Nick Piggin34286d62011-01-07 17:49:57 +1100493}
494
Al Virof5e1c1c2011-02-15 01:32:55 -0500495static struct dentry *
Ian Kentbcdc5e02006-09-27 01:50:44 -0700496do_revalidate(struct dentry *dentry, struct nameidata *nd)
497{
Al Virof5e1c1c2011-02-15 01:32:55 -0500498 int status = d_revalidate(dentry, nd);
Ian Kentbcdc5e02006-09-27 01:50:44 -0700499 if (unlikely(status <= 0)) {
500 /*
501 * The dentry failed validation.
502 * If d_revalidate returned 0 attempt to invalidate
503 * the dentry otherwise d_revalidate is asking us
504 * to return a fail status.
505 */
Nick Piggin34286d62011-01-07 17:49:57 +1100506 if (status < 0) {
Al Virof5e1c1c2011-02-15 01:32:55 -0500507 dput(dentry);
Nick Piggin34286d62011-01-07 17:49:57 +1100508 dentry = ERR_PTR(status);
Al Virof5e1c1c2011-02-15 01:32:55 -0500509 } else if (!d_invalidate(dentry)) {
510 dput(dentry);
511 dentry = NULL;
Ian Kentbcdc5e02006-09-27 01:50:44 -0700512 }
513 }
514 return dentry;
515}
516
Al Viro9f1fafe2011-03-25 11:00:12 -0400517/**
518 * complete_walk - successful completion of path walk
519 * @nd: pointer nameidata
Jeff Layton39159de2009-12-07 12:01:50 -0500520 *
Al Viro9f1fafe2011-03-25 11:00:12 -0400521 * If we had been in RCU mode, drop out of it and legitimize nd->path.
522 * Revalidate the final result, unless we'd already done that during
523 * the path walk or the filesystem doesn't ask for it. Return 0 on
524 * success, -error on failure. In case of failure caller does not
525 * need to drop nd->path.
Jeff Layton39159de2009-12-07 12:01:50 -0500526 */
Al Viro9f1fafe2011-03-25 11:00:12 -0400527static int complete_walk(struct nameidata *nd)
Jeff Layton39159de2009-12-07 12:01:50 -0500528{
Al Viro16c2cd72011-02-22 15:50:10 -0500529 struct dentry *dentry = nd->path.dentry;
Jeff Layton39159de2009-12-07 12:01:50 -0500530 int status;
Jeff Layton39159de2009-12-07 12:01:50 -0500531
Al Viro9f1fafe2011-03-25 11:00:12 -0400532 if (nd->flags & LOOKUP_RCU) {
533 nd->flags &= ~LOOKUP_RCU;
534 if (!(nd->flags & LOOKUP_ROOT))
535 nd->root.mnt = NULL;
536 spin_lock(&dentry->d_lock);
537 if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
538 spin_unlock(&dentry->d_lock);
539 rcu_read_unlock();
540 br_read_unlock(vfsmount_lock);
541 return -ECHILD;
542 }
543 BUG_ON(nd->inode != dentry->d_inode);
544 spin_unlock(&dentry->d_lock);
545 mntget(nd->path.mnt);
546 rcu_read_unlock();
547 br_read_unlock(vfsmount_lock);
548 }
549
Al Viro16c2cd72011-02-22 15:50:10 -0500550 if (likely(!(nd->flags & LOOKUP_JUMPED)))
Jeff Layton39159de2009-12-07 12:01:50 -0500551 return 0;
552
Al Viro16c2cd72011-02-22 15:50:10 -0500553 if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
554 return 0;
555
556 if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
557 return 0;
558
559 /* Note: we do not d_invalidate() */
Nick Piggin34286d62011-01-07 17:49:57 +1100560 status = d_revalidate(dentry, nd);
Jeff Layton39159de2009-12-07 12:01:50 -0500561 if (status > 0)
562 return 0;
563
Al Viro16c2cd72011-02-22 15:50:10 -0500564 if (!status)
Jeff Layton39159de2009-12-07 12:01:50 -0500565 status = -ESTALE;
Al Viro16c2cd72011-02-22 15:50:10 -0500566
Al Viro9f1fafe2011-03-25 11:00:12 -0400567 path_put(&nd->path);
Jeff Layton39159de2009-12-07 12:01:50 -0500568 return status;
569}
570
571/*
Al Virob75b5082009-12-16 01:01:38 -0500572 * Short-cut version of permission(), for calling on directories
573 * during pathname resolution. Combines parts of permission()
574 * and generic_permission(), and tests ONLY for MAY_EXEC permission.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 *
576 * If appropriate, check DAC only. If not appropriate, or
Al Virob75b5082009-12-16 01:01:38 -0500577 * short-cut DAC fails, then call ->permission() to do more
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 * complete permission check.
579 */
Nick Pigginb74c79e2011-01-07 17:49:58 +1100580static inline int exec_permission(struct inode *inode, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700582 int ret;
Serge E. Hallyne795b712011-03-23 16:43:25 -0700583 struct user_namespace *ns = inode_userns(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Linus Torvaldscb9179e2009-08-28 11:08:31 -0700585 if (inode->i_op->permission) {
Nick Pigginb74c79e2011-01-07 17:49:58 +1100586 ret = inode->i_op->permission(inode, MAY_EXEC, flags);
587 } else {
588 ret = acl_permission_check(inode, MAY_EXEC, flags,
589 inode->i_op->check_acl);
Linus Torvaldscb9179e2009-08-28 11:08:31 -0700590 }
Nick Pigginb74c79e2011-01-07 17:49:58 +1100591 if (likely(!ret))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 goto ok;
Nick Pigginb74c79e2011-01-07 17:49:58 +1100593 if (ret == -ECHILD)
Nick Piggin31e6b012011-01-07 17:49:52 +1100594 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Serge E. Hallyne795b712011-03-23 16:43:25 -0700596 if (ns_capable(ns, CAP_DAC_OVERRIDE) ||
597 ns_capable(ns, CAP_DAC_READ_SEARCH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 goto ok;
599
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700600 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601ok:
Nick Pigginb74c79e2011-01-07 17:49:58 +1100602 return security_inode_exec_permission(inode, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
604
Al Viro2a737872009-04-07 11:49:53 -0400605static __always_inline void set_root(struct nameidata *nd)
606{
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200607 if (!nd->root.mnt)
608 get_fs_root(current->fs, &nd->root);
Al Viro2a737872009-04-07 11:49:53 -0400609}
610
Al Viro6de88d72009-08-09 01:41:57 +0400611static int link_path_walk(const char *, struct nameidata *);
612
Nick Piggin31e6b012011-01-07 17:49:52 +1100613static __always_inline void set_root_rcu(struct nameidata *nd)
614{
615 if (!nd->root.mnt) {
616 struct fs_struct *fs = current->fs;
Nick Pigginc28cc362011-01-07 17:49:53 +1100617 unsigned seq;
618
619 do {
620 seq = read_seqcount_begin(&fs->seq);
621 nd->root = fs->root;
Tim Chenc1530012011-04-15 11:39:29 -0700622 nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
Nick Pigginc28cc362011-01-07 17:49:53 +1100623 } while (read_seqcount_retry(&fs->seq, seq));
Nick Piggin31e6b012011-01-07 17:49:52 +1100624 }
625}
626
Arjan van de Venf1662352006-01-14 13:21:31 -0800627static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Nick Piggin31e6b012011-01-07 17:49:52 +1100629 int ret;
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 if (IS_ERR(link))
632 goto fail;
633
634 if (*link == '/') {
Al Viro2a737872009-04-07 11:49:53 -0400635 set_root(nd);
Jan Blunck1d957f92008-02-14 19:34:35 -0800636 path_put(&nd->path);
Al Viro2a737872009-04-07 11:49:53 -0400637 nd->path = nd->root;
638 path_get(&nd->root);
Al Viro16c2cd72011-02-22 15:50:10 -0500639 nd->flags |= LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100641 nd->inode = nd->path.dentry->d_inode;
Christoph Hellwigb4091d52008-11-05 15:07:21 +0100642
Nick Piggin31e6b012011-01-07 17:49:52 +1100643 ret = link_path_walk(link, nd);
644 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645fail:
Jan Blunck1d957f92008-02-14 19:34:35 -0800646 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 return PTR_ERR(link);
648}
649
Jan Blunck1d957f92008-02-14 19:34:35 -0800650static void path_put_conditional(struct path *path, struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700651{
652 dput(path->dentry);
Jan Blunck4ac91372008-02-14 19:34:32 -0800653 if (path->mnt != nd->path.mnt)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700654 mntput(path->mnt);
655}
656
Nick Piggin7b9337a2011-01-14 08:42:43 +0000657static inline void path_to_nameidata(const struct path *path,
658 struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700659{
Nick Piggin31e6b012011-01-07 17:49:52 +1100660 if (!(nd->flags & LOOKUP_RCU)) {
661 dput(nd->path.dentry);
662 if (nd->path.mnt != path->mnt)
663 mntput(nd->path.mnt);
Huang Shijie9a229682010-04-02 17:37:13 +0800664 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100665 nd->path.mnt = path->mnt;
Jan Blunck4ac91372008-02-14 19:34:32 -0800666 nd->path.dentry = path->dentry;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700667}
668
Al Viro574197e2011-03-14 22:20:34 -0400669static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
670{
671 struct inode *inode = link->dentry->d_inode;
672 if (!IS_ERR(cookie) && inode->i_op->put_link)
673 inode->i_op->put_link(link->dentry, nd, cookie);
674 path_put(link);
675}
676
Al Virodef4af32009-12-26 08:37:05 -0500677static __always_inline int
Al Viro574197e2011-03-14 22:20:34 -0400678follow_link(struct path *link, struct nameidata *nd, void **p)
Ian Kent051d3812006-03-27 01:14:53 -0800679{
680 int error;
Nick Piggin7b9337a2011-01-14 08:42:43 +0000681 struct dentry *dentry = link->dentry;
Ian Kent051d3812006-03-27 01:14:53 -0800682
Al Viro844a3912011-02-15 00:38:26 -0500683 BUG_ON(nd->flags & LOOKUP_RCU);
684
Al Viro0e794582011-03-16 02:45:02 -0400685 if (link->mnt == nd->path.mnt)
686 mntget(link->mnt);
687
Al Viro574197e2011-03-14 22:20:34 -0400688 if (unlikely(current->total_link_count >= 40)) {
689 *p = ERR_PTR(-ELOOP); /* no ->put_link(), please */
Al Viro574197e2011-03-14 22:20:34 -0400690 path_put(&nd->path);
691 return -ELOOP;
692 }
693 cond_resched();
694 current->total_link_count++;
695
Nick Piggin7b9337a2011-01-14 08:42:43 +0000696 touch_atime(link->mnt, dentry);
Ian Kent051d3812006-03-27 01:14:53 -0800697 nd_set_link(nd, NULL);
698
Al Viro36f3b4f2011-02-22 21:24:38 -0500699 error = security_inode_follow_link(link->dentry, nd);
700 if (error) {
701 *p = ERR_PTR(error); /* no ->put_link(), please */
702 path_put(&nd->path);
703 return error;
704 }
705
Al Viro86acdca12009-12-22 23:45:11 -0500706 nd->last_type = LAST_BIND;
Al Virodef4af32009-12-26 08:37:05 -0500707 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
708 error = PTR_ERR(*p);
709 if (!IS_ERR(*p)) {
Ian Kent051d3812006-03-27 01:14:53 -0800710 char *s = nd_get_link(nd);
711 error = 0;
712 if (s)
713 error = __vfs_follow_link(nd, s);
Al Virobcda7652011-03-13 16:42:14 -0400714 else if (nd->last_type == LAST_BIND) {
Al Viro16c2cd72011-02-22 15:50:10 -0500715 nd->flags |= LOOKUP_JUMPED;
Al Virob21041d2011-03-14 20:01:51 -0400716 nd->inode = nd->path.dentry->d_inode;
717 if (nd->inode->i_op->follow_link) {
Al Virobcda7652011-03-13 16:42:14 -0400718 /* stepped on a _really_ weird one */
719 path_put(&nd->path);
720 error = -ELOOP;
721 }
722 }
Ian Kent051d3812006-03-27 01:14:53 -0800723 }
Ian Kent051d3812006-03-27 01:14:53 -0800724 return error;
725}
726
Nick Piggin31e6b012011-01-07 17:49:52 +1100727static int follow_up_rcu(struct path *path)
728{
729 struct vfsmount *parent;
730 struct dentry *mountpoint;
731
732 parent = path->mnt->mnt_parent;
733 if (parent == path->mnt)
734 return 0;
735 mountpoint = path->mnt->mnt_mountpoint;
736 path->dentry = mountpoint;
737 path->mnt = parent;
738 return 1;
739}
740
Al Virobab77eb2009-04-18 03:26:48 -0400741int follow_up(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
743 struct vfsmount *parent;
744 struct dentry *mountpoint;
Nick Piggin99b7db72010-08-18 04:37:39 +1000745
746 br_read_lock(vfsmount_lock);
Al Virobab77eb2009-04-18 03:26:48 -0400747 parent = path->mnt->mnt_parent;
748 if (parent == path->mnt) {
Nick Piggin99b7db72010-08-18 04:37:39 +1000749 br_read_unlock(vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 return 0;
751 }
752 mntget(parent);
Al Virobab77eb2009-04-18 03:26:48 -0400753 mountpoint = dget(path->mnt->mnt_mountpoint);
Nick Piggin99b7db72010-08-18 04:37:39 +1000754 br_read_unlock(vfsmount_lock);
Al Virobab77eb2009-04-18 03:26:48 -0400755 dput(path->dentry);
756 path->dentry = mountpoint;
757 mntput(path->mnt);
758 path->mnt = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 return 1;
760}
761
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100762/*
David Howells9875cf82011-01-14 18:45:21 +0000763 * Perform an automount
764 * - return -EISDIR to tell follow_managed() to stop and return the path we
765 * were called with.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 */
David Howells9875cf82011-01-14 18:45:21 +0000767static int follow_automount(struct path *path, unsigned flags,
768 bool *need_mntput)
Nick Piggin31e6b012011-01-07 17:49:52 +1100769{
David Howells9875cf82011-01-14 18:45:21 +0000770 struct vfsmount *mnt;
David Howellsea5b7782011-01-14 19:10:03 +0000771 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +1100772
David Howells9875cf82011-01-14 18:45:21 +0000773 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
774 return -EREMOTE;
Al Viro463ffb22005-06-06 13:36:05 -0700775
David Howells6f45b652011-01-14 18:45:31 +0000776 /* We don't want to mount if someone supplied AT_NO_AUTOMOUNT
777 * and this is the terminal part of the path.
778 */
779 if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_CONTINUE))
780 return -EISDIR; /* we actually want to stop here */
781
David Howells9875cf82011-01-14 18:45:21 +0000782 /* We want to mount if someone is trying to open/create a file of any
783 * type under the mountpoint, wants to traverse through the mountpoint
784 * or wants to open the mounted directory.
785 *
786 * We don't want to mount if someone's just doing a stat and they've
787 * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and
788 * appended a '/' to the name.
789 */
790 if (!(flags & LOOKUP_FOLLOW) &&
791 !(flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY |
792 LOOKUP_OPEN | LOOKUP_CREATE)))
793 return -EISDIR;
794
795 current->total_link_count++;
796 if (current->total_link_count >= 40)
797 return -ELOOP;
798
799 mnt = path->dentry->d_op->d_automount(path);
800 if (IS_ERR(mnt)) {
801 /*
802 * The filesystem is allowed to return -EISDIR here to indicate
803 * it doesn't want to automount. For instance, autofs would do
804 * this so that its userspace daemon can mount on this dentry.
805 *
806 * However, we can only permit this if it's a terminal point in
807 * the path being looked up; if it wasn't then the remainder of
808 * the path is inaccessible and we should say so.
809 */
810 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_CONTINUE))
811 return -EREMOTE;
812 return PTR_ERR(mnt);
813 }
David Howellsea5b7782011-01-14 19:10:03 +0000814
David Howells9875cf82011-01-14 18:45:21 +0000815 if (!mnt) /* mount collision */
816 return 0;
817
Al Viro8aef1882011-06-16 15:10:06 +0100818 if (!*need_mntput) {
819 /* lock_mount() may release path->mnt on error */
820 mntget(path->mnt);
821 *need_mntput = true;
822 }
Al Viro19a167a2011-01-17 01:35:23 -0500823 err = finish_automount(mnt, path);
David Howellsea5b7782011-01-14 19:10:03 +0000824
David Howellsea5b7782011-01-14 19:10:03 +0000825 switch (err) {
826 case -EBUSY:
827 /* Someone else made a mount here whilst we were busy */
Al Viro19a167a2011-01-17 01:35:23 -0500828 return 0;
David Howellsea5b7782011-01-14 19:10:03 +0000829 case 0:
Al Viro8aef1882011-06-16 15:10:06 +0100830 path_put(path);
David Howellsea5b7782011-01-14 19:10:03 +0000831 path->mnt = mnt;
832 path->dentry = dget(mnt->mnt_root);
David Howellsea5b7782011-01-14 19:10:03 +0000833 return 0;
Al Viro19a167a2011-01-17 01:35:23 -0500834 default:
835 return err;
David Howellsea5b7782011-01-14 19:10:03 +0000836 }
Al Viro19a167a2011-01-17 01:35:23 -0500837
David Howells9875cf82011-01-14 18:45:21 +0000838}
839
840/*
841 * Handle a dentry that is managed in some way.
David Howellscc53ce52011-01-14 18:45:26 +0000842 * - Flagged for transit management (autofs)
David Howells9875cf82011-01-14 18:45:21 +0000843 * - Flagged as mountpoint
844 * - Flagged as automount point
845 *
846 * This may only be called in refwalk mode.
847 *
848 * Serialization is taken care of in namespace.c
849 */
850static int follow_managed(struct path *path, unsigned flags)
851{
Al Viro8aef1882011-06-16 15:10:06 +0100852 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
David Howells9875cf82011-01-14 18:45:21 +0000853 unsigned managed;
854 bool need_mntput = false;
Al Viro8aef1882011-06-16 15:10:06 +0100855 int ret = 0;
David Howells9875cf82011-01-14 18:45:21 +0000856
857 /* Given that we're not holding a lock here, we retain the value in a
858 * local variable for each dentry as we look at it so that we don't see
859 * the components of that value change under us */
860 while (managed = ACCESS_ONCE(path->dentry->d_flags),
861 managed &= DCACHE_MANAGED_DENTRY,
862 unlikely(managed != 0)) {
David Howellscc53ce52011-01-14 18:45:26 +0000863 /* Allow the filesystem to manage the transit without i_mutex
864 * being held. */
865 if (managed & DCACHE_MANAGE_TRANSIT) {
866 BUG_ON(!path->dentry->d_op);
867 BUG_ON(!path->dentry->d_op->d_manage);
Al Viro1aed3e42011-03-18 09:09:02 -0400868 ret = path->dentry->d_op->d_manage(path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +0000869 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +0100870 break;
David Howellscc53ce52011-01-14 18:45:26 +0000871 }
872
David Howells9875cf82011-01-14 18:45:21 +0000873 /* Transit to a mounted filesystem. */
874 if (managed & DCACHE_MOUNTED) {
875 struct vfsmount *mounted = lookup_mnt(path);
876 if (mounted) {
877 dput(path->dentry);
878 if (need_mntput)
879 mntput(path->mnt);
880 path->mnt = mounted;
881 path->dentry = dget(mounted->mnt_root);
882 need_mntput = true;
883 continue;
884 }
885
886 /* Something is mounted on this dentry in another
887 * namespace and/or whatever was mounted there in this
888 * namespace got unmounted before we managed to get the
889 * vfsmount_lock */
890 }
891
892 /* Handle an automount point */
893 if (managed & DCACHE_NEED_AUTOMOUNT) {
894 ret = follow_automount(path, flags, &need_mntput);
895 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +0100896 break;
David Howells9875cf82011-01-14 18:45:21 +0000897 continue;
898 }
899
900 /* We didn't change the current path point */
901 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
Al Viro8aef1882011-06-16 15:10:06 +0100903
904 if (need_mntput && path->mnt == mnt)
905 mntput(path->mnt);
906 if (ret == -EISDIR)
907 ret = 0;
908 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910
David Howellscc53ce52011-01-14 18:45:26 +0000911int follow_down_one(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
913 struct vfsmount *mounted;
914
Al Viro1c755af2009-04-18 14:06:57 -0400915 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -0400917 dput(path->dentry);
918 mntput(path->mnt);
919 path->mnt = mounted;
920 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return 1;
922 }
923 return 0;
924}
925
Ian Kent62a73752011-03-25 01:51:02 +0800926static inline bool managed_dentry_might_block(struct dentry *dentry)
927{
928 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
929 dentry->d_op->d_manage(dentry, true) < 0);
930}
931
David Howells9875cf82011-01-14 18:45:21 +0000932/*
Al Viro287548e2011-05-27 06:50:06 -0400933 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
934 * we meet a managed dentry that would need blocking.
David Howells9875cf82011-01-14 18:45:21 +0000935 */
936static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
Al Viro287548e2011-05-27 06:50:06 -0400937 struct inode **inode)
David Howells9875cf82011-01-14 18:45:21 +0000938{
Ian Kent62a73752011-03-25 01:51:02 +0800939 for (;;) {
David Howells9875cf82011-01-14 18:45:21 +0000940 struct vfsmount *mounted;
Ian Kent62a73752011-03-25 01:51:02 +0800941 /*
942 * Don't forget we might have a non-mountpoint managed dentry
943 * that wants to block transit.
944 */
945 *inode = path->dentry->d_inode;
Al Viro287548e2011-05-27 06:50:06 -0400946 if (unlikely(managed_dentry_might_block(path->dentry)))
David Howellsab909112011-01-14 18:46:51 +0000947 return false;
Ian Kent62a73752011-03-25 01:51:02 +0800948
949 if (!d_mountpoint(path->dentry))
950 break;
951
David Howells9875cf82011-01-14 18:45:21 +0000952 mounted = __lookup_mnt(path->mnt, path->dentry, 1);
953 if (!mounted)
954 break;
955 path->mnt = mounted;
956 path->dentry = mounted->mnt_root;
957 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
David Howells9875cf82011-01-14 18:45:21 +0000958 }
David Howells9875cf82011-01-14 18:45:21 +0000959 return true;
960}
961
Al Virodea39372011-05-27 06:53:39 -0400962static void follow_mount_rcu(struct nameidata *nd)
Al Viro287548e2011-05-27 06:50:06 -0400963{
Al Virodea39372011-05-27 06:53:39 -0400964 while (d_mountpoint(nd->path.dentry)) {
Al Viro287548e2011-05-27 06:50:06 -0400965 struct vfsmount *mounted;
Al Virodea39372011-05-27 06:53:39 -0400966 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
Al Viro287548e2011-05-27 06:50:06 -0400967 if (!mounted)
968 break;
Al Virodea39372011-05-27 06:53:39 -0400969 nd->path.mnt = mounted;
970 nd->path.dentry = mounted->mnt_root;
971 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro287548e2011-05-27 06:50:06 -0400972 }
973}
974
Nick Piggin31e6b012011-01-07 17:49:52 +1100975static int follow_dotdot_rcu(struct nameidata *nd)
976{
Nick Piggin31e6b012011-01-07 17:49:52 +1100977 set_root_rcu(nd);
978
David Howells9875cf82011-01-14 18:45:21 +0000979 while (1) {
Nick Piggin31e6b012011-01-07 17:49:52 +1100980 if (nd->path.dentry == nd->root.dentry &&
981 nd->path.mnt == nd->root.mnt) {
982 break;
983 }
984 if (nd->path.dentry != nd->path.mnt->mnt_root) {
985 struct dentry *old = nd->path.dentry;
986 struct dentry *parent = old->d_parent;
987 unsigned seq;
988
989 seq = read_seqcount_begin(&parent->d_seq);
990 if (read_seqcount_retry(&old->d_seq, nd->seq))
Al Viroef7562d2011-03-04 14:35:59 -0500991 goto failed;
Nick Piggin31e6b012011-01-07 17:49:52 +1100992 nd->path.dentry = parent;
993 nd->seq = seq;
994 break;
995 }
996 if (!follow_up_rcu(&nd->path))
997 break;
998 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Nick Piggin31e6b012011-01-07 17:49:52 +1100999 }
Al Virodea39372011-05-27 06:53:39 -04001000 follow_mount_rcu(nd);
1001 nd->inode = nd->path.dentry->d_inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001002 return 0;
Al Viroef7562d2011-03-04 14:35:59 -05001003
1004failed:
1005 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001006 if (!(nd->flags & LOOKUP_ROOT))
1007 nd->root.mnt = NULL;
Al Viroef7562d2011-03-04 14:35:59 -05001008 rcu_read_unlock();
1009 br_read_unlock(vfsmount_lock);
1010 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001011}
1012
David Howells9875cf82011-01-14 18:45:21 +00001013/*
David Howellscc53ce52011-01-14 18:45:26 +00001014 * Follow down to the covering mount currently visible to userspace. At each
1015 * point, the filesystem owning that dentry may be queried as to whether the
1016 * caller is permitted to proceed or not.
David Howellscc53ce52011-01-14 18:45:26 +00001017 */
Al Viro7cc90cc2011-03-18 09:04:20 -04001018int follow_down(struct path *path)
David Howellscc53ce52011-01-14 18:45:26 +00001019{
1020 unsigned managed;
1021 int ret;
1022
1023 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1024 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1025 /* Allow the filesystem to manage the transit without i_mutex
1026 * being held.
1027 *
1028 * We indicate to the filesystem if someone is trying to mount
1029 * something here. This gives autofs the chance to deny anyone
1030 * other than its daemon the right to mount on its
1031 * superstructure.
1032 *
1033 * The filesystem may sleep at this point.
1034 */
1035 if (managed & DCACHE_MANAGE_TRANSIT) {
1036 BUG_ON(!path->dentry->d_op);
1037 BUG_ON(!path->dentry->d_op->d_manage);
David Howellsab909112011-01-14 18:46:51 +00001038 ret = path->dentry->d_op->d_manage(
Al Viro1aed3e42011-03-18 09:09:02 -04001039 path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +00001040 if (ret < 0)
1041 return ret == -EISDIR ? 0 : ret;
1042 }
1043
1044 /* Transit to a mounted filesystem. */
1045 if (managed & DCACHE_MOUNTED) {
1046 struct vfsmount *mounted = lookup_mnt(path);
1047 if (!mounted)
1048 break;
1049 dput(path->dentry);
1050 mntput(path->mnt);
1051 path->mnt = mounted;
1052 path->dentry = dget(mounted->mnt_root);
1053 continue;
1054 }
1055
1056 /* Don't handle automount points here */
1057 break;
1058 }
1059 return 0;
1060}
1061
1062/*
David Howells9875cf82011-01-14 18:45:21 +00001063 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1064 */
1065static void follow_mount(struct path *path)
1066{
1067 while (d_mountpoint(path->dentry)) {
1068 struct vfsmount *mounted = lookup_mnt(path);
1069 if (!mounted)
1070 break;
1071 dput(path->dentry);
1072 mntput(path->mnt);
1073 path->mnt = mounted;
1074 path->dentry = dget(mounted->mnt_root);
1075 }
1076}
1077
Nick Piggin31e6b012011-01-07 17:49:52 +11001078static void follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
Al Viro2a737872009-04-07 11:49:53 -04001080 set_root(nd);
Andreas Mohre518ddb2006-09-29 02:01:22 -07001081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 while(1) {
Jan Blunck4ac91372008-02-14 19:34:32 -08001083 struct dentry *old = nd->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Al Viro2a737872009-04-07 11:49:53 -04001085 if (nd->path.dentry == nd->root.dentry &&
1086 nd->path.mnt == nd->root.mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 break;
1088 }
Jan Blunck4ac91372008-02-14 19:34:32 -08001089 if (nd->path.dentry != nd->path.mnt->mnt_root) {
Al Viro3088dd72010-01-30 15:47:29 -05001090 /* rare case of legitimate dget_parent()... */
1091 nd->path.dentry = dget_parent(nd->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 dput(old);
1093 break;
1094 }
Al Viro3088dd72010-01-30 15:47:29 -05001095 if (!follow_up(&nd->path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 }
Al Viro79ed0222009-04-18 13:59:41 -04001098 follow_mount(&nd->path);
Nick Piggin31e6b012011-01-07 17:49:52 +11001099 nd->inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100}
1101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102/*
Nick Pigginbaa03892010-08-18 04:37:31 +10001103 * Allocate a dentry with name and parent, and perform a parent
1104 * directory ->lookup on it. Returns the new dentry, or ERR_PTR
1105 * on error. parent->d_inode->i_mutex must be held. d_lookup must
1106 * have verified that no child exists while under i_mutex.
1107 */
1108static struct dentry *d_alloc_and_lookup(struct dentry *parent,
1109 struct qstr *name, struct nameidata *nd)
1110{
1111 struct inode *inode = parent->d_inode;
1112 struct dentry *dentry;
1113 struct dentry *old;
1114
1115 /* Don't create child dentry for a dead directory. */
1116 if (unlikely(IS_DEADDIR(inode)))
1117 return ERR_PTR(-ENOENT);
1118
1119 dentry = d_alloc(parent, name);
1120 if (unlikely(!dentry))
1121 return ERR_PTR(-ENOMEM);
1122
1123 old = inode->i_op->lookup(inode, dentry, nd);
1124 if (unlikely(old)) {
1125 dput(dentry);
1126 dentry = old;
1127 }
1128 return dentry;
1129}
1130
1131/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 * It's more convoluted than I'd like it to be, but... it's still fairly
1133 * small and for now I'd prefer to have fast path as straight as possible.
1134 * It _is_ time-critical.
1135 */
1136static int do_lookup(struct nameidata *nd, struct qstr *name,
Nick Piggin31e6b012011-01-07 17:49:52 +11001137 struct path *path, struct inode **inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
Jan Blunck4ac91372008-02-14 19:34:32 -08001139 struct vfsmount *mnt = nd->path.mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +11001140 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro5a18fff2011-03-11 04:44:53 -05001141 int need_reval = 1;
1142 int status = 1;
David Howells9875cf82011-01-14 18:45:21 +00001143 int err;
1144
Al Viro3cac2602009-08-13 18:27:43 +04001145 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001146 * Rename seqlock is not required here because in the off chance
1147 * of a false negative due to a concurrent rename, we're going to
1148 * do the non-racy lookup, below.
1149 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001150 if (nd->flags & LOOKUP_RCU) {
1151 unsigned seq;
Nick Piggin31e6b012011-01-07 17:49:52 +11001152 *inode = nd->inode;
1153 dentry = __d_lookup_rcu(parent, name, &seq, inode);
Al Viro5a18fff2011-03-11 04:44:53 -05001154 if (!dentry)
1155 goto unlazy;
1156
Nick Piggin31e6b012011-01-07 17:49:52 +11001157 /* Memory barrier in read_seqcount_begin of child is enough */
1158 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1159 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001160 nd->seq = seq;
Al Viro5a18fff2011-03-11 04:44:53 -05001161
Al Viro24643082011-02-15 01:26:22 -05001162 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
Al Viro5a18fff2011-03-11 04:44:53 -05001163 status = d_revalidate(dentry, nd);
1164 if (unlikely(status <= 0)) {
1165 if (status != -ECHILD)
1166 need_reval = 0;
1167 goto unlazy;
1168 }
Al Viro24643082011-02-15 01:26:22 -05001169 }
Nick Piggin31e6b012011-01-07 17:49:52 +11001170 path->mnt = mnt;
1171 path->dentry = dentry;
Al Virod6e9bd22011-05-27 07:03:15 -04001172 if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1173 goto unlazy;
1174 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1175 goto unlazy;
1176 return 0;
Al Viro5a18fff2011-03-11 04:44:53 -05001177unlazy:
Al Viro19660af2011-03-25 10:32:48 -04001178 if (unlazy_walk(nd, dentry))
1179 return -ECHILD;
Al Viro5a18fff2011-03-11 04:44:53 -05001180 } else {
1181 dentry = __d_lookup(parent, name);
Nick Piggin31e6b012011-01-07 17:49:52 +11001182 }
Al Viro5a18fff2011-03-11 04:44:53 -05001183
1184retry:
1185 if (unlikely(!dentry)) {
1186 struct inode *dir = parent->d_inode;
1187 BUG_ON(nd->inode != dir);
1188
1189 mutex_lock(&dir->i_mutex);
1190 dentry = d_lookup(parent, name);
1191 if (likely(!dentry)) {
1192 dentry = d_alloc_and_lookup(parent, name, nd);
1193 if (IS_ERR(dentry)) {
1194 mutex_unlock(&dir->i_mutex);
1195 return PTR_ERR(dentry);
1196 }
1197 /* known good */
1198 need_reval = 0;
1199 status = 1;
1200 }
1201 mutex_unlock(&dir->i_mutex);
Al Viro24643082011-02-15 01:26:22 -05001202 }
Al Viro5a18fff2011-03-11 04:44:53 -05001203 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
1204 status = d_revalidate(dentry, nd);
1205 if (unlikely(status <= 0)) {
1206 if (status < 0) {
1207 dput(dentry);
1208 return status;
1209 }
1210 if (!d_invalidate(dentry)) {
1211 dput(dentry);
1212 dentry = NULL;
1213 need_reval = 1;
1214 goto retry;
1215 }
1216 }
1217
David Howells9875cf82011-01-14 18:45:21 +00001218 path->mnt = mnt;
1219 path->dentry = dentry;
1220 err = follow_managed(path, nd->flags);
Ian Kent89312212011-01-18 12:06:10 +08001221 if (unlikely(err < 0)) {
1222 path_put_conditional(path, nd);
David Howells9875cf82011-01-14 18:45:21 +00001223 return err;
Ian Kent89312212011-01-18 12:06:10 +08001224 }
David Howells9875cf82011-01-14 18:45:21 +00001225 *inode = path->dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227}
1228
Al Viro52094c82011-02-21 21:34:47 -05001229static inline int may_lookup(struct nameidata *nd)
1230{
1231 if (nd->flags & LOOKUP_RCU) {
1232 int err = exec_permission(nd->inode, IPERM_FLAG_RCU);
1233 if (err != -ECHILD)
1234 return err;
Al Viro19660af2011-03-25 10:32:48 -04001235 if (unlazy_walk(nd, NULL))
Al Viro52094c82011-02-21 21:34:47 -05001236 return -ECHILD;
1237 }
1238 return exec_permission(nd->inode, 0);
1239}
1240
Al Viro9856fa12011-03-04 14:22:06 -05001241static inline int handle_dots(struct nameidata *nd, int type)
1242{
1243 if (type == LAST_DOTDOT) {
1244 if (nd->flags & LOOKUP_RCU) {
1245 if (follow_dotdot_rcu(nd))
1246 return -ECHILD;
1247 } else
1248 follow_dotdot(nd);
1249 }
1250 return 0;
1251}
1252
Al Viro951361f2011-03-04 14:44:37 -05001253static void terminate_walk(struct nameidata *nd)
1254{
1255 if (!(nd->flags & LOOKUP_RCU)) {
1256 path_put(&nd->path);
1257 } else {
1258 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001259 if (!(nd->flags & LOOKUP_ROOT))
1260 nd->root.mnt = NULL;
Al Viro951361f2011-03-04 14:44:37 -05001261 rcu_read_unlock();
1262 br_read_unlock(vfsmount_lock);
1263 }
1264}
1265
Al Viroce57dfc2011-03-13 19:58:58 -04001266static inline int walk_component(struct nameidata *nd, struct path *path,
1267 struct qstr *name, int type, int follow)
1268{
1269 struct inode *inode;
1270 int err;
1271 /*
1272 * "." and ".." are special - ".." especially so because it has
1273 * to be able to know about the current root directory and
1274 * parent relationships.
1275 */
1276 if (unlikely(type != LAST_NORM))
1277 return handle_dots(nd, type);
1278 err = do_lookup(nd, name, path, &inode);
1279 if (unlikely(err)) {
1280 terminate_walk(nd);
1281 return err;
1282 }
1283 if (!inode) {
1284 path_to_nameidata(path, nd);
1285 terminate_walk(nd);
1286 return -ENOENT;
1287 }
1288 if (unlikely(inode->i_op->follow_link) && follow) {
Al Viro19660af2011-03-25 10:32:48 -04001289 if (nd->flags & LOOKUP_RCU) {
1290 if (unlikely(unlazy_walk(nd, path->dentry))) {
1291 terminate_walk(nd);
1292 return -ECHILD;
1293 }
1294 }
Al Viroce57dfc2011-03-13 19:58:58 -04001295 BUG_ON(inode != path->dentry->d_inode);
1296 return 1;
1297 }
1298 path_to_nameidata(path, nd);
1299 nd->inode = inode;
1300 return 0;
1301}
1302
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303/*
Al Virob3563792011-03-14 21:54:55 -04001304 * This limits recursive symlink follows to 8, while
1305 * limiting consecutive symlinks to 40.
1306 *
1307 * Without that kind of total limit, nasty chains of consecutive
1308 * symlinks can cause almost arbitrarily long lookups.
1309 */
1310static inline int nested_symlink(struct path *path, struct nameidata *nd)
1311{
1312 int res;
1313
Al Virob3563792011-03-14 21:54:55 -04001314 if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1315 path_put_conditional(path, nd);
1316 path_put(&nd->path);
1317 return -ELOOP;
1318 }
Erez Zadok1a4022f2011-05-21 01:19:59 -04001319 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
Al Virob3563792011-03-14 21:54:55 -04001320
1321 nd->depth++;
1322 current->link_count++;
1323
1324 do {
1325 struct path link = *path;
1326 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04001327
1328 res = follow_link(&link, nd, &cookie);
Al Virob3563792011-03-14 21:54:55 -04001329 if (!res)
1330 res = walk_component(nd, path, &nd->last,
1331 nd->last_type, LOOKUP_FOLLOW);
Al Viro574197e2011-03-14 22:20:34 -04001332 put_link(nd, &link, cookie);
Al Virob3563792011-03-14 21:54:55 -04001333 } while (res > 0);
1334
1335 current->link_count--;
1336 nd->depth--;
1337 return res;
1338}
1339
1340/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01001342 * This is the basic name resolution function, turning a pathname into
1343 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01001345 * Returns 0 and nd will have valid dentry and mnt on success.
1346 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 */
Al Viro6de88d72009-08-09 01:41:57 +04001348static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
1350 struct path next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 int err;
1352 unsigned int lookup_flags = nd->flags;
1353
1354 while (*name=='/')
1355 name++;
1356 if (!*name)
Al Viro086e1832011-02-22 20:56:27 -05001357 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 /* At this point we know we have a real path component. */
1360 for(;;) {
1361 unsigned long hash;
1362 struct qstr this;
1363 unsigned int c;
Al Virofe479a52011-02-22 15:10:03 -05001364 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Trond Myklebustcdce5d62005-10-18 14:20:18 -07001366 nd->flags |= LOOKUP_CONTINUE;
Al Viro52094c82011-02-21 21:34:47 -05001367
1368 err = may_lookup(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 if (err)
1370 break;
1371
1372 this.name = name;
1373 c = *(const unsigned char *)name;
1374
1375 hash = init_name_hash();
1376 do {
1377 name++;
1378 hash = partial_name_hash(c, hash);
1379 c = *(const unsigned char *)name;
1380 } while (c && (c != '/'));
1381 this.len = name - (const char *) this.name;
1382 this.hash = end_name_hash(hash);
1383
Al Virofe479a52011-02-22 15:10:03 -05001384 type = LAST_NORM;
1385 if (this.name[0] == '.') switch (this.len) {
1386 case 2:
Al Viro16c2cd72011-02-22 15:50:10 -05001387 if (this.name[1] == '.') {
Al Virofe479a52011-02-22 15:10:03 -05001388 type = LAST_DOTDOT;
Al Viro16c2cd72011-02-22 15:50:10 -05001389 nd->flags |= LOOKUP_JUMPED;
1390 }
Al Virofe479a52011-02-22 15:10:03 -05001391 break;
1392 case 1:
1393 type = LAST_DOT;
1394 }
Al Viro5a202bc2011-03-08 14:17:44 -05001395 if (likely(type == LAST_NORM)) {
1396 struct dentry *parent = nd->path.dentry;
Al Viro16c2cd72011-02-22 15:50:10 -05001397 nd->flags &= ~LOOKUP_JUMPED;
Al Viro5a202bc2011-03-08 14:17:44 -05001398 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1399 err = parent->d_op->d_hash(parent, nd->inode,
1400 &this);
1401 if (err < 0)
1402 break;
1403 }
1404 }
Al Virofe479a52011-02-22 15:10:03 -05001405
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 /* remove trailing slashes? */
1407 if (!c)
1408 goto last_component;
1409 while (*++name == '/');
1410 if (!*name)
Al Virob3563792011-03-14 21:54:55 -04001411 goto last_component;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Al Viroce57dfc2011-03-13 19:58:58 -04001413 err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1414 if (err < 0)
1415 return err;
Al Virofe479a52011-02-22 15:10:03 -05001416
Al Viroce57dfc2011-03-13 19:58:58 -04001417 if (err) {
Al Virob3563792011-03-14 21:54:55 -04001418 err = nested_symlink(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 if (err)
Al Viroa7472ba2011-03-04 14:39:30 -05001420 return err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 err = -ENOTDIR;
Nick Piggin31e6b012011-01-07 17:49:52 +11001423 if (!nd->inode->i_op->lookup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 break;
1425 continue;
1426 /* here ends the main loop */
1427
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428last_component:
Trond Myklebustf55eab82006-02-04 23:28:01 -08001429 /* Clear LOOKUP_CONTINUE iff it was previously unset */
1430 nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
Al Virob3563792011-03-14 21:54:55 -04001431 nd->last = this;
1432 nd->last_type = type;
Al Viro086e1832011-02-22 20:56:27 -05001433 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 }
Al Viro951361f2011-03-04 14:44:37 -05001435 terminate_walk(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 return err;
1437}
1438
Al Viro70e9b352011-03-05 21:12:22 -05001439static int path_init(int dfd, const char *name, unsigned int flags,
1440 struct nameidata *nd, struct file **fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
Prasanna Medaea3834d2005-04-29 16:00:17 +01001442 int retval = 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001443 int fput_needed;
1444 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
1446 nd->last_type = LAST_ROOT; /* if there are only slashes... */
Al Viro16c2cd72011-02-22 15:50:10 -05001447 nd->flags = flags | LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 nd->depth = 0;
Al Viro5b6ca022011-03-09 23:04:47 -05001449 if (flags & LOOKUP_ROOT) {
1450 struct inode *inode = nd->root.dentry->d_inode;
Al Viro73d049a2011-03-11 12:08:24 -05001451 if (*name) {
1452 if (!inode->i_op->lookup)
1453 return -ENOTDIR;
1454 retval = inode_permission(inode, MAY_EXEC);
1455 if (retval)
1456 return retval;
1457 }
Al Viro5b6ca022011-03-09 23:04:47 -05001458 nd->path = nd->root;
1459 nd->inode = inode;
1460 if (flags & LOOKUP_RCU) {
1461 br_read_lock(vfsmount_lock);
1462 rcu_read_lock();
1463 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1464 } else {
1465 path_get(&nd->path);
1466 }
1467 return 0;
1468 }
1469
Al Viro2a737872009-04-07 11:49:53 -04001470 nd->root.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 if (*name=='/') {
Al Viroe41f7d42011-02-22 14:02:58 -05001473 if (flags & LOOKUP_RCU) {
1474 br_read_lock(vfsmount_lock);
1475 rcu_read_lock();
1476 set_root_rcu(nd);
1477 } else {
1478 set_root(nd);
1479 path_get(&nd->root);
1480 }
Al Viro2a737872009-04-07 11:49:53 -04001481 nd->path = nd->root;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001482 } else if (dfd == AT_FDCWD) {
Al Viroe41f7d42011-02-22 14:02:58 -05001483 if (flags & LOOKUP_RCU) {
1484 struct fs_struct *fs = current->fs;
1485 unsigned seq;
1486
1487 br_read_lock(vfsmount_lock);
1488 rcu_read_lock();
1489
1490 do {
1491 seq = read_seqcount_begin(&fs->seq);
1492 nd->path = fs->pwd;
1493 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1494 } while (read_seqcount_retry(&fs->seq, seq));
1495 } else {
1496 get_fs_pwd(current->fs, &nd->path);
1497 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001498 } else {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001499 struct dentry *dentry;
1500
Al Viro1abf0c72011-03-13 03:51:11 -04001501 file = fget_raw_light(dfd, &fput_needed);
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001502 retval = -EBADF;
1503 if (!file)
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001504 goto out_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001505
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001506 dentry = file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001507
Al Virof52e0c12011-03-14 18:56:51 -04001508 if (*name) {
1509 retval = -ENOTDIR;
1510 if (!S_ISDIR(dentry->d_inode->i_mode))
1511 goto fput_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001512
Al Virof52e0c12011-03-14 18:56:51 -04001513 retval = file_permission(file, MAY_EXEC);
1514 if (retval)
1515 goto fput_fail;
1516 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001517
Jan Blunck5dd784d02008-02-14 19:34:38 -08001518 nd->path = file->f_path;
Al Viroe41f7d42011-02-22 14:02:58 -05001519 if (flags & LOOKUP_RCU) {
1520 if (fput_needed)
Al Viro70e9b352011-03-05 21:12:22 -05001521 *fp = file;
Al Viroe41f7d42011-02-22 14:02:58 -05001522 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1523 br_read_lock(vfsmount_lock);
1524 rcu_read_lock();
1525 } else {
1526 path_get(&file->f_path);
1527 fput_light(file, fput_needed);
1528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 }
Al Viroe41f7d42011-02-22 14:02:58 -05001530
Nick Piggin31e6b012011-01-07 17:49:52 +11001531 nd->inode = nd->path.dentry->d_inode;
Al Viro9b4a9b12009-04-07 11:44:16 -04001532 return 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001533
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001534fput_fail:
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001535 fput_light(file, fput_needed);
Al Viro9b4a9b12009-04-07 11:44:16 -04001536out_fail:
1537 return retval;
1538}
1539
Al Virobd92d7f2011-03-14 19:54:59 -04001540static inline int lookup_last(struct nameidata *nd, struct path *path)
1541{
1542 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1543 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1544
1545 nd->flags &= ~LOOKUP_PARENT;
1546 return walk_component(nd, path, &nd->last, nd->last_type,
1547 nd->flags & LOOKUP_FOLLOW);
1548}
1549
Al Viro9b4a9b12009-04-07 11:44:16 -04001550/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroee0827c2011-02-21 23:38:09 -05001551static int path_lookupat(int dfd, const char *name,
Al Viro9b4a9b12009-04-07 11:44:16 -04001552 unsigned int flags, struct nameidata *nd)
1553{
Al Viro70e9b352011-03-05 21:12:22 -05001554 struct file *base = NULL;
Al Virobd92d7f2011-03-14 19:54:59 -04001555 struct path path;
1556 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001557
1558 /*
1559 * Path walking is largely split up into 2 different synchronisation
1560 * schemes, rcu-walk and ref-walk (explained in
1561 * Documentation/filesystems/path-lookup.txt). These share much of the
1562 * path walk code, but some things particularly setup, cleanup, and
1563 * following mounts are sufficiently divergent that functions are
1564 * duplicated. Typically there is a function foo(), and its RCU
1565 * analogue, foo_rcu().
1566 *
1567 * -ECHILD is the error number of choice (just to avoid clashes) that
1568 * is returned if some aspect of an rcu-walk fails. Such an error must
1569 * be handled by restarting a traditional ref-walk (which will always
1570 * be able to complete).
1571 */
Al Virobd92d7f2011-03-14 19:54:59 -04001572 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
Al Viroee0827c2011-02-21 23:38:09 -05001573
Al Virobd92d7f2011-03-14 19:54:59 -04001574 if (unlikely(err))
1575 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001576
1577 current->total_link_count = 0;
Al Virobd92d7f2011-03-14 19:54:59 -04001578 err = link_path_walk(name, nd);
1579
1580 if (!err && !(flags & LOOKUP_PARENT)) {
Al Virobd92d7f2011-03-14 19:54:59 -04001581 err = lookup_last(nd, &path);
1582 while (err > 0) {
1583 void *cookie;
1584 struct path link = path;
Al Virobd92d7f2011-03-14 19:54:59 -04001585 nd->flags |= LOOKUP_PARENT;
Al Viro574197e2011-03-14 22:20:34 -04001586 err = follow_link(&link, nd, &cookie);
Al Virobd92d7f2011-03-14 19:54:59 -04001587 if (!err)
1588 err = lookup_last(nd, &path);
Al Viro574197e2011-03-14 22:20:34 -04001589 put_link(nd, &link, cookie);
Al Virobd92d7f2011-03-14 19:54:59 -04001590 }
1591 }
Al Viroee0827c2011-02-21 23:38:09 -05001592
Al Viro9f1fafe2011-03-25 11:00:12 -04001593 if (!err)
1594 err = complete_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04001595
1596 if (!err && nd->flags & LOOKUP_DIRECTORY) {
1597 if (!nd->inode->i_op->lookup) {
1598 path_put(&nd->path);
Al Virobd23a532011-03-23 09:56:30 -04001599 err = -ENOTDIR;
Al Virobd92d7f2011-03-14 19:54:59 -04001600 }
1601 }
Al Viro16c2cd72011-02-22 15:50:10 -05001602
Al Viro70e9b352011-03-05 21:12:22 -05001603 if (base)
1604 fput(base);
Al Viroee0827c2011-02-21 23:38:09 -05001605
Al Viro5b6ca022011-03-09 23:04:47 -05001606 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
Al Viro2a737872009-04-07 11:49:53 -04001607 path_put(&nd->root);
1608 nd->root.mnt = NULL;
1609 }
Al Virobd92d7f2011-03-14 19:54:59 -04001610 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001611}
Nick Piggin31e6b012011-01-07 17:49:52 +11001612
Al Viroee0827c2011-02-21 23:38:09 -05001613static int do_path_lookup(int dfd, const char *name,
1614 unsigned int flags, struct nameidata *nd)
1615{
1616 int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1617 if (unlikely(retval == -ECHILD))
1618 retval = path_lookupat(dfd, name, flags, nd);
1619 if (unlikely(retval == -ESTALE))
1620 retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11001621
1622 if (likely(!retval)) {
1623 if (unlikely(!audit_dummy_context())) {
1624 if (nd->path.dentry && nd->inode)
1625 audit_inode(name, nd->path.dentry);
1626 }
1627 }
Al Viro9b4a9b12009-04-07 11:44:16 -04001628 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629}
1630
Al Viroc9c6cac2011-02-16 15:15:47 -05001631int kern_path_parent(const char *name, struct nameidata *nd)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001632{
Al Viroc9c6cac2011-02-16 15:15:47 -05001633 return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001634}
1635
Al Virod1811462008-08-02 00:49:18 -04001636int kern_path(const char *name, unsigned int flags, struct path *path)
1637{
1638 struct nameidata nd;
1639 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1640 if (!res)
1641 *path = nd.path;
1642 return res;
1643}
1644
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001645/**
1646 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1647 * @dentry: pointer to dentry of the base directory
1648 * @mnt: pointer to vfs mount of the base directory
1649 * @name: pointer to file name
1650 * @flags: lookup flags
1651 * @nd: pointer to nameidata
1652 */
1653int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
1654 const char *name, unsigned int flags,
1655 struct nameidata *nd)
1656{
Al Viro5b6ca022011-03-09 23:04:47 -05001657 nd->root.dentry = dentry;
1658 nd->root.mnt = mnt;
1659 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
1660 return do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, nd);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001661}
1662
Christoph Hellwigeead1912007-10-16 23:25:38 -07001663static struct dentry *__lookup_hash(struct qstr *name,
1664 struct dentry *base, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665{
Christoph Hellwig81fca442010-10-06 10:47:47 +02001666 struct inode *inode = base->d_inode;
James Morris057f6c02007-04-26 00:12:05 -07001667 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 int err;
1669
Nick Pigginb74c79e2011-01-07 17:49:58 +11001670 err = exec_permission(inode, 0);
Christoph Hellwig81fca442010-10-06 10:47:47 +02001671 if (err)
1672 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
1674 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001675 * Don't bother with __d_lookup: callers are for creat as
1676 * well as unlink, so a lot of the time it would cost
1677 * a double lookup.
Al Viro6e6b1bd2009-08-13 23:38:37 +04001678 */
Nick Pigginb04f7842010-08-18 04:37:34 +10001679 dentry = d_lookup(base, name);
Al Viro6e6b1bd2009-08-13 23:38:37 +04001680
Nick Pigginfb045ad2011-01-07 17:49:55 +11001681 if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE))
Al Viro6e6b1bd2009-08-13 23:38:37 +04001682 dentry = do_revalidate(dentry, nd);
1683
Nick Pigginbaa03892010-08-18 04:37:31 +10001684 if (!dentry)
1685 dentry = d_alloc_and_lookup(base, name, nd);
Al Viro5a202bc2011-03-08 14:17:44 -05001686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 return dentry;
1688}
1689
James Morris057f6c02007-04-26 00:12:05 -07001690/*
1691 * Restricted form of lookup. Doesn't follow links, single-component only,
1692 * needs parent already locked. Doesn't follow mounts.
1693 * SMP-safe.
1694 */
Adrian Bunka244e162006-03-31 02:32:11 -08001695static struct dentry *lookup_hash(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696{
Jan Blunck4ac91372008-02-14 19:34:32 -08001697 return __lookup_hash(&nd->last, nd->path.dentry, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698}
1699
Christoph Hellwigeead1912007-10-16 23:25:38 -07001700/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07001701 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07001702 * @name: pathname component to lookup
1703 * @base: base directory to lookup from
1704 * @len: maximum length @len should be interpreted to
1705 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07001706 * Note that this routine is purely a helper for filesystem usage and should
1707 * not be called by generic code. Also note that by using this function the
Christoph Hellwigeead1912007-10-16 23:25:38 -07001708 * nameidata argument is passed to the filesystem methods and a filesystem
1709 * using this helper needs to be prepared for that.
1710 */
James Morris057f6c02007-04-26 00:12:05 -07001711struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1712{
James Morris057f6c02007-04-26 00:12:05 -07001713 struct qstr this;
Al Viro6a96ba52011-03-07 23:49:20 -05001714 unsigned long hash;
1715 unsigned int c;
James Morris057f6c02007-04-26 00:12:05 -07001716
David Woodhouse2f9092e2009-04-20 23:18:37 +01001717 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
1718
Al Viro6a96ba52011-03-07 23:49:20 -05001719 this.name = name;
1720 this.len = len;
1721 if (!len)
1722 return ERR_PTR(-EACCES);
1723
1724 hash = init_name_hash();
1725 while (len--) {
1726 c = *(const unsigned char *)name++;
1727 if (c == '/' || c == '\0')
1728 return ERR_PTR(-EACCES);
1729 hash = partial_name_hash(c, hash);
1730 }
1731 this.hash = end_name_hash(hash);
Al Viro5a202bc2011-03-08 14:17:44 -05001732 /*
1733 * See if the low-level filesystem might want
1734 * to use its own hash..
1735 */
1736 if (base->d_flags & DCACHE_OP_HASH) {
1737 int err = base->d_op->d_hash(base, base->d_inode, &this);
1738 if (err < 0)
1739 return ERR_PTR(err);
1740 }
Christoph Hellwigeead1912007-10-16 23:25:38 -07001741
Christoph Hellwig49705b72005-11-08 21:35:06 -08001742 return __lookup_hash(&this, base, NULL);
James Morris057f6c02007-04-26 00:12:05 -07001743}
1744
Al Viro2d8f3032008-07-22 09:59:21 -04001745int user_path_at(int dfd, const char __user *name, unsigned flags,
1746 struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747{
Al Viro2d8f3032008-07-22 09:59:21 -04001748 struct nameidata nd;
Al Virof52e0c12011-03-14 18:56:51 -04001749 char *tmp = getname_flags(name, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 int err = PTR_ERR(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 if (!IS_ERR(tmp)) {
Al Viro2d8f3032008-07-22 09:59:21 -04001752
1753 BUG_ON(flags & LOOKUP_PARENT);
1754
1755 err = do_path_lookup(dfd, tmp, flags, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 putname(tmp);
Al Viro2d8f3032008-07-22 09:59:21 -04001757 if (!err)
1758 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 }
1760 return err;
1761}
1762
Al Viro2ad94ae2008-07-21 09:32:51 -04001763static int user_path_parent(int dfd, const char __user *path,
1764 struct nameidata *nd, char **name)
1765{
1766 char *s = getname(path);
1767 int error;
1768
1769 if (IS_ERR(s))
1770 return PTR_ERR(s);
1771
1772 error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
1773 if (error)
1774 putname(s);
1775 else
1776 *name = s;
1777
1778 return error;
1779}
1780
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781/*
1782 * It's inline, so penalty for filesystems that don't use sticky bit is
1783 * minimal.
1784 */
1785static inline int check_sticky(struct inode *dir, struct inode *inode)
1786{
David Howellsda9592e2008-11-14 10:39:05 +11001787 uid_t fsuid = current_fsuid();
1788
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 if (!(dir->i_mode & S_ISVTX))
1790 return 0;
Serge E. Hallyne795b712011-03-23 16:43:25 -07001791 if (current_user_ns() != inode_userns(inode))
1792 goto other_userns;
David Howellsda9592e2008-11-14 10:39:05 +11001793 if (inode->i_uid == fsuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 return 0;
David Howellsda9592e2008-11-14 10:39:05 +11001795 if (dir->i_uid == fsuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 return 0;
Serge E. Hallyne795b712011-03-23 16:43:25 -07001797
1798other_userns:
1799 return !ns_capable(inode_userns(inode), CAP_FOWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800}
1801
1802/*
1803 * Check whether we can remove a link victim from directory dir, check
1804 * whether the type of victim is right.
1805 * 1. We can't do it if dir is read-only (done in permission())
1806 * 2. We should have write and exec permissions on dir
1807 * 3. We can't remove anything from append-only dir
1808 * 4. We can't do anything with immutable dir (done in permission())
1809 * 5. If the sticky bit on dir is set we should either
1810 * a. be owner of dir, or
1811 * b. be owner of victim, or
1812 * c. have CAP_FOWNER capability
1813 * 6. If the victim is append-only or immutable we can't do antyhing with
1814 * links pointing to it.
1815 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1816 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1817 * 9. We can't remove a root or mountpoint.
1818 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1819 * nfs_async_unlink().
1820 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001821static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822{
1823 int error;
1824
1825 if (!victim->d_inode)
1826 return -ENOENT;
1827
1828 BUG_ON(victim->d_parent->d_inode != dir);
Al Virocccc6bb2009-12-25 05:07:33 -05001829 audit_inode_child(victim, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
Al Virof419a2e2008-07-22 00:07:17 -04001831 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 if (error)
1833 return error;
1834 if (IS_APPEND(dir))
1835 return -EPERM;
1836 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
Hugh Dickinsf9454542008-11-19 15:36:38 -08001837 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 return -EPERM;
1839 if (isdir) {
1840 if (!S_ISDIR(victim->d_inode->i_mode))
1841 return -ENOTDIR;
1842 if (IS_ROOT(victim))
1843 return -EBUSY;
1844 } else if (S_ISDIR(victim->d_inode->i_mode))
1845 return -EISDIR;
1846 if (IS_DEADDIR(dir))
1847 return -ENOENT;
1848 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1849 return -EBUSY;
1850 return 0;
1851}
1852
1853/* Check whether we can create an object with dentry child in directory
1854 * dir.
1855 * 1. We can't do it if child already exists (open has special treatment for
1856 * this case, but since we are inlined it's OK)
1857 * 2. We can't do it if dir is read-only (done in permission())
1858 * 3. We should have write and exec permissions on dir
1859 * 4. We can't do it if dir is immutable (done in permission())
1860 */
Miklos Szeredia95164d2008-07-30 15:08:48 +02001861static inline int may_create(struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862{
1863 if (child->d_inode)
1864 return -EEXIST;
1865 if (IS_DEADDIR(dir))
1866 return -ENOENT;
Al Virof419a2e2008-07-22 00:07:17 -04001867 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868}
1869
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870/*
1871 * p1 and p2 should be directories on the same fs.
1872 */
1873struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1874{
1875 struct dentry *p;
1876
1877 if (p1 == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07001878 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 return NULL;
1880 }
1881
Arjan van de Vena11f3a02006-03-23 03:00:33 -08001882 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001884 p = d_ancestor(p2, p1);
1885 if (p) {
1886 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1887 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
1888 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 }
1890
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001891 p = d_ancestor(p1, p2);
1892 if (p) {
1893 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1894 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1895 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 }
1897
Ingo Molnarf2eace22006-07-03 00:25:05 -07001898 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1899 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 return NULL;
1901}
1902
1903void unlock_rename(struct dentry *p1, struct dentry *p2)
1904{
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001905 mutex_unlock(&p1->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 if (p1 != p2) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001907 mutex_unlock(&p2->d_inode->i_mutex);
Arjan van de Vena11f3a02006-03-23 03:00:33 -08001908 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 }
1910}
1911
1912int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1913 struct nameidata *nd)
1914{
Miklos Szeredia95164d2008-07-30 15:08:48 +02001915 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
1917 if (error)
1918 return error;
1919
Al Viroacfa4382008-12-04 10:06:33 -05001920 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 return -EACCES; /* shouldn't it be ENOSYS? */
1922 mode &= S_IALLUGO;
1923 mode |= S_IFREG;
1924 error = security_inode_create(dir, dentry, mode);
1925 if (error)
1926 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 error = dir->i_op->create(dir, dentry, mode, nd);
Stephen Smalleya74574a2005-09-09 13:01:44 -07001928 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00001929 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 return error;
1931}
1932
Al Viro73d049a2011-03-11 12:08:24 -05001933static int may_open(struct path *path, int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001935 struct dentry *dentry = path->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 struct inode *inode = dentry->d_inode;
1937 int error;
1938
Al Virobcda7652011-03-13 16:42:14 -04001939 /* O_PATH? */
1940 if (!acc_mode)
1941 return 0;
1942
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 if (!inode)
1944 return -ENOENT;
1945
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001946 switch (inode->i_mode & S_IFMT) {
1947 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001949 case S_IFDIR:
1950 if (acc_mode & MAY_WRITE)
1951 return -EISDIR;
1952 break;
1953 case S_IFBLK:
1954 case S_IFCHR:
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001955 if (path->mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001957 /*FALLTHRU*/
1958 case S_IFIFO:
1959 case S_IFSOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001961 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08001962 }
Dave Hansenb41572e2007-10-16 23:31:14 -07001963
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001964 error = inode_permission(inode, acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07001965 if (error)
1966 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05001967
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 /*
1969 * An append-only file must be opened in append mode for writing.
1970 */
1971 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05001972 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05001973 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05001975 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 }
1977
1978 /* O_NOATIME can only be set by the owner or superuser */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001979 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
Al Viro7715b522009-12-16 03:54:00 -05001980 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
1982 /*
1983 * Ensure there are no outstanding leases on the file.
1984 */
Al Virob65a9cf2009-12-16 06:27:40 -05001985 return break_lease(inode, flag);
Al Viro7715b522009-12-16 03:54:00 -05001986}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
Jeff Laytone1181ee2010-12-07 16:19:50 -05001988static int handle_truncate(struct file *filp)
Al Viro7715b522009-12-16 03:54:00 -05001989{
Jeff Laytone1181ee2010-12-07 16:19:50 -05001990 struct path *path = &filp->f_path;
Al Viro7715b522009-12-16 03:54:00 -05001991 struct inode *inode = path->dentry->d_inode;
1992 int error = get_write_access(inode);
1993 if (error)
1994 return error;
1995 /*
1996 * Refuse to truncate files with mandatory locks held on them.
1997 */
1998 error = locks_verify_locked(inode);
1999 if (!error)
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +09002000 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05002001 if (!error) {
2002 error = do_truncate(path->dentry, 0,
2003 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
Jeff Laytone1181ee2010-12-07 16:19:50 -05002004 filp);
Al Viro7715b522009-12-16 03:54:00 -05002005 }
2006 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04002007 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008}
2009
Dave Hansend57999e2008-02-15 14:37:27 -08002010/*
Dave Hansend57999e2008-02-15 14:37:27 -08002011 * Note that while the flag value (low two bits) for sys_open means:
2012 * 00 - read-only
2013 * 01 - write-only
2014 * 10 - read-write
2015 * 11 - special
2016 * it is changed into
2017 * 00 - no permissions needed
2018 * 01 - read-permission
2019 * 10 - write-permission
2020 * 11 - read-write
2021 * for the internal routines (ie open_namei()/follow_link() etc)
2022 * This is more logical, and also allows the 00 "no perm needed"
2023 * to be used for symlinks (where the permissions are checked
2024 * later).
2025 *
2026*/
2027static inline int open_to_namei_flags(int flag)
2028{
2029 if ((flag+1) & O_ACCMODE)
2030 flag++;
2031 return flag;
2032}
2033
Nick Piggin31e6b012011-01-07 17:49:52 +11002034/*
Al Virofe2d35f2011-03-05 22:58:25 -05002035 * Handle the last step of open()
Nick Piggin31e6b012011-01-07 17:49:52 +11002036 */
Al Virofb1cc552009-12-24 01:58:28 -05002037static struct file *do_last(struct nameidata *nd, struct path *path,
Al Viroc3e380b2011-02-23 13:39:45 -05002038 const struct open_flags *op, const char *pathname)
Al Virofb1cc552009-12-24 01:58:28 -05002039{
Al Viroa1e28032009-12-24 02:12:06 -05002040 struct dentry *dir = nd->path.dentry;
Al Viro6c0d46c2011-03-09 00:59:59 -05002041 struct dentry *dentry;
Al Viroca344a892011-03-09 00:36:45 -05002042 int open_flag = op->open_flag;
Al Viro6c0d46c2011-03-09 00:59:59 -05002043 int will_truncate = open_flag & O_TRUNC;
Al Viroca344a892011-03-09 00:36:45 -05002044 int want_write = 0;
Al Virobcda7652011-03-13 16:42:14 -04002045 int acc_mode = op->acc_mode;
Al Virofb1cc552009-12-24 01:58:28 -05002046 struct file *filp;
Al Viro16c2cd72011-02-22 15:50:10 -05002047 int error;
Al Virofb1cc552009-12-24 01:58:28 -05002048
Al Viroc3e380b2011-02-23 13:39:45 -05002049 nd->flags &= ~LOOKUP_PARENT;
2050 nd->flags |= op->intent;
2051
Al Viro1f36f772009-12-26 10:56:19 -05002052 switch (nd->last_type) {
2053 case LAST_DOTDOT:
Neil Brown176306f2010-05-24 16:57:56 +10002054 case LAST_DOT:
Al Virofe2d35f2011-03-05 22:58:25 -05002055 error = handle_dots(nd, nd->last_type);
2056 if (error)
2057 return ERR_PTR(error);
Al Viro1f36f772009-12-26 10:56:19 -05002058 /* fallthrough */
Al Viro1f36f772009-12-26 10:56:19 -05002059 case LAST_ROOT:
Al Viro9f1fafe2011-03-25 11:00:12 -04002060 error = complete_walk(nd);
Al Viro16c2cd72011-02-22 15:50:10 -05002061 if (error)
Al Viro9f1fafe2011-03-25 11:00:12 -04002062 return ERR_PTR(error);
Al Virofe2d35f2011-03-05 22:58:25 -05002063 audit_inode(pathname, nd->path.dentry);
Al Viroca344a892011-03-09 00:36:45 -05002064 if (open_flag & O_CREAT) {
Al Virofe2d35f2011-03-05 22:58:25 -05002065 error = -EISDIR;
2066 goto exit;
2067 }
2068 goto ok;
Al Viro1f36f772009-12-26 10:56:19 -05002069 case LAST_BIND:
Al Viro9f1fafe2011-03-25 11:00:12 -04002070 error = complete_walk(nd);
Al Viro16c2cd72011-02-22 15:50:10 -05002071 if (error)
Al Viro9f1fafe2011-03-25 11:00:12 -04002072 return ERR_PTR(error);
Al Viro1f36f772009-12-26 10:56:19 -05002073 audit_inode(pathname, dir);
Al Viro67ee3ad2009-12-26 07:01:01 -05002074 goto ok;
Al Viro1f36f772009-12-26 10:56:19 -05002075 }
Al Viro67ee3ad2009-12-26 07:01:01 -05002076
Al Viroca344a892011-03-09 00:36:45 -05002077 if (!(open_flag & O_CREAT)) {
Al Virobcda7652011-03-13 16:42:14 -04002078 int symlink_ok = 0;
Al Virofe2d35f2011-03-05 22:58:25 -05002079 if (nd->last.name[nd->last.len])
2080 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
Al Virobcda7652011-03-13 16:42:14 -04002081 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2082 symlink_ok = 1;
Al Virofe2d35f2011-03-05 22:58:25 -05002083 /* we _can_ be in RCU mode here */
Al Viroce57dfc2011-03-13 19:58:58 -04002084 error = walk_component(nd, path, &nd->last, LAST_NORM,
2085 !symlink_ok);
2086 if (error < 0)
Al Virofe2d35f2011-03-05 22:58:25 -05002087 return ERR_PTR(error);
Al Viroce57dfc2011-03-13 19:58:58 -04002088 if (error) /* symlink */
Al Virofe2d35f2011-03-05 22:58:25 -05002089 return NULL;
Al Virofe2d35f2011-03-05 22:58:25 -05002090 /* sayonara */
Al Viro9f1fafe2011-03-25 11:00:12 -04002091 error = complete_walk(nd);
2092 if (error)
2093 return ERR_PTR(-ECHILD);
Al Virofe2d35f2011-03-05 22:58:25 -05002094
2095 error = -ENOTDIR;
2096 if (nd->flags & LOOKUP_DIRECTORY) {
Al Viroce57dfc2011-03-13 19:58:58 -04002097 if (!nd->inode->i_op->lookup)
Al Virofe2d35f2011-03-05 22:58:25 -05002098 goto exit;
2099 }
2100 audit_inode(pathname, nd->path.dentry);
2101 goto ok;
2102 }
2103
2104 /* create side of things */
Al Viro9f1fafe2011-03-25 11:00:12 -04002105 error = complete_walk(nd);
2106 if (error)
2107 return ERR_PTR(error);
Al Virofe2d35f2011-03-05 22:58:25 -05002108
2109 audit_inode(pathname, dir);
Al Viro16c2cd72011-02-22 15:50:10 -05002110 error = -EISDIR;
Al Viro1f36f772009-12-26 10:56:19 -05002111 /* trailing slashes? */
Nick Piggin31e6b012011-01-07 17:49:52 +11002112 if (nd->last.name[nd->last.len])
2113 goto exit;
Al Viroa2c36b42009-12-24 03:39:50 -05002114
Al Viroa1e28032009-12-24 02:12:06 -05002115 mutex_lock(&dir->d_inode->i_mutex);
2116
Al Viro6c0d46c2011-03-09 00:59:59 -05002117 dentry = lookup_hash(nd);
2118 error = PTR_ERR(dentry);
2119 if (IS_ERR(dentry)) {
Al Virofb1cc552009-12-24 01:58:28 -05002120 mutex_unlock(&dir->d_inode->i_mutex);
2121 goto exit;
2122 }
2123
Al Viro6c0d46c2011-03-09 00:59:59 -05002124 path->dentry = dentry;
2125 path->mnt = nd->path.mnt;
2126
Al Virofb1cc552009-12-24 01:58:28 -05002127 /* Negative dentry, just create the file */
Al Viro6c0d46c2011-03-09 00:59:59 -05002128 if (!dentry->d_inode) {
2129 int mode = op->mode;
2130 if (!IS_POSIXACL(dir->d_inode))
2131 mode &= ~current_umask();
Al Virofb1cc552009-12-24 01:58:28 -05002132 /*
2133 * This write is needed to ensure that a
Al Viro6c0d46c2011-03-09 00:59:59 -05002134 * rw->ro transition does not occur between
Al Virofb1cc552009-12-24 01:58:28 -05002135 * the time when the file is created and when
2136 * a permanent write count is taken through
2137 * the 'struct file' in nameidata_to_filp().
2138 */
2139 error = mnt_want_write(nd->path.mnt);
2140 if (error)
2141 goto exit_mutex_unlock;
Al Viroca344a892011-03-09 00:36:45 -05002142 want_write = 1;
Al Viro9b44f1b2011-03-09 00:17:27 -05002143 /* Don't check for write permission, don't truncate */
Al Viroca344a892011-03-09 00:36:45 -05002144 open_flag &= ~O_TRUNC;
Al Viro6c0d46c2011-03-09 00:59:59 -05002145 will_truncate = 0;
Al Virobcda7652011-03-13 16:42:14 -04002146 acc_mode = MAY_OPEN;
Al Viro6c0d46c2011-03-09 00:59:59 -05002147 error = security_path_mknod(&nd->path, dentry, mode, 0);
2148 if (error)
2149 goto exit_mutex_unlock;
2150 error = vfs_create(dir->d_inode, dentry, mode, nd);
2151 if (error)
2152 goto exit_mutex_unlock;
2153 mutex_unlock(&dir->d_inode->i_mutex);
2154 dput(nd->path.dentry);
2155 nd->path.dentry = dentry;
Al Viroca344a892011-03-09 00:36:45 -05002156 goto common;
Al Virofb1cc552009-12-24 01:58:28 -05002157 }
2158
2159 /*
2160 * It already exists.
2161 */
2162 mutex_unlock(&dir->d_inode->i_mutex);
2163 audit_inode(pathname, path->dentry);
2164
2165 error = -EEXIST;
Al Viroca344a892011-03-09 00:36:45 -05002166 if (open_flag & O_EXCL)
Al Virofb1cc552009-12-24 01:58:28 -05002167 goto exit_dput;
2168
David Howells9875cf82011-01-14 18:45:21 +00002169 error = follow_managed(path, nd->flags);
2170 if (error < 0)
2171 goto exit_dput;
Al Virofb1cc552009-12-24 01:58:28 -05002172
2173 error = -ENOENT;
2174 if (!path->dentry->d_inode)
2175 goto exit_dput;
Al Viro9e67f362009-12-26 07:04:50 -05002176
2177 if (path->dentry->d_inode->i_op->follow_link)
Al Virofb1cc552009-12-24 01:58:28 -05002178 return NULL;
Al Virofb1cc552009-12-24 01:58:28 -05002179
2180 path_to_nameidata(path, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11002181 nd->inode = path->dentry->d_inode;
Al Virofb1cc552009-12-24 01:58:28 -05002182 error = -EISDIR;
Nick Piggin31e6b012011-01-07 17:49:52 +11002183 if (S_ISDIR(nd->inode->i_mode))
Al Virofb1cc552009-12-24 01:58:28 -05002184 goto exit;
Al Viro67ee3ad2009-12-26 07:01:01 -05002185ok:
Al Viro6c0d46c2011-03-09 00:59:59 -05002186 if (!S_ISREG(nd->inode->i_mode))
2187 will_truncate = 0;
2188
Al Viro0f9d1a12011-03-09 00:13:14 -05002189 if (will_truncate) {
2190 error = mnt_want_write(nd->path.mnt);
2191 if (error)
2192 goto exit;
Al Viroca344a892011-03-09 00:36:45 -05002193 want_write = 1;
Al Viro0f9d1a12011-03-09 00:13:14 -05002194 }
Al Viroca344a892011-03-09 00:36:45 -05002195common:
Al Virobcda7652011-03-13 16:42:14 -04002196 error = may_open(&nd->path, acc_mode, open_flag);
Al Viroca344a892011-03-09 00:36:45 -05002197 if (error)
Al Viro0f9d1a12011-03-09 00:13:14 -05002198 goto exit;
Al Viro0f9d1a12011-03-09 00:13:14 -05002199 filp = nameidata_to_filp(nd);
2200 if (!IS_ERR(filp)) {
2201 error = ima_file_check(filp, op->acc_mode);
2202 if (error) {
2203 fput(filp);
2204 filp = ERR_PTR(error);
2205 }
2206 }
2207 if (!IS_ERR(filp)) {
2208 if (will_truncate) {
2209 error = handle_truncate(filp);
2210 if (error) {
2211 fput(filp);
2212 filp = ERR_PTR(error);
2213 }
2214 }
2215 }
Al Viroca344a892011-03-09 00:36:45 -05002216out:
2217 if (want_write)
Al Viro0f9d1a12011-03-09 00:13:14 -05002218 mnt_drop_write(nd->path.mnt);
2219 path_put(&nd->path);
Al Virofb1cc552009-12-24 01:58:28 -05002220 return filp;
2221
2222exit_mutex_unlock:
2223 mutex_unlock(&dir->d_inode->i_mutex);
2224exit_dput:
2225 path_put_conditional(path, nd);
2226exit:
Al Viroca344a892011-03-09 00:36:45 -05002227 filp = ERR_PTR(error);
2228 goto out;
Al Virofb1cc552009-12-24 01:58:28 -05002229}
2230
Al Viro13aab422011-02-23 17:54:08 -05002231static struct file *path_openat(int dfd, const char *pathname,
Al Viro73d049a2011-03-11 12:08:24 -05002232 struct nameidata *nd, const struct open_flags *op, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233{
Al Virofe2d35f2011-03-05 22:58:25 -05002234 struct file *base = NULL;
Dave Hansen4a3fd212008-02-15 14:37:48 -08002235 struct file *filp;
Al Viro9850c052010-01-13 15:01:15 -05002236 struct path path;
Al Viro13aab422011-02-23 17:54:08 -05002237 int error;
Nick Piggin31e6b012011-01-07 17:49:52 +11002238
2239 filp = get_empty_filp();
2240 if (!filp)
2241 return ERR_PTR(-ENFILE);
2242
Al Viro47c805d2011-02-23 17:44:09 -05002243 filp->f_flags = op->open_flag;
Al Viro73d049a2011-03-11 12:08:24 -05002244 nd->intent.open.file = filp;
2245 nd->intent.open.flags = open_to_namei_flags(op->open_flag);
2246 nd->intent.open.create_mode = op->mode;
Nick Piggin31e6b012011-01-07 17:49:52 +11002247
Al Viro73d049a2011-03-11 12:08:24 -05002248 error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
Nick Piggin31e6b012011-01-07 17:49:52 +11002249 if (unlikely(error))
Al Viro13aab422011-02-23 17:54:08 -05002250 goto out_filp;
Nick Piggin31e6b012011-01-07 17:49:52 +11002251
Al Virofe2d35f2011-03-05 22:58:25 -05002252 current->total_link_count = 0;
Al Viro73d049a2011-03-11 12:08:24 -05002253 error = link_path_walk(pathname, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11002254 if (unlikely(error))
2255 goto out_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
Al Viro73d049a2011-03-11 12:08:24 -05002257 filp = do_last(nd, &path, op, pathname);
Al Viro806b6812009-12-26 07:16:40 -05002258 while (unlikely(!filp)) { /* trailing symlink */
Nick Piggin7b9337a2011-01-14 08:42:43 +00002259 struct path link = path;
Al Virodef4af32009-12-26 08:37:05 -05002260 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04002261 if (!(nd->flags & LOOKUP_FOLLOW)) {
Al Viro73d049a2011-03-11 12:08:24 -05002262 path_put_conditional(&path, nd);
2263 path_put(&nd->path);
Al Viro40b39132011-03-09 16:22:18 -05002264 filp = ERR_PTR(-ELOOP);
2265 break;
2266 }
Al Viro73d049a2011-03-11 12:08:24 -05002267 nd->flags |= LOOKUP_PARENT;
2268 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
Al Viro574197e2011-03-14 22:20:34 -04002269 error = follow_link(&link, nd, &cookie);
Al Viroc3e380b2011-02-23 13:39:45 -05002270 if (unlikely(error))
Al Virof1afe9e2011-02-22 22:27:28 -05002271 filp = ERR_PTR(error);
Al Viroc3e380b2011-02-23 13:39:45 -05002272 else
Al Viro73d049a2011-03-11 12:08:24 -05002273 filp = do_last(nd, &path, op, pathname);
Al Viro574197e2011-03-14 22:20:34 -04002274 put_link(nd, &link, cookie);
Al Viro806b6812009-12-26 07:16:40 -05002275 }
Al Viro10fa8e62009-12-26 07:09:49 -05002276out:
Al Viro73d049a2011-03-11 12:08:24 -05002277 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
2278 path_put(&nd->root);
Al Virofe2d35f2011-03-05 22:58:25 -05002279 if (base)
2280 fput(base);
Al Viro73d049a2011-03-11 12:08:24 -05002281 release_open_intent(nd);
Al Viro10fa8e62009-12-26 07:09:49 -05002282 return filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
Nick Piggin31e6b012011-01-07 17:49:52 +11002284out_filp:
Al Viro806b6812009-12-26 07:16:40 -05002285 filp = ERR_PTR(error);
Al Viro10fa8e62009-12-26 07:09:49 -05002286 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287}
2288
Al Viro13aab422011-02-23 17:54:08 -05002289struct file *do_filp_open(int dfd, const char *pathname,
2290 const struct open_flags *op, int flags)
2291{
Al Viro73d049a2011-03-11 12:08:24 -05002292 struct nameidata nd;
Al Viro13aab422011-02-23 17:54:08 -05002293 struct file *filp;
2294
Al Viro73d049a2011-03-11 12:08:24 -05002295 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
Al Viro13aab422011-02-23 17:54:08 -05002296 if (unlikely(filp == ERR_PTR(-ECHILD)))
Al Viro73d049a2011-03-11 12:08:24 -05002297 filp = path_openat(dfd, pathname, &nd, op, flags);
Al Viro13aab422011-02-23 17:54:08 -05002298 if (unlikely(filp == ERR_PTR(-ESTALE)))
Al Viro73d049a2011-03-11 12:08:24 -05002299 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
Al Viro13aab422011-02-23 17:54:08 -05002300 return filp;
2301}
2302
Al Viro73d049a2011-03-11 12:08:24 -05002303struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
2304 const char *name, const struct open_flags *op, int flags)
2305{
2306 struct nameidata nd;
2307 struct file *file;
2308
2309 nd.root.mnt = mnt;
2310 nd.root.dentry = dentry;
2311
2312 flags |= LOOKUP_ROOT;
2313
Al Virobcda7652011-03-13 16:42:14 -04002314 if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
Al Viro73d049a2011-03-11 12:08:24 -05002315 return ERR_PTR(-ELOOP);
2316
2317 file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
2318 if (unlikely(file == ERR_PTR(-ECHILD)))
2319 file = path_openat(-1, name, &nd, op, flags);
2320 if (unlikely(file == ERR_PTR(-ESTALE)))
2321 file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
2322 return file;
2323}
2324
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325/**
2326 * lookup_create - lookup a dentry, creating it if it doesn't exist
2327 * @nd: nameidata info
2328 * @is_dir: directory flag
2329 *
2330 * Simple function to lookup and return a dentry and create it
2331 * if it doesn't exist. Is SMP-safe.
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002332 *
Jan Blunck4ac91372008-02-14 19:34:32 -08002333 * Returns with nd->path.dentry->d_inode->i_mutex locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 */
2335struct dentry *lookup_create(struct nameidata *nd, int is_dir)
2336{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002337 struct dentry *dentry = ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
Jan Blunck4ac91372008-02-14 19:34:32 -08002339 mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002340 /*
2341 * Yucky last component or no last component at all?
2342 * (foo/., foo/.., /////)
2343 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 if (nd->last_type != LAST_NORM)
2345 goto fail;
2346 nd->flags &= ~LOOKUP_PARENT;
Al Viro35165862008-08-05 03:00:49 -04002347 nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
ASANO Masahiroa6349042006-08-22 20:06:02 -04002348 nd->intent.open.flags = O_EXCL;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002349
2350 /*
2351 * Do the final lookup.
2352 */
Christoph Hellwig49705b72005-11-08 21:35:06 -08002353 dentry = lookup_hash(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 if (IS_ERR(dentry))
2355 goto fail;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002356
Al Viroe9baf6e2008-05-15 04:49:12 -04002357 if (dentry->d_inode)
2358 goto eexist;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002359 /*
2360 * Special case - lookup gave negative, but... we had foo/bar/
2361 * From the vfs_mknod() POV we just have a negative dentry -
2362 * all is fine. Let's be bastards - you had / on the end, you've
2363 * been asking for (non-existent) directory. -ENOENT for you.
2364 */
Al Viroe9baf6e2008-05-15 04:49:12 -04002365 if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
2366 dput(dentry);
2367 dentry = ERR_PTR(-ENOENT);
2368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 return dentry;
Al Viroe9baf6e2008-05-15 04:49:12 -04002370eexist:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 dput(dentry);
Al Viroe9baf6e2008-05-15 04:49:12 -04002372 dentry = ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373fail:
2374 return dentry;
2375}
Christoph Hellwigf81a0bf2005-05-19 12:26:43 -07002376EXPORT_SYMBOL_GPL(lookup_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
2378int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
2379{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002380 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
2382 if (error)
2383 return error;
2384
Serge E. Hallyne795b712011-03-23 16:43:25 -07002385 if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
2386 !ns_capable(inode_userns(dir), CAP_MKNOD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 return -EPERM;
2388
Al Viroacfa4382008-12-04 10:06:33 -05002389 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 return -EPERM;
2391
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07002392 error = devcgroup_inode_mknod(mode, dev);
2393 if (error)
2394 return error;
2395
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 error = security_inode_mknod(dir, dentry, mode, dev);
2397 if (error)
2398 return error;
2399
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002401 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002402 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 return error;
2404}
2405
Dave Hansen463c3192008-02-15 14:37:57 -08002406static int may_mknod(mode_t mode)
2407{
2408 switch (mode & S_IFMT) {
2409 case S_IFREG:
2410 case S_IFCHR:
2411 case S_IFBLK:
2412 case S_IFIFO:
2413 case S_IFSOCK:
2414 case 0: /* zero mode translates to S_IFREG */
2415 return 0;
2416 case S_IFDIR:
2417 return -EPERM;
2418 default:
2419 return -EINVAL;
2420 }
2421}
2422
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002423SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
2424 unsigned, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425{
Al Viro2ad94ae2008-07-21 09:32:51 -04002426 int error;
2427 char *tmp;
2428 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 struct nameidata nd;
2430
2431 if (S_ISDIR(mode))
2432 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
Al Viro2ad94ae2008-07-21 09:32:51 -04002434 error = user_path_parent(dfd, filename, &nd, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002436 return error;
2437
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 dentry = lookup_create(&nd, 0);
Dave Hansen463c3192008-02-15 14:37:57 -08002439 if (IS_ERR(dentry)) {
2440 error = PTR_ERR(dentry);
2441 goto out_unlock;
2442 }
Jan Blunck4ac91372008-02-14 19:34:32 -08002443 if (!IS_POSIXACL(nd.path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04002444 mode &= ~current_umask();
Dave Hansen463c3192008-02-15 14:37:57 -08002445 error = may_mknod(mode);
2446 if (error)
2447 goto out_dput;
2448 error = mnt_want_write(nd.path.mnt);
2449 if (error)
2450 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002451 error = security_path_mknod(&nd.path, dentry, mode, dev);
2452 if (error)
2453 goto out_drop_write;
Dave Hansen463c3192008-02-15 14:37:57 -08002454 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 case 0: case S_IFREG:
Jan Blunck4ac91372008-02-14 19:34:32 -08002456 error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 break;
2458 case S_IFCHR: case S_IFBLK:
Jan Blunck4ac91372008-02-14 19:34:32 -08002459 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 new_decode_dev(dev));
2461 break;
2462 case S_IFIFO: case S_IFSOCK:
Jan Blunck4ac91372008-02-14 19:34:32 -08002463 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 }
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002466out_drop_write:
Dave Hansen463c3192008-02-15 14:37:57 -08002467 mnt_drop_write(nd.path.mnt);
2468out_dput:
2469 dput(dentry);
2470out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002471 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002472 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 putname(tmp);
2474
2475 return error;
2476}
2477
Heiko Carstens3480b252009-01-14 14:14:16 +01002478SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002479{
2480 return sys_mknodat(AT_FDCWD, filename, mode, dev);
2481}
2482
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2484{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002485 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
2487 if (error)
2488 return error;
2489
Al Viroacfa4382008-12-04 10:06:33 -05002490 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 return -EPERM;
2492
2493 mode &= (S_IRWXUGO|S_ISVTX);
2494 error = security_inode_mkdir(dir, dentry, mode);
2495 if (error)
2496 return error;
2497
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002499 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002500 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 return error;
2502}
2503
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002504SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505{
2506 int error = 0;
2507 char * tmp;
Dave Hansen6902d922006-09-30 23:29:01 -07002508 struct dentry *dentry;
2509 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
Al Viro2ad94ae2008-07-21 09:32:51 -04002511 error = user_path_parent(dfd, pathname, &nd, &tmp);
2512 if (error)
Dave Hansen6902d922006-09-30 23:29:01 -07002513 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514
Dave Hansen6902d922006-09-30 23:29:01 -07002515 dentry = lookup_create(&nd, 1);
2516 error = PTR_ERR(dentry);
2517 if (IS_ERR(dentry))
2518 goto out_unlock;
2519
Jan Blunck4ac91372008-02-14 19:34:32 -08002520 if (!IS_POSIXACL(nd.path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04002521 mode &= ~current_umask();
Dave Hansen463c3192008-02-15 14:37:57 -08002522 error = mnt_want_write(nd.path.mnt);
2523 if (error)
2524 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002525 error = security_path_mkdir(&nd.path, dentry, mode);
2526 if (error)
2527 goto out_drop_write;
Jan Blunck4ac91372008-02-14 19:34:32 -08002528 error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002529out_drop_write:
Dave Hansen463c3192008-02-15 14:37:57 -08002530 mnt_drop_write(nd.path.mnt);
2531out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002532 dput(dentry);
2533out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002534 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002535 path_put(&nd.path);
Dave Hansen6902d922006-09-30 23:29:01 -07002536 putname(tmp);
2537out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 return error;
2539}
2540
Heiko Carstens3cdad422009-01-14 14:14:22 +01002541SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002542{
2543 return sys_mkdirat(AT_FDCWD, pathname, mode);
2544}
2545
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546/*
Sage Weila71905f2011-05-24 13:06:08 -07002547 * The dentry_unhash() helper will try to drop the dentry early: we
2548 * should have a usage count of 2 if we're the only user of this
2549 * dentry, and if that is true (possibly after pruning the dcache),
2550 * then we drop the dentry now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 *
2552 * A low-level filesystem can, if it choses, legally
2553 * do a
2554 *
2555 * if (!d_unhashed(dentry))
2556 * return -EBUSY;
2557 *
2558 * if it cannot handle the case of removing a directory
2559 * that is still in use by something else..
2560 */
2561void dentry_unhash(struct dentry *dentry)
2562{
Vasily Averindc168422006-12-06 20:37:07 -08002563 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 spin_lock(&dentry->d_lock);
Sage Weil64252c72011-05-24 13:06:05 -07002565 if (dentry->d_count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 __d_drop(dentry);
2567 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568}
2569
2570int vfs_rmdir(struct inode *dir, struct dentry *dentry)
2571{
2572 int error = may_delete(dir, dentry, 1);
2573
2574 if (error)
2575 return error;
2576
Al Viroacfa4382008-12-04 10:06:33 -05002577 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 return -EPERM;
2579
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002580 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
Sage Weil912dbc12011-05-24 13:06:11 -07002582 error = -EBUSY;
2583 if (d_mountpoint(dentry))
2584 goto out;
2585
2586 error = security_inode_rmdir(dir, dentry);
2587 if (error)
2588 goto out;
2589
Sage Weil3cebde22011-05-29 21:20:59 -07002590 shrink_dcache_parent(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07002591 error = dir->i_op->rmdir(dir, dentry);
2592 if (error)
2593 goto out;
2594
2595 dentry->d_inode->i_flags |= S_DEAD;
2596 dont_mount(dentry);
2597
2598out:
2599 mutex_unlock(&dentry->d_inode->i_mutex);
2600 if (!error)
2601 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 return error;
2603}
2604
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002605static long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606{
2607 int error = 0;
2608 char * name;
2609 struct dentry *dentry;
2610 struct nameidata nd;
2611
Al Viro2ad94ae2008-07-21 09:32:51 -04002612 error = user_path_parent(dfd, pathname, &nd, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002614 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
2616 switch(nd.last_type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002617 case LAST_DOTDOT:
2618 error = -ENOTEMPTY;
2619 goto exit1;
2620 case LAST_DOT:
2621 error = -EINVAL;
2622 goto exit1;
2623 case LAST_ROOT:
2624 error = -EBUSY;
2625 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002627
2628 nd.flags &= ~LOOKUP_PARENT;
2629
Jan Blunck4ac91372008-02-14 19:34:32 -08002630 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002631 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002633 if (IS_ERR(dentry))
2634 goto exit2;
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04002635 if (!dentry->d_inode) {
2636 error = -ENOENT;
2637 goto exit3;
2638 }
Dave Hansen06227532008-02-15 14:37:34 -08002639 error = mnt_want_write(nd.path.mnt);
2640 if (error)
2641 goto exit3;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002642 error = security_path_rmdir(&nd.path, dentry);
2643 if (error)
2644 goto exit4;
Jan Blunck4ac91372008-02-14 19:34:32 -08002645 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002646exit4:
Dave Hansen06227532008-02-15 14:37:34 -08002647 mnt_drop_write(nd.path.mnt);
2648exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07002649 dput(dentry);
2650exit2:
Jan Blunck4ac91372008-02-14 19:34:32 -08002651 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002653 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 putname(name);
2655 return error;
2656}
2657
Heiko Carstens3cdad422009-01-14 14:14:22 +01002658SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002659{
2660 return do_rmdir(AT_FDCWD, pathname);
2661}
2662
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663int vfs_unlink(struct inode *dir, struct dentry *dentry)
2664{
2665 int error = may_delete(dir, dentry, 0);
2666
2667 if (error)
2668 return error;
2669
Al Viroacfa4382008-12-04 10:06:33 -05002670 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 return -EPERM;
2672
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002673 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 if (d_mountpoint(dentry))
2675 error = -EBUSY;
2676 else {
2677 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05002678 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 error = dir->i_op->unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05002680 if (!error)
Al Virod83c49f2010-04-30 17:17:09 -04002681 dont_mount(dentry);
Al Virobec10522010-03-03 14:12:08 -05002682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002684 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685
2686 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2687 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
Jan Karaece95912008-02-06 01:37:13 -08002688 fsnotify_link_count(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 }
Robert Love0eeca282005-07-12 17:06:03 -04002691
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 return error;
2693}
2694
2695/*
2696 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002697 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 * writeout happening, and we don't want to prevent access to the directory
2699 * while waiting on the I/O.
2700 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002701static long do_unlinkat(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702{
Al Viro2ad94ae2008-07-21 09:32:51 -04002703 int error;
2704 char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 struct dentry *dentry;
2706 struct nameidata nd;
2707 struct inode *inode = NULL;
2708
Al Viro2ad94ae2008-07-21 09:32:51 -04002709 error = user_path_parent(dfd, pathname, &nd, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002711 return error;
2712
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 error = -EISDIR;
2714 if (nd.last_type != LAST_NORM)
2715 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002716
2717 nd.flags &= ~LOOKUP_PARENT;
2718
Jan Blunck4ac91372008-02-14 19:34:32 -08002719 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002720 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 error = PTR_ERR(dentry);
2722 if (!IS_ERR(dentry)) {
2723 /* Why not before? Because we want correct error value */
Török Edwin50338b82011-06-16 00:06:14 +03002724 if (nd.last.name[nd.last.len])
2725 goto slashes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 inode = dentry->d_inode;
Török Edwin50338b82011-06-16 00:06:14 +03002727 if (!inode)
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04002728 goto slashes;
2729 ihold(inode);
Dave Hansen06227532008-02-15 14:37:34 -08002730 error = mnt_want_write(nd.path.mnt);
2731 if (error)
2732 goto exit2;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002733 error = security_path_unlink(&nd.path, dentry);
2734 if (error)
2735 goto exit3;
Jan Blunck4ac91372008-02-14 19:34:32 -08002736 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002737exit3:
Dave Hansen06227532008-02-15 14:37:34 -08002738 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 exit2:
2740 dput(dentry);
2741 }
Jan Blunck4ac91372008-02-14 19:34:32 -08002742 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 if (inode)
2744 iput(inode); /* truncate the inode here */
2745exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002746 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 putname(name);
2748 return error;
2749
2750slashes:
2751 error = !dentry->d_inode ? -ENOENT :
2752 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2753 goto exit2;
2754}
2755
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002756SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002757{
2758 if ((flag & ~AT_REMOVEDIR) != 0)
2759 return -EINVAL;
2760
2761 if (flag & AT_REMOVEDIR)
2762 return do_rmdir(dfd, pathname);
2763
2764 return do_unlinkat(dfd, pathname);
2765}
2766
Heiko Carstens3480b252009-01-14 14:14:16 +01002767SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002768{
2769 return do_unlinkat(AT_FDCWD, pathname);
2770}
2771
Miklos Szeredidb2e7472008-06-24 16:50:16 +02002772int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002774 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775
2776 if (error)
2777 return error;
2778
Al Viroacfa4382008-12-04 10:06:33 -05002779 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 return -EPERM;
2781
2782 error = security_inode_symlink(dir, dentry, oldname);
2783 if (error)
2784 return error;
2785
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002787 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002788 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 return error;
2790}
2791
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002792SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
2793 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794{
Al Viro2ad94ae2008-07-21 09:32:51 -04002795 int error;
2796 char *from;
2797 char *to;
Dave Hansen6902d922006-09-30 23:29:01 -07002798 struct dentry *dentry;
2799 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800
2801 from = getname(oldname);
Al Viro2ad94ae2008-07-21 09:32:51 -04002802 if (IS_ERR(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 return PTR_ERR(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04002804
2805 error = user_path_parent(newdfd, newname, &nd, &to);
2806 if (error)
Dave Hansen6902d922006-09-30 23:29:01 -07002807 goto out_putname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808
Dave Hansen6902d922006-09-30 23:29:01 -07002809 dentry = lookup_create(&nd, 0);
2810 error = PTR_ERR(dentry);
2811 if (IS_ERR(dentry))
2812 goto out_unlock;
2813
Dave Hansen75c3f292008-02-15 14:37:45 -08002814 error = mnt_want_write(nd.path.mnt);
2815 if (error)
2816 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002817 error = security_path_symlink(&nd.path, dentry, from);
2818 if (error)
2819 goto out_drop_write;
Miklos Szeredidb2e7472008-06-24 16:50:16 +02002820 error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002821out_drop_write:
Dave Hansen75c3f292008-02-15 14:37:45 -08002822 mnt_drop_write(nd.path.mnt);
2823out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002824 dput(dentry);
2825out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002826 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002827 path_put(&nd.path);
Dave Hansen6902d922006-09-30 23:29:01 -07002828 putname(to);
2829out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 putname(from);
2831 return error;
2832}
2833
Heiko Carstens3480b252009-01-14 14:14:16 +01002834SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002835{
2836 return sys_symlinkat(oldname, AT_FDCWD, newname);
2837}
2838
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2840{
2841 struct inode *inode = old_dentry->d_inode;
2842 int error;
2843
2844 if (!inode)
2845 return -ENOENT;
2846
Miklos Szeredia95164d2008-07-30 15:08:48 +02002847 error = may_create(dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 if (error)
2849 return error;
2850
2851 if (dir->i_sb != inode->i_sb)
2852 return -EXDEV;
2853
2854 /*
2855 * A link to an append-only or immutable file cannot be created.
2856 */
2857 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2858 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05002859 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002861 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 return -EPERM;
2863
2864 error = security_inode_link(old_dentry, dir, new_dentry);
2865 if (error)
2866 return error;
2867
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002868 mutex_lock(&inode->i_mutex);
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05302869 /* Make sure we don't allow creating hardlink to an unlinked file */
2870 if (inode->i_nlink == 0)
2871 error = -ENOENT;
2872 else
2873 error = dir->i_op->link(old_dentry, dir, new_dentry);
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002874 mutex_unlock(&inode->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07002875 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002876 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 return error;
2878}
2879
2880/*
2881 * Hardlinks are often used in delicate situations. We avoid
2882 * security-related surprises by not following symlinks on the
2883 * newname. --KAB
2884 *
2885 * We don't follow them on the oldname either to be compatible
2886 * with linux 2.0, and to avoid hard-linking to directories
2887 * and other special files. --ADM
2888 */
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002889SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
2890 int, newdfd, const char __user *, newname, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891{
2892 struct dentry *new_dentry;
Al Viro2d8f3032008-07-22 09:59:21 -04002893 struct nameidata nd;
2894 struct path old_path;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05302895 int how = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 int error;
Al Viro2ad94ae2008-07-21 09:32:51 -04002897 char *to;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05302899 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002900 return -EINVAL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05302901 /*
2902 * To use null names we require CAP_DAC_READ_SEARCH
2903 * This ensures that not everyone will be able to create
2904 * handlink using the passed filedescriptor.
2905 */
2906 if (flags & AT_EMPTY_PATH) {
2907 if (!capable(CAP_DAC_READ_SEARCH))
2908 return -ENOENT;
2909 how = LOOKUP_EMPTY;
2910 }
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002911
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05302912 if (flags & AT_SYMLINK_FOLLOW)
2913 how |= LOOKUP_FOLLOW;
2914
2915 error = user_path_at(olddfd, oldname, how, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002917 return error;
2918
2919 error = user_path_parent(newdfd, newname, &nd, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 if (error)
2921 goto out;
2922 error = -EXDEV;
Al Viro2d8f3032008-07-22 09:59:21 -04002923 if (old_path.mnt != nd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 goto out_release;
2925 new_dentry = lookup_create(&nd, 0);
2926 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002927 if (IS_ERR(new_dentry))
2928 goto out_unlock;
Dave Hansen75c3f292008-02-15 14:37:45 -08002929 error = mnt_want_write(nd.path.mnt);
2930 if (error)
2931 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002932 error = security_path_link(old_path.dentry, &nd.path, new_dentry);
2933 if (error)
2934 goto out_drop_write;
Al Viro2d8f3032008-07-22 09:59:21 -04002935 error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002936out_drop_write:
Dave Hansen75c3f292008-02-15 14:37:45 -08002937 mnt_drop_write(nd.path.mnt);
2938out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002939 dput(new_dentry);
2940out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002941 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942out_release:
Jan Blunck1d957f92008-02-14 19:34:35 -08002943 path_put(&nd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04002944 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945out:
Al Viro2d8f3032008-07-22 09:59:21 -04002946 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947
2948 return error;
2949}
2950
Heiko Carstens3480b252009-01-14 14:14:16 +01002951SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002952{
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002953 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002954}
2955
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956/*
2957 * The worst of all namespace operations - renaming directory. "Perverted"
2958 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2959 * Problems:
2960 * a) we can get into loop creation. Check is done in is_subdir().
2961 * b) race potential - two innocent renames can create a loop together.
2962 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002963 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 * story.
2965 * c) we have to lock _three_ objects - parents and victim (if it exists).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002966 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 * whether the target exists). Solution: try to be smart with locking
2968 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002969 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 * move will be locked. Thus we can rank directories by the tree
2971 * (ancestors first) and rank all non-directories after them.
2972 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002973 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 * HOWEVER, it relies on the assumption that any object with ->lookup()
2975 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2976 * we'd better make sure that there's no link(2) for them.
Sage Weile4eaac02011-05-24 13:06:07 -07002977 * d) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002978 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05002980 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 * locking].
2982 */
Adrian Bunk75c96f82005-05-05 16:16:09 -07002983static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
2984 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985{
2986 int error = 0;
Sage Weil9055cba2011-05-24 13:06:12 -07002987 struct inode *target = new_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988
2989 /*
2990 * If we are going to change the parent - check write permissions,
2991 * we'll need to flip '..'.
2992 */
2993 if (new_dir != old_dir) {
Al Virof419a2e2008-07-22 00:07:17 -04002994 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 if (error)
2996 return error;
2997 }
2998
2999 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3000 if (error)
3001 return error;
3002
Al Virod83c49f2010-04-30 17:17:09 -04003003 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003004 mutex_lock(&target->i_mutex);
Sage Weil9055cba2011-05-24 13:06:12 -07003005
3006 error = -EBUSY;
3007 if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
3008 goto out;
3009
Sage Weil3cebde22011-05-29 21:20:59 -07003010 if (target)
3011 shrink_dcache_parent(new_dentry);
Sage Weil9055cba2011-05-24 13:06:12 -07003012 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3013 if (error)
3014 goto out;
3015
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 if (target) {
Sage Weil9055cba2011-05-24 13:06:12 -07003017 target->i_flags |= S_DEAD;
3018 dont_mount(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 }
Sage Weil9055cba2011-05-24 13:06:12 -07003020out:
3021 if (target)
3022 mutex_unlock(&target->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07003023 if (!error)
Mark Fasheh349457c2006-09-08 14:22:21 -07003024 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3025 d_move(old_dentry,new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 return error;
3027}
3028
Adrian Bunk75c96f82005-05-05 16:16:09 -07003029static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
3030 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031{
Sage Weil51892bb2011-05-24 13:06:13 -07003032 struct inode *target = new_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 int error;
3034
3035 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3036 if (error)
3037 return error;
3038
3039 dget(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003041 mutex_lock(&target->i_mutex);
Sage Weil51892bb2011-05-24 13:06:13 -07003042
3043 error = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
Sage Weil51892bb2011-05-24 13:06:13 -07003045 goto out;
3046
3047 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3048 if (error)
3049 goto out;
3050
3051 if (target)
3052 dont_mount(new_dentry);
3053 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3054 d_move(old_dentry, new_dentry);
3055out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003057 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 dput(new_dentry);
3059 return error;
3060}
3061
3062int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
3063 struct inode *new_dir, struct dentry *new_dentry)
3064{
3065 int error;
3066 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
Eric Paris59b0df22010-02-08 12:53:52 -05003067 const unsigned char *old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068
3069 if (old_dentry->d_inode == new_dentry->d_inode)
3070 return 0;
3071
3072 error = may_delete(old_dir, old_dentry, is_dir);
3073 if (error)
3074 return error;
3075
3076 if (!new_dentry->d_inode)
Miklos Szeredia95164d2008-07-30 15:08:48 +02003077 error = may_create(new_dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 else
3079 error = may_delete(new_dir, new_dentry, is_dir);
3080 if (error)
3081 return error;
3082
Al Viroacfa4382008-12-04 10:06:33 -05003083 if (!old_dir->i_op->rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 return -EPERM;
3085
Robert Love0eeca282005-07-12 17:06:03 -04003086 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
3087
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 if (is_dir)
3089 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
3090 else
3091 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
Al Viro123df292009-12-25 04:57:57 -05003092 if (!error)
3093 fsnotify_move(old_dir, new_dir, old_name, is_dir,
Al Viro5a190ae2007-06-07 12:19:32 -04003094 new_dentry->d_inode, old_dentry);
Robert Love0eeca282005-07-12 17:06:03 -04003095 fsnotify_oldname_free(old_name);
3096
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 return error;
3098}
3099
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003100SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
3101 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102{
Al Viro2ad94ae2008-07-21 09:32:51 -04003103 struct dentry *old_dir, *new_dir;
3104 struct dentry *old_dentry, *new_dentry;
3105 struct dentry *trap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 struct nameidata oldnd, newnd;
Al Viro2ad94ae2008-07-21 09:32:51 -04003107 char *from;
3108 char *to;
3109 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110
Al Viro2ad94ae2008-07-21 09:32:51 -04003111 error = user_path_parent(olddfd, oldname, &oldnd, &from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 if (error)
3113 goto exit;
3114
Al Viro2ad94ae2008-07-21 09:32:51 -04003115 error = user_path_parent(newdfd, newname, &newnd, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 if (error)
3117 goto exit1;
3118
3119 error = -EXDEV;
Jan Blunck4ac91372008-02-14 19:34:32 -08003120 if (oldnd.path.mnt != newnd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 goto exit2;
3122
Jan Blunck4ac91372008-02-14 19:34:32 -08003123 old_dir = oldnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 error = -EBUSY;
3125 if (oldnd.last_type != LAST_NORM)
3126 goto exit2;
3127
Jan Blunck4ac91372008-02-14 19:34:32 -08003128 new_dir = newnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 if (newnd.last_type != LAST_NORM)
3130 goto exit2;
3131
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003132 oldnd.flags &= ~LOOKUP_PARENT;
3133 newnd.flags &= ~LOOKUP_PARENT;
OGAWA Hirofumi4e9ed2f2008-10-16 07:50:29 +09003134 newnd.flags |= LOOKUP_RENAME_TARGET;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003135
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 trap = lock_rename(new_dir, old_dir);
3137
Christoph Hellwig49705b72005-11-08 21:35:06 -08003138 old_dentry = lookup_hash(&oldnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 error = PTR_ERR(old_dentry);
3140 if (IS_ERR(old_dentry))
3141 goto exit3;
3142 /* source must exist */
3143 error = -ENOENT;
3144 if (!old_dentry->d_inode)
3145 goto exit4;
3146 /* unless the source is a directory trailing slashes give -ENOTDIR */
3147 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
3148 error = -ENOTDIR;
3149 if (oldnd.last.name[oldnd.last.len])
3150 goto exit4;
3151 if (newnd.last.name[newnd.last.len])
3152 goto exit4;
3153 }
3154 /* source should not be ancestor of target */
3155 error = -EINVAL;
3156 if (old_dentry == trap)
3157 goto exit4;
Christoph Hellwig49705b72005-11-08 21:35:06 -08003158 new_dentry = lookup_hash(&newnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 error = PTR_ERR(new_dentry);
3160 if (IS_ERR(new_dentry))
3161 goto exit4;
3162 /* target should not be an ancestor of source */
3163 error = -ENOTEMPTY;
3164 if (new_dentry == trap)
3165 goto exit5;
3166
Dave Hansen9079b1e2008-02-15 14:37:49 -08003167 error = mnt_want_write(oldnd.path.mnt);
3168 if (error)
3169 goto exit5;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003170 error = security_path_rename(&oldnd.path, old_dentry,
3171 &newnd.path, new_dentry);
3172 if (error)
3173 goto exit6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 error = vfs_rename(old_dir->d_inode, old_dentry,
3175 new_dir->d_inode, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003176exit6:
Dave Hansen9079b1e2008-02-15 14:37:49 -08003177 mnt_drop_write(oldnd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178exit5:
3179 dput(new_dentry);
3180exit4:
3181 dput(old_dentry);
3182exit3:
3183 unlock_rename(new_dir, old_dir);
3184exit2:
Jan Blunck1d957f92008-02-14 19:34:35 -08003185 path_put(&newnd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04003186 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003188 path_put(&oldnd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 putname(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04003190exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 return error;
3192}
3193
Heiko Carstensa26eab22009-01-14 14:14:17 +01003194SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003195{
3196 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
3197}
3198
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
3200{
3201 int len;
3202
3203 len = PTR_ERR(link);
3204 if (IS_ERR(link))
3205 goto out;
3206
3207 len = strlen(link);
3208 if (len > (unsigned) buflen)
3209 len = buflen;
3210 if (copy_to_user(buffer, link, len))
3211 len = -EFAULT;
3212out:
3213 return len;
3214}
3215
3216/*
3217 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
3218 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
3219 * using) it for any given inode is up to filesystem.
3220 */
3221int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3222{
3223 struct nameidata nd;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003224 void *cookie;
Marcin Slusarz694a1762008-06-09 16:40:37 -07003225 int res;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003226
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 nd.depth = 0;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003228 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
Marcin Slusarz694a1762008-06-09 16:40:37 -07003229 if (IS_ERR(cookie))
3230 return PTR_ERR(cookie);
3231
3232 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
3233 if (dentry->d_inode->i_op->put_link)
3234 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3235 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236}
3237
3238int vfs_follow_link(struct nameidata *nd, const char *link)
3239{
3240 return __vfs_follow_link(nd, link);
3241}
3242
3243/* get the link contents into pagecache */
3244static char *page_getlink(struct dentry * dentry, struct page **ppage)
3245{
Duane Griffinebd09ab2008-12-19 20:47:12 +00003246 char *kaddr;
3247 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 struct address_space *mapping = dentry->d_inode->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -07003249 page = read_mapping_page(mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 if (IS_ERR(page))
Nick Piggin6fe69002007-05-06 14:49:04 -07003251 return (char*)page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 *ppage = page;
Duane Griffinebd09ab2008-12-19 20:47:12 +00003253 kaddr = kmap(page);
3254 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3255 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256}
3257
3258int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3259{
3260 struct page *page = NULL;
3261 char *s = page_getlink(dentry, &page);
3262 int res = vfs_readlink(dentry,buffer,buflen,s);
3263 if (page) {
3264 kunmap(page);
3265 page_cache_release(page);
3266 }
3267 return res;
3268}
3269
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003270void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003272 struct page *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 nd_set_link(nd, page_getlink(dentry, &page));
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003274 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275}
3276
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003277void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003279 struct page *page = cookie;
3280
3281 if (page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 kunmap(page);
3283 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 }
3285}
3286
Nick Piggin54566b22009-01-04 12:00:53 -08003287/*
3288 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
3289 */
3290int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291{
3292 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003293 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07003294 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08003295 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 char *kaddr;
Nick Piggin54566b22009-01-04 12:00:53 -08003297 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
3298 if (nofs)
3299 flags |= AOP_FLAG_NOFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300
NeilBrown7e53cac2006-03-25 03:07:57 -08003301retry:
Nick Pigginafddba42007-10-16 01:25:01 -07003302 err = pagecache_write_begin(NULL, mapping, 0, len-1,
Nick Piggin54566b22009-01-04 12:00:53 -08003303 flags, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07003305 goto fail;
3306
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 kaddr = kmap_atomic(page, KM_USER0);
3308 memcpy(kaddr, symname, len-1);
3309 kunmap_atomic(kaddr, KM_USER0);
Nick Pigginafddba42007-10-16 01:25:01 -07003310
3311 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3312 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 if (err < 0)
3314 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07003315 if (err < len-1)
3316 goto retry;
3317
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 mark_inode_dirty(inode);
3319 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320fail:
3321 return err;
3322}
3323
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003324int page_symlink(struct inode *inode, const char *symname, int len)
3325{
3326 return __page_symlink(inode, symname, len,
Nick Piggin54566b22009-01-04 12:00:53 -08003327 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003328}
3329
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003330const struct inode_operations page_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 .readlink = generic_readlink,
3332 .follow_link = page_follow_link_light,
3333 .put_link = page_put_link,
3334};
3335
Al Viro2d8f3032008-07-22 09:59:21 -04003336EXPORT_SYMBOL(user_path_at);
David Howellscc53ce52011-01-14 18:45:26 +00003337EXPORT_SYMBOL(follow_down_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338EXPORT_SYMBOL(follow_down);
3339EXPORT_SYMBOL(follow_up);
3340EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
3341EXPORT_SYMBOL(getname);
3342EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343EXPORT_SYMBOL(lookup_one_len);
3344EXPORT_SYMBOL(page_follow_link_light);
3345EXPORT_SYMBOL(page_put_link);
3346EXPORT_SYMBOL(page_readlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003347EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348EXPORT_SYMBOL(page_symlink);
3349EXPORT_SYMBOL(page_symlink_inode_operations);
Al Viroc9c6cac2011-02-16 15:15:47 -05003350EXPORT_SYMBOL(kern_path_parent);
Al Virod1811462008-08-02 00:49:18 -04003351EXPORT_SYMBOL(kern_path);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07003352EXPORT_SYMBOL(vfs_path_lookup);
Al Virof419a2e2008-07-22 00:07:17 -04003353EXPORT_SYMBOL(inode_permission);
Christoph Hellwig8c744fb2005-11-08 21:35:04 -08003354EXPORT_SYMBOL(file_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355EXPORT_SYMBOL(unlock_rename);
3356EXPORT_SYMBOL(vfs_create);
3357EXPORT_SYMBOL(vfs_follow_link);
3358EXPORT_SYMBOL(vfs_link);
3359EXPORT_SYMBOL(vfs_mkdir);
3360EXPORT_SYMBOL(vfs_mknod);
3361EXPORT_SYMBOL(generic_permission);
3362EXPORT_SYMBOL(vfs_readlink);
3363EXPORT_SYMBOL(vfs_rename);
3364EXPORT_SYMBOL(vfs_rmdir);
3365EXPORT_SYMBOL(vfs_symlink);
3366EXPORT_SYMBOL(vfs_unlink);
3367EXPORT_SYMBOL(dentry_unhash);
3368EXPORT_SYMBOL(generic_readlink);