blob: 49472a1b79e74f7a413c9ee7be2dd2d261da383d [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
Andy Whitcroftb0917c32011-11-02 09:44:39 +0100139static char *getname_flags(const char __user *filename, int flags, int *empty)
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) {
Andy Whitcroftb0917c32011-11-02 09:44:39 +0100150 if (retval == -ENOENT && empty)
151 *empty = 1;
Al Virof52e0c12011-03-14 18:56:51 -0400152 if (retval != -ENOENT || !(flags & LOOKUP_EMPTY)) {
153 __putname(tmp);
154 result = ERR_PTR(retval);
155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 }
157 }
158 audit_getname(result);
159 return result;
160}
161
Al Virof52e0c12011-03-14 18:56:51 -0400162char *getname(const char __user * filename)
163{
Andy Whitcroftb0917c32011-11-02 09:44:39 +0100164 return getname_flags(filename, 0, 0);
Al Virof52e0c12011-03-14 18:56:51 -0400165}
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167#ifdef CONFIG_AUDITSYSCALL
168void putname(const char *name)
169{
Al Viro5ac3a9c2006-07-16 06:38:45 -0400170 if (unlikely(!audit_dummy_context()))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 audit_putname(name);
172 else
173 __putname(name);
174}
175EXPORT_SYMBOL(putname);
176#endif
177
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700178/*
179 * This does basic POSIX ACL permission checking
180 */
Nick Pigginb74c79e2011-01-07 17:49:58 +1100181static int acl_permission_check(struct inode *inode, int mask, unsigned int flags,
182 int (*check_acl)(struct inode *inode, int mask, unsigned int flags))
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700183{
Linus Torvalds26cf46b2011-05-13 11:51:01 -0700184 unsigned int mode = inode->i_mode;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700185
186 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
187
Serge E. Hallyne795b712011-03-23 16:43:25 -0700188 if (current_user_ns() != inode_userns(inode))
189 goto other_perms;
190
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700191 if (current_fsuid() == inode->i_uid)
192 mode >>= 6;
193 else {
194 if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
Nick Pigginb74c79e2011-01-07 17:49:58 +1100195 int error = check_acl(inode, mask, flags);
196 if (error != -EAGAIN)
197 return error;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700198 }
199
200 if (in_group_p(inode->i_gid))
201 mode >>= 3;
202 }
203
Serge E. Hallyne795b712011-03-23 16:43:25 -0700204other_perms:
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700205 /*
206 * If the DACs are ok we don't need any capability check.
207 */
208 if ((mask & ~mode) == 0)
209 return 0;
210 return -EACCES;
211}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213/**
Nick Pigginb74c79e2011-01-07 17:49:58 +1100214 * generic_permission - check for access rights on a Posix-like filesystem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 * @inode: inode to check access rights for
216 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
217 * @check_acl: optional callback to check for Posix ACLs
Randy Dunlap39191622011-01-08 19:36:21 -0800218 * @flags: IPERM_FLAG_ flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 *
220 * Used to check for read/write/execute permissions on a file.
221 * We use "fsuid" for this, letting us set arbitrary permissions
222 * for filesystem access without changing the "normal" uids which
Nick Pigginb74c79e2011-01-07 17:49:58 +1100223 * are used for other things.
224 *
225 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
226 * request cannot be satisfied (eg. requires blocking or too much complexity).
227 * It would then be called again in ref-walk mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 */
Nick Pigginb74c79e2011-01-07 17:49:58 +1100229int generic_permission(struct inode *inode, int mask, unsigned int flags,
230 int (*check_acl)(struct inode *inode, int mask, unsigned int flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700232 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 /*
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700235 * Do the basic POSIX ACL permission checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 */
Nick Pigginb74c79e2011-01-07 17:49:58 +1100237 ret = acl_permission_check(inode, mask, flags, check_acl);
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700238 if (ret != -EACCES)
239 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 /*
242 * Read/write DACs are always overridable.
Al Viro8e833fd2011-06-19 01:56:53 -0400243 * Executable DACs are overridable for all directories and
244 * for non-directories that have least one exec bit set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 */
Miklos Szeredif696a362008-07-31 13:41:58 +0200246 if (!(mask & MAY_EXEC) || execute_ok(inode))
Serge E. Hallyne795b712011-03-23 16:43:25 -0700247 if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return 0;
249
250 /*
251 * Searching includes executable on directories, else just read.
252 */
Serge E. Hallyn7ea66002009-12-29 14:50:19 -0600253 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
Serge E. Hallyne795b712011-03-23 16:43:25 -0700255 if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return 0;
257
258 return -EACCES;
259}
260
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200261/**
262 * inode_permission - check for access rights to a given inode
263 * @inode: inode to check permission on
264 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
265 *
266 * Used to check for read/write/execute permissions on an inode.
267 * We use "fsuid" for this, letting us set arbitrary permissions
268 * for filesystem access without changing the "normal" uids which
269 * are used for other things.
270 */
Al Virof419a2e2008-07-22 00:07:17 -0400271int inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Al Viroe6305c42008-07-15 21:03:57 -0400273 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 if (mask & MAY_WRITE) {
Miklos Szeredi22590e42007-10-16 23:27:08 -0700276 umode_t mode = inode->i_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 /*
279 * Nobody gets write access to a read-only fs.
280 */
281 if (IS_RDONLY(inode) &&
282 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
283 return -EROFS;
284
285 /*
286 * Nobody gets write access to an immutable file.
287 */
288 if (IS_IMMUTABLE(inode))
289 return -EACCES;
290 }
291
Al Viroacfa4382008-12-04 10:06:33 -0500292 if (inode->i_op->permission)
Nick Pigginb74c79e2011-01-07 17:49:58 +1100293 retval = inode->i_op->permission(inode, mask, 0);
Miklos Szeredif696a362008-07-31 13:41:58 +0200294 else
Nick Pigginb74c79e2011-01-07 17:49:58 +1100295 retval = generic_permission(inode, mask, 0,
296 inode->i_op->check_acl);
Miklos Szeredif696a362008-07-31 13:41:58 +0200297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 if (retval)
299 return retval;
300
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700301 retval = devcgroup_inode_permission(inode, mask);
302 if (retval)
303 return retval;
304
Eric Parisd09ca732010-07-23 11:43:57 -0400305 return security_inode_permission(inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800308/**
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800309 * file_permission - check for additional access rights to a given file
310 * @file: file to check access rights for
311 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
312 *
313 * Used to check for read/write/execute permissions on an already opened
314 * file.
315 *
316 * Note:
317 * Do not use this function in new code. All access checks should
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200318 * be done using inode_permission().
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800319 */
320int file_permission(struct file *file, int mask)
321{
Al Virof419a2e2008-07-22 00:07:17 -0400322 return inode_permission(file->f_path.dentry->d_inode, mask);
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800323}
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325/*
326 * get_write_access() gets write permission for a file.
327 * put_write_access() releases this write permission.
328 * This is used for regular files.
329 * We cannot support write (and maybe mmap read-write shared) accesses and
330 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
331 * can have the following values:
332 * 0: no writers, no VM_DENYWRITE mappings
333 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
334 * > 0: (i_writecount) users are writing to the file.
335 *
336 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
337 * except for the cases where we don't hold i_writecount yet. Then we need to
338 * use {get,deny}_write_access() - these functions check the sign and refuse
339 * to do the change if sign is wrong. Exclusion between them is provided by
340 * the inode->i_lock spinlock.
341 */
342
343int get_write_access(struct inode * inode)
344{
345 spin_lock(&inode->i_lock);
346 if (atomic_read(&inode->i_writecount) < 0) {
347 spin_unlock(&inode->i_lock);
348 return -ETXTBSY;
349 }
350 atomic_inc(&inode->i_writecount);
351 spin_unlock(&inode->i_lock);
352
353 return 0;
354}
355
356int deny_write_access(struct file * file)
357{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800358 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 spin_lock(&inode->i_lock);
361 if (atomic_read(&inode->i_writecount) > 0) {
362 spin_unlock(&inode->i_lock);
363 return -ETXTBSY;
364 }
365 atomic_dec(&inode->i_writecount);
366 spin_unlock(&inode->i_lock);
367
368 return 0;
369}
370
Jan Blunck1d957f92008-02-14 19:34:35 -0800371/**
Jan Blunck5dd784d2008-02-14 19:34:38 -0800372 * path_get - get a reference to a path
373 * @path: path to get the reference to
374 *
375 * Given a path increment the reference count to the dentry and the vfsmount.
376 */
377void path_get(struct path *path)
378{
379 mntget(path->mnt);
380 dget(path->dentry);
381}
382EXPORT_SYMBOL(path_get);
383
384/**
Jan Blunck1d957f92008-02-14 19:34:35 -0800385 * path_put - put a reference to a path
386 * @path: path to put the reference to
387 *
388 * Given a path decrement the reference count to the dentry and the vfsmount.
389 */
390void path_put(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
Jan Blunck1d957f92008-02-14 19:34:35 -0800392 dput(path->dentry);
393 mntput(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
Jan Blunck1d957f92008-02-14 19:34:35 -0800395EXPORT_SYMBOL(path_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Al Viro19660af2011-03-25 10:32:48 -0400397/*
Nick Piggin31e6b012011-01-07 17:49:52 +1100398 * Path walking has 2 modes, rcu-walk and ref-walk (see
Al Viro19660af2011-03-25 10:32:48 -0400399 * Documentation/filesystems/path-lookup.txt). In situations when we can't
400 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
401 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
402 * mode. Refcounts are grabbed at the last known good point before rcu-walk
403 * got stuck, so ref-walk may continue from there. If this is not successful
404 * (eg. a seqcount has changed), then failure is returned and it's up to caller
405 * to restart the path walk from the beginning in ref-walk mode.
Nick Piggin31e6b012011-01-07 17:49:52 +1100406 */
Nick Piggin31e6b012011-01-07 17:49:52 +1100407
408/**
Al Viro19660af2011-03-25 10:32:48 -0400409 * unlazy_walk - try to switch to ref-walk mode.
410 * @nd: nameidata pathwalk data
411 * @dentry: child of nd->path.dentry or NULL
Randy Dunlap39191622011-01-08 19:36:21 -0800412 * Returns: 0 on success, -ECHILD on failure
Nick Piggin31e6b012011-01-07 17:49:52 +1100413 *
Al Viro19660af2011-03-25 10:32:48 -0400414 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
415 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
416 * @nd or NULL. Must be called from rcu-walk context.
Nick Piggin31e6b012011-01-07 17:49:52 +1100417 */
Al Viro19660af2011-03-25 10:32:48 -0400418static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
Nick Piggin31e6b012011-01-07 17:49:52 +1100419{
420 struct fs_struct *fs = current->fs;
421 struct dentry *parent = nd->path.dentry;
Al Viro5b6ca022011-03-09 23:04:47 -0500422 int want_root = 0;
Nick Piggin31e6b012011-01-07 17:49:52 +1100423
424 BUG_ON(!(nd->flags & LOOKUP_RCU));
Al Viro5b6ca022011-03-09 23:04:47 -0500425 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
426 want_root = 1;
Nick Piggin31e6b012011-01-07 17:49:52 +1100427 spin_lock(&fs->lock);
428 if (nd->root.mnt != fs->root.mnt ||
429 nd->root.dentry != fs->root.dentry)
430 goto err_root;
431 }
432 spin_lock(&parent->d_lock);
Al Viro19660af2011-03-25 10:32:48 -0400433 if (!dentry) {
434 if (!__d_rcu_to_refcount(parent, nd->seq))
435 goto err_parent;
436 BUG_ON(nd->inode != parent->d_inode);
437 } else {
Al Viro94c0d4e2011-07-12 21:40:23 -0400438 if (dentry->d_parent != parent)
439 goto err_parent;
Al Viro19660af2011-03-25 10:32:48 -0400440 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
441 if (!__d_rcu_to_refcount(dentry, nd->seq))
442 goto err_child;
443 /*
444 * If the sequence check on the child dentry passed, then
445 * the child has not been removed from its parent. This
446 * means the parent dentry must be valid and able to take
447 * a reference at this point.
448 */
449 BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
450 BUG_ON(!parent->d_count);
451 parent->d_count++;
452 spin_unlock(&dentry->d_lock);
453 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100454 spin_unlock(&parent->d_lock);
Al Viro5b6ca022011-03-09 23:04:47 -0500455 if (want_root) {
Nick Piggin31e6b012011-01-07 17:49:52 +1100456 path_get(&nd->root);
457 spin_unlock(&fs->lock);
458 }
459 mntget(nd->path.mnt);
460
461 rcu_read_unlock();
462 br_read_unlock(vfsmount_lock);
463 nd->flags &= ~LOOKUP_RCU;
464 return 0;
Al Viro19660af2011-03-25 10:32:48 -0400465
466err_child:
Nick Piggin31e6b012011-01-07 17:49:52 +1100467 spin_unlock(&dentry->d_lock);
Al Viro19660af2011-03-25 10:32:48 -0400468err_parent:
Nick Piggin31e6b012011-01-07 17:49:52 +1100469 spin_unlock(&parent->d_lock);
470err_root:
Al Viro5b6ca022011-03-09 23:04:47 -0500471 if (want_root)
Nick Piggin31e6b012011-01-07 17:49:52 +1100472 spin_unlock(&fs->lock);
473 return -ECHILD;
474}
475
Nick Piggin31e6b012011-01-07 17:49:52 +1100476/**
Trond Myklebust834f2a42005-10-18 14:20:16 -0700477 * release_open_intent - free up open intent resources
478 * @nd: pointer to nameidata
479 */
480void release_open_intent(struct nameidata *nd)
481{
Linus Torvalds2dab5972011-02-11 15:53:38 -0800482 struct file *file = nd->intent.open.file;
483
484 if (file && !IS_ERR(file)) {
485 if (file->f_path.dentry == NULL)
486 put_filp(file);
487 else
488 fput(file);
489 }
Trond Myklebust834f2a42005-10-18 14:20:16 -0700490}
491
Al Virof60aef72011-02-15 01:35:28 -0500492static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
Nick Piggin34286d62011-01-07 17:49:57 +1100493{
Al Virof60aef72011-02-15 01:35:28 -0500494 return dentry->d_op->d_revalidate(dentry, nd);
Nick Piggin34286d62011-01-07 17:49:57 +1100495}
496
Al Virof5e1c1c2011-02-15 01:32:55 -0500497static struct dentry *
Ian Kentbcdc5e02006-09-27 01:50:44 -0700498do_revalidate(struct dentry *dentry, struct nameidata *nd)
499{
Al Virof5e1c1c2011-02-15 01:32:55 -0500500 int status = d_revalidate(dentry, nd);
Ian Kentbcdc5e02006-09-27 01:50:44 -0700501 if (unlikely(status <= 0)) {
502 /*
503 * The dentry failed validation.
504 * If d_revalidate returned 0 attempt to invalidate
505 * the dentry otherwise d_revalidate is asking us
506 * to return a fail status.
507 */
Nick Piggin34286d62011-01-07 17:49:57 +1100508 if (status < 0) {
Al Virof5e1c1c2011-02-15 01:32:55 -0500509 dput(dentry);
Nick Piggin34286d62011-01-07 17:49:57 +1100510 dentry = ERR_PTR(status);
Al Virof5e1c1c2011-02-15 01:32:55 -0500511 } else if (!d_invalidate(dentry)) {
512 dput(dentry);
513 dentry = NULL;
Ian Kentbcdc5e02006-09-27 01:50:44 -0700514 }
515 }
516 return dentry;
517}
518
Al Viro9f1fafe2011-03-25 11:00:12 -0400519/**
520 * complete_walk - successful completion of path walk
521 * @nd: pointer nameidata
Jeff Layton39159de2009-12-07 12:01:50 -0500522 *
Al Viro9f1fafe2011-03-25 11:00:12 -0400523 * If we had been in RCU mode, drop out of it and legitimize nd->path.
524 * Revalidate the final result, unless we'd already done that during
525 * the path walk or the filesystem doesn't ask for it. Return 0 on
526 * success, -error on failure. In case of failure caller does not
527 * need to drop nd->path.
Jeff Layton39159de2009-12-07 12:01:50 -0500528 */
Al Viro9f1fafe2011-03-25 11:00:12 -0400529static int complete_walk(struct nameidata *nd)
Jeff Layton39159de2009-12-07 12:01:50 -0500530{
Al Viro16c2cd72011-02-22 15:50:10 -0500531 struct dentry *dentry = nd->path.dentry;
Jeff Layton39159de2009-12-07 12:01:50 -0500532 int status;
Jeff Layton39159de2009-12-07 12:01:50 -0500533
Al Viro9f1fafe2011-03-25 11:00:12 -0400534 if (nd->flags & LOOKUP_RCU) {
535 nd->flags &= ~LOOKUP_RCU;
536 if (!(nd->flags & LOOKUP_ROOT))
537 nd->root.mnt = NULL;
538 spin_lock(&dentry->d_lock);
539 if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
540 spin_unlock(&dentry->d_lock);
541 rcu_read_unlock();
542 br_read_unlock(vfsmount_lock);
543 return -ECHILD;
544 }
545 BUG_ON(nd->inode != dentry->d_inode);
546 spin_unlock(&dentry->d_lock);
547 mntget(nd->path.mnt);
548 rcu_read_unlock();
549 br_read_unlock(vfsmount_lock);
550 }
551
Al Viro16c2cd72011-02-22 15:50:10 -0500552 if (likely(!(nd->flags & LOOKUP_JUMPED)))
Jeff Layton39159de2009-12-07 12:01:50 -0500553 return 0;
554
Al Viro16c2cd72011-02-22 15:50:10 -0500555 if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
556 return 0;
557
558 if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
559 return 0;
560
561 /* Note: we do not d_invalidate() */
Nick Piggin34286d62011-01-07 17:49:57 +1100562 status = d_revalidate(dentry, nd);
Jeff Layton39159de2009-12-07 12:01:50 -0500563 if (status > 0)
564 return 0;
565
Al Viro16c2cd72011-02-22 15:50:10 -0500566 if (!status)
Jeff Layton39159de2009-12-07 12:01:50 -0500567 status = -ESTALE;
Al Viro16c2cd72011-02-22 15:50:10 -0500568
Al Viro9f1fafe2011-03-25 11:00:12 -0400569 path_put(&nd->path);
Jeff Layton39159de2009-12-07 12:01:50 -0500570 return status;
571}
572
573/*
Al Virob75b5082009-12-16 01:01:38 -0500574 * Short-cut version of permission(), for calling on directories
575 * during pathname resolution. Combines parts of permission()
576 * and generic_permission(), and tests ONLY for MAY_EXEC permission.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 *
578 * If appropriate, check DAC only. If not appropriate, or
Al Virob75b5082009-12-16 01:01:38 -0500579 * short-cut DAC fails, then call ->permission() to do more
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 * complete permission check.
581 */
Nick Pigginb74c79e2011-01-07 17:49:58 +1100582static inline int exec_permission(struct inode *inode, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700584 int ret;
Serge E. Hallyne795b712011-03-23 16:43:25 -0700585 struct user_namespace *ns = inode_userns(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Linus Torvaldscb9179e2009-08-28 11:08:31 -0700587 if (inode->i_op->permission) {
Nick Pigginb74c79e2011-01-07 17:49:58 +1100588 ret = inode->i_op->permission(inode, MAY_EXEC, flags);
589 } else {
590 ret = acl_permission_check(inode, MAY_EXEC, flags,
591 inode->i_op->check_acl);
Linus Torvaldscb9179e2009-08-28 11:08:31 -0700592 }
Nick Pigginb74c79e2011-01-07 17:49:58 +1100593 if (likely(!ret))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 goto ok;
Nick Pigginb74c79e2011-01-07 17:49:58 +1100595 if (ret == -ECHILD)
Nick Piggin31e6b012011-01-07 17:49:52 +1100596 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Serge E. Hallyne795b712011-03-23 16:43:25 -0700598 if (ns_capable(ns, CAP_DAC_OVERRIDE) ||
599 ns_capable(ns, CAP_DAC_READ_SEARCH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 goto ok;
601
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700602 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603ok:
Nick Pigginb74c79e2011-01-07 17:49:58 +1100604 return security_inode_exec_permission(inode, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
606
Al Viro2a737872009-04-07 11:49:53 -0400607static __always_inline void set_root(struct nameidata *nd)
608{
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200609 if (!nd->root.mnt)
610 get_fs_root(current->fs, &nd->root);
Al Viro2a737872009-04-07 11:49:53 -0400611}
612
Al Viro6de88d72009-08-09 01:41:57 +0400613static int link_path_walk(const char *, struct nameidata *);
614
Nick Piggin31e6b012011-01-07 17:49:52 +1100615static __always_inline void set_root_rcu(struct nameidata *nd)
616{
617 if (!nd->root.mnt) {
618 struct fs_struct *fs = current->fs;
Nick Pigginc28cc362011-01-07 17:49:53 +1100619 unsigned seq;
620
621 do {
622 seq = read_seqcount_begin(&fs->seq);
623 nd->root = fs->root;
Tim Chenc1530012011-04-15 11:39:29 -0700624 nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
Nick Pigginc28cc362011-01-07 17:49:53 +1100625 } while (read_seqcount_retry(&fs->seq, seq));
Nick Piggin31e6b012011-01-07 17:49:52 +1100626 }
627}
628
Arjan van de Venf1662352006-01-14 13:21:31 -0800629static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Nick Piggin31e6b012011-01-07 17:49:52 +1100631 int ret;
632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 if (IS_ERR(link))
634 goto fail;
635
636 if (*link == '/') {
Al Viro2a737872009-04-07 11:49:53 -0400637 set_root(nd);
Jan Blunck1d957f92008-02-14 19:34:35 -0800638 path_put(&nd->path);
Al Viro2a737872009-04-07 11:49:53 -0400639 nd->path = nd->root;
640 path_get(&nd->root);
Al Viro16c2cd72011-02-22 15:50:10 -0500641 nd->flags |= LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100643 nd->inode = nd->path.dentry->d_inode;
Christoph Hellwigb4091d52008-11-05 15:07:21 +0100644
Nick Piggin31e6b012011-01-07 17:49:52 +1100645 ret = link_path_walk(link, nd);
646 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647fail:
Jan Blunck1d957f92008-02-14 19:34:35 -0800648 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return PTR_ERR(link);
650}
651
Jan Blunck1d957f92008-02-14 19:34:35 -0800652static void path_put_conditional(struct path *path, struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700653{
654 dput(path->dentry);
Jan Blunck4ac91372008-02-14 19:34:32 -0800655 if (path->mnt != nd->path.mnt)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700656 mntput(path->mnt);
657}
658
Nick Piggin7b9337a2011-01-14 08:42:43 +0000659static inline void path_to_nameidata(const struct path *path,
660 struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700661{
Nick Piggin31e6b012011-01-07 17:49:52 +1100662 if (!(nd->flags & LOOKUP_RCU)) {
663 dput(nd->path.dentry);
664 if (nd->path.mnt != path->mnt)
665 mntput(nd->path.mnt);
Huang Shijie9a229682010-04-02 17:37:13 +0800666 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100667 nd->path.mnt = path->mnt;
Jan Blunck4ac91372008-02-14 19:34:32 -0800668 nd->path.dentry = path->dentry;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700669}
670
Al Viro574197e2011-03-14 22:20:34 -0400671static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
672{
673 struct inode *inode = link->dentry->d_inode;
674 if (!IS_ERR(cookie) && inode->i_op->put_link)
675 inode->i_op->put_link(link->dentry, nd, cookie);
676 path_put(link);
677}
678
Al Virodef4af32009-12-26 08:37:05 -0500679static __always_inline int
Al Viro574197e2011-03-14 22:20:34 -0400680follow_link(struct path *link, struct nameidata *nd, void **p)
Ian Kent051d3812006-03-27 01:14:53 -0800681{
682 int error;
Nick Piggin7b9337a2011-01-14 08:42:43 +0000683 struct dentry *dentry = link->dentry;
Ian Kent051d3812006-03-27 01:14:53 -0800684
Al Viro844a3912011-02-15 00:38:26 -0500685 BUG_ON(nd->flags & LOOKUP_RCU);
686
Al Viro0e794582011-03-16 02:45:02 -0400687 if (link->mnt == nd->path.mnt)
688 mntget(link->mnt);
689
Al Viro574197e2011-03-14 22:20:34 -0400690 if (unlikely(current->total_link_count >= 40)) {
691 *p = ERR_PTR(-ELOOP); /* no ->put_link(), please */
Al Viro574197e2011-03-14 22:20:34 -0400692 path_put(&nd->path);
693 return -ELOOP;
694 }
695 cond_resched();
696 current->total_link_count++;
697
Nick Piggin7b9337a2011-01-14 08:42:43 +0000698 touch_atime(link->mnt, dentry);
Ian Kent051d3812006-03-27 01:14:53 -0800699 nd_set_link(nd, NULL);
700
Al Viro36f3b4f2011-02-22 21:24:38 -0500701 error = security_inode_follow_link(link->dentry, nd);
702 if (error) {
703 *p = ERR_PTR(error); /* no ->put_link(), please */
704 path_put(&nd->path);
705 return error;
706 }
707
Al Viro86acdca12009-12-22 23:45:11 -0500708 nd->last_type = LAST_BIND;
Al Virodef4af32009-12-26 08:37:05 -0500709 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
710 error = PTR_ERR(*p);
711 if (!IS_ERR(*p)) {
Ian Kent051d3812006-03-27 01:14:53 -0800712 char *s = nd_get_link(nd);
713 error = 0;
714 if (s)
715 error = __vfs_follow_link(nd, s);
Al Virobcda7652011-03-13 16:42:14 -0400716 else if (nd->last_type == LAST_BIND) {
Al Viro16c2cd72011-02-22 15:50:10 -0500717 nd->flags |= LOOKUP_JUMPED;
Al Virob21041d2011-03-14 20:01:51 -0400718 nd->inode = nd->path.dentry->d_inode;
719 if (nd->inode->i_op->follow_link) {
Al Virobcda7652011-03-13 16:42:14 -0400720 /* stepped on a _really_ weird one */
721 path_put(&nd->path);
722 error = -ELOOP;
723 }
724 }
Ian Kent051d3812006-03-27 01:14:53 -0800725 }
Ian Kent051d3812006-03-27 01:14:53 -0800726 return error;
727}
728
Nick Piggin31e6b012011-01-07 17:49:52 +1100729static int follow_up_rcu(struct path *path)
730{
731 struct vfsmount *parent;
732 struct dentry *mountpoint;
733
734 parent = path->mnt->mnt_parent;
735 if (parent == path->mnt)
736 return 0;
737 mountpoint = path->mnt->mnt_mountpoint;
738 path->dentry = mountpoint;
739 path->mnt = parent;
740 return 1;
741}
742
Al Virobab77eb2009-04-18 03:26:48 -0400743int follow_up(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
745 struct vfsmount *parent;
746 struct dentry *mountpoint;
Nick Piggin99b7db72010-08-18 04:37:39 +1000747
748 br_read_lock(vfsmount_lock);
Al Virobab77eb2009-04-18 03:26:48 -0400749 parent = path->mnt->mnt_parent;
750 if (parent == path->mnt) {
Nick Piggin99b7db72010-08-18 04:37:39 +1000751 br_read_unlock(vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 return 0;
753 }
754 mntget(parent);
Al Virobab77eb2009-04-18 03:26:48 -0400755 mountpoint = dget(path->mnt->mnt_mountpoint);
Nick Piggin99b7db72010-08-18 04:37:39 +1000756 br_read_unlock(vfsmount_lock);
Al Virobab77eb2009-04-18 03:26:48 -0400757 dput(path->dentry);
758 path->dentry = mountpoint;
759 mntput(path->mnt);
760 path->mnt = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 return 1;
762}
763
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100764/*
David Howells9875cf82011-01-14 18:45:21 +0000765 * Perform an automount
766 * - return -EISDIR to tell follow_managed() to stop and return the path we
767 * were called with.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 */
David Howells9875cf82011-01-14 18:45:21 +0000769static int follow_automount(struct path *path, unsigned flags,
770 bool *need_mntput)
Nick Piggin31e6b012011-01-07 17:49:52 +1100771{
David Howells9875cf82011-01-14 18:45:21 +0000772 struct vfsmount *mnt;
David Howellsea5b7782011-01-14 19:10:03 +0000773 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +1100774
David Howells9875cf82011-01-14 18:45:21 +0000775 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
776 return -EREMOTE;
Al Viro463ffb22005-06-06 13:36:05 -0700777
David Howells6f45b652011-01-14 18:45:31 +0000778 /* We don't want to mount if someone supplied AT_NO_AUTOMOUNT
779 * and this is the terminal part of the path.
780 */
781 if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_CONTINUE))
782 return -EISDIR; /* we actually want to stop here */
783
Miklos Szeredidef3f172011-10-25 13:59:46 +0200784 /* We don't want to mount if someone's just doing a stat -
785 * unless they're stat'ing a directory and appended a '/' to
786 * the name.
787 *
788 * We do, however, want to mount if someone wants to open or
789 * create a file of any type under the mountpoint, wants to
790 * traverse through the mountpoint or wants to open the
791 * mounted directory. Also, autofs may mark negative dentries
792 * as being automount points. These will need the attentions
793 * of the daemon to instantiate them before they can be used.
David Howells9875cf82011-01-14 18:45:21 +0000794 */
Miklos Szeredidef3f172011-10-25 13:59:46 +0200795 if (!(flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY |
Linus Torvaldseb4b2df2011-10-25 13:59:48 +0200796 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
Miklos Szeredidef3f172011-10-25 13:59:46 +0200797 path->dentry->d_inode)
798 return -EISDIR;
799
David Howells9875cf82011-01-14 18:45:21 +0000800 current->total_link_count++;
801 if (current->total_link_count >= 40)
802 return -ELOOP;
803
804 mnt = path->dentry->d_op->d_automount(path);
805 if (IS_ERR(mnt)) {
806 /*
807 * The filesystem is allowed to return -EISDIR here to indicate
808 * it doesn't want to automount. For instance, autofs would do
809 * this so that its userspace daemon can mount on this dentry.
810 *
811 * However, we can only permit this if it's a terminal point in
812 * the path being looked up; if it wasn't then the remainder of
813 * the path is inaccessible and we should say so.
814 */
815 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_CONTINUE))
816 return -EREMOTE;
817 return PTR_ERR(mnt);
818 }
David Howellsea5b7782011-01-14 19:10:03 +0000819
David Howells9875cf82011-01-14 18:45:21 +0000820 if (!mnt) /* mount collision */
821 return 0;
822
Al Viro8aef1882011-06-16 15:10:06 +0100823 if (!*need_mntput) {
824 /* lock_mount() may release path->mnt on error */
825 mntget(path->mnt);
826 *need_mntput = true;
827 }
Al Viro19a167a2011-01-17 01:35:23 -0500828 err = finish_automount(mnt, path);
David Howellsea5b7782011-01-14 19:10:03 +0000829
David Howellsea5b7782011-01-14 19:10:03 +0000830 switch (err) {
831 case -EBUSY:
832 /* Someone else made a mount here whilst we were busy */
Al Viro19a167a2011-01-17 01:35:23 -0500833 return 0;
David Howellsea5b7782011-01-14 19:10:03 +0000834 case 0:
Al Viro8aef1882011-06-16 15:10:06 +0100835 path_put(path);
David Howellsea5b7782011-01-14 19:10:03 +0000836 path->mnt = mnt;
837 path->dentry = dget(mnt->mnt_root);
David Howellsea5b7782011-01-14 19:10:03 +0000838 return 0;
Al Viro19a167a2011-01-17 01:35:23 -0500839 default:
840 return err;
David Howellsea5b7782011-01-14 19:10:03 +0000841 }
Al Viro19a167a2011-01-17 01:35:23 -0500842
David Howells9875cf82011-01-14 18:45:21 +0000843}
844
845/*
846 * Handle a dentry that is managed in some way.
David Howellscc53ce52011-01-14 18:45:26 +0000847 * - Flagged for transit management (autofs)
David Howells9875cf82011-01-14 18:45:21 +0000848 * - Flagged as mountpoint
849 * - Flagged as automount point
850 *
851 * This may only be called in refwalk mode.
852 *
853 * Serialization is taken care of in namespace.c
854 */
855static int follow_managed(struct path *path, unsigned flags)
856{
Al Viro8aef1882011-06-16 15:10:06 +0100857 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
David Howells9875cf82011-01-14 18:45:21 +0000858 unsigned managed;
859 bool need_mntput = false;
Al Viro8aef1882011-06-16 15:10:06 +0100860 int ret = 0;
David Howells9875cf82011-01-14 18:45:21 +0000861
862 /* Given that we're not holding a lock here, we retain the value in a
863 * local variable for each dentry as we look at it so that we don't see
864 * the components of that value change under us */
865 while (managed = ACCESS_ONCE(path->dentry->d_flags),
866 managed &= DCACHE_MANAGED_DENTRY,
867 unlikely(managed != 0)) {
David Howellscc53ce52011-01-14 18:45:26 +0000868 /* Allow the filesystem to manage the transit without i_mutex
869 * being held. */
870 if (managed & DCACHE_MANAGE_TRANSIT) {
871 BUG_ON(!path->dentry->d_op);
872 BUG_ON(!path->dentry->d_op->d_manage);
Al Viro1aed3e42011-03-18 09:09:02 -0400873 ret = path->dentry->d_op->d_manage(path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +0000874 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +0100875 break;
David Howellscc53ce52011-01-14 18:45:26 +0000876 }
877
David Howells9875cf82011-01-14 18:45:21 +0000878 /* Transit to a mounted filesystem. */
879 if (managed & DCACHE_MOUNTED) {
880 struct vfsmount *mounted = lookup_mnt(path);
881 if (mounted) {
882 dput(path->dentry);
883 if (need_mntput)
884 mntput(path->mnt);
885 path->mnt = mounted;
886 path->dentry = dget(mounted->mnt_root);
887 need_mntput = true;
888 continue;
889 }
890
891 /* Something is mounted on this dentry in another
892 * namespace and/or whatever was mounted there in this
893 * namespace got unmounted before we managed to get the
894 * vfsmount_lock */
895 }
896
897 /* Handle an automount point */
898 if (managed & DCACHE_NEED_AUTOMOUNT) {
899 ret = follow_automount(path, flags, &need_mntput);
900 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +0100901 break;
David Howells9875cf82011-01-14 18:45:21 +0000902 continue;
903 }
904
905 /* We didn't change the current path point */
906 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 }
Al Viro8aef1882011-06-16 15:10:06 +0100908
909 if (need_mntput && path->mnt == mnt)
910 mntput(path->mnt);
911 if (ret == -EISDIR)
912 ret = 0;
913 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
915
David Howellscc53ce52011-01-14 18:45:26 +0000916int follow_down_one(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
918 struct vfsmount *mounted;
919
Al Viro1c755af2009-04-18 14:06:57 -0400920 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -0400922 dput(path->dentry);
923 mntput(path->mnt);
924 path->mnt = mounted;
925 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 return 1;
927 }
928 return 0;
929}
930
Ian Kent62a73752011-03-25 01:51:02 +0800931static inline bool managed_dentry_might_block(struct dentry *dentry)
932{
933 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
934 dentry->d_op->d_manage(dentry, true) < 0);
935}
936
David Howells9875cf82011-01-14 18:45:21 +0000937/*
Al Viro287548e2011-05-27 06:50:06 -0400938 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
939 * we meet a managed dentry that would need blocking.
David Howells9875cf82011-01-14 18:45:21 +0000940 */
941static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
Al Viro287548e2011-05-27 06:50:06 -0400942 struct inode **inode)
David Howells9875cf82011-01-14 18:45:21 +0000943{
Ian Kent62a73752011-03-25 01:51:02 +0800944 for (;;) {
David Howells9875cf82011-01-14 18:45:21 +0000945 struct vfsmount *mounted;
Ian Kent62a73752011-03-25 01:51:02 +0800946 /*
947 * Don't forget we might have a non-mountpoint managed dentry
948 * that wants to block transit.
949 */
Al Viro287548e2011-05-27 06:50:06 -0400950 if (unlikely(managed_dentry_might_block(path->dentry)))
David Howellsab909112011-01-14 18:46:51 +0000951 return false;
Ian Kent62a73752011-03-25 01:51:02 +0800952
953 if (!d_mountpoint(path->dentry))
954 break;
955
David Howells9875cf82011-01-14 18:45:21 +0000956 mounted = __lookup_mnt(path->mnt, path->dentry, 1);
957 if (!mounted)
958 break;
959 path->mnt = mounted;
960 path->dentry = mounted->mnt_root;
961 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
Linus Torvalds59430262011-07-18 15:43:29 -0700962 /*
963 * Update the inode too. We don't need to re-check the
964 * dentry sequence number here after this d_inode read,
965 * because a mount-point is always pinned.
966 */
967 *inode = path->dentry->d_inode;
David Howells9875cf82011-01-14 18:45:21 +0000968 }
David Howells9875cf82011-01-14 18:45:21 +0000969 return true;
970}
971
Al Virodea39372011-05-27 06:53:39 -0400972static void follow_mount_rcu(struct nameidata *nd)
Al Viro287548e2011-05-27 06:50:06 -0400973{
Al Virodea39372011-05-27 06:53:39 -0400974 while (d_mountpoint(nd->path.dentry)) {
Al Viro287548e2011-05-27 06:50:06 -0400975 struct vfsmount *mounted;
Al Virodea39372011-05-27 06:53:39 -0400976 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
Al Viro287548e2011-05-27 06:50:06 -0400977 if (!mounted)
978 break;
Al Virodea39372011-05-27 06:53:39 -0400979 nd->path.mnt = mounted;
980 nd->path.dentry = mounted->mnt_root;
981 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro287548e2011-05-27 06:50:06 -0400982 }
983}
984
Nick Piggin31e6b012011-01-07 17:49:52 +1100985static int follow_dotdot_rcu(struct nameidata *nd)
986{
Nick Piggin31e6b012011-01-07 17:49:52 +1100987 set_root_rcu(nd);
988
David Howells9875cf82011-01-14 18:45:21 +0000989 while (1) {
Nick Piggin31e6b012011-01-07 17:49:52 +1100990 if (nd->path.dentry == nd->root.dentry &&
991 nd->path.mnt == nd->root.mnt) {
992 break;
993 }
994 if (nd->path.dentry != nd->path.mnt->mnt_root) {
995 struct dentry *old = nd->path.dentry;
996 struct dentry *parent = old->d_parent;
997 unsigned seq;
998
999 seq = read_seqcount_begin(&parent->d_seq);
1000 if (read_seqcount_retry(&old->d_seq, nd->seq))
Al Viroef7562d2011-03-04 14:35:59 -05001001 goto failed;
Nick Piggin31e6b012011-01-07 17:49:52 +11001002 nd->path.dentry = parent;
1003 nd->seq = seq;
1004 break;
1005 }
1006 if (!follow_up_rcu(&nd->path))
1007 break;
1008 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Nick Piggin31e6b012011-01-07 17:49:52 +11001009 }
Al Virodea39372011-05-27 06:53:39 -04001010 follow_mount_rcu(nd);
1011 nd->inode = nd->path.dentry->d_inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001012 return 0;
Al Viroef7562d2011-03-04 14:35:59 -05001013
1014failed:
1015 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001016 if (!(nd->flags & LOOKUP_ROOT))
1017 nd->root.mnt = NULL;
Al Viroef7562d2011-03-04 14:35:59 -05001018 rcu_read_unlock();
1019 br_read_unlock(vfsmount_lock);
1020 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001021}
1022
David Howells9875cf82011-01-14 18:45:21 +00001023/*
David Howellscc53ce52011-01-14 18:45:26 +00001024 * Follow down to the covering mount currently visible to userspace. At each
1025 * point, the filesystem owning that dentry may be queried as to whether the
1026 * caller is permitted to proceed or not.
David Howellscc53ce52011-01-14 18:45:26 +00001027 */
Al Viro7cc90cc2011-03-18 09:04:20 -04001028int follow_down(struct path *path)
David Howellscc53ce52011-01-14 18:45:26 +00001029{
1030 unsigned managed;
1031 int ret;
1032
1033 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1034 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1035 /* Allow the filesystem to manage the transit without i_mutex
1036 * being held.
1037 *
1038 * We indicate to the filesystem if someone is trying to mount
1039 * something here. This gives autofs the chance to deny anyone
1040 * other than its daemon the right to mount on its
1041 * superstructure.
1042 *
1043 * The filesystem may sleep at this point.
1044 */
1045 if (managed & DCACHE_MANAGE_TRANSIT) {
1046 BUG_ON(!path->dentry->d_op);
1047 BUG_ON(!path->dentry->d_op->d_manage);
David Howellsab909112011-01-14 18:46:51 +00001048 ret = path->dentry->d_op->d_manage(
Al Viro1aed3e42011-03-18 09:09:02 -04001049 path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +00001050 if (ret < 0)
1051 return ret == -EISDIR ? 0 : ret;
1052 }
1053
1054 /* Transit to a mounted filesystem. */
1055 if (managed & DCACHE_MOUNTED) {
1056 struct vfsmount *mounted = lookup_mnt(path);
1057 if (!mounted)
1058 break;
1059 dput(path->dentry);
1060 mntput(path->mnt);
1061 path->mnt = mounted;
1062 path->dentry = dget(mounted->mnt_root);
1063 continue;
1064 }
1065
1066 /* Don't handle automount points here */
1067 break;
1068 }
1069 return 0;
1070}
1071
1072/*
David Howells9875cf82011-01-14 18:45:21 +00001073 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1074 */
1075static void follow_mount(struct path *path)
1076{
1077 while (d_mountpoint(path->dentry)) {
1078 struct vfsmount *mounted = lookup_mnt(path);
1079 if (!mounted)
1080 break;
1081 dput(path->dentry);
1082 mntput(path->mnt);
1083 path->mnt = mounted;
1084 path->dentry = dget(mounted->mnt_root);
1085 }
1086}
1087
Nick Piggin31e6b012011-01-07 17:49:52 +11001088static void follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
Al Viro2a737872009-04-07 11:49:53 -04001090 set_root(nd);
Andreas Mohre518ddb2006-09-29 02:01:22 -07001091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 while(1) {
Jan Blunck4ac91372008-02-14 19:34:32 -08001093 struct dentry *old = nd->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Al Viro2a737872009-04-07 11:49:53 -04001095 if (nd->path.dentry == nd->root.dentry &&
1096 nd->path.mnt == nd->root.mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 break;
1098 }
Jan Blunck4ac91372008-02-14 19:34:32 -08001099 if (nd->path.dentry != nd->path.mnt->mnt_root) {
Al Viro3088dd72010-01-30 15:47:29 -05001100 /* rare case of legitimate dget_parent()... */
1101 nd->path.dentry = dget_parent(nd->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 dput(old);
1103 break;
1104 }
Al Viro3088dd72010-01-30 15:47:29 -05001105 if (!follow_up(&nd->path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 }
Al Viro79ed0222009-04-18 13:59:41 -04001108 follow_mount(&nd->path);
Nick Piggin31e6b012011-01-07 17:49:52 +11001109 nd->inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110}
1111
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112/*
Nick Pigginbaa03892010-08-18 04:37:31 +10001113 * Allocate a dentry with name and parent, and perform a parent
1114 * directory ->lookup on it. Returns the new dentry, or ERR_PTR
1115 * on error. parent->d_inode->i_mutex must be held. d_lookup must
1116 * have verified that no child exists while under i_mutex.
1117 */
1118static struct dentry *d_alloc_and_lookup(struct dentry *parent,
1119 struct qstr *name, struct nameidata *nd)
1120{
1121 struct inode *inode = parent->d_inode;
1122 struct dentry *dentry;
1123 struct dentry *old;
1124
1125 /* Don't create child dentry for a dead directory. */
1126 if (unlikely(IS_DEADDIR(inode)))
1127 return ERR_PTR(-ENOENT);
1128
1129 dentry = d_alloc(parent, name);
1130 if (unlikely(!dentry))
1131 return ERR_PTR(-ENOMEM);
1132
1133 old = inode->i_op->lookup(inode, dentry, nd);
1134 if (unlikely(old)) {
1135 dput(dentry);
1136 dentry = old;
1137 }
1138 return dentry;
1139}
1140
1141/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 * It's more convoluted than I'd like it to be, but... it's still fairly
1143 * small and for now I'd prefer to have fast path as straight as possible.
1144 * It _is_ time-critical.
1145 */
1146static int do_lookup(struct nameidata *nd, struct qstr *name,
Nick Piggin31e6b012011-01-07 17:49:52 +11001147 struct path *path, struct inode **inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
Jan Blunck4ac91372008-02-14 19:34:32 -08001149 struct vfsmount *mnt = nd->path.mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +11001150 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro5a18fff2011-03-11 04:44:53 -05001151 int need_reval = 1;
1152 int status = 1;
David Howells9875cf82011-01-14 18:45:21 +00001153 int err;
1154
Al Viro3cac2602009-08-13 18:27:43 +04001155 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001156 * Rename seqlock is not required here because in the off chance
1157 * of a false negative due to a concurrent rename, we're going to
1158 * do the non-racy lookup, below.
1159 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001160 if (nd->flags & LOOKUP_RCU) {
1161 unsigned seq;
Nick Piggin31e6b012011-01-07 17:49:52 +11001162 *inode = nd->inode;
1163 dentry = __d_lookup_rcu(parent, name, &seq, inode);
Al Viro5a18fff2011-03-11 04:44:53 -05001164 if (!dentry)
1165 goto unlazy;
1166
Nick Piggin31e6b012011-01-07 17:49:52 +11001167 /* Memory barrier in read_seqcount_begin of child is enough */
1168 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1169 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001170 nd->seq = seq;
Al Viro5a18fff2011-03-11 04:44:53 -05001171
Al Viro24643082011-02-15 01:26:22 -05001172 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
Al Viro5a18fff2011-03-11 04:44:53 -05001173 status = d_revalidate(dentry, nd);
1174 if (unlikely(status <= 0)) {
1175 if (status != -ECHILD)
1176 need_reval = 0;
1177 goto unlazy;
1178 }
Al Viro24643082011-02-15 01:26:22 -05001179 }
Nick Piggin31e6b012011-01-07 17:49:52 +11001180 path->mnt = mnt;
1181 path->dentry = dentry;
Al Virod6e9bd22011-05-27 07:03:15 -04001182 if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1183 goto unlazy;
1184 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1185 goto unlazy;
1186 return 0;
Al Viro5a18fff2011-03-11 04:44:53 -05001187unlazy:
Al Viro19660af2011-03-25 10:32:48 -04001188 if (unlazy_walk(nd, dentry))
1189 return -ECHILD;
Al Viro5a18fff2011-03-11 04:44:53 -05001190 } else {
1191 dentry = __d_lookup(parent, name);
Nick Piggin31e6b012011-01-07 17:49:52 +11001192 }
Al Viro5a18fff2011-03-11 04:44:53 -05001193
1194retry:
1195 if (unlikely(!dentry)) {
1196 struct inode *dir = parent->d_inode;
1197 BUG_ON(nd->inode != dir);
1198
1199 mutex_lock(&dir->i_mutex);
1200 dentry = d_lookup(parent, name);
1201 if (likely(!dentry)) {
1202 dentry = d_alloc_and_lookup(parent, name, nd);
1203 if (IS_ERR(dentry)) {
1204 mutex_unlock(&dir->i_mutex);
1205 return PTR_ERR(dentry);
1206 }
1207 /* known good */
1208 need_reval = 0;
1209 status = 1;
1210 }
1211 mutex_unlock(&dir->i_mutex);
Al Viro24643082011-02-15 01:26:22 -05001212 }
Al Viro5a18fff2011-03-11 04:44:53 -05001213 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
1214 status = d_revalidate(dentry, nd);
1215 if (unlikely(status <= 0)) {
1216 if (status < 0) {
1217 dput(dentry);
1218 return status;
1219 }
1220 if (!d_invalidate(dentry)) {
1221 dput(dentry);
1222 dentry = NULL;
1223 need_reval = 1;
1224 goto retry;
1225 }
1226 }
1227
David Howells9875cf82011-01-14 18:45:21 +00001228 path->mnt = mnt;
1229 path->dentry = dentry;
1230 err = follow_managed(path, nd->flags);
Ian Kent89312212011-01-18 12:06:10 +08001231 if (unlikely(err < 0)) {
1232 path_put_conditional(path, nd);
David Howells9875cf82011-01-14 18:45:21 +00001233 return err;
Ian Kent89312212011-01-18 12:06:10 +08001234 }
David Howells9875cf82011-01-14 18:45:21 +00001235 *inode = path->dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237}
1238
Al Viro52094c82011-02-21 21:34:47 -05001239static inline int may_lookup(struct nameidata *nd)
1240{
1241 if (nd->flags & LOOKUP_RCU) {
1242 int err = exec_permission(nd->inode, IPERM_FLAG_RCU);
1243 if (err != -ECHILD)
1244 return err;
Al Viro19660af2011-03-25 10:32:48 -04001245 if (unlazy_walk(nd, NULL))
Al Viro52094c82011-02-21 21:34:47 -05001246 return -ECHILD;
1247 }
1248 return exec_permission(nd->inode, 0);
1249}
1250
Al Viro9856fa12011-03-04 14:22:06 -05001251static inline int handle_dots(struct nameidata *nd, int type)
1252{
1253 if (type == LAST_DOTDOT) {
1254 if (nd->flags & LOOKUP_RCU) {
1255 if (follow_dotdot_rcu(nd))
1256 return -ECHILD;
1257 } else
1258 follow_dotdot(nd);
1259 }
1260 return 0;
1261}
1262
Al Viro951361f2011-03-04 14:44:37 -05001263static void terminate_walk(struct nameidata *nd)
1264{
1265 if (!(nd->flags & LOOKUP_RCU)) {
1266 path_put(&nd->path);
1267 } else {
1268 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001269 if (!(nd->flags & LOOKUP_ROOT))
1270 nd->root.mnt = NULL;
Al Viro951361f2011-03-04 14:44:37 -05001271 rcu_read_unlock();
1272 br_read_unlock(vfsmount_lock);
1273 }
1274}
1275
Al Viroce57dfc2011-03-13 19:58:58 -04001276static inline int walk_component(struct nameidata *nd, struct path *path,
1277 struct qstr *name, int type, int follow)
1278{
1279 struct inode *inode;
1280 int err;
1281 /*
1282 * "." and ".." are special - ".." especially so because it has
1283 * to be able to know about the current root directory and
1284 * parent relationships.
1285 */
1286 if (unlikely(type != LAST_NORM))
1287 return handle_dots(nd, type);
1288 err = do_lookup(nd, name, path, &inode);
1289 if (unlikely(err)) {
1290 terminate_walk(nd);
1291 return err;
1292 }
1293 if (!inode) {
1294 path_to_nameidata(path, nd);
1295 terminate_walk(nd);
1296 return -ENOENT;
1297 }
1298 if (unlikely(inode->i_op->follow_link) && follow) {
Al Viro19660af2011-03-25 10:32:48 -04001299 if (nd->flags & LOOKUP_RCU) {
1300 if (unlikely(unlazy_walk(nd, path->dentry))) {
1301 terminate_walk(nd);
1302 return -ECHILD;
1303 }
1304 }
Al Viroce57dfc2011-03-13 19:58:58 -04001305 BUG_ON(inode != path->dentry->d_inode);
1306 return 1;
1307 }
1308 path_to_nameidata(path, nd);
1309 nd->inode = inode;
1310 return 0;
1311}
1312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313/*
Al Virob3563792011-03-14 21:54:55 -04001314 * This limits recursive symlink follows to 8, while
1315 * limiting consecutive symlinks to 40.
1316 *
1317 * Without that kind of total limit, nasty chains of consecutive
1318 * symlinks can cause almost arbitrarily long lookups.
1319 */
1320static inline int nested_symlink(struct path *path, struct nameidata *nd)
1321{
1322 int res;
1323
Al Virob3563792011-03-14 21:54:55 -04001324 if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1325 path_put_conditional(path, nd);
1326 path_put(&nd->path);
1327 return -ELOOP;
1328 }
Erez Zadok1a4022f2011-05-21 01:19:59 -04001329 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
Al Virob3563792011-03-14 21:54:55 -04001330
1331 nd->depth++;
1332 current->link_count++;
1333
1334 do {
1335 struct path link = *path;
1336 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04001337
1338 res = follow_link(&link, nd, &cookie);
Al Virob3563792011-03-14 21:54:55 -04001339 if (!res)
1340 res = walk_component(nd, path, &nd->last,
1341 nd->last_type, LOOKUP_FOLLOW);
Al Viro574197e2011-03-14 22:20:34 -04001342 put_link(nd, &link, cookie);
Al Virob3563792011-03-14 21:54:55 -04001343 } while (res > 0);
1344
1345 current->link_count--;
1346 nd->depth--;
1347 return res;
1348}
1349
1350/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01001352 * This is the basic name resolution function, turning a pathname into
1353 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01001355 * Returns 0 and nd will have valid dentry and mnt on success.
1356 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 */
Al Viro6de88d72009-08-09 01:41:57 +04001358static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
1360 struct path next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 int err;
1362 unsigned int lookup_flags = nd->flags;
1363
1364 while (*name=='/')
1365 name++;
1366 if (!*name)
Al Viro086e1832011-02-22 20:56:27 -05001367 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 /* At this point we know we have a real path component. */
1370 for(;;) {
1371 unsigned long hash;
1372 struct qstr this;
1373 unsigned int c;
Al Virofe479a52011-02-22 15:10:03 -05001374 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Trond Myklebustcdce5d62005-10-18 14:20:18 -07001376 nd->flags |= LOOKUP_CONTINUE;
Al Viro52094c82011-02-21 21:34:47 -05001377
1378 err = may_lookup(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 if (err)
1380 break;
1381
1382 this.name = name;
1383 c = *(const unsigned char *)name;
1384
1385 hash = init_name_hash();
1386 do {
1387 name++;
1388 hash = partial_name_hash(c, hash);
1389 c = *(const unsigned char *)name;
1390 } while (c && (c != '/'));
1391 this.len = name - (const char *) this.name;
1392 this.hash = end_name_hash(hash);
1393
Al Virofe479a52011-02-22 15:10:03 -05001394 type = LAST_NORM;
1395 if (this.name[0] == '.') switch (this.len) {
1396 case 2:
Al Viro16c2cd72011-02-22 15:50:10 -05001397 if (this.name[1] == '.') {
Al Virofe479a52011-02-22 15:10:03 -05001398 type = LAST_DOTDOT;
Al Viro16c2cd72011-02-22 15:50:10 -05001399 nd->flags |= LOOKUP_JUMPED;
1400 }
Al Virofe479a52011-02-22 15:10:03 -05001401 break;
1402 case 1:
1403 type = LAST_DOT;
1404 }
Al Viro5a202bc2011-03-08 14:17:44 -05001405 if (likely(type == LAST_NORM)) {
1406 struct dentry *parent = nd->path.dentry;
Al Viro16c2cd72011-02-22 15:50:10 -05001407 nd->flags &= ~LOOKUP_JUMPED;
Al Viro5a202bc2011-03-08 14:17:44 -05001408 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1409 err = parent->d_op->d_hash(parent, nd->inode,
1410 &this);
1411 if (err < 0)
1412 break;
1413 }
1414 }
Al Virofe479a52011-02-22 15:10:03 -05001415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 /* remove trailing slashes? */
1417 if (!c)
1418 goto last_component;
1419 while (*++name == '/');
1420 if (!*name)
Al Virob3563792011-03-14 21:54:55 -04001421 goto last_component;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Al Viroce57dfc2011-03-13 19:58:58 -04001423 err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1424 if (err < 0)
1425 return err;
Al Virofe479a52011-02-22 15:10:03 -05001426
Al Viroce57dfc2011-03-13 19:58:58 -04001427 if (err) {
Al Virob3563792011-03-14 21:54:55 -04001428 err = nested_symlink(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 if (err)
Al Viroa7472ba2011-03-04 14:39:30 -05001430 return err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 err = -ENOTDIR;
Nick Piggin31e6b012011-01-07 17:49:52 +11001433 if (!nd->inode->i_op->lookup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 break;
1435 continue;
1436 /* here ends the main loop */
1437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438last_component:
Trond Myklebustf55eab82006-02-04 23:28:01 -08001439 /* Clear LOOKUP_CONTINUE iff it was previously unset */
1440 nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
Al Virob3563792011-03-14 21:54:55 -04001441 nd->last = this;
1442 nd->last_type = type;
Al Viro086e1832011-02-22 20:56:27 -05001443 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 }
Al Viro951361f2011-03-04 14:44:37 -05001445 terminate_walk(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 return err;
1447}
1448
Al Viro70e9b352011-03-05 21:12:22 -05001449static int path_init(int dfd, const char *name, unsigned int flags,
1450 struct nameidata *nd, struct file **fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451{
Prasanna Medaea3834d2005-04-29 16:00:17 +01001452 int retval = 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001453 int fput_needed;
1454 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
1456 nd->last_type = LAST_ROOT; /* if there are only slashes... */
Al Viro16c2cd72011-02-22 15:50:10 -05001457 nd->flags = flags | LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 nd->depth = 0;
Al Viro5b6ca022011-03-09 23:04:47 -05001459 if (flags & LOOKUP_ROOT) {
1460 struct inode *inode = nd->root.dentry->d_inode;
Al Viro73d049a2011-03-11 12:08:24 -05001461 if (*name) {
1462 if (!inode->i_op->lookup)
1463 return -ENOTDIR;
1464 retval = inode_permission(inode, MAY_EXEC);
1465 if (retval)
1466 return retval;
1467 }
Al Viro5b6ca022011-03-09 23:04:47 -05001468 nd->path = nd->root;
1469 nd->inode = inode;
1470 if (flags & LOOKUP_RCU) {
1471 br_read_lock(vfsmount_lock);
1472 rcu_read_lock();
1473 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1474 } else {
1475 path_get(&nd->path);
1476 }
1477 return 0;
1478 }
1479
Al Viro2a737872009-04-07 11:49:53 -04001480 nd->root.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 if (*name=='/') {
Al Viroe41f7d42011-02-22 14:02:58 -05001483 if (flags & LOOKUP_RCU) {
1484 br_read_lock(vfsmount_lock);
1485 rcu_read_lock();
1486 set_root_rcu(nd);
1487 } else {
1488 set_root(nd);
1489 path_get(&nd->root);
1490 }
Al Viro2a737872009-04-07 11:49:53 -04001491 nd->path = nd->root;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001492 } else if (dfd == AT_FDCWD) {
Al Viroe41f7d42011-02-22 14:02:58 -05001493 if (flags & LOOKUP_RCU) {
1494 struct fs_struct *fs = current->fs;
1495 unsigned seq;
1496
1497 br_read_lock(vfsmount_lock);
1498 rcu_read_lock();
1499
1500 do {
1501 seq = read_seqcount_begin(&fs->seq);
1502 nd->path = fs->pwd;
1503 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1504 } while (read_seqcount_retry(&fs->seq, seq));
1505 } else {
1506 get_fs_pwd(current->fs, &nd->path);
1507 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001508 } else {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001509 struct dentry *dentry;
1510
Al Viro1abf0c72011-03-13 03:51:11 -04001511 file = fget_raw_light(dfd, &fput_needed);
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001512 retval = -EBADF;
1513 if (!file)
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001514 goto out_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001515
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001516 dentry = file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001517
Al Virof52e0c12011-03-14 18:56:51 -04001518 if (*name) {
1519 retval = -ENOTDIR;
1520 if (!S_ISDIR(dentry->d_inode->i_mode))
1521 goto fput_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001522
Al Virof52e0c12011-03-14 18:56:51 -04001523 retval = file_permission(file, MAY_EXEC);
1524 if (retval)
1525 goto fput_fail;
1526 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001527
Jan Blunck5dd784d2008-02-14 19:34:38 -08001528 nd->path = file->f_path;
Al Viroe41f7d42011-02-22 14:02:58 -05001529 if (flags & LOOKUP_RCU) {
1530 if (fput_needed)
Al Viro70e9b352011-03-05 21:12:22 -05001531 *fp = file;
Al Viroe41f7d42011-02-22 14:02:58 -05001532 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1533 br_read_lock(vfsmount_lock);
1534 rcu_read_lock();
1535 } else {
1536 path_get(&file->f_path);
1537 fput_light(file, fput_needed);
1538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 }
Al Viroe41f7d42011-02-22 14:02:58 -05001540
Nick Piggin31e6b012011-01-07 17:49:52 +11001541 nd->inode = nd->path.dentry->d_inode;
Al Viro9b4a9b12009-04-07 11:44:16 -04001542 return 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001543
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001544fput_fail:
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001545 fput_light(file, fput_needed);
Al Viro9b4a9b12009-04-07 11:44:16 -04001546out_fail:
1547 return retval;
1548}
1549
Al Virobd92d7f2011-03-14 19:54:59 -04001550static inline int lookup_last(struct nameidata *nd, struct path *path)
1551{
1552 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1553 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1554
1555 nd->flags &= ~LOOKUP_PARENT;
1556 return walk_component(nd, path, &nd->last, nd->last_type,
1557 nd->flags & LOOKUP_FOLLOW);
1558}
1559
Al Viro9b4a9b12009-04-07 11:44:16 -04001560/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroee0827c2011-02-21 23:38:09 -05001561static int path_lookupat(int dfd, const char *name,
Al Viro9b4a9b12009-04-07 11:44:16 -04001562 unsigned int flags, struct nameidata *nd)
1563{
Al Viro70e9b352011-03-05 21:12:22 -05001564 struct file *base = NULL;
Al Virobd92d7f2011-03-14 19:54:59 -04001565 struct path path;
1566 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001567
1568 /*
1569 * Path walking is largely split up into 2 different synchronisation
1570 * schemes, rcu-walk and ref-walk (explained in
1571 * Documentation/filesystems/path-lookup.txt). These share much of the
1572 * path walk code, but some things particularly setup, cleanup, and
1573 * following mounts are sufficiently divergent that functions are
1574 * duplicated. Typically there is a function foo(), and its RCU
1575 * analogue, foo_rcu().
1576 *
1577 * -ECHILD is the error number of choice (just to avoid clashes) that
1578 * is returned if some aspect of an rcu-walk fails. Such an error must
1579 * be handled by restarting a traditional ref-walk (which will always
1580 * be able to complete).
1581 */
Al Virobd92d7f2011-03-14 19:54:59 -04001582 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
Al Viroee0827c2011-02-21 23:38:09 -05001583
Al Virobd92d7f2011-03-14 19:54:59 -04001584 if (unlikely(err))
1585 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001586
1587 current->total_link_count = 0;
Al Virobd92d7f2011-03-14 19:54:59 -04001588 err = link_path_walk(name, nd);
1589
1590 if (!err && !(flags & LOOKUP_PARENT)) {
Al Virobd92d7f2011-03-14 19:54:59 -04001591 err = lookup_last(nd, &path);
1592 while (err > 0) {
1593 void *cookie;
1594 struct path link = path;
Al Virobd92d7f2011-03-14 19:54:59 -04001595 nd->flags |= LOOKUP_PARENT;
Al Viro574197e2011-03-14 22:20:34 -04001596 err = follow_link(&link, nd, &cookie);
Al Virobd92d7f2011-03-14 19:54:59 -04001597 if (!err)
1598 err = lookup_last(nd, &path);
Al Viro574197e2011-03-14 22:20:34 -04001599 put_link(nd, &link, cookie);
Al Virobd92d7f2011-03-14 19:54:59 -04001600 }
1601 }
Al Viroee0827c2011-02-21 23:38:09 -05001602
Al Viro9f1fafe2011-03-25 11:00:12 -04001603 if (!err)
1604 err = complete_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04001605
1606 if (!err && nd->flags & LOOKUP_DIRECTORY) {
1607 if (!nd->inode->i_op->lookup) {
1608 path_put(&nd->path);
Al Virobd23a532011-03-23 09:56:30 -04001609 err = -ENOTDIR;
Al Virobd92d7f2011-03-14 19:54:59 -04001610 }
1611 }
Al Viro16c2cd72011-02-22 15:50:10 -05001612
Al Viro70e9b352011-03-05 21:12:22 -05001613 if (base)
1614 fput(base);
Al Viroee0827c2011-02-21 23:38:09 -05001615
Al Viro5b6ca022011-03-09 23:04:47 -05001616 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
Al Viro2a737872009-04-07 11:49:53 -04001617 path_put(&nd->root);
1618 nd->root.mnt = NULL;
1619 }
Al Virobd92d7f2011-03-14 19:54:59 -04001620 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001621}
Nick Piggin31e6b012011-01-07 17:49:52 +11001622
Al Viroee0827c2011-02-21 23:38:09 -05001623static int do_path_lookup(int dfd, const char *name,
1624 unsigned int flags, struct nameidata *nd)
1625{
1626 int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1627 if (unlikely(retval == -ECHILD))
1628 retval = path_lookupat(dfd, name, flags, nd);
1629 if (unlikely(retval == -ESTALE))
1630 retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11001631
1632 if (likely(!retval)) {
1633 if (unlikely(!audit_dummy_context())) {
1634 if (nd->path.dentry && nd->inode)
1635 audit_inode(name, nd->path.dentry);
1636 }
1637 }
Al Viro9b4a9b12009-04-07 11:44:16 -04001638 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639}
1640
Al Viroc9c6cac2011-02-16 15:15:47 -05001641int kern_path_parent(const char *name, struct nameidata *nd)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001642{
Al Viroc9c6cac2011-02-16 15:15:47 -05001643 return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001644}
1645
Al Virod1811462008-08-02 00:49:18 -04001646int kern_path(const char *name, unsigned int flags, struct path *path)
1647{
1648 struct nameidata nd;
1649 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1650 if (!res)
1651 *path = nd.path;
1652 return res;
1653}
1654
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001655/**
1656 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1657 * @dentry: pointer to dentry of the base directory
1658 * @mnt: pointer to vfs mount of the base directory
1659 * @name: pointer to file name
1660 * @flags: lookup flags
1661 * @nd: pointer to nameidata
1662 */
1663int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
1664 const char *name, unsigned int flags,
1665 struct nameidata *nd)
1666{
Al Viro5b6ca022011-03-09 23:04:47 -05001667 nd->root.dentry = dentry;
1668 nd->root.mnt = mnt;
1669 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
1670 return do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, nd);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001671}
1672
Christoph Hellwigeead1912007-10-16 23:25:38 -07001673static struct dentry *__lookup_hash(struct qstr *name,
1674 struct dentry *base, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675{
Christoph Hellwig81fca442010-10-06 10:47:47 +02001676 struct inode *inode = base->d_inode;
James Morris057f6c02007-04-26 00:12:05 -07001677 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 int err;
1679
Nick Pigginb74c79e2011-01-07 17:49:58 +11001680 err = exec_permission(inode, 0);
Christoph Hellwig81fca442010-10-06 10:47:47 +02001681 if (err)
1682 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
1684 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001685 * Don't bother with __d_lookup: callers are for creat as
1686 * well as unlink, so a lot of the time it would cost
1687 * a double lookup.
Al Viro6e6b1bd2009-08-13 23:38:37 +04001688 */
Nick Pigginb04f7842010-08-18 04:37:34 +10001689 dentry = d_lookup(base, name);
Al Viro6e6b1bd2009-08-13 23:38:37 +04001690
Nick Pigginfb045ad2011-01-07 17:49:55 +11001691 if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE))
Al Viro6e6b1bd2009-08-13 23:38:37 +04001692 dentry = do_revalidate(dentry, nd);
1693
Nick Pigginbaa03892010-08-18 04:37:31 +10001694 if (!dentry)
1695 dentry = d_alloc_and_lookup(base, name, nd);
Al Viro5a202bc2011-03-08 14:17:44 -05001696
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 return dentry;
1698}
1699
James Morris057f6c02007-04-26 00:12:05 -07001700/*
1701 * Restricted form of lookup. Doesn't follow links, single-component only,
1702 * needs parent already locked. Doesn't follow mounts.
1703 * SMP-safe.
1704 */
Adrian Bunka244e162006-03-31 02:32:11 -08001705static struct dentry *lookup_hash(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706{
Jan Blunck4ac91372008-02-14 19:34:32 -08001707 return __lookup_hash(&nd->last, nd->path.dentry, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708}
1709
Christoph Hellwigeead1912007-10-16 23:25:38 -07001710/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07001711 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07001712 * @name: pathname component to lookup
1713 * @base: base directory to lookup from
1714 * @len: maximum length @len should be interpreted to
1715 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07001716 * Note that this routine is purely a helper for filesystem usage and should
1717 * not be called by generic code. Also note that by using this function the
Christoph Hellwigeead1912007-10-16 23:25:38 -07001718 * nameidata argument is passed to the filesystem methods and a filesystem
1719 * using this helper needs to be prepared for that.
1720 */
James Morris057f6c02007-04-26 00:12:05 -07001721struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1722{
James Morris057f6c02007-04-26 00:12:05 -07001723 struct qstr this;
Al Viro6a96ba52011-03-07 23:49:20 -05001724 unsigned long hash;
1725 unsigned int c;
James Morris057f6c02007-04-26 00:12:05 -07001726
David Woodhouse2f9092e2009-04-20 23:18:37 +01001727 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
1728
Al Viro6a96ba52011-03-07 23:49:20 -05001729 this.name = name;
1730 this.len = len;
1731 if (!len)
1732 return ERR_PTR(-EACCES);
1733
1734 hash = init_name_hash();
1735 while (len--) {
1736 c = *(const unsigned char *)name++;
1737 if (c == '/' || c == '\0')
1738 return ERR_PTR(-EACCES);
1739 hash = partial_name_hash(c, hash);
1740 }
1741 this.hash = end_name_hash(hash);
Al Viro5a202bc2011-03-08 14:17:44 -05001742 /*
1743 * See if the low-level filesystem might want
1744 * to use its own hash..
1745 */
1746 if (base->d_flags & DCACHE_OP_HASH) {
1747 int err = base->d_op->d_hash(base, base->d_inode, &this);
1748 if (err < 0)
1749 return ERR_PTR(err);
1750 }
Christoph Hellwigeead1912007-10-16 23:25:38 -07001751
Christoph Hellwig49705b72005-11-08 21:35:06 -08001752 return __lookup_hash(&this, base, NULL);
James Morris057f6c02007-04-26 00:12:05 -07001753}
1754
Andy Whitcroftb0917c32011-11-02 09:44:39 +01001755int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
1756 struct path *path, int *empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757{
Al Viro2d8f3032008-07-22 09:59:21 -04001758 struct nameidata nd;
Andy Whitcroftb0917c32011-11-02 09:44:39 +01001759 char *tmp = getname_flags(name, flags, empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 int err = PTR_ERR(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 if (!IS_ERR(tmp)) {
Al Viro2d8f3032008-07-22 09:59:21 -04001762
1763 BUG_ON(flags & LOOKUP_PARENT);
1764
1765 err = do_path_lookup(dfd, tmp, flags, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 putname(tmp);
Al Viro2d8f3032008-07-22 09:59:21 -04001767 if (!err)
1768 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 }
1770 return err;
1771}
1772
Andy Whitcroftb0917c32011-11-02 09:44:39 +01001773int user_path_at(int dfd, const char __user *name, unsigned flags,
1774 struct path *path)
1775{
1776 return user_path_at_empty(dfd, name, flags, path, 0);
1777}
1778
Al Viro2ad94ae2008-07-21 09:32:51 -04001779static int user_path_parent(int dfd, const char __user *path,
1780 struct nameidata *nd, char **name)
1781{
1782 char *s = getname(path);
1783 int error;
1784
1785 if (IS_ERR(s))
1786 return PTR_ERR(s);
1787
1788 error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
1789 if (error)
1790 putname(s);
1791 else
1792 *name = s;
1793
1794 return error;
1795}
1796
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797/*
1798 * It's inline, so penalty for filesystems that don't use sticky bit is
1799 * minimal.
1800 */
1801static inline int check_sticky(struct inode *dir, struct inode *inode)
1802{
David Howellsda9592e2008-11-14 10:39:05 +11001803 uid_t fsuid = current_fsuid();
1804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 if (!(dir->i_mode & S_ISVTX))
1806 return 0;
Serge E. Hallyne795b712011-03-23 16:43:25 -07001807 if (current_user_ns() != inode_userns(inode))
1808 goto other_userns;
David Howellsda9592e2008-11-14 10:39:05 +11001809 if (inode->i_uid == fsuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 return 0;
David Howellsda9592e2008-11-14 10:39:05 +11001811 if (dir->i_uid == fsuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 return 0;
Serge E. Hallyne795b712011-03-23 16:43:25 -07001813
1814other_userns:
1815 return !ns_capable(inode_userns(inode), CAP_FOWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816}
1817
1818/*
1819 * Check whether we can remove a link victim from directory dir, check
1820 * whether the type of victim is right.
1821 * 1. We can't do it if dir is read-only (done in permission())
1822 * 2. We should have write and exec permissions on dir
1823 * 3. We can't remove anything from append-only dir
1824 * 4. We can't do anything with immutable dir (done in permission())
1825 * 5. If the sticky bit on dir is set we should either
1826 * a. be owner of dir, or
1827 * b. be owner of victim, or
1828 * c. have CAP_FOWNER capability
1829 * 6. If the victim is append-only or immutable we can't do antyhing with
1830 * links pointing to it.
1831 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1832 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1833 * 9. We can't remove a root or mountpoint.
1834 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1835 * nfs_async_unlink().
1836 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001837static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838{
1839 int error;
1840
1841 if (!victim->d_inode)
1842 return -ENOENT;
1843
1844 BUG_ON(victim->d_parent->d_inode != dir);
Al Virocccc6bb2009-12-25 05:07:33 -05001845 audit_inode_child(victim, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
Al Virof419a2e2008-07-22 00:07:17 -04001847 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 if (error)
1849 return error;
1850 if (IS_APPEND(dir))
1851 return -EPERM;
1852 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
Hugh Dickinsf9454542008-11-19 15:36:38 -08001853 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 return -EPERM;
1855 if (isdir) {
1856 if (!S_ISDIR(victim->d_inode->i_mode))
1857 return -ENOTDIR;
1858 if (IS_ROOT(victim))
1859 return -EBUSY;
1860 } else if (S_ISDIR(victim->d_inode->i_mode))
1861 return -EISDIR;
1862 if (IS_DEADDIR(dir))
1863 return -ENOENT;
1864 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1865 return -EBUSY;
1866 return 0;
1867}
1868
1869/* Check whether we can create an object with dentry child in directory
1870 * dir.
1871 * 1. We can't do it if child already exists (open has special treatment for
1872 * this case, but since we are inlined it's OK)
1873 * 2. We can't do it if dir is read-only (done in permission())
1874 * 3. We should have write and exec permissions on dir
1875 * 4. We can't do it if dir is immutable (done in permission())
1876 */
Miklos Szeredia95164d2008-07-30 15:08:48 +02001877static inline int may_create(struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878{
1879 if (child->d_inode)
1880 return -EEXIST;
1881 if (IS_DEADDIR(dir))
1882 return -ENOENT;
Al Virof419a2e2008-07-22 00:07:17 -04001883 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884}
1885
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886/*
1887 * p1 and p2 should be directories on the same fs.
1888 */
1889struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1890{
1891 struct dentry *p;
1892
1893 if (p1 == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07001894 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 return NULL;
1896 }
1897
Arjan van de Vena11f3a02006-03-23 03:00:33 -08001898 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001900 p = d_ancestor(p2, p1);
1901 if (p) {
1902 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1903 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
1904 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 }
1906
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001907 p = d_ancestor(p1, p2);
1908 if (p) {
1909 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1910 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1911 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 }
1913
Ingo Molnarf2eace22006-07-03 00:25:05 -07001914 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1915 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 return NULL;
1917}
1918
1919void unlock_rename(struct dentry *p1, struct dentry *p2)
1920{
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001921 mutex_unlock(&p1->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 if (p1 != p2) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001923 mutex_unlock(&p2->d_inode->i_mutex);
Arjan van de Vena11f3a02006-03-23 03:00:33 -08001924 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 }
1926}
1927
1928int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1929 struct nameidata *nd)
1930{
Miklos Szeredia95164d2008-07-30 15:08:48 +02001931 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
1933 if (error)
1934 return error;
1935
Al Viroacfa4382008-12-04 10:06:33 -05001936 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 return -EACCES; /* shouldn't it be ENOSYS? */
1938 mode &= S_IALLUGO;
1939 mode |= S_IFREG;
1940 error = security_inode_create(dir, dentry, mode);
1941 if (error)
1942 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 error = dir->i_op->create(dir, dentry, mode, nd);
Stephen Smalleya74574a2005-09-09 13:01:44 -07001944 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00001945 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 return error;
1947}
1948
Al Viro73d049a2011-03-11 12:08:24 -05001949static int may_open(struct path *path, int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001951 struct dentry *dentry = path->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 struct inode *inode = dentry->d_inode;
1953 int error;
1954
Al Virobcda7652011-03-13 16:42:14 -04001955 /* O_PATH? */
1956 if (!acc_mode)
1957 return 0;
1958
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 if (!inode)
1960 return -ENOENT;
1961
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001962 switch (inode->i_mode & S_IFMT) {
1963 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001965 case S_IFDIR:
1966 if (acc_mode & MAY_WRITE)
1967 return -EISDIR;
1968 break;
1969 case S_IFBLK:
1970 case S_IFCHR:
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001971 if (path->mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001973 /*FALLTHRU*/
1974 case S_IFIFO:
1975 case S_IFSOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001977 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08001978 }
Dave Hansenb41572e2007-10-16 23:31:14 -07001979
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001980 error = inode_permission(inode, acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07001981 if (error)
1982 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05001983
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 /*
1985 * An append-only file must be opened in append mode for writing.
1986 */
1987 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05001988 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05001989 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05001991 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 }
1993
1994 /* O_NOATIME can only be set by the owner or superuser */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001995 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
Al Viro7715b522009-12-16 03:54:00 -05001996 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
1998 /*
1999 * Ensure there are no outstanding leases on the file.
2000 */
Al Virob65a9cf2009-12-16 06:27:40 -05002001 return break_lease(inode, flag);
Al Viro7715b522009-12-16 03:54:00 -05002002}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
Jeff Laytone1181ee2010-12-07 16:19:50 -05002004static int handle_truncate(struct file *filp)
Al Viro7715b522009-12-16 03:54:00 -05002005{
Jeff Laytone1181ee2010-12-07 16:19:50 -05002006 struct path *path = &filp->f_path;
Al Viro7715b522009-12-16 03:54:00 -05002007 struct inode *inode = path->dentry->d_inode;
2008 int error = get_write_access(inode);
2009 if (error)
2010 return error;
2011 /*
2012 * Refuse to truncate files with mandatory locks held on them.
2013 */
2014 error = locks_verify_locked(inode);
2015 if (!error)
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +09002016 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05002017 if (!error) {
2018 error = do_truncate(path->dentry, 0,
2019 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
Jeff Laytone1181ee2010-12-07 16:19:50 -05002020 filp);
Al Viro7715b522009-12-16 03:54:00 -05002021 }
2022 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04002023 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024}
2025
Dave Hansend57999e2008-02-15 14:37:27 -08002026/*
Dave Hansend57999e2008-02-15 14:37:27 -08002027 * Note that while the flag value (low two bits) for sys_open means:
2028 * 00 - read-only
2029 * 01 - write-only
2030 * 10 - read-write
2031 * 11 - special
2032 * it is changed into
2033 * 00 - no permissions needed
2034 * 01 - read-permission
2035 * 10 - write-permission
2036 * 11 - read-write
2037 * for the internal routines (ie open_namei()/follow_link() etc)
2038 * This is more logical, and also allows the 00 "no perm needed"
2039 * to be used for symlinks (where the permissions are checked
2040 * later).
2041 *
2042*/
2043static inline int open_to_namei_flags(int flag)
2044{
2045 if ((flag+1) & O_ACCMODE)
2046 flag++;
2047 return flag;
2048}
2049
Nick Piggin31e6b012011-01-07 17:49:52 +11002050/*
Al Virofe2d35f2011-03-05 22:58:25 -05002051 * Handle the last step of open()
Nick Piggin31e6b012011-01-07 17:49:52 +11002052 */
Al Virofb1cc552009-12-24 01:58:28 -05002053static struct file *do_last(struct nameidata *nd, struct path *path,
Al Viroc3e380b2011-02-23 13:39:45 -05002054 const struct open_flags *op, const char *pathname)
Al Virofb1cc552009-12-24 01:58:28 -05002055{
Al Viroa1e28032009-12-24 02:12:06 -05002056 struct dentry *dir = nd->path.dentry;
Al Viro6c0d46c2011-03-09 00:59:59 -05002057 struct dentry *dentry;
Al Viroca344a892011-03-09 00:36:45 -05002058 int open_flag = op->open_flag;
Al Viro6c0d46c2011-03-09 00:59:59 -05002059 int will_truncate = open_flag & O_TRUNC;
Al Viroca344a892011-03-09 00:36:45 -05002060 int want_write = 0;
Al Virobcda7652011-03-13 16:42:14 -04002061 int acc_mode = op->acc_mode;
Al Virofb1cc552009-12-24 01:58:28 -05002062 struct file *filp;
Al Viro16c2cd72011-02-22 15:50:10 -05002063 int error;
Al Virofb1cc552009-12-24 01:58:28 -05002064
Al Viroc3e380b2011-02-23 13:39:45 -05002065 nd->flags &= ~LOOKUP_PARENT;
2066 nd->flags |= op->intent;
2067
Al Viro1f36f772009-12-26 10:56:19 -05002068 switch (nd->last_type) {
2069 case LAST_DOTDOT:
Neil Brown176306f2010-05-24 16:57:56 +10002070 case LAST_DOT:
Al Virofe2d35f2011-03-05 22:58:25 -05002071 error = handle_dots(nd, nd->last_type);
2072 if (error)
2073 return ERR_PTR(error);
Al Viro1f36f772009-12-26 10:56:19 -05002074 /* fallthrough */
Al Viro1f36f772009-12-26 10:56:19 -05002075 case LAST_ROOT:
Al Viro9f1fafe2011-03-25 11:00:12 -04002076 error = complete_walk(nd);
Al Viro16c2cd72011-02-22 15:50:10 -05002077 if (error)
Al Viro9f1fafe2011-03-25 11:00:12 -04002078 return ERR_PTR(error);
Al Virofe2d35f2011-03-05 22:58:25 -05002079 audit_inode(pathname, nd->path.dentry);
Al Viroca344a892011-03-09 00:36:45 -05002080 if (open_flag & O_CREAT) {
Al Virofe2d35f2011-03-05 22:58:25 -05002081 error = -EISDIR;
2082 goto exit;
2083 }
2084 goto ok;
Al Viro1f36f772009-12-26 10:56:19 -05002085 case LAST_BIND:
Al Viro9f1fafe2011-03-25 11:00:12 -04002086 error = complete_walk(nd);
Al Viro16c2cd72011-02-22 15:50:10 -05002087 if (error)
Al Viro9f1fafe2011-03-25 11:00:12 -04002088 return ERR_PTR(error);
Al Viro1f36f772009-12-26 10:56:19 -05002089 audit_inode(pathname, dir);
Al Viro67ee3ad2009-12-26 07:01:01 -05002090 goto ok;
Al Viro1f36f772009-12-26 10:56:19 -05002091 }
Al Viro67ee3ad2009-12-26 07:01:01 -05002092
Al Viroca344a892011-03-09 00:36:45 -05002093 if (!(open_flag & O_CREAT)) {
Al Virobcda7652011-03-13 16:42:14 -04002094 int symlink_ok = 0;
Al Virofe2d35f2011-03-05 22:58:25 -05002095 if (nd->last.name[nd->last.len])
2096 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
Al Virobcda7652011-03-13 16:42:14 -04002097 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2098 symlink_ok = 1;
Al Virofe2d35f2011-03-05 22:58:25 -05002099 /* we _can_ be in RCU mode here */
Al Viroce57dfc2011-03-13 19:58:58 -04002100 error = walk_component(nd, path, &nd->last, LAST_NORM,
2101 !symlink_ok);
2102 if (error < 0)
Al Virofe2d35f2011-03-05 22:58:25 -05002103 return ERR_PTR(error);
Al Viroce57dfc2011-03-13 19:58:58 -04002104 if (error) /* symlink */
Al Virofe2d35f2011-03-05 22:58:25 -05002105 return NULL;
Al Virofe2d35f2011-03-05 22:58:25 -05002106 /* sayonara */
Al Viro9f1fafe2011-03-25 11:00:12 -04002107 error = complete_walk(nd);
2108 if (error)
2109 return ERR_PTR(-ECHILD);
Al Virofe2d35f2011-03-05 22:58:25 -05002110
2111 error = -ENOTDIR;
2112 if (nd->flags & LOOKUP_DIRECTORY) {
Al Viroce57dfc2011-03-13 19:58:58 -04002113 if (!nd->inode->i_op->lookup)
Al Virofe2d35f2011-03-05 22:58:25 -05002114 goto exit;
2115 }
2116 audit_inode(pathname, nd->path.dentry);
2117 goto ok;
2118 }
2119
2120 /* create side of things */
Al Viro9f1fafe2011-03-25 11:00:12 -04002121 error = complete_walk(nd);
2122 if (error)
2123 return ERR_PTR(error);
Al Virofe2d35f2011-03-05 22:58:25 -05002124
2125 audit_inode(pathname, dir);
Al Viro16c2cd72011-02-22 15:50:10 -05002126 error = -EISDIR;
Al Viro1f36f772009-12-26 10:56:19 -05002127 /* trailing slashes? */
Nick Piggin31e6b012011-01-07 17:49:52 +11002128 if (nd->last.name[nd->last.len])
2129 goto exit;
Al Viroa2c36b42009-12-24 03:39:50 -05002130
Al Viroa1e28032009-12-24 02:12:06 -05002131 mutex_lock(&dir->d_inode->i_mutex);
2132
Al Viro6c0d46c2011-03-09 00:59:59 -05002133 dentry = lookup_hash(nd);
2134 error = PTR_ERR(dentry);
2135 if (IS_ERR(dentry)) {
Al Virofb1cc552009-12-24 01:58:28 -05002136 mutex_unlock(&dir->d_inode->i_mutex);
2137 goto exit;
2138 }
2139
Al Viro6c0d46c2011-03-09 00:59:59 -05002140 path->dentry = dentry;
2141 path->mnt = nd->path.mnt;
2142
Al Virofb1cc552009-12-24 01:58:28 -05002143 /* Negative dentry, just create the file */
Al Viro6c0d46c2011-03-09 00:59:59 -05002144 if (!dentry->d_inode) {
2145 int mode = op->mode;
2146 if (!IS_POSIXACL(dir->d_inode))
2147 mode &= ~current_umask();
Al Virofb1cc552009-12-24 01:58:28 -05002148 /*
2149 * This write is needed to ensure that a
Al Viro6c0d46c2011-03-09 00:59:59 -05002150 * rw->ro transition does not occur between
Al Virofb1cc552009-12-24 01:58:28 -05002151 * the time when the file is created and when
2152 * a permanent write count is taken through
2153 * the 'struct file' in nameidata_to_filp().
2154 */
2155 error = mnt_want_write(nd->path.mnt);
2156 if (error)
2157 goto exit_mutex_unlock;
Al Viroca344a892011-03-09 00:36:45 -05002158 want_write = 1;
Al Viro9b44f1b2011-03-09 00:17:27 -05002159 /* Don't check for write permission, don't truncate */
Al Viroca344a892011-03-09 00:36:45 -05002160 open_flag &= ~O_TRUNC;
Al Viro6c0d46c2011-03-09 00:59:59 -05002161 will_truncate = 0;
Al Virobcda7652011-03-13 16:42:14 -04002162 acc_mode = MAY_OPEN;
Al Viro6c0d46c2011-03-09 00:59:59 -05002163 error = security_path_mknod(&nd->path, dentry, mode, 0);
2164 if (error)
2165 goto exit_mutex_unlock;
2166 error = vfs_create(dir->d_inode, dentry, mode, nd);
2167 if (error)
2168 goto exit_mutex_unlock;
2169 mutex_unlock(&dir->d_inode->i_mutex);
2170 dput(nd->path.dentry);
2171 nd->path.dentry = dentry;
Al Viroca344a892011-03-09 00:36:45 -05002172 goto common;
Al Virofb1cc552009-12-24 01:58:28 -05002173 }
2174
2175 /*
2176 * It already exists.
2177 */
2178 mutex_unlock(&dir->d_inode->i_mutex);
2179 audit_inode(pathname, path->dentry);
2180
2181 error = -EEXIST;
Al Viroca344a892011-03-09 00:36:45 -05002182 if (open_flag & O_EXCL)
Al Virofb1cc552009-12-24 01:58:28 -05002183 goto exit_dput;
2184
David Howells9875cf82011-01-14 18:45:21 +00002185 error = follow_managed(path, nd->flags);
2186 if (error < 0)
2187 goto exit_dput;
Al Virofb1cc552009-12-24 01:58:28 -05002188
2189 error = -ENOENT;
2190 if (!path->dentry->d_inode)
2191 goto exit_dput;
Al Viro9e67f362009-12-26 07:04:50 -05002192
2193 if (path->dentry->d_inode->i_op->follow_link)
Al Virofb1cc552009-12-24 01:58:28 -05002194 return NULL;
Al Virofb1cc552009-12-24 01:58:28 -05002195
2196 path_to_nameidata(path, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11002197 nd->inode = path->dentry->d_inode;
Al Virofb1cc552009-12-24 01:58:28 -05002198 error = -EISDIR;
Nick Piggin31e6b012011-01-07 17:49:52 +11002199 if (S_ISDIR(nd->inode->i_mode))
Al Virofb1cc552009-12-24 01:58:28 -05002200 goto exit;
Al Viro67ee3ad2009-12-26 07:01:01 -05002201ok:
Al Viro6c0d46c2011-03-09 00:59:59 -05002202 if (!S_ISREG(nd->inode->i_mode))
2203 will_truncate = 0;
2204
Al Viro0f9d1a12011-03-09 00:13:14 -05002205 if (will_truncate) {
2206 error = mnt_want_write(nd->path.mnt);
2207 if (error)
2208 goto exit;
Al Viroca344a892011-03-09 00:36:45 -05002209 want_write = 1;
Al Viro0f9d1a12011-03-09 00:13:14 -05002210 }
Al Viroca344a892011-03-09 00:36:45 -05002211common:
Al Virobcda7652011-03-13 16:42:14 -04002212 error = may_open(&nd->path, acc_mode, open_flag);
Al Viroca344a892011-03-09 00:36:45 -05002213 if (error)
Al Viro0f9d1a12011-03-09 00:13:14 -05002214 goto exit;
Al Viro0f9d1a12011-03-09 00:13:14 -05002215 filp = nameidata_to_filp(nd);
2216 if (!IS_ERR(filp)) {
2217 error = ima_file_check(filp, op->acc_mode);
2218 if (error) {
2219 fput(filp);
2220 filp = ERR_PTR(error);
2221 }
2222 }
2223 if (!IS_ERR(filp)) {
2224 if (will_truncate) {
2225 error = handle_truncate(filp);
2226 if (error) {
2227 fput(filp);
2228 filp = ERR_PTR(error);
2229 }
2230 }
2231 }
Al Viroca344a892011-03-09 00:36:45 -05002232out:
2233 if (want_write)
Al Viro0f9d1a12011-03-09 00:13:14 -05002234 mnt_drop_write(nd->path.mnt);
2235 path_put(&nd->path);
Al Virofb1cc552009-12-24 01:58:28 -05002236 return filp;
2237
2238exit_mutex_unlock:
2239 mutex_unlock(&dir->d_inode->i_mutex);
2240exit_dput:
2241 path_put_conditional(path, nd);
2242exit:
Al Viroca344a892011-03-09 00:36:45 -05002243 filp = ERR_PTR(error);
2244 goto out;
Al Virofb1cc552009-12-24 01:58:28 -05002245}
2246
Al Viro13aab422011-02-23 17:54:08 -05002247static struct file *path_openat(int dfd, const char *pathname,
Al Viro73d049a2011-03-11 12:08:24 -05002248 struct nameidata *nd, const struct open_flags *op, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249{
Al Virofe2d35f2011-03-05 22:58:25 -05002250 struct file *base = NULL;
Dave Hansen4a3fd212008-02-15 14:37:48 -08002251 struct file *filp;
Al Viro9850c052010-01-13 15:01:15 -05002252 struct path path;
Al Viro13aab422011-02-23 17:54:08 -05002253 int error;
Nick Piggin31e6b012011-01-07 17:49:52 +11002254
2255 filp = get_empty_filp();
2256 if (!filp)
2257 return ERR_PTR(-ENFILE);
2258
Al Viro47c805d2011-02-23 17:44:09 -05002259 filp->f_flags = op->open_flag;
Al Viro73d049a2011-03-11 12:08:24 -05002260 nd->intent.open.file = filp;
2261 nd->intent.open.flags = open_to_namei_flags(op->open_flag);
2262 nd->intent.open.create_mode = op->mode;
Nick Piggin31e6b012011-01-07 17:49:52 +11002263
Al Viro73d049a2011-03-11 12:08:24 -05002264 error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
Nick Piggin31e6b012011-01-07 17:49:52 +11002265 if (unlikely(error))
Al Viro13aab422011-02-23 17:54:08 -05002266 goto out_filp;
Nick Piggin31e6b012011-01-07 17:49:52 +11002267
Al Virofe2d35f2011-03-05 22:58:25 -05002268 current->total_link_count = 0;
Al Viro73d049a2011-03-11 12:08:24 -05002269 error = link_path_walk(pathname, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11002270 if (unlikely(error))
2271 goto out_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
Al Viro73d049a2011-03-11 12:08:24 -05002273 filp = do_last(nd, &path, op, pathname);
Al Viro806b6812009-12-26 07:16:40 -05002274 while (unlikely(!filp)) { /* trailing symlink */
Nick Piggin7b9337a2011-01-14 08:42:43 +00002275 struct path link = path;
Al Virodef4af32009-12-26 08:37:05 -05002276 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04002277 if (!(nd->flags & LOOKUP_FOLLOW)) {
Al Viro73d049a2011-03-11 12:08:24 -05002278 path_put_conditional(&path, nd);
2279 path_put(&nd->path);
Al Viro40b39132011-03-09 16:22:18 -05002280 filp = ERR_PTR(-ELOOP);
2281 break;
2282 }
Al Viro73d049a2011-03-11 12:08:24 -05002283 nd->flags |= LOOKUP_PARENT;
2284 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
Al Viro574197e2011-03-14 22:20:34 -04002285 error = follow_link(&link, nd, &cookie);
Al Viroc3e380b2011-02-23 13:39:45 -05002286 if (unlikely(error))
Al Virof1afe9e2011-02-22 22:27:28 -05002287 filp = ERR_PTR(error);
Al Viroc3e380b2011-02-23 13:39:45 -05002288 else
Al Viro73d049a2011-03-11 12:08:24 -05002289 filp = do_last(nd, &path, op, pathname);
Al Viro574197e2011-03-14 22:20:34 -04002290 put_link(nd, &link, cookie);
Al Viro806b6812009-12-26 07:16:40 -05002291 }
Al Viro10fa8e62009-12-26 07:09:49 -05002292out:
Al Viro73d049a2011-03-11 12:08:24 -05002293 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
2294 path_put(&nd->root);
Al Virofe2d35f2011-03-05 22:58:25 -05002295 if (base)
2296 fput(base);
Al Viro73d049a2011-03-11 12:08:24 -05002297 release_open_intent(nd);
Al Viro10fa8e62009-12-26 07:09:49 -05002298 return filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
Nick Piggin31e6b012011-01-07 17:49:52 +11002300out_filp:
Al Viro806b6812009-12-26 07:16:40 -05002301 filp = ERR_PTR(error);
Al Viro10fa8e62009-12-26 07:09:49 -05002302 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303}
2304
Al Viro13aab422011-02-23 17:54:08 -05002305struct file *do_filp_open(int dfd, const char *pathname,
2306 const struct open_flags *op, int flags)
2307{
Al Viro73d049a2011-03-11 12:08:24 -05002308 struct nameidata nd;
Al Viro13aab422011-02-23 17:54:08 -05002309 struct file *filp;
2310
Al Viro73d049a2011-03-11 12:08:24 -05002311 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
Al Viro13aab422011-02-23 17:54:08 -05002312 if (unlikely(filp == ERR_PTR(-ECHILD)))
Al Viro73d049a2011-03-11 12:08:24 -05002313 filp = path_openat(dfd, pathname, &nd, op, flags);
Al Viro13aab422011-02-23 17:54:08 -05002314 if (unlikely(filp == ERR_PTR(-ESTALE)))
Al Viro73d049a2011-03-11 12:08:24 -05002315 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
Al Viro13aab422011-02-23 17:54:08 -05002316 return filp;
2317}
2318
Al Viro73d049a2011-03-11 12:08:24 -05002319struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
2320 const char *name, const struct open_flags *op, int flags)
2321{
2322 struct nameidata nd;
2323 struct file *file;
2324
2325 nd.root.mnt = mnt;
2326 nd.root.dentry = dentry;
2327
2328 flags |= LOOKUP_ROOT;
2329
Al Virobcda7652011-03-13 16:42:14 -04002330 if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
Al Viro73d049a2011-03-11 12:08:24 -05002331 return ERR_PTR(-ELOOP);
2332
2333 file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
2334 if (unlikely(file == ERR_PTR(-ECHILD)))
2335 file = path_openat(-1, name, &nd, op, flags);
2336 if (unlikely(file == ERR_PTR(-ESTALE)))
2337 file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
2338 return file;
2339}
2340
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341/**
2342 * lookup_create - lookup a dentry, creating it if it doesn't exist
2343 * @nd: nameidata info
2344 * @is_dir: directory flag
2345 *
2346 * Simple function to lookup and return a dentry and create it
2347 * if it doesn't exist. Is SMP-safe.
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002348 *
Jan Blunck4ac91372008-02-14 19:34:32 -08002349 * Returns with nd->path.dentry->d_inode->i_mutex locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 */
2351struct dentry *lookup_create(struct nameidata *nd, int is_dir)
2352{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002353 struct dentry *dentry = ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
Jan Blunck4ac91372008-02-14 19:34:32 -08002355 mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002356 /*
2357 * Yucky last component or no last component at all?
2358 * (foo/., foo/.., /////)
2359 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 if (nd->last_type != LAST_NORM)
2361 goto fail;
2362 nd->flags &= ~LOOKUP_PARENT;
Al Viro35165862008-08-05 03:00:49 -04002363 nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
ASANO Masahiroa6349042006-08-22 20:06:02 -04002364 nd->intent.open.flags = O_EXCL;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002365
2366 /*
2367 * Do the final lookup.
2368 */
Christoph Hellwig49705b72005-11-08 21:35:06 -08002369 dentry = lookup_hash(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 if (IS_ERR(dentry))
2371 goto fail;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002372
Al Viroe9baf6e2008-05-15 04:49:12 -04002373 if (dentry->d_inode)
2374 goto eexist;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002375 /*
2376 * Special case - lookup gave negative, but... we had foo/bar/
2377 * From the vfs_mknod() POV we just have a negative dentry -
2378 * all is fine. Let's be bastards - you had / on the end, you've
2379 * been asking for (non-existent) directory. -ENOENT for you.
2380 */
Al Viroe9baf6e2008-05-15 04:49:12 -04002381 if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
2382 dput(dentry);
2383 dentry = ERR_PTR(-ENOENT);
2384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 return dentry;
Al Viroe9baf6e2008-05-15 04:49:12 -04002386eexist:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 dput(dentry);
Al Viroe9baf6e2008-05-15 04:49:12 -04002388 dentry = ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389fail:
2390 return dentry;
2391}
Christoph Hellwigf81a0bf2005-05-19 12:26:43 -07002392EXPORT_SYMBOL_GPL(lookup_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
2394int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
2395{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002396 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
2398 if (error)
2399 return error;
2400
Serge E. Hallyne795b712011-03-23 16:43:25 -07002401 if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
2402 !ns_capable(inode_userns(dir), CAP_MKNOD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 return -EPERM;
2404
Al Viroacfa4382008-12-04 10:06:33 -05002405 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 return -EPERM;
2407
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07002408 error = devcgroup_inode_mknod(mode, dev);
2409 if (error)
2410 return error;
2411
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 error = security_inode_mknod(dir, dentry, mode, dev);
2413 if (error)
2414 return error;
2415
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002417 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002418 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 return error;
2420}
2421
Dave Hansen463c3192008-02-15 14:37:57 -08002422static int may_mknod(mode_t mode)
2423{
2424 switch (mode & S_IFMT) {
2425 case S_IFREG:
2426 case S_IFCHR:
2427 case S_IFBLK:
2428 case S_IFIFO:
2429 case S_IFSOCK:
2430 case 0: /* zero mode translates to S_IFREG */
2431 return 0;
2432 case S_IFDIR:
2433 return -EPERM;
2434 default:
2435 return -EINVAL;
2436 }
2437}
2438
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002439SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
2440 unsigned, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441{
Al Viro2ad94ae2008-07-21 09:32:51 -04002442 int error;
2443 char *tmp;
2444 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 struct nameidata nd;
2446
2447 if (S_ISDIR(mode))
2448 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
Al Viro2ad94ae2008-07-21 09:32:51 -04002450 error = user_path_parent(dfd, filename, &nd, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002452 return error;
2453
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 dentry = lookup_create(&nd, 0);
Dave Hansen463c3192008-02-15 14:37:57 -08002455 if (IS_ERR(dentry)) {
2456 error = PTR_ERR(dentry);
2457 goto out_unlock;
2458 }
Jan Blunck4ac91372008-02-14 19:34:32 -08002459 if (!IS_POSIXACL(nd.path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04002460 mode &= ~current_umask();
Dave Hansen463c3192008-02-15 14:37:57 -08002461 error = may_mknod(mode);
2462 if (error)
2463 goto out_dput;
2464 error = mnt_want_write(nd.path.mnt);
2465 if (error)
2466 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002467 error = security_path_mknod(&nd.path, dentry, mode, dev);
2468 if (error)
2469 goto out_drop_write;
Dave Hansen463c3192008-02-15 14:37:57 -08002470 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 case 0: case S_IFREG:
Jan Blunck4ac91372008-02-14 19:34:32 -08002472 error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 break;
2474 case S_IFCHR: case S_IFBLK:
Jan Blunck4ac91372008-02-14 19:34:32 -08002475 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 new_decode_dev(dev));
2477 break;
2478 case S_IFIFO: case S_IFSOCK:
Jan Blunck4ac91372008-02-14 19:34:32 -08002479 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 }
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002482out_drop_write:
Dave Hansen463c3192008-02-15 14:37:57 -08002483 mnt_drop_write(nd.path.mnt);
2484out_dput:
2485 dput(dentry);
2486out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002487 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002488 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 putname(tmp);
2490
2491 return error;
2492}
2493
Heiko Carstens3480b252009-01-14 14:14:16 +01002494SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002495{
2496 return sys_mknodat(AT_FDCWD, filename, mode, dev);
2497}
2498
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2500{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002501 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
2503 if (error)
2504 return error;
2505
Al Viroacfa4382008-12-04 10:06:33 -05002506 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 return -EPERM;
2508
2509 mode &= (S_IRWXUGO|S_ISVTX);
2510 error = security_inode_mkdir(dir, dentry, mode);
2511 if (error)
2512 return error;
2513
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002515 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002516 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 return error;
2518}
2519
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002520SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521{
2522 int error = 0;
2523 char * tmp;
Dave Hansen6902d922006-09-30 23:29:01 -07002524 struct dentry *dentry;
2525 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
Al Viro2ad94ae2008-07-21 09:32:51 -04002527 error = user_path_parent(dfd, pathname, &nd, &tmp);
2528 if (error)
Dave Hansen6902d922006-09-30 23:29:01 -07002529 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530
Dave Hansen6902d922006-09-30 23:29:01 -07002531 dentry = lookup_create(&nd, 1);
2532 error = PTR_ERR(dentry);
2533 if (IS_ERR(dentry))
2534 goto out_unlock;
2535
Jan Blunck4ac91372008-02-14 19:34:32 -08002536 if (!IS_POSIXACL(nd.path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04002537 mode &= ~current_umask();
Dave Hansen463c3192008-02-15 14:37:57 -08002538 error = mnt_want_write(nd.path.mnt);
2539 if (error)
2540 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002541 error = security_path_mkdir(&nd.path, dentry, mode);
2542 if (error)
2543 goto out_drop_write;
Jan Blunck4ac91372008-02-14 19:34:32 -08002544 error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002545out_drop_write:
Dave Hansen463c3192008-02-15 14:37:57 -08002546 mnt_drop_write(nd.path.mnt);
2547out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002548 dput(dentry);
2549out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002550 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002551 path_put(&nd.path);
Dave Hansen6902d922006-09-30 23:29:01 -07002552 putname(tmp);
2553out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 return error;
2555}
2556
Heiko Carstens3cdad422009-01-14 14:14:22 +01002557SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002558{
2559 return sys_mkdirat(AT_FDCWD, pathname, mode);
2560}
2561
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562/*
Sage Weila71905f2011-05-24 13:06:08 -07002563 * The dentry_unhash() helper will try to drop the dentry early: we
2564 * should have a usage count of 2 if we're the only user of this
2565 * dentry, and if that is true (possibly after pruning the dcache),
2566 * then we drop the dentry now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 *
2568 * A low-level filesystem can, if it choses, legally
2569 * do a
2570 *
2571 * if (!d_unhashed(dentry))
2572 * return -EBUSY;
2573 *
2574 * if it cannot handle the case of removing a directory
2575 * that is still in use by something else..
2576 */
2577void dentry_unhash(struct dentry *dentry)
2578{
Vasily Averindc168422006-12-06 20:37:07 -08002579 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 spin_lock(&dentry->d_lock);
Sage Weil64252c72011-05-24 13:06:05 -07002581 if (dentry->d_count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 __d_drop(dentry);
2583 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584}
2585
2586int vfs_rmdir(struct inode *dir, struct dentry *dentry)
2587{
2588 int error = may_delete(dir, dentry, 1);
2589
2590 if (error)
2591 return error;
2592
Al Viroacfa4382008-12-04 10:06:33 -05002593 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 return -EPERM;
2595
Al Viro97abc522011-09-14 18:55:41 +01002596 dget(dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002597 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598
Sage Weil912dbc12011-05-24 13:06:11 -07002599 error = -EBUSY;
2600 if (d_mountpoint(dentry))
2601 goto out;
2602
2603 error = security_inode_rmdir(dir, dentry);
2604 if (error)
2605 goto out;
2606
Sage Weil3cebde22011-05-29 21:20:59 -07002607 shrink_dcache_parent(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07002608 error = dir->i_op->rmdir(dir, dentry);
2609 if (error)
2610 goto out;
2611
2612 dentry->d_inode->i_flags |= S_DEAD;
2613 dont_mount(dentry);
2614
2615out:
2616 mutex_unlock(&dentry->d_inode->i_mutex);
Al Viro97abc522011-09-14 18:55:41 +01002617 dput(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07002618 if (!error)
2619 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 return error;
2621}
2622
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002623static long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624{
2625 int error = 0;
2626 char * name;
2627 struct dentry *dentry;
2628 struct nameidata nd;
2629
Al Viro2ad94ae2008-07-21 09:32:51 -04002630 error = user_path_parent(dfd, pathname, &nd, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002632 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633
2634 switch(nd.last_type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002635 case LAST_DOTDOT:
2636 error = -ENOTEMPTY;
2637 goto exit1;
2638 case LAST_DOT:
2639 error = -EINVAL;
2640 goto exit1;
2641 case LAST_ROOT:
2642 error = -EBUSY;
2643 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002645
2646 nd.flags &= ~LOOKUP_PARENT;
2647
Jan Blunck4ac91372008-02-14 19:34:32 -08002648 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002649 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002651 if (IS_ERR(dentry))
2652 goto exit2;
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04002653 if (!dentry->d_inode) {
2654 error = -ENOENT;
2655 goto exit3;
2656 }
Dave Hansen06227532008-02-15 14:37:34 -08002657 error = mnt_want_write(nd.path.mnt);
2658 if (error)
2659 goto exit3;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002660 error = security_path_rmdir(&nd.path, dentry);
2661 if (error)
2662 goto exit4;
Jan Blunck4ac91372008-02-14 19:34:32 -08002663 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002664exit4:
Dave Hansen06227532008-02-15 14:37:34 -08002665 mnt_drop_write(nd.path.mnt);
2666exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07002667 dput(dentry);
2668exit2:
Jan Blunck4ac91372008-02-14 19:34:32 -08002669 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002671 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 putname(name);
2673 return error;
2674}
2675
Heiko Carstens3cdad422009-01-14 14:14:22 +01002676SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002677{
2678 return do_rmdir(AT_FDCWD, pathname);
2679}
2680
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681int vfs_unlink(struct inode *dir, struct dentry *dentry)
2682{
2683 int error = may_delete(dir, dentry, 0);
2684
2685 if (error)
2686 return error;
2687
Al Viroacfa4382008-12-04 10:06:33 -05002688 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 return -EPERM;
2690
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002691 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 if (d_mountpoint(dentry))
2693 error = -EBUSY;
2694 else {
2695 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05002696 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 error = dir->i_op->unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05002698 if (!error)
Al Virod83c49f2010-04-30 17:17:09 -04002699 dont_mount(dentry);
Al Virobec10522010-03-03 14:12:08 -05002700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002702 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703
2704 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2705 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
Jan Karaece95912008-02-06 01:37:13 -08002706 fsnotify_link_count(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 }
Robert Love0eeca282005-07-12 17:06:03 -04002709
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 return error;
2711}
2712
2713/*
2714 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002715 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 * writeout happening, and we don't want to prevent access to the directory
2717 * while waiting on the I/O.
2718 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002719static long do_unlinkat(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720{
Al Viro2ad94ae2008-07-21 09:32:51 -04002721 int error;
2722 char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 struct dentry *dentry;
2724 struct nameidata nd;
2725 struct inode *inode = NULL;
2726
Al Viro2ad94ae2008-07-21 09:32:51 -04002727 error = user_path_parent(dfd, pathname, &nd, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002729 return error;
2730
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 error = -EISDIR;
2732 if (nd.last_type != LAST_NORM)
2733 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002734
2735 nd.flags &= ~LOOKUP_PARENT;
2736
Jan Blunck4ac91372008-02-14 19:34:32 -08002737 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002738 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 error = PTR_ERR(dentry);
2740 if (!IS_ERR(dentry)) {
2741 /* Why not before? Because we want correct error value */
Török Edwin50338b82011-06-16 00:06:14 +03002742 if (nd.last.name[nd.last.len])
2743 goto slashes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 inode = dentry->d_inode;
Török Edwin50338b82011-06-16 00:06:14 +03002745 if (!inode)
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04002746 goto slashes;
2747 ihold(inode);
Dave Hansen06227532008-02-15 14:37:34 -08002748 error = mnt_want_write(nd.path.mnt);
2749 if (error)
2750 goto exit2;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002751 error = security_path_unlink(&nd.path, dentry);
2752 if (error)
2753 goto exit3;
Jan Blunck4ac91372008-02-14 19:34:32 -08002754 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002755exit3:
Dave Hansen06227532008-02-15 14:37:34 -08002756 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 exit2:
2758 dput(dentry);
2759 }
Jan Blunck4ac91372008-02-14 19:34:32 -08002760 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 if (inode)
2762 iput(inode); /* truncate the inode here */
2763exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002764 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 putname(name);
2766 return error;
2767
2768slashes:
2769 error = !dentry->d_inode ? -ENOENT :
2770 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2771 goto exit2;
2772}
2773
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002774SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002775{
2776 if ((flag & ~AT_REMOVEDIR) != 0)
2777 return -EINVAL;
2778
2779 if (flag & AT_REMOVEDIR)
2780 return do_rmdir(dfd, pathname);
2781
2782 return do_unlinkat(dfd, pathname);
2783}
2784
Heiko Carstens3480b252009-01-14 14:14:16 +01002785SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002786{
2787 return do_unlinkat(AT_FDCWD, pathname);
2788}
2789
Miklos Szeredidb2e7472008-06-24 16:50:16 +02002790int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002792 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793
2794 if (error)
2795 return error;
2796
Al Viroacfa4382008-12-04 10:06:33 -05002797 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 return -EPERM;
2799
2800 error = security_inode_symlink(dir, dentry, oldname);
2801 if (error)
2802 return error;
2803
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002805 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002806 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 return error;
2808}
2809
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002810SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
2811 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812{
Al Viro2ad94ae2008-07-21 09:32:51 -04002813 int error;
2814 char *from;
2815 char *to;
Dave Hansen6902d922006-09-30 23:29:01 -07002816 struct dentry *dentry;
2817 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818
2819 from = getname(oldname);
Al Viro2ad94ae2008-07-21 09:32:51 -04002820 if (IS_ERR(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 return PTR_ERR(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04002822
2823 error = user_path_parent(newdfd, newname, &nd, &to);
2824 if (error)
Dave Hansen6902d922006-09-30 23:29:01 -07002825 goto out_putname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826
Dave Hansen6902d922006-09-30 23:29:01 -07002827 dentry = lookup_create(&nd, 0);
2828 error = PTR_ERR(dentry);
2829 if (IS_ERR(dentry))
2830 goto out_unlock;
2831
Dave Hansen75c3f292008-02-15 14:37:45 -08002832 error = mnt_want_write(nd.path.mnt);
2833 if (error)
2834 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002835 error = security_path_symlink(&nd.path, dentry, from);
2836 if (error)
2837 goto out_drop_write;
Miklos Szeredidb2e7472008-06-24 16:50:16 +02002838 error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002839out_drop_write:
Dave Hansen75c3f292008-02-15 14:37:45 -08002840 mnt_drop_write(nd.path.mnt);
2841out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002842 dput(dentry);
2843out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002844 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002845 path_put(&nd.path);
Dave Hansen6902d922006-09-30 23:29:01 -07002846 putname(to);
2847out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 putname(from);
2849 return error;
2850}
2851
Heiko Carstens3480b252009-01-14 14:14:16 +01002852SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002853{
2854 return sys_symlinkat(oldname, AT_FDCWD, newname);
2855}
2856
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2858{
2859 struct inode *inode = old_dentry->d_inode;
2860 int error;
2861
2862 if (!inode)
2863 return -ENOENT;
2864
Miklos Szeredia95164d2008-07-30 15:08:48 +02002865 error = may_create(dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 if (error)
2867 return error;
2868
2869 if (dir->i_sb != inode->i_sb)
2870 return -EXDEV;
2871
2872 /*
2873 * A link to an append-only or immutable file cannot be created.
2874 */
2875 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2876 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05002877 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002879 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 return -EPERM;
2881
2882 error = security_inode_link(old_dentry, dir, new_dentry);
2883 if (error)
2884 return error;
2885
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002886 mutex_lock(&inode->i_mutex);
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05302887 /* Make sure we don't allow creating hardlink to an unlinked file */
2888 if (inode->i_nlink == 0)
2889 error = -ENOENT;
2890 else
2891 error = dir->i_op->link(old_dentry, dir, new_dentry);
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002892 mutex_unlock(&inode->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07002893 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002894 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 return error;
2896}
2897
2898/*
2899 * Hardlinks are often used in delicate situations. We avoid
2900 * security-related surprises by not following symlinks on the
2901 * newname. --KAB
2902 *
2903 * We don't follow them on the oldname either to be compatible
2904 * with linux 2.0, and to avoid hard-linking to directories
2905 * and other special files. --ADM
2906 */
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002907SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
2908 int, newdfd, const char __user *, newname, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909{
2910 struct dentry *new_dentry;
Al Viro2d8f3032008-07-22 09:59:21 -04002911 struct nameidata nd;
2912 struct path old_path;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05302913 int how = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 int error;
Al Viro2ad94ae2008-07-21 09:32:51 -04002915 char *to;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05302917 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002918 return -EINVAL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05302919 /*
2920 * To use null names we require CAP_DAC_READ_SEARCH
2921 * This ensures that not everyone will be able to create
2922 * handlink using the passed filedescriptor.
2923 */
2924 if (flags & AT_EMPTY_PATH) {
2925 if (!capable(CAP_DAC_READ_SEARCH))
2926 return -ENOENT;
2927 how = LOOKUP_EMPTY;
2928 }
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002929
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05302930 if (flags & AT_SYMLINK_FOLLOW)
2931 how |= LOOKUP_FOLLOW;
2932
2933 error = user_path_at(olddfd, oldname, how, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002935 return error;
2936
2937 error = user_path_parent(newdfd, newname, &nd, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 if (error)
2939 goto out;
2940 error = -EXDEV;
Al Viro2d8f3032008-07-22 09:59:21 -04002941 if (old_path.mnt != nd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 goto out_release;
2943 new_dentry = lookup_create(&nd, 0);
2944 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002945 if (IS_ERR(new_dentry))
2946 goto out_unlock;
Dave Hansen75c3f292008-02-15 14:37:45 -08002947 error = mnt_want_write(nd.path.mnt);
2948 if (error)
2949 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002950 error = security_path_link(old_path.dentry, &nd.path, new_dentry);
2951 if (error)
2952 goto out_drop_write;
Al Viro2d8f3032008-07-22 09:59:21 -04002953 error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002954out_drop_write:
Dave Hansen75c3f292008-02-15 14:37:45 -08002955 mnt_drop_write(nd.path.mnt);
2956out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002957 dput(new_dentry);
2958out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002959 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960out_release:
Jan Blunck1d957f92008-02-14 19:34:35 -08002961 path_put(&nd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04002962 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963out:
Al Viro2d8f3032008-07-22 09:59:21 -04002964 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965
2966 return error;
2967}
2968
Heiko Carstens3480b252009-01-14 14:14:16 +01002969SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002970{
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002971 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002972}
2973
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974/*
2975 * The worst of all namespace operations - renaming directory. "Perverted"
2976 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2977 * Problems:
2978 * a) we can get into loop creation. Check is done in is_subdir().
2979 * b) race potential - two innocent renames can create a loop together.
2980 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002981 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 * story.
2983 * c) we have to lock _three_ objects - parents and victim (if it exists).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002984 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 * whether the target exists). Solution: try to be smart with locking
2986 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002987 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 * move will be locked. Thus we can rank directories by the tree
2989 * (ancestors first) and rank all non-directories after them.
2990 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002991 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 * HOWEVER, it relies on the assumption that any object with ->lookup()
2993 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2994 * we'd better make sure that there's no link(2) for them.
Sage Weile4eaac02011-05-24 13:06:07 -07002995 * d) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002996 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05002998 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 * locking].
3000 */
Adrian Bunk75c96f82005-05-05 16:16:09 -07003001static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
3002 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003{
3004 int error = 0;
Sage Weil9055cba2011-05-24 13:06:12 -07003005 struct inode *target = new_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
3007 /*
3008 * If we are going to change the parent - check write permissions,
3009 * we'll need to flip '..'.
3010 */
3011 if (new_dir != old_dir) {
Al Virof419a2e2008-07-22 00:07:17 -04003012 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 if (error)
3014 return error;
3015 }
3016
3017 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3018 if (error)
3019 return error;
3020
Al Viro97abc522011-09-14 18:55:41 +01003021 dget(new_dentry);
Al Virod83c49f2010-04-30 17:17:09 -04003022 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003023 mutex_lock(&target->i_mutex);
Sage Weil9055cba2011-05-24 13:06:12 -07003024
3025 error = -EBUSY;
3026 if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
3027 goto out;
3028
Sage Weil3cebde22011-05-29 21:20:59 -07003029 if (target)
3030 shrink_dcache_parent(new_dentry);
Sage Weil9055cba2011-05-24 13:06:12 -07003031 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3032 if (error)
3033 goto out;
3034
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 if (target) {
Sage Weil9055cba2011-05-24 13:06:12 -07003036 target->i_flags |= S_DEAD;
3037 dont_mount(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 }
Sage Weil9055cba2011-05-24 13:06:12 -07003039out:
3040 if (target)
3041 mutex_unlock(&target->i_mutex);
Al Viro97abc522011-09-14 18:55:41 +01003042 dput(new_dentry);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07003043 if (!error)
Mark Fasheh349457c2006-09-08 14:22:21 -07003044 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3045 d_move(old_dentry,new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 return error;
3047}
3048
Adrian Bunk75c96f82005-05-05 16:16:09 -07003049static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
3050 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051{
Sage Weil51892bb2011-05-24 13:06:13 -07003052 struct inode *target = new_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 int error;
3054
3055 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3056 if (error)
3057 return error;
3058
3059 dget(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003061 mutex_lock(&target->i_mutex);
Sage Weil51892bb2011-05-24 13:06:13 -07003062
3063 error = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
Sage Weil51892bb2011-05-24 13:06:13 -07003065 goto out;
3066
3067 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3068 if (error)
3069 goto out;
3070
3071 if (target)
3072 dont_mount(new_dentry);
3073 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3074 d_move(old_dentry, new_dentry);
3075out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003077 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 dput(new_dentry);
3079 return error;
3080}
3081
3082int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
3083 struct inode *new_dir, struct dentry *new_dentry)
3084{
3085 int error;
3086 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
Eric Paris59b0df22010-02-08 12:53:52 -05003087 const unsigned char *old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088
3089 if (old_dentry->d_inode == new_dentry->d_inode)
3090 return 0;
3091
3092 error = may_delete(old_dir, old_dentry, is_dir);
3093 if (error)
3094 return error;
3095
3096 if (!new_dentry->d_inode)
Miklos Szeredia95164d2008-07-30 15:08:48 +02003097 error = may_create(new_dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 else
3099 error = may_delete(new_dir, new_dentry, is_dir);
3100 if (error)
3101 return error;
3102
Al Viroacfa4382008-12-04 10:06:33 -05003103 if (!old_dir->i_op->rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 return -EPERM;
3105
Robert Love0eeca282005-07-12 17:06:03 -04003106 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
3107
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 if (is_dir)
3109 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
3110 else
3111 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
Al Viro123df292009-12-25 04:57:57 -05003112 if (!error)
3113 fsnotify_move(old_dir, new_dir, old_name, is_dir,
Al Viro5a190ae2007-06-07 12:19:32 -04003114 new_dentry->d_inode, old_dentry);
Robert Love0eeca282005-07-12 17:06:03 -04003115 fsnotify_oldname_free(old_name);
3116
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 return error;
3118}
3119
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003120SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
3121 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122{
Al Viro2ad94ae2008-07-21 09:32:51 -04003123 struct dentry *old_dir, *new_dir;
3124 struct dentry *old_dentry, *new_dentry;
3125 struct dentry *trap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 struct nameidata oldnd, newnd;
Al Viro2ad94ae2008-07-21 09:32:51 -04003127 char *from;
3128 char *to;
3129 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130
Al Viro2ad94ae2008-07-21 09:32:51 -04003131 error = user_path_parent(olddfd, oldname, &oldnd, &from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 if (error)
3133 goto exit;
3134
Al Viro2ad94ae2008-07-21 09:32:51 -04003135 error = user_path_parent(newdfd, newname, &newnd, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 if (error)
3137 goto exit1;
3138
3139 error = -EXDEV;
Jan Blunck4ac91372008-02-14 19:34:32 -08003140 if (oldnd.path.mnt != newnd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 goto exit2;
3142
Jan Blunck4ac91372008-02-14 19:34:32 -08003143 old_dir = oldnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 error = -EBUSY;
3145 if (oldnd.last_type != LAST_NORM)
3146 goto exit2;
3147
Jan Blunck4ac91372008-02-14 19:34:32 -08003148 new_dir = newnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 if (newnd.last_type != LAST_NORM)
3150 goto exit2;
3151
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003152 oldnd.flags &= ~LOOKUP_PARENT;
3153 newnd.flags &= ~LOOKUP_PARENT;
OGAWA Hirofumi4e9ed2f2008-10-16 07:50:29 +09003154 newnd.flags |= LOOKUP_RENAME_TARGET;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003155
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 trap = lock_rename(new_dir, old_dir);
3157
Christoph Hellwig49705b72005-11-08 21:35:06 -08003158 old_dentry = lookup_hash(&oldnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 error = PTR_ERR(old_dentry);
3160 if (IS_ERR(old_dentry))
3161 goto exit3;
3162 /* source must exist */
3163 error = -ENOENT;
3164 if (!old_dentry->d_inode)
3165 goto exit4;
3166 /* unless the source is a directory trailing slashes give -ENOTDIR */
3167 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
3168 error = -ENOTDIR;
3169 if (oldnd.last.name[oldnd.last.len])
3170 goto exit4;
3171 if (newnd.last.name[newnd.last.len])
3172 goto exit4;
3173 }
3174 /* source should not be ancestor of target */
3175 error = -EINVAL;
3176 if (old_dentry == trap)
3177 goto exit4;
Christoph Hellwig49705b72005-11-08 21:35:06 -08003178 new_dentry = lookup_hash(&newnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 error = PTR_ERR(new_dentry);
3180 if (IS_ERR(new_dentry))
3181 goto exit4;
3182 /* target should not be an ancestor of source */
3183 error = -ENOTEMPTY;
3184 if (new_dentry == trap)
3185 goto exit5;
3186
Dave Hansen9079b1e2008-02-15 14:37:49 -08003187 error = mnt_want_write(oldnd.path.mnt);
3188 if (error)
3189 goto exit5;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003190 error = security_path_rename(&oldnd.path, old_dentry,
3191 &newnd.path, new_dentry);
3192 if (error)
3193 goto exit6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 error = vfs_rename(old_dir->d_inode, old_dentry,
3195 new_dir->d_inode, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003196exit6:
Dave Hansen9079b1e2008-02-15 14:37:49 -08003197 mnt_drop_write(oldnd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198exit5:
3199 dput(new_dentry);
3200exit4:
3201 dput(old_dentry);
3202exit3:
3203 unlock_rename(new_dir, old_dir);
3204exit2:
Jan Blunck1d957f92008-02-14 19:34:35 -08003205 path_put(&newnd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04003206 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003208 path_put(&oldnd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 putname(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04003210exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 return error;
3212}
3213
Heiko Carstensa26eab22009-01-14 14:14:17 +01003214SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003215{
3216 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
3217}
3218
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
3220{
3221 int len;
3222
3223 len = PTR_ERR(link);
3224 if (IS_ERR(link))
3225 goto out;
3226
3227 len = strlen(link);
3228 if (len > (unsigned) buflen)
3229 len = buflen;
3230 if (copy_to_user(buffer, link, len))
3231 len = -EFAULT;
3232out:
3233 return len;
3234}
3235
3236/*
3237 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
3238 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
3239 * using) it for any given inode is up to filesystem.
3240 */
3241int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3242{
3243 struct nameidata nd;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003244 void *cookie;
Marcin Slusarz694a1762008-06-09 16:40:37 -07003245 int res;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003246
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 nd.depth = 0;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003248 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
Marcin Slusarz694a1762008-06-09 16:40:37 -07003249 if (IS_ERR(cookie))
3250 return PTR_ERR(cookie);
3251
3252 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
3253 if (dentry->d_inode->i_op->put_link)
3254 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3255 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256}
3257
3258int vfs_follow_link(struct nameidata *nd, const char *link)
3259{
3260 return __vfs_follow_link(nd, link);
3261}
3262
3263/* get the link contents into pagecache */
3264static char *page_getlink(struct dentry * dentry, struct page **ppage)
3265{
Duane Griffinebd09ab2008-12-19 20:47:12 +00003266 char *kaddr;
3267 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 struct address_space *mapping = dentry->d_inode->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -07003269 page = read_mapping_page(mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 if (IS_ERR(page))
Nick Piggin6fe69002007-05-06 14:49:04 -07003271 return (char*)page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 *ppage = page;
Duane Griffinebd09ab2008-12-19 20:47:12 +00003273 kaddr = kmap(page);
3274 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3275 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276}
3277
3278int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3279{
3280 struct page *page = NULL;
3281 char *s = page_getlink(dentry, &page);
3282 int res = vfs_readlink(dentry,buffer,buflen,s);
3283 if (page) {
3284 kunmap(page);
3285 page_cache_release(page);
3286 }
3287 return res;
3288}
3289
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003290void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003292 struct page *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 nd_set_link(nd, page_getlink(dentry, &page));
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003294 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295}
3296
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003297void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003299 struct page *page = cookie;
3300
3301 if (page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 kunmap(page);
3303 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 }
3305}
3306
Nick Piggin54566b22009-01-04 12:00:53 -08003307/*
3308 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
3309 */
3310int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311{
3312 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003313 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07003314 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08003315 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 char *kaddr;
Nick Piggin54566b22009-01-04 12:00:53 -08003317 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
3318 if (nofs)
3319 flags |= AOP_FLAG_NOFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320
NeilBrown7e53cac2006-03-25 03:07:57 -08003321retry:
Nick Pigginafddba42007-10-16 01:25:01 -07003322 err = pagecache_write_begin(NULL, mapping, 0, len-1,
Nick Piggin54566b22009-01-04 12:00:53 -08003323 flags, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07003325 goto fail;
3326
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 kaddr = kmap_atomic(page, KM_USER0);
3328 memcpy(kaddr, symname, len-1);
3329 kunmap_atomic(kaddr, KM_USER0);
Nick Pigginafddba42007-10-16 01:25:01 -07003330
3331 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3332 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 if (err < 0)
3334 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07003335 if (err < len-1)
3336 goto retry;
3337
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 mark_inode_dirty(inode);
3339 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340fail:
3341 return err;
3342}
3343
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003344int page_symlink(struct inode *inode, const char *symname, int len)
3345{
3346 return __page_symlink(inode, symname, len,
Nick Piggin54566b22009-01-04 12:00:53 -08003347 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003348}
3349
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003350const struct inode_operations page_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 .readlink = generic_readlink,
3352 .follow_link = page_follow_link_light,
3353 .put_link = page_put_link,
3354};
3355
Al Viro2d8f3032008-07-22 09:59:21 -04003356EXPORT_SYMBOL(user_path_at);
David Howellscc53ce52011-01-14 18:45:26 +00003357EXPORT_SYMBOL(follow_down_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358EXPORT_SYMBOL(follow_down);
3359EXPORT_SYMBOL(follow_up);
3360EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
3361EXPORT_SYMBOL(getname);
3362EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363EXPORT_SYMBOL(lookup_one_len);
3364EXPORT_SYMBOL(page_follow_link_light);
3365EXPORT_SYMBOL(page_put_link);
3366EXPORT_SYMBOL(page_readlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003367EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368EXPORT_SYMBOL(page_symlink);
3369EXPORT_SYMBOL(page_symlink_inode_operations);
Al Viroc9c6cac2011-02-16 15:15:47 -05003370EXPORT_SYMBOL(kern_path_parent);
Al Virod1811462008-08-02 00:49:18 -04003371EXPORT_SYMBOL(kern_path);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07003372EXPORT_SYMBOL(vfs_path_lookup);
Al Virof419a2e2008-07-22 00:07:17 -04003373EXPORT_SYMBOL(inode_permission);
Christoph Hellwig8c744fb2005-11-08 21:35:04 -08003374EXPORT_SYMBOL(file_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375EXPORT_SYMBOL(unlock_rename);
3376EXPORT_SYMBOL(vfs_create);
3377EXPORT_SYMBOL(vfs_follow_link);
3378EXPORT_SYMBOL(vfs_link);
3379EXPORT_SYMBOL(vfs_mkdir);
3380EXPORT_SYMBOL(vfs_mknod);
3381EXPORT_SYMBOL(generic_permission);
3382EXPORT_SYMBOL(vfs_readlink);
3383EXPORT_SYMBOL(vfs_rename);
3384EXPORT_SYMBOL(vfs_rmdir);
3385EXPORT_SYMBOL(vfs_symlink);
3386EXPORT_SYMBOL(vfs_unlink);
3387EXPORT_SYMBOL(dentry_unhash);
3388EXPORT_SYMBOL(generic_readlink);