blob: abc8d2df121cb414e2cbdebbb9c6a8dfa98bbf3d [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
139char * getname(const char __user * filename)
140{
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) {
150 __putname(tmp);
151 result = ERR_PTR(retval);
152 }
153 }
154 audit_getname(result);
155 return result;
156}
157
158#ifdef CONFIG_AUDITSYSCALL
159void putname(const char *name)
160{
Al Viro5ac3a9c2006-07-16 06:38:45 -0400161 if (unlikely(!audit_dummy_context()))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 audit_putname(name);
163 else
164 __putname(name);
165}
166EXPORT_SYMBOL(putname);
167#endif
168
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700169/*
170 * This does basic POSIX ACL permission checking
171 */
Nick Pigginb74c79e2011-01-07 17:49:58 +1100172static int acl_permission_check(struct inode *inode, int mask, unsigned int flags,
173 int (*check_acl)(struct inode *inode, int mask, unsigned int flags))
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700174{
175 umode_t mode = inode->i_mode;
176
177 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
178
179 if (current_fsuid() == inode->i_uid)
180 mode >>= 6;
181 else {
182 if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
Nick Pigginb74c79e2011-01-07 17:49:58 +1100183 int error = check_acl(inode, mask, flags);
184 if (error != -EAGAIN)
185 return error;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700186 }
187
188 if (in_group_p(inode->i_gid))
189 mode >>= 3;
190 }
191
192 /*
193 * If the DACs are ok we don't need any capability check.
194 */
195 if ((mask & ~mode) == 0)
196 return 0;
197 return -EACCES;
198}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200/**
Nick Pigginb74c79e2011-01-07 17:49:58 +1100201 * generic_permission - check for access rights on a Posix-like filesystem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 * @inode: inode to check access rights for
203 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
204 * @check_acl: optional callback to check for Posix ACLs
Randy Dunlap39191622011-01-08 19:36:21 -0800205 * @flags: IPERM_FLAG_ flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 *
207 * Used to check for read/write/execute permissions on a file.
208 * We use "fsuid" for this, letting us set arbitrary permissions
209 * for filesystem access without changing the "normal" uids which
Nick Pigginb74c79e2011-01-07 17:49:58 +1100210 * are used for other things.
211 *
212 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
213 * request cannot be satisfied (eg. requires blocking or too much complexity).
214 * It would then be called again in ref-walk mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 */
Nick Pigginb74c79e2011-01-07 17:49:58 +1100216int generic_permission(struct inode *inode, int mask, unsigned int flags,
217 int (*check_acl)(struct inode *inode, int mask, unsigned int flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700219 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 /*
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700222 * Do the basic POSIX ACL permission checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 */
Nick Pigginb74c79e2011-01-07 17:49:58 +1100224 ret = acl_permission_check(inode, mask, flags, check_acl);
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700225 if (ret != -EACCES)
226 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 /*
229 * Read/write DACs are always overridable.
230 * Executable DACs are overridable if at least one exec bit is set.
231 */
Miklos Szeredif696a362008-07-31 13:41:58 +0200232 if (!(mask & MAY_EXEC) || execute_ok(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (capable(CAP_DAC_OVERRIDE))
234 return 0;
235
236 /*
237 * Searching includes executable on directories, else just read.
238 */
Serge E. Hallyn7ea66002009-12-29 14:50:19 -0600239 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
241 if (capable(CAP_DAC_READ_SEARCH))
242 return 0;
243
244 return -EACCES;
245}
246
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200247/**
248 * inode_permission - check for access rights to a given inode
249 * @inode: inode to check permission on
250 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
251 *
252 * Used to check for read/write/execute permissions on an inode.
253 * We use "fsuid" for this, letting us set arbitrary permissions
254 * for filesystem access without changing the "normal" uids which
255 * are used for other things.
256 */
Al Virof419a2e2008-07-22 00:07:17 -0400257int inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
Al Viroe6305c42008-07-15 21:03:57 -0400259 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 if (mask & MAY_WRITE) {
Miklos Szeredi22590e42007-10-16 23:27:08 -0700262 umode_t mode = inode->i_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 /*
265 * Nobody gets write access to a read-only fs.
266 */
267 if (IS_RDONLY(inode) &&
268 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
269 return -EROFS;
270
271 /*
272 * Nobody gets write access to an immutable file.
273 */
274 if (IS_IMMUTABLE(inode))
275 return -EACCES;
276 }
277
Al Viroacfa4382008-12-04 10:06:33 -0500278 if (inode->i_op->permission)
Nick Pigginb74c79e2011-01-07 17:49:58 +1100279 retval = inode->i_op->permission(inode, mask, 0);
Miklos Szeredif696a362008-07-31 13:41:58 +0200280 else
Nick Pigginb74c79e2011-01-07 17:49:58 +1100281 retval = generic_permission(inode, mask, 0,
282 inode->i_op->check_acl);
Miklos Szeredif696a362008-07-31 13:41:58 +0200283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if (retval)
285 return retval;
286
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700287 retval = devcgroup_inode_permission(inode, mask);
288 if (retval)
289 return retval;
290
Eric Parisd09ca732010-07-23 11:43:57 -0400291 return security_inode_permission(inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800294/**
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800295 * file_permission - check for additional access rights to a given file
296 * @file: file to check access rights for
297 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
298 *
299 * Used to check for read/write/execute permissions on an already opened
300 * file.
301 *
302 * Note:
303 * Do not use this function in new code. All access checks should
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200304 * be done using inode_permission().
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800305 */
306int file_permission(struct file *file, int mask)
307{
Al Virof419a2e2008-07-22 00:07:17 -0400308 return inode_permission(file->f_path.dentry->d_inode, mask);
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800309}
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311/*
312 * get_write_access() gets write permission for a file.
313 * put_write_access() releases this write permission.
314 * This is used for regular files.
315 * We cannot support write (and maybe mmap read-write shared) accesses and
316 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
317 * can have the following values:
318 * 0: no writers, no VM_DENYWRITE mappings
319 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
320 * > 0: (i_writecount) users are writing to the file.
321 *
322 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
323 * except for the cases where we don't hold i_writecount yet. Then we need to
324 * use {get,deny}_write_access() - these functions check the sign and refuse
325 * to do the change if sign is wrong. Exclusion between them is provided by
326 * the inode->i_lock spinlock.
327 */
328
329int get_write_access(struct inode * inode)
330{
331 spin_lock(&inode->i_lock);
332 if (atomic_read(&inode->i_writecount) < 0) {
333 spin_unlock(&inode->i_lock);
334 return -ETXTBSY;
335 }
336 atomic_inc(&inode->i_writecount);
337 spin_unlock(&inode->i_lock);
338
339 return 0;
340}
341
342int deny_write_access(struct file * file)
343{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800344 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 spin_lock(&inode->i_lock);
347 if (atomic_read(&inode->i_writecount) > 0) {
348 spin_unlock(&inode->i_lock);
349 return -ETXTBSY;
350 }
351 atomic_dec(&inode->i_writecount);
352 spin_unlock(&inode->i_lock);
353
354 return 0;
355}
356
Jan Blunck1d957f92008-02-14 19:34:35 -0800357/**
Jan Blunck5dd784d2008-02-14 19:34:38 -0800358 * path_get - get a reference to a path
359 * @path: path to get the reference to
360 *
361 * Given a path increment the reference count to the dentry and the vfsmount.
362 */
363void path_get(struct path *path)
364{
365 mntget(path->mnt);
366 dget(path->dentry);
367}
368EXPORT_SYMBOL(path_get);
369
370/**
Jan Blunck1d957f92008-02-14 19:34:35 -0800371 * path_put - put a reference to a path
372 * @path: path to put the reference to
373 *
374 * Given a path decrement the reference count to the dentry and the vfsmount.
375 */
376void path_put(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Jan Blunck1d957f92008-02-14 19:34:35 -0800378 dput(path->dentry);
379 mntput(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380}
Jan Blunck1d957f92008-02-14 19:34:35 -0800381EXPORT_SYMBOL(path_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Trond Myklebust834f2a42005-10-18 14:20:16 -0700383/**
Nick Piggin31e6b012011-01-07 17:49:52 +1100384 * nameidata_drop_rcu - drop this nameidata out of rcu-walk
385 * @nd: nameidata pathwalk data to drop
Randy Dunlap39191622011-01-08 19:36:21 -0800386 * Returns: 0 on success, -ECHILD on failure
Nick Piggin31e6b012011-01-07 17:49:52 +1100387 *
388 * Path walking has 2 modes, rcu-walk and ref-walk (see
389 * Documentation/filesystems/path-lookup.txt). __drop_rcu* functions attempt
390 * to drop out of rcu-walk mode and take normal reference counts on dentries
391 * and vfsmounts to transition to rcu-walk mode. __drop_rcu* functions take
392 * refcounts at the last known good point before rcu-walk got stuck, so
393 * ref-walk may continue from there. If this is not successful (eg. a seqcount
394 * has changed), then failure is returned and path walk restarts from the
395 * beginning in ref-walk mode.
396 *
397 * nameidata_drop_rcu attempts to drop the current nd->path and nd->root into
398 * ref-walk. Must be called from rcu-walk context.
399 */
400static int nameidata_drop_rcu(struct nameidata *nd)
401{
402 struct fs_struct *fs = current->fs;
403 struct dentry *dentry = nd->path.dentry;
Al Viro5b6ca022011-03-09 23:04:47 -0500404 int want_root = 0;
Nick Piggin31e6b012011-01-07 17:49:52 +1100405
406 BUG_ON(!(nd->flags & LOOKUP_RCU));
Al Viro5b6ca022011-03-09 23:04:47 -0500407 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
408 want_root = 1;
Nick Piggin31e6b012011-01-07 17:49:52 +1100409 spin_lock(&fs->lock);
410 if (nd->root.mnt != fs->root.mnt ||
411 nd->root.dentry != fs->root.dentry)
412 goto err_root;
413 }
414 spin_lock(&dentry->d_lock);
415 if (!__d_rcu_to_refcount(dentry, nd->seq))
416 goto err;
417 BUG_ON(nd->inode != dentry->d_inode);
418 spin_unlock(&dentry->d_lock);
Al Viro5b6ca022011-03-09 23:04:47 -0500419 if (want_root) {
Nick Piggin31e6b012011-01-07 17:49:52 +1100420 path_get(&nd->root);
421 spin_unlock(&fs->lock);
422 }
423 mntget(nd->path.mnt);
424
425 rcu_read_unlock();
426 br_read_unlock(vfsmount_lock);
427 nd->flags &= ~LOOKUP_RCU;
428 return 0;
429err:
430 spin_unlock(&dentry->d_lock);
431err_root:
Al Viro5b6ca022011-03-09 23:04:47 -0500432 if (want_root)
Nick Piggin31e6b012011-01-07 17:49:52 +1100433 spin_unlock(&fs->lock);
434 return -ECHILD;
435}
436
437/* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing. */
438static inline int nameidata_drop_rcu_maybe(struct nameidata *nd)
439{
440 if (nd->flags & LOOKUP_RCU)
441 return nameidata_drop_rcu(nd);
442 return 0;
443}
444
445/**
446 * nameidata_dentry_drop_rcu - drop nameidata and dentry out of rcu-walk
447 * @nd: nameidata pathwalk data to drop
448 * @dentry: dentry to drop
Randy Dunlap39191622011-01-08 19:36:21 -0800449 * Returns: 0 on success, -ECHILD on failure
Nick Piggin31e6b012011-01-07 17:49:52 +1100450 *
451 * nameidata_dentry_drop_rcu attempts to drop the current nd->path and nd->root,
452 * and dentry into ref-walk. @dentry must be a path found by a do_lookup call on
453 * @nd. Must be called from rcu-walk context.
454 */
455static int nameidata_dentry_drop_rcu(struct nameidata *nd, struct dentry *dentry)
456{
457 struct fs_struct *fs = current->fs;
458 struct dentry *parent = nd->path.dentry;
Al Viro5b6ca022011-03-09 23:04:47 -0500459 int want_root = 0;
Nick Piggin31e6b012011-01-07 17:49:52 +1100460
461 BUG_ON(!(nd->flags & LOOKUP_RCU));
Al Viro5b6ca022011-03-09 23:04:47 -0500462 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
463 want_root = 1;
Nick Piggin31e6b012011-01-07 17:49:52 +1100464 spin_lock(&fs->lock);
465 if (nd->root.mnt != fs->root.mnt ||
466 nd->root.dentry != fs->root.dentry)
467 goto err_root;
468 }
469 spin_lock(&parent->d_lock);
470 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
471 if (!__d_rcu_to_refcount(dentry, nd->seq))
472 goto err;
473 /*
474 * If the sequence check on the child dentry passed, then the child has
475 * not been removed from its parent. This means the parent dentry must
476 * be valid and able to take a reference at this point.
477 */
478 BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
479 BUG_ON(!parent->d_count);
480 parent->d_count++;
481 spin_unlock(&dentry->d_lock);
482 spin_unlock(&parent->d_lock);
Al Viro5b6ca022011-03-09 23:04:47 -0500483 if (want_root) {
Nick Piggin31e6b012011-01-07 17:49:52 +1100484 path_get(&nd->root);
485 spin_unlock(&fs->lock);
486 }
487 mntget(nd->path.mnt);
488
489 rcu_read_unlock();
490 br_read_unlock(vfsmount_lock);
491 nd->flags &= ~LOOKUP_RCU;
492 return 0;
493err:
494 spin_unlock(&dentry->d_lock);
495 spin_unlock(&parent->d_lock);
496err_root:
Al Viro5b6ca022011-03-09 23:04:47 -0500497 if (want_root)
Nick Piggin31e6b012011-01-07 17:49:52 +1100498 spin_unlock(&fs->lock);
499 return -ECHILD;
500}
501
502/* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing. */
503static inline int nameidata_dentry_drop_rcu_maybe(struct nameidata *nd, struct dentry *dentry)
504{
Al Viroa7472ba2011-03-04 14:39:30 -0500505 if (nd->flags & LOOKUP_RCU) {
506 if (unlikely(nameidata_dentry_drop_rcu(nd, dentry))) {
507 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -0500508 if (!(nd->flags & LOOKUP_ROOT))
509 nd->root.mnt = NULL;
Al Viroa7472ba2011-03-04 14:39:30 -0500510 rcu_read_unlock();
511 br_read_unlock(vfsmount_lock);
512 return -ECHILD;
513 }
514 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100515 return 0;
516}
517
518/**
519 * nameidata_drop_rcu_last - drop nameidata ending path walk out of rcu-walk
520 * @nd: nameidata pathwalk data to drop
Randy Dunlap39191622011-01-08 19:36:21 -0800521 * Returns: 0 on success, -ECHILD on failure
Nick Piggin31e6b012011-01-07 17:49:52 +1100522 *
523 * nameidata_drop_rcu_last attempts to drop the current nd->path into ref-walk.
524 * nd->path should be the final element of the lookup, so nd->root is discarded.
525 * Must be called from rcu-walk context.
526 */
527static int nameidata_drop_rcu_last(struct nameidata *nd)
528{
529 struct dentry *dentry = nd->path.dentry;
530
531 BUG_ON(!(nd->flags & LOOKUP_RCU));
532 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -0500533 if (!(nd->flags & LOOKUP_ROOT))
534 nd->root.mnt = NULL;
Nick Piggin31e6b012011-01-07 17:49:52 +1100535 spin_lock(&dentry->d_lock);
536 if (!__d_rcu_to_refcount(dentry, nd->seq))
537 goto err_unlock;
538 BUG_ON(nd->inode != dentry->d_inode);
539 spin_unlock(&dentry->d_lock);
540
541 mntget(nd->path.mnt);
542
543 rcu_read_unlock();
544 br_read_unlock(vfsmount_lock);
545
546 return 0;
547
548err_unlock:
549 spin_unlock(&dentry->d_lock);
550 rcu_read_unlock();
551 br_read_unlock(vfsmount_lock);
552 return -ECHILD;
553}
554
Nick Piggin31e6b012011-01-07 17:49:52 +1100555/**
Trond Myklebust834f2a42005-10-18 14:20:16 -0700556 * release_open_intent - free up open intent resources
557 * @nd: pointer to nameidata
558 */
559void release_open_intent(struct nameidata *nd)
560{
Linus Torvalds2dab5972011-02-11 15:53:38 -0800561 struct file *file = nd->intent.open.file;
562
563 if (file && !IS_ERR(file)) {
564 if (file->f_path.dentry == NULL)
565 put_filp(file);
566 else
567 fput(file);
568 }
Trond Myklebust834f2a42005-10-18 14:20:16 -0700569}
570
Al Virof60aef72011-02-15 01:35:28 -0500571static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
Nick Piggin34286d62011-01-07 17:49:57 +1100572{
Al Virof60aef72011-02-15 01:35:28 -0500573 return dentry->d_op->d_revalidate(dentry, nd);
Nick Piggin34286d62011-01-07 17:49:57 +1100574}
575
Al Virof5e1c1c2011-02-15 01:32:55 -0500576static struct dentry *
Ian Kentbcdc5e02006-09-27 01:50:44 -0700577do_revalidate(struct dentry *dentry, struct nameidata *nd)
578{
Al Virof5e1c1c2011-02-15 01:32:55 -0500579 int status = d_revalidate(dentry, nd);
Ian Kentbcdc5e02006-09-27 01:50:44 -0700580 if (unlikely(status <= 0)) {
581 /*
582 * The dentry failed validation.
583 * If d_revalidate returned 0 attempt to invalidate
584 * the dentry otherwise d_revalidate is asking us
585 * to return a fail status.
586 */
Nick Piggin34286d62011-01-07 17:49:57 +1100587 if (status < 0) {
Al Virof5e1c1c2011-02-15 01:32:55 -0500588 dput(dentry);
Nick Piggin34286d62011-01-07 17:49:57 +1100589 dentry = ERR_PTR(status);
Al Virof5e1c1c2011-02-15 01:32:55 -0500590 } else if (!d_invalidate(dentry)) {
591 dput(dentry);
592 dentry = NULL;
Ian Kentbcdc5e02006-09-27 01:50:44 -0700593 }
594 }
595 return dentry;
596}
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598/*
Al Viro16c2cd72011-02-22 15:50:10 -0500599 * handle_reval_path - force revalidation of a dentry
Jeff Layton39159de2009-12-07 12:01:50 -0500600 *
601 * In some situations the path walking code will trust dentries without
602 * revalidating them. This causes problems for filesystems that depend on
603 * d_revalidate to handle file opens (e.g. NFSv4). When FS_REVAL_DOT is set
604 * (which indicates that it's possible for the dentry to go stale), force
605 * a d_revalidate call before proceeding.
606 *
607 * Returns 0 if the revalidation was successful. If the revalidation fails,
608 * either return the error returned by d_revalidate or -ESTALE if the
609 * revalidation it just returned 0. If d_revalidate returns 0, we attempt to
610 * invalidate the dentry. It's up to the caller to handle putting references
611 * to the path if necessary.
612 */
Al Viro16c2cd72011-02-22 15:50:10 -0500613static inline int handle_reval_path(struct nameidata *nd)
Jeff Layton39159de2009-12-07 12:01:50 -0500614{
Al Viro16c2cd72011-02-22 15:50:10 -0500615 struct dentry *dentry = nd->path.dentry;
Jeff Layton39159de2009-12-07 12:01:50 -0500616 int status;
Jeff Layton39159de2009-12-07 12:01:50 -0500617
Al Viro16c2cd72011-02-22 15:50:10 -0500618 if (likely(!(nd->flags & LOOKUP_JUMPED)))
Jeff Layton39159de2009-12-07 12:01:50 -0500619 return 0;
620
Al Viro16c2cd72011-02-22 15:50:10 -0500621 if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
622 return 0;
623
624 if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
625 return 0;
626
627 /* Note: we do not d_invalidate() */
Nick Piggin34286d62011-01-07 17:49:57 +1100628 status = d_revalidate(dentry, nd);
Jeff Layton39159de2009-12-07 12:01:50 -0500629 if (status > 0)
630 return 0;
631
Al Viro16c2cd72011-02-22 15:50:10 -0500632 if (!status)
Jeff Layton39159de2009-12-07 12:01:50 -0500633 status = -ESTALE;
Al Viro16c2cd72011-02-22 15:50:10 -0500634
Jeff Layton39159de2009-12-07 12:01:50 -0500635 return status;
636}
637
638/*
Al Virob75b5082009-12-16 01:01:38 -0500639 * Short-cut version of permission(), for calling on directories
640 * during pathname resolution. Combines parts of permission()
641 * and generic_permission(), and tests ONLY for MAY_EXEC permission.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 *
643 * If appropriate, check DAC only. If not appropriate, or
Al Virob75b5082009-12-16 01:01:38 -0500644 * short-cut DAC fails, then call ->permission() to do more
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 * complete permission check.
646 */
Nick Pigginb74c79e2011-01-07 17:49:58 +1100647static inline int exec_permission(struct inode *inode, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700649 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Linus Torvaldscb9179e2009-08-28 11:08:31 -0700651 if (inode->i_op->permission) {
Nick Pigginb74c79e2011-01-07 17:49:58 +1100652 ret = inode->i_op->permission(inode, MAY_EXEC, flags);
653 } else {
654 ret = acl_permission_check(inode, MAY_EXEC, flags,
655 inode->i_op->check_acl);
Linus Torvaldscb9179e2009-08-28 11:08:31 -0700656 }
Nick Pigginb74c79e2011-01-07 17:49:58 +1100657 if (likely(!ret))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 goto ok;
Nick Pigginb74c79e2011-01-07 17:49:58 +1100659 if (ret == -ECHILD)
Nick Piggin31e6b012011-01-07 17:49:52 +1100660 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Linus Torvaldsf1ac9f62009-08-28 10:53:56 -0700662 if (capable(CAP_DAC_OVERRIDE) || capable(CAP_DAC_READ_SEARCH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 goto ok;
664
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700665 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666ok:
Nick Pigginb74c79e2011-01-07 17:49:58 +1100667 return security_inode_exec_permission(inode, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
669
Al Viro2a737872009-04-07 11:49:53 -0400670static __always_inline void set_root(struct nameidata *nd)
671{
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200672 if (!nd->root.mnt)
673 get_fs_root(current->fs, &nd->root);
Al Viro2a737872009-04-07 11:49:53 -0400674}
675
Al Viro6de88d72009-08-09 01:41:57 +0400676static int link_path_walk(const char *, struct nameidata *);
677
Nick Piggin31e6b012011-01-07 17:49:52 +1100678static __always_inline void set_root_rcu(struct nameidata *nd)
679{
680 if (!nd->root.mnt) {
681 struct fs_struct *fs = current->fs;
Nick Pigginc28cc362011-01-07 17:49:53 +1100682 unsigned seq;
683
684 do {
685 seq = read_seqcount_begin(&fs->seq);
686 nd->root = fs->root;
687 } while (read_seqcount_retry(&fs->seq, seq));
Nick Piggin31e6b012011-01-07 17:49:52 +1100688 }
689}
690
Arjan van de Venf1662352006-01-14 13:21:31 -0800691static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
Nick Piggin31e6b012011-01-07 17:49:52 +1100693 int ret;
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if (IS_ERR(link))
696 goto fail;
697
698 if (*link == '/') {
Al Viro2a737872009-04-07 11:49:53 -0400699 set_root(nd);
Jan Blunck1d957f92008-02-14 19:34:35 -0800700 path_put(&nd->path);
Al Viro2a737872009-04-07 11:49:53 -0400701 nd->path = nd->root;
702 path_get(&nd->root);
Al Viro16c2cd72011-02-22 15:50:10 -0500703 nd->flags |= LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100705 nd->inode = nd->path.dentry->d_inode;
Christoph Hellwigb4091d52008-11-05 15:07:21 +0100706
Nick Piggin31e6b012011-01-07 17:49:52 +1100707 ret = link_path_walk(link, nd);
708 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709fail:
Jan Blunck1d957f92008-02-14 19:34:35 -0800710 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 return PTR_ERR(link);
712}
713
Jan Blunck1d957f92008-02-14 19:34:35 -0800714static void path_put_conditional(struct path *path, struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700715{
716 dput(path->dentry);
Jan Blunck4ac91372008-02-14 19:34:32 -0800717 if (path->mnt != nd->path.mnt)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700718 mntput(path->mnt);
719}
720
Nick Piggin7b9337a2011-01-14 08:42:43 +0000721static inline void path_to_nameidata(const struct path *path,
722 struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700723{
Nick Piggin31e6b012011-01-07 17:49:52 +1100724 if (!(nd->flags & LOOKUP_RCU)) {
725 dput(nd->path.dentry);
726 if (nd->path.mnt != path->mnt)
727 mntput(nd->path.mnt);
Huang Shijie9a229682010-04-02 17:37:13 +0800728 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100729 nd->path.mnt = path->mnt;
Jan Blunck4ac91372008-02-14 19:34:32 -0800730 nd->path.dentry = path->dentry;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700731}
732
Al Virodef4af32009-12-26 08:37:05 -0500733static __always_inline int
Nick Piggin7b9337a2011-01-14 08:42:43 +0000734__do_follow_link(const struct path *link, struct nameidata *nd, void **p)
Ian Kent051d3812006-03-27 01:14:53 -0800735{
736 int error;
Nick Piggin7b9337a2011-01-14 08:42:43 +0000737 struct dentry *dentry = link->dentry;
Ian Kent051d3812006-03-27 01:14:53 -0800738
Al Viro844a3912011-02-15 00:38:26 -0500739 BUG_ON(nd->flags & LOOKUP_RCU);
740
Nick Piggin7b9337a2011-01-14 08:42:43 +0000741 touch_atime(link->mnt, dentry);
Ian Kent051d3812006-03-27 01:14:53 -0800742 nd_set_link(nd, NULL);
743
David Howells87556ef2011-01-14 18:46:46 +0000744 if (link->mnt == nd->path.mnt)
745 mntget(link->mnt);
Nick Piggin31e6b012011-01-07 17:49:52 +1100746
Al Viro36f3b4f2011-02-22 21:24:38 -0500747 error = security_inode_follow_link(link->dentry, nd);
748 if (error) {
749 *p = ERR_PTR(error); /* no ->put_link(), please */
750 path_put(&nd->path);
751 return error;
752 }
753
Al Viro86acdca12009-12-22 23:45:11 -0500754 nd->last_type = LAST_BIND;
Al Virodef4af32009-12-26 08:37:05 -0500755 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
756 error = PTR_ERR(*p);
757 if (!IS_ERR(*p)) {
Ian Kent051d3812006-03-27 01:14:53 -0800758 char *s = nd_get_link(nd);
759 error = 0;
760 if (s)
761 error = __vfs_follow_link(nd, s);
Al Viro16c2cd72011-02-22 15:50:10 -0500762 else if (nd->last_type == LAST_BIND)
763 nd->flags |= LOOKUP_JUMPED;
Ian Kent051d3812006-03-27 01:14:53 -0800764 }
Ian Kent051d3812006-03-27 01:14:53 -0800765 return error;
766}
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768/*
769 * This limits recursive symlink follows to 8, while
770 * limiting consecutive symlinks to 40.
771 *
772 * Without that kind of total limit, nasty chains of consecutive
773 * symlinks can cause almost arbitrarily long lookups.
774 */
Linus Torvalds3abb17e2011-02-16 08:56:55 -0800775static inline int do_follow_link(struct inode *inode, struct path *path, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
Al Virodef4af32009-12-26 08:37:05 -0500777 void *cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 int err = -ELOOP;
Al Viro844a3912011-02-15 00:38:26 -0500779
780 /* We drop rcu-walk here */
781 if (nameidata_dentry_drop_rcu_maybe(nd, path->dentry))
782 return -ECHILD;
Linus Torvalds3abb17e2011-02-16 08:56:55 -0800783 BUG_ON(inode != path->dentry->d_inode);
Al Viro844a3912011-02-15 00:38:26 -0500784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 if (current->link_count >= MAX_NESTED_LINKS)
786 goto loop;
787 if (current->total_link_count >= 40)
788 goto loop;
789 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
790 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 current->link_count++;
792 current->total_link_count++;
793 nd->depth++;
Al Virodef4af32009-12-26 08:37:05 -0500794 err = __do_follow_link(path, nd, &cookie);
795 if (!IS_ERR(cookie) && path->dentry->d_inode->i_op->put_link)
796 path->dentry->d_inode->i_op->put_link(path->dentry, nd, cookie);
Al Viro258fa992009-08-09 01:32:02 +0400797 path_put(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 current->link_count--;
799 nd->depth--;
800 return err;
801loop:
Jan Blunck1d957f92008-02-14 19:34:35 -0800802 path_put_conditional(path, nd);
803 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 return err;
805}
806
Nick Piggin31e6b012011-01-07 17:49:52 +1100807static int follow_up_rcu(struct path *path)
808{
809 struct vfsmount *parent;
810 struct dentry *mountpoint;
811
812 parent = path->mnt->mnt_parent;
813 if (parent == path->mnt)
814 return 0;
815 mountpoint = path->mnt->mnt_mountpoint;
816 path->dentry = mountpoint;
817 path->mnt = parent;
818 return 1;
819}
820
Al Virobab77eb2009-04-18 03:26:48 -0400821int follow_up(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
823 struct vfsmount *parent;
824 struct dentry *mountpoint;
Nick Piggin99b7db72010-08-18 04:37:39 +1000825
826 br_read_lock(vfsmount_lock);
Al Virobab77eb2009-04-18 03:26:48 -0400827 parent = path->mnt->mnt_parent;
828 if (parent == path->mnt) {
Nick Piggin99b7db72010-08-18 04:37:39 +1000829 br_read_unlock(vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 return 0;
831 }
832 mntget(parent);
Al Virobab77eb2009-04-18 03:26:48 -0400833 mountpoint = dget(path->mnt->mnt_mountpoint);
Nick Piggin99b7db72010-08-18 04:37:39 +1000834 br_read_unlock(vfsmount_lock);
Al Virobab77eb2009-04-18 03:26:48 -0400835 dput(path->dentry);
836 path->dentry = mountpoint;
837 mntput(path->mnt);
838 path->mnt = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 return 1;
840}
841
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100842/*
David Howells9875cf82011-01-14 18:45:21 +0000843 * Perform an automount
844 * - return -EISDIR to tell follow_managed() to stop and return the path we
845 * were called with.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 */
David Howells9875cf82011-01-14 18:45:21 +0000847static int follow_automount(struct path *path, unsigned flags,
848 bool *need_mntput)
Nick Piggin31e6b012011-01-07 17:49:52 +1100849{
David Howells9875cf82011-01-14 18:45:21 +0000850 struct vfsmount *mnt;
David Howellsea5b7782011-01-14 19:10:03 +0000851 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +1100852
David Howells9875cf82011-01-14 18:45:21 +0000853 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
854 return -EREMOTE;
Al Viro463ffb22005-06-06 13:36:05 -0700855
David Howells6f45b652011-01-14 18:45:31 +0000856 /* We don't want to mount if someone supplied AT_NO_AUTOMOUNT
857 * and this is the terminal part of the path.
858 */
859 if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_CONTINUE))
860 return -EISDIR; /* we actually want to stop here */
861
David Howells9875cf82011-01-14 18:45:21 +0000862 /* We want to mount if someone is trying to open/create a file of any
863 * type under the mountpoint, wants to traverse through the mountpoint
864 * or wants to open the mounted directory.
865 *
866 * We don't want to mount if someone's just doing a stat and they've
867 * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and
868 * appended a '/' to the name.
869 */
870 if (!(flags & LOOKUP_FOLLOW) &&
871 !(flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY |
872 LOOKUP_OPEN | LOOKUP_CREATE)))
873 return -EISDIR;
874
875 current->total_link_count++;
876 if (current->total_link_count >= 40)
877 return -ELOOP;
878
879 mnt = path->dentry->d_op->d_automount(path);
880 if (IS_ERR(mnt)) {
881 /*
882 * The filesystem is allowed to return -EISDIR here to indicate
883 * it doesn't want to automount. For instance, autofs would do
884 * this so that its userspace daemon can mount on this dentry.
885 *
886 * However, we can only permit this if it's a terminal point in
887 * the path being looked up; if it wasn't then the remainder of
888 * the path is inaccessible and we should say so.
889 */
890 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_CONTINUE))
891 return -EREMOTE;
892 return PTR_ERR(mnt);
893 }
David Howellsea5b7782011-01-14 19:10:03 +0000894
David Howells9875cf82011-01-14 18:45:21 +0000895 if (!mnt) /* mount collision */
896 return 0;
897
Al Viro19a167a2011-01-17 01:35:23 -0500898 err = finish_automount(mnt, path);
David Howellsea5b7782011-01-14 19:10:03 +0000899
David Howellsea5b7782011-01-14 19:10:03 +0000900 switch (err) {
901 case -EBUSY:
902 /* Someone else made a mount here whilst we were busy */
Al Viro19a167a2011-01-17 01:35:23 -0500903 return 0;
David Howellsea5b7782011-01-14 19:10:03 +0000904 case 0:
David Howellsea5b7782011-01-14 19:10:03 +0000905 dput(path->dentry);
906 if (*need_mntput)
907 mntput(path->mnt);
908 path->mnt = mnt;
909 path->dentry = dget(mnt->mnt_root);
910 *need_mntput = true;
911 return 0;
Al Viro19a167a2011-01-17 01:35:23 -0500912 default:
913 return err;
David Howellsea5b7782011-01-14 19:10:03 +0000914 }
Al Viro19a167a2011-01-17 01:35:23 -0500915
David Howells9875cf82011-01-14 18:45:21 +0000916}
917
918/*
919 * Handle a dentry that is managed in some way.
David Howellscc53ce52011-01-14 18:45:26 +0000920 * - Flagged for transit management (autofs)
David Howells9875cf82011-01-14 18:45:21 +0000921 * - Flagged as mountpoint
922 * - Flagged as automount point
923 *
924 * This may only be called in refwalk mode.
925 *
926 * Serialization is taken care of in namespace.c
927 */
928static int follow_managed(struct path *path, unsigned flags)
929{
930 unsigned managed;
931 bool need_mntput = false;
932 int ret;
933
934 /* Given that we're not holding a lock here, we retain the value in a
935 * local variable for each dentry as we look at it so that we don't see
936 * the components of that value change under us */
937 while (managed = ACCESS_ONCE(path->dentry->d_flags),
938 managed &= DCACHE_MANAGED_DENTRY,
939 unlikely(managed != 0)) {
David Howellscc53ce52011-01-14 18:45:26 +0000940 /* Allow the filesystem to manage the transit without i_mutex
941 * being held. */
942 if (managed & DCACHE_MANAGE_TRANSIT) {
943 BUG_ON(!path->dentry->d_op);
944 BUG_ON(!path->dentry->d_op->d_manage);
David Howellsab909112011-01-14 18:46:51 +0000945 ret = path->dentry->d_op->d_manage(path->dentry,
946 false, false);
David Howellscc53ce52011-01-14 18:45:26 +0000947 if (ret < 0)
948 return ret == -EISDIR ? 0 : ret;
949 }
950
David Howells9875cf82011-01-14 18:45:21 +0000951 /* Transit to a mounted filesystem. */
952 if (managed & DCACHE_MOUNTED) {
953 struct vfsmount *mounted = lookup_mnt(path);
954 if (mounted) {
955 dput(path->dentry);
956 if (need_mntput)
957 mntput(path->mnt);
958 path->mnt = mounted;
959 path->dentry = dget(mounted->mnt_root);
960 need_mntput = true;
961 continue;
962 }
963
964 /* Something is mounted on this dentry in another
965 * namespace and/or whatever was mounted there in this
966 * namespace got unmounted before we managed to get the
967 * vfsmount_lock */
968 }
969
970 /* Handle an automount point */
971 if (managed & DCACHE_NEED_AUTOMOUNT) {
972 ret = follow_automount(path, flags, &need_mntput);
973 if (ret < 0)
974 return ret == -EISDIR ? 0 : ret;
975 continue;
976 }
977
978 /* We didn't change the current path point */
979 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 }
David Howells9875cf82011-01-14 18:45:21 +0000981 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982}
983
David Howellscc53ce52011-01-14 18:45:26 +0000984int follow_down_one(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
986 struct vfsmount *mounted;
987
Al Viro1c755af2009-04-18 14:06:57 -0400988 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -0400990 dput(path->dentry);
991 mntput(path->mnt);
992 path->mnt = mounted;
993 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 return 1;
995 }
996 return 0;
997}
998
David Howells9875cf82011-01-14 18:45:21 +0000999/*
1000 * Skip to top of mountpoint pile in rcuwalk mode. We abort the rcu-walk if we
David Howellscc53ce52011-01-14 18:45:26 +00001001 * meet a managed dentry and we're not walking to "..". True is returned to
David Howells9875cf82011-01-14 18:45:21 +00001002 * continue, false to abort.
1003 */
1004static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
1005 struct inode **inode, bool reverse_transit)
1006{
1007 while (d_mountpoint(path->dentry)) {
1008 struct vfsmount *mounted;
David Howellsab909112011-01-14 18:46:51 +00001009 if (unlikely(path->dentry->d_flags & DCACHE_MANAGE_TRANSIT) &&
1010 !reverse_transit &&
1011 path->dentry->d_op->d_manage(path->dentry, false, true) < 0)
1012 return false;
David Howells9875cf82011-01-14 18:45:21 +00001013 mounted = __lookup_mnt(path->mnt, path->dentry, 1);
1014 if (!mounted)
1015 break;
1016 path->mnt = mounted;
1017 path->dentry = mounted->mnt_root;
1018 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
1019 *inode = path->dentry->d_inode;
1020 }
1021
1022 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1023 return reverse_transit;
1024 return true;
1025}
1026
Nick Piggin31e6b012011-01-07 17:49:52 +11001027static int follow_dotdot_rcu(struct nameidata *nd)
1028{
1029 struct inode *inode = nd->inode;
1030
1031 set_root_rcu(nd);
1032
David Howells9875cf82011-01-14 18:45:21 +00001033 while (1) {
Nick Piggin31e6b012011-01-07 17:49:52 +11001034 if (nd->path.dentry == nd->root.dentry &&
1035 nd->path.mnt == nd->root.mnt) {
1036 break;
1037 }
1038 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1039 struct dentry *old = nd->path.dentry;
1040 struct dentry *parent = old->d_parent;
1041 unsigned seq;
1042
1043 seq = read_seqcount_begin(&parent->d_seq);
1044 if (read_seqcount_retry(&old->d_seq, nd->seq))
Al Viroef7562d2011-03-04 14:35:59 -05001045 goto failed;
Nick Piggin31e6b012011-01-07 17:49:52 +11001046 inode = parent->d_inode;
1047 nd->path.dentry = parent;
1048 nd->seq = seq;
1049 break;
1050 }
1051 if (!follow_up_rcu(&nd->path))
1052 break;
1053 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
1054 inode = nd->path.dentry->d_inode;
1055 }
David Howells9875cf82011-01-14 18:45:21 +00001056 __follow_mount_rcu(nd, &nd->path, &inode, true);
Nick Piggin31e6b012011-01-07 17:49:52 +11001057 nd->inode = inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001058 return 0;
Al Viroef7562d2011-03-04 14:35:59 -05001059
1060failed:
1061 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001062 if (!(nd->flags & LOOKUP_ROOT))
1063 nd->root.mnt = NULL;
Al Viroef7562d2011-03-04 14:35:59 -05001064 rcu_read_unlock();
1065 br_read_unlock(vfsmount_lock);
1066 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001067}
1068
David Howells9875cf82011-01-14 18:45:21 +00001069/*
David Howellscc53ce52011-01-14 18:45:26 +00001070 * Follow down to the covering mount currently visible to userspace. At each
1071 * point, the filesystem owning that dentry may be queried as to whether the
1072 * caller is permitted to proceed or not.
1073 *
1074 * Care must be taken as namespace_sem may be held (indicated by mounting_here
1075 * being true).
1076 */
1077int follow_down(struct path *path, bool mounting_here)
1078{
1079 unsigned managed;
1080 int ret;
1081
1082 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1083 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1084 /* Allow the filesystem to manage the transit without i_mutex
1085 * being held.
1086 *
1087 * We indicate to the filesystem if someone is trying to mount
1088 * something here. This gives autofs the chance to deny anyone
1089 * other than its daemon the right to mount on its
1090 * superstructure.
1091 *
1092 * The filesystem may sleep at this point.
1093 */
1094 if (managed & DCACHE_MANAGE_TRANSIT) {
1095 BUG_ON(!path->dentry->d_op);
1096 BUG_ON(!path->dentry->d_op->d_manage);
David Howellsab909112011-01-14 18:46:51 +00001097 ret = path->dentry->d_op->d_manage(
1098 path->dentry, mounting_here, false);
David Howellscc53ce52011-01-14 18:45:26 +00001099 if (ret < 0)
1100 return ret == -EISDIR ? 0 : ret;
1101 }
1102
1103 /* Transit to a mounted filesystem. */
1104 if (managed & DCACHE_MOUNTED) {
1105 struct vfsmount *mounted = lookup_mnt(path);
1106 if (!mounted)
1107 break;
1108 dput(path->dentry);
1109 mntput(path->mnt);
1110 path->mnt = mounted;
1111 path->dentry = dget(mounted->mnt_root);
1112 continue;
1113 }
1114
1115 /* Don't handle automount points here */
1116 break;
1117 }
1118 return 0;
1119}
1120
1121/*
David Howells9875cf82011-01-14 18:45:21 +00001122 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1123 */
1124static void follow_mount(struct path *path)
1125{
1126 while (d_mountpoint(path->dentry)) {
1127 struct vfsmount *mounted = lookup_mnt(path);
1128 if (!mounted)
1129 break;
1130 dput(path->dentry);
1131 mntput(path->mnt);
1132 path->mnt = mounted;
1133 path->dentry = dget(mounted->mnt_root);
1134 }
1135}
1136
Nick Piggin31e6b012011-01-07 17:49:52 +11001137static void follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
Al Viro2a737872009-04-07 11:49:53 -04001139 set_root(nd);
Andreas Mohre518ddb2006-09-29 02:01:22 -07001140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 while(1) {
Jan Blunck4ac91372008-02-14 19:34:32 -08001142 struct dentry *old = nd->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Al Viro2a737872009-04-07 11:49:53 -04001144 if (nd->path.dentry == nd->root.dentry &&
1145 nd->path.mnt == nd->root.mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 break;
1147 }
Jan Blunck4ac91372008-02-14 19:34:32 -08001148 if (nd->path.dentry != nd->path.mnt->mnt_root) {
Al Viro3088dd72010-01-30 15:47:29 -05001149 /* rare case of legitimate dget_parent()... */
1150 nd->path.dentry = dget_parent(nd->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 dput(old);
1152 break;
1153 }
Al Viro3088dd72010-01-30 15:47:29 -05001154 if (!follow_up(&nd->path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 }
Al Viro79ed0222009-04-18 13:59:41 -04001157 follow_mount(&nd->path);
Nick Piggin31e6b012011-01-07 17:49:52 +11001158 nd->inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159}
1160
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161/*
Nick Pigginbaa03892010-08-18 04:37:31 +10001162 * Allocate a dentry with name and parent, and perform a parent
1163 * directory ->lookup on it. Returns the new dentry, or ERR_PTR
1164 * on error. parent->d_inode->i_mutex must be held. d_lookup must
1165 * have verified that no child exists while under i_mutex.
1166 */
1167static struct dentry *d_alloc_and_lookup(struct dentry *parent,
1168 struct qstr *name, struct nameidata *nd)
1169{
1170 struct inode *inode = parent->d_inode;
1171 struct dentry *dentry;
1172 struct dentry *old;
1173
1174 /* Don't create child dentry for a dead directory. */
1175 if (unlikely(IS_DEADDIR(inode)))
1176 return ERR_PTR(-ENOENT);
1177
1178 dentry = d_alloc(parent, name);
1179 if (unlikely(!dentry))
1180 return ERR_PTR(-ENOMEM);
1181
1182 old = inode->i_op->lookup(inode, dentry, nd);
1183 if (unlikely(old)) {
1184 dput(dentry);
1185 dentry = old;
1186 }
1187 return dentry;
1188}
1189
1190/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 * It's more convoluted than I'd like it to be, but... it's still fairly
1192 * small and for now I'd prefer to have fast path as straight as possible.
1193 * It _is_ time-critical.
1194 */
1195static int do_lookup(struct nameidata *nd, struct qstr *name,
Nick Piggin31e6b012011-01-07 17:49:52 +11001196 struct path *path, struct inode **inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197{
Jan Blunck4ac91372008-02-14 19:34:32 -08001198 struct vfsmount *mnt = nd->path.mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +11001199 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro5a18fff2011-03-11 04:44:53 -05001200 int need_reval = 1;
1201 int status = 1;
David Howells9875cf82011-01-14 18:45:21 +00001202 int err;
1203
Al Viro3cac2602009-08-13 18:27:43 +04001204 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001205 * Rename seqlock is not required here because in the off chance
1206 * of a false negative due to a concurrent rename, we're going to
1207 * do the non-racy lookup, below.
1208 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001209 if (nd->flags & LOOKUP_RCU) {
1210 unsigned seq;
Nick Piggin31e6b012011-01-07 17:49:52 +11001211 *inode = nd->inode;
1212 dentry = __d_lookup_rcu(parent, name, &seq, inode);
Al Viro5a18fff2011-03-11 04:44:53 -05001213 if (!dentry)
1214 goto unlazy;
1215
Nick Piggin31e6b012011-01-07 17:49:52 +11001216 /* Memory barrier in read_seqcount_begin of child is enough */
1217 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1218 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001219 nd->seq = seq;
Al Viro5a18fff2011-03-11 04:44:53 -05001220
Al Viro24643082011-02-15 01:26:22 -05001221 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
Al Viro5a18fff2011-03-11 04:44:53 -05001222 status = d_revalidate(dentry, nd);
1223 if (unlikely(status <= 0)) {
1224 if (status != -ECHILD)
1225 need_reval = 0;
1226 goto unlazy;
1227 }
Al Viro24643082011-02-15 01:26:22 -05001228 }
Nick Piggin31e6b012011-01-07 17:49:52 +11001229 path->mnt = mnt;
1230 path->dentry = dentry;
David Howells9875cf82011-01-14 18:45:21 +00001231 if (likely(__follow_mount_rcu(nd, path, inode, false)))
1232 return 0;
Al Viro5a18fff2011-03-11 04:44:53 -05001233unlazy:
1234 if (dentry) {
1235 if (nameidata_dentry_drop_rcu(nd, dentry))
1236 return -ECHILD;
1237 } else {
1238 if (nameidata_drop_rcu(nd))
1239 return -ECHILD;
1240 }
1241 } else {
1242 dentry = __d_lookup(parent, name);
Nick Piggin31e6b012011-01-07 17:49:52 +11001243 }
Al Viro5a18fff2011-03-11 04:44:53 -05001244
1245retry:
1246 if (unlikely(!dentry)) {
1247 struct inode *dir = parent->d_inode;
1248 BUG_ON(nd->inode != dir);
1249
1250 mutex_lock(&dir->i_mutex);
1251 dentry = d_lookup(parent, name);
1252 if (likely(!dentry)) {
1253 dentry = d_alloc_and_lookup(parent, name, nd);
1254 if (IS_ERR(dentry)) {
1255 mutex_unlock(&dir->i_mutex);
1256 return PTR_ERR(dentry);
1257 }
1258 /* known good */
1259 need_reval = 0;
1260 status = 1;
1261 }
1262 mutex_unlock(&dir->i_mutex);
Al Viro24643082011-02-15 01:26:22 -05001263 }
Al Viro5a18fff2011-03-11 04:44:53 -05001264 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
1265 status = d_revalidate(dentry, nd);
1266 if (unlikely(status <= 0)) {
1267 if (status < 0) {
1268 dput(dentry);
1269 return status;
1270 }
1271 if (!d_invalidate(dentry)) {
1272 dput(dentry);
1273 dentry = NULL;
1274 need_reval = 1;
1275 goto retry;
1276 }
1277 }
1278
David Howells9875cf82011-01-14 18:45:21 +00001279 path->mnt = mnt;
1280 path->dentry = dentry;
1281 err = follow_managed(path, nd->flags);
Ian Kent89312212011-01-18 12:06:10 +08001282 if (unlikely(err < 0)) {
1283 path_put_conditional(path, nd);
David Howells9875cf82011-01-14 18:45:21 +00001284 return err;
Ian Kent89312212011-01-18 12:06:10 +08001285 }
David Howells9875cf82011-01-14 18:45:21 +00001286 *inode = path->dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288}
1289
Al Viro52094c82011-02-21 21:34:47 -05001290static inline int may_lookup(struct nameidata *nd)
1291{
1292 if (nd->flags & LOOKUP_RCU) {
1293 int err = exec_permission(nd->inode, IPERM_FLAG_RCU);
1294 if (err != -ECHILD)
1295 return err;
1296 if (nameidata_drop_rcu(nd))
1297 return -ECHILD;
1298 }
1299 return exec_permission(nd->inode, 0);
1300}
1301
Al Viro9856fa12011-03-04 14:22:06 -05001302static inline int handle_dots(struct nameidata *nd, int type)
1303{
1304 if (type == LAST_DOTDOT) {
1305 if (nd->flags & LOOKUP_RCU) {
1306 if (follow_dotdot_rcu(nd))
1307 return -ECHILD;
1308 } else
1309 follow_dotdot(nd);
1310 }
1311 return 0;
1312}
1313
Al Viro951361f2011-03-04 14:44:37 -05001314static void terminate_walk(struct nameidata *nd)
1315{
1316 if (!(nd->flags & LOOKUP_RCU)) {
1317 path_put(&nd->path);
1318 } else {
1319 nd->flags &= ~LOOKUP_RCU;
Al Viro5b6ca022011-03-09 23:04:47 -05001320 if (!(nd->flags & LOOKUP_ROOT))
1321 nd->root.mnt = NULL;
Al Viro951361f2011-03-04 14:44:37 -05001322 rcu_read_unlock();
1323 br_read_unlock(vfsmount_lock);
1324 }
1325}
1326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327/*
1328 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01001329 * This is the basic name resolution function, turning a pathname into
1330 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01001332 * Returns 0 and nd will have valid dentry and mnt on success.
1333 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 */
Al Viro6de88d72009-08-09 01:41:57 +04001335static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
1337 struct path next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 int err;
1339 unsigned int lookup_flags = nd->flags;
1340
1341 while (*name=='/')
1342 name++;
1343 if (!*name)
Al Viro086e1832011-02-22 20:56:27 -05001344 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 if (nd->depth)
Trond Myklebustf55eab82006-02-04 23:28:01 -08001347 lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349 /* At this point we know we have a real path component. */
1350 for(;;) {
Nick Piggin31e6b012011-01-07 17:49:52 +11001351 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 unsigned long hash;
1353 struct qstr this;
1354 unsigned int c;
Al Virofe479a52011-02-22 15:10:03 -05001355 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Trond Myklebustcdce5d62005-10-18 14:20:18 -07001357 nd->flags |= LOOKUP_CONTINUE;
Al Viro52094c82011-02-21 21:34:47 -05001358
1359 err = may_lookup(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 if (err)
1361 break;
1362
1363 this.name = name;
1364 c = *(const unsigned char *)name;
1365
1366 hash = init_name_hash();
1367 do {
1368 name++;
1369 hash = partial_name_hash(c, hash);
1370 c = *(const unsigned char *)name;
1371 } while (c && (c != '/'));
1372 this.len = name - (const char *) this.name;
1373 this.hash = end_name_hash(hash);
1374
Al Virofe479a52011-02-22 15:10:03 -05001375 type = LAST_NORM;
1376 if (this.name[0] == '.') switch (this.len) {
1377 case 2:
Al Viro16c2cd72011-02-22 15:50:10 -05001378 if (this.name[1] == '.') {
Al Virofe479a52011-02-22 15:10:03 -05001379 type = LAST_DOTDOT;
Al Viro16c2cd72011-02-22 15:50:10 -05001380 nd->flags |= LOOKUP_JUMPED;
1381 }
Al Virofe479a52011-02-22 15:10:03 -05001382 break;
1383 case 1:
1384 type = LAST_DOT;
1385 }
Al Viro5a202bc2011-03-08 14:17:44 -05001386 if (likely(type == LAST_NORM)) {
1387 struct dentry *parent = nd->path.dentry;
Al Viro16c2cd72011-02-22 15:50:10 -05001388 nd->flags &= ~LOOKUP_JUMPED;
Al Viro5a202bc2011-03-08 14:17:44 -05001389 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1390 err = parent->d_op->d_hash(parent, nd->inode,
1391 &this);
1392 if (err < 0)
1393 break;
1394 }
1395 }
Al Virofe479a52011-02-22 15:10:03 -05001396
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 /* remove trailing slashes? */
1398 if (!c)
1399 goto last_component;
1400 while (*++name == '/');
1401 if (!*name)
1402 goto last_with_slashes;
1403
1404 /*
1405 * "." and ".." are special - ".." especially so because it has
1406 * to be able to know about the current root directory and
1407 * parent relationships.
1408 */
Al Virofe479a52011-02-22 15:10:03 -05001409 if (unlikely(type != LAST_NORM)) {
Al Viroef7562d2011-03-04 14:35:59 -05001410 if (handle_dots(nd, type))
1411 return -ECHILD;
Al Virofe479a52011-02-22 15:10:03 -05001412 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 }
Al Virofe479a52011-02-22 15:10:03 -05001414
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 /* This does the actual lookups.. */
Nick Piggin31e6b012011-01-07 17:49:52 +11001416 err = do_lookup(nd, &this, &next, &inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 if (err)
1418 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Al Viro7bc055d2011-02-23 19:41:31 -05001420 if (inode && inode->i_op->follow_link) {
Linus Torvalds3abb17e2011-02-16 08:56:55 -08001421 err = do_follow_link(inode, &next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 if (err)
Al Viroa7472ba2011-03-04 14:39:30 -05001423 return err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001424 nd->inode = nd->path.dentry->d_inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001425 } else {
Miklos Szeredi09dd17d2005-09-06 15:18:21 -07001426 path_to_nameidata(&next, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11001427 nd->inode = inode;
1428 }
Al Viro7bc055d2011-02-23 19:41:31 -05001429 err = -ENOENT;
1430 if (!nd->inode)
1431 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 err = -ENOTDIR;
Nick Piggin31e6b012011-01-07 17:49:52 +11001433 if (!nd->inode->i_op->lookup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 break;
1435 continue;
1436 /* here ends the main loop */
1437
1438last_with_slashes:
1439 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1440last_component:
Trond Myklebustf55eab82006-02-04 23:28:01 -08001441 /* Clear LOOKUP_CONTINUE iff it was previously unset */
1442 nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 if (lookup_flags & LOOKUP_PARENT)
1444 goto lookup_parent;
Al Viroef7562d2011-03-04 14:35:59 -05001445 if (unlikely(type != LAST_NORM))
1446 return handle_dots(nd, type);
Nick Piggin31e6b012011-01-07 17:49:52 +11001447 err = do_lookup(nd, &this, &next, &inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 if (err)
1449 break;
David Howellsdb372912011-01-14 18:45:53 +00001450 if (inode && unlikely(inode->i_op->follow_link) &&
1451 (lookup_flags & LOOKUP_FOLLOW)) {
Linus Torvalds3abb17e2011-02-16 08:56:55 -08001452 err = do_follow_link(inode, &next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 if (err)
Al Viroa7472ba2011-03-04 14:39:30 -05001454 return err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001455 nd->inode = nd->path.dentry->d_inode;
1456 } else {
Miklos Szeredi09dd17d2005-09-06 15:18:21 -07001457 path_to_nameidata(&next, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11001458 nd->inode = inode;
1459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 err = -ENOENT;
Nick Piggin31e6b012011-01-07 17:49:52 +11001461 if (!nd->inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 break;
1463 if (lookup_flags & LOOKUP_DIRECTORY) {
1464 err = -ENOTDIR;
Nick Piggin31e6b012011-01-07 17:49:52 +11001465 if (!nd->inode->i_op->lookup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 break;
1467 }
Al Viro086e1832011-02-22 20:56:27 -05001468 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469lookup_parent:
1470 nd->last = this;
Al Virofe479a52011-02-22 15:10:03 -05001471 nd->last_type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 }
Al Viro951361f2011-03-04 14:44:37 -05001474 terminate_walk(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 return err;
1476}
1477
Al Viro70e9b352011-03-05 21:12:22 -05001478static int path_init(int dfd, const char *name, unsigned int flags,
1479 struct nameidata *nd, struct file **fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
Prasanna Medaea3834d2005-04-29 16:00:17 +01001481 int retval = 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001482 int fput_needed;
1483 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
1485 nd->last_type = LAST_ROOT; /* if there are only slashes... */
Al Viro16c2cd72011-02-22 15:50:10 -05001486 nd->flags = flags | LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 nd->depth = 0;
Al Viro5b6ca022011-03-09 23:04:47 -05001488 if (flags & LOOKUP_ROOT) {
1489 struct inode *inode = nd->root.dentry->d_inode;
Al Viro73d049a2011-03-11 12:08:24 -05001490 if (*name) {
1491 if (!inode->i_op->lookup)
1492 return -ENOTDIR;
1493 retval = inode_permission(inode, MAY_EXEC);
1494 if (retval)
1495 return retval;
1496 }
Al Viro5b6ca022011-03-09 23:04:47 -05001497 nd->path = nd->root;
1498 nd->inode = inode;
1499 if (flags & LOOKUP_RCU) {
1500 br_read_lock(vfsmount_lock);
1501 rcu_read_lock();
1502 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1503 } else {
1504 path_get(&nd->path);
1505 }
1506 return 0;
1507 }
1508
Al Viro2a737872009-04-07 11:49:53 -04001509 nd->root.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 if (*name=='/') {
Al Viroe41f7d42011-02-22 14:02:58 -05001512 if (flags & LOOKUP_RCU) {
1513 br_read_lock(vfsmount_lock);
1514 rcu_read_lock();
1515 set_root_rcu(nd);
1516 } else {
1517 set_root(nd);
1518 path_get(&nd->root);
1519 }
Al Viro2a737872009-04-07 11:49:53 -04001520 nd->path = nd->root;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001521 } else if (dfd == AT_FDCWD) {
Al Viroe41f7d42011-02-22 14:02:58 -05001522 if (flags & LOOKUP_RCU) {
1523 struct fs_struct *fs = current->fs;
1524 unsigned seq;
1525
1526 br_read_lock(vfsmount_lock);
1527 rcu_read_lock();
1528
1529 do {
1530 seq = read_seqcount_begin(&fs->seq);
1531 nd->path = fs->pwd;
1532 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1533 } while (read_seqcount_retry(&fs->seq, seq));
1534 } else {
1535 get_fs_pwd(current->fs, &nd->path);
1536 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001537 } else {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001538 struct dentry *dentry;
1539
1540 file = fget_light(dfd, &fput_needed);
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001541 retval = -EBADF;
1542 if (!file)
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001543 goto out_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001544
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001545 dentry = file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001546
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001547 retval = -ENOTDIR;
1548 if (!S_ISDIR(dentry->d_inode->i_mode))
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001549 goto fput_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001550
1551 retval = file_permission(file, MAY_EXEC);
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001552 if (retval)
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001553 goto fput_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001554
Jan Blunck5dd784d2008-02-14 19:34:38 -08001555 nd->path = file->f_path;
Al Viroe41f7d42011-02-22 14:02:58 -05001556 if (flags & LOOKUP_RCU) {
1557 if (fput_needed)
Al Viro70e9b352011-03-05 21:12:22 -05001558 *fp = file;
Al Viroe41f7d42011-02-22 14:02:58 -05001559 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1560 br_read_lock(vfsmount_lock);
1561 rcu_read_lock();
1562 } else {
1563 path_get(&file->f_path);
1564 fput_light(file, fput_needed);
1565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 }
Al Viroe41f7d42011-02-22 14:02:58 -05001567
Nick Piggin31e6b012011-01-07 17:49:52 +11001568 nd->inode = nd->path.dentry->d_inode;
Al Viro9b4a9b12009-04-07 11:44:16 -04001569 return 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001570
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001571fput_fail:
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001572 fput_light(file, fput_needed);
Al Viro9b4a9b12009-04-07 11:44:16 -04001573out_fail:
1574 return retval;
1575}
1576
1577/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroee0827c2011-02-21 23:38:09 -05001578static int path_lookupat(int dfd, const char *name,
Al Viro9b4a9b12009-04-07 11:44:16 -04001579 unsigned int flags, struct nameidata *nd)
1580{
Al Viro70e9b352011-03-05 21:12:22 -05001581 struct file *base = NULL;
Nick Piggin31e6b012011-01-07 17:49:52 +11001582 int retval;
1583
1584 /*
1585 * Path walking is largely split up into 2 different synchronisation
1586 * schemes, rcu-walk and ref-walk (explained in
1587 * Documentation/filesystems/path-lookup.txt). These share much of the
1588 * path walk code, but some things particularly setup, cleanup, and
1589 * following mounts are sufficiently divergent that functions are
1590 * duplicated. Typically there is a function foo(), and its RCU
1591 * analogue, foo_rcu().
1592 *
1593 * -ECHILD is the error number of choice (just to avoid clashes) that
1594 * is returned if some aspect of an rcu-walk fails. Such an error must
1595 * be handled by restarting a traditional ref-walk (which will always
1596 * be able to complete).
1597 */
Al Viro70e9b352011-03-05 21:12:22 -05001598 retval = path_init(dfd, name, flags, nd, &base);
Al Viroee0827c2011-02-21 23:38:09 -05001599
Nick Piggin31e6b012011-01-07 17:49:52 +11001600 if (unlikely(retval))
1601 return retval;
Al Viroee0827c2011-02-21 23:38:09 -05001602
1603 current->total_link_count = 0;
1604 retval = link_path_walk(name, nd);
1605
1606 if (nd->flags & LOOKUP_RCU) {
Al Viro4455ca62011-03-04 14:28:10 -05001607 /* went all way through without dropping RCU */
1608 BUG_ON(retval);
1609 if (nameidata_drop_rcu_last(nd))
1610 retval = -ECHILD;
Al Viroee0827c2011-02-21 23:38:09 -05001611 }
1612
Al Viro16c2cd72011-02-22 15:50:10 -05001613 if (!retval)
1614 retval = handle_reval_path(nd);
1615
Al Viro70e9b352011-03-05 21:12:22 -05001616 if (base)
1617 fput(base);
Al Viroee0827c2011-02-21 23:38:09 -05001618
Al Viro5b6ca022011-03-09 23:04:47 -05001619 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
Al Viro2a737872009-04-07 11:49:53 -04001620 path_put(&nd->root);
1621 nd->root.mnt = NULL;
1622 }
Al Viroee0827c2011-02-21 23:38:09 -05001623 return retval;
1624}
Nick Piggin31e6b012011-01-07 17:49:52 +11001625
Al Viroee0827c2011-02-21 23:38:09 -05001626static int do_path_lookup(int dfd, const char *name,
1627 unsigned int flags, struct nameidata *nd)
1628{
1629 int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1630 if (unlikely(retval == -ECHILD))
1631 retval = path_lookupat(dfd, name, flags, nd);
1632 if (unlikely(retval == -ESTALE))
1633 retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11001634
1635 if (likely(!retval)) {
1636 if (unlikely(!audit_dummy_context())) {
1637 if (nd->path.dentry && nd->inode)
1638 audit_inode(name, nd->path.dentry);
1639 }
1640 }
Al Viro9b4a9b12009-04-07 11:44:16 -04001641 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642}
1643
Al Viroc9c6cac2011-02-16 15:15:47 -05001644int kern_path_parent(const char *name, struct nameidata *nd)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001645{
Al Viroc9c6cac2011-02-16 15:15:47 -05001646 return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001647}
1648
Al Virod1811462008-08-02 00:49:18 -04001649int kern_path(const char *name, unsigned int flags, struct path *path)
1650{
1651 struct nameidata nd;
1652 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1653 if (!res)
1654 *path = nd.path;
1655 return res;
1656}
1657
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001658/**
1659 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1660 * @dentry: pointer to dentry of the base directory
1661 * @mnt: pointer to vfs mount of the base directory
1662 * @name: pointer to file name
1663 * @flags: lookup flags
1664 * @nd: pointer to nameidata
1665 */
1666int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
1667 const char *name, unsigned int flags,
1668 struct nameidata *nd)
1669{
Al Viro5b6ca022011-03-09 23:04:47 -05001670 nd->root.dentry = dentry;
1671 nd->root.mnt = mnt;
1672 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
1673 return do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, nd);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001674}
1675
Christoph Hellwigeead1912007-10-16 23:25:38 -07001676static struct dentry *__lookup_hash(struct qstr *name,
1677 struct dentry *base, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678{
Christoph Hellwig81fca442010-10-06 10:47:47 +02001679 struct inode *inode = base->d_inode;
James Morris057f6c02007-04-26 00:12:05 -07001680 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 int err;
1682
Nick Pigginb74c79e2011-01-07 17:49:58 +11001683 err = exec_permission(inode, 0);
Christoph Hellwig81fca442010-10-06 10:47:47 +02001684 if (err)
1685 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
1687 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001688 * Don't bother with __d_lookup: callers are for creat as
1689 * well as unlink, so a lot of the time it would cost
1690 * a double lookup.
Al Viro6e6b1bd2009-08-13 23:38:37 +04001691 */
Nick Pigginb04f7842010-08-18 04:37:34 +10001692 dentry = d_lookup(base, name);
Al Viro6e6b1bd2009-08-13 23:38:37 +04001693
Nick Pigginfb045ad2011-01-07 17:49:55 +11001694 if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE))
Al Viro6e6b1bd2009-08-13 23:38:37 +04001695 dentry = do_revalidate(dentry, nd);
1696
Nick Pigginbaa03892010-08-18 04:37:31 +10001697 if (!dentry)
1698 dentry = d_alloc_and_lookup(base, name, nd);
Al Viro5a202bc2011-03-08 14:17:44 -05001699
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 return dentry;
1701}
1702
James Morris057f6c02007-04-26 00:12:05 -07001703/*
1704 * Restricted form of lookup. Doesn't follow links, single-component only,
1705 * needs parent already locked. Doesn't follow mounts.
1706 * SMP-safe.
1707 */
Adrian Bunka244e162006-03-31 02:32:11 -08001708static struct dentry *lookup_hash(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709{
Jan Blunck4ac91372008-02-14 19:34:32 -08001710 return __lookup_hash(&nd->last, nd->path.dentry, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711}
1712
Christoph Hellwigeead1912007-10-16 23:25:38 -07001713/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07001714 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07001715 * @name: pathname component to lookup
1716 * @base: base directory to lookup from
1717 * @len: maximum length @len should be interpreted to
1718 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07001719 * Note that this routine is purely a helper for filesystem usage and should
1720 * not be called by generic code. Also note that by using this function the
Christoph Hellwigeead1912007-10-16 23:25:38 -07001721 * nameidata argument is passed to the filesystem methods and a filesystem
1722 * using this helper needs to be prepared for that.
1723 */
James Morris057f6c02007-04-26 00:12:05 -07001724struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1725{
James Morris057f6c02007-04-26 00:12:05 -07001726 struct qstr this;
Al Viro6a96ba52011-03-07 23:49:20 -05001727 unsigned long hash;
1728 unsigned int c;
James Morris057f6c02007-04-26 00:12:05 -07001729
David Woodhouse2f9092e2009-04-20 23:18:37 +01001730 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
1731
Al Viro6a96ba52011-03-07 23:49:20 -05001732 this.name = name;
1733 this.len = len;
1734 if (!len)
1735 return ERR_PTR(-EACCES);
1736
1737 hash = init_name_hash();
1738 while (len--) {
1739 c = *(const unsigned char *)name++;
1740 if (c == '/' || c == '\0')
1741 return ERR_PTR(-EACCES);
1742 hash = partial_name_hash(c, hash);
1743 }
1744 this.hash = end_name_hash(hash);
Al Viro5a202bc2011-03-08 14:17:44 -05001745 /*
1746 * See if the low-level filesystem might want
1747 * to use its own hash..
1748 */
1749 if (base->d_flags & DCACHE_OP_HASH) {
1750 int err = base->d_op->d_hash(base, base->d_inode, &this);
1751 if (err < 0)
1752 return ERR_PTR(err);
1753 }
Christoph Hellwigeead1912007-10-16 23:25:38 -07001754
Christoph Hellwig49705b72005-11-08 21:35:06 -08001755 return __lookup_hash(&this, base, NULL);
James Morris057f6c02007-04-26 00:12:05 -07001756}
1757
Al Viro2d8f3032008-07-22 09:59:21 -04001758int user_path_at(int dfd, const char __user *name, unsigned flags,
1759 struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760{
Al Viro2d8f3032008-07-22 09:59:21 -04001761 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 char *tmp = getname(name);
1763 int err = PTR_ERR(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 if (!IS_ERR(tmp)) {
Al Viro2d8f3032008-07-22 09:59:21 -04001765
1766 BUG_ON(flags & LOOKUP_PARENT);
1767
1768 err = do_path_lookup(dfd, tmp, flags, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 putname(tmp);
Al Viro2d8f3032008-07-22 09:59:21 -04001770 if (!err)
1771 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 }
1773 return err;
1774}
1775
Al Viro2ad94ae2008-07-21 09:32:51 -04001776static int user_path_parent(int dfd, const char __user *path,
1777 struct nameidata *nd, char **name)
1778{
1779 char *s = getname(path);
1780 int error;
1781
1782 if (IS_ERR(s))
1783 return PTR_ERR(s);
1784
1785 error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
1786 if (error)
1787 putname(s);
1788 else
1789 *name = s;
1790
1791 return error;
1792}
1793
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794/*
1795 * It's inline, so penalty for filesystems that don't use sticky bit is
1796 * minimal.
1797 */
1798static inline int check_sticky(struct inode *dir, struct inode *inode)
1799{
David Howellsda9592e2008-11-14 10:39:05 +11001800 uid_t fsuid = current_fsuid();
1801
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 if (!(dir->i_mode & S_ISVTX))
1803 return 0;
David Howellsda9592e2008-11-14 10:39:05 +11001804 if (inode->i_uid == fsuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 return 0;
David Howellsda9592e2008-11-14 10:39:05 +11001806 if (dir->i_uid == fsuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 return 0;
1808 return !capable(CAP_FOWNER);
1809}
1810
1811/*
1812 * Check whether we can remove a link victim from directory dir, check
1813 * whether the type of victim is right.
1814 * 1. We can't do it if dir is read-only (done in permission())
1815 * 2. We should have write and exec permissions on dir
1816 * 3. We can't remove anything from append-only dir
1817 * 4. We can't do anything with immutable dir (done in permission())
1818 * 5. If the sticky bit on dir is set we should either
1819 * a. be owner of dir, or
1820 * b. be owner of victim, or
1821 * c. have CAP_FOWNER capability
1822 * 6. If the victim is append-only or immutable we can't do antyhing with
1823 * links pointing to it.
1824 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1825 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1826 * 9. We can't remove a root or mountpoint.
1827 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1828 * nfs_async_unlink().
1829 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001830static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831{
1832 int error;
1833
1834 if (!victim->d_inode)
1835 return -ENOENT;
1836
1837 BUG_ON(victim->d_parent->d_inode != dir);
Al Virocccc6bb2009-12-25 05:07:33 -05001838 audit_inode_child(victim, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
Al Virof419a2e2008-07-22 00:07:17 -04001840 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 if (error)
1842 return error;
1843 if (IS_APPEND(dir))
1844 return -EPERM;
1845 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
Hugh Dickinsf9454542008-11-19 15:36:38 -08001846 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 return -EPERM;
1848 if (isdir) {
1849 if (!S_ISDIR(victim->d_inode->i_mode))
1850 return -ENOTDIR;
1851 if (IS_ROOT(victim))
1852 return -EBUSY;
1853 } else if (S_ISDIR(victim->d_inode->i_mode))
1854 return -EISDIR;
1855 if (IS_DEADDIR(dir))
1856 return -ENOENT;
1857 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1858 return -EBUSY;
1859 return 0;
1860}
1861
1862/* Check whether we can create an object with dentry child in directory
1863 * dir.
1864 * 1. We can't do it if child already exists (open has special treatment for
1865 * this case, but since we are inlined it's OK)
1866 * 2. We can't do it if dir is read-only (done in permission())
1867 * 3. We should have write and exec permissions on dir
1868 * 4. We can't do it if dir is immutable (done in permission())
1869 */
Miklos Szeredia95164d2008-07-30 15:08:48 +02001870static inline int may_create(struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871{
1872 if (child->d_inode)
1873 return -EEXIST;
1874 if (IS_DEADDIR(dir))
1875 return -ENOENT;
Al Virof419a2e2008-07-22 00:07:17 -04001876 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877}
1878
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879/*
1880 * p1 and p2 should be directories on the same fs.
1881 */
1882struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1883{
1884 struct dentry *p;
1885
1886 if (p1 == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07001887 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 return NULL;
1889 }
1890
Arjan van de Vena11f3a02006-03-23 03:00:33 -08001891 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001893 p = d_ancestor(p2, p1);
1894 if (p) {
1895 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1896 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
1897 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 }
1899
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001900 p = d_ancestor(p1, p2);
1901 if (p) {
1902 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1903 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1904 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 }
1906
Ingo Molnarf2eace22006-07-03 00:25:05 -07001907 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1908 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 return NULL;
1910}
1911
1912void unlock_rename(struct dentry *p1, struct dentry *p2)
1913{
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001914 mutex_unlock(&p1->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 if (p1 != p2) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001916 mutex_unlock(&p2->d_inode->i_mutex);
Arjan van de Vena11f3a02006-03-23 03:00:33 -08001917 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 }
1919}
1920
1921int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1922 struct nameidata *nd)
1923{
Miklos Szeredia95164d2008-07-30 15:08:48 +02001924 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
1926 if (error)
1927 return error;
1928
Al Viroacfa4382008-12-04 10:06:33 -05001929 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 return -EACCES; /* shouldn't it be ENOSYS? */
1931 mode &= S_IALLUGO;
1932 mode |= S_IFREG;
1933 error = security_inode_create(dir, dentry, mode);
1934 if (error)
1935 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 error = dir->i_op->create(dir, dentry, mode, nd);
Stephen Smalleya74574a2005-09-09 13:01:44 -07001937 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00001938 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 return error;
1940}
1941
Al Viro73d049a2011-03-11 12:08:24 -05001942static int may_open(struct path *path, int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001944 struct dentry *dentry = path->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 struct inode *inode = dentry->d_inode;
1946 int error;
1947
1948 if (!inode)
1949 return -ENOENT;
1950
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001951 switch (inode->i_mode & S_IFMT) {
1952 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001954 case S_IFDIR:
1955 if (acc_mode & MAY_WRITE)
1956 return -EISDIR;
1957 break;
1958 case S_IFBLK:
1959 case S_IFCHR:
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001960 if (path->mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001962 /*FALLTHRU*/
1963 case S_IFIFO:
1964 case S_IFSOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001966 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08001967 }
Dave Hansenb41572e2007-10-16 23:31:14 -07001968
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001969 error = inode_permission(inode, acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07001970 if (error)
1971 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05001972
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 /*
1974 * An append-only file must be opened in append mode for writing.
1975 */
1976 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05001977 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05001978 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05001980 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 }
1982
1983 /* O_NOATIME can only be set by the owner or superuser */
Al Viro7715b522009-12-16 03:54:00 -05001984 if (flag & O_NOATIME && !is_owner_or_cap(inode))
1985 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
1987 /*
1988 * Ensure there are no outstanding leases on the file.
1989 */
Al Virob65a9cf2009-12-16 06:27:40 -05001990 return break_lease(inode, flag);
Al Viro7715b522009-12-16 03:54:00 -05001991}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
Jeff Laytone1181ee2010-12-07 16:19:50 -05001993static int handle_truncate(struct file *filp)
Al Viro7715b522009-12-16 03:54:00 -05001994{
Jeff Laytone1181ee2010-12-07 16:19:50 -05001995 struct path *path = &filp->f_path;
Al Viro7715b522009-12-16 03:54:00 -05001996 struct inode *inode = path->dentry->d_inode;
1997 int error = get_write_access(inode);
1998 if (error)
1999 return error;
2000 /*
2001 * Refuse to truncate files with mandatory locks held on them.
2002 */
2003 error = locks_verify_locked(inode);
2004 if (!error)
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +09002005 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05002006 if (!error) {
2007 error = do_truncate(path->dentry, 0,
2008 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
Jeff Laytone1181ee2010-12-07 16:19:50 -05002009 filp);
Al Viro7715b522009-12-16 03:54:00 -05002010 }
2011 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04002012 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013}
2014
Dave Hansend57999e2008-02-15 14:37:27 -08002015/*
Dave Hansend57999e2008-02-15 14:37:27 -08002016 * Note that while the flag value (low two bits) for sys_open means:
2017 * 00 - read-only
2018 * 01 - write-only
2019 * 10 - read-write
2020 * 11 - special
2021 * it is changed into
2022 * 00 - no permissions needed
2023 * 01 - read-permission
2024 * 10 - write-permission
2025 * 11 - read-write
2026 * for the internal routines (ie open_namei()/follow_link() etc)
2027 * This is more logical, and also allows the 00 "no perm needed"
2028 * to be used for symlinks (where the permissions are checked
2029 * later).
2030 *
2031*/
2032static inline int open_to_namei_flags(int flag)
2033{
2034 if ((flag+1) & O_ACCMODE)
2035 flag++;
2036 return flag;
2037}
2038
Nick Piggin31e6b012011-01-07 17:49:52 +11002039/*
Al Virofe2d35f2011-03-05 22:58:25 -05002040 * Handle the last step of open()
Nick Piggin31e6b012011-01-07 17:49:52 +11002041 */
Al Virofb1cc552009-12-24 01:58:28 -05002042static struct file *do_last(struct nameidata *nd, struct path *path,
Al Viroc3e380b2011-02-23 13:39:45 -05002043 const struct open_flags *op, const char *pathname)
Al Virofb1cc552009-12-24 01:58:28 -05002044{
Al Viroa1e28032009-12-24 02:12:06 -05002045 struct dentry *dir = nd->path.dentry;
Al Viro6c0d46c2011-03-09 00:59:59 -05002046 struct dentry *dentry;
Al Viroca344a892011-03-09 00:36:45 -05002047 int open_flag = op->open_flag;
Al Viro6c0d46c2011-03-09 00:59:59 -05002048 int will_truncate = open_flag & O_TRUNC;
Al Viroca344a892011-03-09 00:36:45 -05002049 int want_write = 0;
2050 int skip_perm = 0;
Al Virofb1cc552009-12-24 01:58:28 -05002051 struct file *filp;
Al Virofe2d35f2011-03-05 22:58:25 -05002052 struct inode *inode;
Al Viro16c2cd72011-02-22 15:50:10 -05002053 int error;
Al Virofb1cc552009-12-24 01:58:28 -05002054
Al Viroc3e380b2011-02-23 13:39:45 -05002055 nd->flags &= ~LOOKUP_PARENT;
2056 nd->flags |= op->intent;
2057
Al Viro1f36f772009-12-26 10:56:19 -05002058 switch (nd->last_type) {
2059 case LAST_DOTDOT:
Neil Brown176306f2010-05-24 16:57:56 +10002060 case LAST_DOT:
Al Virofe2d35f2011-03-05 22:58:25 -05002061 error = handle_dots(nd, nd->last_type);
2062 if (error)
2063 return ERR_PTR(error);
Al Viro1f36f772009-12-26 10:56:19 -05002064 /* fallthrough */
Al Viro1f36f772009-12-26 10:56:19 -05002065 case LAST_ROOT:
Al Virofe2d35f2011-03-05 22:58:25 -05002066 if (nd->flags & LOOKUP_RCU) {
2067 if (nameidata_drop_rcu_last(nd))
2068 return ERR_PTR(-ECHILD);
2069 }
Al Viro16c2cd72011-02-22 15:50:10 -05002070 error = handle_reval_path(nd);
2071 if (error)
2072 goto exit;
Al Virofe2d35f2011-03-05 22:58:25 -05002073 audit_inode(pathname, nd->path.dentry);
Al Viroca344a892011-03-09 00:36:45 -05002074 if (open_flag & O_CREAT) {
Al Virofe2d35f2011-03-05 22:58:25 -05002075 error = -EISDIR;
2076 goto exit;
2077 }
2078 goto ok;
Al Viro1f36f772009-12-26 10:56:19 -05002079 case LAST_BIND:
Al Virofe2d35f2011-03-05 22:58:25 -05002080 /* can't be RCU mode here */
Al Viro16c2cd72011-02-22 15:50:10 -05002081 error = handle_reval_path(nd);
2082 if (error)
2083 goto exit;
Al Viro1f36f772009-12-26 10:56:19 -05002084 audit_inode(pathname, dir);
Al Viro67ee3ad2009-12-26 07:01:01 -05002085 goto ok;
Al Viro1f36f772009-12-26 10:56:19 -05002086 }
Al Viro67ee3ad2009-12-26 07:01:01 -05002087
Al Viroca344a892011-03-09 00:36:45 -05002088 if (!(open_flag & O_CREAT)) {
Al Virofe2d35f2011-03-05 22:58:25 -05002089 if (nd->last.name[nd->last.len])
2090 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2091 /* we _can_ be in RCU mode here */
2092 error = do_lookup(nd, &nd->last, path, &inode);
2093 if (error) {
2094 terminate_walk(nd);
2095 return ERR_PTR(error);
2096 }
2097 if (!inode) {
2098 path_to_nameidata(path, nd);
2099 terminate_walk(nd);
2100 return ERR_PTR(-ENOENT);
2101 }
2102 if (unlikely(inode->i_op->follow_link)) {
2103 /* We drop rcu-walk here */
2104 if (nameidata_dentry_drop_rcu_maybe(nd, path->dentry))
2105 return ERR_PTR(-ECHILD);
2106 return NULL;
2107 }
2108 path_to_nameidata(path, nd);
2109 nd->inode = inode;
2110 /* sayonara */
2111 if (nd->flags & LOOKUP_RCU) {
2112 if (nameidata_drop_rcu_last(nd))
2113 return ERR_PTR(-ECHILD);
2114 }
2115
2116 error = -ENOTDIR;
2117 if (nd->flags & LOOKUP_DIRECTORY) {
2118 if (!inode->i_op->lookup)
2119 goto exit;
2120 }
2121 audit_inode(pathname, nd->path.dentry);
2122 goto ok;
2123 }
2124
2125 /* create side of things */
2126
2127 if (nd->flags & LOOKUP_RCU) {
2128 if (nameidata_drop_rcu_last(nd))
2129 return ERR_PTR(-ECHILD);
2130 }
2131
2132 audit_inode(pathname, dir);
Al Viro16c2cd72011-02-22 15:50:10 -05002133 error = -EISDIR;
Al Viro1f36f772009-12-26 10:56:19 -05002134 /* trailing slashes? */
Nick Piggin31e6b012011-01-07 17:49:52 +11002135 if (nd->last.name[nd->last.len])
2136 goto exit;
Al Viroa2c36b42009-12-24 03:39:50 -05002137
Al Viroa1e28032009-12-24 02:12:06 -05002138 mutex_lock(&dir->d_inode->i_mutex);
2139
Al Viro6c0d46c2011-03-09 00:59:59 -05002140 dentry = lookup_hash(nd);
2141 error = PTR_ERR(dentry);
2142 if (IS_ERR(dentry)) {
Al Virofb1cc552009-12-24 01:58:28 -05002143 mutex_unlock(&dir->d_inode->i_mutex);
2144 goto exit;
2145 }
2146
Al Viro6c0d46c2011-03-09 00:59:59 -05002147 path->dentry = dentry;
2148 path->mnt = nd->path.mnt;
2149
Al Virofb1cc552009-12-24 01:58:28 -05002150 /* Negative dentry, just create the file */
Al Viro6c0d46c2011-03-09 00:59:59 -05002151 if (!dentry->d_inode) {
2152 int mode = op->mode;
2153 if (!IS_POSIXACL(dir->d_inode))
2154 mode &= ~current_umask();
Al Virofb1cc552009-12-24 01:58:28 -05002155 /*
2156 * This write is needed to ensure that a
Al Viro6c0d46c2011-03-09 00:59:59 -05002157 * rw->ro transition does not occur between
Al Virofb1cc552009-12-24 01:58:28 -05002158 * the time when the file is created and when
2159 * a permanent write count is taken through
2160 * the 'struct file' in nameidata_to_filp().
2161 */
2162 error = mnt_want_write(nd->path.mnt);
2163 if (error)
2164 goto exit_mutex_unlock;
Al Viroca344a892011-03-09 00:36:45 -05002165 want_write = 1;
Al Viro9b44f1b2011-03-09 00:17:27 -05002166 /* Don't check for write permission, don't truncate */
Al Viroca344a892011-03-09 00:36:45 -05002167 open_flag &= ~O_TRUNC;
Al Viro6c0d46c2011-03-09 00:59:59 -05002168 will_truncate = 0;
Al Viroca344a892011-03-09 00:36:45 -05002169 skip_perm = 1;
Al Viro6c0d46c2011-03-09 00:59:59 -05002170 error = security_path_mknod(&nd->path, dentry, mode, 0);
2171 if (error)
2172 goto exit_mutex_unlock;
2173 error = vfs_create(dir->d_inode, dentry, mode, nd);
2174 if (error)
2175 goto exit_mutex_unlock;
2176 mutex_unlock(&dir->d_inode->i_mutex);
2177 dput(nd->path.dentry);
2178 nd->path.dentry = dentry;
Al Viroca344a892011-03-09 00:36:45 -05002179 goto common;
Al Virofb1cc552009-12-24 01:58:28 -05002180 }
2181
2182 /*
2183 * It already exists.
2184 */
2185 mutex_unlock(&dir->d_inode->i_mutex);
2186 audit_inode(pathname, path->dentry);
2187
2188 error = -EEXIST;
Al Viroca344a892011-03-09 00:36:45 -05002189 if (open_flag & O_EXCL)
Al Virofb1cc552009-12-24 01:58:28 -05002190 goto exit_dput;
2191
David Howells9875cf82011-01-14 18:45:21 +00002192 error = follow_managed(path, nd->flags);
2193 if (error < 0)
2194 goto exit_dput;
Al Virofb1cc552009-12-24 01:58:28 -05002195
2196 error = -ENOENT;
2197 if (!path->dentry->d_inode)
2198 goto exit_dput;
Al Viro9e67f362009-12-26 07:04:50 -05002199
2200 if (path->dentry->d_inode->i_op->follow_link)
Al Virofb1cc552009-12-24 01:58:28 -05002201 return NULL;
Al Virofb1cc552009-12-24 01:58:28 -05002202
2203 path_to_nameidata(path, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11002204 nd->inode = path->dentry->d_inode;
Al Virofb1cc552009-12-24 01:58:28 -05002205 error = -EISDIR;
Nick Piggin31e6b012011-01-07 17:49:52 +11002206 if (S_ISDIR(nd->inode->i_mode))
Al Virofb1cc552009-12-24 01:58:28 -05002207 goto exit;
Al Viro67ee3ad2009-12-26 07:01:01 -05002208ok:
Al Viro6c0d46c2011-03-09 00:59:59 -05002209 if (!S_ISREG(nd->inode->i_mode))
2210 will_truncate = 0;
2211
Al Viro0f9d1a12011-03-09 00:13:14 -05002212 if (will_truncate) {
2213 error = mnt_want_write(nd->path.mnt);
2214 if (error)
2215 goto exit;
Al Viroca344a892011-03-09 00:36:45 -05002216 want_write = 1;
Al Viro0f9d1a12011-03-09 00:13:14 -05002217 }
Al Viroca344a892011-03-09 00:36:45 -05002218common:
2219 error = may_open(&nd->path, skip_perm ? 0 : op->acc_mode, open_flag);
2220 if (error)
Al Viro0f9d1a12011-03-09 00:13:14 -05002221 goto exit;
Al Viro0f9d1a12011-03-09 00:13:14 -05002222 filp = nameidata_to_filp(nd);
2223 if (!IS_ERR(filp)) {
2224 error = ima_file_check(filp, op->acc_mode);
2225 if (error) {
2226 fput(filp);
2227 filp = ERR_PTR(error);
2228 }
2229 }
2230 if (!IS_ERR(filp)) {
2231 if (will_truncate) {
2232 error = handle_truncate(filp);
2233 if (error) {
2234 fput(filp);
2235 filp = ERR_PTR(error);
2236 }
2237 }
2238 }
Al Viroca344a892011-03-09 00:36:45 -05002239out:
2240 if (want_write)
Al Viro0f9d1a12011-03-09 00:13:14 -05002241 mnt_drop_write(nd->path.mnt);
2242 path_put(&nd->path);
Al Virofb1cc552009-12-24 01:58:28 -05002243 return filp;
2244
2245exit_mutex_unlock:
2246 mutex_unlock(&dir->d_inode->i_mutex);
2247exit_dput:
2248 path_put_conditional(path, nd);
2249exit:
Al Viroca344a892011-03-09 00:36:45 -05002250 filp = ERR_PTR(error);
2251 goto out;
Al Virofb1cc552009-12-24 01:58:28 -05002252}
2253
Al Viro13aab422011-02-23 17:54:08 -05002254static struct file *path_openat(int dfd, const char *pathname,
Al Viro73d049a2011-03-11 12:08:24 -05002255 struct nameidata *nd, const struct open_flags *op, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256{
Al Virofe2d35f2011-03-05 22:58:25 -05002257 struct file *base = NULL;
Dave Hansen4a3fd212008-02-15 14:37:48 -08002258 struct file *filp;
Al Viro9850c052010-01-13 15:01:15 -05002259 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 int count = 0;
Al Viro13aab422011-02-23 17:54:08 -05002261 int error;
Nick Piggin31e6b012011-01-07 17:49:52 +11002262
2263 filp = get_empty_filp();
2264 if (!filp)
2265 return ERR_PTR(-ENFILE);
2266
Al Viro47c805d2011-02-23 17:44:09 -05002267 filp->f_flags = op->open_flag;
Al Viro73d049a2011-03-11 12:08:24 -05002268 nd->intent.open.file = filp;
2269 nd->intent.open.flags = open_to_namei_flags(op->open_flag);
2270 nd->intent.open.create_mode = op->mode;
Nick Piggin31e6b012011-01-07 17:49:52 +11002271
Al Viro73d049a2011-03-11 12:08:24 -05002272 error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
Nick Piggin31e6b012011-01-07 17:49:52 +11002273 if (unlikely(error))
Al Viro13aab422011-02-23 17:54:08 -05002274 goto out_filp;
Nick Piggin31e6b012011-01-07 17:49:52 +11002275
Al Virofe2d35f2011-03-05 22:58:25 -05002276 current->total_link_count = 0;
Al Viro73d049a2011-03-11 12:08:24 -05002277 error = link_path_walk(pathname, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11002278 if (unlikely(error))
2279 goto out_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
Al Viro73d049a2011-03-11 12:08:24 -05002281 filp = do_last(nd, &path, op, pathname);
Al Viro806b6812009-12-26 07:16:40 -05002282 while (unlikely(!filp)) { /* trailing symlink */
Nick Piggin7b9337a2011-01-14 08:42:43 +00002283 struct path link = path;
2284 struct inode *linki = link.dentry->d_inode;
Al Virodef4af32009-12-26 08:37:05 -05002285 void *cookie;
Al Viro73d049a2011-03-11 12:08:24 -05002286 if (!(nd->flags & LOOKUP_FOLLOW) || count++ == 32) {
2287 path_put_conditional(&path, nd);
2288 path_put(&nd->path);
Al Viro40b39132011-03-09 16:22:18 -05002289 filp = ERR_PTR(-ELOOP);
2290 break;
2291 }
Al Viro806b6812009-12-26 07:16:40 -05002292 /*
2293 * This is subtle. Instead of calling do_follow_link() we do
2294 * the thing by hands. The reason is that this way we have zero
2295 * link_count and path_walk() (called from ->follow_link)
2296 * honoring LOOKUP_PARENT. After that we have the parent and
2297 * last component, i.e. we are in the same situation as after
2298 * the first path_walk(). Well, almost - if the last component
2299 * is normal we get its copy stored in nd->last.name and we will
2300 * have to putname() it when we are done. Procfs-like symlinks
2301 * just set LAST_BIND.
2302 */
Al Viro73d049a2011-03-11 12:08:24 -05002303 nd->flags |= LOOKUP_PARENT;
2304 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
2305 error = __do_follow_link(&link, nd, &cookie);
Al Viroc3e380b2011-02-23 13:39:45 -05002306 if (unlikely(error))
Al Virof1afe9e2011-02-22 22:27:28 -05002307 filp = ERR_PTR(error);
Al Viroc3e380b2011-02-23 13:39:45 -05002308 else
Al Viro73d049a2011-03-11 12:08:24 -05002309 filp = do_last(nd, &path, op, pathname);
Al Virof1afe9e2011-02-22 22:27:28 -05002310 if (!IS_ERR(cookie) && linki->i_op->put_link)
Al Viro73d049a2011-03-11 12:08:24 -05002311 linki->i_op->put_link(link.dentry, nd, cookie);
Nick Piggin7b9337a2011-01-14 08:42:43 +00002312 path_put(&link);
Al Viro806b6812009-12-26 07:16:40 -05002313 }
Al Viro10fa8e62009-12-26 07:09:49 -05002314out:
Al Viro73d049a2011-03-11 12:08:24 -05002315 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
2316 path_put(&nd->root);
Al Virofe2d35f2011-03-05 22:58:25 -05002317 if (base)
2318 fput(base);
Al Viro73d049a2011-03-11 12:08:24 -05002319 release_open_intent(nd);
Al Viro10fa8e62009-12-26 07:09:49 -05002320 return filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
Nick Piggin31e6b012011-01-07 17:49:52 +11002322out_filp:
Al Viro806b6812009-12-26 07:16:40 -05002323 filp = ERR_PTR(error);
Al Viro10fa8e62009-12-26 07:09:49 -05002324 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325}
2326
Al Viro13aab422011-02-23 17:54:08 -05002327struct file *do_filp_open(int dfd, const char *pathname,
2328 const struct open_flags *op, int flags)
2329{
Al Viro73d049a2011-03-11 12:08:24 -05002330 struct nameidata nd;
Al Viro13aab422011-02-23 17:54:08 -05002331 struct file *filp;
2332
Al Viro73d049a2011-03-11 12:08:24 -05002333 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
Al Viro13aab422011-02-23 17:54:08 -05002334 if (unlikely(filp == ERR_PTR(-ECHILD)))
Al Viro73d049a2011-03-11 12:08:24 -05002335 filp = path_openat(dfd, pathname, &nd, op, flags);
Al Viro13aab422011-02-23 17:54:08 -05002336 if (unlikely(filp == ERR_PTR(-ESTALE)))
Al Viro73d049a2011-03-11 12:08:24 -05002337 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
Al Viro13aab422011-02-23 17:54:08 -05002338 return filp;
2339}
2340
Al Viro73d049a2011-03-11 12:08:24 -05002341struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
2342 const char *name, const struct open_flags *op, int flags)
2343{
2344 struct nameidata nd;
2345 struct file *file;
2346
2347 nd.root.mnt = mnt;
2348 nd.root.dentry = dentry;
2349
2350 flags |= LOOKUP_ROOT;
2351
2352 if (dentry->d_inode->i_op->follow_link)
2353 return ERR_PTR(-ELOOP);
2354
2355 file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
2356 if (unlikely(file == ERR_PTR(-ECHILD)))
2357 file = path_openat(-1, name, &nd, op, flags);
2358 if (unlikely(file == ERR_PTR(-ESTALE)))
2359 file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
2360 return file;
2361}
2362
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363/**
2364 * lookup_create - lookup a dentry, creating it if it doesn't exist
2365 * @nd: nameidata info
2366 * @is_dir: directory flag
2367 *
2368 * Simple function to lookup and return a dentry and create it
2369 * if it doesn't exist. Is SMP-safe.
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002370 *
Jan Blunck4ac91372008-02-14 19:34:32 -08002371 * Returns with nd->path.dentry->d_inode->i_mutex locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 */
2373struct dentry *lookup_create(struct nameidata *nd, int is_dir)
2374{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002375 struct dentry *dentry = ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
Jan Blunck4ac91372008-02-14 19:34:32 -08002377 mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002378 /*
2379 * Yucky last component or no last component at all?
2380 * (foo/., foo/.., /////)
2381 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 if (nd->last_type != LAST_NORM)
2383 goto fail;
2384 nd->flags &= ~LOOKUP_PARENT;
Al Viro35165862008-08-05 03:00:49 -04002385 nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
ASANO Masahiroa6349042006-08-22 20:06:02 -04002386 nd->intent.open.flags = O_EXCL;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002387
2388 /*
2389 * Do the final lookup.
2390 */
Christoph Hellwig49705b72005-11-08 21:35:06 -08002391 dentry = lookup_hash(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 if (IS_ERR(dentry))
2393 goto fail;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002394
Al Viroe9baf6e2008-05-15 04:49:12 -04002395 if (dentry->d_inode)
2396 goto eexist;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002397 /*
2398 * Special case - lookup gave negative, but... we had foo/bar/
2399 * From the vfs_mknod() POV we just have a negative dentry -
2400 * all is fine. Let's be bastards - you had / on the end, you've
2401 * been asking for (non-existent) directory. -ENOENT for you.
2402 */
Al Viroe9baf6e2008-05-15 04:49:12 -04002403 if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
2404 dput(dentry);
2405 dentry = ERR_PTR(-ENOENT);
2406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 return dentry;
Al Viroe9baf6e2008-05-15 04:49:12 -04002408eexist:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 dput(dentry);
Al Viroe9baf6e2008-05-15 04:49:12 -04002410 dentry = ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411fail:
2412 return dentry;
2413}
Christoph Hellwigf81a0bf2005-05-19 12:26:43 -07002414EXPORT_SYMBOL_GPL(lookup_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415
2416int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
2417{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002418 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419
2420 if (error)
2421 return error;
2422
2423 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
2424 return -EPERM;
2425
Al Viroacfa4382008-12-04 10:06:33 -05002426 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 return -EPERM;
2428
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07002429 error = devcgroup_inode_mknod(mode, dev);
2430 if (error)
2431 return error;
2432
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 error = security_inode_mknod(dir, dentry, mode, dev);
2434 if (error)
2435 return error;
2436
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002438 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002439 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 return error;
2441}
2442
Dave Hansen463c3192008-02-15 14:37:57 -08002443static int may_mknod(mode_t mode)
2444{
2445 switch (mode & S_IFMT) {
2446 case S_IFREG:
2447 case S_IFCHR:
2448 case S_IFBLK:
2449 case S_IFIFO:
2450 case S_IFSOCK:
2451 case 0: /* zero mode translates to S_IFREG */
2452 return 0;
2453 case S_IFDIR:
2454 return -EPERM;
2455 default:
2456 return -EINVAL;
2457 }
2458}
2459
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002460SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
2461 unsigned, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462{
Al Viro2ad94ae2008-07-21 09:32:51 -04002463 int error;
2464 char *tmp;
2465 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 struct nameidata nd;
2467
2468 if (S_ISDIR(mode))
2469 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
Al Viro2ad94ae2008-07-21 09:32:51 -04002471 error = user_path_parent(dfd, filename, &nd, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002473 return error;
2474
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 dentry = lookup_create(&nd, 0);
Dave Hansen463c3192008-02-15 14:37:57 -08002476 if (IS_ERR(dentry)) {
2477 error = PTR_ERR(dentry);
2478 goto out_unlock;
2479 }
Jan Blunck4ac91372008-02-14 19:34:32 -08002480 if (!IS_POSIXACL(nd.path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04002481 mode &= ~current_umask();
Dave Hansen463c3192008-02-15 14:37:57 -08002482 error = may_mknod(mode);
2483 if (error)
2484 goto out_dput;
2485 error = mnt_want_write(nd.path.mnt);
2486 if (error)
2487 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002488 error = security_path_mknod(&nd.path, dentry, mode, dev);
2489 if (error)
2490 goto out_drop_write;
Dave Hansen463c3192008-02-15 14:37:57 -08002491 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 case 0: case S_IFREG:
Jan Blunck4ac91372008-02-14 19:34:32 -08002493 error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 break;
2495 case S_IFCHR: case S_IFBLK:
Jan Blunck4ac91372008-02-14 19:34:32 -08002496 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 new_decode_dev(dev));
2498 break;
2499 case S_IFIFO: case S_IFSOCK:
Jan Blunck4ac91372008-02-14 19:34:32 -08002500 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 }
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002503out_drop_write:
Dave Hansen463c3192008-02-15 14:37:57 -08002504 mnt_drop_write(nd.path.mnt);
2505out_dput:
2506 dput(dentry);
2507out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002508 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002509 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 putname(tmp);
2511
2512 return error;
2513}
2514
Heiko Carstens3480b252009-01-14 14:14:16 +01002515SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002516{
2517 return sys_mknodat(AT_FDCWD, filename, mode, dev);
2518}
2519
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2521{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002522 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
2524 if (error)
2525 return error;
2526
Al Viroacfa4382008-12-04 10:06:33 -05002527 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 return -EPERM;
2529
2530 mode &= (S_IRWXUGO|S_ISVTX);
2531 error = security_inode_mkdir(dir, dentry, mode);
2532 if (error)
2533 return error;
2534
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002536 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002537 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 return error;
2539}
2540
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002541SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542{
2543 int error = 0;
2544 char * tmp;
Dave Hansen6902d922006-09-30 23:29:01 -07002545 struct dentry *dentry;
2546 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547
Al Viro2ad94ae2008-07-21 09:32:51 -04002548 error = user_path_parent(dfd, pathname, &nd, &tmp);
2549 if (error)
Dave Hansen6902d922006-09-30 23:29:01 -07002550 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
Dave Hansen6902d922006-09-30 23:29:01 -07002552 dentry = lookup_create(&nd, 1);
2553 error = PTR_ERR(dentry);
2554 if (IS_ERR(dentry))
2555 goto out_unlock;
2556
Jan Blunck4ac91372008-02-14 19:34:32 -08002557 if (!IS_POSIXACL(nd.path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04002558 mode &= ~current_umask();
Dave Hansen463c3192008-02-15 14:37:57 -08002559 error = mnt_want_write(nd.path.mnt);
2560 if (error)
2561 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002562 error = security_path_mkdir(&nd.path, dentry, mode);
2563 if (error)
2564 goto out_drop_write;
Jan Blunck4ac91372008-02-14 19:34:32 -08002565 error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002566out_drop_write:
Dave Hansen463c3192008-02-15 14:37:57 -08002567 mnt_drop_write(nd.path.mnt);
2568out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002569 dput(dentry);
2570out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002571 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002572 path_put(&nd.path);
Dave Hansen6902d922006-09-30 23:29:01 -07002573 putname(tmp);
2574out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 return error;
2576}
2577
Heiko Carstens3cdad422009-01-14 14:14:22 +01002578SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002579{
2580 return sys_mkdirat(AT_FDCWD, pathname, mode);
2581}
2582
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583/*
2584 * We try to drop the dentry early: we should have
2585 * a usage count of 2 if we're the only user of this
2586 * dentry, and if that is true (possibly after pruning
2587 * the dcache), then we drop the dentry now.
2588 *
2589 * A low-level filesystem can, if it choses, legally
2590 * do a
2591 *
2592 * if (!d_unhashed(dentry))
2593 * return -EBUSY;
2594 *
2595 * if it cannot handle the case of removing a directory
2596 * that is still in use by something else..
2597 */
2598void dentry_unhash(struct dentry *dentry)
2599{
2600 dget(dentry);
Vasily Averindc168422006-12-06 20:37:07 -08002601 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 spin_lock(&dentry->d_lock);
Nick Pigginb7ab39f2011-01-07 17:49:32 +11002603 if (dentry->d_count == 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 __d_drop(dentry);
2605 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606}
2607
2608int vfs_rmdir(struct inode *dir, struct dentry *dentry)
2609{
2610 int error = may_delete(dir, dentry, 1);
2611
2612 if (error)
2613 return error;
2614
Al Viroacfa4382008-12-04 10:06:33 -05002615 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 return -EPERM;
2617
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002618 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 dentry_unhash(dentry);
2620 if (d_mountpoint(dentry))
2621 error = -EBUSY;
2622 else {
2623 error = security_inode_rmdir(dir, dentry);
2624 if (!error) {
2625 error = dir->i_op->rmdir(dir, dentry);
Al Virod83c49f2010-04-30 17:17:09 -04002626 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 dentry->d_inode->i_flags |= S_DEAD;
Al Virod83c49f2010-04-30 17:17:09 -04002628 dont_mount(dentry);
2629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 }
2631 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002632 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 d_delete(dentry);
2635 }
2636 dput(dentry);
2637
2638 return error;
2639}
2640
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002641static long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642{
2643 int error = 0;
2644 char * name;
2645 struct dentry *dentry;
2646 struct nameidata nd;
2647
Al Viro2ad94ae2008-07-21 09:32:51 -04002648 error = user_path_parent(dfd, pathname, &nd, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002650 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651
2652 switch(nd.last_type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002653 case LAST_DOTDOT:
2654 error = -ENOTEMPTY;
2655 goto exit1;
2656 case LAST_DOT:
2657 error = -EINVAL;
2658 goto exit1;
2659 case LAST_ROOT:
2660 error = -EBUSY;
2661 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002663
2664 nd.flags &= ~LOOKUP_PARENT;
2665
Jan Blunck4ac91372008-02-14 19:34:32 -08002666 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002667 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002669 if (IS_ERR(dentry))
2670 goto exit2;
Dave Hansen06227532008-02-15 14:37:34 -08002671 error = mnt_want_write(nd.path.mnt);
2672 if (error)
2673 goto exit3;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002674 error = security_path_rmdir(&nd.path, dentry);
2675 if (error)
2676 goto exit4;
Jan Blunck4ac91372008-02-14 19:34:32 -08002677 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002678exit4:
Dave Hansen06227532008-02-15 14:37:34 -08002679 mnt_drop_write(nd.path.mnt);
2680exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07002681 dput(dentry);
2682exit2:
Jan Blunck4ac91372008-02-14 19:34:32 -08002683 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002685 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 putname(name);
2687 return error;
2688}
2689
Heiko Carstens3cdad422009-01-14 14:14:22 +01002690SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002691{
2692 return do_rmdir(AT_FDCWD, pathname);
2693}
2694
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695int vfs_unlink(struct inode *dir, struct dentry *dentry)
2696{
2697 int error = may_delete(dir, dentry, 0);
2698
2699 if (error)
2700 return error;
2701
Al Viroacfa4382008-12-04 10:06:33 -05002702 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 return -EPERM;
2704
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002705 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 if (d_mountpoint(dentry))
2707 error = -EBUSY;
2708 else {
2709 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05002710 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 error = dir->i_op->unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05002712 if (!error)
Al Virod83c49f2010-04-30 17:17:09 -04002713 dont_mount(dentry);
Al Virobec10522010-03-03 14:12:08 -05002714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002716 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717
2718 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2719 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
Jan Karaece95912008-02-06 01:37:13 -08002720 fsnotify_link_count(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 }
Robert Love0eeca282005-07-12 17:06:03 -04002723
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 return error;
2725}
2726
2727/*
2728 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002729 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 * writeout happening, and we don't want to prevent access to the directory
2731 * while waiting on the I/O.
2732 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002733static long do_unlinkat(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734{
Al Viro2ad94ae2008-07-21 09:32:51 -04002735 int error;
2736 char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 struct dentry *dentry;
2738 struct nameidata nd;
2739 struct inode *inode = NULL;
2740
Al Viro2ad94ae2008-07-21 09:32:51 -04002741 error = user_path_parent(dfd, pathname, &nd, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002743 return error;
2744
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 error = -EISDIR;
2746 if (nd.last_type != LAST_NORM)
2747 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002748
2749 nd.flags &= ~LOOKUP_PARENT;
2750
Jan Blunck4ac91372008-02-14 19:34:32 -08002751 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002752 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 error = PTR_ERR(dentry);
2754 if (!IS_ERR(dentry)) {
2755 /* Why not before? Because we want correct error value */
2756 if (nd.last.name[nd.last.len])
2757 goto slashes;
2758 inode = dentry->d_inode;
2759 if (inode)
Al Viro7de9c6e2010-10-23 11:11:40 -04002760 ihold(inode);
Dave Hansen06227532008-02-15 14:37:34 -08002761 error = mnt_want_write(nd.path.mnt);
2762 if (error)
2763 goto exit2;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002764 error = security_path_unlink(&nd.path, dentry);
2765 if (error)
2766 goto exit3;
Jan Blunck4ac91372008-02-14 19:34:32 -08002767 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002768exit3:
Dave Hansen06227532008-02-15 14:37:34 -08002769 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 exit2:
2771 dput(dentry);
2772 }
Jan Blunck4ac91372008-02-14 19:34:32 -08002773 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 if (inode)
2775 iput(inode); /* truncate the inode here */
2776exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002777 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 putname(name);
2779 return error;
2780
2781slashes:
2782 error = !dentry->d_inode ? -ENOENT :
2783 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2784 goto exit2;
2785}
2786
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002787SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002788{
2789 if ((flag & ~AT_REMOVEDIR) != 0)
2790 return -EINVAL;
2791
2792 if (flag & AT_REMOVEDIR)
2793 return do_rmdir(dfd, pathname);
2794
2795 return do_unlinkat(dfd, pathname);
2796}
2797
Heiko Carstens3480b252009-01-14 14:14:16 +01002798SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002799{
2800 return do_unlinkat(AT_FDCWD, pathname);
2801}
2802
Miklos Szeredidb2e7472008-06-24 16:50:16 +02002803int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002805 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806
2807 if (error)
2808 return error;
2809
Al Viroacfa4382008-12-04 10:06:33 -05002810 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 return -EPERM;
2812
2813 error = security_inode_symlink(dir, dentry, oldname);
2814 if (error)
2815 return error;
2816
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002818 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002819 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 return error;
2821}
2822
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002823SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
2824 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825{
Al Viro2ad94ae2008-07-21 09:32:51 -04002826 int error;
2827 char *from;
2828 char *to;
Dave Hansen6902d922006-09-30 23:29:01 -07002829 struct dentry *dentry;
2830 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831
2832 from = getname(oldname);
Al Viro2ad94ae2008-07-21 09:32:51 -04002833 if (IS_ERR(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 return PTR_ERR(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04002835
2836 error = user_path_parent(newdfd, newname, &nd, &to);
2837 if (error)
Dave Hansen6902d922006-09-30 23:29:01 -07002838 goto out_putname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839
Dave Hansen6902d922006-09-30 23:29:01 -07002840 dentry = lookup_create(&nd, 0);
2841 error = PTR_ERR(dentry);
2842 if (IS_ERR(dentry))
2843 goto out_unlock;
2844
Dave Hansen75c3f292008-02-15 14:37:45 -08002845 error = mnt_want_write(nd.path.mnt);
2846 if (error)
2847 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002848 error = security_path_symlink(&nd.path, dentry, from);
2849 if (error)
2850 goto out_drop_write;
Miklos Szeredidb2e7472008-06-24 16:50:16 +02002851 error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002852out_drop_write:
Dave Hansen75c3f292008-02-15 14:37:45 -08002853 mnt_drop_write(nd.path.mnt);
2854out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002855 dput(dentry);
2856out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002857 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002858 path_put(&nd.path);
Dave Hansen6902d922006-09-30 23:29:01 -07002859 putname(to);
2860out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 putname(from);
2862 return error;
2863}
2864
Heiko Carstens3480b252009-01-14 14:14:16 +01002865SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002866{
2867 return sys_symlinkat(oldname, AT_FDCWD, newname);
2868}
2869
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2871{
2872 struct inode *inode = old_dentry->d_inode;
2873 int error;
2874
2875 if (!inode)
2876 return -ENOENT;
2877
Miklos Szeredia95164d2008-07-30 15:08:48 +02002878 error = may_create(dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 if (error)
2880 return error;
2881
2882 if (dir->i_sb != inode->i_sb)
2883 return -EXDEV;
2884
2885 /*
2886 * A link to an append-only or immutable file cannot be created.
2887 */
2888 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2889 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05002890 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002892 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 return -EPERM;
2894
2895 error = security_inode_link(old_dentry, dir, new_dentry);
2896 if (error)
2897 return error;
2898
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002899 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 error = dir->i_op->link(old_dentry, dir, new_dentry);
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002901 mutex_unlock(&inode->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07002902 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002903 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 return error;
2905}
2906
2907/*
2908 * Hardlinks are often used in delicate situations. We avoid
2909 * security-related surprises by not following symlinks on the
2910 * newname. --KAB
2911 *
2912 * We don't follow them on the oldname either to be compatible
2913 * with linux 2.0, and to avoid hard-linking to directories
2914 * and other special files. --ADM
2915 */
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002916SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
2917 int, newdfd, const char __user *, newname, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918{
2919 struct dentry *new_dentry;
Al Viro2d8f3032008-07-22 09:59:21 -04002920 struct nameidata nd;
2921 struct path old_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 int error;
Al Viro2ad94ae2008-07-21 09:32:51 -04002923 char *to;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924
Ulrich Drepper45c9b112006-06-25 05:49:11 -07002925 if ((flags & ~AT_SYMLINK_FOLLOW) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002926 return -EINVAL;
2927
Al Viro2d8f3032008-07-22 09:59:21 -04002928 error = user_path_at(olddfd, oldname,
2929 flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0,
2930 &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002932 return error;
2933
2934 error = user_path_parent(newdfd, newname, &nd, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 if (error)
2936 goto out;
2937 error = -EXDEV;
Al Viro2d8f3032008-07-22 09:59:21 -04002938 if (old_path.mnt != nd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 goto out_release;
2940 new_dentry = lookup_create(&nd, 0);
2941 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002942 if (IS_ERR(new_dentry))
2943 goto out_unlock;
Dave Hansen75c3f292008-02-15 14:37:45 -08002944 error = mnt_want_write(nd.path.mnt);
2945 if (error)
2946 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002947 error = security_path_link(old_path.dentry, &nd.path, new_dentry);
2948 if (error)
2949 goto out_drop_write;
Al Viro2d8f3032008-07-22 09:59:21 -04002950 error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002951out_drop_write:
Dave Hansen75c3f292008-02-15 14:37:45 -08002952 mnt_drop_write(nd.path.mnt);
2953out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002954 dput(new_dentry);
2955out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002956 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957out_release:
Jan Blunck1d957f92008-02-14 19:34:35 -08002958 path_put(&nd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04002959 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960out:
Al Viro2d8f3032008-07-22 09:59:21 -04002961 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962
2963 return error;
2964}
2965
Heiko Carstens3480b252009-01-14 14:14:16 +01002966SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002967{
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002968 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002969}
2970
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971/*
2972 * The worst of all namespace operations - renaming directory. "Perverted"
2973 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2974 * Problems:
2975 * a) we can get into loop creation. Check is done in is_subdir().
2976 * b) race potential - two innocent renames can create a loop together.
2977 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002978 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 * story.
2980 * c) we have to lock _three_ objects - parents and victim (if it exists).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002981 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 * whether the target exists). Solution: try to be smart with locking
2983 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002984 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 * move will be locked. Thus we can rank directories by the tree
2986 * (ancestors first) and rank all non-directories after them.
2987 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002988 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 * HOWEVER, it relies on the assumption that any object with ->lookup()
2990 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2991 * we'd better make sure that there's no link(2) for them.
2992 * d) some filesystems don't support opened-but-unlinked directories,
2993 * either because of layout or because they are not ready to deal with
2994 * all cases correctly. The latter will be fixed (taking this sort of
2995 * stuff into VFS), but the former is not going away. Solution: the same
2996 * trick as in rmdir().
2997 * e) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002998 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05003000 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 * locking].
3002 */
Adrian Bunk75c96f82005-05-05 16:16:09 -07003003static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
3004 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005{
3006 int error = 0;
3007 struct inode *target;
3008
3009 /*
3010 * If we are going to change the parent - check write permissions,
3011 * we'll need to flip '..'.
3012 */
3013 if (new_dir != old_dir) {
Al Virof419a2e2008-07-22 00:07:17 -04003014 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 if (error)
3016 return error;
3017 }
3018
3019 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3020 if (error)
3021 return error;
3022
3023 target = new_dentry->d_inode;
Al Virod83c49f2010-04-30 17:17:09 -04003024 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003025 mutex_lock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
3027 error = -EBUSY;
Al Virod83c49f2010-04-30 17:17:09 -04003028 else {
3029 if (target)
3030 dentry_unhash(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
Al Virod83c49f2010-04-30 17:17:09 -04003032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 if (target) {
Al Virod83c49f2010-04-30 17:17:09 -04003034 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 target->i_flags |= S_DEAD;
Al Virod83c49f2010-04-30 17:17:09 -04003036 dont_mount(new_dentry);
3037 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003038 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 if (d_unhashed(new_dentry))
3040 d_rehash(new_dentry);
3041 dput(new_dentry);
3042 }
Stephen Smalleye31e14e2005-09-09 13:01:45 -07003043 if (!error)
Mark Fasheh349457c2006-09-08 14:22:21 -07003044 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3045 d_move(old_dentry,new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 return error;
3047}
3048
Adrian Bunk75c96f82005-05-05 16:16:09 -07003049static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
3050 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051{
3052 struct inode *target;
3053 int error;
3054
3055 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3056 if (error)
3057 return error;
3058
3059 dget(new_dentry);
3060 target = new_dentry->d_inode;
3061 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003062 mutex_lock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
3064 error = -EBUSY;
3065 else
3066 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3067 if (!error) {
Al Virobec10522010-03-03 14:12:08 -05003068 if (target)
Al Virod83c49f2010-04-30 17:17:09 -04003069 dont_mount(new_dentry);
Mark Fasheh349457c2006-09-08 14:22:21 -07003070 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 d_move(old_dentry, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 }
3073 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003074 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 dput(new_dentry);
3076 return error;
3077}
3078
3079int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
3080 struct inode *new_dir, struct dentry *new_dentry)
3081{
3082 int error;
3083 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
Eric Paris59b0df22010-02-08 12:53:52 -05003084 const unsigned char *old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085
3086 if (old_dentry->d_inode == new_dentry->d_inode)
3087 return 0;
3088
3089 error = may_delete(old_dir, old_dentry, is_dir);
3090 if (error)
3091 return error;
3092
3093 if (!new_dentry->d_inode)
Miklos Szeredia95164d2008-07-30 15:08:48 +02003094 error = may_create(new_dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 else
3096 error = may_delete(new_dir, new_dentry, is_dir);
3097 if (error)
3098 return error;
3099
Al Viroacfa4382008-12-04 10:06:33 -05003100 if (!old_dir->i_op->rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 return -EPERM;
3102
Robert Love0eeca282005-07-12 17:06:03 -04003103 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
3104
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 if (is_dir)
3106 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
3107 else
3108 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
Al Viro123df292009-12-25 04:57:57 -05003109 if (!error)
3110 fsnotify_move(old_dir, new_dir, old_name, is_dir,
Al Viro5a190ae2007-06-07 12:19:32 -04003111 new_dentry->d_inode, old_dentry);
Robert Love0eeca282005-07-12 17:06:03 -04003112 fsnotify_oldname_free(old_name);
3113
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 return error;
3115}
3116
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003117SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
3118 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119{
Al Viro2ad94ae2008-07-21 09:32:51 -04003120 struct dentry *old_dir, *new_dir;
3121 struct dentry *old_dentry, *new_dentry;
3122 struct dentry *trap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 struct nameidata oldnd, newnd;
Al Viro2ad94ae2008-07-21 09:32:51 -04003124 char *from;
3125 char *to;
3126 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127
Al Viro2ad94ae2008-07-21 09:32:51 -04003128 error = user_path_parent(olddfd, oldname, &oldnd, &from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 if (error)
3130 goto exit;
3131
Al Viro2ad94ae2008-07-21 09:32:51 -04003132 error = user_path_parent(newdfd, newname, &newnd, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 if (error)
3134 goto exit1;
3135
3136 error = -EXDEV;
Jan Blunck4ac91372008-02-14 19:34:32 -08003137 if (oldnd.path.mnt != newnd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 goto exit2;
3139
Jan Blunck4ac91372008-02-14 19:34:32 -08003140 old_dir = oldnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 error = -EBUSY;
3142 if (oldnd.last_type != LAST_NORM)
3143 goto exit2;
3144
Jan Blunck4ac91372008-02-14 19:34:32 -08003145 new_dir = newnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 if (newnd.last_type != LAST_NORM)
3147 goto exit2;
3148
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003149 oldnd.flags &= ~LOOKUP_PARENT;
3150 newnd.flags &= ~LOOKUP_PARENT;
OGAWA Hirofumi4e9ed2f2008-10-16 07:50:29 +09003151 newnd.flags |= LOOKUP_RENAME_TARGET;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003152
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 trap = lock_rename(new_dir, old_dir);
3154
Christoph Hellwig49705b72005-11-08 21:35:06 -08003155 old_dentry = lookup_hash(&oldnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 error = PTR_ERR(old_dentry);
3157 if (IS_ERR(old_dentry))
3158 goto exit3;
3159 /* source must exist */
3160 error = -ENOENT;
3161 if (!old_dentry->d_inode)
3162 goto exit4;
3163 /* unless the source is a directory trailing slashes give -ENOTDIR */
3164 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
3165 error = -ENOTDIR;
3166 if (oldnd.last.name[oldnd.last.len])
3167 goto exit4;
3168 if (newnd.last.name[newnd.last.len])
3169 goto exit4;
3170 }
3171 /* source should not be ancestor of target */
3172 error = -EINVAL;
3173 if (old_dentry == trap)
3174 goto exit4;
Christoph Hellwig49705b72005-11-08 21:35:06 -08003175 new_dentry = lookup_hash(&newnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 error = PTR_ERR(new_dentry);
3177 if (IS_ERR(new_dentry))
3178 goto exit4;
3179 /* target should not be an ancestor of source */
3180 error = -ENOTEMPTY;
3181 if (new_dentry == trap)
3182 goto exit5;
3183
Dave Hansen9079b1e2008-02-15 14:37:49 -08003184 error = mnt_want_write(oldnd.path.mnt);
3185 if (error)
3186 goto exit5;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003187 error = security_path_rename(&oldnd.path, old_dentry,
3188 &newnd.path, new_dentry);
3189 if (error)
3190 goto exit6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 error = vfs_rename(old_dir->d_inode, old_dentry,
3192 new_dir->d_inode, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003193exit6:
Dave Hansen9079b1e2008-02-15 14:37:49 -08003194 mnt_drop_write(oldnd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195exit5:
3196 dput(new_dentry);
3197exit4:
3198 dput(old_dentry);
3199exit3:
3200 unlock_rename(new_dir, old_dir);
3201exit2:
Jan Blunck1d957f92008-02-14 19:34:35 -08003202 path_put(&newnd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04003203 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003205 path_put(&oldnd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 putname(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04003207exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 return error;
3209}
3210
Heiko Carstensa26eab22009-01-14 14:14:17 +01003211SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003212{
3213 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
3214}
3215
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
3217{
3218 int len;
3219
3220 len = PTR_ERR(link);
3221 if (IS_ERR(link))
3222 goto out;
3223
3224 len = strlen(link);
3225 if (len > (unsigned) buflen)
3226 len = buflen;
3227 if (copy_to_user(buffer, link, len))
3228 len = -EFAULT;
3229out:
3230 return len;
3231}
3232
3233/*
3234 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
3235 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
3236 * using) it for any given inode is up to filesystem.
3237 */
3238int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3239{
3240 struct nameidata nd;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003241 void *cookie;
Marcin Slusarz694a1762008-06-09 16:40:37 -07003242 int res;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003243
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 nd.depth = 0;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003245 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
Marcin Slusarz694a1762008-06-09 16:40:37 -07003246 if (IS_ERR(cookie))
3247 return PTR_ERR(cookie);
3248
3249 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
3250 if (dentry->d_inode->i_op->put_link)
3251 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3252 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253}
3254
3255int vfs_follow_link(struct nameidata *nd, const char *link)
3256{
3257 return __vfs_follow_link(nd, link);
3258}
3259
3260/* get the link contents into pagecache */
3261static char *page_getlink(struct dentry * dentry, struct page **ppage)
3262{
Duane Griffinebd09ab2008-12-19 20:47:12 +00003263 char *kaddr;
3264 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 struct address_space *mapping = dentry->d_inode->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -07003266 page = read_mapping_page(mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 if (IS_ERR(page))
Nick Piggin6fe69002007-05-06 14:49:04 -07003268 return (char*)page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 *ppage = page;
Duane Griffinebd09ab2008-12-19 20:47:12 +00003270 kaddr = kmap(page);
3271 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3272 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273}
3274
3275int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3276{
3277 struct page *page = NULL;
3278 char *s = page_getlink(dentry, &page);
3279 int res = vfs_readlink(dentry,buffer,buflen,s);
3280 if (page) {
3281 kunmap(page);
3282 page_cache_release(page);
3283 }
3284 return res;
3285}
3286
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003287void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003289 struct page *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 nd_set_link(nd, page_getlink(dentry, &page));
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003291 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292}
3293
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003294void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003296 struct page *page = cookie;
3297
3298 if (page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 kunmap(page);
3300 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 }
3302}
3303
Nick Piggin54566b22009-01-04 12:00:53 -08003304/*
3305 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
3306 */
3307int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308{
3309 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003310 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07003311 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08003312 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 char *kaddr;
Nick Piggin54566b22009-01-04 12:00:53 -08003314 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
3315 if (nofs)
3316 flags |= AOP_FLAG_NOFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317
NeilBrown7e53cac2006-03-25 03:07:57 -08003318retry:
Nick Pigginafddba42007-10-16 01:25:01 -07003319 err = pagecache_write_begin(NULL, mapping, 0, len-1,
Nick Piggin54566b22009-01-04 12:00:53 -08003320 flags, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07003322 goto fail;
3323
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 kaddr = kmap_atomic(page, KM_USER0);
3325 memcpy(kaddr, symname, len-1);
3326 kunmap_atomic(kaddr, KM_USER0);
Nick Pigginafddba42007-10-16 01:25:01 -07003327
3328 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3329 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 if (err < 0)
3331 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07003332 if (err < len-1)
3333 goto retry;
3334
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 mark_inode_dirty(inode);
3336 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337fail:
3338 return err;
3339}
3340
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003341int page_symlink(struct inode *inode, const char *symname, int len)
3342{
3343 return __page_symlink(inode, symname, len,
Nick Piggin54566b22009-01-04 12:00:53 -08003344 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003345}
3346
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003347const struct inode_operations page_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 .readlink = generic_readlink,
3349 .follow_link = page_follow_link_light,
3350 .put_link = page_put_link,
3351};
3352
Al Viro2d8f3032008-07-22 09:59:21 -04003353EXPORT_SYMBOL(user_path_at);
David Howellscc53ce52011-01-14 18:45:26 +00003354EXPORT_SYMBOL(follow_down_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355EXPORT_SYMBOL(follow_down);
3356EXPORT_SYMBOL(follow_up);
3357EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
3358EXPORT_SYMBOL(getname);
3359EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360EXPORT_SYMBOL(lookup_one_len);
3361EXPORT_SYMBOL(page_follow_link_light);
3362EXPORT_SYMBOL(page_put_link);
3363EXPORT_SYMBOL(page_readlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003364EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365EXPORT_SYMBOL(page_symlink);
3366EXPORT_SYMBOL(page_symlink_inode_operations);
Al Viroc9c6cac2011-02-16 15:15:47 -05003367EXPORT_SYMBOL(kern_path_parent);
Al Virod1811462008-08-02 00:49:18 -04003368EXPORT_SYMBOL(kern_path);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07003369EXPORT_SYMBOL(vfs_path_lookup);
Al Virof419a2e2008-07-22 00:07:17 -04003370EXPORT_SYMBOL(inode_permission);
Christoph Hellwig8c744fb2005-11-08 21:35:04 -08003371EXPORT_SYMBOL(file_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372EXPORT_SYMBOL(unlock_rename);
3373EXPORT_SYMBOL(vfs_create);
3374EXPORT_SYMBOL(vfs_follow_link);
3375EXPORT_SYMBOL(vfs_link);
3376EXPORT_SYMBOL(vfs_mkdir);
3377EXPORT_SYMBOL(vfs_mknod);
3378EXPORT_SYMBOL(generic_permission);
3379EXPORT_SYMBOL(vfs_readlink);
3380EXPORT_SYMBOL(vfs_rename);
3381EXPORT_SYMBOL(vfs_rmdir);
3382EXPORT_SYMBOL(vfs_symlink);
3383EXPORT_SYMBOL(vfs_unlink);
3384EXPORT_SYMBOL(dentry_unhash);
3385EXPORT_SYMBOL(generic_readlink);