blob: 9e7b18a8be66e4f6c3d4c96b4db8c7500319259a [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
73 * the name is a symlink pointing to a non-existant name.
74 *
75 * I don't know which semantics is the right one, since I have no access
76 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
77 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
78 * "old" one. Personally, I think the new semantics is much more logical.
79 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
80 * file does succeed in both HP-UX and SunOs, but not in Solaris
81 * and in the old Linux semantics.
82 */
83
84/* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
85 * semantics. See the comments in "open_namei" and "do_link" below.
86 *
87 * [10-Sep-98 Alan Modra] Another symlink change.
88 */
89
90/* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
91 * inside the path - always follow.
92 * in the last component in creation/removal/renaming - never follow.
93 * if LOOKUP_FOLLOW passed - follow.
94 * if the pathname has trailing slashes - follow.
95 * otherwise - don't follow.
96 * (applied in that order).
97 *
98 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
99 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
100 * During the 2.4 we need to fix the userland stuff depending on it -
101 * hopefully we will be able to get rid of that wart in 2.5. So far only
102 * XEmacs seems to be relying on it...
103 */
104/*
105 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
Arjan van de Vena11f3a02006-03-23 03:00:33 -0800106 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 * any extra contention...
108 */
109
110/* In order to reduce some races, while at the same time doing additional
111 * checking and hopefully speeding things up, we copy filenames to the
112 * kernel data space before using them..
113 *
114 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
115 * PATH_MAX includes the nul terminator --RR.
116 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800117static int do_getname(const char __user *filename, char *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
119 int retval;
120 unsigned long len = PATH_MAX;
121
122 if (!segment_eq(get_fs(), KERNEL_DS)) {
123 if ((unsigned long) filename >= TASK_SIZE)
124 return -EFAULT;
125 if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
126 len = TASK_SIZE - (unsigned long) filename;
127 }
128
129 retval = strncpy_from_user(page, filename, len);
130 if (retval > 0) {
131 if (retval < len)
132 return 0;
133 return -ENAMETOOLONG;
134 } else if (!retval)
135 retval = -ENOENT;
136 return retval;
137}
138
Al Virof52e0c12011-03-14 18:56:51 -0400139static char *getname_flags(const char __user * filename, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
141 char *tmp, *result;
142
143 result = ERR_PTR(-ENOMEM);
144 tmp = __getname();
145 if (tmp) {
146 int retval = do_getname(filename, tmp);
147
148 result = tmp;
149 if (retval < 0) {
Al Virof52e0c12011-03-14 18:56:51 -0400150 if (retval != -ENOENT || !(flags & LOOKUP_EMPTY)) {
151 __putname(tmp);
152 result = ERR_PTR(retval);
153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 }
155 }
156 audit_getname(result);
157 return result;
158}
159
Al Virof52e0c12011-03-14 18:56:51 -0400160char *getname(const char __user * filename)
161{
162 return getname_flags(filename, 0);
163}
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165#ifdef CONFIG_AUDITSYSCALL
166void putname(const char *name)
167{
Al Viro5ac3a9c2006-07-16 06:38:45 -0400168 if (unlikely(!audit_dummy_context()))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 audit_putname(name);
170 else
171 __putname(name);
172}
173EXPORT_SYMBOL(putname);
174#endif
175
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700176/*
177 * This does basic POSIX ACL permission checking
178 */
Nick Pigginb74c79e2011-01-07 17:49:58 +1100179static int acl_permission_check(struct inode *inode, int mask, unsigned int flags,
180 int (*check_acl)(struct inode *inode, int mask, unsigned int flags))
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700181{
182 umode_t mode = inode->i_mode;
183
184 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
185
186 if (current_fsuid() == inode->i_uid)
187 mode >>= 6;
188 else {
189 if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
Nick Pigginb74c79e2011-01-07 17:49:58 +1100190 int error = check_acl(inode, mask, flags);
191 if (error != -EAGAIN)
192 return error;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700193 }
194
195 if (in_group_p(inode->i_gid))
196 mode >>= 3;
197 }
198
199 /*
200 * If the DACs are ok we don't need any capability check.
201 */
202 if ((mask & ~mode) == 0)
203 return 0;
204 return -EACCES;
205}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207/**
Nick Pigginb74c79e2011-01-07 17:49:58 +1100208 * generic_permission - check for access rights on a Posix-like filesystem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 * @inode: inode to check access rights for
210 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
211 * @check_acl: optional callback to check for Posix ACLs
Randy Dunlap39191622011-01-08 19:36:21 -0800212 * @flags: IPERM_FLAG_ flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 *
214 * Used to check for read/write/execute permissions on a file.
215 * We use "fsuid" for this, letting us set arbitrary permissions
216 * for filesystem access without changing the "normal" uids which
Nick Pigginb74c79e2011-01-07 17:49:58 +1100217 * are used for other things.
218 *
219 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
220 * request cannot be satisfied (eg. requires blocking or too much complexity).
221 * It would then be called again in ref-walk mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 */
Nick Pigginb74c79e2011-01-07 17:49:58 +1100223int generic_permission(struct inode *inode, int mask, unsigned int flags,
224 int (*check_acl)(struct inode *inode, int mask, unsigned int flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700226 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 /*
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700229 * Do the basic POSIX ACL permission checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 */
Nick Pigginb74c79e2011-01-07 17:49:58 +1100231 ret = acl_permission_check(inode, mask, flags, check_acl);
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700232 if (ret != -EACCES)
233 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 /*
236 * Read/write DACs are always overridable.
237 * Executable DACs are overridable if at least one exec bit is set.
238 */
Miklos Szeredif696a362008-07-31 13:41:58 +0200239 if (!(mask & MAY_EXEC) || execute_ok(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 if (capable(CAP_DAC_OVERRIDE))
241 return 0;
242
243 /*
244 * Searching includes executable on directories, else just read.
245 */
Serge E. Hallyn7ea66002009-12-29 14:50:19 -0600246 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
248 if (capable(CAP_DAC_READ_SEARCH))
249 return 0;
250
251 return -EACCES;
252}
253
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200254/**
255 * inode_permission - check for access rights to a given inode
256 * @inode: inode to check permission on
257 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
258 *
259 * Used to check for read/write/execute permissions on an inode.
260 * We use "fsuid" for this, letting us set arbitrary permissions
261 * for filesystem access without changing the "normal" uids which
262 * are used for other things.
263 */
Al Virof419a2e2008-07-22 00:07:17 -0400264int inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Al Viroe6305c42008-07-15 21:03:57 -0400266 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 if (mask & MAY_WRITE) {
Miklos Szeredi22590e42007-10-16 23:27:08 -0700269 umode_t mode = inode->i_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 /*
272 * Nobody gets write access to a read-only fs.
273 */
274 if (IS_RDONLY(inode) &&
275 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
276 return -EROFS;
277
278 /*
279 * Nobody gets write access to an immutable file.
280 */
281 if (IS_IMMUTABLE(inode))
282 return -EACCES;
283 }
284
Al Viroacfa4382008-12-04 10:06:33 -0500285 if (inode->i_op->permission)
Nick Pigginb74c79e2011-01-07 17:49:58 +1100286 retval = inode->i_op->permission(inode, mask, 0);
Miklos Szeredif696a362008-07-31 13:41:58 +0200287 else
Nick Pigginb74c79e2011-01-07 17:49:58 +1100288 retval = generic_permission(inode, mask, 0,
289 inode->i_op->check_acl);
Miklos Szeredif696a362008-07-31 13:41:58 +0200290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 if (retval)
292 return retval;
293
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700294 retval = devcgroup_inode_permission(inode, mask);
295 if (retval)
296 return retval;
297
Eric Parisd09ca732010-07-23 11:43:57 -0400298 return security_inode_permission(inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800301/**
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800302 * file_permission - check for additional access rights to a given file
303 * @file: file to check access rights for
304 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
305 *
306 * Used to check for read/write/execute permissions on an already opened
307 * file.
308 *
309 * Note:
310 * Do not use this function in new code. All access checks should
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200311 * be done using inode_permission().
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800312 */
313int file_permission(struct file *file, int mask)
314{
Al Virof419a2e2008-07-22 00:07:17 -0400315 return inode_permission(file->f_path.dentry->d_inode, mask);
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800316}
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318/*
319 * get_write_access() gets write permission for a file.
320 * put_write_access() releases this write permission.
321 * This is used for regular files.
322 * We cannot support write (and maybe mmap read-write shared) accesses and
323 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
324 * can have the following values:
325 * 0: no writers, no VM_DENYWRITE mappings
326 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
327 * > 0: (i_writecount) users are writing to the file.
328 *
329 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
330 * except for the cases where we don't hold i_writecount yet. Then we need to
331 * use {get,deny}_write_access() - these functions check the sign and refuse
332 * to do the change if sign is wrong. Exclusion between them is provided by
333 * the inode->i_lock spinlock.
334 */
335
336int get_write_access(struct inode * inode)
337{
338 spin_lock(&inode->i_lock);
339 if (atomic_read(&inode->i_writecount) < 0) {
340 spin_unlock(&inode->i_lock);
341 return -ETXTBSY;
342 }
343 atomic_inc(&inode->i_writecount);
344 spin_unlock(&inode->i_lock);
345
346 return 0;
347}
348
349int deny_write_access(struct file * file)
350{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800351 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 spin_lock(&inode->i_lock);
354 if (atomic_read(&inode->i_writecount) > 0) {
355 spin_unlock(&inode->i_lock);
356 return -ETXTBSY;
357 }
358 atomic_dec(&inode->i_writecount);
359 spin_unlock(&inode->i_lock);
360
361 return 0;
362}
363
Jan Blunck1d957f92008-02-14 19:34:35 -0800364/**
Jan Blunck5dd784d2008-02-14 19:34:38 -0800365 * path_get - get a reference to a path
366 * @path: path to get the reference to
367 *
368 * Given a path increment the reference count to the dentry and the vfsmount.
369 */
370void path_get(struct path *path)
371{
372 mntget(path->mnt);
373 dget(path->dentry);
374}
375EXPORT_SYMBOL(path_get);
376
377/**
Jan Blunck1d957f92008-02-14 19:34:35 -0800378 * path_put - put a reference to a path
379 * @path: path to put the reference to
380 *
381 * Given a path decrement the reference count to the dentry and the vfsmount.
382 */
383void path_put(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Jan Blunck1d957f92008-02-14 19:34:35 -0800385 dput(path->dentry);
386 mntput(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
Jan Blunck1d957f92008-02-14 19:34:35 -0800388EXPORT_SYMBOL(path_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Trond Myklebust834f2a42005-10-18 14:20:16 -0700390/**
Nick Piggin31e6b012011-01-07 17:49:52 +1100391 * nameidata_drop_rcu - drop this nameidata out of rcu-walk
392 * @nd: nameidata pathwalk data to drop
Randy Dunlap39191622011-01-08 19:36:21 -0800393 * Returns: 0 on success, -ECHILD on failure
Nick Piggin31e6b012011-01-07 17:49:52 +1100394 *
395 * Path walking has 2 modes, rcu-walk and ref-walk (see
396 * Documentation/filesystems/path-lookup.txt). __drop_rcu* functions attempt
397 * to drop out of rcu-walk mode and take normal reference counts on dentries
398 * and vfsmounts to transition to rcu-walk mode. __drop_rcu* functions take
399 * refcounts at the last known good point before rcu-walk got stuck, so
400 * ref-walk may continue from there. If this is not successful (eg. a seqcount
401 * has changed), then failure is returned and path walk restarts from the
402 * beginning in ref-walk mode.
403 *
404 * nameidata_drop_rcu attempts to drop the current nd->path and nd->root into
405 * ref-walk. Must be called from rcu-walk context.
406 */
407static int nameidata_drop_rcu(struct nameidata *nd)
408{
409 struct fs_struct *fs = current->fs;
410 struct dentry *dentry = nd->path.dentry;
Al Viro5b6ca022011-03-09 23:04:47 -0500411 int want_root = 0;
Nick Piggin31e6b012011-01-07 17:49:52 +1100412
413 BUG_ON(!(nd->flags & LOOKUP_RCU));
Al Viro5b6ca022011-03-09 23:04:47 -0500414 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
415 want_root = 1;
Nick Piggin31e6b012011-01-07 17:49:52 +1100416 spin_lock(&fs->lock);
417 if (nd->root.mnt != fs->root.mnt ||
418 nd->root.dentry != fs->root.dentry)
419 goto err_root;
420 }
421 spin_lock(&dentry->d_lock);
422 if (!__d_rcu_to_refcount(dentry, nd->seq))
423 goto err;
424 BUG_ON(nd->inode != dentry->d_inode);
425 spin_unlock(&dentry->d_lock);
Al Viro5b6ca022011-03-09 23:04:47 -0500426 if (want_root) {
Nick Piggin31e6b012011-01-07 17:49:52 +1100427 path_get(&nd->root);
428 spin_unlock(&fs->lock);
429 }
430 mntget(nd->path.mnt);
431
432 rcu_read_unlock();
433 br_read_unlock(vfsmount_lock);
434 nd->flags &= ~LOOKUP_RCU;
435 return 0;
436err:
437 spin_unlock(&dentry->d_lock);
438err_root:
Al Viro5b6ca022011-03-09 23:04:47 -0500439 if (want_root)
Nick Piggin31e6b012011-01-07 17:49:52 +1100440 spin_unlock(&fs->lock);
441 return -ECHILD;
442}
443
444/* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing. */
445static inline int nameidata_drop_rcu_maybe(struct nameidata *nd)
446{
447 if (nd->flags & LOOKUP_RCU)
448 return nameidata_drop_rcu(nd);
449 return 0;
450}
451
452/**
453 * nameidata_dentry_drop_rcu - drop nameidata and dentry out of rcu-walk
454 * @nd: nameidata pathwalk data to drop
455 * @dentry: dentry to drop
Randy Dunlap39191622011-01-08 19:36:21 -0800456 * Returns: 0 on success, -ECHILD on failure
Nick Piggin31e6b012011-01-07 17:49:52 +1100457 *
458 * nameidata_dentry_drop_rcu attempts to drop the current nd->path and nd->root,
459 * and dentry into ref-walk. @dentry must be a path found by a do_lookup call on
460 * @nd. Must be called from rcu-walk context.
461 */
462static int nameidata_dentry_drop_rcu(struct nameidata *nd, struct dentry *dentry)
463{
464 struct fs_struct *fs = current->fs;
465 struct dentry *parent = nd->path.dentry;
Al Viro5b6ca022011-03-09 23:04:47 -0500466 int want_root = 0;
Nick Piggin31e6b012011-01-07 17:49:52 +1100467
468 BUG_ON(!(nd->flags & LOOKUP_RCU));
Al Viro5b6ca022011-03-09 23:04:47 -0500469 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
470 want_root = 1;
Nick Piggin31e6b012011-01-07 17:49:52 +1100471 spin_lock(&fs->lock);
472 if (nd->root.mnt != fs->root.mnt ||
473 nd->root.dentry != fs->root.dentry)
474 goto err_root;
475 }
476 spin_lock(&parent->d_lock);
477 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
478 if (!__d_rcu_to_refcount(dentry, nd->seq))
479 goto err;
480 /*
481 * If the sequence check on the child dentry passed, then the child has
482 * not been removed from its parent. This means the parent dentry must
483 * be valid and able to take a reference at this point.
484 */
485 BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
486 BUG_ON(!parent->d_count);
487 parent->d_count++;
488 spin_unlock(&dentry->d_lock);
489 spin_unlock(&parent->d_lock);
Al Viro5b6ca022011-03-09 23:04:47 -0500490 if (want_root) {
Nick Piggin31e6b012011-01-07 17:49:52 +1100491 path_get(&nd->root);
492 spin_unlock(&fs->lock);
493 }
494 mntget(nd->path.mnt);
495
496 rcu_read_unlock();
497 br_read_unlock(vfsmount_lock);
498 nd->flags &= ~LOOKUP_RCU;
499 return 0;
500err:
501 spin_unlock(&dentry->d_lock);
502 spin_unlock(&parent->d_lock);
503err_root:
Al Viro5b6ca022011-03-09 23:04:47 -0500504 if (want_root)
Nick Piggin31e6b012011-01-07 17:49:52 +1100505 spin_unlock(&fs->lock);
506 return -ECHILD;
507}
508
509/* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing. */
510static inline int nameidata_dentry_drop_rcu_maybe(struct nameidata *nd, struct dentry *dentry)
511{
Al Viroa7472ba2011-03-04 14:39:30 -0500512 if (nd->flags & LOOKUP_RCU) {
513 if (unlikely(nameidata_dentry_drop_rcu(nd, dentry))) {
514 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -0500515 if (!(nd->flags & LOOKUP_ROOT))
516 nd->root.mnt = NULL;
Al Viroa7472ba2011-03-04 14:39:30 -0500517 rcu_read_unlock();
518 br_read_unlock(vfsmount_lock);
519 return -ECHILD;
520 }
521 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100522 return 0;
523}
524
525/**
526 * nameidata_drop_rcu_last - drop nameidata ending path walk out of rcu-walk
527 * @nd: nameidata pathwalk data to drop
Randy Dunlap39191622011-01-08 19:36:21 -0800528 * Returns: 0 on success, -ECHILD on failure
Nick Piggin31e6b012011-01-07 17:49:52 +1100529 *
530 * nameidata_drop_rcu_last attempts to drop the current nd->path into ref-walk.
531 * nd->path should be the final element of the lookup, so nd->root is discarded.
532 * Must be called from rcu-walk context.
533 */
534static int nameidata_drop_rcu_last(struct nameidata *nd)
535{
536 struct dentry *dentry = nd->path.dentry;
537
538 BUG_ON(!(nd->flags & LOOKUP_RCU));
539 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -0500540 if (!(nd->flags & LOOKUP_ROOT))
541 nd->root.mnt = NULL;
Nick Piggin31e6b012011-01-07 17:49:52 +1100542 spin_lock(&dentry->d_lock);
543 if (!__d_rcu_to_refcount(dentry, nd->seq))
544 goto err_unlock;
545 BUG_ON(nd->inode != dentry->d_inode);
546 spin_unlock(&dentry->d_lock);
547
548 mntget(nd->path.mnt);
549
550 rcu_read_unlock();
551 br_read_unlock(vfsmount_lock);
552
553 return 0;
554
555err_unlock:
556 spin_unlock(&dentry->d_lock);
557 rcu_read_unlock();
558 br_read_unlock(vfsmount_lock);
559 return -ECHILD;
560}
561
Nick Piggin31e6b012011-01-07 17:49:52 +1100562/**
Trond Myklebust834f2a42005-10-18 14:20:16 -0700563 * release_open_intent - free up open intent resources
564 * @nd: pointer to nameidata
565 */
566void release_open_intent(struct nameidata *nd)
567{
Linus Torvalds2dab5972011-02-11 15:53:38 -0800568 struct file *file = nd->intent.open.file;
569
570 if (file && !IS_ERR(file)) {
571 if (file->f_path.dentry == NULL)
572 put_filp(file);
573 else
574 fput(file);
575 }
Trond Myklebust834f2a42005-10-18 14:20:16 -0700576}
577
Al Virof60aef72011-02-15 01:35:28 -0500578static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
Nick Piggin34286d62011-01-07 17:49:57 +1100579{
Al Virof60aef72011-02-15 01:35:28 -0500580 return dentry->d_op->d_revalidate(dentry, nd);
Nick Piggin34286d62011-01-07 17:49:57 +1100581}
582
Al Virof5e1c1c2011-02-15 01:32:55 -0500583static struct dentry *
Ian Kentbcdc5e02006-09-27 01:50:44 -0700584do_revalidate(struct dentry *dentry, struct nameidata *nd)
585{
Al Virof5e1c1c2011-02-15 01:32:55 -0500586 int status = d_revalidate(dentry, nd);
Ian Kentbcdc5e02006-09-27 01:50:44 -0700587 if (unlikely(status <= 0)) {
588 /*
589 * The dentry failed validation.
590 * If d_revalidate returned 0 attempt to invalidate
591 * the dentry otherwise d_revalidate is asking us
592 * to return a fail status.
593 */
Nick Piggin34286d62011-01-07 17:49:57 +1100594 if (status < 0) {
Al Virof5e1c1c2011-02-15 01:32:55 -0500595 dput(dentry);
Nick Piggin34286d62011-01-07 17:49:57 +1100596 dentry = ERR_PTR(status);
Al Virof5e1c1c2011-02-15 01:32:55 -0500597 } else if (!d_invalidate(dentry)) {
598 dput(dentry);
599 dentry = NULL;
Ian Kentbcdc5e02006-09-27 01:50:44 -0700600 }
601 }
602 return dentry;
603}
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605/*
Al Viro16c2cd72011-02-22 15:50:10 -0500606 * handle_reval_path - force revalidation of a dentry
Jeff Layton39159de2009-12-07 12:01:50 -0500607 *
608 * In some situations the path walking code will trust dentries without
609 * revalidating them. This causes problems for filesystems that depend on
610 * d_revalidate to handle file opens (e.g. NFSv4). When FS_REVAL_DOT is set
611 * (which indicates that it's possible for the dentry to go stale), force
612 * a d_revalidate call before proceeding.
613 *
614 * Returns 0 if the revalidation was successful. If the revalidation fails,
615 * either return the error returned by d_revalidate or -ESTALE if the
616 * revalidation it just returned 0. If d_revalidate returns 0, we attempt to
617 * invalidate the dentry. It's up to the caller to handle putting references
618 * to the path if necessary.
619 */
Al Viro16c2cd72011-02-22 15:50:10 -0500620static inline int handle_reval_path(struct nameidata *nd)
Jeff Layton39159de2009-12-07 12:01:50 -0500621{
Al Viro16c2cd72011-02-22 15:50:10 -0500622 struct dentry *dentry = nd->path.dentry;
Jeff Layton39159de2009-12-07 12:01:50 -0500623 int status;
Jeff Layton39159de2009-12-07 12:01:50 -0500624
Al Viro16c2cd72011-02-22 15:50:10 -0500625 if (likely(!(nd->flags & LOOKUP_JUMPED)))
Jeff Layton39159de2009-12-07 12:01:50 -0500626 return 0;
627
Al Viro16c2cd72011-02-22 15:50:10 -0500628 if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
629 return 0;
630
631 if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
632 return 0;
633
634 /* Note: we do not d_invalidate() */
Nick Piggin34286d62011-01-07 17:49:57 +1100635 status = d_revalidate(dentry, nd);
Jeff Layton39159de2009-12-07 12:01:50 -0500636 if (status > 0)
637 return 0;
638
Al Viro16c2cd72011-02-22 15:50:10 -0500639 if (!status)
Jeff Layton39159de2009-12-07 12:01:50 -0500640 status = -ESTALE;
Al Viro16c2cd72011-02-22 15:50:10 -0500641
Jeff Layton39159de2009-12-07 12:01:50 -0500642 return status;
643}
644
645/*
Al Virob75b5082009-12-16 01:01:38 -0500646 * Short-cut version of permission(), for calling on directories
647 * during pathname resolution. Combines parts of permission()
648 * and generic_permission(), and tests ONLY for MAY_EXEC permission.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 *
650 * If appropriate, check DAC only. If not appropriate, or
Al Virob75b5082009-12-16 01:01:38 -0500651 * short-cut DAC fails, then call ->permission() to do more
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 * complete permission check.
653 */
Nick Pigginb74c79e2011-01-07 17:49:58 +1100654static inline int exec_permission(struct inode *inode, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700656 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Linus Torvaldscb9179e2009-08-28 11:08:31 -0700658 if (inode->i_op->permission) {
Nick Pigginb74c79e2011-01-07 17:49:58 +1100659 ret = inode->i_op->permission(inode, MAY_EXEC, flags);
660 } else {
661 ret = acl_permission_check(inode, MAY_EXEC, flags,
662 inode->i_op->check_acl);
Linus Torvaldscb9179e2009-08-28 11:08:31 -0700663 }
Nick Pigginb74c79e2011-01-07 17:49:58 +1100664 if (likely(!ret))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 goto ok;
Nick Pigginb74c79e2011-01-07 17:49:58 +1100666 if (ret == -ECHILD)
Nick Piggin31e6b012011-01-07 17:49:52 +1100667 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Linus Torvaldsf1ac9f62009-08-28 10:53:56 -0700669 if (capable(CAP_DAC_OVERRIDE) || capable(CAP_DAC_READ_SEARCH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 goto ok;
671
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700672 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673ok:
Nick Pigginb74c79e2011-01-07 17:49:58 +1100674 return security_inode_exec_permission(inode, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
676
Al Viro2a737872009-04-07 11:49:53 -0400677static __always_inline void set_root(struct nameidata *nd)
678{
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200679 if (!nd->root.mnt)
680 get_fs_root(current->fs, &nd->root);
Al Viro2a737872009-04-07 11:49:53 -0400681}
682
Al Viro6de88d72009-08-09 01:41:57 +0400683static int link_path_walk(const char *, struct nameidata *);
684
Nick Piggin31e6b012011-01-07 17:49:52 +1100685static __always_inline void set_root_rcu(struct nameidata *nd)
686{
687 if (!nd->root.mnt) {
688 struct fs_struct *fs = current->fs;
Nick Pigginc28cc362011-01-07 17:49:53 +1100689 unsigned seq;
690
691 do {
692 seq = read_seqcount_begin(&fs->seq);
693 nd->root = fs->root;
694 } while (read_seqcount_retry(&fs->seq, seq));
Nick Piggin31e6b012011-01-07 17:49:52 +1100695 }
696}
697
Arjan van de Venf1662352006-01-14 13:21:31 -0800698static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
Nick Piggin31e6b012011-01-07 17:49:52 +1100700 int ret;
701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 if (IS_ERR(link))
703 goto fail;
704
705 if (*link == '/') {
Al Viro2a737872009-04-07 11:49:53 -0400706 set_root(nd);
Jan Blunck1d957f92008-02-14 19:34:35 -0800707 path_put(&nd->path);
Al Viro2a737872009-04-07 11:49:53 -0400708 nd->path = nd->root;
709 path_get(&nd->root);
Al Viro16c2cd72011-02-22 15:50:10 -0500710 nd->flags |= LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100712 nd->inode = nd->path.dentry->d_inode;
Christoph Hellwigb4091d52008-11-05 15:07:21 +0100713
Nick Piggin31e6b012011-01-07 17:49:52 +1100714 ret = link_path_walk(link, nd);
715 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716fail:
Jan Blunck1d957f92008-02-14 19:34:35 -0800717 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return PTR_ERR(link);
719}
720
Jan Blunck1d957f92008-02-14 19:34:35 -0800721static void path_put_conditional(struct path *path, struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700722{
723 dput(path->dentry);
Jan Blunck4ac91372008-02-14 19:34:32 -0800724 if (path->mnt != nd->path.mnt)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700725 mntput(path->mnt);
726}
727
Nick Piggin7b9337a2011-01-14 08:42:43 +0000728static inline void path_to_nameidata(const struct path *path,
729 struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700730{
Nick Piggin31e6b012011-01-07 17:49:52 +1100731 if (!(nd->flags & LOOKUP_RCU)) {
732 dput(nd->path.dentry);
733 if (nd->path.mnt != path->mnt)
734 mntput(nd->path.mnt);
Huang Shijie9a229682010-04-02 17:37:13 +0800735 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100736 nd->path.mnt = path->mnt;
Jan Blunck4ac91372008-02-14 19:34:32 -0800737 nd->path.dentry = path->dentry;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700738}
739
Al Virodef4af32009-12-26 08:37:05 -0500740static __always_inline int
Nick Piggin7b9337a2011-01-14 08:42:43 +0000741__do_follow_link(const struct path *link, struct nameidata *nd, void **p)
Ian Kent051d3812006-03-27 01:14:53 -0800742{
743 int error;
Nick Piggin7b9337a2011-01-14 08:42:43 +0000744 struct dentry *dentry = link->dentry;
Ian Kent051d3812006-03-27 01:14:53 -0800745
Al Viro844a3912011-02-15 00:38:26 -0500746 BUG_ON(nd->flags & LOOKUP_RCU);
747
Nick Piggin7b9337a2011-01-14 08:42:43 +0000748 touch_atime(link->mnt, dentry);
Ian Kent051d3812006-03-27 01:14:53 -0800749 nd_set_link(nd, NULL);
750
David Howells87556ef2011-01-14 18:46:46 +0000751 if (link->mnt == nd->path.mnt)
752 mntget(link->mnt);
Nick Piggin31e6b012011-01-07 17:49:52 +1100753
Al Viro36f3b4f2011-02-22 21:24:38 -0500754 error = security_inode_follow_link(link->dentry, nd);
755 if (error) {
756 *p = ERR_PTR(error); /* no ->put_link(), please */
757 path_put(&nd->path);
758 return error;
759 }
760
Al Viro86acdca12009-12-22 23:45:11 -0500761 nd->last_type = LAST_BIND;
Al Virodef4af32009-12-26 08:37:05 -0500762 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
763 error = PTR_ERR(*p);
764 if (!IS_ERR(*p)) {
Ian Kent051d3812006-03-27 01:14:53 -0800765 char *s = nd_get_link(nd);
766 error = 0;
767 if (s)
768 error = __vfs_follow_link(nd, s);
Al Virobcda7652011-03-13 16:42:14 -0400769 else if (nd->last_type == LAST_BIND) {
Al Viro16c2cd72011-02-22 15:50:10 -0500770 nd->flags |= LOOKUP_JUMPED;
Al Virob21041d2011-03-14 20:01:51 -0400771 nd->inode = nd->path.dentry->d_inode;
772 if (nd->inode->i_op->follow_link) {
Al Virobcda7652011-03-13 16:42:14 -0400773 /* stepped on a _really_ weird one */
774 path_put(&nd->path);
775 error = -ELOOP;
776 }
777 }
Ian Kent051d3812006-03-27 01:14:53 -0800778 }
Ian Kent051d3812006-03-27 01:14:53 -0800779 return error;
780}
781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782/*
783 * This limits recursive symlink follows to 8, while
784 * limiting consecutive symlinks to 40.
785 *
786 * Without that kind of total limit, nasty chains of consecutive
787 * symlinks can cause almost arbitrarily long lookups.
788 */
Al Viroce57dfc2011-03-13 19:58:58 -0400789static inline int do_follow_link(struct path *path, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
Al Virodef4af32009-12-26 08:37:05 -0500791 void *cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 int err = -ELOOP;
Al Viro844a3912011-02-15 00:38:26 -0500793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (current->link_count >= MAX_NESTED_LINKS)
795 goto loop;
796 if (current->total_link_count >= 40)
797 goto loop;
798 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
799 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 current->link_count++;
801 current->total_link_count++;
802 nd->depth++;
Al Virodef4af32009-12-26 08:37:05 -0500803 err = __do_follow_link(path, nd, &cookie);
804 if (!IS_ERR(cookie) && path->dentry->d_inode->i_op->put_link)
805 path->dentry->d_inode->i_op->put_link(path->dentry, nd, cookie);
Al Viro258fa992009-08-09 01:32:02 +0400806 path_put(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 current->link_count--;
808 nd->depth--;
809 return err;
810loop:
Jan Blunck1d957f92008-02-14 19:34:35 -0800811 path_put_conditional(path, nd);
812 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 return err;
814}
815
Nick Piggin31e6b012011-01-07 17:49:52 +1100816static int follow_up_rcu(struct path *path)
817{
818 struct vfsmount *parent;
819 struct dentry *mountpoint;
820
821 parent = path->mnt->mnt_parent;
822 if (parent == path->mnt)
823 return 0;
824 mountpoint = path->mnt->mnt_mountpoint;
825 path->dentry = mountpoint;
826 path->mnt = parent;
827 return 1;
828}
829
Al Virobab77eb2009-04-18 03:26:48 -0400830int follow_up(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
832 struct vfsmount *parent;
833 struct dentry *mountpoint;
Nick Piggin99b7db72010-08-18 04:37:39 +1000834
835 br_read_lock(vfsmount_lock);
Al Virobab77eb2009-04-18 03:26:48 -0400836 parent = path->mnt->mnt_parent;
837 if (parent == path->mnt) {
Nick Piggin99b7db72010-08-18 04:37:39 +1000838 br_read_unlock(vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 return 0;
840 }
841 mntget(parent);
Al Virobab77eb2009-04-18 03:26:48 -0400842 mountpoint = dget(path->mnt->mnt_mountpoint);
Nick Piggin99b7db72010-08-18 04:37:39 +1000843 br_read_unlock(vfsmount_lock);
Al Virobab77eb2009-04-18 03:26:48 -0400844 dput(path->dentry);
845 path->dentry = mountpoint;
846 mntput(path->mnt);
847 path->mnt = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return 1;
849}
850
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100851/*
David Howells9875cf82011-01-14 18:45:21 +0000852 * Perform an automount
853 * - return -EISDIR to tell follow_managed() to stop and return the path we
854 * were called with.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 */
David Howells9875cf82011-01-14 18:45:21 +0000856static int follow_automount(struct path *path, unsigned flags,
857 bool *need_mntput)
Nick Piggin31e6b012011-01-07 17:49:52 +1100858{
David Howells9875cf82011-01-14 18:45:21 +0000859 struct vfsmount *mnt;
David Howellsea5b7782011-01-14 19:10:03 +0000860 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +1100861
David Howells9875cf82011-01-14 18:45:21 +0000862 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
863 return -EREMOTE;
Al Viro463ffb22005-06-06 13:36:05 -0700864
David Howells6f45b652011-01-14 18:45:31 +0000865 /* We don't want to mount if someone supplied AT_NO_AUTOMOUNT
866 * and this is the terminal part of the path.
867 */
868 if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_CONTINUE))
869 return -EISDIR; /* we actually want to stop here */
870
David Howells9875cf82011-01-14 18:45:21 +0000871 /* We want to mount if someone is trying to open/create a file of any
872 * type under the mountpoint, wants to traverse through the mountpoint
873 * or wants to open the mounted directory.
874 *
875 * We don't want to mount if someone's just doing a stat and they've
876 * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and
877 * appended a '/' to the name.
878 */
879 if (!(flags & LOOKUP_FOLLOW) &&
880 !(flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY |
881 LOOKUP_OPEN | LOOKUP_CREATE)))
882 return -EISDIR;
883
884 current->total_link_count++;
885 if (current->total_link_count >= 40)
886 return -ELOOP;
887
888 mnt = path->dentry->d_op->d_automount(path);
889 if (IS_ERR(mnt)) {
890 /*
891 * The filesystem is allowed to return -EISDIR here to indicate
892 * it doesn't want to automount. For instance, autofs would do
893 * this so that its userspace daemon can mount on this dentry.
894 *
895 * However, we can only permit this if it's a terminal point in
896 * the path being looked up; if it wasn't then the remainder of
897 * the path is inaccessible and we should say so.
898 */
899 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_CONTINUE))
900 return -EREMOTE;
901 return PTR_ERR(mnt);
902 }
David Howellsea5b7782011-01-14 19:10:03 +0000903
David Howells9875cf82011-01-14 18:45:21 +0000904 if (!mnt) /* mount collision */
905 return 0;
906
Al Viro19a167a2011-01-17 01:35:23 -0500907 err = finish_automount(mnt, path);
David Howellsea5b7782011-01-14 19:10:03 +0000908
David Howellsea5b7782011-01-14 19:10:03 +0000909 switch (err) {
910 case -EBUSY:
911 /* Someone else made a mount here whilst we were busy */
Al Viro19a167a2011-01-17 01:35:23 -0500912 return 0;
David Howellsea5b7782011-01-14 19:10:03 +0000913 case 0:
David Howellsea5b7782011-01-14 19:10:03 +0000914 dput(path->dentry);
915 if (*need_mntput)
916 mntput(path->mnt);
917 path->mnt = mnt;
918 path->dentry = dget(mnt->mnt_root);
919 *need_mntput = true;
920 return 0;
Al Viro19a167a2011-01-17 01:35:23 -0500921 default:
922 return err;
David Howellsea5b7782011-01-14 19:10:03 +0000923 }
Al Viro19a167a2011-01-17 01:35:23 -0500924
David Howells9875cf82011-01-14 18:45:21 +0000925}
926
927/*
928 * Handle a dentry that is managed in some way.
David Howellscc53ce52011-01-14 18:45:26 +0000929 * - Flagged for transit management (autofs)
David Howells9875cf82011-01-14 18:45:21 +0000930 * - Flagged as mountpoint
931 * - Flagged as automount point
932 *
933 * This may only be called in refwalk mode.
934 *
935 * Serialization is taken care of in namespace.c
936 */
937static int follow_managed(struct path *path, unsigned flags)
938{
939 unsigned managed;
940 bool need_mntput = false;
941 int ret;
942
943 /* Given that we're not holding a lock here, we retain the value in a
944 * local variable for each dentry as we look at it so that we don't see
945 * the components of that value change under us */
946 while (managed = ACCESS_ONCE(path->dentry->d_flags),
947 managed &= DCACHE_MANAGED_DENTRY,
948 unlikely(managed != 0)) {
David Howellscc53ce52011-01-14 18:45:26 +0000949 /* Allow the filesystem to manage the transit without i_mutex
950 * being held. */
951 if (managed & DCACHE_MANAGE_TRANSIT) {
952 BUG_ON(!path->dentry->d_op);
953 BUG_ON(!path->dentry->d_op->d_manage);
David Howellsab909112011-01-14 18:46:51 +0000954 ret = path->dentry->d_op->d_manage(path->dentry,
955 false, false);
David Howellscc53ce52011-01-14 18:45:26 +0000956 if (ret < 0)
957 return ret == -EISDIR ? 0 : ret;
958 }
959
David Howells9875cf82011-01-14 18:45:21 +0000960 /* Transit to a mounted filesystem. */
961 if (managed & DCACHE_MOUNTED) {
962 struct vfsmount *mounted = lookup_mnt(path);
963 if (mounted) {
964 dput(path->dentry);
965 if (need_mntput)
966 mntput(path->mnt);
967 path->mnt = mounted;
968 path->dentry = dget(mounted->mnt_root);
969 need_mntput = true;
970 continue;
971 }
972
973 /* Something is mounted on this dentry in another
974 * namespace and/or whatever was mounted there in this
975 * namespace got unmounted before we managed to get the
976 * vfsmount_lock */
977 }
978
979 /* Handle an automount point */
980 if (managed & DCACHE_NEED_AUTOMOUNT) {
981 ret = follow_automount(path, flags, &need_mntput);
982 if (ret < 0)
983 return ret == -EISDIR ? 0 : ret;
984 continue;
985 }
986
987 /* We didn't change the current path point */
988 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 }
David Howells9875cf82011-01-14 18:45:21 +0000990 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991}
992
David Howellscc53ce52011-01-14 18:45:26 +0000993int follow_down_one(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
995 struct vfsmount *mounted;
996
Al Viro1c755af2009-04-18 14:06:57 -0400997 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -0400999 dput(path->dentry);
1000 mntput(path->mnt);
1001 path->mnt = mounted;
1002 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 return 1;
1004 }
1005 return 0;
1006}
1007
David Howells9875cf82011-01-14 18:45:21 +00001008/*
1009 * Skip to top of mountpoint pile in rcuwalk mode. We abort the rcu-walk if we
David Howellscc53ce52011-01-14 18:45:26 +00001010 * meet a managed dentry and we're not walking to "..". True is returned to
David Howells9875cf82011-01-14 18:45:21 +00001011 * continue, false to abort.
1012 */
1013static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
1014 struct inode **inode, bool reverse_transit)
1015{
1016 while (d_mountpoint(path->dentry)) {
1017 struct vfsmount *mounted;
David Howellsab909112011-01-14 18:46:51 +00001018 if (unlikely(path->dentry->d_flags & DCACHE_MANAGE_TRANSIT) &&
1019 !reverse_transit &&
1020 path->dentry->d_op->d_manage(path->dentry, false, true) < 0)
1021 return false;
David Howells9875cf82011-01-14 18:45:21 +00001022 mounted = __lookup_mnt(path->mnt, path->dentry, 1);
1023 if (!mounted)
1024 break;
1025 path->mnt = mounted;
1026 path->dentry = mounted->mnt_root;
1027 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
1028 *inode = path->dentry->d_inode;
1029 }
1030
1031 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1032 return reverse_transit;
1033 return true;
1034}
1035
Nick Piggin31e6b012011-01-07 17:49:52 +11001036static int follow_dotdot_rcu(struct nameidata *nd)
1037{
1038 struct inode *inode = nd->inode;
1039
1040 set_root_rcu(nd);
1041
David Howells9875cf82011-01-14 18:45:21 +00001042 while (1) {
Nick Piggin31e6b012011-01-07 17:49:52 +11001043 if (nd->path.dentry == nd->root.dentry &&
1044 nd->path.mnt == nd->root.mnt) {
1045 break;
1046 }
1047 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1048 struct dentry *old = nd->path.dentry;
1049 struct dentry *parent = old->d_parent;
1050 unsigned seq;
1051
1052 seq = read_seqcount_begin(&parent->d_seq);
1053 if (read_seqcount_retry(&old->d_seq, nd->seq))
Al Viroef7562d2011-03-04 14:35:59 -05001054 goto failed;
Nick Piggin31e6b012011-01-07 17:49:52 +11001055 inode = parent->d_inode;
1056 nd->path.dentry = parent;
1057 nd->seq = seq;
1058 break;
1059 }
1060 if (!follow_up_rcu(&nd->path))
1061 break;
1062 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
1063 inode = nd->path.dentry->d_inode;
1064 }
David Howells9875cf82011-01-14 18:45:21 +00001065 __follow_mount_rcu(nd, &nd->path, &inode, true);
Nick Piggin31e6b012011-01-07 17:49:52 +11001066 nd->inode = inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001067 return 0;
Al Viroef7562d2011-03-04 14:35:59 -05001068
1069failed:
1070 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001071 if (!(nd->flags & LOOKUP_ROOT))
1072 nd->root.mnt = NULL;
Al Viroef7562d2011-03-04 14:35:59 -05001073 rcu_read_unlock();
1074 br_read_unlock(vfsmount_lock);
1075 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001076}
1077
David Howells9875cf82011-01-14 18:45:21 +00001078/*
David Howellscc53ce52011-01-14 18:45:26 +00001079 * Follow down to the covering mount currently visible to userspace. At each
1080 * point, the filesystem owning that dentry may be queried as to whether the
1081 * caller is permitted to proceed or not.
1082 *
1083 * Care must be taken as namespace_sem may be held (indicated by mounting_here
1084 * being true).
1085 */
1086int follow_down(struct path *path, bool mounting_here)
1087{
1088 unsigned managed;
1089 int ret;
1090
1091 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1092 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1093 /* Allow the filesystem to manage the transit without i_mutex
1094 * being held.
1095 *
1096 * We indicate to the filesystem if someone is trying to mount
1097 * something here. This gives autofs the chance to deny anyone
1098 * other than its daemon the right to mount on its
1099 * superstructure.
1100 *
1101 * The filesystem may sleep at this point.
1102 */
1103 if (managed & DCACHE_MANAGE_TRANSIT) {
1104 BUG_ON(!path->dentry->d_op);
1105 BUG_ON(!path->dentry->d_op->d_manage);
David Howellsab909112011-01-14 18:46:51 +00001106 ret = path->dentry->d_op->d_manage(
1107 path->dentry, mounting_here, false);
David Howellscc53ce52011-01-14 18:45:26 +00001108 if (ret < 0)
1109 return ret == -EISDIR ? 0 : ret;
1110 }
1111
1112 /* Transit to a mounted filesystem. */
1113 if (managed & DCACHE_MOUNTED) {
1114 struct vfsmount *mounted = lookup_mnt(path);
1115 if (!mounted)
1116 break;
1117 dput(path->dentry);
1118 mntput(path->mnt);
1119 path->mnt = mounted;
1120 path->dentry = dget(mounted->mnt_root);
1121 continue;
1122 }
1123
1124 /* Don't handle automount points here */
1125 break;
1126 }
1127 return 0;
1128}
1129
1130/*
David Howells9875cf82011-01-14 18:45:21 +00001131 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1132 */
1133static void follow_mount(struct path *path)
1134{
1135 while (d_mountpoint(path->dentry)) {
1136 struct vfsmount *mounted = lookup_mnt(path);
1137 if (!mounted)
1138 break;
1139 dput(path->dentry);
1140 mntput(path->mnt);
1141 path->mnt = mounted;
1142 path->dentry = dget(mounted->mnt_root);
1143 }
1144}
1145
Nick Piggin31e6b012011-01-07 17:49:52 +11001146static void follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
Al Viro2a737872009-04-07 11:49:53 -04001148 set_root(nd);
Andreas Mohre518ddb2006-09-29 02:01:22 -07001149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 while(1) {
Jan Blunck4ac91372008-02-14 19:34:32 -08001151 struct dentry *old = nd->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Al Viro2a737872009-04-07 11:49:53 -04001153 if (nd->path.dentry == nd->root.dentry &&
1154 nd->path.mnt == nd->root.mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 break;
1156 }
Jan Blunck4ac91372008-02-14 19:34:32 -08001157 if (nd->path.dentry != nd->path.mnt->mnt_root) {
Al Viro3088dd72010-01-30 15:47:29 -05001158 /* rare case of legitimate dget_parent()... */
1159 nd->path.dentry = dget_parent(nd->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 dput(old);
1161 break;
1162 }
Al Viro3088dd72010-01-30 15:47:29 -05001163 if (!follow_up(&nd->path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 }
Al Viro79ed0222009-04-18 13:59:41 -04001166 follow_mount(&nd->path);
Nick Piggin31e6b012011-01-07 17:49:52 +11001167 nd->inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168}
1169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170/*
Nick Pigginbaa03892010-08-18 04:37:31 +10001171 * Allocate a dentry with name and parent, and perform a parent
1172 * directory ->lookup on it. Returns the new dentry, or ERR_PTR
1173 * on error. parent->d_inode->i_mutex must be held. d_lookup must
1174 * have verified that no child exists while under i_mutex.
1175 */
1176static struct dentry *d_alloc_and_lookup(struct dentry *parent,
1177 struct qstr *name, struct nameidata *nd)
1178{
1179 struct inode *inode = parent->d_inode;
1180 struct dentry *dentry;
1181 struct dentry *old;
1182
1183 /* Don't create child dentry for a dead directory. */
1184 if (unlikely(IS_DEADDIR(inode)))
1185 return ERR_PTR(-ENOENT);
1186
1187 dentry = d_alloc(parent, name);
1188 if (unlikely(!dentry))
1189 return ERR_PTR(-ENOMEM);
1190
1191 old = inode->i_op->lookup(inode, dentry, nd);
1192 if (unlikely(old)) {
1193 dput(dentry);
1194 dentry = old;
1195 }
1196 return dentry;
1197}
1198
1199/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 * It's more convoluted than I'd like it to be, but... it's still fairly
1201 * small and for now I'd prefer to have fast path as straight as possible.
1202 * It _is_ time-critical.
1203 */
1204static int do_lookup(struct nameidata *nd, struct qstr *name,
Nick Piggin31e6b012011-01-07 17:49:52 +11001205 struct path *path, struct inode **inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
Jan Blunck4ac91372008-02-14 19:34:32 -08001207 struct vfsmount *mnt = nd->path.mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +11001208 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro5a18fff2011-03-11 04:44:53 -05001209 int need_reval = 1;
1210 int status = 1;
David Howells9875cf82011-01-14 18:45:21 +00001211 int err;
1212
Al Viro3cac2602009-08-13 18:27:43 +04001213 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001214 * Rename seqlock is not required here because in the off chance
1215 * of a false negative due to a concurrent rename, we're going to
1216 * do the non-racy lookup, below.
1217 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001218 if (nd->flags & LOOKUP_RCU) {
1219 unsigned seq;
Nick Piggin31e6b012011-01-07 17:49:52 +11001220 *inode = nd->inode;
1221 dentry = __d_lookup_rcu(parent, name, &seq, inode);
Al Viro5a18fff2011-03-11 04:44:53 -05001222 if (!dentry)
1223 goto unlazy;
1224
Nick Piggin31e6b012011-01-07 17:49:52 +11001225 /* Memory barrier in read_seqcount_begin of child is enough */
1226 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1227 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001228 nd->seq = seq;
Al Viro5a18fff2011-03-11 04:44:53 -05001229
Al Viro24643082011-02-15 01:26:22 -05001230 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
Al Viro5a18fff2011-03-11 04:44:53 -05001231 status = d_revalidate(dentry, nd);
1232 if (unlikely(status <= 0)) {
1233 if (status != -ECHILD)
1234 need_reval = 0;
1235 goto unlazy;
1236 }
Al Viro24643082011-02-15 01:26:22 -05001237 }
Nick Piggin31e6b012011-01-07 17:49:52 +11001238 path->mnt = mnt;
1239 path->dentry = dentry;
David Howells9875cf82011-01-14 18:45:21 +00001240 if (likely(__follow_mount_rcu(nd, path, inode, false)))
1241 return 0;
Al Viro5a18fff2011-03-11 04:44:53 -05001242unlazy:
1243 if (dentry) {
1244 if (nameidata_dentry_drop_rcu(nd, dentry))
1245 return -ECHILD;
1246 } else {
1247 if (nameidata_drop_rcu(nd))
1248 return -ECHILD;
1249 }
1250 } else {
1251 dentry = __d_lookup(parent, name);
Nick Piggin31e6b012011-01-07 17:49:52 +11001252 }
Al Viro5a18fff2011-03-11 04:44:53 -05001253
1254retry:
1255 if (unlikely(!dentry)) {
1256 struct inode *dir = parent->d_inode;
1257 BUG_ON(nd->inode != dir);
1258
1259 mutex_lock(&dir->i_mutex);
1260 dentry = d_lookup(parent, name);
1261 if (likely(!dentry)) {
1262 dentry = d_alloc_and_lookup(parent, name, nd);
1263 if (IS_ERR(dentry)) {
1264 mutex_unlock(&dir->i_mutex);
1265 return PTR_ERR(dentry);
1266 }
1267 /* known good */
1268 need_reval = 0;
1269 status = 1;
1270 }
1271 mutex_unlock(&dir->i_mutex);
Al Viro24643082011-02-15 01:26:22 -05001272 }
Al Viro5a18fff2011-03-11 04:44:53 -05001273 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
1274 status = d_revalidate(dentry, nd);
1275 if (unlikely(status <= 0)) {
1276 if (status < 0) {
1277 dput(dentry);
1278 return status;
1279 }
1280 if (!d_invalidate(dentry)) {
1281 dput(dentry);
1282 dentry = NULL;
1283 need_reval = 1;
1284 goto retry;
1285 }
1286 }
1287
David Howells9875cf82011-01-14 18:45:21 +00001288 path->mnt = mnt;
1289 path->dentry = dentry;
1290 err = follow_managed(path, nd->flags);
Ian Kent89312212011-01-18 12:06:10 +08001291 if (unlikely(err < 0)) {
1292 path_put_conditional(path, nd);
David Howells9875cf82011-01-14 18:45:21 +00001293 return err;
Ian Kent89312212011-01-18 12:06:10 +08001294 }
David Howells9875cf82011-01-14 18:45:21 +00001295 *inode = path->dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297}
1298
Al Viro52094c82011-02-21 21:34:47 -05001299static inline int may_lookup(struct nameidata *nd)
1300{
1301 if (nd->flags & LOOKUP_RCU) {
1302 int err = exec_permission(nd->inode, IPERM_FLAG_RCU);
1303 if (err != -ECHILD)
1304 return err;
1305 if (nameidata_drop_rcu(nd))
1306 return -ECHILD;
1307 }
1308 return exec_permission(nd->inode, 0);
1309}
1310
Al Viro9856fa12011-03-04 14:22:06 -05001311static inline int handle_dots(struct nameidata *nd, int type)
1312{
1313 if (type == LAST_DOTDOT) {
1314 if (nd->flags & LOOKUP_RCU) {
1315 if (follow_dotdot_rcu(nd))
1316 return -ECHILD;
1317 } else
1318 follow_dotdot(nd);
1319 }
1320 return 0;
1321}
1322
Al Viro951361f2011-03-04 14:44:37 -05001323static void terminate_walk(struct nameidata *nd)
1324{
1325 if (!(nd->flags & LOOKUP_RCU)) {
1326 path_put(&nd->path);
1327 } else {
1328 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001329 if (!(nd->flags & LOOKUP_ROOT))
1330 nd->root.mnt = NULL;
Al Viro951361f2011-03-04 14:44:37 -05001331 rcu_read_unlock();
1332 br_read_unlock(vfsmount_lock);
1333 }
1334}
1335
Al Viroce57dfc2011-03-13 19:58:58 -04001336static inline int walk_component(struct nameidata *nd, struct path *path,
1337 struct qstr *name, int type, int follow)
1338{
1339 struct inode *inode;
1340 int err;
1341 /*
1342 * "." and ".." are special - ".." especially so because it has
1343 * to be able to know about the current root directory and
1344 * parent relationships.
1345 */
1346 if (unlikely(type != LAST_NORM))
1347 return handle_dots(nd, type);
1348 err = do_lookup(nd, name, path, &inode);
1349 if (unlikely(err)) {
1350 terminate_walk(nd);
1351 return err;
1352 }
1353 if (!inode) {
1354 path_to_nameidata(path, nd);
1355 terminate_walk(nd);
1356 return -ENOENT;
1357 }
1358 if (unlikely(inode->i_op->follow_link) && follow) {
1359 if (nameidata_dentry_drop_rcu_maybe(nd, path->dentry))
1360 return -ECHILD;
1361 BUG_ON(inode != path->dentry->d_inode);
1362 return 1;
1363 }
1364 path_to_nameidata(path, nd);
1365 nd->inode = inode;
1366 return 0;
1367}
1368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369/*
1370 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01001371 * This is the basic name resolution function, turning a pathname into
1372 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01001374 * Returns 0 and nd will have valid dentry and mnt on success.
1375 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 */
Al Viro6de88d72009-08-09 01:41:57 +04001377static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
1379 struct path next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 int err;
1381 unsigned int lookup_flags = nd->flags;
1382
1383 while (*name=='/')
1384 name++;
1385 if (!*name)
Al Viro086e1832011-02-22 20:56:27 -05001386 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 if (nd->depth)
Trond Myklebustf55eab82006-02-04 23:28:01 -08001389 lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391 /* At this point we know we have a real path component. */
1392 for(;;) {
1393 unsigned long hash;
1394 struct qstr this;
1395 unsigned int c;
Al Virofe479a52011-02-22 15:10:03 -05001396 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Trond Myklebustcdce5d62005-10-18 14:20:18 -07001398 nd->flags |= LOOKUP_CONTINUE;
Al Viro52094c82011-02-21 21:34:47 -05001399
1400 err = may_lookup(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 if (err)
1402 break;
1403
1404 this.name = name;
1405 c = *(const unsigned char *)name;
1406
1407 hash = init_name_hash();
1408 do {
1409 name++;
1410 hash = partial_name_hash(c, hash);
1411 c = *(const unsigned char *)name;
1412 } while (c && (c != '/'));
1413 this.len = name - (const char *) this.name;
1414 this.hash = end_name_hash(hash);
1415
Al Virofe479a52011-02-22 15:10:03 -05001416 type = LAST_NORM;
1417 if (this.name[0] == '.') switch (this.len) {
1418 case 2:
Al Viro16c2cd72011-02-22 15:50:10 -05001419 if (this.name[1] == '.') {
Al Virofe479a52011-02-22 15:10:03 -05001420 type = LAST_DOTDOT;
Al Viro16c2cd72011-02-22 15:50:10 -05001421 nd->flags |= LOOKUP_JUMPED;
1422 }
Al Virofe479a52011-02-22 15:10:03 -05001423 break;
1424 case 1:
1425 type = LAST_DOT;
1426 }
Al Viro5a202bc2011-03-08 14:17:44 -05001427 if (likely(type == LAST_NORM)) {
1428 struct dentry *parent = nd->path.dentry;
Al Viro16c2cd72011-02-22 15:50:10 -05001429 nd->flags &= ~LOOKUP_JUMPED;
Al Viro5a202bc2011-03-08 14:17:44 -05001430 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1431 err = parent->d_op->d_hash(parent, nd->inode,
1432 &this);
1433 if (err < 0)
1434 break;
1435 }
1436 }
Al Virofe479a52011-02-22 15:10:03 -05001437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 /* remove trailing slashes? */
1439 if (!c)
1440 goto last_component;
1441 while (*++name == '/');
1442 if (!*name)
1443 goto last_with_slashes;
1444
Al Viroce57dfc2011-03-13 19:58:58 -04001445 err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1446 if (err < 0)
1447 return err;
Al Virofe479a52011-02-22 15:10:03 -05001448
Al Viroce57dfc2011-03-13 19:58:58 -04001449 if (err) {
1450 err = do_follow_link(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 if (err)
Al Viroa7472ba2011-03-04 14:39:30 -05001452 return err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 err = -ENOTDIR;
Nick Piggin31e6b012011-01-07 17:49:52 +11001455 if (!nd->inode->i_op->lookup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 break;
1457 continue;
1458 /* here ends the main loop */
1459
1460last_with_slashes:
1461 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1462last_component:
Trond Myklebustf55eab82006-02-04 23:28:01 -08001463 /* Clear LOOKUP_CONTINUE iff it was previously unset */
1464 nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
Al Viroce57dfc2011-03-13 19:58:58 -04001465 if (lookup_flags & LOOKUP_PARENT) {
1466 nd->last = this;
1467 nd->last_type = type;
1468 return 0;
1469 }
1470 err = walk_component(nd, &next, &this, type,
1471 lookup_flags & LOOKUP_FOLLOW);
1472 if (err < 0)
1473 return err;
1474 if (err) {
1475 err = do_follow_link(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 if (err)
Al Viroa7472ba2011-03-04 14:39:30 -05001477 return err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 if (lookup_flags & LOOKUP_DIRECTORY) {
1480 err = -ENOTDIR;
Nick Piggin31e6b012011-01-07 17:49:52 +11001481 if (!nd->inode->i_op->lookup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 break;
1483 }
Al Viro086e1832011-02-22 20:56:27 -05001484 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 }
Al Viro951361f2011-03-04 14:44:37 -05001486 terminate_walk(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 return err;
1488}
1489
Al Viro70e9b352011-03-05 21:12:22 -05001490static int path_init(int dfd, const char *name, unsigned int flags,
1491 struct nameidata *nd, struct file **fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
Prasanna Medaea3834d2005-04-29 16:00:17 +01001493 int retval = 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001494 int fput_needed;
1495 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
1497 nd->last_type = LAST_ROOT; /* if there are only slashes... */
Al Viro16c2cd72011-02-22 15:50:10 -05001498 nd->flags = flags | LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 nd->depth = 0;
Al Viro5b6ca022011-03-09 23:04:47 -05001500 if (flags & LOOKUP_ROOT) {
1501 struct inode *inode = nd->root.dentry->d_inode;
Al Viro73d049a2011-03-11 12:08:24 -05001502 if (*name) {
1503 if (!inode->i_op->lookup)
1504 return -ENOTDIR;
1505 retval = inode_permission(inode, MAY_EXEC);
1506 if (retval)
1507 return retval;
1508 }
Al Viro5b6ca022011-03-09 23:04:47 -05001509 nd->path = nd->root;
1510 nd->inode = inode;
1511 if (flags & LOOKUP_RCU) {
1512 br_read_lock(vfsmount_lock);
1513 rcu_read_lock();
1514 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1515 } else {
1516 path_get(&nd->path);
1517 }
1518 return 0;
1519 }
1520
Al Viro2a737872009-04-07 11:49:53 -04001521 nd->root.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 if (*name=='/') {
Al Viroe41f7d42011-02-22 14:02:58 -05001524 if (flags & LOOKUP_RCU) {
1525 br_read_lock(vfsmount_lock);
1526 rcu_read_lock();
1527 set_root_rcu(nd);
1528 } else {
1529 set_root(nd);
1530 path_get(&nd->root);
1531 }
Al Viro2a737872009-04-07 11:49:53 -04001532 nd->path = nd->root;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001533 } else if (dfd == AT_FDCWD) {
Al Viroe41f7d42011-02-22 14:02:58 -05001534 if (flags & LOOKUP_RCU) {
1535 struct fs_struct *fs = current->fs;
1536 unsigned seq;
1537
1538 br_read_lock(vfsmount_lock);
1539 rcu_read_lock();
1540
1541 do {
1542 seq = read_seqcount_begin(&fs->seq);
1543 nd->path = fs->pwd;
1544 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1545 } while (read_seqcount_retry(&fs->seq, seq));
1546 } else {
1547 get_fs_pwd(current->fs, &nd->path);
1548 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001549 } else {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001550 struct dentry *dentry;
1551
Al Viro1abf0c72011-03-13 03:51:11 -04001552 file = fget_raw_light(dfd, &fput_needed);
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001553 retval = -EBADF;
1554 if (!file)
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001555 goto out_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001556
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001557 dentry = file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001558
Al Virof52e0c12011-03-14 18:56:51 -04001559 if (*name) {
1560 retval = -ENOTDIR;
1561 if (!S_ISDIR(dentry->d_inode->i_mode))
1562 goto fput_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001563
Al Virof52e0c12011-03-14 18:56:51 -04001564 retval = file_permission(file, MAY_EXEC);
1565 if (retval)
1566 goto fput_fail;
1567 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001568
Jan Blunck5dd784d2008-02-14 19:34:38 -08001569 nd->path = file->f_path;
Al Viroe41f7d42011-02-22 14:02:58 -05001570 if (flags & LOOKUP_RCU) {
1571 if (fput_needed)
Al Viro70e9b352011-03-05 21:12:22 -05001572 *fp = file;
Al Viroe41f7d42011-02-22 14:02:58 -05001573 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1574 br_read_lock(vfsmount_lock);
1575 rcu_read_lock();
1576 } else {
1577 path_get(&file->f_path);
1578 fput_light(file, fput_needed);
1579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 }
Al Viroe41f7d42011-02-22 14:02:58 -05001581
Nick Piggin31e6b012011-01-07 17:49:52 +11001582 nd->inode = nd->path.dentry->d_inode;
Al Viro9b4a9b12009-04-07 11:44:16 -04001583 return 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001584
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001585fput_fail:
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001586 fput_light(file, fput_needed);
Al Viro9b4a9b12009-04-07 11:44:16 -04001587out_fail:
1588 return retval;
1589}
1590
1591/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroee0827c2011-02-21 23:38:09 -05001592static int path_lookupat(int dfd, const char *name,
Al Viro9b4a9b12009-04-07 11:44:16 -04001593 unsigned int flags, struct nameidata *nd)
1594{
Al Viro70e9b352011-03-05 21:12:22 -05001595 struct file *base = NULL;
Nick Piggin31e6b012011-01-07 17:49:52 +11001596 int retval;
1597
1598 /*
1599 * Path walking is largely split up into 2 different synchronisation
1600 * schemes, rcu-walk and ref-walk (explained in
1601 * Documentation/filesystems/path-lookup.txt). These share much of the
1602 * path walk code, but some things particularly setup, cleanup, and
1603 * following mounts are sufficiently divergent that functions are
1604 * duplicated. Typically there is a function foo(), and its RCU
1605 * analogue, foo_rcu().
1606 *
1607 * -ECHILD is the error number of choice (just to avoid clashes) that
1608 * is returned if some aspect of an rcu-walk fails. Such an error must
1609 * be handled by restarting a traditional ref-walk (which will always
1610 * be able to complete).
1611 */
Al Viro70e9b352011-03-05 21:12:22 -05001612 retval = path_init(dfd, name, flags, nd, &base);
Al Viroee0827c2011-02-21 23:38:09 -05001613
Nick Piggin31e6b012011-01-07 17:49:52 +11001614 if (unlikely(retval))
1615 return retval;
Al Viroee0827c2011-02-21 23:38:09 -05001616
1617 current->total_link_count = 0;
1618 retval = link_path_walk(name, nd);
1619
1620 if (nd->flags & LOOKUP_RCU) {
Al Viro4455ca62011-03-04 14:28:10 -05001621 /* went all way through without dropping RCU */
1622 BUG_ON(retval);
1623 if (nameidata_drop_rcu_last(nd))
1624 retval = -ECHILD;
Al Viroee0827c2011-02-21 23:38:09 -05001625 }
1626
Al Viro16c2cd72011-02-22 15:50:10 -05001627 if (!retval)
1628 retval = handle_reval_path(nd);
1629
Al Viro70e9b352011-03-05 21:12:22 -05001630 if (base)
1631 fput(base);
Al Viroee0827c2011-02-21 23:38:09 -05001632
Al Viro5b6ca022011-03-09 23:04:47 -05001633 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
Al Viro2a737872009-04-07 11:49:53 -04001634 path_put(&nd->root);
1635 nd->root.mnt = NULL;
1636 }
Al Viroee0827c2011-02-21 23:38:09 -05001637 return retval;
1638}
Nick Piggin31e6b012011-01-07 17:49:52 +11001639
Al Viroee0827c2011-02-21 23:38:09 -05001640static int do_path_lookup(int dfd, const char *name,
1641 unsigned int flags, struct nameidata *nd)
1642{
1643 int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1644 if (unlikely(retval == -ECHILD))
1645 retval = path_lookupat(dfd, name, flags, nd);
1646 if (unlikely(retval == -ESTALE))
1647 retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11001648
1649 if (likely(!retval)) {
1650 if (unlikely(!audit_dummy_context())) {
1651 if (nd->path.dentry && nd->inode)
1652 audit_inode(name, nd->path.dentry);
1653 }
1654 }
Al Viro9b4a9b12009-04-07 11:44:16 -04001655 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656}
1657
Al Viroc9c6cac2011-02-16 15:15:47 -05001658int kern_path_parent(const char *name, struct nameidata *nd)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001659{
Al Viroc9c6cac2011-02-16 15:15:47 -05001660 return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001661}
1662
Al Virod1811462008-08-02 00:49:18 -04001663int kern_path(const char *name, unsigned int flags, struct path *path)
1664{
1665 struct nameidata nd;
1666 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1667 if (!res)
1668 *path = nd.path;
1669 return res;
1670}
1671
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001672/**
1673 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1674 * @dentry: pointer to dentry of the base directory
1675 * @mnt: pointer to vfs mount of the base directory
1676 * @name: pointer to file name
1677 * @flags: lookup flags
1678 * @nd: pointer to nameidata
1679 */
1680int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
1681 const char *name, unsigned int flags,
1682 struct nameidata *nd)
1683{
Al Viro5b6ca022011-03-09 23:04:47 -05001684 nd->root.dentry = dentry;
1685 nd->root.mnt = mnt;
1686 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
1687 return do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, nd);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001688}
1689
Christoph Hellwigeead1912007-10-16 23:25:38 -07001690static struct dentry *__lookup_hash(struct qstr *name,
1691 struct dentry *base, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
Christoph Hellwig81fca442010-10-06 10:47:47 +02001693 struct inode *inode = base->d_inode;
James Morris057f6c02007-04-26 00:12:05 -07001694 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 int err;
1696
Nick Pigginb74c79e2011-01-07 17:49:58 +11001697 err = exec_permission(inode, 0);
Christoph Hellwig81fca442010-10-06 10:47:47 +02001698 if (err)
1699 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
1701 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001702 * Don't bother with __d_lookup: callers are for creat as
1703 * well as unlink, so a lot of the time it would cost
1704 * a double lookup.
Al Viro6e6b1bd2009-08-13 23:38:37 +04001705 */
Nick Pigginb04f7842010-08-18 04:37:34 +10001706 dentry = d_lookup(base, name);
Al Viro6e6b1bd2009-08-13 23:38:37 +04001707
Nick Pigginfb045ad2011-01-07 17:49:55 +11001708 if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE))
Al Viro6e6b1bd2009-08-13 23:38:37 +04001709 dentry = do_revalidate(dentry, nd);
1710
Nick Pigginbaa03892010-08-18 04:37:31 +10001711 if (!dentry)
1712 dentry = d_alloc_and_lookup(base, name, nd);
Al Viro5a202bc2011-03-08 14:17:44 -05001713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 return dentry;
1715}
1716
James Morris057f6c02007-04-26 00:12:05 -07001717/*
1718 * Restricted form of lookup. Doesn't follow links, single-component only,
1719 * needs parent already locked. Doesn't follow mounts.
1720 * SMP-safe.
1721 */
Adrian Bunka244e162006-03-31 02:32:11 -08001722static struct dentry *lookup_hash(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723{
Jan Blunck4ac91372008-02-14 19:34:32 -08001724 return __lookup_hash(&nd->last, nd->path.dentry, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725}
1726
Christoph Hellwigeead1912007-10-16 23:25:38 -07001727/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07001728 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07001729 * @name: pathname component to lookup
1730 * @base: base directory to lookup from
1731 * @len: maximum length @len should be interpreted to
1732 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07001733 * Note that this routine is purely a helper for filesystem usage and should
1734 * not be called by generic code. Also note that by using this function the
Christoph Hellwigeead1912007-10-16 23:25:38 -07001735 * nameidata argument is passed to the filesystem methods and a filesystem
1736 * using this helper needs to be prepared for that.
1737 */
James Morris057f6c02007-04-26 00:12:05 -07001738struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1739{
James Morris057f6c02007-04-26 00:12:05 -07001740 struct qstr this;
Al Viro6a96ba52011-03-07 23:49:20 -05001741 unsigned long hash;
1742 unsigned int c;
James Morris057f6c02007-04-26 00:12:05 -07001743
David Woodhouse2f9092e2009-04-20 23:18:37 +01001744 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
1745
Al Viro6a96ba52011-03-07 23:49:20 -05001746 this.name = name;
1747 this.len = len;
1748 if (!len)
1749 return ERR_PTR(-EACCES);
1750
1751 hash = init_name_hash();
1752 while (len--) {
1753 c = *(const unsigned char *)name++;
1754 if (c == '/' || c == '\0')
1755 return ERR_PTR(-EACCES);
1756 hash = partial_name_hash(c, hash);
1757 }
1758 this.hash = end_name_hash(hash);
Al Viro5a202bc2011-03-08 14:17:44 -05001759 /*
1760 * See if the low-level filesystem might want
1761 * to use its own hash..
1762 */
1763 if (base->d_flags & DCACHE_OP_HASH) {
1764 int err = base->d_op->d_hash(base, base->d_inode, &this);
1765 if (err < 0)
1766 return ERR_PTR(err);
1767 }
Christoph Hellwigeead1912007-10-16 23:25:38 -07001768
Christoph Hellwig49705b72005-11-08 21:35:06 -08001769 return __lookup_hash(&this, base, NULL);
James Morris057f6c02007-04-26 00:12:05 -07001770}
1771
Al Viro2d8f3032008-07-22 09:59:21 -04001772int user_path_at(int dfd, const char __user *name, unsigned flags,
1773 struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774{
Al Viro2d8f3032008-07-22 09:59:21 -04001775 struct nameidata nd;
Al Virof52e0c12011-03-14 18:56:51 -04001776 char *tmp = getname_flags(name, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 int err = PTR_ERR(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 if (!IS_ERR(tmp)) {
Al Viro2d8f3032008-07-22 09:59:21 -04001779
1780 BUG_ON(flags & LOOKUP_PARENT);
1781
1782 err = do_path_lookup(dfd, tmp, flags, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 putname(tmp);
Al Viro2d8f3032008-07-22 09:59:21 -04001784 if (!err)
1785 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 }
1787 return err;
1788}
1789
Al Viro2ad94ae2008-07-21 09:32:51 -04001790static int user_path_parent(int dfd, const char __user *path,
1791 struct nameidata *nd, char **name)
1792{
1793 char *s = getname(path);
1794 int error;
1795
1796 if (IS_ERR(s))
1797 return PTR_ERR(s);
1798
1799 error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
1800 if (error)
1801 putname(s);
1802 else
1803 *name = s;
1804
1805 return error;
1806}
1807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808/*
1809 * It's inline, so penalty for filesystems that don't use sticky bit is
1810 * minimal.
1811 */
1812static inline int check_sticky(struct inode *dir, struct inode *inode)
1813{
David Howellsda9592e2008-11-14 10:39:05 +11001814 uid_t fsuid = current_fsuid();
1815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 if (!(dir->i_mode & S_ISVTX))
1817 return 0;
David Howellsda9592e2008-11-14 10:39:05 +11001818 if (inode->i_uid == fsuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 return 0;
David Howellsda9592e2008-11-14 10:39:05 +11001820 if (dir->i_uid == fsuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 return 0;
1822 return !capable(CAP_FOWNER);
1823}
1824
1825/*
1826 * Check whether we can remove a link victim from directory dir, check
1827 * whether the type of victim is right.
1828 * 1. We can't do it if dir is read-only (done in permission())
1829 * 2. We should have write and exec permissions on dir
1830 * 3. We can't remove anything from append-only dir
1831 * 4. We can't do anything with immutable dir (done in permission())
1832 * 5. If the sticky bit on dir is set we should either
1833 * a. be owner of dir, or
1834 * b. be owner of victim, or
1835 * c. have CAP_FOWNER capability
1836 * 6. If the victim is append-only or immutable we can't do antyhing with
1837 * links pointing to it.
1838 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1839 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1840 * 9. We can't remove a root or mountpoint.
1841 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1842 * nfs_async_unlink().
1843 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001844static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845{
1846 int error;
1847
1848 if (!victim->d_inode)
1849 return -ENOENT;
1850
1851 BUG_ON(victim->d_parent->d_inode != dir);
Al Virocccc6bb2009-12-25 05:07:33 -05001852 audit_inode_child(victim, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
Al Virof419a2e2008-07-22 00:07:17 -04001854 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 if (error)
1856 return error;
1857 if (IS_APPEND(dir))
1858 return -EPERM;
1859 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
Hugh Dickinsf9454542008-11-19 15:36:38 -08001860 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 return -EPERM;
1862 if (isdir) {
1863 if (!S_ISDIR(victim->d_inode->i_mode))
1864 return -ENOTDIR;
1865 if (IS_ROOT(victim))
1866 return -EBUSY;
1867 } else if (S_ISDIR(victim->d_inode->i_mode))
1868 return -EISDIR;
1869 if (IS_DEADDIR(dir))
1870 return -ENOENT;
1871 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1872 return -EBUSY;
1873 return 0;
1874}
1875
1876/* Check whether we can create an object with dentry child in directory
1877 * dir.
1878 * 1. We can't do it if child already exists (open has special treatment for
1879 * this case, but since we are inlined it's OK)
1880 * 2. We can't do it if dir is read-only (done in permission())
1881 * 3. We should have write and exec permissions on dir
1882 * 4. We can't do it if dir is immutable (done in permission())
1883 */
Miklos Szeredia95164d2008-07-30 15:08:48 +02001884static inline int may_create(struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885{
1886 if (child->d_inode)
1887 return -EEXIST;
1888 if (IS_DEADDIR(dir))
1889 return -ENOENT;
Al Virof419a2e2008-07-22 00:07:17 -04001890 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891}
1892
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893/*
1894 * p1 and p2 should be directories on the same fs.
1895 */
1896struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1897{
1898 struct dentry *p;
1899
1900 if (p1 == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07001901 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 return NULL;
1903 }
1904
Arjan van de Vena11f3a02006-03-23 03:00:33 -08001905 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001907 p = d_ancestor(p2, p1);
1908 if (p) {
1909 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1910 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
1911 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 }
1913
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001914 p = d_ancestor(p1, p2);
1915 if (p) {
1916 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1917 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1918 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 }
1920
Ingo Molnarf2eace22006-07-03 00:25:05 -07001921 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1922 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 return NULL;
1924}
1925
1926void unlock_rename(struct dentry *p1, struct dentry *p2)
1927{
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001928 mutex_unlock(&p1->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 if (p1 != p2) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001930 mutex_unlock(&p2->d_inode->i_mutex);
Arjan van de Vena11f3a02006-03-23 03:00:33 -08001931 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 }
1933}
1934
1935int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1936 struct nameidata *nd)
1937{
Miklos Szeredia95164d2008-07-30 15:08:48 +02001938 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
1940 if (error)
1941 return error;
1942
Al Viroacfa4382008-12-04 10:06:33 -05001943 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 return -EACCES; /* shouldn't it be ENOSYS? */
1945 mode &= S_IALLUGO;
1946 mode |= S_IFREG;
1947 error = security_inode_create(dir, dentry, mode);
1948 if (error)
1949 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 error = dir->i_op->create(dir, dentry, mode, nd);
Stephen Smalleya74574a2005-09-09 13:01:44 -07001951 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00001952 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 return error;
1954}
1955
Al Viro73d049a2011-03-11 12:08:24 -05001956static int may_open(struct path *path, int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001958 struct dentry *dentry = path->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 struct inode *inode = dentry->d_inode;
1960 int error;
1961
Al Virobcda7652011-03-13 16:42:14 -04001962 /* O_PATH? */
1963 if (!acc_mode)
1964 return 0;
1965
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 if (!inode)
1967 return -ENOENT;
1968
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001969 switch (inode->i_mode & S_IFMT) {
1970 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001972 case S_IFDIR:
1973 if (acc_mode & MAY_WRITE)
1974 return -EISDIR;
1975 break;
1976 case S_IFBLK:
1977 case S_IFCHR:
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001978 if (path->mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001980 /*FALLTHRU*/
1981 case S_IFIFO:
1982 case S_IFSOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001984 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08001985 }
Dave Hansenb41572e2007-10-16 23:31:14 -07001986
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001987 error = inode_permission(inode, acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07001988 if (error)
1989 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05001990
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 /*
1992 * An append-only file must be opened in append mode for writing.
1993 */
1994 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05001995 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05001996 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05001998 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 }
2000
2001 /* O_NOATIME can only be set by the owner or superuser */
Al Viro7715b522009-12-16 03:54:00 -05002002 if (flag & O_NOATIME && !is_owner_or_cap(inode))
2003 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
2005 /*
2006 * Ensure there are no outstanding leases on the file.
2007 */
Al Virob65a9cf2009-12-16 06:27:40 -05002008 return break_lease(inode, flag);
Al Viro7715b522009-12-16 03:54:00 -05002009}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
Jeff Laytone1181ee2010-12-07 16:19:50 -05002011static int handle_truncate(struct file *filp)
Al Viro7715b522009-12-16 03:54:00 -05002012{
Jeff Laytone1181ee2010-12-07 16:19:50 -05002013 struct path *path = &filp->f_path;
Al Viro7715b522009-12-16 03:54:00 -05002014 struct inode *inode = path->dentry->d_inode;
2015 int error = get_write_access(inode);
2016 if (error)
2017 return error;
2018 /*
2019 * Refuse to truncate files with mandatory locks held on them.
2020 */
2021 error = locks_verify_locked(inode);
2022 if (!error)
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +09002023 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05002024 if (!error) {
2025 error = do_truncate(path->dentry, 0,
2026 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
Jeff Laytone1181ee2010-12-07 16:19:50 -05002027 filp);
Al Viro7715b522009-12-16 03:54:00 -05002028 }
2029 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04002030 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031}
2032
Dave Hansend57999e2008-02-15 14:37:27 -08002033/*
Dave Hansend57999e2008-02-15 14:37:27 -08002034 * Note that while the flag value (low two bits) for sys_open means:
2035 * 00 - read-only
2036 * 01 - write-only
2037 * 10 - read-write
2038 * 11 - special
2039 * it is changed into
2040 * 00 - no permissions needed
2041 * 01 - read-permission
2042 * 10 - write-permission
2043 * 11 - read-write
2044 * for the internal routines (ie open_namei()/follow_link() etc)
2045 * This is more logical, and also allows the 00 "no perm needed"
2046 * to be used for symlinks (where the permissions are checked
2047 * later).
2048 *
2049*/
2050static inline int open_to_namei_flags(int flag)
2051{
2052 if ((flag+1) & O_ACCMODE)
2053 flag++;
2054 return flag;
2055}
2056
Nick Piggin31e6b012011-01-07 17:49:52 +11002057/*
Al Virofe2d35f2011-03-05 22:58:25 -05002058 * Handle the last step of open()
Nick Piggin31e6b012011-01-07 17:49:52 +11002059 */
Al Virofb1cc552009-12-24 01:58:28 -05002060static struct file *do_last(struct nameidata *nd, struct path *path,
Al Viroc3e380b2011-02-23 13:39:45 -05002061 const struct open_flags *op, const char *pathname)
Al Virofb1cc552009-12-24 01:58:28 -05002062{
Al Viroa1e28032009-12-24 02:12:06 -05002063 struct dentry *dir = nd->path.dentry;
Al Viro6c0d46c2011-03-09 00:59:59 -05002064 struct dentry *dentry;
Al Viroca344a892011-03-09 00:36:45 -05002065 int open_flag = op->open_flag;
Al Viro6c0d46c2011-03-09 00:59:59 -05002066 int will_truncate = open_flag & O_TRUNC;
Al Viroca344a892011-03-09 00:36:45 -05002067 int want_write = 0;
Al Virobcda7652011-03-13 16:42:14 -04002068 int acc_mode = op->acc_mode;
Al Virofb1cc552009-12-24 01:58:28 -05002069 struct file *filp;
Al Viro16c2cd72011-02-22 15:50:10 -05002070 int error;
Al Virofb1cc552009-12-24 01:58:28 -05002071
Al Viroc3e380b2011-02-23 13:39:45 -05002072 nd->flags &= ~LOOKUP_PARENT;
2073 nd->flags |= op->intent;
2074
Al Viro1f36f772009-12-26 10:56:19 -05002075 switch (nd->last_type) {
2076 case LAST_DOTDOT:
Neil Brown176306f2010-05-24 16:57:56 +10002077 case LAST_DOT:
Al Virofe2d35f2011-03-05 22:58:25 -05002078 error = handle_dots(nd, nd->last_type);
2079 if (error)
2080 return ERR_PTR(error);
Al Viro1f36f772009-12-26 10:56:19 -05002081 /* fallthrough */
Al Viro1f36f772009-12-26 10:56:19 -05002082 case LAST_ROOT:
Al Virofe2d35f2011-03-05 22:58:25 -05002083 if (nd->flags & LOOKUP_RCU) {
2084 if (nameidata_drop_rcu_last(nd))
2085 return ERR_PTR(-ECHILD);
2086 }
Al Viro16c2cd72011-02-22 15:50:10 -05002087 error = handle_reval_path(nd);
2088 if (error)
2089 goto exit;
Al Virofe2d35f2011-03-05 22:58:25 -05002090 audit_inode(pathname, nd->path.dentry);
Al Viroca344a892011-03-09 00:36:45 -05002091 if (open_flag & O_CREAT) {
Al Virofe2d35f2011-03-05 22:58:25 -05002092 error = -EISDIR;
2093 goto exit;
2094 }
2095 goto ok;
Al Viro1f36f772009-12-26 10:56:19 -05002096 case LAST_BIND:
Al Virofe2d35f2011-03-05 22:58:25 -05002097 /* can't be RCU mode here */
Al Viro16c2cd72011-02-22 15:50:10 -05002098 error = handle_reval_path(nd);
2099 if (error)
2100 goto exit;
Al Viro1f36f772009-12-26 10:56:19 -05002101 audit_inode(pathname, dir);
Al Viro67ee3ad2009-12-26 07:01:01 -05002102 goto ok;
Al Viro1f36f772009-12-26 10:56:19 -05002103 }
Al Viro67ee3ad2009-12-26 07:01:01 -05002104
Al Viroca344a892011-03-09 00:36:45 -05002105 if (!(open_flag & O_CREAT)) {
Al Virobcda7652011-03-13 16:42:14 -04002106 int symlink_ok = 0;
Al Virofe2d35f2011-03-05 22:58:25 -05002107 if (nd->last.name[nd->last.len])
2108 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
Al Virobcda7652011-03-13 16:42:14 -04002109 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2110 symlink_ok = 1;
Al Virofe2d35f2011-03-05 22:58:25 -05002111 /* we _can_ be in RCU mode here */
Al Viroce57dfc2011-03-13 19:58:58 -04002112 error = walk_component(nd, path, &nd->last, LAST_NORM,
2113 !symlink_ok);
2114 if (error < 0)
Al Virofe2d35f2011-03-05 22:58:25 -05002115 return ERR_PTR(error);
Al Viroce57dfc2011-03-13 19:58:58 -04002116 if (error) /* symlink */
Al Virofe2d35f2011-03-05 22:58:25 -05002117 return NULL;
Al Virofe2d35f2011-03-05 22:58:25 -05002118 /* sayonara */
2119 if (nd->flags & LOOKUP_RCU) {
2120 if (nameidata_drop_rcu_last(nd))
2121 return ERR_PTR(-ECHILD);
2122 }
2123
2124 error = -ENOTDIR;
2125 if (nd->flags & LOOKUP_DIRECTORY) {
Al Viroce57dfc2011-03-13 19:58:58 -04002126 if (!nd->inode->i_op->lookup)
Al Virofe2d35f2011-03-05 22:58:25 -05002127 goto exit;
2128 }
2129 audit_inode(pathname, nd->path.dentry);
2130 goto ok;
2131 }
2132
2133 /* create side of things */
2134
2135 if (nd->flags & LOOKUP_RCU) {
2136 if (nameidata_drop_rcu_last(nd))
2137 return ERR_PTR(-ECHILD);
2138 }
2139
2140 audit_inode(pathname, dir);
Al Viro16c2cd72011-02-22 15:50:10 -05002141 error = -EISDIR;
Al Viro1f36f772009-12-26 10:56:19 -05002142 /* trailing slashes? */
Nick Piggin31e6b012011-01-07 17:49:52 +11002143 if (nd->last.name[nd->last.len])
2144 goto exit;
Al Viroa2c36b42009-12-24 03:39:50 -05002145
Al Viroa1e28032009-12-24 02:12:06 -05002146 mutex_lock(&dir->d_inode->i_mutex);
2147
Al Viro6c0d46c2011-03-09 00:59:59 -05002148 dentry = lookup_hash(nd);
2149 error = PTR_ERR(dentry);
2150 if (IS_ERR(dentry)) {
Al Virofb1cc552009-12-24 01:58:28 -05002151 mutex_unlock(&dir->d_inode->i_mutex);
2152 goto exit;
2153 }
2154
Al Viro6c0d46c2011-03-09 00:59:59 -05002155 path->dentry = dentry;
2156 path->mnt = nd->path.mnt;
2157
Al Virofb1cc552009-12-24 01:58:28 -05002158 /* Negative dentry, just create the file */
Al Viro6c0d46c2011-03-09 00:59:59 -05002159 if (!dentry->d_inode) {
2160 int mode = op->mode;
2161 if (!IS_POSIXACL(dir->d_inode))
2162 mode &= ~current_umask();
Al Virofb1cc552009-12-24 01:58:28 -05002163 /*
2164 * This write is needed to ensure that a
Al Viro6c0d46c2011-03-09 00:59:59 -05002165 * rw->ro transition does not occur between
Al Virofb1cc552009-12-24 01:58:28 -05002166 * the time when the file is created and when
2167 * a permanent write count is taken through
2168 * the 'struct file' in nameidata_to_filp().
2169 */
2170 error = mnt_want_write(nd->path.mnt);
2171 if (error)
2172 goto exit_mutex_unlock;
Al Viroca344a892011-03-09 00:36:45 -05002173 want_write = 1;
Al Viro9b44f1b2011-03-09 00:17:27 -05002174 /* Don't check for write permission, don't truncate */
Al Viroca344a892011-03-09 00:36:45 -05002175 open_flag &= ~O_TRUNC;
Al Viro6c0d46c2011-03-09 00:59:59 -05002176 will_truncate = 0;
Al Virobcda7652011-03-13 16:42:14 -04002177 acc_mode = MAY_OPEN;
Al Viro6c0d46c2011-03-09 00:59:59 -05002178 error = security_path_mknod(&nd->path, dentry, mode, 0);
2179 if (error)
2180 goto exit_mutex_unlock;
2181 error = vfs_create(dir->d_inode, dentry, mode, nd);
2182 if (error)
2183 goto exit_mutex_unlock;
2184 mutex_unlock(&dir->d_inode->i_mutex);
2185 dput(nd->path.dentry);
2186 nd->path.dentry = dentry;
Al Viroca344a892011-03-09 00:36:45 -05002187 goto common;
Al Virofb1cc552009-12-24 01:58:28 -05002188 }
2189
2190 /*
2191 * It already exists.
2192 */
2193 mutex_unlock(&dir->d_inode->i_mutex);
2194 audit_inode(pathname, path->dentry);
2195
2196 error = -EEXIST;
Al Viroca344a892011-03-09 00:36:45 -05002197 if (open_flag & O_EXCL)
Al Virofb1cc552009-12-24 01:58:28 -05002198 goto exit_dput;
2199
David Howells9875cf82011-01-14 18:45:21 +00002200 error = follow_managed(path, nd->flags);
2201 if (error < 0)
2202 goto exit_dput;
Al Virofb1cc552009-12-24 01:58:28 -05002203
2204 error = -ENOENT;
2205 if (!path->dentry->d_inode)
2206 goto exit_dput;
Al Viro9e67f362009-12-26 07:04:50 -05002207
2208 if (path->dentry->d_inode->i_op->follow_link)
Al Virofb1cc552009-12-24 01:58:28 -05002209 return NULL;
Al Virofb1cc552009-12-24 01:58:28 -05002210
2211 path_to_nameidata(path, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11002212 nd->inode = path->dentry->d_inode;
Al Virofb1cc552009-12-24 01:58:28 -05002213 error = -EISDIR;
Nick Piggin31e6b012011-01-07 17:49:52 +11002214 if (S_ISDIR(nd->inode->i_mode))
Al Virofb1cc552009-12-24 01:58:28 -05002215 goto exit;
Al Viro67ee3ad2009-12-26 07:01:01 -05002216ok:
Al Viro6c0d46c2011-03-09 00:59:59 -05002217 if (!S_ISREG(nd->inode->i_mode))
2218 will_truncate = 0;
2219
Al Viro0f9d1a12011-03-09 00:13:14 -05002220 if (will_truncate) {
2221 error = mnt_want_write(nd->path.mnt);
2222 if (error)
2223 goto exit;
Al Viroca344a892011-03-09 00:36:45 -05002224 want_write = 1;
Al Viro0f9d1a12011-03-09 00:13:14 -05002225 }
Al Viroca344a892011-03-09 00:36:45 -05002226common:
Al Virobcda7652011-03-13 16:42:14 -04002227 error = may_open(&nd->path, acc_mode, open_flag);
Al Viroca344a892011-03-09 00:36:45 -05002228 if (error)
Al Viro0f9d1a12011-03-09 00:13:14 -05002229 goto exit;
Al Viro0f9d1a12011-03-09 00:13:14 -05002230 filp = nameidata_to_filp(nd);
2231 if (!IS_ERR(filp)) {
2232 error = ima_file_check(filp, op->acc_mode);
2233 if (error) {
2234 fput(filp);
2235 filp = ERR_PTR(error);
2236 }
2237 }
2238 if (!IS_ERR(filp)) {
2239 if (will_truncate) {
2240 error = handle_truncate(filp);
2241 if (error) {
2242 fput(filp);
2243 filp = ERR_PTR(error);
2244 }
2245 }
2246 }
Al Viroca344a892011-03-09 00:36:45 -05002247out:
2248 if (want_write)
Al Viro0f9d1a12011-03-09 00:13:14 -05002249 mnt_drop_write(nd->path.mnt);
2250 path_put(&nd->path);
Al Virofb1cc552009-12-24 01:58:28 -05002251 return filp;
2252
2253exit_mutex_unlock:
2254 mutex_unlock(&dir->d_inode->i_mutex);
2255exit_dput:
2256 path_put_conditional(path, nd);
2257exit:
Al Viroca344a892011-03-09 00:36:45 -05002258 filp = ERR_PTR(error);
2259 goto out;
Al Virofb1cc552009-12-24 01:58:28 -05002260}
2261
Al Viro13aab422011-02-23 17:54:08 -05002262static struct file *path_openat(int dfd, const char *pathname,
Al Viro73d049a2011-03-11 12:08:24 -05002263 struct nameidata *nd, const struct open_flags *op, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264{
Al Virofe2d35f2011-03-05 22:58:25 -05002265 struct file *base = NULL;
Dave Hansen4a3fd212008-02-15 14:37:48 -08002266 struct file *filp;
Al Viro9850c052010-01-13 15:01:15 -05002267 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 int count = 0;
Al Viro13aab422011-02-23 17:54:08 -05002269 int error;
Nick Piggin31e6b012011-01-07 17:49:52 +11002270
2271 filp = get_empty_filp();
2272 if (!filp)
2273 return ERR_PTR(-ENFILE);
2274
Al Viro47c805d2011-02-23 17:44:09 -05002275 filp->f_flags = op->open_flag;
Al Viro73d049a2011-03-11 12:08:24 -05002276 nd->intent.open.file = filp;
2277 nd->intent.open.flags = open_to_namei_flags(op->open_flag);
2278 nd->intent.open.create_mode = op->mode;
Nick Piggin31e6b012011-01-07 17:49:52 +11002279
Al Viro73d049a2011-03-11 12:08:24 -05002280 error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
Nick Piggin31e6b012011-01-07 17:49:52 +11002281 if (unlikely(error))
Al Viro13aab422011-02-23 17:54:08 -05002282 goto out_filp;
Nick Piggin31e6b012011-01-07 17:49:52 +11002283
Al Virofe2d35f2011-03-05 22:58:25 -05002284 current->total_link_count = 0;
Al Viro73d049a2011-03-11 12:08:24 -05002285 error = link_path_walk(pathname, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11002286 if (unlikely(error))
2287 goto out_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
Al Viro73d049a2011-03-11 12:08:24 -05002289 filp = do_last(nd, &path, op, pathname);
Al Viro806b6812009-12-26 07:16:40 -05002290 while (unlikely(!filp)) { /* trailing symlink */
Nick Piggin7b9337a2011-01-14 08:42:43 +00002291 struct path link = path;
2292 struct inode *linki = link.dentry->d_inode;
Al Virodef4af32009-12-26 08:37:05 -05002293 void *cookie;
Al Viro73d049a2011-03-11 12:08:24 -05002294 if (!(nd->flags & LOOKUP_FOLLOW) || count++ == 32) {
2295 path_put_conditional(&path, nd);
2296 path_put(&nd->path);
Al Viro40b39132011-03-09 16:22:18 -05002297 filp = ERR_PTR(-ELOOP);
2298 break;
2299 }
Al Viro806b6812009-12-26 07:16:40 -05002300 /*
2301 * This is subtle. Instead of calling do_follow_link() we do
2302 * the thing by hands. The reason is that this way we have zero
2303 * link_count and path_walk() (called from ->follow_link)
2304 * honoring LOOKUP_PARENT. After that we have the parent and
2305 * last component, i.e. we are in the same situation as after
2306 * the first path_walk(). Well, almost - if the last component
2307 * is normal we get its copy stored in nd->last.name and we will
2308 * have to putname() it when we are done. Procfs-like symlinks
2309 * just set LAST_BIND.
2310 */
Al Viro73d049a2011-03-11 12:08:24 -05002311 nd->flags |= LOOKUP_PARENT;
2312 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
2313 error = __do_follow_link(&link, nd, &cookie);
Al Viroc3e380b2011-02-23 13:39:45 -05002314 if (unlikely(error))
Al Virof1afe9e2011-02-22 22:27:28 -05002315 filp = ERR_PTR(error);
Al Viroc3e380b2011-02-23 13:39:45 -05002316 else
Al Viro73d049a2011-03-11 12:08:24 -05002317 filp = do_last(nd, &path, op, pathname);
Al Virof1afe9e2011-02-22 22:27:28 -05002318 if (!IS_ERR(cookie) && linki->i_op->put_link)
Al Viro73d049a2011-03-11 12:08:24 -05002319 linki->i_op->put_link(link.dentry, nd, cookie);
Nick Piggin7b9337a2011-01-14 08:42:43 +00002320 path_put(&link);
Al Viro806b6812009-12-26 07:16:40 -05002321 }
Al Viro10fa8e62009-12-26 07:09:49 -05002322out:
Al Viro73d049a2011-03-11 12:08:24 -05002323 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
2324 path_put(&nd->root);
Al Virofe2d35f2011-03-05 22:58:25 -05002325 if (base)
2326 fput(base);
Al Viro73d049a2011-03-11 12:08:24 -05002327 release_open_intent(nd);
Al Viro10fa8e62009-12-26 07:09:49 -05002328 return filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
Nick Piggin31e6b012011-01-07 17:49:52 +11002330out_filp:
Al Viro806b6812009-12-26 07:16:40 -05002331 filp = ERR_PTR(error);
Al Viro10fa8e62009-12-26 07:09:49 -05002332 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333}
2334
Al Viro13aab422011-02-23 17:54:08 -05002335struct file *do_filp_open(int dfd, const char *pathname,
2336 const struct open_flags *op, int flags)
2337{
Al Viro73d049a2011-03-11 12:08:24 -05002338 struct nameidata nd;
Al Viro13aab422011-02-23 17:54:08 -05002339 struct file *filp;
2340
Al Viro73d049a2011-03-11 12:08:24 -05002341 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
Al Viro13aab422011-02-23 17:54:08 -05002342 if (unlikely(filp == ERR_PTR(-ECHILD)))
Al Viro73d049a2011-03-11 12:08:24 -05002343 filp = path_openat(dfd, pathname, &nd, op, flags);
Al Viro13aab422011-02-23 17:54:08 -05002344 if (unlikely(filp == ERR_PTR(-ESTALE)))
Al Viro73d049a2011-03-11 12:08:24 -05002345 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
Al Viro13aab422011-02-23 17:54:08 -05002346 return filp;
2347}
2348
Al Viro73d049a2011-03-11 12:08:24 -05002349struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
2350 const char *name, const struct open_flags *op, int flags)
2351{
2352 struct nameidata nd;
2353 struct file *file;
2354
2355 nd.root.mnt = mnt;
2356 nd.root.dentry = dentry;
2357
2358 flags |= LOOKUP_ROOT;
2359
Al Virobcda7652011-03-13 16:42:14 -04002360 if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
Al Viro73d049a2011-03-11 12:08:24 -05002361 return ERR_PTR(-ELOOP);
2362
2363 file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
2364 if (unlikely(file == ERR_PTR(-ECHILD)))
2365 file = path_openat(-1, name, &nd, op, flags);
2366 if (unlikely(file == ERR_PTR(-ESTALE)))
2367 file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
2368 return file;
2369}
2370
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371/**
2372 * lookup_create - lookup a dentry, creating it if it doesn't exist
2373 * @nd: nameidata info
2374 * @is_dir: directory flag
2375 *
2376 * Simple function to lookup and return a dentry and create it
2377 * if it doesn't exist. Is SMP-safe.
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002378 *
Jan Blunck4ac91372008-02-14 19:34:32 -08002379 * Returns with nd->path.dentry->d_inode->i_mutex locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 */
2381struct dentry *lookup_create(struct nameidata *nd, int is_dir)
2382{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002383 struct dentry *dentry = ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
Jan Blunck4ac91372008-02-14 19:34:32 -08002385 mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002386 /*
2387 * Yucky last component or no last component at all?
2388 * (foo/., foo/.., /////)
2389 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 if (nd->last_type != LAST_NORM)
2391 goto fail;
2392 nd->flags &= ~LOOKUP_PARENT;
Al Viro35165862008-08-05 03:00:49 -04002393 nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
ASANO Masahiroa6349042006-08-22 20:06:02 -04002394 nd->intent.open.flags = O_EXCL;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002395
2396 /*
2397 * Do the final lookup.
2398 */
Christoph Hellwig49705b72005-11-08 21:35:06 -08002399 dentry = lookup_hash(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 if (IS_ERR(dentry))
2401 goto fail;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002402
Al Viroe9baf6e2008-05-15 04:49:12 -04002403 if (dentry->d_inode)
2404 goto eexist;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002405 /*
2406 * Special case - lookup gave negative, but... we had foo/bar/
2407 * From the vfs_mknod() POV we just have a negative dentry -
2408 * all is fine. Let's be bastards - you had / on the end, you've
2409 * been asking for (non-existent) directory. -ENOENT for you.
2410 */
Al Viroe9baf6e2008-05-15 04:49:12 -04002411 if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
2412 dput(dentry);
2413 dentry = ERR_PTR(-ENOENT);
2414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 return dentry;
Al Viroe9baf6e2008-05-15 04:49:12 -04002416eexist:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 dput(dentry);
Al Viroe9baf6e2008-05-15 04:49:12 -04002418 dentry = ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419fail:
2420 return dentry;
2421}
Christoph Hellwigf81a0bf2005-05-19 12:26:43 -07002422EXPORT_SYMBOL_GPL(lookup_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423
2424int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
2425{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002426 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
2428 if (error)
2429 return error;
2430
2431 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
2432 return -EPERM;
2433
Al Viroacfa4382008-12-04 10:06:33 -05002434 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 return -EPERM;
2436
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07002437 error = devcgroup_inode_mknod(mode, dev);
2438 if (error)
2439 return error;
2440
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 error = security_inode_mknod(dir, dentry, mode, dev);
2442 if (error)
2443 return error;
2444
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002446 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002447 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 return error;
2449}
2450
Dave Hansen463c3192008-02-15 14:37:57 -08002451static int may_mknod(mode_t mode)
2452{
2453 switch (mode & S_IFMT) {
2454 case S_IFREG:
2455 case S_IFCHR:
2456 case S_IFBLK:
2457 case S_IFIFO:
2458 case S_IFSOCK:
2459 case 0: /* zero mode translates to S_IFREG */
2460 return 0;
2461 case S_IFDIR:
2462 return -EPERM;
2463 default:
2464 return -EINVAL;
2465 }
2466}
2467
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002468SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
2469 unsigned, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470{
Al Viro2ad94ae2008-07-21 09:32:51 -04002471 int error;
2472 char *tmp;
2473 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 struct nameidata nd;
2475
2476 if (S_ISDIR(mode))
2477 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
Al Viro2ad94ae2008-07-21 09:32:51 -04002479 error = user_path_parent(dfd, filename, &nd, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002481 return error;
2482
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 dentry = lookup_create(&nd, 0);
Dave Hansen463c3192008-02-15 14:37:57 -08002484 if (IS_ERR(dentry)) {
2485 error = PTR_ERR(dentry);
2486 goto out_unlock;
2487 }
Jan Blunck4ac91372008-02-14 19:34:32 -08002488 if (!IS_POSIXACL(nd.path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04002489 mode &= ~current_umask();
Dave Hansen463c3192008-02-15 14:37:57 -08002490 error = may_mknod(mode);
2491 if (error)
2492 goto out_dput;
2493 error = mnt_want_write(nd.path.mnt);
2494 if (error)
2495 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002496 error = security_path_mknod(&nd.path, dentry, mode, dev);
2497 if (error)
2498 goto out_drop_write;
Dave Hansen463c3192008-02-15 14:37:57 -08002499 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 case 0: case S_IFREG:
Jan Blunck4ac91372008-02-14 19:34:32 -08002501 error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 break;
2503 case S_IFCHR: case S_IFBLK:
Jan Blunck4ac91372008-02-14 19:34:32 -08002504 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 new_decode_dev(dev));
2506 break;
2507 case S_IFIFO: case S_IFSOCK:
Jan Blunck4ac91372008-02-14 19:34:32 -08002508 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 }
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002511out_drop_write:
Dave Hansen463c3192008-02-15 14:37:57 -08002512 mnt_drop_write(nd.path.mnt);
2513out_dput:
2514 dput(dentry);
2515out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002516 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002517 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 putname(tmp);
2519
2520 return error;
2521}
2522
Heiko Carstens3480b252009-01-14 14:14:16 +01002523SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002524{
2525 return sys_mknodat(AT_FDCWD, filename, mode, dev);
2526}
2527
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2529{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002530 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531
2532 if (error)
2533 return error;
2534
Al Viroacfa4382008-12-04 10:06:33 -05002535 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 return -EPERM;
2537
2538 mode &= (S_IRWXUGO|S_ISVTX);
2539 error = security_inode_mkdir(dir, dentry, mode);
2540 if (error)
2541 return error;
2542
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002544 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002545 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 return error;
2547}
2548
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002549SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550{
2551 int error = 0;
2552 char * tmp;
Dave Hansen6902d922006-09-30 23:29:01 -07002553 struct dentry *dentry;
2554 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555
Al Viro2ad94ae2008-07-21 09:32:51 -04002556 error = user_path_parent(dfd, pathname, &nd, &tmp);
2557 if (error)
Dave Hansen6902d922006-09-30 23:29:01 -07002558 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
Dave Hansen6902d922006-09-30 23:29:01 -07002560 dentry = lookup_create(&nd, 1);
2561 error = PTR_ERR(dentry);
2562 if (IS_ERR(dentry))
2563 goto out_unlock;
2564
Jan Blunck4ac91372008-02-14 19:34:32 -08002565 if (!IS_POSIXACL(nd.path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04002566 mode &= ~current_umask();
Dave Hansen463c3192008-02-15 14:37:57 -08002567 error = mnt_want_write(nd.path.mnt);
2568 if (error)
2569 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002570 error = security_path_mkdir(&nd.path, dentry, mode);
2571 if (error)
2572 goto out_drop_write;
Jan Blunck4ac91372008-02-14 19:34:32 -08002573 error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002574out_drop_write:
Dave Hansen463c3192008-02-15 14:37:57 -08002575 mnt_drop_write(nd.path.mnt);
2576out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002577 dput(dentry);
2578out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002579 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002580 path_put(&nd.path);
Dave Hansen6902d922006-09-30 23:29:01 -07002581 putname(tmp);
2582out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 return error;
2584}
2585
Heiko Carstens3cdad422009-01-14 14:14:22 +01002586SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002587{
2588 return sys_mkdirat(AT_FDCWD, pathname, mode);
2589}
2590
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591/*
2592 * We try to drop the dentry early: we should have
2593 * a usage count of 2 if we're the only user of this
2594 * dentry, and if that is true (possibly after pruning
2595 * the dcache), then we drop the dentry now.
2596 *
2597 * A low-level filesystem can, if it choses, legally
2598 * do a
2599 *
2600 * if (!d_unhashed(dentry))
2601 * return -EBUSY;
2602 *
2603 * if it cannot handle the case of removing a directory
2604 * that is still in use by something else..
2605 */
2606void dentry_unhash(struct dentry *dentry)
2607{
2608 dget(dentry);
Vasily Averindc168422006-12-06 20:37:07 -08002609 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 spin_lock(&dentry->d_lock);
Nick Pigginb7ab39f2011-01-07 17:49:32 +11002611 if (dentry->d_count == 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 __d_drop(dentry);
2613 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614}
2615
2616int vfs_rmdir(struct inode *dir, struct dentry *dentry)
2617{
2618 int error = may_delete(dir, dentry, 1);
2619
2620 if (error)
2621 return error;
2622
Al Viroacfa4382008-12-04 10:06:33 -05002623 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 return -EPERM;
2625
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002626 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 dentry_unhash(dentry);
2628 if (d_mountpoint(dentry))
2629 error = -EBUSY;
2630 else {
2631 error = security_inode_rmdir(dir, dentry);
2632 if (!error) {
2633 error = dir->i_op->rmdir(dir, dentry);
Al Virod83c49f2010-04-30 17:17:09 -04002634 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 dentry->d_inode->i_flags |= S_DEAD;
Al Virod83c49f2010-04-30 17:17:09 -04002636 dont_mount(dentry);
2637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 }
2639 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002640 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 d_delete(dentry);
2643 }
2644 dput(dentry);
2645
2646 return error;
2647}
2648
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002649static long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650{
2651 int error = 0;
2652 char * name;
2653 struct dentry *dentry;
2654 struct nameidata nd;
2655
Al Viro2ad94ae2008-07-21 09:32:51 -04002656 error = user_path_parent(dfd, pathname, &nd, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002658 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
2660 switch(nd.last_type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002661 case LAST_DOTDOT:
2662 error = -ENOTEMPTY;
2663 goto exit1;
2664 case LAST_DOT:
2665 error = -EINVAL;
2666 goto exit1;
2667 case LAST_ROOT:
2668 error = -EBUSY;
2669 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002671
2672 nd.flags &= ~LOOKUP_PARENT;
2673
Jan Blunck4ac91372008-02-14 19:34:32 -08002674 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002675 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002677 if (IS_ERR(dentry))
2678 goto exit2;
Dave Hansen06227532008-02-15 14:37:34 -08002679 error = mnt_want_write(nd.path.mnt);
2680 if (error)
2681 goto exit3;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002682 error = security_path_rmdir(&nd.path, dentry);
2683 if (error)
2684 goto exit4;
Jan Blunck4ac91372008-02-14 19:34:32 -08002685 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002686exit4:
Dave Hansen06227532008-02-15 14:37:34 -08002687 mnt_drop_write(nd.path.mnt);
2688exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07002689 dput(dentry);
2690exit2:
Jan Blunck4ac91372008-02-14 19:34:32 -08002691 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002693 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 putname(name);
2695 return error;
2696}
2697
Heiko Carstens3cdad422009-01-14 14:14:22 +01002698SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002699{
2700 return do_rmdir(AT_FDCWD, pathname);
2701}
2702
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703int vfs_unlink(struct inode *dir, struct dentry *dentry)
2704{
2705 int error = may_delete(dir, dentry, 0);
2706
2707 if (error)
2708 return error;
2709
Al Viroacfa4382008-12-04 10:06:33 -05002710 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 return -EPERM;
2712
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002713 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 if (d_mountpoint(dentry))
2715 error = -EBUSY;
2716 else {
2717 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05002718 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 error = dir->i_op->unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05002720 if (!error)
Al Virod83c49f2010-04-30 17:17:09 -04002721 dont_mount(dentry);
Al Virobec10522010-03-03 14:12:08 -05002722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002724 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725
2726 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2727 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
Jan Karaece95912008-02-06 01:37:13 -08002728 fsnotify_link_count(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 }
Robert Love0eeca282005-07-12 17:06:03 -04002731
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 return error;
2733}
2734
2735/*
2736 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002737 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 * writeout happening, and we don't want to prevent access to the directory
2739 * while waiting on the I/O.
2740 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002741static long do_unlinkat(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742{
Al Viro2ad94ae2008-07-21 09:32:51 -04002743 int error;
2744 char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 struct dentry *dentry;
2746 struct nameidata nd;
2747 struct inode *inode = NULL;
2748
Al Viro2ad94ae2008-07-21 09:32:51 -04002749 error = user_path_parent(dfd, pathname, &nd, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002751 return error;
2752
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 error = -EISDIR;
2754 if (nd.last_type != LAST_NORM)
2755 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002756
2757 nd.flags &= ~LOOKUP_PARENT;
2758
Jan Blunck4ac91372008-02-14 19:34:32 -08002759 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002760 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 error = PTR_ERR(dentry);
2762 if (!IS_ERR(dentry)) {
2763 /* Why not before? Because we want correct error value */
2764 if (nd.last.name[nd.last.len])
2765 goto slashes;
2766 inode = dentry->d_inode;
2767 if (inode)
Al Viro7de9c6e2010-10-23 11:11:40 -04002768 ihold(inode);
Dave Hansen06227532008-02-15 14:37:34 -08002769 error = mnt_want_write(nd.path.mnt);
2770 if (error)
2771 goto exit2;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002772 error = security_path_unlink(&nd.path, dentry);
2773 if (error)
2774 goto exit3;
Jan Blunck4ac91372008-02-14 19:34:32 -08002775 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002776exit3:
Dave Hansen06227532008-02-15 14:37:34 -08002777 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 exit2:
2779 dput(dentry);
2780 }
Jan Blunck4ac91372008-02-14 19:34:32 -08002781 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 if (inode)
2783 iput(inode); /* truncate the inode here */
2784exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002785 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 putname(name);
2787 return error;
2788
2789slashes:
2790 error = !dentry->d_inode ? -ENOENT :
2791 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2792 goto exit2;
2793}
2794
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002795SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002796{
2797 if ((flag & ~AT_REMOVEDIR) != 0)
2798 return -EINVAL;
2799
2800 if (flag & AT_REMOVEDIR)
2801 return do_rmdir(dfd, pathname);
2802
2803 return do_unlinkat(dfd, pathname);
2804}
2805
Heiko Carstens3480b252009-01-14 14:14:16 +01002806SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002807{
2808 return do_unlinkat(AT_FDCWD, pathname);
2809}
2810
Miklos Szeredidb2e7472008-06-24 16:50:16 +02002811int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002813 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814
2815 if (error)
2816 return error;
2817
Al Viroacfa4382008-12-04 10:06:33 -05002818 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 return -EPERM;
2820
2821 error = security_inode_symlink(dir, dentry, oldname);
2822 if (error)
2823 return error;
2824
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002826 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002827 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 return error;
2829}
2830
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002831SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
2832 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833{
Al Viro2ad94ae2008-07-21 09:32:51 -04002834 int error;
2835 char *from;
2836 char *to;
Dave Hansen6902d922006-09-30 23:29:01 -07002837 struct dentry *dentry;
2838 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839
2840 from = getname(oldname);
Al Viro2ad94ae2008-07-21 09:32:51 -04002841 if (IS_ERR(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 return PTR_ERR(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04002843
2844 error = user_path_parent(newdfd, newname, &nd, &to);
2845 if (error)
Dave Hansen6902d922006-09-30 23:29:01 -07002846 goto out_putname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847
Dave Hansen6902d922006-09-30 23:29:01 -07002848 dentry = lookup_create(&nd, 0);
2849 error = PTR_ERR(dentry);
2850 if (IS_ERR(dentry))
2851 goto out_unlock;
2852
Dave Hansen75c3f292008-02-15 14:37:45 -08002853 error = mnt_want_write(nd.path.mnt);
2854 if (error)
2855 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002856 error = security_path_symlink(&nd.path, dentry, from);
2857 if (error)
2858 goto out_drop_write;
Miklos Szeredidb2e7472008-06-24 16:50:16 +02002859 error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002860out_drop_write:
Dave Hansen75c3f292008-02-15 14:37:45 -08002861 mnt_drop_write(nd.path.mnt);
2862out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002863 dput(dentry);
2864out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002865 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002866 path_put(&nd.path);
Dave Hansen6902d922006-09-30 23:29:01 -07002867 putname(to);
2868out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 putname(from);
2870 return error;
2871}
2872
Heiko Carstens3480b252009-01-14 14:14:16 +01002873SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002874{
2875 return sys_symlinkat(oldname, AT_FDCWD, newname);
2876}
2877
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2879{
2880 struct inode *inode = old_dentry->d_inode;
2881 int error;
2882
2883 if (!inode)
2884 return -ENOENT;
2885
Miklos Szeredia95164d2008-07-30 15:08:48 +02002886 error = may_create(dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 if (error)
2888 return error;
2889
2890 if (dir->i_sb != inode->i_sb)
2891 return -EXDEV;
2892
2893 /*
2894 * A link to an append-only or immutable file cannot be created.
2895 */
2896 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2897 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05002898 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002900 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 return -EPERM;
2902
2903 error = security_inode_link(old_dentry, dir, new_dentry);
2904 if (error)
2905 return error;
2906
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002907 mutex_lock(&inode->i_mutex);
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05302908 /* Make sure we don't allow creating hardlink to an unlinked file */
2909 if (inode->i_nlink == 0)
2910 error = -ENOENT;
2911 else
2912 error = dir->i_op->link(old_dentry, dir, new_dentry);
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002913 mutex_unlock(&inode->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07002914 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002915 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 return error;
2917}
2918
2919/*
2920 * Hardlinks are often used in delicate situations. We avoid
2921 * security-related surprises by not following symlinks on the
2922 * newname. --KAB
2923 *
2924 * We don't follow them on the oldname either to be compatible
2925 * with linux 2.0, and to avoid hard-linking to directories
2926 * and other special files. --ADM
2927 */
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002928SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
2929 int, newdfd, const char __user *, newname, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930{
2931 struct dentry *new_dentry;
Al Viro2d8f3032008-07-22 09:59:21 -04002932 struct nameidata nd;
2933 struct path old_path;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05302934 int how = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 int error;
Al Viro2ad94ae2008-07-21 09:32:51 -04002936 char *to;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05302938 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002939 return -EINVAL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05302940 /*
2941 * To use null names we require CAP_DAC_READ_SEARCH
2942 * This ensures that not everyone will be able to create
2943 * handlink using the passed filedescriptor.
2944 */
2945 if (flags & AT_EMPTY_PATH) {
2946 if (!capable(CAP_DAC_READ_SEARCH))
2947 return -ENOENT;
2948 how = LOOKUP_EMPTY;
2949 }
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002950
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05302951 if (flags & AT_SYMLINK_FOLLOW)
2952 how |= LOOKUP_FOLLOW;
2953
2954 error = user_path_at(olddfd, oldname, how, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002956 return error;
2957
2958 error = user_path_parent(newdfd, newname, &nd, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 if (error)
2960 goto out;
2961 error = -EXDEV;
Al Viro2d8f3032008-07-22 09:59:21 -04002962 if (old_path.mnt != nd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 goto out_release;
2964 new_dentry = lookup_create(&nd, 0);
2965 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002966 if (IS_ERR(new_dentry))
2967 goto out_unlock;
Dave Hansen75c3f292008-02-15 14:37:45 -08002968 error = mnt_want_write(nd.path.mnt);
2969 if (error)
2970 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002971 error = security_path_link(old_path.dentry, &nd.path, new_dentry);
2972 if (error)
2973 goto out_drop_write;
Al Viro2d8f3032008-07-22 09:59:21 -04002974 error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002975out_drop_write:
Dave Hansen75c3f292008-02-15 14:37:45 -08002976 mnt_drop_write(nd.path.mnt);
2977out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002978 dput(new_dentry);
2979out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002980 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981out_release:
Jan Blunck1d957f92008-02-14 19:34:35 -08002982 path_put(&nd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04002983 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984out:
Al Viro2d8f3032008-07-22 09:59:21 -04002985 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986
2987 return error;
2988}
2989
Heiko Carstens3480b252009-01-14 14:14:16 +01002990SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002991{
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002992 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002993}
2994
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995/*
2996 * The worst of all namespace operations - renaming directory. "Perverted"
2997 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2998 * Problems:
2999 * a) we can get into loop creation. Check is done in is_subdir().
3000 * b) race potential - two innocent renames can create a loop together.
3001 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08003002 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 * story.
3004 * c) we have to lock _three_ objects - parents and victim (if it exists).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003005 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 * whether the target exists). Solution: try to be smart with locking
3007 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08003008 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 * move will be locked. Thus we can rank directories by the tree
3010 * (ancestors first) and rank all non-directories after them.
3011 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08003012 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 * HOWEVER, it relies on the assumption that any object with ->lookup()
3014 * has no more than 1 dentry. If "hybrid" objects will ever appear,
3015 * we'd better make sure that there's no link(2) for them.
3016 * d) some filesystems don't support opened-but-unlinked directories,
3017 * either because of layout or because they are not ready to deal with
3018 * all cases correctly. The latter will be fixed (taking this sort of
3019 * stuff into VFS), but the former is not going away. Solution: the same
3020 * trick as in rmdir().
3021 * e) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003022 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05003024 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 * locking].
3026 */
Adrian Bunk75c96f82005-05-05 16:16:09 -07003027static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
3028 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029{
3030 int error = 0;
3031 struct inode *target;
3032
3033 /*
3034 * If we are going to change the parent - check write permissions,
3035 * we'll need to flip '..'.
3036 */
3037 if (new_dir != old_dir) {
Al Virof419a2e2008-07-22 00:07:17 -04003038 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 if (error)
3040 return error;
3041 }
3042
3043 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3044 if (error)
3045 return error;
3046
3047 target = new_dentry->d_inode;
Al Virod83c49f2010-04-30 17:17:09 -04003048 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003049 mutex_lock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
3051 error = -EBUSY;
Al Virod83c49f2010-04-30 17:17:09 -04003052 else {
3053 if (target)
3054 dentry_unhash(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
Al Virod83c49f2010-04-30 17:17:09 -04003056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 if (target) {
Al Virod83c49f2010-04-30 17:17:09 -04003058 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 target->i_flags |= S_DEAD;
Al Virod83c49f2010-04-30 17:17:09 -04003060 dont_mount(new_dentry);
3061 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003062 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 if (d_unhashed(new_dentry))
3064 d_rehash(new_dentry);
3065 dput(new_dentry);
3066 }
Stephen Smalleye31e14e2005-09-09 13:01:45 -07003067 if (!error)
Mark Fasheh349457c2006-09-08 14:22:21 -07003068 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3069 d_move(old_dentry,new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 return error;
3071}
3072
Adrian Bunk75c96f82005-05-05 16:16:09 -07003073static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
3074 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075{
3076 struct inode *target;
3077 int error;
3078
3079 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3080 if (error)
3081 return error;
3082
3083 dget(new_dentry);
3084 target = new_dentry->d_inode;
3085 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003086 mutex_lock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
3088 error = -EBUSY;
3089 else
3090 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3091 if (!error) {
Al Virobec10522010-03-03 14:12:08 -05003092 if (target)
Al Virod83c49f2010-04-30 17:17:09 -04003093 dont_mount(new_dentry);
Mark Fasheh349457c2006-09-08 14:22:21 -07003094 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 d_move(old_dentry, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 }
3097 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003098 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 dput(new_dentry);
3100 return error;
3101}
3102
3103int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
3104 struct inode *new_dir, struct dentry *new_dentry)
3105{
3106 int error;
3107 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
Eric Paris59b0df22010-02-08 12:53:52 -05003108 const unsigned char *old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109
3110 if (old_dentry->d_inode == new_dentry->d_inode)
3111 return 0;
3112
3113 error = may_delete(old_dir, old_dentry, is_dir);
3114 if (error)
3115 return error;
3116
3117 if (!new_dentry->d_inode)
Miklos Szeredia95164d2008-07-30 15:08:48 +02003118 error = may_create(new_dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 else
3120 error = may_delete(new_dir, new_dentry, is_dir);
3121 if (error)
3122 return error;
3123
Al Viroacfa4382008-12-04 10:06:33 -05003124 if (!old_dir->i_op->rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 return -EPERM;
3126
Robert Love0eeca282005-07-12 17:06:03 -04003127 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
3128
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 if (is_dir)
3130 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
3131 else
3132 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
Al Viro123df292009-12-25 04:57:57 -05003133 if (!error)
3134 fsnotify_move(old_dir, new_dir, old_name, is_dir,
Al Viro5a190ae2007-06-07 12:19:32 -04003135 new_dentry->d_inode, old_dentry);
Robert Love0eeca282005-07-12 17:06:03 -04003136 fsnotify_oldname_free(old_name);
3137
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 return error;
3139}
3140
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003141SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
3142 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143{
Al Viro2ad94ae2008-07-21 09:32:51 -04003144 struct dentry *old_dir, *new_dir;
3145 struct dentry *old_dentry, *new_dentry;
3146 struct dentry *trap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 struct nameidata oldnd, newnd;
Al Viro2ad94ae2008-07-21 09:32:51 -04003148 char *from;
3149 char *to;
3150 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151
Al Viro2ad94ae2008-07-21 09:32:51 -04003152 error = user_path_parent(olddfd, oldname, &oldnd, &from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 if (error)
3154 goto exit;
3155
Al Viro2ad94ae2008-07-21 09:32:51 -04003156 error = user_path_parent(newdfd, newname, &newnd, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 if (error)
3158 goto exit1;
3159
3160 error = -EXDEV;
Jan Blunck4ac91372008-02-14 19:34:32 -08003161 if (oldnd.path.mnt != newnd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 goto exit2;
3163
Jan Blunck4ac91372008-02-14 19:34:32 -08003164 old_dir = oldnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 error = -EBUSY;
3166 if (oldnd.last_type != LAST_NORM)
3167 goto exit2;
3168
Jan Blunck4ac91372008-02-14 19:34:32 -08003169 new_dir = newnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 if (newnd.last_type != LAST_NORM)
3171 goto exit2;
3172
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003173 oldnd.flags &= ~LOOKUP_PARENT;
3174 newnd.flags &= ~LOOKUP_PARENT;
OGAWA Hirofumi4e9ed2f2008-10-16 07:50:29 +09003175 newnd.flags |= LOOKUP_RENAME_TARGET;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003176
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 trap = lock_rename(new_dir, old_dir);
3178
Christoph Hellwig49705b72005-11-08 21:35:06 -08003179 old_dentry = lookup_hash(&oldnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 error = PTR_ERR(old_dentry);
3181 if (IS_ERR(old_dentry))
3182 goto exit3;
3183 /* source must exist */
3184 error = -ENOENT;
3185 if (!old_dentry->d_inode)
3186 goto exit4;
3187 /* unless the source is a directory trailing slashes give -ENOTDIR */
3188 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
3189 error = -ENOTDIR;
3190 if (oldnd.last.name[oldnd.last.len])
3191 goto exit4;
3192 if (newnd.last.name[newnd.last.len])
3193 goto exit4;
3194 }
3195 /* source should not be ancestor of target */
3196 error = -EINVAL;
3197 if (old_dentry == trap)
3198 goto exit4;
Christoph Hellwig49705b72005-11-08 21:35:06 -08003199 new_dentry = lookup_hash(&newnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 error = PTR_ERR(new_dentry);
3201 if (IS_ERR(new_dentry))
3202 goto exit4;
3203 /* target should not be an ancestor of source */
3204 error = -ENOTEMPTY;
3205 if (new_dentry == trap)
3206 goto exit5;
3207
Dave Hansen9079b1e2008-02-15 14:37:49 -08003208 error = mnt_want_write(oldnd.path.mnt);
3209 if (error)
3210 goto exit5;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003211 error = security_path_rename(&oldnd.path, old_dentry,
3212 &newnd.path, new_dentry);
3213 if (error)
3214 goto exit6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 error = vfs_rename(old_dir->d_inode, old_dentry,
3216 new_dir->d_inode, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003217exit6:
Dave Hansen9079b1e2008-02-15 14:37:49 -08003218 mnt_drop_write(oldnd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219exit5:
3220 dput(new_dentry);
3221exit4:
3222 dput(old_dentry);
3223exit3:
3224 unlock_rename(new_dir, old_dir);
3225exit2:
Jan Blunck1d957f92008-02-14 19:34:35 -08003226 path_put(&newnd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04003227 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003229 path_put(&oldnd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 putname(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04003231exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 return error;
3233}
3234
Heiko Carstensa26eab22009-01-14 14:14:17 +01003235SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003236{
3237 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
3238}
3239
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
3241{
3242 int len;
3243
3244 len = PTR_ERR(link);
3245 if (IS_ERR(link))
3246 goto out;
3247
3248 len = strlen(link);
3249 if (len > (unsigned) buflen)
3250 len = buflen;
3251 if (copy_to_user(buffer, link, len))
3252 len = -EFAULT;
3253out:
3254 return len;
3255}
3256
3257/*
3258 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
3259 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
3260 * using) it for any given inode is up to filesystem.
3261 */
3262int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3263{
3264 struct nameidata nd;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003265 void *cookie;
Marcin Slusarz694a1762008-06-09 16:40:37 -07003266 int res;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003267
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 nd.depth = 0;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003269 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
Marcin Slusarz694a1762008-06-09 16:40:37 -07003270 if (IS_ERR(cookie))
3271 return PTR_ERR(cookie);
3272
3273 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
3274 if (dentry->d_inode->i_op->put_link)
3275 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3276 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277}
3278
3279int vfs_follow_link(struct nameidata *nd, const char *link)
3280{
3281 return __vfs_follow_link(nd, link);
3282}
3283
3284/* get the link contents into pagecache */
3285static char *page_getlink(struct dentry * dentry, struct page **ppage)
3286{
Duane Griffinebd09ab2008-12-19 20:47:12 +00003287 char *kaddr;
3288 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 struct address_space *mapping = dentry->d_inode->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -07003290 page = read_mapping_page(mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 if (IS_ERR(page))
Nick Piggin6fe69002007-05-06 14:49:04 -07003292 return (char*)page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 *ppage = page;
Duane Griffinebd09ab2008-12-19 20:47:12 +00003294 kaddr = kmap(page);
3295 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3296 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297}
3298
3299int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3300{
3301 struct page *page = NULL;
3302 char *s = page_getlink(dentry, &page);
3303 int res = vfs_readlink(dentry,buffer,buflen,s);
3304 if (page) {
3305 kunmap(page);
3306 page_cache_release(page);
3307 }
3308 return res;
3309}
3310
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003311void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003313 struct page *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 nd_set_link(nd, page_getlink(dentry, &page));
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003315 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316}
3317
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003318void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003320 struct page *page = cookie;
3321
3322 if (page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 kunmap(page);
3324 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 }
3326}
3327
Nick Piggin54566b22009-01-04 12:00:53 -08003328/*
3329 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
3330 */
3331int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332{
3333 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003334 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07003335 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08003336 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 char *kaddr;
Nick Piggin54566b22009-01-04 12:00:53 -08003338 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
3339 if (nofs)
3340 flags |= AOP_FLAG_NOFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341
NeilBrown7e53cac2006-03-25 03:07:57 -08003342retry:
Nick Pigginafddba42007-10-16 01:25:01 -07003343 err = pagecache_write_begin(NULL, mapping, 0, len-1,
Nick Piggin54566b22009-01-04 12:00:53 -08003344 flags, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07003346 goto fail;
3347
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 kaddr = kmap_atomic(page, KM_USER0);
3349 memcpy(kaddr, symname, len-1);
3350 kunmap_atomic(kaddr, KM_USER0);
Nick Pigginafddba42007-10-16 01:25:01 -07003351
3352 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3353 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 if (err < 0)
3355 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07003356 if (err < len-1)
3357 goto retry;
3358
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 mark_inode_dirty(inode);
3360 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361fail:
3362 return err;
3363}
3364
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003365int page_symlink(struct inode *inode, const char *symname, int len)
3366{
3367 return __page_symlink(inode, symname, len,
Nick Piggin54566b22009-01-04 12:00:53 -08003368 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003369}
3370
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003371const struct inode_operations page_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 .readlink = generic_readlink,
3373 .follow_link = page_follow_link_light,
3374 .put_link = page_put_link,
3375};
3376
Al Viro2d8f3032008-07-22 09:59:21 -04003377EXPORT_SYMBOL(user_path_at);
David Howellscc53ce52011-01-14 18:45:26 +00003378EXPORT_SYMBOL(follow_down_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379EXPORT_SYMBOL(follow_down);
3380EXPORT_SYMBOL(follow_up);
3381EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
3382EXPORT_SYMBOL(getname);
3383EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384EXPORT_SYMBOL(lookup_one_len);
3385EXPORT_SYMBOL(page_follow_link_light);
3386EXPORT_SYMBOL(page_put_link);
3387EXPORT_SYMBOL(page_readlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003388EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389EXPORT_SYMBOL(page_symlink);
3390EXPORT_SYMBOL(page_symlink_inode_operations);
Al Viroc9c6cac2011-02-16 15:15:47 -05003391EXPORT_SYMBOL(kern_path_parent);
Al Virod1811462008-08-02 00:49:18 -04003392EXPORT_SYMBOL(kern_path);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07003393EXPORT_SYMBOL(vfs_path_lookup);
Al Virof419a2e2008-07-22 00:07:17 -04003394EXPORT_SYMBOL(inode_permission);
Christoph Hellwig8c744fb2005-11-08 21:35:04 -08003395EXPORT_SYMBOL(file_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396EXPORT_SYMBOL(unlock_rename);
3397EXPORT_SYMBOL(vfs_create);
3398EXPORT_SYMBOL(vfs_follow_link);
3399EXPORT_SYMBOL(vfs_link);
3400EXPORT_SYMBOL(vfs_mkdir);
3401EXPORT_SYMBOL(vfs_mknod);
3402EXPORT_SYMBOL(generic_permission);
3403EXPORT_SYMBOL(vfs_readlink);
3404EXPORT_SYMBOL(vfs_rename);
3405EXPORT_SYMBOL(vfs_rmdir);
3406EXPORT_SYMBOL(vfs_symlink);
3407EXPORT_SYMBOL(vfs_unlink);
3408EXPORT_SYMBOL(dentry_unhash);
3409EXPORT_SYMBOL(generic_readlink);