blob: 1039cbae0c12707c242b62cc8caead1867f87cfa [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{
182 umode_t mode = inode->i_mode;
183
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.
241 * Executable DACs are overridable if at least one exec bit is set.
242 */
Miklos Szeredif696a362008-07-31 13:41:58 +0200243 if (!(mask & MAY_EXEC) || execute_ok(inode))
Serge E. Hallyne795b712011-03-23 16:43:25 -0700244 if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return 0;
246
247 /*
248 * Searching includes executable on directories, else just read.
249 */
Serge E. Hallyn7ea66002009-12-29 14:50:19 -0600250 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
Serge E. Hallyne795b712011-03-23 16:43:25 -0700252 if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return 0;
254
255 return -EACCES;
256}
257
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200258/**
259 * inode_permission - check for access rights to a given inode
260 * @inode: inode to check permission on
261 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
262 *
263 * Used to check for read/write/execute permissions on an inode.
264 * We use "fsuid" for this, letting us set arbitrary permissions
265 * for filesystem access without changing the "normal" uids which
266 * are used for other things.
267 */
Al Virof419a2e2008-07-22 00:07:17 -0400268int inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
Al Viroe6305c42008-07-15 21:03:57 -0400270 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 if (mask & MAY_WRITE) {
Miklos Szeredi22590e42007-10-16 23:27:08 -0700273 umode_t mode = inode->i_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 /*
276 * Nobody gets write access to a read-only fs.
277 */
278 if (IS_RDONLY(inode) &&
279 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
280 return -EROFS;
281
282 /*
283 * Nobody gets write access to an immutable file.
284 */
285 if (IS_IMMUTABLE(inode))
286 return -EACCES;
287 }
288
Al Viroacfa4382008-12-04 10:06:33 -0500289 if (inode->i_op->permission)
Nick Pigginb74c79e2011-01-07 17:49:58 +1100290 retval = inode->i_op->permission(inode, mask, 0);
Miklos Szeredif696a362008-07-31 13:41:58 +0200291 else
Nick Pigginb74c79e2011-01-07 17:49:58 +1100292 retval = generic_permission(inode, mask, 0,
293 inode->i_op->check_acl);
Miklos Szeredif696a362008-07-31 13:41:58 +0200294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 if (retval)
296 return retval;
297
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700298 retval = devcgroup_inode_permission(inode, mask);
299 if (retval)
300 return retval;
301
Eric Parisd09ca732010-07-23 11:43:57 -0400302 return security_inode_permission(inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800305/**
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800306 * file_permission - check for additional access rights to a given file
307 * @file: file to check access rights for
308 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
309 *
310 * Used to check for read/write/execute permissions on an already opened
311 * file.
312 *
313 * Note:
314 * Do not use this function in new code. All access checks should
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200315 * be done using inode_permission().
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800316 */
317int file_permission(struct file *file, int mask)
318{
Al Virof419a2e2008-07-22 00:07:17 -0400319 return inode_permission(file->f_path.dentry->d_inode, mask);
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800320}
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322/*
323 * get_write_access() gets write permission for a file.
324 * put_write_access() releases this write permission.
325 * This is used for regular files.
326 * We cannot support write (and maybe mmap read-write shared) accesses and
327 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
328 * can have the following values:
329 * 0: no writers, no VM_DENYWRITE mappings
330 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
331 * > 0: (i_writecount) users are writing to the file.
332 *
333 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
334 * except for the cases where we don't hold i_writecount yet. Then we need to
335 * use {get,deny}_write_access() - these functions check the sign and refuse
336 * to do the change if sign is wrong. Exclusion between them is provided by
337 * the inode->i_lock spinlock.
338 */
339
340int get_write_access(struct inode * inode)
341{
342 spin_lock(&inode->i_lock);
343 if (atomic_read(&inode->i_writecount) < 0) {
344 spin_unlock(&inode->i_lock);
345 return -ETXTBSY;
346 }
347 atomic_inc(&inode->i_writecount);
348 spin_unlock(&inode->i_lock);
349
350 return 0;
351}
352
353int deny_write_access(struct file * file)
354{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800355 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 spin_lock(&inode->i_lock);
358 if (atomic_read(&inode->i_writecount) > 0) {
359 spin_unlock(&inode->i_lock);
360 return -ETXTBSY;
361 }
362 atomic_dec(&inode->i_writecount);
363 spin_unlock(&inode->i_lock);
364
365 return 0;
366}
367
Jan Blunck1d957f92008-02-14 19:34:35 -0800368/**
Jan Blunck5dd784d2008-02-14 19:34:38 -0800369 * path_get - get a reference to a path
370 * @path: path to get the reference to
371 *
372 * Given a path increment the reference count to the dentry and the vfsmount.
373 */
374void path_get(struct path *path)
375{
376 mntget(path->mnt);
377 dget(path->dentry);
378}
379EXPORT_SYMBOL(path_get);
380
381/**
Jan Blunck1d957f92008-02-14 19:34:35 -0800382 * path_put - put a reference to a path
383 * @path: path to put the reference to
384 *
385 * Given a path decrement the reference count to the dentry and the vfsmount.
386 */
387void path_put(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
Jan Blunck1d957f92008-02-14 19:34:35 -0800389 dput(path->dentry);
390 mntput(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
Jan Blunck1d957f92008-02-14 19:34:35 -0800392EXPORT_SYMBOL(path_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Al Viro19660af2011-03-25 10:32:48 -0400394/*
Nick Piggin31e6b012011-01-07 17:49:52 +1100395 * Path walking has 2 modes, rcu-walk and ref-walk (see
Al Viro19660af2011-03-25 10:32:48 -0400396 * Documentation/filesystems/path-lookup.txt). In situations when we can't
397 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
398 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
399 * mode. Refcounts are grabbed at the last known good point before rcu-walk
400 * got stuck, so ref-walk may continue from there. If this is not successful
401 * (eg. a seqcount has changed), then failure is returned and it's up to caller
402 * to restart the path walk from the beginning in ref-walk mode.
Nick Piggin31e6b012011-01-07 17:49:52 +1100403 */
Nick Piggin31e6b012011-01-07 17:49:52 +1100404
405/**
Al Viro19660af2011-03-25 10:32:48 -0400406 * unlazy_walk - try to switch to ref-walk mode.
407 * @nd: nameidata pathwalk data
408 * @dentry: child of nd->path.dentry or NULL
Randy Dunlap39191622011-01-08 19:36:21 -0800409 * Returns: 0 on success, -ECHILD on failure
Nick Piggin31e6b012011-01-07 17:49:52 +1100410 *
Al Viro19660af2011-03-25 10:32:48 -0400411 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
412 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
413 * @nd or NULL. Must be called from rcu-walk context.
Nick Piggin31e6b012011-01-07 17:49:52 +1100414 */
Al Viro19660af2011-03-25 10:32:48 -0400415static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
Nick Piggin31e6b012011-01-07 17:49:52 +1100416{
417 struct fs_struct *fs = current->fs;
418 struct dentry *parent = nd->path.dentry;
Al Viro5b6ca022011-03-09 23:04:47 -0500419 int want_root = 0;
Nick Piggin31e6b012011-01-07 17:49:52 +1100420
421 BUG_ON(!(nd->flags & LOOKUP_RCU));
Al Viro5b6ca022011-03-09 23:04:47 -0500422 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
423 want_root = 1;
Nick Piggin31e6b012011-01-07 17:49:52 +1100424 spin_lock(&fs->lock);
425 if (nd->root.mnt != fs->root.mnt ||
426 nd->root.dentry != fs->root.dentry)
427 goto err_root;
428 }
429 spin_lock(&parent->d_lock);
Al Viro19660af2011-03-25 10:32:48 -0400430 if (!dentry) {
431 if (!__d_rcu_to_refcount(parent, nd->seq))
432 goto err_parent;
433 BUG_ON(nd->inode != parent->d_inode);
434 } else {
435 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
436 if (!__d_rcu_to_refcount(dentry, nd->seq))
437 goto err_child;
438 /*
439 * If the sequence check on the child dentry passed, then
440 * the child has not been removed from its parent. This
441 * means the parent dentry must be valid and able to take
442 * a reference at this point.
443 */
444 BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
445 BUG_ON(!parent->d_count);
446 parent->d_count++;
447 spin_unlock(&dentry->d_lock);
448 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100449 spin_unlock(&parent->d_lock);
Al Viro5b6ca022011-03-09 23:04:47 -0500450 if (want_root) {
Nick Piggin31e6b012011-01-07 17:49:52 +1100451 path_get(&nd->root);
452 spin_unlock(&fs->lock);
453 }
454 mntget(nd->path.mnt);
455
456 rcu_read_unlock();
457 br_read_unlock(vfsmount_lock);
458 nd->flags &= ~LOOKUP_RCU;
459 return 0;
Al Viro19660af2011-03-25 10:32:48 -0400460
461err_child:
Nick Piggin31e6b012011-01-07 17:49:52 +1100462 spin_unlock(&dentry->d_lock);
Al Viro19660af2011-03-25 10:32:48 -0400463err_parent:
Nick Piggin31e6b012011-01-07 17:49:52 +1100464 spin_unlock(&parent->d_lock);
465err_root:
Al Viro5b6ca022011-03-09 23:04:47 -0500466 if (want_root)
Nick Piggin31e6b012011-01-07 17:49:52 +1100467 spin_unlock(&fs->lock);
468 return -ECHILD;
469}
470
Nick Piggin31e6b012011-01-07 17:49:52 +1100471/**
472 * nameidata_drop_rcu_last - drop nameidata ending path walk out of rcu-walk
473 * @nd: nameidata pathwalk data to drop
Randy Dunlap39191622011-01-08 19:36:21 -0800474 * Returns: 0 on success, -ECHILD on failure
Nick Piggin31e6b012011-01-07 17:49:52 +1100475 *
476 * nameidata_drop_rcu_last attempts to drop the current nd->path into ref-walk.
477 * nd->path should be the final element of the lookup, so nd->root is discarded.
478 * Must be called from rcu-walk context.
479 */
480static int nameidata_drop_rcu_last(struct nameidata *nd)
481{
482 struct dentry *dentry = nd->path.dentry;
483
484 BUG_ON(!(nd->flags & LOOKUP_RCU));
485 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -0500486 if (!(nd->flags & LOOKUP_ROOT))
487 nd->root.mnt = NULL;
Nick Piggin31e6b012011-01-07 17:49:52 +1100488 spin_lock(&dentry->d_lock);
489 if (!__d_rcu_to_refcount(dentry, nd->seq))
490 goto err_unlock;
491 BUG_ON(nd->inode != dentry->d_inode);
492 spin_unlock(&dentry->d_lock);
493
494 mntget(nd->path.mnt);
495
496 rcu_read_unlock();
497 br_read_unlock(vfsmount_lock);
498
499 return 0;
500
501err_unlock:
502 spin_unlock(&dentry->d_lock);
503 rcu_read_unlock();
504 br_read_unlock(vfsmount_lock);
505 return -ECHILD;
506}
507
Nick Piggin31e6b012011-01-07 17:49:52 +1100508/**
Trond Myklebust834f2a42005-10-18 14:20:16 -0700509 * release_open_intent - free up open intent resources
510 * @nd: pointer to nameidata
511 */
512void release_open_intent(struct nameidata *nd)
513{
Linus Torvalds2dab5972011-02-11 15:53:38 -0800514 struct file *file = nd->intent.open.file;
515
516 if (file && !IS_ERR(file)) {
517 if (file->f_path.dentry == NULL)
518 put_filp(file);
519 else
520 fput(file);
521 }
Trond Myklebust834f2a42005-10-18 14:20:16 -0700522}
523
Al Virof60aef72011-02-15 01:35:28 -0500524static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
Nick Piggin34286d62011-01-07 17:49:57 +1100525{
Al Virof60aef72011-02-15 01:35:28 -0500526 return dentry->d_op->d_revalidate(dentry, nd);
Nick Piggin34286d62011-01-07 17:49:57 +1100527}
528
Al Virof5e1c1c2011-02-15 01:32:55 -0500529static struct dentry *
Ian Kentbcdc5e02006-09-27 01:50:44 -0700530do_revalidate(struct dentry *dentry, struct nameidata *nd)
531{
Al Virof5e1c1c2011-02-15 01:32:55 -0500532 int status = d_revalidate(dentry, nd);
Ian Kentbcdc5e02006-09-27 01:50:44 -0700533 if (unlikely(status <= 0)) {
534 /*
535 * The dentry failed validation.
536 * If d_revalidate returned 0 attempt to invalidate
537 * the dentry otherwise d_revalidate is asking us
538 * to return a fail status.
539 */
Nick Piggin34286d62011-01-07 17:49:57 +1100540 if (status < 0) {
Al Virof5e1c1c2011-02-15 01:32:55 -0500541 dput(dentry);
Nick Piggin34286d62011-01-07 17:49:57 +1100542 dentry = ERR_PTR(status);
Al Virof5e1c1c2011-02-15 01:32:55 -0500543 } else if (!d_invalidate(dentry)) {
544 dput(dentry);
545 dentry = NULL;
Ian Kentbcdc5e02006-09-27 01:50:44 -0700546 }
547 }
548 return dentry;
549}
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551/*
Al Viro16c2cd72011-02-22 15:50:10 -0500552 * handle_reval_path - force revalidation of a dentry
Jeff Layton39159de2009-12-07 12:01:50 -0500553 *
554 * In some situations the path walking code will trust dentries without
555 * revalidating them. This causes problems for filesystems that depend on
556 * d_revalidate to handle file opens (e.g. NFSv4). When FS_REVAL_DOT is set
557 * (which indicates that it's possible for the dentry to go stale), force
558 * a d_revalidate call before proceeding.
559 *
560 * Returns 0 if the revalidation was successful. If the revalidation fails,
561 * either return the error returned by d_revalidate or -ESTALE if the
562 * revalidation it just returned 0. If d_revalidate returns 0, we attempt to
563 * invalidate the dentry. It's up to the caller to handle putting references
564 * to the path if necessary.
565 */
Al Viro16c2cd72011-02-22 15:50:10 -0500566static inline int handle_reval_path(struct nameidata *nd)
Jeff Layton39159de2009-12-07 12:01:50 -0500567{
Al Viro16c2cd72011-02-22 15:50:10 -0500568 struct dentry *dentry = nd->path.dentry;
Jeff Layton39159de2009-12-07 12:01:50 -0500569 int status;
Jeff Layton39159de2009-12-07 12:01:50 -0500570
Al Viro16c2cd72011-02-22 15:50:10 -0500571 if (likely(!(nd->flags & LOOKUP_JUMPED)))
Jeff Layton39159de2009-12-07 12:01:50 -0500572 return 0;
573
Al Viro16c2cd72011-02-22 15:50:10 -0500574 if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
575 return 0;
576
577 if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
578 return 0;
579
580 /* Note: we do not d_invalidate() */
Nick Piggin34286d62011-01-07 17:49:57 +1100581 status = d_revalidate(dentry, nd);
Jeff Layton39159de2009-12-07 12:01:50 -0500582 if (status > 0)
583 return 0;
584
Al Viro16c2cd72011-02-22 15:50:10 -0500585 if (!status)
Jeff Layton39159de2009-12-07 12:01:50 -0500586 status = -ESTALE;
Al Viro16c2cd72011-02-22 15:50:10 -0500587
Jeff Layton39159de2009-12-07 12:01:50 -0500588 return status;
589}
590
591/*
Al Virob75b5082009-12-16 01:01:38 -0500592 * Short-cut version of permission(), for calling on directories
593 * during pathname resolution. Combines parts of permission()
594 * and generic_permission(), and tests ONLY for MAY_EXEC permission.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 *
596 * If appropriate, check DAC only. If not appropriate, or
Al Virob75b5082009-12-16 01:01:38 -0500597 * short-cut DAC fails, then call ->permission() to do more
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 * complete permission check.
599 */
Nick Pigginb74c79e2011-01-07 17:49:58 +1100600static inline int exec_permission(struct inode *inode, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700602 int ret;
Serge E. Hallyne795b712011-03-23 16:43:25 -0700603 struct user_namespace *ns = inode_userns(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Linus Torvaldscb9179e2009-08-28 11:08:31 -0700605 if (inode->i_op->permission) {
Nick Pigginb74c79e2011-01-07 17:49:58 +1100606 ret = inode->i_op->permission(inode, MAY_EXEC, flags);
607 } else {
608 ret = acl_permission_check(inode, MAY_EXEC, flags,
609 inode->i_op->check_acl);
Linus Torvaldscb9179e2009-08-28 11:08:31 -0700610 }
Nick Pigginb74c79e2011-01-07 17:49:58 +1100611 if (likely(!ret))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 goto ok;
Nick Pigginb74c79e2011-01-07 17:49:58 +1100613 if (ret == -ECHILD)
Nick Piggin31e6b012011-01-07 17:49:52 +1100614 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Serge E. Hallyne795b712011-03-23 16:43:25 -0700616 if (ns_capable(ns, CAP_DAC_OVERRIDE) ||
617 ns_capable(ns, CAP_DAC_READ_SEARCH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 goto ok;
619
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700620 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621ok:
Nick Pigginb74c79e2011-01-07 17:49:58 +1100622 return security_inode_exec_permission(inode, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
624
Al Viro2a737872009-04-07 11:49:53 -0400625static __always_inline void set_root(struct nameidata *nd)
626{
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200627 if (!nd->root.mnt)
628 get_fs_root(current->fs, &nd->root);
Al Viro2a737872009-04-07 11:49:53 -0400629}
630
Al Viro6de88d72009-08-09 01:41:57 +0400631static int link_path_walk(const char *, struct nameidata *);
632
Nick Piggin31e6b012011-01-07 17:49:52 +1100633static __always_inline void set_root_rcu(struct nameidata *nd)
634{
635 if (!nd->root.mnt) {
636 struct fs_struct *fs = current->fs;
Nick Pigginc28cc362011-01-07 17:49:53 +1100637 unsigned seq;
638
639 do {
640 seq = read_seqcount_begin(&fs->seq);
641 nd->root = fs->root;
Tim Chenc1530012011-04-15 11:39:29 -0700642 nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
Nick Pigginc28cc362011-01-07 17:49:53 +1100643 } while (read_seqcount_retry(&fs->seq, seq));
Nick Piggin31e6b012011-01-07 17:49:52 +1100644 }
645}
646
Arjan van de Venf1662352006-01-14 13:21:31 -0800647static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
Nick Piggin31e6b012011-01-07 17:49:52 +1100649 int ret;
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 if (IS_ERR(link))
652 goto fail;
653
654 if (*link == '/') {
Al Viro2a737872009-04-07 11:49:53 -0400655 set_root(nd);
Jan Blunck1d957f92008-02-14 19:34:35 -0800656 path_put(&nd->path);
Al Viro2a737872009-04-07 11:49:53 -0400657 nd->path = nd->root;
658 path_get(&nd->root);
Al Viro16c2cd72011-02-22 15:50:10 -0500659 nd->flags |= LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100661 nd->inode = nd->path.dentry->d_inode;
Christoph Hellwigb4091d52008-11-05 15:07:21 +0100662
Nick Piggin31e6b012011-01-07 17:49:52 +1100663 ret = link_path_walk(link, nd);
664 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665fail:
Jan Blunck1d957f92008-02-14 19:34:35 -0800666 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 return PTR_ERR(link);
668}
669
Jan Blunck1d957f92008-02-14 19:34:35 -0800670static void path_put_conditional(struct path *path, struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700671{
672 dput(path->dentry);
Jan Blunck4ac91372008-02-14 19:34:32 -0800673 if (path->mnt != nd->path.mnt)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700674 mntput(path->mnt);
675}
676
Nick Piggin7b9337a2011-01-14 08:42:43 +0000677static inline void path_to_nameidata(const struct path *path,
678 struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700679{
Nick Piggin31e6b012011-01-07 17:49:52 +1100680 if (!(nd->flags & LOOKUP_RCU)) {
681 dput(nd->path.dentry);
682 if (nd->path.mnt != path->mnt)
683 mntput(nd->path.mnt);
Huang Shijie9a229682010-04-02 17:37:13 +0800684 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100685 nd->path.mnt = path->mnt;
Jan Blunck4ac91372008-02-14 19:34:32 -0800686 nd->path.dentry = path->dentry;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700687}
688
Al Viro574197e2011-03-14 22:20:34 -0400689static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
690{
691 struct inode *inode = link->dentry->d_inode;
692 if (!IS_ERR(cookie) && inode->i_op->put_link)
693 inode->i_op->put_link(link->dentry, nd, cookie);
694 path_put(link);
695}
696
Al Virodef4af32009-12-26 08:37:05 -0500697static __always_inline int
Al Viro574197e2011-03-14 22:20:34 -0400698follow_link(struct path *link, struct nameidata *nd, void **p)
Ian Kent051d3812006-03-27 01:14:53 -0800699{
700 int error;
Nick Piggin7b9337a2011-01-14 08:42:43 +0000701 struct dentry *dentry = link->dentry;
Ian Kent051d3812006-03-27 01:14:53 -0800702
Al Viro844a3912011-02-15 00:38:26 -0500703 BUG_ON(nd->flags & LOOKUP_RCU);
704
Al Viro0e794582011-03-16 02:45:02 -0400705 if (link->mnt == nd->path.mnt)
706 mntget(link->mnt);
707
Al Viro574197e2011-03-14 22:20:34 -0400708 if (unlikely(current->total_link_count >= 40)) {
709 *p = ERR_PTR(-ELOOP); /* no ->put_link(), please */
Al Viro574197e2011-03-14 22:20:34 -0400710 path_put(&nd->path);
711 return -ELOOP;
712 }
713 cond_resched();
714 current->total_link_count++;
715
Nick Piggin7b9337a2011-01-14 08:42:43 +0000716 touch_atime(link->mnt, dentry);
Ian Kent051d3812006-03-27 01:14:53 -0800717 nd_set_link(nd, NULL);
718
Al Viro36f3b4f2011-02-22 21:24:38 -0500719 error = security_inode_follow_link(link->dentry, nd);
720 if (error) {
721 *p = ERR_PTR(error); /* no ->put_link(), please */
722 path_put(&nd->path);
723 return error;
724 }
725
Al Viro86acdca12009-12-22 23:45:11 -0500726 nd->last_type = LAST_BIND;
Al Virodef4af32009-12-26 08:37:05 -0500727 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
728 error = PTR_ERR(*p);
729 if (!IS_ERR(*p)) {
Ian Kent051d3812006-03-27 01:14:53 -0800730 char *s = nd_get_link(nd);
731 error = 0;
732 if (s)
733 error = __vfs_follow_link(nd, s);
Al Virobcda7652011-03-13 16:42:14 -0400734 else if (nd->last_type == LAST_BIND) {
Al Viro16c2cd72011-02-22 15:50:10 -0500735 nd->flags |= LOOKUP_JUMPED;
Al Virob21041d2011-03-14 20:01:51 -0400736 nd->inode = nd->path.dentry->d_inode;
737 if (nd->inode->i_op->follow_link) {
Al Virobcda7652011-03-13 16:42:14 -0400738 /* stepped on a _really_ weird one */
739 path_put(&nd->path);
740 error = -ELOOP;
741 }
742 }
Ian Kent051d3812006-03-27 01:14:53 -0800743 }
Ian Kent051d3812006-03-27 01:14:53 -0800744 return error;
745}
746
Nick Piggin31e6b012011-01-07 17:49:52 +1100747static int follow_up_rcu(struct path *path)
748{
749 struct vfsmount *parent;
750 struct dentry *mountpoint;
751
752 parent = path->mnt->mnt_parent;
753 if (parent == path->mnt)
754 return 0;
755 mountpoint = path->mnt->mnt_mountpoint;
756 path->dentry = mountpoint;
757 path->mnt = parent;
758 return 1;
759}
760
Al Virobab77eb2009-04-18 03:26:48 -0400761int follow_up(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
763 struct vfsmount *parent;
764 struct dentry *mountpoint;
Nick Piggin99b7db72010-08-18 04:37:39 +1000765
766 br_read_lock(vfsmount_lock);
Al Virobab77eb2009-04-18 03:26:48 -0400767 parent = path->mnt->mnt_parent;
768 if (parent == path->mnt) {
Nick Piggin99b7db72010-08-18 04:37:39 +1000769 br_read_unlock(vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 return 0;
771 }
772 mntget(parent);
Al Virobab77eb2009-04-18 03:26:48 -0400773 mountpoint = dget(path->mnt->mnt_mountpoint);
Nick Piggin99b7db72010-08-18 04:37:39 +1000774 br_read_unlock(vfsmount_lock);
Al Virobab77eb2009-04-18 03:26:48 -0400775 dput(path->dentry);
776 path->dentry = mountpoint;
777 mntput(path->mnt);
778 path->mnt = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return 1;
780}
781
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100782/*
David Howells9875cf82011-01-14 18:45:21 +0000783 * Perform an automount
784 * - return -EISDIR to tell follow_managed() to stop and return the path we
785 * were called with.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 */
David Howells9875cf82011-01-14 18:45:21 +0000787static int follow_automount(struct path *path, unsigned flags,
788 bool *need_mntput)
Nick Piggin31e6b012011-01-07 17:49:52 +1100789{
David Howells9875cf82011-01-14 18:45:21 +0000790 struct vfsmount *mnt;
David Howellsea5b7782011-01-14 19:10:03 +0000791 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +1100792
David Howells9875cf82011-01-14 18:45:21 +0000793 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
794 return -EREMOTE;
Al Viro463ffb22005-06-06 13:36:05 -0700795
David Howells6f45b652011-01-14 18:45:31 +0000796 /* We don't want to mount if someone supplied AT_NO_AUTOMOUNT
797 * and this is the terminal part of the path.
798 */
799 if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_CONTINUE))
800 return -EISDIR; /* we actually want to stop here */
801
David Howells9875cf82011-01-14 18:45:21 +0000802 /* We want to mount if someone is trying to open/create a file of any
803 * type under the mountpoint, wants to traverse through the mountpoint
804 * or wants to open the mounted directory.
805 *
806 * We don't want to mount if someone's just doing a stat and they've
807 * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and
808 * appended a '/' to the name.
809 */
810 if (!(flags & LOOKUP_FOLLOW) &&
811 !(flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY |
812 LOOKUP_OPEN | LOOKUP_CREATE)))
813 return -EISDIR;
814
815 current->total_link_count++;
816 if (current->total_link_count >= 40)
817 return -ELOOP;
818
819 mnt = path->dentry->d_op->d_automount(path);
820 if (IS_ERR(mnt)) {
821 /*
822 * The filesystem is allowed to return -EISDIR here to indicate
823 * it doesn't want to automount. For instance, autofs would do
824 * this so that its userspace daemon can mount on this dentry.
825 *
826 * However, we can only permit this if it's a terminal point in
827 * the path being looked up; if it wasn't then the remainder of
828 * the path is inaccessible and we should say so.
829 */
830 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_CONTINUE))
831 return -EREMOTE;
832 return PTR_ERR(mnt);
833 }
David Howellsea5b7782011-01-14 19:10:03 +0000834
David Howells9875cf82011-01-14 18:45:21 +0000835 if (!mnt) /* mount collision */
836 return 0;
837
Al Viro19a167a2011-01-17 01:35:23 -0500838 err = finish_automount(mnt, path);
David Howellsea5b7782011-01-14 19:10:03 +0000839
David Howellsea5b7782011-01-14 19:10:03 +0000840 switch (err) {
841 case -EBUSY:
842 /* Someone else made a mount here whilst we were busy */
Al Viro19a167a2011-01-17 01:35:23 -0500843 return 0;
David Howellsea5b7782011-01-14 19:10:03 +0000844 case 0:
David Howellsea5b7782011-01-14 19:10:03 +0000845 dput(path->dentry);
846 if (*need_mntput)
847 mntput(path->mnt);
848 path->mnt = mnt;
849 path->dentry = dget(mnt->mnt_root);
850 *need_mntput = true;
851 return 0;
Al Viro19a167a2011-01-17 01:35:23 -0500852 default:
853 return err;
David Howellsea5b7782011-01-14 19:10:03 +0000854 }
Al Viro19a167a2011-01-17 01:35:23 -0500855
David Howells9875cf82011-01-14 18:45:21 +0000856}
857
858/*
859 * Handle a dentry that is managed in some way.
David Howellscc53ce52011-01-14 18:45:26 +0000860 * - Flagged for transit management (autofs)
David Howells9875cf82011-01-14 18:45:21 +0000861 * - Flagged as mountpoint
862 * - Flagged as automount point
863 *
864 * This may only be called in refwalk mode.
865 *
866 * Serialization is taken care of in namespace.c
867 */
868static int follow_managed(struct path *path, unsigned flags)
869{
870 unsigned managed;
871 bool need_mntput = false;
872 int ret;
873
874 /* Given that we're not holding a lock here, we retain the value in a
875 * local variable for each dentry as we look at it so that we don't see
876 * the components of that value change under us */
877 while (managed = ACCESS_ONCE(path->dentry->d_flags),
878 managed &= DCACHE_MANAGED_DENTRY,
879 unlikely(managed != 0)) {
David Howellscc53ce52011-01-14 18:45:26 +0000880 /* Allow the filesystem to manage the transit without i_mutex
881 * being held. */
882 if (managed & DCACHE_MANAGE_TRANSIT) {
883 BUG_ON(!path->dentry->d_op);
884 BUG_ON(!path->dentry->d_op->d_manage);
Al Viro1aed3e42011-03-18 09:09:02 -0400885 ret = path->dentry->d_op->d_manage(path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +0000886 if (ret < 0)
887 return ret == -EISDIR ? 0 : ret;
888 }
889
David Howells9875cf82011-01-14 18:45:21 +0000890 /* Transit to a mounted filesystem. */
891 if (managed & DCACHE_MOUNTED) {
892 struct vfsmount *mounted = lookup_mnt(path);
893 if (mounted) {
894 dput(path->dentry);
895 if (need_mntput)
896 mntput(path->mnt);
897 path->mnt = mounted;
898 path->dentry = dget(mounted->mnt_root);
899 need_mntput = true;
900 continue;
901 }
902
903 /* Something is mounted on this dentry in another
904 * namespace and/or whatever was mounted there in this
905 * namespace got unmounted before we managed to get the
906 * vfsmount_lock */
907 }
908
909 /* Handle an automount point */
910 if (managed & DCACHE_NEED_AUTOMOUNT) {
911 ret = follow_automount(path, flags, &need_mntput);
912 if (ret < 0)
913 return ret == -EISDIR ? 0 : ret;
914 continue;
915 }
916
917 /* We didn't change the current path point */
918 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 }
David Howells9875cf82011-01-14 18:45:21 +0000920 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921}
922
David Howellscc53ce52011-01-14 18:45:26 +0000923int follow_down_one(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
925 struct vfsmount *mounted;
926
Al Viro1c755af2009-04-18 14:06:57 -0400927 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -0400929 dput(path->dentry);
930 mntput(path->mnt);
931 path->mnt = mounted;
932 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 return 1;
934 }
935 return 0;
936}
937
Ian Kent62a73752011-03-25 01:51:02 +0800938static inline bool managed_dentry_might_block(struct dentry *dentry)
939{
940 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
941 dentry->d_op->d_manage(dentry, true) < 0);
942}
943
David Howells9875cf82011-01-14 18:45:21 +0000944/*
945 * Skip to top of mountpoint pile in rcuwalk mode. We abort the rcu-walk if we
David Howellscc53ce52011-01-14 18:45:26 +0000946 * meet a managed dentry and we're not walking to "..". True is returned to
David Howells9875cf82011-01-14 18:45:21 +0000947 * continue, false to abort.
948 */
949static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
950 struct inode **inode, bool reverse_transit)
951{
Ian Kent62a73752011-03-25 01:51:02 +0800952 for (;;) {
David Howells9875cf82011-01-14 18:45:21 +0000953 struct vfsmount *mounted;
Ian Kent62a73752011-03-25 01:51:02 +0800954 /*
955 * Don't forget we might have a non-mountpoint managed dentry
956 * that wants to block transit.
957 */
958 *inode = path->dentry->d_inode;
959 if (!reverse_transit &&
960 unlikely(managed_dentry_might_block(path->dentry)))
David Howellsab909112011-01-14 18:46:51 +0000961 return false;
Ian Kent62a73752011-03-25 01:51:02 +0800962
963 if (!d_mountpoint(path->dentry))
964 break;
965
David Howells9875cf82011-01-14 18:45:21 +0000966 mounted = __lookup_mnt(path->mnt, path->dentry, 1);
967 if (!mounted)
968 break;
969 path->mnt = mounted;
970 path->dentry = mounted->mnt_root;
971 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
David Howells9875cf82011-01-14 18:45:21 +0000972 }
973
974 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
975 return reverse_transit;
976 return true;
977}
978
Nick Piggin31e6b012011-01-07 17:49:52 +1100979static int follow_dotdot_rcu(struct nameidata *nd)
980{
981 struct inode *inode = nd->inode;
982
983 set_root_rcu(nd);
984
David Howells9875cf82011-01-14 18:45:21 +0000985 while (1) {
Nick Piggin31e6b012011-01-07 17:49:52 +1100986 if (nd->path.dentry == nd->root.dentry &&
987 nd->path.mnt == nd->root.mnt) {
988 break;
989 }
990 if (nd->path.dentry != nd->path.mnt->mnt_root) {
991 struct dentry *old = nd->path.dentry;
992 struct dentry *parent = old->d_parent;
993 unsigned seq;
994
995 seq = read_seqcount_begin(&parent->d_seq);
996 if (read_seqcount_retry(&old->d_seq, nd->seq))
Al Viroef7562d2011-03-04 14:35:59 -0500997 goto failed;
Nick Piggin31e6b012011-01-07 17:49:52 +1100998 inode = parent->d_inode;
999 nd->path.dentry = parent;
1000 nd->seq = seq;
1001 break;
1002 }
1003 if (!follow_up_rcu(&nd->path))
1004 break;
1005 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
1006 inode = nd->path.dentry->d_inode;
1007 }
David Howells9875cf82011-01-14 18:45:21 +00001008 __follow_mount_rcu(nd, &nd->path, &inode, true);
Nick Piggin31e6b012011-01-07 17:49:52 +11001009 nd->inode = inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001010 return 0;
Al Viroef7562d2011-03-04 14:35:59 -05001011
1012failed:
1013 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001014 if (!(nd->flags & LOOKUP_ROOT))
1015 nd->root.mnt = NULL;
Al Viroef7562d2011-03-04 14:35:59 -05001016 rcu_read_unlock();
1017 br_read_unlock(vfsmount_lock);
1018 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001019}
1020
David Howells9875cf82011-01-14 18:45:21 +00001021/*
David Howellscc53ce52011-01-14 18:45:26 +00001022 * Follow down to the covering mount currently visible to userspace. At each
1023 * point, the filesystem owning that dentry may be queried as to whether the
1024 * caller is permitted to proceed or not.
1025 *
1026 * Care must be taken as namespace_sem may be held (indicated by mounting_here
1027 * being true).
1028 */
Al Viro7cc90cc2011-03-18 09:04:20 -04001029int follow_down(struct path *path)
David Howellscc53ce52011-01-14 18:45:26 +00001030{
1031 unsigned managed;
1032 int ret;
1033
1034 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1035 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1036 /* Allow the filesystem to manage the transit without i_mutex
1037 * being held.
1038 *
1039 * We indicate to the filesystem if someone is trying to mount
1040 * something here. This gives autofs the chance to deny anyone
1041 * other than its daemon the right to mount on its
1042 * superstructure.
1043 *
1044 * The filesystem may sleep at this point.
1045 */
1046 if (managed & DCACHE_MANAGE_TRANSIT) {
1047 BUG_ON(!path->dentry->d_op);
1048 BUG_ON(!path->dentry->d_op->d_manage);
David Howellsab909112011-01-14 18:46:51 +00001049 ret = path->dentry->d_op->d_manage(
Al Viro1aed3e42011-03-18 09:09:02 -04001050 path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +00001051 if (ret < 0)
1052 return ret == -EISDIR ? 0 : ret;
1053 }
1054
1055 /* Transit to a mounted filesystem. */
1056 if (managed & DCACHE_MOUNTED) {
1057 struct vfsmount *mounted = lookup_mnt(path);
1058 if (!mounted)
1059 break;
1060 dput(path->dentry);
1061 mntput(path->mnt);
1062 path->mnt = mounted;
1063 path->dentry = dget(mounted->mnt_root);
1064 continue;
1065 }
1066
1067 /* Don't handle automount points here */
1068 break;
1069 }
1070 return 0;
1071}
1072
1073/*
David Howells9875cf82011-01-14 18:45:21 +00001074 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1075 */
1076static void follow_mount(struct path *path)
1077{
1078 while (d_mountpoint(path->dentry)) {
1079 struct vfsmount *mounted = lookup_mnt(path);
1080 if (!mounted)
1081 break;
1082 dput(path->dentry);
1083 mntput(path->mnt);
1084 path->mnt = mounted;
1085 path->dentry = dget(mounted->mnt_root);
1086 }
1087}
1088
Nick Piggin31e6b012011-01-07 17:49:52 +11001089static void follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
Al Viro2a737872009-04-07 11:49:53 -04001091 set_root(nd);
Andreas Mohre518ddb2006-09-29 02:01:22 -07001092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 while(1) {
Jan Blunck4ac91372008-02-14 19:34:32 -08001094 struct dentry *old = nd->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Al Viro2a737872009-04-07 11:49:53 -04001096 if (nd->path.dentry == nd->root.dentry &&
1097 nd->path.mnt == nd->root.mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 break;
1099 }
Jan Blunck4ac91372008-02-14 19:34:32 -08001100 if (nd->path.dentry != nd->path.mnt->mnt_root) {
Al Viro3088dd72010-01-30 15:47:29 -05001101 /* rare case of legitimate dget_parent()... */
1102 nd->path.dentry = dget_parent(nd->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 dput(old);
1104 break;
1105 }
Al Viro3088dd72010-01-30 15:47:29 -05001106 if (!follow_up(&nd->path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 }
Al Viro79ed0222009-04-18 13:59:41 -04001109 follow_mount(&nd->path);
Nick Piggin31e6b012011-01-07 17:49:52 +11001110 nd->inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111}
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113/*
Nick Pigginbaa03892010-08-18 04:37:31 +10001114 * Allocate a dentry with name and parent, and perform a parent
1115 * directory ->lookup on it. Returns the new dentry, or ERR_PTR
1116 * on error. parent->d_inode->i_mutex must be held. d_lookup must
1117 * have verified that no child exists while under i_mutex.
1118 */
1119static struct dentry *d_alloc_and_lookup(struct dentry *parent,
1120 struct qstr *name, struct nameidata *nd)
1121{
1122 struct inode *inode = parent->d_inode;
1123 struct dentry *dentry;
1124 struct dentry *old;
1125
1126 /* Don't create child dentry for a dead directory. */
1127 if (unlikely(IS_DEADDIR(inode)))
1128 return ERR_PTR(-ENOENT);
1129
1130 dentry = d_alloc(parent, name);
1131 if (unlikely(!dentry))
1132 return ERR_PTR(-ENOMEM);
1133
1134 old = inode->i_op->lookup(inode, dentry, nd);
1135 if (unlikely(old)) {
1136 dput(dentry);
1137 dentry = old;
1138 }
1139 return dentry;
1140}
1141
1142/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 * It's more convoluted than I'd like it to be, but... it's still fairly
1144 * small and for now I'd prefer to have fast path as straight as possible.
1145 * It _is_ time-critical.
1146 */
1147static int do_lookup(struct nameidata *nd, struct qstr *name,
Nick Piggin31e6b012011-01-07 17:49:52 +11001148 struct path *path, struct inode **inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149{
Jan Blunck4ac91372008-02-14 19:34:32 -08001150 struct vfsmount *mnt = nd->path.mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +11001151 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro5a18fff2011-03-11 04:44:53 -05001152 int need_reval = 1;
1153 int status = 1;
David Howells9875cf82011-01-14 18:45:21 +00001154 int err;
1155
Al Viro3cac2602009-08-13 18:27:43 +04001156 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001157 * Rename seqlock is not required here because in the off chance
1158 * of a false negative due to a concurrent rename, we're going to
1159 * do the non-racy lookup, below.
1160 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001161 if (nd->flags & LOOKUP_RCU) {
1162 unsigned seq;
Nick Piggin31e6b012011-01-07 17:49:52 +11001163 *inode = nd->inode;
1164 dentry = __d_lookup_rcu(parent, name, &seq, inode);
Al Viro5a18fff2011-03-11 04:44:53 -05001165 if (!dentry)
1166 goto unlazy;
1167
Nick Piggin31e6b012011-01-07 17:49:52 +11001168 /* Memory barrier in read_seqcount_begin of child is enough */
1169 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1170 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001171 nd->seq = seq;
Al Viro5a18fff2011-03-11 04:44:53 -05001172
Al Viro24643082011-02-15 01:26:22 -05001173 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
Al Viro5a18fff2011-03-11 04:44:53 -05001174 status = d_revalidate(dentry, nd);
1175 if (unlikely(status <= 0)) {
1176 if (status != -ECHILD)
1177 need_reval = 0;
1178 goto unlazy;
1179 }
Al Viro24643082011-02-15 01:26:22 -05001180 }
Nick Piggin31e6b012011-01-07 17:49:52 +11001181 path->mnt = mnt;
1182 path->dentry = dentry;
David Howells9875cf82011-01-14 18:45:21 +00001183 if (likely(__follow_mount_rcu(nd, path, inode, false)))
1184 return 0;
Al Viro5a18fff2011-03-11 04:44:53 -05001185unlazy:
Al Viro19660af2011-03-25 10:32:48 -04001186 if (unlazy_walk(nd, dentry))
1187 return -ECHILD;
Al Viro5a18fff2011-03-11 04:44:53 -05001188 } else {
1189 dentry = __d_lookup(parent, name);
Nick Piggin31e6b012011-01-07 17:49:52 +11001190 }
Al Viro5a18fff2011-03-11 04:44:53 -05001191
1192retry:
1193 if (unlikely(!dentry)) {
1194 struct inode *dir = parent->d_inode;
1195 BUG_ON(nd->inode != dir);
1196
1197 mutex_lock(&dir->i_mutex);
1198 dentry = d_lookup(parent, name);
1199 if (likely(!dentry)) {
1200 dentry = d_alloc_and_lookup(parent, name, nd);
1201 if (IS_ERR(dentry)) {
1202 mutex_unlock(&dir->i_mutex);
1203 return PTR_ERR(dentry);
1204 }
1205 /* known good */
1206 need_reval = 0;
1207 status = 1;
1208 }
1209 mutex_unlock(&dir->i_mutex);
Al Viro24643082011-02-15 01:26:22 -05001210 }
Al Viro5a18fff2011-03-11 04:44:53 -05001211 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
1212 status = d_revalidate(dentry, nd);
1213 if (unlikely(status <= 0)) {
1214 if (status < 0) {
1215 dput(dentry);
1216 return status;
1217 }
1218 if (!d_invalidate(dentry)) {
1219 dput(dentry);
1220 dentry = NULL;
1221 need_reval = 1;
1222 goto retry;
1223 }
1224 }
1225
David Howells9875cf82011-01-14 18:45:21 +00001226 path->mnt = mnt;
1227 path->dentry = dentry;
1228 err = follow_managed(path, nd->flags);
Ian Kent89312212011-01-18 12:06:10 +08001229 if (unlikely(err < 0)) {
1230 path_put_conditional(path, nd);
David Howells9875cf82011-01-14 18:45:21 +00001231 return err;
Ian Kent89312212011-01-18 12:06:10 +08001232 }
David Howells9875cf82011-01-14 18:45:21 +00001233 *inode = path->dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235}
1236
Al Viro52094c82011-02-21 21:34:47 -05001237static inline int may_lookup(struct nameidata *nd)
1238{
1239 if (nd->flags & LOOKUP_RCU) {
1240 int err = exec_permission(nd->inode, IPERM_FLAG_RCU);
1241 if (err != -ECHILD)
1242 return err;
Al Viro19660af2011-03-25 10:32:48 -04001243 if (unlazy_walk(nd, NULL))
Al Viro52094c82011-02-21 21:34:47 -05001244 return -ECHILD;
1245 }
1246 return exec_permission(nd->inode, 0);
1247}
1248
Al Viro9856fa12011-03-04 14:22:06 -05001249static inline int handle_dots(struct nameidata *nd, int type)
1250{
1251 if (type == LAST_DOTDOT) {
1252 if (nd->flags & LOOKUP_RCU) {
1253 if (follow_dotdot_rcu(nd))
1254 return -ECHILD;
1255 } else
1256 follow_dotdot(nd);
1257 }
1258 return 0;
1259}
1260
Al Viro951361f2011-03-04 14:44:37 -05001261static void terminate_walk(struct nameidata *nd)
1262{
1263 if (!(nd->flags & LOOKUP_RCU)) {
1264 path_put(&nd->path);
1265 } else {
1266 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001267 if (!(nd->flags & LOOKUP_ROOT))
1268 nd->root.mnt = NULL;
Al Viro951361f2011-03-04 14:44:37 -05001269 rcu_read_unlock();
1270 br_read_unlock(vfsmount_lock);
1271 }
1272}
1273
Al Viroce57dfc2011-03-13 19:58:58 -04001274static inline int walk_component(struct nameidata *nd, struct path *path,
1275 struct qstr *name, int type, int follow)
1276{
1277 struct inode *inode;
1278 int err;
1279 /*
1280 * "." and ".." are special - ".." especially so because it has
1281 * to be able to know about the current root directory and
1282 * parent relationships.
1283 */
1284 if (unlikely(type != LAST_NORM))
1285 return handle_dots(nd, type);
1286 err = do_lookup(nd, name, path, &inode);
1287 if (unlikely(err)) {
1288 terminate_walk(nd);
1289 return err;
1290 }
1291 if (!inode) {
1292 path_to_nameidata(path, nd);
1293 terminate_walk(nd);
1294 return -ENOENT;
1295 }
1296 if (unlikely(inode->i_op->follow_link) && follow) {
Al Viro19660af2011-03-25 10:32:48 -04001297 if (nd->flags & LOOKUP_RCU) {
1298 if (unlikely(unlazy_walk(nd, path->dentry))) {
1299 terminate_walk(nd);
1300 return -ECHILD;
1301 }
1302 }
Al Viroce57dfc2011-03-13 19:58:58 -04001303 BUG_ON(inode != path->dentry->d_inode);
1304 return 1;
1305 }
1306 path_to_nameidata(path, nd);
1307 nd->inode = inode;
1308 return 0;
1309}
1310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311/*
Al Virob3563792011-03-14 21:54:55 -04001312 * This limits recursive symlink follows to 8, while
1313 * limiting consecutive symlinks to 40.
1314 *
1315 * Without that kind of total limit, nasty chains of consecutive
1316 * symlinks can cause almost arbitrarily long lookups.
1317 */
1318static inline int nested_symlink(struct path *path, struct nameidata *nd)
1319{
1320 int res;
1321
1322 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
1323 if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1324 path_put_conditional(path, nd);
1325 path_put(&nd->path);
1326 return -ELOOP;
1327 }
1328
1329 nd->depth++;
1330 current->link_count++;
1331
1332 do {
1333 struct path link = *path;
1334 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04001335
1336 res = follow_link(&link, nd, &cookie);
Al Virob3563792011-03-14 21:54:55 -04001337 if (!res)
1338 res = walk_component(nd, path, &nd->last,
1339 nd->last_type, LOOKUP_FOLLOW);
Al Viro574197e2011-03-14 22:20:34 -04001340 put_link(nd, &link, cookie);
Al Virob3563792011-03-14 21:54:55 -04001341 } while (res > 0);
1342
1343 current->link_count--;
1344 nd->depth--;
1345 return res;
1346}
1347
1348/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01001350 * This is the basic name resolution function, turning a pathname into
1351 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01001353 * Returns 0 and nd will have valid dentry and mnt on success.
1354 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 */
Al Viro6de88d72009-08-09 01:41:57 +04001356static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
1358 struct path next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 int err;
1360 unsigned int lookup_flags = nd->flags;
1361
1362 while (*name=='/')
1363 name++;
1364 if (!*name)
Al Viro086e1832011-02-22 20:56:27 -05001365 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 /* At this point we know we have a real path component. */
1368 for(;;) {
1369 unsigned long hash;
1370 struct qstr this;
1371 unsigned int c;
Al Virofe479a52011-02-22 15:10:03 -05001372 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Trond Myklebustcdce5d62005-10-18 14:20:18 -07001374 nd->flags |= LOOKUP_CONTINUE;
Al Viro52094c82011-02-21 21:34:47 -05001375
1376 err = may_lookup(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 if (err)
1378 break;
1379
1380 this.name = name;
1381 c = *(const unsigned char *)name;
1382
1383 hash = init_name_hash();
1384 do {
1385 name++;
1386 hash = partial_name_hash(c, hash);
1387 c = *(const unsigned char *)name;
1388 } while (c && (c != '/'));
1389 this.len = name - (const char *) this.name;
1390 this.hash = end_name_hash(hash);
1391
Al Virofe479a52011-02-22 15:10:03 -05001392 type = LAST_NORM;
1393 if (this.name[0] == '.') switch (this.len) {
1394 case 2:
Al Viro16c2cd72011-02-22 15:50:10 -05001395 if (this.name[1] == '.') {
Al Virofe479a52011-02-22 15:10:03 -05001396 type = LAST_DOTDOT;
Al Viro16c2cd72011-02-22 15:50:10 -05001397 nd->flags |= LOOKUP_JUMPED;
1398 }
Al Virofe479a52011-02-22 15:10:03 -05001399 break;
1400 case 1:
1401 type = LAST_DOT;
1402 }
Al Viro5a202bc2011-03-08 14:17:44 -05001403 if (likely(type == LAST_NORM)) {
1404 struct dentry *parent = nd->path.dentry;
Al Viro16c2cd72011-02-22 15:50:10 -05001405 nd->flags &= ~LOOKUP_JUMPED;
Al Viro5a202bc2011-03-08 14:17:44 -05001406 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1407 err = parent->d_op->d_hash(parent, nd->inode,
1408 &this);
1409 if (err < 0)
1410 break;
1411 }
1412 }
Al Virofe479a52011-02-22 15:10:03 -05001413
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 /* remove trailing slashes? */
1415 if (!c)
1416 goto last_component;
1417 while (*++name == '/');
1418 if (!*name)
Al Virob3563792011-03-14 21:54:55 -04001419 goto last_component;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Al Viroce57dfc2011-03-13 19:58:58 -04001421 err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1422 if (err < 0)
1423 return err;
Al Virofe479a52011-02-22 15:10:03 -05001424
Al Viroce57dfc2011-03-13 19:58:58 -04001425 if (err) {
Al Virob3563792011-03-14 21:54:55 -04001426 err = nested_symlink(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 if (err)
Al Viroa7472ba2011-03-04 14:39:30 -05001428 return err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 err = -ENOTDIR;
Nick Piggin31e6b012011-01-07 17:49:52 +11001431 if (!nd->inode->i_op->lookup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 break;
1433 continue;
1434 /* here ends the main loop */
1435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436last_component:
Trond Myklebustf55eab82006-02-04 23:28:01 -08001437 /* Clear LOOKUP_CONTINUE iff it was previously unset */
1438 nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
Al Virob3563792011-03-14 21:54:55 -04001439 nd->last = this;
1440 nd->last_type = type;
Al Viro086e1832011-02-22 20:56:27 -05001441 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 }
Al Viro951361f2011-03-04 14:44:37 -05001443 terminate_walk(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 return err;
1445}
1446
Al Viro70e9b352011-03-05 21:12:22 -05001447static int path_init(int dfd, const char *name, unsigned int flags,
1448 struct nameidata *nd, struct file **fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449{
Prasanna Medaea3834d2005-04-29 16:00:17 +01001450 int retval = 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001451 int fput_needed;
1452 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
1454 nd->last_type = LAST_ROOT; /* if there are only slashes... */
Al Viro16c2cd72011-02-22 15:50:10 -05001455 nd->flags = flags | LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 nd->depth = 0;
Al Viro5b6ca022011-03-09 23:04:47 -05001457 if (flags & LOOKUP_ROOT) {
1458 struct inode *inode = nd->root.dentry->d_inode;
Al Viro73d049a2011-03-11 12:08:24 -05001459 if (*name) {
1460 if (!inode->i_op->lookup)
1461 return -ENOTDIR;
1462 retval = inode_permission(inode, MAY_EXEC);
1463 if (retval)
1464 return retval;
1465 }
Al Viro5b6ca022011-03-09 23:04:47 -05001466 nd->path = nd->root;
1467 nd->inode = inode;
1468 if (flags & LOOKUP_RCU) {
1469 br_read_lock(vfsmount_lock);
1470 rcu_read_lock();
1471 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1472 } else {
1473 path_get(&nd->path);
1474 }
1475 return 0;
1476 }
1477
Al Viro2a737872009-04-07 11:49:53 -04001478 nd->root.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 if (*name=='/') {
Al Viroe41f7d42011-02-22 14:02:58 -05001481 if (flags & LOOKUP_RCU) {
1482 br_read_lock(vfsmount_lock);
1483 rcu_read_lock();
1484 set_root_rcu(nd);
1485 } else {
1486 set_root(nd);
1487 path_get(&nd->root);
1488 }
Al Viro2a737872009-04-07 11:49:53 -04001489 nd->path = nd->root;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001490 } else if (dfd == AT_FDCWD) {
Al Viroe41f7d42011-02-22 14:02:58 -05001491 if (flags & LOOKUP_RCU) {
1492 struct fs_struct *fs = current->fs;
1493 unsigned seq;
1494
1495 br_read_lock(vfsmount_lock);
1496 rcu_read_lock();
1497
1498 do {
1499 seq = read_seqcount_begin(&fs->seq);
1500 nd->path = fs->pwd;
1501 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1502 } while (read_seqcount_retry(&fs->seq, seq));
1503 } else {
1504 get_fs_pwd(current->fs, &nd->path);
1505 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001506 } else {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001507 struct dentry *dentry;
1508
Al Viro1abf0c72011-03-13 03:51:11 -04001509 file = fget_raw_light(dfd, &fput_needed);
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001510 retval = -EBADF;
1511 if (!file)
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001512 goto out_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001513
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001514 dentry = file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001515
Al Virof52e0c12011-03-14 18:56:51 -04001516 if (*name) {
1517 retval = -ENOTDIR;
1518 if (!S_ISDIR(dentry->d_inode->i_mode))
1519 goto fput_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001520
Al Virof52e0c12011-03-14 18:56:51 -04001521 retval = file_permission(file, MAY_EXEC);
1522 if (retval)
1523 goto fput_fail;
1524 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001525
Jan Blunck5dd784d2008-02-14 19:34:38 -08001526 nd->path = file->f_path;
Al Viroe41f7d42011-02-22 14:02:58 -05001527 if (flags & LOOKUP_RCU) {
1528 if (fput_needed)
Al Viro70e9b352011-03-05 21:12:22 -05001529 *fp = file;
Al Viroe41f7d42011-02-22 14:02:58 -05001530 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1531 br_read_lock(vfsmount_lock);
1532 rcu_read_lock();
1533 } else {
1534 path_get(&file->f_path);
1535 fput_light(file, fput_needed);
1536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 }
Al Viroe41f7d42011-02-22 14:02:58 -05001538
Nick Piggin31e6b012011-01-07 17:49:52 +11001539 nd->inode = nd->path.dentry->d_inode;
Al Viro9b4a9b12009-04-07 11:44:16 -04001540 return 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001541
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001542fput_fail:
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001543 fput_light(file, fput_needed);
Al Viro9b4a9b12009-04-07 11:44:16 -04001544out_fail:
1545 return retval;
1546}
1547
Al Virobd92d7f2011-03-14 19:54:59 -04001548static inline int lookup_last(struct nameidata *nd, struct path *path)
1549{
1550 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1551 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1552
1553 nd->flags &= ~LOOKUP_PARENT;
1554 return walk_component(nd, path, &nd->last, nd->last_type,
1555 nd->flags & LOOKUP_FOLLOW);
1556}
1557
Al Viro9b4a9b12009-04-07 11:44:16 -04001558/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroee0827c2011-02-21 23:38:09 -05001559static int path_lookupat(int dfd, const char *name,
Al Viro9b4a9b12009-04-07 11:44:16 -04001560 unsigned int flags, struct nameidata *nd)
1561{
Al Viro70e9b352011-03-05 21:12:22 -05001562 struct file *base = NULL;
Al Virobd92d7f2011-03-14 19:54:59 -04001563 struct path path;
1564 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001565
1566 /*
1567 * Path walking is largely split up into 2 different synchronisation
1568 * schemes, rcu-walk and ref-walk (explained in
1569 * Documentation/filesystems/path-lookup.txt). These share much of the
1570 * path walk code, but some things particularly setup, cleanup, and
1571 * following mounts are sufficiently divergent that functions are
1572 * duplicated. Typically there is a function foo(), and its RCU
1573 * analogue, foo_rcu().
1574 *
1575 * -ECHILD is the error number of choice (just to avoid clashes) that
1576 * is returned if some aspect of an rcu-walk fails. Such an error must
1577 * be handled by restarting a traditional ref-walk (which will always
1578 * be able to complete).
1579 */
Al Virobd92d7f2011-03-14 19:54:59 -04001580 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
Al Viroee0827c2011-02-21 23:38:09 -05001581
Al Virobd92d7f2011-03-14 19:54:59 -04001582 if (unlikely(err))
1583 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001584
1585 current->total_link_count = 0;
Al Virobd92d7f2011-03-14 19:54:59 -04001586 err = link_path_walk(name, nd);
1587
1588 if (!err && !(flags & LOOKUP_PARENT)) {
Al Virobd92d7f2011-03-14 19:54:59 -04001589 err = lookup_last(nd, &path);
1590 while (err > 0) {
1591 void *cookie;
1592 struct path link = path;
Al Virobd92d7f2011-03-14 19:54:59 -04001593 nd->flags |= LOOKUP_PARENT;
Al Viro574197e2011-03-14 22:20:34 -04001594 err = follow_link(&link, nd, &cookie);
Al Virobd92d7f2011-03-14 19:54:59 -04001595 if (!err)
1596 err = lookup_last(nd, &path);
Al Viro574197e2011-03-14 22:20:34 -04001597 put_link(nd, &link, cookie);
Al Virobd92d7f2011-03-14 19:54:59 -04001598 }
1599 }
Al Viroee0827c2011-02-21 23:38:09 -05001600
1601 if (nd->flags & LOOKUP_RCU) {
Al Viro4455ca62011-03-04 14:28:10 -05001602 /* went all way through without dropping RCU */
Al Virobd92d7f2011-03-14 19:54:59 -04001603 BUG_ON(err);
Al Viro4455ca62011-03-04 14:28:10 -05001604 if (nameidata_drop_rcu_last(nd))
Al Virobd92d7f2011-03-14 19:54:59 -04001605 err = -ECHILD;
Al Viroee0827c2011-02-21 23:38:09 -05001606 }
1607
Al Virobd23a532011-03-23 09:56:30 -04001608 if (!err) {
Al Virobd92d7f2011-03-14 19:54:59 -04001609 err = handle_reval_path(nd);
Al Virobd23a532011-03-23 09:56:30 -04001610 if (err)
1611 path_put(&nd->path);
1612 }
Al Virobd92d7f2011-03-14 19:54:59 -04001613
1614 if (!err && nd->flags & LOOKUP_DIRECTORY) {
1615 if (!nd->inode->i_op->lookup) {
1616 path_put(&nd->path);
Al Virobd23a532011-03-23 09:56:30 -04001617 err = -ENOTDIR;
Al Virobd92d7f2011-03-14 19:54:59 -04001618 }
1619 }
Al Viro16c2cd72011-02-22 15:50:10 -05001620
Al Viro70e9b352011-03-05 21:12:22 -05001621 if (base)
1622 fput(base);
Al Viroee0827c2011-02-21 23:38:09 -05001623
Al Viro5b6ca022011-03-09 23:04:47 -05001624 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
Al Viro2a737872009-04-07 11:49:53 -04001625 path_put(&nd->root);
1626 nd->root.mnt = NULL;
1627 }
Al Virobd92d7f2011-03-14 19:54:59 -04001628 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001629}
Nick Piggin31e6b012011-01-07 17:49:52 +11001630
Al Viroee0827c2011-02-21 23:38:09 -05001631static int do_path_lookup(int dfd, const char *name,
1632 unsigned int flags, struct nameidata *nd)
1633{
1634 int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1635 if (unlikely(retval == -ECHILD))
1636 retval = path_lookupat(dfd, name, flags, nd);
1637 if (unlikely(retval == -ESTALE))
1638 retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11001639
1640 if (likely(!retval)) {
1641 if (unlikely(!audit_dummy_context())) {
1642 if (nd->path.dentry && nd->inode)
1643 audit_inode(name, nd->path.dentry);
1644 }
1645 }
Al Viro9b4a9b12009-04-07 11:44:16 -04001646 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647}
1648
Al Viroc9c6cac2011-02-16 15:15:47 -05001649int kern_path_parent(const char *name, struct nameidata *nd)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001650{
Al Viroc9c6cac2011-02-16 15:15:47 -05001651 return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001652}
1653
Al Virod1811462008-08-02 00:49:18 -04001654int kern_path(const char *name, unsigned int flags, struct path *path)
1655{
1656 struct nameidata nd;
1657 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1658 if (!res)
1659 *path = nd.path;
1660 return res;
1661}
1662
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001663/**
1664 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1665 * @dentry: pointer to dentry of the base directory
1666 * @mnt: pointer to vfs mount of the base directory
1667 * @name: pointer to file name
1668 * @flags: lookup flags
1669 * @nd: pointer to nameidata
1670 */
1671int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
1672 const char *name, unsigned int flags,
1673 struct nameidata *nd)
1674{
Al Viro5b6ca022011-03-09 23:04:47 -05001675 nd->root.dentry = dentry;
1676 nd->root.mnt = mnt;
1677 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
1678 return do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, nd);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001679}
1680
Christoph Hellwigeead1912007-10-16 23:25:38 -07001681static struct dentry *__lookup_hash(struct qstr *name,
1682 struct dentry *base, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683{
Christoph Hellwig81fca442010-10-06 10:47:47 +02001684 struct inode *inode = base->d_inode;
James Morris057f6c02007-04-26 00:12:05 -07001685 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 int err;
1687
Nick Pigginb74c79e2011-01-07 17:49:58 +11001688 err = exec_permission(inode, 0);
Christoph Hellwig81fca442010-10-06 10:47:47 +02001689 if (err)
1690 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
1692 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001693 * Don't bother with __d_lookup: callers are for creat as
1694 * well as unlink, so a lot of the time it would cost
1695 * a double lookup.
Al Viro6e6b1bd2009-08-13 23:38:37 +04001696 */
Nick Pigginb04f7842010-08-18 04:37:34 +10001697 dentry = d_lookup(base, name);
Al Viro6e6b1bd2009-08-13 23:38:37 +04001698
Nick Pigginfb045ad2011-01-07 17:49:55 +11001699 if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE))
Al Viro6e6b1bd2009-08-13 23:38:37 +04001700 dentry = do_revalidate(dentry, nd);
1701
Nick Pigginbaa03892010-08-18 04:37:31 +10001702 if (!dentry)
1703 dentry = d_alloc_and_lookup(base, name, nd);
Al Viro5a202bc2011-03-08 14:17:44 -05001704
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 return dentry;
1706}
1707
James Morris057f6c02007-04-26 00:12:05 -07001708/*
1709 * Restricted form of lookup. Doesn't follow links, single-component only,
1710 * needs parent already locked. Doesn't follow mounts.
1711 * SMP-safe.
1712 */
Adrian Bunka244e162006-03-31 02:32:11 -08001713static struct dentry *lookup_hash(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714{
Jan Blunck4ac91372008-02-14 19:34:32 -08001715 return __lookup_hash(&nd->last, nd->path.dentry, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716}
1717
Christoph Hellwigeead1912007-10-16 23:25:38 -07001718/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07001719 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07001720 * @name: pathname component to lookup
1721 * @base: base directory to lookup from
1722 * @len: maximum length @len should be interpreted to
1723 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07001724 * Note that this routine is purely a helper for filesystem usage and should
1725 * not be called by generic code. Also note that by using this function the
Christoph Hellwigeead1912007-10-16 23:25:38 -07001726 * nameidata argument is passed to the filesystem methods and a filesystem
1727 * using this helper needs to be prepared for that.
1728 */
James Morris057f6c02007-04-26 00:12:05 -07001729struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1730{
James Morris057f6c02007-04-26 00:12:05 -07001731 struct qstr this;
Al Viro6a96ba52011-03-07 23:49:20 -05001732 unsigned long hash;
1733 unsigned int c;
James Morris057f6c02007-04-26 00:12:05 -07001734
David Woodhouse2f9092e2009-04-20 23:18:37 +01001735 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
1736
Al Viro6a96ba52011-03-07 23:49:20 -05001737 this.name = name;
1738 this.len = len;
1739 if (!len)
1740 return ERR_PTR(-EACCES);
1741
1742 hash = init_name_hash();
1743 while (len--) {
1744 c = *(const unsigned char *)name++;
1745 if (c == '/' || c == '\0')
1746 return ERR_PTR(-EACCES);
1747 hash = partial_name_hash(c, hash);
1748 }
1749 this.hash = end_name_hash(hash);
Al Viro5a202bc2011-03-08 14:17:44 -05001750 /*
1751 * See if the low-level filesystem might want
1752 * to use its own hash..
1753 */
1754 if (base->d_flags & DCACHE_OP_HASH) {
1755 int err = base->d_op->d_hash(base, base->d_inode, &this);
1756 if (err < 0)
1757 return ERR_PTR(err);
1758 }
Christoph Hellwigeead1912007-10-16 23:25:38 -07001759
Christoph Hellwig49705b72005-11-08 21:35:06 -08001760 return __lookup_hash(&this, base, NULL);
James Morris057f6c02007-04-26 00:12:05 -07001761}
1762
Al Viro2d8f3032008-07-22 09:59:21 -04001763int user_path_at(int dfd, const char __user *name, unsigned flags,
1764 struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765{
Al Viro2d8f3032008-07-22 09:59:21 -04001766 struct nameidata nd;
Al Virof52e0c12011-03-14 18:56:51 -04001767 char *tmp = getname_flags(name, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 int err = PTR_ERR(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 if (!IS_ERR(tmp)) {
Al Viro2d8f3032008-07-22 09:59:21 -04001770
1771 BUG_ON(flags & LOOKUP_PARENT);
1772
1773 err = do_path_lookup(dfd, tmp, flags, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 putname(tmp);
Al Viro2d8f3032008-07-22 09:59:21 -04001775 if (!err)
1776 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 }
1778 return err;
1779}
1780
Al Viro2ad94ae2008-07-21 09:32:51 -04001781static int user_path_parent(int dfd, const char __user *path,
1782 struct nameidata *nd, char **name)
1783{
1784 char *s = getname(path);
1785 int error;
1786
1787 if (IS_ERR(s))
1788 return PTR_ERR(s);
1789
1790 error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
1791 if (error)
1792 putname(s);
1793 else
1794 *name = s;
1795
1796 return error;
1797}
1798
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799/*
1800 * It's inline, so penalty for filesystems that don't use sticky bit is
1801 * minimal.
1802 */
1803static inline int check_sticky(struct inode *dir, struct inode *inode)
1804{
David Howellsda9592e2008-11-14 10:39:05 +11001805 uid_t fsuid = current_fsuid();
1806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 if (!(dir->i_mode & S_ISVTX))
1808 return 0;
Serge E. Hallyne795b712011-03-23 16:43:25 -07001809 if (current_user_ns() != inode_userns(inode))
1810 goto other_userns;
David Howellsda9592e2008-11-14 10:39:05 +11001811 if (inode->i_uid == fsuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 return 0;
David Howellsda9592e2008-11-14 10:39:05 +11001813 if (dir->i_uid == fsuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 return 0;
Serge E. Hallyne795b712011-03-23 16:43:25 -07001815
1816other_userns:
1817 return !ns_capable(inode_userns(inode), CAP_FOWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818}
1819
1820/*
1821 * Check whether we can remove a link victim from directory dir, check
1822 * whether the type of victim is right.
1823 * 1. We can't do it if dir is read-only (done in permission())
1824 * 2. We should have write and exec permissions on dir
1825 * 3. We can't remove anything from append-only dir
1826 * 4. We can't do anything with immutable dir (done in permission())
1827 * 5. If the sticky bit on dir is set we should either
1828 * a. be owner of dir, or
1829 * b. be owner of victim, or
1830 * c. have CAP_FOWNER capability
1831 * 6. If the victim is append-only or immutable we can't do antyhing with
1832 * links pointing to it.
1833 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1834 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1835 * 9. We can't remove a root or mountpoint.
1836 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1837 * nfs_async_unlink().
1838 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001839static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840{
1841 int error;
1842
1843 if (!victim->d_inode)
1844 return -ENOENT;
1845
1846 BUG_ON(victim->d_parent->d_inode != dir);
Al Virocccc6bb2009-12-25 05:07:33 -05001847 audit_inode_child(victim, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
Al Virof419a2e2008-07-22 00:07:17 -04001849 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 if (error)
1851 return error;
1852 if (IS_APPEND(dir))
1853 return -EPERM;
1854 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
Hugh Dickinsf9454542008-11-19 15:36:38 -08001855 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 return -EPERM;
1857 if (isdir) {
1858 if (!S_ISDIR(victim->d_inode->i_mode))
1859 return -ENOTDIR;
1860 if (IS_ROOT(victim))
1861 return -EBUSY;
1862 } else if (S_ISDIR(victim->d_inode->i_mode))
1863 return -EISDIR;
1864 if (IS_DEADDIR(dir))
1865 return -ENOENT;
1866 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1867 return -EBUSY;
1868 return 0;
1869}
1870
1871/* Check whether we can create an object with dentry child in directory
1872 * dir.
1873 * 1. We can't do it if child already exists (open has special treatment for
1874 * this case, but since we are inlined it's OK)
1875 * 2. We can't do it if dir is read-only (done in permission())
1876 * 3. We should have write and exec permissions on dir
1877 * 4. We can't do it if dir is immutable (done in permission())
1878 */
Miklos Szeredia95164d2008-07-30 15:08:48 +02001879static inline int may_create(struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880{
1881 if (child->d_inode)
1882 return -EEXIST;
1883 if (IS_DEADDIR(dir))
1884 return -ENOENT;
Al Virof419a2e2008-07-22 00:07:17 -04001885 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886}
1887
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888/*
1889 * p1 and p2 should be directories on the same fs.
1890 */
1891struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1892{
1893 struct dentry *p;
1894
1895 if (p1 == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07001896 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 return NULL;
1898 }
1899
Arjan van de Vena11f3a02006-03-23 03:00:33 -08001900 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001902 p = d_ancestor(p2, p1);
1903 if (p) {
1904 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1905 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
1906 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 }
1908
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001909 p = d_ancestor(p1, p2);
1910 if (p) {
1911 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1912 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1913 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 }
1915
Ingo Molnarf2eace22006-07-03 00:25:05 -07001916 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1917 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 return NULL;
1919}
1920
1921void unlock_rename(struct dentry *p1, struct dentry *p2)
1922{
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001923 mutex_unlock(&p1->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 if (p1 != p2) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001925 mutex_unlock(&p2->d_inode->i_mutex);
Arjan van de Vena11f3a02006-03-23 03:00:33 -08001926 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 }
1928}
1929
1930int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1931 struct nameidata *nd)
1932{
Miklos Szeredia95164d2008-07-30 15:08:48 +02001933 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
1935 if (error)
1936 return error;
1937
Al Viroacfa4382008-12-04 10:06:33 -05001938 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 return -EACCES; /* shouldn't it be ENOSYS? */
1940 mode &= S_IALLUGO;
1941 mode |= S_IFREG;
1942 error = security_inode_create(dir, dentry, mode);
1943 if (error)
1944 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 error = dir->i_op->create(dir, dentry, mode, nd);
Stephen Smalleya74574a2005-09-09 13:01:44 -07001946 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00001947 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 return error;
1949}
1950
Al Viro73d049a2011-03-11 12:08:24 -05001951static int may_open(struct path *path, int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001953 struct dentry *dentry = path->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 struct inode *inode = dentry->d_inode;
1955 int error;
1956
Al Virobcda7652011-03-13 16:42:14 -04001957 /* O_PATH? */
1958 if (!acc_mode)
1959 return 0;
1960
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 if (!inode)
1962 return -ENOENT;
1963
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001964 switch (inode->i_mode & S_IFMT) {
1965 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001967 case S_IFDIR:
1968 if (acc_mode & MAY_WRITE)
1969 return -EISDIR;
1970 break;
1971 case S_IFBLK:
1972 case S_IFCHR:
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001973 if (path->mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001975 /*FALLTHRU*/
1976 case S_IFIFO:
1977 case S_IFSOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001979 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08001980 }
Dave Hansenb41572e2007-10-16 23:31:14 -07001981
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001982 error = inode_permission(inode, acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07001983 if (error)
1984 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05001985
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 /*
1987 * An append-only file must be opened in append mode for writing.
1988 */
1989 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05001990 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05001991 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05001993 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 }
1995
1996 /* O_NOATIME can only be set by the owner or superuser */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001997 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
Al Viro7715b522009-12-16 03:54:00 -05001998 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
2000 /*
2001 * Ensure there are no outstanding leases on the file.
2002 */
Al Virob65a9cf2009-12-16 06:27:40 -05002003 return break_lease(inode, flag);
Al Viro7715b522009-12-16 03:54:00 -05002004}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
Jeff Laytone1181ee2010-12-07 16:19:50 -05002006static int handle_truncate(struct file *filp)
Al Viro7715b522009-12-16 03:54:00 -05002007{
Jeff Laytone1181ee2010-12-07 16:19:50 -05002008 struct path *path = &filp->f_path;
Al Viro7715b522009-12-16 03:54:00 -05002009 struct inode *inode = path->dentry->d_inode;
2010 int error = get_write_access(inode);
2011 if (error)
2012 return error;
2013 /*
2014 * Refuse to truncate files with mandatory locks held on them.
2015 */
2016 error = locks_verify_locked(inode);
2017 if (!error)
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +09002018 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05002019 if (!error) {
2020 error = do_truncate(path->dentry, 0,
2021 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
Jeff Laytone1181ee2010-12-07 16:19:50 -05002022 filp);
Al Viro7715b522009-12-16 03:54:00 -05002023 }
2024 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04002025 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026}
2027
Dave Hansend57999e2008-02-15 14:37:27 -08002028/*
Dave Hansend57999e2008-02-15 14:37:27 -08002029 * Note that while the flag value (low two bits) for sys_open means:
2030 * 00 - read-only
2031 * 01 - write-only
2032 * 10 - read-write
2033 * 11 - special
2034 * it is changed into
2035 * 00 - no permissions needed
2036 * 01 - read-permission
2037 * 10 - write-permission
2038 * 11 - read-write
2039 * for the internal routines (ie open_namei()/follow_link() etc)
2040 * This is more logical, and also allows the 00 "no perm needed"
2041 * to be used for symlinks (where the permissions are checked
2042 * later).
2043 *
2044*/
2045static inline int open_to_namei_flags(int flag)
2046{
2047 if ((flag+1) & O_ACCMODE)
2048 flag++;
2049 return flag;
2050}
2051
Nick Piggin31e6b012011-01-07 17:49:52 +11002052/*
Al Virofe2d35f2011-03-05 22:58:25 -05002053 * Handle the last step of open()
Nick Piggin31e6b012011-01-07 17:49:52 +11002054 */
Al Virofb1cc552009-12-24 01:58:28 -05002055static struct file *do_last(struct nameidata *nd, struct path *path,
Al Viroc3e380b2011-02-23 13:39:45 -05002056 const struct open_flags *op, const char *pathname)
Al Virofb1cc552009-12-24 01:58:28 -05002057{
Al Viroa1e28032009-12-24 02:12:06 -05002058 struct dentry *dir = nd->path.dentry;
Al Viro6c0d46c2011-03-09 00:59:59 -05002059 struct dentry *dentry;
Al Viroca344a892011-03-09 00:36:45 -05002060 int open_flag = op->open_flag;
Al Viro6c0d46c2011-03-09 00:59:59 -05002061 int will_truncate = open_flag & O_TRUNC;
Al Viroca344a892011-03-09 00:36:45 -05002062 int want_write = 0;
Al Virobcda7652011-03-13 16:42:14 -04002063 int acc_mode = op->acc_mode;
Al Virofb1cc552009-12-24 01:58:28 -05002064 struct file *filp;
Al Viro16c2cd72011-02-22 15:50:10 -05002065 int error;
Al Virofb1cc552009-12-24 01:58:28 -05002066
Al Viroc3e380b2011-02-23 13:39:45 -05002067 nd->flags &= ~LOOKUP_PARENT;
2068 nd->flags |= op->intent;
2069
Al Viro1f36f772009-12-26 10:56:19 -05002070 switch (nd->last_type) {
2071 case LAST_DOTDOT:
Neil Brown176306f2010-05-24 16:57:56 +10002072 case LAST_DOT:
Al Virofe2d35f2011-03-05 22:58:25 -05002073 error = handle_dots(nd, nd->last_type);
2074 if (error)
2075 return ERR_PTR(error);
Al Viro1f36f772009-12-26 10:56:19 -05002076 /* fallthrough */
Al Viro1f36f772009-12-26 10:56:19 -05002077 case LAST_ROOT:
Al Virofe2d35f2011-03-05 22:58:25 -05002078 if (nd->flags & LOOKUP_RCU) {
2079 if (nameidata_drop_rcu_last(nd))
2080 return ERR_PTR(-ECHILD);
2081 }
Al Viro16c2cd72011-02-22 15:50:10 -05002082 error = handle_reval_path(nd);
2083 if (error)
2084 goto exit;
Al Virofe2d35f2011-03-05 22:58:25 -05002085 audit_inode(pathname, nd->path.dentry);
Al Viroca344a892011-03-09 00:36:45 -05002086 if (open_flag & O_CREAT) {
Al Virofe2d35f2011-03-05 22:58:25 -05002087 error = -EISDIR;
2088 goto exit;
2089 }
2090 goto ok;
Al Viro1f36f772009-12-26 10:56:19 -05002091 case LAST_BIND:
Al Virofe2d35f2011-03-05 22:58:25 -05002092 /* can't be RCU mode here */
Al Viro16c2cd72011-02-22 15:50:10 -05002093 error = handle_reval_path(nd);
2094 if (error)
2095 goto exit;
Al Viro1f36f772009-12-26 10:56:19 -05002096 audit_inode(pathname, dir);
Al Viro67ee3ad2009-12-26 07:01:01 -05002097 goto ok;
Al Viro1f36f772009-12-26 10:56:19 -05002098 }
Al Viro67ee3ad2009-12-26 07:01:01 -05002099
Al Viroca344a892011-03-09 00:36:45 -05002100 if (!(open_flag & O_CREAT)) {
Al Virobcda7652011-03-13 16:42:14 -04002101 int symlink_ok = 0;
Al Virofe2d35f2011-03-05 22:58:25 -05002102 if (nd->last.name[nd->last.len])
2103 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
Al Virobcda7652011-03-13 16:42:14 -04002104 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2105 symlink_ok = 1;
Al Virofe2d35f2011-03-05 22:58:25 -05002106 /* we _can_ be in RCU mode here */
Al Viroce57dfc2011-03-13 19:58:58 -04002107 error = walk_component(nd, path, &nd->last, LAST_NORM,
2108 !symlink_ok);
2109 if (error < 0)
Al Virofe2d35f2011-03-05 22:58:25 -05002110 return ERR_PTR(error);
Al Viroce57dfc2011-03-13 19:58:58 -04002111 if (error) /* symlink */
Al Virofe2d35f2011-03-05 22:58:25 -05002112 return NULL;
Al Virofe2d35f2011-03-05 22:58:25 -05002113 /* sayonara */
2114 if (nd->flags & LOOKUP_RCU) {
2115 if (nameidata_drop_rcu_last(nd))
2116 return ERR_PTR(-ECHILD);
2117 }
2118
2119 error = -ENOTDIR;
2120 if (nd->flags & LOOKUP_DIRECTORY) {
Al Viroce57dfc2011-03-13 19:58:58 -04002121 if (!nd->inode->i_op->lookup)
Al Virofe2d35f2011-03-05 22:58:25 -05002122 goto exit;
2123 }
2124 audit_inode(pathname, nd->path.dentry);
2125 goto ok;
2126 }
2127
2128 /* create side of things */
2129
2130 if (nd->flags & LOOKUP_RCU) {
2131 if (nameidata_drop_rcu_last(nd))
2132 return ERR_PTR(-ECHILD);
2133 }
2134
2135 audit_inode(pathname, dir);
Al Viro16c2cd72011-02-22 15:50:10 -05002136 error = -EISDIR;
Al Viro1f36f772009-12-26 10:56:19 -05002137 /* trailing slashes? */
Nick Piggin31e6b012011-01-07 17:49:52 +11002138 if (nd->last.name[nd->last.len])
2139 goto exit;
Al Viroa2c36b42009-12-24 03:39:50 -05002140
Al Viroa1e28032009-12-24 02:12:06 -05002141 mutex_lock(&dir->d_inode->i_mutex);
2142
Al Viro6c0d46c2011-03-09 00:59:59 -05002143 dentry = lookup_hash(nd);
2144 error = PTR_ERR(dentry);
2145 if (IS_ERR(dentry)) {
Al Virofb1cc552009-12-24 01:58:28 -05002146 mutex_unlock(&dir->d_inode->i_mutex);
2147 goto exit;
2148 }
2149
Al Viro6c0d46c2011-03-09 00:59:59 -05002150 path->dentry = dentry;
2151 path->mnt = nd->path.mnt;
2152
Al Virofb1cc552009-12-24 01:58:28 -05002153 /* Negative dentry, just create the file */
Al Viro6c0d46c2011-03-09 00:59:59 -05002154 if (!dentry->d_inode) {
2155 int mode = op->mode;
2156 if (!IS_POSIXACL(dir->d_inode))
2157 mode &= ~current_umask();
Al Virofb1cc552009-12-24 01:58:28 -05002158 /*
2159 * This write is needed to ensure that a
Al Viro6c0d46c2011-03-09 00:59:59 -05002160 * rw->ro transition does not occur between
Al Virofb1cc552009-12-24 01:58:28 -05002161 * the time when the file is created and when
2162 * a permanent write count is taken through
2163 * the 'struct file' in nameidata_to_filp().
2164 */
2165 error = mnt_want_write(nd->path.mnt);
2166 if (error)
2167 goto exit_mutex_unlock;
Al Viroca344a892011-03-09 00:36:45 -05002168 want_write = 1;
Al Viro9b44f1b2011-03-09 00:17:27 -05002169 /* Don't check for write permission, don't truncate */
Al Viroca344a892011-03-09 00:36:45 -05002170 open_flag &= ~O_TRUNC;
Al Viro6c0d46c2011-03-09 00:59:59 -05002171 will_truncate = 0;
Al Virobcda7652011-03-13 16:42:14 -04002172 acc_mode = MAY_OPEN;
Al Viro6c0d46c2011-03-09 00:59:59 -05002173 error = security_path_mknod(&nd->path, dentry, mode, 0);
2174 if (error)
2175 goto exit_mutex_unlock;
2176 error = vfs_create(dir->d_inode, dentry, mode, nd);
2177 if (error)
2178 goto exit_mutex_unlock;
2179 mutex_unlock(&dir->d_inode->i_mutex);
2180 dput(nd->path.dentry);
2181 nd->path.dentry = dentry;
Al Viroca344a892011-03-09 00:36:45 -05002182 goto common;
Al Virofb1cc552009-12-24 01:58:28 -05002183 }
2184
2185 /*
2186 * It already exists.
2187 */
2188 mutex_unlock(&dir->d_inode->i_mutex);
2189 audit_inode(pathname, path->dentry);
2190
2191 error = -EEXIST;
Al Viroca344a892011-03-09 00:36:45 -05002192 if (open_flag & O_EXCL)
Al Virofb1cc552009-12-24 01:58:28 -05002193 goto exit_dput;
2194
David Howells9875cf82011-01-14 18:45:21 +00002195 error = follow_managed(path, nd->flags);
2196 if (error < 0)
2197 goto exit_dput;
Al Virofb1cc552009-12-24 01:58:28 -05002198
2199 error = -ENOENT;
2200 if (!path->dentry->d_inode)
2201 goto exit_dput;
Al Viro9e67f362009-12-26 07:04:50 -05002202
2203 if (path->dentry->d_inode->i_op->follow_link)
Al Virofb1cc552009-12-24 01:58:28 -05002204 return NULL;
Al Virofb1cc552009-12-24 01:58:28 -05002205
2206 path_to_nameidata(path, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11002207 nd->inode = path->dentry->d_inode;
Al Virofb1cc552009-12-24 01:58:28 -05002208 error = -EISDIR;
Nick Piggin31e6b012011-01-07 17:49:52 +11002209 if (S_ISDIR(nd->inode->i_mode))
Al Virofb1cc552009-12-24 01:58:28 -05002210 goto exit;
Al Viro67ee3ad2009-12-26 07:01:01 -05002211ok:
Al Viro6c0d46c2011-03-09 00:59:59 -05002212 if (!S_ISREG(nd->inode->i_mode))
2213 will_truncate = 0;
2214
Al Viro0f9d1a12011-03-09 00:13:14 -05002215 if (will_truncate) {
2216 error = mnt_want_write(nd->path.mnt);
2217 if (error)
2218 goto exit;
Al Viroca344a892011-03-09 00:36:45 -05002219 want_write = 1;
Al Viro0f9d1a12011-03-09 00:13:14 -05002220 }
Al Viroca344a892011-03-09 00:36:45 -05002221common:
Al Virobcda7652011-03-13 16:42:14 -04002222 error = may_open(&nd->path, acc_mode, open_flag);
Al Viroca344a892011-03-09 00:36:45 -05002223 if (error)
Al Viro0f9d1a12011-03-09 00:13:14 -05002224 goto exit;
Al Viro0f9d1a12011-03-09 00:13:14 -05002225 filp = nameidata_to_filp(nd);
2226 if (!IS_ERR(filp)) {
2227 error = ima_file_check(filp, op->acc_mode);
2228 if (error) {
2229 fput(filp);
2230 filp = ERR_PTR(error);
2231 }
2232 }
2233 if (!IS_ERR(filp)) {
2234 if (will_truncate) {
2235 error = handle_truncate(filp);
2236 if (error) {
2237 fput(filp);
2238 filp = ERR_PTR(error);
2239 }
2240 }
2241 }
Al Viroca344a892011-03-09 00:36:45 -05002242out:
2243 if (want_write)
Al Viro0f9d1a12011-03-09 00:13:14 -05002244 mnt_drop_write(nd->path.mnt);
2245 path_put(&nd->path);
Al Virofb1cc552009-12-24 01:58:28 -05002246 return filp;
2247
2248exit_mutex_unlock:
2249 mutex_unlock(&dir->d_inode->i_mutex);
2250exit_dput:
2251 path_put_conditional(path, nd);
2252exit:
Al Viroca344a892011-03-09 00:36:45 -05002253 filp = ERR_PTR(error);
2254 goto out;
Al Virofb1cc552009-12-24 01:58:28 -05002255}
2256
Al Viro13aab422011-02-23 17:54:08 -05002257static struct file *path_openat(int dfd, const char *pathname,
Al Viro73d049a2011-03-11 12:08:24 -05002258 struct nameidata *nd, const struct open_flags *op, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259{
Al Virofe2d35f2011-03-05 22:58:25 -05002260 struct file *base = NULL;
Dave Hansen4a3fd212008-02-15 14:37:48 -08002261 struct file *filp;
Al Viro9850c052010-01-13 15:01:15 -05002262 struct path path;
Al Viro13aab422011-02-23 17:54:08 -05002263 int error;
Nick Piggin31e6b012011-01-07 17:49:52 +11002264
2265 filp = get_empty_filp();
2266 if (!filp)
2267 return ERR_PTR(-ENFILE);
2268
Al Viro47c805d2011-02-23 17:44:09 -05002269 filp->f_flags = op->open_flag;
Al Viro73d049a2011-03-11 12:08:24 -05002270 nd->intent.open.file = filp;
2271 nd->intent.open.flags = open_to_namei_flags(op->open_flag);
2272 nd->intent.open.create_mode = op->mode;
Nick Piggin31e6b012011-01-07 17:49:52 +11002273
Al Viro73d049a2011-03-11 12:08:24 -05002274 error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
Nick Piggin31e6b012011-01-07 17:49:52 +11002275 if (unlikely(error))
Al Viro13aab422011-02-23 17:54:08 -05002276 goto out_filp;
Nick Piggin31e6b012011-01-07 17:49:52 +11002277
Al Virofe2d35f2011-03-05 22:58:25 -05002278 current->total_link_count = 0;
Al Viro73d049a2011-03-11 12:08:24 -05002279 error = link_path_walk(pathname, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11002280 if (unlikely(error))
2281 goto out_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
Al Viro73d049a2011-03-11 12:08:24 -05002283 filp = do_last(nd, &path, op, pathname);
Al Viro806b6812009-12-26 07:16:40 -05002284 while (unlikely(!filp)) { /* trailing symlink */
Nick Piggin7b9337a2011-01-14 08:42:43 +00002285 struct path link = path;
Al Virodef4af32009-12-26 08:37:05 -05002286 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04002287 if (!(nd->flags & LOOKUP_FOLLOW)) {
Al Viro73d049a2011-03-11 12:08:24 -05002288 path_put_conditional(&path, nd);
2289 path_put(&nd->path);
Al Viro40b39132011-03-09 16:22:18 -05002290 filp = ERR_PTR(-ELOOP);
2291 break;
2292 }
Al Viro73d049a2011-03-11 12:08:24 -05002293 nd->flags |= LOOKUP_PARENT;
2294 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
Al Viro574197e2011-03-14 22:20:34 -04002295 error = follow_link(&link, nd, &cookie);
Al Viroc3e380b2011-02-23 13:39:45 -05002296 if (unlikely(error))
Al Virof1afe9e2011-02-22 22:27:28 -05002297 filp = ERR_PTR(error);
Al Viroc3e380b2011-02-23 13:39:45 -05002298 else
Al Viro73d049a2011-03-11 12:08:24 -05002299 filp = do_last(nd, &path, op, pathname);
Al Viro574197e2011-03-14 22:20:34 -04002300 put_link(nd, &link, cookie);
Al Viro806b6812009-12-26 07:16:40 -05002301 }
Al Viro10fa8e62009-12-26 07:09:49 -05002302out:
Al Viro73d049a2011-03-11 12:08:24 -05002303 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
2304 path_put(&nd->root);
Al Virofe2d35f2011-03-05 22:58:25 -05002305 if (base)
2306 fput(base);
Al Viro73d049a2011-03-11 12:08:24 -05002307 release_open_intent(nd);
Al Viro10fa8e62009-12-26 07:09:49 -05002308 return filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
Nick Piggin31e6b012011-01-07 17:49:52 +11002310out_filp:
Al Viro806b6812009-12-26 07:16:40 -05002311 filp = ERR_PTR(error);
Al Viro10fa8e62009-12-26 07:09:49 -05002312 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313}
2314
Al Viro13aab422011-02-23 17:54:08 -05002315struct file *do_filp_open(int dfd, const char *pathname,
2316 const struct open_flags *op, int flags)
2317{
Al Viro73d049a2011-03-11 12:08:24 -05002318 struct nameidata nd;
Al Viro13aab422011-02-23 17:54:08 -05002319 struct file *filp;
2320
Al Viro73d049a2011-03-11 12:08:24 -05002321 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
Al Viro13aab422011-02-23 17:54:08 -05002322 if (unlikely(filp == ERR_PTR(-ECHILD)))
Al Viro73d049a2011-03-11 12:08:24 -05002323 filp = path_openat(dfd, pathname, &nd, op, flags);
Al Viro13aab422011-02-23 17:54:08 -05002324 if (unlikely(filp == ERR_PTR(-ESTALE)))
Al Viro73d049a2011-03-11 12:08:24 -05002325 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
Al Viro13aab422011-02-23 17:54:08 -05002326 return filp;
2327}
2328
Al Viro73d049a2011-03-11 12:08:24 -05002329struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
2330 const char *name, const struct open_flags *op, int flags)
2331{
2332 struct nameidata nd;
2333 struct file *file;
2334
2335 nd.root.mnt = mnt;
2336 nd.root.dentry = dentry;
2337
2338 flags |= LOOKUP_ROOT;
2339
Al Virobcda7652011-03-13 16:42:14 -04002340 if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
Al Viro73d049a2011-03-11 12:08:24 -05002341 return ERR_PTR(-ELOOP);
2342
2343 file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
2344 if (unlikely(file == ERR_PTR(-ECHILD)))
2345 file = path_openat(-1, name, &nd, op, flags);
2346 if (unlikely(file == ERR_PTR(-ESTALE)))
2347 file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
2348 return file;
2349}
2350
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351/**
2352 * lookup_create - lookup a dentry, creating it if it doesn't exist
2353 * @nd: nameidata info
2354 * @is_dir: directory flag
2355 *
2356 * Simple function to lookup and return a dentry and create it
2357 * if it doesn't exist. Is SMP-safe.
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002358 *
Jan Blunck4ac91372008-02-14 19:34:32 -08002359 * Returns with nd->path.dentry->d_inode->i_mutex locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 */
2361struct dentry *lookup_create(struct nameidata *nd, int is_dir)
2362{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002363 struct dentry *dentry = ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
Jan Blunck4ac91372008-02-14 19:34:32 -08002365 mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002366 /*
2367 * Yucky last component or no last component at all?
2368 * (foo/., foo/.., /////)
2369 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 if (nd->last_type != LAST_NORM)
2371 goto fail;
2372 nd->flags &= ~LOOKUP_PARENT;
Al Viro35165862008-08-05 03:00:49 -04002373 nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
ASANO Masahiroa6349042006-08-22 20:06:02 -04002374 nd->intent.open.flags = O_EXCL;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002375
2376 /*
2377 * Do the final lookup.
2378 */
Christoph Hellwig49705b72005-11-08 21:35:06 -08002379 dentry = lookup_hash(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 if (IS_ERR(dentry))
2381 goto fail;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002382
Al Viroe9baf6e2008-05-15 04:49:12 -04002383 if (dentry->d_inode)
2384 goto eexist;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002385 /*
2386 * Special case - lookup gave negative, but... we had foo/bar/
2387 * From the vfs_mknod() POV we just have a negative dentry -
2388 * all is fine. Let's be bastards - you had / on the end, you've
2389 * been asking for (non-existent) directory. -ENOENT for you.
2390 */
Al Viroe9baf6e2008-05-15 04:49:12 -04002391 if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
2392 dput(dentry);
2393 dentry = ERR_PTR(-ENOENT);
2394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 return dentry;
Al Viroe9baf6e2008-05-15 04:49:12 -04002396eexist:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 dput(dentry);
Al Viroe9baf6e2008-05-15 04:49:12 -04002398 dentry = ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399fail:
2400 return dentry;
2401}
Christoph Hellwigf81a0bf2005-05-19 12:26:43 -07002402EXPORT_SYMBOL_GPL(lookup_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
2404int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
2405{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002406 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
2408 if (error)
2409 return error;
2410
Serge E. Hallyne795b712011-03-23 16:43:25 -07002411 if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
2412 !ns_capable(inode_userns(dir), CAP_MKNOD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 return -EPERM;
2414
Al Viroacfa4382008-12-04 10:06:33 -05002415 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 return -EPERM;
2417
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07002418 error = devcgroup_inode_mknod(mode, dev);
2419 if (error)
2420 return error;
2421
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 error = security_inode_mknod(dir, dentry, mode, dev);
2423 if (error)
2424 return error;
2425
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002427 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002428 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 return error;
2430}
2431
Dave Hansen463c3192008-02-15 14:37:57 -08002432static int may_mknod(mode_t mode)
2433{
2434 switch (mode & S_IFMT) {
2435 case S_IFREG:
2436 case S_IFCHR:
2437 case S_IFBLK:
2438 case S_IFIFO:
2439 case S_IFSOCK:
2440 case 0: /* zero mode translates to S_IFREG */
2441 return 0;
2442 case S_IFDIR:
2443 return -EPERM;
2444 default:
2445 return -EINVAL;
2446 }
2447}
2448
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002449SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
2450 unsigned, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451{
Al Viro2ad94ae2008-07-21 09:32:51 -04002452 int error;
2453 char *tmp;
2454 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 struct nameidata nd;
2456
2457 if (S_ISDIR(mode))
2458 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
Al Viro2ad94ae2008-07-21 09:32:51 -04002460 error = user_path_parent(dfd, filename, &nd, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002462 return error;
2463
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 dentry = lookup_create(&nd, 0);
Dave Hansen463c3192008-02-15 14:37:57 -08002465 if (IS_ERR(dentry)) {
2466 error = PTR_ERR(dentry);
2467 goto out_unlock;
2468 }
Jan Blunck4ac91372008-02-14 19:34:32 -08002469 if (!IS_POSIXACL(nd.path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04002470 mode &= ~current_umask();
Dave Hansen463c3192008-02-15 14:37:57 -08002471 error = may_mknod(mode);
2472 if (error)
2473 goto out_dput;
2474 error = mnt_want_write(nd.path.mnt);
2475 if (error)
2476 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002477 error = security_path_mknod(&nd.path, dentry, mode, dev);
2478 if (error)
2479 goto out_drop_write;
Dave Hansen463c3192008-02-15 14:37:57 -08002480 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 case 0: case S_IFREG:
Jan Blunck4ac91372008-02-14 19:34:32 -08002482 error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 break;
2484 case S_IFCHR: case S_IFBLK:
Jan Blunck4ac91372008-02-14 19:34:32 -08002485 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 new_decode_dev(dev));
2487 break;
2488 case S_IFIFO: case S_IFSOCK:
Jan Blunck4ac91372008-02-14 19:34:32 -08002489 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 }
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002492out_drop_write:
Dave Hansen463c3192008-02-15 14:37:57 -08002493 mnt_drop_write(nd.path.mnt);
2494out_dput:
2495 dput(dentry);
2496out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002497 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002498 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 putname(tmp);
2500
2501 return error;
2502}
2503
Heiko Carstens3480b252009-01-14 14:14:16 +01002504SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002505{
2506 return sys_mknodat(AT_FDCWD, filename, mode, dev);
2507}
2508
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2510{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002511 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
2513 if (error)
2514 return error;
2515
Al Viroacfa4382008-12-04 10:06:33 -05002516 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 return -EPERM;
2518
2519 mode &= (S_IRWXUGO|S_ISVTX);
2520 error = security_inode_mkdir(dir, dentry, mode);
2521 if (error)
2522 return error;
2523
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002525 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002526 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 return error;
2528}
2529
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002530SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531{
2532 int error = 0;
2533 char * tmp;
Dave Hansen6902d922006-09-30 23:29:01 -07002534 struct dentry *dentry;
2535 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536
Al Viro2ad94ae2008-07-21 09:32:51 -04002537 error = user_path_parent(dfd, pathname, &nd, &tmp);
2538 if (error)
Dave Hansen6902d922006-09-30 23:29:01 -07002539 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
Dave Hansen6902d922006-09-30 23:29:01 -07002541 dentry = lookup_create(&nd, 1);
2542 error = PTR_ERR(dentry);
2543 if (IS_ERR(dentry))
2544 goto out_unlock;
2545
Jan Blunck4ac91372008-02-14 19:34:32 -08002546 if (!IS_POSIXACL(nd.path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04002547 mode &= ~current_umask();
Dave Hansen463c3192008-02-15 14:37:57 -08002548 error = mnt_want_write(nd.path.mnt);
2549 if (error)
2550 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002551 error = security_path_mkdir(&nd.path, dentry, mode);
2552 if (error)
2553 goto out_drop_write;
Jan Blunck4ac91372008-02-14 19:34:32 -08002554 error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002555out_drop_write:
Dave Hansen463c3192008-02-15 14:37:57 -08002556 mnt_drop_write(nd.path.mnt);
2557out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002558 dput(dentry);
2559out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002560 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002561 path_put(&nd.path);
Dave Hansen6902d922006-09-30 23:29:01 -07002562 putname(tmp);
2563out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 return error;
2565}
2566
Heiko Carstens3cdad422009-01-14 14:14:22 +01002567SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002568{
2569 return sys_mkdirat(AT_FDCWD, pathname, mode);
2570}
2571
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572/*
2573 * We try to drop the dentry early: we should have
2574 * a usage count of 2 if we're the only user of this
2575 * dentry, and if that is true (possibly after pruning
2576 * the dcache), then we drop the dentry now.
2577 *
2578 * A low-level filesystem can, if it choses, legally
2579 * do a
2580 *
2581 * if (!d_unhashed(dentry))
2582 * return -EBUSY;
2583 *
2584 * if it cannot handle the case of removing a directory
2585 * that is still in use by something else..
2586 */
2587void dentry_unhash(struct dentry *dentry)
2588{
2589 dget(dentry);
Vasily Averindc168422006-12-06 20:37:07 -08002590 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 spin_lock(&dentry->d_lock);
Nick Pigginb7ab39f2011-01-07 17:49:32 +11002592 if (dentry->d_count == 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 __d_drop(dentry);
2594 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595}
2596
2597int vfs_rmdir(struct inode *dir, struct dentry *dentry)
2598{
2599 int error = may_delete(dir, dentry, 1);
2600
2601 if (error)
2602 return error;
2603
Al Viroacfa4382008-12-04 10:06:33 -05002604 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 return -EPERM;
2606
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002607 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 dentry_unhash(dentry);
2609 if (d_mountpoint(dentry))
2610 error = -EBUSY;
2611 else {
2612 error = security_inode_rmdir(dir, dentry);
2613 if (!error) {
2614 error = dir->i_op->rmdir(dir, dentry);
Al Virod83c49f2010-04-30 17:17:09 -04002615 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 dentry->d_inode->i_flags |= S_DEAD;
Al Virod83c49f2010-04-30 17:17:09 -04002617 dont_mount(dentry);
2618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 }
2620 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002621 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 d_delete(dentry);
2624 }
2625 dput(dentry);
2626
2627 return error;
2628}
2629
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002630static long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631{
2632 int error = 0;
2633 char * name;
2634 struct dentry *dentry;
2635 struct nameidata nd;
2636
Al Viro2ad94ae2008-07-21 09:32:51 -04002637 error = user_path_parent(dfd, pathname, &nd, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002639 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
2641 switch(nd.last_type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002642 case LAST_DOTDOT:
2643 error = -ENOTEMPTY;
2644 goto exit1;
2645 case LAST_DOT:
2646 error = -EINVAL;
2647 goto exit1;
2648 case LAST_ROOT:
2649 error = -EBUSY;
2650 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002652
2653 nd.flags &= ~LOOKUP_PARENT;
2654
Jan Blunck4ac91372008-02-14 19:34:32 -08002655 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002656 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002658 if (IS_ERR(dentry))
2659 goto exit2;
Dave Hansen06227532008-02-15 14:37:34 -08002660 error = mnt_want_write(nd.path.mnt);
2661 if (error)
2662 goto exit3;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002663 error = security_path_rmdir(&nd.path, dentry);
2664 if (error)
2665 goto exit4;
Jan Blunck4ac91372008-02-14 19:34:32 -08002666 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002667exit4:
Dave Hansen06227532008-02-15 14:37:34 -08002668 mnt_drop_write(nd.path.mnt);
2669exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07002670 dput(dentry);
2671exit2:
Jan Blunck4ac91372008-02-14 19:34:32 -08002672 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002674 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 putname(name);
2676 return error;
2677}
2678
Heiko Carstens3cdad422009-01-14 14:14:22 +01002679SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002680{
2681 return do_rmdir(AT_FDCWD, pathname);
2682}
2683
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684int vfs_unlink(struct inode *dir, struct dentry *dentry)
2685{
2686 int error = may_delete(dir, dentry, 0);
2687
2688 if (error)
2689 return error;
2690
Al Viroacfa4382008-12-04 10:06:33 -05002691 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 return -EPERM;
2693
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002694 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 if (d_mountpoint(dentry))
2696 error = -EBUSY;
2697 else {
2698 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05002699 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 error = dir->i_op->unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05002701 if (!error)
Al Virod83c49f2010-04-30 17:17:09 -04002702 dont_mount(dentry);
Al Virobec10522010-03-03 14:12:08 -05002703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002705 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706
2707 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2708 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
Jan Karaece95912008-02-06 01:37:13 -08002709 fsnotify_link_count(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 }
Robert Love0eeca282005-07-12 17:06:03 -04002712
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 return error;
2714}
2715
2716/*
2717 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002718 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 * writeout happening, and we don't want to prevent access to the directory
2720 * while waiting on the I/O.
2721 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002722static long do_unlinkat(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723{
Al Viro2ad94ae2008-07-21 09:32:51 -04002724 int error;
2725 char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 struct dentry *dentry;
2727 struct nameidata nd;
2728 struct inode *inode = NULL;
2729
Al Viro2ad94ae2008-07-21 09:32:51 -04002730 error = user_path_parent(dfd, pathname, &nd, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002732 return error;
2733
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 error = -EISDIR;
2735 if (nd.last_type != LAST_NORM)
2736 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002737
2738 nd.flags &= ~LOOKUP_PARENT;
2739
Jan Blunck4ac91372008-02-14 19:34:32 -08002740 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002741 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 error = PTR_ERR(dentry);
2743 if (!IS_ERR(dentry)) {
2744 /* Why not before? Because we want correct error value */
2745 if (nd.last.name[nd.last.len])
2746 goto slashes;
2747 inode = dentry->d_inode;
2748 if (inode)
Al Viro7de9c6e2010-10-23 11:11:40 -04002749 ihold(inode);
Dave Hansen06227532008-02-15 14:37:34 -08002750 error = mnt_want_write(nd.path.mnt);
2751 if (error)
2752 goto exit2;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002753 error = security_path_unlink(&nd.path, dentry);
2754 if (error)
2755 goto exit3;
Jan Blunck4ac91372008-02-14 19:34:32 -08002756 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002757exit3:
Dave Hansen06227532008-02-15 14:37:34 -08002758 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 exit2:
2760 dput(dentry);
2761 }
Jan Blunck4ac91372008-02-14 19:34:32 -08002762 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 if (inode)
2764 iput(inode); /* truncate the inode here */
2765exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002766 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 putname(name);
2768 return error;
2769
2770slashes:
2771 error = !dentry->d_inode ? -ENOENT :
2772 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2773 goto exit2;
2774}
2775
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002776SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002777{
2778 if ((flag & ~AT_REMOVEDIR) != 0)
2779 return -EINVAL;
2780
2781 if (flag & AT_REMOVEDIR)
2782 return do_rmdir(dfd, pathname);
2783
2784 return do_unlinkat(dfd, pathname);
2785}
2786
Heiko Carstens3480b252009-01-14 14:14:16 +01002787SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002788{
2789 return do_unlinkat(AT_FDCWD, pathname);
2790}
2791
Miklos Szeredidb2e7472008-06-24 16:50:16 +02002792int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002794 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795
2796 if (error)
2797 return error;
2798
Al Viroacfa4382008-12-04 10:06:33 -05002799 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 return -EPERM;
2801
2802 error = security_inode_symlink(dir, dentry, oldname);
2803 if (error)
2804 return error;
2805
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002807 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002808 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 return error;
2810}
2811
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002812SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
2813 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814{
Al Viro2ad94ae2008-07-21 09:32:51 -04002815 int error;
2816 char *from;
2817 char *to;
Dave Hansen6902d922006-09-30 23:29:01 -07002818 struct dentry *dentry;
2819 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
2821 from = getname(oldname);
Al Viro2ad94ae2008-07-21 09:32:51 -04002822 if (IS_ERR(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 return PTR_ERR(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04002824
2825 error = user_path_parent(newdfd, newname, &nd, &to);
2826 if (error)
Dave Hansen6902d922006-09-30 23:29:01 -07002827 goto out_putname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
Dave Hansen6902d922006-09-30 23:29:01 -07002829 dentry = lookup_create(&nd, 0);
2830 error = PTR_ERR(dentry);
2831 if (IS_ERR(dentry))
2832 goto out_unlock;
2833
Dave Hansen75c3f292008-02-15 14:37:45 -08002834 error = mnt_want_write(nd.path.mnt);
2835 if (error)
2836 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002837 error = security_path_symlink(&nd.path, dentry, from);
2838 if (error)
2839 goto out_drop_write;
Miklos Szeredidb2e7472008-06-24 16:50:16 +02002840 error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002841out_drop_write:
Dave Hansen75c3f292008-02-15 14:37:45 -08002842 mnt_drop_write(nd.path.mnt);
2843out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002844 dput(dentry);
2845out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002846 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002847 path_put(&nd.path);
Dave Hansen6902d922006-09-30 23:29:01 -07002848 putname(to);
2849out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 putname(from);
2851 return error;
2852}
2853
Heiko Carstens3480b252009-01-14 14:14:16 +01002854SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002855{
2856 return sys_symlinkat(oldname, AT_FDCWD, newname);
2857}
2858
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2860{
2861 struct inode *inode = old_dentry->d_inode;
2862 int error;
2863
2864 if (!inode)
2865 return -ENOENT;
2866
Miklos Szeredia95164d2008-07-30 15:08:48 +02002867 error = may_create(dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 if (error)
2869 return error;
2870
2871 if (dir->i_sb != inode->i_sb)
2872 return -EXDEV;
2873
2874 /*
2875 * A link to an append-only or immutable file cannot be created.
2876 */
2877 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2878 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05002879 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002881 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 return -EPERM;
2883
2884 error = security_inode_link(old_dentry, dir, new_dentry);
2885 if (error)
2886 return error;
2887
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002888 mutex_lock(&inode->i_mutex);
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05302889 /* Make sure we don't allow creating hardlink to an unlinked file */
2890 if (inode->i_nlink == 0)
2891 error = -ENOENT;
2892 else
2893 error = dir->i_op->link(old_dentry, dir, new_dentry);
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002894 mutex_unlock(&inode->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07002895 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002896 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 return error;
2898}
2899
2900/*
2901 * Hardlinks are often used in delicate situations. We avoid
2902 * security-related surprises by not following symlinks on the
2903 * newname. --KAB
2904 *
2905 * We don't follow them on the oldname either to be compatible
2906 * with linux 2.0, and to avoid hard-linking to directories
2907 * and other special files. --ADM
2908 */
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002909SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
2910 int, newdfd, const char __user *, newname, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911{
2912 struct dentry *new_dentry;
Al Viro2d8f3032008-07-22 09:59:21 -04002913 struct nameidata nd;
2914 struct path old_path;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05302915 int how = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 int error;
Al Viro2ad94ae2008-07-21 09:32:51 -04002917 char *to;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05302919 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002920 return -EINVAL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05302921 /*
2922 * To use null names we require CAP_DAC_READ_SEARCH
2923 * This ensures that not everyone will be able to create
2924 * handlink using the passed filedescriptor.
2925 */
2926 if (flags & AT_EMPTY_PATH) {
2927 if (!capable(CAP_DAC_READ_SEARCH))
2928 return -ENOENT;
2929 how = LOOKUP_EMPTY;
2930 }
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002931
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05302932 if (flags & AT_SYMLINK_FOLLOW)
2933 how |= LOOKUP_FOLLOW;
2934
2935 error = user_path_at(olddfd, oldname, how, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002937 return error;
2938
2939 error = user_path_parent(newdfd, newname, &nd, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 if (error)
2941 goto out;
2942 error = -EXDEV;
Al Viro2d8f3032008-07-22 09:59:21 -04002943 if (old_path.mnt != nd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 goto out_release;
2945 new_dentry = lookup_create(&nd, 0);
2946 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002947 if (IS_ERR(new_dentry))
2948 goto out_unlock;
Dave Hansen75c3f292008-02-15 14:37:45 -08002949 error = mnt_want_write(nd.path.mnt);
2950 if (error)
2951 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002952 error = security_path_link(old_path.dentry, &nd.path, new_dentry);
2953 if (error)
2954 goto out_drop_write;
Al Viro2d8f3032008-07-22 09:59:21 -04002955 error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002956out_drop_write:
Dave Hansen75c3f292008-02-15 14:37:45 -08002957 mnt_drop_write(nd.path.mnt);
2958out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002959 dput(new_dentry);
2960out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002961 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962out_release:
Jan Blunck1d957f92008-02-14 19:34:35 -08002963 path_put(&nd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04002964 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965out:
Al Viro2d8f3032008-07-22 09:59:21 -04002966 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
2968 return error;
2969}
2970
Heiko Carstens3480b252009-01-14 14:14:16 +01002971SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002972{
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002973 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002974}
2975
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976/*
2977 * The worst of all namespace operations - renaming directory. "Perverted"
2978 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2979 * Problems:
2980 * a) we can get into loop creation. Check is done in is_subdir().
2981 * b) race potential - two innocent renames can create a loop together.
2982 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002983 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 * story.
2985 * c) we have to lock _three_ objects - parents and victim (if it exists).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002986 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 * whether the target exists). Solution: try to be smart with locking
2988 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002989 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 * move will be locked. Thus we can rank directories by the tree
2991 * (ancestors first) and rank all non-directories after them.
2992 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002993 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 * HOWEVER, it relies on the assumption that any object with ->lookup()
2995 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2996 * we'd better make sure that there's no link(2) for them.
2997 * d) some filesystems don't support opened-but-unlinked directories,
2998 * either because of layout or because they are not ready to deal with
2999 * all cases correctly. The latter will be fixed (taking this sort of
3000 * stuff into VFS), but the former is not going away. Solution: the same
3001 * trick as in rmdir().
3002 * e) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003003 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05003005 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 * locking].
3007 */
Adrian Bunk75c96f82005-05-05 16:16:09 -07003008static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
3009 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010{
3011 int error = 0;
3012 struct inode *target;
3013
3014 /*
3015 * If we are going to change the parent - check write permissions,
3016 * we'll need to flip '..'.
3017 */
3018 if (new_dir != old_dir) {
Al Virof419a2e2008-07-22 00:07:17 -04003019 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 if (error)
3021 return error;
3022 }
3023
3024 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3025 if (error)
3026 return error;
3027
3028 target = new_dentry->d_inode;
Al Virod83c49f2010-04-30 17:17:09 -04003029 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003030 mutex_lock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
3032 error = -EBUSY;
Al Virod83c49f2010-04-30 17:17:09 -04003033 else {
3034 if (target)
3035 dentry_unhash(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
Al Virod83c49f2010-04-30 17:17:09 -04003037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 if (target) {
Al Virod83c49f2010-04-30 17:17:09 -04003039 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 target->i_flags |= S_DEAD;
Al Virod83c49f2010-04-30 17:17:09 -04003041 dont_mount(new_dentry);
3042 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003043 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 if (d_unhashed(new_dentry))
3045 d_rehash(new_dentry);
3046 dput(new_dentry);
3047 }
Stephen Smalleye31e14e2005-09-09 13:01:45 -07003048 if (!error)
Mark Fasheh349457c2006-09-08 14:22:21 -07003049 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3050 d_move(old_dentry,new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 return error;
3052}
3053
Adrian Bunk75c96f82005-05-05 16:16:09 -07003054static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
3055 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056{
3057 struct inode *target;
3058 int error;
3059
3060 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3061 if (error)
3062 return error;
3063
3064 dget(new_dentry);
3065 target = new_dentry->d_inode;
3066 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003067 mutex_lock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
3069 error = -EBUSY;
3070 else
3071 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3072 if (!error) {
Al Virobec10522010-03-03 14:12:08 -05003073 if (target)
Al Virod83c49f2010-04-30 17:17:09 -04003074 dont_mount(new_dentry);
Mark Fasheh349457c2006-09-08 14:22:21 -07003075 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 d_move(old_dentry, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 }
3078 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003079 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 dput(new_dentry);
3081 return error;
3082}
3083
3084int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
3085 struct inode *new_dir, struct dentry *new_dentry)
3086{
3087 int error;
3088 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
Eric Paris59b0df22010-02-08 12:53:52 -05003089 const unsigned char *old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090
3091 if (old_dentry->d_inode == new_dentry->d_inode)
3092 return 0;
3093
3094 error = may_delete(old_dir, old_dentry, is_dir);
3095 if (error)
3096 return error;
3097
3098 if (!new_dentry->d_inode)
Miklos Szeredia95164d2008-07-30 15:08:48 +02003099 error = may_create(new_dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 else
3101 error = may_delete(new_dir, new_dentry, is_dir);
3102 if (error)
3103 return error;
3104
Al Viroacfa4382008-12-04 10:06:33 -05003105 if (!old_dir->i_op->rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 return -EPERM;
3107
Robert Love0eeca282005-07-12 17:06:03 -04003108 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
3109
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 if (is_dir)
3111 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
3112 else
3113 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
Al Viro123df292009-12-25 04:57:57 -05003114 if (!error)
3115 fsnotify_move(old_dir, new_dir, old_name, is_dir,
Al Viro5a190ae2007-06-07 12:19:32 -04003116 new_dentry->d_inode, old_dentry);
Robert Love0eeca282005-07-12 17:06:03 -04003117 fsnotify_oldname_free(old_name);
3118
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 return error;
3120}
3121
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003122SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
3123 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124{
Al Viro2ad94ae2008-07-21 09:32:51 -04003125 struct dentry *old_dir, *new_dir;
3126 struct dentry *old_dentry, *new_dentry;
3127 struct dentry *trap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 struct nameidata oldnd, newnd;
Al Viro2ad94ae2008-07-21 09:32:51 -04003129 char *from;
3130 char *to;
3131 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132
Al Viro2ad94ae2008-07-21 09:32:51 -04003133 error = user_path_parent(olddfd, oldname, &oldnd, &from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 if (error)
3135 goto exit;
3136
Al Viro2ad94ae2008-07-21 09:32:51 -04003137 error = user_path_parent(newdfd, newname, &newnd, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 if (error)
3139 goto exit1;
3140
3141 error = -EXDEV;
Jan Blunck4ac91372008-02-14 19:34:32 -08003142 if (oldnd.path.mnt != newnd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 goto exit2;
3144
Jan Blunck4ac91372008-02-14 19:34:32 -08003145 old_dir = oldnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 error = -EBUSY;
3147 if (oldnd.last_type != LAST_NORM)
3148 goto exit2;
3149
Jan Blunck4ac91372008-02-14 19:34:32 -08003150 new_dir = newnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 if (newnd.last_type != LAST_NORM)
3152 goto exit2;
3153
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003154 oldnd.flags &= ~LOOKUP_PARENT;
3155 newnd.flags &= ~LOOKUP_PARENT;
OGAWA Hirofumi4e9ed2f2008-10-16 07:50:29 +09003156 newnd.flags |= LOOKUP_RENAME_TARGET;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003157
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 trap = lock_rename(new_dir, old_dir);
3159
Christoph Hellwig49705b72005-11-08 21:35:06 -08003160 old_dentry = lookup_hash(&oldnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 error = PTR_ERR(old_dentry);
3162 if (IS_ERR(old_dentry))
3163 goto exit3;
3164 /* source must exist */
3165 error = -ENOENT;
3166 if (!old_dentry->d_inode)
3167 goto exit4;
3168 /* unless the source is a directory trailing slashes give -ENOTDIR */
3169 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
3170 error = -ENOTDIR;
3171 if (oldnd.last.name[oldnd.last.len])
3172 goto exit4;
3173 if (newnd.last.name[newnd.last.len])
3174 goto exit4;
3175 }
3176 /* source should not be ancestor of target */
3177 error = -EINVAL;
3178 if (old_dentry == trap)
3179 goto exit4;
Christoph Hellwig49705b72005-11-08 21:35:06 -08003180 new_dentry = lookup_hash(&newnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 error = PTR_ERR(new_dentry);
3182 if (IS_ERR(new_dentry))
3183 goto exit4;
3184 /* target should not be an ancestor of source */
3185 error = -ENOTEMPTY;
3186 if (new_dentry == trap)
3187 goto exit5;
3188
Dave Hansen9079b1e2008-02-15 14:37:49 -08003189 error = mnt_want_write(oldnd.path.mnt);
3190 if (error)
3191 goto exit5;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003192 error = security_path_rename(&oldnd.path, old_dentry,
3193 &newnd.path, new_dentry);
3194 if (error)
3195 goto exit6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 error = vfs_rename(old_dir->d_inode, old_dentry,
3197 new_dir->d_inode, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003198exit6:
Dave Hansen9079b1e2008-02-15 14:37:49 -08003199 mnt_drop_write(oldnd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200exit5:
3201 dput(new_dentry);
3202exit4:
3203 dput(old_dentry);
3204exit3:
3205 unlock_rename(new_dir, old_dir);
3206exit2:
Jan Blunck1d957f92008-02-14 19:34:35 -08003207 path_put(&newnd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04003208 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003210 path_put(&oldnd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 putname(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04003212exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 return error;
3214}
3215
Heiko Carstensa26eab22009-01-14 14:14:17 +01003216SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003217{
3218 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
3219}
3220
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
3222{
3223 int len;
3224
3225 len = PTR_ERR(link);
3226 if (IS_ERR(link))
3227 goto out;
3228
3229 len = strlen(link);
3230 if (len > (unsigned) buflen)
3231 len = buflen;
3232 if (copy_to_user(buffer, link, len))
3233 len = -EFAULT;
3234out:
3235 return len;
3236}
3237
3238/*
3239 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
3240 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
3241 * using) it for any given inode is up to filesystem.
3242 */
3243int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3244{
3245 struct nameidata nd;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003246 void *cookie;
Marcin Slusarz694a1762008-06-09 16:40:37 -07003247 int res;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003248
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 nd.depth = 0;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003250 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
Marcin Slusarz694a1762008-06-09 16:40:37 -07003251 if (IS_ERR(cookie))
3252 return PTR_ERR(cookie);
3253
3254 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
3255 if (dentry->d_inode->i_op->put_link)
3256 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3257 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258}
3259
3260int vfs_follow_link(struct nameidata *nd, const char *link)
3261{
3262 return __vfs_follow_link(nd, link);
3263}
3264
3265/* get the link contents into pagecache */
3266static char *page_getlink(struct dentry * dentry, struct page **ppage)
3267{
Duane Griffinebd09ab2008-12-19 20:47:12 +00003268 char *kaddr;
3269 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 struct address_space *mapping = dentry->d_inode->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -07003271 page = read_mapping_page(mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 if (IS_ERR(page))
Nick Piggin6fe69002007-05-06 14:49:04 -07003273 return (char*)page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 *ppage = page;
Duane Griffinebd09ab2008-12-19 20:47:12 +00003275 kaddr = kmap(page);
3276 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3277 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278}
3279
3280int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3281{
3282 struct page *page = NULL;
3283 char *s = page_getlink(dentry, &page);
3284 int res = vfs_readlink(dentry,buffer,buflen,s);
3285 if (page) {
3286 kunmap(page);
3287 page_cache_release(page);
3288 }
3289 return res;
3290}
3291
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003292void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003294 struct page *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 nd_set_link(nd, page_getlink(dentry, &page));
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003296 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297}
3298
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003299void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003301 struct page *page = cookie;
3302
3303 if (page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 kunmap(page);
3305 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 }
3307}
3308
Nick Piggin54566b22009-01-04 12:00:53 -08003309/*
3310 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
3311 */
3312int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313{
3314 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003315 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07003316 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08003317 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 char *kaddr;
Nick Piggin54566b22009-01-04 12:00:53 -08003319 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
3320 if (nofs)
3321 flags |= AOP_FLAG_NOFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322
NeilBrown7e53cac2006-03-25 03:07:57 -08003323retry:
Nick Pigginafddba42007-10-16 01:25:01 -07003324 err = pagecache_write_begin(NULL, mapping, 0, len-1,
Nick Piggin54566b22009-01-04 12:00:53 -08003325 flags, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07003327 goto fail;
3328
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 kaddr = kmap_atomic(page, KM_USER0);
3330 memcpy(kaddr, symname, len-1);
3331 kunmap_atomic(kaddr, KM_USER0);
Nick Pigginafddba42007-10-16 01:25:01 -07003332
3333 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3334 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 if (err < 0)
3336 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07003337 if (err < len-1)
3338 goto retry;
3339
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 mark_inode_dirty(inode);
3341 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342fail:
3343 return err;
3344}
3345
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003346int page_symlink(struct inode *inode, const char *symname, int len)
3347{
3348 return __page_symlink(inode, symname, len,
Nick Piggin54566b22009-01-04 12:00:53 -08003349 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003350}
3351
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003352const struct inode_operations page_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 .readlink = generic_readlink,
3354 .follow_link = page_follow_link_light,
3355 .put_link = page_put_link,
3356};
3357
Al Viro2d8f3032008-07-22 09:59:21 -04003358EXPORT_SYMBOL(user_path_at);
David Howellscc53ce52011-01-14 18:45:26 +00003359EXPORT_SYMBOL(follow_down_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360EXPORT_SYMBOL(follow_down);
3361EXPORT_SYMBOL(follow_up);
3362EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
3363EXPORT_SYMBOL(getname);
3364EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365EXPORT_SYMBOL(lookup_one_len);
3366EXPORT_SYMBOL(page_follow_link_light);
3367EXPORT_SYMBOL(page_put_link);
3368EXPORT_SYMBOL(page_readlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003369EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370EXPORT_SYMBOL(page_symlink);
3371EXPORT_SYMBOL(page_symlink_inode_operations);
Al Viroc9c6cac2011-02-16 15:15:47 -05003372EXPORT_SYMBOL(kern_path_parent);
Al Virod1811462008-08-02 00:49:18 -04003373EXPORT_SYMBOL(kern_path);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07003374EXPORT_SYMBOL(vfs_path_lookup);
Al Virof419a2e2008-07-22 00:07:17 -04003375EXPORT_SYMBOL(inode_permission);
Christoph Hellwig8c744fb2005-11-08 21:35:04 -08003376EXPORT_SYMBOL(file_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377EXPORT_SYMBOL(unlock_rename);
3378EXPORT_SYMBOL(vfs_create);
3379EXPORT_SYMBOL(vfs_follow_link);
3380EXPORT_SYMBOL(vfs_link);
3381EXPORT_SYMBOL(vfs_mkdir);
3382EXPORT_SYMBOL(vfs_mknod);
3383EXPORT_SYMBOL(generic_permission);
3384EXPORT_SYMBOL(vfs_readlink);
3385EXPORT_SYMBOL(vfs_rename);
3386EXPORT_SYMBOL(vfs_rmdir);
3387EXPORT_SYMBOL(vfs_symlink);
3388EXPORT_SYMBOL(vfs_unlink);
3389EXPORT_SYMBOL(dentry_unhash);
3390EXPORT_SYMBOL(generic_readlink);