blob: 0bebd13e5cb767c7540615ea899fae201b0bc5f6 [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 Blunck5dd784d02008-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;
404
405 BUG_ON(!(nd->flags & LOOKUP_RCU));
406 if (nd->root.mnt) {
407 spin_lock(&fs->lock);
408 if (nd->root.mnt != fs->root.mnt ||
409 nd->root.dentry != fs->root.dentry)
410 goto err_root;
411 }
412 spin_lock(&dentry->d_lock);
413 if (!__d_rcu_to_refcount(dentry, nd->seq))
414 goto err;
415 BUG_ON(nd->inode != dentry->d_inode);
416 spin_unlock(&dentry->d_lock);
417 if (nd->root.mnt) {
418 path_get(&nd->root);
419 spin_unlock(&fs->lock);
420 }
421 mntget(nd->path.mnt);
422
423 rcu_read_unlock();
424 br_read_unlock(vfsmount_lock);
425 nd->flags &= ~LOOKUP_RCU;
426 return 0;
427err:
428 spin_unlock(&dentry->d_lock);
429err_root:
430 if (nd->root.mnt)
431 spin_unlock(&fs->lock);
432 return -ECHILD;
433}
434
435/* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing. */
436static inline int nameidata_drop_rcu_maybe(struct nameidata *nd)
437{
438 if (nd->flags & LOOKUP_RCU)
439 return nameidata_drop_rcu(nd);
440 return 0;
441}
442
443/**
444 * nameidata_dentry_drop_rcu - drop nameidata and dentry out of rcu-walk
445 * @nd: nameidata pathwalk data to drop
446 * @dentry: dentry to drop
Randy Dunlap39191622011-01-08 19:36:21 -0800447 * Returns: 0 on success, -ECHILD on failure
Nick Piggin31e6b012011-01-07 17:49:52 +1100448 *
449 * nameidata_dentry_drop_rcu attempts to drop the current nd->path and nd->root,
450 * and dentry into ref-walk. @dentry must be a path found by a do_lookup call on
451 * @nd. Must be called from rcu-walk context.
452 */
453static int nameidata_dentry_drop_rcu(struct nameidata *nd, struct dentry *dentry)
454{
455 struct fs_struct *fs = current->fs;
456 struct dentry *parent = nd->path.dentry;
457
458 BUG_ON(!(nd->flags & LOOKUP_RCU));
459 if (nd->root.mnt) {
460 spin_lock(&fs->lock);
461 if (nd->root.mnt != fs->root.mnt ||
462 nd->root.dentry != fs->root.dentry)
463 goto err_root;
464 }
465 spin_lock(&parent->d_lock);
466 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
467 if (!__d_rcu_to_refcount(dentry, nd->seq))
468 goto err;
469 /*
470 * If the sequence check on the child dentry passed, then the child has
471 * not been removed from its parent. This means the parent dentry must
472 * be valid and able to take a reference at this point.
473 */
474 BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
475 BUG_ON(!parent->d_count);
476 parent->d_count++;
477 spin_unlock(&dentry->d_lock);
478 spin_unlock(&parent->d_lock);
479 if (nd->root.mnt) {
480 path_get(&nd->root);
481 spin_unlock(&fs->lock);
482 }
483 mntget(nd->path.mnt);
484
485 rcu_read_unlock();
486 br_read_unlock(vfsmount_lock);
487 nd->flags &= ~LOOKUP_RCU;
488 return 0;
489err:
490 spin_unlock(&dentry->d_lock);
491 spin_unlock(&parent->d_lock);
492err_root:
493 if (nd->root.mnt)
494 spin_unlock(&fs->lock);
495 return -ECHILD;
496}
497
498/* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing. */
499static inline int nameidata_dentry_drop_rcu_maybe(struct nameidata *nd, struct dentry *dentry)
500{
Al Viroa7472ba2011-03-04 14:39:30 -0500501 if (nd->flags & LOOKUP_RCU) {
502 if (unlikely(nameidata_dentry_drop_rcu(nd, dentry))) {
503 nd->flags &= ~LOOKUP_RCU;
504 nd->root.mnt = NULL;
505 rcu_read_unlock();
506 br_read_unlock(vfsmount_lock);
507 return -ECHILD;
508 }
509 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100510 return 0;
511}
512
513/**
514 * nameidata_drop_rcu_last - drop nameidata ending path walk out of rcu-walk
515 * @nd: nameidata pathwalk data to drop
Randy Dunlap39191622011-01-08 19:36:21 -0800516 * Returns: 0 on success, -ECHILD on failure
Nick Piggin31e6b012011-01-07 17:49:52 +1100517 *
518 * nameidata_drop_rcu_last attempts to drop the current nd->path into ref-walk.
519 * nd->path should be the final element of the lookup, so nd->root is discarded.
520 * Must be called from rcu-walk context.
521 */
522static int nameidata_drop_rcu_last(struct nameidata *nd)
523{
524 struct dentry *dentry = nd->path.dentry;
525
526 BUG_ON(!(nd->flags & LOOKUP_RCU));
527 nd->flags &= ~LOOKUP_RCU;
528 nd->root.mnt = NULL;
529 spin_lock(&dentry->d_lock);
530 if (!__d_rcu_to_refcount(dentry, nd->seq))
531 goto err_unlock;
532 BUG_ON(nd->inode != dentry->d_inode);
533 spin_unlock(&dentry->d_lock);
534
535 mntget(nd->path.mnt);
536
537 rcu_read_unlock();
538 br_read_unlock(vfsmount_lock);
539
540 return 0;
541
542err_unlock:
543 spin_unlock(&dentry->d_lock);
544 rcu_read_unlock();
545 br_read_unlock(vfsmount_lock);
546 return -ECHILD;
547}
548
Nick Piggin31e6b012011-01-07 17:49:52 +1100549/**
Trond Myklebust834f2a42005-10-18 14:20:16 -0700550 * release_open_intent - free up open intent resources
551 * @nd: pointer to nameidata
552 */
553void release_open_intent(struct nameidata *nd)
554{
Linus Torvalds2dab5972011-02-11 15:53:38 -0800555 struct file *file = nd->intent.open.file;
556
557 if (file && !IS_ERR(file)) {
558 if (file->f_path.dentry == NULL)
559 put_filp(file);
560 else
561 fput(file);
562 }
Trond Myklebust834f2a42005-10-18 14:20:16 -0700563}
564
Al Virof60aef72011-02-15 01:35:28 -0500565static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
Nick Piggin34286d62011-01-07 17:49:57 +1100566{
Al Virof60aef72011-02-15 01:35:28 -0500567 return dentry->d_op->d_revalidate(dentry, nd);
Nick Piggin34286d62011-01-07 17:49:57 +1100568}
569
Al Virof5e1c1c2011-02-15 01:32:55 -0500570static struct dentry *
Ian Kentbcdc5e02006-09-27 01:50:44 -0700571do_revalidate(struct dentry *dentry, struct nameidata *nd)
572{
Al Virof5e1c1c2011-02-15 01:32:55 -0500573 int status = d_revalidate(dentry, nd);
Ian Kentbcdc5e02006-09-27 01:50:44 -0700574 if (unlikely(status <= 0)) {
575 /*
576 * The dentry failed validation.
577 * If d_revalidate returned 0 attempt to invalidate
578 * the dentry otherwise d_revalidate is asking us
579 * to return a fail status.
580 */
Nick Piggin34286d62011-01-07 17:49:57 +1100581 if (status < 0) {
Al Virof5e1c1c2011-02-15 01:32:55 -0500582 dput(dentry);
Nick Piggin34286d62011-01-07 17:49:57 +1100583 dentry = ERR_PTR(status);
Al Virof5e1c1c2011-02-15 01:32:55 -0500584 } else if (!d_invalidate(dentry)) {
585 dput(dentry);
586 dentry = NULL;
Ian Kentbcdc5e02006-09-27 01:50:44 -0700587 }
588 }
589 return dentry;
590}
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592/*
Al Viro16c2cd72011-02-22 15:50:10 -0500593 * handle_reval_path - force revalidation of a dentry
Jeff Layton39159de2009-12-07 12:01:50 -0500594 *
595 * In some situations the path walking code will trust dentries without
596 * revalidating them. This causes problems for filesystems that depend on
597 * d_revalidate to handle file opens (e.g. NFSv4). When FS_REVAL_DOT is set
598 * (which indicates that it's possible for the dentry to go stale), force
599 * a d_revalidate call before proceeding.
600 *
601 * Returns 0 if the revalidation was successful. If the revalidation fails,
602 * either return the error returned by d_revalidate or -ESTALE if the
603 * revalidation it just returned 0. If d_revalidate returns 0, we attempt to
604 * invalidate the dentry. It's up to the caller to handle putting references
605 * to the path if necessary.
606 */
Al Viro16c2cd72011-02-22 15:50:10 -0500607static inline int handle_reval_path(struct nameidata *nd)
Jeff Layton39159de2009-12-07 12:01:50 -0500608{
Al Viro16c2cd72011-02-22 15:50:10 -0500609 struct dentry *dentry = nd->path.dentry;
Jeff Layton39159de2009-12-07 12:01:50 -0500610 int status;
Jeff Layton39159de2009-12-07 12:01:50 -0500611
Al Viro16c2cd72011-02-22 15:50:10 -0500612 if (likely(!(nd->flags & LOOKUP_JUMPED)))
Jeff Layton39159de2009-12-07 12:01:50 -0500613 return 0;
614
Al Viro16c2cd72011-02-22 15:50:10 -0500615 if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
616 return 0;
617
618 if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
619 return 0;
620
621 /* Note: we do not d_invalidate() */
Nick Piggin34286d62011-01-07 17:49:57 +1100622 status = d_revalidate(dentry, nd);
Jeff Layton39159de2009-12-07 12:01:50 -0500623 if (status > 0)
624 return 0;
625
Al Viro16c2cd72011-02-22 15:50:10 -0500626 if (!status)
Jeff Layton39159de2009-12-07 12:01:50 -0500627 status = -ESTALE;
Al Viro16c2cd72011-02-22 15:50:10 -0500628
Jeff Layton39159de2009-12-07 12:01:50 -0500629 return status;
630}
631
632/*
Al Virob75b5082009-12-16 01:01:38 -0500633 * Short-cut version of permission(), for calling on directories
634 * during pathname resolution. Combines parts of permission()
635 * and generic_permission(), and tests ONLY for MAY_EXEC permission.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 *
637 * If appropriate, check DAC only. If not appropriate, or
Al Virob75b5082009-12-16 01:01:38 -0500638 * short-cut DAC fails, then call ->permission() to do more
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 * complete permission check.
640 */
Nick Pigginb74c79e2011-01-07 17:49:58 +1100641static inline int exec_permission(struct inode *inode, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700643 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Linus Torvaldscb9179e2009-08-28 11:08:31 -0700645 if (inode->i_op->permission) {
Nick Pigginb74c79e2011-01-07 17:49:58 +1100646 ret = inode->i_op->permission(inode, MAY_EXEC, flags);
647 } else {
648 ret = acl_permission_check(inode, MAY_EXEC, flags,
649 inode->i_op->check_acl);
Linus Torvaldscb9179e2009-08-28 11:08:31 -0700650 }
Nick Pigginb74c79e2011-01-07 17:49:58 +1100651 if (likely(!ret))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 goto ok;
Nick Pigginb74c79e2011-01-07 17:49:58 +1100653 if (ret == -ECHILD)
Nick Piggin31e6b012011-01-07 17:49:52 +1100654 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Linus Torvaldsf1ac9f62009-08-28 10:53:56 -0700656 if (capable(CAP_DAC_OVERRIDE) || capable(CAP_DAC_READ_SEARCH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 goto ok;
658
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700659 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660ok:
Nick Pigginb74c79e2011-01-07 17:49:58 +1100661 return security_inode_exec_permission(inode, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
663
Al Viro2a737872009-04-07 11:49:53 -0400664static __always_inline void set_root(struct nameidata *nd)
665{
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200666 if (!nd->root.mnt)
667 get_fs_root(current->fs, &nd->root);
Al Viro2a737872009-04-07 11:49:53 -0400668}
669
Al Viro6de88d72009-08-09 01:41:57 +0400670static int link_path_walk(const char *, struct nameidata *);
671
Nick Piggin31e6b012011-01-07 17:49:52 +1100672static __always_inline void set_root_rcu(struct nameidata *nd)
673{
674 if (!nd->root.mnt) {
675 struct fs_struct *fs = current->fs;
Nick Pigginc28cc362011-01-07 17:49:53 +1100676 unsigned seq;
677
678 do {
679 seq = read_seqcount_begin(&fs->seq);
680 nd->root = fs->root;
681 } while (read_seqcount_retry(&fs->seq, seq));
Nick Piggin31e6b012011-01-07 17:49:52 +1100682 }
683}
684
Arjan van de Venf1662352006-01-14 13:21:31 -0800685static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
Nick Piggin31e6b012011-01-07 17:49:52 +1100687 int ret;
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 if (IS_ERR(link))
690 goto fail;
691
692 if (*link == '/') {
Al Viro2a737872009-04-07 11:49:53 -0400693 set_root(nd);
Jan Blunck1d957f92008-02-14 19:34:35 -0800694 path_put(&nd->path);
Al Viro2a737872009-04-07 11:49:53 -0400695 nd->path = nd->root;
696 path_get(&nd->root);
Al Viro16c2cd72011-02-22 15:50:10 -0500697 nd->flags |= LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100699 nd->inode = nd->path.dentry->d_inode;
Christoph Hellwigb4091d52008-11-05 15:07:21 +0100700
Nick Piggin31e6b012011-01-07 17:49:52 +1100701 ret = link_path_walk(link, nd);
702 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703fail:
Jan Blunck1d957f92008-02-14 19:34:35 -0800704 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 return PTR_ERR(link);
706}
707
Jan Blunck1d957f92008-02-14 19:34:35 -0800708static void path_put_conditional(struct path *path, struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700709{
710 dput(path->dentry);
Jan Blunck4ac91372008-02-14 19:34:32 -0800711 if (path->mnt != nd->path.mnt)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700712 mntput(path->mnt);
713}
714
Nick Piggin7b9337a2011-01-14 08:42:43 +0000715static inline void path_to_nameidata(const struct path *path,
716 struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700717{
Nick Piggin31e6b012011-01-07 17:49:52 +1100718 if (!(nd->flags & LOOKUP_RCU)) {
719 dput(nd->path.dentry);
720 if (nd->path.mnt != path->mnt)
721 mntput(nd->path.mnt);
Huang Shijie9a229682010-04-02 17:37:13 +0800722 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100723 nd->path.mnt = path->mnt;
Jan Blunck4ac91372008-02-14 19:34:32 -0800724 nd->path.dentry = path->dentry;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700725}
726
Al Virodef4af32009-12-26 08:37:05 -0500727static __always_inline int
Nick Piggin7b9337a2011-01-14 08:42:43 +0000728__do_follow_link(const struct path *link, struct nameidata *nd, void **p)
Ian Kent051d3812006-03-27 01:14:53 -0800729{
730 int error;
Nick Piggin7b9337a2011-01-14 08:42:43 +0000731 struct dentry *dentry = link->dentry;
Ian Kent051d3812006-03-27 01:14:53 -0800732
Al Viro844a3912011-02-15 00:38:26 -0500733 BUG_ON(nd->flags & LOOKUP_RCU);
734
Nick Piggin7b9337a2011-01-14 08:42:43 +0000735 touch_atime(link->mnt, dentry);
Ian Kent051d3812006-03-27 01:14:53 -0800736 nd_set_link(nd, NULL);
737
David Howells87556ef2011-01-14 18:46:46 +0000738 if (link->mnt == nd->path.mnt)
739 mntget(link->mnt);
Nick Piggin31e6b012011-01-07 17:49:52 +1100740
Al Viro36f3b4f2011-02-22 21:24:38 -0500741 error = security_inode_follow_link(link->dentry, nd);
742 if (error) {
743 *p = ERR_PTR(error); /* no ->put_link(), please */
744 path_put(&nd->path);
745 return error;
746 }
747
Al Viro86acdca12009-12-22 23:45:11 -0500748 nd->last_type = LAST_BIND;
Al Virodef4af32009-12-26 08:37:05 -0500749 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
750 error = PTR_ERR(*p);
751 if (!IS_ERR(*p)) {
Ian Kent051d3812006-03-27 01:14:53 -0800752 char *s = nd_get_link(nd);
753 error = 0;
754 if (s)
755 error = __vfs_follow_link(nd, s);
Al Viro16c2cd72011-02-22 15:50:10 -0500756 else if (nd->last_type == LAST_BIND)
757 nd->flags |= LOOKUP_JUMPED;
Ian Kent051d3812006-03-27 01:14:53 -0800758 }
Ian Kent051d3812006-03-27 01:14:53 -0800759 return error;
760}
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762/*
763 * This limits recursive symlink follows to 8, while
764 * limiting consecutive symlinks to 40.
765 *
766 * Without that kind of total limit, nasty chains of consecutive
767 * symlinks can cause almost arbitrarily long lookups.
768 */
Linus Torvalds3abb17e2011-02-16 08:56:55 -0800769static inline int do_follow_link(struct inode *inode, struct path *path, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
Al Virodef4af32009-12-26 08:37:05 -0500771 void *cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 int err = -ELOOP;
Al Viro844a3912011-02-15 00:38:26 -0500773
774 /* We drop rcu-walk here */
775 if (nameidata_dentry_drop_rcu_maybe(nd, path->dentry))
776 return -ECHILD;
Linus Torvalds3abb17e2011-02-16 08:56:55 -0800777 BUG_ON(inode != path->dentry->d_inode);
Al Viro844a3912011-02-15 00:38:26 -0500778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 if (current->link_count >= MAX_NESTED_LINKS)
780 goto loop;
781 if (current->total_link_count >= 40)
782 goto loop;
783 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
784 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 current->link_count++;
786 current->total_link_count++;
787 nd->depth++;
Al Virodef4af32009-12-26 08:37:05 -0500788 err = __do_follow_link(path, nd, &cookie);
789 if (!IS_ERR(cookie) && path->dentry->d_inode->i_op->put_link)
790 path->dentry->d_inode->i_op->put_link(path->dentry, nd, cookie);
Al Viro258fa992009-08-09 01:32:02 +0400791 path_put(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 current->link_count--;
793 nd->depth--;
794 return err;
795loop:
Jan Blunck1d957f92008-02-14 19:34:35 -0800796 path_put_conditional(path, nd);
797 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return err;
799}
800
Nick Piggin31e6b012011-01-07 17:49:52 +1100801static int follow_up_rcu(struct path *path)
802{
803 struct vfsmount *parent;
804 struct dentry *mountpoint;
805
806 parent = path->mnt->mnt_parent;
807 if (parent == path->mnt)
808 return 0;
809 mountpoint = path->mnt->mnt_mountpoint;
810 path->dentry = mountpoint;
811 path->mnt = parent;
812 return 1;
813}
814
Al Virobab77eb2009-04-18 03:26:48 -0400815int follow_up(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
817 struct vfsmount *parent;
818 struct dentry *mountpoint;
Nick Piggin99b7db72010-08-18 04:37:39 +1000819
820 br_read_lock(vfsmount_lock);
Al Virobab77eb2009-04-18 03:26:48 -0400821 parent = path->mnt->mnt_parent;
822 if (parent == path->mnt) {
Nick Piggin99b7db72010-08-18 04:37:39 +1000823 br_read_unlock(vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return 0;
825 }
826 mntget(parent);
Al Virobab77eb2009-04-18 03:26:48 -0400827 mountpoint = dget(path->mnt->mnt_mountpoint);
Nick Piggin99b7db72010-08-18 04:37:39 +1000828 br_read_unlock(vfsmount_lock);
Al Virobab77eb2009-04-18 03:26:48 -0400829 dput(path->dentry);
830 path->dentry = mountpoint;
831 mntput(path->mnt);
832 path->mnt = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 return 1;
834}
835
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100836/*
David Howells9875cf82011-01-14 18:45:21 +0000837 * Perform an automount
838 * - return -EISDIR to tell follow_managed() to stop and return the path we
839 * were called with.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 */
David Howells9875cf82011-01-14 18:45:21 +0000841static int follow_automount(struct path *path, unsigned flags,
842 bool *need_mntput)
Nick Piggin31e6b012011-01-07 17:49:52 +1100843{
David Howells9875cf82011-01-14 18:45:21 +0000844 struct vfsmount *mnt;
David Howellsea5b7782011-01-14 19:10:03 +0000845 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +1100846
David Howells9875cf82011-01-14 18:45:21 +0000847 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
848 return -EREMOTE;
Al Viro463ffb22005-06-06 13:36:05 -0700849
David Howells6f45b652011-01-14 18:45:31 +0000850 /* We don't want to mount if someone supplied AT_NO_AUTOMOUNT
851 * and this is the terminal part of the path.
852 */
853 if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_CONTINUE))
854 return -EISDIR; /* we actually want to stop here */
855
David Howells9875cf82011-01-14 18:45:21 +0000856 /* We want to mount if someone is trying to open/create a file of any
857 * type under the mountpoint, wants to traverse through the mountpoint
858 * or wants to open the mounted directory.
859 *
860 * We don't want to mount if someone's just doing a stat and they've
861 * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and
862 * appended a '/' to the name.
863 */
864 if (!(flags & LOOKUP_FOLLOW) &&
865 !(flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY |
866 LOOKUP_OPEN | LOOKUP_CREATE)))
867 return -EISDIR;
868
869 current->total_link_count++;
870 if (current->total_link_count >= 40)
871 return -ELOOP;
872
873 mnt = path->dentry->d_op->d_automount(path);
874 if (IS_ERR(mnt)) {
875 /*
876 * The filesystem is allowed to return -EISDIR here to indicate
877 * it doesn't want to automount. For instance, autofs would do
878 * this so that its userspace daemon can mount on this dentry.
879 *
880 * However, we can only permit this if it's a terminal point in
881 * the path being looked up; if it wasn't then the remainder of
882 * the path is inaccessible and we should say so.
883 */
884 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_CONTINUE))
885 return -EREMOTE;
886 return PTR_ERR(mnt);
887 }
David Howellsea5b7782011-01-14 19:10:03 +0000888
David Howells9875cf82011-01-14 18:45:21 +0000889 if (!mnt) /* mount collision */
890 return 0;
891
Al Viro19a167a2011-01-17 01:35:23 -0500892 err = finish_automount(mnt, path);
David Howellsea5b7782011-01-14 19:10:03 +0000893
David Howellsea5b7782011-01-14 19:10:03 +0000894 switch (err) {
895 case -EBUSY:
896 /* Someone else made a mount here whilst we were busy */
Al Viro19a167a2011-01-17 01:35:23 -0500897 return 0;
David Howellsea5b7782011-01-14 19:10:03 +0000898 case 0:
David Howellsea5b7782011-01-14 19:10:03 +0000899 dput(path->dentry);
900 if (*need_mntput)
901 mntput(path->mnt);
902 path->mnt = mnt;
903 path->dentry = dget(mnt->mnt_root);
904 *need_mntput = true;
905 return 0;
Al Viro19a167a2011-01-17 01:35:23 -0500906 default:
907 return err;
David Howellsea5b7782011-01-14 19:10:03 +0000908 }
Al Viro19a167a2011-01-17 01:35:23 -0500909
David Howells9875cf82011-01-14 18:45:21 +0000910}
911
912/*
913 * Handle a dentry that is managed in some way.
David Howellscc53ce52011-01-14 18:45:26 +0000914 * - Flagged for transit management (autofs)
David Howells9875cf82011-01-14 18:45:21 +0000915 * - Flagged as mountpoint
916 * - Flagged as automount point
917 *
918 * This may only be called in refwalk mode.
919 *
920 * Serialization is taken care of in namespace.c
921 */
922static int follow_managed(struct path *path, unsigned flags)
923{
924 unsigned managed;
925 bool need_mntput = false;
926 int ret;
927
928 /* Given that we're not holding a lock here, we retain the value in a
929 * local variable for each dentry as we look at it so that we don't see
930 * the components of that value change under us */
931 while (managed = ACCESS_ONCE(path->dentry->d_flags),
932 managed &= DCACHE_MANAGED_DENTRY,
933 unlikely(managed != 0)) {
David Howellscc53ce52011-01-14 18:45:26 +0000934 /* Allow the filesystem to manage the transit without i_mutex
935 * being held. */
936 if (managed & DCACHE_MANAGE_TRANSIT) {
937 BUG_ON(!path->dentry->d_op);
938 BUG_ON(!path->dentry->d_op->d_manage);
David Howellsab909112011-01-14 18:46:51 +0000939 ret = path->dentry->d_op->d_manage(path->dentry,
940 false, false);
David Howellscc53ce52011-01-14 18:45:26 +0000941 if (ret < 0)
942 return ret == -EISDIR ? 0 : ret;
943 }
944
David Howells9875cf82011-01-14 18:45:21 +0000945 /* Transit to a mounted filesystem. */
946 if (managed & DCACHE_MOUNTED) {
947 struct vfsmount *mounted = lookup_mnt(path);
948 if (mounted) {
949 dput(path->dentry);
950 if (need_mntput)
951 mntput(path->mnt);
952 path->mnt = mounted;
953 path->dentry = dget(mounted->mnt_root);
954 need_mntput = true;
955 continue;
956 }
957
958 /* Something is mounted on this dentry in another
959 * namespace and/or whatever was mounted there in this
960 * namespace got unmounted before we managed to get the
961 * vfsmount_lock */
962 }
963
964 /* Handle an automount point */
965 if (managed & DCACHE_NEED_AUTOMOUNT) {
966 ret = follow_automount(path, flags, &need_mntput);
967 if (ret < 0)
968 return ret == -EISDIR ? 0 : ret;
969 continue;
970 }
971
972 /* We didn't change the current path point */
973 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 }
David Howells9875cf82011-01-14 18:45:21 +0000975 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976}
977
David Howellscc53ce52011-01-14 18:45:26 +0000978int follow_down_one(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
980 struct vfsmount *mounted;
981
Al Viro1c755af2009-04-18 14:06:57 -0400982 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -0400984 dput(path->dentry);
985 mntput(path->mnt);
986 path->mnt = mounted;
987 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 return 1;
989 }
990 return 0;
991}
992
David Howells9875cf82011-01-14 18:45:21 +0000993/*
994 * Skip to top of mountpoint pile in rcuwalk mode. We abort the rcu-walk if we
David Howellscc53ce52011-01-14 18:45:26 +0000995 * meet a managed dentry and we're not walking to "..". True is returned to
David Howells9875cf82011-01-14 18:45:21 +0000996 * continue, false to abort.
997 */
998static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
999 struct inode **inode, bool reverse_transit)
1000{
1001 while (d_mountpoint(path->dentry)) {
1002 struct vfsmount *mounted;
David Howellsab909112011-01-14 18:46:51 +00001003 if (unlikely(path->dentry->d_flags & DCACHE_MANAGE_TRANSIT) &&
1004 !reverse_transit &&
1005 path->dentry->d_op->d_manage(path->dentry, false, true) < 0)
1006 return false;
David Howells9875cf82011-01-14 18:45:21 +00001007 mounted = __lookup_mnt(path->mnt, path->dentry, 1);
1008 if (!mounted)
1009 break;
1010 path->mnt = mounted;
1011 path->dentry = mounted->mnt_root;
1012 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
1013 *inode = path->dentry->d_inode;
1014 }
1015
1016 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1017 return reverse_transit;
1018 return true;
1019}
1020
Nick Piggin31e6b012011-01-07 17:49:52 +11001021static int follow_dotdot_rcu(struct nameidata *nd)
1022{
1023 struct inode *inode = nd->inode;
1024
1025 set_root_rcu(nd);
1026
David Howells9875cf82011-01-14 18:45:21 +00001027 while (1) {
Nick Piggin31e6b012011-01-07 17:49:52 +11001028 if (nd->path.dentry == nd->root.dentry &&
1029 nd->path.mnt == nd->root.mnt) {
1030 break;
1031 }
1032 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1033 struct dentry *old = nd->path.dentry;
1034 struct dentry *parent = old->d_parent;
1035 unsigned seq;
1036
1037 seq = read_seqcount_begin(&parent->d_seq);
1038 if (read_seqcount_retry(&old->d_seq, nd->seq))
Al Viroef7562d2011-03-04 14:35:59 -05001039 goto failed;
Nick Piggin31e6b012011-01-07 17:49:52 +11001040 inode = parent->d_inode;
1041 nd->path.dentry = parent;
1042 nd->seq = seq;
1043 break;
1044 }
1045 if (!follow_up_rcu(&nd->path))
1046 break;
1047 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
1048 inode = nd->path.dentry->d_inode;
1049 }
David Howells9875cf82011-01-14 18:45:21 +00001050 __follow_mount_rcu(nd, &nd->path, &inode, true);
Nick Piggin31e6b012011-01-07 17:49:52 +11001051 nd->inode = inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001052 return 0;
Al Viroef7562d2011-03-04 14:35:59 -05001053
1054failed:
1055 nd->flags &= ~LOOKUP_RCU;
1056 nd->root.mnt = NULL;
1057 rcu_read_unlock();
1058 br_read_unlock(vfsmount_lock);
1059 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001060}
1061
David Howells9875cf82011-01-14 18:45:21 +00001062/*
David Howellscc53ce52011-01-14 18:45:26 +00001063 * Follow down to the covering mount currently visible to userspace. At each
1064 * point, the filesystem owning that dentry may be queried as to whether the
1065 * caller is permitted to proceed or not.
1066 *
1067 * Care must be taken as namespace_sem may be held (indicated by mounting_here
1068 * being true).
1069 */
1070int follow_down(struct path *path, bool mounting_here)
1071{
1072 unsigned managed;
1073 int ret;
1074
1075 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1076 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1077 /* Allow the filesystem to manage the transit without i_mutex
1078 * being held.
1079 *
1080 * We indicate to the filesystem if someone is trying to mount
1081 * something here. This gives autofs the chance to deny anyone
1082 * other than its daemon the right to mount on its
1083 * superstructure.
1084 *
1085 * The filesystem may sleep at this point.
1086 */
1087 if (managed & DCACHE_MANAGE_TRANSIT) {
1088 BUG_ON(!path->dentry->d_op);
1089 BUG_ON(!path->dentry->d_op->d_manage);
David Howellsab909112011-01-14 18:46:51 +00001090 ret = path->dentry->d_op->d_manage(
1091 path->dentry, mounting_here, false);
David Howellscc53ce52011-01-14 18:45:26 +00001092 if (ret < 0)
1093 return ret == -EISDIR ? 0 : ret;
1094 }
1095
1096 /* Transit to a mounted filesystem. */
1097 if (managed & DCACHE_MOUNTED) {
1098 struct vfsmount *mounted = lookup_mnt(path);
1099 if (!mounted)
1100 break;
1101 dput(path->dentry);
1102 mntput(path->mnt);
1103 path->mnt = mounted;
1104 path->dentry = dget(mounted->mnt_root);
1105 continue;
1106 }
1107
1108 /* Don't handle automount points here */
1109 break;
1110 }
1111 return 0;
1112}
1113
1114/*
David Howells9875cf82011-01-14 18:45:21 +00001115 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1116 */
1117static void follow_mount(struct path *path)
1118{
1119 while (d_mountpoint(path->dentry)) {
1120 struct vfsmount *mounted = lookup_mnt(path);
1121 if (!mounted)
1122 break;
1123 dput(path->dentry);
1124 mntput(path->mnt);
1125 path->mnt = mounted;
1126 path->dentry = dget(mounted->mnt_root);
1127 }
1128}
1129
Nick Piggin31e6b012011-01-07 17:49:52 +11001130static void follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
Al Viro2a737872009-04-07 11:49:53 -04001132 set_root(nd);
Andreas Mohre518ddb2006-09-29 02:01:22 -07001133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 while(1) {
Jan Blunck4ac91372008-02-14 19:34:32 -08001135 struct dentry *old = nd->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Al Viro2a737872009-04-07 11:49:53 -04001137 if (nd->path.dentry == nd->root.dentry &&
1138 nd->path.mnt == nd->root.mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 break;
1140 }
Jan Blunck4ac91372008-02-14 19:34:32 -08001141 if (nd->path.dentry != nd->path.mnt->mnt_root) {
Al Viro3088dd72010-01-30 15:47:29 -05001142 /* rare case of legitimate dget_parent()... */
1143 nd->path.dentry = dget_parent(nd->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 dput(old);
1145 break;
1146 }
Al Viro3088dd72010-01-30 15:47:29 -05001147 if (!follow_up(&nd->path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 }
Al Viro79ed0222009-04-18 13:59:41 -04001150 follow_mount(&nd->path);
Nick Piggin31e6b012011-01-07 17:49:52 +11001151 nd->inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152}
1153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154/*
Nick Pigginbaa03892010-08-18 04:37:31 +10001155 * Allocate a dentry with name and parent, and perform a parent
1156 * directory ->lookup on it. Returns the new dentry, or ERR_PTR
1157 * on error. parent->d_inode->i_mutex must be held. d_lookup must
1158 * have verified that no child exists while under i_mutex.
1159 */
1160static struct dentry *d_alloc_and_lookup(struct dentry *parent,
1161 struct qstr *name, struct nameidata *nd)
1162{
1163 struct inode *inode = parent->d_inode;
1164 struct dentry *dentry;
1165 struct dentry *old;
1166
1167 /* Don't create child dentry for a dead directory. */
1168 if (unlikely(IS_DEADDIR(inode)))
1169 return ERR_PTR(-ENOENT);
1170
1171 dentry = d_alloc(parent, name);
1172 if (unlikely(!dentry))
1173 return ERR_PTR(-ENOMEM);
1174
1175 old = inode->i_op->lookup(inode, dentry, nd);
1176 if (unlikely(old)) {
1177 dput(dentry);
1178 dentry = old;
1179 }
1180 return dentry;
1181}
1182
1183/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 * It's more convoluted than I'd like it to be, but... it's still fairly
1185 * small and for now I'd prefer to have fast path as straight as possible.
1186 * It _is_ time-critical.
1187 */
1188static int do_lookup(struct nameidata *nd, struct qstr *name,
Nick Piggin31e6b012011-01-07 17:49:52 +11001189 struct path *path, struct inode **inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
Jan Blunck4ac91372008-02-14 19:34:32 -08001191 struct vfsmount *mnt = nd->path.mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +11001192 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro5a18fff2011-03-11 04:44:53 -05001193 int need_reval = 1;
1194 int status = 1;
David Howells9875cf82011-01-14 18:45:21 +00001195 int err;
1196
Al Viro3cac2602009-08-13 18:27:43 +04001197 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001198 * Rename seqlock is not required here because in the off chance
1199 * of a false negative due to a concurrent rename, we're going to
1200 * do the non-racy lookup, below.
1201 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001202 if (nd->flags & LOOKUP_RCU) {
1203 unsigned seq;
Nick Piggin31e6b012011-01-07 17:49:52 +11001204 *inode = nd->inode;
1205 dentry = __d_lookup_rcu(parent, name, &seq, inode);
Al Viro5a18fff2011-03-11 04:44:53 -05001206 if (!dentry)
1207 goto unlazy;
1208
Nick Piggin31e6b012011-01-07 17:49:52 +11001209 /* Memory barrier in read_seqcount_begin of child is enough */
1210 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1211 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001212 nd->seq = seq;
Al Viro5a18fff2011-03-11 04:44:53 -05001213
Al Viro24643082011-02-15 01:26:22 -05001214 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
Al Viro5a18fff2011-03-11 04:44:53 -05001215 status = d_revalidate(dentry, nd);
1216 if (unlikely(status <= 0)) {
1217 if (status != -ECHILD)
1218 need_reval = 0;
1219 goto unlazy;
1220 }
Al Viro24643082011-02-15 01:26:22 -05001221 }
Nick Piggin31e6b012011-01-07 17:49:52 +11001222 path->mnt = mnt;
1223 path->dentry = dentry;
David Howells9875cf82011-01-14 18:45:21 +00001224 if (likely(__follow_mount_rcu(nd, path, inode, false)))
1225 return 0;
Al Viro5a18fff2011-03-11 04:44:53 -05001226unlazy:
1227 if (dentry) {
1228 if (nameidata_dentry_drop_rcu(nd, dentry))
1229 return -ECHILD;
1230 } else {
1231 if (nameidata_drop_rcu(nd))
1232 return -ECHILD;
1233 }
1234 } else {
1235 dentry = __d_lookup(parent, name);
Nick Piggin31e6b012011-01-07 17:49:52 +11001236 }
Al Viro5a18fff2011-03-11 04:44:53 -05001237
1238retry:
1239 if (unlikely(!dentry)) {
1240 struct inode *dir = parent->d_inode;
1241 BUG_ON(nd->inode != dir);
1242
1243 mutex_lock(&dir->i_mutex);
1244 dentry = d_lookup(parent, name);
1245 if (likely(!dentry)) {
1246 dentry = d_alloc_and_lookup(parent, name, nd);
1247 if (IS_ERR(dentry)) {
1248 mutex_unlock(&dir->i_mutex);
1249 return PTR_ERR(dentry);
1250 }
1251 /* known good */
1252 need_reval = 0;
1253 status = 1;
1254 }
1255 mutex_unlock(&dir->i_mutex);
Al Viro24643082011-02-15 01:26:22 -05001256 }
Al Viro5a18fff2011-03-11 04:44:53 -05001257 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
1258 status = d_revalidate(dentry, nd);
1259 if (unlikely(status <= 0)) {
1260 if (status < 0) {
1261 dput(dentry);
1262 return status;
1263 }
1264 if (!d_invalidate(dentry)) {
1265 dput(dentry);
1266 dentry = NULL;
1267 need_reval = 1;
1268 goto retry;
1269 }
1270 }
1271
David Howells9875cf82011-01-14 18:45:21 +00001272 path->mnt = mnt;
1273 path->dentry = dentry;
1274 err = follow_managed(path, nd->flags);
Ian Kent89312212011-01-18 12:06:10 +08001275 if (unlikely(err < 0)) {
1276 path_put_conditional(path, nd);
David Howells9875cf82011-01-14 18:45:21 +00001277 return err;
Ian Kent89312212011-01-18 12:06:10 +08001278 }
David Howells9875cf82011-01-14 18:45:21 +00001279 *inode = path->dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281}
1282
Al Viro52094c82011-02-21 21:34:47 -05001283static inline int may_lookup(struct nameidata *nd)
1284{
1285 if (nd->flags & LOOKUP_RCU) {
1286 int err = exec_permission(nd->inode, IPERM_FLAG_RCU);
1287 if (err != -ECHILD)
1288 return err;
1289 if (nameidata_drop_rcu(nd))
1290 return -ECHILD;
1291 }
1292 return exec_permission(nd->inode, 0);
1293}
1294
Al Viro9856fa12011-03-04 14:22:06 -05001295static inline int handle_dots(struct nameidata *nd, int type)
1296{
1297 if (type == LAST_DOTDOT) {
1298 if (nd->flags & LOOKUP_RCU) {
1299 if (follow_dotdot_rcu(nd))
1300 return -ECHILD;
1301 } else
1302 follow_dotdot(nd);
1303 }
1304 return 0;
1305}
1306
Al Viro951361f2011-03-04 14:44:37 -05001307static void terminate_walk(struct nameidata *nd)
1308{
1309 if (!(nd->flags & LOOKUP_RCU)) {
1310 path_put(&nd->path);
1311 } else {
1312 nd->flags &= ~LOOKUP_RCU;
1313 nd->root.mnt = NULL;
1314 rcu_read_unlock();
1315 br_read_unlock(vfsmount_lock);
1316 }
1317}
1318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319/*
1320 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01001321 * This is the basic name resolution function, turning a pathname into
1322 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01001324 * Returns 0 and nd will have valid dentry and mnt on success.
1325 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 */
Al Viro6de88d72009-08-09 01:41:57 +04001327static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
1329 struct path next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 int err;
1331 unsigned int lookup_flags = nd->flags;
1332
1333 while (*name=='/')
1334 name++;
1335 if (!*name)
Al Viro086e1832011-02-22 20:56:27 -05001336 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 if (nd->depth)
Trond Myklebustf55eab82006-02-04 23:28:01 -08001339 lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341 /* At this point we know we have a real path component. */
1342 for(;;) {
Nick Piggin31e6b012011-01-07 17:49:52 +11001343 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 unsigned long hash;
1345 struct qstr this;
1346 unsigned int c;
Al Virofe479a52011-02-22 15:10:03 -05001347 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Trond Myklebustcdce5d62005-10-18 14:20:18 -07001349 nd->flags |= LOOKUP_CONTINUE;
Al Viro52094c82011-02-21 21:34:47 -05001350
1351 err = may_lookup(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 if (err)
1353 break;
1354
1355 this.name = name;
1356 c = *(const unsigned char *)name;
1357
1358 hash = init_name_hash();
1359 do {
1360 name++;
1361 hash = partial_name_hash(c, hash);
1362 c = *(const unsigned char *)name;
1363 } while (c && (c != '/'));
1364 this.len = name - (const char *) this.name;
1365 this.hash = end_name_hash(hash);
1366
Al Virofe479a52011-02-22 15:10:03 -05001367 type = LAST_NORM;
1368 if (this.name[0] == '.') switch (this.len) {
1369 case 2:
Al Viro16c2cd72011-02-22 15:50:10 -05001370 if (this.name[1] == '.') {
Al Virofe479a52011-02-22 15:10:03 -05001371 type = LAST_DOTDOT;
Al Viro16c2cd72011-02-22 15:50:10 -05001372 nd->flags |= LOOKUP_JUMPED;
1373 }
Al Virofe479a52011-02-22 15:10:03 -05001374 break;
1375 case 1:
1376 type = LAST_DOT;
1377 }
Al Viro5a202bc2011-03-08 14:17:44 -05001378 if (likely(type == LAST_NORM)) {
1379 struct dentry *parent = nd->path.dentry;
Al Viro16c2cd72011-02-22 15:50:10 -05001380 nd->flags &= ~LOOKUP_JUMPED;
Al Viro5a202bc2011-03-08 14:17:44 -05001381 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1382 err = parent->d_op->d_hash(parent, nd->inode,
1383 &this);
1384 if (err < 0)
1385 break;
1386 }
1387 }
Al Virofe479a52011-02-22 15:10:03 -05001388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 /* remove trailing slashes? */
1390 if (!c)
1391 goto last_component;
1392 while (*++name == '/');
1393 if (!*name)
1394 goto last_with_slashes;
1395
1396 /*
1397 * "." and ".." are special - ".." especially so because it has
1398 * to be able to know about the current root directory and
1399 * parent relationships.
1400 */
Al Virofe479a52011-02-22 15:10:03 -05001401 if (unlikely(type != LAST_NORM)) {
Al Viroef7562d2011-03-04 14:35:59 -05001402 if (handle_dots(nd, type))
1403 return -ECHILD;
Al Virofe479a52011-02-22 15:10:03 -05001404 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 }
Al Virofe479a52011-02-22 15:10:03 -05001406
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 /* This does the actual lookups.. */
Nick Piggin31e6b012011-01-07 17:49:52 +11001408 err = do_lookup(nd, &this, &next, &inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 if (err)
1410 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Al Viro7bc055d2011-02-23 19:41:31 -05001412 if (inode && inode->i_op->follow_link) {
Linus Torvalds3abb17e2011-02-16 08:56:55 -08001413 err = do_follow_link(inode, &next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 if (err)
Al Viroa7472ba2011-03-04 14:39:30 -05001415 return err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001416 nd->inode = nd->path.dentry->d_inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001417 } else {
Miklos Szeredi09dd17d2005-09-06 15:18:21 -07001418 path_to_nameidata(&next, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11001419 nd->inode = inode;
1420 }
Al Viro7bc055d2011-02-23 19:41:31 -05001421 err = -ENOENT;
1422 if (!nd->inode)
1423 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 err = -ENOTDIR;
Nick Piggin31e6b012011-01-07 17:49:52 +11001425 if (!nd->inode->i_op->lookup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 break;
1427 continue;
1428 /* here ends the main loop */
1429
1430last_with_slashes:
1431 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1432last_component:
Trond Myklebustf55eab82006-02-04 23:28:01 -08001433 /* Clear LOOKUP_CONTINUE iff it was previously unset */
1434 nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 if (lookup_flags & LOOKUP_PARENT)
1436 goto lookup_parent;
Al Viroef7562d2011-03-04 14:35:59 -05001437 if (unlikely(type != LAST_NORM))
1438 return handle_dots(nd, type);
Nick Piggin31e6b012011-01-07 17:49:52 +11001439 err = do_lookup(nd, &this, &next, &inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 if (err)
1441 break;
David Howellsdb372912011-01-14 18:45:53 +00001442 if (inode && unlikely(inode->i_op->follow_link) &&
1443 (lookup_flags & LOOKUP_FOLLOW)) {
Linus Torvalds3abb17e2011-02-16 08:56:55 -08001444 err = do_follow_link(inode, &next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 if (err)
Al Viroa7472ba2011-03-04 14:39:30 -05001446 return err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001447 nd->inode = nd->path.dentry->d_inode;
1448 } else {
Miklos Szeredi09dd17d2005-09-06 15:18:21 -07001449 path_to_nameidata(&next, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11001450 nd->inode = inode;
1451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 err = -ENOENT;
Nick Piggin31e6b012011-01-07 17:49:52 +11001453 if (!nd->inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 break;
1455 if (lookup_flags & LOOKUP_DIRECTORY) {
1456 err = -ENOTDIR;
Nick Piggin31e6b012011-01-07 17:49:52 +11001457 if (!nd->inode->i_op->lookup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 break;
1459 }
Al Viro086e1832011-02-22 20:56:27 -05001460 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461lookup_parent:
1462 nd->last = this;
Al Virofe479a52011-02-22 15:10:03 -05001463 nd->last_type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 }
Al Viro951361f2011-03-04 14:44:37 -05001466 terminate_walk(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 return err;
1468}
1469
Al Viro70e9b352011-03-05 21:12:22 -05001470static int path_init(int dfd, const char *name, unsigned int flags,
1471 struct nameidata *nd, struct file **fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472{
Prasanna Medaea3834d2005-04-29 16:00:17 +01001473 int retval = 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001474 int fput_needed;
1475 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
1477 nd->last_type = LAST_ROOT; /* if there are only slashes... */
Al Viro16c2cd72011-02-22 15:50:10 -05001478 nd->flags = flags | LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 nd->depth = 0;
Al Viro2a737872009-04-07 11:49:53 -04001480 nd->root.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 if (*name=='/') {
Al Viroe41f7d42011-02-22 14:02:58 -05001483 if (flags & LOOKUP_RCU) {
1484 br_read_lock(vfsmount_lock);
1485 rcu_read_lock();
1486 set_root_rcu(nd);
1487 } else {
1488 set_root(nd);
1489 path_get(&nd->root);
1490 }
Al Viro2a737872009-04-07 11:49:53 -04001491 nd->path = nd->root;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001492 } else if (dfd == AT_FDCWD) {
Al Viroe41f7d42011-02-22 14:02:58 -05001493 if (flags & LOOKUP_RCU) {
1494 struct fs_struct *fs = current->fs;
1495 unsigned seq;
1496
1497 br_read_lock(vfsmount_lock);
1498 rcu_read_lock();
1499
1500 do {
1501 seq = read_seqcount_begin(&fs->seq);
1502 nd->path = fs->pwd;
1503 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1504 } while (read_seqcount_retry(&fs->seq, seq));
1505 } else {
1506 get_fs_pwd(current->fs, &nd->path);
1507 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001508 } else {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001509 struct dentry *dentry;
1510
1511 file = fget_light(dfd, &fput_needed);
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001512 retval = -EBADF;
1513 if (!file)
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001514 goto out_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001515
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001516 dentry = file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001517
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001518 retval = -ENOTDIR;
1519 if (!S_ISDIR(dentry->d_inode->i_mode))
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001520 goto fput_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001521
1522 retval = file_permission(file, MAY_EXEC);
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001523 if (retval)
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001524 goto fput_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001525
Jan Blunck5dd784d02008-02-14 19:34:38 -08001526 nd->path = file->f_path;
Al Viroe41f7d42011-02-22 14:02:58 -05001527 if (flags & LOOKUP_RCU) {
1528 if (fput_needed)
Al Viro70e9b352011-03-05 21:12:22 -05001529 *fp = file;
Al Viroe41f7d42011-02-22 14:02:58 -05001530 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1531 br_read_lock(vfsmount_lock);
1532 rcu_read_lock();
1533 } else {
1534 path_get(&file->f_path);
1535 fput_light(file, fput_needed);
1536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 }
Al Viroe41f7d42011-02-22 14:02:58 -05001538
Nick Piggin31e6b012011-01-07 17:49:52 +11001539 nd->inode = nd->path.dentry->d_inode;
Al Viro9b4a9b12009-04-07 11:44:16 -04001540 return 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001541
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001542fput_fail:
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001543 fput_light(file, fput_needed);
Al Viro9b4a9b12009-04-07 11:44:16 -04001544out_fail:
1545 return retval;
1546}
1547
1548/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroee0827c2011-02-21 23:38:09 -05001549static int path_lookupat(int dfd, const char *name,
Al Viro9b4a9b12009-04-07 11:44:16 -04001550 unsigned int flags, struct nameidata *nd)
1551{
Al Viro70e9b352011-03-05 21:12:22 -05001552 struct file *base = NULL;
Nick Piggin31e6b012011-01-07 17:49:52 +11001553 int retval;
1554
1555 /*
1556 * Path walking is largely split up into 2 different synchronisation
1557 * schemes, rcu-walk and ref-walk (explained in
1558 * Documentation/filesystems/path-lookup.txt). These share much of the
1559 * path walk code, but some things particularly setup, cleanup, and
1560 * following mounts are sufficiently divergent that functions are
1561 * duplicated. Typically there is a function foo(), and its RCU
1562 * analogue, foo_rcu().
1563 *
1564 * -ECHILD is the error number of choice (just to avoid clashes) that
1565 * is returned if some aspect of an rcu-walk fails. Such an error must
1566 * be handled by restarting a traditional ref-walk (which will always
1567 * be able to complete).
1568 */
Al Viro70e9b352011-03-05 21:12:22 -05001569 retval = path_init(dfd, name, flags, nd, &base);
Al Viroee0827c2011-02-21 23:38:09 -05001570
Nick Piggin31e6b012011-01-07 17:49:52 +11001571 if (unlikely(retval))
1572 return retval;
Al Viroee0827c2011-02-21 23:38:09 -05001573
1574 current->total_link_count = 0;
1575 retval = link_path_walk(name, nd);
1576
1577 if (nd->flags & LOOKUP_RCU) {
Al Viro4455ca62011-03-04 14:28:10 -05001578 /* went all way through without dropping RCU */
1579 BUG_ON(retval);
1580 if (nameidata_drop_rcu_last(nd))
1581 retval = -ECHILD;
Al Viroee0827c2011-02-21 23:38:09 -05001582 }
1583
Al Viro16c2cd72011-02-22 15:50:10 -05001584 if (!retval)
1585 retval = handle_reval_path(nd);
1586
Al Viro70e9b352011-03-05 21:12:22 -05001587 if (base)
1588 fput(base);
Al Viroee0827c2011-02-21 23:38:09 -05001589
Al Viro2a737872009-04-07 11:49:53 -04001590 if (nd->root.mnt) {
1591 path_put(&nd->root);
1592 nd->root.mnt = NULL;
1593 }
Al Viroee0827c2011-02-21 23:38:09 -05001594 return retval;
1595}
Nick Piggin31e6b012011-01-07 17:49:52 +11001596
Al Viroee0827c2011-02-21 23:38:09 -05001597static int do_path_lookup(int dfd, const char *name,
1598 unsigned int flags, struct nameidata *nd)
1599{
1600 int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1601 if (unlikely(retval == -ECHILD))
1602 retval = path_lookupat(dfd, name, flags, nd);
1603 if (unlikely(retval == -ESTALE))
1604 retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11001605
1606 if (likely(!retval)) {
1607 if (unlikely(!audit_dummy_context())) {
1608 if (nd->path.dentry && nd->inode)
1609 audit_inode(name, nd->path.dentry);
1610 }
1611 }
Al Viro9b4a9b12009-04-07 11:44:16 -04001612 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613}
1614
Al Viroc9c6cac2011-02-16 15:15:47 -05001615int kern_path_parent(const char *name, struct nameidata *nd)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001616{
Al Viroc9c6cac2011-02-16 15:15:47 -05001617 return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001618}
1619
Al Virod1811462008-08-02 00:49:18 -04001620int kern_path(const char *name, unsigned int flags, struct path *path)
1621{
1622 struct nameidata nd;
1623 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1624 if (!res)
1625 *path = nd.path;
1626 return res;
1627}
1628
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001629/**
1630 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1631 * @dentry: pointer to dentry of the base directory
1632 * @mnt: pointer to vfs mount of the base directory
1633 * @name: pointer to file name
1634 * @flags: lookup flags
1635 * @nd: pointer to nameidata
1636 */
1637int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
1638 const char *name, unsigned int flags,
1639 struct nameidata *nd)
1640{
Al Viroee0827c2011-02-21 23:38:09 -05001641 int result;
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001642
1643 /* same as do_path_lookup */
1644 nd->last_type = LAST_ROOT;
Al Viro16c2cd72011-02-22 15:50:10 -05001645 nd->flags = flags | LOOKUP_JUMPED;
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001646 nd->depth = 0;
1647
Jan Blunckc8e7f442008-06-09 16:40:35 -07001648 nd->path.dentry = dentry;
1649 nd->path.mnt = mnt;
1650 path_get(&nd->path);
Al Viro5b857112009-04-07 11:53:49 -04001651 nd->root = nd->path;
1652 path_get(&nd->root);
Nick Piggin31e6b012011-01-07 17:49:52 +11001653 nd->inode = nd->path.dentry->d_inode;
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001654
Al Viroee0827c2011-02-21 23:38:09 -05001655 current->total_link_count = 0;
1656
1657 result = link_path_walk(name, nd);
Al Viro16c2cd72011-02-22 15:50:10 -05001658 if (!result)
1659 result = handle_reval_path(nd);
Al Viroee0827c2011-02-21 23:38:09 -05001660 if (result == -ESTALE) {
1661 /* nd->path had been dropped */
1662 current->total_link_count = 0;
1663 nd->path.dentry = dentry;
1664 nd->path.mnt = mnt;
1665 nd->inode = dentry->d_inode;
1666 path_get(&nd->path);
Al Viro16c2cd72011-02-22 15:50:10 -05001667 nd->flags = flags | LOOKUP_JUMPED | LOOKUP_REVAL;
1668
Al Viroee0827c2011-02-21 23:38:09 -05001669 result = link_path_walk(name, nd);
Al Viro16c2cd72011-02-22 15:50:10 -05001670 if (!result)
1671 result = handle_reval_path(nd);
Al Viroee0827c2011-02-21 23:38:09 -05001672 }
1673 if (unlikely(!result && !audit_dummy_context() && nd->path.dentry &&
Nick Piggin31e6b012011-01-07 17:49:52 +11001674 nd->inode))
Jan Blunck4ac91372008-02-14 19:34:32 -08001675 audit_inode(name, nd->path.dentry);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001676
Al Viro5b857112009-04-07 11:53:49 -04001677 path_put(&nd->root);
1678 nd->root.mnt = NULL;
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001679
Al Viroee0827c2011-02-21 23:38:09 -05001680 return result;
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001681}
1682
Christoph Hellwigeead1912007-10-16 23:25:38 -07001683static struct dentry *__lookup_hash(struct qstr *name,
1684 struct dentry *base, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685{
Christoph Hellwig81fca442010-10-06 10:47:47 +02001686 struct inode *inode = base->d_inode;
James Morris057f6c02007-04-26 00:12:05 -07001687 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 int err;
1689
Nick Pigginb74c79e2011-01-07 17:49:58 +11001690 err = exec_permission(inode, 0);
Christoph Hellwig81fca442010-10-06 10:47:47 +02001691 if (err)
1692 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
1694 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001695 * Don't bother with __d_lookup: callers are for creat as
1696 * well as unlink, so a lot of the time it would cost
1697 * a double lookup.
Al Viro6e6b1bd2009-08-13 23:38:37 +04001698 */
Nick Pigginb04f7842010-08-18 04:37:34 +10001699 dentry = d_lookup(base, name);
Al Viro6e6b1bd2009-08-13 23:38:37 +04001700
Nick Pigginfb045ad2011-01-07 17:49:55 +11001701 if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE))
Al Viro6e6b1bd2009-08-13 23:38:37 +04001702 dentry = do_revalidate(dentry, nd);
1703
Nick Pigginbaa03892010-08-18 04:37:31 +10001704 if (!dentry)
1705 dentry = d_alloc_and_lookup(base, name, nd);
Al Viro5a202bc2011-03-08 14:17:44 -05001706
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 return dentry;
1708}
1709
James Morris057f6c02007-04-26 00:12:05 -07001710/*
1711 * Restricted form of lookup. Doesn't follow links, single-component only,
1712 * needs parent already locked. Doesn't follow mounts.
1713 * SMP-safe.
1714 */
Adrian Bunka244e162006-03-31 02:32:11 -08001715static struct dentry *lookup_hash(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716{
Jan Blunck4ac91372008-02-14 19:34:32 -08001717 return __lookup_hash(&nd->last, nd->path.dentry, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718}
1719
Christoph Hellwigeead1912007-10-16 23:25:38 -07001720/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07001721 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07001722 * @name: pathname component to lookup
1723 * @base: base directory to lookup from
1724 * @len: maximum length @len should be interpreted to
1725 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07001726 * Note that this routine is purely a helper for filesystem usage and should
1727 * not be called by generic code. Also note that by using this function the
Christoph Hellwigeead1912007-10-16 23:25:38 -07001728 * nameidata argument is passed to the filesystem methods and a filesystem
1729 * using this helper needs to be prepared for that.
1730 */
James Morris057f6c02007-04-26 00:12:05 -07001731struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1732{
James Morris057f6c02007-04-26 00:12:05 -07001733 struct qstr this;
Al Viro6a96ba52011-03-07 23:49:20 -05001734 unsigned long hash;
1735 unsigned int c;
James Morris057f6c02007-04-26 00:12:05 -07001736
David Woodhouse2f9092e2009-04-20 23:18:37 +01001737 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
1738
Al Viro6a96ba52011-03-07 23:49:20 -05001739 this.name = name;
1740 this.len = len;
1741 if (!len)
1742 return ERR_PTR(-EACCES);
1743
1744 hash = init_name_hash();
1745 while (len--) {
1746 c = *(const unsigned char *)name++;
1747 if (c == '/' || c == '\0')
1748 return ERR_PTR(-EACCES);
1749 hash = partial_name_hash(c, hash);
1750 }
1751 this.hash = end_name_hash(hash);
Al Viro5a202bc2011-03-08 14:17:44 -05001752 /*
1753 * See if the low-level filesystem might want
1754 * to use its own hash..
1755 */
1756 if (base->d_flags & DCACHE_OP_HASH) {
1757 int err = base->d_op->d_hash(base, base->d_inode, &this);
1758 if (err < 0)
1759 return ERR_PTR(err);
1760 }
Christoph Hellwigeead1912007-10-16 23:25:38 -07001761
Christoph Hellwig49705b72005-11-08 21:35:06 -08001762 return __lookup_hash(&this, base, NULL);
James Morris057f6c02007-04-26 00:12:05 -07001763}
1764
Al Viro2d8f3032008-07-22 09:59:21 -04001765int user_path_at(int dfd, const char __user *name, unsigned flags,
1766 struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767{
Al Viro2d8f3032008-07-22 09:59:21 -04001768 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 char *tmp = getname(name);
1770 int err = PTR_ERR(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 if (!IS_ERR(tmp)) {
Al Viro2d8f3032008-07-22 09:59:21 -04001772
1773 BUG_ON(flags & LOOKUP_PARENT);
1774
1775 err = do_path_lookup(dfd, tmp, flags, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 putname(tmp);
Al Viro2d8f3032008-07-22 09:59:21 -04001777 if (!err)
1778 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 }
1780 return err;
1781}
1782
Al Viro2ad94ae2008-07-21 09:32:51 -04001783static int user_path_parent(int dfd, const char __user *path,
1784 struct nameidata *nd, char **name)
1785{
1786 char *s = getname(path);
1787 int error;
1788
1789 if (IS_ERR(s))
1790 return PTR_ERR(s);
1791
1792 error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
1793 if (error)
1794 putname(s);
1795 else
1796 *name = s;
1797
1798 return error;
1799}
1800
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801/*
1802 * It's inline, so penalty for filesystems that don't use sticky bit is
1803 * minimal.
1804 */
1805static inline int check_sticky(struct inode *dir, struct inode *inode)
1806{
David Howellsda9592e2008-11-14 10:39:05 +11001807 uid_t fsuid = current_fsuid();
1808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 if (!(dir->i_mode & S_ISVTX))
1810 return 0;
David Howellsda9592e2008-11-14 10:39:05 +11001811 if (inode->i_uid == fsuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 return 0;
David Howellsda9592e2008-11-14 10:39:05 +11001813 if (dir->i_uid == fsuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 return 0;
1815 return !capable(CAP_FOWNER);
1816}
1817
1818/*
1819 * Check whether we can remove a link victim from directory dir, check
1820 * whether the type of victim is right.
1821 * 1. We can't do it if dir is read-only (done in permission())
1822 * 2. We should have write and exec permissions on dir
1823 * 3. We can't remove anything from append-only dir
1824 * 4. We can't do anything with immutable dir (done in permission())
1825 * 5. If the sticky bit on dir is set we should either
1826 * a. be owner of dir, or
1827 * b. be owner of victim, or
1828 * c. have CAP_FOWNER capability
1829 * 6. If the victim is append-only or immutable we can't do antyhing with
1830 * links pointing to it.
1831 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1832 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1833 * 9. We can't remove a root or mountpoint.
1834 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1835 * nfs_async_unlink().
1836 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001837static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838{
1839 int error;
1840
1841 if (!victim->d_inode)
1842 return -ENOENT;
1843
1844 BUG_ON(victim->d_parent->d_inode != dir);
Al Virocccc6bb2009-12-25 05:07:33 -05001845 audit_inode_child(victim, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
Al Virof419a2e2008-07-22 00:07:17 -04001847 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 if (error)
1849 return error;
1850 if (IS_APPEND(dir))
1851 return -EPERM;
1852 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
Hugh Dickinsf9454542008-11-19 15:36:38 -08001853 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 return -EPERM;
1855 if (isdir) {
1856 if (!S_ISDIR(victim->d_inode->i_mode))
1857 return -ENOTDIR;
1858 if (IS_ROOT(victim))
1859 return -EBUSY;
1860 } else if (S_ISDIR(victim->d_inode->i_mode))
1861 return -EISDIR;
1862 if (IS_DEADDIR(dir))
1863 return -ENOENT;
1864 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1865 return -EBUSY;
1866 return 0;
1867}
1868
1869/* Check whether we can create an object with dentry child in directory
1870 * dir.
1871 * 1. We can't do it if child already exists (open has special treatment for
1872 * this case, but since we are inlined it's OK)
1873 * 2. We can't do it if dir is read-only (done in permission())
1874 * 3. We should have write and exec permissions on dir
1875 * 4. We can't do it if dir is immutable (done in permission())
1876 */
Miklos Szeredia95164d2008-07-30 15:08:48 +02001877static inline int may_create(struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878{
1879 if (child->d_inode)
1880 return -EEXIST;
1881 if (IS_DEADDIR(dir))
1882 return -ENOENT;
Al Virof419a2e2008-07-22 00:07:17 -04001883 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884}
1885
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886/*
1887 * p1 and p2 should be directories on the same fs.
1888 */
1889struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1890{
1891 struct dentry *p;
1892
1893 if (p1 == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07001894 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 return NULL;
1896 }
1897
Arjan van de Vena11f3a02006-03-23 03:00:33 -08001898 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001900 p = d_ancestor(p2, p1);
1901 if (p) {
1902 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1903 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
1904 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 }
1906
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001907 p = d_ancestor(p1, p2);
1908 if (p) {
1909 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1910 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1911 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 }
1913
Ingo Molnarf2eace22006-07-03 00:25:05 -07001914 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1915 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 return NULL;
1917}
1918
1919void unlock_rename(struct dentry *p1, struct dentry *p2)
1920{
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001921 mutex_unlock(&p1->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 if (p1 != p2) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001923 mutex_unlock(&p2->d_inode->i_mutex);
Arjan van de Vena11f3a02006-03-23 03:00:33 -08001924 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 }
1926}
1927
1928int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1929 struct nameidata *nd)
1930{
Miklos Szeredia95164d2008-07-30 15:08:48 +02001931 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
1933 if (error)
1934 return error;
1935
Al Viroacfa4382008-12-04 10:06:33 -05001936 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 return -EACCES; /* shouldn't it be ENOSYS? */
1938 mode &= S_IALLUGO;
1939 mode |= S_IFREG;
1940 error = security_inode_create(dir, dentry, mode);
1941 if (error)
1942 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 error = dir->i_op->create(dir, dentry, mode, nd);
Stephen Smalleya74574a2005-09-09 13:01:44 -07001944 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00001945 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 return error;
1947}
1948
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001949int may_open(struct path *path, int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001951 struct dentry *dentry = path->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 struct inode *inode = dentry->d_inode;
1953 int error;
1954
1955 if (!inode)
1956 return -ENOENT;
1957
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001958 switch (inode->i_mode & S_IFMT) {
1959 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001961 case S_IFDIR:
1962 if (acc_mode & MAY_WRITE)
1963 return -EISDIR;
1964 break;
1965 case S_IFBLK:
1966 case S_IFCHR:
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001967 if (path->mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001969 /*FALLTHRU*/
1970 case S_IFIFO:
1971 case S_IFSOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001973 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08001974 }
Dave Hansenb41572e2007-10-16 23:31:14 -07001975
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001976 error = inode_permission(inode, acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07001977 if (error)
1978 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05001979
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 /*
1981 * An append-only file must be opened in append mode for writing.
1982 */
1983 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05001984 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05001985 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05001987 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 }
1989
1990 /* O_NOATIME can only be set by the owner or superuser */
Al Viro7715b522009-12-16 03:54:00 -05001991 if (flag & O_NOATIME && !is_owner_or_cap(inode))
1992 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
1994 /*
1995 * Ensure there are no outstanding leases on the file.
1996 */
Al Virob65a9cf2009-12-16 06:27:40 -05001997 return break_lease(inode, flag);
Al Viro7715b522009-12-16 03:54:00 -05001998}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
Jeff Laytone1181ee2010-12-07 16:19:50 -05002000static int handle_truncate(struct file *filp)
Al Viro7715b522009-12-16 03:54:00 -05002001{
Jeff Laytone1181ee2010-12-07 16:19:50 -05002002 struct path *path = &filp->f_path;
Al Viro7715b522009-12-16 03:54:00 -05002003 struct inode *inode = path->dentry->d_inode;
2004 int error = get_write_access(inode);
2005 if (error)
2006 return error;
2007 /*
2008 * Refuse to truncate files with mandatory locks held on them.
2009 */
2010 error = locks_verify_locked(inode);
2011 if (!error)
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +09002012 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05002013 if (!error) {
2014 error = do_truncate(path->dentry, 0,
2015 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
Jeff Laytone1181ee2010-12-07 16:19:50 -05002016 filp);
Al Viro7715b522009-12-16 03:54:00 -05002017 }
2018 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04002019 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020}
2021
Dave Hansend57999e2008-02-15 14:37:27 -08002022/*
Dave Hansend57999e2008-02-15 14:37:27 -08002023 * Note that while the flag value (low two bits) for sys_open means:
2024 * 00 - read-only
2025 * 01 - write-only
2026 * 10 - read-write
2027 * 11 - special
2028 * it is changed into
2029 * 00 - no permissions needed
2030 * 01 - read-permission
2031 * 10 - write-permission
2032 * 11 - read-write
2033 * for the internal routines (ie open_namei()/follow_link() etc)
2034 * This is more logical, and also allows the 00 "no perm needed"
2035 * to be used for symlinks (where the permissions are checked
2036 * later).
2037 *
2038*/
2039static inline int open_to_namei_flags(int flag)
2040{
2041 if ((flag+1) & O_ACCMODE)
2042 flag++;
2043 return flag;
2044}
2045
Nick Piggin31e6b012011-01-07 17:49:52 +11002046/*
Al Virofe2d35f2011-03-05 22:58:25 -05002047 * Handle the last step of open()
Nick Piggin31e6b012011-01-07 17:49:52 +11002048 */
Al Virofb1cc552009-12-24 01:58:28 -05002049static struct file *do_last(struct nameidata *nd, struct path *path,
Al Viroc3e380b2011-02-23 13:39:45 -05002050 const struct open_flags *op, const char *pathname)
Al Virofb1cc552009-12-24 01:58:28 -05002051{
Al Viroa1e28032009-12-24 02:12:06 -05002052 struct dentry *dir = nd->path.dentry;
Al Viro6c0d46c2011-03-09 00:59:59 -05002053 struct dentry *dentry;
Al Viroca344a892011-03-09 00:36:45 -05002054 int open_flag = op->open_flag;
Al Viro6c0d46c2011-03-09 00:59:59 -05002055 int will_truncate = open_flag & O_TRUNC;
Al Viroca344a892011-03-09 00:36:45 -05002056 int want_write = 0;
2057 int skip_perm = 0;
Al Virofb1cc552009-12-24 01:58:28 -05002058 struct file *filp;
Al Virofe2d35f2011-03-05 22:58:25 -05002059 struct inode *inode;
Al Viro16c2cd72011-02-22 15:50:10 -05002060 int error;
Al Virofb1cc552009-12-24 01:58:28 -05002061
Al Viroc3e380b2011-02-23 13:39:45 -05002062 nd->flags &= ~LOOKUP_PARENT;
2063 nd->flags |= op->intent;
2064
Al Viro1f36f772009-12-26 10:56:19 -05002065 switch (nd->last_type) {
2066 case LAST_DOTDOT:
Neil Brown176306f2010-05-24 16:57:56 +10002067 case LAST_DOT:
Al Virofe2d35f2011-03-05 22:58:25 -05002068 error = handle_dots(nd, nd->last_type);
2069 if (error)
2070 return ERR_PTR(error);
Al Viro1f36f772009-12-26 10:56:19 -05002071 /* fallthrough */
Al Viro1f36f772009-12-26 10:56:19 -05002072 case LAST_ROOT:
Al Virofe2d35f2011-03-05 22:58:25 -05002073 if (nd->flags & LOOKUP_RCU) {
2074 if (nameidata_drop_rcu_last(nd))
2075 return ERR_PTR(-ECHILD);
2076 }
Al Viro16c2cd72011-02-22 15:50:10 -05002077 error = handle_reval_path(nd);
2078 if (error)
2079 goto exit;
Al Virofe2d35f2011-03-05 22:58:25 -05002080 audit_inode(pathname, nd->path.dentry);
Al Viroca344a892011-03-09 00:36:45 -05002081 if (open_flag & O_CREAT) {
Al Virofe2d35f2011-03-05 22:58:25 -05002082 error = -EISDIR;
2083 goto exit;
2084 }
2085 goto ok;
Al Viro1f36f772009-12-26 10:56:19 -05002086 case LAST_BIND:
Al Virofe2d35f2011-03-05 22:58:25 -05002087 /* can't be RCU mode here */
Al Viro16c2cd72011-02-22 15:50:10 -05002088 error = handle_reval_path(nd);
2089 if (error)
2090 goto exit;
Al Viro1f36f772009-12-26 10:56:19 -05002091 audit_inode(pathname, dir);
Al Viro67ee3ad2009-12-26 07:01:01 -05002092 goto ok;
Al Viro1f36f772009-12-26 10:56:19 -05002093 }
Al Viro67ee3ad2009-12-26 07:01:01 -05002094
Al Viroca344a892011-03-09 00:36:45 -05002095 if (!(open_flag & O_CREAT)) {
Al Virofe2d35f2011-03-05 22:58:25 -05002096 if (nd->last.name[nd->last.len])
2097 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2098 /* we _can_ be in RCU mode here */
2099 error = do_lookup(nd, &nd->last, path, &inode);
2100 if (error) {
2101 terminate_walk(nd);
2102 return ERR_PTR(error);
2103 }
2104 if (!inode) {
2105 path_to_nameidata(path, nd);
2106 terminate_walk(nd);
2107 return ERR_PTR(-ENOENT);
2108 }
2109 if (unlikely(inode->i_op->follow_link)) {
2110 /* We drop rcu-walk here */
2111 if (nameidata_dentry_drop_rcu_maybe(nd, path->dentry))
2112 return ERR_PTR(-ECHILD);
2113 return NULL;
2114 }
2115 path_to_nameidata(path, nd);
2116 nd->inode = inode;
2117 /* sayonara */
2118 if (nd->flags & LOOKUP_RCU) {
2119 if (nameidata_drop_rcu_last(nd))
2120 return ERR_PTR(-ECHILD);
2121 }
2122
2123 error = -ENOTDIR;
2124 if (nd->flags & LOOKUP_DIRECTORY) {
2125 if (!inode->i_op->lookup)
2126 goto exit;
2127 }
2128 audit_inode(pathname, nd->path.dentry);
2129 goto ok;
2130 }
2131
2132 /* create side of things */
2133
2134 if (nd->flags & LOOKUP_RCU) {
2135 if (nameidata_drop_rcu_last(nd))
2136 return ERR_PTR(-ECHILD);
2137 }
2138
2139 audit_inode(pathname, dir);
Al Viro16c2cd72011-02-22 15:50:10 -05002140 error = -EISDIR;
Al Viro1f36f772009-12-26 10:56:19 -05002141 /* trailing slashes? */
Nick Piggin31e6b012011-01-07 17:49:52 +11002142 if (nd->last.name[nd->last.len])
2143 goto exit;
Al Viroa2c36b42009-12-24 03:39:50 -05002144
Al Viroa1e28032009-12-24 02:12:06 -05002145 mutex_lock(&dir->d_inode->i_mutex);
2146
Al Viro6c0d46c2011-03-09 00:59:59 -05002147 dentry = lookup_hash(nd);
2148 error = PTR_ERR(dentry);
2149 if (IS_ERR(dentry)) {
Al Virofb1cc552009-12-24 01:58:28 -05002150 mutex_unlock(&dir->d_inode->i_mutex);
2151 goto exit;
2152 }
2153
Al Viro6c0d46c2011-03-09 00:59:59 -05002154 path->dentry = dentry;
2155 path->mnt = nd->path.mnt;
2156
Al Virofb1cc552009-12-24 01:58:28 -05002157 /* Negative dentry, just create the file */
Al Viro6c0d46c2011-03-09 00:59:59 -05002158 if (!dentry->d_inode) {
2159 int mode = op->mode;
2160 if (!IS_POSIXACL(dir->d_inode))
2161 mode &= ~current_umask();
Al Virofb1cc552009-12-24 01:58:28 -05002162 /*
2163 * This write is needed to ensure that a
Al Viro6c0d46c2011-03-09 00:59:59 -05002164 * rw->ro transition does not occur between
Al Virofb1cc552009-12-24 01:58:28 -05002165 * the time when the file is created and when
2166 * a permanent write count is taken through
2167 * the 'struct file' in nameidata_to_filp().
2168 */
2169 error = mnt_want_write(nd->path.mnt);
2170 if (error)
2171 goto exit_mutex_unlock;
Al Viroca344a892011-03-09 00:36:45 -05002172 want_write = 1;
Al Viro9b44f1b2011-03-09 00:17:27 -05002173 /* Don't check for write permission, don't truncate */
Al Viroca344a892011-03-09 00:36:45 -05002174 open_flag &= ~O_TRUNC;
Al Viro6c0d46c2011-03-09 00:59:59 -05002175 will_truncate = 0;
Al Viroca344a892011-03-09 00:36:45 -05002176 skip_perm = 1;
Al Viro6c0d46c2011-03-09 00:59:59 -05002177 error = security_path_mknod(&nd->path, dentry, mode, 0);
2178 if (error)
2179 goto exit_mutex_unlock;
2180 error = vfs_create(dir->d_inode, dentry, mode, nd);
2181 if (error)
2182 goto exit_mutex_unlock;
2183 mutex_unlock(&dir->d_inode->i_mutex);
2184 dput(nd->path.dentry);
2185 nd->path.dentry = dentry;
Al Viroca344a892011-03-09 00:36:45 -05002186 goto common;
Al Virofb1cc552009-12-24 01:58:28 -05002187 }
2188
2189 /*
2190 * It already exists.
2191 */
2192 mutex_unlock(&dir->d_inode->i_mutex);
2193 audit_inode(pathname, path->dentry);
2194
2195 error = -EEXIST;
Al Viroca344a892011-03-09 00:36:45 -05002196 if (open_flag & O_EXCL)
Al Virofb1cc552009-12-24 01:58:28 -05002197 goto exit_dput;
2198
David Howells9875cf82011-01-14 18:45:21 +00002199 error = follow_managed(path, nd->flags);
2200 if (error < 0)
2201 goto exit_dput;
Al Virofb1cc552009-12-24 01:58:28 -05002202
2203 error = -ENOENT;
2204 if (!path->dentry->d_inode)
2205 goto exit_dput;
Al Viro9e67f362009-12-26 07:04:50 -05002206
2207 if (path->dentry->d_inode->i_op->follow_link)
Al Virofb1cc552009-12-24 01:58:28 -05002208 return NULL;
Al Virofb1cc552009-12-24 01:58:28 -05002209
2210 path_to_nameidata(path, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11002211 nd->inode = path->dentry->d_inode;
Al Virofb1cc552009-12-24 01:58:28 -05002212 error = -EISDIR;
Nick Piggin31e6b012011-01-07 17:49:52 +11002213 if (S_ISDIR(nd->inode->i_mode))
Al Virofb1cc552009-12-24 01:58:28 -05002214 goto exit;
Al Viro67ee3ad2009-12-26 07:01:01 -05002215ok:
Al Viro6c0d46c2011-03-09 00:59:59 -05002216 if (!S_ISREG(nd->inode->i_mode))
2217 will_truncate = 0;
2218
Al Viro0f9d1a12011-03-09 00:13:14 -05002219 if (will_truncate) {
2220 error = mnt_want_write(nd->path.mnt);
2221 if (error)
2222 goto exit;
Al Viroca344a892011-03-09 00:36:45 -05002223 want_write = 1;
Al Viro0f9d1a12011-03-09 00:13:14 -05002224 }
Al Viroca344a892011-03-09 00:36:45 -05002225common:
2226 error = may_open(&nd->path, skip_perm ? 0 : op->acc_mode, open_flag);
2227 if (error)
Al Viro0f9d1a12011-03-09 00:13:14 -05002228 goto exit;
Al Viro0f9d1a12011-03-09 00:13:14 -05002229 filp = nameidata_to_filp(nd);
2230 if (!IS_ERR(filp)) {
2231 error = ima_file_check(filp, op->acc_mode);
2232 if (error) {
2233 fput(filp);
2234 filp = ERR_PTR(error);
2235 }
2236 }
2237 if (!IS_ERR(filp)) {
2238 if (will_truncate) {
2239 error = handle_truncate(filp);
2240 if (error) {
2241 fput(filp);
2242 filp = ERR_PTR(error);
2243 }
2244 }
2245 }
Al Viroca344a892011-03-09 00:36:45 -05002246out:
2247 if (want_write)
Al Viro0f9d1a12011-03-09 00:13:14 -05002248 mnt_drop_write(nd->path.mnt);
2249 path_put(&nd->path);
Al Virofb1cc552009-12-24 01:58:28 -05002250 return filp;
2251
2252exit_mutex_unlock:
2253 mutex_unlock(&dir->d_inode->i_mutex);
2254exit_dput:
2255 path_put_conditional(path, nd);
2256exit:
Al Viroca344a892011-03-09 00:36:45 -05002257 filp = ERR_PTR(error);
2258 goto out;
Al Virofb1cc552009-12-24 01:58:28 -05002259}
2260
Al Viro13aab422011-02-23 17:54:08 -05002261static struct file *path_openat(int dfd, const char *pathname,
Al Viro47c805d2011-02-23 17:44:09 -05002262 const struct open_flags *op, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263{
Al Virofe2d35f2011-03-05 22:58:25 -05002264 struct file *base = NULL;
Dave Hansen4a3fd212008-02-15 14:37:48 -08002265 struct file *filp;
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08002266 struct nameidata nd;
Al Viro9850c052010-01-13 15:01:15 -05002267 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 int count = 0;
Al Viro13aab422011-02-23 17:54:08 -05002269 int error;
Nick Piggin31e6b012011-01-07 17:49:52 +11002270
2271 filp = get_empty_filp();
2272 if (!filp)
2273 return ERR_PTR(-ENFILE);
2274
Al Viro47c805d2011-02-23 17:44:09 -05002275 filp->f_flags = op->open_flag;
Nick Piggin31e6b012011-01-07 17:49:52 +11002276 nd.intent.open.file = filp;
Al Viro47c805d2011-02-23 17:44:09 -05002277 nd.intent.open.flags = open_to_namei_flags(op->open_flag);
2278 nd.intent.open.create_mode = op->mode;
Nick Piggin31e6b012011-01-07 17:49:52 +11002279
Al Virofe2d35f2011-03-05 22:58:25 -05002280 error = path_init(dfd, pathname, flags | LOOKUP_PARENT, &nd, &base);
Nick Piggin31e6b012011-01-07 17:49:52 +11002281 if (unlikely(error))
Al Viro13aab422011-02-23 17:54:08 -05002282 goto out_filp;
Nick Piggin31e6b012011-01-07 17:49:52 +11002283
Al Virofe2d35f2011-03-05 22:58:25 -05002284 current->total_link_count = 0;
2285 error = link_path_walk(pathname, &nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11002286 if (unlikely(error))
2287 goto out_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
Al Viro47c805d2011-02-23 17:44:09 -05002289 filp = do_last(&nd, &path, op, pathname);
Al Viro806b6812009-12-26 07:16:40 -05002290 while (unlikely(!filp)) { /* trailing symlink */
Nick Piggin7b9337a2011-01-14 08:42:43 +00002291 struct path link = path;
2292 struct inode *linki = link.dentry->d_inode;
Al Virodef4af32009-12-26 08:37:05 -05002293 void *cookie;
Al Viro40b39132011-03-09 16:22:18 -05002294 if (!(nd.flags & LOOKUP_FOLLOW) || count++ == 32) {
2295 path_put_conditional(&path, &nd);
2296 path_put(&nd.path);
2297 filp = ERR_PTR(-ELOOP);
2298 break;
2299 }
Al Viro806b6812009-12-26 07:16:40 -05002300 /*
2301 * This is subtle. Instead of calling do_follow_link() we do
2302 * the thing by hands. The reason is that this way we have zero
2303 * link_count and path_walk() (called from ->follow_link)
2304 * honoring LOOKUP_PARENT. After that we have the parent and
2305 * last component, i.e. we are in the same situation as after
2306 * the first path_walk(). Well, almost - if the last component
2307 * is normal we get its copy stored in nd->last.name and we will
2308 * have to putname() it when we are done. Procfs-like symlinks
2309 * just set LAST_BIND.
2310 */
2311 nd.flags |= LOOKUP_PARENT;
Al Viroc3e380b2011-02-23 13:39:45 -05002312 nd.flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
Nick Piggin7b9337a2011-01-14 08:42:43 +00002313 error = __do_follow_link(&link, &nd, &cookie);
Al Viroc3e380b2011-02-23 13:39:45 -05002314 if (unlikely(error))
Al Virof1afe9e2011-02-22 22:27:28 -05002315 filp = ERR_PTR(error);
Al Viroc3e380b2011-02-23 13:39:45 -05002316 else
Al Viro47c805d2011-02-23 17:44:09 -05002317 filp = do_last(&nd, &path, op, pathname);
Al Virof1afe9e2011-02-22 22:27:28 -05002318 if (!IS_ERR(cookie) && linki->i_op->put_link)
Nick Piggin7b9337a2011-01-14 08:42:43 +00002319 linki->i_op->put_link(link.dentry, &nd, cookie);
2320 path_put(&link);
Al Viro806b6812009-12-26 07:16:40 -05002321 }
Al Viro10fa8e62009-12-26 07:09:49 -05002322out:
Al Viro2a737872009-04-07 11:49:53 -04002323 if (nd.root.mnt)
2324 path_put(&nd.root);
Al Virofe2d35f2011-03-05 22:58:25 -05002325 if (base)
2326 fput(base);
Linus Torvalds2dab5972011-02-11 15:53:38 -08002327 release_open_intent(&nd);
Al Viro10fa8e62009-12-26 07:09:49 -05002328 return filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
Nick Piggin31e6b012011-01-07 17:49:52 +11002330out_filp:
Al Viro806b6812009-12-26 07:16:40 -05002331 filp = ERR_PTR(error);
Al Viro10fa8e62009-12-26 07:09:49 -05002332 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333}
2334
Al Viro13aab422011-02-23 17:54:08 -05002335struct file *do_filp_open(int dfd, const char *pathname,
2336 const struct open_flags *op, int flags)
2337{
2338 struct file *filp;
2339
2340 filp = path_openat(dfd, pathname, op, flags | LOOKUP_RCU);
2341 if (unlikely(filp == ERR_PTR(-ECHILD)))
2342 filp = path_openat(dfd, pathname, op, flags);
2343 if (unlikely(filp == ERR_PTR(-ESTALE)))
2344 filp = path_openat(dfd, pathname, op, flags | LOOKUP_REVAL);
2345 return filp;
2346}
2347
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348/**
2349 * lookup_create - lookup a dentry, creating it if it doesn't exist
2350 * @nd: nameidata info
2351 * @is_dir: directory flag
2352 *
2353 * Simple function to lookup and return a dentry and create it
2354 * if it doesn't exist. Is SMP-safe.
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002355 *
Jan Blunck4ac91372008-02-14 19:34:32 -08002356 * Returns with nd->path.dentry->d_inode->i_mutex locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 */
2358struct dentry *lookup_create(struct nameidata *nd, int is_dir)
2359{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002360 struct dentry *dentry = ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
Jan Blunck4ac91372008-02-14 19:34:32 -08002362 mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002363 /*
2364 * Yucky last component or no last component at all?
2365 * (foo/., foo/.., /////)
2366 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 if (nd->last_type != LAST_NORM)
2368 goto fail;
2369 nd->flags &= ~LOOKUP_PARENT;
Al Viro35165862008-08-05 03:00:49 -04002370 nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
ASANO Masahiroa6349042006-08-22 20:06:02 -04002371 nd->intent.open.flags = O_EXCL;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002372
2373 /*
2374 * Do the final lookup.
2375 */
Christoph Hellwig49705b72005-11-08 21:35:06 -08002376 dentry = lookup_hash(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 if (IS_ERR(dentry))
2378 goto fail;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002379
Al Viroe9baf6e2008-05-15 04:49:12 -04002380 if (dentry->d_inode)
2381 goto eexist;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002382 /*
2383 * Special case - lookup gave negative, but... we had foo/bar/
2384 * From the vfs_mknod() POV we just have a negative dentry -
2385 * all is fine. Let's be bastards - you had / on the end, you've
2386 * been asking for (non-existent) directory. -ENOENT for you.
2387 */
Al Viroe9baf6e2008-05-15 04:49:12 -04002388 if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
2389 dput(dentry);
2390 dentry = ERR_PTR(-ENOENT);
2391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 return dentry;
Al Viroe9baf6e2008-05-15 04:49:12 -04002393eexist:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 dput(dentry);
Al Viroe9baf6e2008-05-15 04:49:12 -04002395 dentry = ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396fail:
2397 return dentry;
2398}
Christoph Hellwigf81a0bf2005-05-19 12:26:43 -07002399EXPORT_SYMBOL_GPL(lookup_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
2401int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
2402{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002403 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
2405 if (error)
2406 return error;
2407
2408 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
2409 return -EPERM;
2410
Al Viroacfa4382008-12-04 10:06:33 -05002411 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 return -EPERM;
2413
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07002414 error = devcgroup_inode_mknod(mode, dev);
2415 if (error)
2416 return error;
2417
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 error = security_inode_mknod(dir, dentry, mode, dev);
2419 if (error)
2420 return error;
2421
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002423 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002424 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 return error;
2426}
2427
Dave Hansen463c3192008-02-15 14:37:57 -08002428static int may_mknod(mode_t mode)
2429{
2430 switch (mode & S_IFMT) {
2431 case S_IFREG:
2432 case S_IFCHR:
2433 case S_IFBLK:
2434 case S_IFIFO:
2435 case S_IFSOCK:
2436 case 0: /* zero mode translates to S_IFREG */
2437 return 0;
2438 case S_IFDIR:
2439 return -EPERM;
2440 default:
2441 return -EINVAL;
2442 }
2443}
2444
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002445SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
2446 unsigned, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447{
Al Viro2ad94ae2008-07-21 09:32:51 -04002448 int error;
2449 char *tmp;
2450 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 struct nameidata nd;
2452
2453 if (S_ISDIR(mode))
2454 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
Al Viro2ad94ae2008-07-21 09:32:51 -04002456 error = user_path_parent(dfd, filename, &nd, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002458 return error;
2459
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 dentry = lookup_create(&nd, 0);
Dave Hansen463c3192008-02-15 14:37:57 -08002461 if (IS_ERR(dentry)) {
2462 error = PTR_ERR(dentry);
2463 goto out_unlock;
2464 }
Jan Blunck4ac91372008-02-14 19:34:32 -08002465 if (!IS_POSIXACL(nd.path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04002466 mode &= ~current_umask();
Dave Hansen463c3192008-02-15 14:37:57 -08002467 error = may_mknod(mode);
2468 if (error)
2469 goto out_dput;
2470 error = mnt_want_write(nd.path.mnt);
2471 if (error)
2472 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002473 error = security_path_mknod(&nd.path, dentry, mode, dev);
2474 if (error)
2475 goto out_drop_write;
Dave Hansen463c3192008-02-15 14:37:57 -08002476 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 case 0: case S_IFREG:
Jan Blunck4ac91372008-02-14 19:34:32 -08002478 error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 break;
2480 case S_IFCHR: case S_IFBLK:
Jan Blunck4ac91372008-02-14 19:34:32 -08002481 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 new_decode_dev(dev));
2483 break;
2484 case S_IFIFO: case S_IFSOCK:
Jan Blunck4ac91372008-02-14 19:34:32 -08002485 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 }
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002488out_drop_write:
Dave Hansen463c3192008-02-15 14:37:57 -08002489 mnt_drop_write(nd.path.mnt);
2490out_dput:
2491 dput(dentry);
2492out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002493 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002494 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 putname(tmp);
2496
2497 return error;
2498}
2499
Heiko Carstens3480b252009-01-14 14:14:16 +01002500SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002501{
2502 return sys_mknodat(AT_FDCWD, filename, mode, dev);
2503}
2504
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2506{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002507 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
2509 if (error)
2510 return error;
2511
Al Viroacfa4382008-12-04 10:06:33 -05002512 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 return -EPERM;
2514
2515 mode &= (S_IRWXUGO|S_ISVTX);
2516 error = security_inode_mkdir(dir, dentry, mode);
2517 if (error)
2518 return error;
2519
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002521 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002522 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 return error;
2524}
2525
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002526SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527{
2528 int error = 0;
2529 char * tmp;
Dave Hansen6902d922006-09-30 23:29:01 -07002530 struct dentry *dentry;
2531 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532
Al Viro2ad94ae2008-07-21 09:32:51 -04002533 error = user_path_parent(dfd, pathname, &nd, &tmp);
2534 if (error)
Dave Hansen6902d922006-09-30 23:29:01 -07002535 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536
Dave Hansen6902d922006-09-30 23:29:01 -07002537 dentry = lookup_create(&nd, 1);
2538 error = PTR_ERR(dentry);
2539 if (IS_ERR(dentry))
2540 goto out_unlock;
2541
Jan Blunck4ac91372008-02-14 19:34:32 -08002542 if (!IS_POSIXACL(nd.path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04002543 mode &= ~current_umask();
Dave Hansen463c3192008-02-15 14:37:57 -08002544 error = mnt_want_write(nd.path.mnt);
2545 if (error)
2546 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002547 error = security_path_mkdir(&nd.path, dentry, mode);
2548 if (error)
2549 goto out_drop_write;
Jan Blunck4ac91372008-02-14 19:34:32 -08002550 error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002551out_drop_write:
Dave Hansen463c3192008-02-15 14:37:57 -08002552 mnt_drop_write(nd.path.mnt);
2553out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002554 dput(dentry);
2555out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002556 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002557 path_put(&nd.path);
Dave Hansen6902d922006-09-30 23:29:01 -07002558 putname(tmp);
2559out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 return error;
2561}
2562
Heiko Carstens3cdad422009-01-14 14:14:22 +01002563SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002564{
2565 return sys_mkdirat(AT_FDCWD, pathname, mode);
2566}
2567
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568/*
2569 * We try to drop the dentry early: we should have
2570 * a usage count of 2 if we're the only user of this
2571 * dentry, and if that is true (possibly after pruning
2572 * the dcache), then we drop the dentry now.
2573 *
2574 * A low-level filesystem can, if it choses, legally
2575 * do a
2576 *
2577 * if (!d_unhashed(dentry))
2578 * return -EBUSY;
2579 *
2580 * if it cannot handle the case of removing a directory
2581 * that is still in use by something else..
2582 */
2583void dentry_unhash(struct dentry *dentry)
2584{
2585 dget(dentry);
Vasily Averindc168422006-12-06 20:37:07 -08002586 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 spin_lock(&dentry->d_lock);
Nick Pigginb7ab39f2011-01-07 17:49:32 +11002588 if (dentry->d_count == 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 __d_drop(dentry);
2590 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591}
2592
2593int vfs_rmdir(struct inode *dir, struct dentry *dentry)
2594{
2595 int error = may_delete(dir, dentry, 1);
2596
2597 if (error)
2598 return error;
2599
Al Viroacfa4382008-12-04 10:06:33 -05002600 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 return -EPERM;
2602
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002603 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 dentry_unhash(dentry);
2605 if (d_mountpoint(dentry))
2606 error = -EBUSY;
2607 else {
2608 error = security_inode_rmdir(dir, dentry);
2609 if (!error) {
2610 error = dir->i_op->rmdir(dir, dentry);
Al Virod83c49f2010-04-30 17:17:09 -04002611 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 dentry->d_inode->i_flags |= S_DEAD;
Al Virod83c49f2010-04-30 17:17:09 -04002613 dont_mount(dentry);
2614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 }
2616 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002617 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 d_delete(dentry);
2620 }
2621 dput(dentry);
2622
2623 return error;
2624}
2625
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002626static long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627{
2628 int error = 0;
2629 char * name;
2630 struct dentry *dentry;
2631 struct nameidata nd;
2632
Al Viro2ad94ae2008-07-21 09:32:51 -04002633 error = user_path_parent(dfd, pathname, &nd, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002635 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636
2637 switch(nd.last_type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002638 case LAST_DOTDOT:
2639 error = -ENOTEMPTY;
2640 goto exit1;
2641 case LAST_DOT:
2642 error = -EINVAL;
2643 goto exit1;
2644 case LAST_ROOT:
2645 error = -EBUSY;
2646 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002648
2649 nd.flags &= ~LOOKUP_PARENT;
2650
Jan Blunck4ac91372008-02-14 19:34:32 -08002651 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002652 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002654 if (IS_ERR(dentry))
2655 goto exit2;
Dave Hansen06227532008-02-15 14:37:34 -08002656 error = mnt_want_write(nd.path.mnt);
2657 if (error)
2658 goto exit3;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002659 error = security_path_rmdir(&nd.path, dentry);
2660 if (error)
2661 goto exit4;
Jan Blunck4ac91372008-02-14 19:34:32 -08002662 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002663exit4:
Dave Hansen06227532008-02-15 14:37:34 -08002664 mnt_drop_write(nd.path.mnt);
2665exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07002666 dput(dentry);
2667exit2:
Jan Blunck4ac91372008-02-14 19:34:32 -08002668 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002670 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 putname(name);
2672 return error;
2673}
2674
Heiko Carstens3cdad422009-01-14 14:14:22 +01002675SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002676{
2677 return do_rmdir(AT_FDCWD, pathname);
2678}
2679
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680int vfs_unlink(struct inode *dir, struct dentry *dentry)
2681{
2682 int error = may_delete(dir, dentry, 0);
2683
2684 if (error)
2685 return error;
2686
Al Viroacfa4382008-12-04 10:06:33 -05002687 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 return -EPERM;
2689
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002690 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 if (d_mountpoint(dentry))
2692 error = -EBUSY;
2693 else {
2694 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05002695 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 error = dir->i_op->unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05002697 if (!error)
Al Virod83c49f2010-04-30 17:17:09 -04002698 dont_mount(dentry);
Al Virobec10522010-03-03 14:12:08 -05002699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002701 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702
2703 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2704 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
Jan Karaece95912008-02-06 01:37:13 -08002705 fsnotify_link_count(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 }
Robert Love0eeca282005-07-12 17:06:03 -04002708
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 return error;
2710}
2711
2712/*
2713 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002714 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 * writeout happening, and we don't want to prevent access to the directory
2716 * while waiting on the I/O.
2717 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002718static long do_unlinkat(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719{
Al Viro2ad94ae2008-07-21 09:32:51 -04002720 int error;
2721 char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 struct dentry *dentry;
2723 struct nameidata nd;
2724 struct inode *inode = NULL;
2725
Al Viro2ad94ae2008-07-21 09:32:51 -04002726 error = user_path_parent(dfd, pathname, &nd, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002728 return error;
2729
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 error = -EISDIR;
2731 if (nd.last_type != LAST_NORM)
2732 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002733
2734 nd.flags &= ~LOOKUP_PARENT;
2735
Jan Blunck4ac91372008-02-14 19:34:32 -08002736 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002737 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 error = PTR_ERR(dentry);
2739 if (!IS_ERR(dentry)) {
2740 /* Why not before? Because we want correct error value */
2741 if (nd.last.name[nd.last.len])
2742 goto slashes;
2743 inode = dentry->d_inode;
2744 if (inode)
Al Viro7de9c6ee2010-10-23 11:11:40 -04002745 ihold(inode);
Dave Hansen06227532008-02-15 14:37:34 -08002746 error = mnt_want_write(nd.path.mnt);
2747 if (error)
2748 goto exit2;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002749 error = security_path_unlink(&nd.path, dentry);
2750 if (error)
2751 goto exit3;
Jan Blunck4ac91372008-02-14 19:34:32 -08002752 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002753exit3:
Dave Hansen06227532008-02-15 14:37:34 -08002754 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 exit2:
2756 dput(dentry);
2757 }
Jan Blunck4ac91372008-02-14 19:34:32 -08002758 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 if (inode)
2760 iput(inode); /* truncate the inode here */
2761exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002762 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 putname(name);
2764 return error;
2765
2766slashes:
2767 error = !dentry->d_inode ? -ENOENT :
2768 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2769 goto exit2;
2770}
2771
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002772SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002773{
2774 if ((flag & ~AT_REMOVEDIR) != 0)
2775 return -EINVAL;
2776
2777 if (flag & AT_REMOVEDIR)
2778 return do_rmdir(dfd, pathname);
2779
2780 return do_unlinkat(dfd, pathname);
2781}
2782
Heiko Carstens3480b252009-01-14 14:14:16 +01002783SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002784{
2785 return do_unlinkat(AT_FDCWD, pathname);
2786}
2787
Miklos Szeredidb2e7472008-06-24 16:50:16 +02002788int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002790 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791
2792 if (error)
2793 return error;
2794
Al Viroacfa4382008-12-04 10:06:33 -05002795 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 return -EPERM;
2797
2798 error = security_inode_symlink(dir, dentry, oldname);
2799 if (error)
2800 return error;
2801
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002803 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002804 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 return error;
2806}
2807
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002808SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
2809 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810{
Al Viro2ad94ae2008-07-21 09:32:51 -04002811 int error;
2812 char *from;
2813 char *to;
Dave Hansen6902d922006-09-30 23:29:01 -07002814 struct dentry *dentry;
2815 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816
2817 from = getname(oldname);
Al Viro2ad94ae2008-07-21 09:32:51 -04002818 if (IS_ERR(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 return PTR_ERR(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04002820
2821 error = user_path_parent(newdfd, newname, &nd, &to);
2822 if (error)
Dave Hansen6902d922006-09-30 23:29:01 -07002823 goto out_putname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824
Dave Hansen6902d922006-09-30 23:29:01 -07002825 dentry = lookup_create(&nd, 0);
2826 error = PTR_ERR(dentry);
2827 if (IS_ERR(dentry))
2828 goto out_unlock;
2829
Dave Hansen75c3f292008-02-15 14:37:45 -08002830 error = mnt_want_write(nd.path.mnt);
2831 if (error)
2832 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002833 error = security_path_symlink(&nd.path, dentry, from);
2834 if (error)
2835 goto out_drop_write;
Miklos Szeredidb2e7472008-06-24 16:50:16 +02002836 error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002837out_drop_write:
Dave Hansen75c3f292008-02-15 14:37:45 -08002838 mnt_drop_write(nd.path.mnt);
2839out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002840 dput(dentry);
2841out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002842 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002843 path_put(&nd.path);
Dave Hansen6902d922006-09-30 23:29:01 -07002844 putname(to);
2845out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 putname(from);
2847 return error;
2848}
2849
Heiko Carstens3480b252009-01-14 14:14:16 +01002850SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002851{
2852 return sys_symlinkat(oldname, AT_FDCWD, newname);
2853}
2854
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2856{
2857 struct inode *inode = old_dentry->d_inode;
2858 int error;
2859
2860 if (!inode)
2861 return -ENOENT;
2862
Miklos Szeredia95164d2008-07-30 15:08:48 +02002863 error = may_create(dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 if (error)
2865 return error;
2866
2867 if (dir->i_sb != inode->i_sb)
2868 return -EXDEV;
2869
2870 /*
2871 * A link to an append-only or immutable file cannot be created.
2872 */
2873 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2874 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05002875 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002877 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 return -EPERM;
2879
2880 error = security_inode_link(old_dentry, dir, new_dentry);
2881 if (error)
2882 return error;
2883
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002884 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 error = dir->i_op->link(old_dentry, dir, new_dentry);
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002886 mutex_unlock(&inode->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07002887 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002888 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 return error;
2890}
2891
2892/*
2893 * Hardlinks are often used in delicate situations. We avoid
2894 * security-related surprises by not following symlinks on the
2895 * newname. --KAB
2896 *
2897 * We don't follow them on the oldname either to be compatible
2898 * with linux 2.0, and to avoid hard-linking to directories
2899 * and other special files. --ADM
2900 */
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002901SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
2902 int, newdfd, const char __user *, newname, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903{
2904 struct dentry *new_dentry;
Al Viro2d8f3032008-07-22 09:59:21 -04002905 struct nameidata nd;
2906 struct path old_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 int error;
Al Viro2ad94ae2008-07-21 09:32:51 -04002908 char *to;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909
Ulrich Drepper45c9b112006-06-25 05:49:11 -07002910 if ((flags & ~AT_SYMLINK_FOLLOW) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002911 return -EINVAL;
2912
Al Viro2d8f3032008-07-22 09:59:21 -04002913 error = user_path_at(olddfd, oldname,
2914 flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0,
2915 &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002917 return error;
2918
2919 error = user_path_parent(newdfd, newname, &nd, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 if (error)
2921 goto out;
2922 error = -EXDEV;
Al Viro2d8f3032008-07-22 09:59:21 -04002923 if (old_path.mnt != nd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 goto out_release;
2925 new_dentry = lookup_create(&nd, 0);
2926 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002927 if (IS_ERR(new_dentry))
2928 goto out_unlock;
Dave Hansen75c3f292008-02-15 14:37:45 -08002929 error = mnt_want_write(nd.path.mnt);
2930 if (error)
2931 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002932 error = security_path_link(old_path.dentry, &nd.path, new_dentry);
2933 if (error)
2934 goto out_drop_write;
Al Viro2d8f3032008-07-22 09:59:21 -04002935 error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002936out_drop_write:
Dave Hansen75c3f292008-02-15 14:37:45 -08002937 mnt_drop_write(nd.path.mnt);
2938out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002939 dput(new_dentry);
2940out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002941 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942out_release:
Jan Blunck1d957f92008-02-14 19:34:35 -08002943 path_put(&nd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04002944 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945out:
Al Viro2d8f3032008-07-22 09:59:21 -04002946 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947
2948 return error;
2949}
2950
Heiko Carstens3480b252009-01-14 14:14:16 +01002951SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002952{
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002953 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002954}
2955
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956/*
2957 * The worst of all namespace operations - renaming directory. "Perverted"
2958 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2959 * Problems:
2960 * a) we can get into loop creation. Check is done in is_subdir().
2961 * b) race potential - two innocent renames can create a loop together.
2962 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002963 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 * story.
2965 * c) we have to lock _three_ objects - parents and victim (if it exists).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002966 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 * whether the target exists). Solution: try to be smart with locking
2968 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002969 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 * move will be locked. Thus we can rank directories by the tree
2971 * (ancestors first) and rank all non-directories after them.
2972 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002973 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 * HOWEVER, it relies on the assumption that any object with ->lookup()
2975 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2976 * we'd better make sure that there's no link(2) for them.
2977 * d) some filesystems don't support opened-but-unlinked directories,
2978 * either because of layout or because they are not ready to deal with
2979 * all cases correctly. The latter will be fixed (taking this sort of
2980 * stuff into VFS), but the former is not going away. Solution: the same
2981 * trick as in rmdir().
2982 * e) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002983 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05002985 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 * locking].
2987 */
Adrian Bunk75c96f82005-05-05 16:16:09 -07002988static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
2989 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990{
2991 int error = 0;
2992 struct inode *target;
2993
2994 /*
2995 * If we are going to change the parent - check write permissions,
2996 * we'll need to flip '..'.
2997 */
2998 if (new_dir != old_dir) {
Al Virof419a2e2008-07-22 00:07:17 -04002999 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 if (error)
3001 return error;
3002 }
3003
3004 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3005 if (error)
3006 return error;
3007
3008 target = new_dentry->d_inode;
Al Virod83c49f2010-04-30 17:17:09 -04003009 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003010 mutex_lock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
3012 error = -EBUSY;
Al Virod83c49f2010-04-30 17:17:09 -04003013 else {
3014 if (target)
3015 dentry_unhash(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
Al Virod83c49f2010-04-30 17:17:09 -04003017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 if (target) {
Al Virod83c49f2010-04-30 17:17:09 -04003019 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 target->i_flags |= S_DEAD;
Al Virod83c49f2010-04-30 17:17:09 -04003021 dont_mount(new_dentry);
3022 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003023 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 if (d_unhashed(new_dentry))
3025 d_rehash(new_dentry);
3026 dput(new_dentry);
3027 }
Stephen Smalleye31e14e2005-09-09 13:01:45 -07003028 if (!error)
Mark Fasheh349457c2006-09-08 14:22:21 -07003029 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3030 d_move(old_dentry,new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 return error;
3032}
3033
Adrian Bunk75c96f82005-05-05 16:16:09 -07003034static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
3035 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036{
3037 struct inode *target;
3038 int error;
3039
3040 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3041 if (error)
3042 return error;
3043
3044 dget(new_dentry);
3045 target = new_dentry->d_inode;
3046 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003047 mutex_lock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
3049 error = -EBUSY;
3050 else
3051 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3052 if (!error) {
Al Virobec10522010-03-03 14:12:08 -05003053 if (target)
Al Virod83c49f2010-04-30 17:17:09 -04003054 dont_mount(new_dentry);
Mark Fasheh349457c2006-09-08 14:22:21 -07003055 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 d_move(old_dentry, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 }
3058 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003059 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 dput(new_dentry);
3061 return error;
3062}
3063
3064int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
3065 struct inode *new_dir, struct dentry *new_dentry)
3066{
3067 int error;
3068 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
Eric Paris59b0df22010-02-08 12:53:52 -05003069 const unsigned char *old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070
3071 if (old_dentry->d_inode == new_dentry->d_inode)
3072 return 0;
3073
3074 error = may_delete(old_dir, old_dentry, is_dir);
3075 if (error)
3076 return error;
3077
3078 if (!new_dentry->d_inode)
Miklos Szeredia95164d2008-07-30 15:08:48 +02003079 error = may_create(new_dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 else
3081 error = may_delete(new_dir, new_dentry, is_dir);
3082 if (error)
3083 return error;
3084
Al Viroacfa4382008-12-04 10:06:33 -05003085 if (!old_dir->i_op->rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 return -EPERM;
3087
Robert Love0eeca282005-07-12 17:06:03 -04003088 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
3089
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 if (is_dir)
3091 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
3092 else
3093 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
Al Viro123df292009-12-25 04:57:57 -05003094 if (!error)
3095 fsnotify_move(old_dir, new_dir, old_name, is_dir,
Al Viro5a190ae2007-06-07 12:19:32 -04003096 new_dentry->d_inode, old_dentry);
Robert Love0eeca282005-07-12 17:06:03 -04003097 fsnotify_oldname_free(old_name);
3098
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 return error;
3100}
3101
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003102SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
3103 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104{
Al Viro2ad94ae2008-07-21 09:32:51 -04003105 struct dentry *old_dir, *new_dir;
3106 struct dentry *old_dentry, *new_dentry;
3107 struct dentry *trap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 struct nameidata oldnd, newnd;
Al Viro2ad94ae2008-07-21 09:32:51 -04003109 char *from;
3110 char *to;
3111 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112
Al Viro2ad94ae2008-07-21 09:32:51 -04003113 error = user_path_parent(olddfd, oldname, &oldnd, &from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 if (error)
3115 goto exit;
3116
Al Viro2ad94ae2008-07-21 09:32:51 -04003117 error = user_path_parent(newdfd, newname, &newnd, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 if (error)
3119 goto exit1;
3120
3121 error = -EXDEV;
Jan Blunck4ac91372008-02-14 19:34:32 -08003122 if (oldnd.path.mnt != newnd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 goto exit2;
3124
Jan Blunck4ac91372008-02-14 19:34:32 -08003125 old_dir = oldnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 error = -EBUSY;
3127 if (oldnd.last_type != LAST_NORM)
3128 goto exit2;
3129
Jan Blunck4ac91372008-02-14 19:34:32 -08003130 new_dir = newnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 if (newnd.last_type != LAST_NORM)
3132 goto exit2;
3133
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003134 oldnd.flags &= ~LOOKUP_PARENT;
3135 newnd.flags &= ~LOOKUP_PARENT;
OGAWA Hirofumi4e9ed2f2008-10-16 07:50:29 +09003136 newnd.flags |= LOOKUP_RENAME_TARGET;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003137
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 trap = lock_rename(new_dir, old_dir);
3139
Christoph Hellwig49705b72005-11-08 21:35:06 -08003140 old_dentry = lookup_hash(&oldnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 error = PTR_ERR(old_dentry);
3142 if (IS_ERR(old_dentry))
3143 goto exit3;
3144 /* source must exist */
3145 error = -ENOENT;
3146 if (!old_dentry->d_inode)
3147 goto exit4;
3148 /* unless the source is a directory trailing slashes give -ENOTDIR */
3149 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
3150 error = -ENOTDIR;
3151 if (oldnd.last.name[oldnd.last.len])
3152 goto exit4;
3153 if (newnd.last.name[newnd.last.len])
3154 goto exit4;
3155 }
3156 /* source should not be ancestor of target */
3157 error = -EINVAL;
3158 if (old_dentry == trap)
3159 goto exit4;
Christoph Hellwig49705b72005-11-08 21:35:06 -08003160 new_dentry = lookup_hash(&newnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 error = PTR_ERR(new_dentry);
3162 if (IS_ERR(new_dentry))
3163 goto exit4;
3164 /* target should not be an ancestor of source */
3165 error = -ENOTEMPTY;
3166 if (new_dentry == trap)
3167 goto exit5;
3168
Dave Hansen9079b1e2008-02-15 14:37:49 -08003169 error = mnt_want_write(oldnd.path.mnt);
3170 if (error)
3171 goto exit5;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003172 error = security_path_rename(&oldnd.path, old_dentry,
3173 &newnd.path, new_dentry);
3174 if (error)
3175 goto exit6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 error = vfs_rename(old_dir->d_inode, old_dentry,
3177 new_dir->d_inode, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003178exit6:
Dave Hansen9079b1e2008-02-15 14:37:49 -08003179 mnt_drop_write(oldnd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180exit5:
3181 dput(new_dentry);
3182exit4:
3183 dput(old_dentry);
3184exit3:
3185 unlock_rename(new_dir, old_dir);
3186exit2:
Jan Blunck1d957f92008-02-14 19:34:35 -08003187 path_put(&newnd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04003188 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003190 path_put(&oldnd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 putname(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04003192exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 return error;
3194}
3195
Heiko Carstensa26eab22009-01-14 14:14:17 +01003196SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003197{
3198 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
3199}
3200
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
3202{
3203 int len;
3204
3205 len = PTR_ERR(link);
3206 if (IS_ERR(link))
3207 goto out;
3208
3209 len = strlen(link);
3210 if (len > (unsigned) buflen)
3211 len = buflen;
3212 if (copy_to_user(buffer, link, len))
3213 len = -EFAULT;
3214out:
3215 return len;
3216}
3217
3218/*
3219 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
3220 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
3221 * using) it for any given inode is up to filesystem.
3222 */
3223int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3224{
3225 struct nameidata nd;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003226 void *cookie;
Marcin Slusarz694a1762008-06-09 16:40:37 -07003227 int res;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003228
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 nd.depth = 0;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003230 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
Marcin Slusarz694a1762008-06-09 16:40:37 -07003231 if (IS_ERR(cookie))
3232 return PTR_ERR(cookie);
3233
3234 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
3235 if (dentry->d_inode->i_op->put_link)
3236 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3237 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238}
3239
3240int vfs_follow_link(struct nameidata *nd, const char *link)
3241{
3242 return __vfs_follow_link(nd, link);
3243}
3244
3245/* get the link contents into pagecache */
3246static char *page_getlink(struct dentry * dentry, struct page **ppage)
3247{
Duane Griffinebd09ab2008-12-19 20:47:12 +00003248 char *kaddr;
3249 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 struct address_space *mapping = dentry->d_inode->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -07003251 page = read_mapping_page(mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 if (IS_ERR(page))
Nick Piggin6fe69002007-05-06 14:49:04 -07003253 return (char*)page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 *ppage = page;
Duane Griffinebd09ab2008-12-19 20:47:12 +00003255 kaddr = kmap(page);
3256 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3257 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258}
3259
3260int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3261{
3262 struct page *page = NULL;
3263 char *s = page_getlink(dentry, &page);
3264 int res = vfs_readlink(dentry,buffer,buflen,s);
3265 if (page) {
3266 kunmap(page);
3267 page_cache_release(page);
3268 }
3269 return res;
3270}
3271
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003272void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003274 struct page *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 nd_set_link(nd, page_getlink(dentry, &page));
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003276 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277}
3278
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003279void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003281 struct page *page = cookie;
3282
3283 if (page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 kunmap(page);
3285 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 }
3287}
3288
Nick Piggin54566b22009-01-04 12:00:53 -08003289/*
3290 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
3291 */
3292int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293{
3294 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003295 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07003296 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08003297 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 char *kaddr;
Nick Piggin54566b22009-01-04 12:00:53 -08003299 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
3300 if (nofs)
3301 flags |= AOP_FLAG_NOFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302
NeilBrown7e53cac2006-03-25 03:07:57 -08003303retry:
Nick Pigginafddba42007-10-16 01:25:01 -07003304 err = pagecache_write_begin(NULL, mapping, 0, len-1,
Nick Piggin54566b22009-01-04 12:00:53 -08003305 flags, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07003307 goto fail;
3308
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 kaddr = kmap_atomic(page, KM_USER0);
3310 memcpy(kaddr, symname, len-1);
3311 kunmap_atomic(kaddr, KM_USER0);
Nick Pigginafddba42007-10-16 01:25:01 -07003312
3313 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3314 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 if (err < 0)
3316 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07003317 if (err < len-1)
3318 goto retry;
3319
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 mark_inode_dirty(inode);
3321 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322fail:
3323 return err;
3324}
3325
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003326int page_symlink(struct inode *inode, const char *symname, int len)
3327{
3328 return __page_symlink(inode, symname, len,
Nick Piggin54566b22009-01-04 12:00:53 -08003329 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003330}
3331
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003332const struct inode_operations page_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 .readlink = generic_readlink,
3334 .follow_link = page_follow_link_light,
3335 .put_link = page_put_link,
3336};
3337
Al Viro2d8f3032008-07-22 09:59:21 -04003338EXPORT_SYMBOL(user_path_at);
David Howellscc53ce52011-01-14 18:45:26 +00003339EXPORT_SYMBOL(follow_down_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340EXPORT_SYMBOL(follow_down);
3341EXPORT_SYMBOL(follow_up);
3342EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
3343EXPORT_SYMBOL(getname);
3344EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345EXPORT_SYMBOL(lookup_one_len);
3346EXPORT_SYMBOL(page_follow_link_light);
3347EXPORT_SYMBOL(page_put_link);
3348EXPORT_SYMBOL(page_readlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003349EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350EXPORT_SYMBOL(page_symlink);
3351EXPORT_SYMBOL(page_symlink_inode_operations);
Al Viroc9c6cac2011-02-16 15:15:47 -05003352EXPORT_SYMBOL(kern_path_parent);
Al Virod1811462008-08-02 00:49:18 -04003353EXPORT_SYMBOL(kern_path);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07003354EXPORT_SYMBOL(vfs_path_lookup);
Al Virof419a2e2008-07-22 00:07:17 -04003355EXPORT_SYMBOL(inode_permission);
Christoph Hellwig8c744fb2005-11-08 21:35:04 -08003356EXPORT_SYMBOL(file_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357EXPORT_SYMBOL(unlock_rename);
3358EXPORT_SYMBOL(vfs_create);
3359EXPORT_SYMBOL(vfs_follow_link);
3360EXPORT_SYMBOL(vfs_link);
3361EXPORT_SYMBOL(vfs_mkdir);
3362EXPORT_SYMBOL(vfs_mknod);
3363EXPORT_SYMBOL(generic_permission);
3364EXPORT_SYMBOL(vfs_readlink);
3365EXPORT_SYMBOL(vfs_rename);
3366EXPORT_SYMBOL(vfs_rmdir);
3367EXPORT_SYMBOL(vfs_symlink);
3368EXPORT_SYMBOL(vfs_unlink);
3369EXPORT_SYMBOL(dentry_unhash);
3370EXPORT_SYMBOL(generic_readlink);