blob: b3780e3fc88ea18f2b7f5a2666702312f501b8b5 [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>
33#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/namei.h>
35#include <asm/uaccess.h>
36
37#define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
38
39/* [Feb-1997 T. Schoebel-Theuer]
40 * Fundamental changes in the pathname lookup mechanisms (namei)
41 * were necessary because of omirr. The reason is that omirr needs
42 * to know the _real_ pathname, not the user-supplied one, in case
43 * of symlinks (and also when transname replacements occur).
44 *
45 * The new code replaces the old recursive symlink resolution with
46 * an iterative one (in case of non-nested symlink chains). It does
47 * this with calls to <fs>_follow_link().
48 * As a side effect, dir_namei(), _namei() and follow_link() are now
49 * replaced with a single function lookup_dentry() that can handle all
50 * the special cases of the former code.
51 *
52 * With the new dcache, the pathname is stored at each inode, at least as
53 * long as the refcount of the inode is positive. As a side effect, the
54 * size of the dcache depends on the inode cache and thus is dynamic.
55 *
56 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
57 * resolution to correspond with current state of the code.
58 *
59 * Note that the symlink resolution is not *completely* iterative.
60 * There is still a significant amount of tail- and mid- recursion in
61 * the algorithm. Also, note that <fs>_readlink() is not used in
62 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
63 * may return different results than <fs>_follow_link(). Many virtual
64 * filesystems (including /proc) exhibit this behavior.
65 */
66
67/* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
68 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
69 * and the name already exists in form of a symlink, try to create the new
70 * name indicated by the symlink. The old code always complained that the
71 * name already exists, due to not following the symlink even if its target
72 * is nonexistent. The new semantics affects also mknod() and link() when
73 * the name is a symlink pointing to a non-existant name.
74 *
75 * I don't know which semantics is the right one, since I have no access
76 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
77 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
78 * "old" one. Personally, I think the new semantics is much more logical.
79 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
80 * file does succeed in both HP-UX and SunOs, but not in Solaris
81 * and in the old Linux semantics.
82 */
83
84/* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
85 * semantics. See the comments in "open_namei" and "do_link" below.
86 *
87 * [10-Sep-98 Alan Modra] Another symlink change.
88 */
89
90/* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
91 * inside the path - always follow.
92 * in the last component in creation/removal/renaming - never follow.
93 * if LOOKUP_FOLLOW passed - follow.
94 * if the pathname has trailing slashes - follow.
95 * otherwise - don't follow.
96 * (applied in that order).
97 *
98 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
99 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
100 * During the 2.4 we need to fix the userland stuff depending on it -
101 * hopefully we will be able to get rid of that wart in 2.5. So far only
102 * XEmacs seems to be relying on it...
103 */
104/*
105 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
Arjan van de Vena11f3a02006-03-23 03:00:33 -0800106 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 * any extra contention...
108 */
109
110/* In order to reduce some races, while at the same time doing additional
111 * checking and hopefully speeding things up, we copy filenames to the
112 * kernel data space before using them..
113 *
114 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
115 * PATH_MAX includes the nul terminator --RR.
116 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800117static int do_getname(const char __user *filename, char *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
119 int retval;
120 unsigned long len = PATH_MAX;
121
122 if (!segment_eq(get_fs(), KERNEL_DS)) {
123 if ((unsigned long) filename >= TASK_SIZE)
124 return -EFAULT;
125 if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
126 len = TASK_SIZE - (unsigned long) filename;
127 }
128
129 retval = strncpy_from_user(page, filename, len);
130 if (retval > 0) {
131 if (retval < len)
132 return 0;
133 return -ENAMETOOLONG;
134 } else if (!retval)
135 retval = -ENOENT;
136 return retval;
137}
138
139char * getname(const char __user * filename)
140{
141 char *tmp, *result;
142
143 result = ERR_PTR(-ENOMEM);
144 tmp = __getname();
145 if (tmp) {
146 int retval = do_getname(filename, tmp);
147
148 result = tmp;
149 if (retval < 0) {
150 __putname(tmp);
151 result = ERR_PTR(retval);
152 }
153 }
154 audit_getname(result);
155 return result;
156}
157
158#ifdef CONFIG_AUDITSYSCALL
159void putname(const char *name)
160{
Al Viro5ac3a9c2006-07-16 06:38:45 -0400161 if (unlikely(!audit_dummy_context()))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 audit_putname(name);
163 else
164 __putname(name);
165}
166EXPORT_SYMBOL(putname);
167#endif
168
169
170/**
171 * generic_permission - check for access rights on a Posix-like filesystem
172 * @inode: inode to check access rights for
173 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
174 * @check_acl: optional callback to check for Posix ACLs
175 *
176 * Used to check for read/write/execute permissions on a file.
177 * We use "fsuid" for this, letting us set arbitrary permissions
178 * for filesystem access without changing the "normal" uids which
179 * are used for other things..
180 */
181int generic_permission(struct inode *inode, int mask,
182 int (*check_acl)(struct inode *inode, int mask))
183{
184 umode_t mode = inode->i_mode;
185
186 if (current->fsuid == inode->i_uid)
187 mode >>= 6;
188 else {
189 if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
190 int error = check_acl(inode, mask);
191 if (error == -EACCES)
192 goto check_capabilities;
193 else if (error != -EAGAIN)
194 return error;
195 }
196
197 if (in_group_p(inode->i_gid))
198 mode >>= 3;
199 }
200
201 /*
202 * If the DACs are ok we don't need any capability check.
203 */
204 if (((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask))
205 return 0;
206
207 check_capabilities:
208 /*
209 * Read/write DACs are always overridable.
210 * Executable DACs are overridable if at least one exec bit is set.
211 */
212 if (!(mask & MAY_EXEC) ||
213 (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
214 if (capable(CAP_DAC_OVERRIDE))
215 return 0;
216
217 /*
218 * Searching includes executable on directories, else just read.
219 */
220 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
221 if (capable(CAP_DAC_READ_SEARCH))
222 return 0;
223
224 return -EACCES;
225}
226
227int permission(struct inode *inode, int mask, struct nameidata *nd)
228{
Trond Myklebusta343bb72006-08-22 20:06:03 -0400229 umode_t mode = inode->i_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 int retval, submask;
231
232 if (mask & MAY_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 /*
235 * Nobody gets write access to a read-only fs.
236 */
237 if (IS_RDONLY(inode) &&
238 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
239 return -EROFS;
240
241 /*
242 * Nobody gets write access to an immutable file.
243 */
244 if (IS_IMMUTABLE(inode))
245 return -EACCES;
246 }
247
248
Trond Myklebusta343bb72006-08-22 20:06:03 -0400249 /*
250 * MAY_EXEC on regular files requires special handling: We override
Stas Sergeev317a40a2006-12-06 20:35:25 -0800251 * filesystem execute permissions if the mode bits aren't set or
252 * the fs is mounted with the "noexec" flag.
Trond Myklebusta343bb72006-08-22 20:06:03 -0400253 */
Stas Sergeev317a40a2006-12-06 20:35:25 -0800254 if ((mask & MAY_EXEC) && S_ISREG(mode) && (!(mode & S_IXUGO) ||
255 (nd && nd->mnt && (nd->mnt->mnt_flags & MNT_NOEXEC))))
Trond Myklebusta343bb72006-08-22 20:06:03 -0400256 return -EACCES;
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 /* Ordinary permission routines do not understand MAY_APPEND. */
259 submask = mask & ~MAY_APPEND;
260 if (inode->i_op && inode->i_op->permission)
261 retval = inode->i_op->permission(inode, submask, nd);
262 else
263 retval = generic_permission(inode, submask, NULL);
264 if (retval)
265 return retval;
266
267 return security_inode_permission(inode, mask, nd);
268}
269
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800270/**
271 * vfs_permission - check for access rights to a given path
272 * @nd: lookup result that describes the path
273 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
274 *
275 * Used to check for read/write/execute permissions on a path.
276 * We use "fsuid" for this, letting us set arbitrary permissions
277 * for filesystem access without changing the "normal" uids which
278 * are used for other things.
279 */
280int vfs_permission(struct nameidata *nd, int mask)
281{
282 return permission(nd->dentry->d_inode, mask, nd);
283}
284
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800285/**
286 * file_permission - check for additional access rights to a given file
287 * @file: file to check access rights for
288 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
289 *
290 * Used to check for read/write/execute permissions on an already opened
291 * file.
292 *
293 * Note:
294 * Do not use this function in new code. All access checks should
295 * be done using vfs_permission().
296 */
297int file_permission(struct file *file, int mask)
298{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800299 return permission(file->f_path.dentry->d_inode, mask, NULL);
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800300}
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302/*
303 * get_write_access() gets write permission for a file.
304 * put_write_access() releases this write permission.
305 * This is used for regular files.
306 * We cannot support write (and maybe mmap read-write shared) accesses and
307 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
308 * can have the following values:
309 * 0: no writers, no VM_DENYWRITE mappings
310 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
311 * > 0: (i_writecount) users are writing to the file.
312 *
313 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
314 * except for the cases where we don't hold i_writecount yet. Then we need to
315 * use {get,deny}_write_access() - these functions check the sign and refuse
316 * to do the change if sign is wrong. Exclusion between them is provided by
317 * the inode->i_lock spinlock.
318 */
319
320int get_write_access(struct inode * inode)
321{
322 spin_lock(&inode->i_lock);
323 if (atomic_read(&inode->i_writecount) < 0) {
324 spin_unlock(&inode->i_lock);
325 return -ETXTBSY;
326 }
327 atomic_inc(&inode->i_writecount);
328 spin_unlock(&inode->i_lock);
329
330 return 0;
331}
332
333int deny_write_access(struct file * file)
334{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800335 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 spin_lock(&inode->i_lock);
338 if (atomic_read(&inode->i_writecount) > 0) {
339 spin_unlock(&inode->i_lock);
340 return -ETXTBSY;
341 }
342 atomic_dec(&inode->i_writecount);
343 spin_unlock(&inode->i_lock);
344
345 return 0;
346}
347
348void path_release(struct nameidata *nd)
349{
350 dput(nd->dentry);
351 mntput(nd->mnt);
352}
353
354/*
355 * umount() mustn't call path_release()/mntput() as that would clear
356 * mnt_expiry_mark
357 */
358void path_release_on_umount(struct nameidata *nd)
359{
360 dput(nd->dentry);
Miklos Szeredi751c4042005-07-07 17:57:30 -0700361 mntput_no_expire(nd->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}
363
Trond Myklebust834f2a42005-10-18 14:20:16 -0700364/**
365 * release_open_intent - free up open intent resources
366 * @nd: pointer to nameidata
367 */
368void release_open_intent(struct nameidata *nd)
369{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800370 if (nd->intent.open.file->f_path.dentry == NULL)
Trond Myklebust834f2a42005-10-18 14:20:16 -0700371 put_filp(nd->intent.open.file);
372 else
373 fput(nd->intent.open.file);
374}
375
Ian Kentbcdc5e02006-09-27 01:50:44 -0700376static inline struct dentry *
377do_revalidate(struct dentry *dentry, struct nameidata *nd)
378{
379 int status = dentry->d_op->d_revalidate(dentry, nd);
380 if (unlikely(status <= 0)) {
381 /*
382 * The dentry failed validation.
383 * If d_revalidate returned 0 attempt to invalidate
384 * the dentry otherwise d_revalidate is asking us
385 * to return a fail status.
386 */
387 if (!status) {
388 if (!d_invalidate(dentry)) {
389 dput(dentry);
390 dentry = NULL;
391 }
392 } else {
393 dput(dentry);
394 dentry = ERR_PTR(status);
395 }
396 }
397 return dentry;
398}
399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400/*
401 * Internal lookup() using the new generic dcache.
402 * SMP-safe
403 */
404static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
405{
406 struct dentry * dentry = __d_lookup(parent, name);
407
408 /* lockess __d_lookup may fail due to concurrent d_move()
409 * in some unrelated directory, so try with d_lookup
410 */
411 if (!dentry)
412 dentry = d_lookup(parent, name);
413
Ian Kentbcdc5e02006-09-27 01:50:44 -0700414 if (dentry && dentry->d_op && dentry->d_op->d_revalidate)
415 dentry = do_revalidate(dentry, nd);
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 return dentry;
418}
419
420/*
421 * Short-cut version of permission(), for calling by
422 * path_walk(), when dcache lock is held. Combines parts
423 * of permission() and generic_permission(), and tests ONLY for
424 * MAY_EXEC permission.
425 *
426 * If appropriate, check DAC only. If not appropriate, or
427 * short-cut DAC fails, then call permission() to do more
428 * complete permission check.
429 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800430static int exec_permission_lite(struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 struct nameidata *nd)
432{
433 umode_t mode = inode->i_mode;
434
435 if (inode->i_op && inode->i_op->permission)
436 return -EAGAIN;
437
438 if (current->fsuid == inode->i_uid)
439 mode >>= 6;
440 else if (in_group_p(inode->i_gid))
441 mode >>= 3;
442
443 if (mode & MAY_EXEC)
444 goto ok;
445
446 if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE))
447 goto ok;
448
449 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_OVERRIDE))
450 goto ok;
451
452 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH))
453 goto ok;
454
455 return -EACCES;
456ok:
457 return security_inode_permission(inode, MAY_EXEC, nd);
458}
459
460/*
461 * This is called when everything else fails, and we actually have
462 * to go to the low-level filesystem to find out what we should do..
463 *
464 * We get the directory semaphore, and after getting that we also
465 * make sure that nobody added the entry to the dcache in the meantime..
466 * SMP-safe
467 */
468static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
469{
470 struct dentry * result;
471 struct inode *dir = parent->d_inode;
472
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800473 mutex_lock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 /*
475 * First re-do the cached lookup just in case it was created
476 * while we waited for the directory semaphore..
477 *
478 * FIXME! This could use version numbering or similar to
479 * avoid unnecessary cache lookups.
480 *
481 * The "dcache_lock" is purely to protect the RCU list walker
482 * from concurrent renames at this point (we mustn't get false
483 * negatives from the RCU list walk here, unlike the optimistic
484 * fast walk).
485 *
486 * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
487 */
488 result = d_lookup(parent, name);
489 if (!result) {
490 struct dentry * dentry = d_alloc(parent, name);
491 result = ERR_PTR(-ENOMEM);
492 if (dentry) {
493 result = dir->i_op->lookup(dir, dentry, nd);
494 if (result)
495 dput(dentry);
496 else
497 result = dentry;
498 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800499 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return result;
501 }
502
503 /*
504 * Uhhuh! Nasty case: the cache was re-populated while
505 * we waited on the semaphore. Need to revalidate.
506 */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800507 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if (result->d_op && result->d_op->d_revalidate) {
Ian Kentbcdc5e02006-09-27 01:50:44 -0700509 result = do_revalidate(result, nd);
510 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
513 return result;
514}
515
516static int __emul_lookup_dentry(const char *, struct nameidata *);
517
518/* SMP-safe */
Arjan van de Venf1662352006-01-14 13:21:31 -0800519static __always_inline int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520walk_init_root(const char *name, struct nameidata *nd)
521{
Andreas Mohre518ddb2006-09-29 02:01:22 -0700522 struct fs_struct *fs = current->fs;
523
524 read_lock(&fs->lock);
525 if (fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
526 nd->mnt = mntget(fs->altrootmnt);
527 nd->dentry = dget(fs->altroot);
528 read_unlock(&fs->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if (__emul_lookup_dentry(name,nd))
530 return 0;
Andreas Mohre518ddb2006-09-29 02:01:22 -0700531 read_lock(&fs->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
Andreas Mohre518ddb2006-09-29 02:01:22 -0700533 nd->mnt = mntget(fs->rootmnt);
534 nd->dentry = dget(fs->root);
535 read_unlock(&fs->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return 1;
537}
538
Arjan van de Venf1662352006-01-14 13:21:31 -0800539static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
541 int res = 0;
542 char *name;
543 if (IS_ERR(link))
544 goto fail;
545
546 if (*link == '/') {
547 path_release(nd);
548 if (!walk_init_root(link, nd))
549 /* weird __emul_prefix() stuff did it */
550 goto out;
551 }
552 res = link_path_walk(link, nd);
553out:
554 if (nd->depth || res || nd->last_type!=LAST_NORM)
555 return res;
556 /*
557 * If it is an iterative symlinks resolution in open_namei() we
558 * have to copy the last component. And all that crap because of
559 * bloody create() on broken symlinks. Furrfu...
560 */
561 name = __getname();
562 if (unlikely(!name)) {
563 path_release(nd);
564 return -ENOMEM;
565 }
566 strcpy(name, nd->last.name);
567 nd->last.name = name;
568 return 0;
569fail:
570 path_release(nd);
571 return PTR_ERR(link);
572}
573
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700574static inline void dput_path(struct path *path, struct nameidata *nd)
575{
576 dput(path->dentry);
577 if (path->mnt != nd->mnt)
578 mntput(path->mnt);
579}
580
581static inline void path_to_nameidata(struct path *path, struct nameidata *nd)
582{
583 dput(nd->dentry);
584 if (nd->mnt != path->mnt)
585 mntput(nd->mnt);
586 nd->mnt = path->mnt;
587 nd->dentry = path->dentry;
588}
589
Ian Kent051d3812006-03-27 01:14:53 -0800590static __always_inline int __do_follow_link(struct path *path, struct nameidata *nd)
591{
592 int error;
593 void *cookie;
594 struct dentry *dentry = path->dentry;
595
596 touch_atime(path->mnt, dentry);
597 nd_set_link(nd, NULL);
598
599 if (path->mnt != nd->mnt) {
600 path_to_nameidata(path, nd);
601 dget(dentry);
602 }
603 mntget(path->mnt);
604 cookie = dentry->d_inode->i_op->follow_link(dentry, nd);
605 error = PTR_ERR(cookie);
606 if (!IS_ERR(cookie)) {
607 char *s = nd_get_link(nd);
608 error = 0;
609 if (s)
610 error = __vfs_follow_link(nd, s);
611 if (dentry->d_inode->i_op->put_link)
612 dentry->d_inode->i_op->put_link(dentry, nd, cookie);
613 }
614 dput(dentry);
615 mntput(path->mnt);
616
617 return error;
618}
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620/*
621 * This limits recursive symlink follows to 8, while
622 * limiting consecutive symlinks to 40.
623 *
624 * Without that kind of total limit, nasty chains of consecutive
625 * symlinks can cause almost arbitrarily long lookups.
626 */
Al Viro90ebe562005-06-06 13:35:58 -0700627static inline int do_follow_link(struct path *path, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
629 int err = -ELOOP;
630 if (current->link_count >= MAX_NESTED_LINKS)
631 goto loop;
632 if (current->total_link_count >= 40)
633 goto loop;
634 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
635 cond_resched();
Al Viro90ebe562005-06-06 13:35:58 -0700636 err = security_inode_follow_link(path->dentry, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 if (err)
638 goto loop;
639 current->link_count++;
640 current->total_link_count++;
641 nd->depth++;
Al Virocd4e91d2005-06-06 13:36:03 -0700642 err = __do_follow_link(path, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 current->link_count--;
644 nd->depth--;
645 return err;
646loop:
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700647 dput_path(path, nd);
Al Viro839d9f92005-06-06 13:36:02 -0700648 path_release(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return err;
650}
651
652int follow_up(struct vfsmount **mnt, struct dentry **dentry)
653{
654 struct vfsmount *parent;
655 struct dentry *mountpoint;
656 spin_lock(&vfsmount_lock);
657 parent=(*mnt)->mnt_parent;
658 if (parent == *mnt) {
659 spin_unlock(&vfsmount_lock);
660 return 0;
661 }
662 mntget(parent);
663 mountpoint=dget((*mnt)->mnt_mountpoint);
664 spin_unlock(&vfsmount_lock);
665 dput(*dentry);
666 *dentry = mountpoint;
667 mntput(*mnt);
668 *mnt = parent;
669 return 1;
670}
671
672/* no need for dcache_lock, as serialization is taken care in
673 * namespace.c
674 */
Al Viro463ffb22005-06-06 13:36:05 -0700675static int __follow_mount(struct path *path)
676{
677 int res = 0;
678 while (d_mountpoint(path->dentry)) {
679 struct vfsmount *mounted = lookup_mnt(path->mnt, path->dentry);
680 if (!mounted)
681 break;
682 dput(path->dentry);
683 if (res)
684 mntput(path->mnt);
685 path->mnt = mounted;
686 path->dentry = dget(mounted->mnt_root);
687 res = 1;
688 }
689 return res;
690}
691
Al Viro58c465e2005-06-06 13:36:13 -0700692static void follow_mount(struct vfsmount **mnt, struct dentry **dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 while (d_mountpoint(*dentry)) {
695 struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
696 if (!mounted)
697 break;
Al Viro58c465e2005-06-06 13:36:13 -0700698 dput(*dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 mntput(*mnt);
700 *mnt = mounted;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 *dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
705/* no need for dcache_lock, as serialization is taken care in
706 * namespace.c
707 */
Al Viroe13b2102005-06-06 13:36:06 -0700708int follow_down(struct vfsmount **mnt, struct dentry **dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
710 struct vfsmount *mounted;
711
712 mounted = lookup_mnt(*mnt, *dentry);
713 if (mounted) {
Al Viroe13b2102005-06-06 13:36:06 -0700714 dput(*dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 mntput(*mnt);
716 *mnt = mounted;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 *dentry = dget(mounted->mnt_root);
718 return 1;
719 }
720 return 0;
721}
722
Arjan van de Venf1662352006-01-14 13:21:31 -0800723static __always_inline void follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
Andreas Mohre518ddb2006-09-29 02:01:22 -0700725 struct fs_struct *fs = current->fs;
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 while(1) {
728 struct vfsmount *parent;
Al Viro58c465e2005-06-06 13:36:13 -0700729 struct dentry *old = nd->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Andreas Mohre518ddb2006-09-29 02:01:22 -0700731 read_lock(&fs->lock);
732 if (nd->dentry == fs->root &&
733 nd->mnt == fs->rootmnt) {
734 read_unlock(&fs->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 break;
736 }
Andreas Mohre518ddb2006-09-29 02:01:22 -0700737 read_unlock(&fs->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 spin_lock(&dcache_lock);
Al Viro58c465e2005-06-06 13:36:13 -0700739 if (nd->dentry != nd->mnt->mnt_root) {
740 nd->dentry = dget(nd->dentry->d_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 spin_unlock(&dcache_lock);
742 dput(old);
743 break;
744 }
745 spin_unlock(&dcache_lock);
746 spin_lock(&vfsmount_lock);
Al Viro58c465e2005-06-06 13:36:13 -0700747 parent = nd->mnt->mnt_parent;
748 if (parent == nd->mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 spin_unlock(&vfsmount_lock);
750 break;
751 }
752 mntget(parent);
Al Viro58c465e2005-06-06 13:36:13 -0700753 nd->dentry = dget(nd->mnt->mnt_mountpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 spin_unlock(&vfsmount_lock);
755 dput(old);
Al Viro58c465e2005-06-06 13:36:13 -0700756 mntput(nd->mnt);
757 nd->mnt = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
Al Viro58c465e2005-06-06 13:36:13 -0700759 follow_mount(&nd->mnt, &nd->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762/*
763 * It's more convoluted than I'd like it to be, but... it's still fairly
764 * small and for now I'd prefer to have fast path as straight as possible.
765 * It _is_ time-critical.
766 */
767static int do_lookup(struct nameidata *nd, struct qstr *name,
768 struct path *path)
769{
770 struct vfsmount *mnt = nd->mnt;
771 struct dentry *dentry = __d_lookup(nd->dentry, name);
772
773 if (!dentry)
774 goto need_lookup;
775 if (dentry->d_op && dentry->d_op->d_revalidate)
776 goto need_revalidate;
777done:
778 path->mnt = mnt;
779 path->dentry = dentry;
Al Viro634ee702005-06-06 13:36:13 -0700780 __follow_mount(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return 0;
782
783need_lookup:
784 dentry = real_lookup(nd->dentry, name, nd);
785 if (IS_ERR(dentry))
786 goto fail;
787 goto done;
788
789need_revalidate:
Ian Kentbcdc5e02006-09-27 01:50:44 -0700790 dentry = do_revalidate(dentry, nd);
791 if (!dentry)
792 goto need_lookup;
793 if (IS_ERR(dentry))
794 goto fail;
795 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797fail:
798 return PTR_ERR(dentry);
799}
800
801/*
802 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +0100803 * This is the basic name resolution function, turning a pathname into
804 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 *
Prasanna Medaea3834d2005-04-29 16:00:17 +0100806 * Returns 0 and nd will have valid dentry and mnt on success.
807 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 */
809static fastcall int __link_path_walk(const char * name, struct nameidata *nd)
810{
811 struct path next;
812 struct inode *inode;
813 int err;
814 unsigned int lookup_flags = nd->flags;
815
816 while (*name=='/')
817 name++;
818 if (!*name)
819 goto return_reval;
820
821 inode = nd->dentry->d_inode;
822 if (nd->depth)
Trond Myklebustf55eab82006-02-04 23:28:01 -0800823 lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 /* At this point we know we have a real path component. */
826 for(;;) {
827 unsigned long hash;
828 struct qstr this;
829 unsigned int c;
830
Trond Myklebustcdce5d62005-10-18 14:20:18 -0700831 nd->flags |= LOOKUP_CONTINUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 err = exec_permission_lite(inode, nd);
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800833 if (err == -EAGAIN)
834 err = vfs_permission(nd, MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if (err)
836 break;
837
838 this.name = name;
839 c = *(const unsigned char *)name;
840
841 hash = init_name_hash();
842 do {
843 name++;
844 hash = partial_name_hash(c, hash);
845 c = *(const unsigned char *)name;
846 } while (c && (c != '/'));
847 this.len = name - (const char *) this.name;
848 this.hash = end_name_hash(hash);
849
850 /* remove trailing slashes? */
851 if (!c)
852 goto last_component;
853 while (*++name == '/');
854 if (!*name)
855 goto last_with_slashes;
856
857 /*
858 * "." and ".." are special - ".." especially so because it has
859 * to be able to know about the current root directory and
860 * parent relationships.
861 */
862 if (this.name[0] == '.') switch (this.len) {
863 default:
864 break;
865 case 2:
866 if (this.name[1] != '.')
867 break;
Al Viro58c465e2005-06-06 13:36:13 -0700868 follow_dotdot(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 inode = nd->dentry->d_inode;
870 /* fallthrough */
871 case 1:
872 continue;
873 }
874 /*
875 * See if the low-level filesystem might want
876 * to use its own hash..
877 */
878 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
879 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
880 if (err < 0)
881 break;
882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 /* This does the actual lookups.. */
884 err = do_lookup(nd, &this, &next);
885 if (err)
886 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
888 err = -ENOENT;
889 inode = next.dentry->d_inode;
890 if (!inode)
891 goto out_dput;
892 err = -ENOTDIR;
893 if (!inode->i_op)
894 goto out_dput;
895
896 if (inode->i_op->follow_link) {
Al Viro90ebe562005-06-06 13:35:58 -0700897 err = do_follow_link(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 if (err)
899 goto return_err;
900 err = -ENOENT;
901 inode = nd->dentry->d_inode;
902 if (!inode)
903 break;
904 err = -ENOTDIR;
905 if (!inode->i_op)
906 break;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700907 } else
908 path_to_nameidata(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 err = -ENOTDIR;
910 if (!inode->i_op->lookup)
911 break;
912 continue;
913 /* here ends the main loop */
914
915last_with_slashes:
916 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
917last_component:
Trond Myklebustf55eab82006-02-04 23:28:01 -0800918 /* Clear LOOKUP_CONTINUE iff it was previously unset */
919 nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 if (lookup_flags & LOOKUP_PARENT)
921 goto lookup_parent;
922 if (this.name[0] == '.') switch (this.len) {
923 default:
924 break;
925 case 2:
926 if (this.name[1] != '.')
927 break;
Al Viro58c465e2005-06-06 13:36:13 -0700928 follow_dotdot(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 inode = nd->dentry->d_inode;
930 /* fallthrough */
931 case 1:
932 goto return_reval;
933 }
934 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
935 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
936 if (err < 0)
937 break;
938 }
939 err = do_lookup(nd, &this, &next);
940 if (err)
941 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 inode = next.dentry->d_inode;
943 if ((lookup_flags & LOOKUP_FOLLOW)
944 && inode && inode->i_op && inode->i_op->follow_link) {
Al Viro90ebe562005-06-06 13:35:58 -0700945 err = do_follow_link(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 if (err)
947 goto return_err;
948 inode = nd->dentry->d_inode;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700949 } else
950 path_to_nameidata(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 err = -ENOENT;
952 if (!inode)
953 break;
954 if (lookup_flags & LOOKUP_DIRECTORY) {
955 err = -ENOTDIR;
956 if (!inode->i_op || !inode->i_op->lookup)
957 break;
958 }
959 goto return_base;
960lookup_parent:
961 nd->last = this;
962 nd->last_type = LAST_NORM;
963 if (this.name[0] != '.')
964 goto return_base;
965 if (this.len == 1)
966 nd->last_type = LAST_DOT;
967 else if (this.len == 2 && this.name[1] == '.')
968 nd->last_type = LAST_DOTDOT;
969 else
970 goto return_base;
971return_reval:
972 /*
973 * We bypassed the ordinary revalidation routines.
974 * We may need to check the cached dentry for staleness.
975 */
976 if (nd->dentry && nd->dentry->d_sb &&
977 (nd->dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
978 err = -ESTALE;
979 /* Note: we do not d_invalidate() */
980 if (!nd->dentry->d_op->d_revalidate(nd->dentry, nd))
981 break;
982 }
983return_base:
984 return 0;
985out_dput:
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700986 dput_path(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 break;
988 }
989 path_release(nd);
990return_err:
991 return err;
992}
993
994/*
995 * Wrapper to retry pathname resolution whenever the underlying
996 * file system returns an ESTALE.
997 *
998 * Retry the whole path once, forcing real lookup requests
999 * instead of relying on the dcache.
1000 */
1001int fastcall link_path_walk(const char *name, struct nameidata *nd)
1002{
1003 struct nameidata save = *nd;
1004 int result;
1005
1006 /* make sure the stuff we saved doesn't go away */
1007 dget(save.dentry);
1008 mntget(save.mnt);
1009
1010 result = __link_path_walk(name, nd);
1011 if (result == -ESTALE) {
1012 *nd = save;
1013 dget(nd->dentry);
1014 mntget(nd->mnt);
1015 nd->flags |= LOOKUP_REVAL;
1016 result = __link_path_walk(name, nd);
1017 }
1018
1019 dput(save.dentry);
1020 mntput(save.mnt);
1021
1022 return result;
1023}
1024
1025int fastcall path_walk(const char * name, struct nameidata *nd)
1026{
1027 current->total_link_count = 0;
1028 return link_path_walk(name, nd);
1029}
1030
Prasanna Medaea3834d2005-04-29 16:00:17 +01001031/*
1032 * SMP-safe: Returns 1 and nd will have valid dentry and mnt, if
1033 * everything is done. Returns 0 and drops input nd, if lookup failed;
1034 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035static int __emul_lookup_dentry(const char *name, struct nameidata *nd)
1036{
1037 if (path_walk(name, nd))
1038 return 0; /* something went wrong... */
1039
1040 if (!nd->dentry->d_inode || S_ISDIR(nd->dentry->d_inode->i_mode)) {
1041 struct dentry *old_dentry = nd->dentry;
1042 struct vfsmount *old_mnt = nd->mnt;
1043 struct qstr last = nd->last;
1044 int last_type = nd->last_type;
Andreas Mohre518ddb2006-09-29 02:01:22 -07001045 struct fs_struct *fs = current->fs;
1046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 /*
Andreas Mohre518ddb2006-09-29 02:01:22 -07001048 * NAME was not found in alternate root or it's a directory.
1049 * Try to find it in the normal root:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 */
1051 nd->last_type = LAST_ROOT;
Andreas Mohre518ddb2006-09-29 02:01:22 -07001052 read_lock(&fs->lock);
1053 nd->mnt = mntget(fs->rootmnt);
1054 nd->dentry = dget(fs->root);
1055 read_unlock(&fs->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 if (path_walk(name, nd) == 0) {
1057 if (nd->dentry->d_inode) {
1058 dput(old_dentry);
1059 mntput(old_mnt);
1060 return 1;
1061 }
1062 path_release(nd);
1063 }
1064 nd->dentry = old_dentry;
1065 nd->mnt = old_mnt;
1066 nd->last = last;
1067 nd->last_type = last_type;
1068 }
1069 return 1;
1070}
1071
1072void set_fs_altroot(void)
1073{
1074 char *emul = __emul_prefix();
1075 struct nameidata nd;
1076 struct vfsmount *mnt = NULL, *oldmnt;
1077 struct dentry *dentry = NULL, *olddentry;
1078 int err;
Andreas Mohre518ddb2006-09-29 02:01:22 -07001079 struct fs_struct *fs = current->fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081 if (!emul)
1082 goto set_it;
1083 err = path_lookup(emul, LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_NOALT, &nd);
1084 if (!err) {
1085 mnt = nd.mnt;
1086 dentry = nd.dentry;
1087 }
1088set_it:
Andreas Mohre518ddb2006-09-29 02:01:22 -07001089 write_lock(&fs->lock);
1090 oldmnt = fs->altrootmnt;
1091 olddentry = fs->altroot;
1092 fs->altrootmnt = mnt;
1093 fs->altroot = dentry;
1094 write_unlock(&fs->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 if (olddentry) {
1096 dput(olddentry);
1097 mntput(oldmnt);
1098 }
1099}
1100
Prasanna Medaea3834d2005-04-29 16:00:17 +01001101/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001102static int fastcall do_path_lookup(int dfd, const char *name,
1103 unsigned int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
Prasanna Medaea3834d2005-04-29 16:00:17 +01001105 int retval = 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001106 int fput_needed;
1107 struct file *file;
Andreas Mohre518ddb2006-09-29 02:01:22 -07001108 struct fs_struct *fs = current->fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
1110 nd->last_type = LAST_ROOT; /* if there are only slashes... */
1111 nd->flags = flags;
1112 nd->depth = 0;
1113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 if (*name=='/') {
Andreas Mohre518ddb2006-09-29 02:01:22 -07001115 read_lock(&fs->lock);
1116 if (fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
1117 nd->mnt = mntget(fs->altrootmnt);
1118 nd->dentry = dget(fs->altroot);
1119 read_unlock(&fs->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 if (__emul_lookup_dentry(name,nd))
Prasanna Medaea3834d2005-04-29 16:00:17 +01001121 goto out; /* found in altroot */
Andreas Mohre518ddb2006-09-29 02:01:22 -07001122 read_lock(&fs->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 }
Andreas Mohre518ddb2006-09-29 02:01:22 -07001124 nd->mnt = mntget(fs->rootmnt);
1125 nd->dentry = dget(fs->root);
1126 read_unlock(&fs->lock);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001127 } else if (dfd == AT_FDCWD) {
Andreas Mohre518ddb2006-09-29 02:01:22 -07001128 read_lock(&fs->lock);
1129 nd->mnt = mntget(fs->pwdmnt);
1130 nd->dentry = dget(fs->pwd);
1131 read_unlock(&fs->lock);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001132 } else {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001133 struct dentry *dentry;
1134
1135 file = fget_light(dfd, &fput_needed);
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001136 retval = -EBADF;
1137 if (!file)
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001138 goto out_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001139
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001140 dentry = file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001141
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001142 retval = -ENOTDIR;
1143 if (!S_ISDIR(dentry->d_inode->i_mode))
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001144 goto fput_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001145
1146 retval = file_permission(file, MAY_EXEC);
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001147 if (retval)
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001148 goto fput_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001149
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001150 nd->mnt = mntget(file->f_path.mnt);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001151 nd->dentry = dget(dentry);
1152
1153 fput_light(file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 }
Josef 'Jeff' Sipek2dfdd262007-05-09 02:33:41 -07001155
1156 retval = path_walk(name, nd);
Prasanna Medaea3834d2005-04-29 16:00:17 +01001157out:
Josef 'Jeff' Sipek62ce39c2007-05-09 02:33:41 -07001158 if (unlikely(!retval && !audit_dummy_context() && nd->dentry &&
Suzuki3bc8414b2006-02-07 12:58:36 -08001159 nd->dentry->d_inode))
Amy Griffis9c937dc2006-06-08 23:19:31 -04001160 audit_inode(name, nd->dentry->d_inode);
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001161out_fail:
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001162 return retval;
1163
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001164fput_fail:
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001165 fput_light(file, fput_needed);
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001166 goto out_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167}
1168
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001169int fastcall path_lookup(const char *name, unsigned int flags,
1170 struct nameidata *nd)
1171{
1172 return do_path_lookup(AT_FDCWD, name, flags, nd);
1173}
1174
1175static int __path_lookup_intent_open(int dfd, const char *name,
1176 unsigned int lookup_flags, struct nameidata *nd,
1177 int open_flags, int create_mode)
Trond Myklebust834f2a42005-10-18 14:20:16 -07001178{
1179 struct file *filp = get_empty_filp();
1180 int err;
1181
1182 if (filp == NULL)
1183 return -ENFILE;
1184 nd->intent.open.file = filp;
1185 nd->intent.open.flags = open_flags;
1186 nd->intent.open.create_mode = create_mode;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001187 err = do_path_lookup(dfd, name, lookup_flags|LOOKUP_OPEN, nd);
Trond Myklebust834f2a42005-10-18 14:20:16 -07001188 if (IS_ERR(nd->intent.open.file)) {
1189 if (err == 0) {
1190 err = PTR_ERR(nd->intent.open.file);
1191 path_release(nd);
1192 }
1193 } else if (err != 0)
1194 release_open_intent(nd);
1195 return err;
1196}
1197
1198/**
1199 * path_lookup_open - lookup a file path with open intent
Martin Waitz7045f372006-02-01 03:06:57 -08001200 * @dfd: the directory to use as base, or AT_FDCWD
Trond Myklebust834f2a42005-10-18 14:20:16 -07001201 * @name: pointer to file name
1202 * @lookup_flags: lookup intent flags
1203 * @nd: pointer to nameidata
1204 * @open_flags: open intent flags
1205 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001206int path_lookup_open(int dfd, const char *name, unsigned int lookup_flags,
Trond Myklebust834f2a42005-10-18 14:20:16 -07001207 struct nameidata *nd, int open_flags)
1208{
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001209 return __path_lookup_intent_open(dfd, name, lookup_flags, nd,
Trond Myklebust834f2a42005-10-18 14:20:16 -07001210 open_flags, 0);
1211}
1212
1213/**
1214 * path_lookup_create - lookup a file path with open + create intent
Martin Waitz7045f372006-02-01 03:06:57 -08001215 * @dfd: the directory to use as base, or AT_FDCWD
Trond Myklebust834f2a42005-10-18 14:20:16 -07001216 * @name: pointer to file name
1217 * @lookup_flags: lookup intent flags
1218 * @nd: pointer to nameidata
1219 * @open_flags: open intent flags
1220 * @create_mode: create intent flags
1221 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001222static int path_lookup_create(int dfd, const char *name,
1223 unsigned int lookup_flags, struct nameidata *nd,
1224 int open_flags, int create_mode)
Trond Myklebust834f2a42005-10-18 14:20:16 -07001225{
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001226 return __path_lookup_intent_open(dfd, name, lookup_flags|LOOKUP_CREATE,
1227 nd, open_flags, create_mode);
Trond Myklebust834f2a42005-10-18 14:20:16 -07001228}
1229
1230int __user_path_lookup_open(const char __user *name, unsigned int lookup_flags,
1231 struct nameidata *nd, int open_flags)
1232{
1233 char *tmp = getname(name);
1234 int err = PTR_ERR(tmp);
1235
1236 if (!IS_ERR(tmp)) {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001237 err = __path_lookup_intent_open(AT_FDCWD, tmp, lookup_flags, nd, open_flags, 0);
Trond Myklebust834f2a42005-10-18 14:20:16 -07001238 putname(tmp);
1239 }
1240 return err;
1241}
1242
James Morris057f6c02007-04-26 00:12:05 -07001243static inline struct dentry *__lookup_hash_kern(struct qstr *name, struct dentry *base, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
James Morris057f6c02007-04-26 00:12:05 -07001245 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 struct inode *inode;
1247 int err;
1248
1249 inode = base->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
1251 /*
1252 * See if the low-level filesystem might want
1253 * to use its own hash..
1254 */
1255 if (base->d_op && base->d_op->d_hash) {
1256 err = base->d_op->d_hash(base, name);
1257 dentry = ERR_PTR(err);
1258 if (err < 0)
1259 goto out;
1260 }
1261
1262 dentry = cached_lookup(base, name, nd);
1263 if (!dentry) {
1264 struct dentry *new = d_alloc(base, name);
1265 dentry = ERR_PTR(-ENOMEM);
1266 if (!new)
1267 goto out;
1268 dentry = inode->i_op->lookup(inode, new, nd);
1269 if (!dentry)
1270 dentry = new;
1271 else
1272 dput(new);
1273 }
1274out:
1275 return dentry;
1276}
1277
James Morris057f6c02007-04-26 00:12:05 -07001278/*
1279 * Restricted form of lookup. Doesn't follow links, single-component only,
1280 * needs parent already locked. Doesn't follow mounts.
1281 * SMP-safe.
1282 */
1283static inline struct dentry * __lookup_hash(struct qstr *name, struct dentry *base, struct nameidata *nd)
1284{
1285 struct dentry *dentry;
1286 struct inode *inode;
1287 int err;
1288
1289 inode = base->d_inode;
1290
1291 err = permission(inode, MAY_EXEC, nd);
1292 dentry = ERR_PTR(err);
1293 if (err)
1294 goto out;
1295
1296 dentry = __lookup_hash_kern(name, base, nd);
1297out:
1298 return dentry;
1299}
1300
Adrian Bunka244e162006-03-31 02:32:11 -08001301static struct dentry *lookup_hash(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302{
Christoph Hellwig49705b72005-11-08 21:35:06 -08001303 return __lookup_hash(&nd->last, nd->dentry, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304}
1305
1306/* SMP-safe */
James Morris057f6c02007-04-26 00:12:05 -07001307static inline int __lookup_one_len(const char *name, struct qstr *this, struct dentry *base, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308{
1309 unsigned long hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 unsigned int c;
1311
James Morris057f6c02007-04-26 00:12:05 -07001312 this->name = name;
1313 this->len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 if (!len)
James Morris057f6c02007-04-26 00:12:05 -07001315 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 hash = init_name_hash();
1318 while (len--) {
1319 c = *(const unsigned char *)name++;
1320 if (c == '/' || c == '\0')
James Morris057f6c02007-04-26 00:12:05 -07001321 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 hash = partial_name_hash(c, hash);
1323 }
James Morris057f6c02007-04-26 00:12:05 -07001324 this->hash = end_name_hash(hash);
1325 return 0;
1326}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
James Morris057f6c02007-04-26 00:12:05 -07001328struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1329{
1330 int err;
1331 struct qstr this;
1332
1333 err = __lookup_one_len(name, &this, base, len);
1334 if (err)
1335 return ERR_PTR(err);
Christoph Hellwig49705b72005-11-08 21:35:06 -08001336 return __lookup_hash(&this, base, NULL);
James Morris057f6c02007-04-26 00:12:05 -07001337}
1338
1339struct dentry *lookup_one_len_kern(const char *name, struct dentry *base, int len)
1340{
1341 int err;
1342 struct qstr this;
1343
1344 err = __lookup_one_len(name, &this, base, len);
1345 if (err)
1346 return ERR_PTR(err);
1347 return __lookup_hash_kern(&this, base, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348}
1349
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001350int fastcall __user_walk_fd(int dfd, const char __user *name, unsigned flags,
1351 struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
1353 char *tmp = getname(name);
1354 int err = PTR_ERR(tmp);
1355
1356 if (!IS_ERR(tmp)) {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001357 err = do_path_lookup(dfd, tmp, flags, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 putname(tmp);
1359 }
1360 return err;
1361}
1362
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001363int fastcall __user_walk(const char __user *name, unsigned flags, struct nameidata *nd)
1364{
1365 return __user_walk_fd(AT_FDCWD, name, flags, nd);
1366}
1367
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368/*
1369 * It's inline, so penalty for filesystems that don't use sticky bit is
1370 * minimal.
1371 */
1372static inline int check_sticky(struct inode *dir, struct inode *inode)
1373{
1374 if (!(dir->i_mode & S_ISVTX))
1375 return 0;
1376 if (inode->i_uid == current->fsuid)
1377 return 0;
1378 if (dir->i_uid == current->fsuid)
1379 return 0;
1380 return !capable(CAP_FOWNER);
1381}
1382
1383/*
1384 * Check whether we can remove a link victim from directory dir, check
1385 * whether the type of victim is right.
1386 * 1. We can't do it if dir is read-only (done in permission())
1387 * 2. We should have write and exec permissions on dir
1388 * 3. We can't remove anything from append-only dir
1389 * 4. We can't do anything with immutable dir (done in permission())
1390 * 5. If the sticky bit on dir is set we should either
1391 * a. be owner of dir, or
1392 * b. be owner of victim, or
1393 * c. have CAP_FOWNER capability
1394 * 6. If the victim is append-only or immutable we can't do antyhing with
1395 * links pointing to it.
1396 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1397 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1398 * 9. We can't remove a root or mountpoint.
1399 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1400 * nfs_async_unlink().
1401 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001402static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403{
1404 int error;
1405
1406 if (!victim->d_inode)
1407 return -ENOENT;
1408
1409 BUG_ON(victim->d_parent->d_inode != dir);
Amy Griffis73d3ec52006-07-13 13:16:39 -04001410 audit_inode_child(victim->d_name.name, victim->d_inode, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412 error = permission(dir,MAY_WRITE | MAY_EXEC, NULL);
1413 if (error)
1414 return error;
1415 if (IS_APPEND(dir))
1416 return -EPERM;
1417 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1418 IS_IMMUTABLE(victim->d_inode))
1419 return -EPERM;
1420 if (isdir) {
1421 if (!S_ISDIR(victim->d_inode->i_mode))
1422 return -ENOTDIR;
1423 if (IS_ROOT(victim))
1424 return -EBUSY;
1425 } else if (S_ISDIR(victim->d_inode->i_mode))
1426 return -EISDIR;
1427 if (IS_DEADDIR(dir))
1428 return -ENOENT;
1429 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1430 return -EBUSY;
1431 return 0;
1432}
1433
1434/* Check whether we can create an object with dentry child in directory
1435 * dir.
1436 * 1. We can't do it if child already exists (open has special treatment for
1437 * this case, but since we are inlined it's OK)
1438 * 2. We can't do it if dir is read-only (done in permission())
1439 * 3. We should have write and exec permissions on dir
1440 * 4. We can't do it if dir is immutable (done in permission())
1441 */
1442static inline int may_create(struct inode *dir, struct dentry *child,
1443 struct nameidata *nd)
1444{
1445 if (child->d_inode)
1446 return -EEXIST;
1447 if (IS_DEADDIR(dir))
1448 return -ENOENT;
1449 return permission(dir,MAY_WRITE | MAY_EXEC, nd);
1450}
1451
1452/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 * O_DIRECTORY translates into forcing a directory lookup.
1454 */
1455static inline int lookup_flags(unsigned int f)
1456{
1457 unsigned long retval = LOOKUP_FOLLOW;
1458
1459 if (f & O_NOFOLLOW)
1460 retval &= ~LOOKUP_FOLLOW;
1461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 if (f & O_DIRECTORY)
1463 retval |= LOOKUP_DIRECTORY;
1464
1465 return retval;
1466}
1467
1468/*
1469 * p1 and p2 should be directories on the same fs.
1470 */
1471struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1472{
1473 struct dentry *p;
1474
1475 if (p1 == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07001476 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 return NULL;
1478 }
1479
Arjan van de Vena11f3a02006-03-23 03:00:33 -08001480 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
1482 for (p = p1; p->d_parent != p; p = p->d_parent) {
1483 if (p->d_parent == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07001484 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1485 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 return p;
1487 }
1488 }
1489
1490 for (p = p2; p->d_parent != p; p = p->d_parent) {
1491 if (p->d_parent == p1) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07001492 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1493 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 return p;
1495 }
1496 }
1497
Ingo Molnarf2eace22006-07-03 00:25:05 -07001498 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1499 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 return NULL;
1501}
1502
1503void unlock_rename(struct dentry *p1, struct dentry *p2)
1504{
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001505 mutex_unlock(&p1->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 if (p1 != p2) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001507 mutex_unlock(&p2->d_inode->i_mutex);
Arjan van de Vena11f3a02006-03-23 03:00:33 -08001508 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 }
1510}
1511
1512int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1513 struct nameidata *nd)
1514{
1515 int error = may_create(dir, dentry, nd);
1516
1517 if (error)
1518 return error;
1519
1520 if (!dir->i_op || !dir->i_op->create)
1521 return -EACCES; /* shouldn't it be ENOSYS? */
1522 mode &= S_IALLUGO;
1523 mode |= S_IFREG;
1524 error = security_inode_create(dir, dentry, mode);
1525 if (error)
1526 return error;
1527 DQUOT_INIT(dir);
1528 error = dir->i_op->create(dir, dentry, mode, nd);
Stephen Smalleya74574a2005-09-09 13:01:44 -07001529 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00001530 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 return error;
1532}
1533
1534int may_open(struct nameidata *nd, int acc_mode, int flag)
1535{
1536 struct dentry *dentry = nd->dentry;
1537 struct inode *inode = dentry->d_inode;
1538 int error;
1539
1540 if (!inode)
1541 return -ENOENT;
1542
1543 if (S_ISLNK(inode->i_mode))
1544 return -ELOOP;
1545
1546 if (S_ISDIR(inode->i_mode) && (flag & FMODE_WRITE))
1547 return -EISDIR;
1548
Christoph Hellwige4543ed2005-11-08 21:35:04 -08001549 error = vfs_permission(nd, acc_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 if (error)
1551 return error;
1552
1553 /*
1554 * FIFO's, sockets and device files are special: they don't
1555 * actually live on the filesystem itself, and as such you
1556 * can write to them even if the filesystem is read-only.
1557 */
1558 if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
1559 flag &= ~O_TRUNC;
1560 } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
1561 if (nd->mnt->mnt_flags & MNT_NODEV)
1562 return -EACCES;
1563
1564 flag &= ~O_TRUNC;
1565 } else if (IS_RDONLY(inode) && (flag & FMODE_WRITE))
1566 return -EROFS;
1567 /*
1568 * An append-only file must be opened in append mode for writing.
1569 */
1570 if (IS_APPEND(inode)) {
1571 if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
1572 return -EPERM;
1573 if (flag & O_TRUNC)
1574 return -EPERM;
1575 }
1576
1577 /* O_NOATIME can only be set by the owner or superuser */
1578 if (flag & O_NOATIME)
1579 if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
1580 return -EPERM;
1581
1582 /*
1583 * Ensure there are no outstanding leases on the file.
1584 */
1585 error = break_lease(inode, flag);
1586 if (error)
1587 return error;
1588
1589 if (flag & O_TRUNC) {
1590 error = get_write_access(inode);
1591 if (error)
1592 return error;
1593
1594 /*
1595 * Refuse to truncate files with mandatory locks held on them.
1596 */
1597 error = locks_verify_locked(inode);
1598 if (!error) {
1599 DQUOT_INIT(inode);
1600
NeilBrown4a301312006-01-08 01:02:39 -08001601 error = do_truncate(dentry, 0, ATTR_MTIME|ATTR_CTIME, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 }
1603 put_write_access(inode);
1604 if (error)
1605 return error;
1606 } else
1607 if (flag & FMODE_WRITE)
1608 DQUOT_INIT(inode);
1609
1610 return 0;
1611}
1612
Dave Hansenaab520e2006-09-30 23:29:02 -07001613static int open_namei_create(struct nameidata *nd, struct path *path,
1614 int flag, int mode)
1615{
1616 int error;
1617 struct dentry *dir = nd->dentry;
1618
1619 if (!IS_POSIXACL(dir->d_inode))
1620 mode &= ~current->fs->umask;
1621 error = vfs_create(dir->d_inode, path->dentry, mode, nd);
1622 mutex_unlock(&dir->d_inode->i_mutex);
1623 dput(nd->dentry);
1624 nd->dentry = path->dentry;
1625 if (error)
1626 return error;
1627 /* Don't check for write permission, don't truncate */
1628 return may_open(nd, 0, flag & ~O_TRUNC);
1629}
1630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631/*
1632 * open_namei()
1633 *
1634 * namei for open - this is in fact almost the whole open-routine.
1635 *
1636 * Note that the low bits of "flag" aren't the same as in the open
1637 * system call - they are 00 - no permissions needed
1638 * 01 - read permission needed
1639 * 10 - write permission needed
1640 * 11 - read/write permissions needed
1641 * which is a lot more logical, and also allows the "no perm" needed
1642 * for symlinks (where the permissions are checked later).
1643 * SMP-safe
1644 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001645int open_namei(int dfd, const char *pathname, int flag,
1646 int mode, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647{
Trond Myklebust834f2a42005-10-18 14:20:16 -07001648 int acc_mode, error;
Al Viro4e7506e2005-06-06 13:36:00 -07001649 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 struct dentry *dir;
1651 int count = 0;
1652
1653 acc_mode = ACC_MODE(flag);
1654
Trond Myklebust834f2a42005-10-18 14:20:16 -07001655 /* O_TRUNC implies we need access checks for write permissions */
1656 if (flag & O_TRUNC)
1657 acc_mode |= MAY_WRITE;
1658
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 /* Allow the LSM permission hook to distinguish append
1660 access from general write access. */
1661 if (flag & O_APPEND)
1662 acc_mode |= MAY_APPEND;
1663
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 /*
1665 * The simplest case - just a plain lookup.
1666 */
1667 if (!(flag & O_CREAT)) {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001668 error = path_lookup_open(dfd, pathname, lookup_flags(flag),
1669 nd, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 if (error)
1671 return error;
1672 goto ok;
1673 }
1674
1675 /*
1676 * Create - we need to know the parent.
1677 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001678 error = path_lookup_create(dfd,pathname,LOOKUP_PARENT,nd,flag,mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 if (error)
1680 return error;
1681
1682 /*
1683 * We have the parent and last component. First of all, check
1684 * that we are not asked to creat(2) an obvious directory - that
1685 * will not do.
1686 */
1687 error = -EISDIR;
1688 if (nd->last_type != LAST_NORM || nd->last.name[nd->last.len])
1689 goto exit;
1690
1691 dir = nd->dentry;
1692 nd->flags &= ~LOOKUP_PARENT;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001693 mutex_lock(&dir->d_inode->i_mutex);
Christoph Hellwig49705b72005-11-08 21:35:06 -08001694 path.dentry = lookup_hash(nd);
Al Virod73ffe12005-06-06 13:36:01 -07001695 path.mnt = nd->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
1697do_last:
Al Viro4e7506e2005-06-06 13:36:00 -07001698 error = PTR_ERR(path.dentry);
1699 if (IS_ERR(path.dentry)) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001700 mutex_unlock(&dir->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 goto exit;
1702 }
1703
Oleg Drokin4af4c522006-03-25 03:06:54 -08001704 if (IS_ERR(nd->intent.open.file)) {
1705 mutex_unlock(&dir->d_inode->i_mutex);
1706 error = PTR_ERR(nd->intent.open.file);
1707 goto exit_dput;
1708 }
1709
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 /* Negative dentry, just create the file */
Al Viro4e7506e2005-06-06 13:36:00 -07001711 if (!path.dentry->d_inode) {
Dave Hansenaab520e2006-09-30 23:29:02 -07001712 error = open_namei_create(nd, &path, flag, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 if (error)
1714 goto exit;
Dave Hansenaab520e2006-09-30 23:29:02 -07001715 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 }
1717
1718 /*
1719 * It already exists.
1720 */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001721 mutex_unlock(&dir->d_inode->i_mutex);
Amy Griffis3e2efce2006-07-13 13:16:02 -04001722 audit_inode_update(path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
1724 error = -EEXIST;
1725 if (flag & O_EXCL)
1726 goto exit_dput;
1727
Al Viroe13b2102005-06-06 13:36:06 -07001728 if (__follow_mount(&path)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 error = -ELOOP;
Al Viroba7a4c12005-06-06 13:36:08 -07001730 if (flag & O_NOFOLLOW)
1731 goto exit_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 }
Amy Griffis3e2efce2006-07-13 13:16:02 -04001733
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 error = -ENOENT;
Al Viro4e7506e2005-06-06 13:36:00 -07001735 if (!path.dentry->d_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 goto exit_dput;
Al Viro4e7506e2005-06-06 13:36:00 -07001737 if (path.dentry->d_inode->i_op && path.dentry->d_inode->i_op->follow_link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 goto do_link;
1739
Miklos Szeredi09dd17d2005-09-06 15:18:21 -07001740 path_to_nameidata(&path, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 error = -EISDIR;
Al Viro4e7506e2005-06-06 13:36:00 -07001742 if (path.dentry->d_inode && S_ISDIR(path.dentry->d_inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 goto exit;
1744ok:
1745 error = may_open(nd, acc_mode, flag);
1746 if (error)
1747 goto exit;
1748 return 0;
1749
1750exit_dput:
Miklos Szeredi09dd17d2005-09-06 15:18:21 -07001751 dput_path(&path, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752exit:
Trond Myklebust834f2a42005-10-18 14:20:16 -07001753 if (!IS_ERR(nd->intent.open.file))
1754 release_open_intent(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 path_release(nd);
1756 return error;
1757
1758do_link:
1759 error = -ELOOP;
1760 if (flag & O_NOFOLLOW)
1761 goto exit_dput;
1762 /*
1763 * This is subtle. Instead of calling do_follow_link() we do the
1764 * thing by hands. The reason is that this way we have zero link_count
1765 * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1766 * After that we have the parent and last component, i.e.
1767 * we are in the same situation as after the first path_walk().
1768 * Well, almost - if the last component is normal we get its copy
1769 * stored in nd->last.name and we will have to putname() it when we
1770 * are done. Procfs-like symlinks just set LAST_BIND.
1771 */
1772 nd->flags |= LOOKUP_PARENT;
Al Viro4e7506e2005-06-06 13:36:00 -07001773 error = security_inode_follow_link(path.dentry, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 if (error)
1775 goto exit_dput;
Al Virocd4e91d2005-06-06 13:36:03 -07001776 error = __do_follow_link(&path, nd);
Kirill Korotaevde459212006-07-14 00:23:49 -07001777 if (error) {
1778 /* Does someone understand code flow here? Or it is only
1779 * me so stupid? Anathema to whoever designed this non-sense
1780 * with "intent.open".
1781 */
1782 release_open_intent(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 return error;
Kirill Korotaevde459212006-07-14 00:23:49 -07001784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 nd->flags &= ~LOOKUP_PARENT;
Al Virod671d5e2005-06-06 13:36:04 -07001786 if (nd->last_type == LAST_BIND)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 goto ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 error = -EISDIR;
1789 if (nd->last_type != LAST_NORM)
1790 goto exit;
1791 if (nd->last.name[nd->last.len]) {
Linus Torvalds82984112005-10-06 21:54:21 -07001792 __putname(nd->last.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 goto exit;
1794 }
1795 error = -ELOOP;
1796 if (count++==32) {
Linus Torvalds82984112005-10-06 21:54:21 -07001797 __putname(nd->last.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 goto exit;
1799 }
1800 dir = nd->dentry;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001801 mutex_lock(&dir->d_inode->i_mutex);
Christoph Hellwig49705b72005-11-08 21:35:06 -08001802 path.dentry = lookup_hash(nd);
Al Virod671d5e2005-06-06 13:36:04 -07001803 path.mnt = nd->mnt;
Linus Torvalds82984112005-10-06 21:54:21 -07001804 __putname(nd->last.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 goto do_last;
1806}
1807
1808/**
1809 * lookup_create - lookup a dentry, creating it if it doesn't exist
1810 * @nd: nameidata info
1811 * @is_dir: directory flag
1812 *
1813 * Simple function to lookup and return a dentry and create it
1814 * if it doesn't exist. Is SMP-safe.
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07001815 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001816 * Returns with nd->dentry->d_inode->i_mutex locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 */
1818struct dentry *lookup_create(struct nameidata *nd, int is_dir)
1819{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07001820 struct dentry *dentry = ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
Ingo Molnarf2eace22006-07-03 00:25:05 -07001822 mutex_lock_nested(&nd->dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07001823 /*
1824 * Yucky last component or no last component at all?
1825 * (foo/., foo/.., /////)
1826 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 if (nd->last_type != LAST_NORM)
1828 goto fail;
1829 nd->flags &= ~LOOKUP_PARENT;
ASANO Masahiroa6349042006-08-22 20:06:02 -04001830 nd->flags |= LOOKUP_CREATE;
1831 nd->intent.open.flags = O_EXCL;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07001832
1833 /*
1834 * Do the final lookup.
1835 */
Christoph Hellwig49705b72005-11-08 21:35:06 -08001836 dentry = lookup_hash(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 if (IS_ERR(dentry))
1838 goto fail;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07001839
1840 /*
1841 * Special case - lookup gave negative, but... we had foo/bar/
1842 * From the vfs_mknod() POV we just have a negative dentry -
1843 * all is fine. Let's be bastards - you had / on the end, you've
1844 * been asking for (non-existent) directory. -ENOENT for you.
1845 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode)
1847 goto enoent;
1848 return dentry;
1849enoent:
1850 dput(dentry);
1851 dentry = ERR_PTR(-ENOENT);
1852fail:
1853 return dentry;
1854}
Christoph Hellwigf81a0bf2005-05-19 12:26:43 -07001855EXPORT_SYMBOL_GPL(lookup_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
1857int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1858{
1859 int error = may_create(dir, dentry, NULL);
1860
1861 if (error)
1862 return error;
1863
1864 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1865 return -EPERM;
1866
1867 if (!dir->i_op || !dir->i_op->mknod)
1868 return -EPERM;
1869
1870 error = security_inode_mknod(dir, dentry, mode, dev);
1871 if (error)
1872 return error;
1873
1874 DQUOT_INIT(dir);
1875 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07001876 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00001877 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 return error;
1879}
1880
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001881asmlinkage long sys_mknodat(int dfd, const char __user *filename, int mode,
1882 unsigned dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883{
1884 int error = 0;
1885 char * tmp;
1886 struct dentry * dentry;
1887 struct nameidata nd;
1888
1889 if (S_ISDIR(mode))
1890 return -EPERM;
1891 tmp = getname(filename);
1892 if (IS_ERR(tmp))
1893 return PTR_ERR(tmp);
1894
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001895 error = do_path_lookup(dfd, tmp, LOOKUP_PARENT, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 if (error)
1897 goto out;
1898 dentry = lookup_create(&nd, 0);
1899 error = PTR_ERR(dentry);
1900
1901 if (!IS_POSIXACL(nd.dentry->d_inode))
1902 mode &= ~current->fs->umask;
1903 if (!IS_ERR(dentry)) {
1904 switch (mode & S_IFMT) {
1905 case 0: case S_IFREG:
1906 error = vfs_create(nd.dentry->d_inode,dentry,mode,&nd);
1907 break;
1908 case S_IFCHR: case S_IFBLK:
1909 error = vfs_mknod(nd.dentry->d_inode,dentry,mode,
1910 new_decode_dev(dev));
1911 break;
1912 case S_IFIFO: case S_IFSOCK:
1913 error = vfs_mknod(nd.dentry->d_inode,dentry,mode,0);
1914 break;
1915 case S_IFDIR:
1916 error = -EPERM;
1917 break;
1918 default:
1919 error = -EINVAL;
1920 }
1921 dput(dentry);
1922 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001923 mutex_unlock(&nd.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 path_release(&nd);
1925out:
1926 putname(tmp);
1927
1928 return error;
1929}
1930
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001931asmlinkage long sys_mknod(const char __user *filename, int mode, unsigned dev)
1932{
1933 return sys_mknodat(AT_FDCWD, filename, mode, dev);
1934}
1935
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1937{
1938 int error = may_create(dir, dentry, NULL);
1939
1940 if (error)
1941 return error;
1942
1943 if (!dir->i_op || !dir->i_op->mkdir)
1944 return -EPERM;
1945
1946 mode &= (S_IRWXUGO|S_ISVTX);
1947 error = security_inode_mkdir(dir, dentry, mode);
1948 if (error)
1949 return error;
1950
1951 DQUOT_INIT(dir);
1952 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07001953 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00001954 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 return error;
1956}
1957
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001958asmlinkage long sys_mkdirat(int dfd, const char __user *pathname, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959{
1960 int error = 0;
1961 char * tmp;
Dave Hansen6902d922006-09-30 23:29:01 -07001962 struct dentry *dentry;
1963 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
1965 tmp = getname(pathname);
1966 error = PTR_ERR(tmp);
Dave Hansen6902d922006-09-30 23:29:01 -07001967 if (IS_ERR(tmp))
1968 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
Dave Hansen6902d922006-09-30 23:29:01 -07001970 error = do_path_lookup(dfd, tmp, LOOKUP_PARENT, &nd);
1971 if (error)
1972 goto out;
1973 dentry = lookup_create(&nd, 1);
1974 error = PTR_ERR(dentry);
1975 if (IS_ERR(dentry))
1976 goto out_unlock;
1977
1978 if (!IS_POSIXACL(nd.dentry->d_inode))
1979 mode &= ~current->fs->umask;
1980 error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
1981 dput(dentry);
1982out_unlock:
1983 mutex_unlock(&nd.dentry->d_inode->i_mutex);
1984 path_release(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985out:
Dave Hansen6902d922006-09-30 23:29:01 -07001986 putname(tmp);
1987out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 return error;
1989}
1990
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001991asmlinkage long sys_mkdir(const char __user *pathname, int mode)
1992{
1993 return sys_mkdirat(AT_FDCWD, pathname, mode);
1994}
1995
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996/*
1997 * We try to drop the dentry early: we should have
1998 * a usage count of 2 if we're the only user of this
1999 * dentry, and if that is true (possibly after pruning
2000 * the dcache), then we drop the dentry now.
2001 *
2002 * A low-level filesystem can, if it choses, legally
2003 * do a
2004 *
2005 * if (!d_unhashed(dentry))
2006 * return -EBUSY;
2007 *
2008 * if it cannot handle the case of removing a directory
2009 * that is still in use by something else..
2010 */
2011void dentry_unhash(struct dentry *dentry)
2012{
2013 dget(dentry);
Vasily Averindc168422006-12-06 20:37:07 -08002014 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 spin_lock(&dcache_lock);
2016 spin_lock(&dentry->d_lock);
2017 if (atomic_read(&dentry->d_count) == 2)
2018 __d_drop(dentry);
2019 spin_unlock(&dentry->d_lock);
2020 spin_unlock(&dcache_lock);
2021}
2022
2023int vfs_rmdir(struct inode *dir, struct dentry *dentry)
2024{
2025 int error = may_delete(dir, dentry, 1);
2026
2027 if (error)
2028 return error;
2029
2030 if (!dir->i_op || !dir->i_op->rmdir)
2031 return -EPERM;
2032
2033 DQUOT_INIT(dir);
2034
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002035 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 dentry_unhash(dentry);
2037 if (d_mountpoint(dentry))
2038 error = -EBUSY;
2039 else {
2040 error = security_inode_rmdir(dir, dentry);
2041 if (!error) {
2042 error = dir->i_op->rmdir(dir, dentry);
2043 if (!error)
2044 dentry->d_inode->i_flags |= S_DEAD;
2045 }
2046 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002047 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 d_delete(dentry);
2050 }
2051 dput(dentry);
2052
2053 return error;
2054}
2055
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002056static long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057{
2058 int error = 0;
2059 char * name;
2060 struct dentry *dentry;
2061 struct nameidata nd;
2062
2063 name = getname(pathname);
2064 if(IS_ERR(name))
2065 return PTR_ERR(name);
2066
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002067 error = do_path_lookup(dfd, name, LOOKUP_PARENT, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 if (error)
2069 goto exit;
2070
2071 switch(nd.last_type) {
2072 case LAST_DOTDOT:
2073 error = -ENOTEMPTY;
2074 goto exit1;
2075 case LAST_DOT:
2076 error = -EINVAL;
2077 goto exit1;
2078 case LAST_ROOT:
2079 error = -EBUSY;
2080 goto exit1;
2081 }
Ingo Molnarf2eace22006-07-03 00:25:05 -07002082 mutex_lock_nested(&nd.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002083 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002085 if (IS_ERR(dentry))
2086 goto exit2;
2087 error = vfs_rmdir(nd.dentry->d_inode, dentry);
2088 dput(dentry);
2089exit2:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002090 mutex_unlock(&nd.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091exit1:
2092 path_release(&nd);
2093exit:
2094 putname(name);
2095 return error;
2096}
2097
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002098asmlinkage long sys_rmdir(const char __user *pathname)
2099{
2100 return do_rmdir(AT_FDCWD, pathname);
2101}
2102
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103int vfs_unlink(struct inode *dir, struct dentry *dentry)
2104{
2105 int error = may_delete(dir, dentry, 0);
2106
2107 if (error)
2108 return error;
2109
2110 if (!dir->i_op || !dir->i_op->unlink)
2111 return -EPERM;
2112
2113 DQUOT_INIT(dir);
2114
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002115 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 if (d_mountpoint(dentry))
2117 error = -EBUSY;
2118 else {
2119 error = security_inode_unlink(dir, dentry);
2120 if (!error)
2121 error = dir->i_op->unlink(dir, dentry);
2122 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002123 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
2125 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2126 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2127 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 }
Robert Love0eeca282005-07-12 17:06:03 -04002129
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 return error;
2131}
2132
2133/*
2134 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002135 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 * writeout happening, and we don't want to prevent access to the directory
2137 * while waiting on the I/O.
2138 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002139static long do_unlinkat(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140{
2141 int error = 0;
2142 char * name;
2143 struct dentry *dentry;
2144 struct nameidata nd;
2145 struct inode *inode = NULL;
2146
2147 name = getname(pathname);
2148 if(IS_ERR(name))
2149 return PTR_ERR(name);
2150
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002151 error = do_path_lookup(dfd, name, LOOKUP_PARENT, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 if (error)
2153 goto exit;
2154 error = -EISDIR;
2155 if (nd.last_type != LAST_NORM)
2156 goto exit1;
Ingo Molnarf2eace22006-07-03 00:25:05 -07002157 mutex_lock_nested(&nd.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002158 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 error = PTR_ERR(dentry);
2160 if (!IS_ERR(dentry)) {
2161 /* Why not before? Because we want correct error value */
2162 if (nd.last.name[nd.last.len])
2163 goto slashes;
2164 inode = dentry->d_inode;
2165 if (inode)
2166 atomic_inc(&inode->i_count);
2167 error = vfs_unlink(nd.dentry->d_inode, dentry);
2168 exit2:
2169 dput(dentry);
2170 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002171 mutex_unlock(&nd.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 if (inode)
2173 iput(inode); /* truncate the inode here */
2174exit1:
2175 path_release(&nd);
2176exit:
2177 putname(name);
2178 return error;
2179
2180slashes:
2181 error = !dentry->d_inode ? -ENOENT :
2182 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2183 goto exit2;
2184}
2185
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002186asmlinkage long sys_unlinkat(int dfd, const char __user *pathname, int flag)
2187{
2188 if ((flag & ~AT_REMOVEDIR) != 0)
2189 return -EINVAL;
2190
2191 if (flag & AT_REMOVEDIR)
2192 return do_rmdir(dfd, pathname);
2193
2194 return do_unlinkat(dfd, pathname);
2195}
2196
2197asmlinkage long sys_unlink(const char __user *pathname)
2198{
2199 return do_unlinkat(AT_FDCWD, pathname);
2200}
2201
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname, int mode)
2203{
2204 int error = may_create(dir, dentry, NULL);
2205
2206 if (error)
2207 return error;
2208
2209 if (!dir->i_op || !dir->i_op->symlink)
2210 return -EPERM;
2211
2212 error = security_inode_symlink(dir, dentry, oldname);
2213 if (error)
2214 return error;
2215
2216 DQUOT_INIT(dir);
2217 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002218 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002219 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 return error;
2221}
2222
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002223asmlinkage long sys_symlinkat(const char __user *oldname,
2224 int newdfd, const char __user *newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225{
2226 int error = 0;
2227 char * from;
2228 char * to;
Dave Hansen6902d922006-09-30 23:29:01 -07002229 struct dentry *dentry;
2230 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
2232 from = getname(oldname);
2233 if(IS_ERR(from))
2234 return PTR_ERR(from);
2235 to = getname(newname);
2236 error = PTR_ERR(to);
Dave Hansen6902d922006-09-30 23:29:01 -07002237 if (IS_ERR(to))
2238 goto out_putname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Dave Hansen6902d922006-09-30 23:29:01 -07002240 error = do_path_lookup(newdfd, to, LOOKUP_PARENT, &nd);
2241 if (error)
2242 goto out;
2243 dentry = lookup_create(&nd, 0);
2244 error = PTR_ERR(dentry);
2245 if (IS_ERR(dentry))
2246 goto out_unlock;
2247
2248 error = vfs_symlink(nd.dentry->d_inode, dentry, from, S_IALLUGO);
2249 dput(dentry);
2250out_unlock:
2251 mutex_unlock(&nd.dentry->d_inode->i_mutex);
2252 path_release(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253out:
Dave Hansen6902d922006-09-30 23:29:01 -07002254 putname(to);
2255out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 putname(from);
2257 return error;
2258}
2259
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002260asmlinkage long sys_symlink(const char __user *oldname, const char __user *newname)
2261{
2262 return sys_symlinkat(oldname, AT_FDCWD, newname);
2263}
2264
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2266{
2267 struct inode *inode = old_dentry->d_inode;
2268 int error;
2269
2270 if (!inode)
2271 return -ENOENT;
2272
2273 error = may_create(dir, new_dentry, NULL);
2274 if (error)
2275 return error;
2276
2277 if (dir->i_sb != inode->i_sb)
2278 return -EXDEV;
2279
2280 /*
2281 * A link to an append-only or immutable file cannot be created.
2282 */
2283 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2284 return -EPERM;
2285 if (!dir->i_op || !dir->i_op->link)
2286 return -EPERM;
2287 if (S_ISDIR(old_dentry->d_inode->i_mode))
2288 return -EPERM;
2289
2290 error = security_inode_link(old_dentry, dir, new_dentry);
2291 if (error)
2292 return error;
2293
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002294 mutex_lock(&old_dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 DQUOT_INIT(dir);
2296 error = dir->i_op->link(old_dentry, dir, new_dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002297 mutex_unlock(&old_dentry->d_inode->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07002298 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002299 fsnotify_create(dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 return error;
2301}
2302
2303/*
2304 * Hardlinks are often used in delicate situations. We avoid
2305 * security-related surprises by not following symlinks on the
2306 * newname. --KAB
2307 *
2308 * We don't follow them on the oldname either to be compatible
2309 * with linux 2.0, and to avoid hard-linking to directories
2310 * and other special files. --ADM
2311 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002312asmlinkage long sys_linkat(int olddfd, const char __user *oldname,
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002313 int newdfd, const char __user *newname,
2314 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315{
2316 struct dentry *new_dentry;
2317 struct nameidata nd, old_nd;
2318 int error;
2319 char * to;
2320
Ulrich Drepper45c9b11a2006-06-25 05:49:11 -07002321 if ((flags & ~AT_SYMLINK_FOLLOW) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002322 return -EINVAL;
2323
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 to = getname(newname);
2325 if (IS_ERR(to))
2326 return PTR_ERR(to);
2327
Ulrich Drepper45c9b11a2006-06-25 05:49:11 -07002328 error = __user_walk_fd(olddfd, oldname,
2329 flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0,
2330 &old_nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 if (error)
2332 goto exit;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002333 error = do_path_lookup(newdfd, to, LOOKUP_PARENT, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 if (error)
2335 goto out;
2336 error = -EXDEV;
2337 if (old_nd.mnt != nd.mnt)
2338 goto out_release;
2339 new_dentry = lookup_create(&nd, 0);
2340 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002341 if (IS_ERR(new_dentry))
2342 goto out_unlock;
2343 error = vfs_link(old_nd.dentry, nd.dentry->d_inode, new_dentry);
2344 dput(new_dentry);
2345out_unlock:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002346 mutex_unlock(&nd.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347out_release:
2348 path_release(&nd);
2349out:
2350 path_release(&old_nd);
2351exit:
2352 putname(to);
2353
2354 return error;
2355}
2356
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002357asmlinkage long sys_link(const char __user *oldname, const char __user *newname)
2358{
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002359 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002360}
2361
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362/*
2363 * The worst of all namespace operations - renaming directory. "Perverted"
2364 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2365 * Problems:
2366 * a) we can get into loop creation. Check is done in is_subdir().
2367 * b) race potential - two innocent renames can create a loop together.
2368 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002369 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 * story.
2371 * c) we have to lock _three_ objects - parents and victim (if it exists).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002372 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 * whether the target exists). Solution: try to be smart with locking
2374 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002375 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 * move will be locked. Thus we can rank directories by the tree
2377 * (ancestors first) and rank all non-directories after them.
2378 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002379 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 * HOWEVER, it relies on the assumption that any object with ->lookup()
2381 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2382 * we'd better make sure that there's no link(2) for them.
2383 * d) some filesystems don't support opened-but-unlinked directories,
2384 * either because of layout or because they are not ready to deal with
2385 * all cases correctly. The latter will be fixed (taking this sort of
2386 * stuff into VFS), but the former is not going away. Solution: the same
2387 * trick as in rmdir().
2388 * e) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002389 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002391 * ->i_mutex on parents, which works but leads to some truely excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 * locking].
2393 */
Adrian Bunk75c96f82005-05-05 16:16:09 -07002394static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
2395 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396{
2397 int error = 0;
2398 struct inode *target;
2399
2400 /*
2401 * If we are going to change the parent - check write permissions,
2402 * we'll need to flip '..'.
2403 */
2404 if (new_dir != old_dir) {
2405 error = permission(old_dentry->d_inode, MAY_WRITE, NULL);
2406 if (error)
2407 return error;
2408 }
2409
2410 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2411 if (error)
2412 return error;
2413
2414 target = new_dentry->d_inode;
2415 if (target) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002416 mutex_lock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 dentry_unhash(new_dentry);
2418 }
2419 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2420 error = -EBUSY;
2421 else
2422 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2423 if (target) {
2424 if (!error)
2425 target->i_flags |= S_DEAD;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002426 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 if (d_unhashed(new_dentry))
2428 d_rehash(new_dentry);
2429 dput(new_dentry);
2430 }
Stephen Smalleye31e14e2005-09-09 13:01:45 -07002431 if (!error)
Mark Fasheh349457c2006-09-08 14:22:21 -07002432 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
2433 d_move(old_dentry,new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 return error;
2435}
2436
Adrian Bunk75c96f82005-05-05 16:16:09 -07002437static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
2438 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439{
2440 struct inode *target;
2441 int error;
2442
2443 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2444 if (error)
2445 return error;
2446
2447 dget(new_dentry);
2448 target = new_dentry->d_inode;
2449 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002450 mutex_lock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2452 error = -EBUSY;
2453 else
2454 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2455 if (!error) {
Mark Fasheh349457c2006-09-08 14:22:21 -07002456 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 d_move(old_dentry, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 }
2459 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002460 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 dput(new_dentry);
2462 return error;
2463}
2464
2465int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
2466 struct inode *new_dir, struct dentry *new_dentry)
2467{
2468 int error;
2469 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
Robert Love0eeca282005-07-12 17:06:03 -04002470 const char *old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471
2472 if (old_dentry->d_inode == new_dentry->d_inode)
2473 return 0;
2474
2475 error = may_delete(old_dir, old_dentry, is_dir);
2476 if (error)
2477 return error;
2478
2479 if (!new_dentry->d_inode)
2480 error = may_create(new_dir, new_dentry, NULL);
2481 else
2482 error = may_delete(new_dir, new_dentry, is_dir);
2483 if (error)
2484 return error;
2485
2486 if (!old_dir->i_op || !old_dir->i_op->rename)
2487 return -EPERM;
2488
2489 DQUOT_INIT(old_dir);
2490 DQUOT_INIT(new_dir);
2491
Robert Love0eeca282005-07-12 17:06:03 -04002492 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
2493
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 if (is_dir)
2495 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
2496 else
2497 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
2498 if (!error) {
Robert Love0eeca282005-07-12 17:06:03 -04002499 const char *new_name = old_dentry->d_name.name;
John McCutchan89204c42005-08-15 12:13:28 -04002500 fsnotify_move(old_dir, new_dir, old_name, new_name, is_dir,
2501 new_dentry->d_inode, old_dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 }
Robert Love0eeca282005-07-12 17:06:03 -04002503 fsnotify_oldname_free(old_name);
2504
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 return error;
2506}
2507
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002508static int do_rename(int olddfd, const char *oldname,
2509 int newdfd, const char *newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510{
2511 int error = 0;
2512 struct dentry * old_dir, * new_dir;
2513 struct dentry * old_dentry, *new_dentry;
2514 struct dentry * trap;
2515 struct nameidata oldnd, newnd;
2516
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002517 error = do_path_lookup(olddfd, oldname, LOOKUP_PARENT, &oldnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 if (error)
2519 goto exit;
2520
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002521 error = do_path_lookup(newdfd, newname, LOOKUP_PARENT, &newnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 if (error)
2523 goto exit1;
2524
2525 error = -EXDEV;
2526 if (oldnd.mnt != newnd.mnt)
2527 goto exit2;
2528
2529 old_dir = oldnd.dentry;
2530 error = -EBUSY;
2531 if (oldnd.last_type != LAST_NORM)
2532 goto exit2;
2533
2534 new_dir = newnd.dentry;
2535 if (newnd.last_type != LAST_NORM)
2536 goto exit2;
2537
2538 trap = lock_rename(new_dir, old_dir);
2539
Christoph Hellwig49705b72005-11-08 21:35:06 -08002540 old_dentry = lookup_hash(&oldnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 error = PTR_ERR(old_dentry);
2542 if (IS_ERR(old_dentry))
2543 goto exit3;
2544 /* source must exist */
2545 error = -ENOENT;
2546 if (!old_dentry->d_inode)
2547 goto exit4;
2548 /* unless the source is a directory trailing slashes give -ENOTDIR */
2549 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
2550 error = -ENOTDIR;
2551 if (oldnd.last.name[oldnd.last.len])
2552 goto exit4;
2553 if (newnd.last.name[newnd.last.len])
2554 goto exit4;
2555 }
2556 /* source should not be ancestor of target */
2557 error = -EINVAL;
2558 if (old_dentry == trap)
2559 goto exit4;
Christoph Hellwig49705b72005-11-08 21:35:06 -08002560 new_dentry = lookup_hash(&newnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 error = PTR_ERR(new_dentry);
2562 if (IS_ERR(new_dentry))
2563 goto exit4;
2564 /* target should not be an ancestor of source */
2565 error = -ENOTEMPTY;
2566 if (new_dentry == trap)
2567 goto exit5;
2568
2569 error = vfs_rename(old_dir->d_inode, old_dentry,
2570 new_dir->d_inode, new_dentry);
2571exit5:
2572 dput(new_dentry);
2573exit4:
2574 dput(old_dentry);
2575exit3:
2576 unlock_rename(new_dir, old_dir);
2577exit2:
2578 path_release(&newnd);
2579exit1:
2580 path_release(&oldnd);
2581exit:
2582 return error;
2583}
2584
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002585asmlinkage long sys_renameat(int olddfd, const char __user *oldname,
2586 int newdfd, const char __user *newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587{
2588 int error;
2589 char * from;
2590 char * to;
2591
2592 from = getname(oldname);
2593 if(IS_ERR(from))
2594 return PTR_ERR(from);
2595 to = getname(newname);
2596 error = PTR_ERR(to);
2597 if (!IS_ERR(to)) {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002598 error = do_rename(olddfd, from, newdfd, to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 putname(to);
2600 }
2601 putname(from);
2602 return error;
2603}
2604
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002605asmlinkage long sys_rename(const char __user *oldname, const char __user *newname)
2606{
2607 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
2608}
2609
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
2611{
2612 int len;
2613
2614 len = PTR_ERR(link);
2615 if (IS_ERR(link))
2616 goto out;
2617
2618 len = strlen(link);
2619 if (len > (unsigned) buflen)
2620 len = buflen;
2621 if (copy_to_user(buffer, link, len))
2622 len = -EFAULT;
2623out:
2624 return len;
2625}
2626
2627/*
2628 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
2629 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
2630 * using) it for any given inode is up to filesystem.
2631 */
2632int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2633{
2634 struct nameidata nd;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002635 void *cookie;
2636
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 nd.depth = 0;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002638 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
2639 if (!IS_ERR(cookie)) {
2640 int res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 if (dentry->d_inode->i_op->put_link)
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002642 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
2643 cookie = ERR_PTR(res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 }
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002645 return PTR_ERR(cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646}
2647
2648int vfs_follow_link(struct nameidata *nd, const char *link)
2649{
2650 return __vfs_follow_link(nd, link);
2651}
2652
2653/* get the link contents into pagecache */
2654static char *page_getlink(struct dentry * dentry, struct page **ppage)
2655{
2656 struct page * page;
2657 struct address_space *mapping = dentry->d_inode->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -07002658 page = read_mapping_page(mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 if (IS_ERR(page))
Nick Piggin6fe69002007-05-06 14:49:04 -07002660 return (char*)page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 *ppage = page;
2662 return kmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663}
2664
2665int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2666{
2667 struct page *page = NULL;
2668 char *s = page_getlink(dentry, &page);
2669 int res = vfs_readlink(dentry,buffer,buflen,s);
2670 if (page) {
2671 kunmap(page);
2672 page_cache_release(page);
2673 }
2674 return res;
2675}
2676
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002677void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002679 struct page *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 nd_set_link(nd, page_getlink(dentry, &page));
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002681 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682}
2683
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002684void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002686 struct page *page = cookie;
2687
2688 if (page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 kunmap(page);
2690 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 }
2692}
2693
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08002694int __page_symlink(struct inode *inode, const char *symname, int len,
2695 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696{
2697 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08002698 struct page *page;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08002699 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 char *kaddr;
2701
NeilBrown7e53cac2006-03-25 03:07:57 -08002702retry:
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08002703 err = -ENOMEM;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08002704 page = find_or_create_page(mapping, 0, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 if (!page)
2706 goto fail;
2707 err = mapping->a_ops->prepare_write(NULL, page, 0, len-1);
NeilBrown7e53cac2006-03-25 03:07:57 -08002708 if (err == AOP_TRUNCATED_PAGE) {
2709 page_cache_release(page);
2710 goto retry;
2711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 if (err)
2713 goto fail_map;
2714 kaddr = kmap_atomic(page, KM_USER0);
2715 memcpy(kaddr, symname, len-1);
2716 kunmap_atomic(kaddr, KM_USER0);
NeilBrown7e53cac2006-03-25 03:07:57 -08002717 err = mapping->a_ops->commit_write(NULL, page, 0, len-1);
2718 if (err == AOP_TRUNCATED_PAGE) {
2719 page_cache_release(page);
2720 goto retry;
2721 }
2722 if (err)
2723 goto fail_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 /*
2725 * Notice that we are _not_ going to block here - end of page is
2726 * unmapped, so this will only try to map the rest of page, see
2727 * that it is unmapped (typically even will not look into inode -
2728 * ->i_size will be enough for everything) and zero it out.
2729 * OTOH it's obviously correct and should make the page up-to-date.
2730 */
2731 if (!PageUptodate(page)) {
2732 err = mapping->a_ops->readpage(NULL, page);
NeilBrown7e53cac2006-03-25 03:07:57 -08002733 if (err != AOP_TRUNCATED_PAGE)
2734 wait_on_page_locked(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 } else {
2736 unlock_page(page);
2737 }
2738 page_cache_release(page);
2739 if (err < 0)
2740 goto fail;
2741 mark_inode_dirty(inode);
2742 return 0;
2743fail_map:
2744 unlock_page(page);
2745 page_cache_release(page);
2746fail:
2747 return err;
2748}
2749
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08002750int page_symlink(struct inode *inode, const char *symname, int len)
2751{
2752 return __page_symlink(inode, symname, len,
2753 mapping_gfp_mask(inode->i_mapping));
2754}
2755
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002756const struct inode_operations page_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 .readlink = generic_readlink,
2758 .follow_link = page_follow_link_light,
2759 .put_link = page_put_link,
2760};
2761
2762EXPORT_SYMBOL(__user_walk);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002763EXPORT_SYMBOL(__user_walk_fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764EXPORT_SYMBOL(follow_down);
2765EXPORT_SYMBOL(follow_up);
2766EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
2767EXPORT_SYMBOL(getname);
2768EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769EXPORT_SYMBOL(lookup_one_len);
2770EXPORT_SYMBOL(page_follow_link_light);
2771EXPORT_SYMBOL(page_put_link);
2772EXPORT_SYMBOL(page_readlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08002773EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774EXPORT_SYMBOL(page_symlink);
2775EXPORT_SYMBOL(page_symlink_inode_operations);
2776EXPORT_SYMBOL(path_lookup);
2777EXPORT_SYMBOL(path_release);
2778EXPORT_SYMBOL(path_walk);
2779EXPORT_SYMBOL(permission);
Christoph Hellwige4543ed2005-11-08 21:35:04 -08002780EXPORT_SYMBOL(vfs_permission);
Christoph Hellwig8c744fb2005-11-08 21:35:04 -08002781EXPORT_SYMBOL(file_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782EXPORT_SYMBOL(unlock_rename);
2783EXPORT_SYMBOL(vfs_create);
2784EXPORT_SYMBOL(vfs_follow_link);
2785EXPORT_SYMBOL(vfs_link);
2786EXPORT_SYMBOL(vfs_mkdir);
2787EXPORT_SYMBOL(vfs_mknod);
2788EXPORT_SYMBOL(generic_permission);
2789EXPORT_SYMBOL(vfs_readlink);
2790EXPORT_SYMBOL(vfs_rename);
2791EXPORT_SYMBOL(vfs_rmdir);
2792EXPORT_SYMBOL(vfs_symlink);
2793EXPORT_SYMBOL(vfs_unlink);
2794EXPORT_SYMBOL(dentry_unhash);
2795EXPORT_SYMBOL(generic_readlink);