blob: 0223c41fb1146cb529a92c784912498e15829a00 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/namei.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7/*
8 * Some corrections by tytso.
9 */
10
11/* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
12 * lookup logic.
13 */
14/* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
15 */
16
17#include <linux/init.h>
18#include <linux/module.h>
19#include <linux/slab.h>
20#include <linux/fs.h>
21#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/pagemap.h>
Robert Love0eeca282005-07-12 17:06:03 -040023#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/personality.h>
25#include <linux/security.h>
Mimi Zohar6146f0d2009-02-04 09:06:57 -050026#include <linux/ima.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/syscalls.h>
28#include <linux/mount.h>
29#include <linux/audit.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080030#include <linux/capability.h>
Trond Myklebust834f2a42005-10-18 14:20:16 -070031#include <linux/file.h>
Ulrich Drepper5590ff02006-01-18 17:43:53 -080032#include <linux/fcntl.h>
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070033#include <linux/device_cgroup.h>
Al Viro5ad4e532009-03-29 19:50:06 -040034#include <linux/fs_struct.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/uaccess.h>
36
Eric Parise81e3f42009-12-04 15:47:36 -050037#include "internal.h"
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039/* [Feb-1997 T. Schoebel-Theuer]
40 * Fundamental changes in the pathname lookup mechanisms (namei)
41 * were necessary because of omirr. The reason is that omirr needs
42 * to know the _real_ pathname, not the user-supplied one, in case
43 * of symlinks (and also when transname replacements occur).
44 *
45 * The new code replaces the old recursive symlink resolution with
46 * an iterative one (in case of non-nested symlink chains). It does
47 * this with calls to <fs>_follow_link().
48 * As a side effect, dir_namei(), _namei() and follow_link() are now
49 * replaced with a single function lookup_dentry() that can handle all
50 * the special cases of the former code.
51 *
52 * With the new dcache, the pathname is stored at each inode, at least as
53 * long as the refcount of the inode is positive. As a side effect, the
54 * size of the dcache depends on the inode cache and thus is dynamic.
55 *
56 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
57 * resolution to correspond with current state of the code.
58 *
59 * Note that the symlink resolution is not *completely* iterative.
60 * There is still a significant amount of tail- and mid- recursion in
61 * the algorithm. Also, note that <fs>_readlink() is not used in
62 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
63 * may return different results than <fs>_follow_link(). Many virtual
64 * filesystems (including /proc) exhibit this behavior.
65 */
66
67/* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
68 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
69 * and the name already exists in form of a symlink, try to create the new
70 * name indicated by the symlink. The old code always complained that the
71 * name already exists, due to not following the symlink even if its target
72 * is nonexistent. The new semantics affects also mknod() and link() when
Lucas De Marchi25985ed2011-03-30 22:57:33 -030073 * the name is a symlink pointing to a non-existent name.
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 *
75 * I don't know which semantics is the right one, since I have no access
76 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
77 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
78 * "old" one. Personally, I think the new semantics is much more logical.
79 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
80 * file does succeed in both HP-UX and SunOs, but not in Solaris
81 * and in the old Linux semantics.
82 */
83
84/* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
85 * semantics. See the comments in "open_namei" and "do_link" below.
86 *
87 * [10-Sep-98 Alan Modra] Another symlink change.
88 */
89
90/* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
91 * inside the path - always follow.
92 * in the last component in creation/removal/renaming - never follow.
93 * if LOOKUP_FOLLOW passed - follow.
94 * if the pathname has trailing slashes - follow.
95 * otherwise - don't follow.
96 * (applied in that order).
97 *
98 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
99 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
100 * During the 2.4 we need to fix the userland stuff depending on it -
101 * hopefully we will be able to get rid of that wart in 2.5. So far only
102 * XEmacs seems to be relying on it...
103 */
104/*
105 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
Arjan van de Vena11f3a02006-03-23 03:00:33 -0800106 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 * any extra contention...
108 */
109
110/* In order to reduce some races, while at the same time doing additional
111 * checking and hopefully speeding things up, we copy filenames to the
112 * kernel data space before using them..
113 *
114 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
115 * PATH_MAX includes the nul terminator --RR.
116 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800117static int do_getname(const char __user *filename, char *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
119 int retval;
120 unsigned long len = PATH_MAX;
121
122 if (!segment_eq(get_fs(), KERNEL_DS)) {
123 if ((unsigned long) filename >= TASK_SIZE)
124 return -EFAULT;
125 if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
126 len = TASK_SIZE - (unsigned long) filename;
127 }
128
129 retval = strncpy_from_user(page, filename, len);
130 if (retval > 0) {
131 if (retval < len)
132 return 0;
133 return -ENAMETOOLONG;
134 } else if (!retval)
135 retval = -ENOENT;
136 return retval;
137}
138
Al Virof52e0c12011-03-14 18:56:51 -0400139static char *getname_flags(const char __user * filename, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
141 char *tmp, *result;
142
143 result = ERR_PTR(-ENOMEM);
144 tmp = __getname();
145 if (tmp) {
146 int retval = do_getname(filename, tmp);
147
148 result = tmp;
149 if (retval < 0) {
Al Virof52e0c12011-03-14 18:56:51 -0400150 if (retval != -ENOENT || !(flags & LOOKUP_EMPTY)) {
151 __putname(tmp);
152 result = ERR_PTR(retval);
153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 }
155 }
156 audit_getname(result);
157 return result;
158}
159
Al Virof52e0c12011-03-14 18:56:51 -0400160char *getname(const char __user * filename)
161{
162 return getname_flags(filename, 0);
163}
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165#ifdef CONFIG_AUDITSYSCALL
166void putname(const char *name)
167{
Al Viro5ac3a9c2006-07-16 06:38:45 -0400168 if (unlikely(!audit_dummy_context()))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 audit_putname(name);
170 else
171 __putname(name);
172}
173EXPORT_SYMBOL(putname);
174#endif
175
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700176/*
177 * This does basic POSIX ACL permission checking
178 */
Nick Pigginb74c79e2011-01-07 17:49:58 +1100179static int acl_permission_check(struct inode *inode, int mask, unsigned int flags,
180 int (*check_acl)(struct inode *inode, int mask, unsigned int flags))
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700181{
Linus Torvalds26cf46b2011-05-13 11:51:01 -0700182 unsigned int mode = inode->i_mode;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700183
184 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
185
Serge E. Hallyne795b712011-03-23 16:43:25 -0700186 if (current_user_ns() != inode_userns(inode))
187 goto other_perms;
188
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700189 if (current_fsuid() == inode->i_uid)
190 mode >>= 6;
191 else {
192 if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
Nick Pigginb74c79e2011-01-07 17:49:58 +1100193 int error = check_acl(inode, mask, flags);
194 if (error != -EAGAIN)
195 return error;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700196 }
197
198 if (in_group_p(inode->i_gid))
199 mode >>= 3;
200 }
201
Serge E. Hallyne795b712011-03-23 16:43:25 -0700202other_perms:
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700203 /*
204 * If the DACs are ok we don't need any capability check.
205 */
206 if ((mask & ~mode) == 0)
207 return 0;
208 return -EACCES;
209}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211/**
Nick Pigginb74c79e2011-01-07 17:49:58 +1100212 * generic_permission - check for access rights on a Posix-like filesystem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 * @inode: inode to check access rights for
214 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
215 * @check_acl: optional callback to check for Posix ACLs
Randy Dunlap39191622011-01-08 19:36:21 -0800216 * @flags: IPERM_FLAG_ flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 *
218 * Used to check for read/write/execute permissions on a file.
219 * We use "fsuid" for this, letting us set arbitrary permissions
220 * for filesystem access without changing the "normal" uids which
Nick Pigginb74c79e2011-01-07 17:49:58 +1100221 * are used for other things.
222 *
223 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
224 * request cannot be satisfied (eg. requires blocking or too much complexity).
225 * It would then be called again in ref-walk mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 */
Nick Pigginb74c79e2011-01-07 17:49:58 +1100227int generic_permission(struct inode *inode, int mask, unsigned int flags,
228 int (*check_acl)(struct inode *inode, int mask, unsigned int flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700230 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 /*
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700233 * Do the basic POSIX ACL permission checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 */
Nick Pigginb74c79e2011-01-07 17:49:58 +1100235 ret = acl_permission_check(inode, mask, flags, check_acl);
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700236 if (ret != -EACCES)
237 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 /*
240 * Read/write DACs are always overridable.
Al Viro8e833fd2011-06-19 01:56:53 -0400241 * Executable DACs are overridable for all directories and
242 * for non-directories that have least one exec bit set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 */
Miklos Szeredif696a362008-07-31 13:41:58 +0200244 if (!(mask & MAY_EXEC) || execute_ok(inode))
Serge E. Hallyne795b712011-03-23 16:43:25 -0700245 if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return 0;
247
248 /*
249 * Searching includes executable on directories, else just read.
250 */
Serge E. Hallyn7ea66002009-12-29 14:50:19 -0600251 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
Serge E. Hallyne795b712011-03-23 16:43:25 -0700253 if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return 0;
255
256 return -EACCES;
257}
258
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200259/**
260 * inode_permission - check for access rights to a given inode
261 * @inode: inode to check permission on
262 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
263 *
264 * Used to check for read/write/execute permissions on an inode.
265 * We use "fsuid" for this, letting us set arbitrary permissions
266 * for filesystem access without changing the "normal" uids which
267 * are used for other things.
268 */
Al Virof419a2e2008-07-22 00:07:17 -0400269int inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Al Viroe6305c42008-07-15 21:03:57 -0400271 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 if (mask & MAY_WRITE) {
Miklos Szeredi22590e42007-10-16 23:27:08 -0700274 umode_t mode = inode->i_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 /*
277 * Nobody gets write access to a read-only fs.
278 */
279 if (IS_RDONLY(inode) &&
280 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
281 return -EROFS;
282
283 /*
284 * Nobody gets write access to an immutable file.
285 */
286 if (IS_IMMUTABLE(inode))
287 return -EACCES;
288 }
289
Al Viroacfa4382008-12-04 10:06:33 -0500290 if (inode->i_op->permission)
Nick Pigginb74c79e2011-01-07 17:49:58 +1100291 retval = inode->i_op->permission(inode, mask, 0);
Miklos Szeredif696a362008-07-31 13:41:58 +0200292 else
Nick Pigginb74c79e2011-01-07 17:49:58 +1100293 retval = generic_permission(inode, mask, 0,
294 inode->i_op->check_acl);
Miklos Szeredif696a362008-07-31 13:41:58 +0200295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 if (retval)
297 return retval;
298
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700299 retval = devcgroup_inode_permission(inode, mask);
300 if (retval)
301 return retval;
302
Eric Parisd09ca732010-07-23 11:43:57 -0400303 return security_inode_permission(inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800306/**
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800307 * file_permission - check for additional access rights to a given file
308 * @file: file to check access rights for
309 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
310 *
311 * Used to check for read/write/execute permissions on an already opened
312 * file.
313 *
314 * Note:
315 * Do not use this function in new code. All access checks should
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200316 * be done using inode_permission().
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800317 */
318int file_permission(struct file *file, int mask)
319{
Al Virof419a2e2008-07-22 00:07:17 -0400320 return inode_permission(file->f_path.dentry->d_inode, mask);
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800321}
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323/*
324 * get_write_access() gets write permission for a file.
325 * put_write_access() releases this write permission.
326 * This is used for regular files.
327 * We cannot support write (and maybe mmap read-write shared) accesses and
328 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
329 * can have the following values:
330 * 0: no writers, no VM_DENYWRITE mappings
331 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
332 * > 0: (i_writecount) users are writing to the file.
333 *
334 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
335 * except for the cases where we don't hold i_writecount yet. Then we need to
336 * use {get,deny}_write_access() - these functions check the sign and refuse
337 * to do the change if sign is wrong. Exclusion between them is provided by
338 * the inode->i_lock spinlock.
339 */
340
341int get_write_access(struct inode * inode)
342{
343 spin_lock(&inode->i_lock);
344 if (atomic_read(&inode->i_writecount) < 0) {
345 spin_unlock(&inode->i_lock);
346 return -ETXTBSY;
347 }
348 atomic_inc(&inode->i_writecount);
349 spin_unlock(&inode->i_lock);
350
351 return 0;
352}
353
354int deny_write_access(struct file * file)
355{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800356 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 spin_lock(&inode->i_lock);
359 if (atomic_read(&inode->i_writecount) > 0) {
360 spin_unlock(&inode->i_lock);
361 return -ETXTBSY;
362 }
363 atomic_dec(&inode->i_writecount);
364 spin_unlock(&inode->i_lock);
365
366 return 0;
367}
368
Jan Blunck1d957f92008-02-14 19:34:35 -0800369/**
Jan Blunck5dd784d2008-02-14 19:34:38 -0800370 * path_get - get a reference to a path
371 * @path: path to get the reference to
372 *
373 * Given a path increment the reference count to the dentry and the vfsmount.
374 */
375void path_get(struct path *path)
376{
377 mntget(path->mnt);
378 dget(path->dentry);
379}
380EXPORT_SYMBOL(path_get);
381
382/**
Jan Blunck1d957f92008-02-14 19:34:35 -0800383 * path_put - put a reference to a path
384 * @path: path to put the reference to
385 *
386 * Given a path decrement the reference count to the dentry and the vfsmount.
387 */
388void path_put(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
Jan Blunck1d957f92008-02-14 19:34:35 -0800390 dput(path->dentry);
391 mntput(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
Jan Blunck1d957f92008-02-14 19:34:35 -0800393EXPORT_SYMBOL(path_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Al Viro19660af2011-03-25 10:32:48 -0400395/*
Nick Piggin31e6b012011-01-07 17:49:52 +1100396 * Path walking has 2 modes, rcu-walk and ref-walk (see
Al Viro19660af2011-03-25 10:32:48 -0400397 * Documentation/filesystems/path-lookup.txt). In situations when we can't
398 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
399 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
400 * mode. Refcounts are grabbed at the last known good point before rcu-walk
401 * got stuck, so ref-walk may continue from there. If this is not successful
402 * (eg. a seqcount has changed), then failure is returned and it's up to caller
403 * to restart the path walk from the beginning in ref-walk mode.
Nick Piggin31e6b012011-01-07 17:49:52 +1100404 */
Nick Piggin31e6b012011-01-07 17:49:52 +1100405
406/**
Al Viro19660af2011-03-25 10:32:48 -0400407 * unlazy_walk - try to switch to ref-walk mode.
408 * @nd: nameidata pathwalk data
409 * @dentry: child of nd->path.dentry or NULL
Randy Dunlap39191622011-01-08 19:36:21 -0800410 * Returns: 0 on success, -ECHILD on failure
Nick Piggin31e6b012011-01-07 17:49:52 +1100411 *
Al Viro19660af2011-03-25 10:32:48 -0400412 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
413 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
414 * @nd or NULL. Must be called from rcu-walk context.
Nick Piggin31e6b012011-01-07 17:49:52 +1100415 */
Al Viro19660af2011-03-25 10:32:48 -0400416static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
Nick Piggin31e6b012011-01-07 17:49:52 +1100417{
418 struct fs_struct *fs = current->fs;
419 struct dentry *parent = nd->path.dentry;
Al Viro5b6ca022011-03-09 23:04:47 -0500420 int want_root = 0;
Nick Piggin31e6b012011-01-07 17:49:52 +1100421
422 BUG_ON(!(nd->flags & LOOKUP_RCU));
Al Viro5b6ca022011-03-09 23:04:47 -0500423 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
424 want_root = 1;
Nick Piggin31e6b012011-01-07 17:49:52 +1100425 spin_lock(&fs->lock);
426 if (nd->root.mnt != fs->root.mnt ||
427 nd->root.dentry != fs->root.dentry)
428 goto err_root;
429 }
430 spin_lock(&parent->d_lock);
Al Viro19660af2011-03-25 10:32:48 -0400431 if (!dentry) {
432 if (!__d_rcu_to_refcount(parent, nd->seq))
433 goto err_parent;
434 BUG_ON(nd->inode != parent->d_inode);
435 } else {
436 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
437 if (!__d_rcu_to_refcount(dentry, nd->seq))
438 goto err_child;
439 /*
440 * If the sequence check on the child dentry passed, then
441 * the child has not been removed from its parent. This
442 * means the parent dentry must be valid and able to take
443 * a reference at this point.
444 */
445 BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
446 BUG_ON(!parent->d_count);
447 parent->d_count++;
448 spin_unlock(&dentry->d_lock);
449 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100450 spin_unlock(&parent->d_lock);
Al Viro5b6ca022011-03-09 23:04:47 -0500451 if (want_root) {
Nick Piggin31e6b012011-01-07 17:49:52 +1100452 path_get(&nd->root);
453 spin_unlock(&fs->lock);
454 }
455 mntget(nd->path.mnt);
456
457 rcu_read_unlock();
458 br_read_unlock(vfsmount_lock);
459 nd->flags &= ~LOOKUP_RCU;
460 return 0;
Al Viro19660af2011-03-25 10:32:48 -0400461
462err_child:
Nick Piggin31e6b012011-01-07 17:49:52 +1100463 spin_unlock(&dentry->d_lock);
Al Viro19660af2011-03-25 10:32:48 -0400464err_parent:
Nick Piggin31e6b012011-01-07 17:49:52 +1100465 spin_unlock(&parent->d_lock);
466err_root:
Al Viro5b6ca022011-03-09 23:04:47 -0500467 if (want_root)
Nick Piggin31e6b012011-01-07 17:49:52 +1100468 spin_unlock(&fs->lock);
469 return -ECHILD;
470}
471
Nick Piggin31e6b012011-01-07 17:49:52 +1100472/**
Trond Myklebust834f2a42005-10-18 14:20:16 -0700473 * release_open_intent - free up open intent resources
474 * @nd: pointer to nameidata
475 */
476void release_open_intent(struct nameidata *nd)
477{
Linus Torvalds2dab5972011-02-11 15:53:38 -0800478 struct file *file = nd->intent.open.file;
479
480 if (file && !IS_ERR(file)) {
481 if (file->f_path.dentry == NULL)
482 put_filp(file);
483 else
484 fput(file);
485 }
Trond Myklebust834f2a42005-10-18 14:20:16 -0700486}
487
Al Virof60aef72011-02-15 01:35:28 -0500488static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
Nick Piggin34286d62011-01-07 17:49:57 +1100489{
Al Virof60aef72011-02-15 01:35:28 -0500490 return dentry->d_op->d_revalidate(dentry, nd);
Nick Piggin34286d62011-01-07 17:49:57 +1100491}
492
Al Virof5e1c1c2011-02-15 01:32:55 -0500493static struct dentry *
Ian Kentbcdc5e02006-09-27 01:50:44 -0700494do_revalidate(struct dentry *dentry, struct nameidata *nd)
495{
Al Virof5e1c1c2011-02-15 01:32:55 -0500496 int status = d_revalidate(dentry, nd);
Ian Kentbcdc5e02006-09-27 01:50:44 -0700497 if (unlikely(status <= 0)) {
498 /*
499 * The dentry failed validation.
500 * If d_revalidate returned 0 attempt to invalidate
501 * the dentry otherwise d_revalidate is asking us
502 * to return a fail status.
503 */
Nick Piggin34286d62011-01-07 17:49:57 +1100504 if (status < 0) {
Al Virof5e1c1c2011-02-15 01:32:55 -0500505 dput(dentry);
Nick Piggin34286d62011-01-07 17:49:57 +1100506 dentry = ERR_PTR(status);
Al Virof5e1c1c2011-02-15 01:32:55 -0500507 } else if (!d_invalidate(dentry)) {
508 dput(dentry);
509 dentry = NULL;
Ian Kentbcdc5e02006-09-27 01:50:44 -0700510 }
511 }
512 return dentry;
513}
514
Al Viro9f1fafe2011-03-25 11:00:12 -0400515/**
516 * complete_walk - successful completion of path walk
517 * @nd: pointer nameidata
Jeff Layton39159de2009-12-07 12:01:50 -0500518 *
Al Viro9f1fafe2011-03-25 11:00:12 -0400519 * If we had been in RCU mode, drop out of it and legitimize nd->path.
520 * Revalidate the final result, unless we'd already done that during
521 * the path walk or the filesystem doesn't ask for it. Return 0 on
522 * success, -error on failure. In case of failure caller does not
523 * need to drop nd->path.
Jeff Layton39159de2009-12-07 12:01:50 -0500524 */
Al Viro9f1fafe2011-03-25 11:00:12 -0400525static int complete_walk(struct nameidata *nd)
Jeff Layton39159de2009-12-07 12:01:50 -0500526{
Al Viro16c2cd72011-02-22 15:50:10 -0500527 struct dentry *dentry = nd->path.dentry;
Jeff Layton39159de2009-12-07 12:01:50 -0500528 int status;
Jeff Layton39159de2009-12-07 12:01:50 -0500529
Al Viro9f1fafe2011-03-25 11:00:12 -0400530 if (nd->flags & LOOKUP_RCU) {
531 nd->flags &= ~LOOKUP_RCU;
532 if (!(nd->flags & LOOKUP_ROOT))
533 nd->root.mnt = NULL;
534 spin_lock(&dentry->d_lock);
535 if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
536 spin_unlock(&dentry->d_lock);
537 rcu_read_unlock();
538 br_read_unlock(vfsmount_lock);
539 return -ECHILD;
540 }
541 BUG_ON(nd->inode != dentry->d_inode);
542 spin_unlock(&dentry->d_lock);
543 mntget(nd->path.mnt);
544 rcu_read_unlock();
545 br_read_unlock(vfsmount_lock);
546 }
547
Al Viro16c2cd72011-02-22 15:50:10 -0500548 if (likely(!(nd->flags & LOOKUP_JUMPED)))
Jeff Layton39159de2009-12-07 12:01:50 -0500549 return 0;
550
Al Viro16c2cd72011-02-22 15:50:10 -0500551 if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
552 return 0;
553
554 if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
555 return 0;
556
557 /* Note: we do not d_invalidate() */
Nick Piggin34286d62011-01-07 17:49:57 +1100558 status = d_revalidate(dentry, nd);
Jeff Layton39159de2009-12-07 12:01:50 -0500559 if (status > 0)
560 return 0;
561
Al Viro16c2cd72011-02-22 15:50:10 -0500562 if (!status)
Jeff Layton39159de2009-12-07 12:01:50 -0500563 status = -ESTALE;
Al Viro16c2cd72011-02-22 15:50:10 -0500564
Al Viro9f1fafe2011-03-25 11:00:12 -0400565 path_put(&nd->path);
Jeff Layton39159de2009-12-07 12:01:50 -0500566 return status;
567}
568
569/*
Al Virob75b5082009-12-16 01:01:38 -0500570 * Short-cut version of permission(), for calling on directories
571 * during pathname resolution. Combines parts of permission()
572 * and generic_permission(), and tests ONLY for MAY_EXEC permission.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 *
574 * If appropriate, check DAC only. If not appropriate, or
Al Virob75b5082009-12-16 01:01:38 -0500575 * short-cut DAC fails, then call ->permission() to do more
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 * complete permission check.
577 */
Nick Pigginb74c79e2011-01-07 17:49:58 +1100578static inline int exec_permission(struct inode *inode, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700580 int ret;
Serge E. Hallyne795b712011-03-23 16:43:25 -0700581 struct user_namespace *ns = inode_userns(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Linus Torvaldscb9179e2009-08-28 11:08:31 -0700583 if (inode->i_op->permission) {
Nick Pigginb74c79e2011-01-07 17:49:58 +1100584 ret = inode->i_op->permission(inode, MAY_EXEC, flags);
585 } else {
586 ret = acl_permission_check(inode, MAY_EXEC, flags,
587 inode->i_op->check_acl);
Linus Torvaldscb9179e2009-08-28 11:08:31 -0700588 }
Nick Pigginb74c79e2011-01-07 17:49:58 +1100589 if (likely(!ret))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 goto ok;
Nick Pigginb74c79e2011-01-07 17:49:58 +1100591 if (ret == -ECHILD)
Nick Piggin31e6b012011-01-07 17:49:52 +1100592 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Serge E. Hallyne795b712011-03-23 16:43:25 -0700594 if (ns_capable(ns, CAP_DAC_OVERRIDE) ||
595 ns_capable(ns, CAP_DAC_READ_SEARCH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 goto ok;
597
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700598 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599ok:
Nick Pigginb74c79e2011-01-07 17:49:58 +1100600 return security_inode_exec_permission(inode, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
Al Viro2a737872009-04-07 11:49:53 -0400603static __always_inline void set_root(struct nameidata *nd)
604{
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200605 if (!nd->root.mnt)
606 get_fs_root(current->fs, &nd->root);
Al Viro2a737872009-04-07 11:49:53 -0400607}
608
Al Viro6de88d72009-08-09 01:41:57 +0400609static int link_path_walk(const char *, struct nameidata *);
610
Nick Piggin31e6b012011-01-07 17:49:52 +1100611static __always_inline void set_root_rcu(struct nameidata *nd)
612{
613 if (!nd->root.mnt) {
614 struct fs_struct *fs = current->fs;
Nick Pigginc28cc362011-01-07 17:49:53 +1100615 unsigned seq;
616
617 do {
618 seq = read_seqcount_begin(&fs->seq);
619 nd->root = fs->root;
Tim Chenc1530012011-04-15 11:39:29 -0700620 nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
Nick Pigginc28cc362011-01-07 17:49:53 +1100621 } while (read_seqcount_retry(&fs->seq, seq));
Nick Piggin31e6b012011-01-07 17:49:52 +1100622 }
623}
624
Arjan van de Venf1662352006-01-14 13:21:31 -0800625static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
Nick Piggin31e6b012011-01-07 17:49:52 +1100627 int ret;
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 if (IS_ERR(link))
630 goto fail;
631
632 if (*link == '/') {
Al Viro2a737872009-04-07 11:49:53 -0400633 set_root(nd);
Jan Blunck1d957f92008-02-14 19:34:35 -0800634 path_put(&nd->path);
Al Viro2a737872009-04-07 11:49:53 -0400635 nd->path = nd->root;
636 path_get(&nd->root);
Al Viro16c2cd72011-02-22 15:50:10 -0500637 nd->flags |= LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100639 nd->inode = nd->path.dentry->d_inode;
Christoph Hellwigb4091d52008-11-05 15:07:21 +0100640
Nick Piggin31e6b012011-01-07 17:49:52 +1100641 ret = link_path_walk(link, nd);
642 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643fail:
Jan Blunck1d957f92008-02-14 19:34:35 -0800644 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return PTR_ERR(link);
646}
647
Jan Blunck1d957f92008-02-14 19:34:35 -0800648static void path_put_conditional(struct path *path, struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700649{
650 dput(path->dentry);
Jan Blunck4ac91372008-02-14 19:34:32 -0800651 if (path->mnt != nd->path.mnt)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700652 mntput(path->mnt);
653}
654
Nick Piggin7b9337a2011-01-14 08:42:43 +0000655static inline void path_to_nameidata(const struct path *path,
656 struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700657{
Nick Piggin31e6b012011-01-07 17:49:52 +1100658 if (!(nd->flags & LOOKUP_RCU)) {
659 dput(nd->path.dentry);
660 if (nd->path.mnt != path->mnt)
661 mntput(nd->path.mnt);
Huang Shijie9a229682010-04-02 17:37:13 +0800662 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100663 nd->path.mnt = path->mnt;
Jan Blunck4ac91372008-02-14 19:34:32 -0800664 nd->path.dentry = path->dentry;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700665}
666
Al Viro574197e2011-03-14 22:20:34 -0400667static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
668{
669 struct inode *inode = link->dentry->d_inode;
670 if (!IS_ERR(cookie) && inode->i_op->put_link)
671 inode->i_op->put_link(link->dentry, nd, cookie);
672 path_put(link);
673}
674
Al Virodef4af32009-12-26 08:37:05 -0500675static __always_inline int
Al Viro574197e2011-03-14 22:20:34 -0400676follow_link(struct path *link, struct nameidata *nd, void **p)
Ian Kent051d3812006-03-27 01:14:53 -0800677{
678 int error;
Nick Piggin7b9337a2011-01-14 08:42:43 +0000679 struct dentry *dentry = link->dentry;
Ian Kent051d3812006-03-27 01:14:53 -0800680
Al Viro844a3912011-02-15 00:38:26 -0500681 BUG_ON(nd->flags & LOOKUP_RCU);
682
Al Viro0e794582011-03-16 02:45:02 -0400683 if (link->mnt == nd->path.mnt)
684 mntget(link->mnt);
685
Al Viro574197e2011-03-14 22:20:34 -0400686 if (unlikely(current->total_link_count >= 40)) {
687 *p = ERR_PTR(-ELOOP); /* no ->put_link(), please */
Al Viro574197e2011-03-14 22:20:34 -0400688 path_put(&nd->path);
689 return -ELOOP;
690 }
691 cond_resched();
692 current->total_link_count++;
693
Nick Piggin7b9337a2011-01-14 08:42:43 +0000694 touch_atime(link->mnt, dentry);
Ian Kent051d3812006-03-27 01:14:53 -0800695 nd_set_link(nd, NULL);
696
Al Viro36f3b4f2011-02-22 21:24:38 -0500697 error = security_inode_follow_link(link->dentry, nd);
698 if (error) {
699 *p = ERR_PTR(error); /* no ->put_link(), please */
700 path_put(&nd->path);
701 return error;
702 }
703
Al Viro86acdca12009-12-22 23:45:11 -0500704 nd->last_type = LAST_BIND;
Al Virodef4af32009-12-26 08:37:05 -0500705 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
706 error = PTR_ERR(*p);
707 if (!IS_ERR(*p)) {
Ian Kent051d3812006-03-27 01:14:53 -0800708 char *s = nd_get_link(nd);
709 error = 0;
710 if (s)
711 error = __vfs_follow_link(nd, s);
Al Virobcda7652011-03-13 16:42:14 -0400712 else if (nd->last_type == LAST_BIND) {
Al Viro16c2cd72011-02-22 15:50:10 -0500713 nd->flags |= LOOKUP_JUMPED;
Al Virob21041d2011-03-14 20:01:51 -0400714 nd->inode = nd->path.dentry->d_inode;
715 if (nd->inode->i_op->follow_link) {
Al Virobcda7652011-03-13 16:42:14 -0400716 /* stepped on a _really_ weird one */
717 path_put(&nd->path);
718 error = -ELOOP;
719 }
720 }
Ian Kent051d3812006-03-27 01:14:53 -0800721 }
Ian Kent051d3812006-03-27 01:14:53 -0800722 return error;
723}
724
Nick Piggin31e6b012011-01-07 17:49:52 +1100725static int follow_up_rcu(struct path *path)
726{
727 struct vfsmount *parent;
728 struct dentry *mountpoint;
729
730 parent = path->mnt->mnt_parent;
731 if (parent == path->mnt)
732 return 0;
733 mountpoint = path->mnt->mnt_mountpoint;
734 path->dentry = mountpoint;
735 path->mnt = parent;
736 return 1;
737}
738
Al Virobab77eb2009-04-18 03:26:48 -0400739int follow_up(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
741 struct vfsmount *parent;
742 struct dentry *mountpoint;
Nick Piggin99b7db72010-08-18 04:37:39 +1000743
744 br_read_lock(vfsmount_lock);
Al Virobab77eb2009-04-18 03:26:48 -0400745 parent = path->mnt->mnt_parent;
746 if (parent == path->mnt) {
Nick Piggin99b7db72010-08-18 04:37:39 +1000747 br_read_unlock(vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 return 0;
749 }
750 mntget(parent);
Al Virobab77eb2009-04-18 03:26:48 -0400751 mountpoint = dget(path->mnt->mnt_mountpoint);
Nick Piggin99b7db72010-08-18 04:37:39 +1000752 br_read_unlock(vfsmount_lock);
Al Virobab77eb2009-04-18 03:26:48 -0400753 dput(path->dentry);
754 path->dentry = mountpoint;
755 mntput(path->mnt);
756 path->mnt = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 return 1;
758}
759
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100760/*
David Howells9875cf82011-01-14 18:45:21 +0000761 * Perform an automount
762 * - return -EISDIR to tell follow_managed() to stop and return the path we
763 * were called with.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 */
David Howells9875cf82011-01-14 18:45:21 +0000765static int follow_automount(struct path *path, unsigned flags,
766 bool *need_mntput)
Nick Piggin31e6b012011-01-07 17:49:52 +1100767{
David Howells9875cf82011-01-14 18:45:21 +0000768 struct vfsmount *mnt;
David Howellsea5b7782011-01-14 19:10:03 +0000769 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +1100770
David Howells9875cf82011-01-14 18:45:21 +0000771 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
772 return -EREMOTE;
Al Viro463ffb22005-06-06 13:36:05 -0700773
David Howells6f45b652011-01-14 18:45:31 +0000774 /* We don't want to mount if someone supplied AT_NO_AUTOMOUNT
775 * and this is the terminal part of the path.
776 */
777 if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_CONTINUE))
778 return -EISDIR; /* we actually want to stop here */
779
David Howells9875cf82011-01-14 18:45:21 +0000780 /* We want to mount if someone is trying to open/create a file of any
781 * type under the mountpoint, wants to traverse through the mountpoint
782 * or wants to open the mounted directory.
783 *
784 * We don't want to mount if someone's just doing a stat and they've
785 * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and
786 * appended a '/' to the name.
787 */
788 if (!(flags & LOOKUP_FOLLOW) &&
789 !(flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY |
790 LOOKUP_OPEN | LOOKUP_CREATE)))
791 return -EISDIR;
792
793 current->total_link_count++;
794 if (current->total_link_count >= 40)
795 return -ELOOP;
796
797 mnt = path->dentry->d_op->d_automount(path);
798 if (IS_ERR(mnt)) {
799 /*
800 * The filesystem is allowed to return -EISDIR here to indicate
801 * it doesn't want to automount. For instance, autofs would do
802 * this so that its userspace daemon can mount on this dentry.
803 *
804 * However, we can only permit this if it's a terminal point in
805 * the path being looked up; if it wasn't then the remainder of
806 * the path is inaccessible and we should say so.
807 */
808 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_CONTINUE))
809 return -EREMOTE;
810 return PTR_ERR(mnt);
811 }
David Howellsea5b7782011-01-14 19:10:03 +0000812
David Howells9875cf82011-01-14 18:45:21 +0000813 if (!mnt) /* mount collision */
814 return 0;
815
Al Viro8aef1882011-06-16 15:10:06 +0100816 if (!*need_mntput) {
817 /* lock_mount() may release path->mnt on error */
818 mntget(path->mnt);
819 *need_mntput = true;
820 }
Al Viro19a167a2011-01-17 01:35:23 -0500821 err = finish_automount(mnt, path);
David Howellsea5b7782011-01-14 19:10:03 +0000822
David Howellsea5b7782011-01-14 19:10:03 +0000823 switch (err) {
824 case -EBUSY:
825 /* Someone else made a mount here whilst we were busy */
Al Viro19a167a2011-01-17 01:35:23 -0500826 return 0;
David Howellsea5b7782011-01-14 19:10:03 +0000827 case 0:
Al Viro8aef1882011-06-16 15:10:06 +0100828 path_put(path);
David Howellsea5b7782011-01-14 19:10:03 +0000829 path->mnt = mnt;
830 path->dentry = dget(mnt->mnt_root);
David Howellsea5b7782011-01-14 19:10:03 +0000831 return 0;
Al Viro19a167a2011-01-17 01:35:23 -0500832 default:
833 return err;
David Howellsea5b7782011-01-14 19:10:03 +0000834 }
Al Viro19a167a2011-01-17 01:35:23 -0500835
David Howells9875cf82011-01-14 18:45:21 +0000836}
837
838/*
839 * Handle a dentry that is managed in some way.
David Howellscc53ce52011-01-14 18:45:26 +0000840 * - Flagged for transit management (autofs)
David Howells9875cf82011-01-14 18:45:21 +0000841 * - Flagged as mountpoint
842 * - Flagged as automount point
843 *
844 * This may only be called in refwalk mode.
845 *
846 * Serialization is taken care of in namespace.c
847 */
848static int follow_managed(struct path *path, unsigned flags)
849{
Al Viro8aef1882011-06-16 15:10:06 +0100850 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
David Howells9875cf82011-01-14 18:45:21 +0000851 unsigned managed;
852 bool need_mntput = false;
Al Viro8aef1882011-06-16 15:10:06 +0100853 int ret = 0;
David Howells9875cf82011-01-14 18:45:21 +0000854
855 /* Given that we're not holding a lock here, we retain the value in a
856 * local variable for each dentry as we look at it so that we don't see
857 * the components of that value change under us */
858 while (managed = ACCESS_ONCE(path->dentry->d_flags),
859 managed &= DCACHE_MANAGED_DENTRY,
860 unlikely(managed != 0)) {
David Howellscc53ce52011-01-14 18:45:26 +0000861 /* Allow the filesystem to manage the transit without i_mutex
862 * being held. */
863 if (managed & DCACHE_MANAGE_TRANSIT) {
864 BUG_ON(!path->dentry->d_op);
865 BUG_ON(!path->dentry->d_op->d_manage);
Al Viro1aed3e42011-03-18 09:09:02 -0400866 ret = path->dentry->d_op->d_manage(path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +0000867 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +0100868 break;
David Howellscc53ce52011-01-14 18:45:26 +0000869 }
870
David Howells9875cf82011-01-14 18:45:21 +0000871 /* Transit to a mounted filesystem. */
872 if (managed & DCACHE_MOUNTED) {
873 struct vfsmount *mounted = lookup_mnt(path);
874 if (mounted) {
875 dput(path->dentry);
876 if (need_mntput)
877 mntput(path->mnt);
878 path->mnt = mounted;
879 path->dentry = dget(mounted->mnt_root);
880 need_mntput = true;
881 continue;
882 }
883
884 /* Something is mounted on this dentry in another
885 * namespace and/or whatever was mounted there in this
886 * namespace got unmounted before we managed to get the
887 * vfsmount_lock */
888 }
889
890 /* Handle an automount point */
891 if (managed & DCACHE_NEED_AUTOMOUNT) {
892 ret = follow_automount(path, flags, &need_mntput);
893 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +0100894 break;
David Howells9875cf82011-01-14 18:45:21 +0000895 continue;
896 }
897
898 /* We didn't change the current path point */
899 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 }
Al Viro8aef1882011-06-16 15:10:06 +0100901
902 if (need_mntput && path->mnt == mnt)
903 mntput(path->mnt);
904 if (ret == -EISDIR)
905 ret = 0;
906 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}
908
David Howellscc53ce52011-01-14 18:45:26 +0000909int follow_down_one(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
911 struct vfsmount *mounted;
912
Al Viro1c755af2009-04-18 14:06:57 -0400913 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -0400915 dput(path->dentry);
916 mntput(path->mnt);
917 path->mnt = mounted;
918 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 return 1;
920 }
921 return 0;
922}
923
Ian Kent62a73752011-03-25 01:51:02 +0800924static inline bool managed_dentry_might_block(struct dentry *dentry)
925{
926 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
927 dentry->d_op->d_manage(dentry, true) < 0);
928}
929
David Howells9875cf82011-01-14 18:45:21 +0000930/*
Al Viro287548e2011-05-27 06:50:06 -0400931 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
932 * we meet a managed dentry that would need blocking.
David Howells9875cf82011-01-14 18:45:21 +0000933 */
934static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
Al Viro287548e2011-05-27 06:50:06 -0400935 struct inode **inode)
David Howells9875cf82011-01-14 18:45:21 +0000936{
Ian Kent62a73752011-03-25 01:51:02 +0800937 for (;;) {
David Howells9875cf82011-01-14 18:45:21 +0000938 struct vfsmount *mounted;
Ian Kent62a73752011-03-25 01:51:02 +0800939 /*
940 * Don't forget we might have a non-mountpoint managed dentry
941 * that wants to block transit.
942 */
943 *inode = path->dentry->d_inode;
Al Viro287548e2011-05-27 06:50:06 -0400944 if (unlikely(managed_dentry_might_block(path->dentry)))
David Howellsab909112011-01-14 18:46:51 +0000945 return false;
Ian Kent62a73752011-03-25 01:51:02 +0800946
947 if (!d_mountpoint(path->dentry))
948 break;
949
David Howells9875cf82011-01-14 18:45:21 +0000950 mounted = __lookup_mnt(path->mnt, path->dentry, 1);
951 if (!mounted)
952 break;
953 path->mnt = mounted;
954 path->dentry = mounted->mnt_root;
955 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
David Howells9875cf82011-01-14 18:45:21 +0000956 }
David Howells9875cf82011-01-14 18:45:21 +0000957 return true;
958}
959
Al Virodea39372011-05-27 06:53:39 -0400960static void follow_mount_rcu(struct nameidata *nd)
Al Viro287548e2011-05-27 06:50:06 -0400961{
Al Virodea39372011-05-27 06:53:39 -0400962 while (d_mountpoint(nd->path.dentry)) {
Al Viro287548e2011-05-27 06:50:06 -0400963 struct vfsmount *mounted;
Al Virodea39372011-05-27 06:53:39 -0400964 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
Al Viro287548e2011-05-27 06:50:06 -0400965 if (!mounted)
966 break;
Al Virodea39372011-05-27 06:53:39 -0400967 nd->path.mnt = mounted;
968 nd->path.dentry = mounted->mnt_root;
969 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro287548e2011-05-27 06:50:06 -0400970 }
971}
972
Nick Piggin31e6b012011-01-07 17:49:52 +1100973static int follow_dotdot_rcu(struct nameidata *nd)
974{
Nick Piggin31e6b012011-01-07 17:49:52 +1100975 set_root_rcu(nd);
976
David Howells9875cf82011-01-14 18:45:21 +0000977 while (1) {
Nick Piggin31e6b012011-01-07 17:49:52 +1100978 if (nd->path.dentry == nd->root.dentry &&
979 nd->path.mnt == nd->root.mnt) {
980 break;
981 }
982 if (nd->path.dentry != nd->path.mnt->mnt_root) {
983 struct dentry *old = nd->path.dentry;
984 struct dentry *parent = old->d_parent;
985 unsigned seq;
986
987 seq = read_seqcount_begin(&parent->d_seq);
988 if (read_seqcount_retry(&old->d_seq, nd->seq))
Al Viroef7562d2011-03-04 14:35:59 -0500989 goto failed;
Nick Piggin31e6b012011-01-07 17:49:52 +1100990 nd->path.dentry = parent;
991 nd->seq = seq;
992 break;
993 }
994 if (!follow_up_rcu(&nd->path))
995 break;
996 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Nick Piggin31e6b012011-01-07 17:49:52 +1100997 }
Al Virodea39372011-05-27 06:53:39 -0400998 follow_mount_rcu(nd);
999 nd->inode = nd->path.dentry->d_inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001000 return 0;
Al Viroef7562d2011-03-04 14:35:59 -05001001
1002failed:
1003 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001004 if (!(nd->flags & LOOKUP_ROOT))
1005 nd->root.mnt = NULL;
Al Viroef7562d2011-03-04 14:35:59 -05001006 rcu_read_unlock();
1007 br_read_unlock(vfsmount_lock);
1008 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001009}
1010
David Howells9875cf82011-01-14 18:45:21 +00001011/*
David Howellscc53ce52011-01-14 18:45:26 +00001012 * Follow down to the covering mount currently visible to userspace. At each
1013 * point, the filesystem owning that dentry may be queried as to whether the
1014 * caller is permitted to proceed or not.
David Howellscc53ce52011-01-14 18:45:26 +00001015 */
Al Viro7cc90cc2011-03-18 09:04:20 -04001016int follow_down(struct path *path)
David Howellscc53ce52011-01-14 18:45:26 +00001017{
1018 unsigned managed;
1019 int ret;
1020
1021 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1022 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1023 /* Allow the filesystem to manage the transit without i_mutex
1024 * being held.
1025 *
1026 * We indicate to the filesystem if someone is trying to mount
1027 * something here. This gives autofs the chance to deny anyone
1028 * other than its daemon the right to mount on its
1029 * superstructure.
1030 *
1031 * The filesystem may sleep at this point.
1032 */
1033 if (managed & DCACHE_MANAGE_TRANSIT) {
1034 BUG_ON(!path->dentry->d_op);
1035 BUG_ON(!path->dentry->d_op->d_manage);
David Howellsab909112011-01-14 18:46:51 +00001036 ret = path->dentry->d_op->d_manage(
Al Viro1aed3e42011-03-18 09:09:02 -04001037 path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +00001038 if (ret < 0)
1039 return ret == -EISDIR ? 0 : ret;
1040 }
1041
1042 /* Transit to a mounted filesystem. */
1043 if (managed & DCACHE_MOUNTED) {
1044 struct vfsmount *mounted = lookup_mnt(path);
1045 if (!mounted)
1046 break;
1047 dput(path->dentry);
1048 mntput(path->mnt);
1049 path->mnt = mounted;
1050 path->dentry = dget(mounted->mnt_root);
1051 continue;
1052 }
1053
1054 /* Don't handle automount points here */
1055 break;
1056 }
1057 return 0;
1058}
1059
1060/*
David Howells9875cf82011-01-14 18:45:21 +00001061 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1062 */
1063static void follow_mount(struct path *path)
1064{
1065 while (d_mountpoint(path->dentry)) {
1066 struct vfsmount *mounted = lookup_mnt(path);
1067 if (!mounted)
1068 break;
1069 dput(path->dentry);
1070 mntput(path->mnt);
1071 path->mnt = mounted;
1072 path->dentry = dget(mounted->mnt_root);
1073 }
1074}
1075
Nick Piggin31e6b012011-01-07 17:49:52 +11001076static void follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
Al Viro2a737872009-04-07 11:49:53 -04001078 set_root(nd);
Andreas Mohre518ddb2006-09-29 02:01:22 -07001079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 while(1) {
Jan Blunck4ac91372008-02-14 19:34:32 -08001081 struct dentry *old = nd->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Al Viro2a737872009-04-07 11:49:53 -04001083 if (nd->path.dentry == nd->root.dentry &&
1084 nd->path.mnt == nd->root.mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 break;
1086 }
Jan Blunck4ac91372008-02-14 19:34:32 -08001087 if (nd->path.dentry != nd->path.mnt->mnt_root) {
Al Viro3088dd72010-01-30 15:47:29 -05001088 /* rare case of legitimate dget_parent()... */
1089 nd->path.dentry = dget_parent(nd->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 dput(old);
1091 break;
1092 }
Al Viro3088dd72010-01-30 15:47:29 -05001093 if (!follow_up(&nd->path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 }
Al Viro79ed0222009-04-18 13:59:41 -04001096 follow_mount(&nd->path);
Nick Piggin31e6b012011-01-07 17:49:52 +11001097 nd->inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098}
1099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100/*
Nick Pigginbaa03892010-08-18 04:37:31 +10001101 * Allocate a dentry with name and parent, and perform a parent
1102 * directory ->lookup on it. Returns the new dentry, or ERR_PTR
1103 * on error. parent->d_inode->i_mutex must be held. d_lookup must
1104 * have verified that no child exists while under i_mutex.
1105 */
1106static struct dentry *d_alloc_and_lookup(struct dentry *parent,
1107 struct qstr *name, struct nameidata *nd)
1108{
1109 struct inode *inode = parent->d_inode;
1110 struct dentry *dentry;
1111 struct dentry *old;
1112
1113 /* Don't create child dentry for a dead directory. */
1114 if (unlikely(IS_DEADDIR(inode)))
1115 return ERR_PTR(-ENOENT);
1116
1117 dentry = d_alloc(parent, name);
1118 if (unlikely(!dentry))
1119 return ERR_PTR(-ENOMEM);
1120
1121 old = inode->i_op->lookup(inode, dentry, nd);
1122 if (unlikely(old)) {
1123 dput(dentry);
1124 dentry = old;
1125 }
1126 return dentry;
1127}
1128
1129/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 * It's more convoluted than I'd like it to be, but... it's still fairly
1131 * small and for now I'd prefer to have fast path as straight as possible.
1132 * It _is_ time-critical.
1133 */
1134static int do_lookup(struct nameidata *nd, struct qstr *name,
Nick Piggin31e6b012011-01-07 17:49:52 +11001135 struct path *path, struct inode **inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
Jan Blunck4ac91372008-02-14 19:34:32 -08001137 struct vfsmount *mnt = nd->path.mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +11001138 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro5a18fff2011-03-11 04:44:53 -05001139 int need_reval = 1;
1140 int status = 1;
David Howells9875cf82011-01-14 18:45:21 +00001141 int err;
1142
Al Viro3cac2602009-08-13 18:27:43 +04001143 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001144 * Rename seqlock is not required here because in the off chance
1145 * of a false negative due to a concurrent rename, we're going to
1146 * do the non-racy lookup, below.
1147 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001148 if (nd->flags & LOOKUP_RCU) {
1149 unsigned seq;
Nick Piggin31e6b012011-01-07 17:49:52 +11001150 *inode = nd->inode;
1151 dentry = __d_lookup_rcu(parent, name, &seq, inode);
Al Viro5a18fff2011-03-11 04:44:53 -05001152 if (!dentry)
1153 goto unlazy;
1154
Nick Piggin31e6b012011-01-07 17:49:52 +11001155 /* Memory barrier in read_seqcount_begin of child is enough */
1156 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1157 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001158 nd->seq = seq;
Al Viro5a18fff2011-03-11 04:44:53 -05001159
Al Viro24643082011-02-15 01:26:22 -05001160 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
Al Viro5a18fff2011-03-11 04:44:53 -05001161 status = d_revalidate(dentry, nd);
1162 if (unlikely(status <= 0)) {
1163 if (status != -ECHILD)
1164 need_reval = 0;
1165 goto unlazy;
1166 }
Al Viro24643082011-02-15 01:26:22 -05001167 }
Nick Piggin31e6b012011-01-07 17:49:52 +11001168 path->mnt = mnt;
1169 path->dentry = dentry;
Al Virod6e9bd22011-05-27 07:03:15 -04001170 if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1171 goto unlazy;
1172 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1173 goto unlazy;
1174 return 0;
Al Viro5a18fff2011-03-11 04:44:53 -05001175unlazy:
Al Viro19660af2011-03-25 10:32:48 -04001176 if (unlazy_walk(nd, dentry))
1177 return -ECHILD;
Al Viro5a18fff2011-03-11 04:44:53 -05001178 } else {
1179 dentry = __d_lookup(parent, name);
Nick Piggin31e6b012011-01-07 17:49:52 +11001180 }
Al Viro5a18fff2011-03-11 04:44:53 -05001181
1182retry:
1183 if (unlikely(!dentry)) {
1184 struct inode *dir = parent->d_inode;
1185 BUG_ON(nd->inode != dir);
1186
1187 mutex_lock(&dir->i_mutex);
1188 dentry = d_lookup(parent, name);
1189 if (likely(!dentry)) {
1190 dentry = d_alloc_and_lookup(parent, name, nd);
1191 if (IS_ERR(dentry)) {
1192 mutex_unlock(&dir->i_mutex);
1193 return PTR_ERR(dentry);
1194 }
1195 /* known good */
1196 need_reval = 0;
1197 status = 1;
1198 }
1199 mutex_unlock(&dir->i_mutex);
Al Viro24643082011-02-15 01:26:22 -05001200 }
Al Viro5a18fff2011-03-11 04:44:53 -05001201 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
1202 status = d_revalidate(dentry, nd);
1203 if (unlikely(status <= 0)) {
1204 if (status < 0) {
1205 dput(dentry);
1206 return status;
1207 }
1208 if (!d_invalidate(dentry)) {
1209 dput(dentry);
1210 dentry = NULL;
1211 need_reval = 1;
1212 goto retry;
1213 }
1214 }
1215
David Howells9875cf82011-01-14 18:45:21 +00001216 path->mnt = mnt;
1217 path->dentry = dentry;
1218 err = follow_managed(path, nd->flags);
Ian Kent89312212011-01-18 12:06:10 +08001219 if (unlikely(err < 0)) {
1220 path_put_conditional(path, nd);
David Howells9875cf82011-01-14 18:45:21 +00001221 return err;
Ian Kent89312212011-01-18 12:06:10 +08001222 }
David Howells9875cf82011-01-14 18:45:21 +00001223 *inode = path->dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225}
1226
Al Viro52094c82011-02-21 21:34:47 -05001227static inline int may_lookup(struct nameidata *nd)
1228{
1229 if (nd->flags & LOOKUP_RCU) {
1230 int err = exec_permission(nd->inode, IPERM_FLAG_RCU);
1231 if (err != -ECHILD)
1232 return err;
Al Viro19660af2011-03-25 10:32:48 -04001233 if (unlazy_walk(nd, NULL))
Al Viro52094c82011-02-21 21:34:47 -05001234 return -ECHILD;
1235 }
1236 return exec_permission(nd->inode, 0);
1237}
1238
Al Viro9856fa12011-03-04 14:22:06 -05001239static inline int handle_dots(struct nameidata *nd, int type)
1240{
1241 if (type == LAST_DOTDOT) {
1242 if (nd->flags & LOOKUP_RCU) {
1243 if (follow_dotdot_rcu(nd))
1244 return -ECHILD;
1245 } else
1246 follow_dotdot(nd);
1247 }
1248 return 0;
1249}
1250
Al Viro951361f2011-03-04 14:44:37 -05001251static void terminate_walk(struct nameidata *nd)
1252{
1253 if (!(nd->flags & LOOKUP_RCU)) {
1254 path_put(&nd->path);
1255 } else {
1256 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001257 if (!(nd->flags & LOOKUP_ROOT))
1258 nd->root.mnt = NULL;
Al Viro951361f2011-03-04 14:44:37 -05001259 rcu_read_unlock();
1260 br_read_unlock(vfsmount_lock);
1261 }
1262}
1263
Al Viroce57dfc2011-03-13 19:58:58 -04001264static inline int walk_component(struct nameidata *nd, struct path *path,
1265 struct qstr *name, int type, int follow)
1266{
1267 struct inode *inode;
1268 int err;
1269 /*
1270 * "." and ".." are special - ".." especially so because it has
1271 * to be able to know about the current root directory and
1272 * parent relationships.
1273 */
1274 if (unlikely(type != LAST_NORM))
1275 return handle_dots(nd, type);
1276 err = do_lookup(nd, name, path, &inode);
1277 if (unlikely(err)) {
1278 terminate_walk(nd);
1279 return err;
1280 }
1281 if (!inode) {
1282 path_to_nameidata(path, nd);
1283 terminate_walk(nd);
1284 return -ENOENT;
1285 }
1286 if (unlikely(inode->i_op->follow_link) && follow) {
Al Viro19660af2011-03-25 10:32:48 -04001287 if (nd->flags & LOOKUP_RCU) {
1288 if (unlikely(unlazy_walk(nd, path->dentry))) {
1289 terminate_walk(nd);
1290 return -ECHILD;
1291 }
1292 }
Al Viroce57dfc2011-03-13 19:58:58 -04001293 BUG_ON(inode != path->dentry->d_inode);
1294 return 1;
1295 }
1296 path_to_nameidata(path, nd);
1297 nd->inode = inode;
1298 return 0;
1299}
1300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301/*
Al Virob3563792011-03-14 21:54:55 -04001302 * This limits recursive symlink follows to 8, while
1303 * limiting consecutive symlinks to 40.
1304 *
1305 * Without that kind of total limit, nasty chains of consecutive
1306 * symlinks can cause almost arbitrarily long lookups.
1307 */
1308static inline int nested_symlink(struct path *path, struct nameidata *nd)
1309{
1310 int res;
1311
Al Virob3563792011-03-14 21:54:55 -04001312 if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1313 path_put_conditional(path, nd);
1314 path_put(&nd->path);
1315 return -ELOOP;
1316 }
Erez Zadok1a4022f2011-05-21 01:19:59 -04001317 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
Al Virob3563792011-03-14 21:54:55 -04001318
1319 nd->depth++;
1320 current->link_count++;
1321
1322 do {
1323 struct path link = *path;
1324 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04001325
1326 res = follow_link(&link, nd, &cookie);
Al Virob3563792011-03-14 21:54:55 -04001327 if (!res)
1328 res = walk_component(nd, path, &nd->last,
1329 nd->last_type, LOOKUP_FOLLOW);
Al Viro574197e2011-03-14 22:20:34 -04001330 put_link(nd, &link, cookie);
Al Virob3563792011-03-14 21:54:55 -04001331 } while (res > 0);
1332
1333 current->link_count--;
1334 nd->depth--;
1335 return res;
1336}
1337
1338/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01001340 * This is the basic name resolution function, turning a pathname into
1341 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01001343 * Returns 0 and nd will have valid dentry and mnt on success.
1344 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 */
Al Viro6de88d72009-08-09 01:41:57 +04001346static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{
1348 struct path next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 int err;
1350 unsigned int lookup_flags = nd->flags;
1351
1352 while (*name=='/')
1353 name++;
1354 if (!*name)
Al Viro086e1832011-02-22 20:56:27 -05001355 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 /* At this point we know we have a real path component. */
1358 for(;;) {
1359 unsigned long hash;
1360 struct qstr this;
1361 unsigned int c;
Al Virofe479a52011-02-22 15:10:03 -05001362 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Trond Myklebustcdce5d62005-10-18 14:20:18 -07001364 nd->flags |= LOOKUP_CONTINUE;
Al Viro52094c82011-02-21 21:34:47 -05001365
1366 err = may_lookup(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 if (err)
1368 break;
1369
1370 this.name = name;
1371 c = *(const unsigned char *)name;
1372
1373 hash = init_name_hash();
1374 do {
1375 name++;
1376 hash = partial_name_hash(c, hash);
1377 c = *(const unsigned char *)name;
1378 } while (c && (c != '/'));
1379 this.len = name - (const char *) this.name;
1380 this.hash = end_name_hash(hash);
1381
Al Virofe479a52011-02-22 15:10:03 -05001382 type = LAST_NORM;
1383 if (this.name[0] == '.') switch (this.len) {
1384 case 2:
Al Viro16c2cd72011-02-22 15:50:10 -05001385 if (this.name[1] == '.') {
Al Virofe479a52011-02-22 15:10:03 -05001386 type = LAST_DOTDOT;
Al Viro16c2cd72011-02-22 15:50:10 -05001387 nd->flags |= LOOKUP_JUMPED;
1388 }
Al Virofe479a52011-02-22 15:10:03 -05001389 break;
1390 case 1:
1391 type = LAST_DOT;
1392 }
Al Viro5a202bc2011-03-08 14:17:44 -05001393 if (likely(type == LAST_NORM)) {
1394 struct dentry *parent = nd->path.dentry;
Al Viro16c2cd72011-02-22 15:50:10 -05001395 nd->flags &= ~LOOKUP_JUMPED;
Al Viro5a202bc2011-03-08 14:17:44 -05001396 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1397 err = parent->d_op->d_hash(parent, nd->inode,
1398 &this);
1399 if (err < 0)
1400 break;
1401 }
1402 }
Al Virofe479a52011-02-22 15:10:03 -05001403
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 /* remove trailing slashes? */
1405 if (!c)
1406 goto last_component;
1407 while (*++name == '/');
1408 if (!*name)
Al Virob3563792011-03-14 21:54:55 -04001409 goto last_component;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Al Viroce57dfc2011-03-13 19:58:58 -04001411 err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1412 if (err < 0)
1413 return err;
Al Virofe479a52011-02-22 15:10:03 -05001414
Al Viroce57dfc2011-03-13 19:58:58 -04001415 if (err) {
Al Virob3563792011-03-14 21:54:55 -04001416 err = nested_symlink(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 if (err)
Al Viroa7472ba2011-03-04 14:39:30 -05001418 return err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 err = -ENOTDIR;
Nick Piggin31e6b012011-01-07 17:49:52 +11001421 if (!nd->inode->i_op->lookup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 break;
1423 continue;
1424 /* here ends the main loop */
1425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426last_component:
Trond Myklebustf55eab82006-02-04 23:28:01 -08001427 /* Clear LOOKUP_CONTINUE iff it was previously unset */
1428 nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
Al Virob3563792011-03-14 21:54:55 -04001429 nd->last = this;
1430 nd->last_type = type;
Al Viro086e1832011-02-22 20:56:27 -05001431 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 }
Al Viro951361f2011-03-04 14:44:37 -05001433 terminate_walk(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 return err;
1435}
1436
Al Viro70e9b352011-03-05 21:12:22 -05001437static int path_init(int dfd, const char *name, unsigned int flags,
1438 struct nameidata *nd, struct file **fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
Prasanna Medaea3834d2005-04-29 16:00:17 +01001440 int retval = 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001441 int fput_needed;
1442 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
1444 nd->last_type = LAST_ROOT; /* if there are only slashes... */
Al Viro16c2cd72011-02-22 15:50:10 -05001445 nd->flags = flags | LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 nd->depth = 0;
Al Viro5b6ca022011-03-09 23:04:47 -05001447 if (flags & LOOKUP_ROOT) {
1448 struct inode *inode = nd->root.dentry->d_inode;
Al Viro73d049a2011-03-11 12:08:24 -05001449 if (*name) {
1450 if (!inode->i_op->lookup)
1451 return -ENOTDIR;
1452 retval = inode_permission(inode, MAY_EXEC);
1453 if (retval)
1454 return retval;
1455 }
Al Viro5b6ca022011-03-09 23:04:47 -05001456 nd->path = nd->root;
1457 nd->inode = inode;
1458 if (flags & LOOKUP_RCU) {
1459 br_read_lock(vfsmount_lock);
1460 rcu_read_lock();
1461 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1462 } else {
1463 path_get(&nd->path);
1464 }
1465 return 0;
1466 }
1467
Al Viro2a737872009-04-07 11:49:53 -04001468 nd->root.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 if (*name=='/') {
Al Viroe41f7d42011-02-22 14:02:58 -05001471 if (flags & LOOKUP_RCU) {
1472 br_read_lock(vfsmount_lock);
1473 rcu_read_lock();
1474 set_root_rcu(nd);
1475 } else {
1476 set_root(nd);
1477 path_get(&nd->root);
1478 }
Al Viro2a737872009-04-07 11:49:53 -04001479 nd->path = nd->root;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001480 } else if (dfd == AT_FDCWD) {
Al Viroe41f7d42011-02-22 14:02:58 -05001481 if (flags & LOOKUP_RCU) {
1482 struct fs_struct *fs = current->fs;
1483 unsigned seq;
1484
1485 br_read_lock(vfsmount_lock);
1486 rcu_read_lock();
1487
1488 do {
1489 seq = read_seqcount_begin(&fs->seq);
1490 nd->path = fs->pwd;
1491 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1492 } while (read_seqcount_retry(&fs->seq, seq));
1493 } else {
1494 get_fs_pwd(current->fs, &nd->path);
1495 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001496 } else {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001497 struct dentry *dentry;
1498
Al Viro1abf0c72011-03-13 03:51:11 -04001499 file = fget_raw_light(dfd, &fput_needed);
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001500 retval = -EBADF;
1501 if (!file)
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001502 goto out_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001503
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001504 dentry = file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001505
Al Virof52e0c12011-03-14 18:56:51 -04001506 if (*name) {
1507 retval = -ENOTDIR;
1508 if (!S_ISDIR(dentry->d_inode->i_mode))
1509 goto fput_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001510
Al Virof52e0c12011-03-14 18:56:51 -04001511 retval = file_permission(file, MAY_EXEC);
1512 if (retval)
1513 goto fput_fail;
1514 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001515
Jan Blunck5dd784d2008-02-14 19:34:38 -08001516 nd->path = file->f_path;
Al Viroe41f7d42011-02-22 14:02:58 -05001517 if (flags & LOOKUP_RCU) {
1518 if (fput_needed)
Al Viro70e9b352011-03-05 21:12:22 -05001519 *fp = file;
Al Viroe41f7d42011-02-22 14:02:58 -05001520 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1521 br_read_lock(vfsmount_lock);
1522 rcu_read_lock();
1523 } else {
1524 path_get(&file->f_path);
1525 fput_light(file, fput_needed);
1526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 }
Al Viroe41f7d42011-02-22 14:02:58 -05001528
Nick Piggin31e6b012011-01-07 17:49:52 +11001529 nd->inode = nd->path.dentry->d_inode;
Al Viro9b4a9b12009-04-07 11:44:16 -04001530 return 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001531
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001532fput_fail:
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001533 fput_light(file, fput_needed);
Al Viro9b4a9b12009-04-07 11:44:16 -04001534out_fail:
1535 return retval;
1536}
1537
Al Virobd92d7f2011-03-14 19:54:59 -04001538static inline int lookup_last(struct nameidata *nd, struct path *path)
1539{
1540 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1541 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1542
1543 nd->flags &= ~LOOKUP_PARENT;
1544 return walk_component(nd, path, &nd->last, nd->last_type,
1545 nd->flags & LOOKUP_FOLLOW);
1546}
1547
Al Viro9b4a9b12009-04-07 11:44:16 -04001548/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroee0827c2011-02-21 23:38:09 -05001549static int path_lookupat(int dfd, const char *name,
Al Viro9b4a9b12009-04-07 11:44:16 -04001550 unsigned int flags, struct nameidata *nd)
1551{
Al Viro70e9b352011-03-05 21:12:22 -05001552 struct file *base = NULL;
Al Virobd92d7f2011-03-14 19:54:59 -04001553 struct path path;
1554 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001555
1556 /*
1557 * Path walking is largely split up into 2 different synchronisation
1558 * schemes, rcu-walk and ref-walk (explained in
1559 * Documentation/filesystems/path-lookup.txt). These share much of the
1560 * path walk code, but some things particularly setup, cleanup, and
1561 * following mounts are sufficiently divergent that functions are
1562 * duplicated. Typically there is a function foo(), and its RCU
1563 * analogue, foo_rcu().
1564 *
1565 * -ECHILD is the error number of choice (just to avoid clashes) that
1566 * is returned if some aspect of an rcu-walk fails. Such an error must
1567 * be handled by restarting a traditional ref-walk (which will always
1568 * be able to complete).
1569 */
Al Virobd92d7f2011-03-14 19:54:59 -04001570 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
Al Viroee0827c2011-02-21 23:38:09 -05001571
Al Virobd92d7f2011-03-14 19:54:59 -04001572 if (unlikely(err))
1573 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001574
1575 current->total_link_count = 0;
Al Virobd92d7f2011-03-14 19:54:59 -04001576 err = link_path_walk(name, nd);
1577
1578 if (!err && !(flags & LOOKUP_PARENT)) {
Al Virobd92d7f2011-03-14 19:54:59 -04001579 err = lookup_last(nd, &path);
1580 while (err > 0) {
1581 void *cookie;
1582 struct path link = path;
Al Virobd92d7f2011-03-14 19:54:59 -04001583 nd->flags |= LOOKUP_PARENT;
Al Viro574197e2011-03-14 22:20:34 -04001584 err = follow_link(&link, nd, &cookie);
Al Virobd92d7f2011-03-14 19:54:59 -04001585 if (!err)
1586 err = lookup_last(nd, &path);
Al Viro574197e2011-03-14 22:20:34 -04001587 put_link(nd, &link, cookie);
Al Virobd92d7f2011-03-14 19:54:59 -04001588 }
1589 }
Al Viroee0827c2011-02-21 23:38:09 -05001590
Al Viro9f1fafe2011-03-25 11:00:12 -04001591 if (!err)
1592 err = complete_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04001593
1594 if (!err && nd->flags & LOOKUP_DIRECTORY) {
1595 if (!nd->inode->i_op->lookup) {
1596 path_put(&nd->path);
Al Virobd23a532011-03-23 09:56:30 -04001597 err = -ENOTDIR;
Al Virobd92d7f2011-03-14 19:54:59 -04001598 }
1599 }
Al Viro16c2cd72011-02-22 15:50:10 -05001600
Al Viro70e9b352011-03-05 21:12:22 -05001601 if (base)
1602 fput(base);
Al Viroee0827c2011-02-21 23:38:09 -05001603
Al Viro5b6ca022011-03-09 23:04:47 -05001604 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
Al Viro2a737872009-04-07 11:49:53 -04001605 path_put(&nd->root);
1606 nd->root.mnt = NULL;
1607 }
Al Virobd92d7f2011-03-14 19:54:59 -04001608 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001609}
Nick Piggin31e6b012011-01-07 17:49:52 +11001610
Al Viroee0827c2011-02-21 23:38:09 -05001611static int do_path_lookup(int dfd, const char *name,
1612 unsigned int flags, struct nameidata *nd)
1613{
1614 int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1615 if (unlikely(retval == -ECHILD))
1616 retval = path_lookupat(dfd, name, flags, nd);
1617 if (unlikely(retval == -ESTALE))
1618 retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11001619
1620 if (likely(!retval)) {
1621 if (unlikely(!audit_dummy_context())) {
1622 if (nd->path.dentry && nd->inode)
1623 audit_inode(name, nd->path.dentry);
1624 }
1625 }
Al Viro9b4a9b12009-04-07 11:44:16 -04001626 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627}
1628
Al Viroc9c6cac2011-02-16 15:15:47 -05001629int kern_path_parent(const char *name, struct nameidata *nd)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001630{
Al Viroc9c6cac2011-02-16 15:15:47 -05001631 return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001632}
1633
Al Virod1811462008-08-02 00:49:18 -04001634int kern_path(const char *name, unsigned int flags, struct path *path)
1635{
1636 struct nameidata nd;
1637 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1638 if (!res)
1639 *path = nd.path;
1640 return res;
1641}
1642
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001643/**
1644 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1645 * @dentry: pointer to dentry of the base directory
1646 * @mnt: pointer to vfs mount of the base directory
1647 * @name: pointer to file name
1648 * @flags: lookup flags
1649 * @nd: pointer to nameidata
1650 */
1651int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
1652 const char *name, unsigned int flags,
1653 struct nameidata *nd)
1654{
Al Viro5b6ca022011-03-09 23:04:47 -05001655 nd->root.dentry = dentry;
1656 nd->root.mnt = mnt;
1657 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
1658 return do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, nd);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001659}
1660
Christoph Hellwigeead1912007-10-16 23:25:38 -07001661static struct dentry *__lookup_hash(struct qstr *name,
1662 struct dentry *base, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663{
Christoph Hellwig81fca442010-10-06 10:47:47 +02001664 struct inode *inode = base->d_inode;
James Morris057f6c02007-04-26 00:12:05 -07001665 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 int err;
1667
Nick Pigginb74c79e2011-01-07 17:49:58 +11001668 err = exec_permission(inode, 0);
Christoph Hellwig81fca442010-10-06 10:47:47 +02001669 if (err)
1670 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
1672 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001673 * Don't bother with __d_lookup: callers are for creat as
1674 * well as unlink, so a lot of the time it would cost
1675 * a double lookup.
Al Viro6e6b1bd2009-08-13 23:38:37 +04001676 */
Nick Pigginb04f7842010-08-18 04:37:34 +10001677 dentry = d_lookup(base, name);
Al Viro6e6b1bd2009-08-13 23:38:37 +04001678
Nick Pigginfb045ad2011-01-07 17:49:55 +11001679 if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE))
Al Viro6e6b1bd2009-08-13 23:38:37 +04001680 dentry = do_revalidate(dentry, nd);
1681
Nick Pigginbaa03892010-08-18 04:37:31 +10001682 if (!dentry)
1683 dentry = d_alloc_and_lookup(base, name, nd);
Al Viro5a202bc2011-03-08 14:17:44 -05001684
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 return dentry;
1686}
1687
James Morris057f6c02007-04-26 00:12:05 -07001688/*
1689 * Restricted form of lookup. Doesn't follow links, single-component only,
1690 * needs parent already locked. Doesn't follow mounts.
1691 * SMP-safe.
1692 */
Adrian Bunka244e162006-03-31 02:32:11 -08001693static struct dentry *lookup_hash(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694{
Jan Blunck4ac91372008-02-14 19:34:32 -08001695 return __lookup_hash(&nd->last, nd->path.dentry, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696}
1697
Christoph Hellwigeead1912007-10-16 23:25:38 -07001698/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07001699 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07001700 * @name: pathname component to lookup
1701 * @base: base directory to lookup from
1702 * @len: maximum length @len should be interpreted to
1703 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07001704 * Note that this routine is purely a helper for filesystem usage and should
1705 * not be called by generic code. Also note that by using this function the
Christoph Hellwigeead1912007-10-16 23:25:38 -07001706 * nameidata argument is passed to the filesystem methods and a filesystem
1707 * using this helper needs to be prepared for that.
1708 */
James Morris057f6c02007-04-26 00:12:05 -07001709struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1710{
James Morris057f6c02007-04-26 00:12:05 -07001711 struct qstr this;
Al Viro6a96ba52011-03-07 23:49:20 -05001712 unsigned long hash;
1713 unsigned int c;
James Morris057f6c02007-04-26 00:12:05 -07001714
David Woodhouse2f9092e2009-04-20 23:18:37 +01001715 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
1716
Al Viro6a96ba52011-03-07 23:49:20 -05001717 this.name = name;
1718 this.len = len;
1719 if (!len)
1720 return ERR_PTR(-EACCES);
1721
1722 hash = init_name_hash();
1723 while (len--) {
1724 c = *(const unsigned char *)name++;
1725 if (c == '/' || c == '\0')
1726 return ERR_PTR(-EACCES);
1727 hash = partial_name_hash(c, hash);
1728 }
1729 this.hash = end_name_hash(hash);
Al Viro5a202bc2011-03-08 14:17:44 -05001730 /*
1731 * See if the low-level filesystem might want
1732 * to use its own hash..
1733 */
1734 if (base->d_flags & DCACHE_OP_HASH) {
1735 int err = base->d_op->d_hash(base, base->d_inode, &this);
1736 if (err < 0)
1737 return ERR_PTR(err);
1738 }
Christoph Hellwigeead1912007-10-16 23:25:38 -07001739
Christoph Hellwig49705b72005-11-08 21:35:06 -08001740 return __lookup_hash(&this, base, NULL);
James Morris057f6c02007-04-26 00:12:05 -07001741}
1742
Al Viro2d8f3032008-07-22 09:59:21 -04001743int user_path_at(int dfd, const char __user *name, unsigned flags,
1744 struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745{
Al Viro2d8f3032008-07-22 09:59:21 -04001746 struct nameidata nd;
Al Virof52e0c12011-03-14 18:56:51 -04001747 char *tmp = getname_flags(name, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 int err = PTR_ERR(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 if (!IS_ERR(tmp)) {
Al Viro2d8f3032008-07-22 09:59:21 -04001750
1751 BUG_ON(flags & LOOKUP_PARENT);
1752
1753 err = do_path_lookup(dfd, tmp, flags, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 putname(tmp);
Al Viro2d8f3032008-07-22 09:59:21 -04001755 if (!err)
1756 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 }
1758 return err;
1759}
1760
Al Viro2ad94ae2008-07-21 09:32:51 -04001761static int user_path_parent(int dfd, const char __user *path,
1762 struct nameidata *nd, char **name)
1763{
1764 char *s = getname(path);
1765 int error;
1766
1767 if (IS_ERR(s))
1768 return PTR_ERR(s);
1769
1770 error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
1771 if (error)
1772 putname(s);
1773 else
1774 *name = s;
1775
1776 return error;
1777}
1778
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779/*
1780 * It's inline, so penalty for filesystems that don't use sticky bit is
1781 * minimal.
1782 */
1783static inline int check_sticky(struct inode *dir, struct inode *inode)
1784{
David Howellsda9592e2008-11-14 10:39:05 +11001785 uid_t fsuid = current_fsuid();
1786
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 if (!(dir->i_mode & S_ISVTX))
1788 return 0;
Serge E. Hallyne795b712011-03-23 16:43:25 -07001789 if (current_user_ns() != inode_userns(inode))
1790 goto other_userns;
David Howellsda9592e2008-11-14 10:39:05 +11001791 if (inode->i_uid == fsuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 return 0;
David Howellsda9592e2008-11-14 10:39:05 +11001793 if (dir->i_uid == fsuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 return 0;
Serge E. Hallyne795b712011-03-23 16:43:25 -07001795
1796other_userns:
1797 return !ns_capable(inode_userns(inode), CAP_FOWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798}
1799
1800/*
1801 * Check whether we can remove a link victim from directory dir, check
1802 * whether the type of victim is right.
1803 * 1. We can't do it if dir is read-only (done in permission())
1804 * 2. We should have write and exec permissions on dir
1805 * 3. We can't remove anything from append-only dir
1806 * 4. We can't do anything with immutable dir (done in permission())
1807 * 5. If the sticky bit on dir is set we should either
1808 * a. be owner of dir, or
1809 * b. be owner of victim, or
1810 * c. have CAP_FOWNER capability
1811 * 6. If the victim is append-only or immutable we can't do antyhing with
1812 * links pointing to it.
1813 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1814 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1815 * 9. We can't remove a root or mountpoint.
1816 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1817 * nfs_async_unlink().
1818 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001819static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820{
1821 int error;
1822
1823 if (!victim->d_inode)
1824 return -ENOENT;
1825
1826 BUG_ON(victim->d_parent->d_inode != dir);
Al Virocccc6bb2009-12-25 05:07:33 -05001827 audit_inode_child(victim, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Al Virof419a2e2008-07-22 00:07:17 -04001829 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 if (error)
1831 return error;
1832 if (IS_APPEND(dir))
1833 return -EPERM;
1834 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
Hugh Dickinsf9454542008-11-19 15:36:38 -08001835 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 return -EPERM;
1837 if (isdir) {
1838 if (!S_ISDIR(victim->d_inode->i_mode))
1839 return -ENOTDIR;
1840 if (IS_ROOT(victim))
1841 return -EBUSY;
1842 } else if (S_ISDIR(victim->d_inode->i_mode))
1843 return -EISDIR;
1844 if (IS_DEADDIR(dir))
1845 return -ENOENT;
1846 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1847 return -EBUSY;
1848 return 0;
1849}
1850
1851/* Check whether we can create an object with dentry child in directory
1852 * dir.
1853 * 1. We can't do it if child already exists (open has special treatment for
1854 * this case, but since we are inlined it's OK)
1855 * 2. We can't do it if dir is read-only (done in permission())
1856 * 3. We should have write and exec permissions on dir
1857 * 4. We can't do it if dir is immutable (done in permission())
1858 */
Miklos Szeredia95164d2008-07-30 15:08:48 +02001859static inline int may_create(struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860{
1861 if (child->d_inode)
1862 return -EEXIST;
1863 if (IS_DEADDIR(dir))
1864 return -ENOENT;
Al Virof419a2e2008-07-22 00:07:17 -04001865 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866}
1867
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868/*
1869 * p1 and p2 should be directories on the same fs.
1870 */
1871struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1872{
1873 struct dentry *p;
1874
1875 if (p1 == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07001876 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 return NULL;
1878 }
1879
Arjan van de Vena11f3a02006-03-23 03:00:33 -08001880 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001882 p = d_ancestor(p2, p1);
1883 if (p) {
1884 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1885 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
1886 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 }
1888
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001889 p = d_ancestor(p1, p2);
1890 if (p) {
1891 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1892 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1893 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 }
1895
Ingo Molnarf2eace22006-07-03 00:25:05 -07001896 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1897 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 return NULL;
1899}
1900
1901void unlock_rename(struct dentry *p1, struct dentry *p2)
1902{
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001903 mutex_unlock(&p1->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 if (p1 != p2) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001905 mutex_unlock(&p2->d_inode->i_mutex);
Arjan van de Vena11f3a02006-03-23 03:00:33 -08001906 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 }
1908}
1909
1910int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1911 struct nameidata *nd)
1912{
Miklos Szeredia95164d2008-07-30 15:08:48 +02001913 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
1915 if (error)
1916 return error;
1917
Al Viroacfa4382008-12-04 10:06:33 -05001918 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 return -EACCES; /* shouldn't it be ENOSYS? */
1920 mode &= S_IALLUGO;
1921 mode |= S_IFREG;
1922 error = security_inode_create(dir, dentry, mode);
1923 if (error)
1924 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 error = dir->i_op->create(dir, dentry, mode, nd);
Stephen Smalleya74574a2005-09-09 13:01:44 -07001926 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00001927 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 return error;
1929}
1930
Al Viro73d049a2011-03-11 12:08:24 -05001931static int may_open(struct path *path, int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001933 struct dentry *dentry = path->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 struct inode *inode = dentry->d_inode;
1935 int error;
1936
Al Virobcda7652011-03-13 16:42:14 -04001937 /* O_PATH? */
1938 if (!acc_mode)
1939 return 0;
1940
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 if (!inode)
1942 return -ENOENT;
1943
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001944 switch (inode->i_mode & S_IFMT) {
1945 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001947 case S_IFDIR:
1948 if (acc_mode & MAY_WRITE)
1949 return -EISDIR;
1950 break;
1951 case S_IFBLK:
1952 case S_IFCHR:
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001953 if (path->mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001955 /*FALLTHRU*/
1956 case S_IFIFO:
1957 case S_IFSOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001959 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08001960 }
Dave Hansenb41572e2007-10-16 23:31:14 -07001961
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001962 error = inode_permission(inode, acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07001963 if (error)
1964 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05001965
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 /*
1967 * An append-only file must be opened in append mode for writing.
1968 */
1969 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05001970 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05001971 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05001973 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 }
1975
1976 /* O_NOATIME can only be set by the owner or superuser */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001977 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
Al Viro7715b522009-12-16 03:54:00 -05001978 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
1980 /*
1981 * Ensure there are no outstanding leases on the file.
1982 */
Al Virob65a9cf2009-12-16 06:27:40 -05001983 return break_lease(inode, flag);
Al Viro7715b522009-12-16 03:54:00 -05001984}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Jeff Laytone1181ee2010-12-07 16:19:50 -05001986static int handle_truncate(struct file *filp)
Al Viro7715b522009-12-16 03:54:00 -05001987{
Jeff Laytone1181ee2010-12-07 16:19:50 -05001988 struct path *path = &filp->f_path;
Al Viro7715b522009-12-16 03:54:00 -05001989 struct inode *inode = path->dentry->d_inode;
1990 int error = get_write_access(inode);
1991 if (error)
1992 return error;
1993 /*
1994 * Refuse to truncate files with mandatory locks held on them.
1995 */
1996 error = locks_verify_locked(inode);
1997 if (!error)
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +09001998 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05001999 if (!error) {
2000 error = do_truncate(path->dentry, 0,
2001 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
Jeff Laytone1181ee2010-12-07 16:19:50 -05002002 filp);
Al Viro7715b522009-12-16 03:54:00 -05002003 }
2004 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04002005 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006}
2007
Dave Hansend57999e2008-02-15 14:37:27 -08002008/*
Dave Hansend57999e2008-02-15 14:37:27 -08002009 * Note that while the flag value (low two bits) for sys_open means:
2010 * 00 - read-only
2011 * 01 - write-only
2012 * 10 - read-write
2013 * 11 - special
2014 * it is changed into
2015 * 00 - no permissions needed
2016 * 01 - read-permission
2017 * 10 - write-permission
2018 * 11 - read-write
2019 * for the internal routines (ie open_namei()/follow_link() etc)
2020 * This is more logical, and also allows the 00 "no perm needed"
2021 * to be used for symlinks (where the permissions are checked
2022 * later).
2023 *
2024*/
2025static inline int open_to_namei_flags(int flag)
2026{
2027 if ((flag+1) & O_ACCMODE)
2028 flag++;
2029 return flag;
2030}
2031
Nick Piggin31e6b012011-01-07 17:49:52 +11002032/*
Al Virofe2d35f2011-03-05 22:58:25 -05002033 * Handle the last step of open()
Nick Piggin31e6b012011-01-07 17:49:52 +11002034 */
Al Virofb1cc552009-12-24 01:58:28 -05002035static struct file *do_last(struct nameidata *nd, struct path *path,
Al Viroc3e380b2011-02-23 13:39:45 -05002036 const struct open_flags *op, const char *pathname)
Al Virofb1cc552009-12-24 01:58:28 -05002037{
Al Viroa1e28032009-12-24 02:12:06 -05002038 struct dentry *dir = nd->path.dentry;
Al Viro6c0d46c2011-03-09 00:59:59 -05002039 struct dentry *dentry;
Al Viroca344a892011-03-09 00:36:45 -05002040 int open_flag = op->open_flag;
Al Viro6c0d46c2011-03-09 00:59:59 -05002041 int will_truncate = open_flag & O_TRUNC;
Al Viroca344a892011-03-09 00:36:45 -05002042 int want_write = 0;
Al Virobcda7652011-03-13 16:42:14 -04002043 int acc_mode = op->acc_mode;
Al Virofb1cc552009-12-24 01:58:28 -05002044 struct file *filp;
Al Viro16c2cd72011-02-22 15:50:10 -05002045 int error;
Al Virofb1cc552009-12-24 01:58:28 -05002046
Al Viroc3e380b2011-02-23 13:39:45 -05002047 nd->flags &= ~LOOKUP_PARENT;
2048 nd->flags |= op->intent;
2049
Al Viro1f36f772009-12-26 10:56:19 -05002050 switch (nd->last_type) {
2051 case LAST_DOTDOT:
Neil Brown176306f2010-05-24 16:57:56 +10002052 case LAST_DOT:
Al Virofe2d35f2011-03-05 22:58:25 -05002053 error = handle_dots(nd, nd->last_type);
2054 if (error)
2055 return ERR_PTR(error);
Al Viro1f36f772009-12-26 10:56:19 -05002056 /* fallthrough */
Al Viro1f36f772009-12-26 10:56:19 -05002057 case LAST_ROOT:
Al Viro9f1fafe2011-03-25 11:00:12 -04002058 error = complete_walk(nd);
Al Viro16c2cd72011-02-22 15:50:10 -05002059 if (error)
Al Viro9f1fafe2011-03-25 11:00:12 -04002060 return ERR_PTR(error);
Al Virofe2d35f2011-03-05 22:58:25 -05002061 audit_inode(pathname, nd->path.dentry);
Al Viroca344a892011-03-09 00:36:45 -05002062 if (open_flag & O_CREAT) {
Al Virofe2d35f2011-03-05 22:58:25 -05002063 error = -EISDIR;
2064 goto exit;
2065 }
2066 goto ok;
Al Viro1f36f772009-12-26 10:56:19 -05002067 case LAST_BIND:
Al Viro9f1fafe2011-03-25 11:00:12 -04002068 error = complete_walk(nd);
Al Viro16c2cd72011-02-22 15:50:10 -05002069 if (error)
Al Viro9f1fafe2011-03-25 11:00:12 -04002070 return ERR_PTR(error);
Al Viro1f36f772009-12-26 10:56:19 -05002071 audit_inode(pathname, dir);
Al Viro67ee3ad2009-12-26 07:01:01 -05002072 goto ok;
Al Viro1f36f772009-12-26 10:56:19 -05002073 }
Al Viro67ee3ad2009-12-26 07:01:01 -05002074
Al Viroca344a892011-03-09 00:36:45 -05002075 if (!(open_flag & O_CREAT)) {
Al Virobcda7652011-03-13 16:42:14 -04002076 int symlink_ok = 0;
Al Virofe2d35f2011-03-05 22:58:25 -05002077 if (nd->last.name[nd->last.len])
2078 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
Al Virobcda7652011-03-13 16:42:14 -04002079 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2080 symlink_ok = 1;
Al Virofe2d35f2011-03-05 22:58:25 -05002081 /* we _can_ be in RCU mode here */
Al Viroce57dfc2011-03-13 19:58:58 -04002082 error = walk_component(nd, path, &nd->last, LAST_NORM,
2083 !symlink_ok);
2084 if (error < 0)
Al Virofe2d35f2011-03-05 22:58:25 -05002085 return ERR_PTR(error);
Al Viroce57dfc2011-03-13 19:58:58 -04002086 if (error) /* symlink */
Al Virofe2d35f2011-03-05 22:58:25 -05002087 return NULL;
Al Virofe2d35f2011-03-05 22:58:25 -05002088 /* sayonara */
Al Viro9f1fafe2011-03-25 11:00:12 -04002089 error = complete_walk(nd);
2090 if (error)
2091 return ERR_PTR(-ECHILD);
Al Virofe2d35f2011-03-05 22:58:25 -05002092
2093 error = -ENOTDIR;
2094 if (nd->flags & LOOKUP_DIRECTORY) {
Al Viroce57dfc2011-03-13 19:58:58 -04002095 if (!nd->inode->i_op->lookup)
Al Virofe2d35f2011-03-05 22:58:25 -05002096 goto exit;
2097 }
2098 audit_inode(pathname, nd->path.dentry);
2099 goto ok;
2100 }
2101
2102 /* create side of things */
Al Viro9f1fafe2011-03-25 11:00:12 -04002103 error = complete_walk(nd);
2104 if (error)
2105 return ERR_PTR(error);
Al Virofe2d35f2011-03-05 22:58:25 -05002106
2107 audit_inode(pathname, dir);
Al Viro16c2cd72011-02-22 15:50:10 -05002108 error = -EISDIR;
Al Viro1f36f772009-12-26 10:56:19 -05002109 /* trailing slashes? */
Nick Piggin31e6b012011-01-07 17:49:52 +11002110 if (nd->last.name[nd->last.len])
2111 goto exit;
Al Viroa2c36b42009-12-24 03:39:50 -05002112
Al Viroa1e28032009-12-24 02:12:06 -05002113 mutex_lock(&dir->d_inode->i_mutex);
2114
Al Viro6c0d46c2011-03-09 00:59:59 -05002115 dentry = lookup_hash(nd);
2116 error = PTR_ERR(dentry);
2117 if (IS_ERR(dentry)) {
Al Virofb1cc552009-12-24 01:58:28 -05002118 mutex_unlock(&dir->d_inode->i_mutex);
2119 goto exit;
2120 }
2121
Al Viro6c0d46c2011-03-09 00:59:59 -05002122 path->dentry = dentry;
2123 path->mnt = nd->path.mnt;
2124
Al Virofb1cc552009-12-24 01:58:28 -05002125 /* Negative dentry, just create the file */
Al Viro6c0d46c2011-03-09 00:59:59 -05002126 if (!dentry->d_inode) {
2127 int mode = op->mode;
2128 if (!IS_POSIXACL(dir->d_inode))
2129 mode &= ~current_umask();
Al Virofb1cc552009-12-24 01:58:28 -05002130 /*
2131 * This write is needed to ensure that a
Al Viro6c0d46c2011-03-09 00:59:59 -05002132 * rw->ro transition does not occur between
Al Virofb1cc552009-12-24 01:58:28 -05002133 * the time when the file is created and when
2134 * a permanent write count is taken through
2135 * the 'struct file' in nameidata_to_filp().
2136 */
2137 error = mnt_want_write(nd->path.mnt);
2138 if (error)
2139 goto exit_mutex_unlock;
Al Viroca344a892011-03-09 00:36:45 -05002140 want_write = 1;
Al Viro9b44f1b2011-03-09 00:17:27 -05002141 /* Don't check for write permission, don't truncate */
Al Viroca344a892011-03-09 00:36:45 -05002142 open_flag &= ~O_TRUNC;
Al Viro6c0d46c2011-03-09 00:59:59 -05002143 will_truncate = 0;
Al Virobcda7652011-03-13 16:42:14 -04002144 acc_mode = MAY_OPEN;
Al Viro6c0d46c2011-03-09 00:59:59 -05002145 error = security_path_mknod(&nd->path, dentry, mode, 0);
2146 if (error)
2147 goto exit_mutex_unlock;
2148 error = vfs_create(dir->d_inode, dentry, mode, nd);
2149 if (error)
2150 goto exit_mutex_unlock;
2151 mutex_unlock(&dir->d_inode->i_mutex);
2152 dput(nd->path.dentry);
2153 nd->path.dentry = dentry;
Al Viroca344a892011-03-09 00:36:45 -05002154 goto common;
Al Virofb1cc552009-12-24 01:58:28 -05002155 }
2156
2157 /*
2158 * It already exists.
2159 */
2160 mutex_unlock(&dir->d_inode->i_mutex);
2161 audit_inode(pathname, path->dentry);
2162
2163 error = -EEXIST;
Al Viroca344a892011-03-09 00:36:45 -05002164 if (open_flag & O_EXCL)
Al Virofb1cc552009-12-24 01:58:28 -05002165 goto exit_dput;
2166
David Howells9875cf82011-01-14 18:45:21 +00002167 error = follow_managed(path, nd->flags);
2168 if (error < 0)
2169 goto exit_dput;
Al Virofb1cc552009-12-24 01:58:28 -05002170
2171 error = -ENOENT;
2172 if (!path->dentry->d_inode)
2173 goto exit_dput;
Al Viro9e67f362009-12-26 07:04:50 -05002174
2175 if (path->dentry->d_inode->i_op->follow_link)
Al Virofb1cc552009-12-24 01:58:28 -05002176 return NULL;
Al Virofb1cc552009-12-24 01:58:28 -05002177
2178 path_to_nameidata(path, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11002179 nd->inode = path->dentry->d_inode;
Al Virofb1cc552009-12-24 01:58:28 -05002180 error = -EISDIR;
Nick Piggin31e6b012011-01-07 17:49:52 +11002181 if (S_ISDIR(nd->inode->i_mode))
Al Virofb1cc552009-12-24 01:58:28 -05002182 goto exit;
Al Viro67ee3ad2009-12-26 07:01:01 -05002183ok:
Al Viro6c0d46c2011-03-09 00:59:59 -05002184 if (!S_ISREG(nd->inode->i_mode))
2185 will_truncate = 0;
2186
Al Viro0f9d1a12011-03-09 00:13:14 -05002187 if (will_truncate) {
2188 error = mnt_want_write(nd->path.mnt);
2189 if (error)
2190 goto exit;
Al Viroca344a892011-03-09 00:36:45 -05002191 want_write = 1;
Al Viro0f9d1a12011-03-09 00:13:14 -05002192 }
Al Viroca344a892011-03-09 00:36:45 -05002193common:
Al Virobcda7652011-03-13 16:42:14 -04002194 error = may_open(&nd->path, acc_mode, open_flag);
Al Viroca344a892011-03-09 00:36:45 -05002195 if (error)
Al Viro0f9d1a12011-03-09 00:13:14 -05002196 goto exit;
Al Viro0f9d1a12011-03-09 00:13:14 -05002197 filp = nameidata_to_filp(nd);
2198 if (!IS_ERR(filp)) {
2199 error = ima_file_check(filp, op->acc_mode);
2200 if (error) {
2201 fput(filp);
2202 filp = ERR_PTR(error);
2203 }
2204 }
2205 if (!IS_ERR(filp)) {
2206 if (will_truncate) {
2207 error = handle_truncate(filp);
2208 if (error) {
2209 fput(filp);
2210 filp = ERR_PTR(error);
2211 }
2212 }
2213 }
Al Viroca344a892011-03-09 00:36:45 -05002214out:
2215 if (want_write)
Al Viro0f9d1a12011-03-09 00:13:14 -05002216 mnt_drop_write(nd->path.mnt);
2217 path_put(&nd->path);
Al Virofb1cc552009-12-24 01:58:28 -05002218 return filp;
2219
2220exit_mutex_unlock:
2221 mutex_unlock(&dir->d_inode->i_mutex);
2222exit_dput:
2223 path_put_conditional(path, nd);
2224exit:
Al Viroca344a892011-03-09 00:36:45 -05002225 filp = ERR_PTR(error);
2226 goto out;
Al Virofb1cc552009-12-24 01:58:28 -05002227}
2228
Al Viro13aab422011-02-23 17:54:08 -05002229static struct file *path_openat(int dfd, const char *pathname,
Al Viro73d049a2011-03-11 12:08:24 -05002230 struct nameidata *nd, const struct open_flags *op, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231{
Al Virofe2d35f2011-03-05 22:58:25 -05002232 struct file *base = NULL;
Dave Hansen4a3fd212008-02-15 14:37:48 -08002233 struct file *filp;
Al Viro9850c052010-01-13 15:01:15 -05002234 struct path path;
Al Viro13aab422011-02-23 17:54:08 -05002235 int error;
Nick Piggin31e6b012011-01-07 17:49:52 +11002236
2237 filp = get_empty_filp();
2238 if (!filp)
2239 return ERR_PTR(-ENFILE);
2240
Al Viro47c805d2011-02-23 17:44:09 -05002241 filp->f_flags = op->open_flag;
Al Viro73d049a2011-03-11 12:08:24 -05002242 nd->intent.open.file = filp;
2243 nd->intent.open.flags = open_to_namei_flags(op->open_flag);
2244 nd->intent.open.create_mode = op->mode;
Nick Piggin31e6b012011-01-07 17:49:52 +11002245
Al Viro73d049a2011-03-11 12:08:24 -05002246 error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
Nick Piggin31e6b012011-01-07 17:49:52 +11002247 if (unlikely(error))
Al Viro13aab422011-02-23 17:54:08 -05002248 goto out_filp;
Nick Piggin31e6b012011-01-07 17:49:52 +11002249
Al Virofe2d35f2011-03-05 22:58:25 -05002250 current->total_link_count = 0;
Al Viro73d049a2011-03-11 12:08:24 -05002251 error = link_path_walk(pathname, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11002252 if (unlikely(error))
2253 goto out_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
Al Viro73d049a2011-03-11 12:08:24 -05002255 filp = do_last(nd, &path, op, pathname);
Al Viro806b6812009-12-26 07:16:40 -05002256 while (unlikely(!filp)) { /* trailing symlink */
Nick Piggin7b9337a2011-01-14 08:42:43 +00002257 struct path link = path;
Al Virodef4af32009-12-26 08:37:05 -05002258 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04002259 if (!(nd->flags & LOOKUP_FOLLOW)) {
Al Viro73d049a2011-03-11 12:08:24 -05002260 path_put_conditional(&path, nd);
2261 path_put(&nd->path);
Al Viro40b39132011-03-09 16:22:18 -05002262 filp = ERR_PTR(-ELOOP);
2263 break;
2264 }
Al Viro73d049a2011-03-11 12:08:24 -05002265 nd->flags |= LOOKUP_PARENT;
2266 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
Al Viro574197e2011-03-14 22:20:34 -04002267 error = follow_link(&link, nd, &cookie);
Al Viroc3e380b2011-02-23 13:39:45 -05002268 if (unlikely(error))
Al Virof1afe9e2011-02-22 22:27:28 -05002269 filp = ERR_PTR(error);
Al Viroc3e380b2011-02-23 13:39:45 -05002270 else
Al Viro73d049a2011-03-11 12:08:24 -05002271 filp = do_last(nd, &path, op, pathname);
Al Viro574197e2011-03-14 22:20:34 -04002272 put_link(nd, &link, cookie);
Al Viro806b6812009-12-26 07:16:40 -05002273 }
Al Viro10fa8e62009-12-26 07:09:49 -05002274out:
Al Viro73d049a2011-03-11 12:08:24 -05002275 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
2276 path_put(&nd->root);
Al Virofe2d35f2011-03-05 22:58:25 -05002277 if (base)
2278 fput(base);
Al Viro73d049a2011-03-11 12:08:24 -05002279 release_open_intent(nd);
Al Viro10fa8e62009-12-26 07:09:49 -05002280 return filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
Nick Piggin31e6b012011-01-07 17:49:52 +11002282out_filp:
Al Viro806b6812009-12-26 07:16:40 -05002283 filp = ERR_PTR(error);
Al Viro10fa8e62009-12-26 07:09:49 -05002284 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285}
2286
Al Viro13aab422011-02-23 17:54:08 -05002287struct file *do_filp_open(int dfd, const char *pathname,
2288 const struct open_flags *op, int flags)
2289{
Al Viro73d049a2011-03-11 12:08:24 -05002290 struct nameidata nd;
Al Viro13aab422011-02-23 17:54:08 -05002291 struct file *filp;
2292
Al Viro73d049a2011-03-11 12:08:24 -05002293 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
Al Viro13aab422011-02-23 17:54:08 -05002294 if (unlikely(filp == ERR_PTR(-ECHILD)))
Al Viro73d049a2011-03-11 12:08:24 -05002295 filp = path_openat(dfd, pathname, &nd, op, flags);
Al Viro13aab422011-02-23 17:54:08 -05002296 if (unlikely(filp == ERR_PTR(-ESTALE)))
Al Viro73d049a2011-03-11 12:08:24 -05002297 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
Al Viro13aab422011-02-23 17:54:08 -05002298 return filp;
2299}
2300
Al Viro73d049a2011-03-11 12:08:24 -05002301struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
2302 const char *name, const struct open_flags *op, int flags)
2303{
2304 struct nameidata nd;
2305 struct file *file;
2306
2307 nd.root.mnt = mnt;
2308 nd.root.dentry = dentry;
2309
2310 flags |= LOOKUP_ROOT;
2311
Al Virobcda7652011-03-13 16:42:14 -04002312 if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
Al Viro73d049a2011-03-11 12:08:24 -05002313 return ERR_PTR(-ELOOP);
2314
2315 file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
2316 if (unlikely(file == ERR_PTR(-ECHILD)))
2317 file = path_openat(-1, name, &nd, op, flags);
2318 if (unlikely(file == ERR_PTR(-ESTALE)))
2319 file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
2320 return file;
2321}
2322
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323/**
2324 * lookup_create - lookup a dentry, creating it if it doesn't exist
2325 * @nd: nameidata info
2326 * @is_dir: directory flag
2327 *
2328 * Simple function to lookup and return a dentry and create it
2329 * if it doesn't exist. Is SMP-safe.
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002330 *
Jan Blunck4ac91372008-02-14 19:34:32 -08002331 * Returns with nd->path.dentry->d_inode->i_mutex locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 */
2333struct dentry *lookup_create(struct nameidata *nd, int is_dir)
2334{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002335 struct dentry *dentry = ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
Jan Blunck4ac91372008-02-14 19:34:32 -08002337 mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002338 /*
2339 * Yucky last component or no last component at all?
2340 * (foo/., foo/.., /////)
2341 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 if (nd->last_type != LAST_NORM)
2343 goto fail;
2344 nd->flags &= ~LOOKUP_PARENT;
Al Viro35165862008-08-05 03:00:49 -04002345 nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
ASANO Masahiroa6349042006-08-22 20:06:02 -04002346 nd->intent.open.flags = O_EXCL;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002347
2348 /*
2349 * Do the final lookup.
2350 */
Christoph Hellwig49705b72005-11-08 21:35:06 -08002351 dentry = lookup_hash(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 if (IS_ERR(dentry))
2353 goto fail;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002354
Al Viroe9baf6e2008-05-15 04:49:12 -04002355 if (dentry->d_inode)
2356 goto eexist;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002357 /*
2358 * Special case - lookup gave negative, but... we had foo/bar/
2359 * From the vfs_mknod() POV we just have a negative dentry -
2360 * all is fine. Let's be bastards - you had / on the end, you've
2361 * been asking for (non-existent) directory. -ENOENT for you.
2362 */
Al Viroe9baf6e2008-05-15 04:49:12 -04002363 if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
2364 dput(dentry);
2365 dentry = ERR_PTR(-ENOENT);
2366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 return dentry;
Al Viroe9baf6e2008-05-15 04:49:12 -04002368eexist:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 dput(dentry);
Al Viroe9baf6e2008-05-15 04:49:12 -04002370 dentry = ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371fail:
2372 return dentry;
2373}
Christoph Hellwigf81a0bf2005-05-19 12:26:43 -07002374EXPORT_SYMBOL_GPL(lookup_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
2376int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
2377{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002378 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
2380 if (error)
2381 return error;
2382
Serge E. Hallyne795b712011-03-23 16:43:25 -07002383 if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
2384 !ns_capable(inode_userns(dir), CAP_MKNOD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 return -EPERM;
2386
Al Viroacfa4382008-12-04 10:06:33 -05002387 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 return -EPERM;
2389
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07002390 error = devcgroup_inode_mknod(mode, dev);
2391 if (error)
2392 return error;
2393
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 error = security_inode_mknod(dir, dentry, mode, dev);
2395 if (error)
2396 return error;
2397
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002399 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002400 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 return error;
2402}
2403
Dave Hansen463c3192008-02-15 14:37:57 -08002404static int may_mknod(mode_t mode)
2405{
2406 switch (mode & S_IFMT) {
2407 case S_IFREG:
2408 case S_IFCHR:
2409 case S_IFBLK:
2410 case S_IFIFO:
2411 case S_IFSOCK:
2412 case 0: /* zero mode translates to S_IFREG */
2413 return 0;
2414 case S_IFDIR:
2415 return -EPERM;
2416 default:
2417 return -EINVAL;
2418 }
2419}
2420
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002421SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
2422 unsigned, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423{
Al Viro2ad94ae2008-07-21 09:32:51 -04002424 int error;
2425 char *tmp;
2426 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 struct nameidata nd;
2428
2429 if (S_ISDIR(mode))
2430 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
Al Viro2ad94ae2008-07-21 09:32:51 -04002432 error = user_path_parent(dfd, filename, &nd, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002434 return error;
2435
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 dentry = lookup_create(&nd, 0);
Dave Hansen463c3192008-02-15 14:37:57 -08002437 if (IS_ERR(dentry)) {
2438 error = PTR_ERR(dentry);
2439 goto out_unlock;
2440 }
Jan Blunck4ac91372008-02-14 19:34:32 -08002441 if (!IS_POSIXACL(nd.path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04002442 mode &= ~current_umask();
Dave Hansen463c3192008-02-15 14:37:57 -08002443 error = may_mknod(mode);
2444 if (error)
2445 goto out_dput;
2446 error = mnt_want_write(nd.path.mnt);
2447 if (error)
2448 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002449 error = security_path_mknod(&nd.path, dentry, mode, dev);
2450 if (error)
2451 goto out_drop_write;
Dave Hansen463c3192008-02-15 14:37:57 -08002452 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 case 0: case S_IFREG:
Jan Blunck4ac91372008-02-14 19:34:32 -08002454 error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 break;
2456 case S_IFCHR: case S_IFBLK:
Jan Blunck4ac91372008-02-14 19:34:32 -08002457 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 new_decode_dev(dev));
2459 break;
2460 case S_IFIFO: case S_IFSOCK:
Jan Blunck4ac91372008-02-14 19:34:32 -08002461 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 }
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002464out_drop_write:
Dave Hansen463c3192008-02-15 14:37:57 -08002465 mnt_drop_write(nd.path.mnt);
2466out_dput:
2467 dput(dentry);
2468out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002469 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002470 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 putname(tmp);
2472
2473 return error;
2474}
2475
Heiko Carstens3480b252009-01-14 14:14:16 +01002476SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002477{
2478 return sys_mknodat(AT_FDCWD, filename, mode, dev);
2479}
2480
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2482{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002483 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
2485 if (error)
2486 return error;
2487
Al Viroacfa4382008-12-04 10:06:33 -05002488 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 return -EPERM;
2490
2491 mode &= (S_IRWXUGO|S_ISVTX);
2492 error = security_inode_mkdir(dir, dentry, mode);
2493 if (error)
2494 return error;
2495
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002497 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002498 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 return error;
2500}
2501
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002502SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503{
2504 int error = 0;
2505 char * tmp;
Dave Hansen6902d922006-09-30 23:29:01 -07002506 struct dentry *dentry;
2507 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
Al Viro2ad94ae2008-07-21 09:32:51 -04002509 error = user_path_parent(dfd, pathname, &nd, &tmp);
2510 if (error)
Dave Hansen6902d922006-09-30 23:29:01 -07002511 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
Dave Hansen6902d922006-09-30 23:29:01 -07002513 dentry = lookup_create(&nd, 1);
2514 error = PTR_ERR(dentry);
2515 if (IS_ERR(dentry))
2516 goto out_unlock;
2517
Jan Blunck4ac91372008-02-14 19:34:32 -08002518 if (!IS_POSIXACL(nd.path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04002519 mode &= ~current_umask();
Dave Hansen463c3192008-02-15 14:37:57 -08002520 error = mnt_want_write(nd.path.mnt);
2521 if (error)
2522 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002523 error = security_path_mkdir(&nd.path, dentry, mode);
2524 if (error)
2525 goto out_drop_write;
Jan Blunck4ac91372008-02-14 19:34:32 -08002526 error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002527out_drop_write:
Dave Hansen463c3192008-02-15 14:37:57 -08002528 mnt_drop_write(nd.path.mnt);
2529out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002530 dput(dentry);
2531out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002532 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002533 path_put(&nd.path);
Dave Hansen6902d922006-09-30 23:29:01 -07002534 putname(tmp);
2535out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 return error;
2537}
2538
Heiko Carstens3cdad422009-01-14 14:14:22 +01002539SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002540{
2541 return sys_mkdirat(AT_FDCWD, pathname, mode);
2542}
2543
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544/*
Sage Weila71905f2011-05-24 13:06:08 -07002545 * The dentry_unhash() helper will try to drop the dentry early: we
2546 * should have a usage count of 2 if we're the only user of this
2547 * dentry, and if that is true (possibly after pruning the dcache),
2548 * then we drop the dentry now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 *
2550 * A low-level filesystem can, if it choses, legally
2551 * do a
2552 *
2553 * if (!d_unhashed(dentry))
2554 * return -EBUSY;
2555 *
2556 * if it cannot handle the case of removing a directory
2557 * that is still in use by something else..
2558 */
2559void dentry_unhash(struct dentry *dentry)
2560{
Vasily Averindc168422006-12-06 20:37:07 -08002561 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 spin_lock(&dentry->d_lock);
Sage Weil64252c72011-05-24 13:06:05 -07002563 if (dentry->d_count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 __d_drop(dentry);
2565 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566}
2567
2568int vfs_rmdir(struct inode *dir, struct dentry *dentry)
2569{
2570 int error = may_delete(dir, dentry, 1);
2571
2572 if (error)
2573 return error;
2574
Al Viroacfa4382008-12-04 10:06:33 -05002575 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 return -EPERM;
2577
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002578 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579
Sage Weil912dbc12011-05-24 13:06:11 -07002580 error = -EBUSY;
2581 if (d_mountpoint(dentry))
2582 goto out;
2583
2584 error = security_inode_rmdir(dir, dentry);
2585 if (error)
2586 goto out;
2587
Sage Weil3cebde22011-05-29 21:20:59 -07002588 shrink_dcache_parent(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07002589 error = dir->i_op->rmdir(dir, dentry);
2590 if (error)
2591 goto out;
2592
2593 dentry->d_inode->i_flags |= S_DEAD;
2594 dont_mount(dentry);
2595
2596out:
2597 mutex_unlock(&dentry->d_inode->i_mutex);
2598 if (!error)
2599 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 return error;
2601}
2602
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002603static long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604{
2605 int error = 0;
2606 char * name;
2607 struct dentry *dentry;
2608 struct nameidata nd;
2609
Al Viro2ad94ae2008-07-21 09:32:51 -04002610 error = user_path_parent(dfd, pathname, &nd, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002612 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613
2614 switch(nd.last_type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002615 case LAST_DOTDOT:
2616 error = -ENOTEMPTY;
2617 goto exit1;
2618 case LAST_DOT:
2619 error = -EINVAL;
2620 goto exit1;
2621 case LAST_ROOT:
2622 error = -EBUSY;
2623 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002625
2626 nd.flags &= ~LOOKUP_PARENT;
2627
Jan Blunck4ac91372008-02-14 19:34:32 -08002628 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002629 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002631 if (IS_ERR(dentry))
2632 goto exit2;
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04002633 if (!dentry->d_inode) {
2634 error = -ENOENT;
2635 goto exit3;
2636 }
Dave Hansen06227532008-02-15 14:37:34 -08002637 error = mnt_want_write(nd.path.mnt);
2638 if (error)
2639 goto exit3;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002640 error = security_path_rmdir(&nd.path, dentry);
2641 if (error)
2642 goto exit4;
Jan Blunck4ac91372008-02-14 19:34:32 -08002643 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002644exit4:
Dave Hansen06227532008-02-15 14:37:34 -08002645 mnt_drop_write(nd.path.mnt);
2646exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07002647 dput(dentry);
2648exit2:
Jan Blunck4ac91372008-02-14 19:34:32 -08002649 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002651 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 putname(name);
2653 return error;
2654}
2655
Heiko Carstens3cdad422009-01-14 14:14:22 +01002656SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002657{
2658 return do_rmdir(AT_FDCWD, pathname);
2659}
2660
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661int vfs_unlink(struct inode *dir, struct dentry *dentry)
2662{
2663 int error = may_delete(dir, dentry, 0);
2664
2665 if (error)
2666 return error;
2667
Al Viroacfa4382008-12-04 10:06:33 -05002668 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 return -EPERM;
2670
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002671 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 if (d_mountpoint(dentry))
2673 error = -EBUSY;
2674 else {
2675 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05002676 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 error = dir->i_op->unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05002678 if (!error)
Al Virod83c49f2010-04-30 17:17:09 -04002679 dont_mount(dentry);
Al Virobec10522010-03-03 14:12:08 -05002680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002682 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683
2684 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2685 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
Jan Karaece95912008-02-06 01:37:13 -08002686 fsnotify_link_count(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 }
Robert Love0eeca282005-07-12 17:06:03 -04002689
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 return error;
2691}
2692
2693/*
2694 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002695 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 * writeout happening, and we don't want to prevent access to the directory
2697 * while waiting on the I/O.
2698 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002699static long do_unlinkat(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700{
Al Viro2ad94ae2008-07-21 09:32:51 -04002701 int error;
2702 char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 struct dentry *dentry;
2704 struct nameidata nd;
2705 struct inode *inode = NULL;
2706
Al Viro2ad94ae2008-07-21 09:32:51 -04002707 error = user_path_parent(dfd, pathname, &nd, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002709 return error;
2710
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 error = -EISDIR;
2712 if (nd.last_type != LAST_NORM)
2713 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002714
2715 nd.flags &= ~LOOKUP_PARENT;
2716
Jan Blunck4ac91372008-02-14 19:34:32 -08002717 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002718 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 error = PTR_ERR(dentry);
2720 if (!IS_ERR(dentry)) {
2721 /* Why not before? Because we want correct error value */
Török Edwin50338b82011-06-16 00:06:14 +03002722 if (nd.last.name[nd.last.len])
2723 goto slashes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 inode = dentry->d_inode;
Török Edwin50338b82011-06-16 00:06:14 +03002725 if (!inode)
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04002726 goto slashes;
2727 ihold(inode);
Dave Hansen06227532008-02-15 14:37:34 -08002728 error = mnt_want_write(nd.path.mnt);
2729 if (error)
2730 goto exit2;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002731 error = security_path_unlink(&nd.path, dentry);
2732 if (error)
2733 goto exit3;
Jan Blunck4ac91372008-02-14 19:34:32 -08002734 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002735exit3:
Dave Hansen06227532008-02-15 14:37:34 -08002736 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 exit2:
2738 dput(dentry);
2739 }
Jan Blunck4ac91372008-02-14 19:34:32 -08002740 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 if (inode)
2742 iput(inode); /* truncate the inode here */
2743exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002744 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 putname(name);
2746 return error;
2747
2748slashes:
2749 error = !dentry->d_inode ? -ENOENT :
2750 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2751 goto exit2;
2752}
2753
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002754SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002755{
2756 if ((flag & ~AT_REMOVEDIR) != 0)
2757 return -EINVAL;
2758
2759 if (flag & AT_REMOVEDIR)
2760 return do_rmdir(dfd, pathname);
2761
2762 return do_unlinkat(dfd, pathname);
2763}
2764
Heiko Carstens3480b252009-01-14 14:14:16 +01002765SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002766{
2767 return do_unlinkat(AT_FDCWD, pathname);
2768}
2769
Miklos Szeredidb2e7472008-06-24 16:50:16 +02002770int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002772 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773
2774 if (error)
2775 return error;
2776
Al Viroacfa4382008-12-04 10:06:33 -05002777 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 return -EPERM;
2779
2780 error = security_inode_symlink(dir, dentry, oldname);
2781 if (error)
2782 return error;
2783
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002785 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002786 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 return error;
2788}
2789
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002790SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
2791 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792{
Al Viro2ad94ae2008-07-21 09:32:51 -04002793 int error;
2794 char *from;
2795 char *to;
Dave Hansen6902d922006-09-30 23:29:01 -07002796 struct dentry *dentry;
2797 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798
2799 from = getname(oldname);
Al Viro2ad94ae2008-07-21 09:32:51 -04002800 if (IS_ERR(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 return PTR_ERR(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04002802
2803 error = user_path_parent(newdfd, newname, &nd, &to);
2804 if (error)
Dave Hansen6902d922006-09-30 23:29:01 -07002805 goto out_putname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806
Dave Hansen6902d922006-09-30 23:29:01 -07002807 dentry = lookup_create(&nd, 0);
2808 error = PTR_ERR(dentry);
2809 if (IS_ERR(dentry))
2810 goto out_unlock;
2811
Dave Hansen75c3f292008-02-15 14:37:45 -08002812 error = mnt_want_write(nd.path.mnt);
2813 if (error)
2814 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002815 error = security_path_symlink(&nd.path, dentry, from);
2816 if (error)
2817 goto out_drop_write;
Miklos Szeredidb2e7472008-06-24 16:50:16 +02002818 error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002819out_drop_write:
Dave Hansen75c3f292008-02-15 14:37:45 -08002820 mnt_drop_write(nd.path.mnt);
2821out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002822 dput(dentry);
2823out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002824 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002825 path_put(&nd.path);
Dave Hansen6902d922006-09-30 23:29:01 -07002826 putname(to);
2827out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 putname(from);
2829 return error;
2830}
2831
Heiko Carstens3480b252009-01-14 14:14:16 +01002832SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002833{
2834 return sys_symlinkat(oldname, AT_FDCWD, newname);
2835}
2836
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2838{
2839 struct inode *inode = old_dentry->d_inode;
2840 int error;
2841
2842 if (!inode)
2843 return -ENOENT;
2844
Miklos Szeredia95164d2008-07-30 15:08:48 +02002845 error = may_create(dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 if (error)
2847 return error;
2848
2849 if (dir->i_sb != inode->i_sb)
2850 return -EXDEV;
2851
2852 /*
2853 * A link to an append-only or immutable file cannot be created.
2854 */
2855 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2856 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05002857 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002859 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 return -EPERM;
2861
2862 error = security_inode_link(old_dentry, dir, new_dentry);
2863 if (error)
2864 return error;
2865
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002866 mutex_lock(&inode->i_mutex);
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05302867 /* Make sure we don't allow creating hardlink to an unlinked file */
2868 if (inode->i_nlink == 0)
2869 error = -ENOENT;
2870 else
2871 error = dir->i_op->link(old_dentry, dir, new_dentry);
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002872 mutex_unlock(&inode->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07002873 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002874 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 return error;
2876}
2877
2878/*
2879 * Hardlinks are often used in delicate situations. We avoid
2880 * security-related surprises by not following symlinks on the
2881 * newname. --KAB
2882 *
2883 * We don't follow them on the oldname either to be compatible
2884 * with linux 2.0, and to avoid hard-linking to directories
2885 * and other special files. --ADM
2886 */
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002887SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
2888 int, newdfd, const char __user *, newname, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889{
2890 struct dentry *new_dentry;
Al Viro2d8f3032008-07-22 09:59:21 -04002891 struct nameidata nd;
2892 struct path old_path;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05302893 int how = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 int error;
Al Viro2ad94ae2008-07-21 09:32:51 -04002895 char *to;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05302897 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002898 return -EINVAL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05302899 /*
2900 * To use null names we require CAP_DAC_READ_SEARCH
2901 * This ensures that not everyone will be able to create
2902 * handlink using the passed filedescriptor.
2903 */
2904 if (flags & AT_EMPTY_PATH) {
2905 if (!capable(CAP_DAC_READ_SEARCH))
2906 return -ENOENT;
2907 how = LOOKUP_EMPTY;
2908 }
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002909
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05302910 if (flags & AT_SYMLINK_FOLLOW)
2911 how |= LOOKUP_FOLLOW;
2912
2913 error = user_path_at(olddfd, oldname, how, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002915 return error;
2916
2917 error = user_path_parent(newdfd, newname, &nd, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 if (error)
2919 goto out;
2920 error = -EXDEV;
Al Viro2d8f3032008-07-22 09:59:21 -04002921 if (old_path.mnt != nd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 goto out_release;
2923 new_dentry = lookup_create(&nd, 0);
2924 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002925 if (IS_ERR(new_dentry))
2926 goto out_unlock;
Dave Hansen75c3f292008-02-15 14:37:45 -08002927 error = mnt_want_write(nd.path.mnt);
2928 if (error)
2929 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002930 error = security_path_link(old_path.dentry, &nd.path, new_dentry);
2931 if (error)
2932 goto out_drop_write;
Al Viro2d8f3032008-07-22 09:59:21 -04002933 error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002934out_drop_write:
Dave Hansen75c3f292008-02-15 14:37:45 -08002935 mnt_drop_write(nd.path.mnt);
2936out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002937 dput(new_dentry);
2938out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002939 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940out_release:
Jan Blunck1d957f92008-02-14 19:34:35 -08002941 path_put(&nd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04002942 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943out:
Al Viro2d8f3032008-07-22 09:59:21 -04002944 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945
2946 return error;
2947}
2948
Heiko Carstens3480b252009-01-14 14:14:16 +01002949SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002950{
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002951 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002952}
2953
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954/*
2955 * The worst of all namespace operations - renaming directory. "Perverted"
2956 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2957 * Problems:
2958 * a) we can get into loop creation. Check is done in is_subdir().
2959 * b) race potential - two innocent renames can create a loop together.
2960 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002961 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 * story.
2963 * c) we have to lock _three_ objects - parents and victim (if it exists).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002964 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 * whether the target exists). Solution: try to be smart with locking
2966 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002967 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 * move will be locked. Thus we can rank directories by the tree
2969 * (ancestors first) and rank all non-directories after them.
2970 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002971 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 * HOWEVER, it relies on the assumption that any object with ->lookup()
2973 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2974 * we'd better make sure that there's no link(2) for them.
Sage Weile4eaac02011-05-24 13:06:07 -07002975 * d) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002976 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05002978 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 * locking].
2980 */
Adrian Bunk75c96f82005-05-05 16:16:09 -07002981static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
2982 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983{
2984 int error = 0;
Sage Weil9055cba2011-05-24 13:06:12 -07002985 struct inode *target = new_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986
2987 /*
2988 * If we are going to change the parent - check write permissions,
2989 * we'll need to flip '..'.
2990 */
2991 if (new_dir != old_dir) {
Al Virof419a2e2008-07-22 00:07:17 -04002992 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 if (error)
2994 return error;
2995 }
2996
2997 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2998 if (error)
2999 return error;
3000
Al Virod83c49f2010-04-30 17:17:09 -04003001 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003002 mutex_lock(&target->i_mutex);
Sage Weil9055cba2011-05-24 13:06:12 -07003003
3004 error = -EBUSY;
3005 if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
3006 goto out;
3007
Sage Weil3cebde22011-05-29 21:20:59 -07003008 if (target)
3009 shrink_dcache_parent(new_dentry);
Sage Weil9055cba2011-05-24 13:06:12 -07003010 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3011 if (error)
3012 goto out;
3013
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 if (target) {
Sage Weil9055cba2011-05-24 13:06:12 -07003015 target->i_flags |= S_DEAD;
3016 dont_mount(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 }
Sage Weil9055cba2011-05-24 13:06:12 -07003018out:
3019 if (target)
3020 mutex_unlock(&target->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07003021 if (!error)
Mark Fasheh349457c2006-09-08 14:22:21 -07003022 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3023 d_move(old_dentry,new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 return error;
3025}
3026
Adrian Bunk75c96f82005-05-05 16:16:09 -07003027static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
3028 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029{
Sage Weil51892bb2011-05-24 13:06:13 -07003030 struct inode *target = new_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 int error;
3032
3033 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3034 if (error)
3035 return error;
3036
3037 dget(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003039 mutex_lock(&target->i_mutex);
Sage Weil51892bb2011-05-24 13:06:13 -07003040
3041 error = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
Sage Weil51892bb2011-05-24 13:06:13 -07003043 goto out;
3044
3045 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3046 if (error)
3047 goto out;
3048
3049 if (target)
3050 dont_mount(new_dentry);
3051 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3052 d_move(old_dentry, new_dentry);
3053out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003055 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 dput(new_dentry);
3057 return error;
3058}
3059
3060int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
3061 struct inode *new_dir, struct dentry *new_dentry)
3062{
3063 int error;
3064 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
Eric Paris59b0df22010-02-08 12:53:52 -05003065 const unsigned char *old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066
3067 if (old_dentry->d_inode == new_dentry->d_inode)
3068 return 0;
3069
3070 error = may_delete(old_dir, old_dentry, is_dir);
3071 if (error)
3072 return error;
3073
3074 if (!new_dentry->d_inode)
Miklos Szeredia95164d2008-07-30 15:08:48 +02003075 error = may_create(new_dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 else
3077 error = may_delete(new_dir, new_dentry, is_dir);
3078 if (error)
3079 return error;
3080
Al Viroacfa4382008-12-04 10:06:33 -05003081 if (!old_dir->i_op->rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 return -EPERM;
3083
Robert Love0eeca282005-07-12 17:06:03 -04003084 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
3085
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 if (is_dir)
3087 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
3088 else
3089 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
Al Viro123df292009-12-25 04:57:57 -05003090 if (!error)
3091 fsnotify_move(old_dir, new_dir, old_name, is_dir,
Al Viro5a190ae2007-06-07 12:19:32 -04003092 new_dentry->d_inode, old_dentry);
Robert Love0eeca282005-07-12 17:06:03 -04003093 fsnotify_oldname_free(old_name);
3094
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 return error;
3096}
3097
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003098SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
3099 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100{
Al Viro2ad94ae2008-07-21 09:32:51 -04003101 struct dentry *old_dir, *new_dir;
3102 struct dentry *old_dentry, *new_dentry;
3103 struct dentry *trap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 struct nameidata oldnd, newnd;
Al Viro2ad94ae2008-07-21 09:32:51 -04003105 char *from;
3106 char *to;
3107 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108
Al Viro2ad94ae2008-07-21 09:32:51 -04003109 error = user_path_parent(olddfd, oldname, &oldnd, &from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 if (error)
3111 goto exit;
3112
Al Viro2ad94ae2008-07-21 09:32:51 -04003113 error = user_path_parent(newdfd, newname, &newnd, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 if (error)
3115 goto exit1;
3116
3117 error = -EXDEV;
Jan Blunck4ac91372008-02-14 19:34:32 -08003118 if (oldnd.path.mnt != newnd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 goto exit2;
3120
Jan Blunck4ac91372008-02-14 19:34:32 -08003121 old_dir = oldnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 error = -EBUSY;
3123 if (oldnd.last_type != LAST_NORM)
3124 goto exit2;
3125
Jan Blunck4ac91372008-02-14 19:34:32 -08003126 new_dir = newnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 if (newnd.last_type != LAST_NORM)
3128 goto exit2;
3129
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003130 oldnd.flags &= ~LOOKUP_PARENT;
3131 newnd.flags &= ~LOOKUP_PARENT;
OGAWA Hirofumi4e9ed2f2008-10-16 07:50:29 +09003132 newnd.flags |= LOOKUP_RENAME_TARGET;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003133
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 trap = lock_rename(new_dir, old_dir);
3135
Christoph Hellwig49705b72005-11-08 21:35:06 -08003136 old_dentry = lookup_hash(&oldnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 error = PTR_ERR(old_dentry);
3138 if (IS_ERR(old_dentry))
3139 goto exit3;
3140 /* source must exist */
3141 error = -ENOENT;
3142 if (!old_dentry->d_inode)
3143 goto exit4;
3144 /* unless the source is a directory trailing slashes give -ENOTDIR */
3145 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
3146 error = -ENOTDIR;
3147 if (oldnd.last.name[oldnd.last.len])
3148 goto exit4;
3149 if (newnd.last.name[newnd.last.len])
3150 goto exit4;
3151 }
3152 /* source should not be ancestor of target */
3153 error = -EINVAL;
3154 if (old_dentry == trap)
3155 goto exit4;
Christoph Hellwig49705b72005-11-08 21:35:06 -08003156 new_dentry = lookup_hash(&newnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 error = PTR_ERR(new_dentry);
3158 if (IS_ERR(new_dentry))
3159 goto exit4;
3160 /* target should not be an ancestor of source */
3161 error = -ENOTEMPTY;
3162 if (new_dentry == trap)
3163 goto exit5;
3164
Dave Hansen9079b1e2008-02-15 14:37:49 -08003165 error = mnt_want_write(oldnd.path.mnt);
3166 if (error)
3167 goto exit5;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003168 error = security_path_rename(&oldnd.path, old_dentry,
3169 &newnd.path, new_dentry);
3170 if (error)
3171 goto exit6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 error = vfs_rename(old_dir->d_inode, old_dentry,
3173 new_dir->d_inode, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003174exit6:
Dave Hansen9079b1e2008-02-15 14:37:49 -08003175 mnt_drop_write(oldnd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176exit5:
3177 dput(new_dentry);
3178exit4:
3179 dput(old_dentry);
3180exit3:
3181 unlock_rename(new_dir, old_dir);
3182exit2:
Jan Blunck1d957f92008-02-14 19:34:35 -08003183 path_put(&newnd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04003184 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003186 path_put(&oldnd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 putname(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04003188exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 return error;
3190}
3191
Heiko Carstensa26eab22009-01-14 14:14:17 +01003192SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003193{
3194 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
3195}
3196
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
3198{
3199 int len;
3200
3201 len = PTR_ERR(link);
3202 if (IS_ERR(link))
3203 goto out;
3204
3205 len = strlen(link);
3206 if (len > (unsigned) buflen)
3207 len = buflen;
3208 if (copy_to_user(buffer, link, len))
3209 len = -EFAULT;
3210out:
3211 return len;
3212}
3213
3214/*
3215 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
3216 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
3217 * using) it for any given inode is up to filesystem.
3218 */
3219int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3220{
3221 struct nameidata nd;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003222 void *cookie;
Marcin Slusarz694a1762008-06-09 16:40:37 -07003223 int res;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003224
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 nd.depth = 0;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003226 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
Marcin Slusarz694a1762008-06-09 16:40:37 -07003227 if (IS_ERR(cookie))
3228 return PTR_ERR(cookie);
3229
3230 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
3231 if (dentry->d_inode->i_op->put_link)
3232 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3233 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234}
3235
3236int vfs_follow_link(struct nameidata *nd, const char *link)
3237{
3238 return __vfs_follow_link(nd, link);
3239}
3240
3241/* get the link contents into pagecache */
3242static char *page_getlink(struct dentry * dentry, struct page **ppage)
3243{
Duane Griffinebd09ab2008-12-19 20:47:12 +00003244 char *kaddr;
3245 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 struct address_space *mapping = dentry->d_inode->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -07003247 page = read_mapping_page(mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 if (IS_ERR(page))
Nick Piggin6fe69002007-05-06 14:49:04 -07003249 return (char*)page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 *ppage = page;
Duane Griffinebd09ab2008-12-19 20:47:12 +00003251 kaddr = kmap(page);
3252 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3253 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254}
3255
3256int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3257{
3258 struct page *page = NULL;
3259 char *s = page_getlink(dentry, &page);
3260 int res = vfs_readlink(dentry,buffer,buflen,s);
3261 if (page) {
3262 kunmap(page);
3263 page_cache_release(page);
3264 }
3265 return res;
3266}
3267
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003268void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003270 struct page *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 nd_set_link(nd, page_getlink(dentry, &page));
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003272 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273}
3274
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003275void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003277 struct page *page = cookie;
3278
3279 if (page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 kunmap(page);
3281 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 }
3283}
3284
Nick Piggin54566b22009-01-04 12:00:53 -08003285/*
3286 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
3287 */
3288int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289{
3290 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003291 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07003292 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08003293 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 char *kaddr;
Nick Piggin54566b22009-01-04 12:00:53 -08003295 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
3296 if (nofs)
3297 flags |= AOP_FLAG_NOFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298
NeilBrown7e53cac2006-03-25 03:07:57 -08003299retry:
Nick Pigginafddba42007-10-16 01:25:01 -07003300 err = pagecache_write_begin(NULL, mapping, 0, len-1,
Nick Piggin54566b22009-01-04 12:00:53 -08003301 flags, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07003303 goto fail;
3304
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 kaddr = kmap_atomic(page, KM_USER0);
3306 memcpy(kaddr, symname, len-1);
3307 kunmap_atomic(kaddr, KM_USER0);
Nick Pigginafddba42007-10-16 01:25:01 -07003308
3309 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3310 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 if (err < 0)
3312 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07003313 if (err < len-1)
3314 goto retry;
3315
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 mark_inode_dirty(inode);
3317 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318fail:
3319 return err;
3320}
3321
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003322int page_symlink(struct inode *inode, const char *symname, int len)
3323{
3324 return __page_symlink(inode, symname, len,
Nick Piggin54566b22009-01-04 12:00:53 -08003325 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003326}
3327
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003328const struct inode_operations page_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 .readlink = generic_readlink,
3330 .follow_link = page_follow_link_light,
3331 .put_link = page_put_link,
3332};
3333
Al Viro2d8f3032008-07-22 09:59:21 -04003334EXPORT_SYMBOL(user_path_at);
David Howellscc53ce52011-01-14 18:45:26 +00003335EXPORT_SYMBOL(follow_down_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336EXPORT_SYMBOL(follow_down);
3337EXPORT_SYMBOL(follow_up);
3338EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
3339EXPORT_SYMBOL(getname);
3340EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341EXPORT_SYMBOL(lookup_one_len);
3342EXPORT_SYMBOL(page_follow_link_light);
3343EXPORT_SYMBOL(page_put_link);
3344EXPORT_SYMBOL(page_readlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003345EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346EXPORT_SYMBOL(page_symlink);
3347EXPORT_SYMBOL(page_symlink_inode_operations);
Al Viroc9c6cac2011-02-16 15:15:47 -05003348EXPORT_SYMBOL(kern_path_parent);
Al Virod1811462008-08-02 00:49:18 -04003349EXPORT_SYMBOL(kern_path);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07003350EXPORT_SYMBOL(vfs_path_lookup);
Al Virof419a2e2008-07-22 00:07:17 -04003351EXPORT_SYMBOL(inode_permission);
Christoph Hellwig8c744fb2005-11-08 21:35:04 -08003352EXPORT_SYMBOL(file_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353EXPORT_SYMBOL(unlock_rename);
3354EXPORT_SYMBOL(vfs_create);
3355EXPORT_SYMBOL(vfs_follow_link);
3356EXPORT_SYMBOL(vfs_link);
3357EXPORT_SYMBOL(vfs_mkdir);
3358EXPORT_SYMBOL(vfs_mknod);
3359EXPORT_SYMBOL(generic_permission);
3360EXPORT_SYMBOL(vfs_readlink);
3361EXPORT_SYMBOL(vfs_rename);
3362EXPORT_SYMBOL(vfs_rmdir);
3363EXPORT_SYMBOL(vfs_symlink);
3364EXPORT_SYMBOL(vfs_unlink);
3365EXPORT_SYMBOL(dentry_unhash);
3366EXPORT_SYMBOL(generic_readlink);