blob: 7bab4add8a102477170a7b964acad12b5fd063af [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/namei.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7/*
8 * Some corrections by tytso.
9 */
10
11/* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
12 * lookup logic.
13 */
14/* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
15 */
16
17#include <linux/init.h>
18#include <linux/module.h>
19#include <linux/slab.h>
20#include <linux/fs.h>
21#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/pagemap.h>
Robert Love0eeca282005-07-12 17:06:03 -040023#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/personality.h>
25#include <linux/security.h>
Mimi Zohar6146f0d2009-02-04 09:06:57 -050026#include <linux/ima.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/syscalls.h>
28#include <linux/mount.h>
29#include <linux/audit.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080030#include <linux/capability.h>
Trond Myklebust834f2a42005-10-18 14:20:16 -070031#include <linux/file.h>
Ulrich Drepper5590ff02006-01-18 17:43:53 -080032#include <linux/fcntl.h>
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070033#include <linux/device_cgroup.h>
Al Viro5ad4e532009-03-29 19:50:06 -040034#include <linux/fs_struct.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/uaccess.h>
36
Eric Parise81e3f42009-12-04 15:47:36 -050037#include "internal.h"
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039/* [Feb-1997 T. Schoebel-Theuer]
40 * Fundamental changes in the pathname lookup mechanisms (namei)
41 * were necessary because of omirr. The reason is that omirr needs
42 * to know the _real_ pathname, not the user-supplied one, in case
43 * of symlinks (and also when transname replacements occur).
44 *
45 * The new code replaces the old recursive symlink resolution with
46 * an iterative one (in case of non-nested symlink chains). It does
47 * this with calls to <fs>_follow_link().
48 * As a side effect, dir_namei(), _namei() and follow_link() are now
49 * replaced with a single function lookup_dentry() that can handle all
50 * the special cases of the former code.
51 *
52 * With the new dcache, the pathname is stored at each inode, at least as
53 * long as the refcount of the inode is positive. As a side effect, the
54 * size of the dcache depends on the inode cache and thus is dynamic.
55 *
56 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
57 * resolution to correspond with current state of the code.
58 *
59 * Note that the symlink resolution is not *completely* iterative.
60 * There is still a significant amount of tail- and mid- recursion in
61 * the algorithm. Also, note that <fs>_readlink() is not used in
62 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
63 * may return different results than <fs>_follow_link(). Many virtual
64 * filesystems (including /proc) exhibit this behavior.
65 */
66
67/* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
68 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
69 * and the name already exists in form of a symlink, try to create the new
70 * name indicated by the symlink. The old code always complained that the
71 * name already exists, due to not following the symlink even if its target
72 * is nonexistent. The new semantics affects also mknod() and link() when
Lucas De Marchi25985ed2011-03-30 22:57:33 -030073 * the name is a symlink pointing to a non-existent name.
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 *
75 * I don't know which semantics is the right one, since I have no access
76 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
77 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
78 * "old" one. Personally, I think the new semantics is much more logical.
79 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
80 * file does succeed in both HP-UX and SunOs, but not in Solaris
81 * and in the old Linux semantics.
82 */
83
84/* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
85 * semantics. See the comments in "open_namei" and "do_link" below.
86 *
87 * [10-Sep-98 Alan Modra] Another symlink change.
88 */
89
90/* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
91 * inside the path - always follow.
92 * in the last component in creation/removal/renaming - never follow.
93 * if LOOKUP_FOLLOW passed - follow.
94 * if the pathname has trailing slashes - follow.
95 * otherwise - don't follow.
96 * (applied in that order).
97 *
98 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
99 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
100 * During the 2.4 we need to fix the userland stuff depending on it -
101 * hopefully we will be able to get rid of that wart in 2.5. So far only
102 * XEmacs seems to be relying on it...
103 */
104/*
105 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
Arjan van de Vena11f3a02006-03-23 03:00:33 -0800106 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 * any extra contention...
108 */
109
110/* In order to reduce some races, while at the same time doing additional
111 * checking and hopefully speeding things up, we copy filenames to the
112 * kernel data space before using them..
113 *
114 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
115 * PATH_MAX includes the nul terminator --RR.
116 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800117static int do_getname(const char __user *filename, char *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
119 int retval;
120 unsigned long len = PATH_MAX;
121
122 if (!segment_eq(get_fs(), KERNEL_DS)) {
123 if ((unsigned long) filename >= TASK_SIZE)
124 return -EFAULT;
125 if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
126 len = TASK_SIZE - (unsigned long) filename;
127 }
128
129 retval = strncpy_from_user(page, filename, len);
130 if (retval > 0) {
131 if (retval < len)
132 return 0;
133 return -ENAMETOOLONG;
134 } else if (!retval)
135 retval = -ENOENT;
136 return retval;
137}
138
Andy Whitcroftb0917c32011-11-02 09:44:39 +0100139static char *getname_flags(const char __user *filename, int flags, int *empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
141 char *tmp, *result;
142
143 result = ERR_PTR(-ENOMEM);
144 tmp = __getname();
145 if (tmp) {
146 int retval = do_getname(filename, tmp);
147
148 result = tmp;
149 if (retval < 0) {
Andy Whitcroftb0917c32011-11-02 09:44:39 +0100150 if (retval == -ENOENT && empty)
151 *empty = 1;
Al Virof52e0c12011-03-14 18:56:51 -0400152 if (retval != -ENOENT || !(flags & LOOKUP_EMPTY)) {
153 __putname(tmp);
154 result = ERR_PTR(retval);
155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 }
157 }
158 audit_getname(result);
159 return result;
160}
161
Al Virof52e0c12011-03-14 18:56:51 -0400162char *getname(const char __user * filename)
163{
Andy Whitcroftb0917c32011-11-02 09:44:39 +0100164 return getname_flags(filename, 0, 0);
Al Virof52e0c12011-03-14 18:56:51 -0400165}
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167#ifdef CONFIG_AUDITSYSCALL
168void putname(const char *name)
169{
Al Viro5ac3a9c2006-07-16 06:38:45 -0400170 if (unlikely(!audit_dummy_context()))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 audit_putname(name);
172 else
173 __putname(name);
174}
175EXPORT_SYMBOL(putname);
176#endif
177
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700178/*
179 * This does basic POSIX ACL permission checking
180 */
Nick Pigginb74c79e2011-01-07 17:49:58 +1100181static int acl_permission_check(struct inode *inode, int mask, unsigned int flags,
182 int (*check_acl)(struct inode *inode, int mask, unsigned int flags))
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700183{
Linus Torvalds26cf46b2011-05-13 11:51:01 -0700184 unsigned int mode = inode->i_mode;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700185
186 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
187
Serge E. Hallyne795b712011-03-23 16:43:25 -0700188 if (current_user_ns() != inode_userns(inode))
189 goto other_perms;
190
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700191 if (current_fsuid() == inode->i_uid)
192 mode >>= 6;
193 else {
194 if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
Nick Pigginb74c79e2011-01-07 17:49:58 +1100195 int error = check_acl(inode, mask, flags);
196 if (error != -EAGAIN)
197 return error;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700198 }
199
200 if (in_group_p(inode->i_gid))
201 mode >>= 3;
202 }
203
Serge E. Hallyne795b712011-03-23 16:43:25 -0700204other_perms:
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700205 /*
206 * If the DACs are ok we don't need any capability check.
207 */
208 if ((mask & ~mode) == 0)
209 return 0;
210 return -EACCES;
211}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213/**
Nick Pigginb74c79e2011-01-07 17:49:58 +1100214 * generic_permission - check for access rights on a Posix-like filesystem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 * @inode: inode to check access rights for
216 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
217 * @check_acl: optional callback to check for Posix ACLs
Randy Dunlap39191622011-01-08 19:36:21 -0800218 * @flags: IPERM_FLAG_ flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 *
220 * Used to check for read/write/execute permissions on a file.
221 * We use "fsuid" for this, letting us set arbitrary permissions
222 * for filesystem access without changing the "normal" uids which
Nick Pigginb74c79e2011-01-07 17:49:58 +1100223 * are used for other things.
224 *
225 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
226 * request cannot be satisfied (eg. requires blocking or too much complexity).
227 * It would then be called again in ref-walk mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 */
Nick Pigginb74c79e2011-01-07 17:49:58 +1100229int generic_permission(struct inode *inode, int mask, unsigned int flags,
230 int (*check_acl)(struct inode *inode, int mask, unsigned int flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700232 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 /*
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700235 * Do the basic POSIX ACL permission checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 */
Nick Pigginb74c79e2011-01-07 17:49:58 +1100237 ret = acl_permission_check(inode, mask, flags, check_acl);
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700238 if (ret != -EACCES)
239 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 /*
242 * Read/write DACs are always overridable.
Al Viro8e833fd2011-06-19 01:56:53 -0400243 * Executable DACs are overridable for all directories and
244 * for non-directories that have least one exec bit set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 */
Miklos Szeredif696a362008-07-31 13:41:58 +0200246 if (!(mask & MAY_EXEC) || execute_ok(inode))
Serge E. Hallyne795b712011-03-23 16:43:25 -0700247 if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return 0;
249
250 /*
251 * Searching includes executable on directories, else just read.
252 */
Serge E. Hallyn7ea66002009-12-29 14:50:19 -0600253 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
Serge E. Hallyne795b712011-03-23 16:43:25 -0700255 if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return 0;
257
258 return -EACCES;
259}
260
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200261/**
262 * inode_permission - check for access rights to a given inode
263 * @inode: inode to check permission on
264 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
265 *
266 * Used to check for read/write/execute permissions on an inode.
267 * We use "fsuid" for this, letting us set arbitrary permissions
268 * for filesystem access without changing the "normal" uids which
269 * are used for other things.
270 */
Al Virof419a2e2008-07-22 00:07:17 -0400271int inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Al Viroe6305c42008-07-15 21:03:57 -0400273 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 if (mask & MAY_WRITE) {
Miklos Szeredi22590e42007-10-16 23:27:08 -0700276 umode_t mode = inode->i_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 /*
279 * Nobody gets write access to a read-only fs.
280 */
281 if (IS_RDONLY(inode) &&
282 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
283 return -EROFS;
284
285 /*
286 * Nobody gets write access to an immutable file.
287 */
288 if (IS_IMMUTABLE(inode))
289 return -EACCES;
290 }
291
Al Viroacfa4382008-12-04 10:06:33 -0500292 if (inode->i_op->permission)
Nick Pigginb74c79e2011-01-07 17:49:58 +1100293 retval = inode->i_op->permission(inode, mask, 0);
Miklos Szeredif696a362008-07-31 13:41:58 +0200294 else
Nick Pigginb74c79e2011-01-07 17:49:58 +1100295 retval = generic_permission(inode, mask, 0,
296 inode->i_op->check_acl);
Miklos Szeredif696a362008-07-31 13:41:58 +0200297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 if (retval)
299 return retval;
300
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700301 retval = devcgroup_inode_permission(inode, mask);
302 if (retval)
303 return retval;
304
Eric Parisd09ca732010-07-23 11:43:57 -0400305 return security_inode_permission(inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800308/**
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800309 * file_permission - check for additional access rights to a given file
310 * @file: file to check access rights for
311 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
312 *
313 * Used to check for read/write/execute permissions on an already opened
314 * file.
315 *
316 * Note:
317 * Do not use this function in new code. All access checks should
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200318 * be done using inode_permission().
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800319 */
320int file_permission(struct file *file, int mask)
321{
Al Virof419a2e2008-07-22 00:07:17 -0400322 return inode_permission(file->f_path.dentry->d_inode, mask);
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800323}
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325/*
326 * get_write_access() gets write permission for a file.
327 * put_write_access() releases this write permission.
328 * This is used for regular files.
329 * We cannot support write (and maybe mmap read-write shared) accesses and
330 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
331 * can have the following values:
332 * 0: no writers, no VM_DENYWRITE mappings
333 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
334 * > 0: (i_writecount) users are writing to the file.
335 *
336 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
337 * except for the cases where we don't hold i_writecount yet. Then we need to
338 * use {get,deny}_write_access() - these functions check the sign and refuse
339 * to do the change if sign is wrong. Exclusion between them is provided by
340 * the inode->i_lock spinlock.
341 */
342
343int get_write_access(struct inode * inode)
344{
345 spin_lock(&inode->i_lock);
346 if (atomic_read(&inode->i_writecount) < 0) {
347 spin_unlock(&inode->i_lock);
348 return -ETXTBSY;
349 }
350 atomic_inc(&inode->i_writecount);
351 spin_unlock(&inode->i_lock);
352
353 return 0;
354}
355
356int deny_write_access(struct file * file)
357{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800358 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 spin_lock(&inode->i_lock);
361 if (atomic_read(&inode->i_writecount) > 0) {
362 spin_unlock(&inode->i_lock);
363 return -ETXTBSY;
364 }
365 atomic_dec(&inode->i_writecount);
366 spin_unlock(&inode->i_lock);
367
368 return 0;
369}
370
Jan Blunck1d957f92008-02-14 19:34:35 -0800371/**
Jan Blunck5dd784d2008-02-14 19:34:38 -0800372 * path_get - get a reference to a path
373 * @path: path to get the reference to
374 *
375 * Given a path increment the reference count to the dentry and the vfsmount.
376 */
377void path_get(struct path *path)
378{
379 mntget(path->mnt);
380 dget(path->dentry);
381}
382EXPORT_SYMBOL(path_get);
383
384/**
Jan Blunck1d957f92008-02-14 19:34:35 -0800385 * path_put - put a reference to a path
386 * @path: path to put the reference to
387 *
388 * Given a path decrement the reference count to the dentry and the vfsmount.
389 */
390void path_put(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
Jan Blunck1d957f92008-02-14 19:34:35 -0800392 dput(path->dentry);
393 mntput(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
Jan Blunck1d957f92008-02-14 19:34:35 -0800395EXPORT_SYMBOL(path_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Al Viro19660af2011-03-25 10:32:48 -0400397/*
Nick Piggin31e6b012011-01-07 17:49:52 +1100398 * Path walking has 2 modes, rcu-walk and ref-walk (see
Al Viro19660af2011-03-25 10:32:48 -0400399 * Documentation/filesystems/path-lookup.txt). In situations when we can't
400 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
401 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
402 * mode. Refcounts are grabbed at the last known good point before rcu-walk
403 * got stuck, so ref-walk may continue from there. If this is not successful
404 * (eg. a seqcount has changed), then failure is returned and it's up to caller
405 * to restart the path walk from the beginning in ref-walk mode.
Nick Piggin31e6b012011-01-07 17:49:52 +1100406 */
Nick Piggin31e6b012011-01-07 17:49:52 +1100407
408/**
Al Viro19660af2011-03-25 10:32:48 -0400409 * unlazy_walk - try to switch to ref-walk mode.
410 * @nd: nameidata pathwalk data
411 * @dentry: child of nd->path.dentry or NULL
Randy Dunlap39191622011-01-08 19:36:21 -0800412 * Returns: 0 on success, -ECHILD on failure
Nick Piggin31e6b012011-01-07 17:49:52 +1100413 *
Al Viro19660af2011-03-25 10:32:48 -0400414 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
415 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
416 * @nd or NULL. Must be called from rcu-walk context.
Nick Piggin31e6b012011-01-07 17:49:52 +1100417 */
Al Viro19660af2011-03-25 10:32:48 -0400418static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
Nick Piggin31e6b012011-01-07 17:49:52 +1100419{
420 struct fs_struct *fs = current->fs;
421 struct dentry *parent = nd->path.dentry;
Al Viro5b6ca022011-03-09 23:04:47 -0500422 int want_root = 0;
Nick Piggin31e6b012011-01-07 17:49:52 +1100423
424 BUG_ON(!(nd->flags & LOOKUP_RCU));
Al Viro5b6ca022011-03-09 23:04:47 -0500425 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
426 want_root = 1;
Nick Piggin31e6b012011-01-07 17:49:52 +1100427 spin_lock(&fs->lock);
428 if (nd->root.mnt != fs->root.mnt ||
429 nd->root.dentry != fs->root.dentry)
430 goto err_root;
431 }
432 spin_lock(&parent->d_lock);
Al Viro19660af2011-03-25 10:32:48 -0400433 if (!dentry) {
434 if (!__d_rcu_to_refcount(parent, nd->seq))
435 goto err_parent;
436 BUG_ON(nd->inode != parent->d_inode);
437 } else {
Al Viro94c0d4e2011-07-12 21:40:23 -0400438 if (dentry->d_parent != parent)
439 goto err_parent;
Al Viro19660af2011-03-25 10:32:48 -0400440 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
441 if (!__d_rcu_to_refcount(dentry, nd->seq))
442 goto err_child;
443 /*
444 * If the sequence check on the child dentry passed, then
445 * the child has not been removed from its parent. This
446 * means the parent dentry must be valid and able to take
447 * a reference at this point.
448 */
449 BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
450 BUG_ON(!parent->d_count);
451 parent->d_count++;
452 spin_unlock(&dentry->d_lock);
453 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100454 spin_unlock(&parent->d_lock);
Al Viro5b6ca022011-03-09 23:04:47 -0500455 if (want_root) {
Nick Piggin31e6b012011-01-07 17:49:52 +1100456 path_get(&nd->root);
457 spin_unlock(&fs->lock);
458 }
459 mntget(nd->path.mnt);
460
461 rcu_read_unlock();
462 br_read_unlock(vfsmount_lock);
463 nd->flags &= ~LOOKUP_RCU;
464 return 0;
Al Viro19660af2011-03-25 10:32:48 -0400465
466err_child:
Nick Piggin31e6b012011-01-07 17:49:52 +1100467 spin_unlock(&dentry->d_lock);
Al Viro19660af2011-03-25 10:32:48 -0400468err_parent:
Nick Piggin31e6b012011-01-07 17:49:52 +1100469 spin_unlock(&parent->d_lock);
470err_root:
Al Viro5b6ca022011-03-09 23:04:47 -0500471 if (want_root)
Nick Piggin31e6b012011-01-07 17:49:52 +1100472 spin_unlock(&fs->lock);
473 return -ECHILD;
474}
475
Nick Piggin31e6b012011-01-07 17:49:52 +1100476/**
Trond Myklebust834f2a42005-10-18 14:20:16 -0700477 * release_open_intent - free up open intent resources
478 * @nd: pointer to nameidata
479 */
480void release_open_intent(struct nameidata *nd)
481{
Linus Torvalds2dab5972011-02-11 15:53:38 -0800482 struct file *file = nd->intent.open.file;
483
484 if (file && !IS_ERR(file)) {
485 if (file->f_path.dentry == NULL)
486 put_filp(file);
487 else
488 fput(file);
489 }
Trond Myklebust834f2a42005-10-18 14:20:16 -0700490}
491
Al Virof60aef72011-02-15 01:35:28 -0500492static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
Nick Piggin34286d62011-01-07 17:49:57 +1100493{
Al Virof60aef72011-02-15 01:35:28 -0500494 return dentry->d_op->d_revalidate(dentry, nd);
Nick Piggin34286d62011-01-07 17:49:57 +1100495}
496
Al Virof5e1c1c2011-02-15 01:32:55 -0500497static struct dentry *
Ian Kentbcdc5e02006-09-27 01:50:44 -0700498do_revalidate(struct dentry *dentry, struct nameidata *nd)
499{
Al Virof5e1c1c2011-02-15 01:32:55 -0500500 int status = d_revalidate(dentry, nd);
Ian Kentbcdc5e02006-09-27 01:50:44 -0700501 if (unlikely(status <= 0)) {
502 /*
503 * The dentry failed validation.
504 * If d_revalidate returned 0 attempt to invalidate
505 * the dentry otherwise d_revalidate is asking us
506 * to return a fail status.
507 */
Nick Piggin34286d62011-01-07 17:49:57 +1100508 if (status < 0) {
Al Virof5e1c1c2011-02-15 01:32:55 -0500509 dput(dentry);
Nick Piggin34286d62011-01-07 17:49:57 +1100510 dentry = ERR_PTR(status);
Al Virof5e1c1c2011-02-15 01:32:55 -0500511 } else if (!d_invalidate(dentry)) {
512 dput(dentry);
513 dentry = NULL;
Ian Kentbcdc5e02006-09-27 01:50:44 -0700514 }
515 }
516 return dentry;
517}
518
Al Viro9f1fafe2011-03-25 11:00:12 -0400519/**
520 * complete_walk - successful completion of path walk
521 * @nd: pointer nameidata
Jeff Layton39159de2009-12-07 12:01:50 -0500522 *
Al Viro9f1fafe2011-03-25 11:00:12 -0400523 * If we had been in RCU mode, drop out of it and legitimize nd->path.
524 * Revalidate the final result, unless we'd already done that during
525 * the path walk or the filesystem doesn't ask for it. Return 0 on
526 * success, -error on failure. In case of failure caller does not
527 * need to drop nd->path.
Jeff Layton39159de2009-12-07 12:01:50 -0500528 */
Al Viro9f1fafe2011-03-25 11:00:12 -0400529static int complete_walk(struct nameidata *nd)
Jeff Layton39159de2009-12-07 12:01:50 -0500530{
Al Viro16c2cd72011-02-22 15:50:10 -0500531 struct dentry *dentry = nd->path.dentry;
Jeff Layton39159de2009-12-07 12:01:50 -0500532 int status;
Jeff Layton39159de2009-12-07 12:01:50 -0500533
Al Viro9f1fafe2011-03-25 11:00:12 -0400534 if (nd->flags & LOOKUP_RCU) {
535 nd->flags &= ~LOOKUP_RCU;
536 if (!(nd->flags & LOOKUP_ROOT))
537 nd->root.mnt = NULL;
538 spin_lock(&dentry->d_lock);
539 if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
540 spin_unlock(&dentry->d_lock);
541 rcu_read_unlock();
542 br_read_unlock(vfsmount_lock);
543 return -ECHILD;
544 }
545 BUG_ON(nd->inode != dentry->d_inode);
546 spin_unlock(&dentry->d_lock);
547 mntget(nd->path.mnt);
548 rcu_read_unlock();
549 br_read_unlock(vfsmount_lock);
550 }
551
Al Viro16c2cd72011-02-22 15:50:10 -0500552 if (likely(!(nd->flags & LOOKUP_JUMPED)))
Jeff Layton39159de2009-12-07 12:01:50 -0500553 return 0;
554
Al Viro16c2cd72011-02-22 15:50:10 -0500555 if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
556 return 0;
557
558 if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
559 return 0;
560
561 /* Note: we do not d_invalidate() */
Nick Piggin34286d62011-01-07 17:49:57 +1100562 status = d_revalidate(dentry, nd);
Jeff Layton39159de2009-12-07 12:01:50 -0500563 if (status > 0)
564 return 0;
565
Al Viro16c2cd72011-02-22 15:50:10 -0500566 if (!status)
Jeff Layton39159de2009-12-07 12:01:50 -0500567 status = -ESTALE;
Al Viro16c2cd72011-02-22 15:50:10 -0500568
Al Viro9f1fafe2011-03-25 11:00:12 -0400569 path_put(&nd->path);
Jeff Layton39159de2009-12-07 12:01:50 -0500570 return status;
571}
572
573/*
Al Virob75b5082009-12-16 01:01:38 -0500574 * Short-cut version of permission(), for calling on directories
575 * during pathname resolution. Combines parts of permission()
576 * and generic_permission(), and tests ONLY for MAY_EXEC permission.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 *
578 * If appropriate, check DAC only. If not appropriate, or
Al Virob75b5082009-12-16 01:01:38 -0500579 * short-cut DAC fails, then call ->permission() to do more
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 * complete permission check.
581 */
Nick Pigginb74c79e2011-01-07 17:49:58 +1100582static inline int exec_permission(struct inode *inode, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700584 int ret;
Serge E. Hallyne795b712011-03-23 16:43:25 -0700585 struct user_namespace *ns = inode_userns(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Linus Torvaldscb9179e2009-08-28 11:08:31 -0700587 if (inode->i_op->permission) {
Nick Pigginb74c79e2011-01-07 17:49:58 +1100588 ret = inode->i_op->permission(inode, MAY_EXEC, flags);
589 } else {
590 ret = acl_permission_check(inode, MAY_EXEC, flags,
591 inode->i_op->check_acl);
Linus Torvaldscb9179e2009-08-28 11:08:31 -0700592 }
Nick Pigginb74c79e2011-01-07 17:49:58 +1100593 if (likely(!ret))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 goto ok;
Nick Pigginb74c79e2011-01-07 17:49:58 +1100595 if (ret == -ECHILD)
Nick Piggin31e6b012011-01-07 17:49:52 +1100596 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Serge E. Hallyne795b712011-03-23 16:43:25 -0700598 if (ns_capable(ns, CAP_DAC_OVERRIDE) ||
599 ns_capable(ns, CAP_DAC_READ_SEARCH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 goto ok;
601
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700602 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603ok:
Nick Pigginb74c79e2011-01-07 17:49:58 +1100604 return security_inode_exec_permission(inode, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
606
Al Viro2a737872009-04-07 11:49:53 -0400607static __always_inline void set_root(struct nameidata *nd)
608{
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200609 if (!nd->root.mnt)
610 get_fs_root(current->fs, &nd->root);
Al Viro2a737872009-04-07 11:49:53 -0400611}
612
Al Viro6de88d72009-08-09 01:41:57 +0400613static int link_path_walk(const char *, struct nameidata *);
614
Nick Piggin31e6b012011-01-07 17:49:52 +1100615static __always_inline void set_root_rcu(struct nameidata *nd)
616{
617 if (!nd->root.mnt) {
618 struct fs_struct *fs = current->fs;
Nick Pigginc28cc362011-01-07 17:49:53 +1100619 unsigned seq;
620
621 do {
622 seq = read_seqcount_begin(&fs->seq);
623 nd->root = fs->root;
Tim Chenc1530012011-04-15 11:39:29 -0700624 nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
Nick Pigginc28cc362011-01-07 17:49:53 +1100625 } while (read_seqcount_retry(&fs->seq, seq));
Nick Piggin31e6b012011-01-07 17:49:52 +1100626 }
627}
628
Arjan van de Venf1662352006-01-14 13:21:31 -0800629static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Nick Piggin31e6b012011-01-07 17:49:52 +1100631 int ret;
632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 if (IS_ERR(link))
634 goto fail;
635
636 if (*link == '/') {
Al Viro2a737872009-04-07 11:49:53 -0400637 set_root(nd);
Jan Blunck1d957f92008-02-14 19:34:35 -0800638 path_put(&nd->path);
Al Viro2a737872009-04-07 11:49:53 -0400639 nd->path = nd->root;
640 path_get(&nd->root);
Al Viro16c2cd72011-02-22 15:50:10 -0500641 nd->flags |= LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100643 nd->inode = nd->path.dentry->d_inode;
Christoph Hellwigb4091d52008-11-05 15:07:21 +0100644
Nick Piggin31e6b012011-01-07 17:49:52 +1100645 ret = link_path_walk(link, nd);
646 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647fail:
Jan Blunck1d957f92008-02-14 19:34:35 -0800648 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return PTR_ERR(link);
650}
651
Jan Blunck1d957f92008-02-14 19:34:35 -0800652static void path_put_conditional(struct path *path, struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700653{
654 dput(path->dentry);
Jan Blunck4ac91372008-02-14 19:34:32 -0800655 if (path->mnt != nd->path.mnt)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700656 mntput(path->mnt);
657}
658
Nick Piggin7b9337a2011-01-14 08:42:43 +0000659static inline void path_to_nameidata(const struct path *path,
660 struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700661{
Nick Piggin31e6b012011-01-07 17:49:52 +1100662 if (!(nd->flags & LOOKUP_RCU)) {
663 dput(nd->path.dentry);
664 if (nd->path.mnt != path->mnt)
665 mntput(nd->path.mnt);
Huang Shijie9a229682010-04-02 17:37:13 +0800666 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100667 nd->path.mnt = path->mnt;
Jan Blunck4ac91372008-02-14 19:34:32 -0800668 nd->path.dentry = path->dentry;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700669}
670
Al Viro574197e2011-03-14 22:20:34 -0400671static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
672{
673 struct inode *inode = link->dentry->d_inode;
674 if (!IS_ERR(cookie) && inode->i_op->put_link)
675 inode->i_op->put_link(link->dentry, nd, cookie);
676 path_put(link);
677}
678
Al Virodef4af32009-12-26 08:37:05 -0500679static __always_inline int
Al Viro574197e2011-03-14 22:20:34 -0400680follow_link(struct path *link, struct nameidata *nd, void **p)
Ian Kent051d3812006-03-27 01:14:53 -0800681{
682 int error;
Nick Piggin7b9337a2011-01-14 08:42:43 +0000683 struct dentry *dentry = link->dentry;
Ian Kent051d3812006-03-27 01:14:53 -0800684
Al Viro844a3912011-02-15 00:38:26 -0500685 BUG_ON(nd->flags & LOOKUP_RCU);
686
Al Viro0e794582011-03-16 02:45:02 -0400687 if (link->mnt == nd->path.mnt)
688 mntget(link->mnt);
689
Al Viro574197e2011-03-14 22:20:34 -0400690 if (unlikely(current->total_link_count >= 40)) {
691 *p = ERR_PTR(-ELOOP); /* no ->put_link(), please */
Al Viro574197e2011-03-14 22:20:34 -0400692 path_put(&nd->path);
693 return -ELOOP;
694 }
695 cond_resched();
696 current->total_link_count++;
697
Nick Piggin7b9337a2011-01-14 08:42:43 +0000698 touch_atime(link->mnt, dentry);
Ian Kent051d3812006-03-27 01:14:53 -0800699 nd_set_link(nd, NULL);
700
Al Viro36f3b4f2011-02-22 21:24:38 -0500701 error = security_inode_follow_link(link->dentry, nd);
702 if (error) {
703 *p = ERR_PTR(error); /* no ->put_link(), please */
704 path_put(&nd->path);
705 return error;
706 }
707
Al Viro86acdca12009-12-22 23:45:11 -0500708 nd->last_type = LAST_BIND;
Al Virodef4af32009-12-26 08:37:05 -0500709 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
710 error = PTR_ERR(*p);
711 if (!IS_ERR(*p)) {
Ian Kent051d3812006-03-27 01:14:53 -0800712 char *s = nd_get_link(nd);
713 error = 0;
714 if (s)
715 error = __vfs_follow_link(nd, s);
Al Virobcda7652011-03-13 16:42:14 -0400716 else if (nd->last_type == LAST_BIND) {
Al Viro16c2cd72011-02-22 15:50:10 -0500717 nd->flags |= LOOKUP_JUMPED;
Al Virob21041d2011-03-14 20:01:51 -0400718 nd->inode = nd->path.dentry->d_inode;
719 if (nd->inode->i_op->follow_link) {
Al Virobcda7652011-03-13 16:42:14 -0400720 /* stepped on a _really_ weird one */
721 path_put(&nd->path);
722 error = -ELOOP;
723 }
724 }
Ian Kent051d3812006-03-27 01:14:53 -0800725 }
Ian Kent051d3812006-03-27 01:14:53 -0800726 return error;
727}
728
Nick Piggin31e6b012011-01-07 17:49:52 +1100729static int follow_up_rcu(struct path *path)
730{
731 struct vfsmount *parent;
732 struct dentry *mountpoint;
733
734 parent = path->mnt->mnt_parent;
735 if (parent == path->mnt)
736 return 0;
737 mountpoint = path->mnt->mnt_mountpoint;
738 path->dentry = mountpoint;
739 path->mnt = parent;
740 return 1;
741}
742
Al Virobab77eb2009-04-18 03:26:48 -0400743int follow_up(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
745 struct vfsmount *parent;
746 struct dentry *mountpoint;
Nick Piggin99b7db72010-08-18 04:37:39 +1000747
748 br_read_lock(vfsmount_lock);
Al Virobab77eb2009-04-18 03:26:48 -0400749 parent = path->mnt->mnt_parent;
750 if (parent == path->mnt) {
Nick Piggin99b7db72010-08-18 04:37:39 +1000751 br_read_unlock(vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 return 0;
753 }
754 mntget(parent);
Al Virobab77eb2009-04-18 03:26:48 -0400755 mountpoint = dget(path->mnt->mnt_mountpoint);
Nick Piggin99b7db72010-08-18 04:37:39 +1000756 br_read_unlock(vfsmount_lock);
Al Virobab77eb2009-04-18 03:26:48 -0400757 dput(path->dentry);
758 path->dentry = mountpoint;
759 mntput(path->mnt);
760 path->mnt = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 return 1;
762}
763
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100764/*
David Howells9875cf82011-01-14 18:45:21 +0000765 * Perform an automount
766 * - return -EISDIR to tell follow_managed() to stop and return the path we
767 * were called with.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 */
David Howells9875cf82011-01-14 18:45:21 +0000769static int follow_automount(struct path *path, unsigned flags,
770 bool *need_mntput)
Nick Piggin31e6b012011-01-07 17:49:52 +1100771{
David Howells9875cf82011-01-14 18:45:21 +0000772 struct vfsmount *mnt;
David Howellsea5b7782011-01-14 19:10:03 +0000773 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +1100774
David Howells9875cf82011-01-14 18:45:21 +0000775 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
776 return -EREMOTE;
Al Viro463ffb22005-06-06 13:36:05 -0700777
David Howells6f45b652011-01-14 18:45:31 +0000778 /* We don't want to mount if someone supplied AT_NO_AUTOMOUNT
779 * and this is the terminal part of the path.
780 */
781 if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_CONTINUE))
782 return -EISDIR; /* we actually want to stop here */
783
David Howells9875cf82011-01-14 18:45:21 +0000784 /* We want to mount if someone is trying to open/create a file of any
785 * type under the mountpoint, wants to traverse through the mountpoint
786 * or wants to open the mounted directory.
787 *
788 * We don't want to mount if someone's just doing a stat and they've
789 * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and
790 * appended a '/' to the name.
791 */
792 if (!(flags & LOOKUP_FOLLOW) &&
793 !(flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY |
794 LOOKUP_OPEN | LOOKUP_CREATE)))
795 return -EISDIR;
796
797 current->total_link_count++;
798 if (current->total_link_count >= 40)
799 return -ELOOP;
800
801 mnt = path->dentry->d_op->d_automount(path);
802 if (IS_ERR(mnt)) {
803 /*
804 * The filesystem is allowed to return -EISDIR here to indicate
805 * it doesn't want to automount. For instance, autofs would do
806 * this so that its userspace daemon can mount on this dentry.
807 *
808 * However, we can only permit this if it's a terminal point in
809 * the path being looked up; if it wasn't then the remainder of
810 * the path is inaccessible and we should say so.
811 */
812 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_CONTINUE))
813 return -EREMOTE;
814 return PTR_ERR(mnt);
815 }
David Howellsea5b7782011-01-14 19:10:03 +0000816
David Howells9875cf82011-01-14 18:45:21 +0000817 if (!mnt) /* mount collision */
818 return 0;
819
Al Viro8aef1882011-06-16 15:10:06 +0100820 if (!*need_mntput) {
821 /* lock_mount() may release path->mnt on error */
822 mntget(path->mnt);
823 *need_mntput = true;
824 }
Al Viro19a167a2011-01-17 01:35:23 -0500825 err = finish_automount(mnt, path);
David Howellsea5b7782011-01-14 19:10:03 +0000826
David Howellsea5b7782011-01-14 19:10:03 +0000827 switch (err) {
828 case -EBUSY:
829 /* Someone else made a mount here whilst we were busy */
Al Viro19a167a2011-01-17 01:35:23 -0500830 return 0;
David Howellsea5b7782011-01-14 19:10:03 +0000831 case 0:
Al Viro8aef1882011-06-16 15:10:06 +0100832 path_put(path);
David Howellsea5b7782011-01-14 19:10:03 +0000833 path->mnt = mnt;
834 path->dentry = dget(mnt->mnt_root);
David Howellsea5b7782011-01-14 19:10:03 +0000835 return 0;
Al Viro19a167a2011-01-17 01:35:23 -0500836 default:
837 return err;
David Howellsea5b7782011-01-14 19:10:03 +0000838 }
Al Viro19a167a2011-01-17 01:35:23 -0500839
David Howells9875cf82011-01-14 18:45:21 +0000840}
841
842/*
843 * Handle a dentry that is managed in some way.
David Howellscc53ce52011-01-14 18:45:26 +0000844 * - Flagged for transit management (autofs)
David Howells9875cf82011-01-14 18:45:21 +0000845 * - Flagged as mountpoint
846 * - Flagged as automount point
847 *
848 * This may only be called in refwalk mode.
849 *
850 * Serialization is taken care of in namespace.c
851 */
852static int follow_managed(struct path *path, unsigned flags)
853{
Al Viro8aef1882011-06-16 15:10:06 +0100854 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
David Howells9875cf82011-01-14 18:45:21 +0000855 unsigned managed;
856 bool need_mntput = false;
Al Viro8aef1882011-06-16 15:10:06 +0100857 int ret = 0;
David Howells9875cf82011-01-14 18:45:21 +0000858
859 /* Given that we're not holding a lock here, we retain the value in a
860 * local variable for each dentry as we look at it so that we don't see
861 * the components of that value change under us */
862 while (managed = ACCESS_ONCE(path->dentry->d_flags),
863 managed &= DCACHE_MANAGED_DENTRY,
864 unlikely(managed != 0)) {
David Howellscc53ce52011-01-14 18:45:26 +0000865 /* Allow the filesystem to manage the transit without i_mutex
866 * being held. */
867 if (managed & DCACHE_MANAGE_TRANSIT) {
868 BUG_ON(!path->dentry->d_op);
869 BUG_ON(!path->dentry->d_op->d_manage);
Al Viro1aed3e42011-03-18 09:09:02 -0400870 ret = path->dentry->d_op->d_manage(path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +0000871 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +0100872 break;
David Howellscc53ce52011-01-14 18:45:26 +0000873 }
874
David Howells9875cf82011-01-14 18:45:21 +0000875 /* Transit to a mounted filesystem. */
876 if (managed & DCACHE_MOUNTED) {
877 struct vfsmount *mounted = lookup_mnt(path);
878 if (mounted) {
879 dput(path->dentry);
880 if (need_mntput)
881 mntput(path->mnt);
882 path->mnt = mounted;
883 path->dentry = dget(mounted->mnt_root);
884 need_mntput = true;
885 continue;
886 }
887
888 /* Something is mounted on this dentry in another
889 * namespace and/or whatever was mounted there in this
890 * namespace got unmounted before we managed to get the
891 * vfsmount_lock */
892 }
893
894 /* Handle an automount point */
895 if (managed & DCACHE_NEED_AUTOMOUNT) {
896 ret = follow_automount(path, flags, &need_mntput);
897 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +0100898 break;
David Howells9875cf82011-01-14 18:45:21 +0000899 continue;
900 }
901
902 /* We didn't change the current path point */
903 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
Al Viro8aef1882011-06-16 15:10:06 +0100905
906 if (need_mntput && path->mnt == mnt)
907 mntput(path->mnt);
908 if (ret == -EISDIR)
909 ret = 0;
910 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911}
912
David Howellscc53ce52011-01-14 18:45:26 +0000913int follow_down_one(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
915 struct vfsmount *mounted;
916
Al Viro1c755af2009-04-18 14:06:57 -0400917 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -0400919 dput(path->dentry);
920 mntput(path->mnt);
921 path->mnt = mounted;
922 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return 1;
924 }
925 return 0;
926}
927
Ian Kent62a73752011-03-25 01:51:02 +0800928static inline bool managed_dentry_might_block(struct dentry *dentry)
929{
930 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
931 dentry->d_op->d_manage(dentry, true) < 0);
932}
933
David Howells9875cf82011-01-14 18:45:21 +0000934/*
Al Viro287548e2011-05-27 06:50:06 -0400935 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
936 * we meet a managed dentry that would need blocking.
David Howells9875cf82011-01-14 18:45:21 +0000937 */
938static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
Al Viro287548e2011-05-27 06:50:06 -0400939 struct inode **inode)
David Howells9875cf82011-01-14 18:45:21 +0000940{
Ian Kent62a73752011-03-25 01:51:02 +0800941 for (;;) {
David Howells9875cf82011-01-14 18:45:21 +0000942 struct vfsmount *mounted;
Ian Kent62a73752011-03-25 01:51:02 +0800943 /*
944 * Don't forget we might have a non-mountpoint managed dentry
945 * that wants to block transit.
946 */
Al Viro287548e2011-05-27 06:50:06 -0400947 if (unlikely(managed_dentry_might_block(path->dentry)))
David Howellsab909112011-01-14 18:46:51 +0000948 return false;
Ian Kent62a73752011-03-25 01:51:02 +0800949
950 if (!d_mountpoint(path->dentry))
951 break;
952
David Howells9875cf82011-01-14 18:45:21 +0000953 mounted = __lookup_mnt(path->mnt, path->dentry, 1);
954 if (!mounted)
955 break;
956 path->mnt = mounted;
957 path->dentry = mounted->mnt_root;
958 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
Linus Torvalds59430262011-07-18 15:43:29 -0700959 /*
960 * Update the inode too. We don't need to re-check the
961 * dentry sequence number here after this d_inode read,
962 * because a mount-point is always pinned.
963 */
964 *inode = path->dentry->d_inode;
David Howells9875cf82011-01-14 18:45:21 +0000965 }
David Howells9875cf82011-01-14 18:45:21 +0000966 return true;
967}
968
Al Virodea39372011-05-27 06:53:39 -0400969static void follow_mount_rcu(struct nameidata *nd)
Al Viro287548e2011-05-27 06:50:06 -0400970{
Al Virodea39372011-05-27 06:53:39 -0400971 while (d_mountpoint(nd->path.dentry)) {
Al Viro287548e2011-05-27 06:50:06 -0400972 struct vfsmount *mounted;
Al Virodea39372011-05-27 06:53:39 -0400973 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
Al Viro287548e2011-05-27 06:50:06 -0400974 if (!mounted)
975 break;
Al Virodea39372011-05-27 06:53:39 -0400976 nd->path.mnt = mounted;
977 nd->path.dentry = mounted->mnt_root;
978 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro287548e2011-05-27 06:50:06 -0400979 }
980}
981
Nick Piggin31e6b012011-01-07 17:49:52 +1100982static int follow_dotdot_rcu(struct nameidata *nd)
983{
Nick Piggin31e6b012011-01-07 17:49:52 +1100984 set_root_rcu(nd);
985
David Howells9875cf82011-01-14 18:45:21 +0000986 while (1) {
Nick Piggin31e6b012011-01-07 17:49:52 +1100987 if (nd->path.dentry == nd->root.dentry &&
988 nd->path.mnt == nd->root.mnt) {
989 break;
990 }
991 if (nd->path.dentry != nd->path.mnt->mnt_root) {
992 struct dentry *old = nd->path.dentry;
993 struct dentry *parent = old->d_parent;
994 unsigned seq;
995
996 seq = read_seqcount_begin(&parent->d_seq);
997 if (read_seqcount_retry(&old->d_seq, nd->seq))
Al Viroef7562d2011-03-04 14:35:59 -0500998 goto failed;
Nick Piggin31e6b012011-01-07 17:49:52 +1100999 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);
Nick Piggin31e6b012011-01-07 17:49:52 +11001006 }
Al Virodea39372011-05-27 06:53:39 -04001007 follow_mount_rcu(nd);
1008 nd->inode = nd->path.dentry->d_inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001009 return 0;
Al Viroef7562d2011-03-04 14:35:59 -05001010
1011failed:
1012 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001013 if (!(nd->flags & LOOKUP_ROOT))
1014 nd->root.mnt = NULL;
Al Viroef7562d2011-03-04 14:35:59 -05001015 rcu_read_unlock();
1016 br_read_unlock(vfsmount_lock);
1017 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001018}
1019
David Howells9875cf82011-01-14 18:45:21 +00001020/*
David Howellscc53ce52011-01-14 18:45:26 +00001021 * Follow down to the covering mount currently visible to userspace. At each
1022 * point, the filesystem owning that dentry may be queried as to whether the
1023 * caller is permitted to proceed or not.
David Howellscc53ce52011-01-14 18:45:26 +00001024 */
Al Viro7cc90cc2011-03-18 09:04:20 -04001025int follow_down(struct path *path)
David Howellscc53ce52011-01-14 18:45:26 +00001026{
1027 unsigned managed;
1028 int ret;
1029
1030 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1031 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1032 /* Allow the filesystem to manage the transit without i_mutex
1033 * being held.
1034 *
1035 * We indicate to the filesystem if someone is trying to mount
1036 * something here. This gives autofs the chance to deny anyone
1037 * other than its daemon the right to mount on its
1038 * superstructure.
1039 *
1040 * The filesystem may sleep at this point.
1041 */
1042 if (managed & DCACHE_MANAGE_TRANSIT) {
1043 BUG_ON(!path->dentry->d_op);
1044 BUG_ON(!path->dentry->d_op->d_manage);
David Howellsab909112011-01-14 18:46:51 +00001045 ret = path->dentry->d_op->d_manage(
Al Viro1aed3e42011-03-18 09:09:02 -04001046 path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +00001047 if (ret < 0)
1048 return ret == -EISDIR ? 0 : ret;
1049 }
1050
1051 /* Transit to a mounted filesystem. */
1052 if (managed & DCACHE_MOUNTED) {
1053 struct vfsmount *mounted = lookup_mnt(path);
1054 if (!mounted)
1055 break;
1056 dput(path->dentry);
1057 mntput(path->mnt);
1058 path->mnt = mounted;
1059 path->dentry = dget(mounted->mnt_root);
1060 continue;
1061 }
1062
1063 /* Don't handle automount points here */
1064 break;
1065 }
1066 return 0;
1067}
1068
1069/*
David Howells9875cf82011-01-14 18:45:21 +00001070 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1071 */
1072static void follow_mount(struct path *path)
1073{
1074 while (d_mountpoint(path->dentry)) {
1075 struct vfsmount *mounted = lookup_mnt(path);
1076 if (!mounted)
1077 break;
1078 dput(path->dentry);
1079 mntput(path->mnt);
1080 path->mnt = mounted;
1081 path->dentry = dget(mounted->mnt_root);
1082 }
1083}
1084
Nick Piggin31e6b012011-01-07 17:49:52 +11001085static void follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086{
Al Viro2a737872009-04-07 11:49:53 -04001087 set_root(nd);
Andreas Mohre518ddb2006-09-29 02:01:22 -07001088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 while(1) {
Jan Blunck4ac91372008-02-14 19:34:32 -08001090 struct dentry *old = nd->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Al Viro2a737872009-04-07 11:49:53 -04001092 if (nd->path.dentry == nd->root.dentry &&
1093 nd->path.mnt == nd->root.mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 break;
1095 }
Jan Blunck4ac91372008-02-14 19:34:32 -08001096 if (nd->path.dentry != nd->path.mnt->mnt_root) {
Al Viro3088dd72010-01-30 15:47:29 -05001097 /* rare case of legitimate dget_parent()... */
1098 nd->path.dentry = dget_parent(nd->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 dput(old);
1100 break;
1101 }
Al Viro3088dd72010-01-30 15:47:29 -05001102 if (!follow_up(&nd->path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 }
Al Viro79ed0222009-04-18 13:59:41 -04001105 follow_mount(&nd->path);
Nick Piggin31e6b012011-01-07 17:49:52 +11001106 nd->inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107}
1108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109/*
Nick Pigginbaa03892010-08-18 04:37:31 +10001110 * Allocate a dentry with name and parent, and perform a parent
1111 * directory ->lookup on it. Returns the new dentry, or ERR_PTR
1112 * on error. parent->d_inode->i_mutex must be held. d_lookup must
1113 * have verified that no child exists while under i_mutex.
1114 */
1115static struct dentry *d_alloc_and_lookup(struct dentry *parent,
1116 struct qstr *name, struct nameidata *nd)
1117{
1118 struct inode *inode = parent->d_inode;
1119 struct dentry *dentry;
1120 struct dentry *old;
1121
1122 /* Don't create child dentry for a dead directory. */
1123 if (unlikely(IS_DEADDIR(inode)))
1124 return ERR_PTR(-ENOENT);
1125
1126 dentry = d_alloc(parent, name);
1127 if (unlikely(!dentry))
1128 return ERR_PTR(-ENOMEM);
1129
1130 old = inode->i_op->lookup(inode, dentry, nd);
1131 if (unlikely(old)) {
1132 dput(dentry);
1133 dentry = old;
1134 }
1135 return dentry;
1136}
1137
1138/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 * It's more convoluted than I'd like it to be, but... it's still fairly
1140 * small and for now I'd prefer to have fast path as straight as possible.
1141 * It _is_ time-critical.
1142 */
1143static int do_lookup(struct nameidata *nd, struct qstr *name,
Nick Piggin31e6b012011-01-07 17:49:52 +11001144 struct path *path, struct inode **inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
Jan Blunck4ac91372008-02-14 19:34:32 -08001146 struct vfsmount *mnt = nd->path.mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +11001147 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro5a18fff2011-03-11 04:44:53 -05001148 int need_reval = 1;
1149 int status = 1;
David Howells9875cf82011-01-14 18:45:21 +00001150 int err;
1151
Al Viro3cac2602009-08-13 18:27:43 +04001152 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001153 * Rename seqlock is not required here because in the off chance
1154 * of a false negative due to a concurrent rename, we're going to
1155 * do the non-racy lookup, below.
1156 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001157 if (nd->flags & LOOKUP_RCU) {
1158 unsigned seq;
Nick Piggin31e6b012011-01-07 17:49:52 +11001159 *inode = nd->inode;
1160 dentry = __d_lookup_rcu(parent, name, &seq, inode);
Al Viro5a18fff2011-03-11 04:44:53 -05001161 if (!dentry)
1162 goto unlazy;
1163
Nick Piggin31e6b012011-01-07 17:49:52 +11001164 /* Memory barrier in read_seqcount_begin of child is enough */
1165 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1166 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001167 nd->seq = seq;
Al Viro5a18fff2011-03-11 04:44:53 -05001168
Al Viro24643082011-02-15 01:26:22 -05001169 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
Al Viro5a18fff2011-03-11 04:44:53 -05001170 status = d_revalidate(dentry, nd);
1171 if (unlikely(status <= 0)) {
1172 if (status != -ECHILD)
1173 need_reval = 0;
1174 goto unlazy;
1175 }
Al Viro24643082011-02-15 01:26:22 -05001176 }
Nick Piggin31e6b012011-01-07 17:49:52 +11001177 path->mnt = mnt;
1178 path->dentry = dentry;
Al Virod6e9bd22011-05-27 07:03:15 -04001179 if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1180 goto unlazy;
1181 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1182 goto unlazy;
1183 return 0;
Al Viro5a18fff2011-03-11 04:44:53 -05001184unlazy:
Al Viro19660af2011-03-25 10:32:48 -04001185 if (unlazy_walk(nd, dentry))
1186 return -ECHILD;
Al Viro5a18fff2011-03-11 04:44:53 -05001187 } else {
1188 dentry = __d_lookup(parent, name);
Nick Piggin31e6b012011-01-07 17:49:52 +11001189 }
Al Viro5a18fff2011-03-11 04:44:53 -05001190
1191retry:
1192 if (unlikely(!dentry)) {
1193 struct inode *dir = parent->d_inode;
1194 BUG_ON(nd->inode != dir);
1195
1196 mutex_lock(&dir->i_mutex);
1197 dentry = d_lookup(parent, name);
1198 if (likely(!dentry)) {
1199 dentry = d_alloc_and_lookup(parent, name, nd);
1200 if (IS_ERR(dentry)) {
1201 mutex_unlock(&dir->i_mutex);
1202 return PTR_ERR(dentry);
1203 }
1204 /* known good */
1205 need_reval = 0;
1206 status = 1;
1207 }
1208 mutex_unlock(&dir->i_mutex);
Al Viro24643082011-02-15 01:26:22 -05001209 }
Al Viro5a18fff2011-03-11 04:44:53 -05001210 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
1211 status = d_revalidate(dentry, nd);
1212 if (unlikely(status <= 0)) {
1213 if (status < 0) {
1214 dput(dentry);
1215 return status;
1216 }
1217 if (!d_invalidate(dentry)) {
1218 dput(dentry);
1219 dentry = NULL;
1220 need_reval = 1;
1221 goto retry;
1222 }
1223 }
1224
David Howells9875cf82011-01-14 18:45:21 +00001225 path->mnt = mnt;
1226 path->dentry = dentry;
1227 err = follow_managed(path, nd->flags);
Ian Kent89312212011-01-18 12:06:10 +08001228 if (unlikely(err < 0)) {
1229 path_put_conditional(path, nd);
David Howells9875cf82011-01-14 18:45:21 +00001230 return err;
Ian Kent89312212011-01-18 12:06:10 +08001231 }
David Howells9875cf82011-01-14 18:45:21 +00001232 *inode = path->dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234}
1235
Al Viro52094c82011-02-21 21:34:47 -05001236static inline int may_lookup(struct nameidata *nd)
1237{
1238 if (nd->flags & LOOKUP_RCU) {
1239 int err = exec_permission(nd->inode, IPERM_FLAG_RCU);
1240 if (err != -ECHILD)
1241 return err;
Al Viro19660af2011-03-25 10:32:48 -04001242 if (unlazy_walk(nd, NULL))
Al Viro52094c82011-02-21 21:34:47 -05001243 return -ECHILD;
1244 }
1245 return exec_permission(nd->inode, 0);
1246}
1247
Al Viro9856fa12011-03-04 14:22:06 -05001248static inline int handle_dots(struct nameidata *nd, int type)
1249{
1250 if (type == LAST_DOTDOT) {
1251 if (nd->flags & LOOKUP_RCU) {
1252 if (follow_dotdot_rcu(nd))
1253 return -ECHILD;
1254 } else
1255 follow_dotdot(nd);
1256 }
1257 return 0;
1258}
1259
Al Viro951361f2011-03-04 14:44:37 -05001260static void terminate_walk(struct nameidata *nd)
1261{
1262 if (!(nd->flags & LOOKUP_RCU)) {
1263 path_put(&nd->path);
1264 } else {
1265 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001266 if (!(nd->flags & LOOKUP_ROOT))
1267 nd->root.mnt = NULL;
Al Viro951361f2011-03-04 14:44:37 -05001268 rcu_read_unlock();
1269 br_read_unlock(vfsmount_lock);
1270 }
1271}
1272
Al Viroce57dfc2011-03-13 19:58:58 -04001273static inline int walk_component(struct nameidata *nd, struct path *path,
1274 struct qstr *name, int type, int follow)
1275{
1276 struct inode *inode;
1277 int err;
1278 /*
1279 * "." and ".." are special - ".." especially so because it has
1280 * to be able to know about the current root directory and
1281 * parent relationships.
1282 */
1283 if (unlikely(type != LAST_NORM))
1284 return handle_dots(nd, type);
1285 err = do_lookup(nd, name, path, &inode);
1286 if (unlikely(err)) {
1287 terminate_walk(nd);
1288 return err;
1289 }
1290 if (!inode) {
1291 path_to_nameidata(path, nd);
1292 terminate_walk(nd);
1293 return -ENOENT;
1294 }
1295 if (unlikely(inode->i_op->follow_link) && follow) {
Al Viro19660af2011-03-25 10:32:48 -04001296 if (nd->flags & LOOKUP_RCU) {
1297 if (unlikely(unlazy_walk(nd, path->dentry))) {
1298 terminate_walk(nd);
1299 return -ECHILD;
1300 }
1301 }
Al Viroce57dfc2011-03-13 19:58:58 -04001302 BUG_ON(inode != path->dentry->d_inode);
1303 return 1;
1304 }
1305 path_to_nameidata(path, nd);
1306 nd->inode = inode;
1307 return 0;
1308}
1309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310/*
Al Virob3563792011-03-14 21:54:55 -04001311 * This limits recursive symlink follows to 8, while
1312 * limiting consecutive symlinks to 40.
1313 *
1314 * Without that kind of total limit, nasty chains of consecutive
1315 * symlinks can cause almost arbitrarily long lookups.
1316 */
1317static inline int nested_symlink(struct path *path, struct nameidata *nd)
1318{
1319 int res;
1320
Al Virob3563792011-03-14 21:54:55 -04001321 if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1322 path_put_conditional(path, nd);
1323 path_put(&nd->path);
1324 return -ELOOP;
1325 }
Erez Zadok1a4022f2011-05-21 01:19:59 -04001326 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
Al Virob3563792011-03-14 21:54:55 -04001327
1328 nd->depth++;
1329 current->link_count++;
1330
1331 do {
1332 struct path link = *path;
1333 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04001334
1335 res = follow_link(&link, nd, &cookie);
Al Virob3563792011-03-14 21:54:55 -04001336 if (!res)
1337 res = walk_component(nd, path, &nd->last,
1338 nd->last_type, LOOKUP_FOLLOW);
Al Viro574197e2011-03-14 22:20:34 -04001339 put_link(nd, &link, cookie);
Al Virob3563792011-03-14 21:54:55 -04001340 } while (res > 0);
1341
1342 current->link_count--;
1343 nd->depth--;
1344 return res;
1345}
1346
1347/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01001349 * This is the basic name resolution function, turning a pathname into
1350 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01001352 * Returns 0 and nd will have valid dentry and mnt on success.
1353 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 */
Al Viro6de88d72009-08-09 01:41:57 +04001355static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356{
1357 struct path next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 int err;
1359 unsigned int lookup_flags = nd->flags;
1360
1361 while (*name=='/')
1362 name++;
1363 if (!*name)
Al Viro086e1832011-02-22 20:56:27 -05001364 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 /* At this point we know we have a real path component. */
1367 for(;;) {
1368 unsigned long hash;
1369 struct qstr this;
1370 unsigned int c;
Al Virofe479a52011-02-22 15:10:03 -05001371 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Trond Myklebustcdce5d62005-10-18 14:20:18 -07001373 nd->flags |= LOOKUP_CONTINUE;
Al Viro52094c82011-02-21 21:34:47 -05001374
1375 err = may_lookup(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 if (err)
1377 break;
1378
1379 this.name = name;
1380 c = *(const unsigned char *)name;
1381
1382 hash = init_name_hash();
1383 do {
1384 name++;
1385 hash = partial_name_hash(c, hash);
1386 c = *(const unsigned char *)name;
1387 } while (c && (c != '/'));
1388 this.len = name - (const char *) this.name;
1389 this.hash = end_name_hash(hash);
1390
Al Virofe479a52011-02-22 15:10:03 -05001391 type = LAST_NORM;
1392 if (this.name[0] == '.') switch (this.len) {
1393 case 2:
Al Viro16c2cd72011-02-22 15:50:10 -05001394 if (this.name[1] == '.') {
Al Virofe479a52011-02-22 15:10:03 -05001395 type = LAST_DOTDOT;
Al Viro16c2cd72011-02-22 15:50:10 -05001396 nd->flags |= LOOKUP_JUMPED;
1397 }
Al Virofe479a52011-02-22 15:10:03 -05001398 break;
1399 case 1:
1400 type = LAST_DOT;
1401 }
Al Viro5a202bc2011-03-08 14:17:44 -05001402 if (likely(type == LAST_NORM)) {
1403 struct dentry *parent = nd->path.dentry;
Al Viro16c2cd72011-02-22 15:50:10 -05001404 nd->flags &= ~LOOKUP_JUMPED;
Al Viro5a202bc2011-03-08 14:17:44 -05001405 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1406 err = parent->d_op->d_hash(parent, nd->inode,
1407 &this);
1408 if (err < 0)
1409 break;
1410 }
1411 }
Al Virofe479a52011-02-22 15:10:03 -05001412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 /* remove trailing slashes? */
1414 if (!c)
1415 goto last_component;
1416 while (*++name == '/');
1417 if (!*name)
Al Virob3563792011-03-14 21:54:55 -04001418 goto last_component;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Al Viroce57dfc2011-03-13 19:58:58 -04001420 err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1421 if (err < 0)
1422 return err;
Al Virofe479a52011-02-22 15:10:03 -05001423
Al Viroce57dfc2011-03-13 19:58:58 -04001424 if (err) {
Al Virob3563792011-03-14 21:54:55 -04001425 err = nested_symlink(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 if (err)
Al Viroa7472ba2011-03-04 14:39:30 -05001427 return err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 err = -ENOTDIR;
Nick Piggin31e6b012011-01-07 17:49:52 +11001430 if (!nd->inode->i_op->lookup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 break;
1432 continue;
1433 /* here ends the main loop */
1434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435last_component:
Trond Myklebustf55eab82006-02-04 23:28:01 -08001436 /* Clear LOOKUP_CONTINUE iff it was previously unset */
1437 nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
Al Virob3563792011-03-14 21:54:55 -04001438 nd->last = this;
1439 nd->last_type = type;
Al Viro086e1832011-02-22 20:56:27 -05001440 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 }
Al Viro951361f2011-03-04 14:44:37 -05001442 terminate_walk(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 return err;
1444}
1445
Al Viro70e9b352011-03-05 21:12:22 -05001446static int path_init(int dfd, const char *name, unsigned int flags,
1447 struct nameidata *nd, struct file **fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
Prasanna Medaea3834d2005-04-29 16:00:17 +01001449 int retval = 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001450 int fput_needed;
1451 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
1453 nd->last_type = LAST_ROOT; /* if there are only slashes... */
Al Viro16c2cd72011-02-22 15:50:10 -05001454 nd->flags = flags | LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 nd->depth = 0;
Al Viro5b6ca022011-03-09 23:04:47 -05001456 if (flags & LOOKUP_ROOT) {
1457 struct inode *inode = nd->root.dentry->d_inode;
Al Viro73d049a2011-03-11 12:08:24 -05001458 if (*name) {
1459 if (!inode->i_op->lookup)
1460 return -ENOTDIR;
1461 retval = inode_permission(inode, MAY_EXEC);
1462 if (retval)
1463 return retval;
1464 }
Al Viro5b6ca022011-03-09 23:04:47 -05001465 nd->path = nd->root;
1466 nd->inode = inode;
1467 if (flags & LOOKUP_RCU) {
1468 br_read_lock(vfsmount_lock);
1469 rcu_read_lock();
1470 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1471 } else {
1472 path_get(&nd->path);
1473 }
1474 return 0;
1475 }
1476
Al Viro2a737872009-04-07 11:49:53 -04001477 nd->root.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 if (*name=='/') {
Al Viroe41f7d42011-02-22 14:02:58 -05001480 if (flags & LOOKUP_RCU) {
1481 br_read_lock(vfsmount_lock);
1482 rcu_read_lock();
1483 set_root_rcu(nd);
1484 } else {
1485 set_root(nd);
1486 path_get(&nd->root);
1487 }
Al Viro2a737872009-04-07 11:49:53 -04001488 nd->path = nd->root;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001489 } else if (dfd == AT_FDCWD) {
Al Viroe41f7d42011-02-22 14:02:58 -05001490 if (flags & LOOKUP_RCU) {
1491 struct fs_struct *fs = current->fs;
1492 unsigned seq;
1493
1494 br_read_lock(vfsmount_lock);
1495 rcu_read_lock();
1496
1497 do {
1498 seq = read_seqcount_begin(&fs->seq);
1499 nd->path = fs->pwd;
1500 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1501 } while (read_seqcount_retry(&fs->seq, seq));
1502 } else {
1503 get_fs_pwd(current->fs, &nd->path);
1504 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001505 } else {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001506 struct dentry *dentry;
1507
Al Viro1abf0c72011-03-13 03:51:11 -04001508 file = fget_raw_light(dfd, &fput_needed);
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001509 retval = -EBADF;
1510 if (!file)
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001511 goto out_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001512
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001513 dentry = file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001514
Al Virof52e0c12011-03-14 18:56:51 -04001515 if (*name) {
1516 retval = -ENOTDIR;
1517 if (!S_ISDIR(dentry->d_inode->i_mode))
1518 goto fput_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001519
Al Virof52e0c12011-03-14 18:56:51 -04001520 retval = file_permission(file, MAY_EXEC);
1521 if (retval)
1522 goto fput_fail;
1523 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001524
Jan Blunck5dd784d2008-02-14 19:34:38 -08001525 nd->path = file->f_path;
Al Viroe41f7d42011-02-22 14:02:58 -05001526 if (flags & LOOKUP_RCU) {
1527 if (fput_needed)
Al Viro70e9b352011-03-05 21:12:22 -05001528 *fp = file;
Al Viroe41f7d42011-02-22 14:02:58 -05001529 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1530 br_read_lock(vfsmount_lock);
1531 rcu_read_lock();
1532 } else {
1533 path_get(&file->f_path);
1534 fput_light(file, fput_needed);
1535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 }
Al Viroe41f7d42011-02-22 14:02:58 -05001537
Nick Piggin31e6b012011-01-07 17:49:52 +11001538 nd->inode = nd->path.dentry->d_inode;
Al Viro9b4a9b12009-04-07 11:44:16 -04001539 return 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001540
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001541fput_fail:
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001542 fput_light(file, fput_needed);
Al Viro9b4a9b12009-04-07 11:44:16 -04001543out_fail:
1544 return retval;
1545}
1546
Al Virobd92d7f2011-03-14 19:54:59 -04001547static inline int lookup_last(struct nameidata *nd, struct path *path)
1548{
1549 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1550 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1551
1552 nd->flags &= ~LOOKUP_PARENT;
1553 return walk_component(nd, path, &nd->last, nd->last_type,
1554 nd->flags & LOOKUP_FOLLOW);
1555}
1556
Al Viro9b4a9b12009-04-07 11:44:16 -04001557/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroee0827c2011-02-21 23:38:09 -05001558static int path_lookupat(int dfd, const char *name,
Al Viro9b4a9b12009-04-07 11:44:16 -04001559 unsigned int flags, struct nameidata *nd)
1560{
Al Viro70e9b352011-03-05 21:12:22 -05001561 struct file *base = NULL;
Al Virobd92d7f2011-03-14 19:54:59 -04001562 struct path path;
1563 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001564
1565 /*
1566 * Path walking is largely split up into 2 different synchronisation
1567 * schemes, rcu-walk and ref-walk (explained in
1568 * Documentation/filesystems/path-lookup.txt). These share much of the
1569 * path walk code, but some things particularly setup, cleanup, and
1570 * following mounts are sufficiently divergent that functions are
1571 * duplicated. Typically there is a function foo(), and its RCU
1572 * analogue, foo_rcu().
1573 *
1574 * -ECHILD is the error number of choice (just to avoid clashes) that
1575 * is returned if some aspect of an rcu-walk fails. Such an error must
1576 * be handled by restarting a traditional ref-walk (which will always
1577 * be able to complete).
1578 */
Al Virobd92d7f2011-03-14 19:54:59 -04001579 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
Al Viroee0827c2011-02-21 23:38:09 -05001580
Al Virobd92d7f2011-03-14 19:54:59 -04001581 if (unlikely(err))
1582 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001583
1584 current->total_link_count = 0;
Al Virobd92d7f2011-03-14 19:54:59 -04001585 err = link_path_walk(name, nd);
1586
1587 if (!err && !(flags & LOOKUP_PARENT)) {
Al Virobd92d7f2011-03-14 19:54:59 -04001588 err = lookup_last(nd, &path);
1589 while (err > 0) {
1590 void *cookie;
1591 struct path link = path;
Al Virobd92d7f2011-03-14 19:54:59 -04001592 nd->flags |= LOOKUP_PARENT;
Al Viro574197e2011-03-14 22:20:34 -04001593 err = follow_link(&link, nd, &cookie);
Al Virobd92d7f2011-03-14 19:54:59 -04001594 if (!err)
1595 err = lookup_last(nd, &path);
Al Viro574197e2011-03-14 22:20:34 -04001596 put_link(nd, &link, cookie);
Al Virobd92d7f2011-03-14 19:54:59 -04001597 }
1598 }
Al Viroee0827c2011-02-21 23:38:09 -05001599
Al Viro9f1fafe2011-03-25 11:00:12 -04001600 if (!err)
1601 err = complete_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04001602
1603 if (!err && nd->flags & LOOKUP_DIRECTORY) {
1604 if (!nd->inode->i_op->lookup) {
1605 path_put(&nd->path);
Al Virobd23a532011-03-23 09:56:30 -04001606 err = -ENOTDIR;
Al Virobd92d7f2011-03-14 19:54:59 -04001607 }
1608 }
Al Viro16c2cd72011-02-22 15:50:10 -05001609
Al Viro70e9b352011-03-05 21:12:22 -05001610 if (base)
1611 fput(base);
Al Viroee0827c2011-02-21 23:38:09 -05001612
Al Viro5b6ca022011-03-09 23:04:47 -05001613 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
Al Viro2a737872009-04-07 11:49:53 -04001614 path_put(&nd->root);
1615 nd->root.mnt = NULL;
1616 }
Al Virobd92d7f2011-03-14 19:54:59 -04001617 return err;
Al Viroee0827c2011-02-21 23:38:09 -05001618}
Nick Piggin31e6b012011-01-07 17:49:52 +11001619
Al Viroee0827c2011-02-21 23:38:09 -05001620static int do_path_lookup(int dfd, const char *name,
1621 unsigned int flags, struct nameidata *nd)
1622{
1623 int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1624 if (unlikely(retval == -ECHILD))
1625 retval = path_lookupat(dfd, name, flags, nd);
1626 if (unlikely(retval == -ESTALE))
1627 retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11001628
1629 if (likely(!retval)) {
1630 if (unlikely(!audit_dummy_context())) {
1631 if (nd->path.dentry && nd->inode)
1632 audit_inode(name, nd->path.dentry);
1633 }
1634 }
Al Viro9b4a9b12009-04-07 11:44:16 -04001635 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636}
1637
Al Viroc9c6cac2011-02-16 15:15:47 -05001638int kern_path_parent(const char *name, struct nameidata *nd)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001639{
Al Viroc9c6cac2011-02-16 15:15:47 -05001640 return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001641}
1642
Al Virod1811462008-08-02 00:49:18 -04001643int kern_path(const char *name, unsigned int flags, struct path *path)
1644{
1645 struct nameidata nd;
1646 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1647 if (!res)
1648 *path = nd.path;
1649 return res;
1650}
1651
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001652/**
1653 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1654 * @dentry: pointer to dentry of the base directory
1655 * @mnt: pointer to vfs mount of the base directory
1656 * @name: pointer to file name
1657 * @flags: lookup flags
1658 * @nd: pointer to nameidata
1659 */
1660int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
1661 const char *name, unsigned int flags,
1662 struct nameidata *nd)
1663{
Al Viro5b6ca022011-03-09 23:04:47 -05001664 nd->root.dentry = dentry;
1665 nd->root.mnt = mnt;
1666 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
1667 return do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, nd);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001668}
1669
Christoph Hellwigeead1912007-10-16 23:25:38 -07001670static struct dentry *__lookup_hash(struct qstr *name,
1671 struct dentry *base, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672{
Christoph Hellwig81fca442010-10-06 10:47:47 +02001673 struct inode *inode = base->d_inode;
James Morris057f6c02007-04-26 00:12:05 -07001674 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 int err;
1676
Nick Pigginb74c79e2011-01-07 17:49:58 +11001677 err = exec_permission(inode, 0);
Christoph Hellwig81fca442010-10-06 10:47:47 +02001678 if (err)
1679 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
1681 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001682 * Don't bother with __d_lookup: callers are for creat as
1683 * well as unlink, so a lot of the time it would cost
1684 * a double lookup.
Al Viro6e6b1bd2009-08-13 23:38:37 +04001685 */
Nick Pigginb04f7842010-08-18 04:37:34 +10001686 dentry = d_lookup(base, name);
Al Viro6e6b1bd2009-08-13 23:38:37 +04001687
Nick Pigginfb045ad2011-01-07 17:49:55 +11001688 if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE))
Al Viro6e6b1bd2009-08-13 23:38:37 +04001689 dentry = do_revalidate(dentry, nd);
1690
Nick Pigginbaa03892010-08-18 04:37:31 +10001691 if (!dentry)
1692 dentry = d_alloc_and_lookup(base, name, nd);
Al Viro5a202bc2011-03-08 14:17:44 -05001693
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 return dentry;
1695}
1696
James Morris057f6c02007-04-26 00:12:05 -07001697/*
1698 * Restricted form of lookup. Doesn't follow links, single-component only,
1699 * needs parent already locked. Doesn't follow mounts.
1700 * SMP-safe.
1701 */
Adrian Bunka244e162006-03-31 02:32:11 -08001702static struct dentry *lookup_hash(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703{
Jan Blunck4ac91372008-02-14 19:34:32 -08001704 return __lookup_hash(&nd->last, nd->path.dentry, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705}
1706
Christoph Hellwigeead1912007-10-16 23:25:38 -07001707/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07001708 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07001709 * @name: pathname component to lookup
1710 * @base: base directory to lookup from
1711 * @len: maximum length @len should be interpreted to
1712 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07001713 * Note that this routine is purely a helper for filesystem usage and should
1714 * not be called by generic code. Also note that by using this function the
Christoph Hellwigeead1912007-10-16 23:25:38 -07001715 * nameidata argument is passed to the filesystem methods and a filesystem
1716 * using this helper needs to be prepared for that.
1717 */
James Morris057f6c02007-04-26 00:12:05 -07001718struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1719{
James Morris057f6c02007-04-26 00:12:05 -07001720 struct qstr this;
Al Viro6a96ba52011-03-07 23:49:20 -05001721 unsigned long hash;
1722 unsigned int c;
James Morris057f6c02007-04-26 00:12:05 -07001723
David Woodhouse2f9092e2009-04-20 23:18:37 +01001724 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
1725
Al Viro6a96ba52011-03-07 23:49:20 -05001726 this.name = name;
1727 this.len = len;
1728 if (!len)
1729 return ERR_PTR(-EACCES);
1730
1731 hash = init_name_hash();
1732 while (len--) {
1733 c = *(const unsigned char *)name++;
1734 if (c == '/' || c == '\0')
1735 return ERR_PTR(-EACCES);
1736 hash = partial_name_hash(c, hash);
1737 }
1738 this.hash = end_name_hash(hash);
Al Viro5a202bc2011-03-08 14:17:44 -05001739 /*
1740 * See if the low-level filesystem might want
1741 * to use its own hash..
1742 */
1743 if (base->d_flags & DCACHE_OP_HASH) {
1744 int err = base->d_op->d_hash(base, base->d_inode, &this);
1745 if (err < 0)
1746 return ERR_PTR(err);
1747 }
Christoph Hellwigeead1912007-10-16 23:25:38 -07001748
Christoph Hellwig49705b72005-11-08 21:35:06 -08001749 return __lookup_hash(&this, base, NULL);
James Morris057f6c02007-04-26 00:12:05 -07001750}
1751
Andy Whitcroftb0917c32011-11-02 09:44:39 +01001752int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
1753 struct path *path, int *empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754{
Al Viro2d8f3032008-07-22 09:59:21 -04001755 struct nameidata nd;
Andy Whitcroftb0917c32011-11-02 09:44:39 +01001756 char *tmp = getname_flags(name, flags, empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 int err = PTR_ERR(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 if (!IS_ERR(tmp)) {
Al Viro2d8f3032008-07-22 09:59:21 -04001759
1760 BUG_ON(flags & LOOKUP_PARENT);
1761
1762 err = do_path_lookup(dfd, tmp, flags, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 putname(tmp);
Al Viro2d8f3032008-07-22 09:59:21 -04001764 if (!err)
1765 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 }
1767 return err;
1768}
1769
Andy Whitcroftb0917c32011-11-02 09:44:39 +01001770int user_path_at(int dfd, const char __user *name, unsigned flags,
1771 struct path *path)
1772{
1773 return user_path_at_empty(dfd, name, flags, path, 0);
1774}
1775
Al Viro2ad94ae2008-07-21 09:32:51 -04001776static int user_path_parent(int dfd, const char __user *path,
1777 struct nameidata *nd, char **name)
1778{
1779 char *s = getname(path);
1780 int error;
1781
1782 if (IS_ERR(s))
1783 return PTR_ERR(s);
1784
1785 error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
1786 if (error)
1787 putname(s);
1788 else
1789 *name = s;
1790
1791 return error;
1792}
1793
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794/*
1795 * It's inline, so penalty for filesystems that don't use sticky bit is
1796 * minimal.
1797 */
1798static inline int check_sticky(struct inode *dir, struct inode *inode)
1799{
David Howellsda9592e2008-11-14 10:39:05 +11001800 uid_t fsuid = current_fsuid();
1801
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 if (!(dir->i_mode & S_ISVTX))
1803 return 0;
Serge E. Hallyne795b712011-03-23 16:43:25 -07001804 if (current_user_ns() != inode_userns(inode))
1805 goto other_userns;
David Howellsda9592e2008-11-14 10:39:05 +11001806 if (inode->i_uid == fsuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 return 0;
David Howellsda9592e2008-11-14 10:39:05 +11001808 if (dir->i_uid == fsuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 return 0;
Serge E. Hallyne795b712011-03-23 16:43:25 -07001810
1811other_userns:
1812 return !ns_capable(inode_userns(inode), CAP_FOWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813}
1814
1815/*
1816 * Check whether we can remove a link victim from directory dir, check
1817 * whether the type of victim is right.
1818 * 1. We can't do it if dir is read-only (done in permission())
1819 * 2. We should have write and exec permissions on dir
1820 * 3. We can't remove anything from append-only dir
1821 * 4. We can't do anything with immutable dir (done in permission())
1822 * 5. If the sticky bit on dir is set we should either
1823 * a. be owner of dir, or
1824 * b. be owner of victim, or
1825 * c. have CAP_FOWNER capability
1826 * 6. If the victim is append-only or immutable we can't do antyhing with
1827 * links pointing to it.
1828 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1829 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1830 * 9. We can't remove a root or mountpoint.
1831 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1832 * nfs_async_unlink().
1833 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001834static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835{
1836 int error;
1837
1838 if (!victim->d_inode)
1839 return -ENOENT;
1840
1841 BUG_ON(victim->d_parent->d_inode != dir);
Al Virocccc6bb2009-12-25 05:07:33 -05001842 audit_inode_child(victim, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
Al Virof419a2e2008-07-22 00:07:17 -04001844 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 if (error)
1846 return error;
1847 if (IS_APPEND(dir))
1848 return -EPERM;
1849 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
Hugh Dickinsf9454542008-11-19 15:36:38 -08001850 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 return -EPERM;
1852 if (isdir) {
1853 if (!S_ISDIR(victim->d_inode->i_mode))
1854 return -ENOTDIR;
1855 if (IS_ROOT(victim))
1856 return -EBUSY;
1857 } else if (S_ISDIR(victim->d_inode->i_mode))
1858 return -EISDIR;
1859 if (IS_DEADDIR(dir))
1860 return -ENOENT;
1861 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1862 return -EBUSY;
1863 return 0;
1864}
1865
1866/* Check whether we can create an object with dentry child in directory
1867 * dir.
1868 * 1. We can't do it if child already exists (open has special treatment for
1869 * this case, but since we are inlined it's OK)
1870 * 2. We can't do it if dir is read-only (done in permission())
1871 * 3. We should have write and exec permissions on dir
1872 * 4. We can't do it if dir is immutable (done in permission())
1873 */
Miklos Szeredia95164d2008-07-30 15:08:48 +02001874static inline int may_create(struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875{
1876 if (child->d_inode)
1877 return -EEXIST;
1878 if (IS_DEADDIR(dir))
1879 return -ENOENT;
Al Virof419a2e2008-07-22 00:07:17 -04001880 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881}
1882
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883/*
1884 * p1 and p2 should be directories on the same fs.
1885 */
1886struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1887{
1888 struct dentry *p;
1889
1890 if (p1 == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07001891 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 return NULL;
1893 }
1894
Arjan van de Vena11f3a02006-03-23 03:00:33 -08001895 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001897 p = d_ancestor(p2, p1);
1898 if (p) {
1899 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1900 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
1901 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 }
1903
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001904 p = d_ancestor(p1, p2);
1905 if (p) {
1906 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1907 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1908 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 }
1910
Ingo Molnarf2eace22006-07-03 00:25:05 -07001911 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1912 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 return NULL;
1914}
1915
1916void unlock_rename(struct dentry *p1, struct dentry *p2)
1917{
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001918 mutex_unlock(&p1->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 if (p1 != p2) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001920 mutex_unlock(&p2->d_inode->i_mutex);
Arjan van de Vena11f3a02006-03-23 03:00:33 -08001921 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 }
1923}
1924
1925int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1926 struct nameidata *nd)
1927{
Miklos Szeredia95164d2008-07-30 15:08:48 +02001928 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
1930 if (error)
1931 return error;
1932
Al Viroacfa4382008-12-04 10:06:33 -05001933 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 return -EACCES; /* shouldn't it be ENOSYS? */
1935 mode &= S_IALLUGO;
1936 mode |= S_IFREG;
1937 error = security_inode_create(dir, dentry, mode);
1938 if (error)
1939 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 error = dir->i_op->create(dir, dentry, mode, nd);
Stephen Smalleya74574a2005-09-09 13:01:44 -07001941 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00001942 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 return error;
1944}
1945
Al Viro73d049a2011-03-11 12:08:24 -05001946static int may_open(struct path *path, int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001948 struct dentry *dentry = path->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 struct inode *inode = dentry->d_inode;
1950 int error;
1951
Al Virobcda7652011-03-13 16:42:14 -04001952 /* O_PATH? */
1953 if (!acc_mode)
1954 return 0;
1955
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 if (!inode)
1957 return -ENOENT;
1958
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001959 switch (inode->i_mode & S_IFMT) {
1960 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001962 case S_IFDIR:
1963 if (acc_mode & MAY_WRITE)
1964 return -EISDIR;
1965 break;
1966 case S_IFBLK:
1967 case S_IFCHR:
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001968 if (path->mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001970 /*FALLTHRU*/
1971 case S_IFIFO:
1972 case S_IFSOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001974 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08001975 }
Dave Hansenb41572e2007-10-16 23:31:14 -07001976
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001977 error = inode_permission(inode, acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07001978 if (error)
1979 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05001980
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 /*
1982 * An append-only file must be opened in append mode for writing.
1983 */
1984 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05001985 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05001986 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05001988 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 }
1990
1991 /* O_NOATIME can only be set by the owner or superuser */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001992 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
Al Viro7715b522009-12-16 03:54:00 -05001993 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
1995 /*
1996 * Ensure there are no outstanding leases on the file.
1997 */
Al Virob65a9cf2009-12-16 06:27:40 -05001998 return break_lease(inode, flag);
Al Viro7715b522009-12-16 03:54:00 -05001999}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
Jeff Laytone1181ee2010-12-07 16:19:50 -05002001static int handle_truncate(struct file *filp)
Al Viro7715b522009-12-16 03:54:00 -05002002{
Jeff Laytone1181ee2010-12-07 16:19:50 -05002003 struct path *path = &filp->f_path;
Al Viro7715b522009-12-16 03:54:00 -05002004 struct inode *inode = path->dentry->d_inode;
2005 int error = get_write_access(inode);
2006 if (error)
2007 return error;
2008 /*
2009 * Refuse to truncate files with mandatory locks held on them.
2010 */
2011 error = locks_verify_locked(inode);
2012 if (!error)
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +09002013 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05002014 if (!error) {
2015 error = do_truncate(path->dentry, 0,
2016 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
Jeff Laytone1181ee2010-12-07 16:19:50 -05002017 filp);
Al Viro7715b522009-12-16 03:54:00 -05002018 }
2019 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04002020 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021}
2022
Dave Hansend57999e2008-02-15 14:37:27 -08002023/*
Dave Hansend57999e2008-02-15 14:37:27 -08002024 * Note that while the flag value (low two bits) for sys_open means:
2025 * 00 - read-only
2026 * 01 - write-only
2027 * 10 - read-write
2028 * 11 - special
2029 * it is changed into
2030 * 00 - no permissions needed
2031 * 01 - read-permission
2032 * 10 - write-permission
2033 * 11 - read-write
2034 * for the internal routines (ie open_namei()/follow_link() etc)
2035 * This is more logical, and also allows the 00 "no perm needed"
2036 * to be used for symlinks (where the permissions are checked
2037 * later).
2038 *
2039*/
2040static inline int open_to_namei_flags(int flag)
2041{
2042 if ((flag+1) & O_ACCMODE)
2043 flag++;
2044 return flag;
2045}
2046
Nick Piggin31e6b012011-01-07 17:49:52 +11002047/*
Al Virofe2d35f2011-03-05 22:58:25 -05002048 * Handle the last step of open()
Nick Piggin31e6b012011-01-07 17:49:52 +11002049 */
Al Virofb1cc552009-12-24 01:58:28 -05002050static struct file *do_last(struct nameidata *nd, struct path *path,
Al Viroc3e380b2011-02-23 13:39:45 -05002051 const struct open_flags *op, const char *pathname)
Al Virofb1cc552009-12-24 01:58:28 -05002052{
Al Viroa1e28032009-12-24 02:12:06 -05002053 struct dentry *dir = nd->path.dentry;
Al Viro6c0d46c2011-03-09 00:59:59 -05002054 struct dentry *dentry;
Al Viroca344a892011-03-09 00:36:45 -05002055 int open_flag = op->open_flag;
Al Viro6c0d46c2011-03-09 00:59:59 -05002056 int will_truncate = open_flag & O_TRUNC;
Al Viroca344a892011-03-09 00:36:45 -05002057 int want_write = 0;
Al Virobcda7652011-03-13 16:42:14 -04002058 int acc_mode = op->acc_mode;
Al Virofb1cc552009-12-24 01:58:28 -05002059 struct file *filp;
Al Viro16c2cd72011-02-22 15:50:10 -05002060 int error;
Al Virofb1cc552009-12-24 01:58:28 -05002061
Al Viroc3e380b2011-02-23 13:39:45 -05002062 nd->flags &= ~LOOKUP_PARENT;
2063 nd->flags |= op->intent;
2064
Al Viro1f36f772009-12-26 10:56:19 -05002065 switch (nd->last_type) {
2066 case LAST_DOTDOT:
Neil Brown176306f2010-05-24 16:57:56 +10002067 case LAST_DOT:
Al Virofe2d35f2011-03-05 22:58:25 -05002068 error = handle_dots(nd, nd->last_type);
2069 if (error)
2070 return ERR_PTR(error);
Al Viro1f36f772009-12-26 10:56:19 -05002071 /* fallthrough */
Al Viro1f36f772009-12-26 10:56:19 -05002072 case LAST_ROOT:
Al Viro9f1fafe2011-03-25 11:00:12 -04002073 error = complete_walk(nd);
Al Viro16c2cd72011-02-22 15:50:10 -05002074 if (error)
Al Viro9f1fafe2011-03-25 11:00:12 -04002075 return ERR_PTR(error);
Al Virofe2d35f2011-03-05 22:58:25 -05002076 audit_inode(pathname, nd->path.dentry);
Al Viroca344a892011-03-09 00:36:45 -05002077 if (open_flag & O_CREAT) {
Al Virofe2d35f2011-03-05 22:58:25 -05002078 error = -EISDIR;
2079 goto exit;
2080 }
2081 goto ok;
Al Viro1f36f772009-12-26 10:56:19 -05002082 case LAST_BIND:
Al Viro9f1fafe2011-03-25 11:00:12 -04002083 error = complete_walk(nd);
Al Viro16c2cd72011-02-22 15:50:10 -05002084 if (error)
Al Viro9f1fafe2011-03-25 11:00:12 -04002085 return ERR_PTR(error);
Al Viro1f36f772009-12-26 10:56:19 -05002086 audit_inode(pathname, dir);
Al Viro67ee3ad2009-12-26 07:01:01 -05002087 goto ok;
Al Viro1f36f772009-12-26 10:56:19 -05002088 }
Al Viro67ee3ad2009-12-26 07:01:01 -05002089
Al Viroca344a892011-03-09 00:36:45 -05002090 if (!(open_flag & O_CREAT)) {
Al Virobcda7652011-03-13 16:42:14 -04002091 int symlink_ok = 0;
Al Virofe2d35f2011-03-05 22:58:25 -05002092 if (nd->last.name[nd->last.len])
2093 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
Al Virobcda7652011-03-13 16:42:14 -04002094 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2095 symlink_ok = 1;
Al Virofe2d35f2011-03-05 22:58:25 -05002096 /* we _can_ be in RCU mode here */
Al Viroce57dfc2011-03-13 19:58:58 -04002097 error = walk_component(nd, path, &nd->last, LAST_NORM,
2098 !symlink_ok);
2099 if (error < 0)
Al Virofe2d35f2011-03-05 22:58:25 -05002100 return ERR_PTR(error);
Al Viroce57dfc2011-03-13 19:58:58 -04002101 if (error) /* symlink */
Al Virofe2d35f2011-03-05 22:58:25 -05002102 return NULL;
Al Virofe2d35f2011-03-05 22:58:25 -05002103 /* sayonara */
Al Viro9f1fafe2011-03-25 11:00:12 -04002104 error = complete_walk(nd);
2105 if (error)
2106 return ERR_PTR(-ECHILD);
Al Virofe2d35f2011-03-05 22:58:25 -05002107
2108 error = -ENOTDIR;
2109 if (nd->flags & LOOKUP_DIRECTORY) {
Al Viroce57dfc2011-03-13 19:58:58 -04002110 if (!nd->inode->i_op->lookup)
Al Virofe2d35f2011-03-05 22:58:25 -05002111 goto exit;
2112 }
2113 audit_inode(pathname, nd->path.dentry);
2114 goto ok;
2115 }
2116
2117 /* create side of things */
Al Viro9f1fafe2011-03-25 11:00:12 -04002118 error = complete_walk(nd);
2119 if (error)
2120 return ERR_PTR(error);
Al Virofe2d35f2011-03-05 22:58:25 -05002121
2122 audit_inode(pathname, dir);
Al Viro16c2cd72011-02-22 15:50:10 -05002123 error = -EISDIR;
Al Viro1f36f772009-12-26 10:56:19 -05002124 /* trailing slashes? */
Nick Piggin31e6b012011-01-07 17:49:52 +11002125 if (nd->last.name[nd->last.len])
2126 goto exit;
Al Viroa2c36b42009-12-24 03:39:50 -05002127
Al Viroa1e28032009-12-24 02:12:06 -05002128 mutex_lock(&dir->d_inode->i_mutex);
2129
Al Viro6c0d46c2011-03-09 00:59:59 -05002130 dentry = lookup_hash(nd);
2131 error = PTR_ERR(dentry);
2132 if (IS_ERR(dentry)) {
Al Virofb1cc552009-12-24 01:58:28 -05002133 mutex_unlock(&dir->d_inode->i_mutex);
2134 goto exit;
2135 }
2136
Al Viro6c0d46c2011-03-09 00:59:59 -05002137 path->dentry = dentry;
2138 path->mnt = nd->path.mnt;
2139
Al Virofb1cc552009-12-24 01:58:28 -05002140 /* Negative dentry, just create the file */
Al Viro6c0d46c2011-03-09 00:59:59 -05002141 if (!dentry->d_inode) {
2142 int mode = op->mode;
2143 if (!IS_POSIXACL(dir->d_inode))
2144 mode &= ~current_umask();
Al Virofb1cc552009-12-24 01:58:28 -05002145 /*
2146 * This write is needed to ensure that a
Al Viro6c0d46c2011-03-09 00:59:59 -05002147 * rw->ro transition does not occur between
Al Virofb1cc552009-12-24 01:58:28 -05002148 * the time when the file is created and when
2149 * a permanent write count is taken through
2150 * the 'struct file' in nameidata_to_filp().
2151 */
2152 error = mnt_want_write(nd->path.mnt);
2153 if (error)
2154 goto exit_mutex_unlock;
Al Viroca344a892011-03-09 00:36:45 -05002155 want_write = 1;
Al Viro9b44f1b2011-03-09 00:17:27 -05002156 /* Don't check for write permission, don't truncate */
Al Viroca344a892011-03-09 00:36:45 -05002157 open_flag &= ~O_TRUNC;
Al Viro6c0d46c2011-03-09 00:59:59 -05002158 will_truncate = 0;
Al Virobcda7652011-03-13 16:42:14 -04002159 acc_mode = MAY_OPEN;
Al Viro6c0d46c2011-03-09 00:59:59 -05002160 error = security_path_mknod(&nd->path, dentry, mode, 0);
2161 if (error)
2162 goto exit_mutex_unlock;
2163 error = vfs_create(dir->d_inode, dentry, mode, nd);
2164 if (error)
2165 goto exit_mutex_unlock;
2166 mutex_unlock(&dir->d_inode->i_mutex);
2167 dput(nd->path.dentry);
2168 nd->path.dentry = dentry;
Al Viroca344a892011-03-09 00:36:45 -05002169 goto common;
Al Virofb1cc552009-12-24 01:58:28 -05002170 }
2171
2172 /*
2173 * It already exists.
2174 */
2175 mutex_unlock(&dir->d_inode->i_mutex);
2176 audit_inode(pathname, path->dentry);
2177
2178 error = -EEXIST;
Al Viroca344a892011-03-09 00:36:45 -05002179 if (open_flag & O_EXCL)
Al Virofb1cc552009-12-24 01:58:28 -05002180 goto exit_dput;
2181
David Howells9875cf82011-01-14 18:45:21 +00002182 error = follow_managed(path, nd->flags);
2183 if (error < 0)
2184 goto exit_dput;
Al Virofb1cc552009-12-24 01:58:28 -05002185
2186 error = -ENOENT;
2187 if (!path->dentry->d_inode)
2188 goto exit_dput;
Al Viro9e67f362009-12-26 07:04:50 -05002189
2190 if (path->dentry->d_inode->i_op->follow_link)
Al Virofb1cc552009-12-24 01:58:28 -05002191 return NULL;
Al Virofb1cc552009-12-24 01:58:28 -05002192
2193 path_to_nameidata(path, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11002194 nd->inode = path->dentry->d_inode;
Al Virofb1cc552009-12-24 01:58:28 -05002195 error = -EISDIR;
Nick Piggin31e6b012011-01-07 17:49:52 +11002196 if (S_ISDIR(nd->inode->i_mode))
Al Virofb1cc552009-12-24 01:58:28 -05002197 goto exit;
Al Viro67ee3ad2009-12-26 07:01:01 -05002198ok:
Al Viro6c0d46c2011-03-09 00:59:59 -05002199 if (!S_ISREG(nd->inode->i_mode))
2200 will_truncate = 0;
2201
Al Viro0f9d1a12011-03-09 00:13:14 -05002202 if (will_truncate) {
2203 error = mnt_want_write(nd->path.mnt);
2204 if (error)
2205 goto exit;
Al Viroca344a892011-03-09 00:36:45 -05002206 want_write = 1;
Al Viro0f9d1a12011-03-09 00:13:14 -05002207 }
Al Viroca344a892011-03-09 00:36:45 -05002208common:
Al Virobcda7652011-03-13 16:42:14 -04002209 error = may_open(&nd->path, acc_mode, open_flag);
Al Viroca344a892011-03-09 00:36:45 -05002210 if (error)
Al Viro0f9d1a12011-03-09 00:13:14 -05002211 goto exit;
Al Viro0f9d1a12011-03-09 00:13:14 -05002212 filp = nameidata_to_filp(nd);
2213 if (!IS_ERR(filp)) {
2214 error = ima_file_check(filp, op->acc_mode);
2215 if (error) {
2216 fput(filp);
2217 filp = ERR_PTR(error);
2218 }
2219 }
2220 if (!IS_ERR(filp)) {
2221 if (will_truncate) {
2222 error = handle_truncate(filp);
2223 if (error) {
2224 fput(filp);
2225 filp = ERR_PTR(error);
2226 }
2227 }
2228 }
Al Viroca344a892011-03-09 00:36:45 -05002229out:
2230 if (want_write)
Al Viro0f9d1a12011-03-09 00:13:14 -05002231 mnt_drop_write(nd->path.mnt);
2232 path_put(&nd->path);
Al Virofb1cc552009-12-24 01:58:28 -05002233 return filp;
2234
2235exit_mutex_unlock:
2236 mutex_unlock(&dir->d_inode->i_mutex);
2237exit_dput:
2238 path_put_conditional(path, nd);
2239exit:
Al Viroca344a892011-03-09 00:36:45 -05002240 filp = ERR_PTR(error);
2241 goto out;
Al Virofb1cc552009-12-24 01:58:28 -05002242}
2243
Al Viro13aab422011-02-23 17:54:08 -05002244static struct file *path_openat(int dfd, const char *pathname,
Al Viro73d049a2011-03-11 12:08:24 -05002245 struct nameidata *nd, const struct open_flags *op, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246{
Al Virofe2d35f2011-03-05 22:58:25 -05002247 struct file *base = NULL;
Dave Hansen4a3fd212008-02-15 14:37:48 -08002248 struct file *filp;
Al Viro9850c052010-01-13 15:01:15 -05002249 struct path path;
Al Viro13aab422011-02-23 17:54:08 -05002250 int error;
Nick Piggin31e6b012011-01-07 17:49:52 +11002251
2252 filp = get_empty_filp();
2253 if (!filp)
2254 return ERR_PTR(-ENFILE);
2255
Al Viro47c805d2011-02-23 17:44:09 -05002256 filp->f_flags = op->open_flag;
Al Viro73d049a2011-03-11 12:08:24 -05002257 nd->intent.open.file = filp;
2258 nd->intent.open.flags = open_to_namei_flags(op->open_flag);
2259 nd->intent.open.create_mode = op->mode;
Nick Piggin31e6b012011-01-07 17:49:52 +11002260
Al Viro73d049a2011-03-11 12:08:24 -05002261 error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
Nick Piggin31e6b012011-01-07 17:49:52 +11002262 if (unlikely(error))
Al Viro13aab422011-02-23 17:54:08 -05002263 goto out_filp;
Nick Piggin31e6b012011-01-07 17:49:52 +11002264
Al Virofe2d35f2011-03-05 22:58:25 -05002265 current->total_link_count = 0;
Al Viro73d049a2011-03-11 12:08:24 -05002266 error = link_path_walk(pathname, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11002267 if (unlikely(error))
2268 goto out_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
Al Viro73d049a2011-03-11 12:08:24 -05002270 filp = do_last(nd, &path, op, pathname);
Al Viro806b6812009-12-26 07:16:40 -05002271 while (unlikely(!filp)) { /* trailing symlink */
Nick Piggin7b9337a2011-01-14 08:42:43 +00002272 struct path link = path;
Al Virodef4af32009-12-26 08:37:05 -05002273 void *cookie;
Al Viro574197e2011-03-14 22:20:34 -04002274 if (!(nd->flags & LOOKUP_FOLLOW)) {
Al Viro73d049a2011-03-11 12:08:24 -05002275 path_put_conditional(&path, nd);
2276 path_put(&nd->path);
Al Viro40b39132011-03-09 16:22:18 -05002277 filp = ERR_PTR(-ELOOP);
2278 break;
2279 }
Al Viro73d049a2011-03-11 12:08:24 -05002280 nd->flags |= LOOKUP_PARENT;
2281 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
Al Viro574197e2011-03-14 22:20:34 -04002282 error = follow_link(&link, nd, &cookie);
Al Viroc3e380b2011-02-23 13:39:45 -05002283 if (unlikely(error))
Al Virof1afe9e2011-02-22 22:27:28 -05002284 filp = ERR_PTR(error);
Al Viroc3e380b2011-02-23 13:39:45 -05002285 else
Al Viro73d049a2011-03-11 12:08:24 -05002286 filp = do_last(nd, &path, op, pathname);
Al Viro574197e2011-03-14 22:20:34 -04002287 put_link(nd, &link, cookie);
Al Viro806b6812009-12-26 07:16:40 -05002288 }
Al Viro10fa8e62009-12-26 07:09:49 -05002289out:
Al Viro73d049a2011-03-11 12:08:24 -05002290 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
2291 path_put(&nd->root);
Al Virofe2d35f2011-03-05 22:58:25 -05002292 if (base)
2293 fput(base);
Al Viro73d049a2011-03-11 12:08:24 -05002294 release_open_intent(nd);
Al Viro10fa8e62009-12-26 07:09:49 -05002295 return filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
Nick Piggin31e6b012011-01-07 17:49:52 +11002297out_filp:
Al Viro806b6812009-12-26 07:16:40 -05002298 filp = ERR_PTR(error);
Al Viro10fa8e62009-12-26 07:09:49 -05002299 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300}
2301
Al Viro13aab422011-02-23 17:54:08 -05002302struct file *do_filp_open(int dfd, const char *pathname,
2303 const struct open_flags *op, int flags)
2304{
Al Viro73d049a2011-03-11 12:08:24 -05002305 struct nameidata nd;
Al Viro13aab422011-02-23 17:54:08 -05002306 struct file *filp;
2307
Al Viro73d049a2011-03-11 12:08:24 -05002308 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
Al Viro13aab422011-02-23 17:54:08 -05002309 if (unlikely(filp == ERR_PTR(-ECHILD)))
Al Viro73d049a2011-03-11 12:08:24 -05002310 filp = path_openat(dfd, pathname, &nd, op, flags);
Al Viro13aab422011-02-23 17:54:08 -05002311 if (unlikely(filp == ERR_PTR(-ESTALE)))
Al Viro73d049a2011-03-11 12:08:24 -05002312 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
Al Viro13aab422011-02-23 17:54:08 -05002313 return filp;
2314}
2315
Al Viro73d049a2011-03-11 12:08:24 -05002316struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
2317 const char *name, const struct open_flags *op, int flags)
2318{
2319 struct nameidata nd;
2320 struct file *file;
2321
2322 nd.root.mnt = mnt;
2323 nd.root.dentry = dentry;
2324
2325 flags |= LOOKUP_ROOT;
2326
Al Virobcda7652011-03-13 16:42:14 -04002327 if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
Al Viro73d049a2011-03-11 12:08:24 -05002328 return ERR_PTR(-ELOOP);
2329
2330 file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
2331 if (unlikely(file == ERR_PTR(-ECHILD)))
2332 file = path_openat(-1, name, &nd, op, flags);
2333 if (unlikely(file == ERR_PTR(-ESTALE)))
2334 file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
2335 return file;
2336}
2337
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338/**
2339 * lookup_create - lookup a dentry, creating it if it doesn't exist
2340 * @nd: nameidata info
2341 * @is_dir: directory flag
2342 *
2343 * Simple function to lookup and return a dentry and create it
2344 * if it doesn't exist. Is SMP-safe.
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002345 *
Jan Blunck4ac91372008-02-14 19:34:32 -08002346 * Returns with nd->path.dentry->d_inode->i_mutex locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 */
2348struct dentry *lookup_create(struct nameidata *nd, int is_dir)
2349{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002350 struct dentry *dentry = ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
Jan Blunck4ac91372008-02-14 19:34:32 -08002352 mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002353 /*
2354 * Yucky last component or no last component at all?
2355 * (foo/., foo/.., /////)
2356 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 if (nd->last_type != LAST_NORM)
2358 goto fail;
2359 nd->flags &= ~LOOKUP_PARENT;
Al Viro35165862008-08-05 03:00:49 -04002360 nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
ASANO Masahiroa6349042006-08-22 20:06:02 -04002361 nd->intent.open.flags = O_EXCL;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002362
2363 /*
2364 * Do the final lookup.
2365 */
Christoph Hellwig49705b72005-11-08 21:35:06 -08002366 dentry = lookup_hash(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 if (IS_ERR(dentry))
2368 goto fail;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002369
Al Viroe9baf6e2008-05-15 04:49:12 -04002370 if (dentry->d_inode)
2371 goto eexist;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002372 /*
2373 * Special case - lookup gave negative, but... we had foo/bar/
2374 * From the vfs_mknod() POV we just have a negative dentry -
2375 * all is fine. Let's be bastards - you had / on the end, you've
2376 * been asking for (non-existent) directory. -ENOENT for you.
2377 */
Al Viroe9baf6e2008-05-15 04:49:12 -04002378 if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
2379 dput(dentry);
2380 dentry = ERR_PTR(-ENOENT);
2381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 return dentry;
Al Viroe9baf6e2008-05-15 04:49:12 -04002383eexist:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 dput(dentry);
Al Viroe9baf6e2008-05-15 04:49:12 -04002385 dentry = ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386fail:
2387 return dentry;
2388}
Christoph Hellwigf81a0bf2005-05-19 12:26:43 -07002389EXPORT_SYMBOL_GPL(lookup_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
2391int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
2392{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002393 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
2395 if (error)
2396 return error;
2397
Serge E. Hallyne795b712011-03-23 16:43:25 -07002398 if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
2399 !ns_capable(inode_userns(dir), CAP_MKNOD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 return -EPERM;
2401
Al Viroacfa4382008-12-04 10:06:33 -05002402 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 return -EPERM;
2404
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07002405 error = devcgroup_inode_mknod(mode, dev);
2406 if (error)
2407 return error;
2408
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 error = security_inode_mknod(dir, dentry, mode, dev);
2410 if (error)
2411 return error;
2412
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002414 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002415 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 return error;
2417}
2418
Dave Hansen463c3192008-02-15 14:37:57 -08002419static int may_mknod(mode_t mode)
2420{
2421 switch (mode & S_IFMT) {
2422 case S_IFREG:
2423 case S_IFCHR:
2424 case S_IFBLK:
2425 case S_IFIFO:
2426 case S_IFSOCK:
2427 case 0: /* zero mode translates to S_IFREG */
2428 return 0;
2429 case S_IFDIR:
2430 return -EPERM;
2431 default:
2432 return -EINVAL;
2433 }
2434}
2435
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002436SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
2437 unsigned, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438{
Al Viro2ad94ae2008-07-21 09:32:51 -04002439 int error;
2440 char *tmp;
2441 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 struct nameidata nd;
2443
2444 if (S_ISDIR(mode))
2445 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446
Al Viro2ad94ae2008-07-21 09:32:51 -04002447 error = user_path_parent(dfd, filename, &nd, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002449 return error;
2450
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 dentry = lookup_create(&nd, 0);
Dave Hansen463c3192008-02-15 14:37:57 -08002452 if (IS_ERR(dentry)) {
2453 error = PTR_ERR(dentry);
2454 goto out_unlock;
2455 }
Jan Blunck4ac91372008-02-14 19:34:32 -08002456 if (!IS_POSIXACL(nd.path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04002457 mode &= ~current_umask();
Dave Hansen463c3192008-02-15 14:37:57 -08002458 error = may_mknod(mode);
2459 if (error)
2460 goto out_dput;
2461 error = mnt_want_write(nd.path.mnt);
2462 if (error)
2463 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002464 error = security_path_mknod(&nd.path, dentry, mode, dev);
2465 if (error)
2466 goto out_drop_write;
Dave Hansen463c3192008-02-15 14:37:57 -08002467 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 case 0: case S_IFREG:
Jan Blunck4ac91372008-02-14 19:34:32 -08002469 error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 break;
2471 case S_IFCHR: case S_IFBLK:
Jan Blunck4ac91372008-02-14 19:34:32 -08002472 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 new_decode_dev(dev));
2474 break;
2475 case S_IFIFO: case S_IFSOCK:
Jan Blunck4ac91372008-02-14 19:34:32 -08002476 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 }
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002479out_drop_write:
Dave Hansen463c3192008-02-15 14:37:57 -08002480 mnt_drop_write(nd.path.mnt);
2481out_dput:
2482 dput(dentry);
2483out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002484 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002485 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 putname(tmp);
2487
2488 return error;
2489}
2490
Heiko Carstens3480b252009-01-14 14:14:16 +01002491SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002492{
2493 return sys_mknodat(AT_FDCWD, filename, mode, dev);
2494}
2495
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2497{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002498 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
2500 if (error)
2501 return error;
2502
Al Viroacfa4382008-12-04 10:06:33 -05002503 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 return -EPERM;
2505
2506 mode &= (S_IRWXUGO|S_ISVTX);
2507 error = security_inode_mkdir(dir, dentry, mode);
2508 if (error)
2509 return error;
2510
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002512 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002513 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 return error;
2515}
2516
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002517SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518{
2519 int error = 0;
2520 char * tmp;
Dave Hansen6902d922006-09-30 23:29:01 -07002521 struct dentry *dentry;
2522 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
Al Viro2ad94ae2008-07-21 09:32:51 -04002524 error = user_path_parent(dfd, pathname, &nd, &tmp);
2525 if (error)
Dave Hansen6902d922006-09-30 23:29:01 -07002526 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
Dave Hansen6902d922006-09-30 23:29:01 -07002528 dentry = lookup_create(&nd, 1);
2529 error = PTR_ERR(dentry);
2530 if (IS_ERR(dentry))
2531 goto out_unlock;
2532
Jan Blunck4ac91372008-02-14 19:34:32 -08002533 if (!IS_POSIXACL(nd.path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04002534 mode &= ~current_umask();
Dave Hansen463c3192008-02-15 14:37:57 -08002535 error = mnt_want_write(nd.path.mnt);
2536 if (error)
2537 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002538 error = security_path_mkdir(&nd.path, dentry, mode);
2539 if (error)
2540 goto out_drop_write;
Jan Blunck4ac91372008-02-14 19:34:32 -08002541 error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002542out_drop_write:
Dave Hansen463c3192008-02-15 14:37:57 -08002543 mnt_drop_write(nd.path.mnt);
2544out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002545 dput(dentry);
2546out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002547 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002548 path_put(&nd.path);
Dave Hansen6902d922006-09-30 23:29:01 -07002549 putname(tmp);
2550out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 return error;
2552}
2553
Heiko Carstens3cdad422009-01-14 14:14:22 +01002554SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002555{
2556 return sys_mkdirat(AT_FDCWD, pathname, mode);
2557}
2558
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559/*
Sage Weila71905f2011-05-24 13:06:08 -07002560 * The dentry_unhash() helper will try to drop the dentry early: we
2561 * should have a usage count of 2 if we're the only user of this
2562 * dentry, and if that is true (possibly after pruning the dcache),
2563 * then we drop the dentry now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 *
2565 * A low-level filesystem can, if it choses, legally
2566 * do a
2567 *
2568 * if (!d_unhashed(dentry))
2569 * return -EBUSY;
2570 *
2571 * if it cannot handle the case of removing a directory
2572 * that is still in use by something else..
2573 */
2574void dentry_unhash(struct dentry *dentry)
2575{
Vasily Averindc168422006-12-06 20:37:07 -08002576 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 spin_lock(&dentry->d_lock);
Sage Weil64252c72011-05-24 13:06:05 -07002578 if (dentry->d_count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 __d_drop(dentry);
2580 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581}
2582
2583int vfs_rmdir(struct inode *dir, struct dentry *dentry)
2584{
2585 int error = may_delete(dir, dentry, 1);
2586
2587 if (error)
2588 return error;
2589
Al Viroacfa4382008-12-04 10:06:33 -05002590 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 return -EPERM;
2592
Al Viro97abc522011-09-14 18:55:41 +01002593 dget(dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002594 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
Sage Weil912dbc12011-05-24 13:06:11 -07002596 error = -EBUSY;
2597 if (d_mountpoint(dentry))
2598 goto out;
2599
2600 error = security_inode_rmdir(dir, dentry);
2601 if (error)
2602 goto out;
2603
Sage Weil3cebde22011-05-29 21:20:59 -07002604 shrink_dcache_parent(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07002605 error = dir->i_op->rmdir(dir, dentry);
2606 if (error)
2607 goto out;
2608
2609 dentry->d_inode->i_flags |= S_DEAD;
2610 dont_mount(dentry);
2611
2612out:
2613 mutex_unlock(&dentry->d_inode->i_mutex);
Al Viro97abc522011-09-14 18:55:41 +01002614 dput(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07002615 if (!error)
2616 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 return error;
2618}
2619
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002620static long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621{
2622 int error = 0;
2623 char * name;
2624 struct dentry *dentry;
2625 struct nameidata nd;
2626
Al Viro2ad94ae2008-07-21 09:32:51 -04002627 error = user_path_parent(dfd, pathname, &nd, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002629 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
2631 switch(nd.last_type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002632 case LAST_DOTDOT:
2633 error = -ENOTEMPTY;
2634 goto exit1;
2635 case LAST_DOT:
2636 error = -EINVAL;
2637 goto exit1;
2638 case LAST_ROOT:
2639 error = -EBUSY;
2640 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002642
2643 nd.flags &= ~LOOKUP_PARENT;
2644
Jan Blunck4ac91372008-02-14 19:34:32 -08002645 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002646 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002648 if (IS_ERR(dentry))
2649 goto exit2;
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04002650 if (!dentry->d_inode) {
2651 error = -ENOENT;
2652 goto exit3;
2653 }
Dave Hansen06227532008-02-15 14:37:34 -08002654 error = mnt_want_write(nd.path.mnt);
2655 if (error)
2656 goto exit3;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002657 error = security_path_rmdir(&nd.path, dentry);
2658 if (error)
2659 goto exit4;
Jan Blunck4ac91372008-02-14 19:34:32 -08002660 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002661exit4:
Dave Hansen06227532008-02-15 14:37:34 -08002662 mnt_drop_write(nd.path.mnt);
2663exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07002664 dput(dentry);
2665exit2:
Jan Blunck4ac91372008-02-14 19:34:32 -08002666 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002668 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 putname(name);
2670 return error;
2671}
2672
Heiko Carstens3cdad422009-01-14 14:14:22 +01002673SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002674{
2675 return do_rmdir(AT_FDCWD, pathname);
2676}
2677
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678int vfs_unlink(struct inode *dir, struct dentry *dentry)
2679{
2680 int error = may_delete(dir, dentry, 0);
2681
2682 if (error)
2683 return error;
2684
Al Viroacfa4382008-12-04 10:06:33 -05002685 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 return -EPERM;
2687
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002688 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 if (d_mountpoint(dentry))
2690 error = -EBUSY;
2691 else {
2692 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05002693 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 error = dir->i_op->unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05002695 if (!error)
Al Virod83c49f2010-04-30 17:17:09 -04002696 dont_mount(dentry);
Al Virobec10522010-03-03 14:12:08 -05002697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002699 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700
2701 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2702 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
Jan Karaece95912008-02-06 01:37:13 -08002703 fsnotify_link_count(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 }
Robert Love0eeca282005-07-12 17:06:03 -04002706
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 return error;
2708}
2709
2710/*
2711 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002712 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 * writeout happening, and we don't want to prevent access to the directory
2714 * while waiting on the I/O.
2715 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002716static long do_unlinkat(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717{
Al Viro2ad94ae2008-07-21 09:32:51 -04002718 int error;
2719 char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 struct dentry *dentry;
2721 struct nameidata nd;
2722 struct inode *inode = NULL;
2723
Al Viro2ad94ae2008-07-21 09:32:51 -04002724 error = user_path_parent(dfd, pathname, &nd, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002726 return error;
2727
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 error = -EISDIR;
2729 if (nd.last_type != LAST_NORM)
2730 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002731
2732 nd.flags &= ~LOOKUP_PARENT;
2733
Jan Blunck4ac91372008-02-14 19:34:32 -08002734 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002735 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 error = PTR_ERR(dentry);
2737 if (!IS_ERR(dentry)) {
2738 /* Why not before? Because we want correct error value */
Török Edwin50338b82011-06-16 00:06:14 +03002739 if (nd.last.name[nd.last.len])
2740 goto slashes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 inode = dentry->d_inode;
Török Edwin50338b82011-06-16 00:06:14 +03002742 if (!inode)
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04002743 goto slashes;
2744 ihold(inode);
Dave Hansen06227532008-02-15 14:37:34 -08002745 error = mnt_want_write(nd.path.mnt);
2746 if (error)
2747 goto exit2;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002748 error = security_path_unlink(&nd.path, dentry);
2749 if (error)
2750 goto exit3;
Jan Blunck4ac91372008-02-14 19:34:32 -08002751 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002752exit3:
Dave Hansen06227532008-02-15 14:37:34 -08002753 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 exit2:
2755 dput(dentry);
2756 }
Jan Blunck4ac91372008-02-14 19:34:32 -08002757 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 if (inode)
2759 iput(inode); /* truncate the inode here */
2760exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002761 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 putname(name);
2763 return error;
2764
2765slashes:
2766 error = !dentry->d_inode ? -ENOENT :
2767 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2768 goto exit2;
2769}
2770
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002771SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002772{
2773 if ((flag & ~AT_REMOVEDIR) != 0)
2774 return -EINVAL;
2775
2776 if (flag & AT_REMOVEDIR)
2777 return do_rmdir(dfd, pathname);
2778
2779 return do_unlinkat(dfd, pathname);
2780}
2781
Heiko Carstens3480b252009-01-14 14:14:16 +01002782SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002783{
2784 return do_unlinkat(AT_FDCWD, pathname);
2785}
2786
Miklos Szeredidb2e7472008-06-24 16:50:16 +02002787int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002789 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790
2791 if (error)
2792 return error;
2793
Al Viroacfa4382008-12-04 10:06:33 -05002794 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 return -EPERM;
2796
2797 error = security_inode_symlink(dir, dentry, oldname);
2798 if (error)
2799 return error;
2800
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002802 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002803 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 return error;
2805}
2806
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002807SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
2808 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809{
Al Viro2ad94ae2008-07-21 09:32:51 -04002810 int error;
2811 char *from;
2812 char *to;
Dave Hansen6902d922006-09-30 23:29:01 -07002813 struct dentry *dentry;
2814 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815
2816 from = getname(oldname);
Al Viro2ad94ae2008-07-21 09:32:51 -04002817 if (IS_ERR(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 return PTR_ERR(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04002819
2820 error = user_path_parent(newdfd, newname, &nd, &to);
2821 if (error)
Dave Hansen6902d922006-09-30 23:29:01 -07002822 goto out_putname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
Dave Hansen6902d922006-09-30 23:29:01 -07002824 dentry = lookup_create(&nd, 0);
2825 error = PTR_ERR(dentry);
2826 if (IS_ERR(dentry))
2827 goto out_unlock;
2828
Dave Hansen75c3f292008-02-15 14:37:45 -08002829 error = mnt_want_write(nd.path.mnt);
2830 if (error)
2831 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002832 error = security_path_symlink(&nd.path, dentry, from);
2833 if (error)
2834 goto out_drop_write;
Miklos Szeredidb2e7472008-06-24 16:50:16 +02002835 error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002836out_drop_write:
Dave Hansen75c3f292008-02-15 14:37:45 -08002837 mnt_drop_write(nd.path.mnt);
2838out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002839 dput(dentry);
2840out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002841 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002842 path_put(&nd.path);
Dave Hansen6902d922006-09-30 23:29:01 -07002843 putname(to);
2844out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 putname(from);
2846 return error;
2847}
2848
Heiko Carstens3480b252009-01-14 14:14:16 +01002849SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002850{
2851 return sys_symlinkat(oldname, AT_FDCWD, newname);
2852}
2853
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2855{
2856 struct inode *inode = old_dentry->d_inode;
2857 int error;
2858
2859 if (!inode)
2860 return -ENOENT;
2861
Miklos Szeredia95164d2008-07-30 15:08:48 +02002862 error = may_create(dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 if (error)
2864 return error;
2865
2866 if (dir->i_sb != inode->i_sb)
2867 return -EXDEV;
2868
2869 /*
2870 * A link to an append-only or immutable file cannot be created.
2871 */
2872 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2873 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05002874 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002876 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 return -EPERM;
2878
2879 error = security_inode_link(old_dentry, dir, new_dentry);
2880 if (error)
2881 return error;
2882
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002883 mutex_lock(&inode->i_mutex);
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05302884 /* Make sure we don't allow creating hardlink to an unlinked file */
2885 if (inode->i_nlink == 0)
2886 error = -ENOENT;
2887 else
2888 error = dir->i_op->link(old_dentry, dir, new_dentry);
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002889 mutex_unlock(&inode->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07002890 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002891 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 return error;
2893}
2894
2895/*
2896 * Hardlinks are often used in delicate situations. We avoid
2897 * security-related surprises by not following symlinks on the
2898 * newname. --KAB
2899 *
2900 * We don't follow them on the oldname either to be compatible
2901 * with linux 2.0, and to avoid hard-linking to directories
2902 * and other special files. --ADM
2903 */
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002904SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
2905 int, newdfd, const char __user *, newname, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906{
2907 struct dentry *new_dentry;
Al Viro2d8f3032008-07-22 09:59:21 -04002908 struct nameidata nd;
2909 struct path old_path;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05302910 int how = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 int error;
Al Viro2ad94ae2008-07-21 09:32:51 -04002912 char *to;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05302914 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002915 return -EINVAL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05302916 /*
2917 * To use null names we require CAP_DAC_READ_SEARCH
2918 * This ensures that not everyone will be able to create
2919 * handlink using the passed filedescriptor.
2920 */
2921 if (flags & AT_EMPTY_PATH) {
2922 if (!capable(CAP_DAC_READ_SEARCH))
2923 return -ENOENT;
2924 how = LOOKUP_EMPTY;
2925 }
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002926
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05302927 if (flags & AT_SYMLINK_FOLLOW)
2928 how |= LOOKUP_FOLLOW;
2929
2930 error = user_path_at(olddfd, oldname, how, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002932 return error;
2933
2934 error = user_path_parent(newdfd, newname, &nd, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 if (error)
2936 goto out;
2937 error = -EXDEV;
Al Viro2d8f3032008-07-22 09:59:21 -04002938 if (old_path.mnt != nd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 goto out_release;
2940 new_dentry = lookup_create(&nd, 0);
2941 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002942 if (IS_ERR(new_dentry))
2943 goto out_unlock;
Dave Hansen75c3f292008-02-15 14:37:45 -08002944 error = mnt_want_write(nd.path.mnt);
2945 if (error)
2946 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002947 error = security_path_link(old_path.dentry, &nd.path, new_dentry);
2948 if (error)
2949 goto out_drop_write;
Al Viro2d8f3032008-07-22 09:59:21 -04002950 error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002951out_drop_write:
Dave Hansen75c3f292008-02-15 14:37:45 -08002952 mnt_drop_write(nd.path.mnt);
2953out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002954 dput(new_dentry);
2955out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002956 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957out_release:
Jan Blunck1d957f92008-02-14 19:34:35 -08002958 path_put(&nd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04002959 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960out:
Al Viro2d8f3032008-07-22 09:59:21 -04002961 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962
2963 return error;
2964}
2965
Heiko Carstens3480b252009-01-14 14:14:16 +01002966SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002967{
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002968 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002969}
2970
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971/*
2972 * The worst of all namespace operations - renaming directory. "Perverted"
2973 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2974 * Problems:
2975 * a) we can get into loop creation. Check is done in is_subdir().
2976 * b) race potential - two innocent renames can create a loop together.
2977 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002978 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 * story.
2980 * c) we have to lock _three_ objects - parents and victim (if it exists).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002981 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 * whether the target exists). Solution: try to be smart with locking
2983 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002984 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 * move will be locked. Thus we can rank directories by the tree
2986 * (ancestors first) and rank all non-directories after them.
2987 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002988 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 * HOWEVER, it relies on the assumption that any object with ->lookup()
2990 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2991 * we'd better make sure that there's no link(2) for them.
Sage Weile4eaac02011-05-24 13:06:07 -07002992 * d) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002993 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05002995 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 * locking].
2997 */
Adrian Bunk75c96f82005-05-05 16:16:09 -07002998static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
2999 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000{
3001 int error = 0;
Sage Weil9055cba2011-05-24 13:06:12 -07003002 struct inode *target = new_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
3004 /*
3005 * If we are going to change the parent - check write permissions,
3006 * we'll need to flip '..'.
3007 */
3008 if (new_dir != old_dir) {
Al Virof419a2e2008-07-22 00:07:17 -04003009 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 if (error)
3011 return error;
3012 }
3013
3014 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3015 if (error)
3016 return error;
3017
Al Viro97abc522011-09-14 18:55:41 +01003018 dget(new_dentry);
Al Virod83c49f2010-04-30 17:17:09 -04003019 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003020 mutex_lock(&target->i_mutex);
Sage Weil9055cba2011-05-24 13:06:12 -07003021
3022 error = -EBUSY;
3023 if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
3024 goto out;
3025
Sage Weil3cebde22011-05-29 21:20:59 -07003026 if (target)
3027 shrink_dcache_parent(new_dentry);
Sage Weil9055cba2011-05-24 13:06:12 -07003028 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3029 if (error)
3030 goto out;
3031
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 if (target) {
Sage Weil9055cba2011-05-24 13:06:12 -07003033 target->i_flags |= S_DEAD;
3034 dont_mount(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 }
Sage Weil9055cba2011-05-24 13:06:12 -07003036out:
3037 if (target)
3038 mutex_unlock(&target->i_mutex);
Al Viro97abc522011-09-14 18:55:41 +01003039 dput(new_dentry);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07003040 if (!error)
Mark Fasheh349457c2006-09-08 14:22:21 -07003041 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3042 d_move(old_dentry,new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 return error;
3044}
3045
Adrian Bunk75c96f82005-05-05 16:16:09 -07003046static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
3047 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048{
Sage Weil51892bb2011-05-24 13:06:13 -07003049 struct inode *target = new_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 int error;
3051
3052 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3053 if (error)
3054 return error;
3055
3056 dget(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003058 mutex_lock(&target->i_mutex);
Sage Weil51892bb2011-05-24 13:06:13 -07003059
3060 error = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
Sage Weil51892bb2011-05-24 13:06:13 -07003062 goto out;
3063
3064 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3065 if (error)
3066 goto out;
3067
3068 if (target)
3069 dont_mount(new_dentry);
3070 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3071 d_move(old_dentry, new_dentry);
3072out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003074 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 dput(new_dentry);
3076 return error;
3077}
3078
3079int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
3080 struct inode *new_dir, struct dentry *new_dentry)
3081{
3082 int error;
3083 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
Eric Paris59b0df22010-02-08 12:53:52 -05003084 const unsigned char *old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085
3086 if (old_dentry->d_inode == new_dentry->d_inode)
3087 return 0;
3088
3089 error = may_delete(old_dir, old_dentry, is_dir);
3090 if (error)
3091 return error;
3092
3093 if (!new_dentry->d_inode)
Miklos Szeredia95164d2008-07-30 15:08:48 +02003094 error = may_create(new_dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 else
3096 error = may_delete(new_dir, new_dentry, is_dir);
3097 if (error)
3098 return error;
3099
Al Viroacfa4382008-12-04 10:06:33 -05003100 if (!old_dir->i_op->rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 return -EPERM;
3102
Robert Love0eeca282005-07-12 17:06:03 -04003103 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
3104
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 if (is_dir)
3106 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
3107 else
3108 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
Al Viro123df292009-12-25 04:57:57 -05003109 if (!error)
3110 fsnotify_move(old_dir, new_dir, old_name, is_dir,
Al Viro5a190ae2007-06-07 12:19:32 -04003111 new_dentry->d_inode, old_dentry);
Robert Love0eeca282005-07-12 17:06:03 -04003112 fsnotify_oldname_free(old_name);
3113
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 return error;
3115}
3116
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003117SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
3118 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119{
Al Viro2ad94ae2008-07-21 09:32:51 -04003120 struct dentry *old_dir, *new_dir;
3121 struct dentry *old_dentry, *new_dentry;
3122 struct dentry *trap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 struct nameidata oldnd, newnd;
Al Viro2ad94ae2008-07-21 09:32:51 -04003124 char *from;
3125 char *to;
3126 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127
Al Viro2ad94ae2008-07-21 09:32:51 -04003128 error = user_path_parent(olddfd, oldname, &oldnd, &from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 if (error)
3130 goto exit;
3131
Al Viro2ad94ae2008-07-21 09:32:51 -04003132 error = user_path_parent(newdfd, newname, &newnd, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 if (error)
3134 goto exit1;
3135
3136 error = -EXDEV;
Jan Blunck4ac91372008-02-14 19:34:32 -08003137 if (oldnd.path.mnt != newnd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 goto exit2;
3139
Jan Blunck4ac91372008-02-14 19:34:32 -08003140 old_dir = oldnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 error = -EBUSY;
3142 if (oldnd.last_type != LAST_NORM)
3143 goto exit2;
3144
Jan Blunck4ac91372008-02-14 19:34:32 -08003145 new_dir = newnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 if (newnd.last_type != LAST_NORM)
3147 goto exit2;
3148
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003149 oldnd.flags &= ~LOOKUP_PARENT;
3150 newnd.flags &= ~LOOKUP_PARENT;
OGAWA Hirofumi4e9ed2f2008-10-16 07:50:29 +09003151 newnd.flags |= LOOKUP_RENAME_TARGET;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003152
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 trap = lock_rename(new_dir, old_dir);
3154
Christoph Hellwig49705b72005-11-08 21:35:06 -08003155 old_dentry = lookup_hash(&oldnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 error = PTR_ERR(old_dentry);
3157 if (IS_ERR(old_dentry))
3158 goto exit3;
3159 /* source must exist */
3160 error = -ENOENT;
3161 if (!old_dentry->d_inode)
3162 goto exit4;
3163 /* unless the source is a directory trailing slashes give -ENOTDIR */
3164 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
3165 error = -ENOTDIR;
3166 if (oldnd.last.name[oldnd.last.len])
3167 goto exit4;
3168 if (newnd.last.name[newnd.last.len])
3169 goto exit4;
3170 }
3171 /* source should not be ancestor of target */
3172 error = -EINVAL;
3173 if (old_dentry == trap)
3174 goto exit4;
Christoph Hellwig49705b72005-11-08 21:35:06 -08003175 new_dentry = lookup_hash(&newnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 error = PTR_ERR(new_dentry);
3177 if (IS_ERR(new_dentry))
3178 goto exit4;
3179 /* target should not be an ancestor of source */
3180 error = -ENOTEMPTY;
3181 if (new_dentry == trap)
3182 goto exit5;
3183
Dave Hansen9079b1e2008-02-15 14:37:49 -08003184 error = mnt_want_write(oldnd.path.mnt);
3185 if (error)
3186 goto exit5;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003187 error = security_path_rename(&oldnd.path, old_dentry,
3188 &newnd.path, new_dentry);
3189 if (error)
3190 goto exit6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 error = vfs_rename(old_dir->d_inode, old_dentry,
3192 new_dir->d_inode, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003193exit6:
Dave Hansen9079b1e2008-02-15 14:37:49 -08003194 mnt_drop_write(oldnd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195exit5:
3196 dput(new_dentry);
3197exit4:
3198 dput(old_dentry);
3199exit3:
3200 unlock_rename(new_dir, old_dir);
3201exit2:
Jan Blunck1d957f92008-02-14 19:34:35 -08003202 path_put(&newnd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04003203 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003205 path_put(&oldnd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 putname(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04003207exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 return error;
3209}
3210
Heiko Carstensa26eab22009-01-14 14:14:17 +01003211SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003212{
3213 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
3214}
3215
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
3217{
3218 int len;
3219
3220 len = PTR_ERR(link);
3221 if (IS_ERR(link))
3222 goto out;
3223
3224 len = strlen(link);
3225 if (len > (unsigned) buflen)
3226 len = buflen;
3227 if (copy_to_user(buffer, link, len))
3228 len = -EFAULT;
3229out:
3230 return len;
3231}
3232
3233/*
3234 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
3235 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
3236 * using) it for any given inode is up to filesystem.
3237 */
3238int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3239{
3240 struct nameidata nd;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003241 void *cookie;
Marcin Slusarz694a1762008-06-09 16:40:37 -07003242 int res;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003243
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 nd.depth = 0;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003245 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
Marcin Slusarz694a1762008-06-09 16:40:37 -07003246 if (IS_ERR(cookie))
3247 return PTR_ERR(cookie);
3248
3249 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
3250 if (dentry->d_inode->i_op->put_link)
3251 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3252 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253}
3254
3255int vfs_follow_link(struct nameidata *nd, const char *link)
3256{
3257 return __vfs_follow_link(nd, link);
3258}
3259
3260/* get the link contents into pagecache */
3261static char *page_getlink(struct dentry * dentry, struct page **ppage)
3262{
Duane Griffinebd09ab2008-12-19 20:47:12 +00003263 char *kaddr;
3264 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 struct address_space *mapping = dentry->d_inode->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -07003266 page = read_mapping_page(mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 if (IS_ERR(page))
Nick Piggin6fe69002007-05-06 14:49:04 -07003268 return (char*)page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 *ppage = page;
Duane Griffinebd09ab2008-12-19 20:47:12 +00003270 kaddr = kmap(page);
3271 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3272 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273}
3274
3275int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3276{
3277 struct page *page = NULL;
3278 char *s = page_getlink(dentry, &page);
3279 int res = vfs_readlink(dentry,buffer,buflen,s);
3280 if (page) {
3281 kunmap(page);
3282 page_cache_release(page);
3283 }
3284 return res;
3285}
3286
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003287void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003289 struct page *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 nd_set_link(nd, page_getlink(dentry, &page));
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003291 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292}
3293
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003294void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003296 struct page *page = cookie;
3297
3298 if (page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 kunmap(page);
3300 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 }
3302}
3303
Nick Piggin54566b22009-01-04 12:00:53 -08003304/*
3305 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
3306 */
3307int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308{
3309 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003310 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07003311 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08003312 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 char *kaddr;
Nick Piggin54566b22009-01-04 12:00:53 -08003314 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
3315 if (nofs)
3316 flags |= AOP_FLAG_NOFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317
NeilBrown7e53cac2006-03-25 03:07:57 -08003318retry:
Nick Pigginafddba42007-10-16 01:25:01 -07003319 err = pagecache_write_begin(NULL, mapping, 0, len-1,
Nick Piggin54566b22009-01-04 12:00:53 -08003320 flags, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07003322 goto fail;
3323
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 kaddr = kmap_atomic(page, KM_USER0);
3325 memcpy(kaddr, symname, len-1);
3326 kunmap_atomic(kaddr, KM_USER0);
Nick Pigginafddba42007-10-16 01:25:01 -07003327
3328 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3329 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 if (err < 0)
3331 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07003332 if (err < len-1)
3333 goto retry;
3334
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 mark_inode_dirty(inode);
3336 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337fail:
3338 return err;
3339}
3340
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003341int page_symlink(struct inode *inode, const char *symname, int len)
3342{
3343 return __page_symlink(inode, symname, len,
Nick Piggin54566b22009-01-04 12:00:53 -08003344 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003345}
3346
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003347const struct inode_operations page_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 .readlink = generic_readlink,
3349 .follow_link = page_follow_link_light,
3350 .put_link = page_put_link,
3351};
3352
Al Viro2d8f3032008-07-22 09:59:21 -04003353EXPORT_SYMBOL(user_path_at);
David Howellscc53ce52011-01-14 18:45:26 +00003354EXPORT_SYMBOL(follow_down_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355EXPORT_SYMBOL(follow_down);
3356EXPORT_SYMBOL(follow_up);
3357EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
3358EXPORT_SYMBOL(getname);
3359EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360EXPORT_SYMBOL(lookup_one_len);
3361EXPORT_SYMBOL(page_follow_link_light);
3362EXPORT_SYMBOL(page_put_link);
3363EXPORT_SYMBOL(page_readlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003364EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365EXPORT_SYMBOL(page_symlink);
3366EXPORT_SYMBOL(page_symlink_inode_operations);
Al Viroc9c6cac2011-02-16 15:15:47 -05003367EXPORT_SYMBOL(kern_path_parent);
Al Virod1811462008-08-02 00:49:18 -04003368EXPORT_SYMBOL(kern_path);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07003369EXPORT_SYMBOL(vfs_path_lookup);
Al Virof419a2e2008-07-22 00:07:17 -04003370EXPORT_SYMBOL(inode_permission);
Christoph Hellwig8c744fb2005-11-08 21:35:04 -08003371EXPORT_SYMBOL(file_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372EXPORT_SYMBOL(unlock_rename);
3373EXPORT_SYMBOL(vfs_create);
3374EXPORT_SYMBOL(vfs_follow_link);
3375EXPORT_SYMBOL(vfs_link);
3376EXPORT_SYMBOL(vfs_mkdir);
3377EXPORT_SYMBOL(vfs_mknod);
3378EXPORT_SYMBOL(generic_permission);
3379EXPORT_SYMBOL(vfs_readlink);
3380EXPORT_SYMBOL(vfs_rename);
3381EXPORT_SYMBOL(vfs_rmdir);
3382EXPORT_SYMBOL(vfs_symlink);
3383EXPORT_SYMBOL(vfs_unlink);
3384EXPORT_SYMBOL(dentry_unhash);
3385EXPORT_SYMBOL(generic_readlink);