blob: 01e67dddcc3d2033a2038c37f9f791ba0921a781 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/namei.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7/*
8 * Some corrections by tytso.
9 */
10
11/* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
12 * lookup logic.
13 */
14/* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
15 */
16
17#include <linux/init.h>
18#include <linux/module.h>
19#include <linux/slab.h>
20#include <linux/fs.h>
21#include <linux/namei.h>
22#include <linux/quotaops.h>
23#include <linux/pagemap.h>
Robert Love0eeca282005-07-12 17:06:03 -040024#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/personality.h>
26#include <linux/security.h>
27#include <linux/syscalls.h>
28#include <linux/mount.h>
29#include <linux/audit.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080030#include <linux/capability.h>
Trond Myklebust834f2a42005-10-18 14:20:16 -070031#include <linux/file.h>
Ulrich Drepper5590ff02006-01-18 17:43:53 -080032#include <linux/fcntl.h>
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070033#include <linux/device_cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/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
Al Viroa02f76c2008-02-23 15:14:28 +0000110static int __link_path_walk(const char *name, struct nameidata *nd);
Josef 'Jeff' Sipekc4a78082007-07-19 01:48:22 -0700111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112/* In order to reduce some races, while at the same time doing additional
113 * checking and hopefully speeding things up, we copy filenames to the
114 * kernel data space before using them..
115 *
116 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
117 * PATH_MAX includes the nul terminator --RR.
118 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800119static int do_getname(const char __user *filename, char *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
121 int retval;
122 unsigned long len = PATH_MAX;
123
124 if (!segment_eq(get_fs(), KERNEL_DS)) {
125 if ((unsigned long) filename >= TASK_SIZE)
126 return -EFAULT;
127 if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
128 len = TASK_SIZE - (unsigned long) filename;
129 }
130
131 retval = strncpy_from_user(page, filename, len);
132 if (retval > 0) {
133 if (retval < len)
134 return 0;
135 return -ENAMETOOLONG;
136 } else if (!retval)
137 retval = -ENOENT;
138 return retval;
139}
140
141char * getname(const char __user * filename)
142{
143 char *tmp, *result;
144
145 result = ERR_PTR(-ENOMEM);
146 tmp = __getname();
147 if (tmp) {
148 int retval = do_getname(filename, tmp);
149
150 result = tmp;
151 if (retval < 0) {
152 __putname(tmp);
153 result = ERR_PTR(retval);
154 }
155 }
156 audit_getname(result);
157 return result;
158}
159
160#ifdef CONFIG_AUDITSYSCALL
161void putname(const char *name)
162{
Al Viro5ac3a9c2006-07-16 06:38:45 -0400163 if (unlikely(!audit_dummy_context()))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 audit_putname(name);
165 else
166 __putname(name);
167}
168EXPORT_SYMBOL(putname);
169#endif
170
171
172/**
173 * generic_permission - check for access rights on a Posix-like filesystem
174 * @inode: inode to check access rights for
175 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
176 * @check_acl: optional callback to check for Posix ACLs
177 *
178 * Used to check for read/write/execute permissions on a file.
179 * We use "fsuid" for this, letting us set arbitrary permissions
180 * for filesystem access without changing the "normal" uids which
181 * are used for other things..
182 */
183int generic_permission(struct inode *inode, int mask,
184 int (*check_acl)(struct inode *inode, int mask))
185{
186 umode_t mode = inode->i_mode;
187
188 if (current->fsuid == inode->i_uid)
189 mode >>= 6;
190 else {
191 if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
192 int error = check_acl(inode, mask);
193 if (error == -EACCES)
194 goto check_capabilities;
195 else if (error != -EAGAIN)
196 return error;
197 }
198
199 if (in_group_p(inode->i_gid))
200 mode >>= 3;
201 }
202
203 /*
204 * If the DACs are ok we don't need any capability check.
205 */
206 if (((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask))
207 return 0;
208
209 check_capabilities:
210 /*
211 * Read/write DACs are always overridable.
212 * Executable DACs are overridable if at least one exec bit is set.
213 */
214 if (!(mask & MAY_EXEC) ||
215 (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
216 if (capable(CAP_DAC_OVERRIDE))
217 return 0;
218
219 /*
220 * Searching includes executable on directories, else just read.
221 */
222 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
223 if (capable(CAP_DAC_READ_SEARCH))
224 return 0;
225
226 return -EACCES;
227}
228
229int permission(struct inode *inode, int mask, struct nameidata *nd)
230{
231 int retval, submask;
Dave Hansenc7eb2662007-10-16 23:31:14 -0700232 struct vfsmount *mnt = NULL;
233
234 if (nd)
Jan Blunck4ac91372008-02-14 19:34:32 -0800235 mnt = nd->path.mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 if (mask & MAY_WRITE) {
Miklos Szeredi22590e42007-10-16 23:27:08 -0700238 umode_t mode = inode->i_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 /*
241 * Nobody gets write access to a read-only fs.
242 */
243 if (IS_RDONLY(inode) &&
244 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
245 return -EROFS;
246
247 /*
248 * Nobody gets write access to an immutable file.
249 */
250 if (IS_IMMUTABLE(inode))
251 return -EACCES;
252 }
253
Miklos Szeredi22590e42007-10-16 23:27:08 -0700254 if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
255 /*
256 * MAY_EXEC on regular files is denied if the fs is mounted
257 * with the "noexec" flag.
258 */
Dave Hansenc7eb2662007-10-16 23:31:14 -0700259 if (mnt && (mnt->mnt_flags & MNT_NOEXEC))
Miklos Szeredi22590e42007-10-16 23:27:08 -0700260 return -EACCES;
261 }
Trond Myklebusta343bb72006-08-22 20:06:03 -0400262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 /* Ordinary permission routines do not understand MAY_APPEND. */
264 submask = mask & ~MAY_APPEND;
Miklos Szeredi22590e42007-10-16 23:27:08 -0700265 if (inode->i_op && inode->i_op->permission) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 retval = inode->i_op->permission(inode, submask, nd);
Miklos Szeredi22590e42007-10-16 23:27:08 -0700267 if (!retval) {
268 /*
269 * Exec permission on a regular file is denied if none
270 * of the execute bits are set.
271 *
272 * This check should be done by the ->permission()
273 * method.
274 */
275 if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode) &&
276 !(inode->i_mode & S_IXUGO))
277 return -EACCES;
278 }
279 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 retval = generic_permission(inode, submask, NULL);
Miklos Szeredi22590e42007-10-16 23:27:08 -0700281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (retval)
283 return retval;
284
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700285 retval = devcgroup_inode_permission(inode, mask);
286 if (retval)
287 return retval;
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return security_inode_permission(inode, mask, nd);
290}
291
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800292/**
293 * vfs_permission - check for access rights to a given path
294 * @nd: lookup result that describes the path
295 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
296 *
297 * Used to check for read/write/execute permissions on a path.
298 * We use "fsuid" for this, letting us set arbitrary permissions
299 * for filesystem access without changing the "normal" uids which
300 * are used for other things.
301 */
302int vfs_permission(struct nameidata *nd, int mask)
303{
Jan Blunck4ac91372008-02-14 19:34:32 -0800304 return permission(nd->path.dentry->d_inode, mask, nd);
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800305}
306
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800307/**
308 * file_permission - check for additional access rights to a given file
309 * @file: file to check access rights for
310 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
311 *
312 * Used to check for read/write/execute permissions on an already opened
313 * file.
314 *
315 * Note:
316 * Do not use this function in new code. All access checks should
317 * be done using vfs_permission().
318 */
319int file_permission(struct file *file, int mask)
320{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800321 return permission(file->f_path.dentry->d_inode, mask, NULL);
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800322}
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324/*
325 * get_write_access() gets write permission for a file.
326 * put_write_access() releases this write permission.
327 * This is used for regular files.
328 * We cannot support write (and maybe mmap read-write shared) accesses and
329 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
330 * can have the following values:
331 * 0: no writers, no VM_DENYWRITE mappings
332 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
333 * > 0: (i_writecount) users are writing to the file.
334 *
335 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
336 * except for the cases where we don't hold i_writecount yet. Then we need to
337 * use {get,deny}_write_access() - these functions check the sign and refuse
338 * to do the change if sign is wrong. Exclusion between them is provided by
339 * the inode->i_lock spinlock.
340 */
341
342int get_write_access(struct inode * inode)
343{
344 spin_lock(&inode->i_lock);
345 if (atomic_read(&inode->i_writecount) < 0) {
346 spin_unlock(&inode->i_lock);
347 return -ETXTBSY;
348 }
349 atomic_inc(&inode->i_writecount);
350 spin_unlock(&inode->i_lock);
351
352 return 0;
353}
354
355int deny_write_access(struct file * file)
356{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800357 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 spin_lock(&inode->i_lock);
360 if (atomic_read(&inode->i_writecount) > 0) {
361 spin_unlock(&inode->i_lock);
362 return -ETXTBSY;
363 }
364 atomic_dec(&inode->i_writecount);
365 spin_unlock(&inode->i_lock);
366
367 return 0;
368}
369
Jan Blunck1d957f92008-02-14 19:34:35 -0800370/**
Jan Blunck5dd784d2008-02-14 19:34:38 -0800371 * path_get - get a reference to a path
372 * @path: path to get the reference to
373 *
374 * Given a path increment the reference count to the dentry and the vfsmount.
375 */
376void path_get(struct path *path)
377{
378 mntget(path->mnt);
379 dget(path->dentry);
380}
381EXPORT_SYMBOL(path_get);
382
383/**
Jan Blunck1d957f92008-02-14 19:34:35 -0800384 * path_put - put a reference to a path
385 * @path: path to put the reference to
386 *
387 * Given a path decrement the reference count to the dentry and the vfsmount.
388 */
389void path_put(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
Jan Blunck1d957f92008-02-14 19:34:35 -0800391 dput(path->dentry);
392 mntput(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
Jan Blunck1d957f92008-02-14 19:34:35 -0800394EXPORT_SYMBOL(path_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Trond Myklebust834f2a42005-10-18 14:20:16 -0700396/**
397 * release_open_intent - free up open intent resources
398 * @nd: pointer to nameidata
399 */
400void release_open_intent(struct nameidata *nd)
401{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800402 if (nd->intent.open.file->f_path.dentry == NULL)
Trond Myklebust834f2a42005-10-18 14:20:16 -0700403 put_filp(nd->intent.open.file);
404 else
405 fput(nd->intent.open.file);
406}
407
Ian Kentbcdc5e02006-09-27 01:50:44 -0700408static inline struct dentry *
409do_revalidate(struct dentry *dentry, struct nameidata *nd)
410{
411 int status = dentry->d_op->d_revalidate(dentry, nd);
412 if (unlikely(status <= 0)) {
413 /*
414 * The dentry failed validation.
415 * If d_revalidate returned 0 attempt to invalidate
416 * the dentry otherwise d_revalidate is asking us
417 * to return a fail status.
418 */
419 if (!status) {
420 if (!d_invalidate(dentry)) {
421 dput(dentry);
422 dentry = NULL;
423 }
424 } else {
425 dput(dentry);
426 dentry = ERR_PTR(status);
427 }
428 }
429 return dentry;
430}
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432/*
433 * Internal lookup() using the new generic dcache.
434 * SMP-safe
435 */
436static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
437{
438 struct dentry * dentry = __d_lookup(parent, name);
439
440 /* lockess __d_lookup may fail due to concurrent d_move()
441 * in some unrelated directory, so try with d_lookup
442 */
443 if (!dentry)
444 dentry = d_lookup(parent, name);
445
Ian Kentbcdc5e02006-09-27 01:50:44 -0700446 if (dentry && dentry->d_op && dentry->d_op->d_revalidate)
447 dentry = do_revalidate(dentry, nd);
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return dentry;
450}
451
452/*
453 * Short-cut version of permission(), for calling by
454 * path_walk(), when dcache lock is held. Combines parts
455 * of permission() and generic_permission(), and tests ONLY for
456 * MAY_EXEC permission.
457 *
458 * If appropriate, check DAC only. If not appropriate, or
459 * short-cut DAC fails, then call permission() to do more
460 * complete permission check.
461 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800462static int exec_permission_lite(struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 struct nameidata *nd)
464{
465 umode_t mode = inode->i_mode;
466
467 if (inode->i_op && inode->i_op->permission)
468 return -EAGAIN;
469
470 if (current->fsuid == inode->i_uid)
471 mode >>= 6;
472 else if (in_group_p(inode->i_gid))
473 mode >>= 3;
474
475 if (mode & MAY_EXEC)
476 goto ok;
477
478 if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE))
479 goto ok;
480
481 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_OVERRIDE))
482 goto ok;
483
484 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH))
485 goto ok;
486
487 return -EACCES;
488ok:
489 return security_inode_permission(inode, MAY_EXEC, nd);
490}
491
492/*
493 * This is called when everything else fails, and we actually have
494 * to go to the low-level filesystem to find out what we should do..
495 *
496 * We get the directory semaphore, and after getting that we also
497 * make sure that nobody added the entry to the dcache in the meantime..
498 * SMP-safe
499 */
500static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
501{
502 struct dentry * result;
503 struct inode *dir = parent->d_inode;
504
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800505 mutex_lock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 /*
507 * First re-do the cached lookup just in case it was created
508 * while we waited for the directory semaphore..
509 *
510 * FIXME! This could use version numbering or similar to
511 * avoid unnecessary cache lookups.
512 *
513 * The "dcache_lock" is purely to protect the RCU list walker
514 * from concurrent renames at this point (we mustn't get false
515 * negatives from the RCU list walk here, unlike the optimistic
516 * fast walk).
517 *
518 * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
519 */
520 result = d_lookup(parent, name);
521 if (!result) {
522 struct dentry * dentry = d_alloc(parent, name);
523 result = ERR_PTR(-ENOMEM);
524 if (dentry) {
525 result = dir->i_op->lookup(dir, dentry, nd);
526 if (result)
527 dput(dentry);
528 else
529 result = dentry;
530 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800531 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 return result;
533 }
534
535 /*
536 * Uhhuh! Nasty case: the cache was re-populated while
537 * we waited on the semaphore. Need to revalidate.
538 */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800539 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 if (result->d_op && result->d_op->d_revalidate) {
Ian Kentbcdc5e02006-09-27 01:50:44 -0700541 result = do_revalidate(result, nd);
542 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
545 return result;
546}
547
548static int __emul_lookup_dentry(const char *, struct nameidata *);
549
550/* SMP-safe */
Arjan van de Venf1662352006-01-14 13:21:31 -0800551static __always_inline int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552walk_init_root(const char *name, struct nameidata *nd)
553{
Andreas Mohre518ddb2006-09-29 02:01:22 -0700554 struct fs_struct *fs = current->fs;
555
556 read_lock(&fs->lock);
Jan Blunck6ac08c32008-02-14 19:34:38 -0800557 if (fs->altroot.dentry && !(nd->flags & LOOKUP_NOALT)) {
558 nd->path = fs->altroot;
559 path_get(&fs->altroot);
Andreas Mohre518ddb2006-09-29 02:01:22 -0700560 read_unlock(&fs->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 if (__emul_lookup_dentry(name,nd))
562 return 0;
Andreas Mohre518ddb2006-09-29 02:01:22 -0700563 read_lock(&fs->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
Jan Blunck6ac08c32008-02-14 19:34:38 -0800565 nd->path = fs->root;
566 path_get(&fs->root);
Andreas Mohre518ddb2006-09-29 02:01:22 -0700567 read_unlock(&fs->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return 1;
569}
570
Al Viroa02f76c2008-02-23 15:14:28 +0000571/*
572 * Wrapper to retry pathname resolution whenever the underlying
573 * file system returns an ESTALE.
574 *
575 * Retry the whole path once, forcing real lookup requests
576 * instead of relying on the dcache.
577 */
578static __always_inline int link_path_walk(const char *name, struct nameidata *nd)
579{
580 struct path save = nd->path;
581 int result;
582
583 /* make sure the stuff we saved doesn't go away */
Jan Blunckc8e7f442008-06-09 16:40:35 -0700584 path_get(&save);
Al Viroa02f76c2008-02-23 15:14:28 +0000585
586 result = __link_path_walk(name, nd);
587 if (result == -ESTALE) {
588 /* nd->path had been dropped */
589 nd->path = save;
Jan Blunckc8e7f442008-06-09 16:40:35 -0700590 path_get(&nd->path);
Al Viroa02f76c2008-02-23 15:14:28 +0000591 nd->flags |= LOOKUP_REVAL;
592 result = __link_path_walk(name, nd);
593 }
594
595 path_put(&save);
596
597 return result;
598}
599
Arjan van de Venf1662352006-01-14 13:21:31 -0800600static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
602 int res = 0;
603 char *name;
604 if (IS_ERR(link))
605 goto fail;
606
607 if (*link == '/') {
Jan Blunck1d957f92008-02-14 19:34:35 -0800608 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if (!walk_init_root(link, nd))
610 /* weird __emul_prefix() stuff did it */
611 goto out;
612 }
613 res = link_path_walk(link, nd);
614out:
615 if (nd->depth || res || nd->last_type!=LAST_NORM)
616 return res;
617 /*
618 * If it is an iterative symlinks resolution in open_namei() we
619 * have to copy the last component. And all that crap because of
620 * bloody create() on broken symlinks. Furrfu...
621 */
622 name = __getname();
623 if (unlikely(!name)) {
Jan Blunck1d957f92008-02-14 19:34:35 -0800624 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return -ENOMEM;
626 }
627 strcpy(name, nd->last.name);
628 nd->last.name = name;
629 return 0;
630fail:
Jan Blunck1d957f92008-02-14 19:34:35 -0800631 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return PTR_ERR(link);
633}
634
Jan Blunck1d957f92008-02-14 19:34:35 -0800635static void path_put_conditional(struct path *path, struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700636{
637 dput(path->dentry);
Jan Blunck4ac91372008-02-14 19:34:32 -0800638 if (path->mnt != nd->path.mnt)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700639 mntput(path->mnt);
640}
641
642static inline void path_to_nameidata(struct path *path, struct nameidata *nd)
643{
Jan Blunck4ac91372008-02-14 19:34:32 -0800644 dput(nd->path.dentry);
645 if (nd->path.mnt != path->mnt)
646 mntput(nd->path.mnt);
647 nd->path.mnt = path->mnt;
648 nd->path.dentry = path->dentry;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700649}
650
Ian Kent051d3812006-03-27 01:14:53 -0800651static __always_inline int __do_follow_link(struct path *path, struct nameidata *nd)
652{
653 int error;
654 void *cookie;
655 struct dentry *dentry = path->dentry;
656
657 touch_atime(path->mnt, dentry);
658 nd_set_link(nd, NULL);
659
Jan Blunck4ac91372008-02-14 19:34:32 -0800660 if (path->mnt != nd->path.mnt) {
Ian Kent051d3812006-03-27 01:14:53 -0800661 path_to_nameidata(path, nd);
662 dget(dentry);
663 }
664 mntget(path->mnt);
665 cookie = dentry->d_inode->i_op->follow_link(dentry, nd);
666 error = PTR_ERR(cookie);
667 if (!IS_ERR(cookie)) {
668 char *s = nd_get_link(nd);
669 error = 0;
670 if (s)
671 error = __vfs_follow_link(nd, s);
672 if (dentry->d_inode->i_op->put_link)
673 dentry->d_inode->i_op->put_link(dentry, nd, cookie);
674 }
Jan Blunck09da59162008-02-14 19:34:37 -0800675 path_put(path);
Ian Kent051d3812006-03-27 01:14:53 -0800676
677 return error;
678}
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680/*
681 * This limits recursive symlink follows to 8, while
682 * limiting consecutive symlinks to 40.
683 *
684 * Without that kind of total limit, nasty chains of consecutive
685 * symlinks can cause almost arbitrarily long lookups.
686 */
Al Viro90ebe562005-06-06 13:35:58 -0700687static inline int do_follow_link(struct path *path, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
689 int err = -ELOOP;
690 if (current->link_count >= MAX_NESTED_LINKS)
691 goto loop;
692 if (current->total_link_count >= 40)
693 goto loop;
694 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
695 cond_resched();
Al Viro90ebe562005-06-06 13:35:58 -0700696 err = security_inode_follow_link(path->dentry, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if (err)
698 goto loop;
699 current->link_count++;
700 current->total_link_count++;
701 nd->depth++;
Al Virocd4e91d2005-06-06 13:36:03 -0700702 err = __do_follow_link(path, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 current->link_count--;
704 nd->depth--;
705 return err;
706loop:
Jan Blunck1d957f92008-02-14 19:34:35 -0800707 path_put_conditional(path, nd);
708 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return err;
710}
711
712int follow_up(struct vfsmount **mnt, struct dentry **dentry)
713{
714 struct vfsmount *parent;
715 struct dentry *mountpoint;
716 spin_lock(&vfsmount_lock);
717 parent=(*mnt)->mnt_parent;
718 if (parent == *mnt) {
719 spin_unlock(&vfsmount_lock);
720 return 0;
721 }
722 mntget(parent);
723 mountpoint=dget((*mnt)->mnt_mountpoint);
724 spin_unlock(&vfsmount_lock);
725 dput(*dentry);
726 *dentry = mountpoint;
727 mntput(*mnt);
728 *mnt = parent;
729 return 1;
730}
731
732/* no need for dcache_lock, as serialization is taken care in
733 * namespace.c
734 */
Al Viro463ffb22005-06-06 13:36:05 -0700735static int __follow_mount(struct path *path)
736{
737 int res = 0;
738 while (d_mountpoint(path->dentry)) {
739 struct vfsmount *mounted = lookup_mnt(path->mnt, path->dentry);
740 if (!mounted)
741 break;
742 dput(path->dentry);
743 if (res)
744 mntput(path->mnt);
745 path->mnt = mounted;
746 path->dentry = dget(mounted->mnt_root);
747 res = 1;
748 }
749 return res;
750}
751
Al Viro58c465e2005-06-06 13:36:13 -0700752static void follow_mount(struct vfsmount **mnt, struct dentry **dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 while (d_mountpoint(*dentry)) {
755 struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
756 if (!mounted)
757 break;
Al Viro58c465e2005-06-06 13:36:13 -0700758 dput(*dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 mntput(*mnt);
760 *mnt = mounted;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 *dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764
765/* no need for dcache_lock, as serialization is taken care in
766 * namespace.c
767 */
Al Viroe13b2102005-06-06 13:36:06 -0700768int follow_down(struct vfsmount **mnt, struct dentry **dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
770 struct vfsmount *mounted;
771
772 mounted = lookup_mnt(*mnt, *dentry);
773 if (mounted) {
Al Viroe13b2102005-06-06 13:36:06 -0700774 dput(*dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 mntput(*mnt);
776 *mnt = mounted;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 *dentry = dget(mounted->mnt_root);
778 return 1;
779 }
780 return 0;
781}
782
Arjan van de Venf1662352006-01-14 13:21:31 -0800783static __always_inline void follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Andreas Mohre518ddb2006-09-29 02:01:22 -0700785 struct fs_struct *fs = current->fs;
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 while(1) {
788 struct vfsmount *parent;
Jan Blunck4ac91372008-02-14 19:34:32 -0800789 struct dentry *old = nd->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Andreas Mohre518ddb2006-09-29 02:01:22 -0700791 read_lock(&fs->lock);
Jan Blunck6ac08c32008-02-14 19:34:38 -0800792 if (nd->path.dentry == fs->root.dentry &&
793 nd->path.mnt == fs->root.mnt) {
Andreas Mohre518ddb2006-09-29 02:01:22 -0700794 read_unlock(&fs->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 break;
796 }
Andreas Mohre518ddb2006-09-29 02:01:22 -0700797 read_unlock(&fs->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 spin_lock(&dcache_lock);
Jan Blunck4ac91372008-02-14 19:34:32 -0800799 if (nd->path.dentry != nd->path.mnt->mnt_root) {
800 nd->path.dentry = dget(nd->path.dentry->d_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 spin_unlock(&dcache_lock);
802 dput(old);
803 break;
804 }
805 spin_unlock(&dcache_lock);
806 spin_lock(&vfsmount_lock);
Jan Blunck4ac91372008-02-14 19:34:32 -0800807 parent = nd->path.mnt->mnt_parent;
808 if (parent == nd->path.mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 spin_unlock(&vfsmount_lock);
810 break;
811 }
812 mntget(parent);
Jan Blunck4ac91372008-02-14 19:34:32 -0800813 nd->path.dentry = dget(nd->path.mnt->mnt_mountpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 spin_unlock(&vfsmount_lock);
815 dput(old);
Jan Blunck4ac91372008-02-14 19:34:32 -0800816 mntput(nd->path.mnt);
817 nd->path.mnt = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 }
Jan Blunck4ac91372008-02-14 19:34:32 -0800819 follow_mount(&nd->path.mnt, &nd->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820}
821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822/*
823 * It's more convoluted than I'd like it to be, but... it's still fairly
824 * small and for now I'd prefer to have fast path as straight as possible.
825 * It _is_ time-critical.
826 */
827static int do_lookup(struct nameidata *nd, struct qstr *name,
828 struct path *path)
829{
Jan Blunck4ac91372008-02-14 19:34:32 -0800830 struct vfsmount *mnt = nd->path.mnt;
831 struct dentry *dentry = __d_lookup(nd->path.dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 if (!dentry)
834 goto need_lookup;
835 if (dentry->d_op && dentry->d_op->d_revalidate)
836 goto need_revalidate;
837done:
838 path->mnt = mnt;
839 path->dentry = dentry;
Al Viro634ee702005-06-06 13:36:13 -0700840 __follow_mount(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 return 0;
842
843need_lookup:
Jan Blunck4ac91372008-02-14 19:34:32 -0800844 dentry = real_lookup(nd->path.dentry, name, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 if (IS_ERR(dentry))
846 goto fail;
847 goto done;
848
849need_revalidate:
Ian Kentbcdc5e02006-09-27 01:50:44 -0700850 dentry = do_revalidate(dentry, nd);
851 if (!dentry)
852 goto need_lookup;
853 if (IS_ERR(dentry))
854 goto fail;
855 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
857fail:
858 return PTR_ERR(dentry);
859}
860
861/*
862 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +0100863 * This is the basic name resolution function, turning a pathname into
864 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 *
Prasanna Medaea3834d2005-04-29 16:00:17 +0100866 * Returns 0 and nd will have valid dentry and mnt on success.
867 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 */
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -0800869static int __link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
871 struct path next;
872 struct inode *inode;
873 int err;
874 unsigned int lookup_flags = nd->flags;
875
876 while (*name=='/')
877 name++;
878 if (!*name)
879 goto return_reval;
880
Jan Blunck4ac91372008-02-14 19:34:32 -0800881 inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 if (nd->depth)
Trond Myklebustf55eab82006-02-04 23:28:01 -0800883 lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 /* At this point we know we have a real path component. */
886 for(;;) {
887 unsigned long hash;
888 struct qstr this;
889 unsigned int c;
890
Trond Myklebustcdce5d62005-10-18 14:20:18 -0700891 nd->flags |= LOOKUP_CONTINUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 err = exec_permission_lite(inode, nd);
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800893 if (err == -EAGAIN)
894 err = vfs_permission(nd, MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 if (err)
896 break;
897
898 this.name = name;
899 c = *(const unsigned char *)name;
900
901 hash = init_name_hash();
902 do {
903 name++;
904 hash = partial_name_hash(c, hash);
905 c = *(const unsigned char *)name;
906 } while (c && (c != '/'));
907 this.len = name - (const char *) this.name;
908 this.hash = end_name_hash(hash);
909
910 /* remove trailing slashes? */
911 if (!c)
912 goto last_component;
913 while (*++name == '/');
914 if (!*name)
915 goto last_with_slashes;
916
917 /*
918 * "." and ".." are special - ".." especially so because it has
919 * to be able to know about the current root directory and
920 * parent relationships.
921 */
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);
Jan Blunck4ac91372008-02-14 19:34:32 -0800929 inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 /* fallthrough */
931 case 1:
932 continue;
933 }
934 /*
935 * See if the low-level filesystem might want
936 * to use its own hash..
937 */
Jan Blunck4ac91372008-02-14 19:34:32 -0800938 if (nd->path.dentry->d_op && nd->path.dentry->d_op->d_hash) {
939 err = nd->path.dentry->d_op->d_hash(nd->path.dentry,
940 &this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 if (err < 0)
942 break;
943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 /* This does the actual lookups.. */
945 err = do_lookup(nd, &this, &next);
946 if (err)
947 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 err = -ENOENT;
950 inode = next.dentry->d_inode;
951 if (!inode)
952 goto out_dput;
953 err = -ENOTDIR;
954 if (!inode->i_op)
955 goto out_dput;
956
957 if (inode->i_op->follow_link) {
Al Viro90ebe562005-06-06 13:35:58 -0700958 err = do_follow_link(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 if (err)
960 goto return_err;
961 err = -ENOENT;
Jan Blunck4ac91372008-02-14 19:34:32 -0800962 inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 if (!inode)
964 break;
965 err = -ENOTDIR;
966 if (!inode->i_op)
967 break;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700968 } else
969 path_to_nameidata(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 err = -ENOTDIR;
971 if (!inode->i_op->lookup)
972 break;
973 continue;
974 /* here ends the main loop */
975
976last_with_slashes:
977 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
978last_component:
Trond Myklebustf55eab82006-02-04 23:28:01 -0800979 /* Clear LOOKUP_CONTINUE iff it was previously unset */
980 nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 if (lookup_flags & LOOKUP_PARENT)
982 goto lookup_parent;
983 if (this.name[0] == '.') switch (this.len) {
984 default:
985 break;
986 case 2:
987 if (this.name[1] != '.')
988 break;
Al Viro58c465e2005-06-06 13:36:13 -0700989 follow_dotdot(nd);
Jan Blunck4ac91372008-02-14 19:34:32 -0800990 inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 /* fallthrough */
992 case 1:
993 goto return_reval;
994 }
Jan Blunck4ac91372008-02-14 19:34:32 -0800995 if (nd->path.dentry->d_op && nd->path.dentry->d_op->d_hash) {
996 err = nd->path.dentry->d_op->d_hash(nd->path.dentry,
997 &this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 if (err < 0)
999 break;
1000 }
1001 err = do_lookup(nd, &this, &next);
1002 if (err)
1003 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 inode = next.dentry->d_inode;
1005 if ((lookup_flags & LOOKUP_FOLLOW)
1006 && inode && inode->i_op && inode->i_op->follow_link) {
Al Viro90ebe562005-06-06 13:35:58 -07001007 err = do_follow_link(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 if (err)
1009 goto return_err;
Jan Blunck4ac91372008-02-14 19:34:32 -08001010 inode = nd->path.dentry->d_inode;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -07001011 } else
1012 path_to_nameidata(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 err = -ENOENT;
1014 if (!inode)
1015 break;
1016 if (lookup_flags & LOOKUP_DIRECTORY) {
1017 err = -ENOTDIR;
1018 if (!inode->i_op || !inode->i_op->lookup)
1019 break;
1020 }
1021 goto return_base;
1022lookup_parent:
1023 nd->last = this;
1024 nd->last_type = LAST_NORM;
1025 if (this.name[0] != '.')
1026 goto return_base;
1027 if (this.len == 1)
1028 nd->last_type = LAST_DOT;
1029 else if (this.len == 2 && this.name[1] == '.')
1030 nd->last_type = LAST_DOTDOT;
1031 else
1032 goto return_base;
1033return_reval:
1034 /*
1035 * We bypassed the ordinary revalidation routines.
1036 * We may need to check the cached dentry for staleness.
1037 */
Jan Blunck4ac91372008-02-14 19:34:32 -08001038 if (nd->path.dentry && nd->path.dentry->d_sb &&
1039 (nd->path.dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 err = -ESTALE;
1041 /* Note: we do not d_invalidate() */
Jan Blunck4ac91372008-02-14 19:34:32 -08001042 if (!nd->path.dentry->d_op->d_revalidate(
1043 nd->path.dentry, nd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 break;
1045 }
1046return_base:
1047 return 0;
1048out_dput:
Jan Blunck1d957f92008-02-14 19:34:35 -08001049 path_put_conditional(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 break;
1051 }
Jan Blunck1d957f92008-02-14 19:34:35 -08001052 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053return_err:
1054 return err;
1055}
1056
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -08001057static int path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
1059 current->total_link_count = 0;
1060 return link_path_walk(name, nd);
1061}
1062
Prasanna Medaea3834d2005-04-29 16:00:17 +01001063/*
1064 * SMP-safe: Returns 1 and nd will have valid dentry and mnt, if
1065 * everything is done. Returns 0 and drops input nd, if lookup failed;
1066 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067static int __emul_lookup_dentry(const char *name, struct nameidata *nd)
1068{
1069 if (path_walk(name, nd))
1070 return 0; /* something went wrong... */
1071
Jan Blunck4ac91372008-02-14 19:34:32 -08001072 if (!nd->path.dentry->d_inode ||
1073 S_ISDIR(nd->path.dentry->d_inode->i_mode)) {
Jan Blunck09da59162008-02-14 19:34:37 -08001074 struct path old_path = nd->path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 struct qstr last = nd->last;
1076 int last_type = nd->last_type;
Andreas Mohre518ddb2006-09-29 02:01:22 -07001077 struct fs_struct *fs = current->fs;
1078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 /*
Andreas Mohre518ddb2006-09-29 02:01:22 -07001080 * NAME was not found in alternate root or it's a directory.
1081 * Try to find it in the normal root:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 */
1083 nd->last_type = LAST_ROOT;
Andreas Mohre518ddb2006-09-29 02:01:22 -07001084 read_lock(&fs->lock);
Jan Blunck6ac08c32008-02-14 19:34:38 -08001085 nd->path = fs->root;
1086 path_get(&fs->root);
Andreas Mohre518ddb2006-09-29 02:01:22 -07001087 read_unlock(&fs->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 if (path_walk(name, nd) == 0) {
Jan Blunck4ac91372008-02-14 19:34:32 -08001089 if (nd->path.dentry->d_inode) {
Jan Blunck09da59162008-02-14 19:34:37 -08001090 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 return 1;
1092 }
Jan Blunck1d957f92008-02-14 19:34:35 -08001093 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 }
Jan Blunck09da59162008-02-14 19:34:37 -08001095 nd->path = old_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 nd->last = last;
1097 nd->last_type = last_type;
1098 }
1099 return 1;
1100}
1101
1102void set_fs_altroot(void)
1103{
1104 char *emul = __emul_prefix();
1105 struct nameidata nd;
Jan Blunck6ac08c32008-02-14 19:34:38 -08001106 struct path path = {}, old_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 int err;
Andreas Mohre518ddb2006-09-29 02:01:22 -07001108 struct fs_struct *fs = current->fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
1110 if (!emul)
1111 goto set_it;
1112 err = path_lookup(emul, LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_NOALT, &nd);
Jan Blunck6ac08c32008-02-14 19:34:38 -08001113 if (!err)
1114 path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115set_it:
Andreas Mohre518ddb2006-09-29 02:01:22 -07001116 write_lock(&fs->lock);
Jan Blunck6ac08c32008-02-14 19:34:38 -08001117 old_path = fs->altroot;
1118 fs->altroot = path;
Andreas Mohre518ddb2006-09-29 02:01:22 -07001119 write_unlock(&fs->lock);
Jan Blunck6ac08c32008-02-14 19:34:38 -08001120 if (old_path.dentry)
1121 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122}
1123
Prasanna Medaea3834d2005-04-29 16:00:17 +01001124/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -08001125static int do_path_lookup(int dfd, const char *name,
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001126 unsigned int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
Prasanna Medaea3834d2005-04-29 16:00:17 +01001128 int retval = 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001129 int fput_needed;
1130 struct file *file;
Andreas Mohre518ddb2006-09-29 02:01:22 -07001131 struct fs_struct *fs = current->fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
1133 nd->last_type = LAST_ROOT; /* if there are only slashes... */
1134 nd->flags = flags;
1135 nd->depth = 0;
1136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 if (*name=='/') {
Andreas Mohre518ddb2006-09-29 02:01:22 -07001138 read_lock(&fs->lock);
Jan Blunck6ac08c32008-02-14 19:34:38 -08001139 if (fs->altroot.dentry && !(nd->flags & LOOKUP_NOALT)) {
1140 nd->path = fs->altroot;
1141 path_get(&fs->altroot);
Andreas Mohre518ddb2006-09-29 02:01:22 -07001142 read_unlock(&fs->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 if (__emul_lookup_dentry(name,nd))
Prasanna Medaea3834d2005-04-29 16:00:17 +01001144 goto out; /* found in altroot */
Andreas Mohre518ddb2006-09-29 02:01:22 -07001145 read_lock(&fs->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 }
Jan Blunck6ac08c32008-02-14 19:34:38 -08001147 nd->path = fs->root;
1148 path_get(&fs->root);
Andreas Mohre518ddb2006-09-29 02:01:22 -07001149 read_unlock(&fs->lock);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001150 } else if (dfd == AT_FDCWD) {
Andreas Mohre518ddb2006-09-29 02:01:22 -07001151 read_lock(&fs->lock);
Jan Blunck6ac08c32008-02-14 19:34:38 -08001152 nd->path = fs->pwd;
1153 path_get(&fs->pwd);
Andreas Mohre518ddb2006-09-29 02:01:22 -07001154 read_unlock(&fs->lock);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001155 } else {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001156 struct dentry *dentry;
1157
1158 file = fget_light(dfd, &fput_needed);
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001159 retval = -EBADF;
1160 if (!file)
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001161 goto out_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001162
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001163 dentry = file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001164
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001165 retval = -ENOTDIR;
1166 if (!S_ISDIR(dentry->d_inode->i_mode))
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001167 goto fput_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001168
1169 retval = file_permission(file, MAY_EXEC);
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001170 if (retval)
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001171 goto fput_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001172
Jan Blunck5dd784d2008-02-14 19:34:38 -08001173 nd->path = file->f_path;
1174 path_get(&file->f_path);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001175
1176 fput_light(file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 }
Josef 'Jeff' Sipek2dfdd262007-05-09 02:33:41 -07001178
1179 retval = path_walk(name, nd);
Prasanna Medaea3834d2005-04-29 16:00:17 +01001180out:
Jan Blunck4ac91372008-02-14 19:34:32 -08001181 if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry &&
1182 nd->path.dentry->d_inode))
1183 audit_inode(name, nd->path.dentry);
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001184out_fail:
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001185 return retval;
1186
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001187fput_fail:
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001188 fput_light(file, fput_needed);
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001189 goto out_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190}
1191
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -08001192int path_lookup(const char *name, unsigned int flags,
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001193 struct nameidata *nd)
1194{
1195 return do_path_lookup(AT_FDCWD, name, flags, nd);
1196}
1197
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001198/**
1199 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1200 * @dentry: pointer to dentry of the base directory
1201 * @mnt: pointer to vfs mount of the base directory
1202 * @name: pointer to file name
1203 * @flags: lookup flags
1204 * @nd: pointer to nameidata
1205 */
1206int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
1207 const char *name, unsigned int flags,
1208 struct nameidata *nd)
1209{
1210 int retval;
1211
1212 /* same as do_path_lookup */
1213 nd->last_type = LAST_ROOT;
1214 nd->flags = flags;
1215 nd->depth = 0;
1216
Jan Blunckc8e7f442008-06-09 16:40:35 -07001217 nd->path.dentry = dentry;
1218 nd->path.mnt = mnt;
1219 path_get(&nd->path);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001220
1221 retval = path_walk(name, nd);
Jan Blunck4ac91372008-02-14 19:34:32 -08001222 if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry &&
1223 nd->path.dentry->d_inode))
1224 audit_inode(name, nd->path.dentry);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001225
1226 return retval;
1227
1228}
1229
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001230static int __path_lookup_intent_open(int dfd, const char *name,
1231 unsigned int lookup_flags, struct nameidata *nd,
1232 int open_flags, int create_mode)
Trond Myklebust834f2a42005-10-18 14:20:16 -07001233{
1234 struct file *filp = get_empty_filp();
1235 int err;
1236
1237 if (filp == NULL)
1238 return -ENFILE;
1239 nd->intent.open.file = filp;
1240 nd->intent.open.flags = open_flags;
1241 nd->intent.open.create_mode = create_mode;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001242 err = do_path_lookup(dfd, name, lookup_flags|LOOKUP_OPEN, nd);
Trond Myklebust834f2a42005-10-18 14:20:16 -07001243 if (IS_ERR(nd->intent.open.file)) {
1244 if (err == 0) {
1245 err = PTR_ERR(nd->intent.open.file);
Jan Blunck1d957f92008-02-14 19:34:35 -08001246 path_put(&nd->path);
Trond Myklebust834f2a42005-10-18 14:20:16 -07001247 }
1248 } else if (err != 0)
1249 release_open_intent(nd);
1250 return err;
1251}
1252
1253/**
1254 * path_lookup_open - lookup a file path with open intent
Martin Waitz7045f372006-02-01 03:06:57 -08001255 * @dfd: the directory to use as base, or AT_FDCWD
Trond Myklebust834f2a42005-10-18 14:20:16 -07001256 * @name: pointer to file name
1257 * @lookup_flags: lookup intent flags
1258 * @nd: pointer to nameidata
1259 * @open_flags: open intent flags
1260 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001261int path_lookup_open(int dfd, const char *name, unsigned int lookup_flags,
Trond Myklebust834f2a42005-10-18 14:20:16 -07001262 struct nameidata *nd, int open_flags)
1263{
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001264 return __path_lookup_intent_open(dfd, name, lookup_flags, nd,
Trond Myklebust834f2a42005-10-18 14:20:16 -07001265 open_flags, 0);
1266}
1267
1268/**
1269 * path_lookup_create - lookup a file path with open + create intent
Martin Waitz7045f372006-02-01 03:06:57 -08001270 * @dfd: the directory to use as base, or AT_FDCWD
Trond Myklebust834f2a42005-10-18 14:20:16 -07001271 * @name: pointer to file name
1272 * @lookup_flags: lookup intent flags
1273 * @nd: pointer to nameidata
1274 * @open_flags: open intent flags
1275 * @create_mode: create intent flags
1276 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001277static int path_lookup_create(int dfd, const char *name,
1278 unsigned int lookup_flags, struct nameidata *nd,
1279 int open_flags, int create_mode)
Trond Myklebust834f2a42005-10-18 14:20:16 -07001280{
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001281 return __path_lookup_intent_open(dfd, name, lookup_flags|LOOKUP_CREATE,
1282 nd, open_flags, create_mode);
Trond Myklebust834f2a42005-10-18 14:20:16 -07001283}
1284
1285int __user_path_lookup_open(const char __user *name, unsigned int lookup_flags,
1286 struct nameidata *nd, int open_flags)
1287{
1288 char *tmp = getname(name);
1289 int err = PTR_ERR(tmp);
1290
1291 if (!IS_ERR(tmp)) {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001292 err = __path_lookup_intent_open(AT_FDCWD, tmp, lookup_flags, nd, open_flags, 0);
Trond Myklebust834f2a42005-10-18 14:20:16 -07001293 putname(tmp);
1294 }
1295 return err;
1296}
1297
Christoph Hellwigeead1912007-10-16 23:25:38 -07001298static struct dentry *__lookup_hash(struct qstr *name,
1299 struct dentry *base, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
James Morris057f6c02007-04-26 00:12:05 -07001301 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 struct inode *inode;
1303 int err;
1304
1305 inode = base->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 /*
1308 * See if the low-level filesystem might want
1309 * to use its own hash..
1310 */
1311 if (base->d_op && base->d_op->d_hash) {
1312 err = base->d_op->d_hash(base, name);
1313 dentry = ERR_PTR(err);
1314 if (err < 0)
1315 goto out;
1316 }
1317
1318 dentry = cached_lookup(base, name, nd);
1319 if (!dentry) {
1320 struct dentry *new = d_alloc(base, name);
1321 dentry = ERR_PTR(-ENOMEM);
1322 if (!new)
1323 goto out;
1324 dentry = inode->i_op->lookup(inode, new, nd);
1325 if (!dentry)
1326 dentry = new;
1327 else
1328 dput(new);
1329 }
1330out:
1331 return dentry;
1332}
1333
James Morris057f6c02007-04-26 00:12:05 -07001334/*
1335 * Restricted form of lookup. Doesn't follow links, single-component only,
1336 * needs parent already locked. Doesn't follow mounts.
1337 * SMP-safe.
1338 */
Adrian Bunka244e162006-03-31 02:32:11 -08001339static struct dentry *lookup_hash(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
Christoph Hellwigeead1912007-10-16 23:25:38 -07001341 int err;
1342
Jan Blunck4ac91372008-02-14 19:34:32 -08001343 err = permission(nd->path.dentry->d_inode, MAY_EXEC, nd);
Christoph Hellwigeead1912007-10-16 23:25:38 -07001344 if (err)
1345 return ERR_PTR(err);
Jan Blunck4ac91372008-02-14 19:34:32 -08001346 return __lookup_hash(&nd->last, nd->path.dentry, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347}
1348
Christoph Hellwigeead1912007-10-16 23:25:38 -07001349static int __lookup_one_len(const char *name, struct qstr *this,
1350 struct dentry *base, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
1352 unsigned long hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 unsigned int c;
1354
James Morris057f6c02007-04-26 00:12:05 -07001355 this->name = name;
1356 this->len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 if (!len)
James Morris057f6c02007-04-26 00:12:05 -07001358 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 hash = init_name_hash();
1361 while (len--) {
1362 c = *(const unsigned char *)name++;
1363 if (c == '/' || c == '\0')
James Morris057f6c02007-04-26 00:12:05 -07001364 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 hash = partial_name_hash(c, hash);
1366 }
James Morris057f6c02007-04-26 00:12:05 -07001367 this->hash = end_name_hash(hash);
1368 return 0;
1369}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Christoph Hellwigeead1912007-10-16 23:25:38 -07001371/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07001372 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07001373 * @name: pathname component to lookup
1374 * @base: base directory to lookup from
1375 * @len: maximum length @len should be interpreted to
1376 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07001377 * Note that this routine is purely a helper for filesystem usage and should
1378 * not be called by generic code. Also note that by using this function the
Christoph Hellwigeead1912007-10-16 23:25:38 -07001379 * nameidata argument is passed to the filesystem methods and a filesystem
1380 * using this helper needs to be prepared for that.
1381 */
James Morris057f6c02007-04-26 00:12:05 -07001382struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1383{
1384 int err;
1385 struct qstr this;
1386
1387 err = __lookup_one_len(name, &this, base, len);
1388 if (err)
1389 return ERR_PTR(err);
Christoph Hellwigeead1912007-10-16 23:25:38 -07001390
1391 err = permission(base->d_inode, MAY_EXEC, NULL);
1392 if (err)
1393 return ERR_PTR(err);
Christoph Hellwig49705b72005-11-08 21:35:06 -08001394 return __lookup_hash(&this, base, NULL);
James Morris057f6c02007-04-26 00:12:05 -07001395}
1396
Christoph Hellwigeead1912007-10-16 23:25:38 -07001397/**
1398 * lookup_one_noperm - bad hack for sysfs
1399 * @name: pathname component to lookup
1400 * @base: base directory to lookup from
1401 *
1402 * This is a variant of lookup_one_len that doesn't perform any permission
1403 * checks. It's a horrible hack to work around the braindead sysfs
1404 * architecture and should not be used anywhere else.
1405 *
1406 * DON'T USE THIS FUNCTION EVER, thanks.
1407 */
1408struct dentry *lookup_one_noperm(const char *name, struct dentry *base)
James Morris057f6c02007-04-26 00:12:05 -07001409{
1410 int err;
1411 struct qstr this;
1412
Christoph Hellwigeead1912007-10-16 23:25:38 -07001413 err = __lookup_one_len(name, &this, base, strlen(name));
James Morris057f6c02007-04-26 00:12:05 -07001414 if (err)
1415 return ERR_PTR(err);
Christoph Hellwigeead1912007-10-16 23:25:38 -07001416 return __lookup_hash(&this, base, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417}
1418
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -08001419int __user_walk_fd(int dfd, const char __user *name, unsigned flags,
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001420 struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421{
1422 char *tmp = getname(name);
1423 int err = PTR_ERR(tmp);
1424
1425 if (!IS_ERR(tmp)) {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001426 err = do_path_lookup(dfd, tmp, flags, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 putname(tmp);
1428 }
1429 return err;
1430}
1431
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -08001432int __user_walk(const char __user *name, unsigned flags, struct nameidata *nd)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001433{
1434 return __user_walk_fd(AT_FDCWD, name, flags, nd);
1435}
1436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437/*
1438 * It's inline, so penalty for filesystems that don't use sticky bit is
1439 * minimal.
1440 */
1441static inline int check_sticky(struct inode *dir, struct inode *inode)
1442{
1443 if (!(dir->i_mode & S_ISVTX))
1444 return 0;
1445 if (inode->i_uid == current->fsuid)
1446 return 0;
1447 if (dir->i_uid == current->fsuid)
1448 return 0;
1449 return !capable(CAP_FOWNER);
1450}
1451
1452/*
1453 * Check whether we can remove a link victim from directory dir, check
1454 * whether the type of victim is right.
1455 * 1. We can't do it if dir is read-only (done in permission())
1456 * 2. We should have write and exec permissions on dir
1457 * 3. We can't remove anything from append-only dir
1458 * 4. We can't do anything with immutable dir (done in permission())
1459 * 5. If the sticky bit on dir is set we should either
1460 * a. be owner of dir, or
1461 * b. be owner of victim, or
1462 * c. have CAP_FOWNER capability
1463 * 6. If the victim is append-only or immutable we can't do antyhing with
1464 * links pointing to it.
1465 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1466 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1467 * 9. We can't remove a root or mountpoint.
1468 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1469 * nfs_async_unlink().
1470 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001471static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472{
1473 int error;
1474
1475 if (!victim->d_inode)
1476 return -ENOENT;
1477
1478 BUG_ON(victim->d_parent->d_inode != dir);
Al Viro5a190ae2007-06-07 12:19:32 -04001479 audit_inode_child(victim->d_name.name, victim, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
1481 error = permission(dir,MAY_WRITE | MAY_EXEC, NULL);
1482 if (error)
1483 return error;
1484 if (IS_APPEND(dir))
1485 return -EPERM;
1486 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1487 IS_IMMUTABLE(victim->d_inode))
1488 return -EPERM;
1489 if (isdir) {
1490 if (!S_ISDIR(victim->d_inode->i_mode))
1491 return -ENOTDIR;
1492 if (IS_ROOT(victim))
1493 return -EBUSY;
1494 } else if (S_ISDIR(victim->d_inode->i_mode))
1495 return -EISDIR;
1496 if (IS_DEADDIR(dir))
1497 return -ENOENT;
1498 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1499 return -EBUSY;
1500 return 0;
1501}
1502
1503/* Check whether we can create an object with dentry child in directory
1504 * dir.
1505 * 1. We can't do it if child already exists (open has special treatment for
1506 * this case, but since we are inlined it's OK)
1507 * 2. We can't do it if dir is read-only (done in permission())
1508 * 3. We should have write and exec permissions on dir
1509 * 4. We can't do it if dir is immutable (done in permission())
1510 */
1511static inline int may_create(struct inode *dir, struct dentry *child,
1512 struct nameidata *nd)
1513{
1514 if (child->d_inode)
1515 return -EEXIST;
1516 if (IS_DEADDIR(dir))
1517 return -ENOENT;
1518 return permission(dir,MAY_WRITE | MAY_EXEC, nd);
1519}
1520
1521/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 * O_DIRECTORY translates into forcing a directory lookup.
1523 */
1524static inline int lookup_flags(unsigned int f)
1525{
1526 unsigned long retval = LOOKUP_FOLLOW;
1527
1528 if (f & O_NOFOLLOW)
1529 retval &= ~LOOKUP_FOLLOW;
1530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 if (f & O_DIRECTORY)
1532 retval |= LOOKUP_DIRECTORY;
1533
1534 return retval;
1535}
1536
1537/*
1538 * p1 and p2 should be directories on the same fs.
1539 */
1540struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1541{
1542 struct dentry *p;
1543
1544 if (p1 == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07001545 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 return NULL;
1547 }
1548
Arjan van de Vena11f3a02006-03-23 03:00:33 -08001549 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
1551 for (p = p1; p->d_parent != p; p = p->d_parent) {
1552 if (p->d_parent == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07001553 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1554 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 return p;
1556 }
1557 }
1558
1559 for (p = p2; p->d_parent != p; p = p->d_parent) {
1560 if (p->d_parent == p1) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07001561 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1562 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 return p;
1564 }
1565 }
1566
Ingo Molnarf2eace22006-07-03 00:25:05 -07001567 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1568 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 return NULL;
1570}
1571
1572void unlock_rename(struct dentry *p1, struct dentry *p2)
1573{
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001574 mutex_unlock(&p1->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 if (p1 != p2) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001576 mutex_unlock(&p2->d_inode->i_mutex);
Arjan van de Vena11f3a02006-03-23 03:00:33 -08001577 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 }
1579}
1580
1581int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1582 struct nameidata *nd)
1583{
1584 int error = may_create(dir, dentry, nd);
1585
1586 if (error)
1587 return error;
1588
1589 if (!dir->i_op || !dir->i_op->create)
1590 return -EACCES; /* shouldn't it be ENOSYS? */
1591 mode &= S_IALLUGO;
1592 mode |= S_IFREG;
1593 error = security_inode_create(dir, dentry, mode);
1594 if (error)
1595 return error;
1596 DQUOT_INIT(dir);
1597 error = dir->i_op->create(dir, dentry, mode, nd);
Stephen Smalleya74574a2005-09-09 13:01:44 -07001598 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00001599 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 return error;
1601}
1602
1603int may_open(struct nameidata *nd, int acc_mode, int flag)
1604{
Jan Blunck4ac91372008-02-14 19:34:32 -08001605 struct dentry *dentry = nd->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 struct inode *inode = dentry->d_inode;
1607 int error;
1608
1609 if (!inode)
1610 return -ENOENT;
1611
1612 if (S_ISLNK(inode->i_mode))
1613 return -ELOOP;
1614
Linus Torvalds974a9f02008-01-12 14:06:34 -08001615 if (S_ISDIR(inode->i_mode) && (acc_mode & MAY_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 return -EISDIR;
1617
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 /*
1619 * FIFO's, sockets and device files are special: they don't
1620 * actually live on the filesystem itself, and as such you
1621 * can write to them even if the filesystem is read-only.
1622 */
1623 if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
1624 flag &= ~O_TRUNC;
1625 } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
Jan Blunck4ac91372008-02-14 19:34:32 -08001626 if (nd->path.mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 return -EACCES;
1628
1629 flag &= ~O_TRUNC;
Dave Hansen4a3fd212008-02-15 14:37:48 -08001630 }
Dave Hansenb41572e2007-10-16 23:31:14 -07001631
1632 error = vfs_permission(nd, acc_mode);
1633 if (error)
1634 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 /*
1636 * An append-only file must be opened in append mode for writing.
1637 */
1638 if (IS_APPEND(inode)) {
1639 if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
1640 return -EPERM;
1641 if (flag & O_TRUNC)
1642 return -EPERM;
1643 }
1644
1645 /* O_NOATIME can only be set by the owner or superuser */
1646 if (flag & O_NOATIME)
Satyam Sharma3bd858a2007-07-17 15:00:08 +05301647 if (!is_owner_or_cap(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 return -EPERM;
1649
1650 /*
1651 * Ensure there are no outstanding leases on the file.
1652 */
1653 error = break_lease(inode, flag);
1654 if (error)
1655 return error;
1656
1657 if (flag & O_TRUNC) {
1658 error = get_write_access(inode);
1659 if (error)
1660 return error;
1661
1662 /*
1663 * Refuse to truncate files with mandatory locks held on them.
1664 */
1665 error = locks_verify_locked(inode);
1666 if (!error) {
1667 DQUOT_INIT(inode);
Miklos Szeredid139d7f2007-10-18 03:07:00 -07001668
1669 error = do_truncate(dentry, 0,
1670 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
1671 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 }
1673 put_write_access(inode);
1674 if (error)
1675 return error;
1676 } else
1677 if (flag & FMODE_WRITE)
1678 DQUOT_INIT(inode);
1679
1680 return 0;
1681}
1682
Dave Hansend57999e2008-02-15 14:37:27 -08001683/*
1684 * Be careful about ever adding any more callers of this
1685 * function. Its flags must be in the namei format, not
1686 * what get passed to sys_open().
1687 */
1688static int __open_namei_create(struct nameidata *nd, struct path *path,
Dave Hansenaab520e2006-09-30 23:29:02 -07001689 int flag, int mode)
1690{
1691 int error;
Jan Blunck4ac91372008-02-14 19:34:32 -08001692 struct dentry *dir = nd->path.dentry;
Dave Hansenaab520e2006-09-30 23:29:02 -07001693
1694 if (!IS_POSIXACL(dir->d_inode))
1695 mode &= ~current->fs->umask;
1696 error = vfs_create(dir->d_inode, path->dentry, mode, nd);
1697 mutex_unlock(&dir->d_inode->i_mutex);
Jan Blunck4ac91372008-02-14 19:34:32 -08001698 dput(nd->path.dentry);
1699 nd->path.dentry = path->dentry;
Dave Hansenaab520e2006-09-30 23:29:02 -07001700 if (error)
1701 return error;
1702 /* Don't check for write permission, don't truncate */
1703 return may_open(nd, 0, flag & ~O_TRUNC);
1704}
1705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706/*
Dave Hansend57999e2008-02-15 14:37:27 -08001707 * Note that while the flag value (low two bits) for sys_open means:
1708 * 00 - read-only
1709 * 01 - write-only
1710 * 10 - read-write
1711 * 11 - special
1712 * it is changed into
1713 * 00 - no permissions needed
1714 * 01 - read-permission
1715 * 10 - write-permission
1716 * 11 - read-write
1717 * for the internal routines (ie open_namei()/follow_link() etc)
1718 * This is more logical, and also allows the 00 "no perm needed"
1719 * to be used for symlinks (where the permissions are checked
1720 * later).
1721 *
1722*/
1723static inline int open_to_namei_flags(int flag)
1724{
1725 if ((flag+1) & O_ACCMODE)
1726 flag++;
1727 return flag;
1728}
1729
Dave Hansen4a3fd212008-02-15 14:37:48 -08001730static int open_will_write_to_fs(int flag, struct inode *inode)
1731{
1732 /*
1733 * We'll never write to the fs underlying
1734 * a device file.
1735 */
1736 if (special_file(inode->i_mode))
1737 return 0;
1738 return (flag & O_TRUNC);
1739}
1740
Dave Hansend57999e2008-02-15 14:37:27 -08001741/*
Dave Hansen4a3fd212008-02-15 14:37:48 -08001742 * Note that the low bits of the passed in "open_flag"
1743 * are not the same as in the local variable "flag". See
1744 * open_to_namei_flags() for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 */
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001746struct file *do_filp_open(int dfd, const char *pathname,
1747 int open_flag, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748{
Dave Hansen4a3fd212008-02-15 14:37:48 -08001749 struct file *filp;
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001750 struct nameidata nd;
Trond Myklebust834f2a42005-10-18 14:20:16 -07001751 int acc_mode, error;
Al Viro4e7506e2005-06-06 13:36:00 -07001752 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 struct dentry *dir;
1754 int count = 0;
Dave Hansen4a3fd212008-02-15 14:37:48 -08001755 int will_write;
Dave Hansend57999e2008-02-15 14:37:27 -08001756 int flag = open_to_namei_flags(open_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
1758 acc_mode = ACC_MODE(flag);
1759
Trond Myklebust834f2a42005-10-18 14:20:16 -07001760 /* O_TRUNC implies we need access checks for write permissions */
1761 if (flag & O_TRUNC)
1762 acc_mode |= MAY_WRITE;
1763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 /* Allow the LSM permission hook to distinguish append
1765 access from general write access. */
1766 if (flag & O_APPEND)
1767 acc_mode |= MAY_APPEND;
1768
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 /*
1770 * The simplest case - just a plain lookup.
1771 */
1772 if (!(flag & O_CREAT)) {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001773 error = path_lookup_open(dfd, pathname, lookup_flags(flag),
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001774 &nd, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 if (error)
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001776 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 goto ok;
1778 }
1779
1780 /*
1781 * Create - we need to know the parent.
1782 */
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001783 error = path_lookup_create(dfd, pathname, LOOKUP_PARENT,
1784 &nd, flag, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 if (error)
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001786 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
1788 /*
1789 * We have the parent and last component. First of all, check
1790 * that we are not asked to creat(2) an obvious directory - that
1791 * will not do.
1792 */
1793 error = -EISDIR;
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001794 if (nd.last_type != LAST_NORM || nd.last.name[nd.last.len])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 goto exit;
1796
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001797 dir = nd.path.dentry;
1798 nd.flags &= ~LOOKUP_PARENT;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001799 mutex_lock(&dir->d_inode->i_mutex);
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001800 path.dentry = lookup_hash(&nd);
1801 path.mnt = nd.path.mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
1803do_last:
Al Viro4e7506e2005-06-06 13:36:00 -07001804 error = PTR_ERR(path.dentry);
1805 if (IS_ERR(path.dentry)) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001806 mutex_unlock(&dir->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 goto exit;
1808 }
1809
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001810 if (IS_ERR(nd.intent.open.file)) {
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001811 error = PTR_ERR(nd.intent.open.file);
Dave Hansen4a3fd212008-02-15 14:37:48 -08001812 goto exit_mutex_unlock;
Oleg Drokin4af4c522006-03-25 03:06:54 -08001813 }
1814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 /* Negative dentry, just create the file */
Al Viro4e7506e2005-06-06 13:36:00 -07001816 if (!path.dentry->d_inode) {
Dave Hansen4a3fd212008-02-15 14:37:48 -08001817 /*
1818 * This write is needed to ensure that a
1819 * ro->rw transition does not occur between
1820 * the time when the file is created and when
1821 * a permanent write count is taken through
1822 * the 'struct file' in nameidata_to_filp().
1823 */
1824 error = mnt_want_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 if (error)
Dave Hansen4a3fd212008-02-15 14:37:48 -08001826 goto exit_mutex_unlock;
1827 error = __open_namei_create(&nd, &path, flag, mode);
1828 if (error) {
1829 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 goto exit;
Dave Hansen4a3fd212008-02-15 14:37:48 -08001831 }
1832 filp = nameidata_to_filp(&nd, open_flag);
1833 mnt_drop_write(nd.path.mnt);
1834 return filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 }
1836
1837 /*
1838 * It already exists.
1839 */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001840 mutex_unlock(&dir->d_inode->i_mutex);
Al Viro5a190ae2007-06-07 12:19:32 -04001841 audit_inode(pathname, path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
1843 error = -EEXIST;
1844 if (flag & O_EXCL)
1845 goto exit_dput;
1846
Al Viroe13b2102005-06-06 13:36:06 -07001847 if (__follow_mount(&path)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 error = -ELOOP;
Al Viroba7a4c12005-06-06 13:36:08 -07001849 if (flag & O_NOFOLLOW)
1850 goto exit_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 }
Amy Griffis3e2efce2006-07-13 13:16:02 -04001852
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 error = -ENOENT;
Al Viro4e7506e2005-06-06 13:36:00 -07001854 if (!path.dentry->d_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 goto exit_dput;
Al Viro4e7506e2005-06-06 13:36:00 -07001856 if (path.dentry->d_inode->i_op && path.dentry->d_inode->i_op->follow_link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 goto do_link;
1858
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001859 path_to_nameidata(&path, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 error = -EISDIR;
Al Viro4e7506e2005-06-06 13:36:00 -07001861 if (path.dentry->d_inode && S_ISDIR(path.dentry->d_inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 goto exit;
1863ok:
Dave Hansen4a3fd212008-02-15 14:37:48 -08001864 /*
1865 * Consider:
1866 * 1. may_open() truncates a file
1867 * 2. a rw->ro mount transition occurs
1868 * 3. nameidata_to_filp() fails due to
1869 * the ro mount.
1870 * That would be inconsistent, and should
1871 * be avoided. Taking this mnt write here
1872 * ensures that (2) can not occur.
1873 */
1874 will_write = open_will_write_to_fs(flag, nd.path.dentry->d_inode);
1875 if (will_write) {
1876 error = mnt_want_write(nd.path.mnt);
1877 if (error)
1878 goto exit;
1879 }
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001880 error = may_open(&nd, acc_mode, flag);
Dave Hansen4a3fd212008-02-15 14:37:48 -08001881 if (error) {
1882 if (will_write)
1883 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 goto exit;
Dave Hansen4a3fd212008-02-15 14:37:48 -08001885 }
1886 filp = nameidata_to_filp(&nd, open_flag);
1887 /*
1888 * It is now safe to drop the mnt write
1889 * because the filp has had a write taken
1890 * on its behalf.
1891 */
1892 if (will_write)
1893 mnt_drop_write(nd.path.mnt);
1894 return filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Dave Hansen4a3fd212008-02-15 14:37:48 -08001896exit_mutex_unlock:
1897 mutex_unlock(&dir->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898exit_dput:
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001899 path_put_conditional(&path, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900exit:
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001901 if (!IS_ERR(nd.intent.open.file))
1902 release_open_intent(&nd);
1903 path_put(&nd.path);
1904 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
1906do_link:
1907 error = -ELOOP;
1908 if (flag & O_NOFOLLOW)
1909 goto exit_dput;
1910 /*
1911 * This is subtle. Instead of calling do_follow_link() we do the
1912 * thing by hands. The reason is that this way we have zero link_count
1913 * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1914 * After that we have the parent and last component, i.e.
1915 * we are in the same situation as after the first path_walk().
1916 * Well, almost - if the last component is normal we get its copy
1917 * stored in nd->last.name and we will have to putname() it when we
1918 * are done. Procfs-like symlinks just set LAST_BIND.
1919 */
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001920 nd.flags |= LOOKUP_PARENT;
1921 error = security_inode_follow_link(path.dentry, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 if (error)
1923 goto exit_dput;
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001924 error = __do_follow_link(&path, &nd);
Kirill Korotaevde459212006-07-14 00:23:49 -07001925 if (error) {
1926 /* Does someone understand code flow here? Or it is only
1927 * me so stupid? Anathema to whoever designed this non-sense
1928 * with "intent.open".
1929 */
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001930 release_open_intent(&nd);
1931 return ERR_PTR(error);
Kirill Korotaevde459212006-07-14 00:23:49 -07001932 }
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001933 nd.flags &= ~LOOKUP_PARENT;
1934 if (nd.last_type == LAST_BIND)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 goto ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 error = -EISDIR;
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001937 if (nd.last_type != LAST_NORM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 goto exit;
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001939 if (nd.last.name[nd.last.len]) {
1940 __putname(nd.last.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 goto exit;
1942 }
1943 error = -ELOOP;
1944 if (count++==32) {
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001945 __putname(nd.last.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 goto exit;
1947 }
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001948 dir = nd.path.dentry;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001949 mutex_lock(&dir->d_inode->i_mutex);
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001950 path.dentry = lookup_hash(&nd);
1951 path.mnt = nd.path.mnt;
1952 __putname(nd.last.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 goto do_last;
1954}
1955
1956/**
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001957 * filp_open - open file and return file pointer
1958 *
1959 * @filename: path to open
1960 * @flags: open flags as per the open(2) second argument
1961 * @mode: mode for the new file if O_CREAT is set, else ignored
1962 *
1963 * This is the helper to open a file from kernelspace if you really
1964 * have to. But in generally you should not do this, so please move
1965 * along, nothing to see here..
1966 */
1967struct file *filp_open(const char *filename, int flags, int mode)
1968{
1969 return do_filp_open(AT_FDCWD, filename, flags, mode);
1970}
1971EXPORT_SYMBOL(filp_open);
1972
1973/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 * lookup_create - lookup a dentry, creating it if it doesn't exist
1975 * @nd: nameidata info
1976 * @is_dir: directory flag
1977 *
1978 * Simple function to lookup and return a dentry and create it
1979 * if it doesn't exist. Is SMP-safe.
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07001980 *
Jan Blunck4ac91372008-02-14 19:34:32 -08001981 * Returns with nd->path.dentry->d_inode->i_mutex locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 */
1983struct dentry *lookup_create(struct nameidata *nd, int is_dir)
1984{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07001985 struct dentry *dentry = ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
Jan Blunck4ac91372008-02-14 19:34:32 -08001987 mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07001988 /*
1989 * Yucky last component or no last component at all?
1990 * (foo/., foo/.., /////)
1991 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 if (nd->last_type != LAST_NORM)
1993 goto fail;
1994 nd->flags &= ~LOOKUP_PARENT;
ASANO Masahiroa6349042006-08-22 20:06:02 -04001995 nd->flags |= LOOKUP_CREATE;
1996 nd->intent.open.flags = O_EXCL;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07001997
1998 /*
1999 * Do the final lookup.
2000 */
Christoph Hellwig49705b72005-11-08 21:35:06 -08002001 dentry = lookup_hash(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 if (IS_ERR(dentry))
2003 goto fail;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002004
Al Viroe9baf6e2008-05-15 04:49:12 -04002005 if (dentry->d_inode)
2006 goto eexist;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002007 /*
2008 * Special case - lookup gave negative, but... we had foo/bar/
2009 * From the vfs_mknod() POV we just have a negative dentry -
2010 * all is fine. Let's be bastards - you had / on the end, you've
2011 * been asking for (non-existent) directory. -ENOENT for you.
2012 */
Al Viroe9baf6e2008-05-15 04:49:12 -04002013 if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
2014 dput(dentry);
2015 dentry = ERR_PTR(-ENOENT);
2016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 return dentry;
Al Viroe9baf6e2008-05-15 04:49:12 -04002018eexist:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 dput(dentry);
Al Viroe9baf6e2008-05-15 04:49:12 -04002020 dentry = ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021fail:
2022 return dentry;
2023}
Christoph Hellwigf81a0bf2005-05-19 12:26:43 -07002024EXPORT_SYMBOL_GPL(lookup_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
2026int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
2027{
2028 int error = may_create(dir, dentry, NULL);
2029
2030 if (error)
2031 return error;
2032
2033 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
2034 return -EPERM;
2035
2036 if (!dir->i_op || !dir->i_op->mknod)
2037 return -EPERM;
2038
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07002039 error = devcgroup_inode_mknod(mode, dev);
2040 if (error)
2041 return error;
2042
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 error = security_inode_mknod(dir, dentry, mode, dev);
2044 if (error)
2045 return error;
2046
2047 DQUOT_INIT(dir);
2048 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002049 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002050 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 return error;
2052}
2053
Dave Hansen463c3192008-02-15 14:37:57 -08002054static int may_mknod(mode_t mode)
2055{
2056 switch (mode & S_IFMT) {
2057 case S_IFREG:
2058 case S_IFCHR:
2059 case S_IFBLK:
2060 case S_IFIFO:
2061 case S_IFSOCK:
2062 case 0: /* zero mode translates to S_IFREG */
2063 return 0;
2064 case S_IFDIR:
2065 return -EPERM;
2066 default:
2067 return -EINVAL;
2068 }
2069}
2070
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002071asmlinkage long sys_mknodat(int dfd, const char __user *filename, int mode,
2072 unsigned dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073{
2074 int error = 0;
2075 char * tmp;
2076 struct dentry * dentry;
2077 struct nameidata nd;
2078
2079 if (S_ISDIR(mode))
2080 return -EPERM;
2081 tmp = getname(filename);
2082 if (IS_ERR(tmp))
2083 return PTR_ERR(tmp);
2084
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002085 error = do_path_lookup(dfd, tmp, LOOKUP_PARENT, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 if (error)
2087 goto out;
2088 dentry = lookup_create(&nd, 0);
Dave Hansen463c3192008-02-15 14:37:57 -08002089 if (IS_ERR(dentry)) {
2090 error = PTR_ERR(dentry);
2091 goto out_unlock;
2092 }
Jan Blunck4ac91372008-02-14 19:34:32 -08002093 if (!IS_POSIXACL(nd.path.dentry->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 mode &= ~current->fs->umask;
Dave Hansen463c3192008-02-15 14:37:57 -08002095 error = may_mknod(mode);
2096 if (error)
2097 goto out_dput;
2098 error = mnt_want_write(nd.path.mnt);
2099 if (error)
2100 goto out_dput;
2101 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 case 0: case S_IFREG:
Jan Blunck4ac91372008-02-14 19:34:32 -08002103 error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 break;
2105 case S_IFCHR: case S_IFBLK:
Jan Blunck4ac91372008-02-14 19:34:32 -08002106 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 new_decode_dev(dev));
2108 break;
2109 case S_IFIFO: case S_IFSOCK:
Jan Blunck4ac91372008-02-14 19:34:32 -08002110 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 }
Dave Hansen463c3192008-02-15 14:37:57 -08002113 mnt_drop_write(nd.path.mnt);
2114out_dput:
2115 dput(dentry);
2116out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002117 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002118 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119out:
2120 putname(tmp);
2121
2122 return error;
2123}
2124
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002125asmlinkage long sys_mknod(const char __user *filename, int mode, unsigned dev)
2126{
2127 return sys_mknodat(AT_FDCWD, filename, mode, dev);
2128}
2129
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2131{
2132 int error = may_create(dir, dentry, NULL);
2133
2134 if (error)
2135 return error;
2136
2137 if (!dir->i_op || !dir->i_op->mkdir)
2138 return -EPERM;
2139
2140 mode &= (S_IRWXUGO|S_ISVTX);
2141 error = security_inode_mkdir(dir, dentry, mode);
2142 if (error)
2143 return error;
2144
2145 DQUOT_INIT(dir);
2146 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002147 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002148 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 return error;
2150}
2151
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002152asmlinkage long sys_mkdirat(int dfd, const char __user *pathname, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153{
2154 int error = 0;
2155 char * tmp;
Dave Hansen6902d922006-09-30 23:29:01 -07002156 struct dentry *dentry;
2157 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
2159 tmp = getname(pathname);
2160 error = PTR_ERR(tmp);
Dave Hansen6902d922006-09-30 23:29:01 -07002161 if (IS_ERR(tmp))
2162 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
Dave Hansen6902d922006-09-30 23:29:01 -07002164 error = do_path_lookup(dfd, tmp, LOOKUP_PARENT, &nd);
2165 if (error)
2166 goto out;
2167 dentry = lookup_create(&nd, 1);
2168 error = PTR_ERR(dentry);
2169 if (IS_ERR(dentry))
2170 goto out_unlock;
2171
Jan Blunck4ac91372008-02-14 19:34:32 -08002172 if (!IS_POSIXACL(nd.path.dentry->d_inode))
Dave Hansen6902d922006-09-30 23:29:01 -07002173 mode &= ~current->fs->umask;
Dave Hansen463c3192008-02-15 14:37:57 -08002174 error = mnt_want_write(nd.path.mnt);
2175 if (error)
2176 goto out_dput;
Jan Blunck4ac91372008-02-14 19:34:32 -08002177 error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
Dave Hansen463c3192008-02-15 14:37:57 -08002178 mnt_drop_write(nd.path.mnt);
2179out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002180 dput(dentry);
2181out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002182 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002183 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184out:
Dave Hansen6902d922006-09-30 23:29:01 -07002185 putname(tmp);
2186out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 return error;
2188}
2189
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002190asmlinkage long sys_mkdir(const char __user *pathname, int mode)
2191{
2192 return sys_mkdirat(AT_FDCWD, pathname, mode);
2193}
2194
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195/*
2196 * We try to drop the dentry early: we should have
2197 * a usage count of 2 if we're the only user of this
2198 * dentry, and if that is true (possibly after pruning
2199 * the dcache), then we drop the dentry now.
2200 *
2201 * A low-level filesystem can, if it choses, legally
2202 * do a
2203 *
2204 * if (!d_unhashed(dentry))
2205 * return -EBUSY;
2206 *
2207 * if it cannot handle the case of removing a directory
2208 * that is still in use by something else..
2209 */
2210void dentry_unhash(struct dentry *dentry)
2211{
2212 dget(dentry);
Vasily Averindc168422006-12-06 20:37:07 -08002213 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 spin_lock(&dcache_lock);
2215 spin_lock(&dentry->d_lock);
2216 if (atomic_read(&dentry->d_count) == 2)
2217 __d_drop(dentry);
2218 spin_unlock(&dentry->d_lock);
2219 spin_unlock(&dcache_lock);
2220}
2221
2222int vfs_rmdir(struct inode *dir, struct dentry *dentry)
2223{
2224 int error = may_delete(dir, dentry, 1);
2225
2226 if (error)
2227 return error;
2228
2229 if (!dir->i_op || !dir->i_op->rmdir)
2230 return -EPERM;
2231
2232 DQUOT_INIT(dir);
2233
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002234 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 dentry_unhash(dentry);
2236 if (d_mountpoint(dentry))
2237 error = -EBUSY;
2238 else {
2239 error = security_inode_rmdir(dir, dentry);
2240 if (!error) {
2241 error = dir->i_op->rmdir(dir, dentry);
2242 if (!error)
2243 dentry->d_inode->i_flags |= S_DEAD;
2244 }
2245 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002246 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 d_delete(dentry);
2249 }
2250 dput(dentry);
2251
2252 return error;
2253}
2254
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002255static long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256{
2257 int error = 0;
2258 char * name;
2259 struct dentry *dentry;
2260 struct nameidata nd;
2261
2262 name = getname(pathname);
2263 if(IS_ERR(name))
2264 return PTR_ERR(name);
2265
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002266 error = do_path_lookup(dfd, name, LOOKUP_PARENT, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 if (error)
2268 goto exit;
2269
2270 switch(nd.last_type) {
2271 case LAST_DOTDOT:
2272 error = -ENOTEMPTY;
2273 goto exit1;
2274 case LAST_DOT:
2275 error = -EINVAL;
2276 goto exit1;
2277 case LAST_ROOT:
2278 error = -EBUSY;
2279 goto exit1;
2280 }
Jan Blunck4ac91372008-02-14 19:34:32 -08002281 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002282 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002284 if (IS_ERR(dentry))
2285 goto exit2;
Dave Hansen06227532008-02-15 14:37:34 -08002286 error = mnt_want_write(nd.path.mnt);
2287 if (error)
2288 goto exit3;
Jan Blunck4ac91372008-02-14 19:34:32 -08002289 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
Dave Hansen06227532008-02-15 14:37:34 -08002290 mnt_drop_write(nd.path.mnt);
2291exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07002292 dput(dentry);
2293exit2:
Jan Blunck4ac91372008-02-14 19:34:32 -08002294 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002296 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297exit:
2298 putname(name);
2299 return error;
2300}
2301
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002302asmlinkage long sys_rmdir(const char __user *pathname)
2303{
2304 return do_rmdir(AT_FDCWD, pathname);
2305}
2306
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307int vfs_unlink(struct inode *dir, struct dentry *dentry)
2308{
2309 int error = may_delete(dir, dentry, 0);
2310
2311 if (error)
2312 return error;
2313
2314 if (!dir->i_op || !dir->i_op->unlink)
2315 return -EPERM;
2316
2317 DQUOT_INIT(dir);
2318
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002319 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 if (d_mountpoint(dentry))
2321 error = -EBUSY;
2322 else {
2323 error = security_inode_unlink(dir, dentry);
2324 if (!error)
2325 error = dir->i_op->unlink(dir, dentry);
2326 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002327 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
2329 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2330 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
Jan Karaece95912008-02-06 01:37:13 -08002331 fsnotify_link_count(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 }
Robert Love0eeca282005-07-12 17:06:03 -04002334
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 return error;
2336}
2337
2338/*
2339 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002340 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 * writeout happening, and we don't want to prevent access to the directory
2342 * while waiting on the I/O.
2343 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002344static long do_unlinkat(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345{
2346 int error = 0;
2347 char * name;
2348 struct dentry *dentry;
2349 struct nameidata nd;
2350 struct inode *inode = NULL;
2351
2352 name = getname(pathname);
2353 if(IS_ERR(name))
2354 return PTR_ERR(name);
2355
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002356 error = do_path_lookup(dfd, name, LOOKUP_PARENT, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 if (error)
2358 goto exit;
2359 error = -EISDIR;
2360 if (nd.last_type != LAST_NORM)
2361 goto exit1;
Jan Blunck4ac91372008-02-14 19:34:32 -08002362 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002363 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 error = PTR_ERR(dentry);
2365 if (!IS_ERR(dentry)) {
2366 /* Why not before? Because we want correct error value */
2367 if (nd.last.name[nd.last.len])
2368 goto slashes;
2369 inode = dentry->d_inode;
2370 if (inode)
2371 atomic_inc(&inode->i_count);
Dave Hansen06227532008-02-15 14:37:34 -08002372 error = mnt_want_write(nd.path.mnt);
2373 if (error)
2374 goto exit2;
Jan Blunck4ac91372008-02-14 19:34:32 -08002375 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
Dave Hansen06227532008-02-15 14:37:34 -08002376 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 exit2:
2378 dput(dentry);
2379 }
Jan Blunck4ac91372008-02-14 19:34:32 -08002380 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 if (inode)
2382 iput(inode); /* truncate the inode here */
2383exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002384 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385exit:
2386 putname(name);
2387 return error;
2388
2389slashes:
2390 error = !dentry->d_inode ? -ENOENT :
2391 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2392 goto exit2;
2393}
2394
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002395asmlinkage long sys_unlinkat(int dfd, const char __user *pathname, int flag)
2396{
2397 if ((flag & ~AT_REMOVEDIR) != 0)
2398 return -EINVAL;
2399
2400 if (flag & AT_REMOVEDIR)
2401 return do_rmdir(dfd, pathname);
2402
2403 return do_unlinkat(dfd, pathname);
2404}
2405
2406asmlinkage long sys_unlink(const char __user *pathname)
2407{
2408 return do_unlinkat(AT_FDCWD, pathname);
2409}
2410
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname, int mode)
2412{
2413 int error = may_create(dir, dentry, NULL);
2414
2415 if (error)
2416 return error;
2417
2418 if (!dir->i_op || !dir->i_op->symlink)
2419 return -EPERM;
2420
2421 error = security_inode_symlink(dir, dentry, oldname);
2422 if (error)
2423 return error;
2424
2425 DQUOT_INIT(dir);
2426 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002427 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002428 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 return error;
2430}
2431
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002432asmlinkage long sys_symlinkat(const char __user *oldname,
2433 int newdfd, const char __user *newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434{
2435 int error = 0;
2436 char * from;
2437 char * to;
Dave Hansen6902d922006-09-30 23:29:01 -07002438 struct dentry *dentry;
2439 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440
2441 from = getname(oldname);
2442 if(IS_ERR(from))
2443 return PTR_ERR(from);
2444 to = getname(newname);
2445 error = PTR_ERR(to);
Dave Hansen6902d922006-09-30 23:29:01 -07002446 if (IS_ERR(to))
2447 goto out_putname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
Dave Hansen6902d922006-09-30 23:29:01 -07002449 error = do_path_lookup(newdfd, to, LOOKUP_PARENT, &nd);
2450 if (error)
2451 goto out;
2452 dentry = lookup_create(&nd, 0);
2453 error = PTR_ERR(dentry);
2454 if (IS_ERR(dentry))
2455 goto out_unlock;
2456
Dave Hansen75c3f292008-02-15 14:37:45 -08002457 error = mnt_want_write(nd.path.mnt);
2458 if (error)
2459 goto out_dput;
Jan Blunck4ac91372008-02-14 19:34:32 -08002460 error = vfs_symlink(nd.path.dentry->d_inode, dentry, from, S_IALLUGO);
Dave Hansen75c3f292008-02-15 14:37:45 -08002461 mnt_drop_write(nd.path.mnt);
2462out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002463 dput(dentry);
2464out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002465 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002466 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467out:
Dave Hansen6902d922006-09-30 23:29:01 -07002468 putname(to);
2469out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 putname(from);
2471 return error;
2472}
2473
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002474asmlinkage long sys_symlink(const char __user *oldname, const char __user *newname)
2475{
2476 return sys_symlinkat(oldname, AT_FDCWD, newname);
2477}
2478
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2480{
2481 struct inode *inode = old_dentry->d_inode;
2482 int error;
2483
2484 if (!inode)
2485 return -ENOENT;
2486
2487 error = may_create(dir, new_dentry, NULL);
2488 if (error)
2489 return error;
2490
2491 if (dir->i_sb != inode->i_sb)
2492 return -EXDEV;
2493
2494 /*
2495 * A link to an append-only or immutable file cannot be created.
2496 */
2497 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2498 return -EPERM;
2499 if (!dir->i_op || !dir->i_op->link)
2500 return -EPERM;
2501 if (S_ISDIR(old_dentry->d_inode->i_mode))
2502 return -EPERM;
2503
2504 error = security_inode_link(old_dentry, dir, new_dentry);
2505 if (error)
2506 return error;
2507
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002508 mutex_lock(&old_dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 DQUOT_INIT(dir);
2510 error = dir->i_op->link(old_dentry, dir, new_dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002511 mutex_unlock(&old_dentry->d_inode->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07002512 if (!error)
Jan Karaece95912008-02-06 01:37:13 -08002513 fsnotify_link(dir, old_dentry->d_inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 return error;
2515}
2516
2517/*
2518 * Hardlinks are often used in delicate situations. We avoid
2519 * security-related surprises by not following symlinks on the
2520 * newname. --KAB
2521 *
2522 * We don't follow them on the oldname either to be compatible
2523 * with linux 2.0, and to avoid hard-linking to directories
2524 * and other special files. --ADM
2525 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002526asmlinkage long sys_linkat(int olddfd, const char __user *oldname,
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002527 int newdfd, const char __user *newname,
2528 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529{
2530 struct dentry *new_dentry;
2531 struct nameidata nd, old_nd;
2532 int error;
2533 char * to;
2534
Ulrich Drepper45c9b112006-06-25 05:49:11 -07002535 if ((flags & ~AT_SYMLINK_FOLLOW) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002536 return -EINVAL;
2537
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 to = getname(newname);
2539 if (IS_ERR(to))
2540 return PTR_ERR(to);
2541
Ulrich Drepper45c9b112006-06-25 05:49:11 -07002542 error = __user_walk_fd(olddfd, oldname,
2543 flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0,
2544 &old_nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 if (error)
2546 goto exit;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002547 error = do_path_lookup(newdfd, to, LOOKUP_PARENT, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 if (error)
2549 goto out;
2550 error = -EXDEV;
Jan Blunck4ac91372008-02-14 19:34:32 -08002551 if (old_nd.path.mnt != nd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 goto out_release;
2553 new_dentry = lookup_create(&nd, 0);
2554 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002555 if (IS_ERR(new_dentry))
2556 goto out_unlock;
Dave Hansen75c3f292008-02-15 14:37:45 -08002557 error = mnt_want_write(nd.path.mnt);
2558 if (error)
2559 goto out_dput;
Jan Blunck4ac91372008-02-14 19:34:32 -08002560 error = vfs_link(old_nd.path.dentry, nd.path.dentry->d_inode, new_dentry);
Dave Hansen75c3f292008-02-15 14:37:45 -08002561 mnt_drop_write(nd.path.mnt);
2562out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002563 dput(new_dentry);
2564out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002565 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566out_release:
Jan Blunck1d957f92008-02-14 19:34:35 -08002567 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568out:
Jan Blunck1d957f92008-02-14 19:34:35 -08002569 path_put(&old_nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570exit:
2571 putname(to);
2572
2573 return error;
2574}
2575
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002576asmlinkage long sys_link(const char __user *oldname, const char __user *newname)
2577{
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002578 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002579}
2580
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581/*
2582 * The worst of all namespace operations - renaming directory. "Perverted"
2583 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2584 * Problems:
2585 * a) we can get into loop creation. Check is done in is_subdir().
2586 * b) race potential - two innocent renames can create a loop together.
2587 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002588 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 * story.
2590 * c) we have to lock _three_ objects - parents and victim (if it exists).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002591 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 * whether the target exists). Solution: try to be smart with locking
2593 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002594 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 * move will be locked. Thus we can rank directories by the tree
2596 * (ancestors first) and rank all non-directories after them.
2597 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002598 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 * HOWEVER, it relies on the assumption that any object with ->lookup()
2600 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2601 * we'd better make sure that there's no link(2) for them.
2602 * d) some filesystems don't support opened-but-unlinked directories,
2603 * either because of layout or because they are not ready to deal with
2604 * all cases correctly. The latter will be fixed (taking this sort of
2605 * stuff into VFS), but the former is not going away. Solution: the same
2606 * trick as in rmdir().
2607 * e) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002608 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002610 * ->i_mutex on parents, which works but leads to some truely excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 * locking].
2612 */
Adrian Bunk75c96f82005-05-05 16:16:09 -07002613static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
2614 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615{
2616 int error = 0;
2617 struct inode *target;
2618
2619 /*
2620 * If we are going to change the parent - check write permissions,
2621 * we'll need to flip '..'.
2622 */
2623 if (new_dir != old_dir) {
2624 error = permission(old_dentry->d_inode, MAY_WRITE, NULL);
2625 if (error)
2626 return error;
2627 }
2628
2629 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2630 if (error)
2631 return error;
2632
2633 target = new_dentry->d_inode;
2634 if (target) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002635 mutex_lock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 dentry_unhash(new_dentry);
2637 }
2638 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2639 error = -EBUSY;
2640 else
2641 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2642 if (target) {
2643 if (!error)
2644 target->i_flags |= S_DEAD;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002645 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 if (d_unhashed(new_dentry))
2647 d_rehash(new_dentry);
2648 dput(new_dentry);
2649 }
Stephen Smalleye31e14e2005-09-09 13:01:45 -07002650 if (!error)
Mark Fasheh349457c2006-09-08 14:22:21 -07002651 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
2652 d_move(old_dentry,new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 return error;
2654}
2655
Adrian Bunk75c96f82005-05-05 16:16:09 -07002656static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
2657 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658{
2659 struct inode *target;
2660 int error;
2661
2662 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2663 if (error)
2664 return error;
2665
2666 dget(new_dentry);
2667 target = new_dentry->d_inode;
2668 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002669 mutex_lock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2671 error = -EBUSY;
2672 else
2673 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2674 if (!error) {
Mark Fasheh349457c2006-09-08 14:22:21 -07002675 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 d_move(old_dentry, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 }
2678 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002679 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 dput(new_dentry);
2681 return error;
2682}
2683
2684int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
2685 struct inode *new_dir, struct dentry *new_dentry)
2686{
2687 int error;
2688 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
Robert Love0eeca282005-07-12 17:06:03 -04002689 const char *old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
2691 if (old_dentry->d_inode == new_dentry->d_inode)
2692 return 0;
2693
2694 error = may_delete(old_dir, old_dentry, is_dir);
2695 if (error)
2696 return error;
2697
2698 if (!new_dentry->d_inode)
2699 error = may_create(new_dir, new_dentry, NULL);
2700 else
2701 error = may_delete(new_dir, new_dentry, is_dir);
2702 if (error)
2703 return error;
2704
2705 if (!old_dir->i_op || !old_dir->i_op->rename)
2706 return -EPERM;
2707
2708 DQUOT_INIT(old_dir);
2709 DQUOT_INIT(new_dir);
2710
Robert Love0eeca282005-07-12 17:06:03 -04002711 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
2712
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 if (is_dir)
2714 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
2715 else
2716 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
2717 if (!error) {
Robert Love0eeca282005-07-12 17:06:03 -04002718 const char *new_name = old_dentry->d_name.name;
John McCutchan89204c42005-08-15 12:13:28 -04002719 fsnotify_move(old_dir, new_dir, old_name, new_name, is_dir,
Al Viro5a190ae2007-06-07 12:19:32 -04002720 new_dentry->d_inode, old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 }
Robert Love0eeca282005-07-12 17:06:03 -04002722 fsnotify_oldname_free(old_name);
2723
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 return error;
2725}
2726
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002727static int do_rename(int olddfd, const char *oldname,
2728 int newdfd, const char *newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729{
2730 int error = 0;
2731 struct dentry * old_dir, * new_dir;
2732 struct dentry * old_dentry, *new_dentry;
2733 struct dentry * trap;
2734 struct nameidata oldnd, newnd;
2735
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002736 error = do_path_lookup(olddfd, oldname, LOOKUP_PARENT, &oldnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 if (error)
2738 goto exit;
2739
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002740 error = do_path_lookup(newdfd, newname, LOOKUP_PARENT, &newnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 if (error)
2742 goto exit1;
2743
2744 error = -EXDEV;
Jan Blunck4ac91372008-02-14 19:34:32 -08002745 if (oldnd.path.mnt != newnd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 goto exit2;
2747
Jan Blunck4ac91372008-02-14 19:34:32 -08002748 old_dir = oldnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 error = -EBUSY;
2750 if (oldnd.last_type != LAST_NORM)
2751 goto exit2;
2752
Jan Blunck4ac91372008-02-14 19:34:32 -08002753 new_dir = newnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 if (newnd.last_type != LAST_NORM)
2755 goto exit2;
2756
2757 trap = lock_rename(new_dir, old_dir);
2758
Christoph Hellwig49705b72005-11-08 21:35:06 -08002759 old_dentry = lookup_hash(&oldnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 error = PTR_ERR(old_dentry);
2761 if (IS_ERR(old_dentry))
2762 goto exit3;
2763 /* source must exist */
2764 error = -ENOENT;
2765 if (!old_dentry->d_inode)
2766 goto exit4;
2767 /* unless the source is a directory trailing slashes give -ENOTDIR */
2768 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
2769 error = -ENOTDIR;
2770 if (oldnd.last.name[oldnd.last.len])
2771 goto exit4;
2772 if (newnd.last.name[newnd.last.len])
2773 goto exit4;
2774 }
2775 /* source should not be ancestor of target */
2776 error = -EINVAL;
2777 if (old_dentry == trap)
2778 goto exit4;
Christoph Hellwig49705b72005-11-08 21:35:06 -08002779 new_dentry = lookup_hash(&newnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 error = PTR_ERR(new_dentry);
2781 if (IS_ERR(new_dentry))
2782 goto exit4;
2783 /* target should not be an ancestor of source */
2784 error = -ENOTEMPTY;
2785 if (new_dentry == trap)
2786 goto exit5;
2787
Dave Hansen9079b1e2008-02-15 14:37:49 -08002788 error = mnt_want_write(oldnd.path.mnt);
2789 if (error)
2790 goto exit5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 error = vfs_rename(old_dir->d_inode, old_dentry,
2792 new_dir->d_inode, new_dentry);
Dave Hansen9079b1e2008-02-15 14:37:49 -08002793 mnt_drop_write(oldnd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794exit5:
2795 dput(new_dentry);
2796exit4:
2797 dput(old_dentry);
2798exit3:
2799 unlock_rename(new_dir, old_dir);
2800exit2:
Jan Blunck1d957f92008-02-14 19:34:35 -08002801 path_put(&newnd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002803 path_put(&oldnd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804exit:
2805 return error;
2806}
2807
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002808asmlinkage long sys_renameat(int olddfd, const char __user *oldname,
2809 int newdfd, const char __user *newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810{
2811 int error;
2812 char * from;
2813 char * to;
2814
2815 from = getname(oldname);
2816 if(IS_ERR(from))
2817 return PTR_ERR(from);
2818 to = getname(newname);
2819 error = PTR_ERR(to);
2820 if (!IS_ERR(to)) {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002821 error = do_rename(olddfd, from, newdfd, to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 putname(to);
2823 }
2824 putname(from);
2825 return error;
2826}
2827
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002828asmlinkage long sys_rename(const char __user *oldname, const char __user *newname)
2829{
2830 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
2831}
2832
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
2834{
2835 int len;
2836
2837 len = PTR_ERR(link);
2838 if (IS_ERR(link))
2839 goto out;
2840
2841 len = strlen(link);
2842 if (len > (unsigned) buflen)
2843 len = buflen;
2844 if (copy_to_user(buffer, link, len))
2845 len = -EFAULT;
2846out:
2847 return len;
2848}
2849
2850/*
2851 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
2852 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
2853 * using) it for any given inode is up to filesystem.
2854 */
2855int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2856{
2857 struct nameidata nd;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002858 void *cookie;
Marcin Slusarz694a1762008-06-09 16:40:37 -07002859 int res;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002860
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 nd.depth = 0;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002862 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
Marcin Slusarz694a1762008-06-09 16:40:37 -07002863 if (IS_ERR(cookie))
2864 return PTR_ERR(cookie);
2865
2866 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
2867 if (dentry->d_inode->i_op->put_link)
2868 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
2869 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870}
2871
2872int vfs_follow_link(struct nameidata *nd, const char *link)
2873{
2874 return __vfs_follow_link(nd, link);
2875}
2876
2877/* get the link contents into pagecache */
2878static char *page_getlink(struct dentry * dentry, struct page **ppage)
2879{
2880 struct page * page;
2881 struct address_space *mapping = dentry->d_inode->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -07002882 page = read_mapping_page(mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 if (IS_ERR(page))
Nick Piggin6fe69002007-05-06 14:49:04 -07002884 return (char*)page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 *ppage = page;
2886 return kmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887}
2888
2889int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2890{
2891 struct page *page = NULL;
2892 char *s = page_getlink(dentry, &page);
2893 int res = vfs_readlink(dentry,buffer,buflen,s);
2894 if (page) {
2895 kunmap(page);
2896 page_cache_release(page);
2897 }
2898 return res;
2899}
2900
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002901void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002903 struct page *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 nd_set_link(nd, page_getlink(dentry, &page));
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002905 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906}
2907
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002908void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002910 struct page *page = cookie;
2911
2912 if (page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 kunmap(page);
2914 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 }
2916}
2917
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08002918int __page_symlink(struct inode *inode, const char *symname, int len,
2919 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920{
2921 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08002922 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07002923 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08002924 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 char *kaddr;
2926
NeilBrown7e53cac2006-03-25 03:07:57 -08002927retry:
Nick Pigginafddba42007-10-16 01:25:01 -07002928 err = pagecache_write_begin(NULL, mapping, 0, len-1,
2929 AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07002931 goto fail;
2932
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 kaddr = kmap_atomic(page, KM_USER0);
2934 memcpy(kaddr, symname, len-1);
2935 kunmap_atomic(kaddr, KM_USER0);
Nick Pigginafddba42007-10-16 01:25:01 -07002936
2937 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
2938 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 if (err < 0)
2940 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07002941 if (err < len-1)
2942 goto retry;
2943
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 mark_inode_dirty(inode);
2945 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946fail:
2947 return err;
2948}
2949
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08002950int page_symlink(struct inode *inode, const char *symname, int len)
2951{
2952 return __page_symlink(inode, symname, len,
2953 mapping_gfp_mask(inode->i_mapping));
2954}
2955
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002956const struct inode_operations page_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 .readlink = generic_readlink,
2958 .follow_link = page_follow_link_light,
2959 .put_link = page_put_link,
2960};
2961
2962EXPORT_SYMBOL(__user_walk);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002963EXPORT_SYMBOL(__user_walk_fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964EXPORT_SYMBOL(follow_down);
2965EXPORT_SYMBOL(follow_up);
2966EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
2967EXPORT_SYMBOL(getname);
2968EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969EXPORT_SYMBOL(lookup_one_len);
2970EXPORT_SYMBOL(page_follow_link_light);
2971EXPORT_SYMBOL(page_put_link);
2972EXPORT_SYMBOL(page_readlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08002973EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974EXPORT_SYMBOL(page_symlink);
2975EXPORT_SYMBOL(page_symlink_inode_operations);
2976EXPORT_SYMBOL(path_lookup);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002977EXPORT_SYMBOL(vfs_path_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978EXPORT_SYMBOL(permission);
Christoph Hellwige4543ed2005-11-08 21:35:04 -08002979EXPORT_SYMBOL(vfs_permission);
Christoph Hellwig8c744fb2005-11-08 21:35:04 -08002980EXPORT_SYMBOL(file_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981EXPORT_SYMBOL(unlock_rename);
2982EXPORT_SYMBOL(vfs_create);
2983EXPORT_SYMBOL(vfs_follow_link);
2984EXPORT_SYMBOL(vfs_link);
2985EXPORT_SYMBOL(vfs_mkdir);
2986EXPORT_SYMBOL(vfs_mknod);
2987EXPORT_SYMBOL(generic_permission);
2988EXPORT_SYMBOL(vfs_readlink);
2989EXPORT_SYMBOL(vfs_rename);
2990EXPORT_SYMBOL(vfs_rmdir);
2991EXPORT_SYMBOL(vfs_symlink);
2992EXPORT_SYMBOL(vfs_unlink);
2993EXPORT_SYMBOL(dentry_unhash);
2994EXPORT_SYMBOL(generic_readlink);