blob: 631cfdd45c68db880967cbb4763cab6aba09bc73 [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>
22#include <linux/quotaops.h>
23#include <linux/pagemap.h>
Robert Love0eeca282005-07-12 17:06:03 -040024#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/personality.h>
26#include <linux/security.h>
27#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/uaccess.h>
35
36#define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
37
38/* [Feb-1997 T. Schoebel-Theuer]
39 * Fundamental changes in the pathname lookup mechanisms (namei)
40 * were necessary because of omirr. The reason is that omirr needs
41 * to know the _real_ pathname, not the user-supplied one, in case
42 * of symlinks (and also when transname replacements occur).
43 *
44 * The new code replaces the old recursive symlink resolution with
45 * an iterative one (in case of non-nested symlink chains). It does
46 * this with calls to <fs>_follow_link().
47 * As a side effect, dir_namei(), _namei() and follow_link() are now
48 * replaced with a single function lookup_dentry() that can handle all
49 * the special cases of the former code.
50 *
51 * With the new dcache, the pathname is stored at each inode, at least as
52 * long as the refcount of the inode is positive. As a side effect, the
53 * size of the dcache depends on the inode cache and thus is dynamic.
54 *
55 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
56 * resolution to correspond with current state of the code.
57 *
58 * Note that the symlink resolution is not *completely* iterative.
59 * There is still a significant amount of tail- and mid- recursion in
60 * the algorithm. Also, note that <fs>_readlink() is not used in
61 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
62 * may return different results than <fs>_follow_link(). Many virtual
63 * filesystems (including /proc) exhibit this behavior.
64 */
65
66/* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
67 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
68 * and the name already exists in form of a symlink, try to create the new
69 * name indicated by the symlink. The old code always complained that the
70 * name already exists, due to not following the symlink even if its target
71 * is nonexistent. The new semantics affects also mknod() and link() when
72 * the name is a symlink pointing to a non-existant name.
73 *
74 * I don't know which semantics is the right one, since I have no access
75 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
76 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
77 * "old" one. Personally, I think the new semantics is much more logical.
78 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
79 * file does succeed in both HP-UX and SunOs, but not in Solaris
80 * and in the old Linux semantics.
81 */
82
83/* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
84 * semantics. See the comments in "open_namei" and "do_link" below.
85 *
86 * [10-Sep-98 Alan Modra] Another symlink change.
87 */
88
89/* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
90 * inside the path - always follow.
91 * in the last component in creation/removal/renaming - never follow.
92 * if LOOKUP_FOLLOW passed - follow.
93 * if the pathname has trailing slashes - follow.
94 * otherwise - don't follow.
95 * (applied in that order).
96 *
97 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
98 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
99 * During the 2.4 we need to fix the userland stuff depending on it -
100 * hopefully we will be able to get rid of that wart in 2.5. So far only
101 * XEmacs seems to be relying on it...
102 */
103/*
104 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
Arjan van de Vena11f3a02006-03-23 03:00:33 -0800105 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 * any extra contention...
107 */
108
Al Viroa02f76c2008-02-23 15:14:28 +0000109static int __link_path_walk(const char *name, struct nameidata *nd);
Josef 'Jeff' Sipekc4a78082007-07-19 01:48:22 -0700110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111/* In order to reduce some races, while at the same time doing additional
112 * checking and hopefully speeding things up, we copy filenames to the
113 * kernel data space before using them..
114 *
115 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
116 * PATH_MAX includes the nul terminator --RR.
117 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800118static int do_getname(const char __user *filename, char *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
120 int retval;
121 unsigned long len = PATH_MAX;
122
123 if (!segment_eq(get_fs(), KERNEL_DS)) {
124 if ((unsigned long) filename >= TASK_SIZE)
125 return -EFAULT;
126 if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
127 len = TASK_SIZE - (unsigned long) filename;
128 }
129
130 retval = strncpy_from_user(page, filename, len);
131 if (retval > 0) {
132 if (retval < len)
133 return 0;
134 return -ENAMETOOLONG;
135 } else if (!retval)
136 retval = -ENOENT;
137 return retval;
138}
139
140char * getname(const char __user * filename)
141{
142 char *tmp, *result;
143
144 result = ERR_PTR(-ENOMEM);
145 tmp = __getname();
146 if (tmp) {
147 int retval = do_getname(filename, tmp);
148
149 result = tmp;
150 if (retval < 0) {
151 __putname(tmp);
152 result = ERR_PTR(retval);
153 }
154 }
155 audit_getname(result);
156 return result;
157}
158
159#ifdef CONFIG_AUDITSYSCALL
160void putname(const char *name)
161{
Al Viro5ac3a9c2006-07-16 06:38:45 -0400162 if (unlikely(!audit_dummy_context()))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 audit_putname(name);
164 else
165 __putname(name);
166}
167EXPORT_SYMBOL(putname);
168#endif
169
170
171/**
172 * generic_permission - check for access rights on a Posix-like filesystem
173 * @inode: inode to check access rights for
174 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
175 * @check_acl: optional callback to check for Posix ACLs
176 *
177 * Used to check for read/write/execute permissions on a file.
178 * We use "fsuid" for this, letting us set arbitrary permissions
179 * for filesystem access without changing the "normal" uids which
180 * are used for other things..
181 */
182int generic_permission(struct inode *inode, int mask,
183 int (*check_acl)(struct inode *inode, int mask))
184{
185 umode_t mode = inode->i_mode;
186
Al Viroe6305c42008-07-15 21:03:57 -0400187 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
188
David Howellsda9592e2008-11-14 10:39:05 +1100189 if (current_fsuid() == inode->i_uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 mode >>= 6;
191 else {
192 if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
193 int error = check_acl(inode, mask);
194 if (error == -EACCES)
195 goto check_capabilities;
196 else if (error != -EAGAIN)
197 return error;
198 }
199
200 if (in_group_p(inode->i_gid))
201 mode >>= 3;
202 }
203
204 /*
205 * If the DACs are ok we don't need any capability check.
206 */
Al Viroe6305c42008-07-15 21:03:57 -0400207 if ((mask & ~mode) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return 0;
209
210 check_capabilities:
211 /*
212 * Read/write DACs are always overridable.
213 * Executable DACs are overridable if at least one exec bit is set.
214 */
Miklos Szeredif696a362008-07-31 13:41:58 +0200215 if (!(mask & MAY_EXEC) || execute_ok(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 if (capable(CAP_DAC_OVERRIDE))
217 return 0;
218
219 /*
220 * Searching includes executable on directories, else just read.
221 */
222 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
223 if (capable(CAP_DAC_READ_SEARCH))
224 return 0;
225
226 return -EACCES;
227}
228
Al Virof419a2e2008-07-22 00:07:17 -0400229int inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
Al Viroe6305c42008-07-15 21:03:57 -0400231 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 if (mask & MAY_WRITE) {
Miklos Szeredi22590e42007-10-16 23:27:08 -0700234 umode_t mode = inode->i_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 /*
237 * Nobody gets write access to a read-only fs.
238 */
239 if (IS_RDONLY(inode) &&
240 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
241 return -EROFS;
242
243 /*
244 * Nobody gets write access to an immutable file.
245 */
246 if (IS_IMMUTABLE(inode))
247 return -EACCES;
248 }
249
Miklos Szeredif696a362008-07-31 13:41:58 +0200250 if (inode->i_op && inode->i_op->permission)
Al Virob77b0642008-07-17 09:37:02 -0400251 retval = inode->i_op->permission(inode, mask);
Miklos Szeredif696a362008-07-31 13:41:58 +0200252 else
Al Viroe6305c42008-07-15 21:03:57 -0400253 retval = generic_permission(inode, mask, NULL);
Miklos Szeredif696a362008-07-31 13:41:58 +0200254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (retval)
256 return retval;
257
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700258 retval = devcgroup_inode_permission(inode, mask);
259 if (retval)
260 return retval;
261
Al Viroe6305c42008-07-15 21:03:57 -0400262 return security_inode_permission(inode,
Stephen Smalleyf418b002008-07-28 13:32:38 -0400263 mask & (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800266/**
267 * vfs_permission - check for access rights to a given path
268 * @nd: lookup result that describes the path
269 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
270 *
271 * Used to check for read/write/execute permissions on a path.
272 * We use "fsuid" for this, letting us set arbitrary permissions
273 * for filesystem access without changing the "normal" uids which
274 * are used for other things.
275 */
276int vfs_permission(struct nameidata *nd, int mask)
277{
Al Virof419a2e2008-07-22 00:07:17 -0400278 return inode_permission(nd->path.dentry->d_inode, mask);
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800279}
280
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800281/**
282 * file_permission - check for additional access rights to a given file
283 * @file: file to check access rights for
284 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
285 *
286 * Used to check for read/write/execute permissions on an already opened
287 * file.
288 *
289 * Note:
290 * Do not use this function in new code. All access checks should
291 * be done using vfs_permission().
292 */
293int file_permission(struct file *file, int mask)
294{
Al Virof419a2e2008-07-22 00:07:17 -0400295 return inode_permission(file->f_path.dentry->d_inode, mask);
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800296}
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298/*
299 * get_write_access() gets write permission for a file.
300 * put_write_access() releases this write permission.
301 * This is used for regular files.
302 * We cannot support write (and maybe mmap read-write shared) accesses and
303 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
304 * can have the following values:
305 * 0: no writers, no VM_DENYWRITE mappings
306 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
307 * > 0: (i_writecount) users are writing to the file.
308 *
309 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
310 * except for the cases where we don't hold i_writecount yet. Then we need to
311 * use {get,deny}_write_access() - these functions check the sign and refuse
312 * to do the change if sign is wrong. Exclusion between them is provided by
313 * the inode->i_lock spinlock.
314 */
315
316int get_write_access(struct inode * inode)
317{
318 spin_lock(&inode->i_lock);
319 if (atomic_read(&inode->i_writecount) < 0) {
320 spin_unlock(&inode->i_lock);
321 return -ETXTBSY;
322 }
323 atomic_inc(&inode->i_writecount);
324 spin_unlock(&inode->i_lock);
325
326 return 0;
327}
328
329int deny_write_access(struct file * file)
330{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800331 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 spin_lock(&inode->i_lock);
334 if (atomic_read(&inode->i_writecount) > 0) {
335 spin_unlock(&inode->i_lock);
336 return -ETXTBSY;
337 }
338 atomic_dec(&inode->i_writecount);
339 spin_unlock(&inode->i_lock);
340
341 return 0;
342}
343
Jan Blunck1d957f92008-02-14 19:34:35 -0800344/**
Jan Blunck5dd784d2008-02-14 19:34:38 -0800345 * path_get - get a reference to a path
346 * @path: path to get the reference to
347 *
348 * Given a path increment the reference count to the dentry and the vfsmount.
349 */
350void path_get(struct path *path)
351{
352 mntget(path->mnt);
353 dget(path->dentry);
354}
355EXPORT_SYMBOL(path_get);
356
357/**
Jan Blunck1d957f92008-02-14 19:34:35 -0800358 * path_put - put a reference to a path
359 * @path: path to put the reference to
360 *
361 * Given a path decrement the reference count to the dentry and the vfsmount.
362 */
363void path_put(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
Jan Blunck1d957f92008-02-14 19:34:35 -0800365 dput(path->dentry);
366 mntput(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
Jan Blunck1d957f92008-02-14 19:34:35 -0800368EXPORT_SYMBOL(path_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Trond Myklebust834f2a42005-10-18 14:20:16 -0700370/**
371 * release_open_intent - free up open intent resources
372 * @nd: pointer to nameidata
373 */
374void release_open_intent(struct nameidata *nd)
375{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800376 if (nd->intent.open.file->f_path.dentry == NULL)
Trond Myklebust834f2a42005-10-18 14:20:16 -0700377 put_filp(nd->intent.open.file);
378 else
379 fput(nd->intent.open.file);
380}
381
Ian Kentbcdc5e02006-09-27 01:50:44 -0700382static inline struct dentry *
383do_revalidate(struct dentry *dentry, struct nameidata *nd)
384{
385 int status = dentry->d_op->d_revalidate(dentry, nd);
386 if (unlikely(status <= 0)) {
387 /*
388 * The dentry failed validation.
389 * If d_revalidate returned 0 attempt to invalidate
390 * the dentry otherwise d_revalidate is asking us
391 * to return a fail status.
392 */
393 if (!status) {
394 if (!d_invalidate(dentry)) {
395 dput(dentry);
396 dentry = NULL;
397 }
398 } else {
399 dput(dentry);
400 dentry = ERR_PTR(status);
401 }
402 }
403 return dentry;
404}
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406/*
407 * Internal lookup() using the new generic dcache.
408 * SMP-safe
409 */
410static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
411{
412 struct dentry * dentry = __d_lookup(parent, name);
413
414 /* lockess __d_lookup may fail due to concurrent d_move()
415 * in some unrelated directory, so try with d_lookup
416 */
417 if (!dentry)
418 dentry = d_lookup(parent, name);
419
Ian Kentbcdc5e02006-09-27 01:50:44 -0700420 if (dentry && dentry->d_op && dentry->d_op->d_revalidate)
421 dentry = do_revalidate(dentry, nd);
422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return dentry;
424}
425
426/*
427 * Short-cut version of permission(), for calling by
428 * path_walk(), when dcache lock is held. Combines parts
429 * of permission() and generic_permission(), and tests ONLY for
430 * MAY_EXEC permission.
431 *
432 * If appropriate, check DAC only. If not appropriate, or
433 * short-cut DAC fails, then call permission() to do more
434 * complete permission check.
435 */
Al Viro672b16b2008-07-17 09:45:01 -0400436static int exec_permission_lite(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
438 umode_t mode = inode->i_mode;
439
440 if (inode->i_op && inode->i_op->permission)
441 return -EAGAIN;
442
David Howellsda9592e2008-11-14 10:39:05 +1100443 if (current_fsuid() == inode->i_uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 mode >>= 6;
445 else if (in_group_p(inode->i_gid))
446 mode >>= 3;
447
448 if (mode & MAY_EXEC)
449 goto ok;
450
451 if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE))
452 goto ok;
453
454 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_OVERRIDE))
455 goto ok;
456
457 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH))
458 goto ok;
459
460 return -EACCES;
461ok:
Al Virob77b0642008-07-17 09:37:02 -0400462 return security_inode_permission(inode, MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
464
465/*
466 * This is called when everything else fails, and we actually have
467 * to go to the low-level filesystem to find out what we should do..
468 *
469 * We get the directory semaphore, and after getting that we also
470 * make sure that nobody added the entry to the dcache in the meantime..
471 * SMP-safe
472 */
473static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
474{
475 struct dentry * result;
476 struct inode *dir = parent->d_inode;
477
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800478 mutex_lock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 /*
480 * First re-do the cached lookup just in case it was created
481 * while we waited for the directory semaphore..
482 *
483 * FIXME! This could use version numbering or similar to
484 * avoid unnecessary cache lookups.
485 *
486 * The "dcache_lock" is purely to protect the RCU list walker
487 * from concurrent renames at this point (we mustn't get false
488 * negatives from the RCU list walk here, unlike the optimistic
489 * fast walk).
490 *
491 * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
492 */
493 result = d_lookup(parent, name);
494 if (!result) {
Miklos Szeredid70b67c2008-07-02 21:30:15 +0200495 struct dentry *dentry;
496
497 /* Don't create child dentry for a dead directory. */
498 result = ERR_PTR(-ENOENT);
499 if (IS_DEADDIR(dir))
500 goto out_unlock;
501
502 dentry = d_alloc(parent, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 result = ERR_PTR(-ENOMEM);
504 if (dentry) {
505 result = dir->i_op->lookup(dir, dentry, nd);
506 if (result)
507 dput(dentry);
508 else
509 result = dentry;
510 }
Miklos Szeredid70b67c2008-07-02 21:30:15 +0200511out_unlock:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800512 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 return result;
514 }
515
516 /*
517 * Uhhuh! Nasty case: the cache was re-populated while
518 * we waited on the semaphore. Need to revalidate.
519 */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800520 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 if (result->d_op && result->d_op->d_revalidate) {
Ian Kentbcdc5e02006-09-27 01:50:44 -0700522 result = do_revalidate(result, nd);
523 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 }
526 return result;
527}
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529/* SMP-safe */
Al Viro7f2da1e2008-05-10 20:44:54 -0400530static __always_inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531walk_init_root(const char *name, struct nameidata *nd)
532{
Andreas Mohre518ddb2006-09-29 02:01:22 -0700533 struct fs_struct *fs = current->fs;
534
535 read_lock(&fs->lock);
Jan Blunck6ac08c32008-02-14 19:34:38 -0800536 nd->path = fs->root;
537 path_get(&fs->root);
Andreas Mohre518ddb2006-09-29 02:01:22 -0700538 read_unlock(&fs->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
Al Viroa02f76c2008-02-23 15:14:28 +0000541/*
542 * Wrapper to retry pathname resolution whenever the underlying
543 * file system returns an ESTALE.
544 *
545 * Retry the whole path once, forcing real lookup requests
546 * instead of relying on the dcache.
547 */
548static __always_inline int link_path_walk(const char *name, struct nameidata *nd)
549{
550 struct path save = nd->path;
551 int result;
552
553 /* make sure the stuff we saved doesn't go away */
Jan Blunckc8e7f442008-06-09 16:40:35 -0700554 path_get(&save);
Al Viroa02f76c2008-02-23 15:14:28 +0000555
556 result = __link_path_walk(name, nd);
557 if (result == -ESTALE) {
558 /* nd->path had been dropped */
559 nd->path = save;
Jan Blunckc8e7f442008-06-09 16:40:35 -0700560 path_get(&nd->path);
Al Viroa02f76c2008-02-23 15:14:28 +0000561 nd->flags |= LOOKUP_REVAL;
562 result = __link_path_walk(name, nd);
563 }
564
565 path_put(&save);
566
567 return result;
568}
569
Arjan van de Venf1662352006-01-14 13:21:31 -0800570static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
572 int res = 0;
573 char *name;
574 if (IS_ERR(link))
575 goto fail;
576
577 if (*link == '/') {
Jan Blunck1d957f92008-02-14 19:34:35 -0800578 path_put(&nd->path);
Al Viro7f2da1e2008-05-10 20:44:54 -0400579 walk_init_root(link, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
581 res = link_path_walk(link, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 if (nd->depth || res || nd->last_type!=LAST_NORM)
583 return res;
584 /*
585 * If it is an iterative symlinks resolution in open_namei() we
586 * have to copy the last component. And all that crap because of
587 * bloody create() on broken symlinks. Furrfu...
588 */
589 name = __getname();
590 if (unlikely(!name)) {
Jan Blunck1d957f92008-02-14 19:34:35 -0800591 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 return -ENOMEM;
593 }
594 strcpy(name, nd->last.name);
595 nd->last.name = name;
596 return 0;
597fail:
Jan Blunck1d957f92008-02-14 19:34:35 -0800598 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 return PTR_ERR(link);
600}
601
Jan Blunck1d957f92008-02-14 19:34:35 -0800602static void path_put_conditional(struct path *path, struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700603{
604 dput(path->dentry);
Jan Blunck4ac91372008-02-14 19:34:32 -0800605 if (path->mnt != nd->path.mnt)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700606 mntput(path->mnt);
607}
608
609static inline void path_to_nameidata(struct path *path, struct nameidata *nd)
610{
Jan Blunck4ac91372008-02-14 19:34:32 -0800611 dput(nd->path.dentry);
612 if (nd->path.mnt != path->mnt)
613 mntput(nd->path.mnt);
614 nd->path.mnt = path->mnt;
615 nd->path.dentry = path->dentry;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700616}
617
Ian Kent051d3812006-03-27 01:14:53 -0800618static __always_inline int __do_follow_link(struct path *path, struct nameidata *nd)
619{
620 int error;
621 void *cookie;
622 struct dentry *dentry = path->dentry;
623
624 touch_atime(path->mnt, dentry);
625 nd_set_link(nd, NULL);
626
Jan Blunck4ac91372008-02-14 19:34:32 -0800627 if (path->mnt != nd->path.mnt) {
Ian Kent051d3812006-03-27 01:14:53 -0800628 path_to_nameidata(path, nd);
629 dget(dentry);
630 }
631 mntget(path->mnt);
632 cookie = dentry->d_inode->i_op->follow_link(dentry, nd);
633 error = PTR_ERR(cookie);
634 if (!IS_ERR(cookie)) {
635 char *s = nd_get_link(nd);
636 error = 0;
637 if (s)
638 error = __vfs_follow_link(nd, s);
639 if (dentry->d_inode->i_op->put_link)
640 dentry->d_inode->i_op->put_link(dentry, nd, cookie);
641 }
Jan Blunck09da59162008-02-14 19:34:37 -0800642 path_put(path);
Ian Kent051d3812006-03-27 01:14:53 -0800643
644 return error;
645}
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647/*
648 * This limits recursive symlink follows to 8, while
649 * limiting consecutive symlinks to 40.
650 *
651 * Without that kind of total limit, nasty chains of consecutive
652 * symlinks can cause almost arbitrarily long lookups.
653 */
Al Viro90ebe562005-06-06 13:35:58 -0700654static inline int do_follow_link(struct path *path, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
656 int err = -ELOOP;
657 if (current->link_count >= MAX_NESTED_LINKS)
658 goto loop;
659 if (current->total_link_count >= 40)
660 goto loop;
661 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
662 cond_resched();
Al Viro90ebe562005-06-06 13:35:58 -0700663 err = security_inode_follow_link(path->dentry, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 if (err)
665 goto loop;
666 current->link_count++;
667 current->total_link_count++;
668 nd->depth++;
Al Virocd4e91d2005-06-06 13:36:03 -0700669 err = __do_follow_link(path, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 current->link_count--;
671 nd->depth--;
672 return err;
673loop:
Jan Blunck1d957f92008-02-14 19:34:35 -0800674 path_put_conditional(path, nd);
675 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return err;
677}
678
679int follow_up(struct vfsmount **mnt, struct dentry **dentry)
680{
681 struct vfsmount *parent;
682 struct dentry *mountpoint;
683 spin_lock(&vfsmount_lock);
684 parent=(*mnt)->mnt_parent;
685 if (parent == *mnt) {
686 spin_unlock(&vfsmount_lock);
687 return 0;
688 }
689 mntget(parent);
690 mountpoint=dget((*mnt)->mnt_mountpoint);
691 spin_unlock(&vfsmount_lock);
692 dput(*dentry);
693 *dentry = mountpoint;
694 mntput(*mnt);
695 *mnt = parent;
696 return 1;
697}
698
699/* no need for dcache_lock, as serialization is taken care in
700 * namespace.c
701 */
Al Viro463ffb22005-06-06 13:36:05 -0700702static int __follow_mount(struct path *path)
703{
704 int res = 0;
705 while (d_mountpoint(path->dentry)) {
706 struct vfsmount *mounted = lookup_mnt(path->mnt, path->dentry);
707 if (!mounted)
708 break;
709 dput(path->dentry);
710 if (res)
711 mntput(path->mnt);
712 path->mnt = mounted;
713 path->dentry = dget(mounted->mnt_root);
714 res = 1;
715 }
716 return res;
717}
718
Al Viro58c465e2005-06-06 13:36:13 -0700719static void follow_mount(struct vfsmount **mnt, struct dentry **dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 while (d_mountpoint(*dentry)) {
722 struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
723 if (!mounted)
724 break;
Al Viro58c465e2005-06-06 13:36:13 -0700725 dput(*dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 mntput(*mnt);
727 *mnt = mounted;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 *dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730}
731
732/* no need for dcache_lock, as serialization is taken care in
733 * namespace.c
734 */
Al Viroe13b2102005-06-06 13:36:06 -0700735int follow_down(struct vfsmount **mnt, struct dentry **dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
737 struct vfsmount *mounted;
738
739 mounted = lookup_mnt(*mnt, *dentry);
740 if (mounted) {
Al Viroe13b2102005-06-06 13:36:06 -0700741 dput(*dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 mntput(*mnt);
743 *mnt = mounted;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 *dentry = dget(mounted->mnt_root);
745 return 1;
746 }
747 return 0;
748}
749
Arjan van de Venf1662352006-01-14 13:21:31 -0800750static __always_inline void follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
Andreas Mohre518ddb2006-09-29 02:01:22 -0700752 struct fs_struct *fs = current->fs;
753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 while(1) {
755 struct vfsmount *parent;
Jan Blunck4ac91372008-02-14 19:34:32 -0800756 struct dentry *old = nd->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Andreas Mohre518ddb2006-09-29 02:01:22 -0700758 read_lock(&fs->lock);
Jan Blunck6ac08c32008-02-14 19:34:38 -0800759 if (nd->path.dentry == fs->root.dentry &&
760 nd->path.mnt == fs->root.mnt) {
Andreas Mohre518ddb2006-09-29 02:01:22 -0700761 read_unlock(&fs->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 break;
763 }
Andreas Mohre518ddb2006-09-29 02:01:22 -0700764 read_unlock(&fs->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 spin_lock(&dcache_lock);
Jan Blunck4ac91372008-02-14 19:34:32 -0800766 if (nd->path.dentry != nd->path.mnt->mnt_root) {
767 nd->path.dentry = dget(nd->path.dentry->d_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 spin_unlock(&dcache_lock);
769 dput(old);
770 break;
771 }
772 spin_unlock(&dcache_lock);
773 spin_lock(&vfsmount_lock);
Jan Blunck4ac91372008-02-14 19:34:32 -0800774 parent = nd->path.mnt->mnt_parent;
775 if (parent == nd->path.mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 spin_unlock(&vfsmount_lock);
777 break;
778 }
779 mntget(parent);
Jan Blunck4ac91372008-02-14 19:34:32 -0800780 nd->path.dentry = dget(nd->path.mnt->mnt_mountpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 spin_unlock(&vfsmount_lock);
782 dput(old);
Jan Blunck4ac91372008-02-14 19:34:32 -0800783 mntput(nd->path.mnt);
784 nd->path.mnt = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 }
Jan Blunck4ac91372008-02-14 19:34:32 -0800786 follow_mount(&nd->path.mnt, &nd->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787}
788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789/*
790 * It's more convoluted than I'd like it to be, but... it's still fairly
791 * small and for now I'd prefer to have fast path as straight as possible.
792 * It _is_ time-critical.
793 */
794static int do_lookup(struct nameidata *nd, struct qstr *name,
795 struct path *path)
796{
Jan Blunck4ac91372008-02-14 19:34:32 -0800797 struct vfsmount *mnt = nd->path.mnt;
798 struct dentry *dentry = __d_lookup(nd->path.dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 if (!dentry)
801 goto need_lookup;
802 if (dentry->d_op && dentry->d_op->d_revalidate)
803 goto need_revalidate;
804done:
805 path->mnt = mnt;
806 path->dentry = dentry;
Al Viro634ee702005-06-06 13:36:13 -0700807 __follow_mount(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 return 0;
809
810need_lookup:
Jan Blunck4ac91372008-02-14 19:34:32 -0800811 dentry = real_lookup(nd->path.dentry, name, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 if (IS_ERR(dentry))
813 goto fail;
814 goto done;
815
816need_revalidate:
Ian Kentbcdc5e02006-09-27 01:50:44 -0700817 dentry = do_revalidate(dentry, nd);
818 if (!dentry)
819 goto need_lookup;
820 if (IS_ERR(dentry))
821 goto fail;
822 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
824fail:
825 return PTR_ERR(dentry);
826}
827
828/*
829 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +0100830 * This is the basic name resolution function, turning a pathname into
831 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 *
Prasanna Medaea3834d2005-04-29 16:00:17 +0100833 * Returns 0 and nd will have valid dentry and mnt on success.
834 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 */
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -0800836static int __link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
838 struct path next;
839 struct inode *inode;
840 int err;
841 unsigned int lookup_flags = nd->flags;
842
843 while (*name=='/')
844 name++;
845 if (!*name)
846 goto return_reval;
847
Jan Blunck4ac91372008-02-14 19:34:32 -0800848 inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 if (nd->depth)
Trond Myklebustf55eab82006-02-04 23:28:01 -0800850 lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 /* At this point we know we have a real path component. */
853 for(;;) {
854 unsigned long hash;
855 struct qstr this;
856 unsigned int c;
857
Trond Myklebustcdce5d62005-10-18 14:20:18 -0700858 nd->flags |= LOOKUP_CONTINUE;
Al Viro672b16b2008-07-17 09:45:01 -0400859 err = exec_permission_lite(inode);
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800860 if (err == -EAGAIN)
861 err = vfs_permission(nd, MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 if (err)
863 break;
864
865 this.name = name;
866 c = *(const unsigned char *)name;
867
868 hash = init_name_hash();
869 do {
870 name++;
871 hash = partial_name_hash(c, hash);
872 c = *(const unsigned char *)name;
873 } while (c && (c != '/'));
874 this.len = name - (const char *) this.name;
875 this.hash = end_name_hash(hash);
876
877 /* remove trailing slashes? */
878 if (!c)
879 goto last_component;
880 while (*++name == '/');
881 if (!*name)
882 goto last_with_slashes;
883
884 /*
885 * "." and ".." are special - ".." especially so because it has
886 * to be able to know about the current root directory and
887 * parent relationships.
888 */
889 if (this.name[0] == '.') switch (this.len) {
890 default:
891 break;
892 case 2:
893 if (this.name[1] != '.')
894 break;
Al Viro58c465e2005-06-06 13:36:13 -0700895 follow_dotdot(nd);
Jan Blunck4ac91372008-02-14 19:34:32 -0800896 inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 /* fallthrough */
898 case 1:
899 continue;
900 }
901 /*
902 * See if the low-level filesystem might want
903 * to use its own hash..
904 */
Jan Blunck4ac91372008-02-14 19:34:32 -0800905 if (nd->path.dentry->d_op && nd->path.dentry->d_op->d_hash) {
906 err = nd->path.dentry->d_op->d_hash(nd->path.dentry,
907 &this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 if (err < 0)
909 break;
910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 /* This does the actual lookups.. */
912 err = do_lookup(nd, &this, &next);
913 if (err)
914 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 err = -ENOENT;
917 inode = next.dentry->d_inode;
918 if (!inode)
919 goto out_dput;
920 err = -ENOTDIR;
921 if (!inode->i_op)
922 goto out_dput;
923
924 if (inode->i_op->follow_link) {
Al Viro90ebe562005-06-06 13:35:58 -0700925 err = do_follow_link(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 if (err)
927 goto return_err;
928 err = -ENOENT;
Jan Blunck4ac91372008-02-14 19:34:32 -0800929 inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 if (!inode)
931 break;
932 err = -ENOTDIR;
933 if (!inode->i_op)
934 break;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700935 } else
936 path_to_nameidata(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 err = -ENOTDIR;
938 if (!inode->i_op->lookup)
939 break;
940 continue;
941 /* here ends the main loop */
942
943last_with_slashes:
944 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
945last_component:
Trond Myklebustf55eab82006-02-04 23:28:01 -0800946 /* Clear LOOKUP_CONTINUE iff it was previously unset */
947 nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 if (lookup_flags & LOOKUP_PARENT)
949 goto lookup_parent;
950 if (this.name[0] == '.') switch (this.len) {
951 default:
952 break;
953 case 2:
954 if (this.name[1] != '.')
955 break;
Al Viro58c465e2005-06-06 13:36:13 -0700956 follow_dotdot(nd);
Jan Blunck4ac91372008-02-14 19:34:32 -0800957 inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 /* fallthrough */
959 case 1:
960 goto return_reval;
961 }
Jan Blunck4ac91372008-02-14 19:34:32 -0800962 if (nd->path.dentry->d_op && nd->path.dentry->d_op->d_hash) {
963 err = nd->path.dentry->d_op->d_hash(nd->path.dentry,
964 &this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 if (err < 0)
966 break;
967 }
968 err = do_lookup(nd, &this, &next);
969 if (err)
970 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 inode = next.dentry->d_inode;
972 if ((lookup_flags & LOOKUP_FOLLOW)
973 && inode && inode->i_op && inode->i_op->follow_link) {
Al Viro90ebe562005-06-06 13:35:58 -0700974 err = do_follow_link(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 if (err)
976 goto return_err;
Jan Blunck4ac91372008-02-14 19:34:32 -0800977 inode = nd->path.dentry->d_inode;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700978 } else
979 path_to_nameidata(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 err = -ENOENT;
981 if (!inode)
982 break;
983 if (lookup_flags & LOOKUP_DIRECTORY) {
984 err = -ENOTDIR;
985 if (!inode->i_op || !inode->i_op->lookup)
986 break;
987 }
988 goto return_base;
989lookup_parent:
990 nd->last = this;
991 nd->last_type = LAST_NORM;
992 if (this.name[0] != '.')
993 goto return_base;
994 if (this.len == 1)
995 nd->last_type = LAST_DOT;
996 else if (this.len == 2 && this.name[1] == '.')
997 nd->last_type = LAST_DOTDOT;
998 else
999 goto return_base;
1000return_reval:
1001 /*
1002 * We bypassed the ordinary revalidation routines.
1003 * We may need to check the cached dentry for staleness.
1004 */
Jan Blunck4ac91372008-02-14 19:34:32 -08001005 if (nd->path.dentry && nd->path.dentry->d_sb &&
1006 (nd->path.dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 err = -ESTALE;
1008 /* Note: we do not d_invalidate() */
Jan Blunck4ac91372008-02-14 19:34:32 -08001009 if (!nd->path.dentry->d_op->d_revalidate(
1010 nd->path.dentry, nd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 break;
1012 }
1013return_base:
1014 return 0;
1015out_dput:
Jan Blunck1d957f92008-02-14 19:34:35 -08001016 path_put_conditional(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 break;
1018 }
Jan Blunck1d957f92008-02-14 19:34:35 -08001019 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020return_err:
1021 return err;
1022}
1023
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -08001024static int path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
1026 current->total_link_count = 0;
1027 return link_path_walk(name, nd);
1028}
1029
Prasanna Medaea3834d2005-04-29 16:00:17 +01001030/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -08001031static int do_path_lookup(int dfd, const char *name,
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001032 unsigned int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
Prasanna Medaea3834d2005-04-29 16:00:17 +01001034 int retval = 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001035 int fput_needed;
1036 struct file *file;
Andreas Mohre518ddb2006-09-29 02:01:22 -07001037 struct fs_struct *fs = current->fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
1039 nd->last_type = LAST_ROOT; /* if there are only slashes... */
1040 nd->flags = flags;
1041 nd->depth = 0;
1042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 if (*name=='/') {
Andreas Mohre518ddb2006-09-29 02:01:22 -07001044 read_lock(&fs->lock);
Jan Blunck6ac08c32008-02-14 19:34:38 -08001045 nd->path = fs->root;
1046 path_get(&fs->root);
Andreas Mohre518ddb2006-09-29 02:01:22 -07001047 read_unlock(&fs->lock);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001048 } else if (dfd == AT_FDCWD) {
Andreas Mohre518ddb2006-09-29 02:01:22 -07001049 read_lock(&fs->lock);
Jan Blunck6ac08c32008-02-14 19:34:38 -08001050 nd->path = fs->pwd;
1051 path_get(&fs->pwd);
Andreas Mohre518ddb2006-09-29 02:01:22 -07001052 read_unlock(&fs->lock);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001053 } else {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001054 struct dentry *dentry;
1055
1056 file = fget_light(dfd, &fput_needed);
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001057 retval = -EBADF;
1058 if (!file)
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001059 goto out_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001060
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001061 dentry = file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001062
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001063 retval = -ENOTDIR;
1064 if (!S_ISDIR(dentry->d_inode->i_mode))
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001065 goto fput_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001066
1067 retval = file_permission(file, MAY_EXEC);
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001068 if (retval)
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001069 goto fput_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001070
Jan Blunck5dd784d2008-02-14 19:34:38 -08001071 nd->path = file->f_path;
1072 path_get(&file->f_path);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001073
1074 fput_light(file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 }
Josef 'Jeff' Sipek2dfdd262007-05-09 02:33:41 -07001076
1077 retval = path_walk(name, nd);
Jan Blunck4ac91372008-02-14 19:34:32 -08001078 if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry &&
1079 nd->path.dentry->d_inode))
1080 audit_inode(name, nd->path.dentry);
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001081out_fail:
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001082 return retval;
1083
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001084fput_fail:
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001085 fput_light(file, fput_needed);
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001086 goto out_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087}
1088
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -08001089int path_lookup(const char *name, unsigned int flags,
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001090 struct nameidata *nd)
1091{
1092 return do_path_lookup(AT_FDCWD, name, flags, nd);
1093}
1094
Al Virod1811462008-08-02 00:49:18 -04001095int kern_path(const char *name, unsigned int flags, struct path *path)
1096{
1097 struct nameidata nd;
1098 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1099 if (!res)
1100 *path = nd.path;
1101 return res;
1102}
1103
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001104/**
1105 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1106 * @dentry: pointer to dentry of the base directory
1107 * @mnt: pointer to vfs mount of the base directory
1108 * @name: pointer to file name
1109 * @flags: lookup flags
1110 * @nd: pointer to nameidata
1111 */
1112int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
1113 const char *name, unsigned int flags,
1114 struct nameidata *nd)
1115{
1116 int retval;
1117
1118 /* same as do_path_lookup */
1119 nd->last_type = LAST_ROOT;
1120 nd->flags = flags;
1121 nd->depth = 0;
1122
Jan Blunckc8e7f442008-06-09 16:40:35 -07001123 nd->path.dentry = dentry;
1124 nd->path.mnt = mnt;
1125 path_get(&nd->path);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001126
1127 retval = path_walk(name, nd);
Jan Blunck4ac91372008-02-14 19:34:32 -08001128 if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry &&
1129 nd->path.dentry->d_inode))
1130 audit_inode(name, nd->path.dentry);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001131
1132 return retval;
1133
1134}
1135
Trond Myklebust834f2a42005-10-18 14:20:16 -07001136/**
1137 * path_lookup_open - lookup a file path with open intent
Martin Waitz7045f372006-02-01 03:06:57 -08001138 * @dfd: the directory to use as base, or AT_FDCWD
Trond Myklebust834f2a42005-10-18 14:20:16 -07001139 * @name: pointer to file name
1140 * @lookup_flags: lookup intent flags
1141 * @nd: pointer to nameidata
1142 * @open_flags: open intent flags
1143 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001144int path_lookup_open(int dfd, const char *name, unsigned int lookup_flags,
Trond Myklebust834f2a42005-10-18 14:20:16 -07001145 struct nameidata *nd, int open_flags)
1146{
Al Viro8737f3a2008-08-02 22:36:57 -04001147 struct file *filp = get_empty_filp();
1148 int err;
Trond Myklebust834f2a42005-10-18 14:20:16 -07001149
Al Viro8737f3a2008-08-02 22:36:57 -04001150 if (filp == NULL)
1151 return -ENFILE;
1152 nd->intent.open.file = filp;
1153 nd->intent.open.flags = open_flags;
1154 nd->intent.open.create_mode = 0;
1155 err = do_path_lookup(dfd, name, lookup_flags|LOOKUP_OPEN, nd);
1156 if (IS_ERR(nd->intent.open.file)) {
1157 if (err == 0) {
1158 err = PTR_ERR(nd->intent.open.file);
1159 path_put(&nd->path);
1160 }
1161 } else if (err != 0)
1162 release_open_intent(nd);
1163 return err;
Trond Myklebust834f2a42005-10-18 14:20:16 -07001164}
1165
Christoph Hellwigeead1912007-10-16 23:25:38 -07001166static struct dentry *__lookup_hash(struct qstr *name,
1167 struct dentry *base, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
James Morris057f6c02007-04-26 00:12:05 -07001169 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 struct inode *inode;
1171 int err;
1172
1173 inode = base->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
1175 /*
1176 * See if the low-level filesystem might want
1177 * to use its own hash..
1178 */
1179 if (base->d_op && base->d_op->d_hash) {
1180 err = base->d_op->d_hash(base, name);
1181 dentry = ERR_PTR(err);
1182 if (err < 0)
1183 goto out;
1184 }
1185
1186 dentry = cached_lookup(base, name, nd);
1187 if (!dentry) {
Miklos Szeredid70b67c2008-07-02 21:30:15 +02001188 struct dentry *new;
1189
1190 /* Don't create child dentry for a dead directory. */
1191 dentry = ERR_PTR(-ENOENT);
1192 if (IS_DEADDIR(inode))
1193 goto out;
1194
1195 new = d_alloc(base, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 dentry = ERR_PTR(-ENOMEM);
1197 if (!new)
1198 goto out;
1199 dentry = inode->i_op->lookup(inode, new, nd);
1200 if (!dentry)
1201 dentry = new;
1202 else
1203 dput(new);
1204 }
1205out:
1206 return dentry;
1207}
1208
James Morris057f6c02007-04-26 00:12:05 -07001209/*
1210 * Restricted form of lookup. Doesn't follow links, single-component only,
1211 * needs parent already locked. Doesn't follow mounts.
1212 * SMP-safe.
1213 */
Adrian Bunka244e162006-03-31 02:32:11 -08001214static struct dentry *lookup_hash(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
Christoph Hellwigeead1912007-10-16 23:25:38 -07001216 int err;
1217
Al Virof419a2e2008-07-22 00:07:17 -04001218 err = inode_permission(nd->path.dentry->d_inode, MAY_EXEC);
Christoph Hellwigeead1912007-10-16 23:25:38 -07001219 if (err)
1220 return ERR_PTR(err);
Jan Blunck4ac91372008-02-14 19:34:32 -08001221 return __lookup_hash(&nd->last, nd->path.dentry, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222}
1223
Christoph Hellwigeead1912007-10-16 23:25:38 -07001224static int __lookup_one_len(const char *name, struct qstr *this,
1225 struct dentry *base, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
1227 unsigned long hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 unsigned int c;
1229
James Morris057f6c02007-04-26 00:12:05 -07001230 this->name = name;
1231 this->len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 if (!len)
James Morris057f6c02007-04-26 00:12:05 -07001233 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 hash = init_name_hash();
1236 while (len--) {
1237 c = *(const unsigned char *)name++;
1238 if (c == '/' || c == '\0')
James Morris057f6c02007-04-26 00:12:05 -07001239 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 hash = partial_name_hash(c, hash);
1241 }
James Morris057f6c02007-04-26 00:12:05 -07001242 this->hash = end_name_hash(hash);
1243 return 0;
1244}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Christoph Hellwigeead1912007-10-16 23:25:38 -07001246/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07001247 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07001248 * @name: pathname component to lookup
1249 * @base: base directory to lookup from
1250 * @len: maximum length @len should be interpreted to
1251 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07001252 * Note that this routine is purely a helper for filesystem usage and should
1253 * not be called by generic code. Also note that by using this function the
Christoph Hellwigeead1912007-10-16 23:25:38 -07001254 * nameidata argument is passed to the filesystem methods and a filesystem
1255 * using this helper needs to be prepared for that.
1256 */
James Morris057f6c02007-04-26 00:12:05 -07001257struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1258{
1259 int err;
1260 struct qstr this;
1261
1262 err = __lookup_one_len(name, &this, base, len);
1263 if (err)
1264 return ERR_PTR(err);
Christoph Hellwigeead1912007-10-16 23:25:38 -07001265
Al Virof419a2e2008-07-22 00:07:17 -04001266 err = inode_permission(base->d_inode, MAY_EXEC);
Christoph Hellwigeead1912007-10-16 23:25:38 -07001267 if (err)
1268 return ERR_PTR(err);
Christoph Hellwig49705b72005-11-08 21:35:06 -08001269 return __lookup_hash(&this, base, NULL);
James Morris057f6c02007-04-26 00:12:05 -07001270}
1271
Christoph Hellwigeead1912007-10-16 23:25:38 -07001272/**
1273 * lookup_one_noperm - bad hack for sysfs
1274 * @name: pathname component to lookup
1275 * @base: base directory to lookup from
1276 *
1277 * This is a variant of lookup_one_len that doesn't perform any permission
1278 * checks. It's a horrible hack to work around the braindead sysfs
1279 * architecture and should not be used anywhere else.
1280 *
1281 * DON'T USE THIS FUNCTION EVER, thanks.
1282 */
1283struct dentry *lookup_one_noperm(const char *name, struct dentry *base)
James Morris057f6c02007-04-26 00:12:05 -07001284{
1285 int err;
1286 struct qstr this;
1287
Christoph Hellwigeead1912007-10-16 23:25:38 -07001288 err = __lookup_one_len(name, &this, base, strlen(name));
James Morris057f6c02007-04-26 00:12:05 -07001289 if (err)
1290 return ERR_PTR(err);
Christoph Hellwigeead1912007-10-16 23:25:38 -07001291 return __lookup_hash(&this, base, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292}
1293
Al Viro2d8f3032008-07-22 09:59:21 -04001294int user_path_at(int dfd, const char __user *name, unsigned flags,
1295 struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
Al Viro2d8f3032008-07-22 09:59:21 -04001297 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 char *tmp = getname(name);
1299 int err = PTR_ERR(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 if (!IS_ERR(tmp)) {
Al Viro2d8f3032008-07-22 09:59:21 -04001301
1302 BUG_ON(flags & LOOKUP_PARENT);
1303
1304 err = do_path_lookup(dfd, tmp, flags, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 putname(tmp);
Al Viro2d8f3032008-07-22 09:59:21 -04001306 if (!err)
1307 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 }
1309 return err;
1310}
1311
Al Viro2ad94ae2008-07-21 09:32:51 -04001312static int user_path_parent(int dfd, const char __user *path,
1313 struct nameidata *nd, char **name)
1314{
1315 char *s = getname(path);
1316 int error;
1317
1318 if (IS_ERR(s))
1319 return PTR_ERR(s);
1320
1321 error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
1322 if (error)
1323 putname(s);
1324 else
1325 *name = s;
1326
1327 return error;
1328}
1329
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330/*
1331 * It's inline, so penalty for filesystems that don't use sticky bit is
1332 * minimal.
1333 */
1334static inline int check_sticky(struct inode *dir, struct inode *inode)
1335{
David Howellsda9592e2008-11-14 10:39:05 +11001336 uid_t fsuid = current_fsuid();
1337
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 if (!(dir->i_mode & S_ISVTX))
1339 return 0;
David Howellsda9592e2008-11-14 10:39:05 +11001340 if (inode->i_uid == fsuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 return 0;
David Howellsda9592e2008-11-14 10:39:05 +11001342 if (dir->i_uid == fsuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 return 0;
1344 return !capable(CAP_FOWNER);
1345}
1346
1347/*
1348 * Check whether we can remove a link victim from directory dir, check
1349 * whether the type of victim is right.
1350 * 1. We can't do it if dir is read-only (done in permission())
1351 * 2. We should have write and exec permissions on dir
1352 * 3. We can't remove anything from append-only dir
1353 * 4. We can't do anything with immutable dir (done in permission())
1354 * 5. If the sticky bit on dir is set we should either
1355 * a. be owner of dir, or
1356 * b. be owner of victim, or
1357 * c. have CAP_FOWNER capability
1358 * 6. If the victim is append-only or immutable we can't do antyhing with
1359 * links pointing to it.
1360 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1361 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1362 * 9. We can't remove a root or mountpoint.
1363 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1364 * nfs_async_unlink().
1365 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001366static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367{
1368 int error;
1369
1370 if (!victim->d_inode)
1371 return -ENOENT;
1372
1373 BUG_ON(victim->d_parent->d_inode != dir);
Al Viro5a190ae2007-06-07 12:19:32 -04001374 audit_inode_child(victim->d_name.name, victim, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Al Virof419a2e2008-07-22 00:07:17 -04001376 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 if (error)
1378 return error;
1379 if (IS_APPEND(dir))
1380 return -EPERM;
1381 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
Hugh Dickinsf9454542008-11-19 15:36:38 -08001382 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 return -EPERM;
1384 if (isdir) {
1385 if (!S_ISDIR(victim->d_inode->i_mode))
1386 return -ENOTDIR;
1387 if (IS_ROOT(victim))
1388 return -EBUSY;
1389 } else if (S_ISDIR(victim->d_inode->i_mode))
1390 return -EISDIR;
1391 if (IS_DEADDIR(dir))
1392 return -ENOENT;
1393 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1394 return -EBUSY;
1395 return 0;
1396}
1397
1398/* Check whether we can create an object with dentry child in directory
1399 * dir.
1400 * 1. We can't do it if child already exists (open has special treatment for
1401 * this case, but since we are inlined it's OK)
1402 * 2. We can't do it if dir is read-only (done in permission())
1403 * 3. We should have write and exec permissions on dir
1404 * 4. We can't do it if dir is immutable (done in permission())
1405 */
Miklos Szeredia95164d2008-07-30 15:08:48 +02001406static inline int may_create(struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407{
1408 if (child->d_inode)
1409 return -EEXIST;
1410 if (IS_DEADDIR(dir))
1411 return -ENOENT;
Al Virof419a2e2008-07-22 00:07:17 -04001412 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413}
1414
1415/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 * O_DIRECTORY translates into forcing a directory lookup.
1417 */
1418static inline int lookup_flags(unsigned int f)
1419{
1420 unsigned long retval = LOOKUP_FOLLOW;
1421
1422 if (f & O_NOFOLLOW)
1423 retval &= ~LOOKUP_FOLLOW;
1424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 if (f & O_DIRECTORY)
1426 retval |= LOOKUP_DIRECTORY;
1427
1428 return retval;
1429}
1430
1431/*
1432 * p1 and p2 should be directories on the same fs.
1433 */
1434struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1435{
1436 struct dentry *p;
1437
1438 if (p1 == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07001439 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 return NULL;
1441 }
1442
Arjan van de Vena11f3a02006-03-23 03:00:33 -08001443 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001445 p = d_ancestor(p2, p1);
1446 if (p) {
1447 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1448 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
1449 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 }
1451
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001452 p = d_ancestor(p1, p2);
1453 if (p) {
1454 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1455 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1456 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 }
1458
Ingo Molnarf2eace22006-07-03 00:25:05 -07001459 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1460 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 return NULL;
1462}
1463
1464void unlock_rename(struct dentry *p1, struct dentry *p2)
1465{
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001466 mutex_unlock(&p1->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 if (p1 != p2) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001468 mutex_unlock(&p2->d_inode->i_mutex);
Arjan van de Vena11f3a02006-03-23 03:00:33 -08001469 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 }
1471}
1472
1473int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1474 struct nameidata *nd)
1475{
Miklos Szeredia95164d2008-07-30 15:08:48 +02001476 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
1478 if (error)
1479 return error;
1480
1481 if (!dir->i_op || !dir->i_op->create)
1482 return -EACCES; /* shouldn't it be ENOSYS? */
1483 mode &= S_IALLUGO;
1484 mode |= S_IFREG;
1485 error = security_inode_create(dir, dentry, mode);
1486 if (error)
1487 return error;
1488 DQUOT_INIT(dir);
1489 error = dir->i_op->create(dir, dentry, mode, nd);
Stephen Smalleya74574a2005-09-09 13:01:44 -07001490 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00001491 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 return error;
1493}
1494
1495int may_open(struct nameidata *nd, int acc_mode, int flag)
1496{
Jan Blunck4ac91372008-02-14 19:34:32 -08001497 struct dentry *dentry = nd->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 struct inode *inode = dentry->d_inode;
1499 int error;
1500
1501 if (!inode)
1502 return -ENOENT;
1503
1504 if (S_ISLNK(inode->i_mode))
1505 return -ELOOP;
1506
Linus Torvalds974a9f02008-01-12 14:06:34 -08001507 if (S_ISDIR(inode->i_mode) && (acc_mode & MAY_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 return -EISDIR;
1509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 /*
1511 * FIFO's, sockets and device files are special: they don't
1512 * actually live on the filesystem itself, and as such you
1513 * can write to them even if the filesystem is read-only.
1514 */
1515 if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
1516 flag &= ~O_TRUNC;
1517 } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
Jan Blunck4ac91372008-02-14 19:34:32 -08001518 if (nd->path.mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 return -EACCES;
1520
1521 flag &= ~O_TRUNC;
Dave Hansen4a3fd212008-02-15 14:37:48 -08001522 }
Dave Hansenb41572e2007-10-16 23:31:14 -07001523
1524 error = vfs_permission(nd, acc_mode);
1525 if (error)
1526 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 /*
1528 * An append-only file must be opened in append mode for writing.
1529 */
1530 if (IS_APPEND(inode)) {
1531 if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
1532 return -EPERM;
1533 if (flag & O_TRUNC)
1534 return -EPERM;
1535 }
1536
1537 /* O_NOATIME can only be set by the owner or superuser */
1538 if (flag & O_NOATIME)
Satyam Sharma3bd858a2007-07-17 15:00:08 +05301539 if (!is_owner_or_cap(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 return -EPERM;
1541
1542 /*
1543 * Ensure there are no outstanding leases on the file.
1544 */
1545 error = break_lease(inode, flag);
1546 if (error)
1547 return error;
1548
1549 if (flag & O_TRUNC) {
1550 error = get_write_access(inode);
1551 if (error)
1552 return error;
1553
1554 /*
1555 * Refuse to truncate files with mandatory locks held on them.
1556 */
1557 error = locks_verify_locked(inode);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09001558 if (!error)
1559 error = security_path_truncate(&nd->path, 0,
1560 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 if (!error) {
1562 DQUOT_INIT(inode);
Miklos Szeredid139d7f2007-10-18 03:07:00 -07001563
1564 error = do_truncate(dentry, 0,
1565 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
1566 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 }
1568 put_write_access(inode);
1569 if (error)
1570 return error;
1571 } else
1572 if (flag & FMODE_WRITE)
1573 DQUOT_INIT(inode);
1574
1575 return 0;
1576}
1577
Dave Hansend57999e2008-02-15 14:37:27 -08001578/*
1579 * Be careful about ever adding any more callers of this
1580 * function. Its flags must be in the namei format, not
1581 * what get passed to sys_open().
1582 */
1583static int __open_namei_create(struct nameidata *nd, struct path *path,
Dave Hansenaab520e2006-09-30 23:29:02 -07001584 int flag, int mode)
1585{
1586 int error;
Jan Blunck4ac91372008-02-14 19:34:32 -08001587 struct dentry *dir = nd->path.dentry;
Dave Hansenaab520e2006-09-30 23:29:02 -07001588
1589 if (!IS_POSIXACL(dir->d_inode))
1590 mode &= ~current->fs->umask;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09001591 error = security_path_mknod(&nd->path, path->dentry, mode, 0);
1592 if (error)
1593 goto out_unlock;
Dave Hansenaab520e2006-09-30 23:29:02 -07001594 error = vfs_create(dir->d_inode, path->dentry, mode, nd);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09001595out_unlock:
Dave Hansenaab520e2006-09-30 23:29:02 -07001596 mutex_unlock(&dir->d_inode->i_mutex);
Jan Blunck4ac91372008-02-14 19:34:32 -08001597 dput(nd->path.dentry);
1598 nd->path.dentry = path->dentry;
Dave Hansenaab520e2006-09-30 23:29:02 -07001599 if (error)
1600 return error;
1601 /* Don't check for write permission, don't truncate */
1602 return may_open(nd, 0, flag & ~O_TRUNC);
1603}
1604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605/*
Dave Hansend57999e2008-02-15 14:37:27 -08001606 * Note that while the flag value (low two bits) for sys_open means:
1607 * 00 - read-only
1608 * 01 - write-only
1609 * 10 - read-write
1610 * 11 - special
1611 * it is changed into
1612 * 00 - no permissions needed
1613 * 01 - read-permission
1614 * 10 - write-permission
1615 * 11 - read-write
1616 * for the internal routines (ie open_namei()/follow_link() etc)
1617 * This is more logical, and also allows the 00 "no perm needed"
1618 * to be used for symlinks (where the permissions are checked
1619 * later).
1620 *
1621*/
1622static inline int open_to_namei_flags(int flag)
1623{
1624 if ((flag+1) & O_ACCMODE)
1625 flag++;
1626 return flag;
1627}
1628
Dave Hansen4a3fd212008-02-15 14:37:48 -08001629static int open_will_write_to_fs(int flag, struct inode *inode)
1630{
1631 /*
1632 * We'll never write to the fs underlying
1633 * a device file.
1634 */
1635 if (special_file(inode->i_mode))
1636 return 0;
1637 return (flag & O_TRUNC);
1638}
1639
Dave Hansend57999e2008-02-15 14:37:27 -08001640/*
Dave Hansen4a3fd212008-02-15 14:37:48 -08001641 * Note that the low bits of the passed in "open_flag"
1642 * are not the same as in the local variable "flag". See
1643 * open_to_namei_flags() for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 */
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001645struct file *do_filp_open(int dfd, const char *pathname,
1646 int open_flag, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647{
Dave Hansen4a3fd212008-02-15 14:37:48 -08001648 struct file *filp;
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001649 struct nameidata nd;
Trond Myklebust834f2a42005-10-18 14:20:16 -07001650 int acc_mode, error;
Al Viro4e7506e2005-06-06 13:36:00 -07001651 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 struct dentry *dir;
1653 int count = 0;
Dave Hansen4a3fd212008-02-15 14:37:48 -08001654 int will_write;
Dave Hansend57999e2008-02-15 14:37:27 -08001655 int flag = open_to_namei_flags(open_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
Al Virob77b0642008-07-17 09:37:02 -04001657 acc_mode = MAY_OPEN | ACC_MODE(flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Trond Myklebust834f2a42005-10-18 14:20:16 -07001659 /* O_TRUNC implies we need access checks for write permissions */
1660 if (flag & O_TRUNC)
1661 acc_mode |= MAY_WRITE;
1662
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 /* Allow the LSM permission hook to distinguish append
1664 access from general write access. */
1665 if (flag & O_APPEND)
1666 acc_mode |= MAY_APPEND;
1667
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 /*
1669 * The simplest case - just a plain lookup.
1670 */
1671 if (!(flag & O_CREAT)) {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001672 error = path_lookup_open(dfd, pathname, lookup_flags(flag),
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001673 &nd, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 if (error)
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001675 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 goto ok;
1677 }
1678
1679 /*
1680 * Create - we need to know the parent.
1681 */
Al Viro8737f3a2008-08-02 22:36:57 -04001682 error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 if (error)
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001684 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
1686 /*
1687 * We have the parent and last component. First of all, check
1688 * that we are not asked to creat(2) an obvious directory - that
1689 * will not do.
1690 */
1691 error = -EISDIR;
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001692 if (nd.last_type != LAST_NORM || nd.last.name[nd.last.len])
Al Viro8737f3a2008-08-02 22:36:57 -04001693 goto exit_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Al Viro8737f3a2008-08-02 22:36:57 -04001695 error = -ENFILE;
1696 filp = get_empty_filp();
1697 if (filp == NULL)
1698 goto exit_parent;
1699 nd.intent.open.file = filp;
1700 nd.intent.open.flags = flag;
1701 nd.intent.open.create_mode = mode;
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001702 dir = nd.path.dentry;
1703 nd.flags &= ~LOOKUP_PARENT;
Al Viro8737f3a2008-08-02 22:36:57 -04001704 nd.flags |= LOOKUP_CREATE | LOOKUP_OPEN;
Al Viro35165862008-08-05 03:00:49 -04001705 if (flag & O_EXCL)
1706 nd.flags |= LOOKUP_EXCL;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001707 mutex_lock(&dir->d_inode->i_mutex);
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001708 path.dentry = lookup_hash(&nd);
1709 path.mnt = nd.path.mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
1711do_last:
Al Viro4e7506e2005-06-06 13:36:00 -07001712 error = PTR_ERR(path.dentry);
1713 if (IS_ERR(path.dentry)) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001714 mutex_unlock(&dir->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 goto exit;
1716 }
1717
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001718 if (IS_ERR(nd.intent.open.file)) {
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001719 error = PTR_ERR(nd.intent.open.file);
Dave Hansen4a3fd212008-02-15 14:37:48 -08001720 goto exit_mutex_unlock;
Oleg Drokin4af4c522006-03-25 03:06:54 -08001721 }
1722
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 /* Negative dentry, just create the file */
Al Viro4e7506e2005-06-06 13:36:00 -07001724 if (!path.dentry->d_inode) {
Dave Hansen4a3fd212008-02-15 14:37:48 -08001725 /*
1726 * This write is needed to ensure that a
1727 * ro->rw transition does not occur between
1728 * the time when the file is created and when
1729 * a permanent write count is taken through
1730 * the 'struct file' in nameidata_to_filp().
1731 */
1732 error = mnt_want_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 if (error)
Dave Hansen4a3fd212008-02-15 14:37:48 -08001734 goto exit_mutex_unlock;
1735 error = __open_namei_create(&nd, &path, flag, mode);
1736 if (error) {
1737 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 goto exit;
Dave Hansen4a3fd212008-02-15 14:37:48 -08001739 }
1740 filp = nameidata_to_filp(&nd, open_flag);
1741 mnt_drop_write(nd.path.mnt);
1742 return filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 }
1744
1745 /*
1746 * It already exists.
1747 */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001748 mutex_unlock(&dir->d_inode->i_mutex);
Al Viro5a190ae2007-06-07 12:19:32 -04001749 audit_inode(pathname, path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
1751 error = -EEXIST;
1752 if (flag & O_EXCL)
1753 goto exit_dput;
1754
Al Viroe13b2102005-06-06 13:36:06 -07001755 if (__follow_mount(&path)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 error = -ELOOP;
Al Viroba7a4c12005-06-06 13:36:08 -07001757 if (flag & O_NOFOLLOW)
1758 goto exit_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 }
Amy Griffis3e2efce2006-07-13 13:16:02 -04001760
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 error = -ENOENT;
Al Viro4e7506e2005-06-06 13:36:00 -07001762 if (!path.dentry->d_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 goto exit_dput;
Al Viro4e7506e2005-06-06 13:36:00 -07001764 if (path.dentry->d_inode->i_op && path.dentry->d_inode->i_op->follow_link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 goto do_link;
1766
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001767 path_to_nameidata(&path, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 error = -EISDIR;
Al Viro4e7506e2005-06-06 13:36:00 -07001769 if (path.dentry->d_inode && S_ISDIR(path.dentry->d_inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 goto exit;
1771ok:
Dave Hansen4a3fd212008-02-15 14:37:48 -08001772 /*
1773 * Consider:
1774 * 1. may_open() truncates a file
1775 * 2. a rw->ro mount transition occurs
1776 * 3. nameidata_to_filp() fails due to
1777 * the ro mount.
1778 * That would be inconsistent, and should
1779 * be avoided. Taking this mnt write here
1780 * ensures that (2) can not occur.
1781 */
1782 will_write = open_will_write_to_fs(flag, nd.path.dentry->d_inode);
1783 if (will_write) {
1784 error = mnt_want_write(nd.path.mnt);
1785 if (error)
1786 goto exit;
1787 }
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001788 error = may_open(&nd, acc_mode, flag);
Dave Hansen4a3fd212008-02-15 14:37:48 -08001789 if (error) {
1790 if (will_write)
1791 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 goto exit;
Dave Hansen4a3fd212008-02-15 14:37:48 -08001793 }
1794 filp = nameidata_to_filp(&nd, open_flag);
1795 /*
1796 * It is now safe to drop the mnt write
1797 * because the filp has had a write taken
1798 * on its behalf.
1799 */
1800 if (will_write)
1801 mnt_drop_write(nd.path.mnt);
1802 return filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
Dave Hansen4a3fd212008-02-15 14:37:48 -08001804exit_mutex_unlock:
1805 mutex_unlock(&dir->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806exit_dput:
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001807 path_put_conditional(&path, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808exit:
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001809 if (!IS_ERR(nd.intent.open.file))
1810 release_open_intent(&nd);
Al Viro8737f3a2008-08-02 22:36:57 -04001811exit_parent:
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001812 path_put(&nd.path);
1813 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
1815do_link:
1816 error = -ELOOP;
1817 if (flag & O_NOFOLLOW)
1818 goto exit_dput;
1819 /*
1820 * This is subtle. Instead of calling do_follow_link() we do the
1821 * thing by hands. The reason is that this way we have zero link_count
1822 * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1823 * After that we have the parent and last component, i.e.
1824 * we are in the same situation as after the first path_walk().
1825 * Well, almost - if the last component is normal we get its copy
1826 * stored in nd->last.name and we will have to putname() it when we
1827 * are done. Procfs-like symlinks just set LAST_BIND.
1828 */
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001829 nd.flags |= LOOKUP_PARENT;
1830 error = security_inode_follow_link(path.dentry, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 if (error)
1832 goto exit_dput;
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001833 error = __do_follow_link(&path, &nd);
Kirill Korotaevde459212006-07-14 00:23:49 -07001834 if (error) {
1835 /* Does someone understand code flow here? Or it is only
1836 * me so stupid? Anathema to whoever designed this non-sense
1837 * with "intent.open".
1838 */
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001839 release_open_intent(&nd);
1840 return ERR_PTR(error);
Kirill Korotaevde459212006-07-14 00:23:49 -07001841 }
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001842 nd.flags &= ~LOOKUP_PARENT;
1843 if (nd.last_type == LAST_BIND)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 goto ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 error = -EISDIR;
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001846 if (nd.last_type != LAST_NORM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 goto exit;
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001848 if (nd.last.name[nd.last.len]) {
1849 __putname(nd.last.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 goto exit;
1851 }
1852 error = -ELOOP;
1853 if (count++==32) {
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001854 __putname(nd.last.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 goto exit;
1856 }
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001857 dir = nd.path.dentry;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001858 mutex_lock(&dir->d_inode->i_mutex);
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001859 path.dentry = lookup_hash(&nd);
1860 path.mnt = nd.path.mnt;
1861 __putname(nd.last.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 goto do_last;
1863}
1864
1865/**
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001866 * filp_open - open file and return file pointer
1867 *
1868 * @filename: path to open
1869 * @flags: open flags as per the open(2) second argument
1870 * @mode: mode for the new file if O_CREAT is set, else ignored
1871 *
1872 * This is the helper to open a file from kernelspace if you really
1873 * have to. But in generally you should not do this, so please move
1874 * along, nothing to see here..
1875 */
1876struct file *filp_open(const char *filename, int flags, int mode)
1877{
1878 return do_filp_open(AT_FDCWD, filename, flags, mode);
1879}
1880EXPORT_SYMBOL(filp_open);
1881
1882/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 * lookup_create - lookup a dentry, creating it if it doesn't exist
1884 * @nd: nameidata info
1885 * @is_dir: directory flag
1886 *
1887 * Simple function to lookup and return a dentry and create it
1888 * if it doesn't exist. Is SMP-safe.
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07001889 *
Jan Blunck4ac91372008-02-14 19:34:32 -08001890 * Returns with nd->path.dentry->d_inode->i_mutex locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 */
1892struct dentry *lookup_create(struct nameidata *nd, int is_dir)
1893{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07001894 struct dentry *dentry = ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Jan Blunck4ac91372008-02-14 19:34:32 -08001896 mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07001897 /*
1898 * Yucky last component or no last component at all?
1899 * (foo/., foo/.., /////)
1900 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 if (nd->last_type != LAST_NORM)
1902 goto fail;
1903 nd->flags &= ~LOOKUP_PARENT;
Al Viro35165862008-08-05 03:00:49 -04001904 nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
ASANO Masahiroa6349042006-08-22 20:06:02 -04001905 nd->intent.open.flags = O_EXCL;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07001906
1907 /*
1908 * Do the final lookup.
1909 */
Christoph Hellwig49705b72005-11-08 21:35:06 -08001910 dentry = lookup_hash(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 if (IS_ERR(dentry))
1912 goto fail;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07001913
Al Viroe9baf6e2008-05-15 04:49:12 -04001914 if (dentry->d_inode)
1915 goto eexist;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07001916 /*
1917 * Special case - lookup gave negative, but... we had foo/bar/
1918 * From the vfs_mknod() POV we just have a negative dentry -
1919 * all is fine. Let's be bastards - you had / on the end, you've
1920 * been asking for (non-existent) directory. -ENOENT for you.
1921 */
Al Viroe9baf6e2008-05-15 04:49:12 -04001922 if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
1923 dput(dentry);
1924 dentry = ERR_PTR(-ENOENT);
1925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 return dentry;
Al Viroe9baf6e2008-05-15 04:49:12 -04001927eexist:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 dput(dentry);
Al Viroe9baf6e2008-05-15 04:49:12 -04001929 dentry = ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930fail:
1931 return dentry;
1932}
Christoph Hellwigf81a0bf2005-05-19 12:26:43 -07001933EXPORT_SYMBOL_GPL(lookup_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
1935int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1936{
Miklos Szeredia95164d2008-07-30 15:08:48 +02001937 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
1939 if (error)
1940 return error;
1941
1942 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1943 return -EPERM;
1944
1945 if (!dir->i_op || !dir->i_op->mknod)
1946 return -EPERM;
1947
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07001948 error = devcgroup_inode_mknod(mode, dev);
1949 if (error)
1950 return error;
1951
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 error = security_inode_mknod(dir, dentry, mode, dev);
1953 if (error)
1954 return error;
1955
1956 DQUOT_INIT(dir);
1957 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07001958 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00001959 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 return error;
1961}
1962
Dave Hansen463c3192008-02-15 14:37:57 -08001963static int may_mknod(mode_t mode)
1964{
1965 switch (mode & S_IFMT) {
1966 case S_IFREG:
1967 case S_IFCHR:
1968 case S_IFBLK:
1969 case S_IFIFO:
1970 case S_IFSOCK:
1971 case 0: /* zero mode translates to S_IFREG */
1972 return 0;
1973 case S_IFDIR:
1974 return -EPERM;
1975 default:
1976 return -EINVAL;
1977 }
1978}
1979
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001980asmlinkage long sys_mknodat(int dfd, const char __user *filename, int mode,
1981 unsigned dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982{
Al Viro2ad94ae2008-07-21 09:32:51 -04001983 int error;
1984 char *tmp;
1985 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 struct nameidata nd;
1987
1988 if (S_ISDIR(mode))
1989 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
Al Viro2ad94ae2008-07-21 09:32:51 -04001991 error = user_path_parent(dfd, filename, &nd, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04001993 return error;
1994
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 dentry = lookup_create(&nd, 0);
Dave Hansen463c3192008-02-15 14:37:57 -08001996 if (IS_ERR(dentry)) {
1997 error = PTR_ERR(dentry);
1998 goto out_unlock;
1999 }
Jan Blunck4ac91372008-02-14 19:34:32 -08002000 if (!IS_POSIXACL(nd.path.dentry->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 mode &= ~current->fs->umask;
Dave Hansen463c3192008-02-15 14:37:57 -08002002 error = may_mknod(mode);
2003 if (error)
2004 goto out_dput;
2005 error = mnt_want_write(nd.path.mnt);
2006 if (error)
2007 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002008 error = security_path_mknod(&nd.path, dentry, mode, dev);
2009 if (error)
2010 goto out_drop_write;
Dave Hansen463c3192008-02-15 14:37:57 -08002011 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 case 0: case S_IFREG:
Jan Blunck4ac91372008-02-14 19:34:32 -08002013 error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 break;
2015 case S_IFCHR: case S_IFBLK:
Jan Blunck4ac91372008-02-14 19:34:32 -08002016 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 new_decode_dev(dev));
2018 break;
2019 case S_IFIFO: case S_IFSOCK:
Jan Blunck4ac91372008-02-14 19:34:32 -08002020 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 }
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002023out_drop_write:
Dave Hansen463c3192008-02-15 14:37:57 -08002024 mnt_drop_write(nd.path.mnt);
2025out_dput:
2026 dput(dentry);
2027out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002028 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002029 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 putname(tmp);
2031
2032 return error;
2033}
2034
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002035asmlinkage long sys_mknod(const char __user *filename, int mode, unsigned dev)
2036{
2037 return sys_mknodat(AT_FDCWD, filename, mode, dev);
2038}
2039
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2041{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002042 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
2044 if (error)
2045 return error;
2046
2047 if (!dir->i_op || !dir->i_op->mkdir)
2048 return -EPERM;
2049
2050 mode &= (S_IRWXUGO|S_ISVTX);
2051 error = security_inode_mkdir(dir, dentry, mode);
2052 if (error)
2053 return error;
2054
2055 DQUOT_INIT(dir);
2056 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002057 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002058 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 return error;
2060}
2061
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002062asmlinkage long sys_mkdirat(int dfd, const char __user *pathname, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063{
2064 int error = 0;
2065 char * tmp;
Dave Hansen6902d922006-09-30 23:29:01 -07002066 struct dentry *dentry;
2067 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
Al Viro2ad94ae2008-07-21 09:32:51 -04002069 error = user_path_parent(dfd, pathname, &nd, &tmp);
2070 if (error)
Dave Hansen6902d922006-09-30 23:29:01 -07002071 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Dave Hansen6902d922006-09-30 23:29:01 -07002073 dentry = lookup_create(&nd, 1);
2074 error = PTR_ERR(dentry);
2075 if (IS_ERR(dentry))
2076 goto out_unlock;
2077
Jan Blunck4ac91372008-02-14 19:34:32 -08002078 if (!IS_POSIXACL(nd.path.dentry->d_inode))
Dave Hansen6902d922006-09-30 23:29:01 -07002079 mode &= ~current->fs->umask;
Dave Hansen463c3192008-02-15 14:37:57 -08002080 error = mnt_want_write(nd.path.mnt);
2081 if (error)
2082 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002083 error = security_path_mkdir(&nd.path, dentry, mode);
2084 if (error)
2085 goto out_drop_write;
Jan Blunck4ac91372008-02-14 19:34:32 -08002086 error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002087out_drop_write:
Dave Hansen463c3192008-02-15 14:37:57 -08002088 mnt_drop_write(nd.path.mnt);
2089out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002090 dput(dentry);
2091out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002092 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002093 path_put(&nd.path);
Dave Hansen6902d922006-09-30 23:29:01 -07002094 putname(tmp);
2095out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 return error;
2097}
2098
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002099asmlinkage long sys_mkdir(const char __user *pathname, int mode)
2100{
2101 return sys_mkdirat(AT_FDCWD, pathname, mode);
2102}
2103
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104/*
2105 * We try to drop the dentry early: we should have
2106 * a usage count of 2 if we're the only user of this
2107 * dentry, and if that is true (possibly after pruning
2108 * the dcache), then we drop the dentry now.
2109 *
2110 * A low-level filesystem can, if it choses, legally
2111 * do a
2112 *
2113 * if (!d_unhashed(dentry))
2114 * return -EBUSY;
2115 *
2116 * if it cannot handle the case of removing a directory
2117 * that is still in use by something else..
2118 */
2119void dentry_unhash(struct dentry *dentry)
2120{
2121 dget(dentry);
Vasily Averindc168422006-12-06 20:37:07 -08002122 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 spin_lock(&dcache_lock);
2124 spin_lock(&dentry->d_lock);
2125 if (atomic_read(&dentry->d_count) == 2)
2126 __d_drop(dentry);
2127 spin_unlock(&dentry->d_lock);
2128 spin_unlock(&dcache_lock);
2129}
2130
2131int vfs_rmdir(struct inode *dir, struct dentry *dentry)
2132{
2133 int error = may_delete(dir, dentry, 1);
2134
2135 if (error)
2136 return error;
2137
2138 if (!dir->i_op || !dir->i_op->rmdir)
2139 return -EPERM;
2140
2141 DQUOT_INIT(dir);
2142
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002143 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 dentry_unhash(dentry);
2145 if (d_mountpoint(dentry))
2146 error = -EBUSY;
2147 else {
2148 error = security_inode_rmdir(dir, dentry);
2149 if (!error) {
2150 error = dir->i_op->rmdir(dir, dentry);
2151 if (!error)
2152 dentry->d_inode->i_flags |= S_DEAD;
2153 }
2154 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002155 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 d_delete(dentry);
2158 }
2159 dput(dentry);
2160
2161 return error;
2162}
2163
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002164static long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165{
2166 int error = 0;
2167 char * name;
2168 struct dentry *dentry;
2169 struct nameidata nd;
2170
Al Viro2ad94ae2008-07-21 09:32:51 -04002171 error = user_path_parent(dfd, pathname, &nd, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002173 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
2175 switch(nd.last_type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002176 case LAST_DOTDOT:
2177 error = -ENOTEMPTY;
2178 goto exit1;
2179 case LAST_DOT:
2180 error = -EINVAL;
2181 goto exit1;
2182 case LAST_ROOT:
2183 error = -EBUSY;
2184 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002186
2187 nd.flags &= ~LOOKUP_PARENT;
2188
Jan Blunck4ac91372008-02-14 19:34:32 -08002189 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002190 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002192 if (IS_ERR(dentry))
2193 goto exit2;
Dave Hansen06227532008-02-15 14:37:34 -08002194 error = mnt_want_write(nd.path.mnt);
2195 if (error)
2196 goto exit3;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002197 error = security_path_rmdir(&nd.path, dentry);
2198 if (error)
2199 goto exit4;
Jan Blunck4ac91372008-02-14 19:34:32 -08002200 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002201exit4:
Dave Hansen06227532008-02-15 14:37:34 -08002202 mnt_drop_write(nd.path.mnt);
2203exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07002204 dput(dentry);
2205exit2:
Jan Blunck4ac91372008-02-14 19:34:32 -08002206 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002208 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 putname(name);
2210 return error;
2211}
2212
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002213asmlinkage long sys_rmdir(const char __user *pathname)
2214{
2215 return do_rmdir(AT_FDCWD, pathname);
2216}
2217
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218int vfs_unlink(struct inode *dir, struct dentry *dentry)
2219{
2220 int error = may_delete(dir, dentry, 0);
2221
2222 if (error)
2223 return error;
2224
2225 if (!dir->i_op || !dir->i_op->unlink)
2226 return -EPERM;
2227
2228 DQUOT_INIT(dir);
2229
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002230 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 if (d_mountpoint(dentry))
2232 error = -EBUSY;
2233 else {
2234 error = security_inode_unlink(dir, dentry);
2235 if (!error)
2236 error = dir->i_op->unlink(dir, dentry);
2237 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002238 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
2240 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2241 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
Jan Karaece95912008-02-06 01:37:13 -08002242 fsnotify_link_count(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 }
Robert Love0eeca282005-07-12 17:06:03 -04002245
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 return error;
2247}
2248
2249/*
2250 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002251 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 * writeout happening, and we don't want to prevent access to the directory
2253 * while waiting on the I/O.
2254 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002255static long do_unlinkat(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256{
Al Viro2ad94ae2008-07-21 09:32:51 -04002257 int error;
2258 char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 struct dentry *dentry;
2260 struct nameidata nd;
2261 struct inode *inode = NULL;
2262
Al Viro2ad94ae2008-07-21 09:32:51 -04002263 error = user_path_parent(dfd, pathname, &nd, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002265 return error;
2266
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 error = -EISDIR;
2268 if (nd.last_type != LAST_NORM)
2269 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002270
2271 nd.flags &= ~LOOKUP_PARENT;
2272
Jan Blunck4ac91372008-02-14 19:34:32 -08002273 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002274 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 error = PTR_ERR(dentry);
2276 if (!IS_ERR(dentry)) {
2277 /* Why not before? Because we want correct error value */
2278 if (nd.last.name[nd.last.len])
2279 goto slashes;
2280 inode = dentry->d_inode;
2281 if (inode)
2282 atomic_inc(&inode->i_count);
Dave Hansen06227532008-02-15 14:37:34 -08002283 error = mnt_want_write(nd.path.mnt);
2284 if (error)
2285 goto exit2;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002286 error = security_path_unlink(&nd.path, dentry);
2287 if (error)
2288 goto exit3;
Jan Blunck4ac91372008-02-14 19:34:32 -08002289 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002290exit3:
Dave Hansen06227532008-02-15 14:37:34 -08002291 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 exit2:
2293 dput(dentry);
2294 }
Jan Blunck4ac91372008-02-14 19:34:32 -08002295 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 if (inode)
2297 iput(inode); /* truncate the inode here */
2298exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002299 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 putname(name);
2301 return error;
2302
2303slashes:
2304 error = !dentry->d_inode ? -ENOENT :
2305 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2306 goto exit2;
2307}
2308
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002309asmlinkage long sys_unlinkat(int dfd, const char __user *pathname, int flag)
2310{
2311 if ((flag & ~AT_REMOVEDIR) != 0)
2312 return -EINVAL;
2313
2314 if (flag & AT_REMOVEDIR)
2315 return do_rmdir(dfd, pathname);
2316
2317 return do_unlinkat(dfd, pathname);
2318}
2319
2320asmlinkage long sys_unlink(const char __user *pathname)
2321{
2322 return do_unlinkat(AT_FDCWD, pathname);
2323}
2324
Miklos Szeredidb2e7472008-06-24 16:50:16 +02002325int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002327 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
2329 if (error)
2330 return error;
2331
2332 if (!dir->i_op || !dir->i_op->symlink)
2333 return -EPERM;
2334
2335 error = security_inode_symlink(dir, dentry, oldname);
2336 if (error)
2337 return error;
2338
2339 DQUOT_INIT(dir);
2340 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002341 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002342 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 return error;
2344}
2345
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002346asmlinkage long sys_symlinkat(const char __user *oldname,
2347 int newdfd, const char __user *newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348{
Al Viro2ad94ae2008-07-21 09:32:51 -04002349 int error;
2350 char *from;
2351 char *to;
Dave Hansen6902d922006-09-30 23:29:01 -07002352 struct dentry *dentry;
2353 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
2355 from = getname(oldname);
Al Viro2ad94ae2008-07-21 09:32:51 -04002356 if (IS_ERR(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 return PTR_ERR(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04002358
2359 error = user_path_parent(newdfd, newname, &nd, &to);
2360 if (error)
Dave Hansen6902d922006-09-30 23:29:01 -07002361 goto out_putname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
Dave Hansen6902d922006-09-30 23:29:01 -07002363 dentry = lookup_create(&nd, 0);
2364 error = PTR_ERR(dentry);
2365 if (IS_ERR(dentry))
2366 goto out_unlock;
2367
Dave Hansen75c3f292008-02-15 14:37:45 -08002368 error = mnt_want_write(nd.path.mnt);
2369 if (error)
2370 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002371 error = security_path_symlink(&nd.path, dentry, from);
2372 if (error)
2373 goto out_drop_write;
Miklos Szeredidb2e7472008-06-24 16:50:16 +02002374 error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002375out_drop_write:
Dave Hansen75c3f292008-02-15 14:37:45 -08002376 mnt_drop_write(nd.path.mnt);
2377out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002378 dput(dentry);
2379out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002380 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002381 path_put(&nd.path);
Dave Hansen6902d922006-09-30 23:29:01 -07002382 putname(to);
2383out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 putname(from);
2385 return error;
2386}
2387
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002388asmlinkage long sys_symlink(const char __user *oldname, const char __user *newname)
2389{
2390 return sys_symlinkat(oldname, AT_FDCWD, newname);
2391}
2392
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2394{
2395 struct inode *inode = old_dentry->d_inode;
2396 int error;
2397
2398 if (!inode)
2399 return -ENOENT;
2400
Miklos Szeredia95164d2008-07-30 15:08:48 +02002401 error = may_create(dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 if (error)
2403 return error;
2404
2405 if (dir->i_sb != inode->i_sb)
2406 return -EXDEV;
2407
2408 /*
2409 * A link to an append-only or immutable file cannot be created.
2410 */
2411 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2412 return -EPERM;
2413 if (!dir->i_op || !dir->i_op->link)
2414 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002415 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 return -EPERM;
2417
2418 error = security_inode_link(old_dentry, dir, new_dentry);
2419 if (error)
2420 return error;
2421
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002422 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 DQUOT_INIT(dir);
2424 error = dir->i_op->link(old_dentry, dir, new_dentry);
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002425 mutex_unlock(&inode->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07002426 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002427 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 return error;
2429}
2430
2431/*
2432 * Hardlinks are often used in delicate situations. We avoid
2433 * security-related surprises by not following symlinks on the
2434 * newname. --KAB
2435 *
2436 * We don't follow them on the oldname either to be compatible
2437 * with linux 2.0, and to avoid hard-linking to directories
2438 * and other special files. --ADM
2439 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002440asmlinkage long sys_linkat(int olddfd, const char __user *oldname,
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002441 int newdfd, const char __user *newname,
2442 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443{
2444 struct dentry *new_dentry;
Al Viro2d8f3032008-07-22 09:59:21 -04002445 struct nameidata nd;
2446 struct path old_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 int error;
Al Viro2ad94ae2008-07-21 09:32:51 -04002448 char *to;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
Ulrich Drepper45c9b112006-06-25 05:49:11 -07002450 if ((flags & ~AT_SYMLINK_FOLLOW) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002451 return -EINVAL;
2452
Al Viro2d8f3032008-07-22 09:59:21 -04002453 error = user_path_at(olddfd, oldname,
2454 flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0,
2455 &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002457 return error;
2458
2459 error = user_path_parent(newdfd, newname, &nd, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 if (error)
2461 goto out;
2462 error = -EXDEV;
Al Viro2d8f3032008-07-22 09:59:21 -04002463 if (old_path.mnt != nd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 goto out_release;
2465 new_dentry = lookup_create(&nd, 0);
2466 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002467 if (IS_ERR(new_dentry))
2468 goto out_unlock;
Dave Hansen75c3f292008-02-15 14:37:45 -08002469 error = mnt_want_write(nd.path.mnt);
2470 if (error)
2471 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002472 error = security_path_link(old_path.dentry, &nd.path, new_dentry);
2473 if (error)
2474 goto out_drop_write;
Al Viro2d8f3032008-07-22 09:59:21 -04002475 error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002476out_drop_write:
Dave Hansen75c3f292008-02-15 14:37:45 -08002477 mnt_drop_write(nd.path.mnt);
2478out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002479 dput(new_dentry);
2480out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002481 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482out_release:
Jan Blunck1d957f92008-02-14 19:34:35 -08002483 path_put(&nd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04002484 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485out:
Al Viro2d8f3032008-07-22 09:59:21 -04002486 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
2488 return error;
2489}
2490
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002491asmlinkage long sys_link(const char __user *oldname, const char __user *newname)
2492{
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002493 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002494}
2495
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496/*
2497 * The worst of all namespace operations - renaming directory. "Perverted"
2498 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2499 * Problems:
2500 * a) we can get into loop creation. Check is done in is_subdir().
2501 * b) race potential - two innocent renames can create a loop together.
2502 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002503 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 * story.
2505 * c) we have to lock _three_ objects - parents and victim (if it exists).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002506 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 * whether the target exists). Solution: try to be smart with locking
2508 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002509 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 * move will be locked. Thus we can rank directories by the tree
2511 * (ancestors first) and rank all non-directories after them.
2512 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002513 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 * HOWEVER, it relies on the assumption that any object with ->lookup()
2515 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2516 * we'd better make sure that there's no link(2) for them.
2517 * d) some filesystems don't support opened-but-unlinked directories,
2518 * either because of layout or because they are not ready to deal with
2519 * all cases correctly. The latter will be fixed (taking this sort of
2520 * stuff into VFS), but the former is not going away. Solution: the same
2521 * trick as in rmdir().
2522 * e) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002523 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002525 * ->i_mutex on parents, which works but leads to some truely excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 * locking].
2527 */
Adrian Bunk75c96f82005-05-05 16:16:09 -07002528static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
2529 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530{
2531 int error = 0;
2532 struct inode *target;
2533
2534 /*
2535 * If we are going to change the parent - check write permissions,
2536 * we'll need to flip '..'.
2537 */
2538 if (new_dir != old_dir) {
Al Virof419a2e2008-07-22 00:07:17 -04002539 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 if (error)
2541 return error;
2542 }
2543
2544 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2545 if (error)
2546 return error;
2547
2548 target = new_dentry->d_inode;
2549 if (target) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002550 mutex_lock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 dentry_unhash(new_dentry);
2552 }
2553 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2554 error = -EBUSY;
2555 else
2556 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2557 if (target) {
2558 if (!error)
2559 target->i_flags |= S_DEAD;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002560 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 if (d_unhashed(new_dentry))
2562 d_rehash(new_dentry);
2563 dput(new_dentry);
2564 }
Stephen Smalleye31e14e2005-09-09 13:01:45 -07002565 if (!error)
Mark Fasheh349457c2006-09-08 14:22:21 -07002566 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
2567 d_move(old_dentry,new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 return error;
2569}
2570
Adrian Bunk75c96f82005-05-05 16:16:09 -07002571static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
2572 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573{
2574 struct inode *target;
2575 int error;
2576
2577 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2578 if (error)
2579 return error;
2580
2581 dget(new_dentry);
2582 target = new_dentry->d_inode;
2583 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002584 mutex_lock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2586 error = -EBUSY;
2587 else
2588 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2589 if (!error) {
Mark Fasheh349457c2006-09-08 14:22:21 -07002590 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 d_move(old_dentry, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 }
2593 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002594 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 dput(new_dentry);
2596 return error;
2597}
2598
2599int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
2600 struct inode *new_dir, struct dentry *new_dentry)
2601{
2602 int error;
2603 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
Robert Love0eeca282005-07-12 17:06:03 -04002604 const char *old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
2606 if (old_dentry->d_inode == new_dentry->d_inode)
2607 return 0;
2608
2609 error = may_delete(old_dir, old_dentry, is_dir);
2610 if (error)
2611 return error;
2612
2613 if (!new_dentry->d_inode)
Miklos Szeredia95164d2008-07-30 15:08:48 +02002614 error = may_create(new_dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 else
2616 error = may_delete(new_dir, new_dentry, is_dir);
2617 if (error)
2618 return error;
2619
2620 if (!old_dir->i_op || !old_dir->i_op->rename)
2621 return -EPERM;
2622
2623 DQUOT_INIT(old_dir);
2624 DQUOT_INIT(new_dir);
2625
Robert Love0eeca282005-07-12 17:06:03 -04002626 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
2627
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 if (is_dir)
2629 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
2630 else
2631 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
2632 if (!error) {
Robert Love0eeca282005-07-12 17:06:03 -04002633 const char *new_name = old_dentry->d_name.name;
John McCutchan89204c42005-08-15 12:13:28 -04002634 fsnotify_move(old_dir, new_dir, old_name, new_name, is_dir,
Al Viro5a190ae2007-06-07 12:19:32 -04002635 new_dentry->d_inode, old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 }
Robert Love0eeca282005-07-12 17:06:03 -04002637 fsnotify_oldname_free(old_name);
2638
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 return error;
2640}
2641
Al Viro2ad94ae2008-07-21 09:32:51 -04002642asmlinkage long sys_renameat(int olddfd, const char __user *oldname,
2643 int newdfd, const char __user *newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644{
Al Viro2ad94ae2008-07-21 09:32:51 -04002645 struct dentry *old_dir, *new_dir;
2646 struct dentry *old_dentry, *new_dentry;
2647 struct dentry *trap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 struct nameidata oldnd, newnd;
Al Viro2ad94ae2008-07-21 09:32:51 -04002649 char *from;
2650 char *to;
2651 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652
Al Viro2ad94ae2008-07-21 09:32:51 -04002653 error = user_path_parent(olddfd, oldname, &oldnd, &from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 if (error)
2655 goto exit;
2656
Al Viro2ad94ae2008-07-21 09:32:51 -04002657 error = user_path_parent(newdfd, newname, &newnd, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 if (error)
2659 goto exit1;
2660
2661 error = -EXDEV;
Jan Blunck4ac91372008-02-14 19:34:32 -08002662 if (oldnd.path.mnt != newnd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 goto exit2;
2664
Jan Blunck4ac91372008-02-14 19:34:32 -08002665 old_dir = oldnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 error = -EBUSY;
2667 if (oldnd.last_type != LAST_NORM)
2668 goto exit2;
2669
Jan Blunck4ac91372008-02-14 19:34:32 -08002670 new_dir = newnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 if (newnd.last_type != LAST_NORM)
2672 goto exit2;
2673
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002674 oldnd.flags &= ~LOOKUP_PARENT;
2675 newnd.flags &= ~LOOKUP_PARENT;
OGAWA Hirofumi4e9ed2f2008-10-16 07:50:29 +09002676 newnd.flags |= LOOKUP_RENAME_TARGET;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002677
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 trap = lock_rename(new_dir, old_dir);
2679
Christoph Hellwig49705b72005-11-08 21:35:06 -08002680 old_dentry = lookup_hash(&oldnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 error = PTR_ERR(old_dentry);
2682 if (IS_ERR(old_dentry))
2683 goto exit3;
2684 /* source must exist */
2685 error = -ENOENT;
2686 if (!old_dentry->d_inode)
2687 goto exit4;
2688 /* unless the source is a directory trailing slashes give -ENOTDIR */
2689 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
2690 error = -ENOTDIR;
2691 if (oldnd.last.name[oldnd.last.len])
2692 goto exit4;
2693 if (newnd.last.name[newnd.last.len])
2694 goto exit4;
2695 }
2696 /* source should not be ancestor of target */
2697 error = -EINVAL;
2698 if (old_dentry == trap)
2699 goto exit4;
Christoph Hellwig49705b72005-11-08 21:35:06 -08002700 new_dentry = lookup_hash(&newnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 error = PTR_ERR(new_dentry);
2702 if (IS_ERR(new_dentry))
2703 goto exit4;
2704 /* target should not be an ancestor of source */
2705 error = -ENOTEMPTY;
2706 if (new_dentry == trap)
2707 goto exit5;
2708
Dave Hansen9079b1e2008-02-15 14:37:49 -08002709 error = mnt_want_write(oldnd.path.mnt);
2710 if (error)
2711 goto exit5;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002712 error = security_path_rename(&oldnd.path, old_dentry,
2713 &newnd.path, new_dentry);
2714 if (error)
2715 goto exit6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 error = vfs_rename(old_dir->d_inode, old_dentry,
2717 new_dir->d_inode, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002718exit6:
Dave Hansen9079b1e2008-02-15 14:37:49 -08002719 mnt_drop_write(oldnd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720exit5:
2721 dput(new_dentry);
2722exit4:
2723 dput(old_dentry);
2724exit3:
2725 unlock_rename(new_dir, old_dir);
2726exit2:
Jan Blunck1d957f92008-02-14 19:34:35 -08002727 path_put(&newnd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04002728 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002730 path_put(&oldnd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 putname(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04002732exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 return error;
2734}
2735
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002736asmlinkage long sys_rename(const char __user *oldname, const char __user *newname)
2737{
2738 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
2739}
2740
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
2742{
2743 int len;
2744
2745 len = PTR_ERR(link);
2746 if (IS_ERR(link))
2747 goto out;
2748
2749 len = strlen(link);
2750 if (len > (unsigned) buflen)
2751 len = buflen;
2752 if (copy_to_user(buffer, link, len))
2753 len = -EFAULT;
2754out:
2755 return len;
2756}
2757
2758/*
2759 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
2760 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
2761 * using) it for any given inode is up to filesystem.
2762 */
2763int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2764{
2765 struct nameidata nd;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002766 void *cookie;
Marcin Slusarz694a1762008-06-09 16:40:37 -07002767 int res;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002768
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 nd.depth = 0;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002770 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
Marcin Slusarz694a1762008-06-09 16:40:37 -07002771 if (IS_ERR(cookie))
2772 return PTR_ERR(cookie);
2773
2774 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
2775 if (dentry->d_inode->i_op->put_link)
2776 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
2777 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778}
2779
2780int vfs_follow_link(struct nameidata *nd, const char *link)
2781{
2782 return __vfs_follow_link(nd, link);
2783}
2784
2785/* get the link contents into pagecache */
2786static char *page_getlink(struct dentry * dentry, struct page **ppage)
2787{
Duane Griffinebd09ab2008-12-19 20:47:12 +00002788 char *kaddr;
2789 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 struct address_space *mapping = dentry->d_inode->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -07002791 page = read_mapping_page(mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 if (IS_ERR(page))
Nick Piggin6fe69002007-05-06 14:49:04 -07002793 return (char*)page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 *ppage = page;
Duane Griffinebd09ab2008-12-19 20:47:12 +00002795 kaddr = kmap(page);
2796 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
2797 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798}
2799
2800int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2801{
2802 struct page *page = NULL;
2803 char *s = page_getlink(dentry, &page);
2804 int res = vfs_readlink(dentry,buffer,buflen,s);
2805 if (page) {
2806 kunmap(page);
2807 page_cache_release(page);
2808 }
2809 return res;
2810}
2811
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002812void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002814 struct page *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 nd_set_link(nd, page_getlink(dentry, &page));
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002816 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817}
2818
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002819void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002821 struct page *page = cookie;
2822
2823 if (page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 kunmap(page);
2825 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 }
2827}
2828
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08002829int __page_symlink(struct inode *inode, const char *symname, int len,
2830 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831{
2832 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08002833 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07002834 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08002835 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 char *kaddr;
2837
NeilBrown7e53cac2006-03-25 03:07:57 -08002838retry:
Nick Pigginafddba42007-10-16 01:25:01 -07002839 err = pagecache_write_begin(NULL, mapping, 0, len-1,
2840 AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07002842 goto fail;
2843
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 kaddr = kmap_atomic(page, KM_USER0);
2845 memcpy(kaddr, symname, len-1);
2846 kunmap_atomic(kaddr, KM_USER0);
Nick Pigginafddba42007-10-16 01:25:01 -07002847
2848 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
2849 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 if (err < 0)
2851 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07002852 if (err < len-1)
2853 goto retry;
2854
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 mark_inode_dirty(inode);
2856 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857fail:
2858 return err;
2859}
2860
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08002861int page_symlink(struct inode *inode, const char *symname, int len)
2862{
2863 return __page_symlink(inode, symname, len,
2864 mapping_gfp_mask(inode->i_mapping));
2865}
2866
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002867const struct inode_operations page_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 .readlink = generic_readlink,
2869 .follow_link = page_follow_link_light,
2870 .put_link = page_put_link,
2871};
2872
Al Viro2d8f3032008-07-22 09:59:21 -04002873EXPORT_SYMBOL(user_path_at);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874EXPORT_SYMBOL(follow_down);
2875EXPORT_SYMBOL(follow_up);
2876EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
2877EXPORT_SYMBOL(getname);
2878EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879EXPORT_SYMBOL(lookup_one_len);
2880EXPORT_SYMBOL(page_follow_link_light);
2881EXPORT_SYMBOL(page_put_link);
2882EXPORT_SYMBOL(page_readlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08002883EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884EXPORT_SYMBOL(page_symlink);
2885EXPORT_SYMBOL(page_symlink_inode_operations);
2886EXPORT_SYMBOL(path_lookup);
Al Virod1811462008-08-02 00:49:18 -04002887EXPORT_SYMBOL(kern_path);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002888EXPORT_SYMBOL(vfs_path_lookup);
Al Virof419a2e2008-07-22 00:07:17 -04002889EXPORT_SYMBOL(inode_permission);
Christoph Hellwige4543ed2005-11-08 21:35:04 -08002890EXPORT_SYMBOL(vfs_permission);
Christoph Hellwig8c744fb2005-11-08 21:35:04 -08002891EXPORT_SYMBOL(file_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892EXPORT_SYMBOL(unlock_rename);
2893EXPORT_SYMBOL(vfs_create);
2894EXPORT_SYMBOL(vfs_follow_link);
2895EXPORT_SYMBOL(vfs_link);
2896EXPORT_SYMBOL(vfs_mkdir);
2897EXPORT_SYMBOL(vfs_mknod);
2898EXPORT_SYMBOL(generic_permission);
2899EXPORT_SYMBOL(vfs_readlink);
2900EXPORT_SYMBOL(vfs_rename);
2901EXPORT_SYMBOL(vfs_rmdir);
2902EXPORT_SYMBOL(vfs_symlink);
2903EXPORT_SYMBOL(vfs_unlink);
2904EXPORT_SYMBOL(dentry_unhash);
2905EXPORT_SYMBOL(generic_readlink);